-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulation.cpp
209 lines (180 loc) · 4.89 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
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
#include "Simulation.h"
Simulation::Simulation(Model& _model, const bfs::path& _paramFilePath, const bfs::path& _inputFilePath, const bfs::path& _outputFilePath)
: model(_model),
paramFilePath(_paramFilePath),
inputFilePath(_inputFilePath),
outputFilePath(_outputFilePath) {}
void Simulation::Simulate(const float tFinal, const float dt) {
ConfirmFiles();
ReadParamFile();
const int nSteps = int(tFinal / dt) + 1;
for (int i = 0; i < nSteps; i++) {
float t = i * dt;
if (model.HasInputs())
ReadInputFile(t);
model.Process(t, dt);
WriteOutputFile(t);
}
}
void Simulation::ConfirmFiles() {
int choice;
if (bfs::exists(outputFilePath)) {
std::cout << "\nAre you sure you want to overwrite " << outputFilePath << "? Enter 1 for yes, 2 for no: ";
std::cin >> choice;
switch (choice) {
case 1:
break;
case 2:
exit(1);
default:
std::cerr << "Not an option!\n";
exit(1);
}
}
if (!bfs::exists(paramFilePath) || (!bfs::exists(inputFilePath) && model.HasInputs())) {
if (!bfs::exists(paramFilePath))
CreateTemplateParamFile();
if (!bfs::exists(inputFilePath) && model.HasInputs())
CreateTemplateInputFile();
std::cout << "\nEdit, save, and close new template files, then enter 1: ";
std::cin >> choice;
}
}
void Simulation::ReadParamFile() {
bfs::ifstream paramFile(paramFilePath);
if (!paramFile) {
std::cerr << paramFilePath << " could not be opened!\n";
exit(1);
}
// skip the header
char c;
do {
paramFile.get(c);
}
while (c != '\n');
static int column = 0;
std::string s = "";
do {
paramFile.get(c);
if (c != ',' && c != '\n')
s += c;
else {
if (s != "")
model.SetParam(column, s);
else {
std::cerr << "Must give a value for a param!\n";
exit(1);
}
column++;
s = "";
}
}
while (c != '\n' && !paramFile.eof());
}
void Simulation::ReadInputFile(float t) {
// only read inputs when it is the right time
static float tNextInput = 0;
if (t != tNextInput)
return;
// open file
static bfs::ifstream inputFile(inputFilePath);
if (!inputFile) {
std::cerr << inputFilePath << " could not be opened!\n";
exit(1);
}
// skip the header on the first call
static bool first = true;
char c;
if (first) {
do {
inputFile.get(c);
}
while (c != '\n');
}
static int column = 0;
bool reachedLineEnd = false;
std::string s = "";
do {
inputFile.get(c);
if (c != ',' && c != '\n')
s += c;
else {
if (s != "") {
if (column == 0) // time of the input (tInput) is always in column 0
tNextInput = std::stof(s);
else
model.SetInput(column - 1, s); // SetInput calls for an index of the input starting at 0, but time is not included, hence the - 1
}
if (c == '\n') {
column = 0;
reachedLineEnd = true;
}
else
column++;
s = "";
}
}
while ((!reachedLineEnd || column != 1) && !inputFile.eof()); // read the tInput of the next line and then stop
first = false;
}
void Simulation::WriteOutputFile(float t) {
static bool first = true;
if (first && !bfs::exists(outputFilePath))
std::cout << "\nCreated " << outputFilePath << ".\n";
static bfs::ofstream outputFile(outputFilePath);
auto paramNames = model.GetParamNames();
auto inputNames = model.GetInputNames();
auto outputNames = model.GetOutputNames();
if (first) {
for (auto i = paramNames.begin(); i != paramNames.end(); i++)
outputFile << *i << ",";
outputFile << "t";
for (auto i = inputNames.begin(); i != inputNames.end(); i++)
outputFile << "," << *i;
for (auto i = outputNames.begin(); i != outputNames.end(); i++)
outputFile << "," << *i;
outputFile << "\n";
}
for (unsigned int i = 0; i < paramNames.size(); i++) {
if (first)
outputFile << model.GetParam(i);
outputFile << ",";
}
outputFile << t;
for (unsigned int i = 0; i < inputNames.size(); i++)
outputFile << "," << model.GetInput(i);
for (unsigned int i = 0; i < outputNames.size(); i++)
outputFile << "," << model.GetOutput(i);
outputFile << "\n";
first = false;
}
void Simulation::CreateTemplateParamFile() {
bfs::ofstream paramFile(paramFilePath);
auto paramNames = model.GetParamNames();
for (auto i = paramNames.begin(); i != paramNames.end(); i++) {
if (i != paramNames.begin())
paramFile << ",";
paramFile << *i;
}
paramFile << "\n";
for (auto i = paramNames.begin(); i != paramNames.end(); i++) {
if (i != paramNames.begin())
paramFile << ",";
paramFile << "-";
}
paramFile << "\n";
std::cout << "Created " << paramFilePath << " as a template.\n";
}
void Simulation::CreateTemplateInputFile() {
bfs::ofstream inputFile(inputFilePath);
auto inputNames = model.GetInputNames();
inputFile << "t";
for (auto i = inputNames.begin(); i != inputNames.end(); i++)
inputFile << "," << *i;
inputFile << "\n";
inputFile << "0";
for (auto i = inputNames.begin(); i != inputNames.end(); i++)
inputFile << "," << "-";
inputFile << "\n";
std::cout << "Created " << inputFilePath << " as a template.\n";
}