-
Notifications
You must be signed in to change notification settings - Fork 298
/
DataReconciliation.mo
2020 lines (1841 loc) · 90.5 KB
/
DataReconciliation.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 DataReconciliation
" file: dataReconciliation.mo
package: dataReconciliation
description: performs the extractionAlgorithm for dataReconciliation Problem"
public import BackendDAE;
public import DAE;
public import SymbolicJacobian;
public import BackendDump;
public import ExpressionDump;
protected
import BackendDAEUtil;
import BackendEquation;
import BackendVariable;
import ComponentReference;
import Expression;
import Error;
import Flags;
import DAEDump;
import Sorting;
import List;
import Matching;
import Util;
import System;
protected type ExtAdjacencyMatrixRow = tuple<Integer,list<Integer>>;
protected type ExtAdjacencyMatrix = list<ExtAdjacencyMatrixRow>;
public constant String UNDERLINE = "==========================================================================";
public function extractionAlgorithm
"runs the extraction Algorithm for dataReconciliation
Which return two sets of equations namely SET-C and SET-S"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
protected
BackendDAE.EqSystem currentSystem;
BackendDAE.EquationArray newOrderedEquationArray, outOtherEqns, outResidualEqns;
list<BackendDAE.Equation> newEqnsLst, setC_Eq, setS_Eq, residualEquations;
BackendDAE.AdjacencyMatrix adjacencyMatrix, newAdjacencyMatrix;
array<list<Integer>> mapEqnIncRow;
array<Integer> mapIncRowEqn, match1, match2;
list<tuple<Integer,Integer>> solvedEqsAndVarsInfo;
Integer varCount, eqCount, setEBLTRank, eqIndex;
list<Integer> matchedEqsLst, unMatchedEqsLst, unMatchedEqsLstCorrectIndex, approximatedEquations, constantEquations, tempSetC, setC, tempSetS, setS, boundaryConditionEquations;
list<list<Integer>> s_BLTBlocks, e_BLTBlocks, allBlocks, tmpAdjacencyMatrix;
list<list<String>> allBlocksStatusVarInfo;
list<tuple<Integer, BackendDAE.Equation>> e_BLT_EquationsWithIndex;
ExtAdjacencyMatrix eBltAdjacencyMatrix, sBltAdjacencyMatrix, setS_BLTAdjacencyMatrix;
list<tuple<Integer, Integer>> e_BLTSolvedEqsAndVars;
list<tuple<list<Integer>, Integer>> e_BLTBlockRanks, s_BLTBlockRanks;
list<tuple<list<Integer>, list<tuple<list<Integer>, Integer>>, list<tuple<list<String>, Integer>>>> s_BLTBlockTargetInfo;
list<tuple<list<Integer>, list<tuple<list<Integer>, Integer>>, list<tuple<list<String>, Integer>>, list<Integer>, list<Integer>, list<Integer>>> predecessorBlockTargetInfo;
list<BackendDAE.Var> paramVars, setSVars, residualVars;
list<DAE.ComponentRef> cr_lst;
BackendDAE.Jacobian simCodeJacobian;
BackendDAE.Shared shared;
String str, modelicaOutput, modelicaFileName;
list<Integer> allVarsList, knowns, unknowns, boundaryConditionVars, exactEquationVars, extractedVarsfromSetS, constantVars, knownVariablesWithEquationBinding, boundaryConditionTaggedEquationSolvedVars, unknownVarsInSetC;
BackendDAE.Variables inputVars, outDiffVars, outOtherVars, outResidualVars;
Boolean debug = false;
algorithm
if Flags.isSet(Flags.DUMP_DATARECONCILIATION) then
debug := true;
end if ;
{currentSystem} := inDAE.eqs;
shared := inDAE.shared;
print("\nModelInfo: " + shared.info.fileNamePrefix + "\n" + UNDERLINE + "\n\n");
BackendDump.dumpVariables(currentSystem.orderedVars, "OrderedVariables");
BackendDump.dumpEquationArray(currentSystem.orderedEqs, "OrderedEquation");
//BackendDump.dumpVariables(shared.globalKnownVars, "GlobalKnownVars");
(currentSystem, shared) := setBoundaryConditionEquationsAndVars(currentSystem, inDAE.shared, debug);
if debug then
BackendDump.dumpVariables(currentSystem.orderedVars, "Updated-OrderedVariables-withBoundaryConditionVars");
BackendDump.dumpEquationArray(currentSystem.orderedEqs, "Updated-OrderedVariables-withBoundaryConditionEqs");
BackendDump.dumpVariables(shared.globalKnownVars, "Updated-GlobalKnownVars-withBoundaryConditionVarsRemoved");
end if;
allVarsList := List.intRange(BackendVariable.varsSize(currentSystem.orderedVars));
// get the adjacency matrix of the Current System Square System
(adjacencyMatrix, _, _, _) := BackendDAEUtil.adjacencyMatrixScalar(currentSystem, BackendDAE.NORMAL(), NONE(), BackendDAEUtil.isInitializationDAE(shared));
// extract knowns, BoundaryConditions and exactEquationVars
(knowns, boundaryConditionVars, exactEquationVars) := getVariablesBlockCategories(currentSystem.orderedVars, allVarsList);
if debug then
print("\nVariablesCategories\n=============================");
print("\nknownVars :" + dumplistInteger(knowns));
print("\nboundaryConditionVars :" + dumplistInteger(boundaryConditionVars));
print("\nexactEquationVars :" + dumplistInteger(exactEquationVars));
print("\nadjacencyMatrix :" + anyString(adjacencyMatrix));
print("\n");
end if;
allVarsList := List.intRange(BackendVariable.varsSize(currentSystem.orderedVars));
// getEquations with known bindings
knownVariablesWithEquationBinding := getUncertainRefineVariablesBindedEquations(adjacencyMatrix, knowns);
if debug then
print("\nEquations with KnownBindings:\n===================================");
print("\nAdjacency Matrix :" + anyString(adjacencyMatrix));
print("\nLength of Adjacency Matrix :" + intString(arrayLength(adjacencyMatrix)));
print("\nList of known equation with bindings :" + anyString(knownVariablesWithEquationBinding));
print("\n");
end if;
// inverse the Modelica Model by introducing new set of equations
newEqnsLst := inverseModelicaModel(currentSystem.orderedVars, knownVariablesWithEquationBinding);
// add the new equations to the equation system to make the system overdetermined
currentSystem.orderedEqs := BackendEquation.merge(currentSystem.orderedEqs, BackendEquation.listEquation(newEqnsLst));
BackendDump.dumpEquationArray(currentSystem.orderedEqs, "OverDetermined-System-Equations");
// get the adjacency matrix for the overdetermined equation system
(adjacencyMatrix, _, mapEqnIncRow, mapIncRowEqn) := BackendDAEUtil.adjacencyMatrixScalar(currentSystem, BackendDAE.NORMAL(), NONE(), BackendDAEUtil.isInitializationDAE(shared));
varCount := currentSystem.orderedVars.numberOfVars;
eqCount := BackendEquation.equationArraySize(currentSystem.orderedEqs);
if debug then
print("\nOverDetermined-Systems-Information : \n====================================\n");
print("\nAdjacency Matrix :" + anyString(adjacencyMatrix));
print("\nNumber of Vars :" + intString(varCount));
print("\nNumber of Equations :" + intString(eqCount));
print("\n\n");
end if;
// Perform limited matching on the overdermined System to get subset of equations which are not matched to form the E-BLT
(match1, match2) := Matching.RegularMatching(adjacencyMatrix, varCount, eqCount);
BackendDump.dumpMatching(match1);
// get list of solved Vars and equations
(solvedEqsAndVarsInfo, matchedEqsLst) := getSolvedEquationAndVarsInfo(match1);
// Find the list of equations which are not matched (i.e) Equations which forms the E-BLT
unMatchedEqsLst := List.setDifference(List.intRange(eqCount), matchedEqsLst);
//unMatchedEqsLst := {105, 138, 143, 144, 147, 148};
// get the actual index of the Equation
unMatchedEqsLstCorrectIndex := List.unique(List.map1r(unMatchedEqsLst, listGet, arrayList(mapIncRowEqn)));
if debug then
print("\nFinding unmatched subset of equations : \n=========================================\n");
print("\nSolvedEqsAndVarsInfo :" + anyString(solvedEqsAndVarsInfo));
print("\nList of Equations :" + intString(BackendEquation.getNumberOfEquations(currentSystem.orderedEqs)));
print("\nMatchedEquationsLst :" + anyString(List.sort(matchedEqsLst, intGt)));
print("\nSizeofMatchedEquationLST :" + intString(listLength(matchedEqsLst)));
print("\nUnMatchedSubSetOfEquations :" + anyString(unMatchedEqsLst));
print("\nUnMatchedSubSetOfEquationsMappedIndex :" + anyString(unMatchedEqsLstCorrectIndex));
print("\n");
end if;
//unMatchedEquations := BackendEquation.getList(unMatchedEqsLstCorrectIndex, currentSystem.orderedEqs);
// Construct the E-BLT equations with index and rank from the unmatchedEquation List
(e_BLT_EquationsWithIndex, eBltAdjacencyMatrix, e_BLTSolvedEqsAndVars, e_BLTBlocks, e_BLTBlockRanks) := setEBLTEquationsWithIndexAndRank(unMatchedEqsLst, unMatchedEqsLstCorrectIndex, currentSystem.orderedEqs, adjacencyMatrix);
BackendDump.dumpEquationList(List.map1r(unMatchedEqsLstCorrectIndex, BackendEquation.get, currentSystem.orderedEqs), "E-BLT-Equations " + dumplistInteger(unMatchedEqsLst));
if debug then
print("\nE-BLT Information \n================");
print("\nE-BLT-Blocks :" + anyString(e_BLTBlocks));
print("\nE-BLT-Blocks-with ranks :" + anyString(e_BLTBlockRanks));
print("\nE-BLT-Adjacency-Matrix :" + anyString(eBltAdjacencyMatrix));
print("\nE_BLTSolvedEqsAndVars :" + anyString(e_BLTSolvedEqsAndVars));
print("\n");
end if;
/* Prepare the S-BLT System */
// delete the unmatched equations from the current system
currentSystem := deleteEquationsFromEqSyst(currentSystem, unMatchedEqsLstCorrectIndex);
varCount := currentSystem.orderedVars.numberOfVars;
eqCount := BackendEquation.equationArraySize(currentSystem.orderedEqs);
//BackendDump.dumpEquationList({BackendEquation.get(newOrderedEquationArray, 3)}, "Test get equation 3");
BackendDump.dumpEquationArray(currentSystem.orderedEqs, "reOrdered-Equations-after-removal");
BackendDump.dumpVariables(currentSystem.orderedVars,"reOrderedVariables");
// get the new adjacency matrix with new-reordered equations(square system) after removing the equations
(adjacencyMatrix, _, mapEqnIncRow, mapIncRowEqn) := BackendDAEUtil.adjacencyMatrixScalar(currentSystem, BackendDAE.NORMAL(), NONE(), BackendDAEUtil.isInitializationDAE(shared));
// Perform the matching on the new Square-System of equations
(match1, match2) := Matching.RegularMatching(adjacencyMatrix, varCount, eqCount);
BackendDump.dumpMatching(match1);
s_BLTBlocks := Sorting.Tarjan(adjacencyMatrix, match1); // run the tarjan algorithm to create the S-BLT on the Square-System
sBltAdjacencyMatrix := getSBLTAdjacencyMatrix(adjacencyMatrix); // get adjacency matrix with equations and list of variables present
(solvedEqsAndVarsInfo, _) := getSolvedEquationAndVarsInfo(match1); // get the solved Equations and Vars information from the new matching
s_BLTBlockRanks := List.toListWithPositions(s_BLTBlocks);
if debug then
print("\nS-BLT-Information\n================");
print("\nS-BLT Number of Vars :" + intString(varCount));
print("\nS-BLT Number of Equations :" + intString(eqCount));
print("\nS-BLT-Blocks :" + anyString(s_BLTBlocks));
print("\nS-BLT-Blocks-with ranks :" + anyString(s_BLTBlockRanks));
print("\nS-BLT Adjacency Matrix :" + anyString(sBltAdjacencyMatrix));
print("\nS_BLTSolvedEqsAndVars :" + anyString(solvedEqsAndVarsInfo));
print("\n\n");
end if;
/* Merge E-BLT blocks with S-BLT */
s_BLTBlocks := listAppend(s_BLTBlocks, e_BLTBlocks) annotation(__OpenModelica_DisableListAppendWarning=true); // append the E-BLT blocks to the end with S-BLT
s_BLTBlockRanks := listAppend(s_BLTBlockRanks, e_BLTBlockRanks) annotation(__OpenModelica_DisableListAppendWarning=true); // append E-BLT block ranks with S-BLT block ranks
sBltAdjacencyMatrix := listAppend(sBltAdjacencyMatrix, eBltAdjacencyMatrix) annotation(__OpenModelica_DisableListAppendWarning=true); // append the E-BLT Adjacency matrix with S-BLT Adjacency matrix
solvedEqsAndVarsInfo := listAppend(solvedEqsAndVarsInfo, e_BLTSolvedEqsAndVars) annotation(__OpenModelica_DisableListAppendWarning=true); // append the E-BLT Solved Equations and Vars to S-BLT vars
if debug then
print("\nCombined S-BLT and E-BLT Information \n================================");
print("\nCombined S-BLT-Blocks and E-BLT-Blocks :" + anyString(s_BLTBlocks));
print("\nCombined S-BLT-Blocks and E-BLT-Blocks with Ranks :" + anyString(s_BLTBlockRanks));
print("\nCombined Adjacency Matrix with S-BLT and E-BLT :" + anyString(sBltAdjacencyMatrix));
print("\nCombined SolvedEquationsVarsInfo with S-BLT and E-BLT :" + anyString(solvedEqsAndVarsInfo));
print("\n");
end if;
// dump BLT BLOCKS
dumpListList(s_BLTBlocks,"BLT_BLOCKS");
// Extract equations tagged as annotation(__OpenModelica_BoundaryCondition = true) and annotation(__OpenModelica_ApproximatedEquation = true)
(approximatedEquations, boundaryConditionEquations) := getEquationsTaggedApproximatedOrBoundaryCondition(BackendEquation.equationList(currentSystem.orderedEqs), 1);
if debug then
BackendDump.dumpEquationList(List.map1r(approximatedEquations, BackendEquation.get, currentSystem.orderedEqs), "ApproximatedEquations");
BackendDump.dumpEquationList(List.map1r(boundaryConditionEquations, BackendEquation.get, currentSystem.orderedEqs), "boundaryConditionEquations");
end if;
// get the Index mapping for approximated and constant equations
approximatedEquations := List.flatten(List.map1r(approximatedEquations, listGet, arrayList(mapEqnIncRow)));
boundaryConditionEquations := List.flatten(List.map1r(boundaryConditionEquations, listGet, arrayList(mapEqnIncRow)));
if debug then
print("\nApproximated and BoundaryCondition Equation Indexes :\n===========================================");
print("\nApproximatedEquationIndexes :" + dumplistInteger(approximatedEquations));
print("\nBoundayConditionEquationIndexes :" + dumplistInteger(boundaryConditionEquations));
print("\n");
end if;
// extract the constant variables
// constantVars := getExactConstantVariables(constantEquations, solvedEqsAndVarsInfo);
// extract boundaryConditionVariables
boundaryConditionTaggedEquationSolvedVars := getBoundaryConditionVariables(boundaryConditionEquations, solvedEqsAndVarsInfo);
// dump BoundaryCondition Equation Vars
if debug then
BackendDump.dumpVarList(List.map1r(listReverse(boundaryConditionTaggedEquationSolvedVars), BackendVariable.getVarAt, currentSystem.orderedVars),"boundaryConditionTaggedEquationSolvedVars");
end if;
// update the unknown variables without constantVars
//unknowns := List.setDifferenceOnTrue(unknowns, constantVars, intEq);
// update the exactEquation vars
exactEquationVars := List.setDifferenceOnTrue(exactEquationVars, boundaryConditionTaggedEquationSolvedVars, intEq);
// update the boundaryCondtions vars
boundaryConditionVars := listAppend(boundaryConditionVars, boundaryConditionTaggedEquationSolvedVars) annotation(__OpenModelica_DisableListAppendWarning=true);
if debug then
print("\nUpdatedVariablesCategories\n=============================");
print("\nknownVars :" + dumplistInteger(knowns));
print("\nboundaryConditionVars :" + dumplistInteger(boundaryConditionVars));
print("\nexactEquationVars :" + dumplistInteger(exactEquationVars));
print("\n");
end if;
(allBlocks, allBlocksStatusVarInfo) := traverseBLTAndUpdateBlockStatus(s_BLTBlocks, knowns, boundaryConditionVars, exactEquationVars, solvedEqsAndVarsInfo);
if debug then
dumpBlockStatus(allBlocks, allBlocksStatusVarInfo);
end if;
// find the block targets for blocks in S-BLT, as EBLT block targets are not useful for extraction algorithm
s_BLTBlockTargetInfo := findBlockTargets(allBlocks, allBlocksStatusVarInfo, solvedEqsAndVarsInfo, sBltAdjacencyMatrix, s_BLTBlockRanks, debug);
if debug then
dumpBlockTargets(s_BLTBlockTargetInfo);
end if;
// find predeccsorblocks
predecessorBlockTargetInfo := findPredecessorBlocks(s_BLTBlockTargetInfo);
dumpPredecessorBlocks(predecessorBlockTargetInfo);
// extract Set-C and Set-S equations using setOperation formula
(tempSetC, tempSetS) := ExtractEquationsUsingSetOperations(predecessorBlockTargetInfo, e_BLTBlockRanks, approximatedEquations, debug);
print("\nFINAL SET OF EQUATIONS After Reconciliation \n" + UNDERLINE + "\n" +"SET_C: "+dumplistInteger(tempSetC)+"\n" +"SET_S: "+ dumplistInteger(tempSetS)+ "\n\n" );
if debug then
dumpSetSVarsSolvedInfo(tempSetS, solvedEqsAndVarsInfo, mapIncRowEqn, currentSystem.orderedEqs, currentSystem.orderedVars);
end if;
//extractedVarsfromSetS := getVariablesAfterExtraction({}, tempSetS, sBltAdjacencyMatrix);
//extractedVarsfromSetS := List.setDifferenceOnTrue(extractedVarsfromSetS, knowns, intEq);
setC := List.unique(getAbsoluteIndexHelper(tempSetC, mapIncRowEqn));
setS := List.unique(getAbsoluteIndexHelper(tempSetS, mapIncRowEqn));
setC_Eq := getEquationsFromSBLTAndEBLT(setC, currentSystem.orderedEqs, e_BLT_EquationsWithIndex);
setS_Eq := getEquationsFromSBLTAndEBLT(setS, currentSystem.orderedEqs, e_BLT_EquationsWithIndex);
//BackendDump.dumpEquationList(setC_Eq,"SET_C");
//BackendDump.dumpEquationList(setS_Eq,"SET_S");
BackendDump.dumpEquationArray(BackendEquation.listEquation(setC_Eq), "SET_C");
BackendDump.dumpEquationArray(BackendEquation.listEquation(setS_Eq), "SET_S");
unknownVarsInSetC := getVariablesAfterExtraction(tempSetC, {}, sBltAdjacencyMatrix);
unknownVarsInSetC := listReverse(List.setDifferenceOnTrue(unknownVarsInSetC, knowns, intEq));
// get the blt adjacencyMatrix asscoiated with extracted SET-S
setS_BLTAdjacencyMatrix := getSetSAdjacencyMatrix(sBltAdjacencyMatrix, tempSetS);
if debug then
print("\nStart of Extract Minimal Set-S Algorithm\n" + UNDERLINE + "\n");
print("\nSet-S Adjacency MAtrix : " + intString(listLength(setS_BLTAdjacencyMatrix)) + "\n" + UNDERLINE + "\n" + anyString(setS_BLTAdjacencyMatrix));
print("\nS' : {}");
print("\nV_C :" + dumplistInteger(unknownVarsInSetC) + "\n");
end if;
// run the minimal SET-S extraction algorithm
(_, tempSetS) := extractMinimalSetS(unknownVarsInSetC, setS_BLTAdjacencyMatrix, knowns, currentSystem.orderedVars, currentSystem.orderedEqs, mapIncRowEqn, {}, debug);
if debug then
print("\n****End of Minimal extraction Algorithm****\n");
print("\nSet-S after running minimal extraction algorithm \n" + UNDERLINE + "\n" +"SET_S: "+dumplistInteger(tempSetS)+ "\n\n");
end if;
extractedVarsfromSetS := getVariablesAfterExtraction({}, tempSetS, sBltAdjacencyMatrix);
extractedVarsfromSetS := List.setDifferenceOnTrue(extractedVarsfromSetS, knowns, intEq);
setC := List.unique(getAbsoluteIndexHelper(tempSetC, mapIncRowEqn));
setS := List.unique(getAbsoluteIndexHelper(tempSetS, mapIncRowEqn));
//print("\nFINAL SET OF EQUATIONS After Mapping \n" + UNDERLINE + "\n" +"SET_C: "+dumplistInteger(setC)+"\n" +"SET_S: "+ dumplistInteger(setS)+ "\n\n" );
setC_Eq := getEquationsFromSBLTAndEBLT(setC, currentSystem.orderedEqs, e_BLT_EquationsWithIndex);
setS_Eq := getEquationsFromSBLTAndEBLT(setS, currentSystem.orderedEqs, e_BLT_EquationsWithIndex);
// dump minimal SET-S equations
BackendDump.dumpEquationArray(BackendEquation.listEquation(setS_Eq), "SET_S_After_Minimal_Extraction");
// prepare outdiff vars (i.e) variables of interest
outDiffVars := BackendVariable.listVar(List.map1r(knowns, BackendVariable.getVarAt, currentSystem.orderedVars));
// set uncertain variables unreplaceable attributes to be true
outDiffVars := BackendVariable.listVar(List.map1(BackendVariable.varList(outDiffVars), BackendVariable.setVarUnreplaceable, true));
// prepare set-c residual equations and residual vars
(_, residualEquations) := BackendEquation.traverseEquationArray(BackendEquation.listEquation(setC_Eq), BackendEquation.traverseEquationToScalarResidualForm, (shared.functionTree, {}));
(residualEquations, residualVars) := BackendEquation.convertResidualsIntoSolvedEquations(listReverse(residualEquations), "$res_F_", BackendVariable.makeVar(DAE.emptyCref), 1);
outResidualVars := BackendVariable.listVar(listReverse(residualVars));
outResidualEqns := BackendEquation.listEquation(residualEquations);
// prepare set-s other equations
outOtherEqns := BackendEquation.listEquation(setS_Eq);
// extract parameters from set-s equations
paramVars := BackendEquation.equationsVars(outOtherEqns, shared.globalKnownVars);
//setSVars := BackendEquation.equationsVars(outOtherEqns, currentSystem.orderedVars);
// prepare variables stucture from list of extracted equations
outOtherVars := BackendVariable.listVar(List.map1r(extractedVarsfromSetS, BackendVariable.getVarAt, currentSystem.orderedVars));
dumpSetSVars(outOtherVars, "Unknown variables in SET_S ");
//BackendDump.dumpVariables(BackendVariable.listVar(setSVars),"Unknown variables in SET_S_checks ");
BackendDump.dumpVariables(BackendVariable.listVar(paramVars),"Parameters in SET_S");
VerifyDataReconciliation(tempSetC, tempSetS, knowns, boundaryConditionVars, sBltAdjacencyMatrix, solvedEqsAndVarsInfo, exactEquationVars, approximatedEquations, currentSystem.orderedVars, currentSystem.orderedEqs, mapIncRowEqn, outOtherVars, setS_Eq);
if debug then
BackendDump.dumpVariables(outDiffVars, "Jacobian_knownVariables");
BackendDump.dumpVariables(outResidualVars, "Jacobian_outResidualVars");
BackendDump.dumpVariables(outOtherVars, "Jacobian_outOtherVars");
BackendDump.dumpEquationArray(outResidualEqns, "Jacobian_ResidualEquation");
BackendDump.dumpEquationArray(outOtherEqns, "Jacobian_other_Equation");
end if;
// generate symbolicJacobian matrix F
(simCodeJacobian, shared) := SymbolicJacobian.getSymbolicJacobian(outDiffVars, outResidualEqns, outResidualVars, outOtherEqns, outOtherVars, shared, outOtherVars, "F", false);
// put the jacobian also into shared object
shared.dataReconciliationData := SOME(BackendDAE.DATA_RECON(symbolicJacobian=simCodeJacobian, setcVars=outResidualVars, datareconinputs=outDiffVars));
// Prepare the final DAE System with Set-C equations as residual equations
currentSystem := BackendDAEUtil.setEqSystVars(currentSystem, BackendVariable.mergeVariables(outResidualVars, outOtherVars));
currentSystem := BackendDAEUtil.setEqSystEqs(currentSystem, BackendEquation.merge(outResidualEqns, outOtherEqns));
inputVars := BackendVariable.listVar(List.map1(BackendVariable.varList(outDiffVars), BackendVariable.setVarDirection, DAE.INPUT()));
shared := BackendDAEUtil.setSharedGlobalKnownVars(shared, BackendVariable.mergeVariables(shared.globalKnownVars, inputVars));
// write the list of known variables to the csv file with the headers
if not System.regularFileExists(inDAE.shared.info.fileNamePrefix + "_Inputs.csv") then
str := "Variable Names,Measured Value-x,HalfWidthConfidenceInterval,xi,xk,rx_ik\n";
str := dumpToCsv(str, BackendVariable.varList(outDiffVars));
System.writeFile(shared.info.fileNamePrefix + "_Inputs.csv", str);
end if;
// write the new Reconciled vars and equations to .mo File
modelicaFileName := "Reconciled_"+ System.stringReplace(shared.info.fileNamePrefix, ".","_");
modelicaOutput := "/* This is not Complete ThermoSysPro variables and functions needs to be corrected manually */\n";
modelicaOutput := modelicaOutput + "model " + modelicaFileName ;
// Variables Declaration section
modelicaOutput := dumpExtractedVars(modelicaOutput, BackendVariable.varList(outDiffVars), "Variables of Interest");
modelicaOutput := dumpExtractedVars(modelicaOutput, paramVars, "parameters in SET-S");
modelicaOutput := dumpResidualVars(modelicaOutput, BackendVariable.varList(outResidualVars), "residualVars");
modelicaOutput := dumpExtractedVars(modelicaOutput, BackendVariable.varList(outOtherVars), "remaining variables in setS");
// Equation Declaration section
modelicaOutput := modelicaOutput + "\nequation";
modelicaOutput := dumpExtractedEquations(modelicaOutput, outResidualEqns, "set-C Canonical form");
modelicaOutput := dumpExtractedEquations(modelicaOutput, outOtherEqns, "remaining equations in Set-S");
modelicaOutput := modelicaOutput + "\nend " + modelicaFileName + ";";
System.writeFile(modelicaFileName + ".mo", modelicaOutput);
// update the DAE with new system of equations and vars computed by the dataReconciliation extraction algorithm
outDAE := BackendDAE.DAE({currentSystem}, shared);
end extractionAlgorithm;
protected function getSetSAdjacencyMatrix
"return the adjacency matrix associated with set-S"
input ExtAdjacencyMatrix sBltAdjacencyMatrix;
input list<Integer> setS;
output ExtAdjacencyMatrix setS_BltAdjacencyMatrix = {};
protected
Integer eq;
algorithm
for i in sBltAdjacencyMatrix loop
(eq, _) := i;
if listMember(eq, setS) then
setS_BltAdjacencyMatrix := i :: setS_BltAdjacencyMatrix;
end if;
end for;
end getSetSAdjacencyMatrix;
protected function extractMinimalSetS
"construct a minimal set-S using recursive algorithm, which are needed to solve intermediate
variables in set-c and also avoid complication when calculating jacobians"
input output list<Integer> unknownsInSetC;
input ExtAdjacencyMatrix sBltAdjacencyMatrix;
input list<Integer> knownVars;
input BackendDAE.Variables orderedVars;
input BackendDAE.EquationArray orderedEqs;
input array<Integer> mapIncRowEqn;
input output list<Integer> minimalSetS = {};
input Boolean debug;
protected
Integer firstMatchedEquation, mappedEq;
BackendDAE.Var var;
BackendDAE.Equation tmpEq;
list<Integer> rest, vars, intermediateVars = {}, V_EQ;
algorithm
for varIndex in unknownsInSetC loop
// break the recursion loop
if listEmpty(unknownsInSetC) then
break;
end if;
if debug then
print("\nIntermediate varList : " + dumplistInteger(unknownsInSetC) + "\n" + UNDERLINE + "\n");
end if;
_ :: rest := unknownsInSetC;
(firstMatchedEquation, vars) := getVariableFirstOccurrenceInEquation(sBltAdjacencyMatrix, varIndex, minimalSetS);
var := BackendVariable.getVarAt(orderedVars, varIndex);
if not intEq(firstMatchedEquation, 0) then // equation exists
minimalSetS := firstMatchedEquation :: minimalSetS; // insert the equation into S'
minimalSetS := List.unique(minimalSetS);
intermediateVars := List.setDifferenceOnTrue(vars, knownVars, intEq); // get intermediate vars in matched equations
V_EQ := List.unique(listAppend(intermediateVars, rest));
if debug then
dumpMininimalExtraction(varIndex, var, firstMatchedEquation, mapIncRowEqn, orderedEqs, minimalSetS, intermediateVars, rest, V_EQ, false);
end if;
// V_EQ exist, recursive call
(unknownsInSetC, minimalSetS) := extractMinimalSetS(V_EQ, sBltAdjacencyMatrix, knownVars, orderedVars, orderedEqs, mapIncRowEqn, minimalSetS, debug);
else // equation not exist
if debug then
dumpMininimalExtraction(varIndex, var, 0, mapIncRowEqn, orderedEqs, {}, {}, rest, {}, true);
end if;
unknownsInSetC := rest; // update the list with the remaining Vars
end if;
end for;
end extractMinimalSetS;
protected function dumpMininimalExtraction
"dumps the minimal set-S extraction algorithm"
input Integer varIndex;
input BackendDAE.Var var;
input Integer firstMatchedEquation;
input array<Integer> mapIncRowEqn;
input BackendDAE.EquationArray orderedEqs;
input list<Integer> minimalSetS;
input list<Integer> intermediateVars;
input list<Integer> rest;
input list<Integer> V_EQ;
input Boolean falseBlock;
protected
Integer mappedEq;
BackendDAE.Equation tmpEq;
algorithm
if falseBlock then
print("\nVarIndex : " + intString(varIndex));
print("\nVariable Name : " + ComponentReference.printComponentRefStr(var.varName));
print("\nEquation Not Exist : " + "NIL");
print("\nRemainingVars : " + dumplistInteger(rest) + "\n");
else
mappedEq := listGet(arrayList(mapIncRowEqn), firstMatchedEquation);
tmpEq := BackendEquation.get(orderedEqs, mappedEq);
//print("\n" + " (" + intString(mappedEq) + "/" + intString(firstMatchedEquation) + "): " + BackendDump.equationString(tmpEq) + "\n");
print("\nVarIndex : " + intString(varIndex));
print("\nVariable Name : " + ComponentReference.printComponentRefStr(var.varName));
print("\nEquation Exist : " + intString(firstMatchedEquation));
print("\nmappedEquation : " + intString(mappedEq));
print("\nMatched Equation : " + BackendDump.equationString(tmpEq));
print("\nS' : " + dumplistInteger(minimalSetS));
print("\nUnknowns in matchedEquation : " + dumplistInteger(intermediateVars));
print("\nRemaining Vars : " + dumplistInteger(rest));
print("\nV_EQ : " + dumplistInteger(V_EQ) + "\n");
end if;
end dumpMininimalExtraction;
protected function getVariableFirstOccurrenceInEquation
"returns the first equation that contains the variable index (e.g)
var index = 6
BLT = {(1, {26, 5}), (2, {25, 6}, (3, {1, 2, 6})}
result = (2,{25, 6})"
input ExtAdjacencyMatrix m;
input Integer varIndex;
input list<Integer> minimalSetS;
output tuple<Integer, list<Integer>> matchedEquation = (0, {}) "default value 0 means equation does not exist";
protected
list<Integer> ret, vars, matchedeq;
Integer eq, eqnum, varnum;
algorithm
for i in m loop
(eq, vars) := i;
//print("\nVarcheck : " + intString(varIndex) + "=>" + anyString(eq) + " => " + anyString(vars));
if eq > 0 then
if not listMember(eq, minimalSetS) then
if listMember(varIndex, vars) then
//print("\n Found the first equation : " + anyString(i));
matchedEquation := i;
break;
end if;
end if;
end if;
end for;
end getVariableFirstOccurrenceInEquation;
protected function dumpResidualVars
"returns the variables of interest in modelica format"
input String instring;
input list<BackendDAE.Var> invar;
input String comment;
output String outstring="";
protected
DAE.ComponentRef cr;
algorithm
outstring := "\n //" + comment;
for var in invar loop
cr := BackendVariable.varCref(var);
outstring := outstring + "\n " + DAEDump.daeTypeStr(var.varType) + " " + System.stringReplace(ComponentReference.crefStr(cr), ".", "_") + ";";
outstring := System.stringReplace(outstring, "$", "");
end for;
outstring := instring+outstring;
end dumpResidualVars;
protected function dumpExtractedVars
"returns the variables and parameters in set-S in modelica format"
input String instring;
input list<BackendDAE.Var> invar;
input String comment;
output String outstring="";
protected
DAE.ComponentRef cr;
algorithm
outstring := "\n //"+ comment;
for var in invar loop
cr := BackendVariable.varCref(var);
if BackendVariable.varHasUncertainValueRefine(var) then
outstring := outstring + "\n parameter " + DAEDump.daeTypeStr(var.varType) + " " + System.stringReplace(ComponentReference.crefStr(cr), ".", "_") + ";";
elseif BackendVariable.isParam(var) then
outstring := outstring + "\n parameter " + DAEDump.daeTypeStr(var.varType) + " " + System.stringReplace(ComponentReference.crefStr(cr), ".", "_") + " = " + ExpressionDump.printOptExpStr(var.bindExp) +";";
else
outstring := outstring + "\n " + DAEDump.daeTypeStr(var.varType) + " " + System.stringReplace(ComponentReference.crefStr(cr), ".", "_") + ";";
end if;
end for;
outstring := instring+outstring;
end dumpExtractedVars;
protected function dumpExtractedEquations
"returns the Equation in modelica format"
input String instring;
input BackendDAE.EquationArray eqs;
input String comment;
output String outstring="";
algorithm
outstring := "\n //"+ comment;
for eq in BackendEquation.equationList(eqs) loop
outstring := outstring + "\n " + dumpEquationString(eq) + ";";
end for;
outstring := instring+outstring;
end dumpExtractedEquations;
public function setBoundaryConditionEquationsAndVars
"Function which iterates shared.globalKnownVars Real parameters and
check for boundaryCondition vars declared as
(e.g) parameter Real x = 1 annotation(__OpenModelica_BoundaryCondition = true);
and add it to the orderedEqs and orederedVars"
input output BackendDAE.EqSystem currentSystem;
input output BackendDAE.Shared shared;
input Boolean debug;
protected
list<BackendDAE.Equation> eqnLst={};
list<BackendDAE.Var> daeVarsLst = {};
list<BackendDAE.Var> updatedGlobalKnownVarsLst = {};
DAE.Exp lhs, rhs;
BackendDAE.Equation eqn;
algorithm
for var in BackendVariable.varList(shared.globalKnownVars) loop
// check for param Vars, as we are only interested in causality = calculatedParameters
if BackendVariable.isRealParam(var) and BackendVariable.hasOpenModelicaBoundaryConditionAnnotation(var) then
//print("\n knownsVars :" + anyString(var.bindExp));
lhs := BackendVariable.varExp(var);
rhs := BackendVariable.varBindExp(var);
eqn := BackendDAE.EQUATION(lhs, rhs, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_BINDING);
eqnLst := eqn :: eqnLst;
var := BackendVariable.setVarKind(var, BackendDAE.VARIABLE());
var := BackendVariable.setBindExp(var, NONE());
daeVarsLst := var :: daeVarsLst;
elseif (BackendVariable.isIntParam(var) or BackendVariable.isBoolParam(var)) and BackendVariable.hasOpenModelicaBoundaryConditionAnnotation(var) then
Error.addMessage(Error.INTERNAL_ERROR, {": Boundary Condition cannot be set on Integer or Boolean parameters: " + ComponentReference.printComponentRefStr(var.varName) + " must be Real, The extraction algorithm will fail"});
fail();
else
updatedGlobalKnownVarsLst := var :: updatedGlobalKnownVarsLst;
//inDAE.shared := BackendVariable.addGlobalKnownVarDAE(var, inDAE.shared);
end if;
end for;
if debug then
BackendDump.dumpVarList(daeVarsLst, "boundaryConditionVarsTaggedAsParmeters");
end if;
// update the EqSyst with new boundary Conditions vars and equations
currentSystem := BackendVariable.addVarsDAE(daeVarsLst, currentSystem);
currentSystem.orderedEqs := BackendEquation.merge(currentSystem.orderedEqs, BackendEquation.listEquation(eqnLst));
shared := BackendDAEUtil.setSharedGlobalKnownVars(shared, BackendVariable.listVar(updatedGlobalKnownVarsLst));
end setBoundaryConditionEquationsAndVars;
protected function deleteEquationsFromEqSyst
"deletes the umatched equations from Matching algorithm, the removed equations are part of E_BLT blocks"
input output BackendDAE.EqSystem currentSystem;
input list<Integer> eqIndex;
protected
BackendDAE.EquationArray newEqArray, newOrderedEquationArray;
algorithm
currentSystem.orderedEqs := BackendEquation.deleteList(currentSystem.orderedEqs, eqIndex);
//currentSystem.orderedEqs :=BackendEquation.addList(unMatchedEquations, BackendEquation.deleteList(currentSystem.orderedEqs, unMatchedEqsLst));
// add the new set of equations
newOrderedEquationArray := BackendEquation.emptyEqns();
// add the new set of equations to have continuous indices after deleting the equations
BackendEquation.addList(BackendEquation.equationList(currentSystem.orderedEqs), newOrderedEquationArray);
//BackendDump.dumpEquationArray(newOrderedEquationArray, "After new update of equations");
currentSystem := BackendDAEUtil.setEqSystEqs(currentSystem, newOrderedEquationArray);
end deleteEquationsFromEqSyst;
protected function getBoundaryConditionsEquationIndex
"returns the boundary condition variables equation indexes
eg: Real q0 = 100 annotation(__OpenModelica_BoundaryCondition = true)"
input BackendDAE.AdjacencyMatrix adjacencyMatrix;
input list<Integer> boundaryConditions;
output list<Integer> boundaryConditionsEquationIndexes = {};
protected
Integer count = 1;
algorithm
/* get equation index which have annotation __OpenModelica_BoundaryCondition=true*/
for i in arrayList(adjacencyMatrix) loop
for j in boundaryConditions loop
if valueEq(i, {j}) then
boundaryConditionsEquationIndexes := count :: boundaryConditionsEquationIndexes;
break;
end if;
end for;
count := count + 1;
end for;
end getBoundaryConditionsEquationIndex;
protected function getUncertainRefineVariablesBindedEquations
"returns if the uncertainRefine variables is already binded with equation
eg: Real q1(uncertain=Uncertainty.refine) = 1; "
input BackendDAE.AdjacencyMatrix adjacencyMatrix;
input list<Integer> knowns;
output list<Integer> knownsWithBindedEquations = {};
algorithm
/* check already binded equations for variables of interest*/
for i in arrayList(adjacencyMatrix) loop
for j in knowns loop
if valueEq(i, {j}) then
knownsWithBindedEquations := j :: knownsWithBindedEquations;
end if;
end for;
end for;
end getUncertainRefineVariablesBindedEquations;
protected function getExactConstantVariables
"return the solved variables for equations that are tagged as annotation( __OpenModelica_ExactConstantEquation = true)"
input list<Integer> constantEquations;
input list<tuple<Integer, Integer>> solvedEqsVarInfo;
output list<Integer> constantVariables = {};
protected
Integer varNumber;
algorithm
for eq in constantEquations loop
(_, varNumber) := getSolvedVariableNumber(eq, solvedEqsVarInfo);
constantVariables := varNumber :: constantVariables;
end for;
end getExactConstantVariables;
protected function getBoundaryConditionVariables
"return the solved variables for equations that are tagged as annotation(__OpenModelica_BoundaryCondition = true)"
input list<Integer> boundaryConditionEquations;
input list<tuple<Integer, Integer>> solvedEqsVarInfo;
output list<Integer> boundaryConditionVariables = {};
protected
Integer varNumber;
algorithm
for eq in boundaryConditionEquations loop
(_, varNumber) := getSolvedVariableNumber(eq, solvedEqsVarInfo);
boundaryConditionVariables := varNumber :: boundaryConditionVariables;
end for;
end getBoundaryConditionVariables;
protected function getEquationsFromSBLTAndEBLT
"function which returns the List of Equations from S-BLT and E-BLT"
input list<Integer> inList;
input BackendDAE.EquationArray sBLT_Equations;
input list<tuple<Integer, BackendDAE.Equation>> eBLT_Equations;
output list<BackendDAE.Equation> outEquationsList = {};
algorithm
for eqIndex in inList loop
if eqIndex > 0 then // S-BLT equations
outEquationsList := BackendEquation.get(sBLT_Equations, eqIndex) :: outEquationsList;
else
// negative index, eqs in E-BLT blocks
outEquationsList := getEquationsFromEBLT(eqIndex, eBLT_Equations) :: outEquationsList;
end if;
end for;
outEquationsList := listReverse(outEquationsList);
end getEquationsFromSBLTAndEBLT;
protected function getEquationsFromEBLT
"returns the equations from EBLT blocks which has negative index"
input Integer eBLTIndex;
input list<tuple<Integer, BackendDAE.Equation>> eBLT_Equations;
output BackendDAE.Equation outEquations;
protected
Integer index;
BackendDAE.Equation eq;
algorithm
for eqs in eBLT_Equations loop
(index, eq) := eqs;
if intEq(eBLTIndex, index) then
outEquations := eq;
break;
end if;
end for;
end getEquationsFromEBLT;
protected function getAbsoluteIndexHelper
"returns the absolute index of equations, For equations in E-BLT, negative index is returned"
input list<Integer> inList;
input array<Integer> mapIncRowEqn;
output list<Integer> outList = {};
algorithm
for i in inList loop
if i > 0 then
outList := listGet(arrayList(mapIncRowEqn), i) :: outList;
else
// equations in E-BLT, have negativeindex
outList := i :: outList;
end if;
end for;
outList := listReverse(outList);
end getAbsoluteIndexHelper;
protected function dumpSetSVarsSolvedInfo
"dumps set-S equations solved var info along with equations"
input list<Integer> tempSetS;
input list<tuple<Integer, Integer>> solvedEqsVarInfo;
input array<Integer> mapIncRowEqn;
input BackendDAE.EquationArray orderedEqs;
input BackendDAE.Variables orderedVars;
protected
Integer count = 1, varNumber, mappedEq;
BackendDAE.Var var;
BackendDAE.Equation tmpEq;
algorithm
print("\nSet-S Solved-Variables Information :" + "(" + intString(listLength(tempSetS)) + ")" + "\n==========================================\n");
for eq in tempSetS loop
(_, varNumber) := getSolvedVariableNumber(eq, solvedEqsVarInfo);
var := BackendVariable.getVarAt(orderedVars, varNumber);
mappedEq := listGet(arrayList(mapIncRowEqn), eq);
tmpEq := BackendEquation.get(orderedEqs, mappedEq);
print("\n" + intString(count) + ": " + "eqn " + intString(eq) + " solves var " + intString(varNumber) + " => " + ComponentReference.printComponentRefStr(var.varName));
print("\n" + " (" + intString(mappedEq) + "/" + intString(eq) + "): " + BackendDump.equationString(tmpEq) + "\n");
count := count + 1;
end for;
print("\n\n");
end dumpSetSVarsSolvedInfo;
protected function dumpSetSVars
"dumps variables involved in set-S equations except parameters and constants"
input BackendDAE.Variables setSVars;
input String heading;
protected
Integer count = 1, varNumber;
BackendDAE.Var var;
algorithm
print("\n" + heading + " (" + intString(BackendVariable.varsSize(setSVars)) + ")\n" + "========================================" + "\n");
for var in BackendVariable.varList(setSVars) loop
print("\n" + intString(count) + ": " + ComponentReference.printComponentRefStr(var.varName) + " type: " + DAEDump.daeTypeStr(var.varType));
count := count + 1;
end for;
print("\n\n");
end dumpSetSVars;
protected function dumpBlockStatus
"dumps the blt block status with the varInfo (i.e) knowns, unkowns and constants"
input list<list<Integer>> allBlocks;
input list<list<String>> allBlocksStatusVarInfo;
protected
Integer count = 1;
algorithm
print("\nBLT-BLOCK_STATUS\n=================\n");
for blocks in allBlocks loop
print ("\nBlock :" + dumplistInteger(blocks) + " || blockStatusVarInfo :" + anyString(listGet(allBlocksStatusVarInfo, count)));
count := count + 1;
end for;
print("\n");
end dumpBlockStatus;
protected function dumpBlockTargets
"dumps block targets of all blocks in BLT "
input list<tuple<list<Integer>, list<tuple<list<Integer>, Integer>>,list<tuple<list<String>, Integer>>>> s_BLTBlockTargetInfo;
protected
list<Integer> mainBlock;
list<tuple<list<Integer>, Integer>> targetBlocks;
list<tuple<list<String>, Integer>> targetBlocksStatusVarInfo;
algorithm
print("\nS-BLTBlocks-TargetInfo\n=======================\n");
for blocks in s_BLTBlockTargetInfo loop
(mainBlock, targetBlocks, targetBlocksStatusVarInfo) := blocks;
print ("\nBlock :" + dumplistInteger(mainBlock) + " || blockTargetsInfo :" + anyString(targetBlocks) + " || blockStatusVarInfo :" + anyString(targetBlocksStatusVarInfo));
end for;
print("\n");
end dumpBlockTargets;
protected function dumpPredecessorBlocks
"dumps predecessorBlocks information"
input list<tuple<list<Integer>, list<tuple<list<Integer>, Integer>>, list<tuple<list<String>, Integer>>, list<Integer>, list<Integer>, list<Integer>>> predecessorBlockInfo;
protected
list<Integer> knownBlocks, constantBlocks;
list<tuple<list<Integer>, list<tuple<list<Integer>, Integer>>, list<tuple<list<String>, Integer>>, list<Integer>, list<Integer>, list<Integer>>> blueBlocksTargets = {}, redBlocksTargets = {}, constantBlocksTargets = {};
algorithm
print("\nTargets of blocks without predecessors:\n========================================");
for blocks in predecessorBlockInfo loop
(_, _, _, knownBlocks, constantBlocks, _) := blocks;
if not listEmpty(knownBlocks) then
blueBlocksTargets := blocks :: blueBlocksTargets;
elseif not listEmpty(constantBlocks) then
constantBlocksTargets := blocks :: constantBlocksTargets;
else
redBlocksTargets := blocks :: redBlocksTargets;
end if;
//print ("\nBlock :" + dumplistInteger(mainBlock) + " || blockTargetsInfo :" + anyString(targetBlocks) + " || KnownBlocks :" + dumplistInteger(knownBlocks) + " || constantBlocks :" + dumplistInteger(constantBlocks));
end for;
print("\n");
dumpPredecessorBlocksHelper(blueBlocksTargets, "knowns", "Targets of Blue blocks");
dumpPredecessorBlocksHelper(redBlocksTargets, "unknowns", "Targets of Red blocks");
dumpPredecessorBlocksHelper(constantBlocksTargets, "constant", "Targets of Brown blocks");
end dumpPredecessorBlocks;
protected function dumpPredecessorBlocksHelper
"helper function which dumps predecessor blocks into different categories
namely B
Blue - known Blocks
Red - BoundaryCondition Blocks
Brown - All remaining Blocks which are not known or Boundary Conditions Blocks"
input list<tuple<list<Integer>, list<tuple<list<Integer>, Integer>>, list<tuple<list<String>, Integer>>, list<Integer>, list<Integer>, list<Integer>>> predecessorBlockInfo;
input String blockInfo;
input String header;
protected
list<Integer> mainBlock;
list<tuple<list<Integer>, Integer>> targetBlocks;
list<Integer> knownBlocks, constantBlocks;
algorithm
print("\n" + header + " (" + intString(listLength(predecessorBlockInfo)) + ")" +"\n==============================\n");
for blocks in listReverse(predecessorBlockInfo) loop
(mainBlock, targetBlocks, _, knownBlocks, constantBlocks, _) := blocks;
print ("\nBlock :" + dumplistInteger(mainBlock) + " || blockTargetsInfo :" + anyString(targetBlocks) + " || KnownBlocks :" + dumplistInteger(knownBlocks) + " || constantBlocks :" + dumplistInteger(constantBlocks));
end for;
print("\n\n");
end dumpPredecessorBlocksHelper;
public function ExtractEquationsUsingSetOperations
"Extraction the equations from predecessor Blocks and group them
into two categories namely set-C and Set-S using setOperation formula"
input list<tuple<list<Integer>, list<tuple<list<Integer>, Integer>>, list<tuple<list<String>, Integer>>, list<Integer>, list<Integer>, list<Integer>>> predecessorBlockInfo;
input list<tuple<list<Integer>, Integer>> e_BLTBlockRanks;
input list<Integer> approximatedEquations;
input Boolean debug;
output list<Integer> setC;
output list<Integer> setS;
protected
list<Integer> mainBlock, tmpSetC_1, tmpSetC_2, tmpSetS_1, tmpSetS_2, z1, z2;
list<tuple<list<Integer>, Integer>> targetBlocks;
list<Integer> knownBlocks, constantBlocks, e_BLTBlockRanksWithoutRanks = {}, targetBlocksWithKnowns = {}, targetBlocksWithUnknowns = {}, targetBlocksWithConstants = {};
algorithm
for blocks in predecessorBlockInfo loop
(mainBlock, targetBlocks, _, knownBlocks, constantBlocks, _) := blocks;
if not listEmpty(knownBlocks) then // known Blocks
targetBlocksWithKnowns := filterTargetBlocksWithoutRanks(List.rest(targetBlocks), targetBlocksWithKnowns);
elseif not listEmpty(constantBlocks) then // constant Blocks
targetBlocksWithConstants := filterTargetBlocksWithoutRanks(targetBlocks, targetBlocksWithConstants);
else // unknown Blocks
targetBlocksWithUnknowns := filterTargetBlocksWithoutRanks(targetBlocks, targetBlocksWithUnknowns);
end if;
end for;
targetBlocksWithKnowns := List.unique(targetBlocksWithKnowns);
targetBlocksWithUnknowns := List.unique(targetBlocksWithUnknowns);
targetBlocksWithConstants := List.unique(targetBlocksWithConstants);
// collect all E-BLT blocks
e_BLTBlockRanksWithoutRanks := filterTargetBlocksWithoutRanks(e_BLTBlockRanks, e_BLTBlockRanksWithoutRanks);
if debug then
print("\nUnion of Blue, Red and Yellow and E-BLT-Blocks\n=====================================================");
print("\nUnion-E-BLT-blocks :" + dumplistInteger(e_BLTBlockRanksWithoutRanks));
print("\nUnion-Blue-TargetBlockInfo (blocks with Knowns) :" + dumplistInteger(targetBlocksWithKnowns));
print("\nUnion-Red-TargetBlockInfo (blocks with UnKnowns) :" + dumplistInteger(targetBlocksWithUnknowns));
print("\nUnion-Brown-TargetBlockInfo (blocks with Exact eqns) :" + dumplistInteger(targetBlocksWithConstants));
end if;
/*
Extract Set-C equation
SetC = (targetBlocksWithKnowns) intersection (e_BLTBlockRanksWithoutRanks) - (targetBlocksWithUnknowns) intersection (e_BLTBlockRanksWithoutRanks)
*/
tmpSetC_1 := List.intersectionOnTrue(targetBlocksWithKnowns, e_BLTBlockRanksWithoutRanks, intEq);
tmpSetC_2 := List.intersectionOnTrue(targetBlocksWithUnknowns, e_BLTBlockRanksWithoutRanks, intEq);
setC := List.setDifferenceOnTrue(tmpSetC_1, tmpSetC_2, intEq);
// remove approximated equation from setC if present
setC:= List.setDifferenceOnTrue(setC, approximatedEquations, intEq);
if debug then
print("\n\nSetC-Operations\n====================");
print("\n(BlocksWithKnowns) intersection (e_BLTBlocks) :" + dumplistInteger(tmpSetC_1));
print("\n(BlocksWithUnknowns) intersection (e_BLTBlocks) :" + dumplistInteger(tmpSetC_2));
print("\nSetC :" + dumplistInteger(setC));
print("\n");
end if;
/*
Extract Set-S equation