/
BackendDAECreate.mo
3254 lines (3044 loc) · 142 KB
/
BackendDAECreate.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 BackendDAECreate
" file: BackendDAECreate.mo
package: BackendDAECreate
description: This file contains all functions for transforming the DAE structure to the BackendDAE.
RCS: $Id$
"
public import Absyn;
public import BackendDAE;
public import DAE;
public import Env;
protected import BackendDAEUtil;
protected import BackendDump;
protected import BackendEquation;
protected import BackendVariable;
protected import BackendVarTransform;
protected import BaseHashTable;
protected import CheckModel;
protected import ComponentReference;
protected import Config;
protected import ClassInf;
protected import DAEDump;
protected import DAEUtil;
protected import Debug;
protected import DynamicOptimization;
protected import Error;
protected import Expression;
protected import ExpressionDump;
protected import Flags;
protected import Global;
protected import HashTableExpToExp;
protected import HashTableExpToIndex;
protected import HashTable;
protected import HashTableCrToExpSourceTpl;
protected import Inline;
protected import List;
protected import SimCodeUtil;
protected import SCode;
protected import System;
protected import Types;
protected import Util;
protected type Functiontuple = tuple<Option<DAE.FunctionTree>,list<DAE.InlineType>>;
public function lower "This function translates a DAE, which is the result from instantiating a
class, into a more precise form, called BackendDAE.BackendDAE defined in this module.
The BackendDAE.BackendDAE representation splits the DAE into equations and variables
and further divides variables into known and unknown variables and the
equations into simple and nonsimple equations.
The variables are inserted into a hash table. This gives a lookup cost of
O(1) for finding a variable. The equations are put in an expandable
array. Where adding a new equation can be done in O(1) time if space
is available.
inputs: lst: DAE.DAElist, inCache: Env.Cache, inEnv: Env.Env
outputs: BackendDAE.BackendDAE"
input DAE.DAElist lst;
input Env.Cache inCache;
input Env.Env inEnv;
input BackendDAE.ExtraInfo inExtraInfo;
output BackendDAE.BackendDAE outBackendDAE;
protected
BackendDAE.Variables vars, knvars, vars_1, extVars, aliasVars;
list<BackendDAE.Equation> eqns, reqns, ieqns, algeqns, algeqns1, ialgeqns, multidimeqns, imultidimeqns, eqns_1, ceeqns, iceeqns;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
list<BackendDAE.WhenClause> whenclauses, whenclauses_1;
BackendDAE.EquationArray eqnarr, reqnarr, ieqnarr;
BackendDAE.ExternalObjectClasses extObjCls;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.EventInfo einfo;
list<DAE.Element> elems, aliaseqns;
list<BackendDAE.ZeroCrossing> zero_crossings;
DAE.FunctionTree functionTree;
list<BackendDAE.TimeEvent> timeEvents;
String neqStr,nvarStr;
algorithm
// reset dumped file sequence number
System.tmpTickResetIndex(0, Global.backendDAE_fileSequence);
functionTree := Env.getFunctionTree(inCache);
(DAE.DAE(elems), functionTree, timeEvents) := processBuiltinExpressions(lst, functionTree);
vars := BackendVariable.emptyVars();
knvars := BackendVariable.emptyVars();
extVars := BackendVariable.emptyVars();
(vars, knvars, extVars, eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, _) :=
lower2(listReverse(elems), functionTree, vars, knvars, extVars, {}, {}, {}, {}, {}, {}, {}, {}, HashTableExpToExp.emptyHashTable());
whenclauses_1 := listReverse(whenclauses);
aliasVars := BackendVariable.emptyVars();
// handle alias equations
(vars, knvars, extVars, aliasVars, eqns, reqns, ieqns, whenclauses_1) := handleAliasEquations(aliaseqns, vars, knvars, extVars, aliasVars, eqns, reqns, ieqns, whenclauses_1);
vars_1 := detectImplicitDiscrete(vars, knvars, eqns);
(vars_1, eqns, clsAttrs) := DynamicOptimization.addOptimizationVarsEqns(vars_1, eqns, Config.acceptOptimicaGrammar(), clsAttrs, constrs, knvars);
eqnarr := BackendEquation.listEquation(eqns);
reqnarr := BackendEquation.listEquation(reqns);
ieqnarr := BackendEquation.listEquation(ieqns);
einfo := BackendDAE.EVENT_INFO(timeEvents, whenclauses_1, {}, {}, {}, 0, 0);
symjacs := {(NONE(), ({}, {}, ({}, {})), {}), (NONE(), ({}, {}, ({}, {})), {}), (NONE(), ({}, {}, ({}, {})), {}), (NONE(), ({}, {}, ({}, {})), {})};
outBackendDAE := BackendDAE.DAE(BackendDAE.EQSYSTEM(vars_1,
eqnarr,
NONE(),
NONE(),
BackendDAE.NO_MATCHING(), {}, BackendDAE.UNKNOWN_PARTITION())::{},
BackendDAE.SHARED(knvars,
extVars,
aliasVars,
ieqnarr,
reqnarr,
constrs,
clsAttrs,
inCache,
inEnv,
functionTree,
einfo,
extObjCls,
BackendDAE.SIMULATION(),
symjacs,inExtraInfo));
BackendDAEUtil.checkBackendDAEWithErrorMsg(outBackendDAE);
neqStr := intString(BackendDAEUtil.equationSize(eqnarr));
nvarStr := intString(BackendVariable.varsSize(vars_1));
Error.assertionOrAddSourceMessage(not Flags.isSet(Flags.DUMP_BACKENDDAE_INFO),Error.BACKENDDAEINFO_LOWER,{neqStr,nvarStr},Absyn.dummyInfo);
SimCodeUtil.execStat("Generate backend data structure");
end lower;
protected function lower2
"Helper function to lower."
input list<DAE.Element> inElements "input is in reverse order. this is faster than reversing all accumulators at the end";
input DAE.FunctionTree functionTree;
input BackendDAE.Variables inVars "The time depend Variables";
input BackendDAE.Variables inKnVars "The time independend Variables";
input BackendDAE.Variables inExVars "The external Variables";
input list<BackendDAE.Equation> inEqnsLst "The dynamic Equations/Algoritms";
input list<BackendDAE.Equation> inREqnsLst "The algebraic Equations";
input list<BackendDAE.Equation> inIEqnsLst "The initial Equations";
input list<DAE.Constraint> inConstraintLst;
input list<DAE.ClassAttributes> inClassAttributeLst;
input list<BackendDAE.WhenClause> inWhenClauseLst;
input BackendDAE.ExternalObjectClasses inExtObjClasses;
input list<DAE.Element> iAliaseqns "List with all EqualityEquations";
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnownVariables;
output BackendDAE.Variables outExternalVariables;
output list<BackendDAE.Equation> oEqnsLst;
output list<BackendDAE.Equation> oREqnsLst;
output list<BackendDAE.Equation> oIEqnsLst;
output list<DAE.Constraint> outConstraintLst;
output list<DAE.ClassAttributes> outClassAttributeLst;
output list<BackendDAE.WhenClause> outWhenClauseLst;
output BackendDAE.ExternalObjectClasses outExtObjClasses;
output list<DAE.Element> oAliaseqns;
output HashTableExpToExp.HashTable oinlineHT "workaround to speed up inlining of array parameters";
algorithm
(outVariables, outKnownVariables, outExternalVariables, oEqnsLst, oREqnsLst, oIEqnsLst,
outConstraintLst, outClassAttributeLst, outWhenClauseLst, outExtObjClasses, oAliaseqns, oinlineHT):=
match (inElements, functionTree, inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst,
inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT)
local
BackendDAE.Variables vars, knvars, extVars;
list<BackendDAE.WhenClause> whenclauses;
list<BackendDAE.Equation> eqns, reqns, ieqns;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
BackendDAE.ExternalObjectClasses extObjCls;
DAE.Element daeEl;
list<DAE.Element> daeLstRest, aliaseqns;
HashTableExpToExp.HashTable inlineHT;
// the empty case
case ({}, _, _, _, _, _, _, _, _, _, _, _, _,_)
then
(inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
case (daeEl::daeLstRest, _, _, _, _, _, _, _, _, _, _, _, _,_)
equation
(vars, knvars, extVars, eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, inlineHT) =
lower3(daeEl, functionTree, inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
(vars, knvars, extVars, eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, inlineHT) =
lower2(daeLstRest, functionTree, vars, knvars, extVars, eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, inlineHT);
then
(vars, knvars, extVars, eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, inlineHT);
end match;
end lower2;
protected function lower3
"Helper function to lower."
input DAE.Element inElement "input is in reverse order. this is faster than reversing all accumulators at the end";
input DAE.FunctionTree functionTree;
input BackendDAE.Variables inVars "The time depend Variables";
input BackendDAE.Variables inKnVars "The time independend Variables";
input BackendDAE.Variables inExVars "The external Variables";
input list<BackendDAE.Equation> inEqnsLst "The dynamic Equations/Algoritms";
input list<BackendDAE.Equation> inREqnsLst "The algebraic Equations";
input list<BackendDAE.Equation> inIEqnsLst "The initial Equations";
input list<DAE.Constraint> inConstraintLst;
input list<DAE.ClassAttributes> inClassAttributeLst;
input list<BackendDAE.WhenClause> inWhenClauseLst;
input BackendDAE.ExternalObjectClasses inExtObjClasses;
input list<DAE.Element> iAliaseqns "List with all EqualityEquations";
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnownVariables;
output BackendDAE.Variables outExternalVariables;
output list<BackendDAE.Equation> oEqnsLst;
output list<BackendDAE.Equation> oREqnsLst;
output list<BackendDAE.Equation> oIEqnsLst;
output list<DAE.Constraint> outConstraintLst;
output list<DAE.ClassAttributes> outClassAttributeLst;
output list<BackendDAE.WhenClause> outWhenClauseLst;
output BackendDAE.ExternalObjectClasses outExtObjClasses;
output list<DAE.Element> oAliaseqns;
output HashTableExpToExp.HashTable oinlineHT "workaround to speed up inlining of array parameters";
algorithm
(outVariables, outKnownVariables, outExternalVariables, oEqnsLst, oREqnsLst, oIEqnsLst,
outConstraintLst, outClassAttributeLst, outWhenClauseLst, outExtObjClasses, oAliaseqns, oinlineHT):=
match (inElement, functionTree, inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst,
inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT)
local
list<DAE.Element> daeElts, aliaseqns;
DAE.Constraint cons_1;
list<DAE.Constraint> constrs;
DAE.ClassAttributes clsattrs_1;
list<DAE.ClassAttributes> clsAttrs;
Absyn.Path path;
BackendDAE.Variables vars, knvars, extVars;
BackendDAE.ExternalObjectClasses extObjCls;
BackendDAE.ExternalObjectClass extObjCl;
list<BackendDAE.Equation> eqns, reqns, ieqns;
list<BackendDAE.WhenClause> whenclauses;
DAE.ElementSource source;
HashTableExpToExp.HashTable inlineHT;
// class for external object
case (DAE.EXTOBJECTCLASS(path, source), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
extObjCl = BackendDAE.EXTOBJCLASS(path, source);
then
(inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst, inConstraintLst, inClassAttributeLst, inWhenClauseLst, extObjCl::inExtObjClasses, iAliaseqns, iInlineHT);
// variables
case (DAE.VAR(componentRef = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(vars, knvars, extVars, eqns, inlineHT) = lowerVar(inElement, functionTree, inVars, inKnVars, inExVars, inEqnsLst, iInlineHT);
then
(vars, knvars, extVars, eqns, inREqnsLst, inIEqnsLst, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, inlineHT);
// scalar equations
case (DAE.EQUATION(exp = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// initial equations
case (DAE.INITIALEQUATION(exp1 = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// effort variable equality equations, seperated to generate alias variables
case (DAE.EQUEQUATION(cr1 = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
//eqns = inEqnsLst;
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, /*inElement::*/iAliaseqns, iInlineHT);
// a solved equation
case (DAE.DEFINE(componentRef = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// a initial solved equation
case (DAE.INITIALDEFINE(componentRef = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// complex equations
case (DAE.COMPLEX_EQUATION(lhs = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// complex initial equations
case (DAE.INITIAL_COMPLEX_EQUATION(lhs = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// array equations
case (DAE.ARRAY_EQUATION(exp = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// initial array equations
case (DAE.INITIAL_ARRAY_EQUATION(exp = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// when equations
case (DAE.WHEN_EQUATION(condition = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, whenclauses) = lowerWhenEqn(inElement, functionTree, inEqnsLst, inWhenClauseLst);
then
(inVars, inKnVars, inExVars, eqns, inREqnsLst, inIEqnsLst, inConstraintLst, inClassAttributeLst, whenclauses, inExtObjClasses, iAliaseqns, iInlineHT);
// if equation
case (DAE.IF_EQUATION(condition1 = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// initial if equation
case (DAE.INITIAL_IF_EQUATION(condition1 = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerEqn(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst);
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// algorithm
case (DAE.ALGORITHM(algorithm_ = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerAlgorithm(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst, DAE.EXPAND());
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// initial algorithm
case (DAE.INITIALALGORITHM(algorithm_ = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(_,_, ieqns) = lowerAlgorithm(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst, DAE.EXPAND());
then
(inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// flat class / COMP
case (DAE.COMP(dAElist = daeElts), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(vars, knvars, extVars, eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, inlineHT) = lower2(listReverse(daeElts), functionTree, inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
then
(vars, knvars, extVars, eqns, reqns, ieqns, constrs, clsAttrs, whenclauses, extObjCls, aliaseqns, inlineHT);
// assert in equation section is converted to ALGORITHM
case (DAE.ASSERT(condition = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerAlgorithm(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst, DAE.NOT_EXPAND());
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// terminate in equation section is converted to ALGORITHM
case (DAE.TERMINATE(message = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerAlgorithm(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst, DAE.NOT_EXPAND());
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
case (DAE.NORETCALL(exp = _), _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
(eqns, reqns, ieqns) = lowerAlgorithm(inElement, functionTree, inEqnsLst, inREqnsLst, inIEqnsLst, DAE.NOT_EXPAND());
then
(inVars, inKnVars, inExVars, eqns, reqns, ieqns, inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
// constraint (Optimica) Just pass the constraints for now. Should anything more be done here?
case (DAE.CONSTRAINT(constraints = cons_1), _, _, _, _, _, _, _, _, _, _, _, _, _)
then
(inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst, cons_1::inConstraintLst, inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
case (DAE.CLASS_ATTRIBUTES(classAttrs = clsattrs_1), _, _, _, _, _, _, _, _, _, _, _, _, _)
then
(inVars, inKnVars, inExVars, inEqnsLst, inREqnsLst, inIEqnsLst, inConstraintLst, clsattrs_1::inClassAttributeLst, inWhenClauseLst, inExtObjClasses, iAliaseqns, iInlineHT);
case (_, _, _, _, _, _, _, _, _, _, _, _, _, _)
equation
// show only on failtrace!
true = Flags.isSet(Flags.FAILTRACE);
Debug.traceln("- BackendDAECreate.lower3 failed on: " +& DAEDump.dumpElementsStr({inElement}));
then
fail();
end match;
end lower3;
// =============================================================================
// section for processing builtin expressions
//
// Insert a unique index (starting with 1) before the first arguments of some
// builtin calls. Equal calls will get the same index.
// - delay(expr, delayTime, delayMax)
// => delay(index, expr, delayTime, delayMax)
// - sample(start, interval)
// => sample(index, start, interval)
// =============================================================================
protected function processBuiltinExpressions "author: lochel
Assign some builtin calls with a unique id argument."
input DAE.DAElist inDAE;
input DAE.FunctionTree functionTree;
output DAE.DAElist outDAE;
output DAE.FunctionTree outTree;
output list<BackendDAE.TimeEvent> outTimeEvents;
protected
HashTableExpToIndex.HashTable ht;
algorithm
ht := HashTableExpToIndex.emptyHashTable();
(outDAE, outTree, (_, (ht, _, _, outTimeEvents))) := DAEUtil.traverseDAE(inDAE, functionTree, Expression.traverseSubexpressionsHelper, (transformBuiltinExpression, (ht, 0, 0, {})));
end processBuiltinExpressions;
protected function transformBuiltinExpression "author: lochel
Helper for transformBuiltinExpressions"
input DAE.Exp inExp;
input tuple<HashTableExpToIndex.HashTable, Integer /*iDelay*/, Integer /*iSample*/, list<BackendDAE.TimeEvent>> inTuple;
output DAE.Exp outExp;
output tuple<HashTableExpToIndex.HashTable, Integer /*iDelay*/, Integer /*iSample*/, list<BackendDAE.TimeEvent>> outTuple;
algorithm
(outExp,outTuple) := matchcontinue (inExp,inTuple)
local
DAE.Exp e, start, interval;
list<DAE.Exp> es;
HashTableExpToIndex.HashTable ht;
Integer iDelay, iSample, i;
list<BackendDAE.TimeEvent> timeEvents;
DAE.CallAttributes attr;
// delay [already in ht]
case (e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht, iDelay, iSample, timeEvents)) equation
i = BaseHashTable.get(e, ht);
then (DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(i)::es, attr), (ht, iDelay, iSample, timeEvents));
// delay [not yet in ht]
case (e as DAE.CALL(Absyn.IDENT("delay"), es, attr), (ht, iDelay, iSample, timeEvents)) equation
ht = BaseHashTable.add((e, iDelay+1), ht);
then (DAE.CALL(Absyn.IDENT("delay"), DAE.ICONST(iDelay)::es, attr), (ht, iDelay+1, iSample, timeEvents));
// sample [already in ht]
case (e as DAE.CALL(Absyn.IDENT("sample"), es, attr), (ht, iDelay, iSample, timeEvents)) equation
i = BaseHashTable.get(e, ht);
then (DAE.CALL(Absyn.IDENT("sample"), DAE.ICONST(i)::es, attr), (ht, iDelay, iSample, timeEvents));
// sample [not yet in ht]
case (e as DAE.CALL(Absyn.IDENT("sample"), es as {start, interval}, attr), (ht, iDelay, iSample, timeEvents)) equation
iSample = iSample+1;
timeEvents = listAppend(timeEvents, {BackendDAE.SAMPLE_TIME_EVENT(iSample, start, interval)});
ht = BaseHashTable.add((e, iSample), ht);
then (DAE.CALL(Absyn.IDENT("sample"), DAE.ICONST(iSample)::es, attr), (ht, iDelay, iSample, timeEvents));
else (inExp,inTuple);
end matchcontinue;
end transformBuiltinExpression;
/*
* lower all variables
*/
public function lowerVars
input list<DAE.Element> inElements;
input DAE.FunctionTree functionTree;
input BackendDAE.Variables inVars "The time depend Variables";
input BackendDAE.Variables inKnVars "The time independend Variables";
input BackendDAE.Variables inExVars "The external Variables";
input list<BackendDAE.Equation> inEqnsLst "The dynamic Equations/Algoritms";
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnownVariables;
output BackendDAE.Variables outExternalVariables;
output list<BackendDAE.Equation> oEqnsLst;
algorithm
(outVariables, outKnownVariables, outExternalVariables, oEqnsLst) :=
matchcontinue (inElements, functionTree, inVars, inKnVars, inExVars, inEqnsLst)
local
list<DAE.Element> rest, newVars;
DAE.Element elem,var;
DAE.Type tp, arrtp;
DAE.ComponentRef cref;
list<DAE.ComponentRef> crefs;
BackendDAE.Variables vars, knvars, exvars;
list<BackendDAE.Equation> eqns;
String str;
case ({}, _, _, _, _, _) then (inVars, inKnVars, inExVars, inEqnsLst);
case ((var as DAE.VAR(componentRef = cref, ty = DAE.T_ARRAY(ty = arrtp)))::rest, _, _, _, _, _)
equation
crefs = ComponentReference.expandCref(cref, false);
elem = DAEUtil.replaceTypeInVar(arrtp, var);
newVars = List.map1(crefs, DAEUtil.replaceCrefInVar, elem);
(vars, knvars, exvars, eqns) = lowerVars(newVars, functionTree, inVars, inKnVars, inExVars, inEqnsLst);
//(vars, knvars, exvars, eqns, _) = lowerVar(elem, functionTree, vars, knvars, exvars, eqns, HashTableExpToExp.emptyHashTable());
(vars, knvars, exvars, eqns) = lowerVars(rest, functionTree, vars, knvars, exvars, eqns);
then (vars, knvars, exvars, eqns);
case ((elem as DAE.VAR(componentRef = _))::rest, _, _, _, _, _)
equation
(vars, knvars, exvars, eqns, _) = lowerVar(elem, functionTree, inVars, inKnVars, inExVars, inEqnsLst, HashTableExpToExp.emptyHashTable());
(vars, knvars, exvars, eqns) = lowerVars(rest, functionTree, vars, knvars, exvars, eqns);
then (vars, knvars, exvars, eqns);
case (elem::_, _, _, _, _, _)
equation
str = "BackendDAECreate.lowerVars failed for " +& DAEDump.dumpElementsStr({elem});
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end matchcontinue;
end lowerVars;
protected function lowerVar
"Transforms a DAE variable to DAE variable.
Includes changing the ComponentRef name to a simpler form
\'a\'.\'b\'{2}\'c\'{5} becomes
\'a.b{2}.c\' (as CREF_IDENT(\"a.b.c\", {2}) )
inputs: (DAE.Element)
outputs: Var"
input DAE.Element inElement;
input DAE.FunctionTree functionTree;
input BackendDAE.Variables inVars "The time depend Variables";
input BackendDAE.Variables inKnVars "The time independend Variables";
input BackendDAE.Variables inExVars "The external Variables";
input list<BackendDAE.Equation> inEqnsLst "The dynamic Equations/Algoritms";
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnownVariables;
output BackendDAE.Variables outExternalVariables;
output list<BackendDAE.Equation> oEqnsLst;
output HashTableExpToExp.HashTable oInlineHT "workaround to speed up inlining of array parameters";
algorithm
(outVariables, outKnownVariables, outExternalVariables, oEqnsLst, oInlineHT) :=
matchcontinue (inElement, functionTree, inVars, inKnVars, inExVars, inEqnsLst, iInlineHT)
local
String str;
DAE.ComponentRef cr;
DAE.ElementSource source;
DAE.Exp e1, e2;
BackendDAE.Var backendVar1;
BackendDAE.Variables vars, knvars, extvars;
HashTableExpToExp.HashTable inlineHT;
list<BackendDAE.Equation> assrtEqs, eqLst; // assrtEqs pop up when asserts are inlined
list<DAE.Statement> assrtLst;
// external object variables
case (DAE.VAR(ty = DAE.T_COMPLEX(complexClassType = ClassInf.EXTERNAL_OBJ(path=_))), _, _, _, _, _, _)
equation
backendVar1 = lowerExtObjVar(inElement, functionTree);
extvars = BackendVariable.addVar(backendVar1, inExVars);
then
(inVars, inKnVars, extvars, inEqnsLst, iInlineHT);
// variables: states and algebraic variables with binding equation
case (DAE.VAR(componentRef = cr, binding=SOME(e2), source = source), _, _, _, _, _, _)
equation
// adrpo 2009-09-07 - according to MathCore
// add the binding as an equation and remove the binding from variable!
true = isStateOrAlgvar(inElement);
(backendVar1) = lowerDynamicVar(inElement, functionTree);
(e2, source, _,_) = Inline.inlineExp(e2, (SOME(functionTree), {DAE.NORM_INLINE()}), source);
vars = BackendVariable.addVar(backendVar1, inVars);
e1 = Expression.crefExp(cr);
then
(vars, inKnVars, inExVars, BackendDAE.EQUATION(e1, e2, source, BackendDAE.EQUATION_ATTRIBUTES(false, BackendDAE.BINDING_EQUATION(), BackendDAE.UNKNOWN_SUB_PARTITION()))::inEqnsLst, iInlineHT);
// variables: states and algebraic variables with NO binding equation
case (DAE.VAR(binding=NONE()), _, _, _, _, _, _)
equation
true = isStateOrAlgvar(inElement);
(backendVar1) = lowerDynamicVar(inElement, functionTree);
vars = BackendVariable.addVar(backendVar1, inVars);
then
(vars, inKnVars, inExVars, inEqnsLst, iInlineHT);
// variables: states and algebraic variables with NO binding equation
case (DAE.VAR(binding=NONE()), _, _, _, _, _, _)
equation
true = isStateOrAlgvar(inElement);
(backendVar1) = lowerDynamicVar(inElement, functionTree);
vars = BackendVariable.addVar(backendVar1, inVars);
then
(vars, inKnVars, inExVars, inEqnsLst, iInlineHT);
// known variables: parameters and constants
case (DAE.VAR(componentRef = _), _, _, _, _, _, _)
equation
(backendVar1, inlineHT,assrtEqs) = lowerKnownVar(inElement, functionTree, iInlineHT) "in previous rule, lower_var failed." ;
knvars = BackendVariable.addVar(backendVar1, inKnVars);
eqLst = listAppend(inEqnsLst,assrtEqs);
then
(inVars, knvars, inExVars, eqLst, inlineHT); // CHANGE HERE TO EQLST
else
equation
str = "BackendDAECreate.lowerVar failed for " +& DAEDump.dumpElementsStr({inElement});
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end matchcontinue;
end lowerVar;
protected function isStateOrAlgvar
"@author adrpo
check if this variable is a state or algebraic"
input DAE.Element e;
output Boolean out;
algorithm
out := match (e)
case (DAE.VAR(kind = DAE.VARIABLE())) then true;
case (DAE.VAR(kind = DAE.DISCRETE())) then true;
else false;
end match;
end isStateOrAlgvar;
protected function lowerDynamicVar
"Transforms a DAE variable to DAE variable.
Includes changing the ComponentRef name to a simpler form
\'a\'.\'b\'{2}\'c\'{5} becomes
\'a.b{2}.c\' (as CREF_IDENT(\"a.b.c\", {2}) )
inputs: DAE.Element
outputs: Var"
input DAE.Element inElement;
input DAE.FunctionTree functionTree;
output BackendDAE.Var outVar;
algorithm
(outVar) := match (inElement, functionTree)
local
list<DAE.Dimension> dims;
DAE.ComponentRef name;
BackendDAE.VarKind kind_1;
DAE.VarKind kind;
DAE.VarDirection dir;
DAE.VarParallelism prl;
BackendDAE.Type tp;
DAE.ConnectorType ct;
DAE.ElementSource source;
Option<DAE.VariableAttributes> dae_var_attr;
Option<SCode.Comment> comment;
DAE.Type t;
DAE.VarVisibility protection;
Boolean b;
case (DAE.VAR(componentRef = name,
kind = kind,
direction = dir,
parallelism = prl,
protection = protection,
ty = t,
dims = dims,
connectorType = ct,
source = source,
variableAttributesOption = dae_var_attr,
comment = comment), _)
equation
(kind_1) = lowerVarkind(kind, t, name, dir, ct, dae_var_attr);
tp = lowerType(t);
b = DAEUtil.boolVarVisibility(protection);
dae_var_attr = DAEUtil.setProtectedAttr(dae_var_attr, b);
dae_var_attr = setMinMaxFromEnumeration(t, dae_var_attr);
_ = BackendVariable.getMinMaxAsserts(dae_var_attr, name, source, kind_1, tp, {});
_ = BackendVariable.getNominalAssert(dae_var_attr, name, source, kind_1, tp, {});
(dae_var_attr, source, _) = Inline.inlineStartAttribute(dae_var_attr, source, (SOME(functionTree), {DAE.NORM_INLINE()}));
then
(BackendDAE.VAR(name, kind_1, dir, prl, tp, NONE(), NONE(), dims, source, dae_var_attr, comment, ct));
end match;
end lowerDynamicVar;
protected function lowerKnownVar
"Helper function to lower2"
input DAE.Element inElement;
input DAE.FunctionTree functionTree;
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output BackendDAE.Var outVar;
output HashTableExpToExp.HashTable oInlineHT "workaround to speed up inlining of array parameters";
output list<BackendDAE.Equation> assrtEqOut;
algorithm
(outVar,oInlineHT,assrtEqOut) := matchcontinue (inElement, functionTree, iInlineHT)
local
list<DAE.Dimension> dims;
DAE.ComponentRef name;
BackendDAE.VarKind kind_1;
Option<DAE.Exp> bind, bind1;
DAE.VarKind kind;
DAE.VarDirection dir;
DAE.VarParallelism prl;
BackendDAE.Type tp;
DAE.ConnectorType ct;
DAE.ElementSource source;
Option<DAE.VariableAttributes> dae_var_attr;
Option<SCode.Comment> comment;
DAE.Type t;
DAE.VarVisibility protection;
Boolean b;
String str;
Functiontuple fnstpl;
HashTableExpToExp.HashTable inlineHT;
list<DAE.Statement> assrtLst;
list<BackendDAE.Equation> eqLst;
case (DAE.VAR(componentRef = name,
kind = kind,
direction = dir,
parallelism = prl,
protection = protection,
ty = t,
binding = bind,
dims = dims,
connectorType = ct,
source = source,
variableAttributesOption = dae_var_attr,
comment = comment), _, _)
equation
kind_1 = lowerKnownVarkind(kind, name, dir, ct);
// bind = fixParameterStartBinding(bind, t, dae_var_attr, kind_1);
tp = lowerType(t);
b = DAEUtil.boolVarVisibility(protection);
dae_var_attr = DAEUtil.setProtectedAttr(dae_var_attr, b);
dae_var_attr = setMinMaxFromEnumeration(t, dae_var_attr);
_ = BackendVariable.getMinMaxAsserts(dae_var_attr, name, source, kind_1, tp, {});
_ = BackendVariable.getNominalAssert(dae_var_attr, name, source, kind_1, tp, {});
fnstpl = (SOME(functionTree), {DAE.NORM_INLINE()});
// build algorithms for the inlined asserts
(bind1, source, inlineHT,assrtLst) = inlineExpOpt(bind, fnstpl, source, iInlineHT);
eqLst = buildAssertAlgorithms(assrtLst,source,{});
// building an algorithm of the assert
(dae_var_attr, source, _) = Inline.inlineStartAttribute(dae_var_attr, source, fnstpl);
then
(BackendDAE.VAR(name, kind_1, dir, prl, tp, bind1, NONE(), dims, source, dae_var_attr, comment, ct), inlineHT,eqLst);
else
equation
str = "BackendDAECreate.lowerKnownVar failed for " +& DAEDump.dumpElementsStr({inElement});
Error.addMessage(Error.INTERNAL_ERROR, {str});
then fail();
end matchcontinue;
end lowerKnownVar;
protected function buildAssertAlgorithms "builds BackendDAE.ALGORITHM out of the given assert statements
author:Waurich TUD 2013-10"
input list<DAE.Statement> assrtIn;
input DAE.ElementSource source;
input list<BackendDAE.Equation> eqIn;
output list<BackendDAE.Equation> eqOut;
algorithm
eqOut := matchcontinue(assrtIn,source,eqIn)
local
BackendDAE.Equation eq;
DAE.Statement assrt;
list<DAE.Statement> rest;
list<BackendDAE.Equation> eqLst;
case({},_,_)
then
eqIn;
case(assrt::rest,_,_)
equation
eq = BackendDAE.ALGORITHM(0,DAE.ALGORITHM_STMTS({assrt}),source,DAE.EXPAND(),BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
eqLst = buildAssertAlgorithms(rest,source,eq::eqIn);
then
eqLst;
else
equation
print("buildAssertAlgorithm failed!\n");
then
fail();
end matchcontinue;
end buildAssertAlgorithms;
protected function inlineExpOpt
"author Frenkel TUD 2013-02"
input Option<DAE.Exp> iOptExp;
input Functiontuple fnstpl;
input DAE.ElementSource iSource;
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output Option<DAE.Exp> oOptExp;
output DAE.ElementSource oSource;
output HashTableExpToExp.HashTable oInlineHT "workaround to speed up inlining of array parameters";
output list<DAE.Statement> assrtLstOut;
algorithm
(oOptExp,oSource,oInlineHT,assrtLstOut) := match(iOptExp,fnstpl,iSource,iInlineHT)
local
DAE.Exp e;
DAE.ElementSource source;
HashTableExpToExp.HashTable inlineHT;
list<DAE.Statement> assrtLst;
case (NONE(),_,_,_) then (iOptExp,iSource,iInlineHT,{});
case (SOME(e),_,_,_)
equation
(e, source, inlineHT,assrtLst) = inlineExpOpt1(e, fnstpl, iSource, iInlineHT);
then (SOME(e),source,inlineHT,assrtLst);
end match;
end inlineExpOpt;
protected function inlineExpOpt1
"author Frenkel TUD 2013-02"
input DAE.Exp iExp;
input Functiontuple fnstpl;
input DAE.ElementSource iSource;
input HashTableExpToExp.HashTable iInlineHT "workaround to speed up inlining of array parameters";
output DAE.Exp oExp;
output DAE.ElementSource oSource;
output HashTableExpToExp.HashTable oInlineHT "workaround to speed up inlining of array parameters";
output list<DAE.Statement> assrtLstOut;
algorithm
(oExp,oSource,oInlineHT,assrtLstOut) := matchcontinue(iExp,fnstpl,iSource,iInlineHT)
local
DAE.Exp e,e1;
list<DAE.Exp> elst;
DAE.ElementSource source;
HashTableExpToExp.HashTable inlineHT;
Boolean inlined;
list<DAE.Statement> assrtLst,assrtLst1,assrtLst2;
case (DAE.CALL(path=_),_,_,_)
equation
e1 = BaseHashTable.get(iExp,iInlineHT);
// print("use chache Inline\n" +& ExpressionDump.printExpStr(iExp) +& "\n");
source = DAEUtil.addSymbolicTransformation(iSource,DAE.OP_INLINE(DAE.PARTIAL_EQUATION(iExp),DAE.PARTIAL_EQUATION(e1)));
then (e1,source,iInlineHT,{});
case (DAE.CALL(path=_),_,_,_)
equation
// print("add chache Inline\n" +& ExpressionDump.printExpStr(iExp) +& "\n");
(e1, source, inlined,_) = Inline.inlineExp(iExp, fnstpl, iSource);
inlineHT = Debug.bcallret2(inlined, BaseHashTable.add, (iExp,e1), iInlineHT, iInlineHT);
then (e1,source,inlineHT,{});
case (DAE.ASUB(e,elst),_,_,_)
equation
e1 = BaseHashTable.get(e,iInlineHT);
// print("use chache Inline\n" +& ExpressionDump.printExpStr(iExp) +& "\n");
source = DAEUtil.addSymbolicTransformation(iSource,DAE.OP_INLINE(DAE.PARTIAL_EQUATION(e),DAE.PARTIAL_EQUATION(e1)));
(e, source, _,_) = Inline.inlineExp(DAE.ASUB(e1,elst), fnstpl, source);
then (e,source,iInlineHT,{});
case (DAE.ASUB(e,elst),_,_,_)
equation
// print("add chache Inline(1)\n" +& ExpressionDump.printExpStr(iExp) +& "\n");
(e1, _, inlined,assrtLst1) = Inline.inlineExp(e, fnstpl, iSource);
inlineHT = Debug.bcallret2(inlined, BaseHashTable.add, (e,e1), iInlineHT, iInlineHT);
(e, source, _,assrtLst2) = Inline.inlineExp(DAE.ASUB(e1,elst), fnstpl, iSource);
assrtLst = listAppend(assrtLst1,assrtLst2);
then (e,source,inlineHT,assrtLst);
case (_,_,_,_)
equation
// print("no chache Inline\n" +& ExpressionDump.printExpStr(iExp) +& "\n");
(e, source, _,_) = Inline.inlineExp(iExp, fnstpl, iSource);
then (e,source,iInlineHT,{});
end matchcontinue;
end inlineExpOpt1;
protected function setMinMaxFromEnumeration
input DAE.Type inType;
input Option<DAE.VariableAttributes> inVarAttr;
output Option<DAE.VariableAttributes> outVarAttr;
algorithm
outVarAttr := matchcontinue (inType, inVarAttr)
local
Option<DAE.Exp> min, max;
list<String> names;
Absyn.Path path;
case (DAE.T_ENUMERATION(path=path, names = names), _)
equation
(min, max) = DAEUtil.getMinMaxValues(inVarAttr);
then
setMinMaxFromEnumeration1(min, max, inVarAttr, path, names);
else inVarAttr;
end matchcontinue;
end setMinMaxFromEnumeration;
protected function setMinMaxFromEnumeration1
input Option<DAE.Exp> inMin;
input Option<DAE.Exp> inMax;
input Option<DAE.VariableAttributes> inVarAttr;
input Absyn.Path inPath;
input list<String> inNames;
output Option<DAE.VariableAttributes> outVarAttr;
algorithm
outVarAttr := matchcontinue (inMin, inMax, inVarAttr, inPath, inNames)
local
Integer i;
Absyn.Path namee1, nameen;
String s1, sn;
DAE.Exp e;
case (NONE(), NONE(), _, _, _)
equation
i = listLength(inNames);
s1 = listGet(inNames, 1);
namee1 = Absyn.joinPaths(inPath, Absyn.IDENT(s1));
sn = listGet(inNames, i);
nameen = Absyn.joinPaths(inPath, Absyn.IDENT(sn));
then
DAEUtil.setMinMax(inVarAttr, SOME(DAE.ENUM_LITERAL(namee1, 1)), SOME(DAE.ENUM_LITERAL(nameen, i)));
case (NONE(), SOME(e), _, _, _)
equation
_ = listLength(inNames);
s1 = listGet(inNames, 1);
namee1 = Absyn.joinPaths(inPath, Absyn.IDENT(s1));
then
DAEUtil.setMinMax(inVarAttr, SOME(DAE.ENUM_LITERAL(namee1, 1)), SOME(e));
case (SOME(e), NONE(), _, _, _)
equation
i = listLength(inNames);
sn = listGet(inNames, i);
nameen = Absyn.joinPaths(inPath, Absyn.IDENT(sn));
then
DAEUtil.setMinMax(inVarAttr, SOME(e), SOME(DAE.ENUM_LITERAL(nameen, i)));
else inVarAttr;
end matchcontinue;
end setMinMaxFromEnumeration1;
// protected function fixParameterStartBinding
// input Option<DAE.Exp> bind;
// input DAE.Type ty;
// input Option<DAE.VariableAttributes> attr;
// input BackendDAE.VarKind kind;
// output Option<DAE.Exp> outBind;
// algorithm
// outBind := matchcontinue (bind, ty, attr, kind)
// local
// DAE.Exp exp;
// case (NONE(), DAE.T_REAL(source=_), _, BackendDAE.PARAM())
// equation
// exp = DAEUtil.getStartAttr(attr);
// then SOME(exp);
// else bind;
// end matchcontinue;
// end fixParameterStartBinding;
protected function lowerVarkind
"Helper function to lowerVar.
inputs: (DAE.VarKind,
Type,
DAE.ComponentRef,
DAE.VarDirection, /* input/output/bidir */
DAE.ConnectorType)
outputs VarKind
NOTE: Fails for not states that are not algebraic
variables, e.g. parameters and constants"
input DAE.VarKind inVarKind;
input DAE.Type inType;
input DAE.ComponentRef inComponentRef;
input DAE.VarDirection inVarDirection;
input DAE.ConnectorType inConnectorType;
input Option<DAE.VariableAttributes> daeAttr;
output BackendDAE.VarKind outVarKind;
algorithm
(outVarKind) := matchcontinue (inVarKind, inType, inComponentRef, inVarDirection, inConnectorType, daeAttr)
// variable -> state if have stateSelect=StateSelect.always
case (DAE.VARIABLE(), _, _, _, _, SOME(DAE.VAR_ATTR_REAL(stateSelectOption = SOME(DAE.ALWAYS()))))
then (BackendDAE.STATE(1,NONE()));
// variable -> state if have stateSelect=StateSelect.prefer
case (DAE.VARIABLE(), _, _, _, _, SOME(DAE.VAR_ATTR_REAL(stateSelectOption = SOME(DAE.PREFER()))))
then (BackendDAE.STATE(1,NONE()));
case (DAE.VARIABLE(), DAE.T_BOOL(varLst = _), _, _, _, _)
equation
failure(BackendVariable.topLevelInput(inComponentRef, inVarDirection, inConnectorType));
then
(BackendDAE.DISCRETE());
case (DAE.DISCRETE(), DAE.T_BOOL(varLst = _), _, _, _, _)
equation
failure(BackendVariable.topLevelInput(inComponentRef, inVarDirection, inConnectorType));
then
(BackendDAE.DISCRETE());
case (DAE.VARIABLE(), DAE.T_INTEGER(varLst = _), _, _, _, _)
equation
failure(BackendVariable.topLevelInput(inComponentRef, inVarDirection, inConnectorType));
then
(BackendDAE.DISCRETE());
case (DAE.DISCRETE(), DAE.T_INTEGER(varLst = _), _, _, _, _)