/
BackendDAEOptimize.mo
3376 lines (3154 loc) · 148 KB
/
BackendDAEOptimize.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.
*
*/
encapsulated package BackendDAEOptimize
" file: BackendDAEOptimize.mo
package: BackendDAEOptimize
description: BackendDAEOPtimize contains functions that do some kind of
optimazation on the BackendDAE datatype:
- removing simpleEquations
- Tearing/Relaxation
- Linearization
- Inline Integration
- and so on ...
RCS: $Id$
"
public import Absyn;
public import BackendDAE;
public import DAE;
protected import BackendDump;
protected import BackendDAECreate;
protected import BackendDAETransform;
protected import BackendDAEUtil;
protected import BackendEquation;
protected import BackendVarTransform;
protected import BackendVariable;
protected import Builtin;
protected import ClassInf;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import Derive;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSolve;
protected import ExpressionSimplify;
protected import Error;
protected import Inline;
protected import RTOpts;
protected import Util;
protected import VarTransform;
protected import Values;
protected import ValuesUtil;
/*
* inline functions stuff
*/
public function lateInlineDAE
"function lateInlineDAE"
input BackendDAE.BackendDAE inDAE;
input DAE.FunctionTree inFunctionTree;
input BackendDAE.IncidenceMatrix inM;
input BackendDAE.IncidenceMatrix inMT;
input array<Integer> inAss1;
input array<Integer> inAss2;
input list<list<Integer>> inComps;
output BackendDAE.BackendDAE outDAE;
output BackendDAE.IncidenceMatrix outM;
output BackendDAE.IncidenceMatrix outMT;
output array<Integer> outAss1;
output array<Integer> outAss2;
output list<list<Integer>> outComps;
output Boolean outRunMatching;
algorithm
outDAE := Inline.inlineCalls(SOME(inFunctionTree),{DAE.NORM_INLINE(),DAE.AFTER_INDEX_RED_INLINE()},inDAE);
outM := inM;
outMT := inMT;
outAss1 := inAss1;
outAss2 := inAss2;
outComps := inComps;
outRunMatching := false;
end lateInlineDAE;
/*
* remove simply equations stuff
*/
public function removeSimpleEquationsPast
"function lateInlineDAE"
input BackendDAE.BackendDAE inDAE;
input DAE.FunctionTree inFunctionTree;
input BackendDAE.IncidenceMatrix inM;
input BackendDAE.IncidenceMatrix inMT;
input array<Integer> inAss1;
input array<Integer> inAss2;
input list<list<Integer>> inComps;
output BackendDAE.BackendDAE outDAE;
output BackendDAE.IncidenceMatrix outM;
output BackendDAE.IncidenceMatrix outMT;
output array<Integer> outAss1;
output array<Integer> outAss2;
output list<list<Integer>> outComps;
output Boolean outRunMatching;
protected
Option<BackendDAE.IncidenceMatrix> om,omT;
algorithm
(outDAE,om,omT) := removeSimpleEquations(inDAE,inFunctionTree,SOME(inM),SOME(inMT));
(outM,outMT) := BackendDAEUtil.getIncidenceMatrixfromOption(outDAE,om,omT);
outAss1 := inAss1;
outAss2 := inAss2;
outComps := inComps;
outRunMatching := true; // until remove simple equations does not update incidence matrix
end removeSimpleEquationsPast;
public function removeSimpleEquations
"function: removeSimpleEquations
This function moves simple equations on the form a=b from equations 2nd
in BackendDAE.BackendDAE to simple equations 3rd in BackendDAE.BackendDAE to speed up assignment alg.
inputs: (vars: Variables,
knownVars: Variables,
eqns: BackendDAE.Equation list,
simpleEqns: BackendDAE.Equation list,
initEqns : Equatoin list,
binTree: BinTree)
outputs: (Variables, BackendDAE.Variables, BackendDAE.Equation list, BackendDAE.Equation list
BackendDAE.Equation list)"
input BackendDAE.BackendDAE inDAE;
input DAE.FunctionTree inFunctionTree;
input Option<BackendDAE.IncidenceMatrix> inM;
input Option<BackendDAE.IncidenceMatrix> inMT;
output BackendDAE.BackendDAE outDAE;
output Option<BackendDAE.IncidenceMatrix> outM;
output Option<BackendDAE.IncidenceMatrix> outMT;
algorithm
(outDAE,outM,outMT):=
match (inDAE,inFunctionTree,inM,inMT)
local
DAE.FunctionTree funcs;
Option<BackendDAE.IncidenceMatrix> m,mT;
BackendDAE.Variables vars,vars_1,knvars,exobj,knvars_1,knvars_2;
BackendDAE.AliasVariables av,varsAliases;
BackendDAE.EquationArray eqns,eqns1,remeqns,remeqns1,inieqns,inieqns1;
array<BackendDAE.MultiDimEquation> arreqns,arreqns1;
array<DAE.Algorithm> algorithms,algorithms1;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
VarTransform.VariableReplacements repl,replc,replc_1,vartransf,vartransf1;
list<BackendDAE.Equation> eqns_1,seqns,eqns_2,seqns_1,ieqns_1,eqns_3,seqns_2,ieqns_2,seqns_4,seqns_3,lsteqns,reqns,reqns_1,ieqns;
list<BackendDAE.MultiDimEquation> lstarreqns,lstarreqns1,lstarreqns2;
BackendDAE.BinTree movedvars_1,outputs;
list<DAE.Algorithm> algs,algs_1;
list<tuple<list<DAE.Exp>,list<DAE.Exp>>> inputsoutputs;
case (BackendDAE.DAE(vars,knvars,exobj,av,eqns,remeqns,inieqns,arreqns,algorithms,einfo,eoc),funcs,m,mT)
equation
repl = VarTransform.emptyReplacements();
replc = VarTransform.emptyReplacements();
lsteqns = BackendDAEUtil.equationList(eqns);
reqns = BackendDAEUtil.equationList(remeqns);
ieqns = BackendDAEUtil.equationList(inieqns);
lstarreqns = arrayList(arreqns);
algs = arrayList(algorithms);
outputs = BackendDAE.emptyBintree;
outputs = getOutputsFromAlgorithms(lsteqns,outputs);
(eqns_1,seqns,movedvars_1,vartransf,_,replc_1) = removeSimpleEquations2(lsteqns, simpleEquation, vars, knvars, BackendDAE.emptyBintree, outputs, repl, {},replc);
vartransf1 = VarTransform.addMultiDimReplacements(vartransf);
Debug.fcall("dumprepl", VarTransform.dumpReplacements, vartransf1);
Debug.fcall("dumpreplc", VarTransform.dumpReplacements, replc_1);
eqns_2 = BackendVarTransform.replaceEquations(eqns_1, replc_1);
seqns_1 = BackendVarTransform.replaceEquations(seqns, replc_1);
ieqns_1 = BackendVarTransform.replaceEquations(ieqns, replc_1);
lstarreqns1 = BackendVarTransform.replaceMultiDimEquations(lstarreqns, replc_1);
eqns_3 = BackendVarTransform.replaceEquations(eqns_2, vartransf1);
seqns_2 = BackendVarTransform.replaceEquations(seqns_1, vartransf1);
ieqns_2 = BackendVarTransform.replaceEquations(ieqns_1, vartransf1);
reqns_1 = BackendVarTransform.replaceEquations(reqns, vartransf1);
lstarreqns2 = BackendVarTransform.replaceMultiDimEquations(lstarreqns1, vartransf1);
algs_1 = BackendVarTransform.replaceAlgorithms(algs,vartransf1);
(vars_1,knvars_1) = BackendVariable.moveVariables(vars, knvars, movedvars_1);
inputsoutputs = Util.listMap1r(algs_1,BackendDAECreate.lowerAlgorithmInputsOutputs,vars_1);
eqns_3 = Util.listMap1(eqns_3,updateAlgorithmInputsOutputs,inputsoutputs);
seqns_3 = listAppend(seqns_2, reqns_1) "& print_vars_statistics(vars\',knvars\')" ;
(knvars_2,seqns_4,varsAliases) = removeConstantEqns(knvars_1,seqns_3,av);
Debug.fcall("dumpalias", BackendDump.dumpAliasVariables, varsAliases);
eqns1 = BackendDAEUtil.listEquation(eqns_3);
remeqns1 = BackendDAEUtil.listEquation(seqns_4);
inieqns1 = BackendDAEUtil.listEquation(ieqns_2);
arreqns1 = listArray(lstarreqns2);
algorithms1 = listArray(algs_1);
then
(BackendDAE.DAE(vars_1,knvars_2,exobj,varsAliases,eqns1,remeqns1,inieqns1,arreqns1,algorithms1,einfo,eoc),NONE(),NONE());
end match;
end removeSimpleEquations;
protected function removeSimpleEquations2
"Traverses all equations and puts those that are simple in
a separate list. It builds a set of varable replacements that
are later used to replace these variable substitutions in the
equations that are left."
input list<BackendDAE.Equation> eqns;
input FuncTypeSimpleEquation funcSimpleEquation "function as argument so it can be distinguish between a=b/a=-b and a=const.";
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
input BackendDAE.BinTree mvars;
input BackendDAE.BinTree outputs;
input VarTransform.VariableReplacements repl;
input list<DAE.ComponentRef> inExtendLst;
input VarTransform.VariableReplacements replc;
output list<BackendDAE.Equation> outEqns;
output list<BackendDAE.Equation> outSimpleEqns;
output BackendDAE.BinTree outMvars;
output VarTransform.VariableReplacements outRepl;
output list<DAE.ComponentRef> outExtendLst;
output VarTransform.VariableReplacements outReplc;
partial function FuncTypeSimpleEquation
input BackendDAE.Equation eqn;
input Boolean swap;
output DAE.Exp e1;
output DAE.Exp e2;
output DAE.ElementSource source;
end FuncTypeSimpleEquation;
algorithm
(outEqns,outSimpleEqns,outMvars,outRepl,outExtendLst,outReplc) := matchcontinue (eqns,funcSimpleEquation,vars,knvars,mvars,outputs,repl,inExtendLst,replc)
local
BackendDAE.BinTree mvars_1,mvars_2;
VarTransform.VariableReplacements repl_1,repl_2,replc_1,replc_2;
DAE.ComponentRef cr1;
list<BackendDAE.Equation> eqns_1,seqns_1;
BackendDAE.Equation e;
DAE.ExpType t;
DAE.Exp e1,e2;
DAE.ElementSource source "the element source";
list<DAE.ComponentRef> extlst,extlst1;
BackendDAE.Equation eq1,eq2;
case ({},funcSimpleEquation,vars,knvars,mvars,outputs,repl,extlst,replc) then ({},{},mvars,repl,extlst,replc);
case (e::eqns,funcSimpleEquation,vars,knvars,mvars,outputs,repl,inExtendLst,replc) equation
{e} = BackendVarTransform.replaceEquations({e},replc);
{e} = BackendVarTransform.replaceEquations({e},repl);
(e1 as DAE.CREF(cr1,t),e2,source) = funcSimpleEquation(e,false);
false = BackendVariable.isState(cr1, vars) "cr1 not state";
BackendVariable.isVariable(cr1, vars, knvars) "cr1 not constant";
false = BackendVariable.isTopLevelInputOrOutput(cr1,vars,knvars);
failure(_ = BackendDAEUtil.treeGet(outputs, cr1)) "cr1 not output of algorithm";
(extlst,_,replc_1) = removeSimpleEquations3(inExtendLst,replc,cr1,NONE(),e2,t);
repl_1 = VarTransform.addReplacement(repl, cr1, e2);
mvars_1 = BackendDAEUtil.treeAdd(mvars, cr1, 0);
(eqns_1,seqns_1,mvars_2,repl_2,extlst1,replc_2) = removeSimpleEquations2(eqns, funcSimpleEquation, vars, knvars, mvars_1, outputs, repl_1, extlst,replc_1);
then
(eqns_1,(BackendDAE.SOLVED_EQUATION(cr1,e2,source) :: seqns_1),mvars_2,repl_2,extlst1,replc_2);
// Swapped args
case (e::eqns,funcSimpleEquation,vars,knvars,mvars,outputs,repl,inExtendLst,replc) equation
{e} = BackendVarTransform.replaceEquations({e},replc);
{BackendDAE.EQUATION(e1,e2,source)} = BackendVarTransform.replaceEquations({e},repl);
(e1 as DAE.CREF(cr1,t),e2,source) = simpleEquation(BackendDAE.EQUATION(e2,e1,source),true);
false = BackendVariable.isState(cr1, vars) "cr1 not state";
BackendVariable.isVariable(cr1, vars, knvars) "cr1 not constant";
false = BackendVariable.isTopLevelInputOrOutput(cr1,vars,knvars);
failure(_ = BackendDAEUtil.treeGet(outputs, cr1)) "cr1 not output of algorithm";
(extlst,_,replc_1) = removeSimpleEquations3(inExtendLst,replc,cr1,NONE(),e2,t);
repl_1 = VarTransform.addReplacement(repl, cr1, e2);
mvars_1 = BackendDAEUtil.treeAdd(mvars, cr1, 0);
(eqns_1,seqns_1,mvars_2,repl_2,extlst1,replc_2) = removeSimpleEquations2(eqns, funcSimpleEquation, vars, knvars, mvars_1, outputs, repl_1, extlst,replc_1);
then
(eqns_1,(BackendDAE.SOLVED_EQUATION(cr1,e2,source) :: seqns_1),mvars_2,repl_2,extlst1,replc_2);
// try next equation.
case ((e :: eqns),funcSimpleEquation,vars,knvars,mvars,outputs,repl,extlst,replc)
equation
{eq1} = BackendVarTransform.replaceEquations({e},replc);
{eq2} = BackendVarTransform.replaceEquations({eq1},repl);
//print("not removed simple ");print(equationStr(e));print("\n -> ");print(equationStr(eq1));
//print("\n\n");
(eqns_1,seqns_1,mvars_1,repl_1,extlst1,replc_1) = removeSimpleEquations2(eqns, funcSimpleEquation, vars, knvars, mvars, outputs, repl, extlst,replc) "Not a simple variable, check rest" ;
then
((e :: eqns_1),seqns_1,mvars_1,repl_1,extlst1,replc_1);
end matchcontinue;
end removeSimpleEquations2;
protected function removeSimpleEquations3"
Author: Frenkel TUD 2010-07 function removeSimpleEquations3
helper for removeSimpleEquations2
if a cref has to be replaced the parent types
have to be extended. For example x[2] has to be
replace all crefs x should be extended to {x[1],x[2],..}
to replace x[2]. The same for records"
input list<DAE.ComponentRef> increflst;
input VarTransform.VariableReplacements inrepl;
input DAE.ComponentRef cr;
input Option<DAE.ComponentRef> precr;
input DAE.Exp e;
input DAE.ExpType t;
output list<DAE.ComponentRef> outcreflst;
output list<DAE.ComponentRef> outcreflst1;
output VarTransform.VariableReplacements outrepl;
algorithm
(outcreflst,outcreflst1,outrepl) := matchcontinue (increflst,inrepl,cr,precr,e,t)
local
list<DAE.ComponentRef> crlst,crlst1,crlst_1;
VarTransform.VariableReplacements repl,repl_1,repl_2;
DAE.Exp e1;
DAE.ComponentRef sc,sc1,cr1,pcr;
list<DAE.Subscript> subscriptLst;
list<DAE.ExpVar> varLst;
list<DAE.Exp> expl;
DAE.ExpType ty;
DAE.Ident ident;
Absyn.Path name;
// array ?
case (crlst,repl,cr as DAE.CREF_QUAL(ident=ident,identType=ty as DAE.ET_ARRAY(ty=_),subscriptLst={},componentRef=cr1),NONE(),e,t)
equation
sc = ComponentReference.makeCrefIdent(ident,ty,{});
// check List
failure(_ = Util.listFindWithCompareFunc(crlst,sc,ComponentReference.crefEqualNoStringCompare,false));
// extend cr
e1 = Expression.makeCrefExp(sc,ty);
((e1,_)) = BackendDAEUtil.extendArrExp((e1,NONE()));
// add
repl_1 = VarTransform.addReplacement(repl, sc, e1);
(crlst1,crlst_1,repl_2) = removeSimpleEquations3(sc::crlst,repl_1,cr1,SOME(sc),e,t);
then
(crlst1,crlst_1,repl_2);
case (crlst,repl,cr as DAE.CREF_QUAL(ident=ident,identType=ty as DAE.ET_ARRAY(ty=_),subscriptLst={},componentRef=cr1),SOME(pcr),e,t)
equation
sc1 = ComponentReference.makeCrefIdent(ident,ty,{});
sc = ComponentReference.joinCrefs(pcr,sc1);
// check List
failure(_ = Util.listFindWithCompareFunc(crlst,sc,ComponentReference.crefEqualNoStringCompare,false));
// extend cr
e1 = Expression.makeCrefExp(sc,ty);
((e1,_)) = BackendDAEUtil.extendArrExp((e1,NONE()));
// add
repl_1 = VarTransform.addReplacement(repl, sc, e1);
(crlst1,crlst_1,repl_2) = removeSimpleEquations3(sc::crlst,repl_1,cr1,SOME(sc),e,t);
then
(crlst1,crlst_1,repl_2);
// record ?
case (crlst,repl,cr as DAE.CREF_QUAL(ident=ident,identType=ty as DAE.ET_COMPLEX(name=name,varLst=varLst,complexClassType=ClassInf.RECORD(_)),subscriptLst=subscriptLst,componentRef=cr1),NONE(),e,t)
equation
sc = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
// check List
failure(_ = Util.listFindWithCompareFunc(crlst,sc,ComponentReference.crefEqualNoStringCompare,false));
// extend cr
expl = Util.listMap1(varLst,Expression.generateCrefsExpFromExpVar,sc);
e1 = DAE.CALL(name,expl,false,false,ty,DAE.NO_INLINE());
// add
repl_1 = VarTransform.addReplacement(repl, sc, e1);
(crlst1,crlst_1,repl_2) = removeSimpleEquations3(sc::crlst,repl_1,cr1,SOME(sc),e,t);
then
(crlst1,crlst_1,repl_2);
case (crlst,repl,cr as DAE.CREF_QUAL(ident=ident,identType=ty as DAE.ET_COMPLEX(name=name,varLst=varLst,complexClassType=ClassInf.RECORD(_)),subscriptLst=subscriptLst,componentRef=cr1),SOME(pcr),e,t)
equation
sc1 = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
sc = ComponentReference.joinCrefs(pcr,sc1);
// check List
failure(_ = Util.listFindWithCompareFunc(crlst,sc,ComponentReference.crefEqualNoStringCompare,false));
// extend cr
expl = Util.listMap1(varLst,Expression.generateCrefsExpFromExpVar,sc);
e1 = DAE.CALL(name,expl,false,false,ty,DAE.NO_INLINE());
// add
repl_1 = VarTransform.addReplacement(repl, sc, e1);
(crlst1,crlst_1,repl_2) = removeSimpleEquations3(sc::crlst,repl_1,cr1,SOME(sc),e,t);
then
(crlst1,crlst_1,repl_2);
case (crlst,repl,cr as DAE.CREF_IDENT(ident=ident,identType=ty as DAE.ET_COMPLEX(name=name,varLst=varLst,complexClassType=ClassInf.RECORD(_)),subscriptLst=subscriptLst),NONE(),e as DAE.CREF(componentRef=_),t)
then
(crlst,{cr},repl);
case (crlst,repl,cr as DAE.CREF_IDENT(ident=ident,identType=ty as DAE.ET_COMPLEX(name=name,varLst=varLst,complexClassType=ClassInf.RECORD(_)),subscriptLst=subscriptLst),SOME(pcr),e as DAE.CREF(componentRef=_),t)
equation
sc = ComponentReference.joinCrefs(pcr,cr);
then
(crlst,{sc},repl);
// other
case (crlst,repl,cr as DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=cr1),NONE(),e,t)
equation
sc = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
(crlst1,crlst_1,repl_2) = removeSimpleEquations3(crlst,repl,cr1,SOME(sc),e,t);
then
(crlst1,crlst_1,repl_2);
case (crlst,repl,cr as DAE.CREF_QUAL(ident=ident,identType=ty,subscriptLst=subscriptLst,componentRef=cr1),SOME(pcr),e,t)
equation
sc1 = ComponentReference.makeCrefIdent(ident,ty,subscriptLst);
sc = ComponentReference.joinCrefs(pcr,sc1);
(crlst1,crlst_1,repl_2) = removeSimpleEquations3(crlst,repl,cr1,SOME(sc),e,t);
then
(crlst1,crlst_1,repl_2);
case (crlst,repl,_,_,_,_) then (crlst,{},repl);
end matchcontinue;
end removeSimpleEquations3;
protected function replaceCrefFromExp "function: replaceCrefFromExp
author: Frenkel TUD 2010-12
helper for removeSimpleEquations"
input tuple<DAE.Exp, tuple<VarTransform.VariableReplacements,list<DAE.ComponentRef>>> inTpl;
output tuple<DAE.Exp, tuple<VarTransform.VariableReplacements,list<DAE.ComponentRef>>> outTpl;
algorithm
outTpl := matchcontinue(inTpl)
local
VarTransform.VariableReplacements repl;
list<DAE.ComponentRef> recordcrefs;
DAE.ComponentRef cr,sc;
DAE.Exp e,e1,e2;
list<DAE.Subscript> subs;
// as it is
case((DAE.CREF(componentRef=cr), (repl,recordcrefs)))
equation
(e) = VarTransform.getReplacement(repl, cr);
((e1,(_,_))) = Expression.traverseExp(e,replaceCrefFromExp,(repl,recordcrefs));
then
((e1, (repl,recordcrefs) ));
// array elements
case((DAE.CREF(componentRef=cr), (repl,recordcrefs)))
equation
subs = ComponentReference.crefLastSubs(cr);
sc = ComponentReference.crefStripLastSubs(cr);
(e) = VarTransform.getReplacement(repl, sc);
e1 = Expression.applyExpSubscripts(e,subs);
((e2,(_,_))) = Expression.traverseExp(e1,replaceCrefFromExp,(repl,recordcrefs));
then
((e2, (repl,recordcrefs) ));
// record elements
case((DAE.CREF(componentRef=cr), (repl,recordcrefs)))
equation
e1 = replaceRecordCrefFromExp(cr,recordcrefs,repl);
((e2,(_,_))) = Expression.traverseExp(e1,replaceCrefFromExp,(repl,recordcrefs));
then
((e2, (repl,recordcrefs) ));
case(inTpl) then inTpl;
end matchcontinue;
end replaceCrefFromExp;
protected function replaceRecordCrefFromExp "function: replaceRecordCrefFromExp
author: Frenkel TUD 2010-12
helper for removeSimpleEquations"
input DAE.ComponentRef inCref;
input list<DAE.ComponentRef> inRecCrefs;
input VarTransform.VariableReplacements inRepl;
output DAE.Exp outExp;
algorithm
outExp := matchcontinue(inCref,inRecCrefs,inRepl)
local
VarTransform.VariableReplacements repl;
list<DAE.ComponentRef> recordcrefs;
DAE.ComponentRef cr,sc,rcr,lcr,ncr;
DAE.Exp e,e1;
case(cr,rcr::recordcrefs,repl)
equation
true = ComponentReference.crefPrefixOf(rcr,cr);
(e as DAE.CREF(componentRef=sc)) = VarTransform.getReplacement(repl, cr);
lcr = ComponentReference.crefStripPrefix(cr,rcr);
ncr = ComponentReference.joinCrefs(sc,lcr);
e1 = Expression.crefExp(ncr);
then
e1;
case(cr,rcr::recordcrefs,repl)
equation
false = ComponentReference.crefPrefixOf(rcr,cr);
e = replaceRecordCrefFromExp(cr,recordcrefs,repl);
then
e;
end matchcontinue;
end replaceRecordCrefFromExp;
protected function getOutputsFromAlgorithms"
Author: Frenkel TUD 2010-09 function getOutputsFromAlgorithms
helper for removeSimpleEquations
collect all outpus from algorithms to avoid replacement
of a algorithm output"
input list<BackendDAE.Equation> inEqns;
input BackendDAE.BinTree inBinTree;
output BackendDAE.BinTree outBinTree;
algorithm
outBinTree := matchcontinue (inEqns,inBinTree)
local
list<BackendDAE.Equation> es;
BackendDAE.Equation e;
BackendDAE.BinTree bt,bt_1,bt_2;
list<DAE.Exp> explst;
list<DAE.ComponentRef> crefs;
case ({},bt) then bt;
case (BackendDAE.ALGORITHM(out=explst)::es,bt)
equation
crefs = Util.listFlatten(Util.listMap(explst,Expression.extractCrefsFromExp));
bt_1 = BackendDAEUtil.treeAddList(bt,crefs);
bt_2 = getOutputsFromAlgorithms(es,bt_1);
then bt_2;
case (e::es,bt)
equation
bt_1 = getOutputsFromAlgorithms(es,bt);
then bt_1;
end matchcontinue;
end getOutputsFromAlgorithms;
protected function updateAlgorithmInputsOutputs"
Author: Frenkel TUD 2010-09 function updateAlgorithmInputsOutputs
helper for removeSimpleEquations
update inputs and outputs of algorithms after remove simple equations"
input BackendDAE.Equation inEqn;
input list<tuple<list<DAE.Exp>,list<DAE.Exp>>> inAlgsInputsOutputs;
output BackendDAE.Equation outEqn;
algorithm
outEqn := matchcontinue (inEqn,inAlgsInputsOutputs)
local
BackendDAE.Equation e;
list<tuple<list<DAE.Exp>,list<DAE.Exp>>> inputsoutputs;
Integer index;
list<DAE.Exp> inputs,outputs;
DAE.ElementSource source;
case (BackendDAE.ALGORITHM(index=index,source=source),inputsoutputs)
equation
true = listLength(inputsoutputs) > index;
((inputs,outputs)) = listNth(inputsoutputs,index);
then BackendDAE.ALGORITHM(index,inputs,outputs,source);
case (e,_) then e;
end matchcontinue;
end updateAlgorithmInputsOutputs;
public function countSimpleEquations
"Counts the number of trivial/simple equations
e.g on form a=b, a=-b or a=constant"
input BackendDAE.EquationArray eqns;
output Integer numEqns;
protected Integer elimLevel;
algorithm
elimLevel := RTOpts.eliminationLevel();
RTOpts.setEliminationLevel(2) "Full elimination";
numEqns := BackendEquation.traverseBackendDAEEqns(eqns,countSimpleEquations2,0);
RTOpts.setEliminationLevel(elimLevel);
end countSimpleEquations;
protected function countSimpleEquations2
input tuple<BackendDAE.Equation, Integer> inTpl;
output tuple<BackendDAE.Equation, Integer> outTpl;
algorithm
outTpl := matchcontinue(inTpl)
local
BackendDAE.Equation e;
Integer partialSum;
case ((e,partialSum))
equation
(_,_,_) = simpleEquation(e,false);
then ((e,partialSum+1));
// Swaped args in simpleEquation
case ((e,partialSum))
equation
(_,_,_) = simpleEquation(e,true);
then ((e,partialSum+1));
//Not simple eqn.
case inTpl then inTpl;
end matchcontinue;
end countSimpleEquations2;
protected function simpleEquation
"Returns the two sides of an equation as expressions if it is a
simple equation. Simple equations are
a+b=0, a-b=0, a=constant, a=-b, etc.
The first expression returned, e1, is always a CREF.
If the equation is not simple, this function will fail."
input BackendDAE.Equation eqn;
input Boolean swap "if true swap args.";
output DAE.Exp e1;
output DAE.Exp e2;
output DAE.ElementSource source;
algorithm
(e1,e2,source) := matchcontinue (eqn,swap)
local
DAE.Exp e;
DAE.ExpType t;
DAE.ElementSource src "the element source";
// a = b;
case (BackendDAE.EQUATION(e1 as DAE.CREF(componentRef = _),e2 as DAE.CREF(componentRef = _),src),swap)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
then (e1,e2,src);
// a-b = 0
case (BackendDAE.EQUATION(DAE.BINARY(e1 as DAE.CREF(_,_),DAE.SUB(_),e2 as DAE.CREF(_,_)),e,src),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then
(e1,e2,src);
case (BackendDAE.EQUATION(DAE.BINARY(e1 as DAE.CREF(_,_),DAE.SUB_ARR(_),e2 as DAE.CREF(_,_)),e,src),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then
(e1,e2,src);
// a-b = 0 swap
case (BackendDAE.EQUATION(DAE.BINARY(e1 as DAE.CREF(_,_),DAE.SUB(_),e2 as DAE.CREF(_,_)),e,src),true)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then
(e2,e1,src);
case (BackendDAE.EQUATION(DAE.BINARY(e1 as DAE.CREF(_,_),DAE.SUB_ARR(_),e2 as DAE.CREF(_,_)),e,src),true)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then
(e2,e1,src);
// 0 = a-b
case (BackendDAE.EQUATION(e,DAE.BINARY(e1 as DAE.CREF(_,_),DAE.SUB(_),e2 as DAE.CREF(_,_)),src),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then
(e1,e2,src);
case (BackendDAE.EQUATION(e,DAE.BINARY(e1 as DAE.CREF(_,_),DAE.SUB_ARR(_),e2 as DAE.CREF(_,_)),src),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then
(e1,e2,src);
// 0 = a-b swap
case (BackendDAE.EQUATION(e,DAE.BINARY(e1 as DAE.CREF(_,_),DAE.SUB(_),e2 as DAE.CREF(_,_)),src),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then
(e2,e1,src);
case (BackendDAE.EQUATION(e,DAE.BINARY(e1 as DAE.CREF(_,_),DAE.SUB_ARR(_),e2 as DAE.CREF(_,_)),src),false)
equation
true = RTOpts.eliminationLevel() > 0;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then
(e2,e1,src);
// a + b = 0
case (BackendDAE.EQUATION(DAE.BINARY(e1 as DAE.CREF(_,_),DAE.ADD(t),e2 as DAE.CREF(_,_)),e,src),false) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e1,DAE.UNARY(DAE.UMINUS(t),e2),src);
case (BackendDAE.EQUATION(DAE.BINARY(e1 as DAE.CREF(_,_),DAE.ADD_ARR(t),e2 as DAE.CREF(_,_)),e,src),false) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e1,DAE.UNARY(DAE.UMINUS_ARR(t),e2),src);
// a + b = 0 swap
case (BackendDAE.EQUATION(DAE.BINARY(e1 as DAE.CREF(_,_),DAE.ADD(t),e2 as DAE.CREF(_,_)),e,src),true) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e2,DAE.UNARY(DAE.UMINUS(t),e1),src);
case (BackendDAE.EQUATION(DAE.BINARY(e1 as DAE.CREF(_,_),DAE.ADD_ARR(t),e2 as DAE.CREF(_,_)),e,src),true) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e2,DAE.UNARY(DAE.UMINUS_ARR(t),e1),src);
// 0 = a+b
case (BackendDAE.EQUATION(e,DAE.BINARY(e1 as DAE.CREF(_,_),DAE.ADD(t),e2 as DAE.CREF(_,_)),src),false) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e1,DAE.UNARY(DAE.UMINUS(t),e2),src);
case (BackendDAE.EQUATION(e,DAE.BINARY(e1 as DAE.CREF(_,_),DAE.ADD_ARR(t),e2 as DAE.CREF(_,_)),src),false) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e1,DAE.UNARY(DAE.UMINUS_ARR(t),e2),src);
// 0 = a+b swap
case (BackendDAE.EQUATION(e,DAE.BINARY(e1 as DAE.CREF(_,_),DAE.ADD(t),e2 as DAE.CREF(_,_)),src),true) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e2,DAE.UNARY(DAE.UMINUS(t),e1),src);
case (BackendDAE.EQUATION(e,DAE.BINARY(e1 as DAE.CREF(_,_),DAE.ADD_ARR(t),e2 as DAE.CREF(_,_)),src),true) equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e2,DAE.UNARY(DAE.UMINUS_ARR(t),e1),src);
// a = -b
case (BackendDAE.EQUATION(e1 as DAE.CREF(_,_),e2 as DAE.UNARY(DAE.UMINUS(_),DAE.CREF(_,_)),src),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,e2,src);
case (BackendDAE.EQUATION(e1 as DAE.CREF(_,_),e2 as DAE.UNARY(DAE.UMINUS_ARR(_),DAE.CREF(_,_)),src),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,e2,src);
// -a = b => a = -b
case (BackendDAE.EQUATION(DAE.UNARY(DAE.UMINUS(t),e1 as DAE.CREF(_,_)),e2 as DAE.CREF(_,_),src),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,DAE.UNARY(DAE.UMINUS(t),e2),src);
case (BackendDAE.EQUATION(DAE.UNARY(DAE.UMINUS_ARR(t),e1 as DAE.CREF(_,_)),e2 as DAE.CREF(_,_),src),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,DAE.UNARY(DAE.UMINUS_ARR(t),e2),src);
// -b - a = 0 => a = -b
case (BackendDAE.EQUATION(DAE.BINARY(e2 as DAE.UNARY(DAE.UMINUS(_),DAE.CREF(_,_)),DAE.SUB(_),e1 as DAE.CREF(_,_)),e,src),false)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e1,e2,src);
case (BackendDAE.EQUATION(DAE.BINARY(e2 as DAE.UNARY(DAE.UMINUS_ARR(_),DAE.CREF(_,_)),DAE.SUB_ARR(_),e1 as DAE.CREF(_,_)),e,src),false)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e1,e2,src);
// -b - a = 0 => a = -b swap
case (BackendDAE.EQUATION(DAE.BINARY(DAE.UNARY(DAE.UMINUS(t),e2 as DAE.CREF(_,_)),DAE.SUB(_),e1 as DAE.CREF(_,_)),e,src),true)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e2,DAE.UNARY(DAE.UMINUS(t),e1),src);
case (BackendDAE.EQUATION(DAE.BINARY(DAE.UNARY(DAE.UMINUS_ARR(t),e2 as DAE.CREF(_,_)),DAE.SUB_ARR(_),e1 as DAE.CREF(_,_)),e,src),true)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e2,DAE.UNARY(DAE.UMINUS(t),e1),src);
// 0 = -b - a => a = -b
case (BackendDAE.EQUATION(e,DAE.BINARY(e2 as DAE.UNARY(DAE.UMINUS(_),DAE.CREF(_,_)),DAE.SUB(_),e1 as DAE.CREF(_,_)),src),false)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e1,e2,src);
case (BackendDAE.EQUATION(e,DAE.BINARY(e2 as DAE.UNARY(DAE.UMINUS_ARR(_),DAE.CREF(_,_)),DAE.SUB_ARR(_),e1 as DAE.CREF(_,_)),src),false)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e1,e2,src);
// 0 = -b - a => a = -b swap
case (BackendDAE.EQUATION(e,DAE.BINARY(DAE.UNARY(DAE.UMINUS(t),e2 as DAE.CREF(_,_)),DAE.SUB(_),e1 as DAE.CREF(_,_)),src),true)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e2,DAE.UNARY(DAE.UMINUS(t),e1),src);
case (BackendDAE.EQUATION(e,DAE.BINARY(DAE.UNARY(DAE.UMINUS_ARR(t),e2 as DAE.CREF(_,_)),DAE.SUB_ARR(_),e1 as DAE.CREF(_,_)),src),true)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
true = Expression.isZero(e);
then (e2,DAE.UNARY(DAE.UMINUS_ARR(t),e1),src);
// -a = -b
case (BackendDAE.EQUATION(DAE.UNARY(DAE.UMINUS(_),e1 as DAE.CREF(_,_)),DAE.UNARY(DAE.UMINUS(_),e2 as DAE.CREF(_,_)),src),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,e2,src);
case (BackendDAE.EQUATION(DAE.UNARY(DAE.UMINUS_ARR(_),e1 as DAE.CREF(_,_)),DAE.UNARY(DAE.UMINUS_ARR(_),e2 as DAE.CREF(_,_)),src),swap)
equation
true = RTOpts.eliminationLevel() > 1;
true = RTOpts.eliminationLevel() <> 3;
then (e1,e2,src);
// a = constant
case (BackendDAE.EQUATION(e1 as DAE.CREF(_,_),e,src),swap) equation
true = RTOpts.eliminationLevel() > 1;
true = Expression.isConst(e);
then (e1,e,src);
// -a = constant
case (BackendDAE.EQUATION(DAE.UNARY(DAE.UMINUS(t),e1 as DAE.CREF(_,_)),e,src),swap) equation
true = RTOpts.eliminationLevel() > 1;
true = Expression.isConst(e);
then (e1,DAE.UNARY(DAE.UMINUS(t),e),src);
case (BackendDAE.EQUATION(DAE.UNARY(DAE.UMINUS_ARR(t),e1 as DAE.CREF(_,_)),e,src),swap) equation
true = RTOpts.eliminationLevel() > 1;
true = Expression.isConst(e);
then (e1,DAE.UNARY(DAE.UMINUS_ARR(t),e),src);
end matchcontinue;
end simpleEquation;
protected function removeConstantEqns
"autor: Frenkel TUD 2010-12"
input BackendDAE.Variables inVars;
input list<BackendDAE.Equation> inEqns;
input BackendDAE.AliasVariables inAlias;
output BackendDAE.Variables outVars;
output list<BackendDAE.Equation> outEqns;
output BackendDAE.AliasVariables outAlias;
algorithm
(outVars,outEqns,outAlias) :=
matchcontinue (inVars,inEqns,inAlias)
local
BackendDAE.Variables vars,vars1;
BackendDAE.Var var,var2,var3;
DAE.ComponentRef cr;
DAE.Exp e;
BackendDAE.Equation eqn;
list<BackendDAE.Equation> rest,eqns;
BackendDAE.AliasVariables aliasvars,aliasvars1;
case (inVars,{},inAlias) then (inVars,{},inAlias);
// constant equations
case (inVars,BackendDAE.SOLVED_EQUATION(componentRef=cr,exp=e)::rest,inAlias)
equation
// check exp is const
true = Expression.isConst(e);
({var},_) = BackendVariable.getVar(cr,inVars);
// set kind to PARAM
// do not set varKind because of simulation results
//var1 = BackendVariable.setVarKind(var,BackendDAE.PARAM);
// add bindExp
var2 = BackendVariable.setBindExp(var,e);
// add bindValue if constant
var3 = setbindValue(e,var2);
// update vars
vars = BackendVariable.addVar(var3,inVars);
// next
(vars1,eqns,aliasvars) = removeConstantEqns(vars,rest,inAlias);
then
(vars1,eqns,aliasvars);
// alias vars
case (inVars,(eqn as BackendDAE.SOLVED_EQUATION(componentRef=cr,exp=e))::rest,inAlias)
equation
// test exp
removeConstantEqns1(e);
// get var
({var},_) = BackendVariable.getVar(cr,inVars);
// add bindExp
var2 = BackendVariable.setBindExp(var,e);
// add
aliasvars = BackendDAEUtil.addAliasVariables(inAlias,var2,e);
// next
(vars,eqns,aliasvars1) = removeConstantEqns(inVars,rest,aliasvars);
then
(vars,eqn::eqns,aliasvars1);
case (inVars,eqn::rest,inAlias)
equation
(vars,eqns,aliasvars) = removeConstantEqns(inVars,rest,inAlias);
then
(vars,eqn::eqns,aliasvars);
end matchcontinue;
end removeConstantEqns;
protected function removeConstantEqns1
"autor: Frenkel TUD 2010-12"
input DAE.Exp inExp;
algorithm
_ :=
match (inExp)
case (DAE.CREF(componentRef=_)) then ();
case (DAE.UNARY(exp = DAE.CREF(componentRef=_))) then ();
end match;
end removeConstantEqns1;
/*
* remove parameter equations
*/
public function removeParameterEqns
" function: removeParameterEqns
autor: Frenkel TUD 2010-12
Detect all equations with only one time depend variable and
check if it is a time independend variable. In case of time
independendce it change the variability from VARIABLE to Parameter
and add a bind expression."
input BackendDAE.BackendDAE inDlow;
input DAE.FunctionTree inFunctionTree;
input Option<BackendDAE.IncidenceMatrix> inM;
input Option<BackendDAE.IncidenceMatrixT> inMT;
output BackendDAE.BackendDAE outDlow;
output Option<BackendDAE.IncidenceMatrix> outM;
output Option<BackendDAE.IncidenceMatrixT> outMT;
algorithm
(outDlow,outM,outMT):=
matchcontinue (inDlow,inFunctionTree,inM,inMT)
local
BackendDAE.BackendDAE dlow,dlow1;
DAE.FunctionTree funcs;
Option<BackendDAE.IncidenceMatrix> om,omT;
BackendDAE.IncidenceMatrix m,m_1,m_2;
BackendDAE.IncidenceMatrixT mT,mT_1;
BackendDAE.Variables ordvars,knvars,exobj,ordvars1,knvars1,ordvars2,knvars2;
BackendDAE.AliasVariables av;
BackendDAE.EquationArray eqns,remeqns,inieqns,eqns1,eqns2;
array<BackendDAE.MultiDimEquation> arreqns;
array<DAE.Algorithm> algorithms;
BackendDAE.EventInfo einfo,einfo1;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.BinTree movedVars;
list<Integer> meqns;
case (dlow,funcs,NONE(),_)
equation
m = BackendDAEUtil.incidenceMatrix(dlow, BackendDAE.NORMAL());
mT = BackendDAEUtil.transposeMatrix(m);
(dlow,om,omT) = removeParameterEqns(dlow,funcs,SOME(m),SOME(mT));
then
(dlow,om,omT);
case (dlow as BackendDAE.DAE(ordvars,knvars,exobj,av,eqns,remeqns,inieqns,arreqns,algorithms,einfo,eoc),funcs,SOME(m),SOME(mT))
equation
// for now check interactive flag to avoid errors there
// false = RTOpts.debugFlag("");
// check equations
(m_1,(ordvars1,knvars1,eqns1,_,movedVars,meqns)) = traverseIncidenceMatrix(m,removeParameterEqnsFinder,(ordvars,knvars,eqns,mT,BackendDAE.emptyBintree,{}));
// move changed variables
(ordvars2,knvars2) = BackendVariable.moveVariables(ordvars1,knvars1,movedVars);
// remove changed eqns
eqns2 = BackendEquation.equationDelete(eqns1,meqns);
// update EventInfo
einfo1 = removeEqnsfromEventInfo(meqns,einfo);
// update IncidenceMatrix
dlow1 = BackendDAE.DAE(ordvars2,knvars2,exobj,av,eqns2,remeqns,inieqns,arreqns,algorithms,einfo1,eoc);
m_2 = BackendDAEUtil.incidenceMatrix(dlow1, BackendDAE.NORMAL());
// update transposed IncidenceMatrix
mT_1 = BackendDAEUtil.transposeMatrix(m_2);
then (dlow1,SOME(m_2),SOME(mT_1));
case (dlow,_,om,omT) then (dlow,om,omT);
end matchcontinue;
end removeParameterEqns;
protected function traverseIncidenceMatrix
" function: traverseIncidenceMatrix
autor: Frenkel TUD 2010-12"
replaceable type Type_a subtypeof Any;
input BackendDAE.IncidenceMatrix inM;
input FuncType func;
input Type_a inTypeA;
output BackendDAE.IncidenceMatrix outM;
output Type_a outTypeA;
partial function FuncType
input tuple<BackendDAE.IncidenceMatrixElement,Integer,BackendDAE.IncidenceMatrix,Type_a> inTpl;
output tuple<BackendDAE.IncidenceMatrixElement,BackendDAE.IncidenceMatrix,Type_a> outTpl;
end FuncType;
algorithm
(outM,outTypeA) := traverseIncidenceMatrix1(inM,func,1,arrayLength(inM),inTypeA);
end traverseIncidenceMatrix;
protected function traverseIncidenceMatrix1
" function: traverseIncidenceMatrix1
autor: Frenkel TUD 2010-12"
replaceable type Type_a subtypeof Any;
input BackendDAE.IncidenceMatrix inM;
input FuncType func;
input Integer pos "iterated 1..len";
input Integer len "length of array";
input Type_a inTypeA;
output BackendDAE.IncidenceMatrix outM;
output Type_a outTypeA;
partial function FuncType
input tuple<BackendDAE.IncidenceMatrixElement,Integer,BackendDAE.IncidenceMatrix,Type_a> inTpl;
output tuple<BackendDAE.IncidenceMatrixElement,BackendDAE.IncidenceMatrix,Type_a> outTpl;
end FuncType;
algorithm
(outM,outTypeA) := matchcontinue(inM,func,pos,len,inTypeA)
local
BackendDAE.IncidenceMatrix m,m1,m2;
BackendDAE.IncidenceMatrixElement newElt;
Type_a extArg,extArg1;
case(inM,func,pos,len,inTypeA) equation
true = pos > len;
then (inM,inTypeA);
case(inM,func,pos,len,inTypeA) equation
((newElt,m,extArg)) = func((inM[pos],pos,inM,inTypeA));
m1 = arrayUpdate(m,pos,newElt);
(m2,extArg1) = traverseIncidenceMatrix1(m1,func,pos+1,len,extArg);
then (m2,extArg1);
end matchcontinue;
end traverseIncidenceMatrix1;