/
DAELow.mo
18774 lines (17685 loc) · 658 KB
/
DAELow.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-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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.
*
*/
package DAELow
" file: DAELow.mo
package: DAELow
description: DAELow a lower form of DAE including sparse matrises for
BLT decomposition, etc.
RCS: $Id$
This module is a lowered form of a DAE including equations
and simple equations in
two separate lists. The variables are split into known variables
parameters and constants, and unknown variables,
states and algebraic variables.
The module includes the BLT sorting algorithm which sorts the
equations into blocks, and the index reduction algorithm using
dummy derivatives for solving higher index problems.
It also includes the tarjan algorithm to detect strong components
in the BLT sorting."
public import Absyn;
public import DAE;
public import SCode;
public import Values;
public import Builtin;
public import HashTable2;
public constant String derivativeNamePrefix="$DER";
public constant String partialDerivativeNamePrefix="$pDER";
public uniontype Type "
Once we are in DAELow, the Type can be only basic types or enumeration.
We cannot do this in DAE because functions may contain many more types."
record REAL end REAL;
record INT end INT;
record BOOL end BOOL;
record STRING end STRING;
record ENUMERATION
list<String> stringLst;
end ENUMERATION;
record EXT_OBJECT
Absyn.Path fullClassName;
end EXT_OBJECT;
end Type;
public
uniontype VarKind "- Variabile kind"
record VARIABLE end VARIABLE;
record STATE end STATE;
record STATE_DER end STATE_DER;
record DUMMY_DER end DUMMY_DER;
record DUMMY_STATE end DUMMY_STATE;
record DISCRETE end DISCRETE;
record PARAM end PARAM;
record CONST end CONST;
record EXTOBJ Absyn.Path fullClassName; end EXTOBJ;
end VarKind;
public
uniontype Var "- Variables"
record VAR
DAE.ComponentRef varName "varName ; variable name" ;
VarKind varKind "varKind ; Kind of variable" ;
DAE.VarDirection varDirection "varDirection ; input, output or bidirectional" ;
Type varType "varType ; builtin type or enumeration" ;
Option<DAE.Exp> bindExp "bindExp ; Binding expression e.g. for parameters" ;
Option<Values.Value> bindValue "bindValue ; binding value for parameters" ;
DAE.InstDims arryDim "arryDim ; array dimensions on nonexpanded var" ;
Integer index "index ; index in impl. vector" ;
DAE.ElementSource source "origin of variable" ;
Option<DAE.VariableAttributes> values "values ; values on builtin attributes" ;
Option<SCode.Comment> comment "comment ; this contains the comment and annotation from Absyn" ;
DAE.Flow flowPrefix "flow ; if the variable is a flow" ;
DAE.Stream streamPrefix "stream ; if the variable is a stream variable. Modelica 3.1 specs" ;
end VAR;
end Var;
public
uniontype Equation "- Equation"
record EQUATION
DAE.Exp exp;
DAE.Exp scalar "scalar" ;
DAE.ElementSource source "origin of equation";
end EQUATION;
record ARRAY_EQUATION
Integer index "index ; index in arrayequations 0..n-1" ;
list<DAE.Exp> crefOrDerCref "crefOrDerCref ; CREF or der(CREF)" ;
DAE.ElementSource source "origin of equation";
end ARRAY_EQUATION;
record SOLVED_EQUATION
DAE.ComponentRef componentRef "componentRef" ;
DAE.Exp exp "exp" ;
DAE.ElementSource source "origin of equation";
end SOLVED_EQUATION;
record RESIDUAL_EQUATION
DAE.Exp exp "exp ; not present from front end" ;
DAE.ElementSource source "origin of equation";
end RESIDUAL_EQUATION;
record ALGORITHM
Integer index "Index in algorithms, 0..n-1" ;
list<DAE.Exp> in_ "Inputs CREF or der(CREF)" ;
list<DAE.Exp> out "Outputs CREF or der(CREF)" ;
DAE.ElementSource source "origin of algorithm";
end ALGORITHM;
record WHEN_EQUATION
WhenEquation whenEquation "whenEquation" ;
DAE.ElementSource source "origin of equation";
end WHEN_EQUATION;
record COMPLEX_EQUATION "complex equations: recordX = function call(x, y, ..);"
Integer index "Index in algorithm clauses";
DAE.Exp lhs "left ; lhs";
DAE.Exp rhs "right ; rhs";
DAE.ElementSource source "origin of equation";
end COMPLEX_EQUATION;
end Equation;
public
uniontype WhenEquation "- When Equation"
record WHEN_EQ
Integer index "Index in when clauses" ;
DAE.ComponentRef left "Left hand side of equation" ;
DAE.Exp right "Right hand side of equation" ;
Option<WhenEquation> elsewhenPart "elsewhen equation with the same cref on the left hand side.";
end WHEN_EQ;
end WhenEquation;
public
uniontype ReinitStatement "- Reinit Statement"
record REINIT
DAE.ComponentRef stateVar "State variable to reinit" ;
DAE.Exp value "Value after reinit" ;
DAE.ElementSource source "origin of equation";
end REINIT;
record EMPTY_REINIT
end EMPTY_REINIT;
end ReinitStatement;
public
uniontype WhenClause "- When Clause"
record WHEN_CLAUSE
DAE.Exp condition "The when-condition" ;
list<ReinitStatement> reinitStmtLst "List of reinit statements associated to the when clause." ;
Option<Integer> elseClause "index of elsewhen clause" ;
// HL only needs to know if it is an elsewhen the equations take care of which clauses are related.
// The equations associated to the clause are linked to this when clause by the index in the
// when clause list where this when clause is stored.
end WHEN_CLAUSE;
end WhenClause;
public
uniontype ZeroCrossing "- Zero Crossing"
record ZERO_CROSSING
DAE.Exp relation_ "function" ;
list<Integer> occurEquLst "List of equations where the function occurs" ;
list<Integer> occurWhenLst "List of when clauses where the function occurs" ;
end ZERO_CROSSING;
end ZeroCrossing;
public
uniontype EventInfo "- EventInfo"
record EVENT_INFO
list<WhenClause> whenClauseLst "List of when clauses. The WhenEquation datatype refer to this list by position" ;
list<ZeroCrossing> zeroCrossingLst "zeroCrossingLst" ;
end EVENT_INFO;
end EventInfo;
public
uniontype DAELow "THE LOWERED DAE consist of variables and equations. The variables are split into
two lists, one for unknown variables states and algebraic and one for known variables
constants and parameters.
The equations are also split into two lists, one with simple equations, a=b, a-b=0, etc., that
are removed from the set of equations to speed up calculations.
- DAELow"
record DAELOW
Variables orderedVars "orderedVars ; ordered Variables, only states and alg. vars" ;
Variables knownVars "knownVars ; Known variables, i.e. constants and parameters" ;
Variables externalObjects "External object variables";
AliasVariables aliasVars "mappings of alias-variables to real-variables"; // added asodja 2010-03-03
EquationArray orderedEqs "orderedEqs ; ordered Equations" ;
EquationArray removedEqs "removedEqs ; Removed equations a=b" ;
EquationArray initialEqs "initialEqs ; Initial equations" ;
MultiDimEquation[:] arrayEqs "arrayEqs ; Array equations" ;
DAE.Algorithm[:] algorithms "algorithms ; Algorithms" ;
EventInfo eventInfo "eventInfo" ;
ExternalObjectClasses extObjClasses "classes of external objects, contains constructor & destructor";
end DAELOW;
end DAELow;
type ExternalObjectClasses = list<ExternalObjectClass> "classes of external objects stored in list";
uniontype ExternalObjectClass "class of external objects"
record EXTOBJCLASS
Absyn.Path path "className of external object";
DAE.Element constructor "constructor is an EXTFUNCTION";
DAE.Element destructor "destructor is an EXTFUNCTION";
DAE.ElementSource source "origin of equation";
end EXTOBJCLASS;
end ExternalObjectClass;
public
uniontype Variables "- Variables"
record VARIABLES
list<CrefIndex>[:] crefIdxLstArr "crefIdxLstArr ; HashTB, cref->indx" ;
list<StringIndex>[:] strIdxLstArr "strIdxLstArr ; HashTB, cref->indx for old names" ;
VariableArray varArr "varArr ; Array of variables" ;
Integer bucketSize "bucketSize ; bucket size" ;
Integer numberOfVars "numberOfVars ; no. of vars" ;
end VARIABLES;
end Variables;
public
uniontype AliasVariables "
Data originating from removed simple equations needed to build
variables' lookup table (in C output).
In that way, double buffering of variables in pre()-buffer, extrapolation
buffer and results caching, etc., is avoided, but in C-code output all the
data about variables' names, comments, units, etc. is preserved as well as
pinter to their values (trajectories).
"
record ALIASVARS
HashTable2.HashTable varMappings "replacements from trivial equations of kind a=b or a=-b";
Variables aliasVars "hash table with (removed) variables metadata";
end ALIASVARS;
end AliasVariables;
public
uniontype MultiDimEquation "- Multi Dimensional Equation"
record MULTIDIM_EQUATION
list<Integer> dimSize "dimSize ; dimension sizes" ;
DAE.Exp left "left ; lhs" ;
DAE.Exp right "right ; rhs" ;
DAE.ElementSource source "the element source";
end MULTIDIM_EQUATION;
end MultiDimEquation;
public
uniontype CrefIndex "- Component Reference Index"
record CREFINDEX
DAE.ComponentRef cref "cref" ;
Integer index "index" ;
end CREFINDEX;
end CrefIndex;
public
uniontype StringIndex "- String Index"
record STRINGINDEX
String str "str" ;
Integer index "index" ;
end STRINGINDEX;
end StringIndex;
public
uniontype VariableArray "array of Equations are expandable, to amortize the cost of adding
equations in a more efficient manner
- Variable Array"
record VARIABLE_ARRAY
Integer numberOfElements "numberOfElements ; no. elements" ;
Integer arrSize "arrSize ; array size" ;
Option<Var>[:] varOptArr "varOptArr" ;
end VARIABLE_ARRAY;
end VariableArray;
public
uniontype EquationArray "- Equation Array"
record EQUATION_ARRAY
Integer numberOfElement "numberOfElement ; no. elements" ;
Integer arrSize "arrSize ; array size" ;
Option<Equation>[:] equOptArr "equOptArr" ;
end EQUATION_ARRAY;
end EquationArray;
public
uniontype Assignments "Assignments of variables to equations and vice versa are implemented by a
expandable array to amortize addition of array elements more efficient
- Assignments"
record ASSIGNMENTS
Integer actualSize "actualSize ; actual size" ;
Integer allocatedSize "allocatedSize ; allocated size >= actual size" ;
Integer[:] arrOfIndices "arrOfIndices ; array of indices" ;
end ASSIGNMENTS;
end Assignments;
public
uniontype BinTree "Generic Binary tree implementation
- Binary Tree"
record TREENODE
Option<TreeValue> value "value ; Value" ;
Option<BinTree> leftSubTree "leftSubTree ; left subtree" ;
Option<BinTree> rightSubTree "rightSubTree ; right subtree" ;
end TREENODE;
end BinTree;
public
uniontype TreeValue "Each node in the binary tree can have a value associated with it.
- Tree Value"
record TREEVALUE
Key key "Key" ;
Value value "Value" ;
end TREEVALUE;
end TreeValue;
public
type Key = DAE.ComponentRef "A key is a Component Reference
- Key" ;
public
type Value = Integer "- Value" ;
public
type IncidenceMatrix = list<Integer>[:];
public
type IncidenceMatrixT = IncidenceMatrix "IncidenceMatrixT : a list of equation indexes (1..n),
one for each variable. Equations that -only-
contain the state variable and not the derivative
has a negative index.
- Incidence Matrix T" ;
public
uniontype JacobianType "- Jacobian Type"
record JAC_CONSTANT "If jacobian has only constant values, for system
of equations this means that it can be solved statically." end JAC_CONSTANT;
record JAC_TIME_VARYING "If jacobian has time varying parts, like parameters or
algebraic variables" end JAC_TIME_VARYING;
record JAC_NONLINEAR "If jacobian contains variables that are solved for,
means that a nonlinear system of equations needs to be
solved" end JAC_NONLINEAR;
record JAC_NO_ANALYTIC "No analytic jacobian available" end JAC_NO_ANALYTIC;
end JacobianType;
public
uniontype IndexReduction "- Index Reduction"
record INDEX_REDUCTION "Use index reduction during matching" end INDEX_REDUCTION;
record NO_INDEX_REDUCTION "do not use index reduction during matching" end NO_INDEX_REDUCTION;
end IndexReduction;
public
uniontype EquationConstraints "- Equation Constraints"
record ALLOW_UNDERCONSTRAINED "for e.g. initial eqns.
where not all variables
have a solution" end ALLOW_UNDERCONSTRAINED;
record EXACT "exact as many equations
as variables" end EXACT;
end EquationConstraints;
public
uniontype EquationReduction
record REMOVE_SIMPLE_EQN end REMOVE_SIMPLE_EQN;
record KEEP_SIMPLE_EQN "removes simple equation after index reduction does not remove simple equations after index reduction" end KEEP_SIMPLE_EQN;
end EquationReduction;
public
type MatchingOptions = tuple<IndexReduction, EquationConstraints, EquationReduction> "- Matching Options" ;
public
uniontype DivZeroExpReplace "- Should the division operator replaced by a operator with check of the denominator"
record ALL " check all expressions" end ALL;
record ONLY_VARIABLES " for expressions with variable variables(no parameters)" end ONLY_VARIABLES;
end DivZeroExpReplace;
protected import Algorithm;
protected import BackendVarTransform;
protected import Ceval;
protected import ClassInf;
protected import DAEEXT;
protected import DAEUtil;
protected import Debug;
protected import Derive;
protected import Env;
protected import Error;
protected import Exp;
protected import OptManager;
protected import RTOpts;
protected import System;
protected import Util;
protected import DAEDump;
protected import IOStream;
protected import Inline;
protected import ValuesUtil;
protected import VarTransform;
protected import DAELowUtil;
public constant BinTree emptyBintree=TREENODE(NONE,NONE,NONE) " Empty binary tree " ;
public function dumpDAELowEqnList
input list<Equation> inDAELowEqnList;
input String header;
input Boolean printExpTree;
algorithm
print(header);
dumpDAELowEqnList2(inDAELowEqnList,printExpTree);
print("===================\n");
end dumpDAELowEqnList;
protected function dumpDAELowEqnList2
input list<Equation> inDAELowEqnList;
input Boolean printExpTree;
algorithm
_ := matchcontinue (inDAELowEqnList,printExpTree)
local
DAE.Exp e1_1,e2_1,e1,e2,e_1,e;
String str;
list<String> strList;
list<Equation> res;
list<DAE.Exp> expList,expList2;
Integer i;
DAE.ElementSource source "the element source";
case ({},_) then ();
case (EQUATION(e1,e2,source)::res,printExpTree) /* header */
equation
dumpDAELowEqnList2(res,printExpTree);
print("EQUATION: ");
str = Exp.printExpStr(e1);
print(str);
print("\n");
str = Exp.dumpExpStr(e1,0);
str = Util.if_(printExpTree,str,"");
print(str);
print("\n");
then
();
case (COMPLEX_EQUATION(i,e1,e2,source)::res,printExpTree) /* header */
equation
dumpDAELowEqnList2(res,printExpTree);
print("COMPLEX_EQUATION: ");
str = Exp.printExpStr(e1);
print(str);
print("\n");
str = Exp.dumpExpStr(e1,0);
str = Util.if_(printExpTree,str,"");
print(str);
print("\n");
then
();
case (SOLVED_EQUATION(_,e,source)::res,printExpTree)
equation
dumpDAELowEqnList2(res,printExpTree);
print("SOLVED_EQUATION: ");
str = Exp.printExpStr(e);
print(str);
print("\n");
str = Exp.dumpExpStr(e,0);
str = Util.if_(printExpTree,str,"");
print(str);
print("\n");
then
();
case (RESIDUAL_EQUATION(e,source)::res,printExpTree)
equation
dumpDAELowEqnList2(res,printExpTree);
print("RESIDUAL_EQUATION: ");
str = Exp.printExpStr(e);
print(str);
print("\n");
str = Exp.dumpExpStr(e,0);
str = Util.if_(printExpTree,str,"");
print(str);
print("\n");
then
();
case (ARRAY_EQUATION(_,expList,source)::res,printExpTree)
equation
dumpDAELowEqnList2(res,printExpTree);
print("ARRAY_EQUATION: ");
strList = Util.listMap(expList,Exp.printExpStr);
str = Util.stringDelimitList(strList," | ");
print(str);
print("\n");
then
();
case (ALGORITHM(_,expList,expList2,source)::res,printExpTree)
equation
dumpDAELowEqnList2(res,printExpTree);
print("ALGORITHM: ");
strList = Util.listMap(expList,Exp.printExpStr);
str = Util.stringDelimitList(strList," | ");
print(str);
print("\n");
strList = Util.listMap(expList2,Exp.printExpStr);
str = Util.stringDelimitList(strList," | ");
print(str);
print("\n");
then
();
case (WHEN_EQUATION(WHEN_EQ(_,_,e,_/*TODO handle elsewhe also*/),source)::res,printExpTree)
equation
dumpDAELowEqnList2(res,printExpTree);
print("WHEN_EQUATION: ");
str = Exp.printExpStr(e);
print(str);
print("\n");
str = Exp.dumpExpStr(e,0);
str = Util.if_(printExpTree,str,"");
print(str);
print("\n");
then
();
case (_::res,printExpTree)
equation
then ();
end matchcontinue;
end dumpDAELowEqnList2;
protected function hasNoStates
"@author: adrpo
this function tells if there are NO states in the binary tree"
input BinTree states;
output Boolean out;
algorithm
out := matchcontinue (states)
// if the tree is empty then there are no states
case (TREENODE(NONE,NONE,NONE)) then true;
case (_) then false;
end matchcontinue;
end hasNoStates;
public function lower
"function: lower
This function translates a DAE, which is the result from instantiating a
class, into a more precise form, called DAELow defined in this module.
The DAELow representation splits the DAE into equations and variables
and further divides variables into known and unknown variables and the
equations into simple and nonsimple equations.
The variables are inserted into a hash table. This gives a lookup cost of
O(1) for finding a variable. The equations are put in an expandable
array. Where adding a new equation can be done in O(1) time if space
is available.
inputs: daeList: DAE.DAElist, simplify: bool)
outputs: DAELow"
input DAE.DAElist lst;
input Boolean addDummyDerivativeIfNeeded;
input Boolean simplify;
// input Boolean removeTrivEqs "temporal input, for legacy purposes; doesn't add trivial equations to removed equations";
output DAELow outDAELow;
algorithm
outDAELow := matchcontinue(lst, addDummyDerivativeIfNeeded, simplify)
local
BinTree s;
Variables vars,knvars,vars_1,extVars;
AliasVariables aliasVars "hash table with alias vars' replacements (a=b or a=-b)";
list<Equation> eqns,reqns,ieqns,algeqns,multidimeqns,imultidimeqns,eqns_1;
list<MultiDimEquation> aeqns,aeqns1,iaeqns;
list<DAE.Algorithm> algs,algs_1;
list<WhenClause> whenclauses,whenclauses_1;
list<ZeroCrossing> zero_crossings;
EquationArray eqnarr,reqnarr,ieqnarr;
MultiDimEquation[:] arr_md_eqns;
DAE.Algorithm[:] algarr;
ExternalObjectClasses extObjCls;
Boolean daeContainsNoStates, shouldAddDummyDerivative;
EventInfo einfo;
DAE.FunctionTree funcs;
case(lst, addDummyDerivativeIfNeeded, true) // simplify by default
equation
lst = processDelayExpressions(lst);
s = states(lst, emptyBintree);
vars = emptyVars();
knvars = emptyVars();
extVars = emptyVars();
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,whenclauses,extObjCls,s) = lower2(lst, s, vars, knvars, extVars, {});
daeContainsNoStates = hasNoStates(s); // check if the DAE has states
// adrpo: add the dummy derivative state ONLY IF the DAE contains
// no states AND ONLY if addDummyDerivative is set to true!
shouldAddDummyDerivative = boolAnd(addDummyDerivativeIfNeeded, daeContainsNoStates);
(vars,eqns) = addDummyState(vars, eqns, shouldAddDummyDerivative);
whenclauses_1 = listReverse(whenclauses);
algeqns = lowerAlgorithms(vars, algs);
(multidimeqns,imultidimeqns) = lowerMultidimeqns(vars, aeqns, iaeqns);
eqns = listAppend(algeqns, eqns);
eqns = listAppend(multidimeqns, eqns);
ieqns = listAppend(imultidimeqns, ieqns);
aeqns = listAppend(aeqns,iaeqns);
(vars,knvars,eqns,reqns,ieqns,aeqns1,algs_1,aliasVars) = removeSimpleEquations(vars, knvars, eqns, reqns, ieqns, aeqns, algs, s);
vars_1 = detectImplicitDiscrete(vars, eqns);
eqns_1 = sortEqn(eqns);
(eqns_1,ieqns,aeqns1,algs,vars_1) = expandDerOperator(vars_1,eqns_1,ieqns,aeqns1,algs_1,DAEUtil.daeFunctionTree(lst));
(zero_crossings) = findZeroCrossings(vars_1,knvars,eqns_1,aeqns1,whenclauses_1,algs);
eqnarr = listEquation(eqns_1);
reqnarr = listEquation(reqns);
ieqnarr = listEquation(ieqns);
arr_md_eqns = listArray(aeqns1);
algarr = listArray(algs);
funcs = DAEUtil.daeFunctionTree(lst);
einfo = Inline.inlineEventInfo(EVENT_INFO(whenclauses_1,zero_crossings),(NONE(),SOME(funcs),{DAE.NORM_INLINE()}));
DAELowUtil.checkDEALowWithErrorMsg(DAELOW(vars_1,knvars,extVars,aliasVars,eqnarr,reqnarr,ieqnarr,arr_md_eqns,algarr,einfo,extObjCls));
then DAELOW(vars_1,knvars,extVars,aliasVars,eqnarr,reqnarr,ieqnarr,arr_md_eqns,algarr,einfo,extObjCls);
case(lst, addDummyDerivativeIfNeeded, false) // do not simplify
equation
lst = processDelayExpressions(lst);
s = states(lst, emptyBintree);
vars = emptyVars();
knvars = emptyVars();
extVars = emptyVars();
(vars,knvars,extVars,eqns,reqns,ieqns,aeqns,iaeqns,algs,whenclauses,extObjCls,s) = lower2(lst, s, vars, knvars, extVars, {});
daeContainsNoStates = hasNoStates(s); // check if the DAE has states
// adrpo: add the dummy derivative state ONLY IF the DAE contains
// no states AND ONLY if addDummyDerivative is set to true!
shouldAddDummyDerivative = boolAnd(addDummyDerivativeIfNeeded, daeContainsNoStates);
(vars,eqns) = addDummyState(vars, eqns, shouldAddDummyDerivative);
whenclauses_1 = listReverse(whenclauses);
algeqns = lowerAlgorithms(vars, algs);
(multidimeqns,imultidimeqns) = lowerMultidimeqns(vars, aeqns, iaeqns);
eqns = listAppend(algeqns, eqns);
eqns = listAppend(multidimeqns, eqns);
ieqns = listAppend(imultidimeqns, ieqns);
// no simplify (vars,knvars,eqns,reqns,ieqns,aeqns1) = removeSimpleEquations(vars, knvars, eqns, reqns, ieqns, aeqns, s);
aliasVars = emptyAliasVariables();
vars_1 = detectImplicitDiscrete(vars, eqns);
eqns_1 = sortEqn(eqns);
// no simplify (eqns_1,ieqns,aeqns1,algs,vars_1) = expandDerOperator(vars_1,eqns_1,ieqns,aeqns1,algs);
(zero_crossings) = findZeroCrossings(vars_1,knvars,eqns_1,aeqns,whenclauses_1,algs);
eqnarr = listEquation(eqns_1);
reqnarr = listEquation(reqns);
ieqnarr = listEquation(ieqns);
arr_md_eqns = listArray(aeqns);
algarr = listArray(algs);
funcs = DAEUtil.daeFunctionTree(lst);
einfo = Inline.inlineEventInfo(EVENT_INFO(whenclauses_1,zero_crossings),(NONE(),SOME(funcs),{DAE.NORM_INLINE()}));
DAELowUtil.checkDEALowWithErrorMsg(DAELOW(vars_1,knvars,extVars,aliasVars,eqnarr,reqnarr,ieqnarr,arr_md_eqns,algarr,einfo,extObjCls));
then DAELOW(vars_1,knvars,extVars,aliasVars,eqnarr,reqnarr,ieqnarr,arr_md_eqns,algarr,einfo,extObjCls);
end matchcontinue;
end lower;
protected function expandDerOperator
"function expandDerOperator
expands der(expr) using Derive.differentiteExpTime.
This can not be done in Static, since we need all time-
dependent variables, which is only available in DAELow."
input Variables vars;
input list<Equation> eqns;
input list<Equation> ieqns;
input list<MultiDimEquation> aeqns;
input list<DAE.Algorithm> algs;
input DAE.FunctionTree functions;
output list<Equation> outEqns;
output list<Equation> outIeqns;
output list<MultiDimEquation> outAeqns;
output list<DAE.Algorithm> outAlgs;
output Variables outVars;
algorithm
(outEqns, outIeqns,outAeqns,outAlgs,outVars) :=
matchcontinue(vars,eqns,ieqns,aeqns,algs,functions)
case(vars,eqns,ieqns,aeqns,algs,functions) equation
(eqns,(vars,_)) = expandDerOperatorEqns(eqns,(vars,functions));
(ieqns,(vars,_)) = expandDerOperatorEqns(ieqns,(vars,functions));
(aeqns,(vars,_)) = expandDerOperatorArrEqns(aeqns,(vars,functions));
(algs,(vars,_)) = expandDerOperatorAlgs(algs,(vars,functions));
then(eqns,ieqns,aeqns,algs,vars);
end matchcontinue;
end expandDerOperator;
protected function expandDerOperatorEqns
"Help function to expandDerOperator"
input list<Equation> eqns;
input tuple<Variables,DAE.FunctionTree> vars;
output list<Equation> outEqns;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outEqns,outVars) := matchcontinue(eqns,vars)
local Equation e;
case({},vars) then ({},vars);
case(e::eqns,vars) equation
(e,vars) = expandDerOperatorEqn(e,vars);
(eqns,vars) = expandDerOperatorEqns(eqns,vars);
then (e::eqns,vars);
case(_,_) equation
Debug.fprint("failtrace", "-DAELow.expandDerOperatorEqns failed\n");
then fail();
end matchcontinue;
end expandDerOperatorEqns;
protected function expandDerOperatorEqn
"Help function to expandDerOperator, handles Equations"
input Equation eqn;
input tuple<Variables,DAE.FunctionTree> vars;
output Equation outEqn;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outEqn,outVars) := matchcontinue(eqn,vars)
local
DAE.Exp e1,e2; list<DAE.Exp> expl; Integer i;
DAE.ComponentRef cr; WhenEquation wheneq;
DAE.ElementSource source "the element source";
case(EQUATION(e1,e2,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
((e2,vars)) = Exp.traverseExp(e2,expandDerExp,vars);
then (EQUATION(e1,e2,source),vars);
case(COMPLEX_EQUATION(i,e1,e2,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
((e2,vars)) = Exp.traverseExp(e2,expandDerExp,vars);
then (COMPLEX_EQUATION(i,e1,e2,source),vars);
case (ARRAY_EQUATION(i,expl,source),vars)
then (ARRAY_EQUATION(i,expl,source),vars);
case (SOLVED_EQUATION(cr,e1,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (SOLVED_EQUATION(cr,e1,source),vars);
case(RESIDUAL_EQUATION(e1,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (RESIDUAL_EQUATION(e1,source),vars);
case (eqn as ALGORITHM(index = _),vars) then (eqn,vars);
case (WHEN_EQUATION(wheneq,source),vars) equation
(wheneq,vars) = expandDerOperatorWhenEqn(wheneq,vars);
then (WHEN_EQUATION(wheneq,source),vars);
case (eqn ,vars) equation
true = RTOpts.debugFlag("failtrace");
Debug.fprint("failtrace", "- DAELow.expandDerOperatorEqn, eqn =");
Debug.fprint("failtrace", equationStr(eqn));
Debug.fprint("failtrace", " failed\n");
then fail();
end matchcontinue;
end expandDerOperatorEqn;
protected function expandDerOperatorWhenEqn
"Helper function to expandDerOperatorWhenEqn"
input WhenEquation wheneq;
input tuple<Variables,DAE.FunctionTree> vars;
output WhenEquation outWheneq;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outWheneq, outVars) := matchcontinue(wheneq,vars)
local DAE.ComponentRef cr; DAE.Exp e1; Integer indx; WhenEquation elsewheneq;
case(WHEN_EQ(indx,cr,e1,SOME(elsewheneq)),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
(elsewheneq,vars) = expandDerOperatorWhenEqn(elsewheneq,vars);
then (WHEN_EQ(indx,cr,e1,SOME(elsewheneq)),vars);
case(WHEN_EQ(indx,cr,e1,NONE),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (WHEN_EQ(indx,cr,e1,NONE),vars);
end matchcontinue;
end expandDerOperatorWhenEqn;
protected function expandDerOperatorAlgs
"Help function to expandDerOperator"
input list<DAE.Algorithm> algs;
input tuple<Variables,DAE.FunctionTree> vars;
output list<DAE.Algorithm> outAlgs;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outAlgs,outVars) := matchcontinue(algs,vars)
local DAE.Algorithm a;
case({},vars) then ({},vars);
case(a::algs,vars) equation
(a,vars) = expandDerOperatorAlg(a,vars);
(algs,vars) = expandDerOperatorAlgs(algs,vars);
then (a::algs,vars);
case(_,_) equation
Debug.fprint("failtrace", "-DAELow.expandDerOperatorAlgs failed\n");
then fail();
end matchcontinue;
end expandDerOperatorAlgs;
protected function expandDerOperatorAlg
"Help function to to expandDerOperator, handles Algorithms"
input DAE.Algorithm alg;
input tuple<Variables,DAE.FunctionTree> vars;
output DAE.Algorithm outAlg;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outAlg,outVars) := matchcontinue(alg,vars)
local list<Algorithm.Statement> stmts;
case(DAE.ALGORITHM_STMTS(stmts),vars) equation
(stmts,vars) = expandDerOperatorStmts(stmts,vars);
then (DAE.ALGORITHM_STMTS(stmts),vars);
end matchcontinue;
end expandDerOperatorAlg;
protected function expandDerOperatorStmts
"Help function to expandDerOperatorAlg"
input list<Algorithm.Statement> stmts;
input tuple<Variables,DAE.FunctionTree> vars;
output list<Algorithm.Statement> outStmts;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outStmts,outVars) := matchcontinue(stmts,vars)
local Algorithm.Statement s;
case({},vars) then ({},vars);
case(s::stmts,vars) equation
(s,vars) = expandDerOperatorStmt(s,vars);
(stmts,vars) = expandDerOperatorStmts(stmts,vars);
then (s::stmts,vars);
end matchcontinue;
end expandDerOperatorStmts;
protected function expandDerOperatorStmt
"Help function to expandDerOperatorAlg."
input Algorithm.Statement stmt;
input tuple<Variables,DAE.FunctionTree> vars;
output Algorithm.Statement outStmt;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outStmt,outVars) := matchcontinue(stmt,vars)
local DAE.ExpType tp; DAE.ComponentRef cr;
list<DAE.Exp> expl;
Algorithm.Ident id; Boolean b;
list<Algorithm.Statement> stmts;
list<Integer> hv;
Algorithm.Statement stmt;
DAE.Exp e1,e2;
Algorithm.Else elseB;
DAE.ElementSource source;
case(DAE.STMT_ASSIGN(tp,e2,e1,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
((e2,vars)) = Exp.traverseExp(e2,expandDerExp,vars);
then (DAE.STMT_ASSIGN(tp,e2,e1,source),vars);
case(DAE.STMT_TUPLE_ASSIGN(tp,expl,e1,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
(expl,vars) = expandDerExps(expl,vars);
then (DAE.STMT_TUPLE_ASSIGN(tp,expl,e1,source),vars);
case(DAE.STMT_ASSIGN_ARR(tp,cr,e1,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (DAE.STMT_ASSIGN_ARR(tp,cr,e1,source),vars);
case(DAE.STMT_IF(e1,stmts,elseB,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
(stmts,vars) = expandDerOperatorStmts(stmts,vars);
(elseB,vars) = expandDerOperatorElseBranch(elseB,vars);
then (DAE.STMT_IF(e1,stmts,elseB,source),vars);
case(DAE.STMT_FOR(tp,b,id,e1,stmts,source),vars) equation
(stmts,vars) = expandDerOperatorStmts(stmts,vars);
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (DAE.STMT_FOR(tp,b,id,e1,stmts,source),vars);
case(DAE.STMT_WHILE(e1,stmts,source),vars) equation
(stmts,vars) = expandDerOperatorStmts(stmts,vars);
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (DAE.STMT_WHILE(e1,stmts,source),vars);
case(DAE.STMT_WHEN(e1,stmts,SOME(stmt),hv,source),vars) equation
(stmts,vars) = expandDerOperatorStmts(stmts,vars);
(stmt,vars) = expandDerOperatorStmt(stmt,vars);
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (DAE.STMT_WHEN(e1,stmts,SOME(stmt),hv,source),vars);
case(DAE.STMT_WHEN(e1,stmts,NONE,hv,source),vars) equation
(stmts,vars) = expandDerOperatorStmts(stmts,vars);
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (DAE.STMT_WHEN(e1,stmts,NONE,hv,source),vars);
case(DAE.STMT_ASSERT(e1,e2,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
((e2,vars)) = Exp.traverseExp(e2,expandDerExp,vars);
then (DAE.STMT_ASSERT(e1,e2,source),vars);
case(DAE.STMT_TERMINATE(e1,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
then (DAE.STMT_TERMINATE(e1,source),vars);
case(DAE.STMT_REINIT(e1,e2,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
((e1,vars)) = Exp.traverseExp(e2,expandDerExp,vars);
then (DAE.STMT_REINIT(e1,e2,source),vars);
case(stmt,vars) then (stmt,vars);
end matchcontinue;
end expandDerOperatorStmt;
protected function expandDerOperatorElseBranch
"Help function to expandDerOperatorStmt, for else branches in if statements"
input Algorithm.Else elseB;
input tuple<Variables,DAE.FunctionTree> vars;
output Algorithm.Else outElseB;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outElseB,outVars) := matchcontinue(elseB,vars)
local DAE.Exp e1;
list<Algorithm.Statement> stmts;
Algorithm.Else elseB;
case(DAE.NOELSE(),vars) then (DAE.NOELSE(),vars);
case(DAE.ELSEIF(e1,stmts,elseB),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
(stmts,vars) = expandDerOperatorStmts(stmts,vars);
(elseB,vars) = expandDerOperatorElseBranch(elseB,vars);
then (DAE.ELSEIF(e1,stmts,elseB),vars);
end matchcontinue;
end expandDerOperatorElseBranch;
protected function expandDerOperatorArrEqns
"Help function to expandDerOperator"
input list<MultiDimEquation> eqns;
input tuple<Variables,DAE.FunctionTree> vars;
output list<MultiDimEquation> outEqns;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outEqns,outVars) := matchcontinue(eqns,vars)
local MultiDimEquation e;
case({},vars) then ({},vars);
case(e::eqns,vars) equation
(e,vars) = expandDerOperatorArrEqn(e,vars);
(eqns,vars) = expandDerOperatorArrEqns(eqns,vars);
then (e::eqns,vars);
case(_,_) equation
Debug.fprint("failtrace", "-DAELow.expandDerOperatorArrEqns failed\n");
then fail();
end matchcontinue;
end expandDerOperatorArrEqns;
protected function expandDerOperatorArrEqn
"Help function to to expandDerOperator, handles Array equations"
input MultiDimEquation arrEqn;
input tuple<Variables,DAE.FunctionTree> vars;
output MultiDimEquation outArrEqn;
output tuple<Variables,DAE.FunctionTree> outVars;
algorithm
(outArrEqn,outVars) := matchcontinue(arrEqn,vars)
local
list<Integer> dims; DAE.Exp e1,e2;
DAE.ElementSource source "the element source";
case(MULTIDIM_EQUATION(dims,e1,e2,source),vars) equation
((e1,vars)) = Exp.traverseExp(e1,expandDerExp,vars);
((e2,vars)) = Exp.traverseExp(e2,expandDerExp,vars);
then (MULTIDIM_EQUATION(dims,e1,e2,source),vars);