/
BackendDAEUtil.mo
8312 lines (7627 loc) · 310 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-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 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 Absyn;
public import BackendDAE;
public import BackendDAEFunc;
public import DAE;
public import FCore;
public import Util;
protected import Algorithm;
protected import Array;
protected import BackendDAEOptimize;
protected import BackendDAETransform;
protected import BackendDump;
protected import BackendEquation;
protected import BackendInline;
protected import BackendVariable;
protected import BackendVarTransform;
protected import BinaryTree;
protected import Causalize;
protected import Ceval;
protected import CheckModel;
protected import ClassInf;
protected import ClockIndexes;
protected import CommonSubExpression;
protected import ComponentReference;
protected import Config;
protected import DAEDump;
protected import DAEUtil;
protected import Debug;
protected import Differentiate;
protected import DumpGraphML;
protected import DynamicOptimization;
protected import Error;
protected import EvaluateFunctions;
protected import EvaluateParameter;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import ExpressionSolve;
protected import FindZeroCrossings;
protected import Flags;
protected import Global;
protected import HpcOmEqSystems;
protected import HpcOmTaskGraph;
protected import IndexReduction;
protected import Initialization;
protected import Inline;
protected import InlineArrayEquations;
protected import List;
protected import Matching;
protected import OnRelaxation;
protected import RemoveSimpleEquations;
protected import ResolveLoops;
protected import SCode;
protected import SimCodeUtil;
protected import StateMachineFeatures;
protected import SymbolicJacobian;
protected import SynchronousFeatures;
protected import System;
protected import Tearing;
protected import Types;
protected import UnitCheck;
protected import Values;
protected import XMLDump;
protected
type Var = BackendDAE.Var;
type VarKind = BackendDAE.VarKind;
type VariableArray = BackendDAE.VariableArray;
type ExternalObjectClasses = BackendDAE.ExternalObjectClasses;
type BackendDAEType = BackendDAE.BackendDAEType;
type SymbolicJacobians = BackendDAE.SymbolicJacobians;
type EqSystems = BackendDAE.EqSystems;
type WhenClause = BackendDAE.WhenClause;
type ZeroCrossing = BackendDAE.ZeroCrossing;
public function isInitializationDAE
input BackendDAE.Shared inShared;
output Boolean res;
algorithm
res := match(inShared)
case (BackendDAE.SHARED(backendDAEType=BackendDAE.INITIALSYSTEM())) then true;
else false;
end match;
end isInitializationDAE;
public function isSimulationDAE
input BackendDAE.Shared inShared;
output Boolean res;
algorithm
res := match(inShared)
case (BackendDAE.SHARED(backendDAEType=BackendDAE.SIMULATION())) then true;
else false;
end match;
end isSimulationDAE;
/*************************************************
* checkBackendDAE and stuff
************************************************/
public 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
Integer nVars, nEqns;
Boolean samesize;
BackendDAE.Variables vars;
case (_) equation
false = Flags.isSet(Flags.CHECK_BACKEND_DAE);
then ();
case (BackendDAE.DAE(eqs=BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=BackendDAE.EQUATION_ARRAY(size=nEqns))::{})) equation
//true = Flags.isSet(Flags.CHECK_BACKEND_DAE);
//Check for correct size
nVars = BackendVariable.varsSize(vars);
samesize = nVars == nEqns;
if Flags.isSet(Flags.CHECK_BACKEND_DAE) then
print("No. of Equations: " + intString(nVars) + " No. of BackendDAE.Variables: " + intString(nEqns) + " Samesize: " + boolString(samesize) + "\n");
end if;
(expCrefs, wrongEqns) = checkBackendDAE(inBackendDAE);
printcheckBackendDAEWithErrorMsg(expCrefs, wrongEqns);
then ();
else equation
Error.addMessage(Error.INTERNAL_ERROR, {"./Compiler/BackEnd/BackendDAEUtil.mo: function checkBackendDAEWithErrorMsg failed"});
then fail();
end matchcontinue;
end checkBackendDAEWithErrorMsg;
public function printcheckBackendDAEWithErrorMsg"author: Frenkel TUD
helper for checkDEALowWithErrorMsg"
input list<tuple<DAE.Exp,list<DAE.ComponentRef>>> inExpCrefs;
input list<BackendDAE.Equation> inWrongEqns;
algorithm
_ := match (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;
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 match;
end printcheckBackendDAEWithErrorMsg;
protected 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.Type t1,t2;
String eqnstr, t1str, t2str, tstr;
DAE.ElementSource source;
case (eqn as BackendDAE.EQUATION(exp=e1,scalar=e2,source=source))
equation
eqnstr = BackendDump.equationString(eqn);
t1 = Expression.typeof(e1);
t2 = Expression.typeof(e2);
t1str = Types.unparseTypeNoAttr(t1);
t2str = Types.unparseTypeNoAttr(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.equationString(eqn);
t1 = Expression.typeof(e1);
t2 = ComponentReference.crefLastType(cr);
t1str = Types.unparseTypeNoAttr(t1);
t2str = Types.unparseTypeNoAttr(t2);
tstr = stringAppendList({t1str," != ", t2str});
Error.addSourceMessage(Error.EQUATION_TYPE_MISMATCH_ERROR, {eqnstr,tstr}, DAEUtil.getElementSourceFileInfo(source));
then ();
//
else ();
end matchcontinue;
end printEqnSizeError;
public 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;
list<BackendDAE.WhenClause> whenClauseLst;
list<BackendDAE.Var> varlst1,varlst2,allvarslst;
list<tuple<DAE.Exp,list<DAE.ComponentRef>>> expcrefs,expcrefs1,expcrefs2,expcrefs3,expcrefs4,expcrefs5;
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,
eventInfo = BackendDAE.EVENT_INFO(whenClauseLst=whenClauseLst))))
equation
allvars = BackendVariable.mergeVariables(vars1, vars2);
((_,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)) = BackendDAETransform.traverseBackendDAEExpsWhenClauseLst(whenClauseLst,checkBackendDAEExp,(allvars,expcrefs4));
wrongEqns = BackendEquation.traverseEquationArray(eqns,checkEquationSize,{});
wrongEqns1 = BackendEquation.traverseEquationArray(reqns,checkEquationSize,wrongEqns);
wrongEqns2 = BackendEquation.traverseEquationArray(ieqns,checkEquationSize,wrongEqns1);
then
(expcrefs5,wrongEqns2);
else
equation
true = Flags.isSet(Flags.FAILTRACE);
Debug.trace("- BackendDAEUtil.checkBackendDAE failed\n");
then
fail();
end matchcontinue;
end checkBackendDAE;
protected function checkBackendDAEExp
input DAE.Exp inExp;
input tuple<BackendDAE.Variables,list<tuple<DAE.Exp,list<DAE.ComponentRef>>>> inTpl;
output DAE.Exp outExp;
output tuple<BackendDAE.Variables,list<tuple<DAE.Exp,list<DAE.ComponentRef>>>> outTpl;
algorithm
(outExp,outTpl) := matchcontinue (inExp,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.traverseExpBottomUp(exp,traversecheckBackendDAEExp,(vars,{}));
lstExpCrefs1 = if not listEmpty(crefs) then (exp,crefs)::lstExpCrefs else lstExpCrefs;
then (exp,(vars,lstExpCrefs1));
else (inExp,inTpl);
end matchcontinue;
end checkBackendDAEExp;
protected function traversecheckBackendDAEExp
input DAE.Exp inExp;
input tuple<BackendDAE.Variables,list<DAE.ComponentRef>> inTuple;
output DAE.Exp outExp;
output tuple<BackendDAE.Variables,list<DAE.ComponentRef>> outTuple;
algorithm
(outExp,outTuple) := matchcontinue (inExp,inTuple)
local
DAE.Exp e,e1;
BackendDAE.Variables vars,vars1;
DAE.ComponentRef cr;
list<DAE.ComponentRef> crefs,crefs1;
list<DAE.Exp> expl;
list<DAE.Var> varLst;
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.T_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.T_ARRAY()),(vars,crefs))
equation
(e1,true) = Expression.extendArrExp(e,false);
(_,(vars1,crefs1)) = Expression.traverseExpBottomUp(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.T_FUNCTION_REFERENCE_FUNC()),(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));
else (inExp,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.T_INTEGER_DEFAULT,{});
backendVar := BackendDAE.VAR(cr, BackendDAE.VARIABLE(), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_INTEGER_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
end makeIterVariable;
protected function checkEquationSize"author: Frenkel TUD 2010-12
- check if the left hand side and the rigth hand side have equal types."
input BackendDAE.Equation inEq;
input list<BackendDAE.Equation> inEqs;
output BackendDAE.Equation outEq;
output list<BackendDAE.Equation> outEqs;
algorithm
(outEq,outEqs) := matchcontinue (inEq,inEqs)
local
BackendDAE.Equation e;
list<BackendDAE.Equation> wrongEqns,wrongEqns1;
DAE.Exp e1, e2;
DAE.ComponentRef cr;
DAE.Type 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);
//
else (inEq,inEqs);
end matchcontinue;
end checkEquationSize;
public function checkAssertCondition "Succeds if condition of assert is not constant false"
input DAE.Exp cond;
input DAE.Exp message;
input DAE.Exp level;
input SourceInfo info;
algorithm
_ := matchcontinue(cond,message,level,info)
local
String messageStr;
case(_, _, _,_)
equation
// Don't check assertions when checking models
true = Flags.getConfigBool(Flags.CHECK_MODEL);
then ();
case (_,_,_,_)
equation
false = Expression.isConstFalse(cond);
then ();
case (_,_,_,_)
equation
failure(DAE.ENUM_LITERAL(index=1) = level);
then ();
case(_,_,_,_)
equation
true = Expression.isConstFalse(cond);
messageStr = ExpressionDump.printExpStr(message);
Error.addSourceMessage(Error.ASSERT_CONSTANT_FALSE_ERROR,{messageStr},info);
then fail();
end matchcontinue;
end checkAssertCondition;
// =============================================================================
// Util function at Backend using for lowering and other stuff
//
// =============================================================================
public function copyBackendDAE "author: Frenkel TUD, wbraun
Copy the dae to avoid changes in vectors."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
outDAE := mapEqSystem(inDAE, copyEqSystemAndShared);
end copyBackendDAE;
public function copyEqSystemAndShared
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
output BackendDAE.EqSystem outSystem;
output BackendDAE.Shared outShared;
algorithm
outSystem := copyEqSystem(inSystem);
outShared := copyBackendDAEShared(inShared);
end copyEqSystemAndShared;
public function copyEqSystem
input BackendDAE.EqSystem inSystem;
output BackendDAE.EqSystem outSystem;
protected
BackendDAE.Variables ordvars, ordvars1;
BackendDAE.EquationArray eqns, eqns1;
Option<BackendDAE.IncidenceMatrix> m, mT, m1, mT1;
BackendDAE.Matching matching, matching1;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
algorithm
BackendDAE.EQSYSTEM(ordvars, eqns, m, mT, matching, stateSets, partitionKind) := inSystem;
ordvars1 := BackendVariable.copyVariables(ordvars);
eqns1 := BackendEquation.copyEquationArray(eqns);
m1 := copyIncidenceMatrix(m);
mT1 := copyIncidenceMatrix(mT);
matching1 := copyMatching(matching);
outSystem := BackendDAE.EQSYSTEM(ordvars1, eqns1, m1, mT1, matching1, stateSets, partitionKind);
end copyEqSystem;
public function copyBackendDAEShared
" author: Frenkel TUD, wbraun
Copy the shared part of an BackendDAE to avoid changes in
vectors."
input BackendDAE.Shared inShared;
output BackendDAE.Shared outShared;
algorithm
outShared:=
match (inShared)
local
BackendDAE.Variables knvars,exobj,knvars1,exobj1,av;
BackendDAE.EquationArray remeqns,inieqns,remeqns1,inieqns1;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
FCore.Cache cache;
FCore.Graph graph;
DAE.FunctionTree funcTree;
BackendDAE.EventInfo einfo;
ExternalObjectClasses eoc;
BackendDAEType btp;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.ExtraInfo ei;
case (BackendDAE.SHARED(knvars,exobj,av,inieqns,remeqns,constrs,clsAttrs,cache,graph,funcTree,einfo,eoc,btp,symjacs,ei))
equation
knvars1 = BackendVariable.copyVariables(knvars);
_ = BackendVariable.copyVariables(exobj);
inieqns1 = BackendEquation.copyEquationArray(inieqns);
remeqns1 = BackendEquation.copyEquationArray(remeqns);
then
BackendDAE.SHARED(knvars1,exobj,av,inieqns1,remeqns1,constrs,clsAttrs,cache,graph,funcTree,einfo,eoc,btp,symjacs,ei);
end match;
end copyBackendDAEShared;
public function copyMatching
input BackendDAE.Matching inMatching;
output BackendDAE.Matching outMatching;
algorithm
outMatching := match (inMatching)
local
array<Integer> ass1, cass1, ass2, cass2;
BackendDAE.StrongComponents comps;
case (BackendDAE.NO_MATCHING()) then BackendDAE.NO_MATCHING();
case (BackendDAE.MATCHING(ass1=ass1,ass2=ass2,comps=comps))
equation
cass1 = arrayCopy(ass1);
cass2 = arrayCopy(ass2);
then BackendDAE.MATCHING(cass1,cass2,comps);
end match;
end copyMatching;
public function addBackendDAEKnVars
" That function replace the KnownVars in BackendDAE.
author: wbraun"
input BackendDAE.Variables inKnVars;
input BackendDAE.BackendDAE inBDAE;
output BackendDAE.BackendDAE outBDAE;
protected
BackendDAE.Variables exobj,av;
BackendDAE.EquationArray remeqns,inieqns;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
FCore.Cache cache;
FCore.Graph graph;
DAE.FunctionTree funcTree;
BackendDAE.EventInfo einfo;
ExternalObjectClasses eoc;
BackendDAEType btp;
BackendDAE.SymbolicJacobians symjacs;
EqSystems eqs;
BackendDAE.ExtraInfo ei;
algorithm
BackendDAE.DAE(eqs=eqs, shared=BackendDAE.SHARED(_, exobj, av, inieqns, remeqns, constrs, clsAttrs, cache, graph, funcTree, einfo, eoc, btp, symjacs, ei)) := inBDAE;
outBDAE := BackendDAE.DAE(eqs, BackendDAE.SHARED(inKnVars, exobj, av, inieqns, remeqns, constrs, clsAttrs, cache, graph, funcTree, einfo, eoc, btp, symjacs, ei));
end addBackendDAEKnVars;
public function addBackendDAEFunctionTree
" That function replace the FunctionTree in BackendDAE.
author: wbraun"
input DAE.FunctionTree inFunctionTree;
input BackendDAE.BackendDAE inBDAE;
output BackendDAE.BackendDAE outBDAE;
algorithm
outBDAE := match (inFunctionTree, inBDAE)
local
BackendDAE.Variables knvars,exobj,av;
BackendDAE.EquationArray remeqns,inieqns;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
FCore.Cache cache;
FCore.Graph graph;
BackendDAE.EventInfo einfo;
ExternalObjectClasses eoc;
BackendDAEType btp;
BackendDAE.SymbolicJacobians symjacs;
EqSystems eqs;
BackendDAE.ExtraInfo ei;
case (_,BackendDAE.DAE(eqs=eqs,shared=BackendDAE.SHARED(knvars,exobj,av,inieqns,remeqns,constrs,clsAttrs,cache,graph,_,einfo,eoc,btp,symjacs,ei)))
then BackendDAE.DAE(eqs,BackendDAE.SHARED(knvars,exobj,av,inieqns,remeqns,constrs,clsAttrs,cache,graph,inFunctionTree,einfo,eoc,btp,symjacs,ei));
end match;
end addBackendDAEFunctionTree;
public function addFunctionTree
" function: addBackendDAEFunctionTree
That function replace the FunctionTree in shared object.
author: wbraun"
input DAE.FunctionTree inFunctionTree;
input BackendDAE.Shared inShared;
output BackendDAE.Shared outShared;
algorithm
outShared := match (inFunctionTree, inShared)
local
BackendDAE.Variables knvars,exobj,av;
BackendDAE.EquationArray remeqns,inieqns;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
FCore.Cache cache;
FCore.Graph graph;
BackendDAE.EventInfo einfo;
ExternalObjectClasses eoc;
BackendDAEType btp;
BackendDAE.SymbolicJacobians symjacs;
EqSystems eqs;
BackendDAE.ExtraInfo ei;
case (_,BackendDAE.SHARED(knvars,exobj,av,inieqns,remeqns,constrs,clsAttrs,cache,graph,_,einfo,eoc,btp,symjacs,ei))
then BackendDAE.SHARED(knvars,exobj,av,inieqns,remeqns,constrs,clsAttrs,cache,graph,inFunctionTree,einfo,eoc,btp,symjacs,ei);
end match;
end addFunctionTree;
public function addVarsToEqSystem
input BackendDAE.EqSystem syst;
input list<BackendDAE.Var> varlst;
output BackendDAE.EqSystem osyst;
protected
BackendDAE.Variables vars;
BackendDAE.EquationArray eqs;
Option<BackendDAE.IncidenceMatrix> m,mT;
BackendDAE.Matching matching;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
algorithm
BackendDAE.EQSYSTEM(vars, eqs, m, mT, matching, stateSets, partitionKind) := syst;
vars := BackendVariable.addVars(varlst, vars);
osyst := BackendDAE.EQSYSTEM(vars, eqs, m, mT, matching, stateSets, partitionKind);
end addVarsToEqSystem;
public function addDummyStateIfNeeded
"author: Frenkel TUD 2012-09
adds a dummy state if dae contains no states"
input BackendDAE.BackendDAE inBackendDAE;
output BackendDAE.BackendDAE outBackendDAE;
protected
BackendDAE.EqSystems systs;
BackendDAE.Shared shared;
Boolean daeContainsNoStates;
algorithm
BackendDAE.DAE(eqs=systs,shared=shared) := inBackendDAE;
// check if the DAE has states
daeContainsNoStates := addDummyStateIfNeeded1(systs);
// adrpo: add the dummy derivative state ONLY IF the DAE contains no states
systs := if daeContainsNoStates then addDummyState(systs) else systs;
outBackendDAE := if daeContainsNoStates then BackendDAE.DAE(systs,shared) else inBackendDAE;
end addDummyStateIfNeeded;
protected function addDummyStateIfNeeded1
input BackendDAE.EqSystems iSysts;
output Boolean oContainsNoStates;
algorithm
oContainsNoStates := match(iSysts)
local
BackendDAE.EqSystems systs;
BackendDAE.Variables vars;
Boolean containsNoStates;
case ({}) then true;
case (BackendDAE.EQSYSTEM(orderedVars = vars)::systs)
equation
containsNoStates = BackendVariable.traverseBackendDAEVarsWithStop(vars, traverserVaraddDummyStateIfNeeded, true);
containsNoStates = if containsNoStates then addDummyStateIfNeeded1(systs) else containsNoStates;
then
containsNoStates;
end match;
end addDummyStateIfNeeded1;
protected function traverserVaraddDummyStateIfNeeded
input BackendDAE.Var v;
input Boolean b;
output BackendDAE.Var ov;
output Boolean cont;
output Boolean ob;
algorithm
(ov,cont,ob) := match (v,b)
case (BackendDAE.VAR(varKind=BackendDAE.STATE()),_)
then (v,false,false);
case (_,_) then (v,b,b);
end match;
end traverserVaraddDummyStateIfNeeded;
protected function addDummyState
"In order for the solver to work correctly at least one state variable
must exist in the equation system. This function therefore adds a
dummy state variable and an equation for that variable."
input BackendDAE.EqSystems isysts;
output BackendDAE.EqSystems osysts;
protected
DAE.ComponentRef cr;
BackendDAE.Var v;
BackendDAE.Variables vars;
DAE.Exp exp;
BackendDAE.Equation eqn;
BackendDAE.EquationArray eqns;
array<Integer> ass;
BackendDAE.EqSystem syst;
algorithm
// generate dummy state
(v,cr) := BackendVariable.createDummyVar();
// generate vars
vars := BackendVariable.listVar({v});
/*
* adrpo: after a bit of talk with Francesco Casella & Peter Aronsson we will add der($dummy) = 0;
*/
exp := Expression.crefExp(cr);
eqn := BackendDAE.EQUATION(DAE.CALL(Absyn.IDENT("der"),{exp},DAE.callAttrBuiltinReal),DAE.RCONST(0.0), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);
eqns := BackendEquation.listEquation({eqn});
// generate equationsystem
ass := arrayCreate(1, 1);
syst := BackendDAE.EQSYSTEM(vars,eqns,NONE(),NONE(),BackendDAE.MATCHING(ass,ass,{BackendDAE.SINGLEEQUATION(1,1)}),{},BackendDAE.UNSPECIFIED_PARTITION());
// add system to list of systems
osysts := syst::isysts;
end addDummyState;
public function numberOfZeroCrossings "author: lochel"
input BackendDAE.BackendDAE inBackendDAE;
output Integer outNumZeroCrossings "number of ordinary zerocrossings" ;
output Integer outNumTimeEvents "number of zerocrossings that are time events" ;
output Integer outNumRelations;
output Integer outNumMathEventFunctions;
protected
list<BackendDAE.TimeEvent> timeEvents;
list<ZeroCrossing> zeroCrossingLst, relationsLst;
algorithm
BackendDAE.DAE(shared=BackendDAE.SHARED(eventInfo=BackendDAE.EVENT_INFO(timeEvents=timeEvents,
zeroCrossingLst=zeroCrossingLst,
relationsLst=relationsLst,
numberMathEvents=outNumMathEventFunctions))) := inBackendDAE;
outNumZeroCrossings := listLength(zeroCrossingLst);
outNumTimeEvents := listLength(timeEvents);
outNumRelations := listLength(relationsLst);
end numberOfZeroCrossings;
public function numberOfDiscreteVars "author: lochel"
input BackendDAE.BackendDAE inBackendDAE;
output Integer outNumDiscreteReal;
algorithm
outNumDiscreteReal := countDiscreteVars(inBackendDAE);
end numberOfDiscreteVars;
protected function countDiscreteVars "author: lochel"
input BackendDAE.BackendDAE inDAE;
output Integer outNumDiscreteVars;
protected
BackendDAE.EqSystems systs;
BackendDAE.Variables knownVars, alias;
algorithm
BackendDAE.DAE(eqs = systs, shared = BackendDAE.SHARED(knownVars=knownVars, aliasVars=alias)) := inDAE;
outNumDiscreteVars := countDiscreteVars1(systs);
outNumDiscreteVars := BackendVariable.traverseBackendDAEVars(knownVars, countDiscreteVars3, outNumDiscreteVars);
outNumDiscreteVars := BackendVariable.traverseBackendDAEVars(alias, countDiscreteVars3, outNumDiscreteVars);
end countDiscreteVars;
protected function countDiscreteVars1 "author: lochel"
input BackendDAE.EqSystems inEqSystems;
output Integer outNumDiscreteVars;
algorithm
outNumDiscreteVars := 0;
outNumDiscreteVars := List.fold(inEqSystems, countDiscreteVars2, outNumDiscreteVars);
end countDiscreteVars1;
protected function countDiscreteVars2 "author: lochel"
input BackendDAE.EqSystem inEqSystem;
input Integer inNumDiscreteVars;
output Integer outNumDiscreteVars;
protected
BackendDAE.Variables vars;
algorithm
BackendDAE.EQSYSTEM(orderedVars=vars) := inEqSystem;
outNumDiscreteVars := BackendVariable.traverseBackendDAEVars(vars, countDiscreteVars3, inNumDiscreteVars);
end countDiscreteVars2;
protected function countDiscreteVars3 "author: lochel"
input BackendDAE.Var var;
input Integer nDiscreteVars;
output BackendDAE.Var outVar;
output Integer outCount;
algorithm
(outVar,outCount) := match (var,nDiscreteVars)
// discrete
case (BackendDAE.VAR(varKind=BackendDAE.DISCRETE(), varType=DAE.T_REAL()), _)
then (var, nDiscreteVars+1);
else (var,nDiscreteVars);
end match;
end countDiscreteVars3;
protected function calculateValues "author: PA
This function calculates the values from the parameter binding expressions.
modefication: wbraun
Use really only parameter bindungs for evaluation."
input BackendDAE.BackendDAE inBackendDAE;
output BackendDAE.BackendDAE outBackendDAE;
algorithm
outBackendDAE := match (inBackendDAE)
local
list<BackendDAE.Var> knvarlst,varlst1,varlst2;
BackendDAE.Variables knvars,extVars,paramvars,av;
BackendDAE.EquationArray seqns,ie;
list<DAE.Constraint> constrs;
list<DAE.ClassAttributes> clsAttrs;
FCore.Cache cache;
FCore.Graph graph;
DAE.FunctionTree funcs;
BackendDAE.EventInfo wc;
ExternalObjectClasses extObjCls;
EqSystems eqs;
BackendDAEType btp;
BackendDAE.SymbolicJacobians symjacs;
BackendDAE.ExtraInfo ei;
case (BackendDAE.DAE(eqs,BackendDAE.SHARED(knownVars = knvars,externalObjects=extVars,aliasVars = av,
initialEqs = ie,removedEqs = seqns, constraints = constrs,classAttrs = clsAttrs,
cache=cache,graph=graph, functionTree = funcs, eventInfo = wc, extObjClasses=extObjCls,
backendDAEType=btp, symjacs=symjacs, info=ei)))
algorithm
knvars := BackendVariable.traverseBackendDAEVarsWithUpdate(
knvars, function calculateValueTraverser(inEnv = graph), cache);
// Reverse the order of the known vars in the hashtable. This is stupid,
// but things break otherwise.
knvars := BackendVariable.listVar(BackendVariable.varList(knvars));
then
BackendDAE.DAE(eqs,BackendDAE.SHARED(knvars,extVars,av,ie,seqns,constrs,clsAttrs,cache,graph,funcs,wc,extObjCls,btp,symjacs,ei));
end match;
end calculateValues;
protected function calculateValueTraverser
input BackendDAE.Var inVar;
input FCore.Cache inCache;
input FCore.Graph inEnv;
output BackendDAE.Var outVar;
output FCore.Cache outCache = inCache;
algorithm
if BackendVariable.isParam(inVar) then
outVar := calculateValue(inVar, inCache, inEnv);
else
outVar := inVar;
end if;
end calculateValueTraverser;
protected function calculateValue
input BackendDAE.Var inVar;
input FCore.Cache cache;
input FCore.Graph graph;
output BackendDAE.Var outVar;
algorithm
outVar := matchcontinue(inVar)
local
Var var;
DAE.ComponentRef cr;
VarKind vk;
DAE.VarDirection vd;
DAE.VarParallelism prl;
BackendDAE.Type ty;
DAE.Exp e;
DAE.InstDims dims;
DAE.ElementSource src;
Option<DAE.VariableAttributes> va;
Option<BackendDAE.TearingSelect> ts;
Option<SCode.Comment> c;
DAE.ConnectorType ct;
Values.Value v;
DAE.VarInnerOuter io;
case BackendDAE.VAR(bindValue = SOME(_))
equation
print("*** Not Ceval.eval var: ");
BackendDump.printVar(inVar);
print("\n");
then
inVar;
case BackendDAE.VAR(varName = cr, varKind = vk, varDirection = vd, varParallelism = prl,
varType = ty, bindExp = SOME(e), arryDim = dims, source = src,
values = va, tearingSelectOption = ts, comment = c, connectorType = ct, innerOuter = io)
equation
// wbraun: Evaluate parameter expressions only if they are
// constant at compile time otherwise we solve them
// much faster at runtime.
//((e, _)) = Expression.traverseExpBottomUp(e, replaceCrefsWithValues, (vars, cr_orign));
true = Expression.isConst(e);
(_, v, _) = Ceval.ceval(cache, graph, e, false, NONE(), Absyn.NO_MSG(),0);
then
BackendDAE.VAR(cr, vk, vd, prl, ty, SOME(e), SOME(v), dims, src, va, ts, c, ct, io, false);
else inVar;
end matchcontinue;
end calculateValue;
public function replaceCrefsWithValues
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, DAE.ComponentRef> inTuple;
output DAE.Exp outExp;
output tuple<BackendDAE.Variables, DAE.ComponentRef> outTuple;
algorithm
(outExp,outTuple) := matchcontinue (inExp,inTuple)
local
DAE.Exp e;
BackendDAE.Variables vars;
DAE.ComponentRef cr, cr_orign;
case (DAE.CREF(cr, _), (vars, cr_orign))
equation
false = ComponentReference.crefEqualNoStringCompare(cr, cr_orign);
({BackendDAE.VAR(bindExp = SOME(e))}, _) = BackendVariable.getVar(cr, vars);
(e, _) = Expression.traverseExpBottomUp(e, replaceCrefsWithValues, (vars, cr_orign));
then (e, (vars,cr_orign));
else (inExp,inTuple);
end matchcontinue;
end replaceCrefsWithValues;
public function makeExpType
"Transforms a BackendDAE.Type to DAE.Type"
input BackendDAE.Type inType;
output DAE.Type outType;
algorithm
outType := inType;
end makeExpType;
public function isDiscreteEquation
input BackendDAE.Equation eqn;
input BackendDAE.Variables vars;
input BackendDAE.Variables knvars;
output Boolean b;
algorithm
b := matchcontinue(eqn)
local
DAE.Exp e1, e2;
DAE.ComponentRef cr;
list<DAE.Exp> expl;
list<DAE.Statement> stmts;
case BackendDAE.EQUATION(exp=e1, scalar=e2) equation
b = isDiscreteExp(e1, vars, knvars) and isDiscreteExp(e2, vars, knvars);
then b;
case BackendDAE.COMPLEX_EQUATION(left=e1, right=e2) equation
b = isDiscreteExp(e1, vars, knvars) and isDiscreteExp(e2, vars, knvars);
then b;
case BackendDAE.ARRAY_EQUATION(left=e1, right=e2) equation
b = isDiscreteExp(e1, vars, knvars) and isDiscreteExp(e2, vars, knvars);
then b;
case BackendDAE.SOLVED_EQUATION(componentRef=cr, exp=e2) equation
e1 = Expression.crefExp(cr);
b = isDiscreteExp(e1, vars, knvars) and isDiscreteExp(e2, vars, knvars);
then b;
case BackendDAE.RESIDUAL_EQUATION(exp=e1) equation
b = isDiscreteExp(e1, vars, knvars);
then b;
case BackendDAE.ALGORITHM(alg=DAE.ALGORITHM_STMTS(stmts)) equation
(_, (_, _, true)) = DAEUtil.traverseDAEEquationsStmts(stmts, isDiscreteExp1, (vars, knvars, false));
then true;
case BackendDAE.WHEN_EQUATION()
then true;
else false;
end matchcontinue;
end isDiscreteEquation;
public function isDiscreteExp "Returns true if expression is a discrete expression."
input DAE.Exp inExp;
input BackendDAE.Variables inVariables;
input BackendDAE.Variables inKnvars;
output Boolean outBoolean;
protected
Option<Boolean> obool;
algorithm
(_,(_, _, obool)) := Expression.traverseExpTopDown(inExp, traversingisDiscreteExpFinder, (inVariables, inKnvars, NONE()));
outBoolean := Util.getOptionOrDefault(obool, false);
end isDiscreteExp;
// Why is this so similar to isDiscreteExp? What is the difference?