Skip to content

Commit

Permalink
Adapt handling of start-values
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Mar 29, 2017
1 parent fbffa36 commit bca32a3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 46 deletions.
14 changes: 8 additions & 6 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -3663,9 +3663,9 @@ end removeLocalKnownVars2;
//
// Real a[3];
// algorithm --> algorithm
// a[1] := 1.0; a[1] := $_start(a[1]);
// a[2] := $_start(a[2]);
// a[3] := $_start(a[3]);
// a[1] := 1.0; a[1] := $START.a[1];
// a[2] := $START.a[2];
// a[3] := $START.a[3];
// a[1] := 1.0;
// =============================================================================

Expand Down Expand Up @@ -3738,7 +3738,6 @@ algorithm
DAE.Statement stmt;
DAE.Type type_;
list<DAE.Statement> statements;
Boolean b;

case(statements, {}, _)
then statements;
Expand All @@ -3748,8 +3747,11 @@ algorithm
type_ = Expression.typeof(out);
type_ = Expression.arrayEltType(type_);
(var::_, _) = BackendVariable.getVar(cref, inVars);
b = BackendVariable.isVarDiscrete(var);
initExp = Expression.makePureBuiltinCall(if b then "pre" else "$_start", {out}, type_);
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);
end match;
Expand Down
25 changes: 18 additions & 7 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -2631,7 +2631,7 @@ public function traversingincidenceRowExpSolvableFinder "Helper for statesAndVar
algorithm
(outExp, cont, outTpl) := matchcontinue (inExp, inTpl)
local
list<Integer> p, ilst;
list<Integer> p, p2, ilst;
AvlSetInt.Tree pa;
DAE.ComponentRef cr;
BackendDAE.Variables vars;
Expand Down Expand Up @@ -2685,10 +2685,20 @@ algorithm
(_, tpl) = Expression.traverseExpTopDown(e1, traversingincidenceRowExpSolvableFinder, tpl);
then (inExp, false, tpl);

// cref and $START.cref
case (DAE.CREF(componentRef=cr), (vars, pa, ofunctionTree)) equation
(varslst, p) = BackendVariable.getVar(cr, vars);
(_, p2) = BackendVariable.getVar(ComponentReference.crefPrefixStart(cr), vars);

pa = incidenceRowExp1(varslst, p, pa, 0);
then (inExp, false,(vars, pa, ofunctionTree));
pa = incidenceRowExp1(varslst, p2, pa, 0);
then (inExp, true, (vars, pa, ofunctionTree));

// only cref
case (DAE.CREF(componentRef=cr), (vars, pa, ofunctionTree)) equation
(varslst, p) = BackendVariable.getVar(cr, vars);
pa = incidenceRowExp1(varslst, p, pa, 0);
then (inExp, true, (vars, pa, ofunctionTree));

case (DAE.CALL(path=Absyn.IDENT(name="der"), expLst={DAE.CREF(componentRef=cr)}), (vars, pa, ofunctionTree)) equation
(varslst, p) = BackendVariable.getVar(cr, vars);
Expand All @@ -2702,6 +2712,7 @@ algorithm
then (inExp, false,(vars, pa, ofunctionTree));

// lochel: internally generated call start(v) depends not on v
// TODO: REMOVE THIS CASE
case (DAE.CALL(path=Absyn.IDENT(name="$_start")), tpl)
then (inExp, false, tpl);

Expand Down Expand Up @@ -2843,17 +2854,17 @@ algorithm
Integer i;
String str;

// var and var.start
// cref and $START.cref
case (e as DAE.CREF(componentRef=cr), (vars, pa))
equation
(varslst, p) = BackendVariable.getVar(cr, vars);
(varslst, p2) = BackendVariable.getVar(ComponentReference.crefPrefixStart(cr), vars);
(_, p2) = BackendVariable.getVar(ComponentReference.crefPrefixStart(cr), vars);

