Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit be2bc89

Browse files
Willi BraunOpenModelica-Hudson
authored andcommitted
[BE] Fixing initialization of algorithms
- added initial statements also to the initial system algorithms and initial algorithms. - improve expandAlgorithmStmts to handle array variables - fixes ticket:4568 Belonging to [master]: - #2249 - OpenModelica/OpenModelica-testsuite#866
1 parent f5c5629 commit be2bc89

File tree

4 files changed

+39
-26
lines changed

4 files changed

+39
-26
lines changed

Compiler/BackEnd/BackendDAEOptimize.mo

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3538,31 +3538,34 @@ public function addInitialStmtsToAlgorithms "
35383538
- A non-discrete variable is initialized with its start value (i.e. the value of the start-attribute).
35393539
- A discrete variable v is initialized with pre(v)."
35403540
input BackendDAE.BackendDAE inDAE;
3541+
input Boolean isInitialSystem;
35413542
output BackendDAE.BackendDAE outDAE;
35423543
algorithm
3543-
outDAE := BackendDAEUtil.mapEqSystem(inDAE, addInitialStmtsToAlgorithms1);
3544+
outDAE := BackendDAEUtil.mapEqSystem1(inDAE, addInitialStmtsToAlgorithms1, isInitialSystem);
35443545
end addInitialStmtsToAlgorithms;
35453546

35463547
protected function addInitialStmtsToAlgorithms1 "Helper function to addInitialStmtsToAlgorithms."
35473548
input BackendDAE.EqSystem syst;
3549+
input Boolean isInitialSystem;
35483550
input BackendDAE.Shared shared;
35493551
output BackendDAE.EqSystem osyst = syst;
35503552
output BackendDAE.Shared oshared = shared;
35513553
protected
3552-
BackendDAE.Variables ordvars;
3554+
BackendDAE.Variables ordvars, allVars;
35533555
BackendDAE.EquationArray ordeqns;
3556+
BackendDAE.EquationArray initEqns;
35543557
algorithm
35553558
BackendDAE.EQSYSTEM(orderedVars=ordvars, orderedEqs=ordeqns) := osyst;
3556-
BackendEquation.traverseEquationArray_WithUpdate(ordeqns, eaddInitialStmtsToAlgorithms1Helper, ordvars);
3559+
BackendEquation.traverseEquationArray_WithUpdate(ordeqns, eaddInitialStmtsToAlgorithms1Helper, (ordvars, isInitialSystem));
35573560
end addInitialStmtsToAlgorithms1;
35583561

35593562
protected function eaddInitialStmtsToAlgorithms1Helper "Helper function to addInitialStmtsToAlgorithms1."
35603563
input BackendDAE.Equation inEq;
3561-
input BackendDAE.Variables inVars;
3564+
input tuple<BackendDAE.Variables, Boolean> inTpl;
35623565
output BackendDAE.Equation outEq;
3563-
output BackendDAE.Variables outVars;
3566+
output tuple<BackendDAE.Variables, Boolean> outTpl = inTpl;
35643567
algorithm
3565-
(outEq,outVars) := match (inEq,inVars)
3568+
(outEq) := match (inEq, inTpl)
35663569
local
35673570
DAE.Algorithm alg;
35683571
list<DAE.Statement> statements;
@@ -3574,49 +3577,53 @@ algorithm
35743577
list<DAE.ComponentRef> crlst;
35753578
DAE.Expand crExpand;
35763579
BackendDAE.EquationAttributes attr;
3580+
Boolean isInitialEquations;
35773581

3578-
case (BackendDAE.ALGORITHM(size=size, alg=alg as DAE.ALGORITHM_STMTS(statements), source=source, expand=crExpand, attr=attr), vars)
3582+
case (BackendDAE.ALGORITHM(size=size, alg=alg as DAE.ALGORITHM_STMTS(statements), source=source, expand=crExpand, attr=attr), (vars, isInitialEquations))
35793583
equation
35803584
crlst = CheckModel.checkAndGetAlgorithmOutputs(alg, source, crExpand);
35813585
outputs = List.map(crlst, Expression.crefExp);
3582-
statements = expandAlgorithmStmts(statements, outputs, vars);
3583-
then (BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(statements), source, crExpand, attr), vars);
3586+
statements = expandAlgorithmStmts(statements, outputs, vars, isInitialEquations);
3587+
then BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(statements), source, crExpand, attr);
35843588

3585-
else (inEq,inVars);
3589+
else inEq;
35863590
end match;
35873591
end eaddInitialStmtsToAlgorithms1Helper;
35883592

