Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. public class NBody {
  9.  
  10. public static void main(String[] args){
  11. double totalTime = 157788000.0;
  12. double dt = 25000.0;
  13. String pfile = "data/planets.txt"; // ./data/planets.txt
  14. if (args.length > 2) {
  15. totalTime = Double.parseDouble(args[0]);
  16. dt = Double.parseDouble(args[1]);
  17. pfile = args[2];
  18. }
  19.  
  20. String fname = pfile;
  21. Planet[] planets = readPlanets(fname);
  22. double radius = readRadius(fname);
  23.  
  24. System.out.printf("%d\n", planets.length);
  25. System.out.printf("%.2e\n", radius);
  26. for (int i = 0; i < planets.length; i++) {
  27. System.out.printf("%11.4e %11.4e %11.4e %11.4e %11.4e %12s\n",
  28. planets[i].myXPos, planets[i].myYPos,
  29. planets[i].myXVel, planets[i].myYVel,
  30. planets[i].myMass, planets[i].myFileName);
  31. }
  32.  
  33. StdDraw.setScale(-radius, radius);
  34. StdDraw.picture(0,0,"images/starfield.jpg");
  35.  
  36. for(double t = 0.0; t < totalTime; t += dt) {
  37. double[] xForces = new double[planets.length];
  38. double[] yForces = new double[planets.length];
  39.  
  40. for (int i = 0; i < planets.length; i++) {
  41. xForces[i] = planets[i].calcNetForceExertedByX(planets);
  42. yForces[i] = planets[i].calcNetForceExertedByY(planets);
  43. }
  44.  
  45. for (int i = 0; i < planets.length ; i++) {
  46. planets[i].update(dt, xForces[i], yForces[i]);
  47. }
  48.  
  49. StdDraw.picture(0,0,"images/starfield.jpg");
  50. Arrays.stream(planets).forEach(p -> p.draw());
  51. StdDraw.show(10);
  52. }
  53.  
  54. Arrays.stream(planets).forEach(p -> p.draw());
  55. }
  56.  
  57. public static double readRadius(String fname) {
  58. try {
  59. Scanner scanner = new Scanner(new File(fname));
  60. scanner.next();
  61. double radius = scanner.nextDouble();
  62. scanner.close();
  63. return radius;
  64. } catch (FileNotFoundException e) {
  65. e.printStackTrace();
  66. }
  67. return -1.0;
  68. }
  69.  
  70. public static Planet[] readPlanets(String fname) {
  71. Planet[] planets = new Planet[5];
  72. try {
  73. Scanner scanner = new Scanner(new File(fname));
  74. scanner.next();
  75. scanner.next();
  76. for (int i = 0; i < planets.length; i++) {
  77. planets[i] = new Planet(scanner.nextDouble(),
  78. scanner.nextDouble(),
  79. scanner.nextDouble(),
  80. scanner.nextDouble(),
  81. scanner.nextDouble(),
  82. scanner.next());
  83. }
  84. scanner.close();
  85. } catch (FileNotFoundException ex) {
  86. Logger.getLogger(NBody.class.getName()).log(Level.SEVERE, null, ex);
  87. }
  88. return planets;
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement