Skip to content

Commit

Permalink
Refine treatment of clocked start values to fix ticket:3770
Browse files Browse the repository at this point in the history
Hold start values at first clock tick for clocked continuous states
and for clocked discrete states with fixed=true attribute.
  • Loading branch information
rfranke committed Jun 4, 2016
1 parent 6a480c8 commit 8987eb2
Showing 1 changed file with 38 additions and 27 deletions.
65 changes: 38 additions & 27 deletions Compiler/BackEnd/SynchronousFeatures.mo
Expand Up @@ -124,9 +124,7 @@ algorithm

(clockedSysts, shared) := subClockPartitioning1(clockedSysts, shared, holdComps);

(clockedSysts, shared) := solveContinuousEquations(clockedSysts, shared);

shared := List.fold(clockedSysts, makePreviousFixed, shared);
(clockedSysts, shared) := treatClockedStates(clockedSysts, shared);

unpartRemEqs := createBoolClockWhenClauses(shared, unpartRemEqs);
shared.removedEqs := BackendEquation.addEquations(unpartRemEqs, shared.removedEqs);
Expand Down Expand Up @@ -166,9 +164,9 @@ algorithm
end for;
end createBoolClockWhenClauses;

protected function solveContinuousEquations
"Convert continuous equations in clocked partitions to clocked equations.
author: rfranke"
protected function treatClockedStates
"Convert continuous equations in clocked partitions to clocked equations
and call markClockedStates. author: rfranke"
input list<BackendDAE.EqSystem> inSysts;
input BackendDAE.Shared inShared;
output list<BackendDAE.EqSystem> outSysts = {};
Expand All @@ -184,6 +182,7 @@ algorithm
BackendDAE.EqSystem syst;
list<BackendDAE.Equation> lstEqs;
BackendDAE.Equation eq;
list<DAE.ComponentRef> derVars = {};
case syst as BackendDAE.EQSYSTEM(orderedEqs = eqs)
algorithm
BackendDAE.CLOCKED_PARTITION(idx) := syst.partitionKind;
Expand All @@ -192,49 +191,53 @@ algorithm
lstEqs := {};
for i in 1:BackendDAEUtil.equationArraySize(eqs) loop
eq := BackendEquation.equationNth1(eqs, i);
(eq, solverMethod) := BackendEquation.traverseExpsOfEquation(eq, applySolverMethod1, solverMethod);
(eq, (solverMethod, derVars)) := BackendEquation.traverseExpsOfEquation(eq, applySolverMethod1, (solverMethod, derVars));
lstEqs := eq::lstEqs;
end for;
syst.orderedEqs := BackendEquation.listEquation(listReverse(lstEqs));
shared := markClockedStates(syst, shared, derVars);
then syst;
end match;
outSysts := BackendDAEUtil.clearEqSyst(syst1) :: outSysts;
end for;
outSysts := listReverse(outSysts);
end solveContinuousEquations;
end treatClockedStates;

protected function applySolverMethod1 "helper to applySolverMethod"
input DAE.Exp inExp;
input String inSolverMethod;
input tuple<String, list<DAE.ComponentRef>> inTpl;
output DAE.Exp outExp;
output String outSolverMethod;
output tuple<String, list<DAE.ComponentRef>> outTpl;
algorithm
(outExp, outSolverMethod) := Expression.traverseExpBottomUp(inExp, applySolverMethod, inSolverMethod);
(outExp, outTpl) := Expression.traverseExpBottomUp(inExp, applySolverMethod, inTpl);
end applySolverMethod1;

