forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFMIStaticInput.c
306 lines (200 loc) · 7.15 KB
/
FMIStaticInput.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "csv.h"
#include "FMIUtil.h"
#include "FMIStaticInput.h"
#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)
FMIStaticInput* FMIReadInput(const FMIModelDescription* modelDescription, const char* filename) {
FMIStatus status = FMIOK;
FMIStaticInput* input = NULL;
CsvHandle handle = NULL;
CALL(FMICalloc((void**)&input, 1, sizeof(FMIStaticInput)));
input->fmiMajorVersion = modelDescription->fmiMajorVersion;
char* row = NULL;
handle = CsvOpen(filename);
if (!handle) {
FMILogError("Failed to read input file %s.\n", filename);
status = FMIError;
goto TERMINATE;
}
// variable names
row = CsvReadNextRow(handle);
const char* col = CsvReadNextCol(row, handle);
while ((col = CsvReadNextCol(row, handle))) {
const FMIModelVariable* variable = FMIModelVariableForName(modelDescription, col);
if (!variable) {
FMILogError("Variable %s not found.\n", col);
status = FMIError;
goto TERMINATE;
}
CALL(FMIRealloc((void**)&input->variables, (input->nVariables + 1) * sizeof(FMIModelVariable*)));
input->variables[input->nVariables] = variable;
input->nVariables++;
}
// data
while ((row = CsvReadNextRow(handle))) {
CALL(FMIRealloc((void**)&input->time, (input->nRows + 1) * sizeof(double)));
CALL(FMIRealloc((void**)&input->nValues, (input->nRows + 1) * input->nVariables * sizeof(size_t)));
CALL(FMIRealloc((void**)&input->values, (input->nRows + 1) * input->nVariables * sizeof(void*)));
const size_t index = input->nRows * input->nVariables;
memset(&input->nValues[index], 0x0, input->nVariables * sizeof(size_t));
memset(&input->values [index], 0x0, input->nVariables * sizeof(void*));
char* eptr;
// time
col = CsvReadNextCol(row, handle);
input->time[input->nRows] = strtod(col, &eptr);
if (input->nRows > 0 && input->time[input->nRows - 1] > input->time[input->nRows]) {
FMILogError("Values in first column (time) must be monotonically increasing.\n");
status = FMIError;
goto TERMINATE;
}
size_t i = 0; // variable index
while ((col = CsvReadNextCol(row, handle))) {
if (i >= input->nVariables) {
FMILogError("The number of columns must be equal to the number of variables.\n");
status = FMIError;
goto TERMINATE;
}
const FMIModelVariable* variable = input->variables[i];
const size_t index = (input->nRows * input->nVariables) + i;
CALL(FMIParseValues(modelDescription->fmiMajorVersion, variable->type, col, &input->nValues[index], &input->values[index]));
i++;
}
input->nRows++;
}
TERMINATE:
if (handle) {
CsvClose(handle);
}
if (status == FMIOK) {
return input;
}
FMIFreeInput(input);
return NULL;
}
void FMIFreeInput(FMIStaticInput* input) {
if (!input) {
return;
}
FMIFree((void**)&input->variables);
FMIFree((void**)&input->nValues);
FMIFree((void**)&input->buffer);
FMIFree((void**)&input);
}
double FMINextInputEvent(const FMIStaticInput* input, double time) {
if (!input) {
return INFINITY;
}
for (size_t i = 0; i < input->nRows - 1; i++) {
const double t0 = input->time[i];
const double t1 = input->time[i + 1];
if (time >= t1) {
continue;
}
if (t0 == t1) {
return t0; // discrete change of a continuous variable
}
for (size_t j = 0; j < input->nVariables; j++) {
const FMIModelVariable* variable = input->variables[j];
const FMIVariableType type = variable->type;
const size_t nValues = input->nValues[j];
if (variable->variability == FMIContinuous) {
continue; // skip continuous variables
}
const void* values0 = input->values[i * input->nVariables + j];
const void* values1 = input->values[(i + 1) * input->nVariables + j];
const size_t size = FMISizeOfVariableType(type, input->fmiMajorVersion) * nValues;
if (memcmp(values0, values1, size)) {
return t1;
}
}
}
return INFINITY;
}
FMIStatus FMIApplyInput(FMIInstance* instance, const FMIStaticInput* input, double time, bool discrete, bool continuous, bool afterEvent) {
FMIStatus status = FMIOK;
if (!input) {
goto TERMINATE;
}
for (size_t i = 0; i < input->nVariables; i++) {
const FMIModelVariable* variable = input->variables[i];
const FMIVariableType type = variable->type;
const FMIValueReference vr = variable->valueReference;
if (continuous && variable->variability == FMIContinuous) {
size_t row = 0;
while (row < input->nRows - 1) {
const double nextTime = input->time[row + 1];
if (afterEvent ? nextTime > time : nextTime >= time) {
break;
}
row++;
}
const size_t j = row * input->nVariables + i;
const size_t nValues = input->nValues[j];
if (nValues == 0) continue;
const size_t requiredBufferSize = nValues * sizeof(double);
if (input->bufferSize < requiredBufferSize) {
// TODO: allocate in FMIReadInput()
CALL(FMIRealloc((void**)&input->buffer, requiredBufferSize));
((FMIStaticInput*)input)->bufferSize = requiredBufferSize;
}
if (variable->type == FMIFloat32Type) {
float* buffer = (float*)input->buffer;
const float* values0 = (float*)input->values[row * input->nVariables + i];
const float* values1 = (float*)input->values[(row + 1) * input->nVariables + i];
for (size_t k = 0; k < nValues; k++) {
if (row >= input->nRows - 1) {
buffer[k] = values0[k];
} else {
const double t0 = input->time[row];
const double t1 = input->time[row + 1];
const double x0 = values0[k];
const double x1 = values1[k];
buffer[k] = (float)(x0 + (time - t0) * (x1 - x0) / (t1 - t0));
}
}
} else if (variable->type == FMIFloat64Type) {
double* buffer = (double*)input->buffer;
const double* values0 = (double*)input->values[row * input->nVariables + i];
const double* values1 = (double*)input->values[(row + 1) * input->nVariables + i];
for (size_t k = 0; k < nValues; k++) {
if (row >= input->nRows - 1) {
buffer[k] = values0[k];
} else {
const double t0 = input->time[row];
const double t1 = input->time[row + 1];
const double x0 = values0[k];
const double x1 = values1[k];
buffer[k] = x0 + (time - t0) * (x1 - x0) / (t1 - t0);
}
}
}
CALL(FMISetValues(instance, type, &vr, 1, NULL, input->buffer, nValues));
} else if (discrete && (variable->variability == FMIDiscrete || variable->variability == FMITunable)) {
size_t row = 0;
for (size_t i = 1; i < input->nRows; i++) {
const double t = input->time[i];
if (t >= time) {
break;
}
row = i;
}
if (afterEvent) {
while (row < input->nRows - 1) {
if (input->time[row + 1] > time) {
break;
}
row++;
}
}
const size_t j = row * input->nVariables + i;
const size_t nValues = input->nValues[j];
const void* values = input->values[j];
if (nValues == 0) continue;
CALL(FMISetValues(instance, type, &vr, 1, NULL, values, nValues));
}
}
TERMINATE:
return status;
}