Skip to content

Commit

Permalink
[BE] Fixing initialization of algorithms
Browse files Browse the repository at this point in the history
 - added initial statements also to the initial system algorithms and
   initial algorithms.
 - improve expandAlgorithmStmts to handle array variables
 - fixes ticket:4568

Belonging to [master]:
  - OpenModelica/OMCompiler#2249
  - OpenModelica/OpenModelica-testsuite#866
  • Loading branch information
Willi Braun authored and OpenModelica-Hudson committed Mar 2, 2018
1 parent f5c5629 commit be2bc89
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
53 changes: 30 additions & 23 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -3538,31 +3538,34 @@ public function addInitialStmtsToAlgorithms "
- A non-discrete variable is initialized with its start value (i.e. the value of the start-attribute).
- A discrete variable v is initialized with pre(v)."
input BackendDAE.BackendDAE inDAE;
input Boolean isInitialSystem;
output BackendDAE.BackendDAE outDAE;
algorithm
outDAE := BackendDAEUtil.mapEqSystem(inDAE, addInitialStmtsToAlgorithms1);
outDAE := BackendDAEUtil.mapEqSystem1(inDAE, addInitialStmtsToAlgorithms1, isInitialSystem);
end addInitialStmtsToAlgorithms;

protected function addInitialStmtsToAlgorithms1 "Helper function to addInitialStmtsToAlgorithms."
input BackendDAE.EqSystem syst;
input Boolean isInitialSystem;
input BackendDAE.Shared shared;
output BackendDAE.EqSystem osyst = syst;
output BackendDAE.Shared oshared = shared;
protected
BackendDAE.Variables ordvars;
BackendDAE.Variables ordvars, allVars;
BackendDAE.EquationArray ordeqns;
BackendDAE.EquationArray initEqns;
algorithm
BackendDAE.EQSYSTEM(orderedVars=ordvars, orderedEqs=ordeqns) := osyst;
BackendEquation.traverseEquationArray_WithUpdate(ordeqns, eaddInitialStmtsToAlgorithms1Helper, ordvars);
BackendEquation.traverseEquationArray_WithUpdate(ordeqns, eaddInitialStmtsToAlgorithms1Helper, (ordvars, isInitialSystem));
end addInitialStmtsToAlgorithms1;

protected function eaddInitialStmtsToAlgorithms1Helper "Helper function to addInitialStmtsToAlgorithms1."
input BackendDAE.Equation inEq;
input BackendDAE.Variables inVars;
input tuple<BackendDAE.Variables, Boolean> inTpl;
output BackendDAE.Equation outEq;
output BackendDAE.Variables outVars;
output tuple<BackendDAE.Variables, Boolean> outTpl = inTpl;
algorithm
(outEq,outVars) := match (inEq,inVars)
(outEq) := match (inEq, inTpl)
local
DAE.Algorithm alg;
list<DAE.Statement> statements;
Expand All @@ -3574,49 +3577,53 @@ algorithm
list<DAE.ComponentRef> crlst;
DAE.Expand crExpand;
BackendDAE.EquationAttributes attr;
Boolean isInitialEquations;

case (BackendDAE.ALGORITHM(size=size, alg=alg as DAE.ALGORITHM_STMTS(statements), source=source, expand=crExpand, attr=attr), vars)
case (BackendDAE.ALGORITHM(size=size, alg=alg as DAE.ALGORITHM_STMTS(statements), source=source, expand=crExpand, attr=attr), (vars, isInitialEquations))
equation
crlst = CheckModel.checkAndGetAlgorithmOutputs(alg, source, crExpand);
outputs = List.map(crlst, Expression.crefExp);
statements = expandAlgorithmStmts(statements, outputs, vars);
then (BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(statements), source, crExpand, attr), vars);
statements = expandAlgorithmStmts(statements, outputs, vars, isInitialEquations);
then BackendDAE.ALGORITHM(size, DAE.ALGORITHM_STMTS(statements), source, crExpand, attr);

