-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNBody.java
More file actions
294 lines (255 loc) · 9.7 KB
/
NBody.java
File metadata and controls
294 lines (255 loc) · 9.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.Scanner;
/**
* This is the driving class for the N body solution for the problem using the Euler computational analysis method.
* not to be confused with the Euler general solution for 3 body problems involving two fixed bodies.
* @author Matthew Williams, Yulia Kosharych
* @version 2018-05-16
**/
public class NBody{
//presets
public static SolarBody body0 = SolarBody.Sol;
// public static SolarBody body1 = SolarBody.Earth;
public static SolarBody body1 = SolarBody.Jupiter;
// public static SolarBody body2 = new SolarBody("satellite", 100, SolarBody.au/7, SolarBody.vJupiter, 0.001, 3*Math.PI/2);
// public static SolarBody body2 = SolarBody.PlanetX;
public static SolarBody body2 = SolarBody.Horseshoe;
// public static SolarBody body2 = SolarBody.Luna;
// public static SolarBody body2 = new SolarBody("Tadpole orbit", 10, dis, BodyMaths.circleVelocityG(body0.mass, dis), 0, -Math.PI/6);
// public static SolarBody body2 = SolarBody.IrregularTadpole;
// public static SolarBody body3 = new SolarBody("probe", -1, -1, SolarBody.vEarth, 0, 0);
public static SolarBody body3 = SolarBody.Jupiter;
// if set to true, it shows the path the body without exerting any gravity on other bodies
public static boolean setPath0 = false;
public static boolean setPath1 = false;
public static boolean setPath2 = false;
public static boolean setPath3 = false;
// useful to compare the affects of gravity on another object
public static final double dt = 1.; //delta t in hours
// public static final double tmax = 200; //number of hours to run
public static final double tmax = 4*BodyMaths.orbitalPeriod(body0.mass, body1.solDis); //number of "years" to run (in hours)
public static final int imax =(int) (tmax/dt); //number of time steps
// number of bodies to use
public static final int bodies = 3;
// gravitational constant
public static final double G = SolarBody.G;
// toggle outputs
public static final boolean graphing = true; //plots locations
public static final boolean print = false; //prints all steps in calculations. not recommended.
public static final boolean verbose = true; //provides details as to what the program is doing
public static final boolean inertialPlot = true;
public static final int inertiaNum = 1; //which body the inertial frame is in reference to
public static void main(String[] args) {
final int maxJavaHeap = 55855815; //max number of points that java can handle
int nPoints=bodies*3*2*imax+bodies*5;
if (nPoints>maxJavaHeap) {
System.out.println("WARNING. Datapoints exceed maximum acceptable number of points within java (trying to use "+nPoints+" with a maximum of "+maxJavaHeap+" points)\nConsider either lowering accuracy or lowering the max time of simulation");
// System.exit(0);
}
// double angle = 0;
// double v = 0.2;
// SolarBody body0 = new SolarBody("body1", Math.pow(G, -1), 5, v, 0, angle);
// angle += 2./3*Math.PI+0.00000001;
// SolarBody body1 = new SolarBody("body2", Math.pow(G, -1), 5, v, 0, angle);
// angle += 2./3*Math.PI;
// SolarBody body2 = new SolarBody("body3", Math.pow(G, -1), 5, v, 0, angle);
// modification tools
// body2.setName("Tadpole");
// body2.setDisAU();
// body2.addDis();
// body2.addVel();
if (setPath0) {
body0.setPath();
}
if (setPath1) {
body1.setPath();
}
if (setPath2) {
body2.setPath();
}
if (setPath3) {
body3.setPath();
}
Scanner kb = new Scanner(System.in);
double[] mass = new double[bodies];
double[][][][] body= new double[bodies][3][2][imax];//[body][pos][x/y][timestep]
String bodyName[] = new String[bodies];
double vInit[] = new double[bodies];
double radius[] = new double[bodies];
double theta[] = new double[bodies];
double distance;
for (int i =0;i<body.length;i++) {
body[i][1]=new double[2][1];
}
// for (;l<10;l+=0.1) {//long for(;;) loop
if (verbose) {
System.out.println("\nstarting input phase");
System.out.print("inputting names...");
}
// names
bodyName[0]=body0.name;
bodyName[1]=body1.name;
if (bodyName.length>2) {
bodyName[2]=body2.name;
}
if (bodyName.length>3) {
bodyName[3]=body3.name;
}
for (int i = 4;i<bodyName.length;i++) {
bodyName[i]="body "+(i+1);
}
if (verbose) {
System.out.print("done\ninputting mass...");
}
// masses
mass[0]=body0.mass;
mass[1]=body1.mass;
if (mass.length>2) {
mass[2]=body2.mass;
}
if (mass.length>3) {
mass[3]=body3.mass;
}
for (int i=4;i<mass.length;i++) {
mass[i] = -1;
}
for (int i=0;i<mass.length;i++) {
if (mass[i]<0) {
System.out.print("\nMass #"+i+":");
mass[i] = kb.nextDouble();
}
}
if (verbose) {
System.out.print("done\ninputting angles...");
}
// thetas
theta[0]=body0.theta;
theta[1]=body1.theta;
if (theta.length>2) {
theta[2]=body2.theta;
}
if (theta.length>3) {
theta[3]=body3.theta;
}
for (int i=4;i<theta.length;i++) {
System.out.print("\nTheta #"+i+":");
theta[i] = kb.nextDouble();
}
while (body0.solDis<0){
System.out.println("distance from centre for "+body0.name);
body0.solDis = kb.nextDouble();
}
while (body1.solDis<0) {
System.out.println("distance from centre for "+body1.name);
body1.solDis = kb.nextDouble();
}
if (verbose) {
System.out.print("done\ninputting initial positions...");
}
// initial position
body[0][0][0][0]=body0.solDis*Math.cos(theta[0]); //body 1 x
body[0][0][1][0]=body0.solDis*Math.sin(theta[0]); //body 1 y
body[1][0][0][0]=body1.solDis*Math.cos(theta[1]); //body 2 x
body[1][0][1][0]=body1.solDis*Math.sin(theta[1]); //body 2 y
if (body.length>2){
while (body2.solDis<0) {
System.out.println("distance from centre for "+body2.name);
body2.solDis = kb.nextDouble();
}
body[2][0][0][0]=body2.solDis*Math.cos(theta[2]);
body[2][0][1][0]=body2.solDis*Math.sin(theta[2]);
}
if (body.length>3){
while (body3.solDis<0) {
System.out.println("distance from centre for "+body3.name);
body3.solDis = kb.nextDouble();
}
body[3][0][0][0]=body3.solDis*Math.cos(theta[3]);
body[3][0][1][0]=body3.solDis*Math.sin(theta[3]);
}
for (int i = 4;i<body.length;i++) {
System.out.println("distance from centre");
distance = kb.nextDouble();
body[i][0][0][0]=distance*Math.cos(theta[i]);
body[i][0][1][0]=distance*Math.sin(theta[i]);
}
if (verbose) {
System.out.print("done\ninputting initial velocities...");
}
body[0][1][0][0]=-body0.vInitial*Math.sin(theta[0]);
body[0][1][1][0]=body0.vInitial*Math.cos(theta[0]);
body[1][1][0][0]=-body1.vInitial*Math.sin(theta[1]);
body[1][1][1][0]=body1.vInitial*Math.cos(theta[1]);
if (body.length>2){
body[2][1][0][0]=-body2.vInitial*Math.sin(theta[2]);
body[2][1][1][0]=body2.vInitial*Math.cos(theta[2]);
}
if (body.length>3){
body[3][1][0][0]=-body3.vInitial*Math.sin(theta[3]);
body[3][1][1][0]=body3.vInitial*Math.cos(theta[3]);
}
for (int i = 4;i<body.length;i++) {
System.out.println("initial velocity");
distance = kb.nextDouble();
body[i][1][0][0]=-distance*Math.sin(theta[i]);
body[i][1][1][0]=distance*Math.cos(theta[i]);
}
vInit = null;
theta = null;
if (verbose) {
System.out.print("done\nFinished input\n");
}
if (verbose) {
System.out.println("\nStarting calculation...");
}
for (int t=1;t<imax;t++) {
for (int num=0;num<bodies;num++) {
for (int j=0;j<bodies;j++) {
if (print){System.out.println("J:"+j+" num:"+num);}
if (j!=num) {
if (print){System.out.println("update accel x");}
body[num][2][0][t]+=BodyMaths.gravityAccel(body[num][0], body[j][0], mass[j], t-1, 0);
if (print){System.out.println("update accel y");}
body[num][2][1][t]+=BodyMaths.gravityAccel(body[num][0], body[j][0], mass[j], t-1, 1);
}
else if (print) {
System.out.println("j=num");
}
}
BodyMaths.updateBody(body[num], dt, t);
if (print) {
System.out.println("body"+ (num+1) +" x,y");
System.out.println(body[num][0][0][t]+","+body[num][0][1][t]);
}
}
}
if (verbose) {
System.out.println("calculation complete");
}
if (graphing) {
if (verbose) {
System.out.println("starting graph");
}
NBodyGraph.graphNBody(bodyName, body, "cartesian plot");
if (verbose) {
System.out.println("graph input complete");
}
}
else if (inertialPlot) {
if (verbose) {
System.out.println("calculating with respect to an inertial frame of reference...");
}
BodyMaths.inertialReference(body, inertiaNum);
if (verbose) {
System.out.println("inertial calculations complete\nstarting inertial graph");
}
NBodyGraph.graphNBody(bodyName, body, "inertial Reference plot");
if (verbose) {
System.out.println("inertial graph input complete");
}
}
// }//long for(;;) loop
}//main()
}//class