forked from CATIA-Systems/FMIKit-Simulink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfun_fmurun.cpp
1059 lines (777 loc) · 30.2 KB
/
sfun_fmurun.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
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************
* Copyright (c) Dassault Systemes. All rights reserved. *
* This file is part of FMIKit. See LICENSE.txt in the project *
* root for license information. *
*****************************************************************/
#define S_FUNCTION_NAME sfun_fmurun
#define S_FUNCTION_LEVEL 2
#ifdef _WIN32
#include "shlwapi.h"
#include <wininet.h>
#endif
#include <string>
extern "C" {
#include "simstruc.h"
}
#include "FMU1.h"
#include "FMU2.h"
using namespace std;
using namespace fmikit;
#define MAX_MESSAGE_SIZE 4096
enum Parameter {
fmiVersionParam,
runAsKindParam,
guidParam,
modelIdentifierParam,
unzipDirectoryParam,
logLevelParam,
errorDiagnosticsParam,
relativeToleranceParam,
sampleTimeParam,
offsetTimeParam,
nxParam,
nzParam,
scalarStartTypesParam,
scalarStartVRsParam,
scalarStartValuesParam,
stringStartVRsParam,
stringStartValuesParam,
inputPortWidthsParam,
directInputParam,
inputPortDirectFeedThroughParam,
inputPortTypesParam,
inputPortVariableVRsParam,
canInterpolateInputsParam,
outputPortWidthsParam,
outputPortTypesParam,
outputPortVariableVRsParam,
numParams
};
string getStringParam(SimStruct *S, int index)
{
auto pa = ssGetSFcnParam(S, index);
auto data = (char *)mxGetData(pa);
auto lendata = mxGetNumberOfElements(pa);
size_t buflen = mxGetN(pa) * sizeof(mxChar) + 1;
auto str = (char *)mxMalloc(buflen);
auto status = mxGetString(pa, str, buflen);
string cppstr(str);
mxFree(str);
return cppstr;
}
static string fmiVersion(SimStruct *S) {
return getStringParam(S, fmiVersionParam);
}
inline fmikit::Kind runAsKind(SimStruct *S) { return static_cast<fmikit::Kind>( static_cast<int>( mxGetScalar( ssGetSFcnParam(S, runAsKindParam) ) ) ); }
static string guid(SimStruct *S) {
return getStringParam(S, guidParam);
}
static string modelIdentifier(SimStruct *S) {
return getStringParam(S, modelIdentifierParam);
}
static string unzipDirectory(SimStruct *S) {
return getStringParam(S, unzipDirectoryParam);
}
inline fmikit::LogLevel logLevel(SimStruct *S) { return static_cast<fmikit::LogLevel>(static_cast<int>(mxGetScalar(ssGetSFcnParam(S, logLevelParam )))); }
static string errorDiagnostics(SimStruct *S) {
return getStringParam(S, errorDiagnosticsParam);
}
inline double relativeTolerance(SimStruct *S) { return mxGetScalar(ssGetSFcnParam(S, relativeToleranceParam)); }
inline double sampleTime(SimStruct *S) { return mxGetScalar(ssGetSFcnParam(S, sampleTimeParam)); }
inline double offsetTime(SimStruct *S) { return mxGetScalar(ssGetSFcnParam(S, offsetTimeParam)); }
// number of continuous states
inline int nx(SimStruct *S) { return static_cast<int>(mxGetScalar(ssGetSFcnParam(S, nxParam))); }
// number of zero-crossings
inline int nz(SimStruct *S) { return static_cast<int>(mxGetScalar(ssGetSFcnParam(S, nzParam))); }
inline int nScalarStartValues(SimStruct *S) { return mxGetNumberOfElements(ssGetSFcnParam(S, scalarStartVRsParam)); }
static int inputPortWidth(SimStruct *S, int index) {
auto portWidths = static_cast<real_T *>(mxGetData(ssGetSFcnParam(S, inputPortWidthsParam)));
return static_cast<int>(portWidths[index]);
}
static bool inputPortDirectFeedThrough(SimStruct *S, int index) {
return static_cast<real_T *>(mxGetData(ssGetSFcnParam(S, inputPortDirectFeedThroughParam)))[index] != 0;
}
static bool canInterpolateInputs(SimStruct *S) {
return static_cast<real_T *>(mxGetData(ssGetSFcnParam(S, canInterpolateInputsParam)))[0] != 0;
}
// number of input ports
inline size_t nu(SimStruct *S) { return mxGetNumberOfElements(ssGetSFcnParam(S, inputPortWidthsParam)); }
// number of input variables
inline size_t nuv(SimStruct *S) { return mxGetNumberOfElements(ssGetSFcnParam(S, inputPortVariableVRsParam)); }
inline ValueReference valueReference(SimStruct *S, Parameter parameter, int index) {
auto param = ssGetSFcnParam(S, parameter);
auto realValue = static_cast<real_T *>(mxGetData(param))[index];
return static_cast<fmikit::ValueReference>(realValue);
}
inline Type variableType(SimStruct *S, Parameter parameter, int index) {
auto param = ssGetSFcnParam(S, parameter);
auto realValue = static_cast<real_T *>(mxGetData(param))[index];
auto intValue = static_cast<int>(realValue);
return static_cast<fmikit::Type>(intValue);
}
inline DTypeId simulinkVariableType(SimStruct *S, Parameter parameter, int index) {
auto param = ssGetSFcnParam(S, parameter);
auto realValue = static_cast<real_T *>(mxGetData(param))[index];
auto intValue = static_cast<int>(realValue);
auto type = static_cast<Type>(intValue);
switch(type) {
case fmikit::REAL: return SS_DOUBLE;
case fmikit::INTEGER: return SS_INT32;
case fmikit::BOOLEAN: return SS_BOOLEAN;
default: return -1; // error
}
}
inline real_T scalarValue(SimStruct *S, Parameter parameter, int index) {
auto param = ssGetSFcnParam(S, parameter);
return static_cast<real_T *>(mxGetData(param))[index];
}
static bool directInput(SimStruct *S) {
return static_cast<real_T *>(mxGetData(ssGetSFcnParam(S, directInputParam)))[0] != 0;
}
static int outputPortWidth(SimStruct *S, int index) {
auto portWidths = static_cast<real_T *>(mxGetData(ssGetSFcnParam(S, outputPortWidthsParam)));
return static_cast<int>(portWidths[index]);
}
inline size_t ny(SimStruct *S) { return mxGetNumberOfElements(ssGetSFcnParam(S, outputPortWidthsParam)); }
template<typename T> T *component(SimStruct *S) {
auto fmu = static_cast<FMU *>(ssGetPWork(S)[0]);
return dynamic_cast<T *>(fmu);
}
static void logMessage(LogLevel level, const char* category, const char* message, va_list args) {
char buf[MAX_MESSAGE_SIZE];
vsnprintf(buf, MAX_MESSAGE_SIZE, message, args);
strncat(buf, "\n", MAX_MESSAGE_SIZE);
ssPrintf(buf);
}
static void logDebug(SimStruct *S, const char* message, ...) {
va_list args;
va_start(args, message);
if (logLevel(S) <= DEBUG) {
char buf[MAX_MESSAGE_SIZE];
vsnprintf(buf, MAX_MESSAGE_SIZE, message, args);
strncat(buf, "\n", MAX_MESSAGE_SIZE);
ssPrintf(buf);
}
va_end(args);
}
static void setInput(SimStruct *S) {
auto fmu = component<FMU>(S);
const auto feedThrough = directInput(S);
// don't apply the delayed input at the first step
if (!feedThrough && ssGetT(S) == ssGetTStart(S)) {
return;
}
auto preu = ssGetRWork(S) + 2 * nz(S); // previous inputs
int iu = 0;
for (int i = 0; i < nu(S); i++) {
auto type = variableType(S, inputPortTypesParam, i);
const void *y = feedThrough ? ssGetInputPortSignal(S, i) : nullptr;
for (int j = 0; j < inputPortWidth(S, i); j++) {
auto vr = valueReference(S, inputPortVariableVRsParam, iu);
// set the input
switch (type) {
case Type::REAL:
fmu->setReal(vr, feedThrough ? static_cast<const real_T*>(y)[j] : preu[iu]);
break;
case Type::INTEGER:
fmu->setInteger(vr, feedThrough ? static_cast<const int32_T*>(y)[j] : preu[iu]);
break;
case Type::BOOLEAN:
fmu->setBoolean(vr, feedThrough ? static_cast<const boolean_T*>(y)[j] : preu[iu]);
break;
default:
break;
}
iu++;
}
}
}
/* Set the input derivatives for all real input ports with direct feed-through */
static void setInputDerivatives(SimStruct *S, double h) {
//if (h <= 0) {
// return;
//}
auto slave = component<Slave>(S);
const real_T *preu = ssGetRWork(S) + 2 * nz(S);
int iu = 0;
for (int i = 0; i < nu(S); i++) {
if (!ssGetInputPortDirectFeedThrough(S, i)) {
continue;
}
auto type = variableType(S, inputPortTypesParam, i);
if (type != fmikit::REAL) {
continue;
}
const real_T *u = ssGetInputPortRealSignal(S, i);
for (int j = 0; j < inputPortWidth(S, i); j++) {
auto vr = valueReference(S, inputPortVariableVRsParam, iu);
auto du = (u[j] - preu[iu]) / h;
slave->setRealInputDerivative(vr, 1, du);
iu++;
}
}
}
static void setOutput(SimStruct *S, FMU *fmu) {
int iy = 0;
for (int i = 0; i < ny(S); i++) {
auto type = variableType(S, outputPortTypesParam, i);
void *y = ssGetOutputPortSignal(S, i);
for (int j = 0; j < outputPortWidth(S, i); j++) {
auto vr = valueReference(S, outputPortVariableVRsParam, iy);
switch (type) {
case Type::REAL:
static_cast<real_T *>(y)[j] = fmu->getReal(vr);
break;
case Type::INTEGER:
static_cast<int32_T *>(y)[j] = fmu->getInteger(vr);
break;
case Type::BOOLEAN:
static_cast<boolean_T *>(y)[j] = fmu->getBoolean(vr);
break;
default:
break;
}
iy++;
}
}
}
static void getLibraryPath(SimStruct *S, char *path) {
#ifdef _WIN32
strcpy(path, unzipDirectory(S).c_str());
PathAppend(path, "binaries");
#ifdef _WIN64
PathAppend(path, "win64");
#else
PathAppend(path, "win32");
#endif
PathAppend(path, modelIdentifier(S).c_str());
PathAddExtension(path, ".dll");
#else
// TODO
#endif
}
static void setStartValues(SimStruct *S, FMU *fmu) {
// scalar start values
for (int i = 0; i < nScalarStartValues(S); i++) {
auto vr = valueReference(S, scalarStartVRsParam, i);
auto type = variableType(S, scalarStartTypesParam, i);
auto value = scalarValue(S, scalarStartValuesParam, i);
switch (type) {
case Type::REAL: fmu->setReal(vr, value); break;
case Type::INTEGER: fmu->setInteger(vr, static_cast<int>(value)); break;
case Type::BOOLEAN: fmu->setBoolean(vr, value != 0.0); break;
default: break;
}
}
// string start values
auto pa = ssGetSFcnParam(S, stringStartValuesParam);
auto size = mxGetNumberOfElements(pa) + 1;
auto m = mxGetM(pa);
auto n = mxGetN(pa);
auto buffer = static_cast<char *>(calloc(size, sizeof(char)));
auto value = static_cast<char *>(calloc(n + 1, sizeof(char)));
if (mxGetString(pa, buffer, size) != 0) {
ssSetErrorStatus(S, "Failed to convert string parameters");
return;
}
for (int i = 0; i < m; i++) {
// copy the row
for (int j = 0; j < n; j++) value[j] = buffer[j * m + i];
// remove the trailing blanks
for (int j = n - 1; j >= 0; j--) {
if (value[j] != ' ') break;
value[j] = '\0';
}
auto vr = valueReference(S, stringStartVRsParam, i);
fmu->setString(vr, value);
}
free(buffer);
free(value);
}
static void update(SimStruct *S) {
auto fmu = component<FMU>(S);
auto model = component<Model>(S);
if (model) {
double time = fmu->getTime();
bool upcomingTimeEvent;
double nextEventTime;
auto model1 = component<FMU1Model>(S);
auto model2 = component<FMU2Model>(S);
if (model1) {
upcomingTimeEvent = model1->upcomingTimeEvent();
} else {
upcomingTimeEvent = model2->nextEventTimeDefined();
}
nextEventTime = model->nextEventTime();
// Work around for the event handling in Dymola FMUs:
bool timeEvent = time >= nextEventTime;
if (timeEvent && logLevel(S) <= DEBUG) ssPrintf("Time event at t=%.16g\n", time);
bool stepEvent = model->completedIntegratorStep();
if (stepEvent && logLevel(S) <= DEBUG) ssPrintf("Step event at t=%.16g\n", time);
bool stateEvent = false;
if (nz(S) > 0) {
real_T *prez = ssGetRWork(S);
real_T *z = prez + nz(S);
model->getEventIndicators(z, nz(S));
// check for state events
for (int i = 0; i < nz(S); i++) {
bool rising = (prez[i] < 0 && z[i] >= 0) || (prez[i] == 0 && z[i] > 0);
bool falling = (prez[i] > 0 && z[i] <= 0) || (prez[i] == 0 && z[i] < 0);
if (rising || falling) {
logDebug(S, "State event %s z[%d] at t=%.16g\n", rising ? "-\\+" : "+/-", i, fmu->getTime());
stateEvent = true;
// TODO: break?
}
}
// remember the current event indicators
for (int i = 0; i < nz(S); i++) prez[i] = z[i];
}
if (timeEvent || stepEvent || stateEvent) {
if (model1) {
model1->eventUpdate();
} else {
model2->enterEventMode();
do {
model2->newDiscreteStates();
} while (model2->newDiscreteStatesNeeded() && !model2->terminateSimulation());
model2->enterContinuousTimeMode();
}
if (nx(S) > 0) {
auto x = ssGetContStates(S);
model->getContinuousStates(x, nx(S));
}
if (nz(S) > 0) {
auto prez = ssGetRWork(S);
model->getEventIndicators(prez, nz(S));
}
ssSetSolverNeedsReset(S);
}
}
}
#define MDL_CHECK_PARAMETERS
#if defined(MDL_CHECK_PARAMETERS) && defined(MATLAB_MEX_FILE)
static void mdlCheckParameters(SimStruct *S) {
logDebug(S, "\nmdlCheckParameters() called on %s\n", ssGetPath(S));
if (!mxIsChar(ssGetSFcnParam(S, fmiVersionParam)) || (fmiVersion(S) != "1.0" && fmiVersion(S) != "2.0")) {
ssSetErrorStatus(S, "Parameter 1 (FMI version) must be one of '1.0' or '2.0'");
return;
}
if (!mxIsNumeric(ssGetSFcnParam(S, runAsKindParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, runAsKindParam)) != 1 || (runAsKind(S) != MODEL_EXCHANGE && runAsKind(S) != CO_SIMULATION)) {
ssSetErrorStatus(S, "Parameter 2 (run as kind) must be one of 0 (= MODEL_EXCHANGE) or 1 (= CO_SIMULATION)");
return;
}
if (!mxIsChar(ssGetSFcnParam(S, guidParam))) {
ssSetErrorStatus(S, "Parameter 3 (GUID) must be a string");
return;
}
if (!mxIsChar(ssGetSFcnParam(S, modelIdentifierParam))) {
ssSetErrorStatus(S, "Parameter 4 (model identifier) must be a string");
return;
}
if (!mxIsChar(ssGetSFcnParam(S, unzipDirectoryParam))) {
ssSetErrorStatus(S, "Parameter 5 (unzip directory) must be a string");
return;
}
if (!mxIsNumeric(ssGetSFcnParam(S, logLevelParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, logLevelParam)) != 1 || (logLevel(S) != 1 && logLevel(S) != 2 && logLevel(S) != 3)) {
ssSetErrorStatus(S, "Parameter 6 (log level) must be one of 1 (= DEBUG), 2 (= INFO) or 3 (= WARNING)");
return;
}
if (!mxIsChar(ssGetSFcnParam(S, errorDiagnosticsParam)) || (errorDiagnostics(S) != "ignore" && errorDiagnostics(S) != "warning" && errorDiagnostics(S) != "error")) {
ssSetErrorStatus(S, "Parameter 7 (error diagnostics) must be one of 'ignore', 'warning' or 'error'");
return;
}
if (!mxIsNumeric(ssGetSFcnParam(S, relativeToleranceParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, relativeToleranceParam)) != 1) {
ssSetErrorStatus(S, "Parameter 8 (relative tolerance) must be numeric");
return;
}
if (!mxIsNumeric(ssGetSFcnParam(S, sampleTimeParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, sampleTimeParam)) != 1) {
ssSetErrorStatus(S, "Parameter 9 (sample time) must be numeric");
return;
}
if (!mxIsNumeric(ssGetSFcnParam(S, offsetTimeParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, offsetTimeParam)) != 1) {
ssSetErrorStatus(S, "Parameter 10 (offset time) must be numeric");
return;
}
if (!mxIsNumeric(ssGetSFcnParam(S, nxParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, nxParam)) != 1) {
ssSetErrorStatus(S, "Parameter 11 (number of continuous states) must be a scalar");
return;
}
if (!mxIsNumeric(ssGetSFcnParam(S, nzParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, nzParam)) != 1) {
ssSetErrorStatus(S, "Parameter 12 (number of event indicators) must be a scalar");
return;
}
if (!mxIsDouble(ssGetSFcnParam(S, scalarStartTypesParam))) {
ssSetErrorStatus(S, "Parameter 13 (scalar start value types) must be a double array");
return;
}
if (!mxIsDouble(ssGetSFcnParam(S, scalarStartVRsParam))) {
ssSetErrorStatus(S, "Parameter 14 (scalar start value references) must be a double array");
return;
}
if (mxGetNumberOfElements(ssGetSFcnParam(S, scalarStartVRsParam)) != mxGetNumberOfElements(ssGetSFcnParam(S, scalarStartTypesParam))) {
ssSetErrorStatus(S, "The number of elements in parameter 13 (scalar start value references) and parameter 12 (scalar start value types) must be equal");
return;
}
// TODO: check VRS values!
if (!mxIsDouble(ssGetSFcnParam(S, scalarStartValuesParam))) {
ssSetErrorStatus(S, "Parameter 15 (scalar start values) must be a double array");
return;
}
if (mxGetNumberOfElements(ssGetSFcnParam(S, scalarStartTypesParam)) != mxGetNumberOfElements(ssGetSFcnParam(S, scalarStartValuesParam))) {
ssSetErrorStatus(S, "The number of elements in parameter 15 (scalar start values) and parameter 12 (scalar start value types) must be equal");
return;
}
if (!mxIsDouble(ssGetSFcnParam(S, stringStartVRsParam))) {
ssSetErrorStatus(S, "Parameter 16 (string start value references) must be a double array");
return;
}
// TODO: check VRS values!
if (!mxIsChar(ssGetSFcnParam(S, stringStartValuesParam))) {
ssSetErrorStatus(S, "Parameter 17 (string start values) must be a char matrix");
return;
}
if (mxGetM(ssGetSFcnParam(S, stringStartValuesParam)) != mxGetNumberOfElements(ssGetSFcnParam(S, stringStartVRsParam))) {
ssSetErrorStatus(S, "The number of rows in parameter 17 (string start values) must be equal to the number of elements in parameter 15 (string start value references)");
return;
}
if (!mxIsDouble(ssGetSFcnParam(S, inputPortWidthsParam))) {
ssSetErrorStatus(S, "Parameter 18 (input port widths) must be a double array");
return;
}
if (!mxIsDouble(ssGetSFcnParam(S, inputPortDirectFeedThroughParam))) {
ssSetErrorStatus(S, "Parameter 20 (input port direct feed through) must be a double array");
return;
}
if (mxGetNumberOfElements(ssGetSFcnParam(S, inputPortDirectFeedThroughParam)) != mxGetNumberOfElements(ssGetSFcnParam(S, inputPortWidthsParam))) {
ssSetErrorStatus(S, "The number of elements in parameter XX (input port direct feed through) must be equal to the number of elements in parameter XX (inport port widths)");
return;
}
int nu = 0; // number of input variables
for (int i = 0; i < mxGetNumberOfElements(ssGetSFcnParam(S, inputPortWidthsParam)); i++) {
if (inputPortWidth(S, i) < 1) {
ssSetErrorStatus(S, "Elements in parameter 18 (input port widths) must be >= 1");
return;
}
nu += inputPortWidth(S, i);
}
if (!mxIsDouble(ssGetSFcnParam(S, directInputParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, directInputParam)) != 1) {
ssSetErrorStatus(S, "Parameter 19 (direct input) must be a double scalar");
return;
}
if (!mxIsDouble(ssGetSFcnParam(S, inputPortDirectFeedThroughParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, inputPortDirectFeedThroughParam)) != mxGetNumberOfElements(ssGetSFcnParam(S, inputPortWidthsParam))) {
ssSetErrorStatus(S, "Parameter 19 (direct input) must be a doulbe array with the same number of elements as parameter 18 (input port widths)");
return;
}
if (mxGetNumberOfElements(ssGetSFcnParam(S, inputPortDirectFeedThroughParam)) != mxGetNumberOfElements(ssGetSFcnParam(S, inputPortWidthsParam))) {
ssSetErrorStatus(S, "The number of elements in parameter 20 (inport port direct feed through) must be equal to the number of elements in parameter 18 (inport port widths)");
return;
}
if (!mxIsDouble(ssGetSFcnParam(S, inputPortTypesParam))) {
ssSetErrorStatus(S, "Parameter 19 (inport variable types) must be a double array");
return;
}
for (int i = 0; i < mxGetNumberOfElements(ssGetSFcnParam(S, inputPortTypesParam)); i++) {
auto v = static_cast<real_T *>(mxGetData(ssGetSFcnParam(S, inputPortTypesParam)))[i];
if (v != 0 && v != 1 && v != 2) {
ssSetErrorStatus(S, "Elements in parameter 19 (input port types) must be one of 0 (= Real), 1 (= Integer) or 2 (= Boolean)");
return;
}
}
if (mxGetNumberOfElements(ssGetSFcnParam(S, inputPortTypesParam)) != mxGetNumberOfElements(ssGetSFcnParam(S, inputPortWidthsParam))) {
ssSetErrorStatus(S, "The number of elements in parameter 19 (inport port types) must be equal to the number of the elements in parameter 18 (inport port widths)");
return;
}
if (!mxIsDouble(ssGetSFcnParam(S, inputPortVariableVRsParam))) {
ssSetErrorStatus(S, "Parameter 20 (inport variable value references) must be a double array");
return;
}
if (mxGetNumberOfElements(ssGetSFcnParam(S, inputPortVariableVRsParam)) != nu) {
ssSetErrorStatus(S, "The number of elements in parameter 20 (inport variable value references) must be equal to the sum of the elements in parameter 18 (inport port widths)");
return;
}
// TODO: check VRS values!
if (!mxIsDouble(ssGetSFcnParam(S, outputPortWidthsParam))) {
ssSetErrorStatus(S, "Parameter 21 (output port widths) must be a double array");
return;
}
int ny = 0; // number of output variables
for (int i = 0; i < mxGetNumberOfElements(ssGetSFcnParam(S, outputPortWidthsParam)); i++) {
if (outputPortWidth(S, i) < 1) {
ssSetErrorStatus(S, "Elements in parameter 21 (output port widths) must be >= 1");
return;
}
ny += outputPortWidth(S, i);
}
if (!mxIsDouble(ssGetSFcnParam(S, outputPortTypesParam))) {
ssSetErrorStatus(S, "Parameter 22 (output variable types) must be a double array");
return;
}
if (mxGetNumberOfElements(ssGetSFcnParam(S, outputPortTypesParam)) != mxGetNumberOfElements(ssGetSFcnParam(S, outputPortWidthsParam))) {
ssSetErrorStatus(S, "The number of elements in parameter 22 (output port types) must be equal to the number of the elements in parameter 21 (output port widths)");
return;
}
for (int i = 0; i < mxGetNumberOfElements(ssGetSFcnParam(S, outputPortWidthsParam)); i++) {
auto v = variableType(S, outputPortTypesParam, i);
if (v != 0 && v != 1 && v != 2) { // TODO: check this
ssSetErrorStatus(S, "Elements in parameter 22 (output variable types) must be one of 0 (= Real), 1 (= Integer) or 2 (= Boolean)");
return;
}
}
if (!mxIsDouble(ssGetSFcnParam(S, outputPortVariableVRsParam))) {
ssSetErrorStatus(S, "Parameter 23 (output variable value references) must be a double array");
return;
}
if (mxGetNumberOfElements(ssGetSFcnParam(S, outputPortVariableVRsParam)) != ny) {
ssSetErrorStatus(S, "The number of elements in parameter 23 (output variable value references) must be equal to the sum of the elements in parameter 21 (output port widths)");
return;
}
// TODO: check VRS values!
//if (!mxIsLogical(ssGetSFcnParam(S, directInputParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, directInputParam)) != 1) {
// ssSetErrorStatus(S, "Parameter 24 (direct input) must be a logical scalar");
// return;
//}
if (!mxIsDouble(ssGetSFcnParam(S, canInterpolateInputsParam)) || mxGetNumberOfElements(ssGetSFcnParam(S, canInterpolateInputsParam)) != 1) {
ssSetErrorStatus(S, "Parameter 25 (can interpolate inputs) must be a double scalar");
return;
}
}
#endif /* MDL_CHECK_PARAMETERS */
static void mdlInitializeSizes(SimStruct *S) {
logDebug(S, "\nmdlInitializeSizes() called on %s\n", ssGetPath(S));
ssSetNumSFcnParams(S, numParams);
#if defined(MATLAB_MEX_FILE)
if (ssGetNumSFcnParams(S) == ssGetSFcnParamsCount(S)) {
mdlCheckParameters(S);
if (ssGetErrorStatus(S) != NULL) {
return;
}
} else {
return; // parameter mismatch will be reported by Simulink
}
#endif
ssSetNumContStates(S, (runAsKind(S) == MODEL_EXCHANGE) ? nx(S) : 0);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, nu(S))) return;
for (int i = 0; i < nu(S); i++) {
ssSetInputPortWidth(S, i, inputPortWidth(S, i));
ssSetInputPortRequiredContiguous(S, i, 1); // direct input signal access
DTypeId type = simulinkVariableType(S, inputPortTypesParam, i);
ssSetInputPortDataType(S, i, type);
ssSetInputPortDirectFeedThrough(S, i, directInput(S) || inputPortDirectFeedThrough(S, i)); // direct feed through
logDebug(S, "\nssSetInputPortDirectFeedThrough(S, %d, %d) called on %s\n", i, 1, ssGetPath(S));
}
if (!ssSetNumOutputPorts(S, ny(S))) return;
for (int i = 0; i < ny(S); i++) {
ssSetOutputPortWidth(S, i, outputPortWidth(S, i));
DTypeId type = simulinkVariableType(S, outputPortTypesParam, i);
ssSetOutputPortDataType(S, i, type);
}
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 2 * nz(S) + nuv(S)); // prez & z, preu
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 1); // FMU
ssSetNumModes(S, 3); // [stateEvent, timeEvent, stepEvent]
ssSetNumNonsampledZCs(S, (runAsKind(S) == MODEL_EXCHANGE) ? nz(S) + 1 : 0);
// specify the sim state compliance to be same as a built-in block
//ssSetSimStateCompliance(S, USE_DEFAULT_SIM_STATE);
ssSetOptions(S, 0);
}
static void mdlInitializeSampleTimes(SimStruct *S) {
logDebug(S, "\nmdlInitializeSampleTimes() called on %s\n", ssGetPath(S));
if (runAsKind(S) == CO_SIMULATION) {
ssSetSampleTime(S, 0, sampleTime(S));
ssSetOffsetTime(S, 0, offsetTime(S));
} else {
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, offsetTime(S));
}
}
#define MDL_START
#if defined(MDL_START)
static void mdlStart(SimStruct *S) {
logDebug(S, "\nmdlStart() called on %s\n", ssGetPath(S));
auto instanceName = ssGetPath(S);
auto time = ssGetT(S);
FMU::m_messageLogger = logMessage;
char libraryFile[1000];
getLibraryPath(S, libraryFile);
#ifdef _WIN32
if (!PathFileExists(libraryFile)) {
#ifdef _WIN64
ssSetErrorStatus(S, "The current platform (Windows 64-bit) is not supported by the FMU");
#else
ssSetErrorStatus(S, "The current platform (Windows 32-bit) is not supported by the FMU");
#endif
return;
}
#endif
void **p = ssGetPWork(S);
bool toleranceDefined = relativeTolerance(S) > 0;
auto level = logLevel(S);
FMU::setLogLevel(level);
bool loggingOn = level == DEBUG;
ErrorDiagnostics diagnostics = ErrorDiagnosticsError;
if (errorDiagnostics(S) == "ignore") {
diagnostics = ErrorDiagnosticsIgnore;
} else if (errorDiagnostics(S) == "warning") {
diagnostics = ErrorDiagnosticsWarning;
}
if (fmiVersion(S) == "1.0") {
if (runAsKind(S) == CO_SIMULATION) {
auto slave = new FMU1Slave(guid(S), modelIdentifier(S), unzipDirectory(S), instanceName, 0.0, loggingOn, calloc, free);
slave->setErrorDiagnostics(diagnostics);
setStartValues(S, slave);
slave->initializeSlave(time, true, ssGetTFinal(S));
p[0] = slave;
} else {
auto model = new FMU1Model(guid(S), modelIdentifier(S), unzipDirectory(S), instanceName, loggingOn, calloc, free);
model->setErrorDiagnostics(diagnostics);
setStartValues(S, model);
model->setTime(time);
model->initialize(toleranceDefined, relativeTolerance(S));
if (model->terminateSimulation()) ssSetErrorStatus(S, "Model requested termination at init");
p[0] = model;
}
} else {
FMU2 *fmu;
if (runAsKind(S) == CO_SIMULATION) {
fmu = new FMU2Slave(guid(S), modelIdentifier(S), unzipDirectory(S), instanceName, calloc, free);
} else {
fmu = new FMU2Model(guid(S), modelIdentifier(S), unzipDirectory(S), instanceName, calloc, free);
}
fmu->instantiate(loggingOn);
fmu->setErrorDiagnostics(diagnostics);
setStartValues(S, fmu);
fmu->setupExperiment(toleranceDefined, relativeTolerance(S), time, true, ssGetTFinal(S));
fmu->enterInitializationMode();
fmu->exitInitializationMode();
p[0] = fmu;
}
}
#endif /* MDL_START */
#define MDL_INITIALIZE_CONDITIONS
#if defined(MDL_INITIALIZE_CONDITIONS)
static void mdlInitializeConditions(SimStruct *S) {
logDebug(S, "\nmdlInitializeConditions() called on %s\n", ssGetPath(S));
auto model = component<Model>(S);
if (model) {
// initialize the continuous states
auto x = ssGetContStates(S);
model->getContinuousStates(x, nx(S));
model->getContinuousStates(x, nx(S));
// initialize the event indicators
if (nz(S) > 0) {
auto prez = ssGetRWork(S);
auto z = prez + nz(S);
model->getEventIndicators(prez, nz(S));
model->getEventIndicators(z, nz(S));
}
}
}
#endif
static void mdlOutputs(SimStruct *S, int_T tid) {
logDebug(S, "\nmdlOutputs() called on %s (t=%.16g, %s)\n", ssGetPath(S), ssGetT(S), ssIsMajorTimeStep(S) ? "major" : "minor");
auto fmu = component<FMU>(S);
auto model = component<Model>(S);
if (model) {
auto x = ssGetContStates(S);
auto model2 = component<FMU2Model>(S);
if (model2 && model2->getState() == EventModeState) {
if (directInput(S)) {
setInput(S);
}
do {
model2->newDiscreteStates();
} while (model2->newDiscreteStatesNeeded() && !model2->terminateSimulation());
model2->enterContinuousTimeMode();
}
if (model2 && model2->getState() != ContinuousTimeModeState) model2->enterContinuousTimeMode();
model->setTime(ssGetT(S));
model->setContinuousStates(x, nx(S));
setInput(S);
if (ssIsMajorTimeStep(S)) {
update(S);
}
} else {
time_T h = ssGetT(S) - fmu->getTime();
auto slave = dynamic_cast<Slave *>(fmu);
if (h > 0) {
//setRecordedInput(S);
setInput(S);
if (!directInput(S) && canInterpolateInputs(S)) {
setInputDerivatives(S, h);
}
slave->doStep(h);
}
}
setOutput(S, fmu);
}
#define MDL_UPDATE
#if defined(MDL_UPDATE)
static void mdlUpdate(SimStruct *S, int_T tid) {
logDebug(S, "\nmdlUpdate() called on %s (t=%.16g, %s)\n", ssGetPath(S), ssGetT(S), ssIsMajorTimeStep(S) ? "major" : "minor");
// record the inputs
real_T *preu = ssGetRWork(S) + 2 * nz(S);
int iu = 0;
for (int i = 0; i < nu(S); i++) {
auto type = variableType(S, inputPortTypesParam, i);
const void *u = ssGetInputPortSignal(S, i);
for (int j = 0; j < inputPortWidth(S, i); j++) {
auto vr = valueReference(S, inputPortVariableVRsParam, iu);
switch (type) {
case Type::REAL:
preu[iu] = static_cast<const real_T*>(u)[j];
break;
case Type::INTEGER:
preu[iu] = static_cast<const int32_T*>(u)[j];
break;
case Type::BOOLEAN:
preu[iu] = static_cast<const boolean_T*>(u)[j];
break;
default:
break;
}
iu++;
}
}
}
#endif // MDL_UPDATE