-
Notifications
You must be signed in to change notification settings - Fork 19
/
Simulation.cpp
executable file
·123 lines (103 loc) · 2.97 KB
/
Simulation.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
#include <Simulation.h>
Simulation::Simulation(Planet* planet, const std::string& name, double speed, double dt)
{
m_planet = planet;
m_name = name;
m_satellites = new std::vector<Satellite>();
sim_t = 0.0;
sim_dt = dt;
sim_speed = speed;
m_play = Constants::autoPlay;
m_verbose = Constants::verbose;
m_writeLog = Constants::writeLog;
}
Simulation::~Simulation()
{
delete m_satellites;
}
void Simulation::update(void)
{
if(m_play)
{
// Add time interval to sim time
sim_t += dt();
//Print info
if(m_verbose)
{
std::cout << "t = " << t() << std::endl;
for(unsigned int i=0; i<m_satellites->size(); ++i)
{
std::cout << m_satellites->at(i).getName() << std::endl;
std::cout << "v = " << m_satellites->at(i).getOrbit()->getV();
std::cout << " / E = " << m_satellites->at(i).getOrbit()->getE();
std::cout << " / M = " << m_satellites->at(i).getOrbit()->getM() << std::endl;
m_satellites->at(i).getCurrentPosition().print();
}
}
// Update each satellite's orbit and position
for(unsigned int i=0; i<m_satellites->size(); ++i)
{
m_satellites->at(i).update(dt());
}
}
return;
}
void Simulation::addSatellite(const Satellite &sat)
{
if(sat.getPlanet() == m_planet)
m_satellites->push_back(sat);
return;
}
void Simulation::reset(void)
{
//Reset time to 0
sim_t = 0.0;
// Reset each satellite's position to initial state
for(unsigned int i=0; i<m_satellites->size(); ++i)
{
m_satellites->at(i).reset();
}
return;
}
void Simulation::resetAll(void)
{
//Reset time to 0
sim_t = 0.0;
//Remove all satellites
m_satellites->clear();
return;
}
int Simulation::saveToFile(const std::string &path, const std::string &date) const
{
std::ofstream file(path.c_str());
if(file)
{
file << Constants::programName << std::endl;
file << date << std::endl;
file << path << std::endl;
file << "----------" << std::endl;
file << "Simulation" << std::endl;
file << toString();
file << "----------" << std::endl;
file << "Planet" << std::endl;
file << m_planet->toString();
file << "----------" << std::endl;
file << "Satellites" << std::endl;
for(unsigned int i=0; i<m_satellites->size(); ++i)
{
file << m_satellites->at(i).toString();
}
return 0;
}
return 1;
}
const std::string Simulation::toString(void) const
{
std::stringstream output;
output << "Name: " << m_name << std::endl;
output << "t: " << sim_t << std::endl;
output << "dt: " << sim_dt << std::endl;
output << "Speed: " << sim_speed << std::endl;
output << "n: " << m_satellites->size() << std::endl;
return output.str();
}