Skip to content

Commit

Permalink
more fixes to make when equation work again
Browse files Browse the repository at this point in the history
  • Loading branch information
Willi Braun authored and OpenModelica-Hudson committed Aug 13, 2015
1 parent 88a5a66 commit e7db13c
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 61 deletions.
83 changes: 45 additions & 38 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -303,7 +303,7 @@ algorithm
outEqns := listAppend(List.map1(eqns, BackendEquation.setEquationAttributes, eq_attrs), outEqns);
whenClkCnt := whenClkCnt + 1;
else
(outEqns, outWhenClauses) := lowerWhenEqn(el, inFunctions, outEqns, outWhenClauses);
(outEqns, outREqns, outWhenClauses) := lowerWhenEqn(el, inFunctions, outEqns, outREqns, outWhenClauses);
end if;
then
();
Expand Down Expand Up @@ -1684,16 +1684,19 @@ protected function lowerWhenEqn
input DAE.Element inElement;
input DAE.FunctionTree functionTree;
input list<BackendDAE.Equation> inEquationLst;
input list<BackendDAE.Equation> inREquationLst;
input list<BackendDAE.WhenClause> inWhenClauseLst;
output list<BackendDAE.Equation> outEquationLst;
output list<BackendDAE.Equation> outREquationLst;
output list<BackendDAE.WhenClause> outWhenClauseLst;
protected
Inline.Functiontuple fns = (SOME(functionTree), {DAE.NORM_INLINE()});
algorithm
(outEquationLst, outWhenClauseLst):= matchcontinue inElement
(outEquationLst, outREquationLst, outWhenClauseLst):= matchcontinue inElement
local
list<BackendDAE.Equation> res;
list<BackendDAE.Equation> res, rEqns;
list<BackendDAE.Equation> trueEqnLst, elseEqnLst;
list<BackendDAE.Equation> trueREqns, elsREqnLst;
list<BackendDAE.WhenOperator> reinit;
list<BackendDAE.WhenClause> whenClauseList;
DAE.Exp cond;
Expand All @@ -1706,22 +1709,23 @@ algorithm
equation
(DAE.PARTIAL_EQUATION(cond), source) =
Inline.simplifyAndInlineEquationExp(DAE.PARTIAL_EQUATION(cond), fns, source);
(res, reinit) = lowerWhenEqn2(listReverse(eqnl), cond, functionTree, inEquationLst, {});
(res, rEqns, reinit) = lowerWhenEqn2(listReverse(eqnl), cond, functionTree, inEquationLst, inREquationLst, {});
whenClauseList = makeWhenClauses(not listEmpty(reinit), cond, reinit, inWhenClauseLst);
then
(res, whenClauseList);
(res, rEqns, whenClauseList);


case DAE.WHEN_EQUATION(condition = cond, equations = eqnl, elsewhen_ = SOME(elsePart), source = source)
equation
(DAE.PARTIAL_EQUATION(cond), source) =
Inline.simplifyAndInlineEquationExp(DAE.PARTIAL_EQUATION(cond), fns, source);
(elseEqnLst, whenClauseList) = lowerWhenEqn(elsePart, functionTree, {}, inWhenClauseLst);
(trueEqnLst, reinit) = lowerWhenEqn2(listReverse(eqnl), cond, functionTree, {}, {});
(elseEqnLst, elsREqnLst, whenClauseList) = lowerWhenEqn(elsePart, functionTree, {}, {} , inWhenClauseLst);
(trueEqnLst, trueREqns, reinit) = lowerWhenEqn2(listReverse(eqnl), cond, functionTree, {}, {}, {});
whenClauseList = makeWhenClauses(not listEmpty(reinit), cond, reinit, whenClauseList);
res = mergeClauses(trueEqnLst, elseEqnLst, inEquationLst);
rEqns = mergeClauses(trueREqns, elsREqnLst, inREquationLst);
then
(res, whenClauseList);
(res, rEqns, whenClauseList);

else
equation
Expand All @@ -1740,17 +1744,20 @@ protected function lowerWhenEqn2
input DAE.Exp inCond;
input DAE.FunctionTree functionTree;
input list<BackendDAE.Equation> iEquationLst;
input list<BackendDAE.Equation> iREquationLst;
input list<BackendDAE.WhenOperator> iReinitStatementLst;
output list<BackendDAE.Equation> outEquationLst;
output list<BackendDAE.Equation> outREquationLst;
output list<BackendDAE.WhenOperator> outReinitStatementLst;
protected
Inline.Functiontuple fns = (SOME(functionTree), {DAE.NORM_INLINE()});
algorithm
(outEquationLst, outReinitStatementLst):=
(outEquationLst, outREquationLst, outReinitStatementLst):=
matchcontinue inDAEElementLst
local
Integer size;
list<BackendDAE.Equation> eqnl;
list<BackendDAE.Equation> reqnl;
list<BackendDAE.WhenOperator> reinit;
DAE.Exp cre, e, cond, level;
DAE.ComponentRef cr, cr2;
Expand All @@ -1768,59 +1775,59 @@ algorithm
BackendDAE.WhenEquation whenEq;
BackendDAE.WhenOperator whenOp;

