-
Notifications
You must be signed in to change notification settings - Fork 1
/
orbit.cpp
161 lines (133 loc) · 3.92 KB
/
orbit.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
#include <cmath>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
class Planet
{
public:
double mass;
double coord[2];
double velocity[2];
};
class SolarSystem
{
public:
int n;
Planet* p;
SolarSystem(char *filename)
{
string line;
ifstream file(filename);
getline(file, line);
stringstream(line) >> n; // load number of planets
p = new Planet[n];
for(int i = 0; i < n; i++)
{
getline(file, line);
stringstream(line) >> p[i].mass;
getline(file, line);
stringstream ss(line);
ss >> p[i].coord[0];
ss >> p[i].coord[1];
ss >> p[i].velocity[0];
ss >> p[i].velocity[1];
}
}
SolarSystem(int nplanets)
{
n = nplanets;
p = new Planet[nplanets];
}
};
void update_coord(double h, SolarSystem s)
{
for(int i = 0; i < s.n; i++) // for all planets
for(int a = 0; a < 2; a++) // for both x and y coordinates
s.p[i].coord[a] += h * s.p[i].velocity[a];
}
void update_velocity(double h, SolarSystem s, double g_newton)
{
for(int i = 0; i < s.n; i++) // for all planets
for(int j = 0; j < s.n; j++) // for all other planets
if(j != i)
{
double r_ij[2], r_ij2 = 0;
for(int a = 0; a < 2; a++)
{
r_ij[a] = s.p[i].coord[a] - s.p[j].coord[a];
r_ij2 += r_ij[a] * r_ij[a];
}
double r_ij3 = pow(r_ij2, 1.5);
for(int a = 0; a < 2; a++)
s.p[i].velocity[a] -= h * g_newton * s.p[j].mass * r_ij[a] / r_ij3;
}
}
void update(double h, SolarSystem s, double g_newton)
{
update_coord(0.5 * h, s);
update_velocity(h, s, g_newton);
update_coord(0.5 * h, s);
}
double energy(SolarSystem &s, double g_newton)
{
double r_ij[s.n], r_ij2 = 0;
double T = 0, V = 0;
for(int i = 0; i < s.n; i++)
{
T += 0.5 * s.p[i].mass
* (s.p[i].velocity[0] * s.p[i].velocity[0]
+ s.p[i].velocity[1] * s.p[i].velocity[1]);
for(int j = i; j < s.n; j++)
if(i != j) // I bet this could be better
{
for(int a = 0; a < s.n; a++)
{
r_ij[a] = s.p[i].coord[a] - s.p[j].coord[a];
r_ij2 += r_ij[a] * r_ij[a];
}
double r_ij3 = pow(r_ij2, 0.5);
V -= g_newton * s.p[i].mass * s.p[j].mass / r_ij3;
}
}
return T + V;
}
double angularmomentum(SolarSystem &s)
{
double L = 0, origin[2] = {0, 0};
for(int i = 0; i < s.n; i++)
L += s.p[i].mass * ((s.p[i].coord[0] - origin[0]) * s.p[i].velocity[1]
- (s.p[i].coord[1] - origin[1]) * s.p[i].velocity[0]);
return L;
}
void gallileo(SolarSystem s, double origin[2])
{
// This boosts us into the inertial frame of the sun
cout << origin[0] << "\t" << origin[1] << flush;
for(int i = 1; i < s.n; i++)
{
cout << "\t" << s.p[i].coord[0] - s.p[0].coord[0] + origin[0] << "\t"
<< s.p[i].coord[1] - s.p[0].coord[1] + origin[1] << flush;
}
cout << endl;
}
int main(int argc, char *argv[])
{
if(argc < 2)
{
cout << "Usage: " << argv[0] << " filename" << endl;
return 0;
}
const double g_newton = 10.0;
double h = 0.001, origin[2] = {0, 0};
SolarSystem s(argv[1]);
for(int a = 0; a < 100000; a++)
{
update(h, s, g_newton);
//cout << a << "\t" << energy(s, g_newton)
// << "\t" << angularmomentum(s) << endl;
gallileo(s, origin);
}
return 0;
}