else (inEq,inVars);
else inEq;
end match;
end eaddInitialStmtsToAlgorithms1Helper;

protected function expandAlgorithmStmts "Helper function to eaddInitialStmtsToAlgorithms1Helper."
input list<DAE.Statement> inAlg;
input list<DAE.Exp> inOutputs;
input BackendDAE.Variables inVars;
input Boolean isInitialEquation;
output list<DAE.Statement> outAlg;
algorithm
outAlg := match(inAlg, inOutputs, inVars)
local
DAE.Exp out, initExp;
list<DAE.Exp> rest;
DAE.ComponentRef cref;
BackendDAE.Var var;
list<BackendDAE.Var> vars;
DAE.Statement stmt;
DAE.Type type_;
list<DAE.Statement> statements;

case(statements, {}, _)
then statements;

case(statements, out::rest, _) equation
cref = Expression.expCref(out);
type_ = Expression.typeof(out);
type_ = Expression.arrayEltType(type_);
(var::_, _) = BackendVariable.getVar(cref, inVars);
if BackendVariable.isVarDiscrete(var) then
initExp = Expression.makePureBuiltinCall("pre", {out}, type_);
else
initExp = Expression.crefExp(ComponentReference.crefPrefixStart(cref));
end if;
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);
then expandAlgorithmStmts(stmt::statements, rest, inVars);
case(statements, out::rest, _) algorithm
cref := Expression.expCref(out);
(vars, _) := BackendVariable.getVar(cref, inVars);
for v in vars loop
type_ := v.varType;
if BackendVariable.isVarDiscrete(v) and not isInitialEquation then
initExp := Expression.makePureBuiltinCall("pre", {Expression.crefExp(v.varName)}, type_);
else
initExp := Expression.crefExp(ComponentReference.crefPrefixStart(v.varName));
end if;
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);
statements := stmt::statements;
end for;
then expandAlgorithmStmts(statements, rest, inVars, isInitialEquation);
end match;
end expandAlgorithmStmts;

Expand Down
7 changes: 5 additions & 2 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -6862,7 +6862,10 @@ algorithm
// Set updated globalKnownVars
simDAE := setDAEGlobalKnownVars(simDAE, globalKnownVars);

simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE);
// add initial assignmnents to all algorithms
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE, false);

// remove homotopy and initial calls
simDAE := Initialization.removeInitializationStuff(simDAE);

// generate inline solver
Expand Down Expand Up @@ -7330,7 +7333,7 @@ algorithm
// Set updated globalKnownVars
simDAE := setDAEGlobalKnownVars(simDAE, globalKnownVars);

simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE);
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE, false);
simDAE := Initialization.removeInitializationStuff(simDAE);

// generate inline solver
Expand Down
2 changes: 1 addition & 1 deletion Compiler/BackEnd/DAEMode.mo
Expand Up @@ -130,7 +130,7 @@ algorithm

// Set updated globalKnownVars
simDAE := BackendDAEUtil.setDAEGlobalKnownVars(simDAE, globalKnownVars);
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE);
simDAE := BackendDAEOptimize.addInitialStmtsToAlgorithms(simDAE, false);
simDAE := Initialization.removeInitializationStuff(simDAE);

// post-optimization phase
Expand Down
3 changes: 3 additions & 0 deletions Compiler/BackEnd/Initialization.mo
Expand Up @@ -219,6 +219,9 @@ algorithm
initdae := BackendDAEUtil.transformBackendDAE(initdae, SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())), NONE(), NONE());
execStat("matching and sorting (n="+String(BackendDAEUtil.daeSize(initdae))+") (initialization)");

// add initial assignmnents to all algorithms
initdae := BackendDAEOptimize.addInitialStmtsToAlgorithms(initdae, true);

if useHomotopy then
initdae0 := BackendDAEUtil.copyBackendDAE(initdae);
end if;
Expand Down

0 comments on commit be2bc89

Please sign in to comment.