forked from precice/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmi2Functions.c
923 lines (681 loc) · 26.1 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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
#if FMI_VERSION != 2
#error FMI_VERSION must be 2
#endif
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "cosimulation.h"
#include "model.h"
// C-code FMUs have functions names prefixed with MODEL_IDENTIFIER_.
// Define DISABLE_PREFIX to build a binary FMU.
#ifndef DISABLE_PREFIX
#define pasteA(a, b) a##b
#define pasteB(a, b) pasteA(a, b)
#define FMI2_FUNCTION_PREFIX pasteB(MODEL_IDENTIFIER, _)
#endif
#include "fmi2Functions.h"
#define ASSERT_NOT_NULL(p) \
do { \
if (!p) { \
logError(S, "Argument %s must not be NULL.", xstr(p)); \
S->state = modelError; \
return (fmi2Status) Error; \
} \
} while (0)
#define GET_VARIABLES(T) \
do { \
Status status = OK; \
if (nvr == 0) \
return (fmi2Status) status; \
ASSERT_NOT_NULL(vr); \
ASSERT_NOT_NULL(value); \
size_t index = 0; \
if (S->isDirtyValues) { \
Status s = calculateValues(S); \
status = max(status, s); \
if (status > Warning) \
return (fmi2Status) status; \
S->isDirtyValues = false; \
} \
for (size_t i = 0; i < nvr; i++) { \
Status s = get##T(S, vr[i], value, &index); \
status = max(status, s); \
if (status > Warning) \
return (fmi2Status) status; \
} \
return (fmi2Status) status; \
} while (0)
#define SET_VARIABLES(T) \
do { \
Status status = OK; \
if (nvr == 0) \
return (fmi2Status) status; \
ASSERT_NOT_NULL(vr); \
ASSERT_NOT_NULL(value); \
size_t index = 0; \
for (size_t i = 0; i < nvr; i++) { \
Status s = set##T(S, vr[i], value, &index); \
status = max(status, s); \
if (status > Warning) \
return (fmi2Status) status; \
} \
if (nvr > 0) \
S->isDirtyValues = true; \
return (fmi2Status) status; \
} while (0)
#define GET_BOOLEAN_VARIABLES \
do { \
Status status = OK; \
for (size_t i = 0; i < nvr; i++) { \
bool v = false; \
size_t index = 0; \
Status s = getBoolean(S, vr[i], &v, &index); \
value[i] = v; \
status = max(status, s); \
if (status > Warning) \
return (fmi2Status) status; \
} \
return (fmi2Status) status; \
} while (0)
#define SET_BOOLEAN_VARIABLES \
do { \
Status status = OK; \
for (size_t i = 0; i < nvr; i++) { \
bool v = value[i]; \
size_t index = 0; \
Status s = setBoolean(S, vr[i], &v, &index); \
status = max(status, s); \
if (status > Warning) \
return (fmi2Status) status; \
} \
return (fmi2Status) status; \
} while (0)
#ifndef max
#define max(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef DT_EVENT_DETECT
#define DT_EVENT_DETECT 1e-10
#endif
// ---------------------------------------------------------------------------
// Function calls allowed state masks for both Model-exchange and Co-simulation
// ---------------------------------------------------------------------------
#define MASK_fmi2GetTypesPlatform (StartAndEnd | Instantiated | InitializationMode | EventMode | ContinuousTimeMode | StepComplete | StepInProgress | StepFailed | StepCanceled | Terminated | Error)
#define MASK_fmi2GetVersion MASK_fmi2GetTypesPlatform
#define MASK_fmi2SetDebugLogging (Instantiated | InitializationMode | EventMode | ContinuousTimeMode | StepComplete | StepInProgress | StepFailed | StepCanceled | Terminated | Error)
#define MASK_fmi2Instantiate (StartAndEnd)
#define MASK_fmi2FreeInstance (Instantiated | InitializationMode | EventMode | ContinuousTimeMode | StepComplete | StepFailed | StepCanceled | Terminated | Error)
#define MASK_fmi2SetupExperiment Instantiated
#define MASK_fmi2EnterInitializationMode Instantiated
#define MASK_fmi2ExitInitializationMode InitializationMode
#define MASK_fmi2Terminate (EventMode | ContinuousTimeMode | StepComplete | StepFailed)
#define MASK_fmi2Reset MASK_fmi2FreeInstance
#define MASK_fmi2GetReal (InitializationMode | EventMode | ContinuousTimeMode | StepComplete | StepFailed | StepCanceled | Terminated | Error)
#define MASK_fmi2GetInteger MASK_fmi2GetReal
#define MASK_fmi2GetBoolean MASK_fmi2GetReal
#define MASK_fmi2GetString MASK_fmi2GetReal
#define MASK_fmi2SetReal (Instantiated | InitializationMode | EventMode | ContinuousTimeMode | StepComplete)
#define MASK_fmi2SetInteger (Instantiated | InitializationMode | EventMode | StepComplete)
#define MASK_fmi2SetBoolean MASK_fmi2SetInteger
#define MASK_fmi2SetString MASK_fmi2SetInteger
#define MASK_fmi2GetFMUstate MASK_fmi2FreeInstance
#define MASK_fmi2SetFMUstate MASK_fmi2FreeInstance
#define MASK_fmi2FreeFMUstate MASK_fmi2FreeInstance
#define MASK_fmi2SerializedFMUstateSize MASK_fmi2FreeInstance
#define MASK_fmi2SerializeFMUstate MASK_fmi2FreeInstance
#define MASK_fmi2DeSerializeFMUstate MASK_fmi2FreeInstance
#define MASK_fmi2GetDirectionalDerivative (InitializationMode | EventMode | ContinuousTimeMode | StepComplete | StepFailed | StepCanceled | Terminated | Error)
// ---------------------------------------------------------------------------
// Function calls allowed state masks for Model-exchange
// ---------------------------------------------------------------------------
#define MASK_fmi2EnterEventMode (EventMode | ContinuousTimeMode)
#define MASK_fmi2NewDiscreteStates EventMode
#define MASK_fmi2EnterContinuousTimeMode EventMode
#define MASK_fmi2CompletedIntegratorStep ContinuousTimeMode
#define MASK_fmi2SetTime (EventMode | ContinuousTimeMode)
#define MASK_fmi2SetContinuousStates ContinuousTimeMode
#define MASK_fmi2GetEventIndicators (InitializationMode | EventMode | ContinuousTimeMode | Terminated | Error)
#define MASK_fmi2GetContinuousStates MASK_fmi2GetEventIndicators
#define MASK_fmi2GetDerivatives (EventMode | ContinuousTimeMode | Terminated | Error)
#define MASK_fmi2GetNominalsOfContinuousStates (Instantiated | EventMode | ContinuousTimeMode | Terminated | Error)
// ---------------------------------------------------------------------------
// Function calls allowed state masks for Co-simulation
// ---------------------------------------------------------------------------
#define MASK_fmi2SetRealInputDerivatives (Instantiated | InitializationMode | StepComplete)
#define MASK_fmi2GetRealOutputDerivatives (StepComplete | StepFailed | StepCanceled | Terminated | Error)
#define MASK_fmi2DoStep StepComplete
#define MASK_fmi2CancelStep StepInProgress
#define MASK_fmi2GetStatus (StepComplete | StepInProgress | StepFailed | Terminated)
#define MASK_fmi2GetRealStatus MASK_fmi2GetStatus
#define MASK_fmi2GetIntegerStatus MASK_fmi2GetStatus
#define MASK_fmi2GetBooleanStatus MASK_fmi2GetStatus
#define MASK_fmi2GetStringStatus MASK_fmi2GetStatus
// shorthand to access the instance
#define S ((ModelInstance *) c)
#define ASSERT_STATE(S) \
if (!allowedState(c, MASK_fmi2##S, #S)) \
return fmi2Error;
static bool allowedState(ModelInstance *instance, int statesExpected, char *name)
{
if (!instance) {
return false;
}
if (!(instance->state & statesExpected)) {
logError(instance, "fmi2%s: Illegal call sequence.", name);
return false;
}
return true;
}
// ---------------------------------------------------------------------------
// FMI functions
// ---------------------------------------------------------------------------
fmi2Component fmi2Instantiate(fmi2String instanceName, fmi2Type fmuType, fmi2String fmuGUID,
fmi2String fmuResourceLocation, const fmi2CallbackFunctions *functions,
fmi2Boolean visible, fmi2Boolean loggingOn)
{
UNUSED(visible);
if (!functions || !functions->logger) {
return NULL;
}
return createModelInstance(
(loggerType) functions->logger,
NULL,
functions->componentEnvironment,
instanceName,
fmuGUID,
fmuResourceLocation,
loggingOn,
(InterfaceType) fmuType);
}
fmi2Status fmi2SetupExperiment(fmi2Component c, fmi2Boolean toleranceDefined, fmi2Real tolerance,
fmi2Real startTime, fmi2Boolean stopTimeDefined, fmi2Real stopTime)
{
UNUSED(toleranceDefined);
UNUSED(tolerance);
UNUSED(stopTimeDefined);
UNUSED(stopTime);
ASSERT_STATE(SetupExperiment)
S->startTime = startTime;
S->time = startTime;
return fmi2OK;
}
fmi2Status fmi2EnterInitializationMode(fmi2Component c)
{
ASSERT_STATE(EnterInitializationMode)
S->state = InitializationMode;
return fmi2OK;
}
fmi2Status fmi2ExitInitializationMode(fmi2Component c)
{
ASSERT_STATE(ExitInitializationMode);
fmi2Status status = fmi2OK;
// if values were set and no fmi2GetXXX triggered update before,
// ensure calculated values are updated now
if (S->isDirtyValues) {
status = (fmi2Status) calculateValues(S);
S->isDirtyValues = false;
}
if (S->type == ModelExchange) {
S->state = EventMode;
} else {
S->state = StepComplete;
}
return status;
}
fmi2Status fmi2Terminate(fmi2Component c)
{
ASSERT_STATE(Terminate)
S->state = Terminated;
return fmi2OK;
}
fmi2Status fmi2Reset(fmi2Component c)
{
ASSERT_STATE(Reset);
reset(S);
return fmi2OK;
}
void fmi2FreeInstance(fmi2Component c)
{
if (S) {
freeModelInstance(S);
}
}
// ---------------------------------------------------------------------------
// FMI functions: class methods not depending of a specific model instance
// ---------------------------------------------------------------------------
const char *fmi2GetVersion()
{
return fmi2Version;
}
const char *fmi2GetTypesPlatform()
{
return fmi2TypesPlatform;
}
// ---------------------------------------------------------------------------
// FMI functions: logging control, setters and getters for Real, Integer,
// Boolean, String
// ---------------------------------------------------------------------------
fmi2Status fmi2SetDebugLogging(fmi2Component c, fmi2Boolean loggingOn, size_t nCategories, const fmi2String categories[])
{
ASSERT_STATE(SetDebugLogging)
return (fmi2Status) setDebugLogging(S, loggingOn, nCategories, categories);
}
fmi2Status fmi2GetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Real value[])
{
ASSERT_STATE(GetReal)
if (nvr > 0 && nullPointer(S, "fmi2GetReal", "vr[]", vr))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2GetReal", "value[]", value))
return fmi2Error;
if (nvr > 0 && S->isDirtyValues) {
calculateValues(S);
S->isDirtyValues = false;
}
GET_VARIABLES(Float64);
}
fmi2Status fmi2GetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Integer value[])
{
ASSERT_STATE(GetInteger)
if (nvr > 0 && nullPointer(S, "fmi2GetInteger", "vr[]", vr))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2GetInteger", "value[]", value))
return fmi2Error;
if (nvr > 0 && S->isDirtyValues) {
calculateValues(S);
S->isDirtyValues = false;
}
GET_VARIABLES(Int32);
}
fmi2Status fmi2GetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2Boolean value[])
{
ASSERT_STATE(GetBoolean)
if (nvr > 0 && nullPointer(S, "fmi2GetBoolean", "vr[]", vr))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2GetBoolean", "value[]", value))
return fmi2Error;
if (nvr > 0 && S->isDirtyValues) {
calculateValues(S);
S->isDirtyValues = false;
}
GET_BOOLEAN_VARIABLES;
}
fmi2Status fmi2GetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, fmi2String value[])
{
ASSERT_STATE(GetString)
if (nvr > 0 && nullPointer(S, "fmi2GetString", "vr[]", vr))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2GetString", "value[]", value))
return fmi2Error;
if (nvr > 0 && S->isDirtyValues) {
calculateValues(S);
S->isDirtyValues = false;
}
GET_VARIABLES(String);
}
fmi2Status fmi2SetReal(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Real value[])
{
ASSERT_STATE(SetReal)
if (invalidState(S, "fmi2SetReal", MASK_fmi2SetReal))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2SetReal", "vr[]", vr))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2SetReal", "value[]", value))
return fmi2Error;
SET_VARIABLES(Float64);
}
fmi2Status fmi2SetInteger(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Integer value[])
{
ASSERT_STATE(SetInteger)
if (nvr > 0 && nullPointer(S, "fmi2SetInteger", "vr[]", vr))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2SetInteger", "value[]", value))
return fmi2Error;
SET_VARIABLES(Int32);
}
fmi2Status fmi2SetBoolean(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2Boolean value[])
{
ASSERT_STATE(SetBoolean)
if (nvr > 0 && nullPointer(S, "fmi2SetBoolean", "vr[]", vr))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2SetBoolean", "value[]", value))
return fmi2Error;
SET_BOOLEAN_VARIABLES;
}
fmi2Status fmi2SetString(fmi2Component c, const fmi2ValueReference vr[], size_t nvr, const fmi2String value[])
{
ASSERT_STATE(SetString);
if (nvr > 0 && nullPointer(S, "fmi2SetString", "vr[]", vr))
return fmi2Error;
if (nvr > 0 && nullPointer(S, "fmi2SetString", "value[]", value))
return fmi2Error;
SET_VARIABLES(String);
}
fmi2Status fmi2GetFMUstate(fmi2Component c, fmi2FMUstate *FMUstate)
{
ASSERT_STATE(GetFMUstate);
*FMUstate = getFMUState(S);
return fmi2OK;
}
fmi2Status fmi2SetFMUstate(fmi2Component c, fmi2FMUstate FMUstate)
{
ASSERT_STATE(SetFMUstate);
if (nullPointer(S, "fmi2SetFMUstate", "FMUstate", FMUstate)) {
return fmi2Error;
}
setFMUState(S, FMUstate);
return fmi2OK;
}
fmi2Status fmi2FreeFMUstate(fmi2Component c, fmi2FMUstate *FMUstate)
{
ASSERT_STATE(FreeFMUstate);
free(*FMUstate);
*FMUstate = NULL;
return fmi2OK;
}
fmi2Status fmi2SerializedFMUstateSize(fmi2Component c, fmi2FMUstate FMUstate, size_t *size)
{
UNUSED(c);
UNUSED(FMUstate);
ASSERT_STATE(SerializedFMUstateSize);
*size = sizeof(ModelInstance);
return fmi2OK;
}
fmi2Status fmi2SerializeFMUstate(fmi2Component c, fmi2FMUstate FMUstate, fmi2Byte serializedState[], size_t size)
{
ASSERT_STATE(SerializeFMUstate);
if (nullPointer(S, "fmi2SerializeFMUstate", "FMUstate", FMUstate)) {
return fmi2Error;
}
if (invalidNumber(S, "fmi2SerializeFMUstate", "size", size, sizeof(ModelInstance))) {
return fmi2Error;
}
memcpy(serializedState, FMUstate, sizeof(ModelInstance));
return fmi2OK;
}
fmi2Status fmi2DeSerializeFMUstate(fmi2Component c, const fmi2Byte serializedState[], size_t size, fmi2FMUstate *FMUstate)
{
ASSERT_STATE(DeSerializeFMUstate);
if (invalidNumber(S, "fmi2DeSerializeFMUstate", "size", size, sizeof(ModelInstance))) {
return fmi2Error;
}
if (*FMUstate == NULL) {
*FMUstate = calloc(1, sizeof(ModelInstance));
}
memcpy(*FMUstate, serializedState, sizeof(ModelInstance));
return fmi2OK;
}
fmi2Status fmi2GetDirectionalDerivative(fmi2Component c, const fmi2ValueReference vUnknown_ref[], size_t nUnknown,
const fmi2ValueReference vKnown_ref[], size_t nKnown,
const fmi2Real dvKnown[], fmi2Real dvUnknown[])
{
ASSERT_STATE(GetDirectionalDerivative);
// TODO: check value references
// TODO: assert nUnknowns == nDeltaOfUnknowns
// TODO: assert nKnowns == nDeltaKnowns
Status status = OK;
for (size_t i = 0; i < nUnknown; i++) {
dvUnknown[i] = 0;
for (size_t j = 0; j < nKnown; j++) {
double partialDerivative = 0;
Status s = getPartialDerivative(S, vUnknown_ref[i], vKnown_ref[j], &partialDerivative);
status = max(status, s);
if (status > Warning) {
return (fmi2Status) status;
}
dvUnknown[i] += partialDerivative * dvKnown[j];
}
}
return fmi2OK;
}
// ---------------------------------------------------------------------------
// Functions for FMI for Co-Simulation
// ---------------------------------------------------------------------------
/* Simulating the slave */
fmi2Status fmi2SetRealInputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
const fmi2Integer order[], const fmi2Real value[])
{
UNUSED(vr);
UNUSED(nvr);
UNUSED(order);
UNUSED(value);
ASSERT_STATE(SetRealInputDerivatives);
logError(S, "fmi2SetRealInputDerivatives: ignoring function call."
" This model cannot interpolate inputs: canInterpolateInputs=\"fmi2False\"");
return fmi2Error;
}
fmi2Status fmi2GetRealOutputDerivatives(fmi2Component c, const fmi2ValueReference vr[], size_t nvr,
const fmi2Integer order[], fmi2Real value[])
{
ASSERT_STATE(GetRealOutputDerivatives);
#ifdef GET_OUTPUT_DERIVATIVE
Status status = OK;
for (size_t i = 0; i < nvr; i++) {
const Status s = getOutputDerivative(S, vr[i], order[i], &value[i]);
status = max(status, s);
if (status > Warning) {
return (fmi2Status) status;
}
}
return (fmi2Status) status;
#else
UNUSED(vr);
UNUSED(nvr);
UNUSED(order);
UNUSED(value);
logError(S, "fmi2GetRealOutputDerivatives: ignoring function call."
" This model cannot compute derivatives of outputs: MaxOutputDerivativeOrder=\"0\"");
return fmi2Error;
#endif
}
fmi2Status fmi2CancelStep(fmi2Component c)
{
ASSERT_STATE(CancelStep);
logError(S, "fmi2CancelStep: Can be called when fmi2DoStep returned fmi2Pending."
" This is not the case.");
return fmi2Error;
}
fmi2Status fmi2DoStep(fmi2Component c, fmi2Real currentCommunicationPoint,
fmi2Real communicationStepSize, fmi2Boolean noSetFMUStatePriorToCurrentPoint)
{
UNUSED(noSetFMUStatePriorToCurrentPoint);
ASSERT_STATE(DoStep);
if (communicationStepSize <= 0) {
logError(S, "fmi2DoStep: communication step size must be > 0 but was %g.", communicationStepSize);
S->state = modelError;
return fmi2Error;
}
const fmi2Real nextCommunicationPoint = currentCommunicationPoint + communicationStepSize + EPSILON;
S->solverStepSize = communicationStepSize;
fmi2Boolean nextCommunicationPointReached;
while (true) {
nextCommunicationPointReached = S->time + S->solverStepSize > nextCommunicationPoint;
if (nextCommunicationPointReached) {
break; // next communcation point reached
}
bool stateEvent, timeEvent;
doAlphaStep(S, &stateEvent, &timeEvent);
#ifdef EVENT_UPDATE
if (stateEvent || timeEvent) {
eventUpdate(S);
}
#endif
}
return S->terminateSimulation ? fmi2Discard : fmi2OK;
}
/* Inquire slave status */
static fmi2Status getStatus(char *fname, fmi2Component c, const fmi2StatusKind s)
{
if (invalidState(S, fname, MASK_fmi2GetStatus)) // all get status have the same MASK_fmi2GetStatus
return fmi2Error;
switch (s) {
case fmi2DoStepStatus:
logError(S,
"%s: Can be called with fmi2DoStepStatus when fmi2DoStep returned fmi2Pending."
" This is not the case.",
fname);
break;
case fmi2PendingStatus:
logError(S,
"%s: Can be called with fmi2PendingStatus when fmi2DoStep returned fmi2Pending."
" This is not the case.",
fname);
break;
case fmi2LastSuccessfulTime:
logError(S,
"%s: Can be called with fmi2LastSuccessfulTime when fmi2DoStep returned fmi2Discard."
" This is not the case.",
fname);
break;
case fmi2Terminated:
logError(S,
"%s: Can be called with fmi2Terminated when fmi2DoStep returned fmi2Discard."
" This is not the case.",
fname);
break;
}
return fmi2Discard;
}
fmi2Status fmi2GetStatus(fmi2Component c, const fmi2StatusKind s, fmi2Status *value)
{
UNUSED(value);
ASSERT_STATE(GetStatus);
return getStatus("fmi2GetStatus", c, s);
}
fmi2Status fmi2GetRealStatus(fmi2Component c, const fmi2StatusKind s, fmi2Real *value)
{
ASSERT_STATE(GetRealStatus);
if (s == fmi2LastSuccessfulTime) {
*value = S->time;
return fmi2OK;
}
return getStatus("fmi2GetRealStatus", c, s);
}
fmi2Status fmi2GetIntegerStatus(fmi2Component c, const fmi2StatusKind s, fmi2Integer *value)
{
UNUSED(value);
ASSERT_STATE(GetIntegerStatus);
return getStatus("fmi2GetIntegerStatus", c, s);
}
fmi2Status fmi2GetBooleanStatus(fmi2Component c, const fmi2StatusKind s, fmi2Boolean *value)
{
ASSERT_STATE(GetBooleanStatus);
if (s == fmi2Terminated) {
*value = S->terminateSimulation;
return fmi2OK;
}
return getStatus("fmi2GetBooleanStatus", c, s);
}
fmi2Status fmi2GetStringStatus(fmi2Component c, const fmi2StatusKind s, fmi2String *value)
{
UNUSED(value);
ASSERT_STATE(GetStringStatus);
return getStatus("fmi2GetStringStatus", c, s);
}
// ---------------------------------------------------------------------------
// Functions for FMI2 for Model Exchange
// ---------------------------------------------------------------------------
/* Enter and exit the different modes */
fmi2Status fmi2EnterEventMode(fmi2Component c)
{
ASSERT_STATE(EnterEventMode);
S->state = EventMode;
return fmi2OK;
}
fmi2Status fmi2NewDiscreteStates(fmi2Component c, fmi2EventInfo *eventInfo)
{
ASSERT_STATE(NewDiscreteStates);
#ifdef EVENT_UPDATE
eventUpdate(S);
#endif
eventInfo->newDiscreteStatesNeeded = S->newDiscreteStatesNeeded;
eventInfo->terminateSimulation = S->terminateSimulation;
eventInfo->nominalsOfContinuousStatesChanged = S->nominalsOfContinuousStatesChanged;
eventInfo->valuesOfContinuousStatesChanged = S->valuesOfContinuousStatesChanged;
eventInfo->nextEventTimeDefined = S->nextEventTimeDefined;
eventInfo->nextEventTime = S->nextEventTime;
return fmi2OK;
}
fmi2Status fmi2EnterContinuousTimeMode(fmi2Component c)
{
ASSERT_STATE(EnterContinuousTimeMode);
S->state = ContinuousTimeMode;
return fmi2OK;
}
fmi2Status fmi2CompletedIntegratorStep(fmi2Component c, fmi2Boolean noSetFMUStatePriorToCurrentPoint,
fmi2Boolean *enterEventMode, fmi2Boolean *terminateSimulation)
{
UNUSED(noSetFMUStatePriorToCurrentPoint);
ASSERT_STATE(CompletedIntegratorStep);
if (nullPointer(S, "fmi2CompletedIntegratorStep", "enterEventMode", enterEventMode))
return fmi2Error;
if (nullPointer(S, "fmi2CompletedIntegratorStep", "terminateSimulation", terminateSimulation))
return fmi2Error;
*enterEventMode = fmi2False;
*terminateSimulation = fmi2False;
return fmi2OK;
}
/* Providing independent variables and re-initialization of caching */
fmi2Status fmi2SetTime(fmi2Component c, fmi2Real time)
{
ASSERT_STATE(SetTime);
S->time = time;
return fmi2OK;
}
fmi2Status fmi2SetContinuousStates(fmi2Component c, const fmi2Real x[], size_t nx)
{
ASSERT_STATE(SetContinuousStates);
if (invalidNumber(S, "fmi2SetContinuousStates", "nx", nx, NX))
return fmi2Error;
if (nullPointer(S, "fmi2SetContinuousStates", "x[]", x))
return fmi2Error;
setContinuousStates(S, x, nx);
return fmi2OK;
}
/* Evaluation of the model equations */
fmi2Status fmi2GetDerivatives(fmi2Component c, fmi2Real derivatives[], size_t nx)
{
ASSERT_STATE(GetDerivatives);
if (invalidNumber(S, "fmi2GetDerivatives", "nx", nx, NX))
return fmi2Error;
if (nullPointer(S, "fmi2GetDerivatives", "derivatives[]", derivatives))
return fmi2Error;
getDerivatives(S, derivatives, nx);
return fmi2OK;
}
fmi2Status fmi2GetEventIndicators(fmi2Component c, fmi2Real eventIndicators[], size_t ni)
{
ASSERT_STATE(GetEventIndicators);
#if NZ > 0
if (invalidNumber(S, "fmi2GetEventIndicators", "ni", ni, NZ))
return fmi2Error;
getEventIndicators(S, eventIndicators, ni);
#else
UNUSED(c);
UNUSED(eventIndicators);
if (ni > 0)
return fmi2Error;
#endif
return fmi2OK;
}
fmi2Status fmi2GetContinuousStates(fmi2Component c, fmi2Real states[], size_t nx)
{
ASSERT_STATE(GetContinuousStates);
if (invalidNumber(S, "fmi2GetContinuousStates", "nx", nx, NX))
return fmi2Error;
if (nullPointer(S, "fmi2GetContinuousStates", "states[]", states))
return fmi2Error;
getContinuousStates(S, states, nx);
return fmi2OK;
}
fmi2Status fmi2GetNominalsOfContinuousStates(fmi2Component c, fmi2Real x_nominal[], size_t nx)
{
ASSERT_STATE(GetNominalsOfContinuousStates);
if (invalidNumber(S, "fmi2GetNominalContinuousStates", "nx", nx, NX))
return fmi2Error;
if (nullPointer(S, "fmi2GetNominalContinuousStates", "x_nominal[]", x_nominal))
return fmi2Error;
for (size_t i = 0; i < nx; i++)
x_nominal[i] = 1;
return fmi2OK;
}