/
BackendDAEUtil.mo
6928 lines (6411 loc) · 247 KB
/
BackendDAEUtil.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-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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 BackendDAEUtil
" file: BackendDAEUtil.mo
package: BackendDAEUtil
description: BackendDAEUtil comprised functions for BackendDAE data types.
RCS: $Id$
This module is a lowered form of a DAE including equations
and simple equations in
two separate lists. The variables are split into known variables
parameters and constants, and unknown variables,
states and algebraic variables.
The module includes the BLT sorting algorithm which sorts the
equations into blocks, and the index reduction algorithm using
dummy derivatives for solving higher index problems.
It also includes the tarjan algorithm to detect strong components
in the BLT sorting."
public import BackendDAE;
public import DAE;
public import Env;
protected import Absyn;
protected import BackendDump;
protected import BackendDAECreate;
protected import BackendDAEOptimize;
protected import BackendDAETransform;
protected import BackendEquation;
protected import BackendVariable;
protected import BaseHashTable;
protected import ComponentReference;
protected import Ceval;
protected import ClassInf;
protected import DAEUtil;
protected import Derive;
protected import Debug;
protected import Error;
protected import Expression;
protected import ExpressionSimplify;
protected import ExpressionDump;
protected import HashTable2;
protected import HashTable4;
protected import List;
protected import OptManager;
protected import RTOpts;
protected import SCode;
protected import System;
protected import Util;
protected import Values;
protected import VarTransform;
/*************************************************
* checkBackendDAE and stuff
************************************************/
public function checkBackendDAEWithErrorMsg"function: checkBackendDAEWithErrorMsg
author: Frenkel TUD
run checkDEALow and prints all errors"
input BackendDAE.BackendDAE inBackendDAE;
protected
list<tuple<DAE.Exp,list<DAE.ComponentRef>>> expCrefs;
list<BackendDAE.Equation> wrongEqns;
algorithm
_ := matchcontinue (inBackendDAE)
local BackendDAE.BackendDAE bdae;
case (bdae)
equation
false = RTOpts.debugFlag("checkBackendDAE");
then
();
case (bdae)
equation
true = RTOpts.debugFlag("checkBackendDAE");
(expCrefs,wrongEqns) = checkBackendDAE(inBackendDAE);
printcheckBackendDAEWithErrorMsg(expCrefs,wrongEqns);
then
();
end matchcontinue;
end checkBackendDAEWithErrorMsg;
public function printcheckBackendDAEWithErrorMsg"function: printcheckBackendDAEWithErrorMsg
author: Frenkel TUD
helper for checkDEALowWithErrorMsg"
input list<tuple<DAE.Exp,list<DAE.ComponentRef>>> inExpCrefs;
input list<BackendDAE.Equation> inWrongEqns;
algorithm
_ := matchcontinue (inExpCrefs,inWrongEqns)
local
DAE.Exp e;
list<DAE.ComponentRef> crefs;
list<tuple<DAE.Exp,list<DAE.ComponentRef>>> res;
list<String> strcrefs;
String crefstring, expstr,scopestr;
BackendDAE.Equation eqn;
list<BackendDAE.Equation> wrongEqns;
DAE.ElementSource source;
case ({},{}) then ();
case ({},eqn::wrongEqns)
equation
printEqnSizeError(eqn);
printcheckBackendDAEWithErrorMsg({},wrongEqns);
then ();
case (((e,crefs))::res,wrongEqns)
equation
strcrefs = List.map(crefs,ComponentReference.crefStr);
crefstring = stringDelimitList(strcrefs,", ");
expstr = ExpressionDump.printExpStr(e);
scopestr = stringAppendList({crefstring," from Expression: ",expstr});
Error.addMessage(Error.LOOKUP_VARIABLE_ERROR, {scopestr,"BackendDAE object"});
printcheckBackendDAEWithErrorMsg(res,wrongEqns);
then
();
end matchcontinue;
end printcheckBackendDAEWithErrorMsg;
protected function printEqnSizeError"function: printEqnSizeError
author: Frenkel TUD 2010-12"
input BackendDAE.Equation inEqn;
algorithm
_ := matchcontinue(inEqn)
local
BackendDAE.Equation eqn;
DAE.Exp e1, e2;
DAE.ComponentRef cr;
DAE.ExpType t1,t2;
String eqnstr, t1str, t2str, tstr;
DAE.ElementSource source;
case (eqn as BackendDAE.EQUATION(exp=e1,scalar=e2,source=source))
equation
eqnstr = BackendDump.equationStr(eqn);
t1 = Expression.typeof(e1);
t2 = Expression.typeof(e2);
t1str = ExpressionDump.typeString(t1);
t2str = ExpressionDump.typeString(t2);
tstr = stringAppendList({t1str," != ", t2str});
Error.addSourceMessage(Error.EQUATION_TYPE_MISMATCH_ERROR, {eqnstr,tstr}, DAEUtil.getElementSourceFileInfo(source));
then ();
case (eqn as BackendDAE.SOLVED_EQUATION(componentRef=cr,exp=e1,source=source))
equation
eqnstr = BackendDump.equationStr(eqn);
t1 = Expression.typeof(e1);
t2 = ComponentReference.crefLastType(cr);
t1str = ExpressionDump.typeString(t1);
t2str = ExpressionDump.typeString(t2);
tstr = stringAppendList({t1str," != ", t2str});
Error.addSourceMessage(Error.EQUATION_TYPE_MISMATCH_ERROR, {eqnstr,tstr}, DAEUtil.getElementSourceFileInfo(source));
then ();
case (eqn as BackendDAE.COMPLEX_EQUATION(lhs=e1,rhs=e2,source=source))
equation
eqnstr = BackendDump.equationStr(eqn);
t1 = Expression.typeof(e1);
t2 = Expression.typeof(e2);
t1str = ExpressionDump.typeString(t1);
t2str = ExpressionDump.typeString(t2);
tstr = stringAppendList({t1str," != ", t2str});
Error.addSourceMessage(Error.EQUATION_TYPE_MISMATCH_ERROR, {eqnstr,tstr}, DAEUtil.getElementSourceFileInfo(source));
then ();
//
case eqn then ();
end matchcontinue;
end printEqnSizeError;
public function checkBackendDAE "function: checkBackendDAE
author: Frenkel TUD
This function checks the BackendDAE object if
- all component refercences used in the expressions are
part of the BackendDAE object.
- all variables that are reinit are states
Returns all component references which not part of the BackendDAE object."
input BackendDAE.BackendDAE inBackendDAE;
output list<tuple<DAE.Exp,list<DAE.ComponentRef>>> outExpCrefs;
output list<BackendDAE.Equation> outWrongEqns;
algorithm
(outExpCrefs,outWrongEqns) := matchcontinue (inBackendDAE)
local
BackendDAE.Variables vars1,vars2,allvars;
BackendDAE.EquationArray eqns,reqns,ieqns;
array<BackendDAE.MultiDimEquation> ae;
array<DAE.Algorithm> algs;
BackendDAE.EventInfo einfo;
list<BackendDAE.WhenClause> whenClauseLst;
list<BackendDAE.Var> varlst1,varlst2,allvarslst;
list<tuple<DAE.Exp,list<DAE.ComponentRef>>> expcrefs,expcrefs1,expcrefs2,expcrefs3,expcrefs4,expcrefs5,expcrefs7;
list<BackendDAE.Equation> wrongEqns,wrongEqns1,wrongEqns2;
case (BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(orderedVars = vars1,orderedEqs = eqns)::{},shared=BackendDAE.SHARED(knownVars = vars2,initialEqs = ieqns,removedEqs = reqns,
arrayEqs = ae,algorithms = algs, eventInfo = BackendDAE.EVENT_INFO(whenClauseLst=whenClauseLst))))
equation
varlst1 = varList(vars1);
varlst2 = varList(vars2);
allvarslst = listAppend(varlst1,varlst2);
allvars = listVar(allvarslst);
((_,expcrefs)) = traverseBackendDAEExpsVars(vars1,checkBackendDAEExp,(allvars,{}));
((_,expcrefs1)) = traverseBackendDAEExpsVars(vars2,checkBackendDAEExp,(allvars,expcrefs));
((_,expcrefs2)) = traverseBackendDAEExpsEqns(eqns,checkBackendDAEExp,(allvars,expcrefs1));
((_,expcrefs3)) = traverseBackendDAEExpsEqns(reqns,checkBackendDAEExp,(allvars,expcrefs2));
((_,expcrefs4)) = traverseBackendDAEExpsEqns(ieqns,checkBackendDAEExp,(allvars,expcrefs3));
((_,expcrefs5)) = traverseBackendDAEArrayNoCopy(ae,checkBackendDAEExp,traverseBackendDAEExpsArrayEqn,1,arrayLength(ae),(allvars,expcrefs4));
//((_,expcrefs6)) = traverseBackendDAEArrayNoCopy(algs,checkBackendDAEExp,traverseAlgorithmExps,1,arrayLength(algs),(allvars,expcrefs5));
(_,(_,expcrefs7)) = BackendDAETransform.traverseBackendDAEExpsWhenClauseLst(whenClauseLst,checkBackendDAEExp,(allvars,expcrefs5));
wrongEqns = BackendEquation.traverseBackendDAEEqns(eqns,checkEquationSize,{});
wrongEqns1 = BackendEquation.traverseBackendDAEEqns(reqns,checkEquationSize,wrongEqns);
wrongEqns2 = BackendEquation.traverseBackendDAEEqns(ieqns,checkEquationSize,wrongEqns1);
then
(expcrefs7,wrongEqns2);
else
equation
Debug.fprintln("failtrace", "- BackendDAEUtil.checkBackendDAE failed");
then
fail();
end matchcontinue;
end checkBackendDAE;
protected function checkBackendDAEExp
input tuple<DAE.Exp, tuple<BackendDAE.Variables,list<tuple<DAE.Exp,list<DAE.ComponentRef>>>>> inTpl;
output tuple<DAE.Exp, tuple<BackendDAE.Variables,list<tuple<DAE.Exp,list<DAE.ComponentRef>>>>> outTpl;
algorithm
outTpl :=
matchcontinue inTpl
local
DAE.Exp exp;
BackendDAE.Variables vars;
list<DAE.ComponentRef> crefs;
list<tuple<DAE.Exp,list<DAE.ComponentRef>>> lstExpCrefs,lstExpCrefs1;
case ((exp,(vars,lstExpCrefs)))
equation
((_,(_,crefs))) = Expression.traverseExp(exp,traversecheckBackendDAEExp,(vars,{}));
lstExpCrefs1 = Util.if_(listLength(crefs)>0,(exp,crefs)::lstExpCrefs,lstExpCrefs);
then
((exp,(vars,lstExpCrefs1)));
case inTpl then inTpl;
end matchcontinue;
end checkBackendDAEExp;
protected function traversecheckBackendDAEExp
input tuple<DAE.Exp, tuple<BackendDAE.Variables,list<DAE.ComponentRef>>> inTuple;
output tuple<DAE.Exp, tuple<BackendDAE.Variables,list<DAE.ComponentRef>>> outTuple;
algorithm
outTuple := matchcontinue(inTuple)
local
DAE.Exp e,e1;
BackendDAE.Variables vars,vars1;
DAE.ComponentRef cr;
list<DAE.ComponentRef> crefs,crefs1;
list<DAE.Exp> expl;
list<DAE.ExpVar> varLst;
DAE.Ident ident;
list<BackendDAE.Var> backendVars;
DAE.ReductionIterators riters;
// special case for time, it is never part of the equation system
case ((e as DAE.CREF(componentRef = DAE.CREF_IDENT(ident="time")),(vars,crefs)))
then ((e, (vars,crefs)));
// Special Case for Records
case ((e as DAE.CREF(componentRef = cr,ty= DAE.ET_COMPLEX(varLst=varLst,complexClassType=ClassInf.RECORD(_))),(vars,crefs)))
equation
expl = List.map1(varLst,Expression.generateCrefsExpFromExpVar,cr);
((_,(vars1,crefs1))) = Expression.traverseExpList(expl,traversecheckBackendDAEExp,(vars,crefs));
then
((e, (vars1,crefs1)));
// Special Case for Arrays
case ((e as DAE.CREF(ty = DAE.ET_ARRAY(ty=_)),(vars,crefs)))
equation
((e1,(_,true))) = extendArrExp((e,(NONE(),false)));
((_,(vars1,crefs1))) = Expression.traverseExp(e1,traversecheckBackendDAEExp,(vars,crefs));
then
((e, (vars1,crefs1)));
// case for Reductions
case ((e as DAE.REDUCTION(iterators = riters),(vars,crefs)))
equation
// add idents to vars
backendVars = List.map(riters,makeIterVariable);
vars = BackendVariable.addVars(backendVars,vars);
then
((e, (vars,crefs)));
// case for functionpointers
case ((e as DAE.CREF(ty=DAE.ET_FUNCTION_REFERENCE_FUNC(builtin=_)),(vars,crefs)))
then
((e, (vars,crefs)));
case ((e as DAE.CREF(componentRef = cr),(vars,crefs)))
equation
(_,_) = BackendVariable.getVar(cr, vars);
then
((e, (vars,crefs)));
case ((e as DAE.CREF(componentRef = cr),(vars,crefs)))
equation
failure((_,_) = BackendVariable.getVar(cr, vars));
then
((e, (vars,cr::crefs)));
case inTuple then inTuple;
end matchcontinue;
end traversecheckBackendDAEExp;
protected function makeIterVariable
input DAE.ReductionIterator iter;
output BackendDAE.Var backendVar;
protected
String name;
DAE.ComponentRef cr;
algorithm
name := Expression.reductionIterName(iter);
cr := ComponentReference.makeCrefIdent(name,DAE.ET_INT(),{});
backendVar := BackendDAE.VAR(cr,BackendDAE.VARIABLE(),DAE.BIDIR(),BackendDAE.INT(),NONE(),NONE(),{},0,
DAE.emptyElementSource,NONE(),NONE(),DAE.NON_CONNECTOR(),DAE.NON_STREAM_CONNECTOR());
end makeIterVariable;
protected function checkEquationSize"function: checkEquationSize
author: Frenkel TUD 2010-12
- check if the left hand site and thr rigth hand site have equal types.
"
input tuple<BackendDAE.Equation, list<BackendDAE.Equation>> inTpl;
output tuple<BackendDAE.Equation, list<BackendDAE.Equation>> outTpl;
algorithm
outTpl := matchcontinue(inTpl)
local
BackendDAE.Equation e;
list<BackendDAE.Equation> wrongEqns,wrongEqns1;
DAE.Exp e1, e2;
DAE.ComponentRef cr;
DAE.ExpType t1,t2;
Boolean b;
case ((e as BackendDAE.EQUATION(exp=e1,scalar=e2),wrongEqns))
equation
t1 = Expression.typeof(e1);
t2 = Expression.typeof(e2);
b = Expression.equalTypes(t1,t2);
wrongEqns1 = List.consOnTrue(not b,e,wrongEqns);
then ((e,wrongEqns1));
case ((e as BackendDAE.SOLVED_EQUATION(componentRef=cr,exp=e1),wrongEqns))
equation
t1 = Expression.typeof(e1);
t2 = ComponentReference.crefLastType(cr);
b = Expression.equalTypes(t1,t2);
wrongEqns1 = List.consOnTrue(not b,e,wrongEqns);
then ((e,wrongEqns1));
case ((e as BackendDAE.COMPLEX_EQUATION(lhs=e1,rhs=e2),wrongEqns))
equation
t1 = Expression.typeof(e1);
t2 = Expression.typeof(e2);
b = Expression.equalTypes(t1,t2);
wrongEqns1 = List.consOnTrue(not b,e,wrongEqns);
then ((e,wrongEqns1));
//
case inTpl then inTpl;
end matchcontinue;
end checkEquationSize;
public function checkAssertCondition "Succeds if condition of assert is not constant false"
input DAE.Exp cond;
input DAE.Exp message;
algorithm
_ := matchcontinue(cond,message)
local
String messageStr;
case(_, _)
equation
// Don't check assertions when checking models
true = OptManager.getOption("checkModel");
then ();
case(cond,message) equation
false = Expression.isConstFalse(cond);
then ();
case(cond,message)
equation
true = Expression.isConstFalse(cond);
messageStr = ExpressionDump.printExpStr(message);
Error.addMessage(Error.ASSERT_CONSTANT_FALSE_ERROR,{messageStr});
then fail();
end matchcontinue;
end checkAssertCondition;
/*************************************************
* Initialisation and stuff
************************************************/
public function checkInitialSystem "function: checkInitialSystem
author: Frenkel TUD 2010-12
- check if the inital conditions full specified and fix it
if not.
"
input BackendDAE.BackendDAE inDAE;
input DAE.FunctionTree funcs;
output BackendDAE.BackendDAE outDAE;
algorithm
outDAE := mapEqSystem1(inDAE,checkInitialSystemWork,funcs);
end checkInitialSystem;
protected function checkInitialSystemWork "function: checkInitialSystem
author: Frenkel TUD 2010-12
- check if the inital conditions full specified and fix it
if not.
"
input BackendDAE.EqSystem syst;
input DAE.FunctionTree funcs;
input BackendDAE.Shared shared;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
algorithm
(osyst,oshared) := matchcontinue (syst,funcs,shared)
local
BackendDAE.Variables variables,knvars,exObj,aliasVars;
BackendDAE.EquationArray orderedEqs,removedEqs,initialEqs;
array<DAE.Algorithm> algs;
array<BackendDAE.MultiDimEquation> arrayEqs;
BackendDAE.EventInfo eventInfo;
BackendDAE.ExternalObjectClasses extObjClasses;
Integer nie,nie1,nie2,unfixed,unfixed1;
BackendDAE.BackendDAE dae,dae1;
list<BackendDAE.WhenClause> whenClauseLst;
list<DAE.ComponentRef> vars,varsws,states,statesws;
BackendDAE.AliasVariables av;
case (syst as BackendDAE.EQSYSTEM(orderedVars=variables,orderedEqs=orderedEqs),funcs,shared as BackendDAE.SHARED(knownVars=knvars,initialEqs=initialEqs,externalObjects=exObj,aliasVars=av,removedEqs=removedEqs,
arrayEqs=arrayEqs,algorithms=algs,eventInfo=eventInfo,extObjClasses=extObjClasses))
equation
/* count the unfixed variables */
// vars
((vars,varsws,states,statesws,unfixed)) = BackendVariable.traverseBackendDAEVars(variables,countInitialVars,({},{},{},{},0));
// kvars
((vars,varsws,states,statesws,unfixed1)) = BackendVariable.traverseBackendDAEVars(knvars,countInitialVars,(vars,varsws,states,statesws,unfixed));
/* count the equations */
nie = equationSize(initialEqs);
BackendDAE.EVENT_INFO(whenClauseLst=whenClauseLst) = eventInfo;
((nie1,_)) = BackendEquation.traverseBackendDAEEqns(orderedEqs,countInitialEqns,(nie,whenClauseLst));
((nie2,_)) = BackendEquation.traverseBackendDAEEqns(removedEqs,countInitialEqns,(nie1,whenClauseLst));
(syst,shared) = checkInitialSystem1(unfixed1,nie2,syst,shared,funcs,vars,varsws,states,statesws);
then (syst,shared);
case (syst,_,shared)
equation
Error.addMessage(Error.INTERNAL_ERROR,{"BackendDAEUtil.checkInitialSystemWork failed"});
then (syst,shared);
end matchcontinue;
end checkInitialSystemWork;
protected function checkInitialSystem1"function: checkInitialSystem
author: Frenkel TUD 2010-12"
input Integer inUnfixed;
input Integer inInitialEqns;
input BackendDAE.EqSystem syst;
input BackendDAE.Shared shared;
input DAE.FunctionTree funcs;
input list<DAE.ComponentRef> inVars;
input list<DAE.ComponentRef> inVarsWS;
input list<DAE.ComponentRef> inStates;
input list<DAE.ComponentRef> inStatesWS;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
algorithm
(osyst,oshared) := matchcontinue (inUnfixed,inInitialEqns,syst,shared,funcs,inVars,inVarsWS,inStates,inStatesWS)
local
BackendDAE.Variables vars,knvars,exObj,vars1,knvars1;
BackendDAE.EquationArray orderedEqs,removedEqs,initialEqs;
array<BackendDAE.MultiDimEquation> arrayEqs;
array<DAE.Algorithm> algs;
BackendDAE.EventInfo eventInfo;
BackendDAE.ExternalObjectClasses extObjClasses;
BackendDAE.AliasVariables alisvars;
BackendDAE.BackendDAE dae1;
Option<BackendDAE.IncidenceMatrix> om,omT;
BackendDAE.BackendDAEType btp;
BackendDAE.Matching matching;
// unfixed equal equations
case (inUnfixed,inInitialEqns,syst,shared,_,_,_,_,_)
equation
true = intEq(inUnfixed,inInitialEqns);
then
(syst,shared);
// unfixed less than equations
case (inUnfixed,inInitialEqns,syst,shared,_,_,_,_,_)
equation
true = intLt(inUnfixed,inInitialEqns);
then
(syst,shared);
// unfixed greater than equations
case (inUnfixed,inInitialEqns,BackendDAE.EQSYSTEM(vars,orderedEqs,om,omT,matching),BackendDAE.SHARED(knownVars=knvars,initialEqs=initialEqs,externalObjects=exObj,aliasVars=alisvars,removedEqs=removedEqs,
arrayEqs=arrayEqs,algorithms=algs,eventInfo=eventInfo,extObjClasses=extObjClasses,backendDAEType=btp),funcs,inVars,inVarsWS,inStates,inStatesWS)
equation
true = RTOpts.debugFlag("dumpInit");
print("Warning initial conditions not fully specified.\n");
print("Variables with fixed=false: ");print(intString(inUnfixed)); print("\n");
print("Number of equations for initialisation: ");print(intString(inInitialEqns)); print("\n");
true = intGt(inUnfixed,inInitialEqns);
// change fixed to true until equal equations
(vars1,knvars1) = fixInitalVars(inUnfixed,inInitialEqns,vars,knvars,inVars,inVarsWS,inStates,inStatesWS);
syst = BackendDAE.EQSYSTEM(vars1,orderedEqs,om,omT,matching);
shared = BackendDAE.SHARED(knvars1,exObj,alisvars,initialEqs,removedEqs,arrayEqs,algs,eventInfo,extObjClasses,btp);
then
(syst,shared);
// unfixed greater than equations
case (inUnfixed,inInitialEqns,BackendDAE.EQSYSTEM(vars,orderedEqs,om,omT,matching),BackendDAE.SHARED(knownVars=knvars,externalObjects=exObj,aliasVars=alisvars,initialEqs=initialEqs,removedEqs=removedEqs,
arrayEqs=arrayEqs,algorithms=algs,eventInfo=eventInfo,extObjClasses=extObjClasses,backendDAEType=btp),funcs,inVars,inVarsWS,inStates,inStatesWS)
equation
true = intGt(inUnfixed,inInitialEqns);
// change fixed to true until equal equations
(vars1,knvars1) = fixInitalVars(inUnfixed,inInitialEqns,vars,knvars,inVars,inVarsWS,inStates,inStatesWS);
syst = BackendDAE.EQSYSTEM(vars1,orderedEqs,om,omT,matching);
shared = BackendDAE.SHARED(knvars1,exObj,alisvars,initialEqs,removedEqs,arrayEqs,algs,eventInfo,extObjClasses,btp);
then
(syst,shared);
case (_,_,syst,shared,_,_,_,_,_)
equation
Error.addMessage(Error.INTERNAL_ERROR,{"- BackendDAEUtil.checkInitialSystem1 failed"});
then
(syst,shared);
end matchcontinue;
end checkInitialSystem1;
protected function countInitialVars
" function countInitialVars
autor: Frenkel TUD 2010-12"
input tuple<BackendDAE.Var, tuple<list<DAE.ComponentRef>,list<DAE.ComponentRef>,list<DAE.ComponentRef>,list<DAE.ComponentRef>,Integer>> inTpl;
output tuple<BackendDAE.Var, tuple<list<DAE.ComponentRef>,list<DAE.ComponentRef>,list<DAE.ComponentRef>,list<DAE.ComponentRef>,Integer>> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
BackendDAE.Var var;
list<DAE.ComponentRef> vars,varsws,states,statesws;
Integer unfixed;
BackendDAE.VarKind kind;
DAE.ComponentRef cr;
String s,scr;
// constants
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
true = BackendVariable.isConst(var);
then
((var,(vars,varsws,states,statesws,unfixed)));
// parameters with fixed=false : become variable
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
true = BackendVariable.isParam(var);
false = BackendVariable.varFixed(var);
then
((var,(vars,varsws,states,statesws,unfixed+1)));
// parameters with fixed=true
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
true = BackendVariable.isParam(var);
then
((var,(vars,varsws,states,statesws,unfixed)));
// states with fixed=true
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
true = BackendVariable.isStateVar(var);
true = BackendVariable.varFixed(var);
then
((var,(vars,varsws,states,statesws,unfixed+1 /*for derivative*/)));
// states with fixed=false and start value
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
true = BackendVariable.isStateVar(var);
false = BackendVariable.varFixed(var);
_ = BackendVariable.varStartValueFail(var);
cr = BackendVariable.varCref(var);
then
((var,(vars,varsws,states,cr::statesws,unfixed+2/*+1 for derivative*/)));
// states with fixed=false and without start value
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
true = BackendVariable.isStateVar(var);
false = BackendVariable.varFixed(var);
cr = BackendVariable.varCref(var);
then
((var,(vars,varsws,cr::states,statesws,unfixed+2/*+1 for derivative*/)));
// vars with fixed=true
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
true = BackendVariable.varFixed(var);
kind = BackendVariable.varKind(var);
BackendVariable.isVarKindVariable(kind);
then
((var,(vars,varsws,states,statesws,unfixed)));
// vars with fixed=false and bound expression
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
false = BackendVariable.varFixed(var);
kind = BackendVariable.varKind(var);
BackendVariable.isVarKindVariable(kind);
_ = BackendVariable.varBindExp(var);
then
((var,(vars,varsws,states,statesws,unfixed)));
// vars with fixed=false and start value
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
false = BackendVariable.varFixed(var);
kind = BackendVariable.varKind(var);
BackendVariable.isVarKindVariable(kind);
_ = BackendVariable.varStartValueFail(var);
cr = BackendVariable.varCref(var);
then
((var,(vars,cr::varsws,states,statesws,unfixed+1)));
// vars with fixed=false and without start value
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
false = BackendVariable.varFixed(var);
kind = BackendVariable.varKind(var);
BackendVariable.isVarKindVariable(kind);
cr = BackendVariable.varCref(var);
then
((var,(cr::vars,varsws,states,statesws,unfixed+1)));
// vars with no case print
case ((var,(vars,varsws,states,statesws,unfixed)))
equation
true = RTOpts.debugFlag("dumpInit");
cr = BackendVariable.varCref(var);
scr = ComponentReference.printComponentRefStr(cr);
s = stringAppendList({"countInitialVars: No case for ",scr,"\n"});
print(s);
then
((var,(vars,varsws,states,statesws,unfixed)));
case (inTpl) then inTpl;
end matchcontinue;
end countInitialVars;
protected function countInitialEqns
"autor: Frenkel TUD 2010-11"
input tuple<BackendDAE.Equation, tuple<Integer,list<BackendDAE.WhenClause>>> inTpl;
output tuple<BackendDAE.Equation, tuple<Integer,list<BackendDAE.WhenClause>>> outTpl;
algorithm
outTpl:=
matchcontinue (inTpl)
local
BackendDAE.Equation e;
BackendDAE.WhenEquation weqn;
list<BackendDAE.WhenClause> whenClauseLst;
Integer nie;
// only when eqns with initial()
case ((e as BackendDAE.WHEN_EQUATION(whenEquation=weqn),(nie,whenClauseLst)))
equation
then ((e,(nie,whenClauseLst)));
case ((e,(nie,whenClauseLst)))
then ((e,(nie+1,whenClauseLst)));
end matchcontinue;
end countInitialEqns;
protected function fixInitalVars"function: fixInitalVars
author: Frenkel TUD 2010-12"
input Integer inUnfixed;
input Integer inInitialEqns;
input BackendDAE.Variables inVariables;
input BackendDAE.Variables inKnVariables;
input list<DAE.ComponentRef> inVars;
input list<DAE.ComponentRef> inVarsWS;
input list<DAE.ComponentRef> inStates;
input list<DAE.ComponentRef> inStatesWS;
output BackendDAE.Variables outVariables;
output BackendDAE.Variables outKnVariables;
algorithm
(outVariables,outKnVariables) := matchcontinue (inUnfixed,inInitialEqns,inVariables,inKnVariables,inVars,inVarsWS,inStates,inStatesWS)
local
BackendDAE.Variables variables,variables1,knvariables1;
list<DAE.ComponentRef> vars,varsws,states,statesws;
DAE.ComponentRef cr;
BackendDAE.Var var,var1;
Integer unfixed,ine;
case (unfixed,ine,inVariables,inKnVariables,{},{},{},{})
then
(inVariables,inKnVariables);
// unfixed equal equations
case (unfixed,ine,inVariables,inKnVariables,_,_,_,_)
equation
true = intEq(unfixed,ine);
then
(inVariables,inKnVariables);
// first states with start value
case (unfixed,ine,inVariables,inKnVariables,vars,varsws,states,cr::statesws)
equation
// get Var
((var :: _),_) = BackendVariable.getVar(cr, inVariables);
// add Warning
warningInitialSystem(cr,false);
// set fixed=true
var1 = BackendVariable.setVarFixed(var,true);
// update variables
variables = BackendVariable.addVar(var1,inVariables);
(variables1,knvariables1) = fixInitalVars(inUnfixed-1,inInitialEqns,variables,inKnVariables,vars,varsws,states,statesws);
then
(variables1,knvariables1);
// then states
case (unfixed,ine,inVariables,inKnVariables,vars,varsws,cr::states,statesws)
equation
// get Var
((var :: _),_) = BackendVariable.getVar(cr, inVariables);
// add Warning
warningInitialSystem(cr,true);
// set fixed=true
var1 = BackendVariable.setVarFixed(var,true);
var1 = BackendVariable.setVarStartValue(var1,DAE.RCONST(0.0));
// update variables
variables = BackendVariable.addVar(var1,inVariables);
(variables1,knvariables1) = fixInitalVars(inUnfixed-1,inInitialEqns,variables,inKnVariables,vars,varsws,states,statesws);
then
(variables1,knvariables1);
// then variables with start value
case (unfixed,ine,inVariables,inKnVariables,vars,cr::varsws,states,statesws)
equation
// get Var
((var :: _),_) = BackendVariable.getVar(cr, inVariables);
// add Warning
warningInitialSystem(cr,false);
// set fixed=true
var1 = BackendVariable.setVarFixed(var,false);
// update variables
variables = BackendVariable.addVar(var1,inVariables);
(variables1,knvariables1) = fixInitalVars(inUnfixed-1,inInitialEqns,variables,inKnVariables,vars,varsws,states,statesws);
then
(variables1,knvariables1);
case (unfixed,ine,inVariables,inKnVariables,vars,cr::varsws,states,statesws)
equation
// get Var
((var :: _),_) = BackendVariable.getVar(cr, inKnVariables);
// add Warning
warningInitialSystem(cr,false);
// set fixed=true
var1 = BackendVariable.setVarFixed(var,false);
// update variables
variables = BackendVariable.addVar(var1,inKnVariables);
(variables1,knvariables1) = fixInitalVars(inUnfixed-1,inInitialEqns,inVariables,variables,vars,varsws,states,statesws);
then
(variables1,knvariables1);
// then variables
case (unfixed,ine,inVariables,inKnVariables,cr::vars,varsws,states,statesws)
equation
// get Var
((var :: _),_) = BackendVariable.getVar(cr, inVariables);
// add Warning
warningInitialSystem(cr,false);
// set fixed=true
var1 = BackendVariable.setVarFixed(var,false);
// update variables
variables = BackendVariable.addVar(var1,inVariables);
(variables1,knvariables1) = fixInitalVars(inUnfixed-1,inInitialEqns,variables,inKnVariables,vars,varsws,states,statesws);
then
(variables1,knvariables1);
case (unfixed,ine,inVariables,inKnVariables,cr::vars,varsws,states,statesws)
equation
// get Var
((var :: _),_) = BackendVariable.getVar(cr, inKnVariables);
// add Warning
warningInitialSystem(cr,true);
// set fixed=true
var1 = BackendVariable.setVarFixed(var,false);
// update variables
variables = BackendVariable.addVar(var1,inKnVariables);
(variables1,knvariables1) = fixInitalVars(inUnfixed-1,inInitialEqns,inVariables,variables,vars,varsws,states,statesws);
then
(variables1,knvariables1);
case (_,_,inVariables,inKnVariables,_,_,_,_)
equation
true = RTOpts.debugFlag("dumpInit");
print("- BackendDAEUtil.fixInitalVars failed\n");
then
(inVariables,inKnVariables);
case (_,_,inVariables,inKnVariables,_,_,_,_)
then
(inVariables,inKnVariables);
end matchcontinue;
end fixInitalVars;
protected function warningInitialSystem
input DAE.ComponentRef cr;
input Boolean flag;
algorithm
_ :=
matchcontinue (cr,flag)
local
String scr,s;
case (cr,false)
equation
true = RTOpts.debugFlag("dumpInit");
scr = ComponentReference.printComponentRefStr(cr);
s = stringAppendList({"Set ",scr," fixed=true.\n"});
print(s);
then
();
case (cr,true)
equation
true = RTOpts.debugFlag("dumpInit");
scr = ComponentReference.printComponentRefStr(cr);
s = stringAppendList({"Set ",scr," fixed=true.\n"});
s = stringAppendList({s, "Set default start value = 0.0.\n"});
print(s);
then
();
case (_,_) then ();
end matchcontinue;
end warningInitialSystem;
/************************************************************
Util function at Backend using for lowering and other stuff
************************************************************/
public function translateDae "function: translateDae
author: PA
Translates the dae so variables are indexed into different arrays:
- x for states
- xd for derivatives
- y_real for algebraic variables
- p_real for parameters
- y_int for algebraic variables
- p_int for parameters
- y_bool for algebraic variables
- p_bool for parameters
- y_str for algebraic variables
- p_str for parameters
"
input BackendDAE.BackendDAE inBackendDAE;
input Option<String> dummy;
output BackendDAE.BackendDAE outBackendDAE;
algorithm
outBackendDAE := match (inBackendDAE,dummy)
local
list<list<BackendDAE.Var>> varlst;
list<BackendDAE.Var> knvarlst,extvarlst;
array<BackendDAE.MultiDimEquation> ae;
array<DAE.Algorithm> al;
list<BackendDAE.WhenClause> wc;
list<BackendDAE.ZeroCrossing> zc;
BackendDAE.Variables vars, knvars, extVars;
BackendDAE.AliasVariables av;
BackendDAE.EquationArray eqns,seqns,ieqns;
BackendDAE.BackendDAE trans_dae;
BackendDAE.ExternalObjectClasses extObjCls;
Option<BackendDAE.IncidenceMatrix> m,mT;
BackendDAE.BackendDAEType btp;
BackendDAE.Matching matching;
BackendDAE.EqSystems systs;
case (BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,BackendDAE.EVENT_INFO(whenClauseLst = wc,zeroCrossingLst = zc),extObjCls,btp)),_)
equation
varlst = List.mapMap(systs,BackendVariable.daeVars,varList);
knvarlst = varList(knvars);
extvarlst = varList(extVars);
// varlst = Util.listMap(varlst,listReverse);
// knvarlst = listReverse(knvarlst);
// extvarlst = listReverse(extvarlst);
(varlst,knvarlst,extvarlst) = BackendVariable.calculateIndexes(varlst, knvarlst,extvarlst);
systs = List.threadMap(systs,varlst,addVarsToEqSystem);
knvars = BackendVariable.addVars(knvarlst, knvars);
extVars = BackendVariable.addVars(extvarlst, extVars);
then
BackendDAE.DAE(systs,BackendDAE.SHARED(knvars,extVars,av,ieqns,seqns,ae,al,BackendDAE.EVENT_INFO(wc,zc),extObjCls,btp));
end match;
end translateDae;
protected function addVarsToEqSystem
input BackendDAE.EqSystem syst;
input list<BackendDAE.Var> varlst;
output BackendDAE.EqSystem osyst;
algorithm
osyst := match (syst,varlst)
local
BackendDAE.Variables vars;
BackendDAE.EquationArray eqs;
Option<BackendDAE.IncidenceMatrix> m,mT;
BackendDAE.Matching matching;
case (BackendDAE.EQSYSTEM(vars, eqs, m, mT, matching),varlst)
equation
vars = BackendVariable.addVars(varlst, vars);
then BackendDAE.EQSYSTEM(vars, eqs, m, mT, matching);
end match;
end addVarsToEqSystem;
public function calculateSizes "function: calculateSizes
author: PA
Calculates the number of state variables, nx,
the number of algebraic variables, ny
and the number of parameters/constants, np.
inputs: BackendDAE
outputs: (int, /* nx */
int, /* ny */
int, /* np */
int /* ng */
int) next"
input BackendDAE.BackendDAE inBackendDAE;
output Integer outnx "number of states";
output Integer outny "number of alg. vars";
output Integer outnp "number of parameters";
output Integer outng "number of zerocrossings";
output Integer outng_sample "number of zerocrossings that are samples";
output Integer outnext "number of external objects";
// nx cannot be strings
output Integer outny_string "number of alg.vars which are strings";
output Integer outnp_string "number of parameters which are strings";
// nx cannot be int
output Integer outny_int "number of alg.vars which are ints";
output Integer outnp_int "number of parameters which are ints";
// nx cannot be int
output Integer outny_bool "number of alg.vars which are bools";
output Integer outnp_bool "number of parameters which are bools";
algorithm
(outnx,outny,outnp,outng,outng_sample,outnext, outny_string, outnp_string, outny_int, outnp_int, outny_bool, outnp_bool):=
match (inBackendDAE)
local
BackendDAE.Value np,ng,nsam,nx,ny,nx_1,ny_1,next,ny_string,np_string,ny_1_string,np_int,np_bool,ny_int,ny_1_int,ny_bool,ny_1_bool;
BackendDAE.Variables vars,knvars,extvars;
list<BackendDAE.WhenClause> wc;
list<BackendDAE.ZeroCrossing> zc;
case (BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(orderedVars = vars)::{},shared=BackendDAE.SHARED(knownVars = knvars, externalObjects = extvars,
eventInfo = BackendDAE.EVENT_INFO(whenClauseLst = wc,
zeroCrossingLst = zc))))
equation
// input variables are put in the known var list, but they should be counted by the ny counter
next = BackendVariable.varsSize(extvars);
((np,np_string,np_int, np_bool)) = BackendVariable.traverseBackendDAEVars(knvars,calculateParamSizes,(0,0,0,0));
(ng,nsam) = calculateNumberZeroCrossings(zc, 0, 0);
((nx,ny,ny_string,ny_int, ny_bool)) = BackendVariable.traverseBackendDAEVars(vars,calculateVarSizes,(0, 0, 0, 0, 0));
((nx_1,ny_1,ny_1_string,ny_1_int, ny_1_bool)) = BackendVariable.traverseBackendDAEVars(knvars,calculateVarSizes,(nx, ny, ny_string, ny_int, ny_bool));
then
(nx_1,ny_1,np,ng,nsam,next,ny_1_string, np_string, ny_1_int, np_int, ny_1_bool, np_bool);
end match;
end calculateSizes;
public function numberOfZeroCrossings "function: numberOfZeroCrossings
author: Frenkel TUD"
input BackendDAE.BackendDAE inBackendDAE;
output Integer outng "number of zerocrossings";
output Integer outng_sample "number of zerocrossings that are samples";
algorithm
(outng,outng_sample):=
match (inBackendDAE)