/
BackendDAETransform.mo
3580 lines (3369 loc) · 143 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-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 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;
public import HashTableCG;
protected import BackendDAEEXT;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVariable;
protected import BaseHashTable;
protected import ComponentReference;
protected import DAEUtil;
protected import Debug;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import Derive;
protected import Error;
protected import RTOpts;
protected import SCode;
protected import Util;
protected import Values;
/******************************************
matchingAlgorithm and stuff
*****************************************/
public function matchingAlgorithm
"function: matchingAlgorithm
author: PA
This function performs the matching algorithm, which is the first
part of sorting the equations into BLT (Block Lower Triangular) form.
The matching algorithm finds a variable that is solved in each equation.
But to also find out which equations forms a block of equations, the
the second algorithm of the BLT sorting: strong components
algorithm is run.
This function returns the updated DAE in case of index reduction has
added equations and variables, and the incidence matrix. The variable
assignments is returned as a vector of variable indices, as well as its
inverse, i.e. which equation a variable is solved in as a vector of
equation indices.
BackendDAE.MatchingOptions contain options given to the algorithm.
- if index reduction should be used or not.
- if the equation system is allowed to be under constrained or not
which is used when generating code for initial equations.
inputs: (BackendDAE,IncidenceMatrix, BackendDAE.IncidenceMatrixT, MatchingOptions)
outputs: (int vector /* vector of equation indices */ ,
int vector /* vector of variable indices */,
BackendDAE,IncidenceMatrix, IncidenceMatrixT)"
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.IncidenceMatrix inIncidenceMatrix;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT;
input BackendDAE.MatchingOptions inMatchingOptions;
input daeHandlerFunc daeHandler;
input DAE.FunctionTree inFunctions;
output array<Integer> outIntegerArray1;
output array<Integer> outIntegerArray2;
output BackendDAE.BackendDAE outBackendDAE3;
output BackendDAE.IncidenceMatrix outIncidenceMatrix4;
output BackendDAE.IncidenceMatrixT outIncidenceMatrixT5;
partial function daeHandlerFunc
input BackendDAE.DAEHandlerJop inJop;
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.IncidenceMatrix inIncidenceMatrix;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT;
input DAE.FunctionTree inFunctions;
input list<tuple<Integer,Integer,Integer>> inDerivedAlgs;
input list<tuple<Integer,Integer,Integer>> inDerivedMultiEqn;
input BackendDAE.DAEHandlerArg inArg;
output BackendDAE.BackendDAE outBackendDAE;
output BackendDAE.IncidenceMatrix outIncidenceMatrix;
output BackendDAE.IncidenceMatrixT outIncidenceMatrixT;
output list<tuple<Integer,Integer,Integer>> outDerivedAlgs;
output list<tuple<Integer,Integer,Integer>> outDerivedMultiEqn;
output BackendDAE.DAEHandlerArg outArg;
output Boolean outRunMatching;
end daeHandlerFunc;
algorithm
(outIntegerArray1,outIntegerArray2,outBackendDAE3,outIncidenceMatrix4,outIncidenceMatrixT5) :=
matchcontinue (inBackendDAE,inIncidenceMatrix,inIncidenceMatrixT,inMatchingOptions,daeHandler,inFunctions)
local
BackendDAE.Value nvars,neqns,memsize;
String ns,ne;
BackendDAE.Assignments assign1,assign2,ass1,ass2;
BackendDAE.BackendDAE dae;
array<list<BackendDAE.Value>> m,mt,m_1,mt_1;
array<BackendDAE.Value> vec1,vec2;
BackendDAE.MatchingOptions match_opts;
BackendDAE.DAEHandlerArg arg,arg1,arg2;
/* fail case if daelow is empty */
case (dae,m,mt,_,_,_)
equation
nvars = arrayLength(m);
neqns = arrayLength(mt);
(nvars == 0) = true;
(neqns == 0) = true;
vec1 = listArray({});
vec2 = listArray({});
then
(vec1,vec2,dae,m,mt);
case (dae,m,mt,match_opts,daeHandler,inFunctions)
equation
BackendDAEEXT.clearDifferentiated();
checkMatching(dae, match_opts);
nvars = arrayLength(m);
neqns = arrayLength(mt);
ns = intString(nvars);
ne = intString(neqns);
(nvars > 0) = true;
(neqns > 0) = true;
memsize = nvars + nvars "Worst case, all eqns are differentiated once. Create nvars2 assignment elements" ;
assign1 = assignmentsCreate(nvars, memsize, 0);
assign2 = assignmentsCreate(nvars, memsize, 0);
arg = emptyDAEHandlerArg();
(dae,m,mt,_,_,arg1,_) = daeHandler(BackendDAE.STARTSTEP(),dae, m, mt,inFunctions,{},{},arg);
(ass1,ass2,dae,m_1,mt_1,_,_,arg2) = matchingAlgorithm2(dae, m, mt, nvars, neqns, 1, assign1, assign2, match_opts, daeHandler, arg1, inFunctions, {}, {});
(dae,m_1,mt_1,_,_,_,_) = daeHandler(BackendDAE.ENDSTEP(),dae, m_1, mt_1,inFunctions,{},{},arg2);
vec1 = assignmentsVector(ass1);
vec2 = assignmentsVector(ass2);
then
(vec1,vec2,dae,m_1,mt_1);
case (_,_,_,_,_,_)
equation
Debug.fprint("failtrace", "- BackendDAE.MatchingAlgorithm failed\n");
then
fail();
end matchcontinue;
end matchingAlgorithm;
public function checkMatching
"function: checkMatching
author: PA
Checks that the matching is correct, i.e. that the number of variables
is the same as the number of equations. If not, the function fails and
prints an error message.
If matching options indicate that underconstrained systems are ok, no
check is performed."
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.MatchingOptions inMatchingOptions;
algorithm
_ := matchcontinue (inBackendDAE,inMatchingOptions)
local
BackendDAE.Value esize,vars_size;
BackendDAE.EquationArray eqns;
String esize_str,vsize_str;
case (_,(_,BackendDAE.ALLOW_UNDERCONSTRAINED())) then ();
case (BackendDAE.DAE(orderedVars = BackendDAE.VARIABLES(numberOfVars = vars_size),orderedEqs = eqns),_)
equation
esize = BackendDAEUtil.equationSize(eqns);
((esize) == vars_size) = true;
then
();
case (BackendDAE.DAE(orderedVars = BackendDAE.VARIABLES(numberOfVars = vars_size),orderedEqs = eqns),_)
equation
esize = BackendDAEUtil.equationSize(eqns);
(esize < vars_size) = true;
esize = esize - 1;
vars_size = vars_size - 1 "remove dummy var" ;
esize_str = intString(esize) "remove dummy var" ;
vsize_str = intString(vars_size);
Error.addMessage(Error.UNDERDET_EQN_SYSTEM, {esize_str,vsize_str});
then
fail();
case (BackendDAE.DAE(orderedVars = BackendDAE.VARIABLES(numberOfVars = vars_size),orderedEqs = eqns),_)
equation
esize = BackendDAEUtil.equationSize(eqns);
(esize > vars_size) = true;
esize = esize - 1;
vars_size = vars_size - 1 "remove dummy var" ;
esize_str = intString(esize) "remove dummy var" ;
vsize_str = intString(vars_size);
Error.addMessage(Error.OVERDET_EQN_SYSTEM, {esize_str,vsize_str});
then
fail();
case (_,_)
equation
Debug.fprint("failtrace", "- BackendDAE.checkMatching failed\n");
then
fail();
end matchcontinue;
end checkMatching;
public function assignmentsVector
"function: assignmentsVector
author: PA
Converts BackendDAE.Assignments to vector of int elements"
input BackendDAE.Assignments inAssignments;
output array<Integer> outIntegerArray;
algorithm
outIntegerArray := matchcontinue (inAssignments)
local
array<BackendDAE.Value> newarr,newarr_1,arr;
array<BackendDAE.Value> vec;
BackendDAE.Value size;
case (BackendDAE.ASSIGNMENTS(actualSize = size,arrOfIndices = arr))
equation
newarr = arrayCreate(size, 0);
newarr_1 = Util.arrayNCopy(arr, newarr, size);
vec = arrayCopy(newarr_1);
then
vec;
case (_)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"- BackendDAETransform.assignmentsVector failed!"});
then
fail();
end matchcontinue;
end assignmentsVector;
public function assignmentsCreate
"function: assignmentsCreate
author: PA
Creates an assignment array of n elements, filled with value v
inputs: (int /* size */, int /* memsize */, int)
outputs: => Assignments"
input Integer n;
input Integer memsize;
input Integer v;
output BackendDAE.Assignments outAssignments;
list<BackendDAE.Value> lst;
array<BackendDAE.Value> arr;
algorithm
arr := arrayCreate(memsize, 0);
outAssignments := BackendDAE.ASSIGNMENTS(n,memsize,arr);
end assignmentsCreate;
protected function assignmentsSetnth
"function: assignmentsSetnth
author: PA
Sets the n:nt assignment Value.
inputs: (Assignments, int /* n */, int /* value */)
outputs: Assignments"
input BackendDAE.Assignments inAssignments1;
input Integer inInteger2;
input Integer inInteger3;
output BackendDAE.Assignments outAssignments;
algorithm
outAssignments := matchcontinue (inAssignments1,inInteger2,inInteger3)
local
array<BackendDAE.Value> arr;
BackendDAE.Value s,ms,n,v;
case (BackendDAE.ASSIGNMENTS(actualSize = s,allocatedSize = ms,arrOfIndices = arr),n,v)
equation
arr = arrayUpdate(arr, n + 1, v);
then
BackendDAE.ASSIGNMENTS(s,ms,arr);
case (_,_,_)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"- BackendDAETransform.assignments_setnth failed!"});
then
fail();
end matchcontinue;
end assignmentsSetnth;
protected function assignmentsExpand
"function: assignmentsExpand
author: PA
Expands the assignments array with n values, initialized with zero.
inputs: (Assignments, int /* n */)
outputs: Assignments"
input BackendDAE.Assignments inAssignments;
input Integer inInteger;
output BackendDAE.Assignments outAssignments;
algorithm
outAssignments := matchcontinue (inAssignments,inInteger)
local
BackendDAE.Assignments ass,ass_1,ass_2;
BackendDAE.Value n_1,n;
case (ass,0) then ass;
case (ass,n)
equation
true = n > 0;
ass_1 = assignmentsAdd(ass, 0);
n_1 = n - 1;
ass_2 = assignmentsExpand(ass_1, n_1);
then
ass_2;
case (ass,_)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"- BackendDAETransform.assignmentsExpand: n should not be negative!"});
then
fail();
end matchcontinue;
end assignmentsExpand;
protected function assignmentsAdd
"function: assignmentsAdd
author: PA
Adds a value to the end of the assignments array. If memsize = actual size
this means copying the whole array, expanding it size to fit the value
Expansion is made by a factor 1.4. Otherwise, the element is inserted taking O(1) in
insertion cost.
inputs: (Assignments, int /* value */)
outputs: Assignments"
input BackendDAE.Assignments inAssignments;
input Integer inInteger;
output BackendDAE.Assignments outAssignments;
algorithm
outAssignments := matchcontinue (inAssignments,inInteger)
local
Real msr,msr_1;
BackendDAE.Value ms_1,s_1,ms_2,s,ms,v;
array<BackendDAE.Value> arr_1,arr_2,arr;
case (BackendDAE.ASSIGNMENTS(actualSize = s,allocatedSize = ms,arrOfIndices = arr),v)
equation
(s == ms) = true "Out of bounds, increase and copy." ;
msr = intReal(ms);
msr_1 = msr *. 0.4;
ms_1 = realInt(msr_1);
s_1 = s + 1;
ms_2 = ms_1 + ms;
arr_1 = Util.arrayExpand(ms_1, arr, 0);
arr_2 = arrayUpdate(arr_1, s + 1, v);
then
BackendDAE.ASSIGNMENTS(s_1,ms_2,arr_2);
case (BackendDAE.ASSIGNMENTS(actualSize = s,allocatedSize = ms,arrOfIndices = arr),v)
equation
arr_1 = arrayUpdate(arr, s + 1, v) "space available, increase size and insert element." ;
s_1 = s + 1;
then
BackendDAE.ASSIGNMENTS(s_1,ms,arr_1);
case (BackendDAE.ASSIGNMENTS(actualSize = s,allocatedSize = ms,arrOfIndices = arr),v)
equation
Error.addMessage(Error.INTERNAL_ERROR, {"-BackendDAETranfrom.assignmentsAdd failed!"});
then
fail();
end matchcontinue;
end assignmentsAdd;
public function matchingAlgorithm2
"function: matchingAlgorithm2
author: PA
This is the outer loop of the matching algorithm
The find_path algorithm is called for each equation/variable.
inputs: (BackendDAE,IncidenceMatrix, IncidenceMatrixT
,int /* number of vars */
,int /* number of eqns */
,int /* current var */
,Assignments /* assignments, array of eqn indices */
,Assignments /* assignments, array of var indices */
,MatchingOptions) /* options for matching alg. */
outputs: (Assignments, /* assignments, array of equation indices */
Assignments, /* assignments, list of variable indices */
BackendDAE, BackendDAE.IncidenceMatrix, IncidenceMatrixT)"
input BackendDAE.BackendDAE inBackendDAE1;
input BackendDAE.IncidenceMatrix inIncidenceMatrix2;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT3;
input Integer inInteger4;
input Integer inInteger5;
input Integer inInteger6;
input BackendDAE.Assignments inAssignments7;
input BackendDAE.Assignments inAssignments8;
input BackendDAE.MatchingOptions inMatchingOptions9;
input daeHandlerFunc daeHandler;
input BackendDAE.DAEHandlerArg inArg;
input DAE.FunctionTree inFunctions;
input list<tuple<Integer,Integer,Integer>> inDerivedAlgs;
input list<tuple<Integer,Integer,Integer>> inDerivedMultiEqn;
output BackendDAE.Assignments outAssignments1;
output BackendDAE.Assignments outAssignments2;
output BackendDAE.BackendDAE outBackendDAE3;
output BackendDAE.IncidenceMatrix outIncidenceMatrix4;
output BackendDAE.IncidenceMatrixT outIncidenceMatrixT5;
output list<tuple<Integer,Integer,Integer>> outDerivedAlgs;
output list<tuple<Integer,Integer,Integer>> outDerivedMultiEqn;
output BackendDAE.DAEHandlerArg outArg;
partial function daeHandlerFunc
input BackendDAE.DAEHandlerJop inJop;
input BackendDAE.BackendDAE inBackendDAE;
input BackendDAE.IncidenceMatrix inIncidenceMatrix;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT;
input DAE.FunctionTree inFunctions;
input list<tuple<Integer,Integer,Integer>> inDerivedAlgs;
input list<tuple<Integer,Integer,Integer>> inDerivedMultiEqn;
input BackendDAE.DAEHandlerArg inArg;
output BackendDAE.BackendDAE outBackendDAE;
output BackendDAE.IncidenceMatrix outIncidenceMatrix;
output BackendDAE.IncidenceMatrixT outIncidenceMatrixT;
output list<tuple<Integer,Integer,Integer>> outDerivedAlgs;
output list<tuple<Integer,Integer,Integer>> outDerivedMultiEqn;
output BackendDAE.DAEHandlerArg outArg;
output Boolean outRunMatching;
end daeHandlerFunc;
algorithm
(outAssignments1,outAssignments2,outBackendDAE3,outIncidenceMatrix4,outIncidenceMatrixT5,outDerivedAlgs,outDerivedMultiEqn,outArg):=
matchcontinue (inBackendDAE1,inIncidenceMatrix2,inIncidenceMatrixT3,inInteger4,inInteger5,inInteger6,inAssignments7,inAssignments8,inMatchingOptions9,daeHandler,inArg,inFunctions,inDerivedAlgs,inDerivedMultiEqn)
local
BackendDAE.Assignments ass1_1,ass2_1,ass1,ass2,ass1_2,ass2_2;
BackendDAE.BackendDAE dae;
array<list<BackendDAE.Value>> m,mt;
BackendDAE.Value nv,nf,i,i_1,nv_1,nkv,nf_1,nvd;
BackendDAE.MatchingOptions match_opts;
BackendDAE.EquationArray eqns;
BackendDAE.EquationConstraints eq_cons;
list<BackendDAE.Value> eqn_lst,var_lst;
String eqn_str,var_str;
list<tuple<Integer,Integer,Integer>> derivedAlgs,derivedAlgs1,derivedAlgs2;
list<tuple<Integer,Integer,Integer>> derivedMultiEqn,derivedMultiEqn1,derivedMultiEqn2;
BackendDAE.DAEHandlerArg arg,arg1;
DAE.ElementSource source;
Absyn.Info info;
case (dae,m,mt,nv,nf,i,ass1,ass2,_,_,inArg,_,derivedAlgs,derivedMultiEqn)
equation
(nv == i) = true;
BackendDAEEXT.initMarks(nv, nf);
(ass1_1,ass2_1) = pathFound(m, mt, i, ass1, ass2) "eMark(i)=vMark(i)=false; eMark(i)=vMark(i)=false exit loop";
then
(ass1_1,ass2_1,dae,m,mt,derivedAlgs,derivedMultiEqn,inArg);
case (dae,m,mt,nv,nf,i,ass1,ass2,match_opts,daeHandler,inArg,inFunctions,derivedAlgs,derivedMultiEqn)
equation
i_1 = i + 1;
BackendDAEEXT.initMarks(nv, nf);
(ass1_1,ass2_1) = pathFound(m, mt, i, ass1, ass2) "eMark(i)=vMark(i)=false" ;
(ass1_2,ass2_2,dae,m,mt,derivedAlgs1,derivedMultiEqn1,arg) = matchingAlgorithm2(dae, m, mt, nv, nf, i_1, ass1_1, ass2_1, match_opts, daeHandler, inArg, inFunctions,derivedAlgs,derivedMultiEqn);
then
(ass1_2,ass2_2,dae,m,mt,derivedAlgs1,derivedMultiEqn1,arg);
case (dae,m,mt,nv,nf,i,ass1,ass2,match_opts as (BackendDAE.INDEX_REDUCTION(),eq_cons),daeHandler,inArg,inFunctions,derivedAlgs,derivedMultiEqn)
equation
(dae,m,mt,derivedAlgs1,derivedMultiEqn1,arg,_) = daeHandler(BackendDAE.REDUCE_INDEX(),dae, m, mt, inFunctions,derivedAlgs,derivedMultiEqn,inArg)
"path_found failed, Try index reduction using dummy derivatives.
When a constraint exist between states and index reduction is needed
the dummy derivative will select one of the states as a dummy state
(and the derivative of that state as a dummy derivative).
For instance, u1=u2 is a constraint between states. Choose u1 as dummy state
and der(u1) as dummy derivative, named der_u1. The differentiated function
then becomes: der_u1 = der(u2).
In the dummy derivative method this equation is added and the original equation
u1=u2 is kept. This is not the case for the original pantilides algorithm, where
the original equation is removed from the system." ;
eqns = BackendEquation.daeEqns(dae);
nf_1 = BackendDAEUtil.equationSize(eqns) "and try again, restarting. This could be optimized later. It should not
be necessary to restart the matching, according to Bernard Bachmann. Instead one
could continue the matching as usual. This was tested (2004-11-22) and it does not
work to continue without restarting.
For instance the Influenca model \"../testsuite/mofiles/Influenca.mo\" does not work if
not restarting.
2004-12-29 PA. This was a bug, assignment lists needed to be expanded with the size
of the system in order to work. SO: Matching is not needed to be restarted from
scratch." ;
nv_1 = BackendVariable.varsSize(BackendVariable.daeVars(dae));
nvd = nv_1 - nv;
ass1_1 = assignmentsExpand(ass1, nvd);
ass2_1 = assignmentsExpand(ass2, nvd);
(ass1_2,ass2_2,dae,m,mt,derivedAlgs2,derivedMultiEqn2,arg1) = matchingAlgorithm2(dae,m,mt,nv_1,nf_1,i,ass1_1,ass2_1,match_opts,daeHandler,arg,inFunctions,derivedAlgs1,derivedMultiEqn1);
then
(ass1_2,ass2_2,dae,m,mt,derivedAlgs2,derivedMultiEqn2,arg1);
case (dae,_,_,_,_,_,_,_,_,_,_,_,_,_)
equation
eqn_lst = BackendDAEEXT.getMarkedEqns() "When index reduction also fails, the model is structurally singular." ;
var_lst = BackendDAEEXT.getMarkedVariables();
eqn_str = BackendDump.dumpMarkedEqns(dae, eqn_lst);
var_str = BackendDump.dumpMarkedVars(dae, var_lst);
i::_ = eqn_lst;
source = BackendEquation.markedEquationSource(dae, i);
info = DAEUtil.getElementSourceFileInfo(source);
Error.addSourceMessage(Error.STRUCT_SINGULAR_SYSTEM, {eqn_str,var_str}, info);
//print("structurally singular. IM:");
//dumpIncidenceMatrix(m);
//print("daelow:");
//dump(dae);
then
fail();
end matchcontinue;
end matchingAlgorithm2;
protected function pathFound "function: pathFound
author: PA
This function is part of the matching algorithm.
It tries to find a matching for the equation index given as
third argument, i.
inputs: (IncidenceMatrix, BackendDAE.IncidenceMatrixT, int /* equation */,
Assignments, Assignments)
outputs: (Assignments, Assignments)
"
input BackendDAE.IncidenceMatrix inIncidenceMatrix1;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT2;
input Integer inInteger3;
input BackendDAE.Assignments inAssignments4;
input BackendDAE.Assignments inAssignments5;
output BackendDAE.Assignments outAssignments1;
output BackendDAE.Assignments outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
matchcontinue (inIncidenceMatrix1,inIncidenceMatrixT2,inInteger3,inAssignments4,inAssignments5)
local
BackendDAE.Assignments ass1_1,ass2_1,ass1,ass2;
array<list<BackendDAE.Value>> m,mt;
BackendDAE.Value i;
case (m,mt,i,ass1,ass2)
equation
BackendDAEEXT.eMark(i) "Side effect" ;
(ass1_1,ass2_1) = assignOneInEqn(m, mt, i, ass1, ass2);
then
(ass1_1,ass2_1);
case (m,mt,i,ass1,ass2)
equation
(ass1_1,ass2_1) = forallUnmarkedVarsInEqn(m, mt, i, ass1, ass2);
then
(ass1_1,ass2_1);
end matchcontinue;
end pathFound;
protected function assignOneInEqn "function: assignOneInEqn
author: PA
Helper function to path_found.
"
input BackendDAE.IncidenceMatrix inIncidenceMatrix1;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT2;
input Integer inInteger3;
input BackendDAE.Assignments inAssignments4;
input BackendDAE.Assignments inAssignments5;
output BackendDAE.Assignments outAssignments1;
output BackendDAE.Assignments outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
match (inIncidenceMatrix1,inIncidenceMatrixT2,inInteger3,inAssignments4,inAssignments5)
local
list<BackendDAE.Value> vars;
BackendDAE.Assignments ass1_1,ass2_1,ass1,ass2;
array<list<BackendDAE.Value>> m,mt;
BackendDAE.Value i;
case (m,mt,i,ass1,ass2)
equation
vars = BackendDAEUtil.varsInEqn(m, i);
(ass1_1,ass2_1) = assignFirstUnassigned(i, vars, ass1, ass2);
then
(ass1_1,ass2_1);
end match;
end assignOneInEqn;
protected function assignFirstUnassigned
"function: assignFirstUnassigned
author: PA
This function assigns the first unassign variable to the equation
given as first argument. It is part of the matching algorithm.
inputs: (int /* equation */,
int list /* variables */,
BackendDAE.Assignments /* ass1 */,
BackendDAE.Assignments /* ass2 */)
outputs: (Assignments, /* ass1 */
Assignments) /* ass2 */"
input Integer inInteger1;
input list<Integer> inIntegerLst2;
input BackendDAE.Assignments inAssignments3;
input BackendDAE.Assignments inAssignments4;
output BackendDAE.Assignments outAssignments1;
output BackendDAE.Assignments outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
matchcontinue (inInteger1,inIntegerLst2,inAssignments3,inAssignments4)
local
BackendDAE.Assignments ass1_1,ass2_1,ass1,ass2;
BackendDAE.Value i,v;
list<BackendDAE.Value> vs;
case (i,(v :: vs),ass1,ass2)
equation
0 = getAssigned(v, ass1, ass2);
(ass1_1,ass2_1) = assign(v, i, ass1, ass2);
then
(ass1_1,ass2_1);
case (i,(v :: vs),ass1,ass2)
equation
(ass1_1,ass2_1) = assignFirstUnassigned(i, vs, ass1, ass2);
then
(ass1_1,ass2_1);
end matchcontinue;
end assignFirstUnassigned;
protected function getAssigned
"function: getAssigned
author: PA
returns the assigned equation for a variable.
inputs: (int /* variable */,
Assignments, /* ass1 */
Assignments) /* ass2 */
outputs: int /* equation */"
input Integer inInteger1;
input BackendDAE.Assignments inAssignments2;
input BackendDAE.Assignments inAssignments3;
output Integer outInteger;
algorithm
outInteger:=
match (inInteger1,inAssignments2,inAssignments3)
local
BackendDAE.Value v;
array<BackendDAE.Value> m;
case (v,BackendDAE.ASSIGNMENTS(arrOfIndices = m),_) then m[v];
end match;
end getAssigned;
protected function assign
"function: assign
author: PA
Assign a variable to an equation, updating both assignment lists.
inputs: (int, /* variable */
int, /* equation */
Assignments, /* ass1 */
Assignments) /* ass2 */
outputs: (Assignments, /* updated ass1 */
Assignments) /* updated ass2 */"
input Integer inInteger1;
input Integer inInteger2;
input BackendDAE.Assignments inAssignments3;
input BackendDAE.Assignments inAssignments4;
output BackendDAE.Assignments outAssignments1;
output BackendDAE.Assignments outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
match (inInteger1,inInteger2,inAssignments3,inAssignments4)
local
BackendDAE.Value v_1,e_1,v,e;
BackendDAE.Assignments ass1_1,ass2_1,ass1,ass2;
case (v,e,ass1,ass2)
equation
v_1 = v - 1 "print \"assign \" & intString v => vs & intString e => es & print vs & print \" to eqn \" & print es & print \"\\n\" &" ;
e_1 = e - 1;
ass1_1 = assignmentsSetnth(ass1, v_1, e);
ass2_1 = assignmentsSetnth(ass2, e_1, v);
then
(ass1_1,ass2_1);
end match;
end assign;
protected function forallUnmarkedVarsInEqn
"function: forallUnmarkedVarsInEqn
author: PA
This function is part of the matching algorithm.
It loops over all umarked variables in an equation.
inputs: (IncidenceMatrix,
IncidenceMatrixT,
int,
BackendDAE.Assignments /* ass1 */,
BackendDAE.Assignments /* ass2 */)
outputs: (Assignments, Assignments)"
input BackendDAE.IncidenceMatrix inIncidenceMatrix1;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT2;
input Integer inInteger3;
input BackendDAE.Assignments inAssignments4;
input BackendDAE.Assignments inAssignments5;
output BackendDAE.Assignments outAssignments1;
output BackendDAE.Assignments outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
match (inIncidenceMatrix1,inIncidenceMatrixT2,inInteger3,inAssignments4,inAssignments5)
local
list<BackendDAE.Value> vars,vars_1;
BackendDAE.Assignments ass1_1,ass2_1,ass1,ass2;
array<list<BackendDAE.Value>> m,mt;
BackendDAE.Value i;
case (m,mt,i,ass1,ass2)
equation
vars = BackendDAEUtil.varsInEqn(m, i);
vars_1 = Util.listFilter(vars, isNotVMarked);
(ass1_1,ass2_1) = forallUnmarkedVarsInEqnBody(m, mt, i, vars_1, ass1, ass2);
then
(ass1_1,ass2_1);
end match;
end forallUnmarkedVarsInEqn;
protected function isNotVMarked
"function: isNotVMarked
author: PA
This function succeds for variables that are not marked."
input Integer i;
algorithm
false := BackendDAEEXT.getVMark(i);
end isNotVMarked;
protected function forallUnmarkedVarsInEqnBody
"function: forallUnmarkedVarsInEqnBody
author: PA
This function is part of the matching algorithm.
It is the body of the loop over all unmarked variables.
inputs: (IncidenceMatrix, BackendDAE.IncidenceMatrixT,
int,
int list /* var list */
Assignments
Assignments)
outputs: (Assignments, Assignments)"
input BackendDAE.IncidenceMatrix inIncidenceMatrix1;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT2;
input Integer inInteger3;
input list<Integer> inIntegerLst4;
input BackendDAE.Assignments inAssignments5;
input BackendDAE.Assignments inAssignments6;
output BackendDAE.Assignments outAssignments1;
output BackendDAE.Assignments outAssignments2;
algorithm
(outAssignments1,outAssignments2):=
matchcontinue (inIncidenceMatrix1,inIncidenceMatrixT2,inInteger3,inIntegerLst4,inAssignments5,inAssignments6)
local
BackendDAE.Value assarg,i,v;
BackendDAE.Assignments ass1_1,ass2_1,ass1_2,ass2_2,ass1,ass2;
array<list<BackendDAE.Value>> m,mt;
list<BackendDAE.Value> vars,vs;
case (m,mt,i,(vars as (v :: vs)),ass1,ass2)
equation
BackendDAEEXT.vMark(v);
assarg = getAssigned(v, ass1, ass2);
(ass1_1,ass2_1) = pathFound(m, mt, assarg, ass1, ass2);
(ass1_2,ass2_2) = assign(v, i, ass1_1, ass2_1);
then
(ass1_2,ass2_2);
case (m,mt,i,(vars as (v :: vs)),ass1,ass2)
equation
BackendDAEEXT.vMark(v);
(ass1_1,ass2_1) = forallUnmarkedVarsInEqnBody(m, mt, i, vs, ass1, ass2);
then
(ass1_1,ass2_1);
end matchcontinue;
end forallUnmarkedVarsInEqnBody;
/******************************************
strongComponents and stuff
*****************************************/
public function strongComponents "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.IncidenceMatrix inIncidenceMatrix1;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT2;
input array<Integer> inIntegerArray3;
input array<Integer> inIntegerArray4;
output list<list<Integer>> outIntegerLstLst;
algorithm
outIntegerLstLst:=
matchcontinue (inIncidenceMatrix1,inIncidenceMatrixT2,inIntegerArray3,inIntegerArray4)
local
BackendDAE.Value n,i;
list<BackendDAE.Value> stack;
list<list<BackendDAE.Value>> comps;
array<list<BackendDAE.Value>> m,mt;
array<BackendDAE.Value> ass1,ass2;
case (m,mt,ass1,ass2)
equation
n = arrayLength(m);
BackendDAEEXT.initLowLink(n);
BackendDAEEXT.initNumber(n);
(i,stack,comps) = strongConnectMain(m, mt, ass1, ass2, n, 0, 1, {}, {});
then
comps;
case (_,_,_,_)
equation
Debug.fprint("failtrace", "strong_components failed\n");
Error.addMessage(Error.INTERNAL_ERROR,
{"sorting equations(strong components failed)"});
then
fail();
end matchcontinue;
end strongComponents;
protected function strongConnectMain "function: strongConnectMain
author: PA
Helper function to strong_components
inputs: (IncidenceMatrix,
IncidenceMatrixT,
int vector, /* Assignment */
int vector, /* Assignment */
int, /* n - number of equations */
int, /* i */
int, /* w */
int list, /* stack */
int list list /* components */)
outputs: (int /* i */, int list /* stack */, int list list /* components */)
"
input BackendDAE.IncidenceMatrix inIncidenceMatrix1;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT2;
input array<Integer> inIntegerArray3;
input array<Integer> inIntegerArray4;
input Integer inInteger5;
input Integer inInteger6;
input Integer inInteger7;
input list<Integer> inIntegerLst8;
input list<list<Integer>> inIntegerLstLst9;
output Integer outInteger;
output list<Integer> outIntegerLst;
output list<list<Integer>> outIntegerLstLst;
algorithm
(outInteger,outIntegerLst,outIntegerLstLst):=
matchcontinue (inIncidenceMatrix1,inIncidenceMatrixT2,inIntegerArray3,inIntegerArray4,inInteger5,inInteger6,inInteger7,inIntegerLst8,inIntegerLstLst9)
local
array<list<BackendDAE.Value>> m,mt;
array<BackendDAE.Value> a1,a2;
BackendDAE.Value n,i,w,w_1,num;
list<BackendDAE.Value> stack,stack_1,stack_2;
list<list<BackendDAE.Value>> comp, comps;
case (m,mt,a1,a2,n,i,w,stack,comp)
equation
(w > n) = true;
then
(i,stack,comp);
case (m,mt,a1,a2,n,i,w,stack,comps)
equation
0 = BackendDAEEXT.getNumber(w);
(i,stack_1,comps) = strongConnect(m, mt, a1, a2, i, w, stack, comps);
w_1 = w + 1;
(i,stack_2,comps) = strongConnectMain(m, mt, a1, a2, n, i, w_1, stack_1, comps);
then
(i,stack_2,comps);
case (m,mt,a1,a2,n,i,w,stack,comps)
equation
num = BackendDAEEXT.getNumber(w);
(num == 0) = false;
w_1 = w + 1;
(i,stack_1,comps) = strongConnectMain(m, mt, a1, a2, n, i, w_1, stack, comps);
then
(i,stack_1,comps);
end matchcontinue;
end strongConnectMain;
protected function strongConnect "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.IncidenceMatrix inIncidenceMatrix1;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT2;
input array<Integer> inIntegerArray3;
input array<Integer> inIntegerArray4;
input Integer inInteger5;
input Integer inInteger6;
input list<Integer> inIntegerLst7;
input list<list<Integer>> inIntegerLstLst8;
output Integer outInteger;
output list<Integer> outIntegerLst;
output list<list<Integer>> outIntegerLstLst;
algorithm
(outInteger,outIntegerLst,outIntegerLstLst):=
matchcontinue (inIncidenceMatrix1,inIncidenceMatrixT2,inIntegerArray3,inIntegerArray4,inInteger5,inInteger6,inIntegerLst7,inIntegerLstLst8)
local
BackendDAE.Value i_1,i,v;
list<BackendDAE.Value> stack_1,eqns,stack_2,stack_3,comp,stack;
list<list<BackendDAE.Value>> comps_1,comps_2,comps;
array<list<BackendDAE.Value>> m,mt;
array<BackendDAE.Value> a1,a2;
case (m,mt,a1,a2,i,v,stack,comps)
equation
i_1 = i + 1;
BackendDAEEXT.setNumber(v, i_1) ;
BackendDAEEXT.setLowLink(v, i_1);
stack_1 = (v :: stack);
eqns = reachableNodes(v, m, mt, a1, a2);
(i_1,stack_2,comps_1) = iterateReachableNodes(eqns, m, mt, a1, a2, i_1, v, stack_1, comps);
(i_1,stack_3,comp) = checkRoot(m, mt, a1, a2, i_1, v, stack_2);
comps_2 = consIfNonempty(comp, comps_1);
then
(i_1,stack_3,comps_2);
case (_,_,_,_,_,_,_,_)
equation
Debug.fprint("failtrace", "-strong_connect failed\n");
then
fail();
end matchcontinue;
end strongConnect;
protected function consIfNonempty "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<BackendDAE.Value>> lst;
list<BackendDAE.Value> e;
case ({},lst) then lst;
case (e,lst) then (e :: lst);
end matchcontinue;
end consIfNonempty;
public function reachableNodes "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 inInteger1;
input BackendDAE.IncidenceMatrix inIncidenceMatrix2;
input BackendDAE.IncidenceMatrixT inIncidenceMatrixT3;
input array<Integer> inIntegerArray4;
input array<Integer> inIntegerArray5;
output list<Integer> outIntegerLst;
algorithm
outIntegerLst:=
matchcontinue (inInteger1,inIncidenceMatrix2,inIncidenceMatrixT3,inIntegerArray4,inIntegerArray5)
local
BackendDAE.Value var,pos,eqn;
list<BackendDAE.Value> reachable,reachable_1,reachable_2;
array<list<BackendDAE.Value>> m,mt;
array<BackendDAE.Value> a1,a2;
String eqnstr;
case (eqn,m,mt,a1,a2)
equation
var = a2[eqn];
reachable = mt[var] "Got the variable that is solved in the equation" ;
reachable_1 = BackendDAEUtil.removeNegative(reachable) "in which other equations is this variable present ?" ;
pos = Util.listPosition(eqn, reachable_1) ".. except this one" ;
reachable_2 = listDelete(reachable_1, pos);
then
reachable_2;
case (eqn,_,_,_,_)
equation
true = RTOpts.debugFlag("failtrace");
Debug.fprint("failtrace", "-reachable_nodes failed, eqn: ");
eqnstr = intString(eqn);
Debug.fprint("failtrace", eqnstr);
Debug.fprint("failtrace", "\n");
then
fail();
end matchcontinue;
end reachableNodes;
protected function iterateReachableNodes "function: iterateReachableNodes
author: PA
Helper function to strong_connect.