-
Notifications
You must be signed in to change notification settings - Fork 299
/
Uncertainties.mo
2084 lines (1832 loc) · 79.9 KB
/
Uncertainties.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
package Uncertainties
import DAE;
import Absyn;
import Algorithm;
import BackendDAE;
import BackendVariable;
import List;
import Env;
import Interactive;
import Values;
import SCode;
import Flags;
import Error;
import System;
import CevalScript;
import Dependency;
import SCodeUtil;
import Inst;
import InnerOuter;
import DAEUtil;
import BackendDAECreate;
import BackendDAEUtil;
import BackendDAETransform;
import BackendEquation;
import HashTable;
import ComponentReference;
import BaseHashTable;
import Expression;
import ClassInf;
import BackendVarTransform;
import ExpressionSolve;
import BackendDump;
import ExpressionSimplify;
import MathematicaDump;
public function dumpComponents "function: dumpComponents
author: PA
Prints the blocks of the BLT sorting on stdout.
"
input list<list<Integer>> l;
algorithm
print("Blocks\n");
print("=======\n");
dumpComponents2(l, 1);
end dumpComponents;
protected function dumpComponents2 "function: dumpComponents2
author: PA
Helper function to dumpComponents.
"
input list<list<Integer>> inIntegerLstLst;
input Integer inInteger;
algorithm
_:=
matchcontinue (inIntegerLstLst,inInteger)
local
Integer ni,i_1,i;
list<String> ls;
String s;
list<Integer> l;
list<list<Integer>> lst;
case ({},_) then ();
case ((l :: lst),i)
equation
print("{");
ls = List.map(l, intString);
s = stringDelimitList(ls, ", ");
print(s);
print("}\n");
i_1 = i + 1;
dumpComponents2(lst, i_1);
then
();
end matchcontinue;
end dumpComponents2;
public function createIndexMap "creates an index map that translates old incices to new, when a list of indices are removed.
For example.
{1,2,3,4,5} - Vars, n == 5
{2,4} - removeIndices
=> {1->1,2->0,3->2,4->0,5->3}
"
input list<Integer> removeIndices "must be sorted in ascending order";
input Integer n;
output array<Integer> map;
protected array<Integer> an;
algorithm
an := arrayCreate(n,0);
map := createIndexMap2(an,removeIndices,1,1);
end createIndexMap;
protected function createIndexMap2 "help function to createIndexMap"
input array<Integer> mapIn;
input list<Integer> removeIn;
input Integer i;
input Integer off;
output array<Integer> outMap;
algorithm
outMap := matchcontinue(mapIn,removeIn,i,off)
local Integer v;
array<Integer> map;
list<Integer> remove;
case(map,{},i,off) equation
map = fillRestMap(map,i,off,arrayLength(map)+1-i);
then
map;
case(map,v::remove,i,off) equation
true = i == v "remove";
map = createIndexMap2(map,remove,i+1,off);
then
map;
case(map,v::remove,i,off) equation
false = i == v "keep";
map = arrayUpdate(map,i,off);
map = createIndexMap2(map,v::remove,i+1,off+1);
then
map;
end matchcontinue;
end createIndexMap2;
protected function fillRestMap "help function to e.g. removeEquationsWithMap, fills the rest of the map after all equations been removed, i.e.
the left over should be kept, indices increased by one in each step"
input array<Integer> mapIn;
input Integer indx;
input Integer i;
input Integer N;
output array<Integer> outMap;
algorithm
outMap := matchcontinue(mapIn,indx,i,N)
local
array<Integer> map;
case(map,indx,i,0) then map;
case(map,indx,i,N) equation
map = arrayUpdate(map,indx,i);
map = fillRestMap(map,indx+1,i+1,N-1);
then map;
case(map,indx,i,N) equation
print("fillRestMap failed\n");
then fail();
end matchcontinue;
end fillRestMap;
protected function removeArrayElements
input array<Type_a> arrayIn;
input list<Integer> elems;
input Type_a typeDefault;
output array<Type_a> arrayOut;
replaceable type Type_a subtypeof Any;
protected array<Type_a> newArray;
protected Integer n;
algorithm
n:=arrayLength(arrayIn)-listLength(elems);
newArray:=arrayCreate(n,typeDefault);
arrayOut:= removeArrayElements2(arrayIn,newArray,1,1,elems,n);
end removeArrayElements;
protected function removeArrayElements2
input array<Type_a> array1In;
input array<Type_a> array2In;
input Integer index1;
input Integer index2;
input list<Integer> elems;
input Integer maxn;
output array<Type_a> arrayOut;
replaceable type Type_a subtypeof Any;
algorithm
arrayOut:=matchcontinue(array1In,array2In,index1,index2,elems,maxn)
local
list<Integer> tail;
Integer head;
array<Type_a> array1,array2;
case(_,array2,_,index2,_,maxn)
equation
true=(index2>maxn);
then array2;
case(array1,array2,index1,index2,elems,maxn)
equation
true=listMember(index1,elems);
then
removeArrayElements2(array1,array2,index1+1,index2,elems,maxn);
case(array1,array2,index1,index2,elems,maxn)
equation
array2=arrayUpdate(array2,index2,array1[index1]);
then
removeArrayElements2(array1,array2,index1+1,index2+1,elems,maxn);
case(_,_,_,_,_,_)
then
fail();
end matchcontinue;
end removeArrayElements2;
protected function intEqZero
input Integer i;
output Boolean o;
algorithm
o:=not intEq(i,0);
end intEqZero;
protected function removeZeros
input list<Integer> l;
output list<Integer> o;
algorithm
o:=List.filterOnTrue(l,intEqZero);
end removeZeros;
protected function fixIndices2
input list<Integer> listIn;
input array<Integer> map;
output list<Integer> listOut;
algorithm
listOut:=List.map1r(listIn,arrayGet,map);
end fixIndices2;
protected function fixIndices
input BackendDAE.IncidenceMatrix listIn;
input array<Integer> map;
output BackendDAE.IncidenceMatrix listOut;
protected list<list<Integer>> l1,l2;
algorithm
l1:=arrayList(listIn);
l2:=List.map1(l1,fixIndices2,map);
l2:=List.map(l2,removeZeros);
listOut:=listArray(l2);
end fixIndices;
protected function fixNonMatchedVariables2
input array<Integer> ass2In;
input BackendDAE.IncidenceMatrix mtIn;
input Integer index;
input Integer max;
output array<Integer> ass2Out;
algorithm
ass2Out:=matchcontinue(ass2In,mtIn,index,max)
local
array<Integer> ass2;
Integer eqn;
case(ass2,_,index,max)
equation
true = (index>max);
then
ass2;
case(ass2,mtIn,index,max)
equation
eqn = ass2[index];
true = intEq(eqn,0); // if the index is zero
eqn = List.first(mtIn[index]); // get a new one
ass2=arrayUpdate(ass2,index,eqn);
then
fixNonMatchedVariables2(ass2,mtIn,index+1,max);
case(ass2,mtIn,index,max)
then
fixNonMatchedVariables2(ass2,mtIn,index+1,max);
end matchcontinue;
end fixNonMatchedVariables2;
protected function fixNonMatchedVariables
input BackendDAE.IncidenceMatrix mtIn;
input array<Integer> ass2In;
output array<Integer> ass2Out;
Integer n;
algorithm
n:=arrayLength(ass2In);
ass2Out:=fixNonMatchedVariables2(ass2In,mtIn,1,n);
end fixNonMatchedVariables;
protected function getRemovedVariables
input BackendDAE.IncidenceMatrix mtIn;
input Integer index;
input Integer maxn;
input list<Integer> acc;
output list<Integer> out;
algorithm
out:=matchcontinue(mtIn,index,maxn,acc)
case(mtIn,index,maxn,acc)
equation
true=(index>maxn);
then acc;
case(mtIn,index,maxn,acc)
equation
true=(intEq(listLength(mtIn[index]),0));
then
getRemovedVariables(mtIn,index+1,maxn,index::acc);
case(mtIn,index,maxn,acc)
then
getRemovedVariables(mtIn,index+1,maxn,acc);
end matchcontinue;
end getRemovedVariables;
protected function removeEquationsFromSystem
input BackendDAE.IncidenceMatrix mIn;
input BackendDAE.IncidenceMatrix mtIn;
input list<Integer> eqns; // equations to remove
output BackendDAE.IncidenceMatrix mOut;
output BackendDAE.IncidenceMatrix mtOut;
output list<Integer> removedVars;
protected array<Integer> eqIndexMap,varIndexMap;
protected Integer maxn;
protected BackendDAE.IncidenceMatrix m,mt;
algorithm
maxn:=arrayLength(mIn);
eqIndexMap:=createIndexMap(List.sort(eqns,intGt),maxn); // creates a map of the indices of the equations
m:=removeArrayElements(mIn,eqns,{}); // removes the equations from the incidence matrix
mt:= fixIndices(mtIn,eqIndexMap); // adjust the indices in the transpose of the incidence matrix
removedVars:=getRemovedVariables(mt,1,arrayLength(mt),{}); // get the variables that do no appear in any equation
mt:=removeArrayElements(mt,removedVars,{}); // remove the variables from the transpose of the incidence matrix
maxn:=arrayLength(mtIn);
varIndexMap:=createIndexMap(List.sort(removedVars,intGt),maxn); // creates a map of the indices for the removed variables
m:=fixIndices(m,varIndexMap); // updates the indices of the variables (in the incidence matrix) after removing variables
mOut:=m;
mtOut:=mt;
end removeEquationsFromSystem;
protected function dumpUncertainVariablesInBlocks
input BackendDAE.BackendDAE dlow;
input BackendDAE.IncidenceMatrix m;
input list<list<Integer>> comps;
algorithm
_:=matchcontinue(dlow,m,comps)
local list<Integer> comp;
list<list<Integer>> tail;
list<Integer> uncVars;
case(_,_,{}) then ();
case(dlow,m,comp::tail) equation
uncVars=getUncertainRefineVariablesInBlocks(dlow,m,{comp});
print("Block: {");
print(stringDelimitList(List.map(comp,intString)," , "));
print("}\n");
print("\tVariables:");
print(stringDelimitList(List.map(uncVars,intString)," , "));
print("\n");
dumpUncertainVariablesInBlocks(dlow,m,tail);
then ();
case(_,_,_)
equation
print("***dumpUncertainVariablesInBlocks: failed");
then ();
end matchcontinue;
end dumpUncertainVariablesInBlocks;
public function modelEquationsUC
input Env.Cache inCache;
input Env.Env inEnv;
input Absyn.Path className "path for the model";
input Interactive.SymbolTable inInteractiveSymbolTable;
input String inFileNamePrefix;
//input Boolean addDummy "if true, add a dummy state";
//input Option<SimulationSettings> inSimSettingsOpt;
//input Absyn.FunctionArgs args "labels for remove terms";
output Env.Cache outCache;
output Values.Value outValue;
output Interactive.SymbolTable outInteractiveSymbolTable;
algorithm
(outCache,outValue,outInteractiveSymbolTable):=
matchcontinue (inCache,inEnv,className,inInteractiveSymbolTable,inFileNamePrefix)
local
String filenameprefix,resstr;
list<SCode.Element> p_1;
DAE.DAElist dae;
list<Env.Frame> env;
BackendDAE.BackendDAE dlow,dlow_1;
Absyn.ComponentRef a_cref;
list<String> libs;
Interactive.SymbolTable st;
Absyn.Program p,ptot;
//DAE.Exp fileprefix;
Env.Cache cache;
DAE.FunctionTree funcs;
Real timeFrontend;
BackendDAE.IncidenceMatrix m,mt;
array<Integer> ass1,ass2;
BackendDAE.Value n;
list<list<Integer>> comps;
Integer varCount;
BackendDAE.Variables vars,kvars;
list<Integer> varIndexList, allVarIndexList, refineVarIndexList, elimVarIndexList,approximatedEquations,equationToExtract,otherEquations,squareBlockEquations,removedVars;
BackendDAE.EquationArray eqns,ieqns;
list<BackendDAE.Equation> eqnLst,ieqnLst;
list<BackendDAE.EqSystem> eqsyslist;
String modelName;
array<Integer> eqMap;
BackendDAE.Variables allVars;
BackendDAE.EquationArray allEqs;
list<Integer> allUncertainVars;
BackendDAE.Shared shared;
BackendDAE.EqSystem newSystem;
BackendDAE.EquationArray newSystemEqns;
BackendDAE.Variables newSystemVars;
case (cache,env,className,(st as Interactive.SYMBOLTABLE(ast = p)),filenameprefix)
equation
//print("* Calling modelEquationsUC\n");
// calculate stuff that we need to create SimCode data structure
System.realtimeTick(CevalScript.RT_CLOCK_UNCERTAINTIES);
//(cache,Values.STRING(filenameprefix),SOME(_)) = Ceval.ceval(cache,env, fileprefix, true, SOME(st),NONE(), msg);
ptot = Dependency.getTotalProgram(className,p);
p_1 = SCodeUtil.translateAbsyn2SCode(ptot);
(cache,env,_,dae) = Inst.instantiateClass(cache,InnerOuter.emptyInstHierarchy,p_1,className);
timeFrontend = System.realtimeTock(CevalScript.RT_CLOCK_UNCERTAINTIES);
System.realtimeTick(CevalScript.RT_CLOCK_BACKEND);
dae = DAEUtil.transformationsBeforeBackend(cache,env,dae);
funcs = Env.getFunctionTree(cache);
//print("* Flatten ok\n");
dlow = BackendDAECreate.lower(dae,cache,env,true);
//(dlow_1,funcs1) = BackendDAEUtil.getSolvedSystem(dlow, funcs,SOME({"removeSimpleEquations","removeFinalParameters", "removeEqualFunctionCalls", "expandDerOperator"}), NONE(), NONE(),NONE());
(dlow_1) = BackendDAEUtil.getSolvedSystem(dlow, SOME({"removeSimpleEquations","removeFinalParameters"}), NONE(), NONE(),SOME({}));
print("* Lowered Ok \n");
//BackendDump.dump(dlow_1);
BackendDAE.DAE(eqsyslist as (BackendDAE.EQSYSTEM(allVars,allEqs,SOME(m),SOME(mt),BackendDAE.MATCHING(ass1,ass2,_))::_),shared) = dlow_1;
allUncertainVars=getUncertainRefineVariableIndexes(allVars,List.intRange(BackendVariable.varsSize(allVars)));
////////////////////////////////////////////
// Eliminate variables whose uncertain attribute is not set to Uncertainty.refine.
allVarIndexList = List.intRange(BackendVariable.varsSize(allVars));
refineVarIndexList = getUncertainRefineVariableIndexes(allVars, allVarIndexList);
elimVarIndexList = List.setDifferenceOnTrue(allVarIndexList, refineVarIndexList, intEq);
//print("Original variables with uncertainty attibute: ");
//print(stringDelimitList(List.map(refineVarIndexList,intString)," , "));
//print("\n");
//print("Variables to eliminate: ");
//print(stringDelimitList(List.map(elimVarIndexList,intString)," , "));
//print("\n");
(dlow_1 as BackendDAE.DAE(BackendDAE.EQSYSTEM(allVars,allEqs,SOME(m),SOME(mt),BackendDAE.MATCHING(ass1,ass2,_))::_,shared as BackendDAE.SHARED(knownVars=kvars,initialEqs=ieqns)))
= eliminateVariablesDAE(elimVarIndexList,dlow_1);
//BackendDump.dump(dlow_1);
////////////////////////////////////////////////////////////////
comps = BackendDAETransform.tarjanAlgorithm(m,mt,ass1,ass2);
//print("All components:\n");
//dumpComponents(comps);
//dumpUncertainVariablesInBlocks(dlow_1,m,comps);
refineVarIndexList = getUncertainRefineVariablesInBlocks(dlow_1, m, comps);
//print("Variables with uncertainty attibute: ");
//print(stringDelimitList(List.map(refineVarIndexList,intString)," , "));
//print("\n");
//BackendDump.dump(dlow_1);
squareBlockEquations = getSquareBlocks(m,comps,{},refineVarIndexList);
//print("Equations in squared blocks: ");
//print(stringDelimitList(List.map(squareBlockEquations,intString)," , "));
//print("\n");
(m,mt,removedVars)=removeEquationsFromSystem(m,mt,squareBlockEquations);
//print("Removed variables from squared blocks: ");
//print(stringDelimitList(List.map(removedVars,intString)," , "));
//print("\n");
newSystemEqns = BackendDAEUtil.listEquation(List.deletePositions(BackendDAEUtil.equationList(allEqs),List.map1(squareBlockEquations,intAdd,-1)));
newSystemVars = BackendDAEUtil.listVar(listReverse(List.deletePositions(BackendDAEUtil.varList(allVars),List.map1(removedVars,intAdd,-1))));
//newSystemVars = allVars;
newSystem=BackendDAE.EQSYSTEM(newSystemVars, newSystemEqns, SOME(m), SOME(mt), BackendDAE.NO_MATCHING());
dlow_1 = BackendDAE.DAE({newSystem},shared);
//BackendDump.dump(dlow_1);
//print("Final components:\n");
//dumpComponents(comps);
//dumpUncertainVariablesInBlocks(dlow_1,m,comps);
//BackendDump.dump(dlow_1);
refineVarIndexList = getUncertainRefineVariableIndexes(newSystemVars,List.intRange(BackendVariable.varsSize(newSystemVars)));
//BackendDump.dump(dlow_1);
//dumpUncertainVariablesInBlocks(dlow_1,m,comps);
//print("Variables with uncertainty attibute: ");
//print(stringDelimitList(List.map(refineVarIndexList,intString)," , "));
//print("\n");
// Extract equations for uncertainty computations.
//uccomps = extractEquationsForUC(dlow_1, m, comps);
//print("UC components:\n");
//dumpUncertainVariablesInBlocks(dlow_1, m, uccomps);
// Get a subsystem with only the equations and variables considered for uncertainty computations.
//eqnIndexList = getEquationsInBlocks(uccomps);
//print("Original equations with uncertainty variables: ");
//print(stringDelimitList(List.map(eqnIndexList,intString)," , "));
//print("\n");
//BackendDAE.DAE(eqs = BackendDAE.EQSYSTEM(orderedEqs=eqns)::_) = dlow_1;
//BackendDump.dumpEqns(List.map1r(eqnIndexList,listGet,BackendDAEUtil.equationList(eqns)));
// get equations with annotation ApproximatedEquation
approximatedEquations=getEquationsWithApproximatedAnnotation(dlow_1);
//print("Approximated Equations: ");
//print(stringDelimitList(List.map(approximatedEquations,intString)," , "));
//print("\n");
///////////refineVarIndexList = getUncertainRefineVariablesInBlocks(dlow_1, m, comps);
otherEquations=getEquationsWithOneVariable(dlow_1,refineVarIndexList);
equationToExtract = List.setDifference(List.setDifference(List.intRange(arrayLength(m)), approximatedEquations),otherEquations);
varIndexList = List.flatten(arrayList(m));
(dlow_1 as BackendDAE.DAE(BackendDAE.EQSYSTEM(orderedVars = vars as BackendDAE.VARIABLES(numberOfVars = varCount),orderedEqs=eqns)::_,BackendDAE.SHARED(knownVars=kvars,initialEqs=ieqns))) = getSubSystemDaeForVars(equationToExtract, varIndexList, dlow_1);
//print("Equations after removing non influencial blocks: \n");
//BackendDump.dumpEqns(BackendDAEUtil.equationList(eqns));
//BackendDump.dump(dlow_1);
// Eliminate variables whose uncertain attribute is not set to Uncertainty.refine.
//allVarIndexList = List.intRange(varCount);
//refineVarIndexList = getUncertainRefineVariableIndexes(vars, allVarIndexList);
//elimVarIndexList = List.setDifferenceOnTrue(allVarIndexList, refineVarIndexList, intEq);
//print("Variables with uncertainty attibute: ");
//print(stringDelimitList(List.map(refineVarIndexList,intString)," , "));
//print("\n");
//print("Variables to eliminate: ");
//print(stringDelimitList(List.map(elimVarIndexList,intString)," , "));
//print("\n");
//(dlow_1 as BackendDAE.DAE(BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns)::_,BackendDAE.SHARED(knownVars=kvars,initialEqs=ieqns,arrayEqs=arrEqns,algorithms=algs))) = eliminateVariablesDAE(elimVarIndexList,dlow_1);
eqnLst = BackendDAEUtil.equationList(eqns);
ieqnLst = BackendDAEUtil.equationList(ieqns);
print("* Uncertainty equations extracted: \n");
BackendDump.dumpEqns(eqnLst);
//modelName=Absyn.pathLastIdent(className);
//print(System.readEnv("TEMP")+&"\\uncertainties.out");
System.writeFile(System.readEnv("TEMP")+&"\\uncertainties.out",
MathematicaDump.dumpMmaDAEStr((vars,kvars,eqnLst,ieqnLst)));
resstr="Done...";
then
(cache,Values.STRING(resstr),st);
case (_,_,className,_,_)
equation
true = Flags.isSet(Flags.FAILTRACE);
resstr = Absyn.pathStringNoQual(className);
resstr = stringAppendList({"modelEquationsUC: The model equations in model",resstr," could not be extracted"});
Error.addMessage(Error.INTERNAL_ERROR, {resstr});
then
fail();
end matchcontinue;
end modelEquationsUC;
protected function getEquationsWithOneVariable
input BackendDAE.BackendDAE dae;
input list<Integer> uncertainVariables;
output list<Integer> outEq;
algorithm
outEq:=matchcontinue(dae,uncertainVariables)
local array<list<Integer>> m;
case (BackendDAE.DAE(BackendDAE.EQSYSTEM(_,_,SOME(m),_,_)::_,_),uncertainVariables)
then
getEquationsWithOneVariable2(arrayList(m),uncertainVariables,1);
end match;
end getEquationsWithOneVariable;
protected function getEquationsWithOneVariable2
input list<list<Integer>> m;
input list<Integer> uncertainVariables;
input Integer index;
output list<Integer> out;
algorithm
out:=matchcontinue(m,uncertainVariables,index)
local
list<list<Integer>> tail;
list<Integer> head,indexList;
case({},_,_)
then {};
case(head::tail,uncertainVariables,index)
equation
false = getEquationsWithOneVariable3(head,uncertainVariables);
indexList = getEquationsWithOneVariable2(tail,uncertainVariables,index+1);
then
index::indexList;
case(head::tail,uncertainVariables,index)
then
getEquationsWithOneVariable2(tail,uncertainVariables,index+1);
end matchcontinue;
end getEquationsWithOneVariable2;
protected function getEquationsWithOneVariable3
input list<Integer> variables;
input list<Integer> uncertainVariables;
output Boolean out;
protected list<Integer> temp;
algorithm
temp:=List.intersectionOnTrue(variables,uncertainVariables,intEq);
out:=listLength(temp)>1;
end getEquationsWithOneVariable3;
protected function getEquationsWithApproximatedAnnotation
input BackendDAE.BackendDAE dae;
output list<Integer> outEqs;
algorithm
outEqs:=match(dae)
local
Integer n;
BackendDAE.EquationArray orderedEqs;
list<Integer> ret;
case(BackendDAE.DAE(BackendDAE.EQSYSTEM(orderedEqs=orderedEqs)::_,_))
equation
ret=getEquationsWithApproximatedAnnotation2(BackendDAEUtil.equationList(orderedEqs),0);
then
ret;
case(_)
then {};
end match;
end getEquationsWithApproximatedAnnotation;
protected function getEquationsWithApproximatedAnnotation2
input list<BackendDAE.Equation> eqs;
input Integer index;
output list<Integer> listOut;
algorithm
listOut:=
matchcontinue(eqs,index)
local
BackendDAE.Equation h;
list<BackendDAE.Equation> t;
list<Integer> inner_ret;
Integer i;
case ({},_)
then
{};
case(h::t,i)
equation
true=isApproximatedEquation(h);
inner_ret = getEquationsWithApproximatedAnnotation2(t,i+1);
then
i::inner_ret;
case(h::t,i)
equation
inner_ret = getEquationsWithApproximatedAnnotation2(t,i+1);
then
inner_ret;
end matchcontinue;
end getEquationsWithApproximatedAnnotation2;
protected function isApproximatedEquation
input BackendDAE.Equation eqn;
output Boolean out;
algorithm
out:= match(eqn)
local
list<SCode.Comment> comment;
Boolean ret;
case(BackendDAE.EQUATION(_,_,DAE.SOURCE(comment=comment)))
equation
ret = isApproximatedEquation2(comment);
then
ret;
case(_)
then
false;
end match;
end isApproximatedEquation;
protected function isApproximatedEquation2
input list<SCode.Comment> commentIn;
output Boolean out;
algorithm
out:= matchcontinue(commentIn)
local
SCode.Comment h;
list<SCode.Comment> t;
Boolean ret;
list<SCode.SubMod> subModLst;
case({})
equation
then false;
case(SCode.COMMENT(annotation_=SOME(SCode.ANNOTATION(SCode.MOD(subModLst=subModLst))))::t)
equation
ret = (List.exist(subModLst,isApproximatedEquation3)) or isApproximatedEquation2(t);
then
ret;
case(h::t)
equation
ret = isApproximatedEquation2(t);
then
ret;
end matchcontinue;
end isApproximatedEquation2;
protected function isApproximatedEquation3
input SCode.SubMod m;
output Boolean out;
algorithm
out:= match(m)
case(SCode.NAMEMOD("__OpenModelica_ApproximatedEquation",SCode.MOD(binding = SOME((Absyn.BOOL(true),_)))))
then true;
case(_)
then false;
end match;
end isApproximatedEquation3;
public function eliminateVariablesDAE
"
author: Daniel Hedberg, 2011-01
Eliminates the specified variables between the given set of equations.
"
input list<Integer> elimVarIndexList;
input BackendDAE.BackendDAE indae;
output BackendDAE.BackendDAE outDae;
algorithm
outDae := match(elimVarIndexList, indae)
local
BackendDAE.BackendDAE dae;
BackendDAE.Variables vars,vars_1,kvars,kvars_1;
BackendDAE.EquationArray eqns,ieqns;
BackendDAE.EqSystem syst;
BackendDAE.Shared shared;
//EventInfo ei;
//ExternalObjectClasses extObjCls;
HashTable.HashTable s;
//list<IfEquation> ifeqns,iifeqns;
HashTable.HashTable crefDouble;
BackendDAE.IncidenceMatrix m;
//IfEquation arr_ifeqns,arr_iifeqns;
HashTable.HashTable movedvars_1;
list<BackendDAE.Equation> seqns,eqnLst,ieqnLst;
BackendVarTransform.VariableReplacements repl;
// Functions funcs;
// Real t1,t2;
case(elimVarIndexList,dae as BackendDAE.DAE((syst as BackendDAE.EQSYSTEM(orderedEqs=eqns,orderedVars=vars))::_,(shared as BackendDAE.SHARED(knownVars=kvars,initialEqs=ieqns)))) equation
ieqnLst = BackendDAEUtil.equationList(ieqns);
eqnLst = BackendDAEUtil.equationList(eqns);
crefDouble = findArraysPartiallyIndexed(eqnLst);
//print("partially indexed crs:"+&Util.stringDelimitList(Util.listMap(crefDouble,Exp.printComponentRefStr),",\n")+&"\n");
repl = BackendVarTransform.emptyReplacements();
(m,_) = BackendDAEUtil.incidenceMatrix(syst, shared, BackendDAE.NORMAL());
(eqnLst,seqns,movedvars_1,repl) = eliminateVariablesDAE2(eqnLst,1,vars,kvars,HashTable.emptyHashTable(),repl,crefDouble,m,elimVarIndexList,false);
//Debug.fcall("dumprepl",BackendVarTransform.dumpReplacements,repl);
dae = setDaeEqns(dae,BackendDAEUtil.listEquation(eqnLst),false);
//dae = setDaeSimpleEqns(dae,listEquation(listAppend(equationList(reqns),seqns)));
dae = replaceDAElow(dae,repl,NONE(),false);
(vars_1,kvars_1) = moveVariables(BackendVariable.daeVars(syst),BackendVariable.daeKnVars(shared),movedvars_1);
dae = setDaeVars(dae,vars_1);
dae = setDaeKnownVars(dae,kvars_1);
dae = BackendDAEUtil.transformBackendDAE(dae,SOME((BackendDAE.NO_INDEX_REDUCTION(),BackendDAE.ALLOW_UNDERCONSTRAINED())),NONE(),NONE());
/*
Debug.fcall("dumprepldae",print," removed "+&intString(listLength(seqns))+&" eqns \n");
Debug.fcall2("dumprepldae",dump,dae,false);
Debug.fcall("dumprepldae",print,"removeSimpleEquationsDAE repl : ");
Debug.fcall("dumprepldae", BackendVarTransform.dumpReplacements, repl); */
then dae;
end match;
end eliminateVariablesDAE;
public function setDaeEqns "set the equations of a dae
public function setEquations
"
input BackendDAE.BackendDAE dae;
input BackendDAE.EquationArray eqns;
input Boolean initEqs "if true, set initialEquations instead of ordered equations";
output BackendDAE.BackendDAE odae;
algorithm
odae := match(dae,eqns,initEqs)
local
BackendDAE.EqSystem syst;
list<BackendDAE.EqSystem> systList;
BackendDAE.Shared shared;
BackendDAE.Variables orderedVars "orderedVars ; ordered Variables, only states and alg. vars" ;
BackendDAE.EquationArray orderedEqs "orderedEqs ; ordered Equations" ;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.Matching matching;
BackendDAE.Variables knownVars "knownVars ; Known variables, i.e. constants and parameters" ;
BackendDAE.Variables externalObjects "External object variables";
BackendDAE.AliasVariables aliasVars "mappings of alias-variables to real-variables"; // added asodja 2010-03-03
BackendDAE.EquationArray initialEqs "initialEqs ; Initial equations" ;
BackendDAE.EquationArray removedEqs "these are equations that cannot solve for a variable. for example assertions, external function calls, algorithm sections without effect" ;
array<DAE.Constraint> constraints "constraints" ;
array<DAE.ClassAttributes> classAttrs;
Env.Cache cache;
Env.Env env;
DAE.FunctionTree funcs;
BackendDAE.EventInfo eventInfo "eventInfo" ;
BackendDAE.ExternalObjectClasses extObjClasses "classes of external objects, contains constructor & destructor";
BackendDAE.BackendDAEType backendDAEType "indicate for what the BackendDAE is used";
BackendDAE.SymbolicJacobians symjacs;
case(BackendDAE.DAE(
(syst as BackendDAE.EQSYSTEM(orderedVars=orderedVars,orderedEqs=orderedEqs,m=m,mT=mT,matching=matching))::systList,
(shared as BackendDAE.SHARED(knownVars=knownVars,externalObjects=externalObjects,aliasVars=aliasVars,initialEqs=initialEqs,
removedEqs=removedEqs,functionTree=funcs,
eventInfo=eventInfo,extObjClasses=extObjClasses,backendDAEType=backendDAEType,symjacs=symjacs))),eqns,false)
equation
syst = BackendDAE.EQSYSTEM(orderedVars,eqns,m,mT,matching);
then
BackendDAE.DAE(syst::systList,shared);
case(BackendDAE.DAE(
(syst as BackendDAE.EQSYSTEM(orderedVars=orderedVars,orderedEqs=orderedEqs,m=m,mT=mT,matching=matching))::systList,
(shared as BackendDAE.SHARED(knownVars=knownVars,externalObjects=externalObjects,aliasVars=aliasVars,initialEqs=initialEqs,
removedEqs=removedEqs,constraints=constraints,classAttrs=classAttrs,cache=cache,env=env,
functionTree=funcs,eventInfo=eventInfo,extObjClasses=extObjClasses,backendDAEType=backendDAEType,symjacs=symjacs))),eqns,true)
equation
shared = BackendDAE.SHARED(knownVars,externalObjects,aliasVars,eqns,removedEqs,constraints,classAttrs,cache,env,funcs,eventInfo,extObjClasses,backendDAEType,symjacs);
then
BackendDAE.DAE(syst::systList,shared);
end match;
end setDaeEqns;
public function replaceDAElow
input BackendDAE.BackendDAE idlow;
input BackendVarTransform.VariableReplacements repl;
input Option<PredicateFunction> func;
partial function PredicateFunction
input DAE.Exp e;
output Boolean b;
end PredicateFunction;
input Boolean replaceVariables "if true, run replacementrules on variablelist also: Note: requires destinations in repl to be crefs!";
output BackendDAE.BackendDAE odae;
algorithm
odae := match(idlow,repl,func,replaceVariables)
local
BackendDAE.EqSystem syst;
list<BackendDAE.EqSystem> systList;
BackendDAE.Shared shared;
BackendDAE.Variables orderedVars "orderedVars ; ordered Variables, only states and alg. vars" ;
BackendDAE.EquationArray orderedEqs "orderedEqs ; ordered Equations" ;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.Matching matching;
BackendDAE.Variables knownVars "knownVars ; Known variables, i.e. constants and parameters" ;
BackendDAE.Variables externalObjects "External object variables";
BackendDAE.AliasVariables aliasVars "mappings of alias-variables to real-variables"; // added asodja 2010-03-03
BackendDAE.EquationArray initialEqs "initialEqs ; Initial equations" ;
BackendDAE.EquationArray removedEqs "these are equations that cannot solve for a variable. for example assertions, external function calls, algorithm sections without effect" ;
array<DAE.Constraint> constraints "constraints" ;
array<DAE.ClassAttributes> classAttrs;
Env.Cache cache;
Env.Env env;
DAE.FunctionTree funcs;
BackendDAE.EventInfo eventInfo "eventInfo" ;
BackendDAE.ExternalObjectClasses extObjClasses "classes of external objects, contains constructor & destructor";
BackendDAE.BackendDAEType backendDAEType "indicate for what the BackendDAE is used";
BackendDAE.SymbolicJacobians symjacs;
case(BackendDAE.DAE(
(syst as BackendDAE.EQSYSTEM(orderedVars=orderedVars,orderedEqs=orderedEqs,m=m,mT=mT,matching=matching))::systList,
(shared as BackendDAE.SHARED(knownVars=knownVars,externalObjects=externalObjects,aliasVars=aliasVars,initialEqs=initialEqs,
removedEqs=removedEqs,constraints=constraints,classAttrs=classAttrs,cache=cache,env=env,
functionTree=funcs,eventInfo=eventInfo,extObjClasses=extObjClasses,backendDAEType=backendDAEType,symjacs=symjacs))),repl,func,replaceVariables)
equation
orderedVars = BackendDAEUtil.listVar(replaceVars(BackendDAEUtil.varList(orderedVars),repl,func,replaceVariables));
orderedEqs = BackendDAEUtil.listEquation(BackendVarTransform.replaceEquations(BackendDAEUtil.equationList(orderedEqs),repl));
syst = BackendDAE.EQSYSTEM(orderedVars,orderedEqs,m,mT,matching);
shared = BackendDAE.SHARED(knownVars,externalObjects,aliasVars,initialEqs,removedEqs,constraints,classAttrs,cache,env,funcs,eventInfo,extObjClasses,backendDAEType,symjacs);
then
BackendDAE.DAE(syst::systList,shared);
end match;
end replaceDAElow;
protected function replaceVars "help function to replaceDAElow, replaces variables.
If replaceName is true it replaced the variable name, fails if destination is not cref.
if replaceName is false it only replaces in binding expression.
"
input list<BackendDAE.Var> invarLst;
input BackendVarTransform.VariableReplacements repl;
input Option<PredicateFunction> func;
input Boolean replaceName;
partial function PredicateFunction
input DAE.Exp e;
output Boolean b;
end PredicateFunction;
output list<BackendDAE.Var> outVarLst;
algorithm
outVarLst := match(invarLst,repl,func,replaceName)
local
BackendDAE.Var v;
DAE.ComponentRef cr;
Option<DAE.Exp> bindExp;
list<BackendDAE.Var> varLst;
case({},repl,func,replaceName) then {};
case(v::varLst,repl,func,replaceName as true) equation
cr = BackendVariable.varCref(v);
bindExp = varBindingOpt(v);
bindExp = replaceExpOpt(bindExp,repl,func);
bindExp = applyOptionSimplify(bindExp);
(DAE.CREF(cr,_),_) = BackendVarTransform.replaceExp(DAE.CREF(cr, DAE.T_REAL_DEFAULT),repl,func);
v = setVarCref(v,cr);
v = setVarBindingOpt(v,bindExp);
varLst = replaceVars(varLst,repl,func,replaceName);
then v::varLst;
case(v::varLst,repl,func,replaceName as false) equation
bindExp = varBindingOpt(v);
bindExp = replaceExpOpt(bindExp,repl,func);
bindExp = applyOptionSimplify(bindExp);
v = setVarBindingOpt(v,bindExp);
varLst = replaceVars(varLst,repl,func,replaceName);
then v::varLst;
end match;
end replaceVars;
public function setVarBindingOpt "
author: PA
sets the optional binding of a variable.
"
input BackendDAE.Var inVar;
input Option<DAE.Exp> bindExp;
output BackendDAE.Var outVar;
algorithm
outVar:=
matchcontinue (inVar,bindExp)
local
DAE.ComponentRef name;
BackendDAE.VarKind kind;
DAE.VarDirection dir;
DAE.VarParallelism prl;
BackendDAE.Type tp;
Option<DAE.Exp> bind ;
Option<Values.Value> bindval;
DAE.InstDims ad;
Integer indx;
DAE.ElementSource source "origin of variable" ;
Option<DAE.VariableAttributes> attr;
Option<SCode.Comment> cmt;
DAE.ConnectorType ct;
case (BackendDAE.VAR(name,kind,dir,prl,tp,bind,bindval,ad,indx,source,attr,cmt,ct),bindExp) then
BackendDAE.VAR(name,kind,dir,prl,tp,bindExp,bindval,ad,indx,source,attr,cmt,ct);
end matchcontinue;
end setVarBindingOpt;
public function setVarCref "
author: PA
sets the ComponentRef of a variable.