/
BackendDAETransform.mo
1482 lines (1375 loc) · 52.3 KB
/
BackendDAETransform.mo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated package BackendDAETransform
" file: BackendDAETransform.mo
package: BackendDAETransform
description: BackendDAETransform contains functions that are needed to perform
a transformation to a Block-Lower-Triangular-DAE.
- matchingAlgorithm
- strongComponents
- reduceIndexDummyDer
RCS: $Id$
"
public import Absyn;
public import BackendDAE;
public import DAE;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendDAEOptimize;
protected import BackendEquation;
protected import BackendVariable;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import Flags;
protected import List;
protected import SCode;
protected import SymbolicJacobian;
protected import System;
protected import Util;
protected import Values;
/******************************************
strongComponents and stuff
*****************************************/
public function strongComponentsScalar "author: PA
This is the second part of the BLT sorting. It takes the variable
assignments and the incidence matrix as input and identifies strong
components, i.e. subsystems of equations.
inputs: (IncidenceMatrix, BackendDAE.IncidenceMatrixT, int vector, int vector)
outputs: (int list list /* list of components */ )
"
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input array<list<Integer>> mapEqnIncRow;
input array<Integer> mapIncRowEqn;
output BackendDAE.EqSystem osyst;
output BackendDAE.StrongComponents outComps;
algorithm
(osyst,outComps) :=
matchcontinue (syst,shared,mapEqnIncRow,mapIncRowEqn)
local
list<list<Integer>> comps;
array<Integer> ass1,ass2;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mt;
BackendDAE.StrongComponents comps1;
BackendDAE.EquationArray eqs;
BackendDAE.Variables vars;
array<Integer> markarray;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
case (BackendDAE.EQSYSTEM(vars,eqs,SOME(_),SOME(mt),BackendDAE.MATCHING(ass1=ass1,ass2=ass2),stateSets=stateSets,partitionKind=partitionKind),_,_,_)
equation
comps = tarjanAlgorithm(mt,ass2);
markarray = arrayCreate(BackendDAEUtil.equationArraySize(eqs),-1);
comps1 = analyseStrongComponentsScalar(comps,syst,shared,ass1,ass2,mapEqnIncRow,mapIncRowEqn,1,markarray,{});
ass1 = varAssignmentNonScalar(ass1,mapIncRowEqn);
//noscalass2 = eqnAssignmentNonScalar(1,arrayLength(mapEqnIncRow),mapEqnIncRow,ass2,{});
then
// Frenkel TUD: Do not hand over the scalar incidence Matrix because following modules does not check if scalar or not
(BackendDAE.EQSYSTEM(vars,eqs,NONE(),NONE(),BackendDAE.MATCHING(ass1,ass2,comps1),stateSets,partitionKind),comps1);
else
equation
Error.addInternalError("function strongComponentsScalar failed
- sorting equations (strongComponents) failed", sourceInfo());
then fail();
end matchcontinue;
end strongComponentsScalar;
public function eqnAssignmentNonScalar
input array<list<Integer>> mapEqnIncRow;
input array<Integer> ass2;
output array<list<Integer>> outAcc;
protected
list<Integer> elst, vlst;
list<list<Integer>> acc := {};
algorithm
for i in 1:arrayLength(mapEqnIncRow) loop
elst := mapEqnIncRow[i];
vlst := list(arrayGet(ass2, e) for e guard(arrayGet(ass2, e) > 0) in elst);
end for;
outAcc := listArray(listReverse(acc));
end eqnAssignmentNonScalar;
public function varAssignmentNonScalar
input array<Integer> ass1;
input array<Integer> mapIncRowEqn;
output array<Integer> outAcc;
protected
Integer e;
list<Integer> acc := {};
algorithm
for i in 1:arrayLength(ass1) loop
e := ass1[i];
e := if e > 0 then mapIncRowEqn[e] else -1;
acc := e :: acc;
end for;
outAcc := listArray(listReverse(acc));
end varAssignmentNonScalar;
protected function analyseStrongComponentsScalar"author: Frenkel TUD 2011-05
analyse the type of the strong connect components and
calculate the jacobian."
input list<list<Integer>> inComps;
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input array<Integer> inAss1;
input array<Integer> inAss2;
input array<list<Integer>> mapEqnIncRow;
input array<Integer> mapIncRowEqn;
input Integer imark;
input array<Integer> markarray;
input BackendDAE.StrongComponents iAcc;
output BackendDAE.StrongComponents outComps;
algorithm
outComps:=
match (inComps,syst,shared,inAss1,inAss2,mapEqnIncRow,mapIncRowEqn,imark,markarray,iAcc)
local
list<Integer> comp;
list<list<Integer>> comps;
BackendDAE.StrongComponent acomp;
Integer mark;
case ({},_,_,_,_,_,_,_,_,_) then listReverse(iAcc);
case (comp::comps,_,_,_,_,_,_,_,_,_)
equation
(acomp,mark) = analyseStrongComponentScalar(comp,syst,shared,inAss1,inAss2,mapEqnIncRow,mapIncRowEqn,imark,markarray);
then
analyseStrongComponentsScalar(comps,syst,shared,inAss1,inAss2,mapEqnIncRow,mapIncRowEqn,mark,markarray,acomp::iAcc);
end match;
end analyseStrongComponentsScalar;
protected function analyseStrongComponentScalar"author: Frenkel TUD 2011-05
helper for analyseStrongComponents."
input list<Integer> inComp;
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input array<Integer> inAss1;
input array<Integer> inAss2;
input array<list<Integer>> mapEqnIncRow;
input array<Integer> mapIncRowEqn;
input Integer imark;
input array<Integer> markarray;
output BackendDAE.StrongComponent outComp;
output Integer omark;
algorithm
(outComp,omark):=
match (inComp,syst,shared,inAss1,inAss2,mapEqnIncRow,mapIncRowEqn,imark,markarray)
local
list<Integer> comp,vlst;
list<BackendDAE.Var> varlst;
list<tuple<BackendDAE.Var,Integer>> var_varindx_lst;
array<Integer> ass1,ass2;
BackendDAE.Variables vars;
list<BackendDAE.Equation> eqn_lst;
BackendDAE.EquationArray eqns;
BackendDAE.StrongComponent compX;
case (comp,BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),_,ass1,ass2,_,_,_,_)
equation
vlst = List.map1r(comp,arrayGet,ass2);
vlst = List.select1(vlst,intGt,0);
varlst = List.map1r(vlst,BackendVariable.getVarAt,vars);
var_varindx_lst = List.threadTuple(varlst,vlst);
// get from scalar eqns indexes the indexes in the equation array
comp = List.map1r(comp,arrayGet,mapIncRowEqn);
comp = List.fold2(comp,uniqueComp,imark,markarray,{});
//comp = List.unique(comp);
eqn_lst = List.map1r(comp,BackendEquation.equationNth1,eqns);
compX = analyseStrongComponentBlock(comp,eqn_lst,var_varindx_lst,syst,shared,ass1,ass2,false);
then
(compX,imark+1);
else
equation
Error.addInternalError("function analyseStrongComponentScalar failed", sourceInfo());
then
fail();
end match;
end analyseStrongComponentScalar;
protected function uniqueComp
input Integer c;
input Integer mark;
input array<Integer> markarray;
input list<Integer> iAcc;
output list<Integer> oAcc;
algorithm
oAcc := matchcontinue(c,mark,markarray,iAcc)
case(_,_,_,_)
equation
false = intEq(mark,markarray[c]);
arrayUpdate(markarray,c,mark);
then
c::iAcc;
else
then
iAcc;
end matchcontinue;
end uniqueComp;
public function strongComponents "author: PA
This is the second part of the BLT sorting. It takes the variable
assignments and the incidence matrix as input and identifies strong
components, i.e. subsystems of equations.
inputs: (IncidenceMatrix, BackendDAE.IncidenceMatrixT, int vector, int vector)
outputs: (int list list /* list of components */ )
"
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
output BackendDAE.EqSystem osyst;
output BackendDAE.StrongComponents outComps;
algorithm
(osyst,outComps) :=
matchcontinue (syst,shared)
local
list<list<Integer>> comps;
array<Integer> ass1,ass2;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mt;
BackendDAE.StrongComponents comps1;
BackendDAE.EquationArray eqs;
BackendDAE.Variables vars;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
case (BackendDAE.EQSYSTEM(vars,eqs,SOME(m),SOME(mt),BackendDAE.MATCHING(ass1=ass1,ass2=ass2),stateSets=stateSets,partitionKind=partitionKind),_)
equation
comps = tarjanAlgorithm(mt,ass2);
comps1 = analyseStrongComponents(comps,syst,shared,ass1,ass2,{});
then
(BackendDAE.EQSYSTEM(vars,eqs,SOME(m),SOME(mt),BackendDAE.MATCHING(ass1,ass2,comps1),stateSets,partitionKind),comps1);
else
equation
Error.addInternalError("function strongComponents failed
- sorting equations (strongComponents) failed", sourceInfo());
then fail();
end matchcontinue;
end strongComponents;
protected function analyseStrongComponents"author: Frenkel TUD 2011-05
analyse the type of the strong connect components and
calculate the jacobian."
input list<list<Integer>> inComps;
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input array<Integer> inAss1;
input array<Integer> inAss2;
input BackendDAE.StrongComponents iAcc;
output BackendDAE.StrongComponents outComps;
algorithm
outComps:=
match (inComps,syst,shared,inAss1,inAss2,iAcc)
local
list<Integer> comp;
list<list<Integer>> comps;
BackendDAE.StrongComponent acomp;
case ({},_,_,_,_,_) then listReverse(iAcc);
case (comp::comps,_,_,_,_,_)
equation
acomp = analyseStrongComponent(comp,syst,shared,inAss1,inAss2);
then
analyseStrongComponents(comps,syst,shared,inAss1,inAss2,acomp::iAcc);
else
equation
Error.addInternalError("function analyseStrongComponents failed", sourceInfo());
then
fail();
end match;
end analyseStrongComponents;
protected function analyseStrongComponent"author: Frenkel TUD 2011-05
helper for analyseStrongComponents."
input list<Integer> inComp;
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input array<Integer> inAss1;
input array<Integer> inAss2;
output BackendDAE.StrongComponent outComp;
algorithm
outComp:=
match (inComp,syst,shared,inAss1,inAss2)
local
list<Integer> comp;
list<tuple<BackendDAE.Var,Integer>> var_varindx_lst;
array<Integer> ass1,ass2;
BackendDAE.Variables vars;
list<BackendDAE.Equation> eqn_lst;
BackendDAE.EquationArray eqns;
BackendDAE.StrongComponent compX;
case (comp,BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns),_,ass1,ass2)
equation
(eqn_lst,var_varindx_lst) = List.map3_2(comp, getEquationAndSolvedVar_Internal, eqns, vars, ass2);
compX = analyseStrongComponentBlock(comp,eqn_lst,var_varindx_lst,syst,shared,ass1,ass2,false);
then
compX;
else
equation
Error.addInternalError("function analyseStrongComponent failed", sourceInfo());
then
fail();
end match;
end analyseStrongComponent;
protected function analyseStrongComponentBlock "author: Frenkel TUD 2011-05
helper for analyseStrongComponent."
input list<Integer> inComp;
input list<BackendDAE.Equation> inEqnLst;
input list<tuple<BackendDAE.Var,Integer>> inVarVarindxLst;
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input array<Integer> inAss1;
input array<Integer> inAss2;
input Boolean inLoop; //true if the function call itself
output BackendDAE.StrongComponent outComp;
algorithm
outComp:=
matchcontinue (inComp,inEqnLst,inVarVarindxLst,isyst,ishared,inAss1,inAss2,inLoop)
local
Integer compelem,v;
list<Integer> comp,varindxs;
list<tuple<BackendDAE.Var,Integer>> var_varindx_lst,var_varindx_lst_cond;
array<Integer> ass1,ass2;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrixT mt;
BackendDAE.Variables vars,vars_1;
list<BackendDAE.Equation> eqn_lst,eqn_lst1,cont_eqn,disc_eqn;
list<BackendDAE.Var> var_lst,var_lst_1,cont_var,disc_var;
list<Integer> indxcont_var,indxdisc_var,indxcont_eqn,indxdisc_eqn;
BackendDAE.EquationArray eqns_1,eqns;
Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> jac;
BackendDAE.JacobianType jac_tp;
BackendDAE.StrongComponent sc;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
String msg;
list<DAE.ComponentRef> crlst;
list<String> slst;
Boolean jacConstant, mixedSystem;
case (compelem::{},BackendDAE.ALGORITHM()::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLEALGORITHM(compelem,varindxs);
case (compelem::{},BackendDAE.ARRAY_EQUATION()::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLEARRAY(compelem,varindxs);
case (compelem::{},BackendDAE.IF_EQUATION()::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLEIFEQUATION(compelem,varindxs);
case (compelem::{},BackendDAE.COMPLEX_EQUATION()::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLECOMPLEXEQUATION(compelem,varindxs);
case (compelem::{},BackendDAE.WHEN_EQUATION()::{},var_varindx_lst,_,_,_,_,false)
equation
varindxs = List.map(var_varindx_lst,Util.tuple22);
then
BackendDAE.SINGLEWHENEQUATION(compelem,varindxs);
case (compelem::{},_,(_,v)::{},_,_,_,_,false)
then BackendDAE.SINGLEEQUATION(compelem,v);
case (comp,eqn_lst,var_varindx_lst,syst as BackendDAE.EQSYSTEM(),shared,_,_,_)
equation
var_lst = List.map(var_varindx_lst,Util.tuple21);
//false = BackendVariable.hasDiscreteVar(var_lst); //lochel: mixed systems and non-linear systems are treated the same
true = BackendVariable.hasContinousVar(var_lst); //lochel: pure discrete equation systems are not supported
varindxs = List.map(var_varindx_lst,Util.tuple22);
eqn_lst1 = BackendEquation.replaceDerOpInEquationList(eqn_lst);
// States are solved for der(x) not x.
var_lst_1 = List.map(var_lst, transformXToXd);
vars_1 = BackendVariable.listVar1(var_lst_1);
eqns_1 = BackendEquation.listEquation(eqn_lst1);
(mixedSystem,_) = BackendEquation.iterationVarsinRelations(eqn_lst1, vars_1);
syst = BackendDAE.EQSYSTEM(vars_1,eqns_1,NONE(),NONE(),BackendDAE.NO_MATCHING(),{},BackendDAE.UNKNOWN_PARTITION());
(m,mt) = BackendDAEUtil.incidenceMatrix(syst,BackendDAE.ABSOLUTE(),NONE());
// calculate jacobian. If constant, linear system of equations. Otherwise nonlinear
(jac,shared) = SymbolicJacobian.calculateJacobian(vars_1, eqns_1, m, true, shared);
// Jacobian of a Linear System is always linear
(jac_tp,jacConstant) = SymbolicJacobian.analyzeJacobian(vars_1,eqns_1,jac);
// if constant check for singular jacobian
true = analyzeConstantJacobian(jacConstant,jac,arrayLength(mt),var_lst,eqn_lst,shared);
then
BackendDAE.EQUATIONSYSTEM(comp,varindxs,BackendDAE.FULL_JACOBIAN(jac), jac_tp, mixedSystem);
case (_,eqn_lst,var_varindx_lst,BackendDAE.EQSYSTEM(),_,_,_,_)
equation
var_lst = List.map(var_varindx_lst,Util.tuple21);
true = BackendVariable.hasDiscreteVar(var_lst);
false = BackendVariable.hasContinousVar(var_lst);
msg = "function analyseStrongComponentBlock failed
Sorry - Support for Discrete Equation Systems is not yet implemented\n";
crlst = List.map(var_lst,BackendVariable.varCref);
slst = List.map(crlst,ComponentReference.printComponentRefStr);
msg = msg + stringDelimitList(slst,"\n");
slst = List.map(eqn_lst,BackendDump.equationString);
msg = msg + "\n" + stringDelimitList(slst,"\n");
Error.addInternalError(msg, sourceInfo());
then
fail();
else
equation
Error.addInternalError("function analyseStrongComponentBlock failed", sourceInfo());
then
fail();
end matchcontinue;
end analyseStrongComponentBlock;
protected function analyzeConstantJacobian
input Boolean jacConstant;
input Option<list<tuple<Integer, Integer, BackendDAE.Equation>>> iJac;
input Integer size;
input list<BackendDAE.Var> iVars;
input list<BackendDAE.Equation> iEqns;
input BackendDAE.Shared shared;
output Boolean valid;
algorithm
valid := matchcontinue(jacConstant,iJac,size,iVars,iEqns,shared)
local
list<tuple<Integer, Integer, BackendDAE.Equation>> jac;
list<Real> rhsVals,solvedVals;
list<list<Real>> jacVals;
Integer linInfo;
String infoStr,syst,varnames,varname,rhsStr,jacStr,eqnstr;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
DAE.FunctionTree funcs;
list<DAE.Exp> beqs;
case(true,SOME(jac),_,_,_,_)
equation
jacVals = SymbolicJacobian.evaluateConstantJacobian(size,jac);
rhsVals = List.fill(0.0,size);
(_,linInfo) = System.dgesv(jacVals,rhsVals);
false = intEq(linInfo,0);
varname = ComponentReference.printComponentRefStr(BackendVariable.varCref(listGet(iVars,linInfo)));
infoStr = intString(linInfo);
varnames = stringDelimitList(List.map(List.map(iVars,BackendVariable.varCref),ComponentReference.printComponentRefStr)," ;\n ");
eqns = BackendEquation.listEquation(iEqns);
vars = BackendVariable.listVar1(iVars);
funcs = BackendDAEUtil.getFunctions(shared);
(beqs,_) = BackendDAEUtil.getEqnSysRhs(eqns,vars,SOME(funcs));
beqs = listReverse(beqs);
rhsStr = stringDelimitList(List.map(beqs, ExpressionDump.printExpStr)," ;\n ");
jacStr = stringDelimitList(List.map1(List.mapList(jacVals,realString),stringDelimitList," , ")," ;\n ");
eqnstr = BackendDump.dumpEqnsStr(iEqns);
syst = stringAppendList({"\n",eqnstr,"\n[\n ", jacStr, "\n]\n *\n[\n ",varnames,"\n]\n =\n[\n ",rhsStr,"\n]"});
if intGt(linInfo,0) then
Error.addMessage(Error.LINEAR_SYSTEM_SINGULAR, {syst,infoStr,varname});
end if;
syst = stringAppendList({eqnstr,"\n[", jacStr, "] * [",varnames,"] = [",rhsStr,"]"});
if intLt(linInfo,0) then
Error.addMessage(Error.LINEAR_SYSTEM_INVALID, {"LAPACK/dgesv",syst});
end if;
then
false;
else true;
end matchcontinue;
end analyzeConstantJacobian;
protected function transformXToXd "author: PA
this function transforms x variables (in the state vector)
to corresponding xd variable (in the derivatives vector)"
input BackendDAE.Var inVar;
output BackendDAE.Var outVar;
algorithm
outVar := match (inVar)
local
DAE.ComponentRef cr;
DAE.VarDirection dir;
DAE.VarParallelism prl;
BackendDAE.Type tp;
Option<DAE.Exp> exp;
Option<Values.Value> v;
list<DAE.Dimension> dim;
Option<DAE.VariableAttributes> attr;
Option<BackendDAE.TearingSelect> ts;
Option<SCode.Comment> comment;
DAE.ConnectorType ct;
DAE.ElementSource source;
case (BackendDAE.VAR(varName = cr,
varKind = BackendDAE.STATE(),
varDirection = dir,
varParallelism = prl,
varType = tp,
bindExp = exp,
bindValue = v,
arryDim = dim,
source = source,
values = attr,
tearingSelectOption = ts,
comment = comment,
connectorType = ct))
equation
cr = ComponentReference.crefPrefixDer(cr);
then
BackendDAE.VAR(cr,BackendDAE.STATE_DER(),dir,prl,tp,exp,v,dim,source,attr,ts,comment,ct);
else inVar;
end match;
end transformXToXd;
public function getEquationAndSolvedVar "author: PA
Retrieves the equation and the variable solved in that equation
given an equation number and the variable assignments2"
input BackendDAE.StrongComponent inComp;
input BackendDAE.EquationArray inEquationArray;
input BackendDAE.Variables inVariables;
output list<BackendDAE.Equation> outEquation;
output list<BackendDAE.Var> outVar;
output Integer outIndex;
algorithm
(outEquation, outVar, outIndex) := matchcontinue (inComp, inEquationArray, inVariables)
local
Integer v, e;
list<Integer> elst, vlst;
BackendDAE.Equation eqn;
BackendDAE.Var var;
list<BackendDAE.Equation> eqnlst, eqnlst1;
list<BackendDAE.Var> varlst, varlst1;
BackendDAE.EquationArray eqns;
BackendDAE.Variables vars;
BackendDAE.StrongComponent comp;
list<tuple<Integer, list<Integer>>> eqnvartpllst;
case (BackendDAE.SINGLEEQUATION(eqn=e, var=v), eqns, vars)
equation
eqn = BackendEquation.equationNth1(eqns, e);
var = BackendVariable.getVarAt(vars, v);
then
({eqn}, {var}, e);
case (BackendDAE.EQUATIONSYSTEM(eqns=elst, vars=vlst), eqns, vars)
equation
eqnlst = BackendEquation.getEqns(elst, eqns);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
e = List.first(elst);
then
(eqnlst, varlst, e);
case (BackendDAE.SINGLEARRAY(eqn=e, vars=vlst), eqns, vars)
equation
eqn = BackendEquation.equationNth1(eqns, e);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
then
({eqn}, varlst, e);
case (BackendDAE.SINGLEIFEQUATION(eqn=e, vars=vlst), eqns, vars)
equation
eqn = BackendEquation.equationNth1(eqns, e);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
then
({eqn}, varlst, e);
case (BackendDAE.SINGLEALGORITHM(eqn=e, vars=vlst), eqns, vars)
equation
eqn = BackendEquation.equationNth1(eqns, e);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
then
({eqn}, varlst, e);
case (BackendDAE.SINGLECOMPLEXEQUATION(eqn=e, vars=vlst), eqns, vars)
equation
eqn = BackendEquation.equationNth1(eqns, e);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
then
({eqn}, varlst, e);
case (BackendDAE.SINGLEWHENEQUATION(eqn=e, vars=vlst), eqns, vars)
equation
eqn = BackendEquation.equationNth1(eqns, e);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
then
({eqn}, varlst, e);
case (BackendDAE.TORNSYSTEM(tearingvars=vlst, residualequations=elst, otherEqnVarTpl=eqnvartpllst), eqns, vars)
equation
eqnlst = BackendEquation.getEqns(elst, eqns);
varlst = List.map1r(vlst, BackendVariable.getVarAt, vars);
eqnlst1 = BackendEquation.getEqns(List.map(eqnvartpllst, Util.tuple21), eqns);
varlst1 = List.map1r(List.flatten(List.map(eqnvartpllst, Util.tuple22)), BackendVariable.getVarAt, vars);
eqnlst = listAppend(eqnlst, eqnlst1);
varlst = listAppend(varlst, varlst1);
e = List.first(elst);
then
(eqnlst, varlst, e);
case (_, _, _)
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("BackendDAETransform.getEquationAndSolvedVar failed!");
then
fail();
end matchcontinue;
end getEquationAndSolvedVar;
protected function getEquationAndSolvedVar_Internal
"author: PA
Retrieves the equation and the variable solved in that equation
given an equation number and the variable assignments2"
input Integer inInteger;
input BackendDAE.EquationArray inEquationArray;
input BackendDAE.Variables inVariables;
input array<Integer> inIntegerArray;
output BackendDAE.Equation outEquation;
output tuple<BackendDAE.Var,Integer> outVar;
algorithm
(outEquation,outVar):=
matchcontinue (inInteger,inEquationArray,inVariables,inIntegerArray)
local
Integer v,e;
BackendDAE.Equation eqn;
BackendDAE.Var var;
BackendDAE.EquationArray eqns;
BackendDAE.Variables vars;
array<Integer> ass2;
case (e,eqns,vars,ass2) /* equation no. assignments2 */
equation
eqn = BackendEquation.equationNth1(eqns, e);
v = ass2[e];
var = BackendVariable.getVarAt(vars, v);
then
(eqn,(var,v));
case (e,_,_,_) /* equation no. assignments2 */
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("BackendDAETransform.getEquationAndSolvedVar_Internal failed at index: " + intString(e));
then
fail();
end matchcontinue;
end getEquationAndSolvedVar_Internal;
public function getEquationAndSolvedVarIndxes
"author: Frenkel TUD
Retrieves the equation and the variable indexes solved in that equation
given an equation number and the variable assignments2"
input BackendDAE.StrongComponent inComp;
output list<Integer> outEquation;
output list<Integer> outVar;
algorithm
(outEquation,outVar):=
matchcontinue(inComp)
local
Integer v,e;
list<Integer> elst,vlst,elst1,vlst1;
BackendDAE.StrongComponent comp;
list<tuple<Integer,list<Integer>>> eqnvartpllst;
case (BackendDAE.SINGLEEQUATION(eqn=e,var=v))
then
({e},{v});
case BackendDAE.EQUATIONSYSTEM(eqns=elst,vars=vlst)
then
(elst,vlst);
case BackendDAE.SINGLEARRAY(eqn=e,vars=vlst)
then
({e},vlst);
case BackendDAE.SINGLEIFEQUATION(eqn=e,vars=vlst)
then
({e},vlst);
case BackendDAE.SINGLEALGORITHM(eqn=e,vars=vlst)
then
({e},vlst);
case BackendDAE.SINGLECOMPLEXEQUATION(eqn=e,vars=vlst)
then
({e},vlst);
case BackendDAE.SINGLEWHENEQUATION(eqn=e,vars=vlst)
then
({e},vlst);
case BackendDAE.TORNSYSTEM(tearingvars=vlst, residualequations=elst, otherEqnVarTpl=eqnvartpllst)
equation
elst1 = List.map(eqnvartpllst,Util.tuple21);
vlst1 = List.flatten(List.map(eqnvartpllst,Util.tuple22));
elst = listAppend(elst1,elst);
vlst = listAppend(vlst1,vlst);
then
(elst,vlst);
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("BackendDAETransform.getEquationAndSolvedVarIndxes failed!");
then
fail();
end matchcontinue;
end getEquationAndSolvedVarIndxes;
public function tarjanAlgorithm "author: PA
This is the second part of the BLT sorting. It takes the variable
assignments and the incidence matrix as input and identifies strong
components, i.e. subsystems of equations.
inputs: (BackendDAE.IncidenceMatrixT, int vector)
outputs: (int list list /* list of components */ )
"
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> ass2 "ass[eqnindx]=varindx";
output list<list<Integer>> outComps;
algorithm
outComps :=
matchcontinue (mt,ass2)
local
Integer n;
list<list<Integer>> comps;
array<Integer> number,lowlink;
array<Boolean> stackflag;
case (_,_)
equation
n = arrayLength(ass2);
number = arrayCreate(n,0);
lowlink = arrayCreate(n,0);
stackflag = arrayCreate(n,false);
(_,comps) = strongConnectMain(mt, ass2, number, lowlink, stackflag, n, 1, {}, {});
then
comps;
else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/function tarjansAlgorithm failed
The sorting of the equations could not be done. (strongComponents failed)
Use +d=failtrace for more information."});
then fail();
end matchcontinue;
end tarjanAlgorithm;
public function strongConnectMain
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
input array<Integer> lowlink;
input array<Boolean> stackflag;
input Integer n;
input Integer w;
input list<Integer> istack;
input list<list<Integer>> icomps;
output list<Integer> ostack;
output list<list<Integer>> ocomps;
algorithm
(ostack,ocomps) := strongConnectMain2(w>n,mt,a2,number,lowlink,stackflag,n,w,istack,icomps);
end strongConnectMain;
protected function strongConnectMain2
input Boolean stop;
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
input array<Integer> lowlink;
input array<Boolean> stackflag;
input Integer n;
input Integer w;
input list<Integer> istack;
input list<list<Integer>> icomps;
output list<Integer> ostack;
output list<list<Integer>> ocomps;
algorithm
(ostack,ocomps) := match (stop,mt,a2,number,lowlink,stackflag,n,w,istack,icomps)
local
Integer num;
list<Integer> stack;
list<list<Integer>> comps;
case (true,_,_,_,_,_,_,_,_,_)
then (istack,icomps);
else
equation
(stack,comps) = strongConnectMain3(intEq(number[w],0),mt,a2,number,lowlink,stackflag,n,w,istack,icomps);
(stack,comps) = strongConnectMain2(w+1>n,mt,a2,number,lowlink, stackflag, n, w + 1, stack, comps);
then (stack,comps);
end match;
end strongConnectMain2;
protected function strongConnectMain3
input Boolean doCalc;
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
input array<Integer> lowlink;
input array<Boolean> stackflag;
input Integer n;
input Integer w;
input list<Integer> istack;
input list<list<Integer>> icomps;
output list<Integer> ostack;
output list<list<Integer>> ocomps;
algorithm
(ostack,ocomps) := match (doCalc,mt,a2,number,lowlink,stackflag,n,w,istack,icomps)
local
Integer num;
list<Integer> stack;
list<list<Integer>> comps;
case (true,_,_,_,_,_,_,_,_,_)
equation
(_,stack,comps) = strongConnect(mt,a2,number,lowlink,stackflag,0,w,istack,icomps);
then (stack,comps);
else (istack,icomps);
end match;
end strongConnectMain3;
protected function strongConnect "author: PA
Helper function to strong_connect_main
inputs: (IncidenceMatrix, BackendDAE.IncidenceMatrixT, int vector, int vector,
int /* i */, int /* v */, int list /* stack */, int list list /* components */)
outputs: (int /* i */, int list /* stack */, int list list /* components */ )
"
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
input array<Integer> lowlink;
input array<Boolean> stackflag;
input Integer i;
input Integer v;
input list<Integer> stack;
input list<list<Integer>> comps;
output Integer oi;
output list<Integer> ostack;
output list<list<Integer>> ocomps;
algorithm
(oi,ostack,ocomps):=
matchcontinue (mt,a2,number,lowlink,stackflag,i,v,stack,comps)
local
Integer i_1;
list<Integer> stack_1,eqns,stack_2,stack_3,comp;
list<list<Integer>> comps_1,comps_2;
case (_,_,_,_,_,_,_,_,_)
equation
i_1 = i + 1;
arrayUpdate(number,v,i_1);
arrayUpdate(lowlink,v,i_1);
stack_1 = (v :: stack);
arrayUpdate(stackflag,v,true);
eqns = reachableNodes(v, mt, a2);
(i_1,stack_2,comps_1) = iterateReachableNodes(eqns, mt, a2, number, lowlink, stackflag, i_1, v, stack_1, comps);
(stack_3,comp) = checkRoot(v, stack_2, number, lowlink, stackflag);
comps_2 = consIfNonempty(comp, comps_1);
then
(i_1,stack_3,comps_2);
else
equation
Debug.traceln("- BackendDAETransform.strongConnect failed for eqn " + intString(v));
then
fail();
end matchcontinue;
end strongConnect;
protected function consIfNonempty "author: PA
Small helper function to avoid empty sublists.
Consider moving to Util?
"
input list<Integer> inIntegerLst;
input list<list<Integer>> inIntegerLstLst;
output list<list<Integer>> outIntegerLstLst;
algorithm
outIntegerLstLst:=
matchcontinue (inIntegerLst,inIntegerLstLst)
local
list<list<Integer>> lst;
list<Integer> e;
case ({},lst) then lst;
case (e,lst) then (e :: lst);
end matchcontinue;
end consIfNonempty;
public function reachableNodes "author: PA
Helper function to strong_connect.
Returns a list of reachable nodes (equations), corresponding
to those equations that uses the solved variable of this equation.
The edges of the graph that identifies strong components/blocks are
dependencies between blocks. A directed edge e = (n1,n2) means
that n1 solves for a variable (e.g. \'a\') that is used in the equation
of n2, i.e. the equation of n1 must be solved before the equation of n2.
"
input Integer eqn;
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
output list<Integer> outIntegerLst;
algorithm
outIntegerLst:=
matchcontinue (eqn,mt,a2)
local
Integer var;
list<Integer> reachable,reachable_1;
String eqnstr;
case (_,_,_)
equation
var = a2[eqn] "Got the variable that is solved in the equation";
reachable = if intGt(var,0) then arrayGet(mt,var) else {} "Got the equations of that variable";
reachable_1 = BackendDAEUtil.removeNegative(reachable) "in which other equations is this variable present ?";
then
List.removeOnTrue(eqn, intEq, reachable_1);
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.trace("-reachable_nodes failed, eqn: ");
eqnstr = intString(eqn);
Debug.traceln(eqnstr);
then
fail();
end matchcontinue;
end reachableNodes;
protected function iterateReachableNodes
input list<Integer> eqns;
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
input array<Integer> lowlink;
input array<Boolean> stackflag;
input Integer i;
input Integer v;
input list<Integer> istack;
input list<list<Integer>> icomps;
output Integer outI;
output list<Integer> outStack;
output list<list<Integer>> outComps;
algorithm
(outI,outStack,outComps) := match (eqns,mt,a2,number,lowlink,stackflag,i,v,istack,icomps)
local
Integer i1,lv,lw,minv,w,nw,nv;
list<Integer> stack,ws;
list<list<Integer>> comps_1,comps_2,comps;
// empty case
case ({},_,_,_,_,_,_,_,_,_) then (i,istack,icomps);
case (w :: ws,_,_,_,_,_,i1,_,stack,comps)
equation
(i1,stack,comps) = iterateReachableNodes2(w, mt, a2, number, lowlink, stackflag, i1, v, stack, comps);
(i1,stack,comps) = iterateReachableNodes(ws, mt, a2, number, lowlink, stackflag, i1, v, stack, comps);
then (i1,stack,comps);
end match;
end iterateReachableNodes;
protected function iterateReachableNodes2
input Integer eqn;
input BackendDAE.IncidenceMatrixT mt;
input array<Integer> a2;
input array<Integer> number;
input array<Integer> lowlink;