/
simulation_input.cpp
276 lines (251 loc) · 9.98 KB
/
simulation_input.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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2010, Linköpings University,
* Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
* LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
* THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
* PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköpings University, either from the above address,
* from the URL: http://www.ida.liu.se/projects/OpenModelica
* and in the OpenModelica distribution.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
* OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
#include "simulation_input.h"
#include "simulation_runtime.h"
#include "options.h"
#include <fstream>
#include <iomanip>
#include <string.h>
using namespace std;
void read_commented_value(ifstream &f, double *res);
void read_commented_value(ifstream &f, modelica_integer *res);
void read_commented_value(ifstream &f, string *str);
void read_commented_value(ifstream &f, const char **str);
void read_commented_value(ifstream &f, signed char *str);
/* \brief
* Reads initial values from a text file.
*
* The textfile should be given as argument to the main function using
* the -f file flag.
*/
void read_input(int argc, char **argv,
DATA *simData,
double *start, double *stop,
double *stepSize, long *outputSteps,
double *tolerance, string* method,
string* outputFormat, string* variableFilter)
{
string *filename=(string*)getFlagValue("f",argc,argv);
if (filename == NULL) {
filename = new string(string(simData->modelFilePrefix)+"_init.txt"); // model_name defined in generated code for model.
}
ifstream file(filename->c_str());
if (!file) {
cerr << "Error, can not read file " << *filename << " as indata to simulation." << endl;
EXIT(-1);
}
// cerr << "opened file" << endl;
read_commented_value(file,start);
if (sim_verbose) { cout << "read start = " << *start << " from init file." << endl; }
read_commented_value(file,stop);
if (sim_verbose) { cout << "read stop = " << *stop << " from init file." << endl; }
read_commented_value(file,stepSize);
if (sim_verbose) { cout << "read stepSize = " << *stepSize << " from init file." << endl; }
globalData->current_stepsize = *stepSize;
if (stepSize < 0) { // stepSize < 0 => Automatic number of outputs
*outputSteps = -1;
} else {
// Calculate outputSteps from stepSize, start and stop
*outputSteps = (long)(int(*stop-*start) /(*stepSize));
}
read_commented_value(file,tolerance);
if (sim_verbose) { cout << "read tolerance = " << *tolerance << " from init file." << endl; }
read_commented_value(file,method);
if (sim_verbose) { cout << "read method = " << *method << " from init file." << endl; }
read_commented_value(file,outputFormat);
if (sim_verbose) { cout << "read outputFormat = " << *outputFormat << " from init file." << endl; }
read_commented_value(file,variableFilter);
if (sim_verbose) { cout << "read variableFilter = " << *variableFilter << " from init file." << endl; }
modelica_integer nxchk,nychk,npchk;
modelica_integer nyintchk,npintchk;
modelica_integer nyboolchk,npboolchk;
modelica_integer nystrchk,npstrchk;
read_commented_value(file,&nxchk);
read_commented_value(file,&nychk);
read_commented_value(file,&npchk);
read_commented_value(file,&npintchk);
read_commented_value(file,&nyintchk);
read_commented_value(file,&npboolchk);
read_commented_value(file,&nyboolchk);
read_commented_value(file,&npstrchk);
read_commented_value(file,&nystrchk);
if (nxchk != simData->nStates || nychk != simData->nAlgebraic || npchk != simData->nParameters
|| npintchk != simData->intVariables.nParameters || nyintchk != simData->intVariables.nAlgebraic
|| npboolchk != simData->boolVariables.nParameters || nyboolchk != simData->boolVariables.nAlgebraic
|| npstrchk != simData->stringVariables.nParameters || nystrchk != simData->stringVariables.nAlgebraic) {
cerr << "Error, input data file does not match model." << endl;
cerr << "nx in initfile: " << nxchk << " from model code :" << simData->nStates << endl;
cerr << "ny in initfile: " << nychk << " from model code :" << simData->nAlgebraic << endl;
cerr << "np in initfile: " << npchk << " from model code :" << simData->nParameters << endl;
cerr << "npint in initfile: " << npintchk << " from model code: " << simData->intVariables.nParameters << endl;
cerr << "nyint in initfile: " << nyintchk << " from model code: " << simData->intVariables.nAlgebraic << endl;
cerr << "npbool in initfile: " << npboolchk << " from model code: " << simData->boolVariables.nParameters << endl;
cerr << "nybool in initfile: " << nyboolchk << " from model code: " << simData->boolVariables.nAlgebraic << endl;
cerr << "npstr in initfile: " << npstrchk << " from model code: " << simData->stringVariables.nParameters << endl;
cerr << "nystr in initfile: " << nystrchk << " from model code: " << simData->stringVariables.nAlgebraic << endl;
EXIT(-1);
}
for(int i = 0; i < simData->nStates; i++) { // Read x initial values
read_commented_value(file,&simData->states[i]);
if (sim_verbose) {
cout << "read " << getName(&simData->states[i]) << " = "
<< simData->states[i] << " from init file." << endl;
}
}
for(int i = 0; i < simData->nStates; i++) { // Read der(x) initial values
read_commented_value(file,&simData->statesDerivatives[i]);
if (sim_verbose) {
cout << "read " << getName(&simData->statesDerivatives[i]) << " = "
<< simData->statesDerivatives[i] << " from init file." << endl;
}
}
for(int i = 0; i < simData->nAlgebraic; i++) { // Read y initial values
read_commented_value(file,&simData->algebraics[i]);
if (sim_verbose) {
cout << "read " << getName(&simData->algebraics[i]) << " = "
<< simData->algebraics[i] << " from init file." << endl;
}
}
for(int i = 0; i < simData->nParameters; i++) { // Read parameter values
read_commented_value(file,&simData->parameters[i]);
if (sim_verbose) {
cout << "read " << getName(&simData->parameters[i]) << " = "
<< simData->parameters[i] << " from init file." << endl;
}
}
for(int i = 0; i < simData->intVariables.nParameters; i++) { // Read parameter values
read_commented_value(file,&simData->intVariables.parameters[i]);
if (sim_verbose) {
cout << "read " << getName(&simData->intVariables.parameters[i]) << " = "
<< simData->intVariables.parameters[i] << " from init file." << endl;
}
}
for(int i = 0; i < simData->intVariables.nAlgebraic; i++) { // Read parameter values
read_commented_value(file,&simData->intVariables.algebraics[i]);
if (sim_verbose) {
cout << "read " << getName(&simData->intVariables.algebraics[i]) << " = "
<< simData->intVariables.algebraics[i] << " from init file." << endl;
}
}
for(int i = 0; i < simData->boolVariables.nParameters; i++) { // Read parameter values
read_commented_value(file,&simData->boolVariables.parameters[i]);
if (sim_verbose) {
cout << "read " << getName(&simData->boolVariables.parameters[i]) << " = "
<< (bool)simData->boolVariables.parameters[i] << " from init file." << endl;
}
}
for(int i = 0; i < simData->boolVariables.nAlgebraic; i++) { // Read parameter values
read_commented_value(file,&simData->boolVariables.algebraics[i]);
if (sim_verbose) {
cout << "read " << getName(&simData->boolVariables.algebraics[i]) << " = "
<< (bool)simData->boolVariables.algebraics[i] << " from init file." << endl;
}
}
for(int i=0; i < simData->stringVariables.nParameters; i++) { // Read string parameter values
read_commented_value(file,&(simData->stringVariables.parameters[i]));
if (sim_verbose) {
cout << "read " << getName((double*)&simData->stringVariables.parameters[i]) << " = \"" << simData->stringVariables.parameters[i] << "\" from init file." << endl;
}
}
for(int i=0; i < simData->stringVariables.nAlgebraic; i++) { // Read string algebraic values
read_commented_value(file,&simData->stringVariables.algebraics[i]);
if (sim_verbose) {
cout << "read " << simData->stringVariables.algebraics[i] << " from init file." << endl;
}
}
file.close();
if (sim_verbose) {
cout << "Read parameter data from file " << *filename << endl;
}
delete filename;
}
inline void read_commented_value(ifstream &f, string *str)
{
string line;
char c[2048];
f.getline(c,2048);
line = c;
int pos;
if (line.find("\"") != line.npos) {
pos = line.rfind("\""); // find end of string
*str = string(line.substr(1,pos-1)); // Remove " at beginning and end
}
}
inline void read_commented_value(ifstream &f, const char **str)
{
if (str == NULL) {
cerr << "error read_commented_value, no data allocated for storing string" << endl;
return;
}
string line;
read_commented_value(f,&line);
*str = strdup(line.c_str());
}
inline void read_commented_value(ifstream &f, double *res)
{
string line;
// f >> *res;
char c[160];
f.getline(c,160);
line = c;
if (line.find("true") != line.npos) {
*res = 1.0;
}
else if (line.find("false") != line.npos) {
*res = 0.0;
}
else {
*res = atof(c);
}
}
inline void read_commented_value( ifstream &f, signed char *res)
{
string line;
// f >> *res;
char c[160];
f.getline(c,160);
line = c;
if (line.find("true") != line.npos) {
*res = 1;
}
else if (line.find("false") != line.npos) {
*res = 0;
}
else {
*res = 0;
//*res = atof(c);
}
}
inline void read_commented_value( ifstream &f, modelica_integer *res)
{
f >> *res;
char c[160];
f.getline(c,160);
}