/
CodegenC.tpl
6569 lines (5959 loc) · 261 KB
/
CodegenC.tpl
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
// This file defines templates for transforming Modelica/MetaModelica code to C
// code. They are used in the code generator phase of the compiler to write
// target code.
//
// There are two root templates intended to be called from the code generator:
// translateModel and translateFunctions. These templates do not return any
// result but instead write the result to files. All other templates return
// text and are used by the root templates (most of them indirectly).
//
// To future maintainers of this file:
//
// - A line like this
// # var = ""
// declares a text buffer that you can later append text to. It can also be
// passed to other templates that in turn can append text to it. In the new
// version of Susan it should be written like this instead:
// let &var = buffer ""
//
// - A line like this
// ..., Text var, ...
// declares that a template takes a tmext buffer as input parameter. In the
// new version of Susan it should be written like this instead:
// ..., Text &var, ...
//
// - A line like this:
// ..., var, ...
// passes a text buffer to a template. In the new version of Susan it should
// be written like this instead:
// ..., &var, ...
//
// - Style guidelines:
//
// - Try (hard) to limit each row to 80 characters
//
// - Code for a template should be indented with 2 spaces
//
// - Exception to this rule is if you have only a single case, then that
// single case can be written using no indentation
//
// This single case can be seen as a clarification of the input to the
// template
//
// - Code after a case should be indented with 2 spaces if not written on the
// same line
package CodegenC
import interface SimCodeTV;
import interface SimCodeBackendTV;
import CodegenUtil.*;
import CodegenUtilSimulation.*;
import CodegenCFunctions.*;
import ExpressionDumpTpl.*;
/* public */ template translateModel(SimCode simCode)
"Generates C code and Makefile for compiling and running a simulation of a
Modelica model.
used in Compiler/SimCode/SimCodeMain.mo"
::=
match simCode
case sc as SIMCODE(modelInfo=modelInfo as MODELINFO(__)) then
let()= System.tmpTickResetIndex(0,2) /* auxFunction index */
let()= System.tmpTickResetIndex(0,20) /*parfor index*/
let &literalsFile = buffer ""
let &literalsFile += redirectToFile('<%fileNamePrefix%>_literals.h')
let &literalsFile += simulationLiteralsFile(fileNamePrefix, literals)
let &literalsFile += closeFile()
let &functionsHeader = buffer ""
let &functionsHeader += redirectToFile('<%fileNamePrefix%>_functions.h')
let &functionsHeader += simulationFunctionsHeaderFile(fileNamePrefix, modelInfo.functions, recordDecls)
let &functionsHeader += closeFile()
let &includes = buffer ""
let &includes += redirectToFile('<%fileNamePrefix%>_includes.h')
let &includes += externalFunctionIncludes(sc.externalFunctionIncludes)
let &includes += closeFile()
let &records = buffer ""
let &records += redirectToFile('<%fileNamePrefix%>_records.c')
let &records += recordsFile(fileNamePrefix, recordDecls, true /*isSimulation*/)
let &records += closeFile()
// adpro: write the main .c file last! Make on windows doesn't seem to realize that
// the .c file is newer than the .o file if we have succesive simulate commands
// for the same model (i.e. see testsuite/linearize/simextfunction.mos).
// If ParModelica generate the kernels file too.
if acceptParModelicaGrammar() then
let &cl = buffer ""
let &cl += redirectToFile('<%fileNamePrefix%>_kernels.cl')
let &cl += simulationParModelicaKernelsFile(fileNamePrefix, modelInfo.functions)
let &cl += closeFile()
""
//this top-level template always returns an empty result
//since generated texts are written to files directly
end match
end translateModel;
/* public */ template simulationHeaderFile(SimCode simCode)
"Generates code for main C file for simulation target.
used in Compiler/Template/CodegenFMU.tpl"
::=
let modelNamePrefixStr = modelNamePrefix(simCode)
match simCode
case simCode as SIMCODE(modelInfo=MODELINFO(functions=functions, varInfo=varInfo as VARINFO(__))) then
<<
/* Simulation code for <%dotPath(modelInfo.name)%> generated by the OpenModelica Compiler <%getVersionNr()%>. */
#if !defined(<%modelNamePrefixStr%>__MODEL_H)
#define <%modelNamePrefixStr%>__MODEL_H
#include "openmodelica.h"
#include "openmodelica_func.h"
#include "simulation_data.h"
#include "simulation/simulation_info_json.h"
#include "simulation/simulation_runtime.h"
#include "util/omc_error.h"
#include "util/parallel_helper.h"
#include "simulation/solver/model_help.h"
#include "simulation/solver/delay.h"
#include "simulation/solver/linearSystem.h"
#include "simulation/solver/nonlinearSystem.h"
#include "simulation/solver/mixedSystem.h"
#if defined(__cplusplus)
extern "C" {
#endif
#include <string.h>
#include "<%fileNamePrefix%>_functions.h"
<%variableDefinitions(modelInfo, timeEvents)%><%functions |> fn hasindex i0 => '#define <%functionName(fn,false)%>_index <%i0%>'; separator="\n"%>
extern void <%symbolName(modelNamePrefixStr,"callExternalObjectDestructors")%>(DATA *_data, threadData_t *threadData);
#if !defined(OMC_NUM_NONLINEAR_SYSTEMS) || OMC_NUM_NONLINEAR_SYSTEMS>0
<% if intGt(varInfo.numNonLinearSystems, 0) then 'extern void <%symbolName(modelNamePrefixStr,"initialNonLinearSystem")%>(int nNonLinearSystems, NONLINEAR_SYSTEM_DATA *data);' %>
#endif
#if !defined(OMC_NUM_LINEAR_SYSTEMS) || OMC_NUM_LINEAR_SYSTEMS>0
<% if intGt(varInfo.numLinearSystems, 0) then 'extern void <%symbolName(modelNamePrefixStr,"initialLinearSystem")%>(int nLinearSystems, LINEAR_SYSTEM_DATA *data);' %>
#endif
#if !defined(OMC_NUM_MIXED_SYSTEMS) || OMC_NUM_MIXED_SYSTEMS>0
<% if intGt(varInfo.numMixedSystems, 0) then 'extern void <%symbolName(modelNamePrefixStr,"initialMixedSystem")%>(int nMixedSystems, MIXED_SYSTEM_DATA *data);' %>
#endif
#if !defined(OMC_NO_STATESELECTION)
extern void <%symbolName(modelNamePrefixStr,"initializeStateSets")%>(int nStateSets, STATE_SET_DATA* statesetData, DATA *data);
#endif
extern int <%symbolName(modelNamePrefixStr,"functionAlgebraics")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"function_storeDelayed")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"updateBoundVariableAttributes")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"functionInitialEquations")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"functionInitialEquations_lambda0")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"functionRemovedInitialEquations")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"updateBoundParameters")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"checkForAsserts")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"function_ZeroCrossingsEquations")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"function_ZeroCrossings")%>(DATA *data, threadData_t *threadData, double* gout);
extern int <%symbolName(modelNamePrefixStr,"function_updateRelations")%>(DATA *data, threadData_t *threadData, int evalZeroCross);
extern const char* <%symbolName(modelNamePrefixStr,"zeroCrossingDescription")%>(int i, int **out_EquationIndexes);
extern const char* <%symbolName(modelNamePrefixStr,"relationDescription")%>(int i);
extern void <%symbolName(modelNamePrefixStr,"function_initSample")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"initialAnalyticJacobianG")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *jacobian);
extern int <%symbolName(modelNamePrefixStr,"initialAnalyticJacobianA")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *jacobian);
extern int <%symbolName(modelNamePrefixStr,"initialAnalyticJacobianB")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *jacobian);
extern int <%symbolName(modelNamePrefixStr,"initialAnalyticJacobianC")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *jacobian);
extern int <%symbolName(modelNamePrefixStr,"initialAnalyticJacobianD")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *jacobian);
extern int <%symbolName(modelNamePrefixStr,"initialAnalyticJacobianF")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *jacobian);
extern int <%symbolName(modelNamePrefixStr,"functionJacG_column")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *thisJacobian, ANALYTIC_JACOBIAN *parentJacobian);
extern int <%symbolName(modelNamePrefixStr,"functionJacA_column")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *thisJacobian, ANALYTIC_JACOBIAN *parentJacobian);
extern int <%symbolName(modelNamePrefixStr,"functionJacB_column")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *thisJacobian, ANALYTIC_JACOBIAN *parentJacobian);
extern int <%symbolName(modelNamePrefixStr,"functionJacC_column")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *thisJacobian, ANALYTIC_JACOBIAN *parentJacobian);
extern int <%symbolName(modelNamePrefixStr,"functionJacD_column")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *thisJacobian, ANALYTIC_JACOBIAN *parentJacobian);
extern int <%symbolName(modelNamePrefixStr,"functionJacF_column")%>(void* data, threadData_t *threadData, ANALYTIC_JACOBIAN *thisJacobian, ANALYTIC_JACOBIAN *parentJacobian);
extern const char* <%symbolName(modelNamePrefixStr,"linear_model_frame")%>(void);
extern const char* <%symbolName(modelNamePrefixStr,"linear_model_datarecovery_frame")%>(void);
extern int <%symbolName(modelNamePrefixStr,"mayer")%>(DATA* data, modelica_real** res, short *);
extern int <%symbolName(modelNamePrefixStr,"lagrange")%>(DATA* data, modelica_real** res, short *, short *);
extern int <%symbolName(modelNamePrefixStr,"pickUpBoundsForInputsInOptimization")%>(DATA* data, modelica_real* min, modelica_real* max, modelica_real*nominal, modelica_boolean *useNominal, char ** name, modelica_real * start, modelica_real * startTimeOpt);
extern int <%symbolName(modelNamePrefixStr,"setInputData")%>(DATA *data, const modelica_boolean file);
extern int <%symbolName(modelNamePrefixStr,"getTimeGrid")%>(DATA *data, modelica_integer * nsi, modelica_real**t);
extern void <%symbolName(modelNamePrefixStr,"function_initSynchronous")%>(DATA * data, threadData_t *threadData);
extern void <%symbolName(modelNamePrefixStr,"function_updateSynchronous")%>(DATA * data, threadData_t *threadData, long i);
extern int <%symbolName(modelNamePrefixStr,"function_equationsSynchronous")%>(DATA * data, threadData_t *threadData, long i);
extern void <%symbolName(modelNamePrefixStr,"read_input_fmu")%>(MODEL_DATA* modelData, SIMULATION_INFO* simulationData);
extern void <%symbolName(modelNamePrefixStr,"function_savePreSynchronous")%>(DATA *data, threadData_t *threadData);
extern int <%symbolName(modelNamePrefixStr,"inputNames")%>(DATA* data, char ** names);
extern int <%symbolName(modelNamePrefixStr,"initializeDAEmodeData")%>(DATA *data, DAEMODE_DATA*);
extern int <%symbolName(modelNamePrefixStr,"functionLocalKnownVars")%>(DATA*, threadData_t*);
extern int <%symbolName(modelNamePrefixStr,"symbolicInlineSystem")%>(DATA*, threadData_t*);
#include "<%fileNamePrefix%>_literals.h"
<%if Flags.isSet(Flags.PARMODAUTO) then "#include \"ParModelica/auto/om_pm_interface.hpp\""%>
<%if stringEq(getConfigString(HPCOM_CODE),"pthreads_spin") then "#include \"util/omc_spinlock.h\""%>
<%if Flags.isSet(HPCOM) then "#define HPCOM"%>
#if defined(HPCOM) && !defined(_OPENMP)
#error "HPCOM requires OpenMP or the results are wrong"
#endif
#if defined(_OPENMP)
#include <omp.h>
#else
/* dummy omp defines */
#define omp_get_max_threads() 1
#endif
#if defined(__cplusplus)
}
#endif
#endif /* !defined(<%modelNamePrefixStr%>__MODEL_H) */
<%\n%>
>>
end match
end simulationHeaderFile;
template simulationFile_mixAndHeader(SimCode simCode, String modelNamePrefix)
::=
let &mixheader = buffer ""
let()= textFileConvertLines(simulationFile_mix(simCode,&mixheader), '<%modelNamePrefix%>_11mix.c')
let()= textFile(&mixheader, '<%modelNamePrefix%>_11mix.h')
""
end simulationFile_mixAndHeader;
template simulationFile_syn(SimCode simCode)
"Synchonous features"
::= match simCode
case simCode as SIMCODE(__) then
<<
/* Synchronous systems */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#if defined(__cplusplus)
extern "C" {
#endif
<%functionInitSynchronous(clockedPartitions, modelNamePrefix(simCode))%>
<%functionUpdateSynchronous(clockedPartitions, modelNamePrefix(simCode))%>
<%functionSystemsSynchronous(getSubPartitions(clockedPartitions), modelNamePrefix(simCode))%>
<%functionSavePreSynchronous(getSubPartitions(clockedPartitions), modelNamePrefix(simCode))%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
end match
end simulationFile_syn;
template functionSavePreSynchronous(list<SimCode.SubPartition> subPartitions, String modelNamePrefix)
::=
let preVars = subPartitions |> subPartition =>
functionSavePreSynchronous1(subPartition); separator="\n"
<<
/* pre(%v%) = %v% */
void <%symbolName(modelNamePrefix,"function_savePreSynchronous")%>(DATA *data, threadData_t *threadData)
{
TRACE_PUSH
<%preVars%>
TRACE_POP
}
>>
end functionSavePreSynchronous;
template functionSavePreSynchronous1(SimCode.SubPartition subPartition)
::=
match subPartition
case SUBPARTITION(__) then functionSavePreSynchronous2(vars)
end functionSavePreSynchronous1;
template functionSavePreSynchronous2(list<tuple<SimCodeVar.SimVar, Boolean>> vars)
::=
vars |> var => functionSavePreSynchronous3(var); separator = "\n"
end functionSavePreSynchronous2;
template functionSavePreSynchronous3(tuple<SimCodeVar.SimVar, Boolean> var)
::=
match var
case (simVar, previous) then
match simVar
case SIMVAR(arrayCref=SOME(c), aliasvar=NOALIAS()) then
'<%cref(c)%> = <%crefPre(c)%>;'
case SIMVAR(aliasvar=NOALIAS()) then
'<%cref(name)%> = <%crefPre(name)%>;'
end functionSavePreSynchronous3;
template isBoolClock(DAE.ClockKind clock)
::=
match clock
case BOOLEAN_CLOCK(__) then boolStrC(true)
else boolStrC(false)
end isBoolClock;
template functionInitSynchronous(list<ClockedPartition> clockedPartitions, String modelNamePrefix)
"Synchonous features"
::=
let body = clockedPartitions |> partition =>
match partition
case CLOCKED_PARTITION(__) then
let boolClock = isBoolClock(baseClock)
let subClocksInfo = subPartitions |> subPartition =>
subPartitionStr(subPartition); separator="\n"
<<
data->modelData->clocksInfo[i].nSubClocks = <%listLength(subPartitions)%>;
data->modelData->clocksInfo[i].subClocks = data->modelData->subClocksInfo + j;
data->modelData->clocksInfo[i].isBoolClock = <%boolClock%>;
i++;
<%subClocksInfo%>
>>
<<
/* Initializes the clocks of model. */
void <%symbolName(modelNamePrefix,"function_initSynchronous")%>(DATA *data, threadData_t *threadData)
{
TRACE_PUSH
long i=0, j=0;
<%body%>
TRACE_POP
}
>>
end functionInitSynchronous;
template subPartitionStr(SubPartition subPartition)
::=
match subPartition
case SUBPARTITION(subClock = SUBCLOCK(__), holdEvents=holdEvents) then
let methodStr = match subClock.solver
case NONE() then ""
else "External"
<<
<%rationalStr("data->modelData->subClocksInfo[j].shift", subClock.shift)%>
<%rationalStr("data->modelData->subClocksInfo[j].factor", subClock.factor)%>
data->modelData->subClocksInfo[j].solverMethod = "<%methodStr%>";
data->modelData->subClocksInfo[j].holdEvents = <%boolStrC(holdEvents)%>;
j++;
>>
end subPartitionStr;
template rationalStr(String prefix, Rational rational)
::=
match rational
case RATIONAL(__) then
<<
<%prefix%>.m = <%nom%>;
<%prefix%>.n = <%denom%>;
>>
end rationalStr;
template functionUpdateSynchronous(list<ClockedPartition> clockedPartitions, String modelNamePrefix)
::=
let &varDecls = buffer ""
let &auxFunction = buffer ""
let body = clockedPartitions |> partition hasindex i =>
match partition
case CLOCKED_PARTITION(__) then
let caseBody = updatePartition(i, baseClock, &varDecls, &auxFunction)
<<
case <%i%>:
<%caseBody%>
break;
>>; separator = "\n"
<<
<%auxFunction%>
/* Update the base clock. */
void <%symbolName(modelNamePrefix,"function_updateSynchronous")%>(DATA *data, threadData_t *threadData, long i)
{
TRACE_PUSH
<%varDecls%>
modelica_boolean ret;
switch (i) {
<%body%>
default:
throwStreamPrint(NULL, "Internal Error: unknown base partition %ld", i);
break;
}
TRACE_POP
}
>>
end functionUpdateSynchronous;
template updatePartition(Integer i, DAE.ClockKind baseClock, Text &varDecls, Text &auxFunction)
::=
match baseClock
case DAE.BOOLEAN_CLOCK(__) then
let cond = cref(expCref(condition))
let &preExp = buffer ""
let si = daeExp(startInterval, contextOther, &preExp, &varDecls, &auxFunction)
<<
<%preExp%>
if (data->simulationInfo->clocksData[i].cnt > 0)
data->simulationInfo->clocksData[i].interval = data->localData[0]->timeValue - data->simulationInfo->clocksData[i].timepoint;
else
data->simulationInfo->clocksData[i].interval = <%si%>;
>>
else
let &preExp = buffer ""
let intvl = match baseClock
case REAL_CLOCK()
case INTEGER_CLOCK()
case BOOLEAN_CLOCK() then
daeExp(getClockInterval(baseClock), contextOther, &preExp, &varDecls, &auxFunction)
else "unspecified"
let interval = match intvl case "unspecified" then '1.0' else intvl
let warning = match intvl case "unspecified" then
'ModelicaMessage("Using default Clock(1.0)!");'
<<
<%preExp%>
data->simulationInfo->clocksData[i].interval = <%interval%>;
<%warning%>
>>
end updatePartition;
template functionSystemsSynchronous(list<SubPartition> subPartitions, String modelNamePrefix)
::=
let systs = subPartitions |> subPartition hasindex i =>
match subPartition
case SUBPARTITION(__) then
functionEquationsSynchronous(i, vars, listAppend(equations, removedEquations), modelNamePrefix)
; separator = "\n"
let cases = subPartitions |> subPartition hasindex i =>
let name = 'functionEquationsSynchronous_system<%i%>'
<<
case <%i%>:
ret = <%symbolName(modelNamePrefix, name)%>(data, threadData);
break;
>>; separator = "\n"
<<
<%systs%>
/*Clocked systems equations */
int <%symbolName(modelNamePrefix,"function_equationsSynchronous")%>(DATA *data, threadData_t *threadData, long i)
{
TRACE_PUSH
int ret;
switch (i) {
<%cases%>
default:
throwStreamPrint(NULL, "Internal Error: unknown sub partition %ld", i);
ret = 1;
break;
}
TRACE_POP
return ret;
}
>>
end functionSystemsSynchronous;
template functionEquationsSynchronous(Integer i, list<tuple<SimCodeVar.SimVar, Boolean>> vars, list<SimEqSystem> equations, String modelNamePrefix)
::=
<<
<%equations |> eq => equation_impl(i, eq, contextOther, modelNamePrefix, false) ; separator="\n"%>
int <%symbolName(modelNamePrefix, 'functionEquationsSynchronous_system<%i%>')%>(DATA *data, threadData_t *threadData)
{
TRACE_PUSH
int i;
<%addRootsTempArray()%>
<%equations |> eq => equation_call(eq, modelNamePrefix) ; separator="\n"%>
TRACE_POP
return 0;
}
>>
end functionEquationsSynchronous;
template simulationFile_exo(SimCode simCode)
"External Objects"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* External objects file */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#if defined(__cplusplus)
extern "C" {
#endif
<%functionCallExternalObjectDestructors(extObjInfo, modelNamePrefix(simCode))%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_exo;
template simulationFile_nls(SimCode simCode)
"Non Linear Systems"
::=
match simCode
case simCode as SIMCODE(modelInfo=MODELINFO(varInfo=varInfo as VARINFO(__),nonLinearSystems=nonLinearSystems)) then
let modelNamePrefixStr = modelNamePrefix(simCode)
<<
/* Non Linear Systems */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#include "<%simCode.fileNamePrefix%>_12jac.h"
#if defined(__cplusplus)
extern "C" {
#endif
<%functionNonLinearResiduals(nonLinearSystems, modelNamePrefixStr)%>
<%if intGt(varInfo.numNonLinearSystems, 0) then functionInitialNonLinearSystems(nonLinearSystems, modelNamePrefixStr)%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_nls;
template simulationFile_lsy(SimCode simCode)
"Linear Systems"
::=
match simCode
case simCode as SIMCODE(modelInfo=MODELINFO(varInfo=varInfo as VARINFO(__),linearSystems=linearSystems)) then
<<
/* Linear Systems */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#include "<%simCode.fileNamePrefix%>_12jac.h"
#if defined(__cplusplus)
extern "C" {
#endif
<%functionSetupLinearSystems(linearSystems, modelNamePrefix(simCode))%>
<% if intGt(varInfo.numLinearSystems,0) then functionInitialLinearSystems(linearSystems, modelNamePrefix(simCode))%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_lsy;
template simulationFile_set(SimCode simCode)
"Initial State Set"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* Initial State Set */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#include "<%simCode.fileNamePrefix%>_11mix.h"
#include "<%simCode.fileNamePrefix%>_12jac.h"
#if defined(__cplusplus)
extern "C" {
#endif
<%functionInitialStateSets(simCode, stateSets, modelNamePrefix(simCode))%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_set;
template simulationFile_evt(SimCode simCode)
"Events: Sample, Zero Crossings, Relations, Discrete Changes"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* Events: Sample, Zero Crossings, Relations, Discrete Changes */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#if defined(__cplusplus)
extern "C" {
#endif
<%functionInitSample(timeEvents, modelNamePrefix(simCode))%>
<%functionZeroCrossing(zeroCrossings, equationsForZeroCrossings, modelNamePrefix(simCode))%>
<%functionRelations(relations, modelNamePrefix(simCode))%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_evt;
template simulationFile_inz(SimCode simCode)
"Initialization"
::=
match simCode
case simCode as SIMCODE(modelInfo=MODELINFO(varInfo=varInfo as VARINFO(__))) then
<<
/* Initialization */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#include "<%simCode.fileNamePrefix%>_11mix.h"
#include "<%simCode.fileNamePrefix%>_12jac.h"
#if defined(__cplusplus)
extern "C" {
#endif
<%functionInitialEquations(initialEquations, listLength(initialEquations), simCode.fileNamePrefix, simCode.fullPathPrefix, modelNamePrefix(simCode),
/* initial: only to be treated as first system if there is no lambda_0 system */ intEq(listLength(initialEquations_lambda0), 0))%>
<%functionInitialEquations_lambda0(initialEquations_lambda0, modelNamePrefix(simCode))%>
<%functionRemovedInitialEquations(removedInitialEquations, modelNamePrefix(simCode))%>
<%if intGt(varInfo.numMixedSystems,0) then functionInitialMixedSystems(initialEquations, initialEquations_lambda0, parameterEquations, allEquations, jacobianMatrixes, modelNamePrefix(simCode))%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_inz;
template simulationFile_dly(SimCode simCode)
"Delay"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* Delay */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#if defined(__cplusplus)
extern "C" {
#endif
<%functionStoreDelayed(delayedExps, modelNamePrefix(simCode))%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_dly;
template simulationFile_bnd(SimCode simCode)
"update bound parameters and variable attributes (start, nominal, min, max)"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* update bound parameters and variable attributes (start, nominal, min, max) */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#if defined(__cplusplus)
extern "C" {
#endif
<%functionUpdateBoundVariableAttributes(simCode, startValueEquations, nominalValueEquations, minValueEquations, maxValueEquations, modelNamePrefix(simCode))%>
<%functionUpdateBoundParameters(selectScalarLiteralAssignments(parameterEquations), filterScalarLiteralAssignments(parameterEquations), simCode.fileNamePrefix, simCode.fullPathPrefix, modelNamePrefix(simCode), simCode)%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_bnd;
template simulationFile_alg(SimCode simCode)
"Algebraic"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* Algebraic */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#ifdef __cplusplus
extern "C" {
#endif
<%functionAlgebraic(algebraicEquations, modelNamePrefix(simCode))%>
#ifdef __cplusplus
}
#endif<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_alg;
template simulationFile_asr(SimCode simCode)
"Asserts"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* Asserts */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#if defined(__cplusplus)
extern "C" {
#endif
<%functionAssertsforCheck(algorithmAndEquationAsserts, modelNamePrefix(simCode))%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_asr;
template simulationFile_mix(SimCode simCode, Text &header)
"Mixed Systems"
::=
match simCode
case simCode as SIMCODE(__) then
let modelNamePrefixStr = modelNamePrefix(simCode)
<<
/* Mixed Systems */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#include "<%simCode.fileNamePrefix%>_11mix.h"
<%functionSetupMixedSystems(initialEquations, initialEquations_lambda0, parameterEquations, allEquations, jacobianMatrixes, &header, modelNamePrefixStr)%>
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_mix;
template simulationFile_jac(SimCode simCode)
"Jacobians"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* Jacobians <%listLength(jacobianMatrixes)%> */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#include "<%fileNamePrefix%>_12jac.h"
<%functionAnalyticJacobians(jacobianMatrixes, modelNamePrefix(simCode))%>
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_jac;
template simulationFile_jac_header(SimCode simCode)
"Jacobians"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* Jacobians */
static const REAL_ATTRIBUTE dummyREAL_ATTRIBUTE = omc_dummyRealAttribute;
<%symJacDefinition(jacobianMatrixes, modelNamePrefix(simCode))%>
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_jac_header;
template simulationFile_opt(SimCode simCode)
"Optimization"
::=
match simCode
case simCode as SIMCODE(__) then
let modelNamePrefixStr = modelNamePrefix(simCode)
<<
/* Optimization */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#include "<%fileNamePrefix%>_12jac.h"
#if defined(__cplusplus)
extern "C" {
#endif
<%optimizationComponents(classAttributes, simCode, modelNamePrefixStr)%>
#if defined(__cplusplus)
}
#endif
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_opt;
template simulationFile_opt_header(SimCode simCode)
"Jacobians"
::=
match simCode
case simCode as SIMCODE(__) then
let modelNamePrefixStr = modelNamePrefix(simCode)
<<
#if defined(__cplusplus)
extern "C" {
#endif
int <%symbolName(modelNamePrefixStr,"mayer")%>(DATA* data, modelica_real** res, short*);
int <%symbolName(modelNamePrefixStr,"lagrange")%>(DATA* data, modelica_real** res, short *, short *);
int <%symbolName(modelNamePrefixStr,"pickUpBoundsForInputsInOptimization")%>(DATA* data, modelica_real* min, modelica_real* max, modelica_real*nominal, modelica_boolean *useNominal, char ** name, modelica_real * start, modelica_real * startTimeOpt);
int <%symbolName(modelNamePrefixStr,"setInputData")%>(DATA *data, const modelica_boolean file);
int <%symbolName(modelNamePrefixStr,"getTimeGrid")%>(DATA *data, modelica_integer * nsi, modelica_real**t);
#if defined(__cplusplus)
}
#endif
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_opt_header;
template simulationFile_lnz(SimCode simCode)
"Linearization"
::=
match simCode
case simCode as SIMCODE(__) then
<<
/* Linearization */
<%simulationFileHeader(simCode.fileNamePrefix)%>
#if defined(__cplusplus)
extern "C" {
#endif
<%
if stringEq(Flags.getConfigString(LINEARIZATION_DUMP_LANGUAGE),"modelica")
then functionlinearmodel(modelInfo, modelNamePrefix(simCode))
else if stringEq(Flags.getConfigString(LINEARIZATION_DUMP_LANGUAGE),"matlab")
then functionlinearmodelMatlab(modelInfo, modelNamePrefix(simCode))
else if stringEq(Flags.getConfigString(LINEARIZATION_DUMP_LANGUAGE),"julia")
then functionlinearmodelJulia(modelInfo, modelNamePrefix(simCode))
else if stringEq(Flags.getConfigString(LINEARIZATION_DUMP_LANGUAGE),"python")
then functionlinearmodelPython(modelInfo, modelNamePrefix(simCode))
%>
#if defined(__cplusplus)
}
#endif
<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
end match
end simulationFile_lnz;
template defineSimVarArray(SimVar simVar, String arrayName)
"Generates a define statement for a parameter."
::=
match simVar
case SIMVAR(arrayCref=SOME(c),aliasvar=NOALIAS()) then
<<
/* <%crefStrNoUnderscore(c)%> */
// #define <%cref(c)%> data->simulationInfo->daeModeData-><%arrayName%>[<%index%>]
/* <%crefStrNoUnderscore(name)%> */
// #define <%cref(name)%> data->simulationInfo->daeModeData-><%arrayName%>[<%index%>]
>>
case SIMVAR(aliasvar=NOALIAS()) then
<<
/* <%crefStrNoUnderscore(name)%> */
// #define <%cref(name)%> data->simulationInfo->daeModeData-><%arrayName%>[<%index%>]
>>
end match
end defineSimVarArray;
// TODO: This is probably not relevant anymore can be removed. We access residual and auxialry
// variables of daemode using cref2simvar now. No need to define them.
template simulationFile_dae_header(SimCode simCode)
"DAEmode header generation"
::=
match simCode
case simCode as SIMCODE(daeModeData=SOME(DAEMODEDATA(residualVars=residualVars,auxiliaryVars=auxiliaryVars))) then
<<
/* residual variable define for daeMode */
<%residualVars |> var =>
defineSimVarArray(var, "residualVars")
;separator="\n"%>
/* auxiliary variable define for daeMode */
<%auxiliaryVars |> var =>
defineSimVarArray(var, "auxiliaryVars")
;separator="\n"%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
case simCode as SIMCODE(__) then
<<
#ifndef <%fileNamePrefix%>_16DAE_H
#define <%fileNamePrefix%>_16DAE_H
#endif
<%\n%>
>>
end match
end simulationFile_dae_header;
template simulationFile_dae(SimCode simCode)
"DAEmode equations generation"
::=
match simCode
case SIMCODE(modelInfo=MODELINFO(vars=SIMVARS(__)),
daeModeData=SOME(DAEMODEDATA(daeEquations=daeEquations, sparsityPattern=sparsityPattern,
algebraicVars=algebraicVars, residualVars=residualVars,
auxiliaryVars=auxiliaryVars))) then
let modelNamePrefixStr = modelNamePrefix(simCode)
let initDAEmode =
match sparsityPattern
case SOME(JAC_MATRIX(sparsity=sparse, coloredCols=colorList, maxColorCols=maxColor)) then
'<%initializeDAEmodeData(listLength(residualVars), algebraicVars, listLength(auxiliaryVars), sparse, colorList, maxColor, modelNamePrefixStr)%>'
case NONE() then
'int <%symbolName(modelNamePrefixStr,"initializeDAEmodeData")%>(DATA *inData, DAEMODE_DATA* daeModeData){ return -1; }'
end match
<<
/* DAE residuals */
<%simulationFileHeader(fileNamePrefix)%>
#include "simulation/solver/dae_mode.h"
#ifdef __cplusplus
extern "C" {
#endif
<%evaluateDAEResiduals(daeEquations, modelNamePrefixStr)%>
<%initDAEmode%>
#ifdef __cplusplus
}
#endif<%\n%>
>>
/* adrpo: leave a newline at the end of file to get rid of the warning */
else
let modelNamePrefixStr = modelNamePrefix(simCode)
<<
/* DAE residuals is empty */
<%simulationFileHeader(fileNamePrefix(simCode))%>
#ifdef __cplusplus
extern "C" {
#endif
int <%symbolName(modelNamePrefixStr,"initializeDAEmodeData")%>(DATA *inData, DAEMODE_DATA* daeModeData){ return -1; }
#ifdef __cplusplus
}
#endif<%\n%>
>>
end match
end simulationFile_dae;
template simulationFile_inl(SimCode simCode)
"DAEmode equations generation"
::=
match simCode
case SIMCODE(modelInfo=MODELINFO(vars=SIMVARS(__)),inlineEquations={}) then
let modelNamePrefixStr = modelNamePrefix(simCode)
<<
/* Inline equation file is empty */
<%simulationFileHeader(fileNamePrefix(simCode))%>
#ifdef __cplusplus
extern "C" {
#endif
int <%symbolName(modelNamePrefixStr,"symbolicInlineSystem")%>(DATA *data, threadData_t *threadData){
<% if intEq(Flags.getConfigEnum(Flags.SYM_SOLVER),0) then
<<
return -1;
>>
%>
<% if intGt(Flags.getConfigEnum(Flags.SYM_SOLVER),0) then
<<
return 0;
>>
%>
}
#ifdef __cplusplus
}
#endif<%\n%>
>>
case SIMCODE(modelInfo=MODELINFO(vars=SIMVARS(__)),inlineEquations=inlineEquations) then
let modelNamePrefixStr = modelNamePrefix(simCode)
let funcNames = (inlineEquations |> eq => equationNames_(eq,contextSimulationNonDiscrete,modelNamePrefixStr); separator="\n")
<<
/* Inline equation file */
<%simulationFileHeader(fileNamePrefix(simCode))%>
#ifdef __cplusplus
extern "C" {
#endif
<%(inlineEquations |> eq hasindex i0 =>
equation_impl(-1, eq, contextSimulationNonDiscrete, modelNamePrefixStr, false)
;separator="\n")%>
/* inline equations*/
int <%symbolName(modelNamePrefixStr,"symbolicInlineSystem")%>(DATA *data, threadData_t *threadData){