Wallace543

Untitled

Jul 25th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. /**
  2. * Given the true wind speed and a race object. Returns the estimated boat speed from the boat
  3. * polar file.
  4. * @param trueWindSpeed Speed of wind relative to the earth.
  5. * @param race Race object that the boat is associated with.
  6. * @return
  7. */
  8. public double getEstimatedBoatSpeed(Double trueWindSpeed, Race race) throws IOException {
  9. Polar closestPolar = getClosestPolar(trueWindSpeed);
  10.  
  11. //Calculate true wind angle from wind direction (apparent wind angle)
  12. Double windDirectionOpposite = (race.getWindDirection() + 180) % 360;
  13. Double trueWindAngle = Math.abs(windDirectionOpposite - this.getHeading());
  14.  
  15. //Generate list of boat speeds from polar file
  16. List<Double> polarBoatSpeeds = listPolarBoatSpeeds(closestPolar);
  17.  
  18. int index = getClosestBoatSpeedIndex(closestPolar, trueWindAngle);
  19.  
  20. return polarBoatSpeeds.get(index);
  21. }
  22.  
  23. /**
  24. * The boat speeds and true wind angles are evenly split into two arraylists.
  25. * This method returns the index value which we use to get the estimated boat speed.
  26. * @param closestPolar Polar object that has the closest TWS value from our given TWS
  27. * @param trueWindAngle previously calculated value from wind direction
  28. * @return integer value
  29. */
  30. private int getClosestBoatSpeedIndex(Polar closestPolar, Double trueWindAngle) {
  31. //Generate list of wind angles from polar file
  32. List<Double> polarWindAngles = listPolarWindAngles(closestPolar);
  33. Double currentMinDif = Math.abs(polarWindAngles.get(0) - trueWindAngle);
  34. int currentIndex = 0;
  35. for (int i = 1; i < 10; i++) {
  36. if (Math.abs(polarWindAngles.get(i) - trueWindAngle) < currentMinDif) {
  37. currentMinDif = Math.abs(polarWindAngles.get(i) - trueWindAngle);
  38. currentIndex = i;
  39. }
  40. }
  41. return currentIndex;
  42. }
  43.  
  44. /**
  45. * Finds the correct polar to be used based on matching the wind speeds
  46. * @param trueWindSpeed Speed of wind relative to the earth.
  47. * @return A polar object that has a wind speed that is the closest match
  48. * to the wind speeds in the polar file.
  49. * @throws IOException
  50. */
  51. private Polar getClosestPolar(Double trueWindSpeed) throws IOException {
  52. //Parse polar file
  53. PolarDataAccessObject polarParser = new AccPolarParser();
  54. List<Polar> allPolars = polarParser.readFile("/accPolarFile.txt");
  55. //Get polar with Tws closest to given True Wind Speed
  56. Polar currentMinPolar = allPolars.get(0);
  57. Double currentMin = Math.abs(trueWindSpeed - allPolars.get(0).getTrueWindSpeed());
  58. for (Polar p : allPolars) {
  59. if (Math.abs(trueWindSpeed - p.getTrueWindSpeed()) < currentMin) {
  60. currentMinPolar = p;
  61. currentMin = Math.abs(trueWindSpeed - p.getTrueWindSpeed());
  62. }
  63. }
  64. return currentMinPolar;
  65. }
  66.  
  67.  
  68. /**
  69. * Sorts the boat speed fields of the given Polar into a list to be used in other methods
  70. * @param p A Polar object
  71. * @return An arraylist of boat speeds
  72. */
  73. private List<Double> listPolarBoatSpeeds(Polar p){
  74. List<Double> polarBoatSpeeds = new ArrayList<>();
  75. polarBoatSpeeds.add(p.getBoatSpeed0());
  76. polarBoatSpeeds.add(p.getBoatSpeed1());
  77. polarBoatSpeeds.add(p.getUpBoatSpeed());
  78. polarBoatSpeeds.add(p.getBoatSpeed2());
  79. polarBoatSpeeds.add(p.getBoatSpeed3());
  80. polarBoatSpeeds.add(p.getBoatSpeed4());
  81. polarBoatSpeeds.add(p.getBoatSpeed5());
  82. polarBoatSpeeds.add(p.getBoatSpeed6());
  83. polarBoatSpeeds.add(p.getDownBoatSpeed());
  84. polarBoatSpeeds.add(p.getBoatSpeed7());
  85. return polarBoatSpeeds;
  86.  
  87.  
  88. }
  89.  
  90. /**
  91. * Sorts the wind angle fields of the given Polar into a list to be used in other methods
  92. * @param p A Polar object
  93. * @return An arraylist of wind angles
  94. */
  95. private List<Double> listPolarWindAngles(Polar p){
  96. List<Double> allIterateableAttributes = new ArrayList<>();
  97. allIterateableAttributes.add(p.getTrueWindAngle0());
  98. allIterateableAttributes.add(p.getTrueWindAngle1());
  99. allIterateableAttributes.add(p.getUpTrueWindAngle());
  100. allIterateableAttributes.add(p.getTrueWindAngle2());
  101. allIterateableAttributes.add(p.getTrueWindAngle3());
  102. allIterateableAttributes.add(p.getTrueWindAngle4());
  103. allIterateableAttributes.add(p.getTrueWindAngle5());
  104. allIterateableAttributes.add(p.getTrueWindAngle6());
  105. allIterateableAttributes.add(p.getDownTrueWindAngle());
  106. allIterateableAttributes.add(p.getTrueWindAngle7());
  107.  
  108. return allIterateableAttributes;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment