forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFMI3CSSimulation.c
257 lines (190 loc) · 7.22 KB
/
FMI3CSSimulation.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
#include <stdlib.h>
#include <math.h>
#include "FMIUtil.h"
#include "FMI3.h"
#include "FMI3CSSimulation.h"
#define FMI_PATH_MAX 4096
#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)
static void recordIntermediateValues(
fmi3InstanceEnvironment instanceEnvironment,
fmi3Float64 intermediateUpdateTime,
fmi3Boolean intermediateVariableSetRequested,
fmi3Boolean intermediateVariableGetAllowed,
fmi3Boolean intermediateStepFinished,
fmi3Boolean canReturnEarly,
fmi3Boolean* earlyReturnRequested,
fmi3Float64* earlyReturnTime) {
FMIInstance* instance = (FMIInstance*)instanceEnvironment;
FMIRecorder* recorder = (FMIRecorder*)instance->userData;
if (intermediateVariableGetAllowed) {
// TODO: handle return code
FMISample(instance, intermediateUpdateTime, recorder);
}
*earlyReturnRequested = fmi3False;
}
FMIStatus FMI3CSSimulate(const FMISimulationSettings* s) {
FMIStatus status = FMIOK;
FMIInstance* S = s->S;
fmi3Boolean inputEvent = fmi3False;
fmi3Boolean eventEncountered = fmi3False;
fmi3Boolean terminateSimulation = fmi3False;
fmi3Boolean earlyReturn = fmi3False;
fmi3Float64 lastSuccessfulTime = s->startTime;
fmi3Float64 time = s->startTime;
fmi3Float64 nextCommunicationPoint = 0.0;
fmi3Float64 nextRegularPoint = 0.0;
fmi3Float64 stepSize = 0.0;
fmi3Float64 nextInputEventTime = INFINITY;
fmi3Boolean discreteStatesNeedUpdate = fmi3True;
fmi3Boolean nominalsOfContinuousStatesChanged = fmi3False;
fmi3Boolean valuesOfContinuousStatesChanged = fmi3False;
fmi3Boolean nextEventTimeDefined = fmi3False;
fmi3Float64 nextEventTime = INFINITY;
fmi3ValueReference* requiredIntermediateVariables = NULL;
size_t nRequiredIntermediateVariables = 0;
fmi3IntermediateUpdateCallback intermediateUpdate = NULL;
char resourcePath[FMI_PATH_MAX] = "";
#ifdef _WIN32
snprintf(resourcePath, FMI_PATH_MAX, "%s\\resources\\", s->unzipdir);
#else
snprintf(resourcePath, FMI_PATH_MAX, "%s/resources/", s->unzipdir);
#endif
if (s->recordIntermediateValues) {
requiredIntermediateVariables = (fmi3ValueReference*)s->recorder->valueReferences;
nRequiredIntermediateVariables = s->recorder->nVariables;
intermediateUpdate = recordIntermediateValues;
S->userData = s->recorder;
}
CALL(FMI3InstantiateCoSimulation(S,
s->modelDescription->instantiationToken,
resourcePath,
s->visible,
s->loggingOn,
s->eventModeUsed,
s->earlyReturnAllowed,
requiredIntermediateVariables,
nRequiredIntermediateVariables,
intermediateUpdate
));
if (s->initialFMUStateFile) {
CALL(FMIRestoreFMUStateFromFile(S, s->initialFMUStateFile));
}
CALL(FMIApplyStartValues(S, s));
if (!s->initialFMUStateFile) {
CALL(FMI3EnterInitializationMode(S, s->tolerance > 0, s->tolerance, s->startTime, fmi3False, 0));
CALL(FMIApplyInput(S, s->input, s->startTime, true, true, false));
CALL(FMI3ExitInitializationMode(S));
if (s->eventModeUsed) {
do {
CALL(FMI3UpdateDiscreteStates(S,
&discreteStatesNeedUpdate,
&terminateSimulation,
&nominalsOfContinuousStatesChanged,
&valuesOfContinuousStatesChanged,
&nextEventTimeDefined,
&nextEventTime));
if (terminateSimulation) {
goto TERMINATE;
}
} while (discreteStatesNeedUpdate);
if (!nextEventTimeDefined) {
nextEventTime = INFINITY;
}
CALL(FMI3EnterStepMode(S));
}
}
CALL(FMIRecorderUpdateSizes(s->initialRecorder));
CALL(FMIRecorderUpdateSizes(s->recorder));
CALL(FMISample(S, time, s->initialRecorder));
CALL(FMISample(S, time, s->recorder));
size_t nSteps = 0;
for (;;) {
if (time >= s->stopTime) {
break;
}
nextRegularPoint = s->startTime + (nSteps + 1) * s->outputInterval;
nextCommunicationPoint = nextRegularPoint;
nextInputEventTime = FMINextInputEvent(s->input, time);
inputEvent = nextCommunicationPoint >= nextInputEventTime;
if (inputEvent) {
nextCommunicationPoint = nextInputEventTime;
}
stepSize = nextCommunicationPoint - time;
CALL(FMIApplyInput(S, s->input, time,
!s->eventModeUsed, // discrete
true, // continuous
!s->eventModeUsed // afterEvent
));
CALL(FMI3DoStep(S,
time, // currentCommunicationPoint
stepSize, // communicationStepSize
fmi3True, // noSetFMUStatePriorToCurrentPoint
&eventEncountered, // eventEncountered
&terminateSimulation, // terminateSimulation
&earlyReturn, // earlyReturn
&lastSuccessfulTime // lastSuccessfulTime
));
if (earlyReturn && !s->earlyReturnAllowed) {
FMILogError("The FMU returned early from fmi3DoStep() but early return is not allowed.");
status = FMIError;
goto TERMINATE;
}
if (earlyReturn && lastSuccessfulTime < nextCommunicationPoint) {
time = lastSuccessfulTime;
} else {
time = nextCommunicationPoint;
}
if (time == nextRegularPoint) {
nSteps++;
}
CALL(FMISample(S, time, s->recorder));
if (terminateSimulation) {
goto TERMINATE;
}
if (s->eventModeUsed && (inputEvent || eventEncountered)) {
CALL(FMI3EnterEventMode(S));
if (inputEvent) {
CALL(FMIApplyInput(S, s->input, time,
true, // discrete
true, // continous
true // after event
));
}
do {
CALL(FMI3UpdateDiscreteStates(S,
&discreteStatesNeedUpdate,
&terminateSimulation,
&nominalsOfContinuousStatesChanged,
&valuesOfContinuousStatesChanged,
&nextEventTimeDefined,
&nextEventTime));
if (terminateSimulation) {
CALL(FMISample(S, time, s->recorder));
goto TERMINATE;
}
} while (discreteStatesNeedUpdate);
if (!nextEventTimeDefined) {
nextEventTime = INFINITY;
}
CALL(FMI3EnterStepMode(S));
CALL(FMISample(S, time, s->recorder));
}
if (s->stepFinished && !s->stepFinished(s, time)) {
break;
}
}
if (s->finalFMUStateFile) {
CALL(FMISaveFMUStateToFile(S, s->finalFMUStateFile));
}
TERMINATE:
if (status < FMIError) {
const FMIStatus terminateStatus = FMI3Terminate(S);
if (terminateStatus > status) {
status = terminateStatus;
}
}
if (status != FMIFatal) {
FMI3FreeInstance(S);
}
return status;
}