-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbody.cpp
executable file
·284 lines (247 loc) · 8.47 KB
/
nbody.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
/*
Taken from:
The Computer Language Benchmarks Game
https://salsa.debian.org/benchmarksgame-team/benchmarksgame/
An implementation pretty much from scratch, with inspiration from the Rust
version, which used the idea of saving some of the ingredients of the
compution in an array instead of recomputing them.
contributed by cvergu
slightly modified by bmmeijers
*/
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
#include <chrono>
using namespace std::chrono;
//reference: https://www.yisu.com/zixun/129174.html
// these values are constant and not allowed to be changed
const double SOLAR_MASS = 4 * M_PI * M_PI;
const double DAYS_PER_YEAR = 365.24;
const unsigned int BODIES_COUNT = 5;
class vector3d {
public:
double x, y, z;
double norm() const noexcept {
return x * x + y * y + z * z;
}
double magnitude(double dt) const noexcept {
double sum = norm();
return dt / (sum * sqrt(sum));
}
};
vector3d operator+(vector3d v1, vector3d v2) {
return vector3d{
v1.x + v2.x, v1.y + v2.y, v1.z + v2.z
};
}
vector3d operator-(vector3d v1, vector3d v2) {
return vector3d{
v1.x - v2.x, v1.y - v2.y, v1.z - v2.z
};
}
vector3d &operator+=(vector3d &v1, vector3d v2) {
v1.x += v2.x;
v1.y += v2.y;
v1.z += v2.z;
return v1;
}
vector3d &operator-=(vector3d &v1, vector3d v2) {
v1.x -= v2.x;
v1.y -= v2.y;
v1.z -= v2.z;
return v1;
}
vector3d &operator*=(vector3d &v, double mag) {
v.x *= mag;
v.y *= mag;
v.z *= mag;
return v;
}
vector3d operator*(vector3d v, double mag) {
return vector3d{
v.x * mag, v.y * mag, v.z * mag
};
}
vector3d operator/(vector3d v, double mag) {
return vector3d{
v.x / mag, v.y / mag, v.z / mag
};
}
class body {
public:
std::string name;
vector3d position;
vector3d velocity;
double mass;
};
void advance(body state[BODIES_COUNT], double dt) {
/*
* We precompute the quantity (r_i - r_j)
*/
// 2D array (to hold: BODIES_COUNT x BODIES_COUNT elements)
vector3d rij[BODIES_COUNT][BODIES_COUNT];
for (unsigned int i = 0; i < BODIES_COUNT; ++i) {
for (unsigned int j = i + 1; j < BODIES_COUNT; ++j) {
rij[i][j] = state[i].position - state[j].position;
}
}
double magnitudes[BODIES_COUNT][BODIES_COUNT];
for (unsigned int i = 0; i < BODIES_COUNT; ++i) {
for (unsigned int j = i + 1; j < BODIES_COUNT; ++j) {
magnitudes[i][j] = rij[i][j].magnitude(dt);
}
}
/*
* Compute the new speed using v_i = a_i dt, where
* a_i = \sum_{j \neq i} m_j (r_i - r_j)/|r_i - r_j|^3
*/
for (unsigned int i = 0; i < BODIES_COUNT; ++i) {
for (unsigned int j = i + 1; j < BODIES_COUNT; ++j) {
vector3d dist = rij[i][j];
double mag = magnitudes[i][j];
state[i].velocity -= dist * (state[j].mass * mag);
state[j].velocity += dist * (state[i].mass * mag);
}
}
/*
* Compute the new positions
*/
for (unsigned int i = 0; i < BODIES_COUNT; ++i) {
state[i].position += state[i].velocity * dt;
}
}
void offset_momentum(body state[BODIES_COUNT]) {
vector3d &sun_velocity = state[0].velocity;
for (unsigned int i = 1; i < BODIES_COUNT; ++i) {
sun_velocity -= state[i].velocity * state[i].mass / SOLAR_MASS;
}
}
double energy(const body state[BODIES_COUNT]) {
double energy = 0;
for (unsigned int i = 0; i < BODIES_COUNT; ++i) {
const body &body1 = state[i];
energy += 0.5 * body1.mass * body1.velocity.norm();
for (unsigned int j = i + 1; j < BODIES_COUNT; ++j) {
const body &body2 = state[j];
vector3d r12 = body1.position - body2.position;
energy -= body1.mass * body2.mass / sqrt(r12.norm());
}
}
return energy;
}
body state[] = {
// Sun
{
.name = "sun",
.position = {
0,
0,
0
},
.velocity = {
0,
0,
0
},
.mass = SOLAR_MASS
},
// Jupiter
{
.name = "jupiter",
.position = {
4.84143144246472090e+00,
-1.16032004402742839e+00,
-1.03622044471123109e-01
},
.velocity = {
1.66007664274403694e-03 * DAYS_PER_YEAR,
7.69901118419740425e-03 * DAYS_PER_YEAR,
-6.90460016972063023e-05 * DAYS_PER_YEAR
},
.mass = 9.54791938424326609e-04 * SOLAR_MASS
},
// Saturn
{
.name = "saturn",
.position = {
8.34336671824457987e+00,
4.12479856412430479e+00,
-4.03523417114321381e-01
},
.velocity = {
-2.76742510726862411e-03 * DAYS_PER_YEAR,
4.99852801234917238e-03 * DAYS_PER_YEAR,
2.30417297573763929e-05 * DAYS_PER_YEAR
},
.mass = 2.85885980666130812e-04 * SOLAR_MASS
},
// Uranus
{
.name = "uranus",
.position = {
1.28943695621391310e+01,
-1.51111514016986312e+01,
-2.23307578892655734e-01
},
.velocity = {
2.96460137564761618e-03 * DAYS_PER_YEAR,
2.37847173959480950e-03 * DAYS_PER_YEAR,
-2.96589568540237556e-05 * DAYS_PER_YEAR
},
.mass = 4.36624404335156298e-05 * SOLAR_MASS
},
// Neptune
{
.name = "neptune",
.position = {
1.53796971148509165e+01,
-2.59193146099879641e+01,
1.79258772950371181e-01
},
.velocity = {
2.68067772490389322e-03 * DAYS_PER_YEAR,
1.62824170038242295e-03 * DAYS_PER_YEAR,
-9.51592254519715870e-05 * DAYS_PER_YEAR
},
.mass = 5.15138902046611451e-05 * SOLAR_MASS
}
};
int main(int argc, char **argv) {
auto start = high_resolution_clock::now();
if (argc == 1) {
std::cout << "This is " << argv[0] << std::endl;
std::cout << "Call this program with two arguments" << std::endl;
std::cout << "(an integer as the number of iterations for the n-body simulation and an string as output filename)." << std::endl;
return EXIT_FAILURE;
} else {
const unsigned int n = atoi(argv[1]);
const unsigned int b = atoi(argv[2]); // add a boolean
offset_momentum(state);
std::cout << energy(state) << std::endl;
if (b == 1) {
std::ofstream outfile;
const std::string filename = "cpp_position.csv";
outfile.open(filename,std::ios::app);
outfile <<"step;name of the body;position x;position y;position z"<<std::endl;
for (int i = 0; i < n; ++i) {
advance(state, 0.01);
for (int j : { 0, 1, 2, 3, 4}){
outfile <<i<<";"<<state[j].name<<";"<<state[j].position.x<<";"<<state[j].position.y<<";"<<state[j].position.z<<std::endl;
}
}
outfile.close();
}
else {
for (int i = 0; i < n; ++i) {
advance(state, 0.01);
}
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<std::chrono::duration<float, std::milli>>(stop - start);
std::cout << "The number of this iteration is " << n << std::endl;
std::cout << "This program spends " << duration.count() << "ms" << std::endl;
std::cout << energy(state) << std::endl;
return EXIT_SUCCESS;
}
}