forked from OpenModelica/OpenModelica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSimCode.mo
831 lines (765 loc) · 39.3 KB
/
NSimCode.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
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-CurrentYear, 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 NSimCode
"file: NSimCode.mo
package: NSimCode
description: This file contains the main data type for the backend containing
all data. It further contains the lower and solve main function.
"
protected
// OF imports
import Absyn;
import AbsynUtil;
import OldExpression = Expression;
// NF imports
import BuiltinCall = NFBuiltinCall;
import Call = NFCall;
import ComponentRef = NFComponentRef;
import ConvertDAE = NFConvertDAE;
import NFTyping.ExpOrigin;
import Expression = NFExpression;
import NFFunction.Function;
import FunctionTree = NFFlatten.FunctionTree;
import NFInstNode.InstNode;
import Type = NFType;
// Old Backend imports
import OldBackendDAE = BackendDAE;
// Backend imports
import AliasInfo = NBStrongComponent.AliasInfo;
import BackendDAE = NBackendDAE;
import BEquation = NBEquation;
import NBEquation.{Equation, EquationPointer, EquationPointers, EqData};
import NBEvents.EventInfo;
import NBVariable.{VariablePointers, VarData};
import BVariable = NBVariable;
import System = NBSystem;
// SimCode imports
import SimCodeUtil = NSimCodeUtil;
import NSimJacobian.SimJacobian;
import SimGenericCall = NSimGenericCall;
import SimStrongComponent = NSimStrongComponent;
import NSimVar.{SimVar, SimVars, VarInfo, ExtObjInfo};
import SymbolTable;
// Old SimCode imports
import HpcOmSimCode;
import OldSimCode = SimCode;
import OldSimCodeFunction = SimCodeFunction;
import OldSimCodeFunctionUtil = SimCodeFunctionUtil;
import OldSimCodeUtil = SimCodeUtil;
// Util imports
import Error;
import StringUtil;
// Script imports
import CevalScriptBackend;
public
uniontype SimCodeIndices
record SIM_CODE_INDICES
"Unique simulation code indices"
Integer uniqueIndex;
Integer realVarIndex;
Integer integerVarIndex;
Integer booleanVarIndex;
Integer stringVarIndex;
Integer realParamIndex;
Integer integerParamIndex;
Integer booleanParamIndex;
Integer stringParamIndex;
Integer realAliasIndex;
Integer integerAliasIndex;
Integer booleanAliasIndex;
Integer stringAliasIndex;
Integer equationIndex;
Integer linearSystemIndex;
Integer nonlinearSystemIndex;
Integer jacobianIndex;
Integer residualIndex;
Integer implicitIndex; // this can be removed i think -> moved to solve
//Integer genericCallIndex; this is replaced by UnorderedMap.size(generic_call_map)
Integer extObjIndex;
UnorderedMap<AliasInfo, Integer> alias_map;
UnorderedMap<Identifier, Integer> generic_call_map;
end SIM_CODE_INDICES;
end SimCodeIndices;
uniontype Identifier
record IDENTIFIER
Pointer<Equation> eqn;
ComponentRef var_cref;
end IDENTIFIER;
function toString
input Identifier ident;
output String str = "cref: " + ComponentRef.toString(ident.var_cref) + "\neqn: " + Equation.pointerToString(ident.eqn);
end toString;
function hash
input Identifier ident;
output Integer i = stringHashDjb2(toString(ident));
end hash;
function isEqual
input Identifier ident1;
input Identifier ident2;
output Boolean b = Equation.equalName(ident1.eqn, ident2.eqn) and ComponentRef.isEqual(ident1.var_cref, ident2.var_cref);
end isEqual;
end Identifier;
function EMPTY_SIM_CODE_INDICES
output SimCodeIndices indices = SIM_CODE_INDICES(
1,
0,0,0,0,
0,0,0,0,
0,0,0,0,
1,0,0,
0,0,0,0,
UnorderedMap.new<Integer>(AliasInfo.hash, AliasInfo.isEqual),
UnorderedMap.new<Integer>(Identifier.hash, Identifier.isEqual)
);
end EMPTY_SIM_CODE_INDICES;
uniontype SimCode
record SIM_CODE
ModelInfo modelInfo;
list<Expression> literals "shared literals";
list<SimCodeFunction.RecordDeclaration> recordDecls;
list<String> externalFunctionIncludes "Names of all external functions that are called";
list<SimGenericCall> generic_loop_calls "Generic for-loop and array calls";
list<SimStrongComponent.Block> independent "state and strictly input dependent variables. they are not inserted into any partion";
list<SimStrongComponent.Block> allSim "All simulation system blocks";
list<list<SimStrongComponent.Block>> ode "Only ode blocks for integrator";
list<list<SimStrongComponent.Block>> algebraic "Additional purely algebraic blocks";
//list<ClockedPartition> clockedPartitions;
list<SimStrongComponent.Block> nominal "Blocks for nominal value equations";
list<SimStrongComponent.Block> min "Blocks for min value equations";
list<SimStrongComponent.Block> max "Blocks for max value equations";
list<SimStrongComponent.Block> param "Blocks for parameter equations";
list<SimStrongComponent.Block> no_ret "Blocks for equations without return value";
list<SimStrongComponent.Block> algorithms "Blocks for algorithms and asserts";
list<SimStrongComponent.Block> event_blocks "Blocks for zero crossing functions";
list<SimStrongComponent.Block> jac_blocks "Blocks for jacobian equations";
list<SimStrongComponent.Block> start "Blocks for start value equations";
list<SimStrongComponent.Block> init "Blocks for initial equations";
list<SimStrongComponent.Block> init_0 "Blocks for initial lambda 0 equations (homotopy)";
list<SimStrongComponent.Block> init_no_ret "Blocks for initial equations without return value";
//list<DAE.Statement> algorithmAndEquationAsserts;
//list<StateSet> stateSets;
//list<DAE.Constraint> constraints;
//list<DAE.ClassAttributes> classAttributes;
list<ComponentRef> discreteVars "List of discrete variables";
ExtObjInfo extObjInfo;
OldSimCodeFunction.MakefileParams makefileParams;
//DelayedExpression delayedExps;
list<SimJacobian> jacobians "List of symbolic jacobians";
Option<OldSimCode.SimulationSettings> simulationSettingsOpt; // replace this with new struct
String fileNamePrefix;//, fullPathPrefix "Used in FMI where files are generated in a special directory";
//String fmuTargetName;
//HpcOmSimCode.HpcOmData hpcomData;
//AvlTreeCRToInt.Tree valueReferences "Used in FMI";
//maps each variable to an array of storage indices (with this information, arrays must not be unrolled) and a list for the array-dimensions
//if the variable is not part of an array (if it is a scalar value), then the array has size 1
//HashTableCrIListArray.HashTable varToArrayIndexMapping;
//*** a protected section *** not exported to SimCodeTV
//HashTableCrILst.HashTable varToIndexMapping;
UnorderedMap<ComponentRef, SimVar> simcode_map;
UnorderedMap<ComponentRef, SimStrongComponent.Block> equation_map;
//HashTable.HashTable crefToClockIndexHT "map variables to clock indices";
//Option<BackendMapping> backendMapping;
//FMI 2.0 data for model structure
//Option<FmiModelStructure> modelStructure;
//PartitionData partitionData;
EventInfo eventInfo;
Option<DaeModeData> daeModeData "Simulation system in case of DAEMode";
list<SimStrongComponent.Block> inlineEquations; // ToDo: what exactly is this?
//Option<OMSIData> omsiData "used for OMSI to generate equations code";
end SIM_CODE;
function toString
input SimCode simCode;
input output String str = "";
protected
Integer idx = 1;
algorithm
str := StringUtil.headline_1("SimCode " + str + "(" + simCode.fileNamePrefix + ")");
str := str + ModelInfo.toString(simCode.modelInfo);
str := str + ExtObjInfo.toString(simCode.extObjInfo);
if not listEmpty(simCode.init_0) then
str := str + SimStrongComponent.Block.listToString(simCode.init_0, " ", "Initial Partition (Lambda = 0)") + "\n";
end if;
str := str + SimStrongComponent.Block.listToString(simCode.init, " ", "Initial Partition") + "\n";
for blck_lst in simCode.ode loop
str := str + SimStrongComponent.Block.listToString(blck_lst, " ", "ODE Partition " + intString(idx)) + "\n";
idx := idx + 1;
end for;
idx := 1;
for blck_lst in simCode.algebraic loop
str := str + SimStrongComponent.Block.listToString(blck_lst, " ", "Algebraic Partition " + intString(idx)) + "\n";
idx := idx + 1;
end for;
str := str + SimStrongComponent.Block.listToString(simCode.allSim, " ", "Event Partition") + "\n";
if not listEmpty(simCode.generic_loop_calls) then
str := str + StringUtil.headline_3("Generic Calls");
str := str + List.toString(simCode.generic_loop_calls, SimGenericCall.toString, "", " ", "\n ", "\n\n");
end if;
if isSome(simCode.daeModeData) then
str := str + DaeModeData.toString(Util.getOption(simCode.daeModeData)) + "\n";
end if;
for jac in simCode.jacobians loop
str := str + SimJacobian.toString(jac);
end for;
str := str + EventInfo.toString(simCode.eventInfo);
//str := str + SimStrongComponent.Block.listToString(simCode.no_ret, " ", "REMOVED / ALIAS / KNOWN") + "\n";
end toString;
function create
input BackendDAE bdae;
input Absyn.Path name;
input String fileNamePrefix;
input Option<OldSimCode.SimulationSettings> simSettingsOpt;
output SimCode simCode;
algorithm
simCode := match bdae
local
// auxillaries
VarData varData;
EqData eqData;
FunctionTree funcTree;
VariablePointers residual_vars;
SimVars vars;
// old SimCode strcutures
Absyn.Program program;
list<String> libs, includes, includeDirs, libPaths;
String directory;
OldSimCodeFunction.MakefileParams makefileParams;
list<OldSimCodeFunction.Function> functions;
list<OldSimCodeFunction.RecordDeclaration> recordDecls;
//tuple<Integer, HashTableExpToIndex.HashTable, list<DAE.Exp>> literals;
// New SimCode structures
ModelInfo modelInfo;
SimCodeIndices simCodeIndices;
list<Expression> literals;
list<String> externalFunctionIncludes;
list<SimGenericCall> generic_loop_calls;
list<SimStrongComponent.Block> independent, allSim = {}, nominal, min, max, param, no_ret, algorithms;
list<SimStrongComponent.Block> event_blocks = {}, jac_blocks;
list<SimStrongComponent.Block> init, init_0, init_no_ret, start;
list<list<SimStrongComponent.Block>> ode, algebraic;
list<SimStrongComponent.Block> linearLoops, nonlinearLoops;
list<ComponentRef> discreteVars;
ExtObjInfo extObjInfo;
list<SimJacobian> jacobians;
UnorderedMap<ComponentRef, SimVar> simcode_map;
UnorderedMap<ComponentRef, SimStrongComponent.Block> equation_map;
Option<DaeModeData> daeModeData;
SimJacobian jacA, jacB, jacC, jacD, jacF, jacH;
list<SimStrongComponent.Block> inlineEquations; // ToDo: what exactly is this?
case BackendDAE.MAIN(varData = varData as BVariable.VAR_DATA_SIM(), eqData = eqData as BEquation.EQ_DATA_SIM())
algorithm
// somehow this cannot be set at definition (metamodelica bug?)
simCodeIndices := EMPTY_SIM_CODE_INDICES();
funcTree := BackendDAE.getFunctionTree(bdae);
// create sim vars before everything else
residual_vars := BackendDAE.getLoopResiduals(bdae);
(vars, simCodeIndices) := SimVars.create(varData, residual_vars, simCodeIndices);
(extObjInfo, vars, simCodeIndices) := ExtObjInfo.create(varData.external_objects, vars, simCodeIndices);
simcode_map := SimCodeUtil.createSimCodeMap(vars, extObjInfo);
// create empty equation map and fill while creating the blocks
equation_map := UnorderedMap.new<SimStrongComponent.Block>(ComponentRef.hash, ComponentRef.isEqual);
literals := {};
externalFunctionIncludes := {};
independent := {};
nominal := {};
min := {};
max := {};
// all non constant parameter equations will be added to the initial system.
// There is no actual need for parameter equations block
param := {};
algorithms := {};
// init before everything else!
(init, simCodeIndices) := SimStrongComponent.Block.createInitialBlocks(bdae.init, simCodeIndices, simcode_map, equation_map);
if isSome(bdae.init_0) then
(init_0, simCodeIndices) := SimStrongComponent.Block.createInitialBlocks(Util.getOption(bdae.init_0), simCodeIndices, simcode_map, equation_map);
else
init_0 := {};
end if;
// start allSim with no return equations
(no_ret, simCodeIndices) := SimStrongComponent.Block.createNoReturnBlocks(eqData.removed, simCodeIndices, NBSystem.SystemType.ODE, simcode_map, equation_map);
init_no_ret := {};
start := {};
discreteVars := {};
jacobians := {};
if isSome(bdae.dae) then
// DAEMode
ode := {};
if not listEmpty(no_ret) then
algebraic := {no_ret};
else
algebraic := {};
end if;
(daeModeData, simCodeIndices) := DaeModeData.create(Util.getOption(bdae.dae), simCodeIndices, simcode_map, equation_map);
else
// Normal Simulation
daeModeData := NONE();
(ode, allSim, simCodeIndices) := SimStrongComponent.Block.createBlocks(bdae.ode, allSim, simCodeIndices, simcode_map, equation_map);
(algebraic, allSim, simCodeIndices) := SimStrongComponent.Block.createBlocks(bdae.algebraic, allSim, simCodeIndices, simcode_map, equation_map);
(ode, allSim, event_blocks, simCodeIndices) := SimStrongComponent.Block.createDiscreteBlocks(bdae.ode_event, ode, allSim, event_blocks, simCodeIndices, simcode_map, equation_map);
(algebraic, allSim, event_blocks, simCodeIndices) := SimStrongComponent.Block.createDiscreteBlocks(bdae.alg_event, algebraic, allSim, event_blocks, simCodeIndices, simcode_map, equation_map);
if not listEmpty(no_ret) then
algebraic := no_ret :: algebraic;
allSim := listAppend(no_ret, allSim);
end if;
end if;
// ToDo add event system
inlineEquations := {};
// ToDo:
// this has to be adapted at some point SimCodeFuntion needs to be translated
// to new simcode and literals have to be based on new Expressions.
// Will probably be mostly the same in all other regards
program := SymbolTable.getAbsyn();
directory := CevalScriptBackend.getFileDir(AbsynUtil.pathToCref(name), program);
(libs, libPaths, _, includeDirs, recordDecls, functions, _) := OldSimCodeUtil.createFunctions(program, ConvertDAE.convertFunctionTree(funcTree));
makefileParams := OldSimCodeFunctionUtil.createMakefileParams(includeDirs, libs, libPaths, false, false);
// This needs to be done after the variables have been created by ModelInfo.create()
// for now do not allow dae mode -- this has to be fixed and redesigned to fit before modelInfo!
//if isSome(bdae.dae) then
//(daeModeData, modelInfo, jacA, simcode_map, simCodeIndices) := DaeModeData.createSparsityJacobian(daeModeData, modelInfo, Util.getOption(bdae.dae), simcode_map, simCodeIndices);
//else
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := collectAlgebraicLoops(init, ode, algebraic, daeModeData, simCodeIndices, simcode_map);
for jac in jacobians loop
if Util.isSome(jac.jac_map) then
vars := SimVars.addSeedAndJacobianVars(vars, UnorderedMap.toList(Util.getOption(jac.jac_map)));
end if;
end for;
(jacA, simCodeIndices) := SimJacobian.createSimulationJacobian(bdae.ode, bdae.ode_event, simCodeIndices, simcode_map);
(jacB, simCodeIndices) := SimJacobian.empty("B", simCodeIndices);
(jacC, simCodeIndices) := SimJacobian.empty("C", simCodeIndices);
(jacD, simCodeIndices) := SimJacobian.empty("D", simCodeIndices);
(jacF, simCodeIndices) := SimJacobian.empty("F", simCodeIndices);
(jacH, simCodeIndices) := SimJacobian.empty("H", simCodeIndices);
//jacobians := jacA :: jacB :: jacC :: jacD :: jacF :: jacobians;
jacobians := listReverse(jacH :: jacF :: jacD :: jacC :: jacB :: jacA :: jacobians);
// jacobian blocks only from simulation jacobians
jac_blocks := SimJacobian.getJacobiansBlocks({jacA, jacB, jacC, jacD, jacF, jacH});
(jac_blocks, simCodeIndices) := SimStrongComponent.Block.fixIndices(jac_blocks, {}, simCodeIndices);
generic_loop_calls := list(SimGenericCall.fromIdentifier(tpl) for tpl in UnorderedMap.toList(simCodeIndices.generic_call_map));
(modelInfo, simCodeIndices) := ModelInfo.create(vars, name, directory, functions, linearLoops, nonlinearLoops, bdae.eventInfo, simCodeIndices);
simCode := SIM_CODE(
modelInfo = modelInfo,
literals = literals,
recordDecls = recordDecls,
externalFunctionIncludes = externalFunctionIncludes,
generic_loop_calls = generic_loop_calls,
independent = independent,
allSim = allSim,
ode = ode,
algebraic = algebraic,
nominal = nominal,
min = min,
max = max,
param = param,
no_ret = no_ret,
algorithms = algorithms,
event_blocks = event_blocks,
jac_blocks = jac_blocks,
start = start,
init = init,
init_0 = init_0,
init_no_ret = init_no_ret,
discreteVars = discreteVars,
extObjInfo = extObjInfo,
makefileParams = makefileParams,
jacobians = jacobians,
simulationSettingsOpt = simSettingsOpt,
fileNamePrefix = fileNamePrefix,
simcode_map = simcode_map,
equation_map = equation_map,
eventInfo = bdae.eventInfo,
daeModeData = daeModeData,
inlineEquations = inlineEquations
);
then simCode;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
then fail();
end match;
end create;
function convert
input SimCode simCode;
output OldSimCode.SimCode oldSimCode;
protected
OldSimCode.ModelInfo modelInfo;
list<DAE.ComponentRef> discreteModelVars = {};
list<OldBackendDAE.ZeroCrossing> zeroCrossings;
list<OldBackendDAE.ZeroCrossing> relations "== zeroCrossings for the most part (only eq pointer different?)";
list<OldBackendDAE.TimeEvent> timeEvents;
HashTableCrIListArray.HashTable varToArrayIndexMapping;
HashTableCrILst.HashTable varToIndexMapping;
OldSimCode.HashTableCrefToSimVar crefToSimVarHT "hidden from typeview - used by cref2simvar() for cref -> SIMVAR lookup available in templates.";
HashTable.HashTable crefToClockIndexHT "map variables to clock indices";
list<SimVar> residualVars;
algorithm
modelInfo := ModelInfo.convert(simCode.modelInfo);
(zeroCrossings, relations, timeEvents) := EventInfo.convert(simCode.eventInfo, simCode.equation_map);
(varToArrayIndexMapping, varToIndexMapping) := OldSimCodeUtil.createVarToArrayIndexMapping(modelInfo);
crefToSimVarHT := SimCodeUtil.convertSimCodeMap(simCode.simcode_map);
// do we still need the following for DAE mode?
if isSome(simCode.daeModeData) then
SOME(DAE_MODE_DATA(residualVars = residualVars)) := simCode.daeModeData;
crefToSimVarHT:= List.fold(SimVar.SimVar.convertList(residualVars), HashTableCrefSimVar.addSimVarToHashTable, crefToSimVarHT);
end if;
crefToClockIndexHT := HashTable.emptyHashTable();
for cref in simCode.discreteVars loop
discreteModelVars := ComponentRef.toDAE(cref) :: discreteModelVars;
end for;
oldSimCode := OldSimCode.SIMCODE(
modelInfo = modelInfo,
literals = {}, // usally set by a traversal below...
recordDecls = simCode.recordDecls, // ToDo: convert this to new structures
externalFunctionIncludes = simCode.externalFunctionIncludes,
generic_loop_calls = list(SimGenericCall.convert(gc) for gc in simCode.generic_loop_calls),
localKnownVars = SimStrongComponent.Block.convertList(simCode.independent),
allEquations = SimStrongComponent.Block.convertList(simCode.allSim),
odeEquations = SimStrongComponent.Block.convertListList(simCode.ode),
algebraicEquations = SimStrongComponent.Block.convertListList(simCode.algebraic),
clockedPartitions = {}, // ToDo: add this once clocked partitions are supported
initialEquations = SimStrongComponent.Block.convertList(simCode.init),
initialEquations_lambda0 = SimStrongComponent.Block.convertList(simCode.init_0),
removedInitialEquations = SimStrongComponent.Block.convertList(simCode.init_no_ret),
startValueEquations = SimStrongComponent.Block.convertList(simCode.start),
nominalValueEquations = SimStrongComponent.Block.convertList(simCode.nominal),
minValueEquations = SimStrongComponent.Block.convertList(simCode.min),
maxValueEquations = SimStrongComponent.Block.convertList(simCode.max),
parameterEquations = SimStrongComponent.Block.convertList(simCode.param),
removedEquations = SimStrongComponent.Block.convertList(simCode.no_ret),
algorithmAndEquationAsserts = SimStrongComponent.Block.convertList(simCode.algorithms),
equationsForZeroCrossings = SimStrongComponent.Block.convertList(simCode.event_blocks),
jacobianEquations = SimStrongComponent.Block.convertList(simCode.jac_blocks),
stateSets = {}, // ToDo: add this once state sets are supported
constraints = {}, // ToDo: add this once constraints are supported
classAttributes = {}, // ToDo: add this once class attributes are supported
zeroCrossings = zeroCrossings,
relations = relations,
timeEvents = timeEvents,
discreteModelVars = discreteModelVars,
extObjInfo = ExtObjInfo.convert(simCode.extObjInfo), // ToDo: add this once external object info is supported
makefileParams = simCode.makefileParams, // ToDo: convert this to new structures
delayedExps = OldSimCode.DELAYED_EXPRESSIONS({}, 0), // ToDo: add this once delayed expressions are supported
spatialInfo = OldSimCode.SPATIAL_DISTRIBUTION_INFO({}, 0),
jacobianMatrices = list(SimJacobian.convert(jac) for jac in simCode.jacobians),
simulationSettingsOpt = simCode.simulationSettingsOpt, // replace with new struct later on
fileNamePrefix = simCode.fileNamePrefix,
fullPathPrefix = "", // FMI stuff
fmuTargetName = "", // FMI stuff
hpcomData = HpcOmSimCode.emptyHpcomData,
valueReferences = AvlTreeCRToInt.EMPTY(), // change to this with fmu: if isFMU then getValueReferenceMapping(modelInfo) else AvlTreeCRToInt.EMPTY(),
varToArrayIndexMapping = varToArrayIndexMapping,
varToIndexMapping = varToIndexMapping,
crefToSimVarHT = crefToSimVarHT,
crefToClockIndexHT = crefToClockIndexHT,
backendMapping = NONE(), // This needs to be added?
modelStructure = NONE(), // FMI stuff
fmiSimulationFlags = NONE(), // FMI stuff
partitionData = OldSimCode.PARTITIONDATA(-1,{},{},{}),
daeModeData = if isSome(simCode.daeModeData) then SOME(DaeModeData.convert(Util.getOption(simCode.daeModeData))) else NONE(),
inlineEquations = {},
omsiData = NONE());
end convert;
function getDirectoryAndLibs
input SimCode simCode;
output String directory;
output list<String> libs;
algorithm
(directory, libs) := match simCode
case SIM_CODE(modelInfo = MODEL_INFO(directory = directory), makefileParams = OldSimCodeFunction.MAKEFILE_PARAMS(libs = libs)) then (directory, libs);
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
then fail();
end match;
end getDirectoryAndLibs;
protected
function collectAlgebraicLoops
"Collects algebraic loops from all systems (ode, init, init_0, dae, ...).
ToDo: Add other systems once implemented!"
input list<SimStrongComponent.Block> init;
input list<list<SimStrongComponent.Block>> ode;
input list<list<SimStrongComponent.Block>> algebraic;
input Option<DaeModeData> daeModeData;
output list<SimStrongComponent.Block> linearLoops = {};
output list<SimStrongComponent.Block> nonlinearLoops = {};
output list<SimJacobian> jacobians = {};
input output SimCodeIndices simCodeIndices;
input UnorderedMap<ComponentRef, SimVar> simcode_map;
protected
list<list<SimStrongComponent.Block>> dae_mode_blcks;
algorithm
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := SimStrongComponent.Block.collectAlgebraicLoops({init}, linearLoops, nonlinearLoops, jacobians, simCodeIndices, simcode_map);
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := SimStrongComponent.Block.collectAlgebraicLoops(ode, linearLoops, nonlinearLoops, jacobians, simCodeIndices, simcode_map);
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := SimStrongComponent.Block.collectAlgebraicLoops(algebraic, linearLoops, nonlinearLoops, jacobians, simCodeIndices, simcode_map);
if isSome(daeModeData) then
SOME(DAE_MODE_DATA(blcks = dae_mode_blcks)) := daeModeData;
(linearLoops, nonlinearLoops, jacobians, simCodeIndices) := SimStrongComponent.Block.collectAlgebraicLoops(dae_mode_blcks, linearLoops, nonlinearLoops, jacobians, simCodeIndices, simcode_map);
end if;
end collectAlgebraicLoops;
end SimCode;
uniontype ModelInfo
record MODEL_INFO
Absyn.Path name;
String description;
String directory;
SimVars vars;
VarInfo varInfo;
list<SimCodeFunction.Function> functions;
list<String> labels;
list<String> resourcePaths "Paths of all resources used by the model. Used in FMI2 to package resources in the FMU.";
list<Absyn.Class> sortedClasses;
//Files files "all the files from SourceInfo and DAE.ElementSource";
Integer nClocks;
Integer nSubClocks;
Integer nSpatialDistributions;
Boolean hasLargeLinearEquationSystems; // True if model has large linear eq. systems that are crucial for performance.
list<SimStrongComponent.Block> linearLoops;
list<SimStrongComponent.Block> nonlinearLoops;
//list<UnitDefinition> unitDefinitions "export unitDefintion in modelDescription.xml";
end MODEL_INFO;
function toString
input ModelInfo modelInfo;
output String str;
algorithm
str := SimVars.toString(modelInfo.vars);
end toString;
function create
input SimVars vars;
input Absyn.Path name;
input String directory;
input list<OldSimCodeFunction.Function> functions;
input list<SimStrongComponent.Block> linearLoops;
input list<SimStrongComponent.Block> nonlinearLoops;
input EventInfo eventInfo;
output ModelInfo modelInfo;
input output SimCodeIndices simCodeIndices;
protected
VarInfo info;
algorithm
info := VarInfo.create(vars, eventInfo, simCodeIndices);
modelInfo := MODEL_INFO(name, "", directory, vars, info, functions, {}, {}, {}, 0, 0, 0, true, linearLoops, nonlinearLoops);
end create;
function setSeedVars
input output ModelInfo modelInfo;
input list<SimVar> seedVars;
algorithm
modelInfo := match modelInfo
local
SimVars vars;
case MODEL_INFO(vars = vars) algorithm
vars.seedVars := seedVars;
modelInfo.vars := vars;
then modelInfo;
else algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
then fail();
end match;
end setSeedVars;
function convert
input ModelInfo modelInfo;
output OldSimCode.ModelInfo oldModelInfo;
protected
OldSimCode.VarInfo varInfo;
algorithm
varInfo := VarInfo.convert(modelInfo.varInfo);
oldModelInfo := OldSimCode.MODELINFO(
name = modelInfo.name,
description = modelInfo.description,
directory = modelInfo.directory,
varInfo = VarInfo.convert(modelInfo.varInfo),
vars = SimVar.SimVars.convert(modelInfo.vars),
functions = modelInfo.functions,
labels = modelInfo.labels,
resourcePaths = modelInfo.resourcePaths,
sortedClasses = modelInfo.sortedClasses,
//Files files "all the files from SourceInfo and DAE.ElementSource";
nClocks = modelInfo.nClocks,
nSubClocks = modelInfo.nSubClocks,
nSpatialDistributions = modelInfo.nSpatialDistributions,
hasLargeLinearEquationSystems = modelInfo.hasLargeLinearEquationSystems,
linearSystems = SimStrongComponent.Block.convertList(modelInfo.linearLoops),
nonLinearSystems = SimStrongComponent.Block.convertList(modelInfo.nonlinearLoops),
unitDefinitions = {} // ToDo: add this once unit definitions are supported
);
end convert;
end ModelInfo;
uniontype DaeModeData
"contains data that belongs to the dae mode"
record DAE_MODE_DATA
list<list<SimStrongComponent.Block>> blcks "daeMode blocks";
Option<SimJacobian> sparsityPattern "contains the sparsity pattern for the daeMode";
list<SimVar> residualVars "variable used to calculate residuals of a DAE form, they are real";
list<SimVar> algebraicVars;
list<SimVar> auxiliaryVars;
DaeModeConfig modeCreated;
end DAE_MODE_DATA;
function toString
input DaeModeData data;
output String str = "";
protected
Integer idx = 1;
algorithm
for blck_lst in data.blcks loop
str := str + SimStrongComponent.Block.listToString(blck_lst, " ", "DAE Partition " + intString(idx));
idx := idx + 1;
end for;
if isSome(data.sparsityPattern) then
str := str + "\n" + SimJacobian.toString(Util.getOption(data.sparsityPattern));
end if;
end toString;
function create
input list<System.System> systems;
output Option<DaeModeData> data;
input output SimCodeIndices simCodeIndices;
input UnorderedMap<ComponentRef, SimVar> simcode_map;
input UnorderedMap<ComponentRef, SimStrongComponent.Block> equation_map;
protected
list<list<SimStrongComponent.Block>> blcks;
list<SimVar> residualVars, algebraicVars;
Option<SimJacobian> daeModeJac;
algorithm
(blcks, residualVars, simCodeIndices) := SimStrongComponent.Block.createDAEModeBlocks(systems, simCodeIndices, simcode_map, equation_map);
//(daeModeJac, simCodeIndices) := SimJacobian.fromSystems(systems, simCodeIndices);
//data := SOME(DAE_MODE_DATA(blcks, daeModeJac, residualVars, {}, {}, DaeModeConfig.ALL));
data := NONE();
end create;
function convert
input DaeModeData data;
output OldSimCode.DaeModeData oldData;
protected
list<list<OldSimCode.SimEqSystem>> simEqSystems = {};
algorithm
/* is this not needed?
for sys_lst in listReverse(SimStrongComponent.Block.convertListList(data.blcks)) loop
simEqSystems := List.map(sys_lst, replaceDerCrefSES) :: simEqSystems;
end for;
*/
simEqSystems := SimStrongComponent.Block.convertListList(data.blcks);
oldData := OldSimCode.DAEMODEDATA(
daeEquations = simEqSystems,
sparsityPattern = SimJacobian.convertOpt(data.sparsityPattern),
residualVars = SimVar.SimVar.convertList(data.residualVars),
algebraicVars = SimVar.SimVar.convertList(data.algebraicVars),
auxiliaryVars = SimVar.SimVar.convertList(data.auxiliaryVars),
modeCreated = convertMode(data.modeCreated));
end convert;
protected
function convertMode
input DaeModeConfig mode;
output OldSimCode.DaeModeConfig oldMode;
algorithm
oldMode := match mode
case DaeModeConfig.ALL then OldSimCode.ALL_EQUATIONS();
case DaeModeConfig.DYNAMIC then OldSimCode.DYNAMIC_EQUATIONS();
end match;
end convertMode;
function createSparsityJacobian
input output Option<DaeModeData> daeModeDataOpt;
input output ModelInfo modelInfo;
input list<System.System> systems;
output SimJacobian jacobian;
input output UnorderedMap<ComponentRef, SimVar> simcode_map;
input output SimCodeIndices simCodeIndices;
algorithm
daeModeDataOpt := match daeModeDataOpt
local
DaeModeData daeModeData;
SimJacobian jac;
Option<SimJacobian> daeModeJac;
case SOME(daeModeData)
algorithm
// get sparsity pattern jacobian and update the hashtable and the jacobian
// update to UnorderedMap once reactivated
/*
simulationHT := HashTableSimCode.addList(daeModeData.residualVars, simulationHT);
if isSome(daeModeData.sparsityPattern) then
SOME(jac) := daeModeData.sparsityPattern;
simulationHT := HashTableSimCode.addList(jac.seedVars, simulationHT);
modelInfo := ModelInfo.setSeedVars(modelInfo, jac.seedVars);
daeModeData.algebraicVars := rewriteAlgebraicVarsIdx(modelInfo.vars.algVars, simulationHT);
daeModeData.auxiliaryVars := {}; // this needs to be updated in the future
//(daeModeJac, simCodeIndices) := SimJacobian.fromSystemsSparsity(systems, daeModeData.sparsityPattern, simulationHT, simCodeIndices);
daeModeData.sparsityPattern := daeModeJac;
jacobian := Util.getOption(daeModeJac);
else
*/
(jacobian, simCodeIndices) := SimJacobian.empty("A", simCodeIndices);
// end if;
then SOME(daeModeData);
else
algorithm
Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName() + " failed!"});
then fail();
end match;
end createSparsityJacobian;
function rewriteAlgebraicVarsIdx
input list<SimVar> simulationAlgVars;
input UnorderedMap<ComponentRef, SimVar> simcode_map;
output list<SimVar> daeModeAlgVars = {};
protected
ComponentRef seedCref, cref;
algorithm
seedCref := ComponentRef.fromNode(InstNode.VAR_NODE(NBVariable.SEED_STR + "_A", Pointer.create(NBVariable.DUMMY_VARIABLE)), Type.UNKNOWN());
for var in listReverse(simulationAlgVars) loop
cref := ComponentRef.append(var.name, seedCref);
print("Searching for: " + ComponentRef.toString(cref) + "\n");
var.index := SimVar.getIndex(cref, simcode_map);
daeModeAlgVars := var :: daeModeAlgVars;
end for;
end rewriteAlgebraicVarsIdx;
function replaceDerCrefSES
input output OldSimCode.SimEqSystem sys;
algorithm
sys := match sys
local
OldSimCode.SimEqSystem qual;
case qual as OldSimCode.SES_RESIDUAL() algorithm
(qual.exp, _) := OldExpression.traverseExpTopDown(qual.exp, replaceDerCref, 0);
then qual;
case qual as OldSimCode.SES_SIMPLE_ASSIGN() algorithm
(qual.exp, _) := OldExpression.traverseExpTopDown(qual.exp, replaceDerCref, 0);
then qual;
end match;
end replaceDerCrefSES;
function replaceDerCref
"this is very bad. temporary fix please remove.
the old structure needs a der() call instead of $DER variable for DAEMode."
input output DAE.Exp exp;
output Boolean b;
input output Integer i;
algorithm
(exp, b) := match exp
local
DAE.ComponentRef cref;
case DAE.CREF(componentRef = DAE.CREF_QUAL(ident="$DER",componentRef=cref))
then (DAE.CALL(Absyn.IDENT("der"), {DAE.CREF(cref, ComponentReference.crefTypeFull(cref))}, DAE.callAttrBuiltinReal), false);
else (exp, true);
end match;
end replaceDerCref;
end DaeModeData;
type DaeModeConfig = enumeration(ALL, DYNAMIC);
annotation(__OpenModelica_Interface="backend");
end NSimCode;