/
BackendEquation.mo
2865 lines (2493 loc) · 106 KB
/
BackendEquation.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 BackendEquation
" file: BackendEquation.mo
package: BackendEquation
description: BackendEquation contains functions that do something with
BackendDAE.Equation data type."
import Absyn;
import BackendDAE;
import DAE;
protected
import Algorithm;
import Array;
import BackendDAEUtil;
import BackendDump;
import BackendVariable;
import BaseHashTable;
import AvlSetInt;
import ClassInf;
import ComponentReference;
import DAEUtil;
import Debug;
import ElementSource;
import Error;
import ExpandableArray;
import Expression;
import ExpressionDump;
import ExpressionSimplify;
import ExpressionSolve;
import Flags;
import HashTable;
import List;
public function emptyEqns "author: lochel
Returns an empty expandable equation array."
output BackendDAE.EquationArray equationArray = emptyEqnsSized(0);
end emptyEqns;
public function emptyEqnsSized "author: lochel
Returns an empty expandable equation array with a given size."
input Integer size;
output BackendDAE.EquationArray outEquationArray = ExpandableArray.new(size, BackendDAE.DUMMY_EQUATION());
end emptyEqnsSized;
public function add "author: lochel
Adds an equation to an EquationArray."
input BackendDAE.Equation inEquation;
input output BackendDAE.EquationArray equationArray;
algorithm
ExpandableArray.add(inEquation, equationArray);
end add;
public function addList "author: hkiel
Adds a list of BackendDAE.Equation to BackendDAE.EquationArray."
input list<BackendDAE.Equation> eqnlst;
input output BackendDAE.EquationArray equationArray;
algorithm
ExpandableArray.expandToSize(ExpandableArray.getLastUsedIndex(equationArray) + listLength(eqnlst), equationArray);
for e in eqnlst loop
equationArray := add(e, equationArray);
end for;
end addList;
public function delete
input Integer inPos "one-based indexing";
input output BackendDAE.EquationArray equationArray;
algorithm
ExpandableArray.delete(inPos, equationArray);
end delete;
public function deleteList
"Deletes all equations from the given list of indices."
input output BackendDAE.EquationArray equationArray;
input list<Integer> inIndices "one-based indexing";
algorithm
for index in inIndices loop
ExpandableArray.delete(index, equationArray);
end for;
end deleteList;
public function merge "author: lochel
This function returns an EquationArray containing all the equations from both
inputs."
input BackendDAE.EquationArray inEqns1;
input BackendDAE.EquationArray inEqns2;
output BackendDAE.EquationArray outEqns;
algorithm
outEqns := copyEquationArray(inEqns2);
outEqns := addList(equationList(inEqns1), outEqns);
end merge;
public function listEquation "author: PA
Transform a list of equations into an expandable BackendDAE.Equation array."
input list<BackendDAE.Equation> inEquationList;
output BackendDAE.EquationArray outEquationArray;
algorithm
outEquationArray := ExpandableArray.new(listLength(inEquationList), BackendDAE.DUMMY_EQUATION());
for eq in inEquationList loop
ExpandableArray.add(eq, outEquationArray);
end for;
end listEquation;
public function equationList "author: lochel
Transform the expandable BackendDAE.Equation array to a list of Equations."
input BackendDAE.EquationArray equationArray;
output list<BackendDAE.Equation> outEquationLst = ExpandableArray.toList(equationArray);
end equationList;
public function copyEquationArray
"Performs a deep copy of an expandable equation array."
input BackendDAE.EquationArray inEquationArray;
output BackendDAE.EquationArray outEquationArray = ExpandableArray.copy(inEquationArray, BackendDAE.DUMMY_EQUATION());
end copyEquationArray;
public function setAtIndex "author: lochel
Sets the n-th array element of an EquationArray.
Please note: one-based indexing"
input output BackendDAE.EquationArray equationArray;
input Integer inPos "one-based indexing";
input BackendDAE.Equation inEquation;
algorithm
if ExpandableArray.occupied(inPos, equationArray) then
ExpandableArray.update(inPos, inEquation, equationArray);
else
ExpandableArray.set(inPos, inEquation, equationArray);
end if;
end setAtIndex;
public function setAtIndexFirst "author: waurich
Sets the n-th array element of an EquationArray but with index at first argument.
Please note: one-based indexing"
input Integer inPos "one-based indexing";
input BackendDAE.Equation inEquation;
input BackendDAE.EquationArray inEquationArray;
output BackendDAE.EquationArray outEquationArray = setAtIndex(inEquationArray, inPos, inEquation);
end setAtIndexFirst;
public function get
"Return the n-th equation from the expandable equation array
indexed from 1..N."
input BackendDAE.EquationArray inEquationArray;
input Integer inPos "one-based indexing";
output BackendDAE.Equation outEquation = ExpandableArray.get(inPos, inEquationArray);
end get;
public function getList "author: Frenkel TUD 2011-05
returns the equations given by the list of indexes"
input list<Integer> inIndices "one-based indexing";
input BackendDAE.EquationArray inEquationArray;
output list<BackendDAE.Equation> outEqns;
algorithm
outEqns := list(get(inEquationArray, index) for index in inIndices);
end getList;
public function equationArraySize "author: lochel
Returns the size of the equations in an BackendDAE.EquationArray, which
should correspond to the number of variables."
input BackendDAE.EquationArray equationArray;
output Integer outSize;
algorithm
outSize := 0;
for i in 1:ExpandableArray.getLastUsedIndex(equationArray) loop
if ExpandableArray.occupied(i, equationArray) then
outSize := outSize + equationSize(ExpandableArray.get(i, equationArray));
end if;
end for;
end equationArraySize;
public function getNumberOfEquations "author: lochel
Returns the number of equations in an BackendDAE.EquationArray, which not
corresponds to the number of equations in the system."
input BackendDAE.EquationArray inEquationArray;
output Integer outSize = ExpandableArray.getNumberOfElements(inEquationArray);
end getNumberOfEquations;
public function traverseEquationArray<T> "author: lochel
Traverses all equations of a BackendDAE.EquationArray."
input BackendDAE.EquationArray equationArray;
input Func inFunc;
input output T extraArg;
partial function Func
input output BackendDAE.Equation eq;
input output T extraArg;
end Func;
protected
BackendDAE.Equation eqn;
algorithm
for i in 1:ExpandableArray.getLastUsedIndex(equationArray) loop
if ExpandableArray.occupied(i, equationArray) then
eqn := ExpandableArray.get(i, equationArray);
(_, extraArg) := inFunc(eqn, extraArg);
end if;
end for;
end traverseEquationArray;
public function traverseEquationArray_WithStop<T> "author: lochel
Traverses all equations of a BackendDAE.EquationArray."
input BackendDAE.EquationArray equationArray;
input FuncWithStop inFuncWithStop;
input output T extraArg;
partial function FuncWithStop
input output BackendDAE.Equation eq;
input T inExtraArg;
output Boolean cont;
output T outExtraArg;
end FuncWithStop;
protected
Boolean continue_ = true;
BackendDAE.Equation eqn;
algorithm
for i in 1:ExpandableArray.getLastUsedIndex(equationArray) loop
if ExpandableArray.occupied(i, equationArray) then
eqn := ExpandableArray.get(i, equationArray);
(_, continue_, extraArg) := inFuncWithStop(eqn, extraArg);
if not continue_ then
break;
end if;
end if;
end for;
end traverseEquationArray_WithStop;
public function traverseEquationArray_WithUpdate<T>
"Traverses all equations of a BackendDAE.EquationArray."
input output BackendDAE.EquationArray equationArray;
input FuncWithUpdate inFuncWithUpdate;
input output T extraArg;
partial function FuncWithUpdate
input output BackendDAE.Equation inoutEq;
input output T inoutA;
end FuncWithUpdate;
protected
BackendDAE.Equation e, new_e;
algorithm
for i in 1:ExpandableArray.getLastUsedIndex(equationArray) loop
if ExpandableArray.occupied(i, equationArray) then
e := ExpandableArray.get(i, equationArray);
(new_e, extraArg) := inFuncWithUpdate(e, extraArg);
if not referenceEq(e, new_e) then
ExpandableArray.update(i, new_e, equationArray);
end if;
end if;
end for;
end traverseEquationArray_WithUpdate;
public function getWhenEquationExpr "Get the left and right hand parts from an equation appearing in a when clause"
input BackendDAE.WhenEquation inWhenEquation;
output DAE.ComponentRef outComponentRef;
output DAE.Exp outExp;
algorithm
try
BackendDAE.WHEN_STMTS(whenStmtLst={BackendDAE.ASSIGN(left=DAE.CREF(componentRef = outComponentRef), right=outExp)}) := inWhenEquation;
else
Error.addInternalError("BackendEquation.getWhenEquationExpr failed\n", sourceInfo());
end try;
end getWhenEquationExpr;
public function setWhenElsePart
"Set the else part of an whenEquation"
input BackendDAE.WhenEquation inWhenEquation;
input BackendDAE.WhenEquation inElseWhenEquation;
output BackendDAE.WhenEquation outWhenEquation;
algorithm
outWhenEquation := match(inWhenEquation)
local
DAE.Exp cond;
BackendDAE.WhenEquation elsewhenPart;
list<BackendDAE.WhenOperator> whenStmtLst;
case (BackendDAE.WHEN_STMTS(condition=cond, whenStmtLst = whenStmtLst, elsewhenPart=NONE()))
then BackendDAE.WHEN_STMTS(cond, whenStmtLst, SOME(inElseWhenEquation));
case (BackendDAE.WHEN_STMTS(condition=cond, whenStmtLst = whenStmtLst, elsewhenPart=SOME(elsewhenPart)))
then BackendDAE.WHEN_STMTS(condition=cond, whenStmtLst = whenStmtLst, elsewhenPart=SOME(setWhenElsePart(elsewhenPart, inElseWhenEquation)));
else fail();
end match;
end setWhenElsePart;
public function equationsLstVars "author: Frenkel TUD 2011-05
From the equations and a variable array return all
occurring variables from the array."
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outVars;
protected
AvlSetInt.Tree indexes;
list<Integer> keys;
algorithm
if listEmpty(inEquationLst) then
outVars := {};
return;
end if;
(_, indexes) := traverseExpsOfEquationList(inEquationLst, function checkEquationsVarsExpTopDownTraverseHelper(vars=inVars), AvlSetInt.new());
keys := AvlSetInt.listKeys(indexes);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsLstVars;
public function equationsVars "author: Frenkel TUD 2011-05
From the equations and a variable array return all
occurring variables from the array."
input BackendDAE.EquationArray inEquations;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outVars;
protected
AvlSetInt.Tree indexes;
list<Integer> keys;
algorithm
indexes := BackendDAEUtil.traverseBackendDAEExpsEqns(inEquations, function checkEquationsVarsExpTopDownTraverseHelper(vars=inVars), AvlSetInt.new());
keys := AvlSetInt.listKeys(indexes);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsVars;
public function equationVars "author: Frenkel TUD 2012-03
From the equation and a variable array return all
variables in the equation."
input BackendDAE.Equation inEquation;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outVars;
protected
AvlSetInt.Tree indexes;
list<Integer> keys;
algorithm
(_, indexes) := traverseExpsOfEquation(inEquation, function checkEquationsVarsExpTopDownTraverseHelper(vars=inVars), AvlSetInt.new());
keys := AvlSetInt.listKeys(indexes);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationVars;
public function expressionVars "author: Frenkel TUD 2012-03
From the expression and a variable array return all
variables in the expression."
input DAE.Exp inExp;
input BackendDAE.Variables vars;
output list<BackendDAE.Var> outVars;
protected
AvlSetInt.Tree indexes;
list<Integer> keys;
algorithm
(_, indexes) := Expression.traverseExpTopDown(inExp, function checkEquationsVarsExpTopDown(vars=vars), AvlSetInt.new());
keys := AvlSetInt.listKeys(indexes);
outVars := List.map1r(keys, BackendVariable.getVarAt, vars);
end expressionVars;
public function expressionVarsIndexes
"From the expression and a variable array return all variables in the expression."
input DAE.Exp exp;
input output AvlSetInt.Tree indexes;
input CheckEquationsVarsExpTopDownFunc func;
partial function CheckEquationsVarsExpTopDownFunc
input output DAE.Exp exp;
output Boolean cont;
input output AvlSetInt.Tree tree;
end CheckEquationsVarsExpTopDownFunc;
algorithm
(_, indexes) := Expression.traverseExpTopDown(exp, func, indexes);
end expressionVarsIndexes;
public function checkEquationsVarsExpTopDownTraverseHelper
"This function traverses expssions to-down to collect all
variables that are involed into the expression."
input output DAE.Exp exp;
input output AvlSetInt.Tree tree;
input BackendDAE.Variables vars;
algorithm
(exp, tree) := Expression.traverseExpTopDown(exp,function checkEquationsVarsExpTopDown(vars=vars),tree);
end checkEquationsVarsExpTopDownTraverseHelper;
public function checkEquationsVarsExpTopDown
input output DAE.Exp exp;
output Boolean cont;
input output AvlSetInt.Tree tree;
input BackendDAE.Variables vars;
algorithm
(cont,tree) := matchcontinue exp
local
DAE.ComponentRef cr;
list<Integer> ilst;
// special case for time, it is never part of the equation system
case DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time"))
then (true, tree);
// case for function pointers
case DAE.CREF(ty=DAE.T_FUNCTION_REFERENCE_FUNC())
then (true, tree);
// case for pre vars
case DAE.CALL(path = Absyn.IDENT(name = "pre"))
then (false, tree);
// case for previous vars
case DAE.CALL(path = Absyn.IDENT(name = "previous"))
then (false, tree);
// add it
case DAE.CREF(componentRef = cr)
algorithm
(_, ilst) := BackendVariable.getVar(cr, vars);
tree := AvlSetInt.addList(tree, ilst);
then (true, tree);
else (true, tree);
end matchcontinue;
end checkEquationsVarsExpTopDown;
public function assertWithCondTrue "author: Frenkel TUD 2012-12"
input BackendDAE.Equation inEqn;
output Boolean b;
algorithm
b := match inEqn
case BackendDAE.ALGORITHM(alg=DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond=DAE.BCONST(true))})) then false;
else true;
end match;
end assertWithCondTrue;
public function equationsParams "author: marcusw
From a list of equations return all occurring parameter variables. Duplicates are removed."
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outParamVars;
output list<Integer> outParamVarsIdc;
algorithm
(_, (_,(outParamVars,outParamVarsIdc,_))) := traverseExpsOfEquationList(inEquationLst, Expression.traverseSubexpressionsHelper, (traversingParamRefFinder, ({}, {}, inVars)));
end equationsParams;
protected function traversingParamRefFinder "author: marcusw
Checks if the given expression is a parameter variable and adds it to the result list on true. Duplicates are removed."
input DAE.Exp inExp;
input tuple<list<BackendDAE.Var>, list<Integer>, BackendDAE.Variables> inTpl; //<result list, result idc, all variables>
output DAE.Exp outExp;
output tuple<list<BackendDAE.Var>, list<Integer>, BackendDAE.Variables> outTpl;
algorithm
outExp := inExp;
outTpl := match(inExp,inTpl)
local
BackendDAE.Variables allVars;
list<BackendDAE.Var> vars, foundVars;
list<Integer> varIdc, foundVarsIdc;
DAE.ComponentRef cr;
case (DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time")), _)
then inTpl;
case ( DAE.CREF(componentRef=cr), (vars, varIdc, allVars)) equation
//print("traversingParamRefFinder: try to get Variable " + ComponentReference.crefStr(cr) + "\n");
(foundVars, foundVarsIdc) = BackendVariable.getVar(cr, allVars);
(vars, varIdc) = traversingParamRefFinder0(foundVars, foundVarsIdc, vars, varIdc);
then (vars, varIdc, allVars);
else inTpl;
end match;
end traversingParamRefFinder;
protected function traversingParamRefFinder0
input list<BackendDAE.Var> iVars;
input list<Integer> iVarIdc;
input list<BackendDAE.Var> iParamVarsList;
input list<Integer> iParamVarsIdc;
output list<BackendDAE.Var> oParamVarsList;
output list<Integer> oParamVarIdc;
protected
BackendDAE.Var var;
list<BackendDAE.Var> vars = iParamVarsList;
list<Integer> varIdc = iParamVarsIdc;
Integer varIdx;
list<Integer> rest = iVarIdc;
algorithm
for var in iVars loop
varIdx::rest := rest;
if(BackendVariable.isParam(var)) then
vars := List.unionEltOnTrue(var, vars, BackendVariable.varEqual);
varIdc := List.unionEltOnTrue(varIdx, varIdc, intEq);
end if;
end for;
oParamVarsList := vars;
oParamVarIdc := varIdc;
end traversingParamRefFinder0;
public function iterationVarsinRelations
"From a list of equations return all variables, which used inside
a relation, then this equation system is marked as mixed and also
the indexes of the relations may be used in future."
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.Variables inVars;
output Boolean mixedSystem;
output list<Integer> indexes;
algorithm
(_, (_,(indexes, _))) := traverseExpsOfEquationList(inEquationLst, Expression.traverseSubexpressionsHelper, (traversingRelationsforIterationVars, ({}, inVars)));
mixedSystem := not listEmpty(indexes);
end iterationVarsinRelations;
protected function traversingRelationsforIterationVars
input DAE.Exp inExp;
input tuple<list<Integer>, BackendDAE.Variables> inTpl;
output DAE.Exp outExp;
output tuple<list<Integer>, BackendDAE.Variables> outTpl;
algorithm
outExp := inExp;
outTpl := match (inExp,inTpl)
local
BackendDAE.Variables vars;
list<Integer> indexes;
DAE.Exp e1, e2;
Integer index;
list<BackendDAE.Var> vlst1,vlst2;
case (DAE.RELATION(exp1=e1, exp2=e2, index=index), (indexes, vars))
equation
vlst1 = expressionVars(e1, vars);
vlst2 = expressionVars(e2, vars);
//if lists are not empty continue
if not(listEmpty(vlst1) and listEmpty(vlst2)) then
outTpl = (index::indexes, vars);
else
outTpl = inTpl;
end if;
then (outTpl);
else (inTpl);
end match;
end traversingRelationsforIterationVars;
public function equationsCrefs "author: PA
From a list of equations return all occurring variables/component references."
input list<BackendDAE.Equation> inEquationLst;
output list<DAE.ComponentRef> outExpComponentRefLst;
algorithm
(_, (_,outExpComponentRefLst)) := traverseExpsOfEquationList(inEquationLst, Expression.traverseSubexpressionsHelper, (Expression.traversingComponentRefFinder, {}));
end equationsCrefs;
public function equationCrefs "author: PA
From one equation return all occurring variables/component references."
input BackendDAE.Equation inEquation;
output list<DAE.ComponentRef> outExpComponentRefLst;
algorithm
(_, (_, outExpComponentRefLst)) := traverseExpsOfEquation(inEquation, Expression.traverseSubexpressionsHelper, (Expression.traversingComponentRefFinder, {}));
end equationCrefs;
public function getAllCrefFromEquations
input BackendDAE.EquationArray inEqns;
output list<DAE.ComponentRef> cr_lst;
algorithm
cr_lst := traverseEquationArray(inEqns, traversingEquationCrefFinder, {});
end getAllCrefFromEquations;
protected function traversingEquationCrefFinder "author: Frenkel TUD 2010-11"
input BackendDAE.Equation inEq;
input list<DAE.ComponentRef> inCrefs;
output BackendDAE.Equation e;
output list<DAE.ComponentRef> cr_lst;
algorithm
e := inEq;
cr_lst := inCrefs;
(_, (_,cr_lst)) := traverseExpsOfEquation(e, Expression.traverseSubexpressionsHelper, (Expression.traversingComponentRefFinder, cr_lst));
end traversingEquationCrefFinder;
public function equationUnknownCrefs "author: Frenkel TUD 2012-05
From the equation and a variable array return all
variables in the equation an not in the variable array."
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.Variables inVars;
input BackendDAE.Variables inKnVars;
output list<DAE.ComponentRef> cr_lst;
protected
HashTable.HashTable ht;
algorithm
ht := HashTable.emptyHashTable();
(_, (_, (_, _, ht))) := traverseExpsOfEquationList(inEquationLst, Expression.traverseSubexpressionsHelper, (checkEquationsUnknownCrefsExp, (inVars, inKnVars, ht)));
cr_lst := BaseHashTable.hashTableKeyList(ht);
end equationUnknownCrefs;
protected function checkEquationsUnknownCrefsExp
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, BackendDAE.Variables, HashTable.HashTable> inTuple;
output DAE.Exp outExp;
output tuple<BackendDAE.Variables, BackendDAE.Variables, HashTable.HashTable> outTuple;
algorithm
(outExp,outTuple) := matchcontinue (inExp,inTuple)
local
DAE.Exp e, e1;
BackendDAE.Variables vars, knvars;
HashTable.HashTable ht;
DAE.ComponentRef cr;
list<DAE.Exp> expl;
list<DAE.Var> varLst;
// special case for time, it is never part of the equation system
case (DAE.CREF(componentRef = DAE.CREF_IDENT(ident="time")), _)
then (inExp, inTuple);
// special case for records
case (e as DAE.CREF(componentRef = cr, ty= DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(_))), _)
equation
expl = List.map1(varLst, Expression.generateCrefsExpFromExpVar, cr);
(_, outTuple) = Expression.traverseExpList(expl, checkEquationsUnknownCrefsExp, inTuple);
then (e, outTuple);
// special case for arrays
case (e as DAE.CREF(ty = DAE.T_ARRAY()), _)
equation
(e1, true) = Expression.extendArrExp(e, false);
(_, outTuple) = Expression.traverseExpBottomUp(e1, checkEquationsUnknownCrefsExp, inTuple);
then (e, outTuple);
// case for function pointers
case (DAE.CREF(ty=DAE.T_FUNCTION_REFERENCE_FUNC()), _)
then (inExp, inTuple);
// already there
case (DAE.CREF(componentRef = cr), (_, _, ht))
equation
_ = BaseHashTable.get(cr, ht);
then (inExp, inTuple);
// known
case (DAE.CREF(componentRef = cr), (vars, _, _))
equation
(_, _) = BackendVariable.getVar(cr, vars);
then (inExp, inTuple);
case (DAE.CREF(componentRef = cr), (_, knvars, _))
equation
(_, _) = BackendVariable.getVar(cr, knvars);
then (inExp, inTuple);
// add it
case (DAE.CREF(componentRef = cr), (vars, knvars, ht))
equation
ht = BaseHashTable.add((cr, 0), ht);
then (inExp, (vars, knvars, ht));
else (inExp,inTuple);
end matchcontinue;
end checkEquationsUnknownCrefsExp;
public function traverseExpsOfEquationList<ArgT> "author: Frenkel TUD 2010-11
Traverse all expressions of a list of Equations. It is possible to change the
equations and the multidim equations and the algorithms."
input list<BackendDAE.Equation> inEquations;
input FuncExpType func;
input ArgT inArg;
output list<BackendDAE.Equation> outEquations = {};
output ArgT outArg = inArg;
partial function FuncExpType
input output DAE.Exp inoutExp;
input output ArgT inoutArg;
end FuncExpType;
algorithm
for eq in inEquations loop
(eq, outArg) := traverseExpsOfEquation(eq, func, outArg);
outEquations := eq::outEquations;
end for;
outEquations := MetaModelica.Dangerous.listReverseInPlace(outEquations);
end traverseExpsOfEquationList;
public function traverseExpsOfEquationList_WithStop "author: Frenkel TUD 2012-09
Traverses all expressions of a list of equations.
It is possible to change the equations."
replaceable type Type_a subtypeof Any;
input list<BackendDAE.Equation> inEquations;
input FuncExpType inFunc;
input Type_a inTypeA;
output Boolean outBoolean = true;
output Type_a outTypeA = inTypeA;
partial function FuncExpType
input DAE.Exp inExp;
input Type_a inTypeA;
output DAE.Exp outExp;
output Boolean cont;
output Type_a outA;
end FuncExpType;
algorithm
for eqn in inEquations loop
(outBoolean, outTypeA) := traverseExpsOfEquation_WithStop(eqn, inFunc, outTypeA);
if not outBoolean then
break;
end if;
end for;
end traverseExpsOfEquationList_WithStop;
public function traverseExpsOfEquationList_WithoutChange<ArgT> "
Traverse all expressions of a list of Equations"
input BackendDAE.Equation inEquation;
input FuncExpType func;
input ArgT inArg;
output ArgT outArg = inArg;
partial function FuncExpType
input output DAE.Exp inoutExp;
input output ArgT inoutArg;
end FuncExpType;
algorithm
(_, outArg) := traverseExpsOfEquation(inEquation, func, outArg);
end traverseExpsOfEquationList_WithoutChange;
protected function traverseExpsOfEquationListList_WithStop<T> "author: Frenkel TUD 2012-09
Traverses all expressions of a list of equations.
It is possible to change the equations."
input list<list<BackendDAE.Equation>> inEquations;
input FuncExpType func;
input T inTypeA;
output Boolean outBoolean = true;
output T outTypeA = inTypeA;
partial function FuncExpType
input DAE.Exp inExp;
input T inTypeA;
output DAE.Exp outExp;
output Boolean cont;
output T outA;
end FuncExpType;
algorithm
for eqn in inEquations loop
(outBoolean, outTypeA) := traverseExpsOfEquationList_WithStop(eqn, func, outTypeA);
if not outBoolean then
break;
end if;
end for;
end traverseExpsOfEquationListList_WithStop;
public function traverseExpsOfEquation<T> "author: Frenkel TUD 2010-11
Traverses all expressions of an equation.
It is possible to change the equation."
input BackendDAE.Equation inEquation;
input FuncExpType inFunc;
input T inTypeA;
output BackendDAE.Equation outEquation;
output T outTypeA;
partial function FuncExpType
input output DAE.Exp inoutExp;
input output T inoutTypeA;
end FuncExpType;
algorithm
(outEquation, outTypeA) := match(inEquation)
local
DAE.Exp e1, e2, e_1, e_2, start, stop, iter;
list<DAE.Exp> expl;
DAE.Type tp;
DAE.ComponentRef cr, cr1;
BackendDAE.WhenEquation we, we_1;
DAE.ElementSource source;
Integer size;
T extArg;
list<Integer> dimSize;
DAE.Algorithm alg;
list<DAE.Statement> stmts, stmts1;
list<BackendDAE.Equation> eqns;
list<list<BackendDAE.Equation>> eqnslst;
DAE.Expand crefExpand;
BackendDAE.EquationAttributes attr;
case BackendDAE.EQUATION(exp=e1, scalar=e2, source=source, attr=attr) equation
(e_1, extArg) = inFunc(e1, inTypeA);
(e_2, extArg) = inFunc(e2, extArg);
then (if referenceEq(e1,e_1) and referenceEq(e2,e_2) then inEquation else BackendDAE.EQUATION(e_1, e_2, source, attr), extArg);
case BackendDAE.ARRAY_EQUATION(dimSize=dimSize, left=e1, right=e2, source=source, attr=attr) equation
(e_1, extArg) = inFunc(e1, inTypeA);
(e_2, extArg) = inFunc(e2, extArg);
then (if referenceEq(e1,e_1) and referenceEq(e2,e_2) then inEquation else BackendDAE.ARRAY_EQUATION(dimSize, e_1, e_2, source, attr), extArg);
case BackendDAE.SOLVED_EQUATION(componentRef=cr, exp=e2, source=source, attr=attr) equation
tp = Expression.typeof(e2);
e1 = Expression.makeCrefExp(cr, tp);
(DAE.CREF(cr1, _), extArg) = inFunc(e1, inTypeA);
(e_2, extArg) = inFunc(e2, extArg);
then (if referenceEq(cr,cr1) and referenceEq(e2,e_2) then inEquation else BackendDAE.SOLVED_EQUATION(cr1, e_2, source, attr), extArg);
case BackendDAE.RESIDUAL_EQUATION(exp=e1, source=source, attr=attr) equation
(e_1, extArg) = inFunc(e1, inTypeA);
then (if referenceEq(e1,e_1) then inEquation else BackendDAE.RESIDUAL_EQUATION(e_1, source, attr), extArg);
case BackendDAE.WHEN_EQUATION(size=size, whenEquation= we, source=source, attr=attr) equation
(we_1, extArg) = traverseExpsOfWhenEquation(we, inFunc, inTypeA);
then (if referenceEq(we,we_1) then inEquation else BackendDAE.WHEN_EQUATION(size, we_1, source, attr), extArg);
case BackendDAE.ALGORITHM(size=size, alg=DAE.ALGORITHM_STMTS(statementLst = stmts), source=source, expand=crefExpand, attr=attr) equation
(stmts1, extArg) = DAEUtil.traverseDAEEquationsStmts(stmts, inFunc, inTypeA);
then (if referenceEq(stmts,stmts1) then inEquation else BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(stmts1), source, crefExpand, attr), extArg);
case BackendDAE.COMPLEX_EQUATION(size=size, left=e1, right=e2, source=source, attr=attr) equation
(e_1, extArg) = inFunc(e1, inTypeA);
(e_2, extArg) = inFunc(e2, extArg);
then (if referenceEq(e1,e_1) and referenceEq(e2,e_2) then inEquation else BackendDAE.COMPLEX_EQUATION(size, e_1, e_2, source, attr), extArg);
case BackendDAE.IF_EQUATION(conditions=expl, eqnstrue=eqnslst, eqnsfalse=eqns, source=source, attr=attr) equation
(expl, extArg) = traverseExpsOfExpList(expl, inFunc, inTypeA);
(eqnslst, extArg) = List.map1Fold(eqnslst, traverseExpsOfEquationList, inFunc, extArg);
(eqns, extArg) = List.map1Fold(eqns, traverseExpsOfEquation, inFunc, extArg);
then (BackendDAE.IF_EQUATION(expl, eqnslst, eqns, source, attr), extArg);
case BackendDAE.FOR_EQUATION(iter=iter,start=start,stop=stop,left=e1, right=e2, source=source, attr=attr) equation
(e_1, extArg) = inFunc(e1, inTypeA);
(e_2, extArg) = inFunc(e2, extArg);
then (if referenceEq(e1,e_1) and referenceEq(e2,e_2) then inEquation else BackendDAE.FOR_EQUATION(iter,start,stop,e_1,e_2,source,attr), extArg);
end match;
end traverseExpsOfEquation;
public function traverseExpsOfEquation_WithStop<T> "author: Frenkel TUD 2010-11
Traverses all expressions of a equation.
It is not possible to change the equation."
input BackendDAE.Equation inEquation;
input FuncExpType func;
input T inTypeA;
output Boolean outBoolean;
output T outTypeA;
partial function FuncExpType
input DAE.Exp inExp;
input T inTypeA;
output DAE.Exp outExp;
output Boolean cont;
output T outA;
end FuncExpType;
algorithm
(outBoolean, outTypeA) := match (inEquation, func, inTypeA)
local
DAE.Exp e1, e2, cond;
list<DAE.Exp> expl;
DAE.Type tp;
DAE.ComponentRef cr;
BackendDAE.WhenEquation we;
DAE.ElementSource source;
Integer size;
T ext_arg, ext_arg_1, ext_arg_2, ext_arg_3;
list<Integer> dimSize;
DAE.Algorithm alg;
list<DAE.Statement> stmts;
list<BackendDAE.Equation> eqns;
list<list<BackendDAE.Equation>> eqnslst;
Boolean b, b1, b2, b3, b4;
BackendDAE.EquationAttributes eqAttr;
case (BackendDAE.EQUATION(exp=e1, scalar=e2), _, _)
equation
(_, b, ext_arg) = func(e1, inTypeA);
if b then
(_, b, ext_arg) = func(e2, ext_arg);
end if;
then (b, ext_arg);
case (BackendDAE.ARRAY_EQUATION(left=e1, right=e2), _, _)
equation
(_, b, ext_arg) = func(e1, inTypeA);
if b then
(_, b, ext_arg) = func(e2, ext_arg);
end if;
then (b, ext_arg);
case (BackendDAE.SOLVED_EQUATION(componentRef=cr, exp=e2), _, _)
equation
tp = Expression.typeof(e2);
e1 = Expression.makeCrefExp(cr, tp);
(_, b, ext_arg) = func(e1, inTypeA);
if b then
(_, b, ext_arg) = func(e2, ext_arg);
end if;
then (b, ext_arg);
case (BackendDAE.RESIDUAL_EQUATION(exp=e1), _, _)
equation
(_, b, ext_arg) = func(e1, inTypeA);
then (b, ext_arg);
case (BackendDAE.WHEN_EQUATION(whenEquation= we), _, _)
equation
(b, ext_arg) = traverseExpsOfWhenEquation_WithStop(we, func, inTypeA);
then (b, ext_arg);
case (BackendDAE.ALGORITHM(alg=DAE.ALGORITHM_STMTS()), _, _)
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("not implemented error - BackendDAE.ALGORITHM - BackendEquation.traverseExpsOfEquation_WithStop\n");
//(_, ext_arg_1) = DAEUtil.traverseDAEEquationsStmts(statementLst, func, inTypeA);
then fail();
//(true, inTypeA);
case (BackendDAE.COMPLEX_EQUATION(left=e1, right=e2), _, _)
equation
(_, b, ext_arg) = func(e1, inTypeA);
if b then
(_, b, ext_arg) = func(e2, ext_arg);
end if;
then (b, ext_arg);
case (BackendDAE.IF_EQUATION(conditions=expl, eqnstrue=eqnslst, eqnsfalse=eqns), _, _)
equation
(b, ext_arg) = traverseExpsOfExpList_WithStop(expl, func, inTypeA);
if b then
(b, ext_arg) = traverseExpsOfEquationListList_WithStop(eqnslst, func, ext_arg);
end if;
if b then
(b, ext_arg) = traverseExpsOfEquationList_WithStop(eqns, func, ext_arg);
end if;
then (b, ext_arg);
end match;
end traverseExpsOfEquation_WithStop;
protected function traverseExpsOfWhenEquation<T>
"Traverses all expressions of a when equation.
Helper function of traverseExpsOfEquation."
input BackendDAE.WhenEquation inWhenEquation;
input FuncExpType inFunc;
input T inTypeA;
output BackendDAE.WhenEquation outWhenEquation;
output T outTypeA;
partial function FuncExpType
input output DAE.Exp inoutExp;
input output T inoutTypeA;
end FuncExpType;
algorithm
(outWhenEquation, outTypeA) := match(inWhenEquation)
local
BackendDAE.WhenEquation we, elsewe;
Option<BackendDAE.WhenEquation> oelsewe;
DAE.Exp cond;
list<BackendDAE.WhenOperator> whenStmtLst;
T extArg;
case BackendDAE.WHEN_STMTS(condition=cond, whenStmtLst=whenStmtLst, elsewhenPart = oelsewe)
equation
(cond, extArg) = inFunc(cond, inTypeA);
(whenStmtLst, extArg) = traverseExpsOfWhenOps(whenStmtLst, inFunc, extArg, {});
if isSome(oelsewe) then
SOME(elsewe) = oelsewe;
(elsewe, extArg) = traverseExpsOfWhenEquation(elsewe, inFunc, extArg);
oelsewe = SOME(elsewe);
else
oelsewe = NONE();
end if;
then (BackendDAE.WHEN_STMTS(cond, whenStmtLst, oelsewe), extArg);
end match;
end traverseExpsOfWhenEquation;
protected function traverseExpsOfWhenOps<T>
"Traverses all expressions of a when equation.
Helper function of traverseExpsOfEquation."
input list<BackendDAE.WhenOperator> inWhenOps;
input FuncExpType inFunc;
input T inTypeA;
input list<BackendDAE.WhenOperator> inAccum;
output list<BackendDAE.WhenOperator> outWhenOps;
output T outTypeA;
partial function FuncExpType
input output DAE.Exp inExp;
input output T inTypeA;
end FuncExpType;
algorithm
(outWhenOps, outTypeA) := match (inWhenOps)
local
DAE.Type tp;
DAE.Exp e1, e2, level;
DAE.ComponentRef cr, cr1;
list<BackendDAE.WhenOperator> rest;
T extArg;
DAE.ElementSource source;