/
BackendDAEOptimize.mo
9626 lines (9068 loc) · 393 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;
public import HashTable2;
//public import IndexReduction;
protected import BackendDAETransform;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVarTransform;
protected import BackendVariable;
protected import BaseHashTable;
protected import Builtin;
protected import Ceval;
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 Flags;
protected import Graph;
protected import Inline;
protected import List;
protected import Matching;
protected import System;
protected import Util;
protected import Values;
protected import ValuesUtil;
/*
* inline arrayeqns stuff
*/
public function inlineArrayEqnPast
"function inlineArrayEqnPast
autor: Frenkel TUD 2011-3"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
output Boolean outRunMatching;
protected
Option<BackendDAE.IncidenceMatrix> om,omT;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
algorithm
(outDAE,outRunMatching) := BackendDAEUtil.mapEqSystemAndFold(inDAE,inlineArrayEqn1,false);
end inlineArrayEqnPast;
public function inlineArrayEqn
"function: inlineArrayEqn
autor: Frenkel TUD 2011-3"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
(outDAE,_) := BackendDAEUtil.mapEqSystemAndFold(inDAE,inlineArrayEqn1,false);
end inlineArrayEqn;
public function inlineArrayEqnShared
"function: inlineArrayEqnShared"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
outDAE:=
match (inDAE)
local
BackendDAE.Variables ordvars,knvars,exobj,knvars1;
BackendDAE.AliasVariables aliasVars;
BackendDAE.EquationArray remeqns,inieqns,eqns1,inieqns1,remeqns1,eqns2;
array<DAE.Constraint> constrs;
DAE.FunctionTree funcTree;
BackendDAE.EventInfo einfo;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.SymbolicJacobians symjacs;
list<BackendDAE.WhenClause> whenClauseLst,whenClauseLst1;
list<BackendDAE.ZeroCrossing> zeroCrossingLst;
BackendDAE.BackendDAEType btp;
BackendDAE.EqSystems systs,systs1;
BackendDAE.Shared shared;
list<BackendDAE.Var> ordvarslst;
list<BackendDAE.Equation> eqnslst;
case (_) then inDAE;
case (BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,constrs,funcTree,BackendDAE.EVENT_INFO(whenClauseLst,zeroCrossingLst),eoc,btp,symjacs)))
equation
eqnslst = BackendDAEUtil.equationList(inieqns);
eqnslst = getScalarArrayEqns(eqnslst,{});
inieqns = BackendDAEUtil.listEquation(eqnslst);
eqnslst = BackendDAEUtil.equationList(remeqns);
eqnslst = getScalarArrayEqns(eqnslst,{});
remeqns = BackendDAEUtil.listEquation(eqnslst);
then BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,exobj,aliasVars,inieqns,remeqns,constrs,funcTree,BackendDAE.EVENT_INFO(whenClauseLst,zeroCrossingLst),eoc,btp,symjacs));
end match;
end inlineArrayEqnShared;
protected function inlineArrayEqn1
"function: inlineArrayEqn1
autor: Frenkel TUD 2011-5"
input BackendDAE.EqSystem isyst;
input tuple<BackendDAE.Shared,Boolean> sharedOptimized;
output BackendDAE.EqSystem osyst;
output tuple<BackendDAE.Shared,Boolean> osharedOptimized;
algorithm
(osyst,osharedOptimized) := matchcontinue (isyst,sharedOptimized)
local
Boolean b1,b2,b;
BackendDAE.Shared shared;
BackendDAE.EqSystem syst;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
list<BackendDAE.Equation> eqnslst;
Integer oldsize,newsize;
BackendDAE.Matching matching;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
case (syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns,m=m,mT=mT,matching=matching),(shared,_))
equation
eqnslst = BackendDAEUtil.equationList(eqns);
oldsize = listLength(eqnslst);
eqnslst = getScalarArrayEqns(eqnslst,{});
newsize = listLength(eqnslst);
true = intGt(newsize,oldsize);
eqns = BackendDAEUtil.listEquation(eqnslst);
then
(BackendDAE.EQSYSTEM(vars,eqns,NONE(),NONE(),BackendDAE.NO_MATCHING()),(shared,true));
else
then
(isyst,sharedOptimized);
end matchcontinue;
end inlineArrayEqn1;
protected function getScalarArrayEqns
input list<BackendDAE.Equation> iEqsLst;
input list<BackendDAE.Equation> iAcc;
output list<BackendDAE.Equation> oEqsLst;
algorithm
oEqsLst := match(iEqsLst,iAcc)
local
BackendDAE.Equation eqn;
list<BackendDAE.Equation> eqns,eqns1;
case ({},_) then listReverse(iAcc);
case (eqn::eqns,_)
equation
eqns1 = getScalarArrayEqns1(eqn,iAcc);
then
getScalarArrayEqns(eqns,eqns1);
end match;
end getScalarArrayEqns;
public function getScalarArrayEqns1"
Author: Frenkel TUD 2012-06"
input BackendDAE.Equation iEqn;
input list<BackendDAE.Equation> iAcc;
output list<BackendDAE.Equation> outEqsLst;
algorithm
outEqsLst :=
matchcontinue (iEqn,iAcc)
local
DAE.ElementSource source;
DAE.Exp e1,e2,e1_1,e2_1;
list<DAE.Exp> ea1,ea2;
case (BackendDAE.ARRAY_EQUATION(left=e1,right=e2,source=source),_)
equation
true = Expression.isArray(e1) or Expression.isMatrix(e1);
true = Expression.isArray(e2) or Expression.isMatrix(e2);
ea1 = Expression.flattenArrayExpToList(e1);
ea2 = Expression.flattenArrayExpToList(e2);
then
generateScalarArrayEqns2(ea1,ea2,source,iAcc);
case (BackendDAE.ARRAY_EQUATION(left=e1 as DAE.CREF(componentRef =_),right=e2,source=source),_)
equation
true = Expression.isArray(e2) or Expression.isMatrix(e2);
((e1_1,(_,_))) = BackendDAEUtil.extendArrExp((e1,(NONE(),false)));
ea1 = Expression.flattenArrayExpToList(e1_1);
ea2 = Expression.flattenArrayExpToList(e2);
then
generateScalarArrayEqns2(ea1,ea2,source,iAcc);
case (BackendDAE.ARRAY_EQUATION(left=e1,right=e2 as DAE.CREF(componentRef =_),source=source),_)
equation
true = Expression.isArray(e1) or Expression.isMatrix(e1);
((e2_1,(_,_))) = BackendDAEUtil.extendArrExp((e2,(NONE(),false)));
ea1 = Expression.flattenArrayExpToList(e1);
ea2 = Expression.flattenArrayExpToList(e2_1);
then
generateScalarArrayEqns2(ea1,ea2,source,iAcc);
case (BackendDAE.ARRAY_EQUATION(left=e1 as DAE.CREF(componentRef =_),right=e2 as DAE.CREF(componentRef =_),source=source),_)
equation
((e1_1,(_,_))) = BackendDAEUtil.extendArrExp((e1,(NONE(),false)));
((e2_1,(_,_))) = BackendDAEUtil.extendArrExp((e2,(NONE(),false)));
ea1 = Expression.flattenArrayExpToList(e1_1);
ea2 = Expression.flattenArrayExpToList(e2_1);
then
generateScalarArrayEqns2(ea1,ea2,source,iAcc);
case (_,_) then iEqn::iAcc;
end matchcontinue;
end getScalarArrayEqns1;
protected function generateScalarArrayEqns2"
Author: Frenkel TUD 2012-06"
input list<DAE.Exp> iE1lst;
input list<DAE.Exp> iE2lst;
input DAE.ElementSource source;
input list<BackendDAE.Equation> iAcc;
output list<BackendDAE.Equation> oEqns;
algorithm
oEqns := match(iE1lst,iE2lst,source,iAcc)
local
DAE.Exp e1,e2;
list<DAE.Exp> e1lst,e2lst;
case ({},{},_,_) then iAcc;
case (e1::e1lst,e2::e2lst,_,_)
then
generateScalarArrayEqns2(e1lst,e2lst,source,BackendDAE.EQUATION(e1,e2,source)::iAcc);
end match;
end generateScalarArrayEqns2;
/*
* inline functions stuff
*/
public function lateInlineFunction
"function lateInlineFunction"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
output Boolean outRunMatching;
algorithm
outDAE := Inline.inlineCalls({DAE.NORM_INLINE(),DAE.AFTER_INDEX_RED_INLINE()},inDAE);
outRunMatching := true;
end lateInlineFunction;
/*
* remove simply equations stuff
*/
public function removeSimpleEquationsFastPast
"function removeSimpleEquationsFastPast"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
output Boolean outRunMatching;
protected
BackendVarTransform.VariableReplacements repl,repl1;
algorithm
repl := BackendVarTransform.emptyReplacements();
(outDAE,(repl1,outRunMatching)) := BackendDAEUtil.mapEqSystemAndFold(inDAE,removeSimpleEquationsFast1,(repl,false));
outDAE := removeSimpleEquationsShared(outRunMatching,outDAE,repl1);
outDAE := BackendDAEUtil.mapEqSystem(outDAE,BackendDAEUtil.getIncidenceMatrixfromOptionForMapEqSystem);
// until remove simple equations does not update assignments and comps
end removeSimpleEquationsFastPast;
public function removeSimpleEquationsFast
"function: removeSimpleEquationsFast
autor: Frenkel TUD 2012-03
This function moves simple equations on the form a=b and a=const and a=f(not time)
in BackendDAE.BackendDAE to get speed up. The functions traverses the equation system
only once and does not jump back if a simple equation is found, hence not alle simple
equations will be detected."
input BackendDAE.BackendDAE dae;
output BackendDAE.BackendDAE odae;
protected
BackendVarTransform.VariableReplacements repl,repl1;
Boolean b;
algorithm
repl := BackendVarTransform.emptyReplacements();
(odae,(repl1,b)) := BackendDAEUtil.mapEqSystemAndFold(dae,removeSimpleEquationsFast1,(repl,false));
odae := removeSimpleEquationsShared(b,odae,repl1);
end removeSimpleEquationsFast;
protected function removeSimpleEquationsFast1
"function: removeSimpleEquationsFast1
autor: Frenkel TUD 2012-03
This function moves simple equations on the form a=b and a=const and a=f(not time)
in BackendDAE.BackendDAE to get speed up"
input BackendDAE.EqSystem isyst;
input tuple<BackendDAE.Shared,tuple<BackendVarTransform.VariableReplacements,Boolean>> sharedOptimized;
output BackendDAE.EqSystem osyst;
output tuple<BackendDAE.Shared,tuple<BackendVarTransform.VariableReplacements,Boolean>> osharedOptimized;
algorithm
(osyst,osharedOptimized):=
match (isyst,sharedOptimized)
local
DAE.FunctionTree funcs;
BackendVarTransform.VariableReplacements repl,repl_1;
BackendDAE.BinTree movedVars,movedAVars;
list<Integer> meqns,deqns;
Boolean b,b1;
BackendDAE.Shared shared;
BackendDAE.EqSystem syst;
HashTable2.HashTable derrepl,derrepl1;
Integer n;
case (syst,(shared,(repl,b1)))
equation
derrepl = HashTable2.emptyHashTable();
// check equations
n = BackendDAEUtil.equationArraySizeDAE(syst);
((syst,shared,repl_1,derrepl1,deqns,movedVars,movedAVars,meqns,b)) =
traverseEquations(1,n,removeSimpleEquationsFastFinder,
(syst,shared,repl,derrepl,{},BackendDAE.emptyBintree,BackendDAE.emptyBintree,{},false));
// replace der(x)=dx
(syst,shared) = replaceDerEquations(deqns,syst,shared,derrepl1);
// replace vars in arrayeqns and algorithms, move vars to knvars and aliasvars, remove eqns
(syst,shared) = removeSimpleEquations2(b,syst,shared,repl_1,movedVars,movedAVars,meqns);
then (syst,(shared,(repl_1,b or b1)));
end match;
end removeSimpleEquationsFast1;
protected function removeSimpleEquationsFastFinder
"autor: Frenkel TUD 2012-03"
input tuple<Integer,tuple<BackendDAE.EqSystem,BackendDAE.Shared,BackendVarTransform.VariableReplacements,HashTable2.HashTable,list<Integer>,BackendDAE.BinTree,BackendDAE.BinTree,list<Integer>,Boolean>> inTpl;
output tuple<BackendDAE.EqSystem,BackendDAE.Shared,BackendVarTransform.VariableReplacements,HashTable2.HashTable,list<Integer>,BackendDAE.BinTree,BackendDAE.BinTree,list<Integer>,Boolean> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
Integer pos,l,eqnType,pos_1;
BackendVarTransform.VariableReplacements repl,repl_1;
BackendDAE.BinTree mvars,mvars_1,mavars,mavars_1;
list<Integer> meqns,meqns1,deeqns,deeqns_1;
BackendDAE.Variables vars,vars1;
BackendDAE.EquationArray eqns,eqns1;
DAE.FunctionTree funcs;
Boolean b;
BackendDAE.EqSystem syst,syst1;
BackendDAE.Shared shared,shared1;
HashTable2.HashTable derrepl,derrepl_1;
list<BackendDAE.Var> varlst;
BackendDAE.Equation eqn;
case ((pos,(syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)))
equation
// get Vars in Eqn
pos_1 = pos-1;
eqn = BackendDAEUtil.equationNth(eqns,pos_1);
{eqn} = BackendVarTransform.replaceEquations({eqn}, repl);
vars1 = BackendEquation.equationVars(eqn,vars);
varlst = BackendDAEUtil.varList(vars1);
l = listLength(varlst);
((syst1,shared1,repl_1,derrepl_1,deeqns_1,mvars_1,mavars_1,meqns1,b)) = removeSimpleEquationsFastFinder1((l,pos,varlst,eqn,(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)));
then ((syst1,shared1,repl_1,derrepl_1,deeqns_1,mvars_1,mavars_1,meqns1,b));
case ((_,(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)))
then ((syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b));
end matchcontinue;
end removeSimpleEquationsFastFinder;
protected function removeSimpleEquationsFastFinder1
"autor: Frenkel TUD 2012-03"
input tuple<Integer,Integer,list<BackendDAE.Var>,BackendDAE.Equation,tuple<BackendDAE.EqSystem,BackendDAE.Shared,BackendVarTransform.VariableReplacements,HashTable2.HashTable,list<Integer>,BackendDAE.BinTree,BackendDAE.BinTree,list<Integer>,Boolean>> inTpl;
output tuple<BackendDAE.EqSystem,BackendDAE.Shared,BackendVarTransform.VariableReplacements,HashTable2.HashTable,list<Integer>,BackendDAE.BinTree,BackendDAE.BinTree,list<Integer>,Boolean> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
Integer pos,l,i,eqnType,pos_1;
BackendVarTransform.VariableReplacements repl,repl_1;
BackendDAE.BinTree mvars,mvars_1,mavars,mavars_1;
list<Integer> meqns,meqns1,vareqns,deeqns;
DAE.ComponentRef cr;
DAE.Exp exp,e1,e2;
Boolean b;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
HashTable2.HashTable derrepl;
list<BackendDAE.Var> varlst;
BackendDAE.Equation eqn;
case ((l,pos,_,BackendDAE.EQUATION(exp=e1,scalar=e2),(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)))
equation
true = intEq(l,0);
pos_1 = pos-1;
true = Expression.isConst(e1);
true = Expression.expEqual(e1,e2);
then ((syst,shared,repl,derrepl,deeqns,mvars,mavars,pos_1::meqns,b));
case ((l,pos,varlst,eqn,(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,_)))
equation
true = intLt(l,3);
true = intGt(l,0);
(cr,i,exp,syst,shared,mvars_1,mavars_1,eqnType) = simpleEquationPast(varlst,pos,eqn,syst,shared,mvars,mavars);
// replace equation if necesarry
(syst,shared,repl_1,derrepl,deeqns,meqns1) = replacementsInEqnsFast(eqnType,cr,i,exp,pos,repl,derrepl,deeqns,syst,shared,meqns);
then ((syst,shared,repl_1,derrepl,deeqns,mvars_1,mavars_1,meqns1,true));
case ((_,_,_,_,(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)))
then ((syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b));
end matchcontinue;
end removeSimpleEquationsFastFinder1;
protected function replacementsInEqnsFast
"function: replacementsInEqnsFast
author: Frenkel TUD 2012-03"
input Integer eqnType;
input DAE.ComponentRef cr;
input Integer i;
input DAE.Exp exp;
input Integer pos;
input BackendVarTransform.VariableReplacements repl;
input HashTable2.HashTable inDerrepl;
input list<Integer> inDeeqn;
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input list<Integer> inMeqns;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output BackendVarTransform.VariableReplacements outRepl;
output HashTable2.HashTable outDerrepl;
output list<Integer> outDeeqn;
output list<Integer> outMeqns;
algorithm
(osyst,oshared,outRepl,outDerrepl,outDeeqn,outMeqns):=
match (eqnType,cr,i,exp,pos,repl,inDerrepl,inDeeqn,isyst,ishared,inMeqns)
local
BackendDAE.Variables ordvars,ordvars1;
BackendDAE.EquationArray eqns;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
Integer pos_1;
list<Integer> meqns,deeqns;
BackendVarTransform.VariableReplacements repl_1;
BackendDAE.Var v;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared,shared1;
BackendDAE.Matching matching;
HashTable2.HashTable derrepl;
case (0,cr,i,exp,pos,repl,derrepl,deeqns,syst as BackendDAE.EQSYSTEM(orderedVars=ordvars,orderedEqs=eqns,m=m,mT=mT,matching=matching),shared,meqns)
equation
// remove var from vars
(ordvars1,v) = BackendVariable.removeVar(i,ordvars);
shared1 = BackendVariable.addKnVarDAE(v, shared);
pos_1 = pos - 1;
then (BackendDAE.EQSYSTEM(ordvars1,eqns,m,mT,matching),shared1,repl,derrepl,deeqns,pos_1::meqns);
case (1,cr,i,exp,pos,repl,derrepl,deeqns,syst as BackendDAE.EQSYSTEM(orderedVars=ordvars,orderedEqs=eqns,m=m,mT=mT,matching=matching),shared,meqns)
equation
// remove var from vars
(ordvars1,v) = BackendVariable.removeVar(i,ordvars);
// update Replacements
repl_1 = BackendVarTransform.addReplacement(repl, cr, exp);
pos_1 = pos - 1;
then (BackendDAE.EQSYSTEM(ordvars1,eqns,m,mT,matching),shared,repl_1,derrepl,deeqns,pos_1::meqns);
case (2,cr,i,exp,pos,repl,derrepl,deeqns,syst as BackendDAE.EQSYSTEM(mT=mT),shared,meqns)
equation
derrepl = BaseHashTable.add((cr,exp),derrepl);
pos_1 = pos - 1;
then (syst,shared,repl,derrepl,pos_1::deeqns,meqns);
end match;
end replacementsInEqnsFast;
protected function traverseEquations
" function: traverseEquations
autor: Frenkel TUD 2012-13"
replaceable type Type_a subtypeof Any;
input Integer inIndx;
input Integer inSysSize;
input FuncType inFunc;
input Type_a inTypeA;
output Type_a outTypeA;
partial function FuncType
input tuple<Integer,Type_a> inTpl;
output Type_a outTpl;
end FuncType;
algorithm
outTypeA :=
matchcontinue (inIndx,inSysSize,inFunc,inTypeA)
local
Integer e;
Type_a arg,arg1;
case (_,_,_,_)
equation
false = intGt(inIndx,inSysSize);
arg1 = inFunc((inIndx,inTypeA));
then traverseEquations(inIndx+1,inSysSize,inFunc,arg1);
case (_,_,_,_)
equation
true = intGt(inIndx,inSysSize);
then inTypeA;
end matchcontinue;
end traverseEquations;
public function removeSimpleEquationsPast
"function removeSimpleEquationsPast
autor: Frenkel TUD 2012-13"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
output Boolean outRunMatching;
protected
BackendVarTransform.VariableReplacements repl,repl1;
algorithm
repl := BackendVarTransform.emptyReplacements();
(outDAE,(repl1,outRunMatching)) := BackendDAEUtil.mapEqSystemAndFold(inDAE,removeSimpleEquationsPast1,(repl,false));
outDAE := removeSimpleEquationsShared(outRunMatching,outDAE,repl1);
outDAE := BackendDAEUtil.mapEqSystem(outDAE,BackendDAEUtil.getIncidenceMatrixfromOptionForMapEqSystem);
// until remove simple equations does not update assignments and comps
end removeSimpleEquationsPast;
protected function removeSimpleEquationsPast1
"function: removeSimpleEquationsPast1
autor: Frenkel TUD 2012-03
This function moves simple equations on the form a=b and a=const and a=f(not time)
in BackendDAE.BackendDAE to get speed up"
input BackendDAE.EqSystem isyst;
input tuple<BackendDAE.Shared,tuple<BackendVarTransform.VariableReplacements,Boolean>> sharedOptimized;
output BackendDAE.EqSystem osyst;
output tuple<BackendDAE.Shared,tuple<BackendVarTransform.VariableReplacements,Boolean>> osharedOptimized;
algorithm
(osyst,osharedOptimized):=
match (isyst,sharedOptimized)
local
BackendVarTransform.VariableReplacements repl,repl_1;
BackendDAE.BinTree movedVars,movedAVars;
list<Integer> meqns,deqns;
Boolean b,b1;
BackendDAE.Shared shared;
BackendDAE.EqSystem syst;
BackendDAE.StrongComponents comps;
HashTable2.HashTable derrepl,derrepl1;
case (syst as BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps)),(shared,(repl,b1)))
equation
derrepl = HashTable2.emptyHashTable();
// check equations
((syst,shared,repl_1,derrepl1,deqns,movedVars,movedAVars,meqns,b)) =
traverseComponents(comps,removeSimpleEquationsPastFinder,
(syst,shared,repl,derrepl,{},BackendDAE.emptyBintree,BackendDAE.emptyBintree,{},false));
// replace der(x)=dx
(syst,shared) = replaceDerEquations(deqns,syst,shared,derrepl1);
// replace vars in arrayeqns and algorithms, move vars to knvars and aliasvars, remove eqns
(syst,shared) = removeSimpleEquations2(b,syst,shared,repl_1,movedVars,movedAVars,meqns);
then (syst,(shared,(repl_1,b or b1)));
end match;
end removeSimpleEquationsPast1;
protected function replaceDerEquations
input list<Integer> inDeEqns;
input BackendDAE.EqSystem inSyst;
input BackendDAE.Shared inShared;
input HashTable2.HashTable inDerrepl;
output BackendDAE.EqSystem outSyst;
output BackendDAE.Shared outShared;
algorithm
(outSyst,outShared) :=
match (inDeEqns,inSyst,inShared,inDerrepl)
local
list<Integer> deeqns;
HashTable2.HashTable derrepl;
BackendDAE.EquationArray eqns,eqns1,ieqns,reqns;
array<DAE.Constraint> constrs;
list<BackendDAE.WhenClause> wclst,wclst1;
list<BackendDAE.ZeroCrossing> zcl;
DAE.FunctionTree funcTree;
BackendDAE.EventInfo einfo;
BackendDAE.Variables vars,knvars,eo;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.Matching matching;
BackendDAE.AliasVariables av;
BackendDAE.ExternalObjectClasses eoc;
BackendDAE.BackendDAEType bdaetype;
BackendDAE.SymbolicJacobians symjacs;
Integer n;
case ({},_,_,_) then (inSyst,inShared);
case (deeqns,BackendDAE.EQSYSTEM(vars,eqns,m,mT,matching),
BackendDAE.SHARED(knownVars=knvars,externalObjects=eo,aliasVars=av,initialEqs=ieqns,removedEqs=reqns,
constraints=constrs,functionTree=funcTree,eventInfo=BackendDAE.EVENT_INFO(wclst,zcl),extObjClasses=eoc,backendDAEType=bdaetype,symjacs=symjacs),derrepl)
equation
Debug.fcall(Flags.DUMP_DERREPL, BaseHashTable.dumpHashTable, derrepl);
n = BackendDAEUtil.equationSize(eqns);
deeqns = List.sort(deeqns,intGt);
(eqns1,wclst1) = replaceDerEquations1(deeqns,0,n,derrepl,eqns,wclst);
einfo = BackendDAE.EVENT_INFO(wclst1,zcl);
then
(BackendDAE.EQSYSTEM(vars,eqns1,m,mT,matching),BackendDAE.SHARED(knvars,eo,av,ieqns,reqns,constrs,funcTree,einfo,eoc,bdaetype,symjacs));
end match;
end replaceDerEquations;
protected function replaceDerEquations1
input list<Integer> inSortDeEqns;
input Integer inIndx;
input Integer EqnSize;
input HashTable2.HashTable inDerrepl;
input BackendDAE.EquationArray inEqns;
input list<BackendDAE.WhenClause> inEinfo;
output BackendDAE.EquationArray outEqns;
output list<BackendDAE.WhenClause> outEinfo;
algorithm
(outEqns,outEinfo) :=
matchcontinue (inSortDeEqns,inIndx,EqnSize,inDerrepl,inEqns,inEinfo)
local
list<Integer> deeqns;
HashTable2.HashTable derrepl;
Integer n,i,de;
BackendDAE.Equation eqn,eqn1;
BackendDAE.EquationArray eqns,eqns1,eqns2;
list<BackendDAE.WhenClause> wclst,wclst1,wclst2;
BackendDAE.EventInfo einfo;
case ({},_,_,_,eqns,wclst) then (eqns,wclst);
case (de::deeqns,i,n,derrepl,eqns,wclst)
equation
true = intLt(i,de);
eqn = BackendDAEUtil.equationNth(eqns,i);
(eqn1,wclst1,_) = BackendDAETransform.traverseBackendDAEExpsEqn(eqn, wclst, replaceDerEquationsFinder,derrepl);
eqns1 = BackendEquation.equationSetnth(eqns,i,eqn1);
(eqns2,wclst2) = replaceDerEquations1(de::deeqns,i+1,n,derrepl,eqns1,wclst1);
then
(eqns2,wclst2);
case (de::deeqns,i,n,derrepl,eqns,wclst)
equation
false = intLt(i,de);
(eqns2,wclst2) = replaceDerEquations1(deeqns,i+1,n,derrepl,eqns,wclst);
then
(eqns2,wclst2);
end matchcontinue;
end replaceDerEquations1;
protected function replaceDerEquationsFinder "function: replaceDerEquationsFinder
author: Frenkel TUD 2012-03
helper for replaceDerEquationsFinder"
input tuple<DAE.Exp, HashTable2.HashTable> inTpl;
output tuple<DAE.Exp, HashTable2.HashTable> outTpl;
algorithm
outTpl := match(inTpl)
local
HashTable2.HashTable r;
DAE.Exp e,e1;
case((e,r))
equation
((e1,_)) = Expression.traverseExp(e, replaceDerEquationsFinder1, r);
then
((e1,r));
end match;
end replaceDerEquationsFinder;
public function replaceDerEquationsFinder1 "
Author: Frenkel TUD 2012-3
helper for replaceDerEquationsFinder"
input tuple<DAE.Exp, HashTable2.HashTable> inExp;
output tuple<DAE.Exp, HashTable2.HashTable> outExp;
algorithm
outExp := matchcontinue(inExp)
local
HashTable2.HashTable r;
DAE.Exp e,de;
DAE.ComponentRef dcr,cr;
case ((DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}),r))
equation
de = BaseHashTable.get(cr,r);
then
((de,r));
case(inExp) then inExp;
end matchcontinue;
end replaceDerEquationsFinder1;
protected function removeSimpleEquationsPastFinder
"autor: Frenkel TUD 2012-03"
input tuple<Integer,list<Integer>,tuple<BackendDAE.EqSystem,BackendDAE.Shared,BackendVarTransform.VariableReplacements,HashTable2.HashTable,list<Integer>,BackendDAE.BinTree,BackendDAE.BinTree,list<Integer>,Boolean>> inTpl;
output tuple<list<Integer>,tuple<BackendDAE.EqSystem,BackendDAE.Shared,BackendVarTransform.VariableReplacements,HashTable2.HashTable,list<Integer>,BackendDAE.BinTree,BackendDAE.BinTree,list<Integer>,Boolean>> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
Integer pos,l,eqnType,pos_1;
BackendVarTransform.VariableReplacements repl,repl_1;
BackendDAE.BinTree mvars,mvars_1,mavars,mavars_1;
list<Integer> meqns,meqns1,vareqns,deeqns,deeqns_1,comp;
BackendDAE.Variables vars,vars1;
BackendDAE.EquationArray eqns,eqns1;
DAE.FunctionTree funcs;
Boolean b;
BackendDAE.EqSystem syst,syst1;
BackendDAE.Shared shared,shared1;
HashTable2.HashTable derrepl,derrepl_1;
list<BackendDAE.Var> varlst;
BackendDAE.Equation eqn;
case ((pos,comp,(syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)))
equation
// get Vars in Eqn
pos_1 = pos-1;
eqn = BackendDAEUtil.equationNth(eqns,pos_1);
{eqn} = BackendVarTransform.replaceEquations({eqn}, repl);
vars1 = BackendEquation.equationVars(eqn,vars);
varlst = BackendDAEUtil.varList(vars1);
l = listLength(varlst);
((vareqns,(syst1,shared1,repl_1,derrepl_1,deeqns_1,mvars_1,mavars_1,meqns1,b))) = removeSimpleEquationsPastFinder1((l,pos,varlst,comp,eqn,(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)));
then ((vareqns,(syst1,shared1,repl_1,derrepl_1,deeqns_1,mvars_1,mavars_1,meqns1,b)));
case ((_,_,(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)))
then (({},(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)));
end matchcontinue;
end removeSimpleEquationsPastFinder;
protected function removeSimpleEquationsPastFinder1
"autor: Frenkel TUD 2012-03"
input tuple<Integer,Integer,list<BackendDAE.Var>,list<Integer>,BackendDAE.Equation,tuple<BackendDAE.EqSystem,BackendDAE.Shared,BackendVarTransform.VariableReplacements,HashTable2.HashTable,list<Integer>,BackendDAE.BinTree,BackendDAE.BinTree,list<Integer>,Boolean>> inTpl;
output tuple<list<Integer>,tuple<BackendDAE.EqSystem,BackendDAE.Shared,BackendVarTransform.VariableReplacements,HashTable2.HashTable,list<Integer>,BackendDAE.BinTree,BackendDAE.BinTree,list<Integer>,Boolean>> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
Integer pos,l,i,eqnType,pos_1;
BackendVarTransform.VariableReplacements repl,repl_1;
BackendDAE.BinTree mvars,mvars_1,mavars,mavars_1;
list<Integer> meqns,meqns1,vareqns,deeqns,comp;
DAE.ComponentRef cr;
DAE.Exp exp,e1,e2;
DAE.FunctionTree funcs;
Boolean b;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
HashTable2.HashTable derrepl;
list<BackendDAE.Var> varlst;
BackendDAE.Equation eqn;
case ((l,pos,_,_,BackendDAE.EQUATION(exp=e1,scalar=e2),(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)))
equation
true = intEq(l,0);
pos_1 = pos-1;
true = Expression.isConst(e1);
true = Expression.expEqual(e1,e2);
then (({},(syst,shared,repl,derrepl,deeqns,mvars,mavars,pos_1::meqns,b)));
case ((l,pos,varlst,comp,eqn,(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,_)))
equation
true = intLt(l,3);
true = intGt(l,0);
(cr,i,exp,syst,shared,mvars_1,mavars_1,eqnType) = simpleEquationPast(varlst,pos,eqn,syst,shared,mvars,mavars);
// replace equation if necesarry
(vareqns,syst,shared,repl_1,derrepl,deeqns,meqns1) = replacementsInEqnsPast(eqnType,cr,i,exp,pos,comp,repl,derrepl,deeqns,syst,shared,meqns);
then ((vareqns,(syst,shared,repl_1,derrepl,deeqns,mvars_1,mavars_1,meqns1,true)));
case ((_,_,_,_,_,(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)))
then (({},(syst,shared,repl,derrepl,deeqns,mvars,mavars,meqns,b)));
end matchcontinue;
end removeSimpleEquationsPastFinder1;
protected function simpleEquationPast
" function: simpleEquationPast
autor: Frenkel TUD 2012-03"
input list<BackendDAE.Var> elem;
input Integer inPos;
input BackendDAE.Equation inEqn;
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input BackendDAE.BinTree mvars;
input BackendDAE.BinTree mavars;
output DAE.ComponentRef outCr;
output Integer outPos;
output DAE.Exp outExp;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output BackendDAE.BinTree outMvars;
output BackendDAE.BinTree outMavars;
output Integer eqnType;
algorithm
(outCr,outPos,outExp,osyst,oshared,outMvars,outMavars,eqnType) :=
matchcontinue(elem,inPos,inEqn,isyst,ishared,mvars,mavars)
local
DAE.ComponentRef cr,cr2;
Integer i,j,pos,pos_1,k,eqTy;
DAE.Exp es,cre,e1,e2;
BackendDAE.BinTree newvars,newvars1;
BackendDAE.Variables vars,knvars;
BackendDAE.Var var,var2,var3;
BackendDAE.EquationArray eqns;
BackendDAE.Equation eqn;
Boolean negate;
DAE.ElementSource source;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
// a = const
// wbraun:
// speacial case for Jacobains, since there are all known variablen
// time depending input variables
case ({var},pos,BackendDAE.EQUATION(exp=e1,scalar=e2,source=source),syst as BackendDAE.EQSYSTEM(orderedVars=vars),shared as BackendDAE.SHARED(backendDAEType = BackendDAE.JACOBIAN()),mvars,mavars)
equation
// no State
false = BackendVariable.isStateorStateDerVar(var);
false = BackendVariable.varHasUncertainValueRefine(var);
// variable time not there
knvars = BackendVariable.daeKnVars(shared);
((_,(false,_,_,_,_))) = Expression.traverseExpTopDown(e1, traversingTimeEqnsFinder, (false,vars,knvars,true,false));
((_,(false,_,_,_,_))) = Expression.traverseExpTopDown(e2, traversingTimeEqnsFinder, (false,vars,knvars,true,false));
// try to solve the equation
cr = BackendVariable.varCref(var);
cre = Expression.crefExp(cr);
(es,{}) = ExpressionSolve.solve(e1,e2,cre);
source = DAEUtil.addSymbolicTransformation(source,DAE.SOLVE(cr,e1,e2,es,{}));
// constant or alias
(syst,shared,newvars,newvars1,eqTy) = constOrAlias(var,cr,es,syst,shared,mvars,mavars,DAEUtil.getSymbolicTransformations(source));
(_,i::_) = BackendVariable.getVar(cr, vars);
then (cr,i,es,syst,shared,newvars,newvars1,eqTy);
// a = const
case ({var},pos,BackendDAE.EQUATION(exp=e1,scalar=e2,source=source),syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,mvars,mavars)
equation
// no State
false = BackendVariable.isStateorStateDerVar(var);
false = BackendVariable.varHasUncertainValueRefine(var);
// variable time not there
knvars = BackendVariable.daeKnVars(shared);
((_,(false,_,_,_,_))) = Expression.traverseExpTopDown(e1, traversingTimeEqnsFinder, (false,vars,knvars,false,false));
((_,(false,_,_,_,_))) = Expression.traverseExpTopDown(e2, traversingTimeEqnsFinder, (false,vars,knvars,false,false));
// try to solve the equation
cr = BackendVariable.varCref(var);
cre = Expression.crefExp(cr);
(es,{}) = ExpressionSolve.solve(e1,e2,cre);
source = DAEUtil.addSymbolicTransformation(source,DAE.SOLVE(cr,e1,e2,es,{}));
// constant or alias
(syst,shared,newvars,newvars1,eqTy) = constOrAlias(var,cr,es,syst,shared,mvars,mavars,DAEUtil.getSymbolicTransformations(source));
(_,i::_) = BackendVariable.getVar(cr, vars);
then (cr,i,es,syst,shared,newvars,newvars1,eqTy);
// a = der(b)
// a is a dummy_der_state
case ({var,var2},pos,eqn,syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,mvars,mavars)
equation
(cr2,cr,e2,es,negate) = BackendEquation.derivativeEquation(eqn);
// select candidate
((var::_),(k::_)) = BackendVariable.getVar(cr,vars);
((var2::_),(j::_)) = BackendVariable.getVar(cr2,vars);
true = BackendVariable.isDummyDerVar(var);
// because this is a self genererated var we need not check for uncertainities
//false = BackendVariable.varHasUncertainValueRefine(var);
(syst,shared,newvars) = selectAlias2(cr,cr2,var,var2,e2,es,syst,shared,mavars,negate,BackendEquation.equationSource(eqn));
then (cr,k,es,syst,shared,mvars,newvars,1);
// a = der(b)
// a is not a state
/* Frenkel TUD: -because we have no way to store the min,max,nominval,start attribute for der(state) variables we cannot replace
this simple equations. */
/*case ({var,var2},pos,eqn,syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,mvars,mavars)
equation
(cr2,cr,e2,es,negate) = BackendEquation.derivativeEquation(eqn);
// select candidate
((var::_),(k::_)) = BackendVariable.getVar(cr,vars);
((var2::_),(j::_)) = BackendVariable.getVar(cr2,vars);
false = BackendVariable.varHasUncertainValueRefine(var);
replaceableAlias(var);
(syst,shared,newvars) = selectAlias2(cr,cr2,var,var2,e2,es,syst,shared,mavars,negate,,BackendEquation.equationSource(eqn));
then (cr,k,es,syst,shared,mvars,newvars,1);
*/
// a = der(b)
case ({var,var2},pos,eqn,syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,mvars,mavars)
equation
(cr,_,es,_,negate) = BackendEquation.derivativeEquation(eqn);
// select candidate
((var::_),(k::_)) = BackendVariable.getVar(cr,vars);
false = BackendVariable.varHasUncertainValueRefine(var);
then (cr,k,es,syst,shared,mvars,mavars,2);
// a = b
case ({var,var2},pos,eqn as BackendDAE.EQUATION(source=source),syst as BackendDAE.EQSYSTEM(orderedEqs=eqns),shared,mvars,mavars)
equation
(cr,cr2,e1,e2,negate) = BackendEquation.aliasEquation(eqn);
// select candidate
(cr,es,k,syst,shared,newvars) = selectAlias(cr,cr2,e1,e2,syst,shared,mavars,negate,source);
then (cr,k,es,syst,shared,mvars,newvars,1);
case ({var,var2},pos,BackendDAE.EQUATION(exp=e1,scalar=e2,source=source),syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,mvars,mavars)
equation
cr = BackendVariable.varCref(var);
cre = Expression.crefExp(cr);
(es,{}) = ExpressionSolve.solve(e1,e2,cre);
(_,i::_) = BackendVariable.getVar(cr, vars);
(cr,k,es,syst,shared,newvars,newvars1,eqTy)= simpleEquation1(BackendDAE.EQUATION(cre,es,source),var,i,cr,es,source,syst,shared,mvars,mavars);
then (cr,k,es,syst,shared,newvars,newvars1,eqTy);
case ({var2,var},pos,BackendDAE.EQUATION(exp=e1,scalar=e2,source=source),syst as BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),shared,mvars,mavars)
equation
cr = BackendVariable.varCref(var);
cre = Expression.crefExp(cr);
(es,{}) = ExpressionSolve.solve(e1,e2,cre);
(_,j::_) = BackendVariable.getVar(cr, vars);
(cr,k,es,syst,shared,newvars,newvars1,eqTy)= simpleEquation1(BackendDAE.EQUATION(cre,es,source),var,j,cr,es,source,syst,shared,mvars,mavars);
then (cr,k,es,syst,shared,newvars,newvars1,eqTy);
end matchcontinue;
end simpleEquationPast;
protected function replacementsInEqnsPast
"function: replacementsInEqnsPast
author: Frenkel TUD 2012-03"
input Integer eqnType;
input DAE.ComponentRef cr;
input Integer i;
input DAE.Exp exp;
input Integer pos;
input list<Integer> inComp;
input BackendVarTransform.VariableReplacements repl;
input HashTable2.HashTable inDerrepl;
input list<Integer> inDeeqn;
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input list<Integer> inMeqns;
output list<Integer> outVareqns;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output BackendVarTransform.VariableReplacements outRepl;
output HashTable2.HashTable outDerrepl;
output list<Integer> outDeeqn;
output list<Integer> outMeqns;
algorithm
(outVareqns,osyst,oshared,outRepl,outDerrepl,outDeeqn,outMeqns):=
match (eqnType,cr,i,exp,pos,inComp,repl,inDerrepl,inDeeqn,isyst,ishared,inMeqns)
local
BackendDAE.Variables ordvars,ordvars1;
BackendDAE.EquationArray eqns;
Option<BackendDAE.IncidenceMatrix> m;
BackendDAE.IncidenceMatrixT mT;
Integer pos_1;
list<Integer> vareqns,vareqns1,vareqns2,meqns,deeqns,comp;
BackendVarTransform.VariableReplacements repl_1;
BackendDAE.Var v;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared,shared1;
BackendDAE.Matching matching;
HashTable2.HashTable derrepl;
case (0,cr,i,exp,pos,_,repl,derrepl,deeqns,syst as BackendDAE.EQSYSTEM(orderedVars=ordvars,orderedEqs=eqns,m=m,mT=SOME(mT),matching=matching),shared,meqns)
equation
// equations of var
vareqns = mT[i];
// remove var from vars
(ordvars1,v) = BackendVariable.removeVar(i,ordvars);
shared1 = BackendVariable.addKnVarDAE(v, shared);
pos_1 = pos - 1;
then (vareqns,BackendDAE.EQSYSTEM(ordvars1,eqns,m,SOME(mT),matching),shared1,repl,derrepl,deeqns,pos_1::meqns);
case (1,cr,i,exp,pos,_,repl,derrepl,deeqns,syst as BackendDAE.EQSYSTEM(orderedVars=ordvars,orderedEqs=eqns,m=m,mT=SOME(mT),matching=matching),shared,meqns)
equation
// equations of var
vareqns = mT[i];
// remove var from vars
(ordvars1,v) = BackendVariable.removeVar(i,ordvars);
// update Replacements
repl_1 = BackendVarTransform.addReplacement(repl, cr, exp);
pos_1 = pos - 1;
then (vareqns,BackendDAE.EQSYSTEM(ordvars1,eqns,m,SOME(mT),matching),shared,repl_1,derrepl,deeqns,pos_1::meqns);
case (2,cr,i,exp,pos,comp,repl,derrepl,deeqns,syst as BackendDAE.EQSYSTEM(mT=SOME(mT)),shared,meqns)
equation
// equations of var
vareqns = mT[i];
vareqns1 = List.removeOnTrue(0, intGt, vareqns);
pos_1 = pos - 1;
vareqns1 = List.removeOnTrue(pos, intEq, vareqns1);
derrepl = BaseHashTable.add((cr,exp),derrepl);
vareqns2 = List.intersectionOnTrue(vareqns1, comp, intEq);
// replace der(a)=b in vareqns
(syst,shared) = replacementsInEqns2(vareqns1,exp,cr,syst,shared);
// update IncidenceMatrix
syst= BackendDAEUtil.updateIncidenceMatrix(syst,shared,vareqns2);
then (vareqns1,syst,shared,repl,derrepl,pos_1::deeqns,meqns);
end match;
end replacementsInEqnsPast;
protected function traverseComponents
" function: traverseComponents
autor: Frenkel TUD 2010-12"
replaceable type Type_a subtypeof Any;