case {} then (iEquationLst, iReinitStatementLst);
case {} then (iEquationLst, iREquationLst, iReinitStatementLst);
case DAE.EQUEQUATION(cr1 = cr, cr2 = cr2, source = source)::xs
equation
e = Expression.crefExp(cr2);
whenEq = BackendDAE.WHEN_EQ(inCond, cr, e, NONE());
eq = BackendDAE.WHEN_EQUATION(1, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.DEFINE(componentRef = cr, exp = e, source = source)::xs
equation
(DAE.PARTIAL_EQUATION(e), source) = Inline.simplifyAndInlineEquationExp(DAE.PARTIAL_EQUATION(e), fns, source);
whenEq = BackendDAE.WHEN_EQ(inCond, cr, e, NONE());
eq = BackendDAE.WHEN_EQUATION(1, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.EQUATION(exp = cre as DAE.TUPLE(PR=expl), scalar = e, source = source)::xs
equation
(DAE.EQUALITY_EXPS(_,e), source) = Inline.simplifyAndInlineEquationExp(DAE.EQUALITY_EXPS(cre,e), fns, source);
eqnl = lowerWhenTupleEqn(expl, inCond, e, source, 1, iEquationLst);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eqnl, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eqnl, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.EQUATION(exp = (cre as DAE.CREF(componentRef = cr)), scalar = e, source = source)::xs
equation
(DAE.EQUALITY_EXPS(_,e), source) = Inline.simplifyAndInlineEquationExp(DAE.EQUALITY_EXPS(cre,e), fns, source);
whenEq = BackendDAE.WHEN_EQ(inCond, cr, e, NONE());
eq = BackendDAE.WHEN_EQUATION(1, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.COMPLEX_EQUATION(lhs = (cre as DAE.CREF(componentRef = cr)), rhs = e, source = source)::xs
equation
size = Expression.sizeOf(Expression.typeof(cre));
(DAE.EQUALITY_EXPS(_,e), source) = Inline.simplifyAndInlineEquationExp(DAE.EQUALITY_EXPS(cre,e), fns, source);
whenEq = BackendDAE.WHEN_EQ(inCond, cr, e, NONE());
eq = BackendDAE.WHEN_EQUATION(size, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.COMPLEX_EQUATION(lhs = cre as DAE.TUPLE(PR=expl), rhs = e, source = source)::xs
equation
(DAE.EQUALITY_EXPS(_,e), source) = Inline.simplifyAndInlineEquationExp(DAE.EQUALITY_EXPS(cre,e), fns, source);
eqnl = lowerWhenTupleEqn(expl, inCond, e, source, 1, iEquationLst);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eqnl, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eqnl, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case (DAE.IF_EQUATION(condition1=expl, equations2=eqnslst, equations3=eqns, source = source))::xs
equation
Expand All @@ -1834,27 +1841,27 @@ algorithm
ht = lowerWhenIfEqns(listReverse(expl), listReverse(eqnslst), functionTree, ht);
crexplst = BaseHashTable.hashTableList(ht);
eqnl = lowerWhenIfEqns2(crexplst, inCond, source, iEquationLst);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eqnl, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eqnl, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.ARRAY_EQUATION(dimension=ds, exp = (cre as DAE.CREF(componentRef = cr)), array = e, source = source)::xs
equation
size = List.fold(Expression.dimensionsSizes(ds), intMul, 1);
(DAE.EQUALITY_EXPS(_,e), source) = Inline.simplifyAndInlineEquationExp(DAE.EQUALITY_EXPS(cre,e), fns, source);
whenEq = BackendDAE.WHEN_EQ(inCond, cr, e, NONE());
eq = BackendDAE.WHEN_EQUATION(size, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.ARRAY_EQUATION(exp = cre as DAE.TUPLE(PR=expl), array = e, source = source)::xs
equation
(DAE.EQUALITY_EXPS(_,e), source) = Inline.simplifyAndInlineEquationExp(DAE.EQUALITY_EXPS(cre,e), fns, source);
eqnl = lowerWhenTupleEqn(expl, inCond, e, source, 1, iEquationLst);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eqnl, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eqnl, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.ASSERT(condition=cond, message = e, level = level, source = source)::xs
equation
Expand All @@ -1863,39 +1870,39 @@ algorithm
whenOp = BackendDAE.ASSERT(cond, e, level, source);
whenEq = BackendDAE.WHEN_STMTS(inCond, {whenOp}, NONE());
eq = BackendDAE.WHEN_EQUATION(0, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, iEquationLst, eq::iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.REINIT(componentRef = cr, exp = e, source = source)::xs
equation
(e, source, _,_) = Inline.inlineExp(e, fns, source);
whenOp = BackendDAE.REINIT(cr, e, source);
whenEq = BackendDAE.WHEN_STMTS(inCond, {whenOp}, NONE());
eq = BackendDAE.WHEN_EQUATION(0, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, iEquationLst, eq::iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.TERMINATE(message = e, source = source)::xs
equation
(e, source, _,_) = Inline.inlineExp(e, fns, source);
whenOp = BackendDAE.TERMINATE(e, source);
whenEq = BackendDAE.WHEN_STMTS(inCond, {whenOp}, NONE());
eq = BackendDAE.WHEN_EQUATION(0, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, iEquationLst, eq::iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

case DAE.NORETCALL(exp=e, source=source)::xs
equation
(e, source, _, _) = Inline.inlineExp(e, fns, source);
whenOp = BackendDAE.NORETCALL(e, source);
whenEq = BackendDAE.WHEN_STMTS(inCond, {whenOp}, NONE());
eq = BackendDAE.WHEN_EQUATION(0, whenEq, source, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, eq::iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, iEquationLst, eq::iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);

// failure
case el::_
Expand All @@ -1911,9 +1918,9 @@ algorithm
case _::xs
equation
true = Flags.getConfigBool(Flags.CHECK_MODEL);
(eqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, iEquationLst, iReinitStatementLst);
(eqnl, reqnl, reinit) = lowerWhenEqn2(xs, inCond, functionTree, iEquationLst, iREquationLst, iReinitStatementLst);
then
(eqnl, reinit);
(eqnl, reqnl, reinit);
end matchcontinue;
end lowerWhenEqn2;

Expand Down
12 changes: 12 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -1246,6 +1246,18 @@ public function equationSize "author: PA
output Integer outInteger = inEquationArray.size;
end equationSize;

public function equationArraySizeBDAE
"author: Frenkel TUD
Returns the size of the dae system, which correspondents to the number of variables."
input BackendDAE.BackendDAE inDAE;
output Integer outSize;
protected
list<Integer> sizes;
algorithm
sizes := List.map(inDAE.eqs, equationArraySizeDAE);
outSize := List.fold(sizes, intAdd, 0);
end equationArraySizeBDAE;

public function equationArraySizeDAE
"author: Frenkel TUD
Returns the number of equations in a system."
Expand Down
7 changes: 7 additions & 0 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -2022,6 +2022,13 @@ algorithm
DAE.Exp e,e1;
Absyn.Path functionName;
list<DAE.Exp> functionArgs;
case BackendDAE.ASSIGN(left=cr, right=e)
equation
scr = ComponentReference.printComponentRefStr(cr);
se = ExpressionDump.printExpStr(e);
str = stringAppendList({scr," = ",se});
then
str;
case BackendDAE.REINIT(stateVar=cr,value=e)
equation
scr = ComponentReference.printComponentRefStr(cr);
Expand Down
26 changes: 22 additions & 4 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -834,20 +834,38 @@ public function traverseExpsOfWhenOps<T>
algorithm
(outWhenOps, outTypeA) := match (inWhenOps)
local
DAE.Type tp;
DAE.Exp e1, e2, level;
DAE.ComponentRef cr;
DAE.ComponentRef cr, cr1;
list<BackendDAE.WhenOperator> rest;
T extArg;
DAE.ElementSource source;

case {} then (listReverse(inAccum),inTypeA);
case (BackendDAE.REINIT(stateVar=cr, value=e2, source = source)::rest)
case (BackendDAE.ASSIGN(left = cr, right = e2, source = source)::rest)
equation
tp = Expression.typeof(e2);
e1 = Expression.makeCrefExp(cr, tp);
(e1, extArg) = inFunc(e1, inTypeA);
if Expression.isCref(e1) then
DAE.CREF(cr1, _) = e1;
else
cr1=cr;
end if;
(e2, extArg) = inFunc(e2, extArg);
(outWhenOps, extArg) = traverseExpsOfWhenOps(rest, inFunc, extArg, BackendDAE.ASSIGN(cr1, e2, source)::inAccum);
then (outWhenOps, extArg);
case (BackendDAE.REINIT(stateVar = cr, value = e2, source = source)::rest)
equation
e1 = Expression.crefExp(cr);
(e1, extArg) = inFunc(e1, inTypeA);
DAE.CREF(cr, _) = e1;
if Expression.isCref(e1) then
DAE.CREF(cr1, _) = e1;
else
cr1=cr;
end if;
(e2, extArg) = inFunc(e2, extArg);
(outWhenOps, extArg) = traverseExpsOfWhenOps(rest, inFunc, extArg, BackendDAE.REINIT(cr, e2, source)::inAccum);
(outWhenOps, extArg) = traverseExpsOfWhenOps(rest, inFunc, extArg, BackendDAE.REINIT(cr1, e2, source)::inAccum);
then (outWhenOps, extArg);
case (BackendDAE.ASSERT(condition = e1, message = e2, level = level, source = source)::rest)
equation
Expand Down

0 comments on commit e7db13c

Please sign in to comment.