-
Notifications
You must be signed in to change notification settings - Fork 0
/
orbit-kutta.cpp
391 lines (325 loc) · 11.7 KB
/
orbit-kutta.cpp
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
/*
TODO:
Add moons and other planets for more accuracy
Add option for relativistic model?
Figure out way to graph
>super trails and multiple integrators simulatanuously for visualization
>trace EXACT solution
>add zoom feature
>display all metrics in units
>mouse functionality
>add menus for RK4 and other features like white background and trace
>fix year
*/
#include <iostream>
#include <cmath>
#include <SFML/Graphics.hpp>
#include <string>
using namespace std;
//config data
string runge = "rk1";
bool trace = false;
bool path = true;
//one pixel = 300k km
//one time unit = 1 s
const int WinX = 2*1920;
const int WinY = 2*1080;
const double SunX = WinX/2;
const double SunY = WinY/2;
//Gravitaional Constant in pix^3 kg^-1 s^-2
const double G = 2.497413*pow(10,-36);
//Mass of Sun in kg
const double Ms = 1.989*pow(10,30);
const double Me = 5.972*pow(10,24);
const double Mm = 7.348*pow(10,22);
const double scale = 300000;
//Initial Position of Earh in pix (x, 0)
//const double Xi = 147099761/scale;
//Initial Velocity of Earh in pix/s
//const double Vi = pow(G*Ms*(2/Xi - 1/((1.496/3)*pow(10,3))),.5);
const double Xi = 152041481.3/scale;
const double Yi = 0/scale;
const double Vxi = -0/scale;
const double Vyi = -29.29445088/scale;
//Initial Position of Moon in pix (x, 0)
const double mXi = 360708.9606/scale;
const double mYi = -0/scale;
const double mVxi = 0/scale;
const double mVyi = 1.090492451/scale;
//Initial Velocity of Moon in pix/s
//const double mVi = pow(G*Ms*(2/Xi - 1/((1.496/3)*pow(10,3))),.5);
const double stepSpeed = 1;
//Step size in seconds
const double dt = 86400/stepSpeed;
//framerate
const int framerate = 30*stepSpeed;
const double sixth = .1666666666666666666666666666666666667;
//defines parameters of planet
class Planet {
public:
double posX,posY,velX,velY,r,a,ax,ay;
std::vector<sf::Vertex> trail;
double uposX = 0;
double uposY = 0;
void euler_step(double SunX, double SunY,double Ms);
void runge_step(double SunX, double SunY);
void rk2_step(double SunX, double SunY);
void verlet_step(double SunX, double SunY);
void reuler_step(double SunX, double SunY);
void update();
};
void Planet::update(){
posX += uposX;
posY += uposY;
uposX = 0;
uposY = 0;
}
//defines Euler Method to update position
void Planet::euler_step(double X, double Y,double M){
r = pow(pow((posX-X),2) + pow((posY-Y),2),.5);
a = (G*M)/(r*r);
ax = a * (X - posX)/r;
ay = a * (Y - posY)/r;
velX += (ax*dt);
velY += (ay*dt);
//cout << "vel " <<velX << " " << velY <<endl;
uposX += (velX*dt);
uposY += (velY*dt);
//cout << posX << " " << posY <<endl;
}
//vectorize params
vector<double> f1(vector<double> pos,vector<double> vel,vector<double> plan){
return vel;
}
vector<double> f2(vector<double> pos,vector<double> vel,vector<double> plan){
vector<double> vprime;
vprime.push_back(-1*G*Ms*(pos[0]-plan[0])*pow((pow((pos[0]-plan[0]),2) + pow((pos[1]-plan[1]),2)),-1.5));
vprime.push_back(-1*G*Ms*(pos[1]-plan[1])*pow((pow((pos[0]-plan[0]),2) + pow((pos[1]-plan[1]),2)),-1.5));
return vprime;
}
vector<double> va(vector<double> a, vector<double> b){
vector<double> z;
z.push_back(a[0]+b[0]);z.push_back(a[1]+b[1]);
return z;
}
vector<double> va3(vector<double> a, vector<double> b,vector<double> c){
vector<double> z;
z.push_back(a[0]+b[0]+c[0]);z.push_back(a[1]+b[1]+c[1]);
return z;
}
vector<double> va4(vector<double> a, vector<double> b,vector<double> c, vector<double> d){
vector<double> z;
z.push_back(a[0]+b[0]+c[0]+d[0]);z.push_back(a[1]+b[1]+c[1]+d[1]);
return z;
}
vector<double> vm(vector<double> a, double b){
vector<double> z;
z.push_back(a[0]*b);z.push_back(a[1]*b);
return z;
}
void Planet::rk2_step(double SunX, double SunY){
r = pow(pow((posX-SunX),2) + pow((posY-SunY),2),.5);
vector<double> pos; pos.push_back(posX); pos.push_back(posY); //pos x1
vector<double> vel; vel.push_back(velX); vel.push_back(velY); //vel x2
vector<double> plan; plan.push_back(SunX); plan.push_back(SunY);
vector<double> k11,k21,k12,k22;
k11 = vm(f1(pos,vel,plan),dt);
k21 = vm(f2(pos,vel,plan),dt);
k12 = vm(f1(va(pos,k11),va(vel,k21),plan),dt);
k22 = vm(f2(va(pos,k11),va(vel,k21),plan),dt);
pos=va(pos,vm(va(k11,k12),.5));
vel=va(vel,vm(va(k21,k22),.5));
posX = pos[0];posY = pos[1];
velX = vel[0];velY = vel[1];
}
void Planet::runge_step(double SunX, double SunY){
r = pow(pow((posX-SunX),2) + pow((posY-SunY),2),.5);
vector<double> pos; pos.push_back(posX); pos.push_back(posY); //pos x1
vector<double> vel; vel.push_back(velX); vel.push_back(velY); //vel x2
vector<double> plan; plan.push_back(SunX); plan.push_back(SunY);
vector<double> k11,k21,k12,k22,k13,k23,k14,k24;
k11 = vm(f1(pos,vel,plan),dt);
k21 = vm(f2(pos,vel,plan),dt);
k12 = vm(f1(va(pos,vm(k11,.5)),va(vel,vm(k21,.5)),plan),dt);
k22 = vm(f2(va(pos,vm(k11,.5)),va(vel,vm(k21,.5)),plan),dt);
k13 = vm(f1(va(pos,vm(k12,.5)),va(vel,vm(k22,.5)),plan),dt);
k23 = vm(f2(va(pos,vm(k12,.5)),va(vel,vm(k22,.5)),plan),dt);
k14 = vm(f1(va(pos,k13),va(vel,k23),plan),dt);
k24 = vm(f2(va(pos,k13),va(vel,k23),plan),dt);
pos=va(pos,vm(va4(k11,vm(k12,2),vm(k13,2),k14),sixth));
vel=va(vel,vm(va4(k21,vm(k22,2),vm(k23,2),k24),sixth));
posX = pos[0];posY = pos[1];
velX = vel[0];velY = vel[1];
//cout << posX << " " << posY <<endl;
}
void Planet::verlet_step(double SunX, double SunY){
r = pow(pow((posX-SunX),2) + pow((posY-SunY),2),.5);
vector<double> pos; pos.push_back(posX); pos.push_back(posY); //pos x1
vector<double> pos_new;
vector<double> vel; vel.push_back(velX); vel.push_back(velY); //vel x2
vector<double> plan; plan.push_back(SunX); plan.push_back(SunY);
pos_new = va3(pos,vm(vel,dt),vm(f2(pos,vel,plan),.5*dt*dt));
vel = va(vel,vm(va(f2(pos,vel,plan),f2(pos_new,vel,plan)),.5*dt));
posX = pos_new[0]; posY = pos_new[1];
velX = vel[0]; velY = vel[1];
}
void Planet::reuler_step(double SunX, double SunY){
r = pow(pow((posX-SunX),2) + pow((posY-SunY),2),.5);
vector<double> pos; pos.push_back(posX); pos.push_back(posY); //pos x1
vector<double> vel; vel.push_back(velX); vel.push_back(velY); //vel x2
vector<double> plan; plan.push_back(SunX); plan.push_back(SunY);
vel = va(vel,vm(f2(pos,vel,plan),dt));
pos = va(pos,vm(vel,dt));
posX = pos[0]; posY = pos[1];
velX = vel[0]; velY = vel[1];
}
//assuming Msun >> Msat
int main(){
freopen("output.csv", "w", stdout);
//define Earth
Planet E;
E.posX = WinX/2 + Xi;
E.posY = WinY/2 + Yi;
E.velX = Vxi;
E.velY = Vyi;
Planet M;
M.posX = WinX/2 + Xi + mXi;
M.posY = WinY/2 + Yi + mYi;
M.velX = Vxi + mVxi;
//M.velY = Vi + mVi;
M.velY = Vyi + mVyi;
M.velY = Vyi;
//SFML Window
// create the window
sf::RenderWindow window(sf::VideoMode(WinX, WinY), "EEgrav");
window.setFramerateLimit(framerate);
// run the program as long as the window is open
int time = 0;
int rad = 50;
//define sun visual
sf::CircleShape Sun(2*rad);
Sun.setFillColor(sf::Color(252, 212, 64));
Sun.setPosition(sf::Vector2f(SunX-2*rad,SunY-2*rad));
//define earth visual
sf::CircleShape Earth(rad/5);
Earth.setFillColor(sf::Color(0, 119, 190));
Earth.setPosition(sf::Vector2f(SunX + Xi,SunY + Yi));
//define moon Visual
sf::CircleShape Moon(rad/8);
Moon.setFillColor(sf::Color(254, 252, 252));
Moon.setPosition(sf::Vector2f(SunX + Xi + mXi,SunY + Yi + mYi));
//display text
sf::Font font;
//initialize distance text
sf::Text text_distance;
text_distance.setFont(font);
text_distance.setString("Distance: " + to_string(E.r));
text_distance.setCharacterSize(64);
text_distance.setFillColor(sf::Color::White);
text_distance.setStyle(sf::Text::Bold);
//initialize perihelion text
sf::Text text_perihelion;
text_perihelion.setFont(font);
text_perihelion.setString("Perihelion: " + to_string(E.r));
text_perihelion.setCharacterSize(64);
text_perihelion.setFillColor(sf::Color::White);
text_perihelion.setStyle(sf::Text::Bold);
text_perihelion.setPosition(sf::Vector2f(0,80));
//initialize aphelion text
sf::Text text_aphelion;
text_aphelion.setFont(font);
text_aphelion.setString("Aphelion: " + to_string(E.r));
text_aphelion.setCharacterSize(64);
text_aphelion.setFillColor(sf::Color::White);
text_aphelion.setStyle(sf::Text::Bold);
text_aphelion.setPosition(sf::Vector2f(0,160));
//initialize year count text
sf::Text text_year;
text_year.setFont(font);
text_year.setString("Aphelion: " + to_string(E.r));
text_year.setCharacterSize(64);
text_year.setFillColor(sf::Color::White);
text_year.setStyle(sf::Text::Bold);
text_year.setPosition(sf::Vector2f(0,240));
//initialize energy text
sf::Text text_energy;
text_energy.setFont(font);
text_energy.setString("Energy " + to_string(0));
text_energy.setCharacterSize(64);
text_energy.setFillColor(sf::Color::White);
text_energy.setStyle(sf::Text::Bold);
text_energy.setPosition(sf::Vector2f(0,320));
cout << "time,time(sol days),Distance from Sun (km), Distance from Sun (AU),max/min"<<endl;
double aphelion = 0;
double perihelion = pow(10,100);
if (!font.loadFromFile("arial.ttf"))
{
cout << "font loading error";
}
while (window.isOpen())
{
if(!trace){window.clear(sf::Color::Black);}
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//cout << E.posX << " "<< E.posY <<endl;
//step code
//if(runge == "rk1"){E.euler_step(SunX,SunY,Ms);M.euler_step(SunX,SunY,Ms);E.euler_step(M.posX,M.posY,Mm);M.euler_step(E.posX,E.posY,Me);}
if(runge == "rk1"){E.euler_step(SunX,SunY,Ms);M.euler_step(SunX,SunY,Ms);M.euler_step(E.posX,E.posY,Me);}
if(runge == "rk2"){E.rk2_step(SunX,SunY);}
if(runge == "rk4"){E.runge_step(SunX,SunY);}
if(runge == "reuler"){E.reuler_step(SunX,SunY);}
if(runge == "verlet"){E.verlet_step(SunX,SunY);}
E.update();
M.update();
//output intit
cout << time << "," << time*dt/86400 << "," << E.r * scale <<"," << E.r * scale/149598000<< ",";
time++;
//energy
double energy = .5*(pow(E.velX,2)+pow(E.velY,2))*Me - G*Ms*Me/(E.r*E.r);
//trail code
if(path){
sf::Vertex vertex;
//changed pix to pos
vertex.position = sf::Vector2f(E.posX, E.posY);
vertex.color = sf::Color::Red;
E.trail.push_back(vertex);
}
//change
if (abs(E.posY - SunY)<1){
text_year.setString("Year Length: "+ to_string(time*dt/31536000));
}
Earth.setPosition(sf::Vector2f(E.posX,E.posY));
Moon.setPosition(sf::Vector2f(M.posX,M.posY));
//get aphelion and perihelion
if(E.r > aphelion){
aphelion = E.r;
cout << "aphelion,";
cout << energy;
}
if(E.r < perihelion){
perihelion = E.r;
cout << "perihelion";
}
//text_year.setString("Year Length: "+ to_string(time*dt/31536000));
text_distance.setString("Distance: " + to_string(E.r * scale) + " km (" + to_string(E.r * scale/149598000) + " AU)");
text_aphelion.setString("Aphelion: " + to_string(aphelion * scale) + " km ("+ to_string(aphelion * scale/149598000) + " AU)");
text_perihelion.setString("Perihelion: " + to_string(perihelion * scale) + " km (" + to_string(perihelion * scale/149598000) + " AU)");
text_energy.setString("Energy: " + to_string(energy));
if(path){window.draw(&E.trail[0], E.trail.size(), sf::Lines);}
window.draw(Sun);
window.draw(Earth);
window.draw(Moon);
window.draw(text_distance);
window.draw(text_aphelion);
window.draw(text_perihelion);
window.draw(text_year);
window.draw(text_energy);
cout << endl;
window.display();
}
return 0;
}