-
Notifications
You must be signed in to change notification settings - Fork 298
/
BackendEquation.mo
3201 lines (2796 loc) · 117 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
BackendDAEEquation data type.
"
public import Absyn;
public import BackendDAE;
public import DAE;
protected import Algorithm;
protected import Array;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendVariable;
protected import BaseHashTable;
protected import BinaryTreeInt;
protected import ClassInf;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import ElementSource;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import ExpressionSolve;
protected import Flags;
protected import HashTable;
protected import List;
protected import Util;
public function listEquation "author: PA
Transform the a list of equations into an expandable BackendDAE.Equation
array."
input list<BackendDAE.Equation> inEquationList;
output BackendDAE.EquationArray outEquationArray;
protected
Integer len, size, arrsize;
Real rlen;
array<Option<BackendDAE.Equation>> optarr;
algorithm
len := listLength(inEquationList);
rlen := intReal(len) * 1.4;
arrsize := realInt(rlen);
optarr := arrayCreate(arrsize, NONE());
(size, optarr) := listEquation1(inEquationList, optarr);
outEquationArray := BackendDAE.EQUATION_ARRAY(size, len, arrsize, optarr);
end listEquation;
protected function listEquation1
input list<BackendDAE.Equation> inEquationList;
input array<Option<BackendDAE.Equation>> inEquOptArr;
output Integer outSize = 0;
output array<Option<BackendDAE.Equation>> outEquOptArr = inEquOptArr;
protected
Integer pos = 1;
algorithm
for eq in inEquationList loop
outSize := outSize + equationSize(eq);
outEquOptArr := arrayUpdate(outEquOptArr, pos, SOME(eq));
pos := pos + 1;
end for;
end listEquation1;
public function emptyEqns "
Returns an empty expandable equation array."
output BackendDAE.EquationArray eqns;
algorithm
eqns := listEquation({});
end emptyEqns;
public function emptyEqnsSized "
Returns an empty expandable equation array with a given size."
input Integer size;
output BackendDAE.EquationArray outEquationArray;
protected
array<Option<BackendDAE.Equation>> equOptArr;
algorithm
equOptArr := arrayCreate(size, NONE());
outEquationArray := BackendDAE.EQUATION_ARRAY(0, 0, size, equOptArr);
end emptyEqnsSized;
public function equationList "author: PA
Transform the expandable BackendDAE.Equation array to a list of Equations."
input BackendDAE.EquationArray inEquationArray;
output list<BackendDAE.Equation> outEquationLst;
protected
array<Option<BackendDAE.Equation>> arr;
Integer n;
algorithm
BackendDAE.EQUATION_ARRAY(numberOfElement=n, equOptArr=arr) := inEquationArray;
outEquationLst := if n == 0 then {}
elseif n == 1 and isSome((arr[1])) then {Util.getOption(arr[1])}
else equationList2(arr, n);
end equationList;
protected function equationList2 "author: PA
Helper function toequationList
inputs: (Equation option array, int /* pos */, int /* lastpos */)
outputs: BackendDAE.Equation list"
input array<Option<BackendDAE.Equation>> arr;
input Integer pos;
output list<BackendDAE.Equation> outEquationLst;
algorithm
outEquationLst := list(Util.getOption(arr[i]) for i guard isSome(arr[i]) in 1:pos);
end equationList2;
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
BackendDAE.WHEN_STMTS(whenStmtLst={BackendDAE.ASSIGN(left=outComponentRef, right=outExp)}) := inWhenEquation;
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 copyEquationArray "Performs a deep copy of an expandable equation array."
input BackendDAE.EquationArray inEquationArray;
output BackendDAE.EquationArray outEquationArray = inEquationArray;
algorithm
outEquationArray.equOptArr := arrayCopy(inEquationArray.equOptArr);
end copyEquationArray;
public function equationsLstVars
"author: Frenkel TUD 2011-05
From the equations and a variable array return all
occurring variables form the array."
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outVars;
protected
BinaryTreeInt.BinTree bt;
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, (_, bt)) := traverseExpsOfEquationList(inEquationLst, checkEquationsVarsExpTopDownTraverseHelper, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
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 form the array."
input BackendDAE.EquationArray inEquations;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outVars;
protected
BinaryTreeInt.BinTree bt;
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, bt) := BackendDAEUtil.traverseBackendDAEExpsEqns(inEquations, checkEquationsVarsExpTopDownTraverseHelper, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsVars;
public function equationsVars2
"author: Frenkel TUD 2011-05
From the equations and a variable array return all
occurring variables form the array."
input BackendDAE.EquationArray inEquations;
input BackendDAE.Variables inVars;
output list<BackendDAE.Var> outVars;
protected
BinaryTreeInt.BinTree bt;
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, bt) := BackendDAEUtil.traverseBackendDAEExpsEqns(inEquations, checkEquationsVarsExpTopDownTraverseHelper, (inVars, bt) );
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end equationsVars2;
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
BinaryTreeInt.BinTree bt;
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, (_, bt)) := traverseExpsOfEquation(inEquation, checkEquationsVarsExpTopDownTraverseHelper, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
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 inVars;
output list<BackendDAE.Var> outVars;
protected
BinaryTreeInt.BinTree bt;
list<Integer> keys;
algorithm
bt := BinaryTreeInt.emptyBinTree;
(_, (_, bt)) := Expression.traverseExpTopDown(inExp, checkEquationsVarsExpTopDown, (inVars, bt));
(keys, _) := BinaryTreeInt.bintreeToList(bt);
outVars := List.map1r(keys, BackendVariable.getVarAt, inVars);
end expressionVars;
public function checkEquationsVarsExpTopDownTraverseHelper
"This function traverses expssions to-down to collect all
variables that are involed into the expression."
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> itpl;
output DAE.Exp outExp;
output tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> otpl;
algorithm
(outExp, otpl) := Expression.traverseExpTopDown(inExp,checkEquationsVarsExpTopDown,itpl);
end checkEquationsVarsExpTopDownTraverseHelper;
protected function checkEquationsVarsExpTopDown
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> inTuple;
output DAE.Exp outExp;
output Boolean cont;
output tuple<BackendDAE.Variables, BinaryTreeInt.BinTree> outTuple;
algorithm
(outExp,cont,outTuple) := matchcontinue (inExp,inTuple)
local
DAE.Exp e;
BackendDAE.Variables vars;
BinaryTreeInt.BinTree bt;
DAE.ComponentRef cr;
list<Integer> ilst;
// special case for time, it is never part of the equation system
case (e as DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time")), _)
then (e, true, inTuple);
// case for function pointers
case (e as DAE.CREF(ty=DAE.T_FUNCTION_REFERENCE_FUNC()), _)
then (e, true, inTuple);
// case for pre vars
case (DAE.CALL(path = Absyn.IDENT(name = "pre")), _)
then (inExp, false, inTuple);
// case for previous vars
case (DAE.CALL(path = Absyn.IDENT(name = "previous")), _)
then (inExp, false, inTuple);
// add it
case (e as DAE.CREF(componentRef = cr), (vars, bt)) equation
(_, ilst) = BackendVariable.getVar(cr, vars);
bt = BinaryTreeInt.treeAddList(bt, ilst);
then (e, true, (vars, bt));
else (inExp, true, inTuple);
end matchcontinue;
end checkEquationsVarsExpTopDown;
public function equationsStates "author: Frenkel TUD
From a list of equations return all occurring state variables references."
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.Variables inVars;
output list<DAE.ComponentRef> outExpComponentRefLst;
algorithm
(_, (_,(outExpComponentRefLst, _))) := traverseExpsOfEquationList(inEquationLst, Expression.traverseSubexpressionsHelper, (traversingStateRefFinder, ({}, inVars)));
end equationsStates;
protected function traversingStateRefFinder "author: Frenkel TUD 2011-05"
input DAE.Exp inExp;
input tuple<list<DAE.ComponentRef>, BackendDAE.Variables> inTpl;
output DAE.Exp outExp;
output tuple<list<DAE.ComponentRef>, BackendDAE.Variables> outTpl;
algorithm
outExp := inExp;
(outTpl) := match (inExp,inTpl)
local
BackendDAE.Variables vars;
list<DAE.ComponentRef> crefs;
DAE.ComponentRef cr;
case (DAE.CREF(componentRef=cr), (crefs, vars))
guard BackendVariable.isState(cr, vars)
equation
crefs = List.unionEltOnTrue(cr, crefs, ComponentReference.crefEqual);
then (crefs, vars);
else inTpl;
end match;
end traversingStateRefFinder;
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 component references."
input BackendDAE.Equation inEquation;
output list<DAE.ComponentRef> outExpComponentRefLst;
algorithm
outExpComponentRefLst := equationsCrefs({inEquation});
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;
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 a 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;
public 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;
public 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;
case {} then (listReverse(inAccum),inTypeA);
case (BackendDAE.ASSIGN(left = cr, right = e2, source = source)::rest)
equation
tp = Expression.typeof(e2);
e1 = Expression.makeCrefExp(cr, tp);
(e1, extArg) = inFunc(e1, inTypeA);
if Expression.isCref(e1) then
DAE.CREF(cr1, _) = e1;
else
cr1=cr;
end if;
(e2, extArg) = inFunc(e2, extArg);
(outWhenOps, extArg) = traverseExpsOfWhenOps(rest, inFunc, extArg, BackendDAE.ASSIGN(cr1, e2, source)::inAccum);
then (outWhenOps, extArg);
case (BackendDAE.REINIT(stateVar = cr, value = e2, source = source)::rest)
equation
e1 = Expression.crefExp(cr);
(e1, extArg) = inFunc(e1, inTypeA);
if Expression.isCref(e1) then
DAE.CREF(cr1, _) = e1;
else
cr1=cr;
end if;
(e2, extArg) = inFunc(e2, extArg);
(outWhenOps, extArg) = traverseExpsOfWhenOps(rest, inFunc, extArg, BackendDAE.REINIT(cr1, e2, source)::inAccum);
then (outWhenOps, extArg);
case (BackendDAE.ASSERT(condition = e1, message = e2, level = level, source = source)::rest)
equation
(e1, extArg) = inFunc(e1, inTypeA);
(e2, extArg) = inFunc(e2, extArg);
(outWhenOps, extArg) = traverseExpsOfWhenOps(rest, inFunc, extArg, BackendDAE.ASSERT(e1, e2, level, source)::inAccum);
then (outWhenOps, extArg);
case (BackendDAE.TERMINATE(message = e1, source = source)::rest)
equation
(e1, extArg) = inFunc(e1, inTypeA);
(outWhenOps, extArg) = traverseExpsOfWhenOps(rest, inFunc, extArg, BackendDAE.TERMINATE(e1, source)::inAccum);
then (outWhenOps, extArg);
case (BackendDAE.NORETCALL(exp = e1, source = source)::rest)
equation
(e1, extArg) = inFunc(e1, inTypeA);
(outWhenOps, extArg) = traverseExpsOfWhenOps(rest, inFunc, extArg, BackendDAE.NORETCALL(e1, source)::inAccum);
then (outWhenOps, extArg);
end match;
end traverseExpsOfWhenOps;
public function traverseExpsOfWhenEquation_WithStop<T>
"Traverses all expressions of a when equation.
Helper function of traverseExpsOfEquation."
input BackendDAE.WhenEquation inWhenEquation;
input FuncExpType inFunc;
input T inTypeA;
output Boolean outCont;
output T outTypeA;
partial function FuncExpType
input DAE.Exp inExp;
input T inTypeA;
output DAE.Exp outExp;
output Boolean cont;
output T outTypeA;
end FuncExpType;
algorithm
(outCont, outTypeA) := match(inWhenEquation)
local
BackendDAE.WhenEquation elsewe;
Option<BackendDAE.WhenEquation> oelsewe;
DAE.Exp cond;
list<BackendDAE.WhenOperator> whenStmtLst;
T extArg;
Boolean b;
case BackendDAE.WHEN_STMTS(condition=cond, whenStmtLst=whenStmtLst, elsewhenPart = oelsewe)
equation
(_, b, extArg) = inFunc(cond, inTypeA);
if b then
(b, extArg) = traverseExpsOfWhenOps_WithStop(whenStmtLst, inFunc, extArg, b);
end if;
if b then
if isSome(oelsewe) then
SOME(elsewe) = oelsewe;
(b, extArg) = traverseExpsOfWhenEquation_WithStop(elsewe, inFunc, extArg);
end if;
end if;
then (b, extArg);
end match;
end traverseExpsOfWhenEquation_WithStop;
public function statementEq
"Creates equation from one statement."
input DAE.Statement iStmts;
output BackendDAE.Equation oEq;
algorithm
(oEq) := match(iStmts)
local
DAE.ComponentRef cr;
DAE.Exp exp;
list<DAE.Exp> explst;
case (DAE.STMT_ASSIGN(exp1 = DAE.CREF(componentRef = cr), exp = exp))
then generateEquation(Expression.crefExp(cr), exp);
case (DAE.STMT_ASSIGN_ARR(lhs = DAE.CREF(componentRef = cr), exp = exp))
then generateEquation(Expression.crefExp(cr), exp);
case (DAE.STMT_TUPLE_ASSIGN(expExpLst = explst, exp = exp))