Skip to content

Commit

Permalink
ExpressionSolve:
Browse files Browse the repository at this point in the history
 - solve cos(x) = y


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23900 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Dec 23, 2014
1 parent 043c82b commit f0bedf7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Compiler/BackEnd/ExpressionSolve.mo
Expand Up @@ -1336,6 +1336,9 @@ algorithm
newVarsCrefs_ = cr::inewVarsCrefs;
else
e = e1;
if b2 then
e = Expression.negate(e);
end if;
eqnForNewVars_ = ieqnForNewVars;
newVarsCrefs_ = inewVarsCrefs;
end if;
Expand All @@ -1356,6 +1359,43 @@ algorithm
e2 = Expression.makePureBuiltinCall("log",{e3},DAE.T_REAL_DEFAULT);
then (e1,e2,true,eqnForNewVars_,newVarsCrefs_,idepth + 1);

// cos(y) = x -> y = acos(x) + 2*pi*k
case (DAE.CALL(path = Absyn.IDENT(name = "cos"),expLst = {e1}),_)
equation
true = expHasCref(e1, inExp3);
false = expHasCref(inExp2, inExp3);
tp = Expression.typeof(e1);

cr = ComponentReference.makeCrefIdent("$TMP_VAR_SOLVE_COS_FOR_EQN_" + intString(uniqueEqIndex) + "_" + intString(idepth), tp , {});
newVarsCrefs_ = cr ::inewVarsCrefs;

eqn = BackendDAE.SOLVED_EQUATION(cr, e1, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);
eqnForNewVars_ = eqn::ieqnForNewVars;

e = Expression.crefExp(cr);
exP = Expression.makePureBuiltinCall("$_initialGuess", {e}, tp);

cr = ComponentReference.makeCrefIdent("$TMP_VAR_SOLVE_COS_INV_FOR_EQN_" + intString(uniqueEqIndex) + "_" + intString(idepth), tp , {});
newVarsCrefs_ = cr ::newVarsCrefs_;

e2 = Expression.makePureBuiltinCall("acos", {inExp2}, tp);
ee2 = Expression.makePureBuiltinCall("$_signNoNull", {exP}, tp);

eqn = BackendDAE.SOLVED_EQUATION(cr, Expression.expMul(ee2,e2), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);
eqnForNewVars_ = eqn::eqnForNewVars_;

e2 = Expression.crefExp(cr);
ee1 = DAE.RCONST(6.283185307179586476925286766559005768394338798750211641949889);

e_1 = Expression.makeDiv(Expression.expSub(exP, e2), ee1);

e3 = Expression.makePureBuiltinCall("$_round", {e_1}, tp);

lhs = Expression.expAdd(Expression.expMul(e3, ee1),e2);

then(e1, lhs, true, eqnForNewVars_, newVarsCrefs_, idepth + 1);


// abs(f(x)) = g(y) -> f(x) = sign(f(x))*g(y)
case(DAE.CALL(path = Absyn.IDENT(name = "abs"),expLst = {e1}), _)
equation
Expand All @@ -1374,6 +1414,9 @@ algorithm
lhs = Expression.expMul(e_1, inExp2);
else
lhs = inExp2;
if b2 then
lhs = Expression.negate(lhs);
end if;
eqnForNewVars_ = ieqnForNewVars;
newVarsCrefs_ = inewVarsCrefs;
end if;
Expand All @@ -1397,6 +1440,9 @@ algorithm
lhs = Expression.expMul(e_1, lhs);
else
lhs = Expression.expPow(inExp2,Expression.inverseFactors(e2));
if b2 then
lhs = Expression.negate(lhs);
end if;
eqnForNewVars_ = ieqnForNewVars;
newVarsCrefs_ = inewVarsCrefs;
end if;
Expand Down
4 changes: 4 additions & 0 deletions Compiler/Template/CodegenC.tpl
Expand Up @@ -8890,6 +8890,10 @@ template daeExpCall(Exp call, Context context, Text &preExp, Text &varDecls, Tex
case CALL(path=IDENT(name="$_signNoNull"), expLst={e1}) then
let var1 = daeExp(e1, context, &preExp, &varDecls, &auxFunction)
'(<%var1%> >= 0.0 ? 1.0:-1.0)'
// round
case CALL(path=IDENT(name="$_round"), expLst={e1}) then
let var1 = daeExp(e1, context, &preExp, &varDecls, &auxFunction)
'((modelica_integer)round((modelica_real)<%var1%>))'
case CALL(path=IDENT(name="edge"), expLst={arg as CREF(__)}) then
'(<%cref(arg.componentRef)%> && !$P$PRE<%cref(arg.componentRef)%>)'
case CALL(path=IDENT(name="edge"), expLst={LUNARY(exp = arg as CREF(__))}) then
Expand Down

0 comments on commit f0bedf7

Please sign in to comment.