res = incidenceRowExp1(varslst, p, pa, 0);
res = incidenceRowExp1(varslst, p2, res, 0);
then (e, true, (vars, res));

// only var
// only cref
case (e as DAE.CREF(componentRef = cr),(vars,pa))
equation
(varslst,p) = BackendVariable.getVar(cr, vars);
Expand All @@ -2879,6 +2890,7 @@ algorithm
then (e,false,(vars,res));

// lochel: internally generated call start(v) depends not on v
// TODO: REMOVE THIS CASE
case (DAE.CALL(path = Absyn.IDENT(name = "$_start")), _) then (inExp, false, inTpl);

/* pre(v) is considered a known variable */
Expand Down Expand Up @@ -6976,8 +6988,7 @@ algorithm
outDAE := BackendDAE.DAE(systs, shared);
execStat("preOpt " + moduleStr);
if Flags.isSet(Flags.OPT_DAE_DUMP) then
print(stringAppendList({"\npre-optimization module ", moduleStr, ":\n\n"}));
BackendDump.printBackendDAE(outDAE);
BackendDump.dumpBackendDAE(outDAE, "pre-optimization module " + moduleStr);
end if;
else
execStat("preOpt " + moduleStr + " <failed>");
Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/CommonSubExpression.mo
Expand Up @@ -874,6 +874,7 @@ algorithm
case DAE.CALL(path=Absyn.IDENT("$_initialGuess")) then true;
case DAE.CALL(path=Absyn.IDENT("$_old")) then true;
case DAE.CALL(path=Absyn.IDENT("$_round")) then true;
// TODO: REMOVE THIS CASE
case DAE.CALL(path=Absyn.IDENT("$_start")) then true;
case DAE.CALL(path=Absyn.IDENT("$getPart")) then true;
case DAE.CALL(path=Absyn.IDENT("abs")) then true;
Expand Down
12 changes: 10 additions & 2 deletions Compiler/BackEnd/Differentiate.mo
Expand Up @@ -558,6 +558,7 @@ algorithm
//String se1;
list<DAE.Exp> sub, expl;
list<list<DAE.Exp>> matrix, dmatrix;
DAE.ComponentRef cref;

// constants => results in zero
case DAE.BCONST(bool=b) then (DAE.BCONST(b), inFunctionTree);
Expand All @@ -577,17 +578,24 @@ algorithm
then (DAE.RECORD(p, listReverse(sub), strLst, tp), functionTree);

// differentiate cref
case DAE.CREF() equation
case DAE.CREF(componentRef=cref, ty=tp) equation
//se1 = ExpressionDump.printExpStr(inExp);
//print("\nExp-Cref\nDifferentiate exp: " + se1);

(res, functionTree) = differentiateCrefs(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1, expStack);
if ComponentReference.isStartCref(cref) then
// differentiate start value
res = Expression.makeConstZero(tp);
functionTree = inFunctionTree;
else
(res, functionTree) = differentiateCrefs(inExp, inDiffwrtCref, inInputData, inDiffType, inFunctionTree, maxIter-1, expStack);
end if;

//se1 = ExpressionDump.printExpStr(res);
//print("\nresults to exp: " + se1);
then (res, functionTree);

// differentiate start value
// TODO: REMOVE THIS CASE
case DAE.CALL(path=Absyn.IDENT(name="$_start"), attr=DAE.CALL_ATTR(ty=tp))
then (Expression.makeConstZero(tp), inFunctionTree);

Expand Down
56 changes: 26 additions & 30 deletions Compiler/BackEnd/ExpressionSolve.mo
Expand Up @@ -362,16 +362,6 @@ algorithm
Real r, r2;
list<DAE.Statement> asserts;

