Skip to content

Commit

Permalink
ExpressionSolve: added rule for solve if-expression inside continuous…
Browse files Browse the repository at this point in the history
… integration
  • Loading branch information
vruge committed Apr 2, 2016
1 parent 49bc0cb commit 392b6e6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions Compiler/BackEnd/ExpressionSolve.mo
Expand Up @@ -120,6 +120,7 @@ protected
DAE.Exp e1,e2,varexp,e;
BackendDAE.EquationAttributes attr;
DAE.ElementSource source;
Boolean isContinuousIntegration = BackendDAEUtil.isSimulationDAE(shared);
algorithm
BackendDAE.EQUATION(exp=e1, scalar=e2, source=source,attr=attr) := eqn;
BackendDAE.VAR(varName = cr) := var;
Expand Down Expand Up @@ -207,7 +208,7 @@ algorithm
(outExp,outAsserts,dummy1, dummy2, dummyI) := matchcontinue inExp1
case _ then solveSimple(inExp1, inExp2, inExp3, 0);
case _ then solveSimple(inExp2, inExp1, inExp3, 0);
case _ then solveWork(inExp1, inExp2, inExp3, NONE(), NONE(), 0, false);
case _ then solveWork(inExp1, inExp2, inExp3, NONE(), NONE(), 0, false, false);
else equation
if Flags.isSet(Flags.FAILTRACE) then
Error.addInternalError("Failed to solve \"" + ExpressionDump.printExpStr(inExp1) + " = " + ExpressionDump.printExpStr(inExp2) + "\" w.r.t. \"" + ExpressionDump.printExpStr(inExp3) + "\"", sourceInfo());
Expand All @@ -230,6 +231,7 @@ public function solve2
input Option<DAE.FunctionTree> functions "need for solve modelica functions";
input Option<Integer> uniqueEqIndex "offset for tmp vars";
input Boolean doInline = true;
input Boolean isContinuousIntegration = false;
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
output list<BackendDAE.Equation> eqnForNewVars "eqn for tmp vars";
Expand All @@ -246,7 +248,7 @@ algorithm
(outExp,outAsserts,eqnForNewVars,newVarsCrefs,dummyI) := matchcontinue inExp1
case _ then solveSimple(inExp1, inExp2, inExp3, 0);
case _ then solveSimple(inExp2, inExp1, inExp3, 0);
case _ then solveWork(inExp1, inExp2, inExp3, functions, uniqueEqIndex, 0, doInline);
case _ then solveWork(inExp1, inExp2, inExp3, functions, uniqueEqIndex, 0, doInline, isContinuousIntegration);
else equation
if Flags.isSet(Flags.FAILTRACE) then
Error.addInternalError("Failed to solve \"" + ExpressionDump.printExpStr(inExp1) + " = " + ExpressionDump.printExpStr(inExp2) + "\" w.r.t. \"" + ExpressionDump.printExpStr(inExp3) + "\"", sourceInfo());
Expand All @@ -267,6 +269,7 @@ protected function solveWork
input Option<Integer> uniqueEqIndex "offset for tmp vars";
input Integer idepth;
input Boolean doInline;
input Boolean isContinuousIntegration;
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
output list<BackendDAE.Equation> eqnForNewVars "eqn for tmp vars";
Expand All @@ -292,7 +295,7 @@ algorithm
end matchcontinue;

(outExp, outAsserts, eqnForNewVars1, newVarsCrefs1, depth) := matchcontinue e1
case _ then solveIfExp(e1, e2, inExp3, functions, uniqueEqIndex, depth, doInline);
case _ then solveIfExp(e1, e2, inExp3, functions, uniqueEqIndex, depth, doInline, isContinuousIntegration);
case _ then solveSimple(e1, e2, inExp3, depth);
case _ then solveLinearSystem(e1, e2, inExp3, functions, depth);
else fail();
Expand Down Expand Up @@ -1712,14 +1715,15 @@ protected function solveIfExp
input Option<Integer> uniqueEqIndex "offset for tmp vars";
input Integer idepth;
input Boolean doInline;
input Boolean isContinuousIntegration;
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
output list<BackendDAE.Equation> eqnForNewVars "eqn for tmp vars";
output list<DAE.ComponentRef> newVarsCrefs;
output Integer odepth;

