-
Notifications
You must be signed in to change notification settings - Fork 63
/
Copy pathFMI2CSSimulation.c
143 lines (98 loc) · 3.75 KB
/
FMI2CSSimulation.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
#include "FMIUtil.h"
#include "FMI2.h"
#include "FMI2CSSimulation.h"
#define FMI_PATH_MAX 4096
#define CALL(f) do { status = f; if (status > FMIOK) goto TERMINATE; } while (0)
FMIStatus FMI2CSSimulate(const FMISimulationSettings* s) {
FMIStatus status = FMIOK;
fmi2Boolean terminateSimulation = fmi2False;
fmi2Real time = s->startTime;
fmi2Real nextRegularPoint = 0.0;
fmi2Real nextCommunicationPoint = 0.0;
fmi2Real nextInputEventTime = 0.0;
fmi2Real stepSize = 0.0;
const bool canHandleVariableCommunicationStepSize = s->modelDescription->coSimulation->canHandleVariableCommunicationStepSize;
char resourcePath[FMI_PATH_MAX] = "";
char resourceURI[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
CALL(FMIPathToURI(resourcePath, resourceURI, FMI_PATH_MAX));
FMIInstance* S = s->S;
CALL(FMI2Instantiate(S,
resourceURI,
fmi2CoSimulation,
s->modelDescription->instantiationToken,
s->visible,
s->loggingOn
));
if (s->initialFMUStateFile) {
CALL(FMIRestoreFMUStateFromFile(S, s->initialFMUStateFile));
}
CALL(FMIApplyStartValues(S, s));
if (!s->initialFMUStateFile) {
CALL(FMI2SetupExperiment(S, s->tolerance > 0, s->tolerance, time, s->setStopTime, s->stopTime));
CALL(FMI2EnterInitializationMode(S));
CALL(FMIApplyInput(S, s->input, s->startTime, true, true, true));
CALL(FMI2ExitInitializationMode(S));
}
CALL(FMISample(S, time, s->initialRecorder));
CALL(FMISample(S, time, s->recorder));
size_t nSteps = 0;
for (;;) {
if (time > s->stopTime || FMIIsClose(time, s->stopTime)) {
break;
}
nextRegularPoint = s->startTime + (nSteps + 1) * s->outputInterval;
nextCommunicationPoint = nextRegularPoint;
nextInputEventTime = FMINextInputEvent(s->input, time);
if (canHandleVariableCommunicationStepSize &&
nextCommunicationPoint > nextInputEventTime &&
!FMIIsClose(nextCommunicationPoint, nextInputEventTime)) {
nextCommunicationPoint = nextInputEventTime;
}
if (nextCommunicationPoint > s->stopTime && !FMIIsClose(nextCommunicationPoint, s->stopTime)) {
if (canHandleVariableCommunicationStepSize) {
nextCommunicationPoint = s->stopTime;
} else {
break;
}
}
stepSize = nextCommunicationPoint - time;
CALL(FMIApplyInput(S, s->input, time, true, true, true));
const FMIStatus doStepStatus = FMI2DoStep(S, time, stepSize, fmi2True);
if (doStepStatus == fmi2Discard) {
CALL(FMI2GetRealStatus(S, fmi2LastSuccessfulTime, &time));
CALL(FMI2GetBooleanStatus(S, fmi2Terminated, &terminateSimulation));
} else {
CALL(doStepStatus);
time = nextCommunicationPoint;
}
if (FMIIsClose(time, nextRegularPoint)) {
nSteps++;
}
CALL(FMISample(S, time, s->recorder));
if (terminateSimulation) {
goto TERMINATE;
}
if (s->stepFinished && !s->stepFinished(s, time)) {
break;
}
}
if (s->finalFMUStateFile) {
CALL(FMISaveFMUStateToFile(S, s->finalFMUStateFile));
}
TERMINATE:
if (status < FMIError) {
const FMIStatus terminateStatus = FMI2Terminate(S);
if (terminateStatus > status) {
status = terminateStatus;
}
}
if (status != FMIFatal && S->component) {
FMI2FreeInstance(S);
}
return status;
}