-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolar_System.cpp
More file actions
121 lines (95 loc) · 2.93 KB
/
Solar_System.cpp
File metadata and controls
121 lines (95 loc) · 2.93 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
#include"main.h"
#include"matplotlibcpp.h"
#include"Vec2D.h"
#include"Planet.h"
#include"Solar_System.h"
using namespace std;
namespace plt = matplotlibcpp;
solar_system::solar_system(double d_t){
time_step = d_t;
current_time = 0;
vector <planet> planets;
}
solar_system::~solar_system(){
}
void solar_system::draw(){
plt::figure_size(800, 800);
for(int i=0; i < planets.size(); i++ ){
ifstream iplik("data/" + planets[i].get_name() + ".txt", std::ios_base::app);
vector < double > T;
vector < double > X;
vector < double > Y;
while(true){
double tt,x,y;
iplik >> tt >> x >> y;
if(iplik.fail()) break;
x = x/149597871000.0;
y = y/149597871000.0;
// cout << x << y << endl;
X.push_back(x);
Y.push_back(y);
}
iplik.close();
vector < double > x_last={X.back()};
vector < double > y_last={Y.back()};
plt::scatter(x_last,y_last,20); // plot latest postion as a point
plt::plot(X,Y); // plot all previous positions as line
}
vector < double > sx;
vector < double > sy;
sx.push_back(0);
sy.push_back(0);
plt::scatter(sx,sy,5); // plot position of the Sun as a point
plt::title("Dzien "+to_string(int(current_time))); //Current time in days
plt::xlabel("x [AU]");
plt::ylabel("y [AU]");
plt::xlim(-40,40);
plt::ylim(-40,40);
plt::save("plots/t="+to_string(int(current_time))+".png");
plt::close();
}
void solar_system::to_file(){
for(int i = 0; i < planets.size(); i++){
string name = planets[i].get_name();
std::ofstream outfile;
if(this->current_time == 0){
outfile.open("data/" + name + ".txt", std::ios_base::out);
}
else{
outfile.open("data/" + name + ".txt", std::ios_base::app);
}
//Columns t, x(t), y(t)
outfile << this->current_time << "\t" << planets[i].get_pos().get_x() << "\t" << planets[i].get_pos().get_y() << endl;
outfile.close();
}
}
void solar_system::from_file(){
//Columns of planets.txt: name , x_0, y_0, v_x_0, v_y_0
ifstream infile("planets.txt");
while(true){
string name;
double x_0;
double y_0;
double v_x_0;
double v_y_0;
infile >> name >> x_0 >> y_0 >> v_x_0 >> v_y_0;
if(!infile){
break;
}
else{
vec2d pos_0(x_0, y_0);
vec2d vel_0(v_x_0, v_y_0);
planet planet1(pos_0, vel_0, name);
planets.push_back(planet1);
}
}
infile.close();
this->to_file(); //Save initial conditions to a file
}
void solar_system::system_evolve(){
current_time += this->time_step;
for(int i = 0; i < planets.size(); i++){
planets[i].delta_t();
}
this->to_file();
}