Skip to content

Commit

Permalink
fix initialization of clocked variables (#3497)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfranke committed Oct 13, 2015
1 parent 3e6f284 commit c1357ac
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions Compiler/BackEnd/Initialization.mo
Expand Up @@ -1963,10 +1963,17 @@ algorithm
(vars, fixvars, eqns, reqns, hs, clkHS) := inTpl;

((vars, fixvars, eqns, hs, clkHS)) := BackendVariable.traverseBackendDAEVars(inEqSystem.orderedVars, collectInitialVars, (vars, fixvars, eqns, hs, clkHS));
((eqns, reqns)) := BackendEquation.traverseEquationArray(inEqSystem.orderedEqs, collectInitialEqns, (eqns, reqns));
//((fixvars, eqns)) := List.fold(inEqSystem.stateSets, collectInitialStateSetVars, (fixvars, eqns));

outTpl := (vars, fixvars, eqns, reqns, hs, clkHS);
outTpl := match inEqSystem
case BackendDAE.EQSYSTEM(partitionKind = BackendDAE.CLOCKED_PARTITION(_)) equation
((eqns, clkHS)) = BackendVariable.traverseBackendDAEVars(inEqSystem.orderedVars, createInitialClockedEqns, (eqns, clkHS));
then (vars, fixvars, eqns, reqns, hs, clkHS);

else equation
((eqns, reqns)) = BackendEquation.traverseEquationArray(inEqSystem.orderedEqs, collectInitialEqns, (eqns, reqns));
//((fixvars, eqns)) = List.fold(inEqSystem.stateSets, collectInitialStateSetVars, (fixvars, eqns));
then (vars, fixvars, eqns, reqns, hs, clkHS);
end match;
end collectInitialVarsEqnsSystem;

protected function collectInitialVars "author: lochel
Expand Down Expand Up @@ -2235,6 +2242,41 @@ algorithm
end matchcontinue;
end collectInitialVars;

protected function createInitialClockedEqns "author: rfranke
This function creates initial equations for a clocked partition.
Previous states are initialized with the states. All other variables are initialized with start values."
input BackendDAE.Var inVar;
input tuple<BackendDAE.EquationArray, HashSet.HashSet> inTpl;
output BackendDAE.Var outVar;
output tuple<BackendDAE.EquationArray, HashSet.HashSet> outTpl;
protected
BackendDAE.EquationArray eqns;
HashSet.HashSet clkHS;
algorithm
(eqns, clkHS) := inTpl;
(outVar, outTpl) := match inVar
local
BackendDAE.Var var;
BackendDAE.Equation eqn;
DAE.ComponentRef cr, previousCR;
DAE.Exp crExp, previousExp, startExp;
Boolean previousUsed;
case (var as BackendDAE.VAR(varName=cr)) equation
previousUsed = BaseHashSet.has(cr, clkHS);
crExp = Expression.crefExp(cr);
previousCR = ComponentReference.crefPrefixPrevious(cr); // cr => $CLKPRRE.cr
previousExp = Expression.crefExp(previousCR);
startExp = BackendVariable.varStartValue(var);
eqn = if previousUsed
then
BackendDAE.EQUATION(previousExp, crExp, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_INITIAL)
else
BackendDAE.EQUATION(crExp, startExp, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_INITIAL);
eqns = BackendEquation.addEquation(eqn, eqns);
then (var, (eqns, clkHS));
end match;
end createInitialClockedEqns;

protected function collectInitialEqns "author: lochel"
input BackendDAE.Equation inEq;
input tuple<BackendDAE.EquationArray, BackendDAE.EquationArray> inTpl;
Expand Down

0 comments on commit c1357ac

Please sign in to comment.