-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainPanel.java
More file actions
539 lines (453 loc) · 16.5 KB
/
MainPanel.java
File metadata and controls
539 lines (453 loc) · 16.5 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JPanel;
/**
* This class contains all the information about three bodies involved in the 3-body problem necessary to produce a graph
* as well as to calculate minimum and maximum values of certain quantities
*
* @author Matthew Williams, Yulia Kosharych
* @version 17-05-2017
*/
public class MainPanel extends JPanel{
public static Dimension MainPanelDim = Main.frameDim;
//Objects
Object sun;
Object sat;
Object earth;
//Dimensions of the simulation box
public static final double x0 = MainPanelDim.getWidth()/2;
public static final double y0 = MainPanelDim.getHeight()/2;
//Radius of simulation models
final double sunR = 75;
final double satR = 35;
final double earthR = 35;
//Simulation colors
Color sunColor;
Color satColor;
Color earthColor;
double t = 0;
public static final double G = 8.64960768*Math.pow(10, -13);//gravitational constant (km^3/kg/h^2)
public static final double dt = 1.0; //delta t in hours
public static final double tmax = 8760 ; //number of hours to run (1 year)
public static double time = 0; //initial time
public static final boolean graphing = true;
public static final double au = 1.49597870700*Math.pow(10,8);//initial distance of earth from the sun in km
//Masses
public static final double massSun=SolarBody.massSol; //kg
public static final double massEarth= SolarBody.massEarth; //kg
public static final double massJupiter =SolarBody.massJupiter;//kg
public static final double massL5 = massEarth/1000; //kg
public static double massSat; //kg
public static double massPlanet; //mass of the planet
//Velocities
public static final double velocityEarth = BodyMaths.circleVelocityG(massSun,au);// initial velocity of Earth
public static final double velocityJupiter = BodyMaths.circleVelocityG(massSun,5.2*au);//velocity Jupiter
public static double scaling =350;
public static Scanner kb = new Scanner(System.in);
public static double [][][] object = new double[3][3][2];
public static double [] maxAccel, minPosSun, minPosEarth;
double satX, satY, velInitialX, velInitialY;
double planetX, planetY, velInitialPlanetX, velInitialPlanetY;
/**
* This method does the Sun-Earth-Moon simulation
*
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public void initSunEarthLuna(){
scaling = 350;
//Sat Conditions
massSat = SolarBody.massLuna;
satX = au-384400;
satY = 0;
velInitialX = 0;
velInitialY = velocityEarth+BodyMaths.circleVelocityG(massEarth,384402);;
//Earth Condition
massPlanet=massEarth;
planetX =au;
planetY =0;
velInitialPlanetX = 0;
velInitialPlanetY =velocityEarth;
}
/**
* This method does the Sun-Earth-L5 simulation
*
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public void initSunEarthL5(){
scaling = 150;
//Sat Conditions
massSat = massL5;
satX = 0.7*au;
satY = 0.7*au;
velInitialX = 0.7*velocityEarth;
velInitialY = 0.7*velocityEarth;
//Planet Condition
massPlanet=massEarth;
planetX =au;
planetY =0;
velInitialPlanetX = 0;
velInitialPlanetY =velocityEarth;
}
/**
* This method does the Sun-Jupiter-PlanetX simulation
*
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public void initSunJupiterProbe(){
scaling =75;
//Sat Conditions
System.out.println("Please enter the mass of the satellite:");
massSat = kb.nextDouble();
System.out.println("Please enter the x position of the satellite:");
satX = kb.nextDouble();
System.out.println("Please enter the y position of the satellite:");
satY = kb.nextDouble();
System.out.println("Please enter the initial x component of the velocity of satellite");
velInitialX = kb.nextDouble();
System.out.println("Please enter the initial y component of the velocity of satellite");
velInitialY = kb.nextDouble();
//Jupiter Condition
massPlanet=massJupiter;
planetX =5.2*au;
planetY =0;
velInitialPlanetX = 0;
velInitialPlanetY =velocityJupiter;
}
/**
* This method assigns values to vector components of velocity, acceleration and position of each object
*
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public void Algorithm(){
//Earth or any random planet
object[0][0][0] = planetX;
object[0][0][1] = planetY;
object[0][1][0] = velInitialPlanetX;
object[0][1][1] = velInitialPlanetY;
object[0][2][0] = 0;
object[0][2][1] = 0;
//Sun
object[1][0][0] = 0;
object[1][0][1] = 0;
object[1][1][0] = 0;
object[1][1][1] = 0;
object[1][2][0] = 0;
object[1][2][1] = 0;
//Satellite
object[2][0][0] = satX;
object[2][0][1] = satY;
object[2][1][0] = velInitialX;
object[2][1][1] = velInitialY;
object[2][2][0] = 0;
object[2][2][1] = 0;
}
/**
* This is the main method which determines the simulation that will be displayed on the screen
*
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public MainPanel() {
this.setSize(Main.dim2);
int sim;
System.out.println("Please enter the simulation number: ");
sim = kb.nextInt();
//Simulation number
if(sim == 1) initSunEarthLuna();
if (sim == 2) initSunEarthL5();
if (sim == 3) initSunJupiterProbe();
//Algorithm for graphing
Algorithm();
sunColor = new Color(255, 172, 0);
sun = new Object(object[1][0][0] + x0, object[1][0][1] + y0, sunR);
satColor = new Color(211,211,211);
sat = new Object((scaling*object[2][0][0]/au) + x0, (275*object[2][0][1]/au) + y0, satR);
earthColor = new Color(47,94,79);
earth = new Object((scaling*object[0][0][0]/au) + x0, (275*object[0][0][1]/au) + y0, earthR);
maxAccel = object[2][2];
minPosSun = differencePos(object[2][0],object[1][0]);
minPosEarth = differencePos(object[2][0],object[0][0]);
}
/**
* This method updates acceleration, velocity and position of each object under the influence of two other objects
*
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public void update() {
object[0][2] = accelSunAnyObj(massSun,object[0][0],object[1][0]);
object[1][2] = accelSunAnyObj(massPlanet,object[1][0],object[0][0]);
object[2][2] = accelSunAnyObj(massSun,object[2][0], object[1][0]);
object[1][2] = add(object[1][2], accelSunAnyObj(massSat,object[1][0], object[2][0]));
object[0][2] = add(object[0][2], accelSunAnyObj(massSat,object[0][0],object[2][0]));
object[2][2] = add(object[2][2], accelSunAnyObj(massPlanet, object[2][0],object[0][0]));
object[0][1] = add((mult(object[0][2])), object[0][1]);
object[1][1] = add((mult(object[1][2])), object[1][1]);
object[2][1] = add((mult(object[2][2])), object[2][1]);
object[0][0] = add((mult(object[0][1])), object[0][0]);
object[1][0] = add((mult(object[1][1])), object[1][0]);
object[2][0] = add((mult(object[2][1])), object[2][0]);
}
/**
* This method does the graphing of the 3-body system
*
* @return add double
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public void paintChildren(Graphics g)
{
super.paintChildren(g);
//findMaxAccel(); //satellite
maxAccel = compareAccel(maxAccel,object[2][2]);
// findMinDis(); //sun
minPosSun = comparePosition(minPosSun, differencePos(object[2][0],object[1][0]));
// findMinDis(); //earth
minPosEarth = comparePosition(minPosEarth, differencePos(object[2][0],object[0][0]));
//Set the background of the graph
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, MainPanelDim.width, MainPanelDim.height);
//Drawing Sun
sun.updateCelestialObj(object[1][0]);
sun.paintObj(g2, sunColor);
//Drawing Planet
earth.updateCelestialObj(object[0][0]);
earth.paintObj(g2, earthColor);
//Drawing Sat
sat.updateCelestialObj(object[2][0]);
sat.paintObj(g2, satColor);
update();
//Adjust for DELAY
//Display Time
g2.setColor(Color.RED);
time += dt;
//Display number of hours and years of simulation on the graph
g2.setStroke(new BasicStroke(3));
g2.drawString("Number of hours : " + Double.toString(time), 10, 12);
g2.drawString("Number of years : " + Double.toString(time/8760), 10, 29);
//Acceleration Vectors
g2.drawLine((int) ((scaling*object[1][0][0]/au) + x0),(int) ((scaling*object[1][0][1]/au) + y0), (int)(((scaling*object[1][0][0]/au) + x0 ) + (0.7*sunR)*getUnit(object[1][2], getMag(object[1][2]))[0]), (int)(((scaling*object[1][0][1]/au) + y0 ) + (0.7*sunR)*getUnit(object[1][2], getMag(object[1][2]))[1]));
g2.drawLine((int) ((scaling*object[0][0][0]/au) + x0),(int) ((scaling*object[0][0][1]/au) + y0 ), (int)(((scaling*object[0][0][0]/au) + x0 ) + (0.7*earthR)*getUnit(object[0][2], getMag(object[0][2]))[0]), (int)(((scaling*object[0][0][1]/au) + y0 ) + (0.7*earthR)*getUnit(object[0][2], getMag(object[0][2]))[1]));
g2.drawLine((int) ((scaling*object[2][0][0]/au) + x0),(int) ((scaling*object[2][0][1]/au) + y0 ), (int)(((scaling*object[2][0][0]/au) + x0 ) + (0.7*satR)*getUnit(object[2][2], getMag(object[2][2]))[0]), (int)(((scaling*object[2][0][1]/au) + y0 ) + (0.7*satR)*getUnit(object[2][2], getMag(object[2][2]))[1]));
repaint();
//Display maximum acceleration reached by the satellite as well as the minimum distance reached to
//two other objects
if(time==tmax) {
System.out.println("The maximim acceleration is: " +getMag(maxAccel));
System.out.println("The minimum position of the satellite to the planet is: "+getMag(minPosEarth));
System.out.println("The minimum position of the satellite to the Sun is: "+getMag(minPosSun));
System.exit(0);
}
}
/**
* This method takes a double array as an input and multiplies it by the derivative of time
*
* @param array[] double
* @return array[] double
* @author Matthew Williams, Yulia Kosharych
* @version 17-05-2018
*/
public static double [] mult(double [] array) {
for(int i =0; i<array.length;i++) {
array[i] = array[i]*dt;
}
return array;
}
/**
* This method takes two double arrays as input and adds them in a new array
*
* @param array double[]
* @return add double
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double [] add(double [] array1, double [] array2) {
double [] add = new double [array1.length];
for(int i = 0; i < array1.length; i++)
{
add[i] = array1[i] + array2[i];
}
return add;
}
/**
* This method takes two double arrays as input representing the position of Sun/Earth and
* Satellite and subtracts the position of Sun/Earth from that of Satellite
*
* @param pos1 double[]
* @param pos2 double[]
* @return result
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double [] differencePos(double[] pos1, double [] pos2){
double [] result = new double[2];
for(int i =0; i<pos1.length;i++) {
result[i] = pos1[i]-pos2[i];
}
return result;
}
/**
* This method takes a double array as an input and multiplies is by the derivative of time
*
* @param objPos1 double[]
* @param objPos2 double[]
* @return array
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double [] getR(double [] objPos1, double [] objPos2) {
double [] rVector = new double [2];
rVector[0]= objPos2[0] - objPos1[0];
rVector[1]= objPos2[1] - objPos1[1];
return rVector;
}
/**
* This method takes a double array as input and calculates the magnitude
* two array elements
*
* @param rVector double[]
* @return rMagnitude
* @author Matthew Williams, Yulia Kosharych
* @version 17-05-2018
*/
public static double getMag(double [] rVector) {
double rMagnitude;
rMagnitude = Math.sqrt(Math.pow(rVector[0],2) + Math.pow(rVector[1], 2));
return rMagnitude;
}
/**
* This method calculates the unit vector which determines the direction of the acceleration vector
*
* @param rVector double[] this array contains the x and y components of the vector
* @param rMagnitude double this is the magnitude of the vector
* @return array
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double [] getUnit(double [] rVector, double rMagnitude ) {
double [] unit = new double[2];
unit[0] = rVector[0]/rMagnitude;
unit[1] = rVector[1]/rMagnitude;
return unit;
}
/**
* This method calculates the x and y components of the instantaneous acceleration vector of any object of mass m
* under influence of another object
*
* @param mPullObj double this is the mass of a object given
* @param objPos1 double[] this is position vector of the first object
* @param objPos2 double[] this is position vector of the second object
* @return accelVector double[]
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double [] accelSunAnyObj( double mPullObj,double [] objPos1, double [] objPos2) {
double rMagnitude;
double [] rVector = new double [2];
double [] unit = new double [2];
double [] accelVector = new double [2];
double accelMagnitude;
rVector = getR(objPos1,objPos2); //vector components of pos
rMagnitude = getMag(rVector); //magnitude of accel
unit = getUnit(rVector,rMagnitude); //direction of accel
accelMagnitude= (G*mPullObj)/ Math.pow(rMagnitude, 2);
accelVector[0] = accelMagnitude * unit[0]; //x component of accel
accelVector[1] = accelMagnitude * unit[1]; //y component of accel
return accelVector;
}
/**
* This method updates instantaneous velocity of an object
*
* @param velInitial double[] this is the initial velocity vector of an object
* @param accel double[] this is the acceleration of the object
* @return velInitial double[] this is the final velocity vector of an object for each dt
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double [] updateVel(double [] velInitial, double [] accel) {
velInitial[0] = accel[0] *dt + velInitial[0];
velInitial[1] = accel[1] *dt + velInitial[1];
return velInitial;
}
/**
* This method updates instantaneous position of an object
*
* @param posInitial double[] this is the initial position of an object
* @param vel double[] this is the velocity of an object
* @return posInitial this is the final position vector of an object for each dt
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double [] updatePos(double [] posInitial, double [] vel) {
posInitial[0] = vel[0] * dt + posInitial[0];
posInitial[1] = vel[1] * dt + posInitial[1];
return posInitial;
}
/**
* This method compares instantaneous accelerations of an object and returns the greatest acceleration reached
*
* @param accelInitial double[]
* @param accelFinal double[] accelFinal
* @return accelFinal if following accel is greater that the previous one
* @return accelInitial if the previous accel is greater than the following one
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double [] compareAccel(double [] accelInitial, double [] accelFinal) {
double accelMagIn = getMag(accelInitial);
double accelMagFin = getMag(accelFinal);
if (accelMagIn < accelMagFin) {
return accelFinal;
}else {
return accelInitial;
}
}
/**
* This method compares instantaneous positions of an object and returns the smallest position the object reaches
* to another object
* @param posInitial double[]
* @param posFinal double[]
* @return posFinal if the following position is closer to the object than the previous one
* @return posInitial if the following position is further to the object than the previous one
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double[] comparePosition(double[] posInitial, double[] posFinal) {
double magPosIn = getMag(posInitial);
double magPosFin = getMag(posFinal);
if (magPosIn > magPosFin ) {
return posFinal;
}else {
return posInitial;
}
}
/**
* This method compares instantaneous velocities of an object and returns the smallest one
* @param velInitial double[]
* @param velFinal double[]
* @return velFinal if the following velocity is greater than the previous one
* @return velInitial if the following velocity is smaller than the previous one
* @author Matthew Williams,Yulia Kosharych
* @version 17-05-2018
*/
public static double compareVelocity(double[] velInitial, double[] velFinal) {
double magVelIn = getMag(velInitial);
double magVelFin = getMag(velFinal);
if (magVelIn < magVelFin ) {
return magVelFin;
}else {
return magVelIn;
}
}
}//end MainPanel class