/
DAE.mo
2039 lines (1709 loc) · 67.5 KB
/
DAE.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 DAE
" file: DAE.mo
package: DAE
description: DAE management and output
This module defines data structures for DAE equations and declarations of
variables and functions. The DAE data structure is the result of flattening,
containing only flat modelica, i.e. equations, algorithms, variables and
functions."
// public imports
import Absyn;
import AbsynUtil;
import BaseAvlTree;
import ClassInf;
import SCode;
import Values;
public type Ident = String;
public type InstDims = list<Dimension>;
public type StartValue = Option<Exp>;
public constant String UNIQUEIO = "$unique$outer$";
public constant String derivativeNamePrefix = "$DER";
public constant String partialDerivativeNamePrefix = "$pDER";
public constant String preNamePrefix = "$PRE";
public constant String previousNamePrefix = "$CLKPRE";
public constant String startNamePrefix = "$START";
public constant String auxNamePrefix = "$AUX";
public uniontype VarKind
record VARIABLE "variable" end VARIABLE;
record DISCRETE "discrete" end DISCRETE;
record PARAM "parameter" end PARAM;
record CONST "constant" end CONST;
end VarKind;
public uniontype ConnectorType
"The type of a connector element."
record POTENTIAL end POTENTIAL;
record FLOW end FLOW;
record STREAM
Option<ComponentRef> associatedFlow;
end STREAM;
record NON_CONNECTOR end NON_CONNECTOR;
end ConnectorType;
public uniontype VarDirection
record INPUT "input" end INPUT;
record OUTPUT "output" end OUTPUT;
record BIDIR "neither input or output" end BIDIR;
end VarDirection;
public uniontype VarParallelism
record PARGLOBAL "Global variables for CUDA and OpenCL" end PARGLOBAL;
record PARLOCAL "Shared for CUDA and local for OpenCL" end PARLOCAL;
record NON_PARALLEL "Non parallel/Normal variables" end NON_PARALLEL;
end VarParallelism;
public uniontype VarVisibility
record PUBLIC "public variables" end PUBLIC;
record PROTECTED "protected variables" end PROTECTED;
end VarVisibility;
public uniontype VarInnerOuter
record INNER "an inner prefix" end INNER;
record OUTER "an outer prefix" end OUTER;
record INNER_OUTER "an inner outer prefix" end INNER_OUTER;
record NOT_INNER_OUTER "no inner outer prefix" end NOT_INNER_OUTER;
end VarInnerOuter;
uniontype ElementSource "gives information about the origin of the element"
record SOURCE
SourceInfo info "the line and column numbers of the equations and algorithms this element came from";
list<Absyn.Within> partOfLst "the model(s) this element came from";
ComponentPrefix instance "the instance(s) this element is part of";
list<tuple<ComponentRef, ComponentRef>> connectEquationOptLst "this element came from this connect(s)";
list<Absyn.Path> typeLst "the classes where the type(s) of the element is defined";
list<SymbolicOperation> operations "the symbolic operations used to end up with the final state of the element";
list<SCode.Comment> comment;
end SOURCE;
end ElementSource;
public constant ElementSource emptyElementSource = SOURCE(AbsynUtil.dummyInfo,{},NOCOMPPRE(),{},{},{},{});
public uniontype SymbolicOperation
record FLATTEN "From one equation/statement to an element"
SCode.Equation scode;
Option<Element> dae;
end FLATTEN;
record SIMPLIFY "Before and after expression is equivalent"
EquationExp before;
EquationExp after;
end SIMPLIFY;
record SUBSTITUTION "A chain of substitutions"
list<Exp> substitutions;
Exp source;
end SUBSTITUTION;
record OP_INLINE "Before and after inlining of function calls"
EquationExp before;
EquationExp after;
end OP_INLINE;
record OP_SCALARIZE "Convert array equation into scalar equations; x = {1,2}, [1] => x[1] = {1}"
EquationExp before;
Integer index;
EquationExp after;
end OP_SCALARIZE;
record OP_DIFFERENTIATE "Differentiate w.r.t. cr"
ComponentRef cr;
Exp before;
Exp after;
end OP_DIFFERENTIATE;
record SOLVE "Solve equation, exp1 = exp2 => cr = exp; note that assertions may have been generated for example in case of divisions"
ComponentRef cr;
Exp exp1;
Exp exp2;
Exp res;
list<Exp> assertConds;
end SOLVE;
record SOLVED "Equation is solved"
ComponentRef cr;
Exp exp;
end SOLVED;
record LINEAR_SOLVED "Solved linear system of equations"
list<ComponentRef> vars;
list<list<Real>> jac;
list<Real> rhs;
list<Real> result;
end LINEAR_SOLVED;
record NEW_DUMMY_DER "Introduced a dummy derivative (from index reduction)"
ComponentRef chosen;
list<ComponentRef> candidates;
end NEW_DUMMY_DER;
record OP_RESIDUAL "Converted the equation into residual form, to use nonlinear equation solvers 0=e (0=e1-e2)"
Exp e1;
Exp e2;
Exp e;
end OP_RESIDUAL;
end SymbolicOperation;
public uniontype EquationExp "An equation on residual or equality form has 1 or 2 expressions. For use with symbolic operation tracing."
record PARTIAL_EQUATION "An expression that is part of the whole equation"
Exp exp;
end PARTIAL_EQUATION;
record RESIDUAL_EXP "0 = exp"
Exp exp;
end RESIDUAL_EXP;
record EQUALITY_EXPS "lhs = rhs"
Exp lhs, rhs;
end EQUALITY_EXPS;
end EquationExp;
public uniontype Element
record VAR
ComponentRef componentRef " The variable name";
VarKind kind "varible kind: variable, constant, parameter, discrete etc." ;
VarDirection direction "input, output or bidir" ;
VarParallelism parallelism "parglobal, parlocal, or non_parallel";
VarVisibility protection "if protected or public";
Type ty "Full type information required";
Option<Exp> binding "Binding expression e.g. for parameters ; value of start attribute";
InstDims dims "dimensions";
ConnectorType connectorType "The connector type: flow, stream, no prefix, or not a connector element.";
ElementSource source "the origins of the component/equation/algorithm";
Option<VariableAttributes> variableAttributesOption;
Option<SCode.Comment> comment;
Absyn.InnerOuter innerOuter "inner/outer required to 'change' outer references";
end VAR;
record DEFINE "A solved equation"
ComponentRef componentRef;
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end DEFINE;
record INITIALDEFINE " A solved initial equation"
ComponentRef componentRef;
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIALDEFINE;
record EQUATION "Scalar equation"
Exp exp;
Exp scalar;
ElementSource source "the origin of the component/equation/algorithm" ;
end EQUATION;
record EQUEQUATION "effort variable equality"
ComponentRef cr1;
ComponentRef cr2;
ElementSource source "the origin of the component/equation/algorithm" ;
end EQUEQUATION;
record ARRAY_EQUATION " an array equation"
Dimensions dimension "dimension sizes" ;
Exp exp;
Exp array;
ElementSource source "the origin of the component/equation/algorithm" ;
end ARRAY_EQUATION;
record INITIAL_ARRAY_EQUATION "An initial array equation"
Dimensions dimension "dimension sizes";
Exp exp;
Exp array;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIAL_ARRAY_EQUATION;
record CONNECT_EQUATION "a connect equation"
Element lhsElement;
Connect.Face lhsFace;
Element rhsElement;
Connect.Face rhsFace;
ElementSource source "the origin of the component/equation/algorithm";
end CONNECT_EQUATION;
record COMPLEX_EQUATION "an equation of complex type, e.g. record = func(..)"
Exp lhs;
Exp rhs;
ElementSource source "the origin of the component/equation/algorithm" ;
end COMPLEX_EQUATION;
record INITIAL_COMPLEX_EQUATION "an initial equation of complex type, e.g. record = func(..)"
Exp lhs;
Exp rhs;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIAL_COMPLEX_EQUATION;
record WHEN_EQUATION " a when equation"
Exp condition "Condition" ;
list<Element> equations "Equations" ;
Option<Element> elsewhen_ "Elsewhen should be of type WHEN_EQUATION" ;
ElementSource source "the origin of the component/equation/algorithm" ;
end WHEN_EQUATION;
record INITIAL_FOR_EQUATION " an initial for-equation"
Type type_ "this is the type of the iterator";
Boolean iterIsArray "True if the iterator has an array type, otherwise false.";
Ident iter "the iterator variable";
Integer index "the index of the iterator variable, to make it unique; used by the new inst";
Exp range "range for the loop";
list<Element> equations "Equations" ;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIAL_FOR_EQUATION;
record FOR_EQUATION " a for-equation"
Type type_ "this is the type of the iterator";
Boolean iterIsArray "True if the iterator has an array type, otherwise false.";
Ident iter "the iterator variable";
Integer index "the index of the iterator variable, to make it unique; used by the new inst";
Exp range "range for the loop";
list<Element> equations "Equations" ;
ElementSource source "the origin of the component/equation/algorithm" ;
end FOR_EQUATION;
record IF_EQUATION " an if-equation"
list<Exp> condition1 "Condition" ;
list<list<Element>> equations2 "Equations of true branch" ;
list<Element> equations3 "Equations of false branch" ;
ElementSource source "the origin of the component/equation/algorithm" ;
end IF_EQUATION;
record INITIAL_IF_EQUATION "An initial if-equation"
list<Exp> condition1 "Condition" ;
list<list<Element>> equations2 "Equations of true branch" ;
list<Element> equations3 "Equations of false branch" ;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIAL_IF_EQUATION;
record INITIALEQUATION " An initial equaton"
Exp exp1;
Exp exp2;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIALEQUATION;
record ALGORITHM " An algorithm section"
Algorithm algorithm_;
ElementSource source "the origin of the component/equation/algorithm" ;
end ALGORITHM;
record INITIALALGORITHM " An initial algorithm section"
Algorithm algorithm_;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIALALGORITHM;
record COMP
Ident ident;
list<Element> dAElist "a component with subelements, normally only used at top level.";
ElementSource source "the origin of the component/equation/algorithm" ; // we might not this here.
Option<SCode.Comment> comment;
end COMP;
record EXTOBJECTCLASS "The 'class' of an external object"
Absyn.Path path "className of external object";
ElementSource source "the origin of the component/equation/algorithm" ;
end EXTOBJECTCLASS;
record ASSERT " The Modelica builtin assert"
Exp condition;
Exp message;
Exp level;
ElementSource source "the origin of the component/equation/algorithm" ;
end ASSERT;
record INITIAL_ASSERT " The Modelica builtin assert"
Exp condition;
Exp message;
Exp level;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIAL_ASSERT;
record TERMINATE " The Modelica builtin terminate(msg)"
Exp message;
ElementSource source "the origin of the component/equation/algorithm" ;
end TERMINATE;
record INITIAL_TERMINATE " The Modelica builtin terminate(msg)"
Exp message;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIAL_TERMINATE;
record REINIT " reinit operator for reinitialization of states"
ComponentRef componentRef;
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end REINIT;
record NORETCALL "call with no return value, i.e. no equation.
Typically sideeffect call of external function but also
Connections.* i.e. Connections.root(...) functions."
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end NORETCALL;
record INITIAL_NORETCALL "call with no return value, i.e. no equation.
Typically sideeffect call of external function but also
Connections.* i.e. Connections.root(...) functions."
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end INITIAL_NORETCALL;
record CONSTRAINT " constraint section"
Constraint constraints;
ElementSource source "the origin of the component/equation/algorithm" ;
end CONSTRAINT;
record CLASS_ATTRIBUTES
ClassAttributes classAttrs;
end CLASS_ATTRIBUTES;
record FLAT_SM "Flat state machine section"
Ident ident;
list<Element> dAElist "The states/modes transitions and variable
merging equations within the the flat state machine";
end FLAT_SM;
record SM_COMP "A state/mode component in a state machine"
ComponentRef componentRef;
list<Element> dAElist "a component with subelements";
end SM_COMP;
record COMMENT
SCode.Comment cmt "Functions store the inherited class annotations in the DAE";
end COMMENT;
end Element;
public constant Type T_ASSERTIONLEVEL = T_ENUMERATION(NONE(), Absyn.FULLYQUALIFIED(Absyn.IDENT("AssertionLevel")), {"warning","error"}, {}, {});
public constant Exp ASSERTIONLEVEL_WARNING = ENUM_LITERAL(Absyn.QUALIFIED("AssertionLevel",Absyn.IDENT("warning")),1);
public constant Exp ASSERTIONLEVEL_ERROR = ENUM_LITERAL(Absyn.QUALIFIED("AssertionLevel",Absyn.IDENT("error")),2);
public uniontype Function
record FUNCTION " A Modelica function"
Absyn.Path path;
list<FunctionDefinition> functions "contains the body and an optional function derivative mapping";
Type type_;
SCode.Visibility visibility;
Boolean partialPrefix "MetaModelica extension";
Boolean isImpure "Modelica 3.3 impure/pure, by default isImpure = false all the time only if prefix *impure* function is specified";
InlineType inlineType;
list<Integer> unusedInputs "The indices of any inputs not used in the function.";
ElementSource source "the origin of the component/equation/algorithm" ;
Option<SCode.Comment> comment;
end FUNCTION;
record RECORD_CONSTRUCTOR "A Modelica record constructor. The function can be generated from the Path and Type alone."
Absyn.Path path;
Type type_;
ElementSource source "the origin of the component/equation/algorithm" ;
end RECORD_CONSTRUCTOR;
end Function;
public uniontype InlineType
record NORM_INLINE "Normal inline, inline as soon as possible"
end NORM_INLINE;
record BUILTIN_EARLY_INLINE "Inline even if inlining is globally disabled by flags."
end BUILTIN_EARLY_INLINE;
record EARLY_INLINE "Inline even earlier than NORM_INLINE. This will display the inlined code in the flattened model and also works for functions calling other functions that should be inlined."
end EARLY_INLINE;
record DEFAULT_INLINE "no user option, tool can inline this functio if necessary"
end DEFAULT_INLINE;
record NO_INLINE "don't inline this function, set with Inline=false"
end NO_INLINE;
record AFTER_INDEX_RED_INLINE "Try to inline after index reduction"
end AFTER_INDEX_RED_INLINE;
end InlineType;
public uniontype FunctionDefinition
record FUNCTION_DEF "Normal function body"
list<Element> body;
end FUNCTION_DEF;
record FUNCTION_EXT "Normal external function declaration"
list<Element> body;
ExternalDecl externalDecl;
end FUNCTION_EXT;
record FUNCTION_DER_MAPPER "Contains derivatives for function"
Absyn.Path derivedFunction "Function that is derived";
Absyn.Path derivativeFunction "Path to derivative function";
Integer derivativeOrder "in case a function have multiple derivatives, include all";
list<tuple<Integer,derivativeCond>> conditionRefs;
Option<Absyn.Path> defaultDerivative "if conditions fails, use default derivative if exists";
list<Absyn.Path> lowerOrderDerivatives;
end FUNCTION_DER_MAPPER;
record FUNCTION_INVERSE "A function inverse declaration"
ComponentRef inputParam "The input parameter the inverse is for";
Exp inverseCall "The inverse function call";
end FUNCTION_INVERSE;
end FunctionDefinition;
public
uniontype derivativeCond "Different conditions on derivatives"
record ZERO_DERIVATIVE end ZERO_DERIVATIVE;
record NO_DERIVATIVE Exp binding; end NO_DERIVATIVE;
end derivativeCond;
public
uniontype VariableAttributes
record VAR_ATTR_REAL
Option<Exp> quantity "quantity";
Option<Exp> unit "unit";
Option<Exp> displayUnit "displayUnit";
Option<Exp> min;
Option<Exp> max;
Option<Exp> start "start value";
Option<Exp> fixed "fixed - true: default for parameter/constant, false - default for other variables";
Option<Exp> nominal "nominal";
Option<StateSelect> stateSelectOption;
Option<Uncertainty> uncertainOption;
Option<Distribution> distributionOption;
Option<Exp> equationBound;
Option<Boolean> isProtected;
Option<Boolean> finalPrefix;
Option<Exp> startOrigin "where did start=X came from? NONE()|SOME(DAE.SCONST binding|type|undefined)";
end VAR_ATTR_REAL;
record VAR_ATTR_INT
Option<Exp> quantity "quantity";
Option<Exp> min;
Option<Exp> max;
Option<Exp> start "start value";
Option<Exp> fixed "fixed - true: default for parameter/constant, false - default for other variables";
Option<Uncertainty> uncertainOption;
Option<Distribution> distributionOption;
Option<Exp> equationBound;
Option<Boolean> isProtected; // ,eb,ip
Option<Boolean> finalPrefix;
Option<Exp> startOrigin "where did start=X came from? NONE()|SOME(DAE.SCONST binding|type|undefined)";
end VAR_ATTR_INT;
record VAR_ATTR_BOOL
Option<Exp> quantity "quantity";
Option<Exp> start "start value";
Option<Exp> fixed "fixed - true: default for parameter/constant, false - default for other variables";
Option<Exp> equationBound;
Option<Boolean> isProtected;
Option<Boolean> finalPrefix;
Option<Exp> startOrigin "where did start=X came from? NONE()|SOME(DAE.SCONST binding|type|undefined)";
end VAR_ATTR_BOOL;
record VAR_ATTR_CLOCK
Option<Boolean> isProtected;
Option<Boolean> finalPrefix;
end VAR_ATTR_CLOCK;
record VAR_ATTR_STRING
Option<Exp> quantity "quantity";
Option<Exp> start "start value";
Option<Exp> fixed "new in Modelica 3.4; fixed - true: default for parameter/constant, false - default for other variables";
Option<Exp> equationBound;
Option<Boolean> isProtected;
Option<Boolean> finalPrefix;
Option<Exp> startOrigin "where did start=X came from? NONE()|SOME(DAE.SCONST binding|type|undefined)";
end VAR_ATTR_STRING;
record VAR_ATTR_ENUMERATION
Option<Exp> quantity "quantity";
Option<Exp> min;
Option<Exp> max;
Option<Exp> start "start";
Option<Exp> fixed "fixed - true: default for parameter/constant, false - default for other variables";
Option<Exp> equationBound;
Option<Boolean> isProtected;
Option<Boolean> finalPrefix;
Option<Exp> startOrigin "where did start=X came from? NONE()|SOME(DAE.SCONST binding|type|undefined)";
end VAR_ATTR_ENUMERATION;
end VariableAttributes;
public constant VariableAttributes emptyVarAttrReal = VAR_ATTR_REAL(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
public constant VariableAttributes emptyVarAttrBool = VAR_ATTR_BOOL(NONE(),NONE(),NONE(),NONE(),NONE(),NONE(),NONE());
public uniontype StateSelect
record NEVER end NEVER;
record AVOID end AVOID;
record DEFAULT end DEFAULT;
record PREFER end PREFER;
record ALWAYS end ALWAYS;
end StateSelect;
public uniontype Uncertainty
record GIVEN end GIVEN;
record SOUGHT end SOUGHT;
record REFINE end REFINE;
end Uncertainty;
public uniontype Distribution
record DISTRIBUTION "see Distribution record in Distribution"
Exp name;
Exp params;
Exp paramNames;
end DISTRIBUTION;
end Distribution;
public uniontype ExtArg
record EXTARG
ComponentRef componentRef;
Absyn.Direction direction;
Type type_;
end EXTARG;
record EXTARGEXP
Exp exp;
Type type_;
end EXTARGEXP;
record EXTARGSIZE
ComponentRef componentRef;
Type type_;
Exp exp;
end EXTARGSIZE;
record NOEXTARG end NOEXTARG;
end ExtArg;
public uniontype ExternalDecl
record EXTERNALDECL
String name;
list<ExtArg> args;
ExtArg returnArg;
String language;
Option<SCode.Annotation> ann;
end EXTERNALDECL;
end ExternalDecl;
public uniontype DAElist "A DAElist is a list of Elements. Variables, equations, functions,
algorithms, etc. are all found in this list.
"
record DAE
list<Element> elementLst;
end DAE;
end DAElist;
/* AVLTree for functions */
public type FunctionTree = AvlTreePathFunction.Tree;
package AvlTreePathFunction "AvlTree for Path to Function"
extends BaseAvlTree;
redeclare type Key = Absyn.Path;
redeclare type Value = Option<Function>;
redeclare function extends keyStr
algorithm
outString := AbsynUtil.pathString(inKey);
end keyStr;
redeclare function extends valueStr
algorithm
outString := match inValue
local
Absyn.Path path;
case SOME(FUNCTION(path=path)) then AbsynUtil.pathString(path);
case SOME(RECORD_CONSTRUCTOR(path=path)) then AbsynUtil.pathString(path);
case SOME(RECORD_CONSTRUCTOR(path=path)) then "<SOME_FUNCTION>";
else "<NO_FUNCTION>";
end match;
end valueStr;
redeclare function extends keyCompare
algorithm
outResult := AbsynUtil.pathCompareNoQual(inKey1,inKey2);
end keyCompare;
redeclare function addConflictDefault = addConflictReplace;
end AvlTreePathFunction;
/* -- Algorithm.mo -- */
public
uniontype Algorithm "The `Algorithm\' type corresponds to a whole algorithm section.
It is simple a list of algorithm statements."
record ALGORITHM_STMTS
list<Statement> statementLst;
end ALGORITHM_STMTS;
end Algorithm;
public
uniontype Constraint "Optimica extension: The `Constraints\' type corresponds to a whole Constraint section.
It is simple a list of expressions."
record CONSTRAINT_EXPS
list<Exp> constraintLst;
end CONSTRAINT_EXPS;
record CONSTRAINT_DT "Constraints needed for proper Dynamic Tearing"
Exp constraint;
Boolean localCon "local or global constraint; local constraints depend on variables that are computed within the algebraic loop itself";
end CONSTRAINT_DT;
end Constraint;
public
uniontype ClassAttributes "currently for Optimica extension: these are the objectives of optimization class"
record OPTIMIZATION_ATTRS
Option<Exp> objetiveE;
Option<Exp> objectiveIntegrandE;
Option<Exp> startTimeE;
Option<Exp> finalTimeE;
end OPTIMIZATION_ATTRS;
end ClassAttributes;
/* TODO: create a backend and a simcode uniontype */
public
uniontype Statement "There are four kinds of statements:
1. assignments ('a := b;')
2. if statements ('if A then B; elseif C; else D;')
3. for loops ('for i in 1:10 loop ...; end for;')
4. when statements ('when E do S; end when;')"
record STMT_ASSIGN
Type type_;
Exp exp1;
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_ASSIGN;
record STMT_TUPLE_ASSIGN
Type type_;
list<Exp> expExpLst;
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_TUPLE_ASSIGN;
record STMT_ASSIGN_ARR
Type type_;
Exp lhs;
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_ASSIGN_ARR;
record STMT_IF
Exp exp;
list<Statement> statementLst;
Else else_;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_IF;
record STMT_FOR
Type type_ "this is the type of the iterator";
Boolean iterIsArray "True if the iterator has an array type, otherwise false.";
Ident iter "the iterator variable";
Exp range "range for the loop";
list<Statement> statementLst;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_FOR;
record STMT_PARFOR
Type type_ "this is the type of the iterator";
Boolean iterIsArray "True if the iterator has an array type, otherwise false.";
Ident iter "the iterator variable";
Exp range "range for the loop";
list<Statement> statementLst;
list<tuple<ComponentRef,SourceInfo>> loopPrlVars "list of parallel variables used/referenced in the parfor loop";
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_PARFOR;
record STMT_WHILE
Exp exp;
list<Statement> statementLst;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_WHILE;
record STMT_WHEN
Exp exp;
list<ComponentRef> conditions; // list of boolean variables as conditions (this is simcode stuff)
Boolean initialCall; // true, if top-level branch with initial() (this is simcode stuff)
list<Statement> statementLst;
Option<Statement> elseWhen;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_WHEN;
record STMT_ASSERT "assert(cond,msg)"
Exp cond;
Exp msg;
Exp level;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_ASSERT;
record STMT_TERMINATE "terminate(msg)"
Exp msg;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_TERMINATE;
record STMT_REINIT
Exp var "Variable";
Exp value "Value ";
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_REINIT;
record STMT_NORETCALL "call with no return value, i.e. no equation.
Typically sideeffect call of external function."
Exp exp;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_NORETCALL;
record STMT_RETURN
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_RETURN;
record STMT_BREAK
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_BREAK;
record STMT_CONTINUE
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_CONTINUE;
record STMT_ARRAY_INIT "For function initialization"
String name;
Type ty;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_ARRAY_INIT;
// MetaModelica extension. KS
record STMT_FAILURE
list<Statement> body;
ElementSource source "the origin of the component/equation/algorithm" ;
end STMT_FAILURE;
end Statement;
public
uniontype Else "An if statements can one or more `elseif\' branches and an
optional `else\' branch."
record NOELSE end NOELSE;
record ELSEIF
Exp exp;
list<Statement> statementLst;
Else else_;
end ELSEIF;
record ELSE
list<Statement> statementLst;
end ELSE;
end Else;
/* -- End Algorithm.mo -- */
/* -- Start Types.mo -- */
public
uniontype Var "- Variables"
record TYPES_VAR
Ident name "name";
Attributes attributes "attributes";
Type ty "type";
Binding binding "equation modification";
Boolean bind_from_outside "true if the binding has come from out of scope. This happens for derived record classes.
e.g. record A = B(k=exp). here the modification 'exp' is a binding from outside. We need
this infor to correctly generate default constructors at codegen time. This binding exp
will have to be supplied from outside for a default constructor of the owner record type";
Option<Const> constOfForIteratorRange "the constant-ness of the range if this is a for iterator, NONE() if is NOT a for iterator";
end TYPES_VAR;
end Var;
public
uniontype Attributes "- Attributes"
record ATTR
ConnectorType connectorType "flow, stream or unspecified";
SCode.Parallelism parallelism "parallelism";
SCode.Variability variability "variability" ;
Absyn.Direction direction "direction" ;
Absyn.InnerOuter innerOuter "inner, outer, inner outer or unspecified";
SCode.Visibility visibility "public, protected";
end ATTR;
end Attributes;
public
constant Attributes dummyAttrVar = ATTR(NON_CONNECTOR(), SCode.NON_PARALLEL(), SCode.VAR(), Absyn.BIDIR(), Absyn.NOT_INNER_OUTER(), SCode.PUBLIC());
constant Attributes dummyAttrParam = ATTR(NON_CONNECTOR(), SCode.NON_PARALLEL(), SCode.PARAM(), Absyn.BIDIR(), Absyn.NOT_INNER_OUTER(), SCode.PUBLIC());
constant Attributes dummyAttrConst = ATTR(NON_CONNECTOR(), SCode.NON_PARALLEL(), SCode.CONST(), Absyn.BIDIR(), Absyn.NOT_INNER_OUTER(), SCode.PUBLIC());
constant Attributes dummyAttrInput = ATTR(NON_CONNECTOR(), SCode.NON_PARALLEL(), SCode.VAR(), Absyn.INPUT(), Absyn.NOT_INNER_OUTER(), SCode.PUBLIC());
public uniontype BindingSource "where this binding came from: either default binding or start value"
record BINDING_FROM_DEFAULT_VALUE "the binding came from the default value" end BINDING_FROM_DEFAULT_VALUE;
record BINDING_FROM_START_VALUE "the binding came from the start value" end BINDING_FROM_START_VALUE;
record BINDING_FROM_RECORD_SUBMODS "the EQ binding is created from the submods of a record VARIABLE declration e.g. 'R r(i=2)' tranformed by instantiation to 'R r = R(i=2)' " end BINDING_FROM_RECORD_SUBMODS;
record BINDING_FROM_DERIVED_RECORD_DECL "the binding is created from the submods of a DERIVED record DECLARATION e.g. 'record K = R(i=3)'" end BINDING_FROM_DERIVED_RECORD_DECL;
end BindingSource;
public
uniontype Binding
record UNBOUND end UNBOUND;
record EQBOUND
Exp exp;
Option<Values.Value> evaluatedExp;
Const constant_;
BindingSource source;
end EQBOUND;
record VALBOUND
Values.Value valBound;
BindingSource source;
end VALBOUND;
end Binding;
public
type EqualityConstraint = Option<tuple<Absyn.Path, Integer, InlineType>>
"contains the path to the equalityConstraint function,
the dimension of the output and the inline type of the function";
// default constants that can be used
constant Type T_REAL_DEFAULT = T_REAL({});
constant Type T_INTEGER_DEFAULT = T_INTEGER({});
constant Type T_STRING_DEFAULT = T_STRING({});
constant Type T_BOOL_DEFAULT = T_BOOL({});
constant Type T_CLOCK_DEFAULT = T_CLOCK({});
constant Type T_ENUMERATION_DEFAULT = T_ENUMERATION(NONE(), Absyn.IDENT(""), {}, {}, {});
constant Type T_REAL_BOXED = T_METABOXED(T_REAL_DEFAULT);
constant Type T_INTEGER_BOXED = T_METABOXED(T_INTEGER_DEFAULT);
constant Type T_STRING_BOXED = T_METABOXED(T_STRING_DEFAULT);
constant Type T_BOOL_BOXED = T_METABOXED(T_BOOL_DEFAULT);
constant Type T_METABOXED_DEFAULT = T_METABOXED(T_UNKNOWN_DEFAULT);
constant Type T_METALIST_DEFAULT = T_METALIST(T_UNKNOWN_DEFAULT);
constant Type T_NONE_DEFAULT = T_METAOPTION(T_UNKNOWN_DEFAULT);
constant Type T_ANYTYPE_DEFAULT = T_ANYTYPE(NONE());
constant Type T_UNKNOWN_DEFAULT = T_UNKNOWN();
constant Type T_NORETCALL_DEFAULT = T_NORETCALL();
constant Type T_METATYPE_DEFAULT = T_METATYPE(T_UNKNOWN_DEFAULT);
constant Type T_COMPLEX_DEFAULT = T_COMPLEX(ClassInf.UNKNOWN(Absyn.IDENT("")), {}, NONE(), false) "default complex with unknown CiState";
constant Type T_COMPLEX_DEFAULT_RECORD = T_COMPLEX(ClassInf.RECORD(Absyn.IDENT("")), {}, NONE(), false) "default complex with record CiState";
constant Type T_SOURCEINFO_DEFAULT_METARECORD = T_METARECORD(Absyn.QUALIFIED("SourceInfo",Absyn.IDENT("SOURCEINFO")), Absyn.IDENT("SourceInfo"), {}, 1, {
TYPES_VAR("fileName", dummyAttrVar, T_STRING_DEFAULT, UNBOUND(), false, NONE()),
TYPES_VAR("isReadOnly", dummyAttrVar, T_BOOL_DEFAULT, UNBOUND(), false, NONE()),
TYPES_VAR("lineNumberStart", dummyAttrVar, T_INTEGER_DEFAULT, UNBOUND(), false, NONE()),
TYPES_VAR("columnNumberStart", dummyAttrVar, T_INTEGER_DEFAULT, UNBOUND(), false, NONE()),
TYPES_VAR("lineNumberEnd", dummyAttrVar, T_INTEGER_DEFAULT, UNBOUND(), false, NONE()),
TYPES_VAR("columnNumberEnd", dummyAttrVar, T_INTEGER_DEFAULT, UNBOUND(), false, NONE()),
TYPES_VAR("lastModification", dummyAttrVar, T_REAL_DEFAULT, UNBOUND(), false, NONE())
}, true);
constant Type T_SOURCEINFO_DEFAULT = T_METAUNIONTYPE({Absyn.QUALIFIED("SourceInfo",Absyn.IDENT("SOURCEINFO"))},{},true,EVAL_SINGLETON_KNOWN_TYPE(T_SOURCEINFO_DEFAULT_METARECORD),Absyn.IDENT("SourceInfo"));
// Arrays of unknown dimension, eg. Real[:]
public constant Type T_ARRAY_REAL_NODIM = T_ARRAY(T_REAL_DEFAULT,{DIM_UNKNOWN()});
public constant Type T_ARRAY_INT_NODIM = T_ARRAY(T_INTEGER_DEFAULT,{DIM_UNKNOWN()});
public constant Type T_ARRAY_BOOL_NODIM = T_ARRAY(T_BOOL_DEFAULT,{DIM_UNKNOWN()});
public constant Type T_ARRAY_STRING_NODIM = T_ARRAY(T_STRING_DEFAULT,{DIM_UNKNOWN()});
public uniontype Type "models the different front-end and back-end types"
record T_INTEGER
list<Var> varLst;
end T_INTEGER;
record T_REAL
list<Var> varLst;
end T_REAL;
record T_STRING
list<Var> varLst;
end T_STRING;
record T_BOOL
list<Var> varLst;
end T_BOOL;
record T_CLOCK
list<Var> varLst; // BTH Since Clock type has no attributes, this is not really needed, but at the moment kept for unified treatment of fundamental types
end T_CLOCK;
record T_ENUMERATION "If the list of names is empty, this is the super-enumeration that is the super-class of all enumerations"
Option<Integer> index "the enumeration value index, SOME for element, NONE() for type" ;
Absyn.Path path "enumeration path" ;
list<String> names "names" ;
list<Var> literalVarLst;
list<Var> attributeLst;
end T_ENUMERATION;
record T_ARRAY
"an array can be represented in two equivalent ways:
1. T_ARRAY(non_array_type, {dim1, dim2, dim3})
2. T_ARRAY(T_ARRAY(T_ARRAY(non_array_type, {dim1}), {dim2}), {dim3})
In general Inst generates 1 and all the others generates 2"
Type ty "Type";
Dimensions dims "dims";
end T_ARRAY;
record T_NORETCALL "For functions not returning any values."
end T_NORETCALL;
record T_UNKNOWN "Used when type is not yet determined"
end T_UNKNOWN;
record T_COMPLEX
ClassInf.State complexClassType "The type of a class";
list<Var> varLst "The variables of a complex type";
EqualityConstraint equalityConstraint;
Boolean usedExternally "If the record is passed to an external function at any point, we need to generate conversion functions for it (for instance to convert 'modelica_integer' to 'int')";
end T_COMPLEX;
record T_SUBTYPE_BASIC
ClassInf.State complexClassType "The type of a class";
list<Var> varLst "complexVarLst; The variables of a complex type! Should be empty, kept here to verify!";
Type complexType "complexType; A complex type can be a subtype of another (primitive) type (through extends)";
EqualityConstraint equalityConstraint;
end T_SUBTYPE_BASIC;
record T_FUNCTION
list<FuncArg> funcArg "funcArg" ;
Type funcResultType "Only single-result" ;
FunctionAttributes functionAttributes;
Absyn.Path path;
end T_FUNCTION;
record T_FUNCTION_REFERENCE_VAR "MetaModelica Function Reference that is a variable"
Type functionType "the type of the function";
end T_FUNCTION_REFERENCE_VAR;
record T_FUNCTION_REFERENCE_FUNC "MetaModelica Function Reference that is a direct reference to a function"
Boolean builtin;
Type functionType "type of the non-boxptr function";
end T_FUNCTION_REFERENCE_FUNC;
record T_TUPLE
list<Type> types "For functions returning multiple values.";
Option<list<String>> names "For tuples elements that have names (function outputs)";
end T_TUPLE;
record T_CODE
CodeType ty;
end T_CODE;