forked from modelica/Reference-FMUs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmusim_fmi1_me.c
190 lines (137 loc) · 5.15 KB
/
fmusim_fmi1_me.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
#include <stdlib.h>
#include <math.h>
#include "fmusim_fmi1_me.h"
#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)
FMIStatus simulateFMI1ME(
FMIInstance* S,
const FMIModelDescription* modelDescription,
FMIRecorder* result,
const FMUStaticInput * input,
const FMISimulationSettings* settings) {
FMIStatus status = FMIOK;
fmi1Real time = settings->startTime;
bool stateEvent = false;
fmi1Boolean inputEvent = fmi1False;
fmi1Boolean timeEvent = fmi1False;
fmi1Boolean stepEvent = fmi1False;
fmi1Boolean resetSolver;
FMISolver* solver = NULL;
size_t nSteps = 0;
fmi1Real nextRegularPoint;
fmi1Real nextCommunicationPoint;
fmi1Real nextInputEventTime;
fmi1EventInfo eventInfo = {
.iterationConverged = fmi1False,
.stateValueReferencesChanged = fmi1False,
.stateValuesChanged = fmi1False,
.terminateSimulation = fmi1False,
.upcomingTimeEvent = fmi1False,
.nextEventTime = INFINITY
};
CALL(FMI1InstantiateModel(S,
modelDescription->modelExchange->modelIdentifier, // modelIdentifier
modelDescription->instantiationToken, // GUID
fmi1False // loggingOn
));
// set start values
CALL(applyStartValues(S, settings));
CALL(FMIApplyInput(S, input, time,
true, // discrete
true, // continous
false // after event
));
// initialize
CALL(FMI1Initialize(S, settings->tolerance > 0, settings->tolerance, &eventInfo));
if (!eventInfo.upcomingTimeEvent) {
eventInfo.nextEventTime = INFINITY;
}
const FMISolverParameters solverFunctions = {
.modelInstance = S,
.input = input,
.startTime = time,
.tolerance = settings->tolerance,
.nx = modelDescription->nContinuousStates,
.nz = modelDescription->nEventIndicators,
.setTime = (FMISolverSetTime)FMI1SetTime,
.applyInput = (FMISolverApplyInput)FMIApplyInput,
.getContinuousStates = (FMISolverGetContinuousStates)FMI1GetContinuousStates,
.setContinuousStates = (FMISolverSetContinuousStates)FMI1SetContinuousStates,
.getNominalsOfContinuousStates = (FMISolverGetNominalsOfContinuousStates)FMI1GetNominalContinuousStates,
.getContinuousStateDerivatives = (FMISolverGetContinuousStateDerivatives)FMI1GetDerivatives,
.getEventIndicators = (FMISolverGetEventIndicators)FMI1GetEventIndicators,
.logError = (FMISolverLogError)FMILogError
};
solver = settings->solverCreate(&solverFunctions);
if (!solver) {
status = FMIError;
goto TERMINATE;
}
for (;;) {
CALL(FMISample(S, time, result));
if (time >= settings->stopTime) {
break;
}
nextRegularPoint = settings->startTime + (nSteps + 1) * settings->outputInterval;
nextCommunicationPoint = nextRegularPoint;
nextInputEventTime = FMINextInputEvent(input, time);
inputEvent = nextCommunicationPoint >= nextInputEventTime;
timeEvent = nextCommunicationPoint >= eventInfo.nextEventTime;
if (inputEvent || timeEvent) {
nextCommunicationPoint = fmin(nextInputEventTime, eventInfo.nextEventTime);
}
CALL(settings->solverStep(solver, nextCommunicationPoint, &time, &stateEvent));
CALL(FMI1SetTime(S, time));
CALL(FMIApplyInput(S, input, time,
false, // discrete
true, // continous
false // after event
));
if (time == nextRegularPoint) {
nSteps++;
}
CALL(FMI1CompletedIntegratorStep(S, &stepEvent));
if (eventInfo.terminateSimulation) {
goto TERMINATE;
}
if (inputEvent || timeEvent || stateEvent || stepEvent) {
// record the values before the event
CALL(FMISample(S, time, result));
if (inputEvent) {
CALL(FMIApplyInput(S, input, time,
true, // discrete
true, // continous
true // after event
));
}
resetSolver = fmi1False;
// event iteration
do {
CALL(FMI1EventUpdate(S, fmi1True, &eventInfo));
if (eventInfo.terminateSimulation) {
goto TERMINATE;
}
resetSolver |= eventInfo.stateValuesChanged;
} while (!eventInfo.iterationConverged);
if (!eventInfo.upcomingTimeEvent) {
eventInfo.nextEventTime = INFINITY;
}
if (resetSolver) {
settings->solverReset(solver, time);
}
}
}
TERMINATE:
if (status < FMIError) {
const FMIStatus terminateStatus = FMI1Terminate(S);
if (terminateStatus > status) {
status = terminateStatus;
}
}
if (status != FMIFatal) {
FMI1FreeModelInstance(S);
}
if (solver) {
settings->solverFree(solver);
}
return status;
}