/
Initialization.mo
2606 lines (2273 loc) · 108 KB
/
Initialization.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
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 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 Initialization
" file: Initialization.mo
package: Initialization
description: Initialization.mo contains everything needed to set up the
BackendDAE for the initial system.
RCS: $Id$"
public import Absyn;
public import BackendDAE;
public import BackendDAEFunc;
public import DAE;
public import Env;
public import HashSet;
public import Util;
protected import BackendDAEEXT;
protected import BackendDAEOptimize;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVariable;
protected import BaseHashSet;
protected import BaseHashTable;
protected import CheckModel;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import Flags;
protected import HashTable;
protected import HashTable2;
protected import List;
protected import Matching;
protected import Tearing;
// =============================================================================
// section for all public functions
//
// These are functions that can be used to access the initialization.
// =============================================================================
public function solveInitialSystem "author: lochel
This function generates a algebraic system of equations for the initialization and solves it."
input BackendDAE.BackendDAE inDAE;
output Option<BackendDAE.BackendDAE> outInitDAE;
output Boolean outUseHomotopy;
algorithm
(outInitDAE, outUseHomotopy) := matchcontinue(inDAE)
local
BackendDAE.BackendDAE dae;
BackendDAE.Variables initVars;
BackendDAE.EqSystems systs;
BackendDAE.Shared shared;
BackendDAE.Variables knvars, vars, fixvars, evars, eavars, avars;
BackendDAE.EquationArray inieqns, eqns, emptyeqns, reeqns;
BackendDAE.EqSystem initsyst;
BackendDAE.BackendDAE initdae;
Env.Cache cache;
Env.Env env;
DAE.FunctionTree functionTree;
list<DAE.Constraint> constraints;
list<DAE.ClassAttributes> classAttrs;
list<BackendDAE.Var> tempVar;
Boolean b;
HashSet.HashSet hs "contains all pre variables";
list<tuple<BackendDAEFunc.postOptimizationDAEModule, String, Boolean>> pastOptModules;
tuple<BackendDAEFunc.StructurallySingularSystemHandlerFunc, String, BackendDAEFunc.stateDeselectionFunc, String> daeHandler;
tuple<BackendDAEFunc.matchingAlgorithmFunc, String> matchingAlgorithm;
Boolean useHomotopy;
list<BackendDAE.Var> dumpVars, dumpVars2;
BackendDAE.ExtraInfo ei;
case(_) equation
// inline all when equations, if active with body else with lhs=pre(lhs)
dae = inlineWhenForInitialization(inDAE);
// Debug.fcall2(Flags.DUMP_INITIAL_SYSTEM, BackendDump.dumpBackendDAE, dae, "inlineWhenForInitialization");
initVars = selectInitializationVariablesDAE(dae);
// Debug.fcall2(Flags.DUMP_INITIAL_SYSTEM, BackendDump.dumpVariables, initVars, "selected initialization variables");
hs = collectPreVariables(dae);
BackendDAE.DAE(systs, shared as BackendDAE.SHARED(knownVars=knvars,
aliasVars=avars,
initialEqs=inieqns,
constraints=constraints,
classAttrs=classAttrs,
cache=cache,
env=env,
functionTree=functionTree,
info = ei)) = dae;
// collect vars and eqns for initial system
vars = BackendVariable.emptyVars();
fixvars = BackendVariable.emptyVars();
eqns = BackendEquation.emptyEqns();
reeqns = BackendEquation.emptyEqns();
((vars, fixvars, eqns, _)) = BackendVariable.traverseBackendDAEVars(avars, introducePreVarsForAliasVariables, (vars, fixvars, eqns, hs));
((vars, fixvars, eqns, _)) = BackendVariable.traverseBackendDAEVars(knvars, collectInitialVars, (vars, fixvars, eqns, hs));
((eqns, reeqns)) = BackendEquation.traverseBackendDAEEqns(inieqns, collectInitialEqns, (eqns, reeqns));
// Debug.fcall2(Flags.DUMP_INITIAL_SYSTEM, BackendDump.dumpEquationArray, eqns, "initial equations");
((vars, fixvars, eqns, reeqns, _)) = List.fold(systs, collectInitialVarsEqnsSystem, ((vars, fixvars, eqns, reeqns, hs)));
((eqns, reeqns)) = BackendVariable.traverseBackendDAEVars(vars, collectInitialBindings, (eqns, reeqns));
// replace initial(), sample(...), delay(...) and homotopy(...)
useHomotopy = BackendDAEUtil.traverseBackendDAEExpsEqnsWithUpdate(eqns, simplifyInitialFunctions, false);
evars = BackendVariable.emptyVars();
eavars = BackendVariable.emptyVars();
emptyeqns = BackendEquation.emptyEqns();
shared = BackendDAE.SHARED(fixvars,
evars,
eavars,
emptyeqns,
reeqns,
constraints,
classAttrs,
cache,
env,
functionTree,
BackendDAE.EVENT_INFO({}, {}, {}, {}, {}, 0, 0),
{},
BackendDAE.INITIALSYSTEM(),
{},
ei);
// generate initial system and pre-balance it
initsyst = BackendDAE.EQSYSTEM(vars, eqns, NONE(), NONE(), BackendDAE.NO_MATCHING(), {});
(initsyst, dumpVars) = preBalanceInitialSystem(initsyst);
// split the initial system into independend subsystems
(systs, shared) = BackendDAEOptimize.partitionIndependentBlocksHelper(initsyst, shared, Error.getNumErrorMessages(), true);
initdae = BackendDAE.DAE(systs, shared);
// initdae = BackendDAE.DAE({initsyst}, shared);
// fix over- and under-constrained subsystems
(initdae, dumpVars2) = analyzeInitialSystem(initdae, dae, initVars);
dumpVars = listAppend(dumpVars, dumpVars2);
// some debug prints
Debug.fcall2(Flags.DUMP_INITIAL_SYSTEM, BackendDump.dumpBackendDAE, initdae, "initial system");
// now let's solve the system!
(initdae, _) = BackendDAEUtil.mapEqSystemAndFold(initdae, solveInitialSystemEqSystem, dae);
// transform and optimize DAE
pastOptModules = BackendDAEUtil.getPostOptModules(SOME({"constantLinearSystem", /* here we need a special case and remove only alias and constant (no variables of the system) variables "removeSimpleEquations", */ "tearingSystem","calculateStrongComponentJacobians"}));
matchingAlgorithm = BackendDAEUtil.getMatchingAlgorithm(NONE());
daeHandler = BackendDAEUtil.getIndexReductionMethod(NONE());
// solve system
initdae = BackendDAEUtil.transformBackendDAE(initdae, SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())), NONE(), NONE());
// simplify system
(initdae, Util.SUCCESS()) = BackendDAEUtil.postOptimizeDAE(initdae, pastOptModules, matchingAlgorithm, daeHandler);
Debug.fcall2(Flags.DUMP_INITIAL_SYSTEM, BackendDump.dumpBackendDAE, initdae, "solved initial system");
Debug.bcall2(Flags.isSet(Flags.DUMP_INITIAL_SYSTEM) and Flags.isSet(Flags.ADDITIONAL_GRAPHVIZ_DUMP), BackendDump.graphvizBackendDAE, initdae, "dumpinitialsystem");
// warn about selected default initial conditions
b = List.isNotEmpty(dumpVars);
Debug.bcall(b and (not Flags.isSet(Flags.INITIALIZATION)), Error.addCompilerWarning, "The initial conditions are not fully specified. Use +d=initialization for more information.");
Debug.bcall(b and Flags.isSet(Flags.INITIALIZATION), Error.addCompilerWarning, "Assuming fixed start value for the following " +& intString(listLength(dumpVars)) +& " variables:\n" +& warnAboutVars2(dumpVars));
// warn about iteration variables with default zero start attribute
b = warnAboutIterationVariablesWithDefaultZeroStartAttribute(initdae);
Debug.bcall(b and (not Flags.isSet(Flags.INITIALIZATION)), Error.addCompilerWarning, "There are iteration variables with default zero start attribute. Use +d=initialization for more information.");
b = Flags.isSet(Flags.DUMP_EQNINORDER) and Flags.isSet(Flags.DUMP_INITIAL_SYSTEM);
Debug.bcall2(b, BackendDump.dumpEqnsSolved, initdae, "initial system: eqns in order");
Debug.fcall(Flags.ITERATION_VARS, BackendDAEOptimize.listAllIterationVariables, initdae);
then (SOME(initdae), useHomotopy);
else (NONE(), false);
end matchcontinue;
end solveInitialSystem;
// =============================================================================
// section for helper functions of solveInitialSystem
//
// =============================================================================
protected function solveInitialSystemEqSystem "author: lochel
This is a helper function of solveInitialSystem and solves the generated system."
input BackendDAE.EqSystem isyst;
input tuple<BackendDAE.Shared, BackendDAE.BackendDAE> sharedOptimized;
output BackendDAE.EqSystem osyst;
output tuple<BackendDAE.Shared, BackendDAE.BackendDAE> osharedOptimized;
algorithm
(osyst, osharedOptimized) := matchcontinue(isyst, sharedOptimized)
local
Integer nVars, nEqns;
// over-determined system: nEqns > nVars
case(_, _) equation
nVars = BackendVariable.varsSize(BackendVariable.daeVars(isyst));
nEqns = BackendDAEUtil.systemSize(isyst);
true = intGt(nEqns, nVars);
Debug.fcall(Flags.INITIALIZATION, Error.addCompilerWarning, "It was not possible to solve the over-determined initial system (" +& intString(nEqns) +& " equations and " +& intString(nVars) +& " variables)");
then fail();
// determined system: nEqns = nVars
case( _, _) equation
nVars = BackendVariable.varsSize(BackendVariable.daeVars(isyst));
nEqns = BackendDAEUtil.systemSize(isyst);
true = intEq(nEqns, nVars);
then (isyst, sharedOptimized);
// under-determined system: nEqns < nVars
case( _, _) equation
nVars = BackendVariable.varsSize(BackendVariable.daeVars(isyst));
nEqns = BackendDAEUtil.systemSize(isyst);
true = intLt(nEqns, nVars);
Debug.fcall(Flags.INITIALIZATION, Error.addCompilerWarning, "It was not possible to solve the under-determined initial system (" +& intString(nEqns) +& " equations and " +& intString(nVars) +& " variables)");
then fail();
end matchcontinue;
end solveInitialSystemEqSystem;
// =============================================================================
// section for inlining when-clauses
//
// This section contains all the helper functions to replace all when-clauses
// from a given BackenDAE to get the initial equation system.
// =============================================================================
protected function inlineWhenForInitialization "author: lochel
This function inlines when-clauses for the initialization."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
protected
BackendDAE.EqSystems systs;
BackendDAE.Shared shared;
algorithm
BackendDAE.DAE(systs, shared) := inDAE;
systs := List.map(systs, inlineWhenForInitializationSystem);
outDAE := BackendDAE.DAE(systs, shared);
end inlineWhenForInitialization;
protected function inlineWhenForInitializationSystem "author: lochel
This is a helper function for inlineWhenForInitialization."
input BackendDAE.EqSystem inEqSystem;
output BackendDAE.EqSystem outEqSystem;
protected
BackendDAE.Variables orderedVars;
BackendDAE.EquationArray orderedEqs;
BackendDAE.EquationArray eqns;
BackendDAE.StateSets stateSets;
list<BackendDAE.Equation> eqnlst;
algorithm
BackendDAE.EQSYSTEM(orderedVars=orderedVars, orderedEqs=orderedEqs, stateSets=stateSets) := inEqSystem;
((orderedVars, eqnlst)) := BackendEquation.traverseBackendDAEEqns(orderedEqs, inlineWhenForInitializationEquation, (orderedVars, {}));
eqns := BackendEquation.listEquation(eqnlst);
outEqSystem := BackendDAE.EQSYSTEM(orderedVars, eqns, NONE(), NONE(), BackendDAE.NO_MATCHING(), stateSets);
end inlineWhenForInitializationSystem;
protected function inlineWhenForInitializationEquation "author: lochel
This is a helper function for inlineWhenForInitialization1."
input tuple<BackendDAE.Equation, tuple<BackendDAE.Variables, list<BackendDAE.Equation>>> inTpl;
output tuple<BackendDAE.Equation, tuple<BackendDAE.Variables, list<BackendDAE.Equation>>> outTpl;
algorithm
outTpl := match(inTpl)
local
DAE.ElementSource source;
BackendDAE.Equation eqn;
DAE.Algorithm alg;
Integer size;
list< DAE.Statement> stmts;
list< BackendDAE.Equation> eqns;
BackendDAE.WhenEquation weqn;
BackendDAE.Variables vars;
list< DAE.ComponentRef> crefLst;
HashTable.HashTable leftCrs;
list<tuple<DAE.ComponentRef, Integer>> crintLst;
DAE.Expand crefExpand;
// when equation during initialization
case ((eqn as BackendDAE.WHEN_EQUATION(whenEquation=weqn, source=source), (vars, eqns))) equation
(eqns, vars) = inlineWhenForInitializationWhenEquation(weqn, source, eqns, vars);
then ((eqn, (vars, eqns)));
// algorithm
case ((eqn as BackendDAE.ALGORITHM(alg=alg, source=source,expand=crefExpand), (vars, eqns))) equation
DAE.ALGORITHM_STMTS(statementLst=stmts) = alg;
(stmts, leftCrs) = generateInitialWhenAlg(stmts, true, {}, HashTable.emptyHashTableSized(50));
alg = DAE.ALGORITHM_STMTS(stmts);
size = listLength(CheckModel.algorithmOutputs(alg, crefExpand));
crintLst = BaseHashTable.hashTableList(leftCrs);
crefLst = List.fold(crintLst, selectSecondZero, {});
(eqns, vars) = generateInactiveWhenEquationForInitialization(crefLst, source, eqns, vars);
eqns = List.consOnTrue(List.isNotEmpty(stmts), BackendDAE.ALGORITHM(size, alg, source, crefExpand, BackendDAE.INITIAL_EQUATION()), eqns);
then ((eqn, (vars, eqns)));
case ((eqn, (vars, eqns)))
then ((eqn, (vars, eqn::eqns)));
end match;
end inlineWhenForInitializationEquation;
protected function selectSecondZero
input tuple<DAE.ComponentRef, Integer> inTpl;
input list<DAE.ComponentRef> iAcc;
output list<DAE.ComponentRef> oAcc;
protected
DAE.ComponentRef cr;
Integer i;
algorithm
(cr, i) := inTpl;
oAcc := List.consOnTrue(intEq(i, 0), cr, iAcc);
end selectSecondZero;
protected function inlineWhenForInitializationWhenEquation "author: lochel
This is a helper function for inlineWhenForInitializationEquation."
input BackendDAE.WhenEquation inWEqn;
input DAE.ElementSource source;
input list<BackendDAE.Equation> iEqns;
input BackendDAE.Variables iVars;
output list<BackendDAE.Equation> oEqns;
output BackendDAE.Variables oVars;
algorithm
(oEqns, oVars) := matchcontinue(inWEqn, source, iEqns, iVars)
local
DAE.ComponentRef left;
DAE.Exp condition, right, crexp;
BackendDAE.Equation eqn;
DAE.Type identType;
list< BackendDAE.Equation> eqns;
BackendDAE.WhenEquation weqn;
BackendDAE.Variables vars;
// active when equation during initialization
case (BackendDAE.WHEN_EQ(condition=condition, left=left, right=right), _, _, _) equation
true = Expression.containsInitialCall(condition, false); // do not use Expression.traverseExp
crexp = Expression.crefExp(left);
identType = Expression.typeof(crexp);
eqn = BackendEquation.generateEquation(crexp, right, identType, source, false);
then (eqn::iEqns, iVars);
// inactive when equation during initialization
case (BackendDAE.WHEN_EQ(condition=condition, left=left, elsewhenPart=NONE()), _, _, _) equation
false = Expression.containsInitialCall(condition, false);
(eqns,_) = generateInactiveWhenEquationForInitialization({left}, source, iEqns, iVars);
then (eqns, iVars);
// inactive when equation during initialization with else when part (no strict Modelica)
case (BackendDAE.WHEN_EQ(condition=condition, elsewhenPart=SOME(weqn)), _, _, _) equation
false = Expression.containsInitialCall(condition, false); // do not use Expression.traverseExp
(eqns, vars) = inlineWhenForInitializationWhenEquation(weqn, source, iEqns, iVars);
then (eqns, vars);
end matchcontinue;
end inlineWhenForInitializationWhenEquation;
protected function generateInitialWhenAlg "author: lochel
This function generates out of a given when-algorithm, a algorithm for the initialization-problem.
This is a helper function for inlineWhenForInitialization3."
input list< DAE.Statement> inStmts;
input Boolean first;
input list< DAE.Statement> inAcc;
input HashTable.HashTable iLeftCrs;
output list< DAE.Statement> outStmts;
output HashTable.HashTable oLeftCrs;
algorithm
(outStmts, oLeftCrs) := matchcontinue(inStmts, first, inAcc, iLeftCrs)
local
DAE.Exp condition;
list< DAE.ComponentRef> crefLst;
DAE.Statement stmt;
list< DAE.Statement> stmts, rest;
HashTable.HashTable leftCrs;
list<tuple<DAE.ComponentRef, Integer>> crintLst;
case ({}, _, _, _)
then (listReverse(inAcc), iLeftCrs);
// single inactive when equation during initialization
case ((DAE.STMT_WHEN(exp=condition, statementLst=stmts, elseWhen=NONE()))::{}, true, _, _) equation
false = Expression.containsInitialCall(condition, false);
crefLst = CheckModel.algorithmStatementListOutputs(stmts, DAE.EXPAND()); // expand as we're in an algorithm
_ = List.map1(crefLst, Util.makeTuple, 1);
leftCrs = List.fold(crefLst, addWhenLeftCr, iLeftCrs);
then ({}, leftCrs);
// when equation during initialization
case ((stmt as DAE.STMT_WHEN(source=_))::rest, _, _, _) equation
// for when statements it is not necessary that all branches have the same left hand side variables
// -> take care that for each left hand site an assigment is generated
(stmts, leftCrs) = inlineWhenForInitializationWhenStmt(stmt, false, iLeftCrs, inAcc);
(stmts, leftCrs) = generateInitialWhenAlg(rest, false, stmts, leftCrs);
then (stmts, leftCrs);
// no when equation
case (stmt::rest, _, _, _) equation
(stmts, leftCrs) = generateInitialWhenAlg(rest, false, stmt::inAcc, iLeftCrs);
then (stmts, leftCrs);
end matchcontinue;
end generateInitialWhenAlg;
protected function inlineWhenForInitializationWhenStmt "author: lochel
This function generates out of a given when-algorithm, a algorithm for the initialization-problem.
This is a helper function for inlineWhenForInitialization3."
input DAE.Statement inWhen;
input Boolean foundAktiv;
input HashTable.HashTable iLeftCrs;
input list< DAE.Statement> inAcc;
output list< DAE.Statement> outStmts;
output HashTable.HashTable oLeftCrs;
algorithm
(outStmts, oLeftCrs) := matchcontinue(inWhen, foundAktiv, iLeftCrs, inAcc)
local
DAE.Exp condition;
list< DAE.ComponentRef> crefLst;
DAE.Statement stmt;
list< DAE.Statement> stmts;
HashTable.HashTable leftCrs;
list<tuple<DAE.ComponentRef, Integer>> crintLst;
// active when equation during initialization
case (DAE.STMT_WHEN(exp=condition, statementLst=stmts, elseWhen=NONE()), _, _, _) equation
true = Expression.containsInitialCall(condition, false);
crefLst = CheckModel.algorithmStatementListOutputs(stmts, DAE.EXPAND()); // expand as we're in an algorithm
crintLst = List.map1(crefLst, Util.makeTuple, 1);
leftCrs = List.fold(crintLst, BaseHashTable.add, iLeftCrs);
stmts = List.foldr(stmts, List.consr, inAcc);
then (stmts, leftCrs);
case (DAE.STMT_WHEN(exp=condition, statementLst=stmts, elseWhen=SOME(stmt)), false, _, _) equation
true = Expression.containsInitialCall(condition, false);
crefLst = CheckModel.algorithmStatementListOutputs(stmts, DAE.EXPAND()); // expand as we're in an algorithm
crintLst = List.map1(crefLst, Util.makeTuple, 1);
leftCrs = List.fold(crintLst, BaseHashTable.add, iLeftCrs);
stmts = List.foldr(stmts, List.consr, inAcc);
(stmts, leftCrs) = inlineWhenForInitializationWhenStmt(stmt, true, leftCrs, stmts);
then (stmts, leftCrs);
// inactive when equation during initialization
case (DAE.STMT_WHEN(exp=condition, statementLst=stmts, elseWhen=NONE()), _, _, _) equation
false = Expression.containsInitialCall(condition, false) and not foundAktiv;
crefLst = CheckModel.algorithmStatementListOutputs(stmts, DAE.EXPAND()); // expand as we're in an algorithm
leftCrs = List.fold(crefLst, addWhenLeftCr, iLeftCrs);
then (inAcc, leftCrs);
// inactive when equation during initialization with elsewhen part
case (DAE.STMT_WHEN(exp=condition, statementLst=stmts, elseWhen=SOME(stmt)), _, _, _) equation
false = Expression.containsInitialCall(condition, false) and not foundAktiv;
crefLst = CheckModel.algorithmStatementListOutputs(stmts, DAE.EXPAND()); // expand as we're in an algorithm
leftCrs = List.fold(crefLst, addWhenLeftCr, iLeftCrs);
(stmts, leftCrs) = inlineWhenForInitializationWhenStmt(stmt, foundAktiv, leftCrs, inAcc);
then (stmts, leftCrs);
else equation
Error.addInternalError("./Compiler/BackEnd/Initialization.mo: function inlineWhenForInitializationWhenStmt failed");
then fail();
end matchcontinue;
end inlineWhenForInitializationWhenStmt;
protected function addWhenLeftCr
input DAE.ComponentRef cr;
input HashTable.HashTable iLeftCrs;
output HashTable.HashTable oLeftCrs;
algorithm
oLeftCrs := matchcontinue(cr, iLeftCrs)
local
HashTable.HashTable leftCrs;
case (_, _) equation
leftCrs = BaseHashTable.addUnique((cr, 0), iLeftCrs);
then leftCrs;
else iLeftCrs;
end matchcontinue;
end addWhenLeftCr;
protected function generateInactiveWhenEquationForInitialization "author: lochel
This is a helper function for inlineWhenForInitialization3."
input list<DAE.ComponentRef> inCrLst;
input DAE.ElementSource inSource;
input list<BackendDAE.Equation> inEqns;
input BackendDAE.Variables iVars;
output list<BackendDAE.Equation> outEqns;
output BackendDAE.Variables oVars;
algorithm
(outEqns, oVars) := match(inCrLst, inSource, inEqns, iVars)
local
DAE.Type identType;
DAE.Exp crefExp, crefPreExp;
DAE.ComponentRef cr;
list<DAE.ComponentRef> rest;
BackendDAE.Equation eqn;
list<BackendDAE.Equation> eqns;
BackendDAE.Variables vars;
case ({}, _, _, _)
then (inEqns, iVars);
case (cr::rest, _, _, _) equation
identType = ComponentReference.crefTypeConsiderSubs(cr);
crefExp = DAE.CREF(cr, identType);
crefPreExp = Expression.makeBuiltinCall("pre", {crefExp}, DAE.T_BOOL_DEFAULT);
eqn = BackendDAE.EQUATION(crefExp, crefPreExp, inSource, false, BackendDAE.INITIAL_EQUATION());
(eqns, vars) = generateInactiveWhenEquationForInitialization(rest, inSource, eqn::inEqns, iVars);
then (eqns, vars);
end match;
end generateInactiveWhenEquationForInitialization;
// =============================================================================
// section for collecting all variables, of which the left limit is also used.
//
// collect all pre variables in time equations
// =============================================================================
protected function collectPreVariables "author: lochel"
input BackendDAE.BackendDAE inDAE;
output HashSet.HashSet outHS;
protected
BackendDAE.EqSystems systs;
BackendDAE.EquationArray ieqns, removedEqs;
// list<DAE.ComponentRef> crefs;
algorithm
// BackendDump.dumpBackendDAE(inDAE, "inDAE");
BackendDAE.DAE(systs, BackendDAE.SHARED(removedEqs=removedEqs, initialEqs=ieqns)) := inDAE;
outHS := HashSet.emptyHashSet();
outHS := List.fold(systs, collectPreVariablesEqSystem, outHS);
outHS := BackendDAEUtil.traverseBackendDAEExpsEqns(removedEqs, collectPreVariablesEquation, outHS); // ???
outHS := BackendDAEUtil.traverseBackendDAEExpsEqns(ieqns, collectPreVariablesEquation, outHS);
// print("collectPreVariables:\n");
// crefs := BaseHashSet.hashSetList(outHS);
// BackendDump.debuglst((crefs,ComponentReference.printComponentRefStr,"\n","\n"));
end collectPreVariables;
public function collectPreVariablesEquation "author: lochel"
input tuple<DAE.Exp, HashSet.HashSet> inTpl;
output tuple<DAE.Exp, HashSet.HashSet> outTpl;
protected
DAE.Exp e;
HashSet.HashSet hs;
algorithm
(e, hs) := inTpl;
((_, hs)) := Expression.traverseExp(e, collectPreVariablesTrverseExp, hs);
outTpl := (e, hs);
end collectPreVariablesEquation;
public function collectPreVariablesEqSystem "author: lochel"
input BackendDAE.EqSystem inEqSystem;
input HashSet.HashSet inHS;
output HashSet.HashSet outHS;
protected
BackendDAE.EquationArray orderedEqs;
BackendDAE.EquationArray eqns;
algorithm
BackendDAE.EQSYSTEM(orderedEqs=orderedEqs) := inEqSystem;
outHS := BackendDAEUtil.traverseBackendDAEExpsEqns(orderedEqs, collectPreVariablesTrverseExpsEqns, inHS);
end collectPreVariablesEqSystem;
protected function collectPreVariablesTrverseExpsEqns "author: lochel"
input tuple<DAE.Exp, HashSet.HashSet> inTpl;
output tuple<DAE.Exp, HashSet.HashSet> outTpl;
protected
DAE.Exp e;
HashSet.HashSet hs;
algorithm
(e, hs) := inTpl;
((_, hs)) := Expression.traverseExp(e, collectPreVariablesTrverseExp, hs);
outTpl := (e, hs);
end collectPreVariablesTrverseExpsEqns;
protected function collectPreVariablesTrverseExp "author: lochel"
input tuple<DAE.Exp, HashSet.HashSet> inTpl;
output tuple<DAE.Exp, HashSet.HashSet> outTpl;
algorithm
outTpl := match(inTpl)
local
DAE.Exp e;
list<DAE.Exp> explst;
HashSet.HashSet hs;
case ((e as DAE.CALL(path=Absyn.IDENT(name="pre")), hs)) equation
((_, hs)) = Expression.traverseExp(e, collectPreVariablesTrverseExp2, hs);
then ((e, hs));
case ((e as DAE.CALL(path=Absyn.IDENT(name="change")), hs)) equation
((_, hs)) = Expression.traverseExp(e, collectPreVariablesTrverseExp2, hs);
then ((e, hs));
case ((e as DAE.CALL(path=Absyn.IDENT(name="edge")), hs)) equation
((_, hs)) = Expression.traverseExp(e, collectPreVariablesTrverseExp2, hs);
then ((e, hs));
else inTpl;
end match;
end collectPreVariablesTrverseExp;
protected function collectPreVariablesTrverseExp2 "author: lochel"
input tuple<DAE.Exp, HashSet.HashSet> inTpl;
output tuple<DAE.Exp, HashSet.HashSet> outTpl;
algorithm
outTpl := match(inTpl)
local
list<DAE.ComponentRef> crefs;
DAE.ComponentRef cr;
HashSet.HashSet hs;
DAE.Exp e;
case((e as DAE.CREF(componentRef=cr), hs)) equation
crefs = ComponentReference.expandCref(cr, true);
hs = List.fold(crefs, BaseHashSet.add, hs);
then ((e, hs));
else inTpl;
end match;
end collectPreVariablesTrverseExp2;
// =============================================================================
// warn about iteration variables with default zero start attribute
//
// =============================================================================
protected function warnAboutIterationVariablesWithDefaultZeroStartAttribute "author: lochel
This function ... read the function name."
input BackendDAE.BackendDAE inBackendDAE;
output Boolean outWarning;
protected
list<BackendDAE.EqSystem> eqs;
algorithm
BackendDAE.DAE(eqs=eqs) := inBackendDAE;
outWarning := warnAboutIterationVariablesWithDefaultZeroStartAttribute0(eqs);
end warnAboutIterationVariablesWithDefaultZeroStartAttribute;
protected function warnAboutIterationVariablesWithDefaultZeroStartAttribute0 "author: lochel"
input list<BackendDAE.EqSystem> inEqs;
output Boolean outWarning;
algorithm
outWarning := match(inEqs)
local
Boolean b1, b2;
BackendDAE.EqSystem eq;
list<BackendDAE.EqSystem> eqs;
case ({}) then false;
case (eq::eqs) equation
b1 = warnAboutIterationVariablesWithDefaultZeroStartAttribute0(eqs);
b2 = warnAboutIterationVariablesWithDefaultZeroStartAttribute1(eq);
then (b1 or b2);
end match;
end warnAboutIterationVariablesWithDefaultZeroStartAttribute0;
protected function warnAboutIterationVariablesWithDefaultZeroStartAttribute1 "author: lochel"
input BackendDAE.EqSystem inEqSystem;
output Boolean outWarning;
protected
BackendDAE.Variables vars;
BackendDAE.StrongComponents comps;
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars,
matching=BackendDAE.MATCHING(comps=comps)) := inEqSystem;
outWarning := warnAboutIterationVariablesWithDefaultZeroStartAttribute2(comps, vars);
end warnAboutIterationVariablesWithDefaultZeroStartAttribute1;
protected function warnAboutIterationVariablesWithDefaultZeroStartAttribute2 "author: lochel"
input BackendDAE.StrongComponents inComps;
input BackendDAE.Variables inVars;
output Boolean outWarning;
algorithm
outWarning := matchcontinue(inComps, inVars)
local
BackendDAE.StrongComponents rest;
list<BackendDAE.Var> varlst;
list<Integer> vlst;
Boolean linear, b;
String str;
case ({}, _) then false;
case (BackendDAE.MIXEDEQUATIONSYSTEM(disc_vars=vlst)::rest, _) equation
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
varlst = filterVarsWithoutStartValue(varlst);
false = List.isEmpty(varlst);
Debug.fcall(Flags.INITIALIZATION, Error.addCompilerWarning, "Iteration variables with default zero start attribute in mixed equation system:\n" +& warnAboutVars2(varlst));
_ = warnAboutIterationVariablesWithDefaultZeroStartAttribute2(rest, inVars);
then true;
case (BackendDAE.EQUATIONSYSTEM(vars=vlst, jacType=BackendDAE.JAC_NONLINEAR())::rest, _) equation
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
varlst = filterVarsWithoutStartValue(varlst);
false = List.isEmpty(varlst);
Debug.fcall(Flags.INITIALIZATION, Error.addCompilerWarning, "Iteration variables with default zero start attribute in nonlinear equation system:\n" +& warnAboutVars2(varlst));
_ = warnAboutIterationVariablesWithDefaultZeroStartAttribute2(rest, inVars);
then true;
case (BackendDAE.EQUATIONSYSTEM(vars=vlst, jacType=BackendDAE.JAC_GENERIC())::rest, _) equation
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
varlst = filterVarsWithoutStartValue(varlst);
false = List.isEmpty(varlst);
Debug.fcall(Flags.INITIALIZATION, Error.addCompilerWarning, "Iteration variables with default zero start attribute in equation system w/o analytic Jacobian:\n" +& warnAboutVars2(varlst));
_ = warnAboutIterationVariablesWithDefaultZeroStartAttribute2(rest, inVars);
then true;
case (BackendDAE.EQUATIONSYSTEM(vars=vlst, jacType=BackendDAE.JAC_NO_ANALYTIC())::rest, _) equation
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
varlst = filterVarsWithoutStartValue(varlst);
false = List.isEmpty(varlst);
Debug.fcall(Flags.INITIALIZATION, Error.addCompilerWarning, "Iteration variables with default zero start attribute in equation system w/o analytic Jacobian:\n" +& warnAboutVars2(varlst));
_ = warnAboutIterationVariablesWithDefaultZeroStartAttribute2(rest, inVars);
then true;
case (BackendDAE.TORNSYSTEM(tearingvars=vlst, linear=linear)::rest, _) equation
varlst = List.map1r(vlst, BackendVariable.getVarAt, inVars);
varlst = filterVarsWithoutStartValue(varlst);
false = List.isEmpty(varlst);
str = Util.if_(linear, "linear", "nonlinear");
Debug.fcall(Flags.INITIALIZATION, Error.addCompilerWarning, "Iteration variables with default zero start attribute in torn " +& str +& " equation system:\n" +& warnAboutVars2(varlst));
_ = warnAboutIterationVariablesWithDefaultZeroStartAttribute2(rest, inVars);
then true;
case (_::rest, _) equation
b = warnAboutIterationVariablesWithDefaultZeroStartAttribute2(rest, inVars);
then b;
end matchcontinue;
end warnAboutIterationVariablesWithDefaultZeroStartAttribute2;
function filterVarsWithoutStartValue "author: lochel"
input list<BackendDAE.Var> inVars;
output list<BackendDAE.Var> outVars;
algorithm
outVars := matchcontinue(inVars)
local
BackendDAE.Var v;
list<BackendDAE.Var> vars;
case ({}) then {};
case (v::vars) equation
_ = BackendVariable.varStartValueFail(v);
vars = filterVarsWithoutStartValue(vars);
then vars;
case (v::vars) equation
vars = filterVarsWithoutStartValue(vars);
then v::vars;
else fail();
end matchcontinue;
end filterVarsWithoutStartValue;
function warnAboutVars2 "author: lochel
TODO: Replace this with an general BackendDump implementation."
input list<BackendDAE.Var> inVars;
output String outString;
algorithm
outString := match(inVars)
local
BackendDAE.Var v;
list<BackendDAE.Var> vars;
String crStr;
String str;
case ({}) then "";
case (v::{}) equation
crStr = " " +& BackendDump.varString(v);
then crStr;
case (v::vars) equation
crStr = BackendDump.varString(v);
str = " " +& crStr +& "\n" +& warnAboutVars2(vars);
then str;
end match;
end warnAboutVars2;
// =============================================================================
// section for selecting initialization variables
//
// - unfixed state
// - unfixed parameter
// - unfixed discrete -> pre(vd)
// =============================================================================
protected function selectInitializationVariablesDAE "author: lochel
This function wraps selectInitializationVariables."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.Variables outVars;
protected
BackendDAE.EqSystems systs;
BackendDAE.Variables knownVars, alias;
algorithm
BackendDAE.DAE(systs, BackendDAE.SHARED(knownVars=knownVars, aliasVars=alias)) := inDAE;
outVars := selectInitializationVariables(systs);
outVars := BackendVariable.traverseBackendDAEVars(knownVars, selectInitializationVariables2, outVars);
outVars := BackendVariable.traverseBackendDAEVars(alias, selectInitializationVariables2, outVars);
end selectInitializationVariablesDAE;
protected function selectInitializationVariables "author: lochel"
input BackendDAE.EqSystems inEqSystems;
output BackendDAE.Variables outVars;
algorithm
outVars := BackendVariable.emptyVars();
outVars := List.fold(inEqSystems, selectInitializationVariables1, outVars);
end selectInitializationVariables;
protected function selectInitializationVariables1 "author: lochel"
input BackendDAE.EqSystem inEqSystem;
input BackendDAE.Variables inVars;
output BackendDAE.Variables outVars;
protected
BackendDAE.Variables vars;
BackendDAE.StateSets stateSets;
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars, stateSets=stateSets) := inEqSystem;
outVars := BackendVariable.traverseBackendDAEVars(vars, selectInitializationVariables2, inVars);
// ignore not the states of the statesets
// outVars := List.fold(stateSets, selectInitialStateSetVars, outVars);
end selectInitializationVariables1;
protected function selectInitializationVariables2 "author: lochel"
input tuple<BackendDAE.Var, BackendDAE.Variables> inTpl;
output tuple<BackendDAE.Var, BackendDAE.Variables> outTpl;
algorithm
outTpl := matchcontinue(inTpl)
local
BackendDAE.Var var, preVar;
BackendDAE.Variables vars;
DAE.ComponentRef cr, preCR;
DAE.Type ty;
DAE.InstDims arryDim;
// unfixed state
case((var as BackendDAE.VAR(varName=_, varKind=BackendDAE.STATE(index=_)), vars)) equation
false = BackendVariable.varFixed(var);
// ignore stateset variables
// false = isStateSetVar(cr);
vars = BackendVariable.addVar(var, vars);
then ((var, vars));
// unfixed parameter
case((var as BackendDAE.VAR(varKind=BackendDAE.PARAM()), vars)) equation
false = BackendVariable.varFixed(var);
vars = BackendVariable.addVar(var, vars);
then ((var, vars));
// unfixed discrete -> pre(vd)
case((var as BackendDAE.VAR(varName=cr, varKind=BackendDAE.DISCRETE(), varType=ty, arryDim=arryDim), vars)) equation
false = BackendVariable.varFixed(var);
preCR = ComponentReference.crefPrefixPre(cr); // cr => $PRE.cr
preVar = BackendDAE.VAR(preCR, BackendDAE.VARIABLE(), DAE.BIDIR(), DAE.NON_PARALLEL(), ty, NONE(), NONE(), arryDim, DAE.emptyElementSource, NONE(), NONE(), DAE.NON_CONNECTOR());
vars = BackendVariable.addVar(preVar, vars);
then ((var, vars));
else
then inTpl;
end matchcontinue;
end selectInitializationVariables2;
// =============================================================================
// section for simplifying initial functions
//
// =============================================================================
protected function simplifyInitialFunctions "author: Frenkel TUD 2012-12
simplify initial() with true and sample with false"
input tuple<DAE.Exp, Boolean /* homotopy used? */> inTpl;
output tuple<DAE.Exp, Boolean /* homotopy used? */> outTpl;
protected
DAE.Exp exp;
Boolean useHomotopy;
algorithm
(exp, useHomotopy) := inTpl;
outTpl := Expression.traverseExp(exp, simplifyInitialFunctionsExp, useHomotopy);
end simplifyInitialFunctions;
protected function simplifyInitialFunctionsExp "author: Frenkel TUD 2012-12
helper for simplifyInitialFunctions"
input tuple<DAE.Exp, Boolean /* homotopy used? */> inExp;
output tuple<DAE.Exp, Boolean /* homotopy used? */> outExp;
algorithm
outExp := matchcontinue(inExp)
local
DAE.Exp e1, e2, e3, actual, simplified;
Boolean useHomotopy;
case ((DAE.CALL(path = Absyn.IDENT(name="initial")), useHomotopy)) then ((DAE.BCONST(true), useHomotopy));
case ((DAE.CALL(path = Absyn.IDENT(name="sample")), useHomotopy)) then ((DAE.BCONST(false), useHomotopy));
case ((DAE.CALL(path = Absyn.IDENT(name="delay"), expLst = _::e1::_ ), useHomotopy)) then ((e1, useHomotopy));
case ((DAE.CALL(path = Absyn.IDENT(name="homotopy"), expLst = actual::simplified::_ ), _)) equation
e1 = Expression.makeBuiltinCall("homotopyParameter", {}, DAE.T_REAL_DEFAULT);
e2 = DAE.BINARY(e1, DAE.MUL(DAE.T_REAL_DEFAULT), actual);
e3 = DAE.BINARY(DAE.RCONST(1.0), DAE.SUB(DAE.T_REAL_DEFAULT), e1);
e1 = DAE.BINARY(e3, DAE.MUL(DAE.T_REAL_DEFAULT), simplified);
e3 = DAE.BINARY(e2, DAE.ADD(DAE.T_REAL_DEFAULT), e1);
then ((e3, true));
else inExp;
end matchcontinue;
end simplifyInitialFunctionsExp;
// =============================================================================
// section for pre-balancing the initial system
//
// This section removes unused pre variables and auto-fixes non-pre variables,
// which occure in no equation.
// =============================================================================
protected function preBalanceInitialSystem "author: lochel"
input BackendDAE.EqSystem inSystem;
output BackendDAE.EqSystem outSystem;
output list<BackendDAE.Var> outDumpVars;
protected
BackendDAE.Variables orderedVars;
BackendDAE.EquationArray orderedEqs;
BackendDAE.Matching matching;
BackendDAE.StateSets stateSets;
Boolean b;
BackendDAE.IncidenceMatrix mt;
algorithm
(_, _, mt) := BackendDAEUtil.getIncidenceMatrix(inSystem, BackendDAE.NORMAL(), NONE());
BackendDAE.EQSYSTEM(orderedVars=orderedVars, orderedEqs=orderedEqs, stateSets=stateSets) := inSystem;
(orderedVars, orderedEqs, b, outDumpVars) := preBalanceInitialSystem1(arrayLength(mt), mt, orderedVars, orderedEqs, false, {});
outSystem := Util.if_(b, BackendDAE.EQSYSTEM(orderedVars, orderedEqs, NONE(), NONE(), BackendDAE.NO_MATCHING(), stateSets), inSystem);
end preBalanceInitialSystem;
protected function preBalanceInitialSystem1 "author: lochel"
input Integer n;
input BackendDAE.IncidenceMatrix mt;
input BackendDAE.Variables inVars;
input BackendDAE.EquationArray inEqs;
input Boolean iB;
input list<BackendDAE.Var> inDumpVars;
output BackendDAE.Variables outVars;
output BackendDAE.EquationArray outEqs;
output Boolean oB;
output list<BackendDAE.Var> outDumpVars;
algorithm
(outVars, outEqs, oB, outDumpVars) := match (n, mt, inVars, inEqs, iB, inDumpVars)
local
list<Integer> row;
Boolean b, useHomotopy;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqs;
list<BackendDAE.Var> rvarlst;
BackendDAE.Var var;
DAE.ComponentRef cref;
list<BackendDAE.Var> dumpVars;
case(0, _, _, _, false, _)
then (inVars, inEqs, false, inDumpVars);
case(0, _, _, _, true, _) equation
vars = BackendVariable.listVar1(BackendVariable.varList(inVars));
then (vars, inEqs, true, inDumpVars);
case(_, _, _, _, _, _) equation
true = n > 0;
(vars, eqs, b, dumpVars) = preBalanceInitialSystem2(n, mt, inVars, inEqs, iB, inDumpVars);
(vars, eqs, b, dumpVars) = preBalanceInitialSystem1(n-1, mt, vars, eqs, b, dumpVars);
then (vars, eqs, b, dumpVars);
end match;
end preBalanceInitialSystem1;
protected function preBalanceInitialSystem2 "author: lochel"