TripleAlpha

vis visa attempt 1

Jan 29th, 2020
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.29 KB | None | 0 0
  1. /* Princeton University Explorer Competition
  2. * Team: Sherlock Ohms */
  3.  
  4. import java.util.Scanner;
  5. import java.util.ArrayList;
  6. import java.io.*;
  7.  
  8. public class HorseshoeModelSimulation {
  9. static final double G = 6.67430 * Math.pow(10, -11); //Newton's gravitational constant
  10. static final double M_SOL = 1.989 * Math.pow(10, 30); //Mass of the Sun (Msol) in kg
  11.  
  12. public static void main(String[] args) throws IOException {
  13. Scanner input = new Scanner(new File("CruithneData.txt"));
  14.  
  15. ArrayList<Double> distances = new ArrayList<Double>(); //contains data for simulated distances of horseshoe asteroid to the Sun (in AU)
  16.  
  17. double e, a;
  18. e = input.nextDouble(); //reads in eccentricity of asteroid
  19. a = input.nextDouble(); //reads in semi-major axis of asteroid (in AU)
  20.  
  21. //populates radii ArrayList with data from Ephemeris simulation
  22. while(input.hasNextLine()) {
  23. distances.add(input.nextDouble());
  24. }
  25.  
  26. ArrayList<Double> modeledDistances = checkModel(distances, a, e);
  27.  
  28. double error;
  29. for(int i = 0; i < modeledDistances.size(); i++) {
  30. error = Math.abs(1 - (modeledDistances.get(i) / distances.get(i)));
  31. System.out.printf("%.4f %.4f %.4f%%\n", distances.get(i), modeledDistances.get(i), error * 100);
  32. }
  33. input.close();
  34. }
  35.  
  36. public static ArrayList<Double> checkModel(ArrayList<Double> distances, double a, double e) {
  37. // Formula: r(theta) = [a * (1 - e^2)] / [1 + e * cos(theta)]
  38.  
  39. ArrayList<Double> modeledDistances = new ArrayList<Double>(); //contains model's calculated distance between horseshoe asteroid and the Sun for each day
  40.  
  41. double distance; //current distance between horseshoe asteroid and the Sun (in AU)
  42. double b = a * (Math.sqrt(1 - Math.pow(e, 2))); //semi-minor axis (in AU)
  43. double theta = 0; //true anomaly: angle between current position of the orbiting object and periapsis (which is the first day of data in the text file) (in radians)
  44.  
  45. double distanceSincePeriapsis = 0; //distance traveled along orbit since periapsis (in AU)
  46.  
  47. //use approximation of ellipse circumference
  48. double orbitCircumference = calcCircumference(a, b);
  49.  
  50. double velocity; //current orbital velocity of asteroid
  51.  
  52. for(int i = 0; i < distances.size(); i++) {
  53. //Now, to obtain the anomaly angle (in radians), we take the percentage of orbit completed and multiply it by 2 pi
  54. theta = (distanceSincePeriapsis / orbitCircumference) * (2 * Math.PI);
  55.  
  56. distance = (a * (1 - Math.pow(e, 2))) / (1 + e * Math.cos(theta));
  57.  
  58. modeledDistances.add(distance);
  59.  
  60. //update distance traveled since periapsis using velocity
  61. velocity = calcVelocity(distances.get(i), a);
  62.  
  63. distanceSincePeriapsis += velocity; //Since velocity is in AU/day, adding it to distanceSincePeriapsis updates distance traveled to current day
  64. }
  65.  
  66. return modeledDistances;
  67. }
  68.  
  69. //performs approximation of elliptical circumference given semi-major axis and semi-minor axis; +/- 5% of true circumference
  70. public static double calcCircumference(double a, double b) {
  71. double circumference = Math.PI;
  72.  
  73. circumference *= (a + b);
  74.  
  75. double tempMult = 3;
  76. tempMult *= Math.pow(a - b, 2);
  77. tempMult /= Math.pow(a + b, 2);
  78.  
  79. double tempDivisorPart = Math.pow(a - b, 2) / Math.pow(a + b, 2);
  80. tempDivisorPart *= -3;
  81. tempDivisorPart += 4;
  82.  
  83. tempMult /= (Math.sqrt(tempDivisorPart) + 10);
  84.  
  85. tempMult += 1;
  86.  
  87. circumference *= tempMult;
  88.  
  89. return circumference;
  90. }
  91.  
  92. //uses vis-visa equation to calculate orbital velocity of horseshoe asteroid
  93. public static double calcVelocity(double r, double a) {
  94. double rInMeters = convertAUToMeters(r);
  95. double aInMeters = convertAUToMeters(a);
  96. double velocity = Math.sqrt(G * M_SOL * ((2 / rInMeters) - (1 / aInMeters))); //yields velocity in m/s
  97. velocity = convertToAUPerDay(velocity);
  98. return velocity;
  99. }
  100.  
  101. //given velocity in m/s, converts to AU/day
  102. public static double convertToAUPerDay(double velocity) {
  103. double convertedVelocity = velocity;
  104.  
  105. //convert from m to AU
  106. convertedVelocity /= (1.496 * Math.pow(10, 11));
  107.  
  108. //convert from /sec to /day
  109. convertedVelocity *= 60 * 60 * 24;
  110.  
  111. return convertedVelocity;
  112. }
  113.  
  114. //converts AU to meters
  115. public static double convertAUToMeters(double inAU) {
  116. return (inAU * (1.496 * Math.pow(10, 11)));
  117. }
  118. }
Add Comment
Please, Sign In to add comment