/
fmu.cpp
376 lines (308 loc) · 9.74 KB
/
fmu.cpp
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
/*
* fmi.cpp
*
* Created on: Aug 21, 2015
* Author: parallels
*/
#include<stdio.h>
/*
* fmi2Functions.c
*
* Created on: May 22, 2015
* Author: kel
*/
#include "fmi2Functions.h"
#include <string.h>
//#include <map>
//static std::map <int, ExternalClient> clients;
#include <vector>
#include <sstream>
#include <cmath>
const fmi2CallbackFunctions *g_functions;
std::string* name;
static int integers[4];
#define ID_NUMBER1 0
#define ID_NUMBER2 1
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
template <class T>
static void log(const fmi2CallbackFunctions *functions, fmi2ComponentEnvironment componentEnvironment,
fmi2String instanceName, fmi2Status status, fmi2String category, fmi2String message,T arg)
{
if (functions != NULL && functions->logger != NULL)
{
functions->logger(componentEnvironment, instanceName, status, category, message,arg);
}
}
static void notimplemented(fmi2Component c, fmi2String message)
{
std::string base("Not implemented: %s");
std::string m(message);
if (g_functions != NULL)
{
log(g_functions, (void*) 2, "", fmi2Error, "error", (base + m).c_str(),"");
}
}
//static void message(fmi2String message)
//{
// std::string m(message);
// if (g_functions != NULL)
// {
// log(g_functions, (void*) 1, "Info", fmi2OK, "Message: %s", m.c_str());
// }
//}
template <class T>
static void fmiprintf(fmi2String message,T arg)
{
if (g_functions != NULL)
{
log(g_functions, (void*) 2, name->c_str(), fmi2OK, "logAll",message, arg);
}
}
// ---------------------------------------------------------------------------
// FMI functions
// ---------------------------------------------------------------------------
extern "C" fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2String fmuGUID,
fmi2String fmuResourceLocation, const fmi2CallbackFunctions *functions, fmi2Boolean visible,
fmi2Boolean loggingOn)
{
integers[ID_NUMBER2]=1000000000;
name = new std::string (instanceName);
g_functions = functions;
fmiprintf("instantiating rollback-test %s\n","");
return (void*) 2;
}
extern "C" fmi2Status fmi2SetupExperiment(fmi2Component c, fmi2Boolean toleranceDefined, fmi2Real tolerance,
fmi2Real startTime, fmi2Boolean stopTimeDefined, fmi2Real stopTime)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2EnterInitializationMode(fmi2Component c)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2ExitInitializationMode(fmi2Component c)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2Terminate(fmi2Component c)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2Reset(fmi2Component c)
{
return fmi2OK;
}
extern "C" void fmi2FreeInstance(fmi2Component c)
{
}
// ---------------------------------------------------------------------------
// FMI functions: class methods not depending of a specific model instance
// ---------------------------------------------------------------------------
extern "C" const char* fmi2GetVersion()
{
return fmi2Version;
}
extern "C" const char* fmi2GetTypesPlatform()
{
return fmi2TypesPlatform;
}
// ---------------------------------------------------------------------------
// FMI functions: logging control, setters and getters for Real, Integer,
// Boolean, String
// ---------------------------------------------------------------------------
extern "C" fmi2Status fmi2SetDebugLogging(fmi2Component c, fmi2Boolean loggingOn, size_t nCategories,
const fmi2String categories[])
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Real value[])
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[])
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[])
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2String value[])
{
return fmi2OK;
}
extern "C" fmi2Status fmi2SetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[])
{
return fmi2OK;
}
extern "C" fmi2Status fmi2SetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
const fmi2Integer value[])
{
printf("setInteger\n");
for(int i; i < nvr ; i++)
{
printf("Setting vr=%d, i=%d, val=%d\n",vr[i],i,value[i]);
integers[vr[i]] = value[i];
}
return fmi2OK;
}
extern "C" fmi2Status fmi2SetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
const fmi2Boolean value[])
{
return fmi2OK;
}
extern "C" fmi2Status fmi2SetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
const fmi2String value[])
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate* FMUstate)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate FMUstate)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate* FMUstate)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2SerializedFMUstateSize(fmi2Component c, fmi2FMUstate FMUstate, size_t *size)
{
notimplemented(c, "fmi2SerializedFMUstateSize");
return fmi2Error;
}
extern "C" fmi2Status fmi2SerializeFMUstate(fmi2Component c, fmi2FMUstate FMUstate, fmi2Byte serializedState[],
size_t size)
{
notimplemented(c, "fmi2SerializeFMUstate");
return fmi2Error;
}
extern "C" fmi2Status fmi2DeSerializeFMUstate(fmi2Component c, const fmi2Byte serializedState[], size_t size,
fmi2FMUstate* FMUstate)
{
notimplemented(c, "fmi2DeSerializeFMUstate");
return fmi2Error;
}
extern "C" fmi2Status fmi2GetDirectionalDerivative(fmi2Component c, const fmi2ValueReference vUnknown_ref[],
size_t nUnknown, const fmi2ValueReference vKnown_ref[], size_t nKnown, const fmi2Real dvKnown[],
fmi2Real dvUnknown[])
{
notimplemented(c, "fmi2GetDirectionalDerivative");
return fmi2Error;
}
// ---------------------------------------------------------------------------
// Functions for FMI for Co-Simulation
// ---------------------------------------------------------------------------
#ifdef FMI_COSIMULATION
/* Simulating the slave */
extern "C" fmi2Status fmi2SetRealInputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
const fmi2Integer order[], const fmi2Real value[])
{
notimplemented(c, "fmi2SetRealInputDerivatives");
return fmi2Error;
}
extern "C" fmi2Status fmi2GetRealOutputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
const fmi2Integer order[], fmi2Real value[])
{
notimplemented(c, "fmi2GetRealOutputDerivatives");
return fmi2Error;
}
extern "C" fmi2Status fmi2CancelStep(fmi2Component c)
{
notimplemented(c, "fmi2CancelStep");
return fmi2Error;
}
float epsilon = 0.000000001;
extern "C" fmi2Status fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint, fmi2Real communicationStepSize,
fmi2Boolean noSetFMUStatePriorToCurrentPoint)
{
long number1, number2, counter;
double result;
number1=integers[ID_NUMBER1];
number2=integers[ID_NUMBER2];
if (g_functions != NULL && g_functions->logger != NULL)
{
// g_functions->logger((void*) 2, name->c_str(), fmi2OK, "logAll","doStep from %d , to %d %s\n", number1, number2, "");
}
for (counter = number1; counter <= number2; counter++) {
result = result + sqrt((double)counter);
}
return fmi2OK;
}
extern "C" fmi2Status fmi2GetStatus(fmi2Component c, const fmi2StatusKind s, fmi2Status *value)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetRealStatus(fmi2Component c, const fmi2StatusKind s, fmi2Real *value)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetIntegerStatus(fmi2Component c, const fmi2StatusKind s, fmi2Integer *value)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetBooleanStatus(fmi2Component c, const fmi2StatusKind s, fmi2Boolean *value)
{
return fmi2OK;
}
extern "C" fmi2Status fmi2GetStringStatus(fmi2Component c, const fmi2StatusKind s, fmi2String *value)
{
return fmi2OK;
}
/* INTO cps specific*/
/*extern "C" fmi2Status fmi2GetMaxStepsize(fmi2Component c, fmi2Real* size)
{
return fmi2OK;
}*/
// ---------------------------------------------------------------------------
// Functions for FMI2 for Model Exchange
// ---------------------------------------------------------------------------
#else
/* Enter and exit the different modes */
fmi2Status fmi2EnterEventMode(fmi2Component c)
{
return fmi2Error;
}
fmi2Status fmi2NewDiscreteStates(fmi2Component c, fmi2EventInfo *eventInfo)
{
return fmi2Error;
}
fmi2Status fmi2EnterContinuousTimeMode(fmi2Component c)
{
return fmi2Error;
}
fmi2Status fmi2CompletedIntegratorStep(fmi2Component c, fmi2Boolean noSetFMUStatePriorToCurrentPoint,
fmi2Boolean *enterEventMode, fmi2Boolean *terminateSimulation)
{
return fmi2Error;
}
/* Providing independent variables and re-initialization of caching */
fmi2Status fmi2SetTime(fmi2Component c, fmi2Real time)
{
return fmi2Error;
}
fmi2Status fmi2SetContinuousStates(fmi2Component c, const fmi2Real x[], size_t nx)
{
return fmi2Error;
}
/* Evaluation of the model equations */
fmi2Status fmi2GetDerivatives(fmi2Component c, fmi2Real derivatives[], size_t nx)
{
return fmi2Error;
}
fmi2Status fmi2GetEventIndicators(fmi2Component c, fmi2Real eventIndicators[], size_t ni)
{
return fmi2Error;
}
fmi2Status fmi2GetContinuousStates(fmi2Component c, fmi2Real states[], size_t nx)
{
return fmi2Error;
}
fmi2Status fmi2GetNominalsOfContinuousStates(fmi2Component c, fmi2Real x_nominal[], size_t nx)
{
return fmi2Error;
}
#endif // Model Exchange