/
fmi2Functions.c
executable file
·389 lines (347 loc) · 10.8 KB
/
fmi2Functions.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**********************************************************
* This file is generated by 20-sim ANSI-C Code Generator
*
* file: src\fmi2Functions.c
* model: R2G2P_Body_Only
* expmt: R2G2P_Body_Only
* date: April 5, 2016
* time: 10:29:15 AM
* user: INTO-CPS
* from: 20-sim 4.6 Professional Single
* build: 4.6.0.6684
**********************************************************/
/* This file contains the implementation of the FMI functions
Please check the fmiSunctions.h file for more details
*/
/* The FMI related headers */
#include "fmi2Functions.h"
#include "fmiGUID.h"
/* Our own include files */
#include "xxsubmod.h"
/* The system include files */
#include <string.h>
/* make a FMI_Dll_Export that is for both FMI1 and FMI2 */
#define FMI_Dll_Export FMI2_Export
/* our own component identifier
this can become a pointer to a full structure that is filled when instantiating
for now a simple non-NULL string pointer is returned
*/
#define xxComponent "Body_Block"
/* Global pointer to co-simulator callback functions */
const fmi2CallbackFunctions* g_fmiCallbackFunctions = NULL;
/* Inquire version numbers of header files */
FMI_Dll_Export const char* fmi2GetTypesPlatform()
{
return fmi2TypesPlatform;
}
FMI_Dll_Export const char* fmi2GetVersion()
{
return fmi2Version;
}
FMI_Dll_Export fmi2Status fmi2SetDebugLogging (fmi2Component c, fmi2Boolean loggingOn,
size_t nCategories,
const fmi2String categories[])
{
return fmi2OK; /* not yet */
}
/* Data Exchange Functions*/
FMI_Dll_Export fmi2Status fmi2GetReal(fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr, fmi2Real value[])
{
size_t i;
for (i = 0; i < nvr; i++)
{
value [i] = xx_MEMORY[ vr[i] ];
}
return fmi2OK;
}
FMI_Dll_Export fmi2Status fmi2GetInteger(fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr,
fmi2Integer value[])
{
/* 20-sim generated C-code uses doubles for the model equation calculations. The variable type (i.e. integer or boolean) are not supported as data type, but are transfered to doubles. In the FMI interface however, the double can be converted to its implicit type */
size_t i;
for (i = 0; i < nvr; i++)
{
value [i] = (fmi2Integer) xx_MEMORY[ vr[i] ];
}
return fmi2OK;
}
FMI_Dll_Export fmi2Status fmi2GetBoolean(fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr,
fmi2Boolean value[])
{
/* 20-sim generated C-code uses doubles for the model equation calculations. The variable type (i.e. integer or boolean) are not supported as data type, but are transfered to doubles. In the FMI interface however, the double can be converted to its implicit type */
size_t i;
for (i = 0; i < nvr; i++)
{
value [i] = (xx_MEMORY[ vr[i] ] == 1.0);
}
return fmi2OK;
}
FMI_Dll_Export fmi2Status fmi2GetString(fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr,
fmi2String value[])
{
return fmi2Error; /* not yet */
}
FMI_Dll_Export fmi2Status fmi2SetReal(fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr,
const fmi2Real value[])
{
size_t i;
for (i = 0; i < nvr; i++)
{
xx_MEMORY[ vr[i] ] = value [i];
}
return fmi2OK;
}
FMI_Dll_Export fmi2Status fmi2SetInteger (fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr,
const fmi2Integer value[])
{
/* 20-sim generated C-code uses doubles for the model equation calculations. The variable type (i.e. integer or boolean) are not supported as data type, but are transfered to doubles. In the FMI interface however, the double can be converted to its implicit type */
size_t i;
for (i = 0; i < nvr; i++)
{
xx_MEMORY[ vr[i] ] = (XXDouble) value [i];
}
return fmi2OK;
}
FMI_Dll_Export fmi2Status fmi2SetBoolean(fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr,
const fmi2Boolean value[])
{
/* temp implementation allowing boolean to real conversion, until proper boolean support is added. */
size_t i;
for (i = 0; i < nvr; i++)
{
xx_MEMORY[vr[i]] = value[i] ? 1.0 : 0.0;
}
return fmi2OK;
}
FMI_Dll_Export fmi2Status fmi2SetString(fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr,
const fmi2String value[])
{
return fmi2Error; /* not yet */
}
fmi2Component fmi2Instantiate(fmi2String instanceName,
fmi2Type fmuType,
fmi2String fmuGUID,
fmi2String fmuResourceLocation,
const fmi2CallbackFunctions* functions,
fmi2Boolean visible,
fmi2Boolean loggingOn)
{
/* we should remember the functions pointer in order to make callback functions */
if (!functions)
{
return NULL; // we cannot even log this problem
}
if (!functions->logger)
{
return NULL; // we cannot even log this problem
}
if (!instanceName || strlen(instanceName)==0) {
functions->logger(NULL, "?", fmi2Error, "error",
"Missing instance name.");
return NULL;
}
else
{
/* Register the callback */
g_fmiCallbackFunctions = functions;
}
/* Check whether the given GUID equals our GUID */
if( strncmp(fmuGUID, FMI_GUID, strlen(fmuGUID)) != 0 )
{
g_fmiCallbackFunctions->logger(NULL, instanceName, fmi2Error, "error",
"Wrong GUID %s. Expected %s.", fmuGUID, FMI_GUID);
return NULL;
}
/* check if we are setup for co-simulation, that's the only possible option for now */
if( fmuType != fmi2CoSimulation )
{
g_fmiCallbackFunctions->logger(NULL, instanceName, fmi2Error, "error",
"FMU can only be used for Co-Simulation, not for Model Exchange");
return NULL;
}
/* only one static instance for now */
return (fmi2Component) xxComponent;
}
fmi2Status fmi2SetupExperiment(fmi2Component c,
fmi2Boolean toleranceDefined,
fmi2Real tolerance,
fmi2Real startTime,
fmi2Boolean stopTimeDefined,
fmi2Real stopTime)
{
/* copy the arguments */
xx_start_time = startTime;
if (stopTimeDefined == fmi2True)
{
xx_finish_time = stopTime;
}
/* initialize the submodel itself */
XXInitializeSubmodel (startTime);
/* all done */
return fmi2OK;
}
fmi2Status fmi2EnterInitializationMode(fmi2Component c)
{
/* nothing to do for now */
return fmi2OK;
}
fmi2Status fmi2ExitInitializationMode(fmi2Component c)
{
/* nothing to do for now */
return fmi2OK;
}
fmi2Status fmi2Terminate(fmi2Component c)
{
/* Perform the final calculations */
XXTerminateSubmodel (xx_time);
/* all done */
return fmi2OK;
}
fmi2Status fmi2Reset(fmi2Component c)
{
/* initialize the submodel itself */
XXInitializeSubmodel (xx_start_time);
/* all done */
return fmi2OK;
}
void fmi2FreeInstance(fmi2Component c)
{
/* only one static instance (done automatically) */
}
fmi2Status fmi2SetRealInputDerivatives(fmi2Component c,
const fmi2ValueReference vr[], size_t nvr,
const fmi2Integer order[],
const fmi2Real value[])
{
/* not yet */
return fmi2Error;
}
fmi2Status fmi2GetRealOutputDerivatives(fmi2Component c,
const fmi2ValueReference vr[],
size_t nvr,
const fmi2Integer order[],
fmi2Real value[])
{
/* not yet */
return fmi2Error;
}
fmi2Status fmi2CancelStep(fmi2Component c)
{
/* not yet */
return fmi2Error;
}
fmi2Status fmi2DoStep(fmi2Component c,
fmi2Real currentCommunicationPoint,
fmi2Real communicationStepSize,
fmi2Boolean noSetFMUStatePriorToCurrentPoint)
{
/* Treat also case of zero step, i.e. during an event iteration */
if (communicationStepSize == 0)
{
return fmi2OK;
}
/* as long as we are not passed our communication point */
while (xx_time < (currentCommunicationPoint + communicationStepSize))
{
/* check for termination first */
if ( (xx_finish_time > 0.0) && (xx_time > xx_finish_time) )
{
if(g_fmiCallbackFunctions != NULL && g_fmiCallbackFunctions->logger != NULL)
{
g_fmiCallbackFunctions->logger(NULL, "Body_Block", fmi2Error, "error",
"Exceeded model finish time: %g > %g\n", xx_time, xx_finish_time);
}
/* we're done */
return fmi2Error;
}
/* Call the submodel to calculate the output, and increase the time as well */
XXCalculateSubmodel (xx_time);
}
/* for now */
return fmi2OK;
}
fmi2Status fmi2GetStatus(fmi2Component c, const fmi2StatusKind s, fmi2Status* value)
{
/* all fine? */
return fmi2OK;
}
fmi2Status fmi2GetRealStatus(fmi2Component c, const fmi2StatusKind s, fmi2Real* value)
{
/* all fine? */
return fmi2OK;
}
fmi2Status fmi2GetIntegerStatus(fmi2Component c, const fmi2StatusKind s, fmi2Integer* value)
{
/* all fine? */
return fmi2OK;
}
fmi2Status fmi2GetBooleanStatus(fmi2Component c, const fmi2StatusKind s, fmi2Boolean* value)
{
/* all fine? */
return fmi2OK;
}
fmi2Status fmi2GetStringStatus(fmi2Component c, const fmi2StatusKind s, fmi2String* value)
{
/* not yet */
return fmi2Discard;
}
fmi2Status fmi2GetContinuousStates(fmi2Component c, fmi2Real x[], size_t nx)
{
size_t i;
if( 20 != nx )
{
return fmi2Error;
}
for( i = 0; i < 20; ++i)
{
x[i] = xx_s[i];
}
return fmi2OK;
}
fmi2Status fmi2GetFMUstate (fmi2Component c, fmi2FMUstate* FMUstate) {
/* not yet */
return fmi2Discard;
}
fmi2Status fmi2SetFMUstate (fmi2Component c, fmi2FMUstate FMUstate) {
/* not yet */
return fmi2Discard;
}
fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate* FMUstate) {
/* not yet */
return fmi2Discard;
}
fmi2Status fmi2SerializedFMUstateSize(fmi2Component c, fmi2FMUstate FMUstate, size_t *size) {
/* not yet */
return fmi2Discard;
}
fmi2Status fmi2SerializeFMUstate (fmi2Component c, fmi2FMUstate FMUstate, fmi2Byte serializedState[], size_t size) {
/* not yet */
return fmi2Discard;
}
fmi2Status fmi2DeSerializeFMUstate (fmi2Component c, const fmi2Byte serializedState[], size_t size,
fmi2FMUstate* FMUstate) {
/* not yet */
return fmi2Discard;
}
fmi2Status fmi2GetDirectionalDerivative(fmi2Component c, const fmi2ValueReference vUnknown_ref[], size_t nUnknown,
const fmi2ValueReference vKnown_ref[] , size_t nKnown,
const fmi2Real dvKnown[], fmi2Real dvUnknown[]) {
/* not yet */
return fmi2Discard;
}