/
IndexReduction.mo
4319 lines (4122 loc) · 175 KB
/
IndexReduction.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 IndexReduction
" file: IndexReduction.mo
package: IndexReduction
description: IndexReduction contains functions that are needed to perform
index reduction
"
public import BackendDAE;
public import DAE;
protected
import Absyn;
import AdjacencyMatrix;
import Array;
import BackendDAEEXT;
import BackendDAEUtil;
import BackendDump;
import BackendEquation;
import BackendInline;
import BackendVariable;
import BackendVarTransform;
import BaseHashTable;
import ComponentReference;
import Differentiate;
import ElementSource;
import Error;
import ErrorExt;
import Expression;
import ExpressionDump;
import ExpressionSimplify;
import Flags;
import HashTable2;
import HashTable3;
import HashTableCG;
import HashTableCrIntToExp;
import Inline;
import InlineArrayEquations;
import List;
import Matching;
import SCode;
import Sorting;
import SymbolicJacobian;
import System;
import Util;
// =============================================================================
// Pantelides index reduction method .
// see:
// C Pantelides, The Consistent Initialization of Differential-Algebraic Systems, SIAM J. Sci. and Stat. Comput. Volume 9, Issue 2, pp. 213–231 (March 1988)
// Soares, R. de P.; Secchi, A. R.: Direct Initialisation and Solution of High-Index DAESystems. in Proceedings of the European Symbosium on Computer Aided Process Engineering - 15, Barcelona, Spain,
// =============================================================================
public function pantelidesIndexReduction "author: Frenkel TUD 2012-04
Index Reduction algorithm to get a index 1 or 0 system."
input list<list<Integer>> inEqns; // the MSSS
input Integer inActualEqn;
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input array<Integer> inAssignments1;
input array<Integer> inAssignments2;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
output list<Integer> changedEqns;
output Integer continueEqn;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output array<Integer> oass1;
output array<Integer> oass2;
output BackendDAE.StructurallySingularSystemHandlerArg outArg;
protected
array<Integer> markarr;
Integer size, newsize;
list<list<Integer>> eqns_1, unassignedStates, unassignedEqns;
algorithm
if listEmpty(inEqns) then
Error.addMessage(Error.INTERNAL_ERROR, {"- IndexReduction.pantelidesIndexReduction called with empty list of equations!"});
if Flags.isSet(Flags.OPT_DAE_DUMP) then
print("Index reduction done.\n");
end if;
fail();
end if;
try
if Flags.isSet(Flags.OPT_DAE_DUMP) then
print("\n\nIndex reduction:\n");
end if;
// BackendDump.printEqSystem(inSystem);
// BackendDump.dumpMatching(inAssignments1);
// BackendDump.dumpMatching(inAssignments2);
// syst = BackendDAEUtil.setEqSystMatching(inSystem, BackendDAE.MATCHING(inAssignments1, inAssignments2, {}));
// dumpSystemGraphML(syst, inShared, NONE(), "ConstrainRevoluteJoint" + intString(listLength(List.flatten(inEqns))) + ".graphml");
// check by count vars of equations, if len(inEqns) > len(vars) stop because of structural singular system
ErrorExt.setCheckpoint("Pantelides");
(eqns_1, unassignedStates, unassignedEqns, _) := minimalStructurallySingularSystem(inEqns, inSystem, inShared, inAssignments1, inAssignments2, inArg);
size := BackendDAEUtil.systemSize(inSystem);
ErrorExt.delCheckpoint("Pantelides");
ErrorExt.setCheckpoint("Pantelides");
markarr := arrayCreate(size, -1);
(osyst, oshared, oass1, oass2, outArg, _) := pantelidesIndexReduction1(unassignedStates, unassignedEqns, inEqns, eqns_1, inActualEqn, inSystem, inShared, inAssignments1, inAssignments2, 1, markarr, inArg, {});
ErrorExt.rollBack("Pantelides");
ErrorExt.setCheckpoint("Pantelides");
// get from inEqns indexes the scalar indexes
newsize := BackendDAEUtil.systemSize(osyst);
changedEqns := if newsize>size then List.intRange2(size+1, newsize) else {};
(changedEqns, continueEqn) := getChangedEqnsAndLowest(newsize, oass2, changedEqns, size);
ErrorExt.delCheckpoint("Pantelides");
if Flags.isSet(Flags.OPT_DAE_DUMP) then
BackendDump.dumpEqSystemShort(osyst, "pantelidesIndexReduction");
print("Index reduction done.\n");
end if;
else
ErrorExt.delCheckpoint("Pantelides");
Error.addMessage(Error.INTERNAL_ERROR, {"- IndexReduction.pantelidesIndexReduction failed!"});
if Flags.isSet(Flags.OPT_DAE_DUMP) then
print("Index reduction done.\n");
end if;
fail();
end try;
end pantelidesIndexReduction;
public function failIfIndexReduction "author: lochel
This function is used as dummy index reduction function if no index reduction
should be performed.
This function fails if it is called."
input list<list<Integer>> inEqns;
output list<Integer> changedEqns = {};
input output Integer inActualEqn;
input output BackendDAE.EqSystem inSystem;
input output BackendDAE.Shared inShared;
input output array<Integer> inAssignments1;
input output array<Integer> inAssignments2;
input output BackendDAE.StructurallySingularSystemHandlerArg inArg;
algorithm
Error.addCompilerError("Structural singular system detected, but no index reduction method has been selected.");
fail();
end failIfIndexReduction;
protected function getChangedEqnsAndLowest
input Integer index;
input array<Integer> ass2;
input list<Integer> iAcc;
input Integer iLowest;
output list<Integer> oAcc = iAcc;
output Integer oLowest = iLowest;
algorithm
for i in index:-1:1 loop
oAcc := List.consOnTrue(intLt(ass2[i], 1), i, oAcc);
oLowest := i;
end for;
end getChangedEqnsAndLowest;
protected function pantelidesIndexReduction1
"author: Frenkel TUD 2012-04
Index Reduction algorithm to get a index 1 or 0 system."
input list<list<Integer>> unassignedStates;
input list<list<Integer>> unassignedEqns;
input list<list<Integer>> alleqns;
input list<list<Integer>> iEqns;
input Integer actualEqn;
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input array<Integer> inAssignments1;
input array<Integer> inAssignments2;
input Integer mark;
input array<Integer> markarr;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
input list<tuple<list<Integer>,list<Integer>,list<Integer>>> iNotDiffableMSS;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output array<Integer> outAssignments1;
output array<Integer> outAssignments2;
output BackendDAE.StructurallySingularSystemHandlerArg outArg;
output list<tuple<list<Integer>,list<Integer>,list<Integer>>> oNotDiffableMSS;
algorithm
(osyst,oshared,outAssignments1,outAssignments2,outArg,oNotDiffableMSS) :=
matchcontinue (unassignedStates, unassignedEqns, alleqns, iEqns)
local
list<Integer> states,eqns,eqns_1,ueqns;
list<list<Integer>> statelst,ueqnsrest,eqnsrest,eqnsrest_1;
array<Integer> ass1,ass2;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
BackendDAE.StructurallySingularSystemHandlerArg arg;
list<tuple<list<Integer>,list<Integer>,list<Integer>>> notDiffableMSS;
case (_, _, _, {})
equation
(syst,shared,ass1,ass2,arg) = handleundifferntiableMSSLst(iNotDiffableMSS,inSystem,inShared,inAssignments1,inAssignments2,inArg);
then
(syst,shared,ass1,ass2,arg,{});
case (states::statelst, ueqns::ueqnsrest, eqns::eqnsrest, eqns_1::eqnsrest_1)
equation
(syst,shared,ass1,ass2,arg,notDiffableMSS) =
pantelidesIndexReductionMSS(states,ueqns,eqns,eqns_1,actualEqn,inSystem,inShared,inAssignments1,inAssignments2,mark,markarr,inArg,iNotDiffableMSS);
// next MSS
(syst,shared,ass1,ass2,arg,notDiffableMSS) =
pantelidesIndexReduction1(statelst,ueqnsrest,eqnsrest,eqnsrest_1,actualEqn,syst,shared,ass1,ass2,mark,markarr,arg,notDiffableMSS);
then
(syst,shared,ass1,ass2,arg,notDiffableMSS);
else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"- IndexReduction.pantelidesIndexReduction1 failed! Use -d=bltdump to get more information."});
then
fail();
end matchcontinue;
end pantelidesIndexReduction1;
protected function pantelidesIndexReductionMSS
"author: Frenkel TUD 2012-04
Index Reduction algorithm to get a index 1 or 0 system."
input list<Integer> unassignedStates;
input list<Integer> unassignedEqns;
input list<Integer> alleqns;
input list<Integer> MSSSeqs;
input Integer actualEqn;
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input array<Integer> inAssignments1;
input array<Integer> inAssignments2;
input Integer mark;
input array<Integer> markarr;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
input list<tuple<list<Integer>,list<Integer>,list<Integer>>> iNotDiffableMSS;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output array<Integer> outAssignments1;
output array<Integer> outAssignments2;
output BackendDAE.StructurallySingularSystemHandlerArg outArg;
output list<tuple<list<Integer>,list<Integer>,list<Integer>>> oNotDiffableMSS;
algorithm
(osyst,oshared,outAssignments1,outAssignments2,outArg,oNotDiffableMSS):=
matchcontinue (unassignedStates,unassignedEqns,alleqns,MSSSeqs,actualEqn,inSystem,inShared,inAssignments1,inAssignments2,mark,markarr,inArg,iNotDiffableMSS)
local
list<Integer> MSSSeqs1;
BackendDAE.StateOrder so;
BackendDAE.ConstraintEquations orgEqnsLst,orgEqnsLst1;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
array<list<Integer>> mapEqnIncRow;
array<Integer> mapIncRowEqn,ass1,ass2;
Integer noofeqns;
BackendDAE.EquationArray eqnsarray;
BackendDAE.Variables vars;
list<tuple<Integer,Option<BackendDAE.Equation>,BackendDAE.Equation>> eqnstpl;//<originalIdx, SOME<derivedEq>, OrigEq>
list<tuple<list<Integer>,list<Integer>,list<Integer>>> notDiffableMSS;
case (_,_,_,_::_,_,BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqnsarray),_,_,_,_,_,(so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn,noofeqns),_)
equation
// get from scalar eqns indexes the indexes in the equation array
MSSSeqs1 = List.map1r(MSSSeqs,arrayGet,mapIncRowEqn);
MSSSeqs1 = List.uniqueIntN(MSSSeqs1,arrayLength(mapIncRowEqn));
// do not differentiate self generated equations $_DER.x = der(x)
MSSSeqs1 = List.select1(MSSSeqs1,intLe,noofeqns);
if Flags.isSet(Flags.BLT_DUMP) then
print("##############--MSSS--##############\n"
+ "Indices of constraint equations: ");
BackendDump.debuglst(MSSSeqs1,intString," ","\n");
print("\n");
end if;
//try to differentiate all equations from the MSSS, eqnstpl is empty if thats not possible
(eqnstpl, shared) = differentiateEqnsLst(MSSSeqs1,vars,eqnsarray,inShared);
//print("\ndifferentiated equations: \n"+stringDelimitList(List.map(eqnstpl,eqnstplDebugString),"\n")+"\n\n");
//try to assemble a system with these differentiated eqs
(syst,shared,ass1,ass2,orgEqnsLst1,mapEqnIncRow,mapIncRowEqn,notDiffableMSS) = differentiateEqns(eqnstpl,MSSSeqs1,unassignedStates,unassignedEqns,inSystem, shared,inAssignments1,inAssignments2,orgEqnsLst,mapEqnIncRow,mapIncRowEqn,iNotDiffableMSS);
then
(syst,shared,ass1,ass2,(so,orgEqnsLst1,mapEqnIncRow,mapIncRowEqn,noofeqns),notDiffableMSS);
else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"- IndexReduction.pantelidesIndexReductionMSS failed! Use -d=bltdump to get more information."});
then
fail();
end matchcontinue;
end pantelidesIndexReductionMSS;
protected function eqnstplDebugString"
prints the eqnstpl that contains the information about derived equations from Index Reduction.
author:Waurich"
input tuple<Integer,Option<BackendDAE.Equation>,BackendDAE.Equation> tpl;
output String s;
algorithm
if Util.isSome(Util.tuple32(tpl)) then
s := "";
else
s := BackendDump.equationString(Util.getOption(Util.tuple32(tpl)));
end if;
s := "Original Eq "+intString(Util.tuple31(tpl))+": "+s+"\n\t-->"+BackendDump.equationString(Util.tuple33(tpl))+"";
end eqnstplDebugString;
protected function minimalStructurallySingularSystem "author: Frenkel TUD - 2012-04,
checks if the subset of equations is minimal structurally singular. The
check is done for all equations and variables and for each subset.
The number of states must be larger or equal to the number of unmatched
equations."
input list<list<Integer>> inEqnsLst;
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input array<Integer> inAssignments1;
input array<Integer> inAssignments2;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
output list<list<Integer>> outEqnsLst "subsets without discrete equations";
output list<list<Integer>> outStateIndxs "states contained in each subset";
output list<list<Integer>> outunassignedEqns "the unassigned eqns of each subset";
output list<Integer> discEqns "discrete equations of all subsets";
protected
list<Integer> unassignedEqns, eqnslst, stateindxs;
BackendDAE.IncidenceMatrix m;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
array<Integer> statemark;
Integer size;
Boolean b;
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns, m=SOME(m)) := syst;
size := BackendVariable.varsSize(vars);
statemark := arrayCreate(size, -1);
// check over all mss
unassignedEqns := List.flatten(inEqnsLst);
stateindxs := List.fold2(unassignedEqns, statesInEquations, (m, statemark, 0), inAssignments1, {});
((unassignedEqns, eqnslst, discEqns)) := List.fold3(unassignedEqns, unassignedContinuesEqns, vars, inAssignments2, m, ({}, {}, {}));
b := intGe(listLength(stateindxs), listLength(unassignedEqns));
singulareSystemError(b, stateindxs, unassignedEqns, eqnslst, syst, shared, inAssignments1, inAssignments2, inArg);
// check each mss
(outEqnsLst, outStateIndxs, outunassignedEqns, discEqns) := minimalStructurallySingularSystemMSS(inEqnsLst, syst, shared, inAssignments1, inAssignments2, inArg, statemark, 1, m, vars, {}, {}, {}, {});
end minimalStructurallySingularSystem;
protected function minimalStructurallySingularSystemMSS
"author: Frenkel TUD - 2012-11,
helper for minimalStructurallySingularSystem"
input list<list<Integer>> inEqnsLst;
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input array<Integer> inAssignments1;
input array<Integer> inAssignments2;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
input array<Integer> statemark;
input Integer mark;
input BackendDAE.IncidenceMatrix m;
input BackendDAE.Variables vars;
input list<list<Integer>> inEqnsLstAcc;
input list<list<Integer>> inStateIndxsAcc;
input list<list<Integer>> inUnassEqnsAcc;
input list<Integer> inDiscEqnsAcc;
output list<list<Integer>> outEqnsLst;
output list<list<Integer>> outStateIndxs;
output list<list<Integer>> outUnassEqnsAcc;
output list<Integer> outDiscEqns;
algorithm
(outEqnsLst, outStateIndxs, outUnassEqnsAcc, outDiscEqns) := match(inEqnsLst)
local
list<Integer> ilst, unassignedEqns, eqnsLst, discEqns, stateIndxs;
list<list<Integer>> rest;
Boolean b;
case {}
then (inEqnsLstAcc, inStateIndxsAcc, inUnassEqnsAcc, inDiscEqnsAcc);
case ilst::rest equation
// print("Eqns " + stringDelimitList(List.map(ilst, intString), ", ") + "\n");
((unassignedEqns, eqnsLst, discEqns)) = List.fold3(ilst, unassignedContinuesEqns, vars, inAssignments2, m, ({}, {}, inDiscEqnsAcc));
// print("unassignedEqns " + stringDelimitList(List.map(unassignedEqns, intString), ", ") + "\n");
stateIndxs = List.fold2(ilst, statesInEquations, (m, statemark, mark), inAssignments1, {});
// print("stateIndxs " + stringDelimitList(List.map(stateIndxs, intString), ", ") + "\n");
b = intGe(listLength(stateIndxs), listLength(unassignedEqns));
singulareSystemError(b, stateIndxs, unassignedEqns, eqnsLst, inSystem, inShared, inAssignments1, inAssignments2, inArg);
(outEqnsLst, outStateIndxs, outUnassEqnsAcc, outDiscEqns) = minimalStructurallySingularSystemMSS(rest, inSystem, inShared, inAssignments1, inAssignments2, inArg, statemark, mark+1, m, vars, eqnsLst::inEqnsLstAcc, stateIndxs::inStateIndxsAcc, unassignedEqns::inUnassEqnsAcc, discEqns);
then (outEqnsLst, outStateIndxs, outUnassEqnsAcc, outDiscEqns);
end match;
end minimalStructurallySingularSystemMSS;
protected function singulareSystemError "author: Frenkel TUD 2012-04
Index Reduction algorithm to get a index 1 or 0 system."
input Boolean b;
input list<Integer> unassignedStates;
input list<Integer> unassignedEqns;
input list<Integer> eqns;
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input array<Integer> inAssignments1;
input array<Integer> inAssignments2;
input BackendDAE.StructurallySingularSystemHandlerArg inArg;
algorithm
_ := match(b, eqns, inArg)
local
list<BackendDAE.Var> varlst;
list<Integer> eqns1;
BackendDAE.EqSystem syst;
array<Integer> mapIncRowEqn;
// OK
case (true, _::_, _) then ();
// Failure
case (_, {}, (_, _, _, mapIncRowEqn, _)) equation
if Flags.isSet(Flags.BLT_DUMP) then
print("Reduce Index failed! Found empty set of continues equations.\nmarked equations:\n");
end if;
// get from scalar eqns indexes the indexes in the equation array
eqns1 = List.map1r(eqns, arrayGet, mapIncRowEqn);
eqns1 = List.uniqueIntN(eqns1, arrayLength(mapIncRowEqn));
if Flags.isSet(Flags.BLT_DUMP) then
print(BackendDump.dumpMarkedEqns(inSystem, eqns1));
end if;
syst = BackendDAEUtil.setEqSystMatching(inSystem, BackendDAE.MATCHING(inAssignments1, inAssignments2, {}));
if Flags.isSet(Flags.BLT_DUMP) then
BackendDump.printBackendDAE(BackendDAE.DAE({syst}, inShared));
end if;
Error.addMessage(Error.INTERNAL_ERROR, {"IndexReduction.pantelidesIndexReduction failed! Found empty set of continues equations. Use -d=bltdump to get more information."});
then fail();
case (false, _::_, (_, _, _, mapIncRowEqn, _)) equation
if Flags.isSet(Flags.BLT_DUMP) then
print("Reduce Index failed! System is structurally singulare and cannot handled because number of unassigned continues equations is larger than number of states.\nmarked equations:\n");
// get from scalar eqns indexes the indexes in the equation array
BackendDump.debuglst(eqns, intString, " ", "\n");
end if;
eqns1 = List.map1r(eqns, arrayGet, mapIncRowEqn);
eqns1 = List.uniqueIntN(eqns1, arrayLength(mapIncRowEqn));
if Flags.isSet(Flags.BLT_DUMP) then
print(BackendDump.dumpMarkedEqns(inSystem, eqns1));
print("unassgined states:\n");
end if;
varlst = List.map1r(unassignedStates, BackendVariable.getVarAt, BackendVariable.daeVars(inSystem));
if Flags.isSet(Flags.BLT_DUMP) then
BackendDump.printVarList(varlst);
end if;
syst = BackendDAEUtil.setEqSystMatching(inSystem, BackendDAE.MATCHING(inAssignments1, inAssignments2, {}));
if Flags.isSet(Flags.BLT_DUMP) then
BackendDump.printBackendDAE(BackendDAE.DAE({syst}, inShared));
end if;
Error.addMessage(Error.INTERNAL_ERROR, {"IndexReduction.pantelidesIndexReduction failed! System is structurally singulare and cannot handled because number of unassigned equations is larger than number of states. Use -d=bltdump to get more information."});
then fail();
end match;
end singulareSystemError;
protected function unassignedContinuesEqns
"author: Frenkel TUD - 2012-11,
check if it is an discrete equation and extract the
states of the equation.
Helper for minimalStructurallySingularSystem."
input Integer eindx;
input BackendDAE.Variables vars;
input array<Integer> ass2;
input BackendDAE.IncidenceMatrix m;
input tuple<list<Integer>,list<Integer>,list<Integer>> inFold;
output tuple<list<Integer>,list<Integer>,list<Integer>> outFold;
algorithm
outFold := matchcontinue(inFold)
local
Integer vindx;
list<Integer> unassignedEqns,eqnsLst,varlst,discEqns;
list<BackendDAE.Var> vlst;
Boolean b,ba;
/* case((unassignedEqns,eqnsLst))
equation
vindx = ass2[eindx];
true = intGt(vindx,0);
v = BackendVariable.getVarAt(vars, vindx);
b = BackendVariable.isVarDiscrete(v);
eqnsLst = List.consOnTrue(not b, eindx, eqnsLst);
then
((unassignedEqns,eqnsLst));
*/ case((unassignedEqns,eqnsLst,discEqns))
equation
vindx = ass2[eindx];
ba = intLt(vindx,1);
varlst = m[eindx];
varlst = List.map(varlst,intAbs);
vlst = List.map1r(varlst,BackendVariable.getVarAt,vars);
// if there is a continues variable then b is false
b = List.mapBoolAnd(vlst,BackendVariable.isVarDiscrete);
eqnsLst = List.consOnTrue(not b, eindx, eqnsLst);
unassignedEqns = List.consOnTrue(ba and not b, eindx, unassignedEqns);
discEqns = List.consOnTrue(b, eindx, discEqns);
then
((unassignedEqns,eqnsLst,discEqns));
case((unassignedEqns,eqnsLst,discEqns))
equation
vindx = ass2[eindx];
false = intGt(vindx,0);
then
((eindx::unassignedEqns,eindx::eqnsLst,discEqns));
end matchcontinue;
end unassignedContinuesEqns;
protected function statesInEquations
"author: Frenkel TUD 2012-04"
input Integer eindx;
input tuple<BackendDAE.IncidenceMatrix,array<Integer>,Integer> inTpl;
input array<Integer> ass1;
input list<Integer> inStateLst;
output list<Integer> outStateLst;
protected
list<Integer> vars;
BackendDAE.IncidenceMatrix m;
array<Integer> statemark;
Integer mark;
algorithm
(m,statemark,mark) := inTpl;
// get States;
vars := List.removeOnTrue(0, intLt, m[eindx]);
// get unassigned
// vars := List.removeOnTrue(ass1, Matching.isUnAssigned, vars);
vars := List.map(vars,intAbs);
vars := List.removeOnTrue((statemark,mark), isMarked, vars);
_ := List.fold1(vars, markTrue, mark, statemark);
// add states to list
outStateLst := listAppend(inStateLst,vars);
end statesInEquations;
protected function isMarked
"author: Frenkel TUD 2012-05"
input tuple<array<Integer>,Integer> ass;
input Integer indx;
output Boolean b;
protected
array<Integer> arr;
Integer mark;
algorithm
(arr,mark) := ass;
b := intEq(arr[intAbs(indx)],mark);
end isMarked;
protected function markTrue
"author: Frenkel TUD 2012-05"
input Integer indx;
input Integer mark;
input output array<Integer> arr;
algorithm
_ := arrayUpdate(arr,intAbs(indx),mark);
end markTrue;
protected function differentiateEqns
"author: Frenkel TUD 2011-05
differentiates the constraint equations for
Pantelides index reduction method."
input list<tuple<Integer,Option<BackendDAE.Equation>,BackendDAE.Equation>> inEqnsTpl;
input list<Integer> MSSSeqs;
input list<Integer> unassignedStates;
input list<Integer> unassignedEqns;
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input array<Integer> inAss1;
input array<Integer> inAss2;
input BackendDAE.ConstraintEquations inOrgEqnsLst;
input array<list<Integer>> imapEqnIncRow;
input array<Integer> imapIncRowEqn;
input list<tuple<list<Integer>,list<Integer>,list<Integer>>> iNotDiffableMSS;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output array<Integer> outAss1;
output array<Integer> outAss2;
output BackendDAE.ConstraintEquations outOrgEqnsLst;
output array<list<Integer>> omapEqnIncRow;
output array<Integer> omapIncRowEqn;
output list<tuple<list<Integer>,list<Integer>,list<Integer>>> oNotDiffableMSS;
protected
BackendDAE.EqSystem syst;
BackendDAE.EquationArray eqns_1, eqns;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrix mt;
BackendDAE.Variables v, v1;
DAE.FunctionTree funcs;
Integer numEqs, numEqs1;
list<Integer> changedVars, eqnslst, eqnslst1, assEqs;
algorithm
if listEmpty(inEqnsTpl) then
// not all equations are differentiated
osyst := inSystem;
oshared := inShared;
outAss1 := inAss1;
outAss2 := inAss2;
outOrgEqnsLst := inOrgEqnsLst;
omapEqnIncRow := imapEqnIncRow;
omapIncRowEqn := imapIncRowEqn;
oNotDiffableMSS := (MSSSeqs,unassignedStates,unassignedEqns)::iNotDiffableMSS;
else
// all equations are differentiated
syst := inSystem;
BackendDAE.EQSYSTEM(orderedVars=v, orderedEqs=eqns, m=SOME(m), mT=SOME(mt)) := syst;
numEqs := BackendEquation.getNumberOfEquations(eqns);
(v1,eqns_1,changedVars,outOrgEqnsLst) := replaceDifferentiatedEqns(inEqnsTpl,v,eqns,mt,imapIncRowEqn,{},inOrgEqnsLst);
numEqs1 := BackendEquation.getNumberOfEquations(eqns_1);
eqnslst := if intGt(numEqs1,numEqs) then List.intRange2(numEqs+1,numEqs1) else {};
// set the assignments for the changed vars and for the assigned equations to -1
assEqs := List.map1r(changedVars,arrayGet,inAss1);
assEqs := List.select1(assEqs,intGt,0);
outAss2 := List.fold1r(assEqs,arrayUpdate,-1,inAss2);
outAss1 := List.fold1r(changedVars,arrayUpdate,-1,inAss1);
//get adjacent equations for the changed vars
eqnslst1 := collectVarEqns(changedVars,mt,arrayLength(mt),arrayLength(m));
syst.orderedVars := v1;
syst.orderedEqs := eqns_1;
eqnslst1 := List.map1r(eqnslst1,arrayGet,imapIncRowEqn);
eqnslst1 := List.uniqueIntN(listAppend(MSSSeqs,eqnslst1),numEqs1);
eqnslst1 := listAppend(eqnslst1,eqnslst);
if Flags.isSet(Flags.BLT_DUMP) then
print("Update Incidence Matrix: ");
BackendDump.debuglst(eqnslst1,intString," ","\n");
print("\n");
end if;
funcs := BackendDAEUtil.getFunctions(inShared);
(syst,omapEqnIncRow,omapIncRowEqn) :=
BackendDAEUtil.updateIncidenceMatrixScalar(syst, BackendDAE.SOLVABLE(), SOME(funcs), eqnslst1, imapEqnIncRow, imapIncRowEqn);
osyst := syst;
oshared := inShared;
oNotDiffableMSS := iNotDiffableMSS;
end if;
end differentiateEqns;
protected function collectVarEqns
"author: Frenkel TUD 2011-05, waurich 12-15
collect all equations of a list with var indexes"
input list<Integer> varIdcsIn;
input BackendDAE.IncidenceMatrixT mT;
input Integer numVars;
input Integer numEqs "size of equations array, maximal entry in inMT";
output list<Integer> eqIdcsOut = {};
protected
Integer varIdx;
list<Integer> eqIdcs;
algorithm
for varIdx in varIdcsIn loop
if intLt(varIdx,numVars) then
eqIdcs := List.map(mT[varIdx],intAbs);
eqIdcsOut := listAppend(eqIdcs,eqIdcsOut);
end if;
end for;
eqIdcsOut := List.uniqueIntN(eqIdcsOut,numEqs);
end collectVarEqns;
protected function searchDerivativesExp "author: Frenkel TUD 2012-11"
input DAE.Exp inExp;
input tuple<list<Integer>,BackendDAE.Variables> tpl;
output DAE.Exp outExp;
output tuple<list<Integer>,BackendDAE.Variables> outTpl;
algorithm
(outExp,outTpl) := matchcontinue (inExp,tpl)
local
BackendDAE.Variables vars;
list<Integer> ilst,i1lst;
DAE.Exp e;
DAE.ComponentRef cr;
case (e as DAE.CALL(path=Absyn.IDENT(name = "der"),expLst={DAE.CREF(componentRef=cr)}),(ilst,vars))
equation
(_,i1lst) = BackendVariable.getVar(cr,vars);
ilst = List.fold1(i1lst,List.removeOnTrue, intEq, ilst);
then (e,(ilst,vars));
else (inExp,tpl);
end matchcontinue;
end searchDerivativesExp;
protected function differentiateEqnsLst
"differentiates the constraint equations for
Pantelides index reduction method. waurich: if one of these equation cannot be derived, output an empty list -> not differentiable MSS"
input list<Integer> inEqns;
input BackendDAE.Variables vars;
input BackendDAE.EquationArray eqns;
input BackendDAE.Shared inShared;
output list<tuple<Integer,Option<BackendDAE.Equation>,BackendDAE.Equation>> outEqnTpl; //<originalIdx, SOME<derivedEq>, OrigEq>
output BackendDAE.Shared oShared;
protected
Integer e;
list<Integer> eqs;
Option<tuple<Integer,Option<BackendDAE.Equation>,BackendDAE.Equation>> eqTplOpt;
algorithm
outEqnTpl := {};
oShared := inShared;
eqs := inEqns;
while not listEmpty(eqs) loop
e::eqs := eqs;
(eqTplOpt, oShared) := differentiateEqnsLst1(e,vars,eqns,oShared);
if Util.isSome(eqTplOpt) then
outEqnTpl := Util.getOption(eqTplOpt)::outEqnTpl;
else
outEqnTpl := {};
oShared := inShared;
return;
end if;
end while;
end differentiateEqnsLst;
protected function differentiateEqnsLst1
"author: Frenkel TUD 2012-11
differentiates the constraint equations for
Pantelides index reduction method."
input Integer eqIdx;
input BackendDAE.Variables vars;
input BackendDAE.EquationArray eqns;
input BackendDAE.Shared inShared;
output Option<tuple<Integer,Option<BackendDAE.Equation>,BackendDAE.Equation>> oEqTpl;
output BackendDAE.Shared oshared;
protected
BackendDAE.Equation eqn;
Option<BackendDAE.Equation> diffEqn;
algorithm
eqn := BackendEquation.get(eqns, eqIdx);
if BackendEquation.isDifferentiated(eqn) then
if Flags.isSet(Flags.BLT_DUMP) then
BackendDump.debugStrEqnStr("Skip already differentiated equation\n",eqn,"\n");
end if;
oEqTpl := SOME((eqIdx, NONE(), eqn));
oshared := inShared;
else
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiate equation " + intString(eqIdx) + " " + BackendDump.equationString(eqn) + "\n"); end if;
(diffEqn, oshared) := Differentiate.differentiateEquationTime(eqn, vars, inShared);
//if Flags.isSet(Flags.BLT_DUMP) then print("differentiated equation " + intString(eqIdx) + " " + BackendDump.equationString(diffEqn) + "\n"); end if;
eqn := BackendEquation.markDifferentiated(eqn);
if isSome(diffEqn) then
oEqTpl := SOME((eqIdx, diffEqn, eqn));
else
oEqTpl := NONE();
oshared := inShared;
end if;
end if;
end differentiateEqnsLst1;
protected function replaceDifferentiatedEqns
"author: Frenkel TUD 2012-11, waurich 2015-12
replace the original equations with the derived, updated var-types, stores all former equations in inOrgEqnsLst"
input list<tuple<Integer, Option<BackendDAE.Equation>, BackendDAE.Equation>> inEqnTplLst; //<origIdx, SOME<derivedEq>, original Eq>
input BackendDAE.Variables vars;
input BackendDAE.EquationArray eqns;
input BackendDAE.IncidenceMatrix mt;
input array<Integer> imapIncRowEqn;
input list<Integer> inChangedVars;
input BackendDAE.ConstraintEquations inOrgEqns;
output BackendDAE.Variables outVars;
output BackendDAE.EquationArray outEqns;
output list<Integer> outChangedVars;
output BackendDAE.ConstraintEquations outOrgEqns;
protected
Integer eqIdx;
list<Integer> changedVars;
BackendDAE.Equation eqOrig, eqDiff;
tuple<Integer, Option<BackendDAE.Equation>, BackendDAE.Equation> eqTpl;
algorithm
outVars := vars;
outEqns := eqns;
outChangedVars := inChangedVars;
outOrgEqns := inOrgEqns;
for eqTpl in inEqnTplLst loop
if Util.isSome(Util.tuple32(eqTpl)) then
// replace der-calls with the derivatives
(eqIdx, SOME(eqDiff), eqOrig) := eqTpl;
(eqDiff, _) := BackendEquation.traverseExpsOfEquation(eqDiff, replaceStateOrderExp, outVars);
// change the variable types (algebraic -> state, 1.der -> 2.der, ...)
(eqDiff, (_, (outVars, outEqns, outChangedVars, _, _, _))) := BackendEquation.traverseExpsOfEquation(eqDiff, Expression.traverseSubexpressionsHelper, (changeDerVariablesToStatesFinder, (outVars, outEqns, outChangedVars, eqIdx, imapIncRowEqn, mt)));
if Flags.isSet(Flags.BLT_DUMP) then
debugdifferentiateEqns((eqOrig, eqDiff, eqIdx));
end if;
outEqns := BackendEquation.setAtIndex(outEqns, eqIdx, eqDiff);
//collect original equations
outOrgEqns := addOrgEqn(eqIdx, eqOrig, outOrgEqns);
end if;
end for;
end replaceDifferentiatedEqns;
protected function replaceStateOrderExp
input DAE.Exp inExp;
input BackendDAE.Variables inVars;
output DAE.Exp e;
output BackendDAE.Variables vars;
algorithm
(e, vars) := Expression.traverseExpTopDown(inExp, replaceStateOrderExpFinder, inVars);
end replaceStateOrderExp;
protected function replaceStateOrderExpFinder
"author: Frenkel TUD 2011-05 "
input DAE.Exp inExp;
input BackendDAE.Variables inVars;
output DAE.Exp outExp;
output Boolean cont;
output BackendDAE.Variables outVars;
algorithm
(outExp,cont,outVars) := matchcontinue (inExp,inVars)
local
DAE.Exp e;
BackendDAE.Variables vars;
DAE.ComponentRef dcr,cr;
DAE.CallAttributes attr;
Integer index;
//if der(x) = y, replace all der(x) with y
case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)}),vars)
equation
(BackendDAE.VAR(varKind=BackendDAE.STATE(derName=SOME(dcr))),_) = BackendVariable.getVarSingle(cr,vars);
e = Expression.crefExp(dcr);
then
(e,false,vars);
case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr),DAE.ICONST(index)},attr=attr),vars)
equation
true = intEq(index,2);
(BackendDAE.VAR(varKind=BackendDAE.STATE(derName=SOME(dcr))),_) = BackendVariable.getVarSingle(cr,vars);
e = Expression.crefExp(dcr);
then
(DAE.CALL(Absyn.IDENT("der"),{e},attr),false,vars);
case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr)})},attr=attr),vars)
equation
(BackendDAE.VAR(varKind=BackendDAE.STATE(derName=SOME(dcr))),_) = BackendVariable.getVarSingle(cr,vars);
e = Expression.crefExp(dcr);
then
(DAE.CALL(Absyn.IDENT("der"),{e},attr),false,vars);
case (e,vars) then (e,true,vars);
end matchcontinue;
end replaceStateOrderExpFinder;
protected function statesWithUnusedDerivative
"author Frenkel TUD 2012-11
add to iAcc all states with no positiv rows in mt"
input Integer state;
input BackendDAE.IncidenceMatrix mt;
input list<Integer> iAcc;
output list<Integer> oAcc;
algorithm
oAcc := matchcontinue(state)
case(_)
equation
List.map1AllValue(mt[state], intLt, true, 0);
then
state::iAcc;
else iAcc;
end matchcontinue;
end statesWithUnusedDerivative;
protected function isStateonIndex
input Integer index;
input BackendDAE.Variables vars;
output Boolean b;
protected
BackendDAE.Var v;
algorithm
v := BackendVariable.getVarAt(vars,index);
b := BackendVariable.isStateVar(v);
end isStateonIndex;
protected function handleundifferntiableMSSLst
"author: Frenkel TUD 2012-12
handle list of undifferentiatable equations for
Pantelides index reduction method."
input list<tuple<list<Integer>,list<Integer>,list<Integer>>> iNotDiffableMSS;
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input array<Integer> inAss1;
input array<Integer> inAss2;
input BackendDAE.StructurallySingularSystemHandlerArg iArg;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output array<Integer> outAss1;
output array<Integer> outAss2;
output BackendDAE.StructurallySingularSystemHandlerArg oArg;
algorithm
(osyst,oshared,outAss1,outAss2,oArg):=
match (iNotDiffableMSS,inSystem,inShared,inAss1,inAss2,iArg)
local
list<tuple<list<Integer>,list<Integer>,list<Integer>>> notDiffableMSS;
list<Integer> unassignedEqns,unassignedStates,eqns,ilst;
BackendDAE.Variables v;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
BackendDAE.StateOrder so;
BackendDAE.ConstraintEquations orgEqnsLst;
array<list<Integer>> mapEqnIncRow;
array<Integer> mapIncRowEqn;
Integer noofeqns;
BackendDAE.StructurallySingularSystemHandlerArg arg;
BackendDAE.IncidenceMatrixT mt;
array<Integer> ass1,ass2;
case ({},_,_,_,_,_)
then (inSystem,inShared,inAss1,inAss2,iArg);
case ((eqns,unassignedStates,unassignedEqns)::notDiffableMSS,BackendDAE.EQSYSTEM(orderedVars=v,mT=SOME(mt)),_,_,_,(so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn,noofeqns))
equation
if Flags.isSet(Flags.BLT_DUMP) then
print("not differentiable minimal singular subset:\n");
print("unassignedEqns:\n");
BackendDump.debuglst(unassignedEqns, intString, ", ", "\n");
print("unassignedStates:\n");
BackendDump.debuglst(unassignedStates,intString, ", ", "\n");
end if;
ilst = List.fold1(unassignedStates, statesWithUnusedDerivative, mt, {});
ilst = List.select1(ilst, isStateonIndex, v);
// check also initial equations (this could be done also once before)
((_,(ilst,_))) = BackendDAEUtil.traverseBackendDAEExpsEqns(BackendEquation.getInitialEqnsFromShared(inShared),Expression.traverseSubexpressionsHelper,(searchDerivativesExp,(ilst,v)));
if Flags.isSet(Flags.BLT_DUMP) then
print("states without used derivative:\n");
BackendDump.debuglst(ilst,intString,", ","\n");
end if;
(syst,shared,ass1,ass2,so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn) =
handleundifferntiableMSS(intLe(listLength(ilst),listLength(unassignedEqns)),ilst,eqns,unassignedStates,unassignedEqns,inSystem,inShared,inAss1,inAss2,so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn);
(syst,shared,ass1,ass2,arg) = handleundifferntiableMSSLst(notDiffableMSS,syst,shared,ass1,ass2,(so,orgEqnsLst,mapEqnIncRow,mapIncRowEqn,noofeqns));
then
(syst,shared,ass1,ass2,arg);
end match;
end handleundifferntiableMSSLst;
protected function handleundifferntiableMSS
"author: Frenkel TUD 2012-11
try to solve a undifferentiable MSS."
input Boolean b "true if length(unassignedEqns) == length(statesWithUnusedDerivative)";
input list<Integer> statesWithUnusedDer;
input list<Integer> inEqns;
input list<Integer> unassignedStates;
input list<Integer> unassignedEqns;
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input array<Integer> inAss1;
input array<Integer> inAss2;
input BackendDAE.StateOrder inStateOrd;
input BackendDAE.ConstraintEquations inOrgEqnsLst;
input array<list<Integer>> imapEqnIncRow;
input array<Integer> imapIncRowEqn;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
output array<Integer> outAss1;
output array<Integer> outAss2;
output BackendDAE.StateOrder outStateOrd;
output BackendDAE.ConstraintEquations outOrgEqnsLst;
output array<list<Integer>> omapEqnIncRow;
output array<Integer> omapIncRowEqn;
algorithm
(osyst,oshared,outAss1,outAss2,outStateOrd,outOrgEqnsLst,omapEqnIncRow,omapIncRowEqn):=
matchcontinue (b,statesWithUnusedDer,inEqns,unassignedStates,unassignedEqns,inSystem)
local
Integer i;
BackendDAE.EquationArray eqns;
list<Integer> ilst, eqnslst, eqnslst1;
BackendDAE.Variables v,v1;
BackendDAE.IncidenceMatrix m;
BackendDAE.IncidenceMatrix mt;
BackendDAE.EqSystem syst;
array<Integer> ass1, ass2, mapIncRowEqn;
array<list<Integer>> mapEqnIncRow;
list<BackendDAE.Var> varlst;
BackendDAE.Var var;
DAE.FunctionTree funcs;
// 1th try to replace final parameter
case (_,_,_,_,_,syst as BackendDAE.EQSYSTEM(m=SOME(_), mT=SOME(_)))
equation
((eqns, eqnslst as _::_, _)) = List.fold1( inEqns, replaceFinalVars, BackendVariable.daeGlobalKnownVars(inShared),
(syst.orderedEqs, {}, BackendVarTransform.emptyReplacements()));
syst.orderedEqs = eqns;
// unassign changed equations and assigned vars
eqnslst1 = List.flatten(List.map1r(eqnslst, arrayGet, imapEqnIncRow));
ilst = List.map1r(eqnslst1, arrayGet, inAss2);
ilst = List.select1(ilst, intGt, 0);
ass2 = List.fold1r(eqnslst1, arrayUpdate, -1, inAss2);
ass1 = List.fold1r(ilst, arrayUpdate, -1, inAss1);
// update IncidenceMatrix
if Flags.isSet(Flags.BLT_DUMP) then
print("Replaced final Parameter in Eqns\n");
print("Update Incidence Matrix: ");
BackendDump.debuglst(eqnslst, intString, " ", "\n");
end if;
funcs = BackendDAEUtil.getFunctions(inShared);
(syst, mapEqnIncRow, mapIncRowEqn) =
BackendDAEUtil.updateIncidenceMatrixScalar(syst, BackendDAE.SOLVABLE(), SOME(funcs), eqnslst, imapEqnIncRow, imapIncRowEqn);
then
(syst, inShared, ass1, ass2, inStateOrd, inOrgEqnsLst, mapEqnIncRow, mapIncRowEqn);
// if size of unmatched eqns is equal to size of states without used derivative change all to algebraic