Eclipse SUMO - Simulation of Urban MObility
fmi2Functions.c
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2020-2023 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
19// Implementation of the FMI2 interface functions
20/****************************************************************************/
21
22#ifdef _MSC_VER
23// Avoid warnings in windows build because of strcpy instead of strcpy_s,
24// because the latter is not available on all platforms
25#define _CRT_SECURE_NO_WARNINGS
26#pragma warning(disable:4820 4514 5045)
27#endif
28
29#include <string.h>
30#include <stdio.h>
31#include <stdarg.h>
33#include "sumo2fmi_bridge.h"
34#include "libsumocpp2c.h"
35
36/* Explicit definition of unused parameters to avoid compiler warnings */
37#define UNREFERENCED_PARAMETER(P) (P)
38
39/* **********************************************************************************************
40 * * IMPLEMENTATION OF GENERIC FUNCTIONALITY
41 * **********************************************************************************************/
42const char* fmi2GetTypesPlatform(void) {
43 return fmi2TypesPlatform;
44}
45
46const char* fmi2GetVersion(void) {
47 return fmi2Version;
48}
49
50/* ***********************************************************************************************
51 * CREATION AND DESTRUCTION OF AN FMU
52 ***********************************************************************************************/
53
54/* Define what should be logged - if logging is enabled globally */
56fmi2SetDebugLogging(fmi2Component c, fmi2Boolean loggingOn, size_t nCategories, const fmi2String categories[]) {
57
58 ModelInstance *comp = (ModelInstance *)c;
59
60 if (loggingOn) {
61 size_t i;
62 for (i = 0; i < nCategories; i++) {
63 if (categories[i] == NULL) {
64 sumo2fmi_logError(comp, "Log category[%d] must not be NULL", i);
65 return fmi2Error;
66 } else if (strcmp(categories[i], "logStatusError") == 0) {
67 comp->logErrors = true;
68 } else if (strcmp(categories[i], "logEvents") == 0) {
69 comp->logEvents = true;
70 } else {
71 sumo2fmi_logError(comp, "Log category[%d] must be one of logEvents or logStatusError but was %s", i, categories[i]);
72 return fmi2Error;
73 }
74 }
75 } else {
76 // Logging is disabled globally, no need for a more fine grained logging
77 comp->logEvents = false;
78 comp->logErrors = false;
79 }
80
81 return fmi2OK;
82}
83
84/* The function returns a new instance of an FMU. If a null pointer is returned, then instantiation
85 failed.*/
87fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2String fmuGUID,
88 fmi2String fmuResourceLocation, const fmi2CallbackFunctions *functions,
89 fmi2Boolean visible, fmi2Boolean loggingOn)
90{
94
95 allocateMemoryType funcAllocateMemory = (allocateMemoryType)functions->allocateMemory;
96 ModelInstance* comp = (ModelInstance *) funcAllocateMemory(1, sizeof(ModelInstance));
97
98 if (comp) {
99 comp->componentEnvironment = functions->componentEnvironment;
100
101 /* Callback functions for specific logging, malloc and free;
102 we need callback functions because we cannot know, which functions
103 the environment will provide for us */
104 comp->logger = (loggerType)functions->logger;
105 comp->allocateMemory = (allocateMemoryType)functions->allocateMemory;
106 comp->freeMemory = (freeMemoryType)functions->freeMemory;
107
108 comp->instanceName = (char *)comp->allocateMemory(1 + strlen(instanceName), sizeof(char));
109 strcpy((char *)comp->instanceName, (char *)instanceName);
110
111 if (fmuResourceLocation) {
112 comp->resourceLocation = (char *)comp->allocateMemory(1 + strlen(fmuResourceLocation), sizeof(char));
113 strcpy((char *)comp->resourceLocation, (char *)fmuResourceLocation);
114 } else {
115 comp->resourceLocation = NULL;
116 }
117
118 comp->logEvents = loggingOn;
119 comp->logErrors = true; // always log errors
120 }
121
122 return comp;
123}
124
125/* Disposes the given instance, unloads the loaded model, and frees all the allocated memory
126and other resources that have been allocated by the functions of the FMU interface. */
127void
129 ModelInstance *comp = (ModelInstance *)c;
130
131 /* Store the pointer to the freeMemory function, because we
132 are going to free comp as well */
133 freeMemoryType freeMemoryFunc = comp->freeMemory;
134
135 /* We want to free everything that we allocated in fmi2Instantiate */
136 freeMemoryFunc((void *)comp->instanceName);
137 freeMemoryFunc((void *)comp->resourceLocation);
138 freeMemoryFunc((void *)comp->libsumoCallOptions);
139 freeMemoryFunc((void *)comp->getterParameters);
140 int i;
141 for (i = 0; i < comp->bufferArrayLength; i++) {
142 freeMemoryFunc((void *)comp->bufferArray[i]);
143 }
144 freeMemoryFunc((void *)comp->bufferArray);
145 freeMemoryFunc((void *)comp);
146}
147
150 fmi2Real startTime, fmi2Boolean stopTimeDefined, fmi2Real stopTime) {
151
152 UNREFERENCED_PARAMETER(toleranceDefined);
153 UNREFERENCED_PARAMETER(tolerance);
154 UNREFERENCED_PARAMETER(stopTimeDefined);
155
156 // ignore arguments: toleranceDefined, tolerance
157 ModelInstance *comp = (ModelInstance *)c;
158
159 // Store the start and stop times of the experiment
160 comp->startTime = startTime;
161 comp->stopTime = stopTime;
162
164
165 return fmi2OK;
166}
167
168// Will be called after instantiation and after initial variables have been set
172
173 return fmi2OK;
174}
175
176// Informs the FMU to exit Initialization Mode
179 ModelInstance *comp = (ModelInstance *)c;
180
181 sumo2fmi_logEvent(comp, "Calling libsumo with the following options: \"%s\"", comp->libsumoCallOptions);
183
184 return fmi2OK;
185}
186
187// Informs the FMU that the simulation run is terminated
188// --> let libsumo know, that we want to close the simulation
192
194 return fmi2OK;
195}
196
197// Is called by the environment to reset the FMU after a simulation run
201
202 // Should we set some start values?
203 return fmi2OK;
204}
205
206// Implementation of the getter features
208fmi2GetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Real value[]) {
213
214 return fmi2Error;
215}
216
218fmi2GetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[]) {
219
220 ModelInstance *comp = (ModelInstance *)c;
221
222 // Check for null pointer errors
223 if (nvr > 0 && (!vr || !value)) {
224 return fmi2Error;
225 }
226
227 fmi2Status status = fmi2OK;
228
229 // Go through the list of arrays and save all requested values
230 size_t i;
231 for (i = 0; i < nvr; i++) {
232 fmi2Status s = sumo2fmi_getInteger(comp, vr[i], &(value[i]));
233 status = s > status ? s : status;
234
235 if (status > fmi2Warning) {
236 return status;
237 }
238 }
239
240 return status;
241}
242
244fmi2GetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[]) {
249
250 return fmi2Error;
251}
252
254fmi2GetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2String value[]) {
255
256 ModelInstance *comp = (ModelInstance *)c;
257
258 // Check for null pointer errors
259 if (nvr > 0 && (!vr || !value)) {
260 return fmi2Error;
261 }
262
263 fmi2Status status = fmi2OK;
264
266 int b;
267 for (b = 0; b < comp->bufferArrayLength; b++) {
268 comp->freeMemory((void *)comp->bufferArray[b]);
269 }
270 comp->freeMemory((void *)comp->bufferArray);
271 comp->bufferArray = (fmi2String *)comp->allocateMemory(nvr, sizeof(fmi2String));
272 comp->bufferArrayLength = (int)nvr;
273
274 // Go through the list of arrays and save all requested values
275 size_t i;
276 for (i = 0; i < nvr; i++) {
277 fmi2Status s = sumo2fmi_getString(comp, vr[i], &(comp->bufferArray[i]));
278 value[i] = comp->bufferArray[i];
279 if (value[i] == NULL) {
280 s = fmi2Error;
281 }
282
283 status = s > status ? s : status;
284 if (status > fmi2Warning) {
285 return status;
286 }
287 }
288
289 return status;
290}
291
292// Implementation of the setter features
294fmi2SetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[]) {
299 return fmi2Error;
300}
301
303fmi2SetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer value[]) {
308
309 return fmi2Error;
310}
311
313fmi2SetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Boolean value[]) {
318
319 return fmi2Error;
320}
321
323fmi2SetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2String value[]) {
324
325 ModelInstance *comp = (ModelInstance *)c;
326 fmi2Status status = fmi2OK;
327
328 size_t i;
329 for (i = 0; i < nvr; i++) {
330 fmi2Status s = sumo2fmi_setString(comp, vr[i], value[i]);
331 status = s > status ? s : status;
332 if (status > fmi2Warning) return status;
333 }
334
335 return status;
336}
337
341 UNREFERENCED_PARAMETER(FMUstate);
342 return fmi2Error; /* Dummy implementation */
343}
344
348 UNREFERENCED_PARAMETER(FMUstate);
349 return fmi2Error; /* Dummy implementation */
350}
351
355 UNREFERENCED_PARAMETER(FMUstate);
356 return fmi2Error; /* Dummy implementation */
357}
358
362 UNREFERENCED_PARAMETER(FMUstate);
364 return fmi2Error; /* Dummy implementation */
365}
366
368fmi2SerializeFMUstate(fmi2Component c, fmi2FMUstate FMUstate, fmi2Byte state[], size_t size) {
370 UNREFERENCED_PARAMETER(FMUstate);
373 return fmi2Error; /* Dummy implementation */
374}
375
377fmi2DeSerializeFMUstate(fmi2Component c, const fmi2Byte serializedState[], size_t size, fmi2FMUstate* FMUstate) {
379 UNREFERENCED_PARAMETER(serializedState);
381 UNREFERENCED_PARAMETER(FMUstate);
382 return fmi2Error; /* Dummy implementation */
383}
384
386fmi2GetDirectionalDerivative(fmi2Component c, const fmi2ValueReference vUnknown_ref[], size_t nUnknown,
387 const fmi2ValueReference vKnown_ref[], size_t nKnown, const fmi2Real dvKnown[], fmi2Real dvUnknown[]) {
389 UNREFERENCED_PARAMETER(vUnknown_ref);
390 UNREFERENCED_PARAMETER(nUnknown);
391 UNREFERENCED_PARAMETER(vKnown_ref);
393 UNREFERENCED_PARAMETER(dvKnown);
394 UNREFERENCED_PARAMETER(dvUnknown);
395 return fmi2Error; /* Dummy implementation */
396}
397
398/* Further functions for interpolation */
400fmi2SetRealInputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer order[], const fmi2Real value[]) {
406
407 return fmi2Error; /* Ignoring - SUMO cannot interpolate inputs */
408}
409
411fmi2GetRealOutputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer order[], fmi2Real value[]) {
415
416 size_t i;
417 for (i = 0; i < nvr; i++) {
418 value[i] = 0; /* We cannot compute derivatives of outputs */
419 }
420 return fmi2Error;
421}
422
423/* Stepping */
425fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPoint) {
426 UNREFERENCED_PARAMETER(noSetFMUStatePriorToCurrentPoint);
427
428 ModelInstance *comp = (ModelInstance *)c;
429
430 if (communicationStepSize <= 0) {
431 return fmi2Error;
432 }
433
434 return sumo2fmi_step(comp, currentCommunicationPoint + communicationStepSize);
435}
436
440
441 return fmi2Error; /* We will never have a modelStepInProgress state */
442}
443
444/* Status functions */
450
451 return fmi2Discard;
452}
453
459
460 return fmi2Discard;
461}
462
468
469 return fmi2Discard;
470}
471
477
478 return fmi2Discard;
479}
480
486
487 return fmi2Discard;
488}
fmi2Status
@ fmi2OK
@ fmi2Error
@ fmi2Discard
@ fmi2Warning
fmi2StatusKind
fmi2Status fmi2SetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer value[])
fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate FMUstate)
fmi2Status fmi2EnterInitializationMode(fmi2Component c)
fmi2Status fmi2GetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[])
fmi2Status fmi2GetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[])
fmi2Status fmi2GetIntegerStatus(fmi2Component c, const fmi2StatusKind s, fmi2Integer *value)
fmi2Status fmi2SerializedFMUstateSize(fmi2Component c, fmi2FMUstate FMUstate, size_t *size)
fmi2Status fmi2GetBooleanStatus(fmi2Component c, const fmi2StatusKind s, fmi2Boolean *value)
fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate *FMUstate)
fmi2Status fmi2CancelStep(fmi2Component c)
fmi2Status fmi2Reset(fmi2Component c)
fmi2Status fmi2SetupExperiment(fmi2Component c, fmi2Boolean toleranceDefined, fmi2Real tolerance, fmi2Real startTime, fmi2Boolean stopTimeDefined, fmi2Real stopTime)
fmi2Status fmi2GetRealOutputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer order[], fmi2Real value[])
fmi2Status fmi2SetRealInputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer order[], const fmi2Real value[])
fmi2Status fmi2GetStatus(fmi2Component c, const fmi2StatusKind s, fmi2Status *value)
fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2String fmuGUID, fmi2String fmuResourceLocation, const fmi2CallbackFunctions *functions, fmi2Boolean visible, fmi2Boolean loggingOn)
Definition: fmi2Functions.c:87
fmi2Status fmi2SetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[])
fmi2Status fmi2ExitInitializationMode(fmi2Component c)
fmi2Status fmi2SetDebugLogging(fmi2Component c, fmi2Boolean loggingOn, size_t nCategories, const fmi2String categories[])
Definition: fmi2Functions.c:56
fmi2Status fmi2SerializeFMUstate(fmi2Component c, fmi2FMUstate FMUstate, fmi2Byte state[], size_t size)
fmi2Status fmi2SetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2String value[])
fmi2Status fmi2SetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Boolean value[])
fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate *FMUstate)
fmi2Status fmi2GetStringStatus(fmi2Component c, const fmi2StatusKind s, fmi2String *value)
#define UNREFERENCED_PARAMETER(P)
Definition: fmi2Functions.c:37
fmi2Status fmi2GetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Real value[])
fmi2Status fmi2GetDirectionalDerivative(fmi2Component c, const fmi2ValueReference vUnknown_ref[], size_t nUnknown, const fmi2ValueReference vKnown_ref[], size_t nKnown, const fmi2Real dvKnown[], fmi2Real dvUnknown[])
void fmi2FreeInstance(fmi2Component c)
fmi2Status fmi2GetRealStatus(fmi2Component c, const fmi2StatusKind s, fmi2Real *value)
fmi2Status fmi2DeSerializeFMUstate(fmi2Component c, const fmi2Byte serializedState[], size_t size, fmi2FMUstate *FMUstate)
fmi2Status fmi2GetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2String value[])
fmi2Status fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPoint)
const char * fmi2GetVersion(void)
Definition: fmi2Functions.c:46
fmi2Status fmi2Terminate(fmi2Component c)
const char * fmi2GetTypesPlatform(void)
Definition: fmi2Functions.c:42
#define fmi2Version
int fmi2Integer
#define fmi2TypesPlatform
void * fmi2FMUstate
double fmi2Real
unsigned int fmi2ValueReference
char fmi2Byte
void * fmi2Component
int fmi2Boolean
const fmi2Char * fmi2String
void libsumo_load(char *callOptions)
void libsumo_close(void)
char * getterParameters
Parameters stored for the next (libsumo) getter call. Workaround for FMIv2 not allowing input values ...
const char * resourceLocation
fmi2String * bufferArray
allocateMemoryType allocateMemory
const char * instanceName
freeMemoryType freeMemory
char * libsumoCallOptions
fmi2CallbackAllocateMemory allocateMemory
fmi2CallbackLogger logger
fmi2CallbackFreeMemory freeMemory
fmi2ComponentEnvironment componentEnvironment
fmi2Status sumo2fmi_getString(ModelInstance *comp, const fmi2ValueReference vr, fmi2String *value)
void sumo2fmi_set_startValues(ModelInstance *comp)
fmi2Status sumo2fmi_step(ModelInstance *comp, double tNext)
fmi2Status sumo2fmi_getInteger(ModelInstance *comp, const fmi2ValueReference vr, int *value)
void sumo2fmi_logError(ModelInstance *comp, const char *message,...)
fmi2Status sumo2fmi_setString(ModelInstance *comp, fmi2ValueReference vr, fmi2String value)
void sumo2fmi_logEvent(ModelInstance *comp, const char *message,...)
void *(* allocateMemoryType)(size_t nobj, size_t size)
void(* freeMemoryType)(void *obj)
void(* loggerType)(void *componentEnvironment, const char *instanceName, int status, const char *category, const char *message,...)