/
HpcOmEqSystems.mo
2070 lines (1922 loc) · 85.7 KB
/
HpcOmEqSystems.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 HpcOmEqSystems
" file: HpcOmEqSystems.mo
package: HpcOmEqSystems
description: HpcOmEqSystems contains the logic to manipulate systems of equations for the parallel simulation.
RCS: $Id: HpcOmEqSystems.mo 15486 2013-05-24 11:12:35Z $
"
// public imports
public import BackendDAE;
public import DAE;
// protected imports
protected import BackendDump;
protected import BackendEquation;
protected import BackendDAEEXT;
protected import BackendDAEUtil;
protected import BackendDAETransform;
protected import BackendVariable;
protected import BackendVarTransform;
protected import ComponentReference;
protected import Debug;
protected import Expression;
protected import Flags;
protected import GraphML;
protected import HpcOmTaskGraph;
protected import List;
protected import Matching;
protected import Util;
//--------------------------------------------------//
// start functions for handling linearTornSystems from here
//-------------------------------------------------//
public function traverseEqSystemsWithIndex "traverse alle EqSystems of the BackendDAE and hold an index of the current torn system.
author:Waurich TUD 2013-10"
input Integer eqSysIdx;
input Integer tornSysIdxIn;
input BackendDAE.BackendDAE daeIn;
output BackendDAE.BackendDAE daeOut;
algorithm
daeOut := matchcontinue(eqSysIdx,tornSysIdxIn,daeIn)
local
Integer tornSysIdx;
BackendDAE.BackendDAE daeTmp;
BackendDAE.EqSystem eqSyst;
BackendDAE.EqSystems eqSysts;
BackendDAE.Shared shared;
case(_,_,BackendDAE.DAE(eqs=eqSysts, shared=shared))
equation
true = listLength(eqSysts) >= eqSysIdx;
eqSyst = listGet(eqSysts,eqSysIdx);
(eqSyst,shared,tornSysIdx) = reduceLinearTornSystem(eqSyst,shared,tornSysIdxIn);
eqSysts = List.replaceAt(eqSyst,eqSysIdx-1,eqSysts);
daeTmp = BackendDAE.DAE(eqSysts,shared);
daeTmp = traverseEqSystemsWithIndex(eqSysIdx+1,tornSysIdx,daeTmp);
then
daeTmp;
case(_,_,BackendDAE.DAE(eqs=eqSysts, shared=shared))
equation
true = listLength(eqSysts) < eqSysIdx;
then
daeIn;
end matchcontinue;
end traverseEqSystemsWithIndex;
public function reduceLinearTornSystem "checks the EqSystem for tornSystems in order to dissassemble them into various SingleEquation and a reduced EquationSystem.
This is useful in order to reduce the execution costs of the equationsystem and generate a bunch of parallel singleEquations. use +d=doLienarTearing,partlintornsystem to activate it.
Remark: this is still under development
author:Waurich TUD 2013-09"
input BackendDAE.EqSystem systIn;
input BackendDAE.Shared sharedIn;
input Integer tornSysIdxIn;
output BackendDAE.EqSystem systOut;
output BackendDAE.Shared sharedOut;
output Integer tornSysIdxOut;
algorithm
(systOut, sharedOut, tornSysIdxOut) := matchcontinue(systIn,sharedIn,tornSysIdxIn)
local
Integer tornSysIdx;
array<Integer> ass1, ass2;
BackendDAE.EqSystem systTmp;
BackendDAE.EquationArray eqs, eqsTmp;
BackendDAE.Matching matching;
BackendDAE.Shared sharedTmp;
BackendDAE.StrongComponents allComps, compsTmp;
BackendDAE.Variables vars, varsTmp;
case(_,_,_)
equation
BackendDAE.EQSYSTEM(matching = BackendDAE.MATCHING(ass1=ass1, ass2=ass2, comps= allComps)) = systIn;
//BackendDump.dumpEqSystem(systIn,"original system");
(systTmp,tornSysIdx) = reduceLinearTornSystem1(1, allComps, ass1, ass2, systIn,sharedIn,tornSysIdxIn);
//BackendDump.dumpEqSystem(systTmp,"new system");
sharedTmp = sharedIn;
then
(systTmp, sharedTmp, tornSysIdx);
else
equation
print("reduceLinearTornSystem failed!");
then
fail();
end matchcontinue;
end reduceLinearTornSystem;
protected function reduceLinearTornSystem1 "traverses all StrongComponents for tornSystems, reduces them and rebuilds the BLT, the matching and the info about vars and equations
author: Waurich TUD 2013-09"
input Integer compIdx;
input BackendDAE.StrongComponents compsIn;
input array<Integer> ass1;
input array<Integer> ass2;
input BackendDAE.EqSystem systIn;
input BackendDAE.Shared sharedIn;
input Integer tornSysIdxIn;
output BackendDAE.EqSystem systOut;
output Integer tornSysIdxOut;
algorithm
(systOut,tornSysIdxOut) := matchcontinue(compIdx,compsIn,ass1,ass2,systIn,sharedIn,tornSysIdxIn)
local
Integer numNewSingleEqs, tornSysIdx;
Boolean linear;
array<Integer> ass1New, ass2New, ass1All, ass2All, ass1Other, ass2Other;
list<Integer> tvarIdcs;
list<Integer> resEqIdcs;
list<tuple<Integer,list<Integer>>> otherEqnVarTpl;
BackendDAE.EqSystem systTmp;
BackendDAE.EquationArray eqs;
BackendDAE.Matching matching, matchingNew, matchingOther;
BackendDAE.Shared sharedTmp;
BackendDAE.StateSets stateSets;
BackendDAE.StrongComponent comp;
BackendDAE.StrongComponents compsNew, compsTmp, otherComps;
BackendDAE.Variables vars;
list<BackendDAE.Equation> eqLst, eqsNew, eqsOld, resEqs;
list<BackendDAE.Var> varLst, varsNew, varsOld, tvars;
case(_,_,_,_,_,_,_)
equation
// completed
true = listLength(compsIn) < compIdx;
//print("finished at:"+&intString(compIdx)+&"\n");
then
(systIn,tornSysIdxIn);
case(_,_,_,_,_,_,_)
equation
// strongComponent is a linear tornSystem
true = listLength(compsIn) >= compIdx;
comp = listGet(compsIn,compIdx);
BackendDAE.TORNSYSTEM(tearingvars = tvarIdcs, residualequations = resEqIdcs, otherEqnVarTpl = otherEqnVarTpl, linear = linear) = comp;
true = linear;
Debug.fcall(Flags.HPCOM_DUMP,print,"handle linear torn systems of size: "+&intString(listLength(tvarIdcs)+listLength(otherEqnVarTpl))+&"\n");
Debug.fcall2(Flags.HPCOM_DUMP,dumpEquationSystemGraphML,(systIn,tornSysIdxIn),comp);
//print("handle tornsystem with compnumber:"+&intString(compIdx)+&"\n");
//BackendDump.dumpEqSystem(systIn,"the original system");
// build the new components, the new variables and the new equations
(varsNew,eqsNew,tvars,resEqs,matchingNew) = reduceLinearTornSystem2(systIn,sharedIn,tvarIdcs,resEqIdcs,otherEqnVarTpl,tornSysIdxIn);
BackendDAE.MATCHING(ass1=ass1New, ass2=ass2New, comps=compsNew) = matchingNew;
// add the new vars and equations to the original EqSystem
BackendDAE.EQSYSTEM(orderedVars = vars, orderedEqs = eqs, stateSets = stateSets) = systIn;
varsOld = BackendVariable.varList(vars);
eqsOld = BackendEquation.equationList(eqs);
varLst = listAppend(varsOld,varsNew);
eqLst = listAppend(eqsOld, eqsNew);
eqLst = List.fold2(List.intRange(listLength(resEqIdcs)),replaceAtPositionFromList,resEqs,resEqIdcs,eqLst); // replaces the old residualEquations with the new ones
vars = BackendVariable.listVar1(varLst); // !!! BackendVariable.listVar outputs the reversed order therefore listVar1
eqs = BackendEquation.listEquation(eqLst);
Debug.fcall(Flags.HPCOM_DUMP,print,"number of equations added: "+&intString(listLength(eqLst))+&" and the size of the linear torn system: "+&intString(listLength(tvarIdcs))+&"\n");
//print("new systemsize:"+&intString(listLength(varLst))+&" vars. and "+&intString(listLength(eqLst))+&" eqs\n");
// build the matching
ass1All = arrayCreate(listLength(varLst),-1);
ass2All = arrayCreate(listLength(varLst),-1); // actually has to be listLength(eqLst), but there is stille the probelm taht ass1 and ass2 have the same size
ass1All = Util.arrayCopy(ass1,ass1All); // the comps before and after the tornsystem
ass2All = Util.arrayCopy(ass2,ass2All);
((ass1All, ass2All)) = List.fold2(List.intRange(listLength(tvarIdcs)),updateResidualMatching,tvarIdcs,resEqIdcs,(ass1All,ass2All)); // sets matching info for the tearingVars and residuals
// get the otherComps and and update the matching for the othercomps
matchingOther = getOtherComps(otherEqnVarTpl,ass1All,ass2All);
BackendDAE.MATCHING(ass1=ass1Other, ass2=ass2Other, comps=otherComps) = matchingOther;
// insert the new components into the BLT instead of the TornSystem, append the updated blocks for the other equations, update matching for the new equations
numNewSingleEqs = listLength(compsNew)-listLength(tvarIdcs);
//print("num of new comps:"+&intString(numNewSingleEqs)+&"\n");
//BackendDump.dumpComponents(compsNew);
compsNew = listAppend(compsNew, otherComps);
compsTmp = List.replaceAtWithList(compsNew,compIdx-1,compsIn);
((ass1All,ass2All)) = List.fold2(List.intRange(arrayLength(ass1New)),updateMatching,(listLength(eqsOld),listLength(varsOld)),(ass1New,ass2New),(ass1All,ass2All));
matching = BackendDAE.MATCHING(ass1All, ass2All, compsTmp);
//build new EqSystem
systTmp = BackendDAE.EQSYSTEM(vars,eqs,NONE(),NONE(),matching,stateSets);
(systTmp,_,_) = BackendDAEUtil.getIncidenceMatrix(systTmp, BackendDAE.NORMAL(),NONE());
//BackendDump.dumpEqSystem(systTmp,"the whole new system");
(systTmp,tornSysIdx) = reduceLinearTornSystem1(compIdx+1+numNewSingleEqs,compsTmp,ass1All,ass2All,systTmp,sharedIn,tornSysIdxIn+1);
then
(systTmp,tornSysIdx);
else
// go to next StrongComponent
equation
//print("no torn system in comp:"+&intString(compIdx)+&"\n");
(systTmp,tornSysIdx) = reduceLinearTornSystem1(compIdx+1,compsIn,ass1,ass2,systIn,sharedIn,tornSysIdxIn);
then
(systTmp,tornSysIdx);
end matchcontinue;
end reduceLinearTornSystem1;
protected function reduceLinearTornSystem2 " builds from a torn system various linear equation systems that can be computed in parallel.
author: Waurich TUD 2013-07"
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input list<Integer> tearingVars;
input list<Integer> residualEqs;
input list<tuple<Integer, list<Integer>>> otherEqsVarTpl;
input Integer tornSysIdx;
output list<BackendDAE.Var> varsNewOut;
output list<BackendDAE.Equation> eqsNewOut;
output list<BackendDAE.Var> tVarsOut;
output list<BackendDAE.Equation> resEqsOut;
output BackendDAE.Matching matchingOut;
protected
Boolean isSingleEq;
array<Integer> ass1New, ass2New;
Integer size, otherEqSize, compSize;
list<Integer> otherEqnsInts, otherVarsInts, tVarRange, rEqIdx;
list<list<Integer>> otherVarsIntsLst;
BackendDAE.EqSystem systNew;
BackendDAE.EquationArray eqns, oeqns, hs0Eqs;
BackendDAE.Matching matchingNew;
BackendDAE.StrongComponent rComp;
BackendDAE.StrongComponents comps, compsNew, oComps;
BackendDAE.Variables vars, kv, diffVars, ovars, dVars;
BackendVarTransform.VariableReplacements repl, repl1;
DAE.FunctionTree functree;
list<BackendDAE.Equation> eqLst,reqns, otherEqnsLst,otherEqnsLstReplaced, eqNew, hs, hs1;
list<BackendDAE.EquationArray> gEqs, hEqs, hsEqs;
list<BackendDAE.Var> varLst, tvars, tvarsReplaced, ovarsLst, xa0, a_0, varNew;
list<BackendDAE.Variables> xaVars, rVars, aVars;
list<BackendVarTransform.VariableReplacements> replLst;
list<list<BackendDAE.Equation>> g_i_lst, g_i_lst1, h_i_lst, h_i_lst1, hs_i_lst, hs_i_lst1, hs_0_lst;
list<list<BackendDAE.Var>> xa_i_lst, xa_i_lst1, r_i_lst, r_i_lst1, a_i_lst, a_i_lst1;
list<DAE.ComponentRef> tcrs,ovcrs;
algorithm
// handle torn systems for the linear case
BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs = eqns, matching = BackendDAE.MATCHING(comps=comps)) := isyst;
BackendDAE.SHARED(knownVars=kv, functionTree=functree) := ishared;
eqLst := BackendEquation.equationList(eqns);
varLst := BackendVariable.varList(vars);
tvars := List.map1r(tearingVars, BackendVariable.getVarAt, vars);
tvarsReplaced := List.map(tvars, BackendVariable.transformXToXd);
tcrs := List.map(tvarsReplaced, BackendVariable.varCref);
// get residual eqns
reqns := BackendEquation.getEqns(residualEqs, eqns);
reqns := BackendEquation.replaceDerOpInEquationList(reqns);
// get the other equations and the other variables
otherEqnsInts := List.map(otherEqsVarTpl, Util.tuple21);
otherEqnsLst := BackendEquation.getEqns(otherEqnsInts, eqns);
oeqns := BackendEquation.listEquation(otherEqnsLst);
otherEqnsLstReplaced := BackendEquation.replaceDerOpInEquationList(otherEqnsLst); // for computing the new equations
otherVarsIntsLst := List.map(otherEqsVarTpl, Util.tuple22);
otherVarsInts := List.unionList(otherVarsIntsLst);
ovarsLst := List.map1r(otherVarsInts, BackendVariable.getVarAt, vars);
ovarsLst := List.map(ovarsLst, BackendVariable.transformXToXd); //try this
ovars := BackendVariable.listVar1(ovarsLst);
ovcrs := List.map(ovarsLst, BackendVariable.varCref);
//build the components and systems to get the system for computing the tearingVars
size := listLength(tvars);
otherEqSize := listLength(otherEqnsLst);
compSize := listLength(comps);
tVarRange := List.intRange2(0,size);
repl1 := BackendVarTransform.emptyReplacements();
// get g_i(xt=e_i, xa=xa_i) with xa_i as variables to be solved
(g_i_lst,xa_i_lst,replLst) := getAlgebraicEquationsForEI(tVarRange,size,otherEqnsLstReplaced,tvarsReplaced,tcrs,ovarsLst,ovcrs,{},{},{},tornSysIdx);
(g_i_lst1,xa_i_lst1,repl1) := simplifyEquations(g_i_lst,xa_i_lst,repl1);
//dumpVarLstLst(xa_i_lst,"xa");
//dumpEqLstLst(g_i_lst,"g");
// compute residualValues h_i(xt=e_i,xa_i,r_i) for r_i
(h_i_lst,r_i_lst) := addResidualVarToEquation(tVarRange,reqns,{},{},tornSysIdx);
h_i_lst := replaceVarsInResidualEquations(tVarRange,h_i_lst,replLst,{});
(h_i_lst1,r_i_lst1,repl1) := simplifyEquations(h_i_lst,r_i_lst,repl1);
//dumpVarLstLst(r_i_lst,"r");
//dumpEqLstLst(h_i_lst,"h");
// get the co-efficients for the new residualEquations a_i from hs_i(r_i,xt=e_i, a_i)
(hs_i_lst,a_i_lst) := getTornSystemCoefficients(tVarRange,size,r_i_lst,{},{},tornSysIdx);
(hs_i_lst1,a_i_lst1,repl1) := simplifyEquations(hs_i_lst,a_i_lst,repl1);
//dumpVarLstLst(a_i_lst,"a");
//dumpEqLstLst(hs_i_lst,"hs_i");
// gather all additional equations and build the strongComponents (not including the new residual equation)
eqsNewOut := List.flatten(listAppend(listAppend(g_i_lst1,h_i_lst1),hs_i_lst1));
varsNewOut := List.flatten(listAppend(listAppend(xa_i_lst1,r_i_lst1),a_i_lst1));
matchingNew := buildSingleEquationSystem(compSize,eqsNewOut,varsNewOut,ishared,{});
BackendDAE.MATCHING(ass1=ass1New, ass2=ass2New, comps=compsNew) := matchingNew;
//BackendDump.dumpComponents(compsNew);
compsNew := List.map2(compsNew,updateIndicesInComp,listLength(varLst),listLength(eqLst));
//BackendDump.dumpVarList(varsNewOut,"varsNew");
//BackendDump.dumpEquationList(eqsNewOut,"eqsNew");
// compute the tearing vars in the new residual equations hs
(a_0::a_i_lst) := a_i_lst;
//a_0 := listReverse(a_0);
hs := buildNewResidualEquation(1,a_i_lst,a_0,tvars,{});
(hs1,_) := BackendVarTransform.replaceEquations(hs,repl1,NONE());
tVarsOut := tvars;
resEqsOut := hs1;
//// get the strongComponent for the residual equations and add it at the end of the new StrongComponents
//BackendDump.dumpEquationList(resEqsOut,"the equations of the system\n");
//BackendDump.dumpVarList(tVarsOut, "the vars of the system\n");
isSingleEq := intEq(listLength(resEqsOut),1);
rComp := buildEqSystemComponent(isSingleEq,tearingVars,residualEqs,a_i_lst);
oComps := List.appendElt(rComp,compsNew);
matchingOut := BackendDAE.MATCHING(ass1New,ass2New,oComps);
//printPartLinTornInfo(tcrs,reqns,otherEqnsLst,ovcrs,xa_i_lst,g_i_lst,r_i_lst,h_i_lst,a_i_lst,hs_i_lst,hs,compsNew);
end reduceLinearTornSystem2;
protected function buildEqSystemComponent "builds a strongComponent for the reduced System. if the system size is 1, a SingleEquation is built, otherwise a EqSystem with jacobian.
author:Waurich TUD 2013-12"
input Boolean isSingleEq;
input list<Integer> varIdcsIn;
input list<Integer> eqIdcsIn;
input list<list<BackendDAE.Var>> jacValuesIn;
output BackendDAE.StrongComponent outComp;
algorithm
outComp := match(isSingleEq,varIdcsIn,eqIdcsIn,jacValuesIn)
local
Integer eqIdx,varIdx;
Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> jac;
BackendDAE.StrongComponent comp;
case(true,_,_,_)
equation
eqIdx = listGet(eqIdcsIn,1);
varIdx = listGet(varIdcsIn,1);
comp = BackendDAE.SINGLEEQUATION(eqIdx,varIdx);
Debug.fcall(Flags.HPCOM_DUMP,print,"a linear equationsystem of size 1 was found and was replaced by a single equation\n\n");
then
comp;
case(false,_,_,_)
equation
jac = buildLinearJacobian(jacValuesIn);
//print("Jac:\n" +& BackendDump.dumpJacobianStr(jac) +& "\n");
comp = BackendDAE.EQUATIONSYSTEM(eqIdcsIn,varIdcsIn,BackendDAE.FULL_JACOBIAN(jac),BackendDAE.JAC_TIME_VARYING());
//print("the eqs of the sys: "+&stringDelimitList(List.map(varIdcsIn,intString),"\n")+&"\n");
//print("the vars of the sys: "+&stringDelimitList(List.map(eqIdcsIn,intString),"\n")+&"\n");
//comp = BackendDAE.EQUATIONSYSTEM(eqIdcsIn,varIdcsIn,NONE(),BackendDAE.JAC_NO_ANALYTIC());
//comp = BackendDAE.TORNSYSTEM(varIdcsIn,eqIdcsIn,{},true);
Debug.fcall(Flags.HPCOM_DUMP,print,"a linear equationsystem of size "+&intString(listLength(eqIdcsIn))+&" is left from the partitioning.\n\n");
then
comp;
end match;
end buildEqSystemComponent;
protected function buildLinearJacobian "builds the jacobian out of the given jacobian-entries
author:Waurich TUD 2013-12"
input list<list<BackendDAE.Var>> inElements; //outer list refers to the row, inner list to the column
output Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> outJac;
protected
list<tuple<Integer, Integer, BackendDAE.Equation>> jac;
algorithm
jac := List.fold1(List.intRange(listLength(inElements)),buildLinearJacobian1,inElements,{});
jac := listReverse(jac);
outJac := SOME(jac);
end buildLinearJacobian;
protected function buildLinearJacobian1 "helper for buildLinearJacobian.
author:Waurich TUD 2013-12"
input Integer rowIdx;
input list<list<BackendDAE.Var>> inElements;
input list<tuple<Integer, Integer, BackendDAE.Equation>> inJac;
output list<tuple<Integer, Integer, BackendDAE.Equation>> outJac;
protected
list<BackendDAE.Var> elements;
list<tuple<Integer, Integer, BackendDAE.Equation>> jac;
algorithm
elements := listGet(inElements,rowIdx);
outJac := List.fold2(List.intRange(listLength(inElements)),buildLinearJacobian2,elements,rowIdx,inJac);
end buildLinearJacobian1;
protected function buildLinearJacobian2 "helper for buildLinearJacobian
author:Waurich TUD 2013-12"
input Integer colIdx;
input list<BackendDAE.Var> inElements;
input Integer rowIdx;
input list<tuple<Integer, Integer, BackendDAE.Equation>> inJac;
output list<tuple<Integer, Integer, BackendDAE.Equation>> outJac;
protected
DAE.ComponentRef cref;
DAE.Exp exp;
BackendDAE.Equation eq;
BackendDAE.Var elem;
tuple<Integer,Integer,BackendDAE.Equation> entry;
list<tuple<Integer, Integer, BackendDAE.Equation>> jac;
algorithm
elem := listGet(inElements,colIdx);
cref := BackendVariable.varCref(elem);
exp := DAE.CREF(cref,DAE.T_REAL_DEFAULT);
exp := DAE.UNARY(DAE.UMINUS(DAE.T_REAL_DEFAULT),exp);
eq := BackendDAE.RESIDUAL_EQUATION(exp,DAE.emptyElementSource,false);
entry := (colIdx,rowIdx,eq);
outJac := entry::inJac;
end buildLinearJacobian2;
protected function updateMatching "inserts the information of matching2 into matching1 by adding an index offset for the vars and eqs of matching2.Actually only one assignment for matching 2 is needed.
author: Waurich TUD 2013-09"
input Integer idx;
input tuple<Integer,Integer> offsetTpl;
input tuple<array<Integer>,array<Integer>> matching2;
input tuple<array<Integer>,array<Integer>> matching1In;
output tuple<array<Integer>,array<Integer>> matching1Out;
protected
Integer eqOffset, varOffset, eqValue, varValue;
array<Integer> ass11, ass21, ass12, ass22;
algorithm
(eqOffset, varOffset) := offsetTpl;
(ass12, ass22) := matching2;
(ass11, ass21) := matching1In;
eqValue := idx + eqOffset;
varValue := arrayGet(ass22,idx)+varOffset;
ass11 := arrayUpdate(ass11,varValue,eqValue);
ass21 := arrayUpdate(ass21, eqValue, varValue);
matching1Out := (ass11, ass21);
end updateMatching;
protected function updateResidualMatching "sets the matching between tearingVars and residuals.
author: Waurich TUD 2013-09"
input Integer idx;
input list<Integer> tvars;
input list<Integer> resEqs;
input tuple<array<Integer>,array<Integer>> tplIn;
output tuple<array<Integer>,array<Integer>> tplOut;
protected
array<Integer> ass1, ass2;
Integer eqIdx, varIdx;
algorithm
(ass1,ass2) := tplIn;
eqIdx := listGet(resEqs,idx);
varIdx := listGet(tvars,idx);
ass1 := arrayUpdate(ass1,varIdx,eqIdx);
ass2 := arrayUpdate(ass2,eqIdx,varIdx);
tplOut := (ass1,ass2);
end updateResidualMatching;
protected function getOtherComps "builds ordered StrongComponents and matching for the other equations.
author: Waurich TUD 2013-09"
input list<tuple<Integer, list<Integer>>> otherEqsVarTpl;
input array<Integer> ass1;
input array<Integer> ass2;
output BackendDAE.Matching matchingOut;
protected
array<Integer> ass1Tmp, ass2Tmp;
BackendDAE.StrongComponents compsTmp;
algorithm
((ass1Tmp,ass2Tmp,compsTmp)) := List.fold(otherEqsVarTpl,getOtherComps1,(ass1,ass2,{}));
compsTmp := listReverse(compsTmp);
matchingOut := BackendDAE.MATCHING(ass1Tmp,ass2Tmp,compsTmp);
end getOtherComps;
protected function getOtherComps1 "implementation of getOtherComps
author:waurich TUD 2013-09"
input tuple<Integer,list<Integer>> otherEqsVarTpl;
input tuple<array<Integer>, array<Integer>, BackendDAE.StrongComponents> tplIn;
output tuple<array<Integer>, array<Integer>, BackendDAE.StrongComponents> tplOut;
algorithm
tplOut := matchcontinue(otherEqsVarTpl, tplIn)
local
Integer eqIdx, varIdx;
array<Integer> ass1, ass2;
list<Integer> varIdcs;
BackendDAE.StrongComponent comp;
BackendDAE.StrongComponents compsIn, compsTmp;
case((eqIdx, varIdcs),(ass1,ass2,compsIn))
equation
true = listLength(varIdcs) == 1;
varIdx = listGet(varIdcs,1);
comp = BackendDAE.SINGLEEQUATION(eqIdx,varIdx);
ass1 = arrayUpdate(ass1,varIdx,eqIdx);
ass2 = arrayUpdate(ass2,eqIdx,varIdx);
compsTmp = comp::compsIn;
then
((ass1,ass2,compsTmp));
else
equation
print("getOtherComps failed\n");
then
fail();
end matchcontinue;
end getOtherComps1;
protected function replaceAtPositionFromList "replaces the entry from inLst indexed by positionLst[n] with with the nth entry in replacingLst. n is first input so it can be used in a folding functions.
author: Waurich TUD 2013-09"
replaceable type ElementType subtypeof Any;
input Integer n;
input list<ElementType> replacingLst;
input list<Integer> positionLst;
input list<ElementType> inLst;
output list<ElementType> outLst;
protected
Integer idx;
ElementType entry;
algorithm
idx := listGet(positionLst,n);
entry := listGet(replacingLst,n);
outLst := List.replaceAt(entry,idx-1,inLst);
end replaceAtPositionFromList;
protected function updateIndicesInComp " raises the indices of the vars and eqs in the given component according to the given offsets.
author: Waurich TUD 2013-09"
input BackendDAE.StrongComponent compIn;
input Integer varOffset;
input Integer eqOffset;
output BackendDAE.StrongComponent compOut;
algorithm
compOut := matchcontinue(compIn,varOffset,eqOffset)
local
Integer varIdx;
Integer eqIdx;
BackendDAE.StrongComponent compTmp;
case(BackendDAE.SINGLEEQUATION(eqn=eqIdx, var=varIdx),_,_)
equation
varIdx = varIdx+varOffset;
eqIdx = eqIdx+eqOffset;
compTmp = BackendDAE.SINGLEEQUATION(eqIdx, varIdx);
then
compTmp;
else
equation
print("updateVarEqIndices failed\n");
then
fail();
end matchcontinue;
end updateIndicesInComp;
protected function buildNewResidualEquation "function to build the new linear residual equations res=0=A*xt+a0 whicht is solved for xt
author: Waurich TUD 2013-09"
input Integer resIdx;
input list<list<BackendDAE.Var>> aCoeffLst;
input list<BackendDAE.Var> a0CoeffLst;
input list<BackendDAE.Var> tvars;
input list<BackendDAE.Equation> resEqsIn;
output list<BackendDAE.Equation> resEqsOut;
algorithm
resEqsOut := matchcontinue(resIdx,aCoeffLst,a0CoeffLst,tvars,resEqsIn)
local
list<BackendDAE.Equation> eqLstTmp;
list<BackendDAE.Var> aCoeffs;
BackendDAE.Equation eqTmp, hs;
BackendDAE.Var a0Coeff;
DAE.Exp lhs, rhs, a0Exp;
DAE.Type ty;
case(_,_,_,_,_)
equation
true = resIdx > listLength(tvars);
eqLstTmp = listReverse(resEqsIn);
then
eqLstTmp;
case(_,_,_,_,_)
equation
true = resIdx <= listLength(tvars);
//aCoeffs = listGet(aCoeffLst,resIdx);
//aCoeffs = listReverse(aCoeffs);
aCoeffs = List.map1(aCoeffLst,listGet,resIdx);
a0Coeff = listGet(a0CoeffLst,resIdx);
a0Exp = varExp(a0Coeff);
ty = DAE.T_REAL_DEFAULT;
rhs = buildNewResidualEquation2(1,aCoeffs,tvars,DAE.RCONST(0.0)); // the start value is random and will be rejected
rhs = DAE.BINARY(rhs, DAE.ADD(ty), a0Exp);
lhs = DAE.RCONST(0.0);
hs = BackendDAE.EQUATION(lhs,rhs,DAE.emptyElementSource,false);
eqLstTmp = hs::resEqsIn;
eqLstTmp = buildNewResidualEquation(resIdx+1,aCoeffLst,a0CoeffLst,tvars,eqLstTmp);
then
eqLstTmp;
else
equation
print("buildNewResidualEquation failed");
then
fail();
end matchcontinue;
end buildNewResidualEquation;
protected function buildNewResidualEquation2 "function to build the sum of the rhs of the new residual equation, i.e. the sum of all tvars and their coefficients
author: Waurich TUD 2013-09"
input Integer idx;
input list<BackendDAE.Var> coeffs;
input list<BackendDAE.Var> tVars;
input DAE.Exp expIn;
output DAE.Exp expOut;
algorithm
expOut := matchcontinue(idx,coeffs,tVars,expIn)
local
BackendDAE.Var coeff;
BackendDAE.Var tVar;
DAE.Exp coeffExp, tVarExp, expTmp;
DAE.Type ty;
case(_,_,_,_)
equation
// the first product of the term
true = idx == 1;
coeff = listGet(coeffs,idx);
coeffExp = varExp(coeff);
tVar = listGet(tVars,idx);
tVarExp = varExp(tVar);
tVarExp = Debug.bcallret1(BackendVariable.isStateVar(tVar), Expression.expDer, tVarExp, tVarExp); // if tvar is a state, use the der(varexp)
ty = DAE.T_REAL_DEFAULT;
expTmp = DAE.BINARY(coeffExp,DAE.MUL(ty),tVarExp);
expTmp = buildNewResidualEquation2(idx+1,coeffs,tVars,expTmp);
then
expTmp;
case(_,_,_,_)
equation
true = idx <= listLength(tVars);
//extend the expression
coeff = listGet(coeffs,idx);
tVar = listGet(tVars,idx);
expTmp = addProductToExp(coeff,tVar,expIn);
expTmp = buildNewResidualEquation2(idx+1,coeffs,tVars,expTmp);
then
expTmp;
case(_,_,_,_)
equation
true = idx > listLength(tVars);
then
expIn;
else
equation
print("buildNewResidualEquation2 failed!\n");
then
fail();
end matchcontinue;
end buildNewResidualEquation2;
protected function addProductToExp " function to add the product of the given 2 BackendDAE.Var to the given inExp. expOut = expIn + fac1*fac2
author: Waurich TUD 2013-09"
input BackendDAE.Var var1;
input BackendDAE.Var var2;
input DAE.Exp inExp;
output DAE.Exp expOut;
protected
DAE.Exp fac1, fac2, prod;
DAE.Type ty;
algorithm
fac1 := varExp(var1);
fac2 := varExp(var2);
fac2 := Debug.bcallret1(BackendVariable.isStateVar(var2), Expression.expDer, fac2, fac2);
ty := DAE.T_REAL_DEFAULT;
prod := DAE.BINARY(fac1, DAE.MUL(ty), fac2);
expOut := DAE.BINARY(inExp, DAE.ADD(ty), prod);
end addProductToExp;
protected function buildSingleEquationSystem "function to build a system of singleEquations which can be solved partially parallel, from an EquationArray and Variables.
author: Waurich TUD 2013-07"
input Integer eqSizeOrig;
input list<BackendDAE.Equation> inEqs;
input list<BackendDAE.Var> inVars;
input BackendDAE.Shared shared;
input BackendDAE.StrongComponents compsIn;
output BackendDAE.Matching matchingOut;
algorithm
matchingOut := matchcontinue(eqSizeOrig,inEqs,inVars,shared,compsIn)
local
array<list<Integer>> mapEqnIncRow;
array<Integer> ass1, ass2;
array<Integer> mapIncRowEqn;
Integer nVars, nEqs, compIdxTmp;
BackendDAE.EquationArray eqArr;
BackendDAE.EqSystem sysTmp;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mt;
BackendDAE.Matching matching, matchingTmp;
BackendDAE.StrongComponents compsTmp;
BackendDAE.Variables vars;
case(_,_,_,_,_)
equation
// build a singleEquation from a list<Equation> and list<Var> which are indexed by compIdx;
// get the EQSYSTEM, the incidenceMatrix and a matching
vars = BackendVariable.listVar1(inVars);
eqArr = BackendEquation.listEquation(inEqs);
sysTmp = BackendDAE.EQSYSTEM(vars,eqArr,NONE(),NONE(),BackendDAE.NO_MATCHING(),{});
(sysTmp,m,mt) = BackendDAEUtil.getIncidenceMatrix(sysTmp,BackendDAE.NORMAL(),NONE());
nVars = listLength(inVars);
nEqs = listLength(inEqs);
ass1 = arrayCreate(nVars, -1);
ass2 = arrayCreate(nEqs, -1);
Matching.matchingExternalsetIncidenceMatrix(nVars, nEqs, m);
BackendDAEEXT.matching(nVars, nEqs, 5, -1, 0.0, 1);
BackendDAEEXT.getAssignment(ass2, ass1);
matching = BackendDAE.MATCHING(ass1, ass2, {});
sysTmp = BackendDAE.EQSYSTEM(vars,eqArr,SOME(m),SOME(mt),matching,{});
// perform BLT to order the StrongComponents
mapIncRowEqn = listArray(List.intRange(nEqs));
mapEqnIncRow = Util.arrayMap(mapIncRowEqn,List.create);
(sysTmp,compsTmp) = BackendDAETransform.strongComponentsScalar(sysTmp,shared,mapEqnIncRow,mapIncRowEqn);
compsTmp = listAppend(compsIn,compsTmp);
matchingTmp = BackendDAE.MATCHING(ass1, ass2, compsTmp);
then
matchingTmp;
else
equation
print("buildSingleEquationSystem failed\n");
then
fail();
end matchcontinue;
end buildSingleEquationSystem;
protected function getTornSystemCoefficients "gets the co-efficients for the new residual equations of the linear torn system
the first index is for the residualvar and the second for the tearingvar
(r1) = (a11 a12..) (xt1)+(a01)
(r2) = (a21 a22..)*(xt2)+(a02)
(:) = (: : ) ( : )+( : )
this is meant to be a matrix :)
author: Waurich TUD 2013-08"
input list<Integer> iValueRange;
input Integer numTVars;
input list<list<BackendDAE.Var>> r_i_lstIn;
input list<list<BackendDAE.Equation>> hs_i_lstIn;
input list<list<BackendDAE.Var>> a_i_lstIn;
input Integer tornSysIdx;
output list<list<BackendDAE.Equation>> hs_i_lstOut;
output list<list<BackendDAE.Var>> a_i_lstOut;
algorithm
(hs_i_lstOut,a_i_lstOut) := matchcontinue(iValueRange, numTVars, r_i_lstIn, hs_i_lstIn, a_i_lstIn,tornSysIdx)
local
Integer iValue;
String varName;
list<Integer> iLstRest;
list<BackendDAE.Equation> hs_i;
list<BackendDAE.Var> a_i, r_i;
list<list<BackendDAE.Equation>> hs_i_lstTmp;
list<list<BackendDAE.Var>> a_i_lstTmp;
BackendDAE.Var aVar;
DAE.ComponentRef varCRef;
DAE.Exp varExp;
case({},_,_,_,_,_)
equation
//completed
hs_i_lstTmp = listDelete(hs_i_lstIn,0);
a_i_lstTmp = listDelete(a_i_lstIn,0);
hs_i_lstTmp = listReverse(hs_i_lstTmp);
a_i_lstTmp = listReverse(a_i_lstTmp);
then
(hs_i_lstTmp,a_i_lstTmp);
case(iValue::iLstRest,_,_,_,_,_)
equation
// gets the equations for computing the coefficients for the new residual equations
r_i = listGet(r_i_lstIn,iValue+1);
(hs_i_lstTmp,a_i_lstTmp) = getTornSystemCoefficients1(List.intRange(numTVars),iValue,r_i,hs_i_lstIn,a_i_lstIn,tornSysIdx);
//BackendDump.dumpVarList(listGet(a_i_lstTmp,1),"a_"+&intString(iValue)+&"\n");
//BackendDump.dumpEquationList(listGet(hs_i_lstTmp,1),"hs_"+&intString(iValue)+&"\n");
(hs_i_lstTmp,a_i_lstTmp) = getTornSystemCoefficients(iLstRest,numTVars,r_i_lstIn,{}::hs_i_lstTmp,{}::a_i_lstTmp,tornSysIdx);
then
(hs_i_lstTmp,a_i_lstTmp);
else
equation
print("getTornSystemCoefficients failed!\n");
then
fail();
end matchcontinue;
end getTornSystemCoefficients;
protected function getTornSystemCoefficients1 "gets the equations with coefficients for one e_i
author: Waurich TUD 2013-08"
input list<Integer> resIdxLst;
input Integer iIdx;
input list<BackendDAE.Var> resVal_iIn;
input list<list<BackendDAE.Equation>>hs_i_lstIn;
input list<list<BackendDAE.Var>> a_i_lstIn;
input Integer tornSysIdx;
output list<list<BackendDAE.Equation>> hs_i_lstOut;
output list<list<BackendDAE.Var>> a_i_lstOut;
algorithm
(hs_i_lstOut, a_i_lstOut) := matchcontinue(resIdxLst, iIdx, resVal_iIn, hs_i_lstIn, a_i_lstIn,tornSysIdx)
local
Integer resIdx,resIdx1;
String aName;
list<Integer> resIdxRest;
list<list<BackendDAE.Equation>> hs_i_lstTmp;
list<list<BackendDAE.Var>> a_i_lstTmp;
list<BackendDAE.Equation> hs_iTmp;
list<BackendDAE.Var> a_iTmp, d_lst;
BackendDAE.Equation hs_ii;
BackendDAE.Var a_ii, r_ii, dVar;
DAE.ComponentRef aCRef;
DAE.Exp aExp, lhs, rhs, dExp;
DAE.Type ty;
case({},_,_,_,_,_)
equation
//complete
//hs_i_lstTmp = listReverse(hs_i_lstIn);
//a_i_lstTmp = listReverse(a_i_lstIn);
then
(hs_i_lstIn,a_i_lstIn);
case(resIdx::resIdxRest,_,_,_,_,_)
equation
true = intEq(0,iIdx);
// build the coefficients (offset d=a_0) of the new residual equations (hs = A*xt+d)
aName = "$a_"+&intString(tornSysIdx)+&intString(iIdx)+&"_"+&intString(resIdx);
//aName = "$a_"+&intString(resIdx)+&"_"+&intString(iIdx);
ty = DAE.T_REAL_DEFAULT;
aCRef = ComponentReference.makeCrefIdent(aName,ty,{});
aExp = Expression.makeCrefExp(aCRef,ty);
a_ii = BackendDAE.VAR(aCRef,BackendDAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),ty,NONE(),NONE(),{},DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR());
// build the equations to solve for the coefficients
resIdx1 = listLength(resVal_iIn)+1-resIdx;
r_ii = listGet(resVal_iIn,resIdx1);
lhs = varExp(r_ii);
rhs = varExp(a_ii);
hs_ii = BackendDAE.EQUATION(lhs,rhs,DAE.emptyElementSource,false);
// update th a_i_lst and the hs_i_lst
a_i_lstTmp = Debug.bcallret1(List.isEmpty(a_i_lstIn), List.create, {a_ii},varInFrontList(a_ii,a_i_lstIn));
hs_i_lstTmp = Debug.bcallret1(List.isEmpty(hs_i_lstIn), List.create, {hs_ii}, eqInFrontList(hs_ii,hs_i_lstIn));
//next residual equation
(hs_i_lstTmp,a_i_lstTmp) = getTornSystemCoefficients1(resIdxRest,iIdx,resVal_iIn,hs_i_lstTmp,a_i_lstTmp,tornSysIdx);
then
(hs_i_lstTmp, a_i_lstTmp);
case(resIdx::resIdxRest,_,_,_,_,_)
equation
true = iIdx > 0;
// build the co-efficients (A-matrix-entries) of the new residual equations (hs = A*xt+d)
//aName = "$a_"+&intString(iIdx)+&"_"+&intString(resIdx);
aName = "$a_"+&intString(tornSysIdx)+&intString(resIdx)+&"_"+&intString(iIdx);
ty = DAE.T_REAL_DEFAULT;
aCRef = ComponentReference.makeCrefIdent(aName,ty,{});
aExp = Expression.makeCrefExp(aCRef,ty);
a_ii = BackendDAE.VAR(aCRef,BackendDAE.VARIABLE(),DAE.BIDIR(),DAE.NON_PARALLEL(),ty,NONE(),NONE(),{},DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR());
// build the equations to solve for the coefficients
resIdx1 = listLength(resVal_iIn)+1-resIdx;
d_lst = List.last(a_i_lstIn);
dVar = listGet(d_lst, resIdx1);
dExp = varExp(dVar);
aExp = varExp(a_ii);
rhs = DAE.BINARY(aExp,DAE.ADD(ty),dExp);
r_ii = listGet(resVal_iIn,resIdx1);
lhs = varExp(r_ii);
hs_ii = BackendDAE.EQUATION(lhs,rhs,DAE.emptyElementSource,false);
// update th a_i_lst and the hs_i_lst
a_i_lstTmp = Debug.bcallret1(List.isEmpty(a_i_lstIn), List.create, {a_ii},varInFrontList(a_ii, a_i_lstIn));
hs_i_lstTmp = Debug.bcallret1(List.isEmpty(hs_i_lstIn), List.create, {hs_ii}, eqInFrontList(hs_ii, hs_i_lstIn));
// next residual equation
(hs_i_lstTmp, a_i_lstTmp) = getTornSystemCoefficients1(resIdxRest, iIdx, resVal_iIn, hs_i_lstTmp, a_i_lstTmp,tornSysIdx);
then
(hs_i_lstTmp, a_i_lstTmp);
else
equation
print("getTornSystemCoefficients1 failed\n");
then
fail();
end matchcontinue;
end getTornSystemCoefficients1;
protected function varExp "gets an DAE.Exp for the CREF of the given BackendDAE.Var
author: Waurich TUD 2013-08"
input BackendDAE.Var varIn;
output DAE.Exp expOut;
protected
DAE.ComponentRef cr;
DAE.Type ty;
algorithm
ty := BackendVariable.varType(varIn);
cr := BackendVariable.varCref(varIn);
expOut := DAE.CREF(cr,ty);
end varExp;
protected function replaceVarsInResidualEquations "replaces the otherVars with xa_i and the tvars with e_i
author: Waurich TUD 2013-08"
input list<Integer> iValueRange;
input list<list<BackendDAE.Equation>> resEqsIn;
input list<BackendVarTransform.VariableReplacements> inReplLst;
input list<list<BackendDAE.Equation>> h_i_lstIn;
output list<list<BackendDAE.Equation>> h_i_lstOut;
algorithm
h_i_lstOut := matchcontinue(iValueRange,resEqsIn,inReplLst,h_i_lstIn)
local
Integer iValue;
list<Integer> iLstRest;
list<BackendDAE.Equation> h_i_Eqs;
BackendVarTransform.VariableReplacements repl;
list<list<BackendDAE.Equation>> h_i_lstTmp;
case({},_,_,_)
//completed
equation
h_i_lstTmp = listReverse(h_i_lstIn);
then
h_i_lstTmp;
case(iValue::iLstRest,_,_,_)
equation
iValue = iValue+1;
repl = listGet(inReplLst,iValue);
h_i_Eqs = listGet(resEqsIn,iValue);
(h_i_Eqs,_) = BackendVarTransform.replaceEquations(h_i_Eqs,repl,NONE());
h_i_lstTmp = replaceVarsInResidualEquations(iLstRest,resEqsIn,inReplLst,h_i_Eqs::h_i_lstIn);
then
h_i_lstTmp;
else
equation
print("replaceVarsInResidualEquations failed \n");
then
fail();
end matchcontinue;
end replaceVarsInResidualEquations;
protected function addResidualVarToEquation "adds a variable r_x to the right hand side of an equation. this corresponds to the residual value in a residual equation
author: Waurich TUD 2013-08"
input list<Integer> iIn;
input list<BackendDAE.Equation> resEqLstIn;
input list<list<BackendDAE.Equation>> h_i_lstIn;
input list<list<BackendDAE.Var>> r_i_lstIn;
input Integer tornSysIdx;
output list<list<BackendDAE.Equation>> h_i_lstOut;
output list<list<BackendDAE.Var>> r_i_lstOut;
algorithm
(h_i_lstOut,r_i_lstOut) := matchcontinue(iIn,resEqLstIn,h_i_lstIn,r_i_lstIn,tornSysIdx)
local
Integer iValue;
String resVarName;
list<Integer> eqIdxRange;
list<Integer> iLstRest;
list<list<BackendDAE.Equation>> h_i_lstTmp;
list<list<BackendDAE.Var>> r_i_lstTmp;
BackendDAE.Equation resEq;
DAE.Exp exp1;
DAE.Exp exp2;
DAE.Exp scalarExp;
case({},_,_,_,_)
// completed
equation
h_i_lstTmp = listDelete(h_i_lstIn,0);
r_i_lstTmp = listDelete(r_i_lstIn,0);
h_i_lstTmp = listReverse(h_i_lstTmp);
r_i_lstTmp = listReverse(r_i_lstTmp);
then