algorithm
(outExp,outAsserts,eqnForNewVars,newVarsCrefs,odepth) := match(inExp1,inExp2,inExp3, functions, uniqueEqIndex)
(outExp,outAsserts,eqnForNewVars,newVarsCrefs,odepth) := match inExp1
local
DAE.Exp e1,e2,e3,res,lhs,rhs;
list<DAE.Statement> asserts,asserts1,asserts2;
Expand All @@ -1731,12 +1735,13 @@ algorithm
// a1 = solve(f(a),f1(a)) for a
// a2 = solve(f(a),f2(a)) for a
// => a = if g(b) then a1 else a2
case (DAE.IFEXP(e1,e2,e3),_,_,_,_)
case DAE.IFEXP(e1,e2,e3)
guard
isContinuousIntegration or not expHasCref(e1, inExp3)
equation
false = expHasCref(e1, inExp3);

(lhs, asserts1, eqns, var, depth) = solveWork(e2, inExp2, inExp3, functions, uniqueEqIndex, idepth, doInline);
(rhs,_, eqns1, var1, depth) = solveWork(e3, inExp2, inExp3, functions, uniqueEqIndex, depth, doInline);
(lhs, asserts1, eqns, var, depth) = solveWork(e2, inExp2, inExp3, functions, uniqueEqIndex, idepth, doInline, isContinuousIntegration);
(rhs,_, eqns1, var1, depth) = solveWork(e3, inExp2, inExp3, functions, uniqueEqIndex, depth, doInline, isContinuousIntegration);

res = DAE.IFEXP(e1,lhs,rhs);
asserts = listAppend(asserts1,asserts1);
Expand Down
8 changes: 4 additions & 4 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -1961,7 +1961,7 @@ algorithm
varexp = Expression.crefExp(cr);
varexp = if BackendVariable.isStateVar(v) then Expression.expDer(varexp) else varexp;
BackendDAE.SHARED(functionTree = funcs) = shared;
(exp_, asserts, solveEqns, solveCr) = ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), SOME(iuniqueEqIndex));
(exp_, asserts, solveEqns, solveCr) = ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), SOME(iuniqueEqIndex), true, BackendDAEUtil.isSimulationDAE(shared));
solveEqns = listReverse(solveEqns);
solveCr = listReverse(solveCr);
cr = if BackendVariable.isStateVar(v) then ComponentReference.crefPrefixDer(cr) else cr;
Expand Down Expand Up @@ -3189,7 +3189,7 @@ algorithm
varexp = Expression.crefExp(cr);
varexp = if BackendVariable.isStateVar(var) then Expression.expDer(varexp) else varexp;
BackendDAE.SHARED(functionTree = funcs) = ishared;
(expr, {}, {}, {}) = ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), NONE());
(expr, {}, {}, {}) = ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), NONE(), true, BackendDAEUtil.isSimulationDAE(ishared));
dcr = if BackendVariable.isStateVar(var) then ComponentReference.crefPrefixDer(cr) else cr;
repl = BackendVarTransform.addReplacement(inRepl, dcr, expr, SOME(BackendVarTransform.skipPreOperator));
repl = if BackendVariable.isStateVar(var) then BackendVarTransform.addDerConstRepl(cr, expr, repl) else repl;
Expand All @@ -3216,7 +3216,7 @@ algorithm
varexp = Expression.crefExp(cr);
varexp = if BackendVariable.isStateVar(var) then Expression.expDer(varexp) else varexp;
BackendDAE.SHARED(functionTree = funcs) = ishared;
(expr, {}, {}, {}) = ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), NONE());
(expr, {}, {}, {}) = ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), NONE(), true, BackendDAEUtil.isSimulationDAE(ishared));
dcr = if BackendVariable.isStateVar(var) then ComponentReference.crefPrefixDer(cr) else cr;
repl = BackendVarTransform.addReplacement(inRepl, dcr, expr, SOME(BackendVarTransform.skipPreOperator));
repl = if BackendVariable.isStateVar(var) then BackendVarTransform.addDerConstRepl(cr, expr, repl) else repl;
Expand Down Expand Up @@ -3265,7 +3265,7 @@ algorithm
varexp = Expression.crefExp(cr);
varexp = if BackendVariable.isStateVar(var) then Expression.expDer(varexp) else varexp;
BackendDAE.SHARED(functionTree = funcs) = ishared;
(expr, {}, {}, {}) = ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), NONE());
(expr, {}, {}, {}) = ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), NONE(), true, BackendDAEUtil.isSimulationDAE(ishared));
dcr = if BackendVariable.isStateVar(var) then ComponentReference.crefPrefixDer(cr) else cr;
repl = BackendVarTransform.addReplacement(inRepl, dcr, expr, SOME(BackendVarTransform.skipPreOperator));
repl = if BackendVariable.isStateVar(var) then BackendVarTransform.addDerConstRepl(cr, expr, repl) else repl;
Expand Down

0 comments on commit 392b6e6

Please sign in to comment.