35893593
protected function expandAlgorithmStmts "Helper function to eaddInitialStmtsToAlgorithms1Helper."
35903594
input list<DAE.Statement> inAlg;
35913595
input list<DAE.Exp> inOutputs;
35923596
input BackendDAE.Variables inVars;
3597+
input Boolean isInitialEquation;
35933598
output list<DAE.Statement> outAlg;
35943599
algorithm
35953600
outAlg := match(inAlg, inOutputs, inVars)
35963601
local
35973602
DAE.Exp out, initExp;
35983603
list<DAE.Exp> rest;
35993604
DAE.ComponentRef cref;
3600-
BackendDAE.Var var;
3605+
list<BackendDAE.Var> vars;
36013606
DAE.Statement stmt;
36023607
DAE.Type type_;
36033608
list<DAE.Statement> statements;
36043609

36053610
case(statements, {}, _)
36063611
then statements;
36073612

3608-
case(statements, out::rest, _) equation
3609-
cref = Expression.expCref(out);
3610-
type_ = Expression.typeof(out);
3611-
type_ = Expression.arrayEltType(type_);
3612-
(var::_, _) = BackendVariable.getVar(cref, inVars);
3613-
if BackendVariable.isVarDiscrete(var) then
3614-
initExp = Expression.makePureBuiltinCall("pre", {out}, type_);
3615-
else
3616-
initExp = Expression.crefExp(ComponentReference.crefPrefixStart(cref));
3617-
end if;
3618-
stmt = Algorithm.makeAssignment(DAE.CREF(cref, type_), DAE.PROP(type_, DAE.C_VAR()), initExp, DAE.PROP(type_, DAE.C_VAR()), DAE.dummyAttrVar, SCode.NON_INITIAL(), DAE.emptyElementSource);
3619-
then expandAlgorithmStmts(stmt::statements, rest, inVars);
3613+
case(statements, out::rest, _) algorithm
3614+
cref := Expression.expCref(out);
3615+
(vars, _) := BackendVariable.getVar(cref, inVars);
3616+
for v in vars loop
3617+
type_ := v.varType;
3618+
if BackendVariable.isVarDiscrete(v) and not isInitialEquation then
3619+
initExp := Expression.makePureBuiltinCall("pre", {Expression.crefExp(v.varName)}, type_);
3620+
else
3621+
initExp := Expression.crefExp(ComponentReference.crefPrefixStart(v.varName));
3622+
end if;
3623+
stmt := Algorithm.makeAssignment(DAE.CREF(v.varName, type_), DAE.PROP(type_, DAE.C_VAR()), initExp, DAE.PROP(type_, DAE.C_VAR()), DAE.dummyAttrVar, SCode.NON_INITIAL(), DAE.emptyElementSource);
3624+
statements := stmt::statements;
3625+
end for;
3626+
then expandAlgorithmStmts(statements, rest, inVars, isInitialEquation);
36203627
end match;
36213628
end expandAlgorithmStmts;
36223629

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6862,7 +6862,10 @@ algorithm
68626862
// Set updated globalKnownVars
68636863
simDAE := setDAEGlobalKnownVars(simDAE, globalKnownVars);
68646864

6865-
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE);
6865+
// add initial assignmnents to all algorithms
6866+
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE, false);
6867+
6868+
// remove homotopy and initial calls
68666869
simDAE := Initialization.removeInitializationStuff(simDAE);
68676870

68686871
// generate inline solver
@@ -7330,7 +7333,7 @@ algorithm
73307333
// Set updated globalKnownVars
73317334
simDAE := setDAEGlobalKnownVars(simDAE, globalKnownVars);
73327335

7333-
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE);
7336+
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE, false);
73347337
simDAE := Initialization.removeInitializationStuff(simDAE);
73357338

73367339
// generate inline solver

Compiler/BackEnd/DAEMode.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ algorithm
130130

131131
// Set updated globalKnownVars
132132
simDAE := BackendDAEUtil.setDAEGlobalKnownVars(simDAE, globalKnownVars);
133-
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE);
133+
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE, false);
134134
simDAE := Initialization.removeInitializationStuff(simDAE);
135135

136136
// post-optimization phase

Compiler/BackEnd/Initialization.mo

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ algorithm
219219
initdae := BackendDAEUtil.transformBackendDAE(initdae, SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())), NONE(), NONE());
220220
execStat("matching and sorting (n="+String(BackendDAEUtil.daeSize(initdae))+") (initialization)");
221221

222+
// add initial assignmnents to all algorithms
223+
initdae := BackendDAEOptimize.addInitialStmtsToAlgorithms(initdae, true);
224+
222225
if useHomotopy then
223226
initdae0 := BackendDAEUtil.copyBackendDAE(initdae);
224227
end if;

0 commit comments

Comments
 (0)