-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathFMI1MESimulation.c
194 lines (139 loc) · 5.54 KB
/
FMI1MESimulation.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
#include <stdlib.h>
#include <math.h>
#include "FMIUtil.h"
#include "FMI1.h"
#include "FMI1MESimulation.h"
#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)
FMIStatus FMI1MESimulate(const FMISimulationSettings* s) {
FMIStatus status = FMIOK;
FMIInstance* S = s->S;
fmi1Real time = s->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,
s->modelDescription->modelExchange->modelIdentifier, // modelIdentifier
s->modelDescription->instantiationToken, // GUID
s->loggingOn // loggingOn
));
// set start values
CALL(FMIApplyStartValues(S, s));
CALL(FMIApplyInput(S, s->input, time,
true, // discrete
true, // continuous
false // after event
));
// initialize
CALL(FMI1Initialize(S, s->tolerance > 0, s->tolerance, &eventInfo));
const FMISolverParameters solverFunctions = {
.modelInstance = S,
.input = s->input,
.startTime = time,
.tolerance = s->tolerance,
.nx = s->modelDescription->nContinuousStates,
.nz = s->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 = s->solverCreate(&solverFunctions);
if (!solver) {
status = FMIError;
goto TERMINATE;
}
CALL(FMISample(S, time, s->initialRecorder));
for (;;) {
CALL(FMISample(S, time, s->recorder));
if (time > s->stopTime || FMIIsClose(time, s->stopTime)) {
break;
}
nextRegularPoint = s->startTime + (nSteps + 1) * s->outputInterval;
nextCommunicationPoint = nextRegularPoint;
nextInputEventTime = FMINextInputEvent(s->input, time);
if (nextCommunicationPoint > nextInputEventTime && !FMIIsClose(nextCommunicationPoint, nextInputEventTime)) {
nextCommunicationPoint = nextInputEventTime;
}
if (eventInfo.upcomingTimeEvent && nextCommunicationPoint > eventInfo.nextEventTime && !FMIIsClose(nextCommunicationPoint, eventInfo.nextEventTime)) {
nextCommunicationPoint = eventInfo.nextEventTime;
}
if (nextCommunicationPoint > s->stopTime && !FMIIsClose(nextCommunicationPoint, s->stopTime)) {
nextCommunicationPoint = s->stopTime;
}
inputEvent = FMIIsClose(nextCommunicationPoint, nextInputEventTime);
timeEvent = eventInfo.upcomingTimeEvent && FMIIsClose(nextCommunicationPoint, eventInfo.nextEventTime);
CALL(s->solverStep(solver, nextCommunicationPoint, &time, &stateEvent));
CALL(FMI1SetTime(S, time));
CALL(FMIApplyInput(S, s->input, time,
false, // discrete
true, // continuous
false // after event
));
if (FMIIsClose(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, s->recorder));
if (inputEvent) {
CALL(FMIApplyInput(S, s->input, time,
true, // discrete
true, // continuous
true // after event
));
}
resetSolver = fmi1False;
do {
CALL(FMI1EventUpdate(S, fmi1True, &eventInfo));
if (eventInfo.terminateSimulation) {
CALL(FMISample(S, time, s->recorder));
goto TERMINATE;
}
resetSolver |= eventInfo.stateValuesChanged;
} while (!eventInfo.iterationConverged);
if (resetSolver) {
s->solverReset(solver, time);
}
}
if (s->stepFinished && !s->stepFinished(s, time)) {
break;
}
}
TERMINATE:
if (status < FMIError) {
const FMIStatus terminateStatus = FMI1Terminate(S);
if (terminateStatus > status) {
status = terminateStatus;
}
}
if (status != FMIFatal && S->component) {
FMI1FreeModelInstance(S);
}
if (solver) {
s->solverFree(solver);
}
return status;
}