/
SimCodeMain.mo
935 lines (853 loc) · 39.2 KB
/
SimCodeMain.mo
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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package SimCodeMain
" file: SimCodeMain.mo
package: SimCodeMain
description: Code generation using Susan templates
The entry points to this module are the translateModel function and the
translateFunctions fuction."
// public imports
public
import Absyn;
import BackendDAE;
import BackendDAEUtil;
import Ceval;
import DAE;
import FCore;
import GlobalScript;
import HashTableExpToIndex;
import Tpl;
import Values;
import SimCode;
// protected imports
protected
import BackendDAECreate;
import Builtin;
import ClockIndexes;
import CevalScriptBackend;
import CodegenC;
import CodegenFMU;
import CodegenFMUCpp;
import CodegenFMUCppHpcom;
import CodegenAdevs;
import CodegenSparseFMI;
import CodegenCSharp;
import CodegenCpp;
import CodegenCppHpcom;
import CodegenXML;
import CodegenJava;
import CodegenJS;
import Config;
import DAEUtil;
import Debug;
import Error;
import ErrorExt;
import Flags;
import FMI;
import GC;
import HpcOmSimCodeMain;
import HpcOmTaskGraph;
import SerializeModelInfo;
import TaskSystemDump;
import SerializeInitXML;
import Serializer;
import SimCodeUtil;
import StringUtil;
import System;
import Util;
import BackendDump;
import ExecStat;
public function createSimulationSettings
input Real startTime;
input Real stopTime;
input Integer inumberOfIntervals;
input Real tolerance;
input String method;
input String options;
input String outputFormat;
input String variableFilter;
input String cflags;
output SimCode.SimulationSettings simSettings;
protected
Real stepSize;
Integer numberOfIntervals;
algorithm
numberOfIntervals := if inumberOfIntervals <= 0 then 1 else inumberOfIntervals;
stepSize := (stopTime - startTime) / intReal(numberOfIntervals);
simSettings := SimCode.SIMULATION_SETTINGS(
startTime, stopTime, numberOfIntervals, stepSize, tolerance,
method, options, outputFormat, variableFilter, cflags);
end createSimulationSettings;
protected function generateModelCodeFMU "
Generates code for a model by creating a SimCode structure and calling the
template-based code generator on it."
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.BackendDAE inInitDAE;
input Boolean inUseHomotopy "true if homotopy(...) is used during initialization";
input Option<BackendDAE.BackendDAE> inInitDAE_lambda0;
input list<BackendDAE.Equation> inRemovedInitialEquationLst;
input list<BackendDAE.Var> inPrimaryParameters "already sorted";
input list<BackendDAE.Var> inAllPrimaryParameters "already sorted";
input Absyn.Program p;
input Absyn.Path className;
input String FMUVersion;
input String FMUType;
input String filenamePrefix;
input SimCode.SimulationSettings simSettings;
output list<String> libs;
output String fileDir;
output Real timeSimCode;
output Real timeTemplates;
protected
list<String> includes,includeDirs;
list<SimCode.Function> functions;
String filename, funcfilename;
SimCode.SimCode simCode;
list<SimCode.RecordDeclaration> recordDecls;
BackendDAE.BackendDAE indexed_dlow,indexed_dlow_1;
Absyn.ComponentRef a_cref;
list<String> libPaths;
tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>> literals;
algorithm
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMCODE);
a_cref := Absyn.pathToCref(className);
fileDir := CevalScriptBackend.getFileDir(a_cref, p);
(libs,libPaths,includes, includeDirs, recordDecls, functions, literals) :=
SimCodeUtil.createFunctions(p, inBackendDAE);
simCode := createSimCode(inBackendDAE, inInitDAE, inUseHomotopy, inInitDAE_lambda0, inRemovedInitialEquationLst, inPrimaryParameters, inAllPrimaryParameters,
className, filenamePrefix, fileDir, functions, includes, includeDirs, libs, libPaths, p, SOME(simSettings), recordDecls, literals, Absyn.FUNCTIONARGS({},{}), isFMU=true, FMUVersion=FMUVersion);
timeSimCode := System.realtimeTock(ClockIndexes.RT_CLOCK_SIMCODE);
ExecStat.execStat("SimCode");
System.realtimeTick(ClockIndexes.RT_CLOCK_TEMPLATES);
callTargetTemplatesFMU(simCode, Config.simCodeTarget(), FMUVersion, FMUType);
timeTemplates := System.realtimeTock(ClockIndexes.RT_CLOCK_TEMPLATES);
end generateModelCodeFMU;
protected function generateModelCodeXML "
Generates code for a model by creating a SimCode structure and calling the
template-based code generator on it."
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.BackendDAE inInitDAE;
input Boolean inUseHomotopy "true if homotopy(...) is used during initialization";
input Option<BackendDAE.BackendDAE> inInitDAE_lambda0;
input list<BackendDAE.Equation> inRemovedInitialEquationLst;
input list<BackendDAE.Var> inPrimaryParameters "already sorted";
input list<BackendDAE.Var> inAllPrimaryParameters "already sorted";
input Absyn.Program p;
input Absyn.Path className;
input String filenamePrefix;
input Option<SimCode.SimulationSettings> simSettingsOpt;
output list<String> libs;
output String fileDir;
output Real timeSimCode;
output Real timeTemplates;
protected
list<String> includes,includeDirs;
list<SimCode.Function> functions;
String filename, funcfilename;
SimCode.SimCode simCode;
list<SimCode.RecordDeclaration> recordDecls;
BackendDAE.BackendDAE indexed_dlow,indexed_dlow_1;
list<String> libPaths;
Absyn.ComponentRef a_cref;
tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>> literals;
list<tuple<String, String>> program;
algorithm
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMCODE);
a_cref := Absyn.pathToCref(className);
fileDir := CevalScriptBackend.getFileDir(a_cref, p);
(libs, libPaths, includes, includeDirs, recordDecls, functions, literals) :=
SimCodeUtil.createFunctions(p, inBackendDAE);
(simCode,_) := SimCodeUtil.createSimCode(inBackendDAE, inInitDAE, inUseHomotopy, inInitDAE_lambda0, inRemovedInitialEquationLst, inPrimaryParameters, inAllPrimaryParameters,
className, filenamePrefix, fileDir, functions, includes, includeDirs, libs,libPaths, p, simSettingsOpt, recordDecls, literals,Absyn.FUNCTIONARGS({},{}));
timeSimCode := System.realtimeTock(ClockIndexes.RT_CLOCK_SIMCODE);
ExecStat.execStat("SimCode");
System.realtimeTick(ClockIndexes.RT_CLOCK_TEMPLATES);
callTargetTemplatesXML(simCode, Config.simCodeTarget());
timeTemplates := System.realtimeTock(ClockIndexes.RT_CLOCK_TEMPLATES);
end generateModelCodeXML;
public function translateModelFMU
"Entry point to translate a Modelica model for FMU export.
Called from other places in the compiler."
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input GlobalScript.SymbolTable inInteractiveSymbolTable;
input String inFMUVersion;
input String inFMUType;
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input SimCode.SimulationSettings inSimSettings;
output FCore.Cache outCache;
output Values.Value outValue;
output GlobalScript.SymbolTable outInteractiveSymbolTable;
output BackendDAE.BackendDAE outBackendDAE;
output list<String> outStringLst;
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(outCache,outValue,outInteractiveSymbolTable,outBackendDAE,outStringLst,outFileDir,resultValues):=
matchcontinue (inCache,inEnv,className,inInteractiveSymbolTable,inFMUVersion,inFMUType,inFileNamePrefix)
local
String FMUVersion,FMUType,filenameprefix,file_dir,resstr;
DAE.DAElist dae;
FCore.Graph graph;
BackendDAE.BackendDAE dlow,dlow_1;
list<String> libs;
GlobalScript.SymbolTable st;
Absyn.Program p;
//DAE.Exp fileprefix;
FCore.Cache cache;
DAE.FunctionTree funcs;
Real timeSimCode, timeTemplates, timeBackend, timeFrontend;
String description;
Boolean symbolicJacActivated;
Boolean fmi20;
Boolean flagValue;
BackendDAE.BackendDAE initDAE;
Option<BackendDAE.BackendDAE> initDAE_lambda0;
Boolean useHomotopy "true if homotopy(...) is used during initialization";
list<BackendDAE.Equation> removedInitialEquationLst;
list<BackendDAE.Var> primaryParameters "already sorted";
list<BackendDAE.Var> allPrimaryParameters "already sorted";
case (cache,graph,_,st as GlobalScript.SYMBOLTABLE(ast=p),FMUVersion,FMUType,filenameprefix)
equation
/* calculate stuff that we need to create SimCode data structure */
System.realtimeTick(ClockIndexes.RT_CLOCK_FRONTEND);
//(cache,Values.STRING(filenameprefix),SOME(_)) = Ceval.ceval(cache,graph, fileprefix, true, SOME(st),NONE(), msg);
(cache,graph,dae,st) = CevalScriptBackend.runFrontEnd(cache,graph,className,st,false);
timeFrontend = System.realtimeTock(ClockIndexes.RT_CLOCK_FRONTEND);
System.realtimeTick(ClockIndexes.RT_CLOCK_BACKEND);
// activate symolic jacobains for fmi 2.0
// to provide dependence information and partial derivatives
fmi20 = FMI.isFMIVersion20(FMUVersion);
symbolicJacActivated = Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION);
Flags.setConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION, fmi20);
if not Flags.isSet(Flags.FMU_EXPERIMENTAL) then
flagValue = Flags.enableDebug(Flags.DIS_SYMJAC_FMI20);
end if;
_ = FCore.getFunctionTree(cache);
dae = DAEUtil.transformationsBeforeBackend(cache,graph,dae);
description = DAEUtil.daeDescription(dae);
dlow = BackendDAECreate.lower(dae, cache, graph, BackendDAE.EXTRA_INFO(description,filenameprefix));
(dlow_1, initDAE, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters) = BackendDAEUtil.getSolvedSystem(dlow, inFileNamePrefix);
timeBackend = System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND);
(libs,file_dir,timeSimCode,timeTemplates) =
generateModelCodeFMU(dlow_1, initDAE, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters, p, className, FMUVersion, FMUType, filenameprefix, inSimSettings);
//reset config flag
Flags.setConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION, symbolicJacActivated);
if not Flags.isSet(Flags.FMU_EXPERIMENTAL) then
Flags.set(Flags.DIS_SYMJAC_FMI20, flagValue);
end if;
resultValues =
{("timeTemplates",Values.REAL(timeTemplates)),
("timeSimCode", Values.REAL(timeSimCode)),
("timeBackend", Values.REAL(timeBackend)),
("timeFrontend", Values.REAL(timeFrontend))
};
resstr = Absyn.pathStringNoQual(className);
resstr = stringAppendList({"SimCode: The model ",resstr," has been translated to FMU"});
then
(cache,Values.STRING(resstr),st,dlow_1,libs,file_dir, resultValues);
else
equation
resstr = Absyn.pathStringNoQual(className);
resstr = stringAppendList({"SimCode: The model ",resstr," could not be translated to FMU"});
Error.addMessage(Error.INTERNAL_ERROR, {resstr});
then
fail();
end matchcontinue;
end translateModelFMU;
public function translateModelXML
"Entry point to translate a Modelica model for XML export.
Called from other places in the compiler."
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input GlobalScript.SymbolTable inInteractiveSymbolTable;
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
output FCore.Cache outCache;
output Values.Value outValue;
output GlobalScript.SymbolTable outInteractiveSymbolTable;
output BackendDAE.BackendDAE outBackendDAE;
output list<String> outStringLst;
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(outCache,outValue,outInteractiveSymbolTable,outBackendDAE,outStringLst,outFileDir,resultValues):=
matchcontinue (inCache,inEnv,className,inInteractiveSymbolTable,inFileNamePrefix,addDummy, inSimSettingsOpt)
local
String filenameprefix,file_dir,resstr,description;
DAE.DAElist dae;
FCore.Graph graph;
BackendDAE.BackendDAE dlow,dlow_1;
list<String> libs;
GlobalScript.SymbolTable st;
Absyn.Program p;
//DAE.Exp fileprefix;
FCore.Cache cache;
DAE.FunctionTree funcs;
Real timeSimCode, timeTemplates, timeBackend, timeFrontend;
BackendDAE.BackendDAE initDAE;
Option<BackendDAE.BackendDAE> initDAE_lambda0;
Boolean useHomotopy "true if homotopy(...) is used during initialization";
list<BackendDAE.Equation> removedInitialEquationLst;
list<BackendDAE.Var> primaryParameters "already sorted";
list<BackendDAE.Var> allPrimaryParameters "already sorted";
case (cache,graph,_,st as GlobalScript.SYMBOLTABLE(ast=p),filenameprefix,_, _)
equation
/* calculate stuff that we need to create SimCode data structure */
System.realtimeTick(ClockIndexes.RT_CLOCK_FRONTEND);
//(cache,Values.STRING(filenameprefix),SOME(_)) = Ceval.ceval(cache,graph, fileprefix, true, SOME(st),NONE(), msg);
(cache,graph,dae,st) = CevalScriptBackend.runFrontEnd(cache,graph,className,st,false);
timeFrontend = System.realtimeTock(ClockIndexes.RT_CLOCK_FRONTEND);
System.realtimeTick(ClockIndexes.RT_CLOCK_BACKEND);
_ = FCore.getFunctionTree(cache);
dae = DAEUtil.transformationsBeforeBackend(cache,graph,dae);
description = DAEUtil.daeDescription(dae);
dlow = BackendDAECreate.lower(dae, cache, graph, BackendDAE.EXTRA_INFO(description,filenameprefix));
(dlow_1, initDAE, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters) = BackendDAEUtil.getSolvedSystem(dlow,inFileNamePrefix);
timeBackend = System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND);
(libs,file_dir,timeSimCode,timeTemplates) =
generateModelCodeXML(dlow_1, initDAE, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters, p, className, filenameprefix, inSimSettingsOpt);
resultValues =
{("timeTemplates",Values.REAL(timeTemplates)),
("timeSimCode", Values.REAL(timeSimCode)),
("timeBackend", Values.REAL(timeBackend)),
("timeFrontend", Values.REAL(timeFrontend))
};
resstr = Absyn.pathStringNoQual(className);
resstr = stringAppendList({"SimCode: The model ",resstr," has been translated to XML"});
then
(cache,Values.STRING(resstr),st,dlow_1,libs,file_dir, resultValues);
case (_,_,_,_,_,_, _)
equation
resstr = Absyn.pathStringNoQual(className);
resstr = stringAppendList({"SimCode: The model ",resstr," could not be translated to XML"});
Error.addMessage(Error.INTERNAL_ERROR, {resstr});
then
fail();
end matchcontinue;
end translateModelXML;
public function generateModelCode "
Generates code for a model by creating a SimCode structure and calling the
template-based code generator on it."
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.BackendDAE inInitDAE;
input Boolean inUseHomotopy "true if homotopy(...) is used during initialization";
input Option<BackendDAE.BackendDAE> inInitDAE_lambda0;
input list<BackendDAE.Equation> inRemovedInitialEquationLst;
input list<BackendDAE.Var> inPrimaryParameters "already sorted";
input list<BackendDAE.Var> inAllPrimaryParameters "already sorted";
input Absyn.Program p;
input Absyn.Path className;
input String filenamePrefix;
input Option<SimCode.SimulationSettings> simSettingsOpt;
input Absyn.FunctionArgs args;
output list<String> libs;
output String fileDir;
output Real timeSimCode;
output Real timeTemplates;
protected
list<String> includes, includeDirs,libPaths;
list<SimCode.Function> functions;
SimCode.SimCode simCode;
list<SimCode.RecordDeclaration> recordDecls;
Absyn.ComponentRef a_cref;
tuple<Integer, HashTableExpToIndex.HashTable, list<DAE.Exp>> literals;
list<tuple<String, String>> program;
algorithm
if Flags.isSet(Flags.GRAPHML) then
HpcOmTaskGraph.dumpTaskGraph(inBackendDAE, filenamePrefix);
end if;
System.realtimeTick(ClockIndexes.RT_CLOCK_SIMCODE);
a_cref := Absyn.pathToCref(className);
fileDir := CevalScriptBackend.getFileDir(a_cref, p);
(libs, libPaths, includes, includeDirs, recordDecls, functions, literals) := SimCodeUtil.createFunctions(p, inBackendDAE);
simCode := createSimCode(inBackendDAE, inInitDAE, inUseHomotopy, inInitDAE_lambda0, inRemovedInitialEquationLst, inPrimaryParameters, inAllPrimaryParameters, className, filenamePrefix, fileDir, functions, includes, includeDirs, libs,libPaths, p, simSettingsOpt, recordDecls, literals, args);
timeSimCode := System.realtimeTock(ClockIndexes.RT_CLOCK_SIMCODE);
ExecStat.execStat("SimCode");
if Flags.isSet(Flags.SERIALIZED_SIZE) then
serializeNotify(simCode, filenamePrefix, "simCode");
ExecStat.execStat("Serialize simCode");
end if;
System.realtimeTick(ClockIndexes.RT_CLOCK_TEMPLATES);
callTargetTemplates(simCode, Config.simCodeTarget());
timeTemplates := System.realtimeTock(ClockIndexes.RT_CLOCK_TEMPLATES);
ExecStat.execStat("Templates");
end generateModelCode;
protected function createSimCode "
SimCode generator switch - if the NUMPROC-Flag is set, the simcode will be extended with parallel informations."
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.BackendDAE inInitDAE;
input Boolean inUseHomotopy "true if homotopy(...) is used during initialization";
input Option<BackendDAE.BackendDAE> inInitDAE_lambda0;
input list<BackendDAE.Equation> inRemovedInitialEquationLst;
input list<BackendDAE.Var> inPrimaryParameters "already sorted";
input list<BackendDAE.Var> inAllPrimaryParameters "already sorted";
input Absyn.Path inClassName;
input String filenamePrefix;
input String inString11;
input list<SimCode.Function> functions;
input list<String> externalFunctionIncludes;
input list<String> includeDirs;
input list<String> libs;
input list<String> libPaths;
input Absyn.Program program;
input Option<SimCode.SimulationSettings> simSettingsOpt;
input list<SimCode.RecordDeclaration> recordDecls;
input tuple<Integer, HashTableExpToIndex.HashTable, list<DAE.Exp>> literals;
input Absyn.FunctionArgs args;
input Boolean isFMU=false;
input String FMUVersion="";
output SimCode.SimCode simCode;
algorithm
simCode := matchcontinue(inBackendDAE, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, libPaths, program,simSettingsOpt, recordDecls, literals, args)
local
Integer numProc;
SimCode.SimCode tmpSimCode;
case(_, _, _, _, _, _, _, _, _, _,_, _, _, _) equation
// MULTI_RATE PARTITIONINIG
true = Flags.isSet(Flags.MULTIRATE_PARTITION);
then HpcOmSimCodeMain.createSimCode(inBackendDAE, inInitDAE, inUseHomotopy, inInitDAE_lambda0, inRemovedInitialEquationLst, inPrimaryParameters, inAllPrimaryParameters, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs,libPaths, program, simSettingsOpt, recordDecls, literals, args);
case(_, _, _, _, _, _, _, _, _, _,_, _, _, _) equation
true = Flags.isSet(Flags.HPCOM);
// either generate code for profiling or for parallel simulation
//bcall(not stringEq("none",Flags.getConfigString(Flags.PROFILING_LEVEL)),print,"Deactivate profiling if you want to simulate in parallel.\n");
//_ = bcallret2((not stringEq("none",Flags.getConfigString(Flags.PROFILING_LEVEL))) and (not stringEq("all_perf",Flags.getConfigString(Flags.PROFILING_LEVEL))),Flags.set,Flags.HPCOM,false,true);
//true = stringEq("none",Flags.getConfigString(Flags.PROFILING_LEVEL)) or stringEq("all_perf",Flags.getConfigString(Flags.PROFILING_LEVEL));
numProc = Flags.getConfigInt(Flags.NUM_PROC);
true = numProc == 0;
print("hpcom computes the ideal number of processors. If you want to set the number manually, use the flag +n=_ \n");
then HpcOmSimCodeMain.createSimCode(inBackendDAE, inInitDAE, inUseHomotopy, inInitDAE_lambda0, inRemovedInitialEquationLst, inPrimaryParameters, inAllPrimaryParameters, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs,libPaths,program, simSettingsOpt, recordDecls, literals, args);
case(_, _, _, _, _, _, _, _, _,_, _, _, _, _) equation
true = Flags.isSet(Flags.HPCOM);
// either generate code for profiling or for parallel simulation
//bcall((not stringEq("none",Flags.getConfigString(Flags.PROFILING_LEVEL))) and (not stringEq("all_perf",Flags.getConfigString(Flags.PROFILING_LEVEL))),print,"Deactivate profiling if you want to simulate in parallel.\n");
//_ = bcallret2(not stringEq("none",Flags.getConfigString(Flags.PROFILING_LEVEL)),Flags.set,Flags.HPCOM,false,true);
//true = stringEq("none",Flags.getConfigString(Flags.PROFILING_LEVEL)) or stringEq("all_perf",Flags.getConfigString(Flags.PROFILING_LEVEL));
numProc = Flags.getConfigInt(Flags.NUM_PROC);
true = (numProc > 0);
then HpcOmSimCodeMain.createSimCode(inBackendDAE, inInitDAE, inUseHomotopy, inInitDAE_lambda0, inRemovedInitialEquationLst, inPrimaryParameters, inAllPrimaryParameters, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs, libPaths,program,simSettingsOpt, recordDecls, literals, args);
else equation
(tmpSimCode, _) = SimCodeUtil.createSimCode(inBackendDAE, inInitDAE, inUseHomotopy, inInitDAE_lambda0, inRemovedInitialEquationLst, inPrimaryParameters, inAllPrimaryParameters, inClassName, filenamePrefix, inString11, functions, externalFunctionIncludes, includeDirs, libs,libPaths,program, simSettingsOpt, recordDecls, literals, args, isFMU=isFMU, FMUVersion=FMUVersion);
then tmpSimCode;
end matchcontinue;
end createSimCode;
// TODO: use another switch ... later make it first class option like -target or so
protected function callTargetTemplates "
Generate target code by passing the SimCode data structure to templates."
input SimCode.SimCode simCode;
input String target;
protected
partial function Func
input Tpl.Text txt;
input SimCode.SimCode a_simCode;
output Tpl.Text out_txt;
end Func;
partial function FuncText
input Tpl.Text txt;
output Tpl.Text out_txt;
end FuncText;
partial function BoolFunc
input Tpl.Text txt;
input SimCode.SimCode a_simCode;
output Tpl.Text out_txt;
end BoolFunc;
Func func;
Tpl.Text txt;
function runTplWriteFile
extends PartialRunTpl;
input FuncText func;
input String file;
protected
Integer nErr;
algorithm
res := (false,{});
try
SimCodeUtil.resetFunctionIndex();
if Config.acceptMetaModelicaGrammar() or Flags.isSet(Flags.GEN_DEBUG_SYMBOLS) then
Tpl.textFileConvertLines(Tpl.tplCallWithFailErrorNoArg(func), file);
else
nErr := Error.getNumErrorMessages();
Tpl.closeFile(Tpl.tplCallWithFailErrorNoArg(func,Tpl.redirectToFile(Tpl.emptyTxt, file)));
Tpl.failIfTrue(Error.getNumErrorMessages() > nErr);
end if;
res := (true,SimCodeUtil.getFunctionIndex());
else
ErrorExt.moveMessagesToParentThread();
end try;
end runTplWriteFile;
function runTpl
extends PartialRunTpl;
input FuncText func;
algorithm
res := (false,{});
try
SimCodeUtil.resetFunctionIndex();
Tpl.tplCallWithFailErrorNoArg(func);
res := (true,SimCodeUtil.getFunctionIndex());
else
ErrorExt.moveMessagesToParentThread();
end try;
end runTpl;
function runToStr
extends PartialRunTpl;
input Func func;
partial function Func
output String str;
end Func;
algorithm
res := (false,{});
try
SimCodeUtil.resetFunctionIndex();
func();
res := (true,SimCodeUtil.getFunctionIndex());
else
ErrorExt.moveMessagesToParentThread();
end try;
end runToStr;
function runCodegenFunc
input PartialRunTpl func;
output tuple<Boolean,list<String>> res;
protected
Boolean b;
algorithm
(res as (b,_)) := func();
if not b then
print(System.dladdr(func) + " failed\n");
end if;
end runCodegenFunc;
function runToBoolean
input Func func;
output tuple<Boolean,list<String>> res;
protected
partial function Func
output Boolean b;
end Func;
algorithm
res := (func(),{});
end runToBoolean;
partial function PartialRunTpl
output tuple<Boolean,list<String>> res;
end PartialRunTpl;
algorithm
setGlobalRoot(Global.optionSimCode, SOME(simCode));
_ := match target
local
String str, guid;
list<PartialRunTpl> codegenFuncs;
Integer numThreads;
list<tuple<Boolean,list<String>>> res;
list<String> strs, tmp;
case "CSharp" equation
Tpl.tplNoret(CodegenCSharp.translateModel, simCode);
then ();
case "Cpp" equation
callTargetTemplatesCPP(simCode);
then ();
case "Adevs" equation
Tpl.tplNoret(CodegenAdevs.translateModel, simCode);
then ();
case "sfmi" equation
Tpl.tplNoret3(CodegenSparseFMI.translateModel, simCode, "2.0", "me");
then ();
case "C"
algorithm
guid := System.getUUIDStr();
System.realtimeTick(ClockIndexes.RT_PROFILER0);
codegenFuncs := {};
codegenFuncs := (function runToBoolean(func=function SerializeInitXML.simulationInitFileReturnBool(simCode=simCode, guid=guid))) :: codegenFuncs;
dumpTaskSystemIfFlag(simCode);
codegenFuncs := (function runTpl(func=function CodegenC.translateModel(in_a_simCode=simCode))) :: codegenFuncs;
for f in {
// external objects
(CodegenC.simulationFile_exo, "_01exo.c"),
// non-linear systems
(CodegenC.simulationFile_nls, "_02nls.c"),
(CodegenC.simulationFile_lsy, "_03lsy.c"),
(CodegenC.simulationFile_set, "_04set.c"),
(CodegenC.simulationFile_evt, "_05evt.c"),
(CodegenC.simulationFile_inz, "_06inz.c"),
(CodegenC.simulationFile_dly, "_07dly.c"),
(CodegenC.simulationFile_bnd, "_08bnd.c"),
(CodegenC.simulationFile_alg, "_09alg.c"),
(CodegenC.simulationFile_asr, "_10asr.c"),
(CodegenC.simulationFile_jac, "_12jac.c"),
(CodegenC.simulationFile_jac_header, "_12jac.h"),
(CodegenC.simulationFile_opt, "_13opt.c"),
(CodegenC.simulationFile_opt_header, "_13opt.h"),
(CodegenC.simulationFile_lnz, "_14lnz.c"),
(CodegenC.simulationFile_syn, "_15syn.c"),
(CodegenC.simulationFile_dae, "_16dae.c"),
(CodegenC.simulationFile_dae_header, "_16dae.h"),
(CodegenC.simulationHeaderFile, "_model.h")
} loop
(func,str) := f;
codegenFuncs := (function runTplWriteFile(func=function func(a_simCode=simCode), file=simCode.fileNamePrefix + str)) :: codegenFuncs;
end for;
codegenFuncs := (function runTpl(func=function CodegenC.simulationFile_mixAndHeader(a_simCode=simCode, a_modelNamePrefix=simCode.fileNamePrefix))) :: codegenFuncs;
codegenFuncs := (function runTplWriteFile(func=function CodegenC.simulationFile(in_a_simCode=simCode, in_a_guid=guid, in_a_isModelExchangeFMU=false), file=simCode.fileNamePrefix + ".c")) :: codegenFuncs;
codegenFuncs := (function runTplWriteFile(func=function CodegenC.simulationFunctionsFile(a_filePrefix=simCode.fileNamePrefix, a_functions=simCode.modelInfo.functions), file=simCode.fileNamePrefix + "_functions.c")) :: codegenFuncs;
codegenFuncs := (function runToStr(func=function SerializeModelInfo.serialize(code=simCode, withOperations=Flags.isSet(Flags.INFO_XML_OPERATIONS)))) :: codegenFuncs;
// Test the parallel code generator in the test suite. Should give decent results given that the task is disk-intensive.
numThreads := max(1, if Config.getRunningTestsuite() then min(2, System.numProcessors()) else Config.noProc());
if (not Flags.isSet(Flags.PARALLEL_CODEGEN)) or numThreads==1 then
res := list(func() for func in codegenFuncs);
else
res := System.launchParallelTasks(numThreads, codegenFuncs, runCodegenFunc);
end if;
strs := {};
for tpl in res loop
(true,tmp) := tpl;
strs := List.append_reverse(tmp, strs);
end for;
strs := listReverse(strs);
// write the makefile last!
Tpl.closeFile(Tpl.tplCallWithFailError3(CodegenC.simulationMakefile,Config.simulationCodeTarget(),simCode,strs,txt=Tpl.redirectToFile(Tpl.emptyTxt, simCode.fileNamePrefix+".makefile")));
then ();
case "JavaScript" equation
guid = System.getUUIDStr();
Tpl.tplNoret(CodegenC.translateModel, simCode);
SerializeInitXML.simulationInitFile(simCode, guid);
System.covertTextFileToCLiteral(simCode.fileNamePrefix+"_init.xml",simCode.fileNamePrefix+"_init.c", Config.simulationCodeTarget());
SerializeModelInfo.serialize(simCode, Flags.isSet(Flags.INFO_XML_OPERATIONS));
Tpl.tplNoret(CodegenJS.markdownFile, simCode);
then ();
case "XML" equation
Tpl.tplNoret(CodegenXML.translateModel, simCode);
then ();
case "Java" equation
Tpl.tplNoret(CodegenJava.translateModel, simCode);
then ();
case "None"
then ();
else equation
str = "Unknown template target: " + target;
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end match;
setGlobalRoot(Global.optionSimCode, NONE());
end callTargetTemplates;
protected function dumpTaskSystemIfFlag
input SimCode.SimCode simCode;
algorithm
if Flags.isSet(Flags.PARMODAUTO) then
Tpl.tplNoret2(TaskSystemDump.dumpTaskSystem, simCode, Flags.isSet(Flags.INFO_XML_OPERATIONS));
end if;
end dumpTaskSystemIfFlag;
protected function callTargetTemplatesCPP
input SimCode.SimCode iSimCode;
algorithm
if(Flags.isSet(Flags.HPCOM)) then
Tpl.tplNoret(CodegenCppHpcom.translateModel, iSimCode);
else
Tpl.tplNoret(CodegenCpp.translateModel, iSimCode);
end if;
end callTargetTemplatesCPP;
protected function callTargetTemplatesFMU
"Generate target code by passing the SimCode data structure to templates."
input SimCode.SimCode simCode;
input String target;
input String FMUVersion;
input String FMUType;
algorithm
setGlobalRoot(Global.optionSimCode, SOME(simCode));
_ := match (simCode,target)
local
String str;
String fmutmp;
Boolean b;
case (SimCode.SIMCODE(),"C")
algorithm
fmutmp := simCode.fileNamePrefix + ".fmutmp";
if System.directoryExists(fmutmp) then
if not System.removeDirectory(fmutmp) then
print("Failed to remove directory: " + fmutmp + "\n");
fail();
end if;
end if;
Util.createDirectoryTree(fmutmp + "/sources/include/");
SerializeModelInfo.serialize(simCode, Flags.isSet(Flags.INFO_XML_OPERATIONS));
str := fmutmp + "/sources/" + simCode.fileNamePrefix;
b := System.covertTextFileToCLiteral(simCode.fileNamePrefix+"_info.json", str+"_info.c", Flags.getConfigString(Flags.TARGET));
if not b then
Error.addMessage(Error.INTERNAL_ERROR, {"System.covertTextFileToCLiteral failed. Could not write "+str+"_info.c\n"});
fail();
end if;
SimCodeUtil.resetFunctionIndex();
Tpl.tplNoret3(CodegenFMU.translateModel, simCode, FMUVersion, FMUType);
Tpl.closeFile(Tpl.tplCallWithFailError4(CodegenFMU.fmuMakefile,Config.simulationCodeTarget(),simCode,FMUVersion,SimCodeUtil.getFunctionIndex(),txt=Tpl.redirectToFile(Tpl.emptyTxt, simCode.fileNamePrefix+".fmutmp/sources/Makefile.in")));
then ();
case (_,"Cpp")
equation
if(Flags.isSet(Flags.HPCOM)) then
Tpl.tplNoret3(CodegenFMUCppHpcom.translateModel, simCode, FMUVersion, FMUType);
else
Tpl.tplNoret3(CodegenFMUCpp.translateModel, simCode, FMUVersion, FMUType);
end if;
then ();
else
equation
str = "Unknown FMU template target: " + target;
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end match;
setGlobalRoot(Global.optionSimCode, NONE());
end callTargetTemplatesFMU;
protected function callTargetTemplatesXML
"Generate target code by passing the SimCode data structure to templates."
input SimCode.SimCode simCode;
input String target;
algorithm
Tpl.tplNoret(CodegenXML.translateModel, simCode);
end callTargetTemplatesXML;
public function translateModel "
Entry point to translate a Modelica model for simulation.
Called from other places in the compiler."
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input GlobalScript.SymbolTable inInteractiveSymbolTable;
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
input Absyn.FunctionArgs args "labels for remove terms";
output FCore.Cache outCache;
output GlobalScript.SymbolTable outInteractiveSymbolTable;
output BackendDAE.BackendDAE outBackendDAE;
output list<String> outStringLst;
output String outFileDir;
output list<tuple<String, Values.Value>> resultValues;
protected
Boolean generateFunctions = false;
algorithm
(outCache, outInteractiveSymbolTable, outBackendDAE, outStringLst, outFileDir, resultValues) :=
matchcontinue (inCache, inEnv, className, inInteractiveSymbolTable, inFileNamePrefix, addDummy, inSimSettingsOpt, args)
local
String filenameprefix, file_dir, resstr, description;
DAE.DAElist dae;
FCore.Graph graph;
BackendDAE.BackendDAE dlow, dlow_1;
list<String> libs;
GlobalScript.SymbolTable st;
Absyn.Program p;
//DAE.Exp fileprefix;
FCore.Cache cache;
Real timeSimCode, timeTemplates, timeBackend, timeFrontend;
BackendDAE.BackendDAE initDAE;
Option<BackendDAE.BackendDAE> initDAE_lambda0;
Boolean useHomotopy "true if homotopy(...) is used during initialization";
list<BackendDAE.Equation> removedInitialEquationLst;
list<BackendDAE.Var> primaryParameters "already sorted";
list<BackendDAE.Var> allPrimaryParameters "already sorted";
Real fsize;
case (cache, graph, _, (st as GlobalScript.SYMBOLTABLE(ast=p)), filenameprefix, _, _, _) equation
// calculate stuff that we need to create SimCode data structure
System.realtimeTick(ClockIndexes.RT_CLOCK_FRONTEND);
ExecStat.execStatReset();
(cache, graph, dae, st) = CevalScriptBackend.runFrontEnd(cache, graph, className, st, false);
ExecStat.execStat("FrontEnd");
if Flags.isSet(Flags.SERIALIZED_SIZE) then
serializeNotify(dae, filenameprefix, "dae");
serializeNotify(graph, filenameprefix, "graph");
serializeNotify(cache, filenameprefix, "cache");
serializeNotify(st, filenameprefix, "st");
ExecStat.execStat("Serialize FrontEnd");
end if;
timeFrontend = System.realtimeTock(ClockIndexes.RT_CLOCK_FRONTEND);
System.realtimeTick(ClockIndexes.RT_CLOCK_BACKEND);
dae = DAEUtil.transformationsBeforeBackend(cache, graph, dae);
ExecStat.execStat("Transformations before backend");
if Flags.isSet(Flags.SERIALIZED_SIZE) then
serializeNotify(dae, filenameprefix, "dae2");
ExecStat.execStat("Serialize DAE (2)");
end if;
generateFunctions = Flags.set(Flags.GEN, false);
// We should not need to lookup constants and classes in the backend,
// so let's free up the old graph and just make it the initial environment.
if not Flags.isSet(Flags.BACKEND_KEEP_ENV_GRAPH) then
(cache,graph) = Builtin.initialGraph(cache);
end if;
description = DAEUtil.daeDescription(dae);
dlow = BackendDAECreate.lower(dae, cache, graph, BackendDAE.EXTRA_INFO(description,filenameprefix));
GC.free(dae);
if Flags.isSet(Flags.SERIALIZED_SIZE) then
serializeNotify(dlow, filenameprefix, "dlow");
ExecStat.execStat("Serialize dlow");
end if;
//BackendDump.printBackendDAE(dlow);
(dlow, initDAE, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters) = BackendDAEUtil.getSolvedSystem(dlow,inFileNamePrefix);
timeBackend = System.realtimeTock(ClockIndexes.RT_CLOCK_BACKEND);
if Flags.isSet(Flags.SERIALIZED_SIZE) then
serializeNotify(dlow, filenameprefix, "simDAE");
serializeNotify(initDAE, filenameprefix, "initDAE");
serializeNotify(initDAE_lambda0, filenameprefix, "initDAE_lambda0");
serializeNotify(removedInitialEquationLst, filenameprefix, "removedInitialEquationLst");
serializeNotify(primaryParameters, filenameprefix, "primaryParameters");
serializeNotify(allPrimaryParameters, filenameprefix, "allPrimaryParameters");
ExecStat.execStat("Serialize solved system");
end if;
(libs, file_dir, timeSimCode, timeTemplates) =
generateModelCode(dlow, initDAE, useHomotopy, initDAE_lambda0, removedInitialEquationLst, primaryParameters, allPrimaryParameters, p, className, filenameprefix, inSimSettingsOpt, args);
resultValues = {("timeTemplates", Values.REAL(timeTemplates)),
("timeSimCode", Values.REAL(timeSimCode)),
("timeBackend", Values.REAL(timeBackend)),
("timeFrontend", Values.REAL(timeFrontend))};
then (cache, st, dlow, libs, file_dir, resultValues);
case (_, _, _, _, _, _, _, _) equation
if generateFunctions then
Flags.set(Flags.GEN, true);
end if;
true = Flags.isSet(Flags.FAILTRACE);
resstr = Absyn.pathStringNoQual(className);
resstr = stringAppendList({"SimCode: The model ", resstr, " could not be translated"});
Error.addMessage(Error.INTERNAL_ERROR, {resstr});
then fail();
end matchcontinue;
if generateFunctions then
Flags.set(Flags.GEN, true);
end if;
end translateModel;
protected function serializeNotify<T>
input T data;
input String prefix;
input String name;
protected
Real fsize;
algorithm
Serializer.outputFile(data, prefix + "_"+name+".bin");
(,fsize,) := System.stat(prefix + "_"+name+".bin");
Error.addMessage(Error.SERIALIZED_SIZE, {name, StringUtil.bytesToReadableUnit(fsize)});
end serializeNotify;
annotation(__OpenModelica_Interface="backend");
end SimCodeMain;