protected function applySolverMethod
"Apply given solverMethod to convert continous-time to clocked expression.
So far ImplicitEuler, replacing der(x) -> (x - previous(x))/interval().
author: rfranke"
input DAE.Exp inExp;
input String inSolverMethod;
input tuple<String, list<DAE.ComponentRef>> inTpl;
output DAE.Exp outExp;
output String outSolverMethod = inSolverMethod;
output tuple<String, list<DAE.ComponentRef>> outTpl;
algorithm
outExp := match inExp
(outExp, outTpl) := match inExp
local
List<DAE.Exp> expLst;
DAE.CallAttributes attr;
DAE.ComponentRef cr;
DAE.ComponentRef x;
DAE.Type ty;
DAE.Exp exp;
String inSolverMethod, outSolverMethod;
list<DAE.ComponentRef> inDerVars;
case DAE.CALL(path = Absyn.IDENT(name = "der"),
expLst = expLst as {DAE.CREF(componentRef = cr)},
expLst = expLst as {DAE.CREF(componentRef = x)},
attr = attr as DAE.CALL_ATTR(ty = ty))
algorithm
(inSolverMethod, inDerVars) := inTpl;
outSolverMethod := "ImplicitEuler";
exp := DAE.CALL(Absyn.IDENT(name = "previous"), expLst, attr);
exp := DAE.BINARY(DAE.CREF(cr, ty), DAE.SUB(DAE.T_REAL_DEFAULT), exp);
exp := DAE.BINARY(DAE.CREF(x, ty), DAE.SUB(DAE.T_REAL_DEFAULT), exp);
exp := DAE.BINARY(exp, DAE.DIV(DAE.T_REAL_DEFAULT),
DAE.CALL(Absyn.IDENT(name = "interval"), {},
DAE.callAttrBuiltinImpureReal));
Expand All @@ -243,23 +246,24 @@ algorithm
outSolverMethod + " instead of specified " +
inSolverMethod + ".");
end if;
then exp;
else inExp;
then (exp, (outSolverMethod, x :: inDerVars));
else (inExp, inTpl);
end match;
end applySolverMethod;

protected function makePreviousFixed
protected function markClockedStates
"Collect discrete states and mark them for further processing.
Use VarKind CLOCKED_STATE. Moreover set the fixed attribute
and list the crefs of all discrete states in
Use VarKind CLOCKED_STATE. Moreover set the isStartFixed flag,
the fixed attribute and list the crefs of all discrete states in
outShared.partitionsInfo.subPartitions[subPartIdx].prevVars."
input BackendDAE.EqSystem inSyst;
input BackendDAE.Shared inShared;
input list<DAE.ComponentRef> derVars;
output BackendDAE.Shared outShared = inShared;
protected
BackendDAE.Equation eq;
list<DAE.ComponentRef> prevVars = {};
array<Boolean> isPrevVarArr;
array<Boolean> isPrevVarArr, isDerVarArr;
list<Integer> varIxs;
BackendDAE.Var var;
Integer idx;
Expand All @@ -271,7 +275,13 @@ algorithm
//rfranke: don't know why partitions with solver shall be excluded here
//if isNone(subPartition.clock.solver) then
isPrevVarArr := arrayCreate(BackendVariable.varsSize(inSyst.orderedVars), false);

isDerVarArr := arrayCreate(BackendVariable.varsSize(inSyst.orderedVars), false);
for cr in derVars loop
varIxs := getVarIxs(cr, inSyst.orderedVars);
for idx in varIxs loop
arrayUpdate(isDerVarArr, idx, true);
end for;
end for;
for i in 1:BackendDAEUtil.equationArraySize(inSyst.orderedEqs) loop
eq := BackendEquation.equationNth1(inSyst.orderedEqs, i);
(_, prevVars) := BackendEquation.traverseExpsOfEquation(eq, collectPrevVars, prevVars);
Expand All @@ -289,10 +299,11 @@ algorithm
prevVars := {};
for i in 1:arrayLength(isPrevVarArr) loop
if isPrevVarArr[i] then
var := BackendVariable.setVarFixed(BackendVariable.getVarAt(inSyst.orderedVars, i), true);
var := BackendVariable.getVarAt(inSyst.orderedVars, i);
var := BackendVariable.setVarKind(var, BackendDAE.CLOCKED_STATE(
previousName = ComponentReference.crefPrefixPrevious(var.varName),
isStartFixed = isSome(subPartition.clock.solver)));
isStartFixed = isDerVarArr[i] or BackendVariable.varFixed(var)));
var := BackendVariable.setVarFixed(var, true);
BackendVariable.setVarAt(inSyst.orderedVars, i, var);
prevVars := var.varName::prevVars;
end if;
Expand All @@ -302,7 +313,7 @@ algorithm
arrayUpdate(outShared.partitionsInfo.subPartitions, idx, subPartition);
//end if;

end makePreviousFixed;
end markClockedStates;

protected function collectPrevVars
input DAE.Exp inExp;
Expand Down

0 comments on commit 8987eb2

Please sign in to comment.