// special case for inital system when already solved, cr1 = $_start(...)
case (DAE.CREF(componentRef = cr1),DAE.CALL(path = Absyn.IDENT(name = "$_start")),DAE.CREF(componentRef = cr))
guard ComponentReference.crefEqual(cr, cr1)
then
(inExp2,{});
case (DAE.CALL(path = Absyn.IDENT(name = "der"),expLst = {DAE.CREF(componentRef = cr1)}),DAE.CALL(path = Absyn.IDENT(name = "$_start")),DAE.CREF(componentRef = cr))
guard ComponentReference.crefEqual(cr, cr1)
then
(inExp2,{});

// special case when already solved, cr1 = rhs, otherwise division by zero when dividing with derivative
case (DAE.CREF(componentRef = cr1),_,DAE.CREF(componentRef = cr))
guard ComponentReference.crefEqual(cr, cr1) and (not Expression.expHasCrefNoPreOrStart(inExp2, cr))
Expand Down Expand Up @@ -1953,7 +1943,7 @@ protected
DAE.Exp con, e;
algorithm
con := Expression.makePureBuiltinCall("initial",{},tp);
(e,_) := Expression.traverseExpBottomUp(iExp2,makeIntialGuess2,(iExp3, "$_start",tp,true));
(e,_) := Expression.traverseExpBottomUp(iExp2,makeIntialGuess2,(iExp3, DAE.startNamePrefix, tp, true));
(oExp,_) := Expression.traverseExpBottomUp(iExp2,makeIntialGuess2,(iExp3, "$_initialGuess",tp,false));
oExp := DAE.IFEXP(con,e,oExp);
end makeIntialGuess;
Expand All @@ -1964,26 +1954,32 @@ protected function makeIntialGuess2
output DAE.Exp oExp;
output tuple<DAE.Exp, String, DAE.Type, Boolean> otpl = itpl;
algorithm
oExp := match(iExp, itpl)
local DAE.ComponentRef cr1,cr2;
DAE.Type tp;
String fun;
DAE.Exp e;

case(DAE.CREF(componentRef = cr1), (DAE.CREF(componentRef = cr2), fun, tp,_))
guard(ComponentReference.crefEqual(cr1, cr2))
then Expression.makePureBuiltinCall(fun,{iExp},tp);
case(_,(_,_,tp,true))
algorithm
try
SOME(e) := makeIntialGuess3(iExp, tp);
else
e := iExp;
end try;
then e;
else iExp;
end match;
oExp := match(iExp, itpl)
local
DAE.ComponentRef cr1,cr2;
DAE.Type tp;
String fun;
DAE.Exp e;

case (DAE.CREF(componentRef=cr1), (DAE.CREF(componentRef=cr2), fun, tp, _))
guard(ComponentReference.crefEqual(cr1, cr2)) algorithm
if fun == DAE.startNamePrefix then
e := Expression.crefExp(ComponentReference.crefPrefixStart(cr1));
else
e := Expression.makePureBuiltinCall(fun, {iExp}, tp);
end if;
then e;

case (_, (_, _, tp, true)) algorithm
try
SOME(e) := makeIntialGuess3(iExp, tp);
else
e := iExp;
end try;
then e;

else iExp;
end match;
end makeIntialGuess2;

protected function makeIntialGuess3
Expand Down
1 change: 0 additions & 1 deletion Compiler/BackEnd/IndexReduction.mo
Expand Up @@ -1658,7 +1658,6 @@ algorithm
end makeStartExp;

protected function setStartExp
"generate the expression: $_start(inExp)"
input BackendDAE.Var inVar;
input DAE.Exp startExp;
input Integer size;
Expand Down
1 change: 1 addition & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -6636,6 +6636,7 @@ algorithm
then (inExp,false,inTpl);
case (DAE.CALL(path = Absyn.IDENT(name = "edge")), _)
then (inExp,false,inTpl);
// TODO: REMOVE THIS CASE
case (DAE.CALL(path = Absyn.IDENT(name = "$_start")), _)
then (inExp,false,inTpl);
case (DAE.CALL(path = Absyn.IDENT(name = "$_initialGuess")), _)
Expand Down

0 comments on commit bca32a3

Please sign in to comment.