-
Notifications
You must be signed in to change notification settings - Fork 0
/
Omega.cpp
71 lines (53 loc) · 1.26 KB
/
Omega.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
#include <iostream>
#include "Point.h"
#include "Omega.h"
Omega::Omega(bool verbose, bool save): verbose(verbose), save(save) {
if(save)
handle.open("out.csv");
}
Omega::~Omega() {
if(save)
handle.close();
}
void Omega::print() {
std::cout << "[ ";
for (auto body : omega)
std::cout << "[\"" << body->r.x << "\", \"" << body->r.y << "\", \"" << body->r.z << "\"], ";
std::cout << " ], " << std::endl;
}
void Omega::write() {
for (auto body : omega)
handle << body->r.x << ", " << body->r.y << ", " << body->r.z << ", ";
handle << "\n";
}
void Omega::run(double dt, double t_0, double t_end) {
if(save) {
int n = omega.size();
for(int i = 0; i < n; ++i)
handle << "b" << i << "_rx, b" << i << "_ry, b" << i << "_rz, ";
handle << "\n";
}
for(double t = t_0; t <= t_end; t += dt) {
update_a();
update_rv(dt);
if(verbose)
print();
if(save)
write();
}
}
void Omega::update_a() {
for (auto body1 : omega) {
Point a = Point(0.0, 0.0, 0.0);
for (auto body2 : omega)
if (body1 != body2)
a += G * (body2->m / body1->getDistanceTo(body2, true)) * (body2->r - body1->r);
body1->setAcceleration(a);
}
}
void Omega::update_rv(double dt) {
for (auto body : omega) {
body->v += body->a * dt;
body->r += body->v * dt;
}
}