/
DAEUtil.mo
6611 lines (6003 loc) · 209 KB
/
DAEUtil.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 DAEUtil
" file: DAEUtil.mo
package: DAE
description: DAE management and output
This module exports some helper functions to the DAE AST."
public import Absyn;
public import AbsynUtil;
public import ClassInf;
public import DAE;
public import FCore;
public import SCode;
public import Values;
public import ValuesUtil;
public import HashTable;
public import HashTable2;
protected
import Algorithm;
import BaseHashTable;
import Ceval;
import DAE.AvlTreePathFunction;
import ComponentReference;
import Config;
import ConnectUtil;
import DAEDump;
import Debug;
import DoubleEnded;
import ElementSource;
import Error;
import Expression;
import ExpressionDump;
import ExpressionSimplify;
import Flags;
import List;
import SCodeUtil;
import System;
import Types;
import Util;
import StateMachineFlatten;
import VarTransform;
public function constStr "return the DAE.Const as a string. (VAR|PARAM|CONST)
Used for debugging."
input DAE.Const const;
output String str;
algorithm
str := match(const)
case(DAE.C_VAR()) then "VAR";
case(DAE.C_PARAM()) then "PARAM";
case(DAE.C_CONST()) then "CONST";
end match;
end constStr;
public function constStrFriendly "return the DAE.Const as a friendly string. Used for debugging."
input DAE.Const const;
output String str;
algorithm
str := match(const)
case(DAE.C_VAR()) then "";
case(DAE.C_PARAM()) then "parameter ";
case(DAE.C_CONST()) then "constant ";
end match;
end constStrFriendly;
public function const2VarKind
input DAE.Const const;
output DAE.VarKind kind;
algorithm
kind := match(const)
case(DAE.C_VAR()) then DAE.VARIABLE();
case(DAE.C_PARAM()) then DAE.PARAM();
case(DAE.C_CONST()) then DAE.CONST();
end match;
end const2VarKind;
public function dumpVarParallelismStr "Dump VarParallelism to a string"
input DAE.VarParallelism inVarParallelism;
output String outString;
algorithm
outString := match (inVarParallelism)
case DAE.NON_PARALLEL() then "";
case DAE.PARGLOBAL() then "parglobal ";
case DAE.PARLOCAL() then "parlocal ";
end match;
end dumpVarParallelismStr;
public function topLevelInput "author: PA
if variable is input declared at the top level of the model,
or if it is an input in a connector instance at top level return true."
input DAE.ComponentRef inComponentRef;
input DAE.VarDirection inVarDirection;
input DAE.ConnectorType inConnectorType;
output Boolean isTopLevel;
algorithm
isTopLevel := match (inVarDirection, inComponentRef)
case (DAE.INPUT(), DAE.CREF_IDENT()) then true;
case (DAE.INPUT(), _)
guard(ConnectUtil.faceEqual(ConnectUtil.componentFaceType(inComponentRef), Connect.OUTSIDE()))
then topLevelConnectorType(inConnectorType);
else false;
end match;
end topLevelInput;
public function topLevelOutput "author: PA
if variable is output declared at the top level of the model,
or if it is an output in a connector instance at top level return true."
input DAE.ComponentRef inComponentRef;
input DAE.VarDirection inVarDirection;
input DAE.ConnectorType inConnectorType;
output Boolean isTopLevel;
algorithm
isTopLevel := match (inVarDirection, inComponentRef)
case (DAE.OUTPUT(), DAE.CREF_IDENT()) then true;
case (DAE.OUTPUT(), _)
guard(ConnectUtil.faceEqual(ConnectUtil.componentFaceType(inComponentRef), Connect.OUTSIDE()))
then topLevelConnectorType(inConnectorType);
else false;
end match;
end topLevelOutput;
protected function topLevelConnectorType
input DAE.ConnectorType inConnectorType;
output Boolean isTopLevel;
algorithm
isTopLevel := match (inConnectorType)
case DAE.FLOW() then true;
case DAE.POTENTIAL() then true;
else false;
end match;
end topLevelConnectorType;
public function expTypeSimple "returns true if type is simple type"
input DAE.Type tp;
output Boolean isSimple;
algorithm
isSimple := match(tp)
case(DAE.T_REAL()) then true;
case(DAE.T_INTEGER()) then true;
case(DAE.T_STRING()) then true;
case(DAE.T_BOOL()) then true;
// BTH
case(DAE.T_CLOCK()) then true;
case(DAE.T_ENUMERATION()) then true;
else false;
end match;
end expTypeSimple;
public function expTypeElementType "returns the element type of an array"
input DAE.Type tp;
output DAE.Type eltTp;
algorithm
eltTp := match(tp)
local
DAE.Type ty;
case (DAE.T_ARRAY(ty=ty)) then expTypeElementType(ty);
else tp;
end match;
end expTypeElementType;
public function expTypeComplex "returns true if type is complex type"
input DAE.Type tp;
output Boolean isComplex;
algorithm
isComplex := match(tp)
case(DAE.T_COMPLEX()) then true;
else false;
end match;
end expTypeComplex;
public function expTypeArray "returns true if type is array type
Alternative names: isArrayType, isExpTypeArray"
input DAE.Type tp;
output Boolean isArray;
algorithm
isArray := match(tp)
case(DAE.T_ARRAY()) then true;
else false;
end match;
end expTypeArray;
public function expTypeTuple
"returns true if type is tuple type."
input DAE.Type tp;
output Boolean isTuple;
algorithm
isTuple := match(tp)
case(DAE.T_TUPLE()) then true;
else false;
end match;
end expTypeTuple;
public function expTypeArrayDimensions "returns the array dimensions of an ExpType"
input DAE.Type tp;
output list<Integer> dims;
algorithm
dims := match(tp)
local DAE.Dimensions array_dims;
case(DAE.T_ARRAY(dims=array_dims)) equation
dims = List.map(array_dims, Expression.dimensionSize);
then dims;
end match;
end expTypeArrayDimensions;
public function dimExp
"Converts a dimension to an expression, covering constants and paramters."
input DAE.Dimension dim;
output DAE.Exp exp;
algorithm
exp := match dim
local
Integer iconst;
case DAE.DIM_INTEGER(iconst) then
DAE.ICONST(iconst);
case DAE.DIM_EXP(exp) then
exp;
else algorithm
Error.addMessage(Error.DIMENSION_NOT_KNOWN, {anyString(dim)});
then fail();
end match;
end dimExp;
public function derivativeOrder "
Function to sort derivatives.
Used for Util.sort"
input tuple<Integer,DAE.derivativeCond> e1,e2; //greaterThanFunc
output Boolean b;
protected
Integer i1,i2;
algorithm
b := match(e1,e2)
case((i1,_),(i2,_))
then Util.isIntGreater(i1,i2);
end match;
end derivativeOrder;
public function getDerivativePaths " collects all paths representing derivative functions for a list of FunctionDefinition's"
input list<DAE.FunctionDefinition> inFuncDefs;
output list<Absyn.Path> paths;
algorithm
paths := matchcontinue(inFuncDefs)
local
list<Absyn.Path> pLst1,pLst2;
Absyn.Path p1,p2;
list<DAE.FunctionDefinition> funcDefs;
case({}) then {};
case(DAE.FUNCTION_DER_MAPPER(derivativeFunction=p1,defaultDerivative=SOME(p2),lowerOrderDerivatives=pLst1)::funcDefs)
equation
pLst2 = getDerivativePaths(funcDefs);
paths = List.union(p1::p2::pLst1,pLst2);
then
paths;
case(DAE.FUNCTION_DER_MAPPER(derivativeFunction=p1,defaultDerivative=NONE(),lowerOrderDerivatives=pLst1)::funcDefs)
equation
pLst2 = getDerivativePaths(funcDefs);
paths = List.union(p1::pLst1,pLst2);
then
paths;
case(_::funcDefs) then getDerivativePaths(funcDefs);
end matchcontinue;
end getDerivativePaths;
public function addEquationBoundString "
Set the optional equationBound value"
input DAE.Exp bindExp;
input Option<DAE.VariableAttributes> attr;
output Option<DAE.VariableAttributes> oattr;
algorithm
oattr := match(bindExp,attr)
local
Option<DAE.Exp> e1,e2,e3,e4,e5,e6,so,min,max;
Option<DAE.StateSelect> sSelectOption,sSelectOption2;
Option<DAE.Uncertainty> unc;
Option<DAE.Distribution> distOption;
Option<Boolean> ip,fn;
String s;
case (_,SOME(DAE.VAR_ATTR_REAL(e1,e2,e3,min,max,e4,e5,e6,sSelectOption,unc,distOption,_,ip,fn,so)))
then (SOME(DAE.VAR_ATTR_REAL(e1,e2,e3,min,max,e4,e5,e6,sSelectOption,unc,distOption,SOME(bindExp),ip,fn,so)));
case (_,SOME(DAE.VAR_ATTR_INT(e1,min,max,e2,e3,unc,distOption,_,ip,fn,so)))
then SOME(DAE.VAR_ATTR_INT(e1,min,max,e2,e3,unc,distOption,SOME(bindExp),ip,fn,so));
case (_,SOME(DAE.VAR_ATTR_BOOL(e1,e2,e3,_,ip,fn,so)))
then SOME(DAE.VAR_ATTR_BOOL(e1,e2,e3,SOME(bindExp),ip,fn,so));
case (_,SOME(DAE.VAR_ATTR_STRING(e1,e2,e3,_,ip,fn,so)))
then SOME(DAE.VAR_ATTR_STRING(e1,e2,e3,SOME(bindExp),ip,fn,so));
case (_,SOME(DAE.VAR_ATTR_ENUMERATION(e1,min,max,e2,e3,_,ip,fn,so)))
then SOME(DAE.VAR_ATTR_ENUMERATION(e1,min,max,e2,e3,SOME(bindExp),ip,fn,so));
else equation print("-failure in DAEUtil.addEquationBoundString\n"); then fail();
end match;
end addEquationBoundString;
public function getClassList "get list of classes from Var"
input DAE.Element v;
output list<Absyn.Path> lst;
algorithm
lst := match(v)
case DAE.VAR(source = DAE.SOURCE(typeLst=lst)) then lst;
else {};
end match;
end getClassList;
public function getBoundStartEquation "
Returned bound equation"
input DAE.VariableAttributes attr;
output DAE.Exp oe;
algorithm
oe := match(attr)
local DAE.Exp beq;
case (DAE.VAR_ATTR_REAL(equationBound = SOME(beq))) then beq;
case (DAE.VAR_ATTR_INT(equationBound = SOME(beq))) then beq;
case (DAE.VAR_ATTR_BOOL(equationBound = SOME(beq))) then beq;
case (DAE.VAR_ATTR_ENUMERATION(equationBound = SOME(beq))) then beq;
end match;
end getBoundStartEquation;
public function splitDAEIntoVarsAndEquations
"Splits the DAE into one with vars and no equations and algorithms
and another one which has all the equations and algorithms but no variables.
Note: the functions are copied to both dae's.
"
input DAE.DAElist inDae;
output DAE.DAElist allVars;
output DAE.DAElist allEqs;
protected
list<DAE.Element> rest;
DoubleEnded.MutableList<DAE.Element> vars, eqs;
algorithm
DAE.DAE(rest) := inDae;
vars := DoubleEnded.fromList({});
eqs := DoubleEnded.fromList({});
for elt in rest loop
_ := match elt
local
DAE.Element v,e;
list<DAE.Element> elts,elts2,elts22,elts1,elts11,elts3,elts33;
String id;
DAE.ElementSource source "the origin of the element";
Option<SCode.Comment> cmt;
case DAE.VAR()
algorithm
DoubleEnded.push_back(vars, elt);
then ();
// adrpo: TODO! FIXME! a DAE.COMP SHOULD NOT EVER BE HERE!
case DAE.COMP(id,elts1,source,cmt)
algorithm
(DAE.DAE(elts11),DAE.DAE(elts3)) := splitDAEIntoVarsAndEquations(DAE.DAE(elts1));
DoubleEnded.push_back(vars, DAE.COMP(id,elts11,source,cmt));
DoubleEnded.push_list_back(eqs, elts3);
then ();
case DAE.EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.EQUEQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIALEQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.ARRAY_EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIAL_ARRAY_EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.COMPLEX_EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIAL_COMPLEX_EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIALDEFINE()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.DEFINE()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.WHEN_EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.FOR_EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.IF_EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIAL_IF_EQUATION()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.ALGORITHM()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIALALGORITHM()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
// adrpo: TODO! FIXME! why are external object constructor calls added to the non-equations DAE??
// PA: are these external object constructor CALLS? Do not think so. But they should anyway be in funcs..
case DAE.EXTOBJECTCLASS()
algorithm
DoubleEnded.push_back(vars, elt);
then ();
case DAE.ASSERT()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIAL_ASSERT()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.TERMINATE()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIAL_TERMINATE()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.REINIT()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
// handle also NORETCALL! Connections.root(...)
case DAE.NORETCALL()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
case DAE.INITIAL_NORETCALL()
algorithm
DoubleEnded.push_back(eqs, elt);
then ();
else
algorithm
Error.addInternalError(getInstanceName() + " failed for " + DAEDump.dumpDAEElementsStr(DAE.DAE({elt})), sourceInfo());
then fail();
end match;
end for;
allVars := DAE.DAE(DoubleEnded.toListAndClear(vars));
allEqs := DAE.DAE(DoubleEnded.toListAndClear(eqs));
end splitDAEIntoVarsAndEquations;
public function removeVariables "Remove the variables in the list from the DAE"
input DAE.DAElist dae;
input list<DAE.ComponentRef> vars;
output DAE.DAElist outDae;
algorithm
// adrpo: TODO! FIXME! rather expensive function!
// implement this by walking dae once and check element with each var in the list
// instead of walking the dae once for each var.
// outDae := List.fold(vars,removeVariable,dae);
outDae := match(dae, vars)
local
list<DAE.Element> elements;
case (_, {}) then dae;
case (DAE.DAE(elements), _)
equation
elements = removeVariablesFromElements(elements, vars);
then
DAE.DAE(elements);
end match;
end removeVariables;
protected function removeVariablesFromElements
"@author: adrpo
remove the variables that match for the element list"
input list<DAE.Element> inElements;
input list<DAE.ComponentRef> variableNames;
output list<DAE.Element> outElements = {};
algorithm
if listEmpty(variableNames) then
outElements := inElements;
return;
end if;
for el in inElements loop
_ := match el
local
DAE.ComponentRef cr;
list<DAE.Element> elist;
DAE.Element v;
String id;
DAE.ElementSource source "the origin of the element";
Option<SCode.Comment> cmt;
Boolean isEmpty;
case (v as DAE.VAR(componentRef = cr))
equation
// variable is in the list! jump over it
if listEmpty(List.select1(variableNames, ComponentReference.crefEqual, cr)) then
outElements = v::outElements;
end if;
then ();
// handle components
case DAE.COMP(id,elist,source,cmt)
equation
elist = removeVariablesFromElements(elist, variableNames);
outElements = DAE.COMP(id,elist,source,cmt)::outElements;
then ();
// anything else, just keep it
else
equation
outElements = el::outElements;
then ();
end match;
end for;
outElements := MetaModelica.Dangerous.listReverseInPlace(outElements);
end removeVariablesFromElements;
protected function removeVariable "Remove the variable from the DAE, UNUSED"
input DAE.ComponentRef var;
input DAE.DAElist dae;
output DAE.DAElist outDae;
algorithm
outDae := matchcontinue(var,dae)
local
DAE.ComponentRef cr;
list<DAE.Element> elist,elist2;
DAE.Element e,v; String id;
DAE.ElementSource source "the origin of the element";
Option<SCode.Comment> cmt;
case(_,DAE.DAE({})) then DAE.DAE({});
case(_,DAE.DAE((DAE.VAR(componentRef = cr))::elist))
equation
true = ComponentReference.crefEqualNoStringCompare(var,cr);
then DAE.DAE(elist);
case(_,DAE.DAE(DAE.COMP(id,elist,source,cmt)::elist2))
equation
DAE.DAE(elist) = removeVariable(var,DAE.DAE(elist));
DAE.DAE(elist2) = removeVariable(var,DAE.DAE(elist2));
then DAE.DAE(DAE.COMP(id,elist,source,cmt)::elist2);
case(_,DAE.DAE(e::elist))
equation
DAE.DAE(elist) = removeVariable(var,DAE.DAE(elist));
then DAE.DAE(e::elist);
end matchcontinue;
end removeVariable;
public function removeInnerAttrs "Remove the inner attribute of all vars in list"
input DAE.DAElist dae;
input list<DAE.ComponentRef> vars;
output DAE.DAElist outDae;
algorithm
outDae := List.fold(vars,removeInnerAttr,dae);
end removeInnerAttrs;
public function removeInnerAttr "Remove the inner attribute from variable in the DAE"
input DAE.ComponentRef var;
input DAE.DAElist dae;
output DAE.DAElist outDae;
algorithm
outDae := match(var,dae)
local
DAE.ComponentRef cr,oldVar,newVar;
list<DAE.Element> elist,elist2;
DAE.Element e,v,u,o; String id;
DAE.VarKind kind; DAE.VarParallelism prl;
DAE.VarDirection dir; DAE.Type tp;
Option<DAE.Exp> bind; DAE.InstDims dim;
DAE.ConnectorType ct; list<Absyn.Path> cls;
Option<DAE.VariableAttributes> attr;
Option<SCode.Comment> cmt; Absyn.InnerOuter io,io2;
DAE.VarVisibility prot;
DAE.ElementSource source "the origin of the element";
case (_,DAE.DAE({})) then DAE.DAE({});
/* When having an inner outer, we declare two variables on the same line.
Since we can not handle this with current instantiation procedure, we create temporary variables in the dae.
These are named uniqly and renamed later in "instClass"
*/
case(_,DAE.DAE(DAE.VAR(oldVar,kind,dir,prl,prot,tp,bind,dim,ct,source,attr,cmt,(Absyn.INNER_OUTER()))::elist))
guard
compareUniquedVarWithNonUnique(var,oldVar)
equation
newVar = nameInnerouterUniqueCref(oldVar);
o = DAE.VAR(oldVar,kind,dir,prl,prot,tp,NONE(),dim,ct,source,attr,cmt,Absyn.OUTER()) "intact";
u = DAE.VAR(newVar,kind,dir,prl,prot,tp,bind,dim,ct,source,attr,cmt,Absyn.NOT_INNER_OUTER()) " unique'ified";
elist= u::o::elist;
then
DAE.DAE(elist);
case(_,DAE.DAE(DAE.VAR(cr,kind,dir,prl,prot,tp,bind,dim,ct,source,attr,cmt,io)::elist))
guard
ComponentReference.crefEqualNoStringCompare(var,cr)
equation
io2 = removeInnerAttribute(io);
then
DAE.DAE(DAE.VAR(cr,kind,dir,prl,prot,tp,bind,dim,ct,source,attr,cmt,io2)::elist);
case(_,DAE.DAE(DAE.COMP(id,elist,source,cmt)::elist2))
equation
DAE.DAE(elist) = removeInnerAttr(var,DAE.DAE(elist));
DAE.DAE(elist2) = removeInnerAttr(var,DAE.DAE(elist2));
then DAE.DAE(DAE.COMP(id,elist,source,cmt)::elist2);
case(_,DAE.DAE(e::elist))
equation
DAE.DAE(elist)= removeInnerAttr(var,DAE.DAE(elist));
then DAE.DAE(e::elist);
end match;
end removeInnerAttr;
protected function compareUniquedVarWithNonUnique "
Author: BZ, workaround to get innerouter elements to work.
This function strips the 'unique identifer' from the cref and compares.
"
input DAE.ComponentRef cr1;
input DAE.ComponentRef cr2;
output Boolean equal;
protected
String s1,s2,s3;
algorithm
s1 := ComponentReference.printComponentRefStr(cr1);
s2 := ComponentReference.printComponentRefStr(cr2);
s1 := System.stringReplace(s1, DAE.UNIQUEIO, "");
s2 := System.stringReplace(s2, DAE.UNIQUEIO, "");
equal := stringEq(s1,s2);
end compareUniquedVarWithNonUnique;
public function nameInnerouterUniqueCref "
Author: BZ, 2008-11
Renames a var to unique name"
input DAE.ComponentRef inCr;
output DAE.ComponentRef outCr;
algorithm outCr := match(inCr)
local
DAE.ComponentRef newChild,child;
String id;
DAE.Type idt;
list<DAE.Subscript> subs;
case(DAE.CREF_IDENT(id,idt,subs))
equation
id = DAE.UNIQUEIO + id;
then
ComponentReference.makeCrefIdent(id,idt,subs);
case(DAE.CREF_QUAL(id,idt,subs,child))
equation
newChild = nameInnerouterUniqueCref(child);
then
ComponentReference.makeCrefQual(id,idt,subs,newChild);
end match;
end nameInnerouterUniqueCref;
public function unNameInnerouterUniqueCref "
Function for stripping a cref of its uniqified part.
Remove 'removalString' from the cref if found
"
input DAE.ComponentRef cr;
input String removalString;
output DAE.ComponentRef ocr;
algorithm ocr := matchcontinue(cr,removalString)
local
String str,str2;
DAE.Type ty;
DAE.ComponentRef child,child_2;
list<DAE.Subscript> subs;
case(DAE.CREF_IDENT(str,ty,subs),_)
equation
str2 = System.stringReplace(str, removalString, "");
then
ComponentReference.makeCrefIdent(str2,ty,subs);
case(DAE.CREF_QUAL(str,ty,subs,child),_)
equation
child_2 = unNameInnerouterUniqueCref(child,removalString);
str2 = System.stringReplace(str, removalString, "");
then
ComponentReference.makeCrefQual(str2,ty,subs,child_2);
case(DAE.WILD(),_) then DAE.WILD();
case(child,_)
equation
print(" failure unNameInnerouterUniqueCref: ");
print(ComponentReference.printComponentRefStr(child) + "\n");
then fail();
end matchcontinue;
end unNameInnerouterUniqueCref;
protected function removeInnerAttribute "Help function to removeInnerAttr"
input Absyn.InnerOuter io;
output Absyn.InnerOuter ioOut;
algorithm
ioOut := match(io)
case(Absyn.INNER()) then Absyn.NOT_INNER_OUTER();
case(Absyn.INNER_OUTER()) then Absyn.OUTER();
else io;
end match;
end removeInnerAttribute;
public function varCref " returns the component reference of a variable"
input DAE.Element elt;
output DAE.ComponentRef cr;
algorithm
DAE.VAR(componentRef = cr) := elt;
end varCref;
public function getVariableAttributes " gets the attributes of a DAE.Element that is VAR"
input DAE.Element elt;
output Option<DAE.VariableAttributes> variableAttributesOption;
algorithm
DAE.VAR(variableAttributesOption=variableAttributesOption) := elt;
end getVariableAttributes;
public function getUnitAttr "
Return the unit attribute"
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output DAE.Exp start;
algorithm
start := match (inVariableAttributesOption)
local
DAE.Exp u;
case (SOME(DAE.VAR_ATTR_REAL(unit=SOME(u)))) then u;
else DAE.SCONST("");
end match;
end getUnitAttr;
public function getStartAttrEmpty "
Return the start attribute."
input Option<DAE.VariableAttributes> inVariableAttributesOption;
input DAE.Exp optExp;
output DAE.Exp start;
algorithm
start := match (inVariableAttributesOption,optExp)
local
DAE.Exp r;
case (SOME(DAE.VAR_ATTR_REAL(start = SOME(r))),_) then r;
case (SOME(DAE.VAR_ATTR_INT(start = SOME(r))),_) then r;
case (SOME(DAE.VAR_ATTR_BOOL(start = SOME(r))),_) then r;
case (SOME(DAE.VAR_ATTR_STRING(start = SOME(r))),_) then r;
case (SOME(DAE.VAR_ATTR_ENUMERATION(start = SOME(r))),_) then r;
else optExp;
end match;
end getStartAttrEmpty;
public function getMinMax "
Author: BZ, returns a list of optional exp, {opt<Min> opt<Max} "
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output list<Option<DAE.Exp>> oExps;
algorithm
oExps := match(inVariableAttributesOption)
local
Option<DAE.Exp> e1,e2;
case(SOME(DAE.VAR_ATTR_ENUMERATION(min = e1, max = e2))) then {e1, e2};
case(SOME(DAE.VAR_ATTR_INT(min = e1, max = e2))) then {e1, e2};
case(SOME(DAE.VAR_ATTR_REAL(min = e1, max = e2))) then {e1, e2};
else {};
end match;
end getMinMax;
public function getMinMaxValues
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output Option<DAE.Exp> outMinValue;
output Option<DAE.Exp> outMaxValue;
algorithm
(outMinValue, outMaxValue) := match(inVariableAttributesOption)
local
Option<DAE.Exp> minValue, maxValue;
case(SOME(DAE.VAR_ATTR_ENUMERATION(min = minValue, max = maxValue)))
then (minValue, maxValue);
case(SOME(DAE.VAR_ATTR_INT(min = minValue, max = maxValue)))
then (minValue, maxValue);
case(SOME(DAE.VAR_ATTR_REAL(min = minValue, max = maxValue)))
then (minValue, maxValue);
else (NONE(), NONE());
end match;
end getMinMaxValues;
public function setMinMax
"Sets the min and max attributes. If inAttr is NONE(), assumes Real attributes."
input Option<DAE.VariableAttributes> inAttr;
input Option<DAE.Exp> inMin;
input Option<DAE.Exp> inMax;
output Option<DAE.VariableAttributes> outAttr;
algorithm
outAttr := match(inAttr, inMin, inMax)
local
Option<DAE.Exp> q,u,du,f,n,i;
Option<DAE.StateSelect> ss;
Option<DAE.Uncertainty> unc;
Option<DAE.Distribution> distOpt;
Option<DAE.Exp> eb;
Option<Boolean> ip,fn;
Option<DAE.Exp> so;
Option<DAE.Exp> min,max;
case (SOME(DAE.VAR_ATTR_REAL(q,u,du,min,max,i,f,n,ss,unc,distOpt,eb,ip,fn,so)), _, _)
then if referenceEq(min,inMin) and referenceEq(max,inMax) then inAttr else SOME(DAE.VAR_ATTR_REAL(q,u,du,inMin,inMax,i,f,n,ss,unc,distOpt,eb,ip,fn,so));
case (SOME(DAE.VAR_ATTR_INT(q,min,max,i,f,unc,distOpt,eb,ip,fn,so)), _, _)
then if referenceEq(min,inMin) and referenceEq(max,inMax) then inAttr else SOME(DAE.VAR_ATTR_INT(q,inMin,inMax,i,f,unc,distOpt,eb,ip,fn,so));
case (SOME(DAE.VAR_ATTR_ENUMERATION(q,min,max,u,du,eb,ip,fn,so)), _, _)
then if referenceEq(min,inMin) and referenceEq(max,inMax) then inAttr else SOME(DAE.VAR_ATTR_ENUMERATION(q,inMin,inMax,u,du,eb,ip,fn,so));
case (NONE(), _, _)
then SOME(DAE.VAR_ATTR_REAL(NONE(),NONE(),NONE(),inMin,inMax,NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE()));
end match;
end setMinMax;
public function getStartAttr "
Return the start attribute."
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output DAE.Exp start;
algorithm
start := match(inVariableAttributesOption)
local
DAE.Exp r;
case (SOME(DAE.VAR_ATTR_REAL(start = SOME(r)))) then r;
case (SOME(DAE.VAR_ATTR_INT(start = SOME(r)))) then r;
case (SOME(DAE.VAR_ATTR_BOOL(start = SOME(r)))) then r;
case (SOME(DAE.VAR_ATTR_STRING(start = SOME(r)))) then r;
case (SOME(DAE.VAR_ATTR_ENUMERATION(start = SOME(r)))) then r;
else DAE.RCONST(0.0);
end match;
end getStartAttr;
public function getStartOrigin "
Return the startOrigin attribute"
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output Option<DAE.Exp> startOrigin;
algorithm startOrigin:= match (inVariableAttributesOption)
local
Option<DAE.Exp> so;
case (SOME(DAE.VAR_ATTR_REAL(startOrigin = so))) then so;
case (SOME(DAE.VAR_ATTR_INT(startOrigin = so))) then so;
case (SOME(DAE.VAR_ATTR_BOOL(startOrigin = so))) then so;
case (SOME(DAE.VAR_ATTR_STRING(startOrigin = so))) then so;
case (SOME(DAE.VAR_ATTR_ENUMERATION(startOrigin = so))) then so;
case (NONE()) then NONE();
end match;
end getStartOrigin;
public function getStartAttrFail "
Return the start attribute. or fails"
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output DAE.Exp start;
algorithm start:= match (inVariableAttributesOption)
local
DAE.Exp r;
case (SOME(DAE.VAR_ATTR_REAL(start = SOME(r)))) then r;
case (SOME(DAE.VAR_ATTR_INT(start = SOME(r)))) then r;
case (SOME(DAE.VAR_ATTR_BOOL(start = SOME(r)))) then r;
case (SOME(DAE.VAR_ATTR_STRING(start = SOME(r)))) then r;
case (SOME(DAE.VAR_ATTR_ENUMERATION(start = SOME(r)))) then r;
end match;
end getStartAttrFail;
public function getNominalAttrFail "
Return the nominal attribute. or fails"
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output DAE.Exp nominal;
algorithm nominal := match(inVariableAttributesOption)
local
DAE.Exp r;
case (SOME(DAE.VAR_ATTR_REAL(nominal = SOME(r)))) then r;
end match;
end getNominalAttrFail;
public function getMinAttrFail "
Return the min attribute. or fails"
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output DAE.Exp outMin;
algorithm
SOME(DAE.VAR_ATTR_REAL(min = SOME(outMin))) := inVariableAttributesOption;
end getMinAttrFail;
public function getMaxAttrFail "
Return the max attribute. or fails"
input Option<DAE.VariableAttributes> inVariableAttributesOption;
output DAE.Exp outMax;
algorithm
SOME(DAE.VAR_ATTR_REAL(max = SOME(outMax))) := inVariableAttributesOption;
end getMaxAttrFail;
public function setVariableAttributes "sets the attributes of a DAE.Element that is VAR"
input DAE.Element var;
input Option<DAE.VariableAttributes> varOpt;
output DAE.Element v = var;
algorithm
v := match v
case DAE.VAR()
algorithm
v.variableAttributesOption := varOpt;
then v;
end match;
end setVariableAttributes;
public function setStateSelect "
sets the stateselect attribute. If NONE(), assumes Real attributes."
input Option<DAE.VariableAttributes> attr;
input DAE.StateSelect s;
output Option<DAE.VariableAttributes> outAttr;
algorithm
outAttr := match attr
local
DAE.VariableAttributes va;
case SOME(va as DAE.VAR_ATTR_REAL())
algorithm
va.stateSelectOption := SOME(s);
then SOME(va);
case NONE()
then SOME(DAE.VAR_ATTR_REAL(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),SOME(s),NONE(),NONE(),NONE(),NONE(),NONE(),NONE()));
end match;
end setStateSelect;
public function setStartAttr "