Skip to content

Commit 9662508

Browse files
author
Vitalij Ruge
committed
ExpressionSolve.solve2:
fixed $_initialGuess and tmp vars. git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@23737 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 9322b32 commit 9662508

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

Compiler/BackEnd/ExpressionSolve.mo

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ preprocessing for solve1,
409409
DAE.ComponentRef cr;
410410
DAE.Boolean con, new_x, collect := true, inlineFun := true;
411411
Integer iter;
412+
Integer depth := 0;
412413

413414
algorithm
414415
(x, _) := ExpressionSimplify.simplify(inExp1);
@@ -436,7 +437,7 @@ preprocessing for solve1,
436437
con := new_x or con;
437438
// TODO: use new defined function, which missing in the cpp runtime
438439
if not stringEqual(Config.simCodeTarget(), "Cpp") then
439-
(x, y, new_x, eqnForNewVars, newVarsCrefs) := preprocessingSolveTmpVars(x, y, inExp3, uniqueEqIndex, eqnForNewVars, newVarsCrefs);
440+
(x, y, new_x, eqnForNewVars, newVarsCrefs, depth) := preprocessingSolveTmpVars(x, y, inExp3, uniqueEqIndex, eqnForNewVars, newVarsCrefs, depth);
440441
con := new_x or con;
441442
end if;
442443

@@ -1212,16 +1213,18 @@ e.g. for solve abs()
12121213
input Option<Integer> uniqueEqIndex "offset for tmp vars";
12131214
input list<BackendDAE.Equation> ieqnForNewVars;
12141215
input list<DAE.ComponentRef> inewVarsCrefs;
1216+
input Integer idepth;
12151217
output DAE.Exp x;
12161218
output DAE.Exp y;
12171219
output Boolean new_x;
12181220
output list<BackendDAE.Equation> eqnForNewVars "eqn for tmp vars";
12191221
output list<DAE.ComponentRef> newVarsCrefs;
1222+
output Integer odepth;
12201223
algorithm
1221-
(x, y, new_x, eqnForNewVars, newVarsCrefs) := match(uniqueEqIndex)
1224+
(x, y, new_x, eqnForNewVars, newVarsCrefs, odepth) := match(uniqueEqIndex)
12221225
local Integer i;
1223-
case(SOME(i)) then preprocessingSolveTmpVarsWork(inExp1, inExp2, inExp3, i, ieqnForNewVars, inewVarsCrefs);
1224-
else then (inExp1, inExp2, false, ieqnForNewVars, inewVarsCrefs);
1226+
case(SOME(i)) then preprocessingSolveTmpVarsWork(inExp1, inExp2, inExp3, i, ieqnForNewVars, inewVarsCrefs, idepth);
1227+
else then (inExp1, inExp2, false, ieqnForNewVars, inewVarsCrefs, idepth);
12251228
end match;
12261229
end preprocessingSolveTmpVars;
12271230

@@ -1237,15 +1240,16 @@ e.g. for solve abs
12371240
input Integer uniqueEqIndex "offset for tmp vars";
12381241
input list<BackendDAE.Equation> ieqnForNewVars;
12391242
input list<DAE.ComponentRef> inewVarsCrefs;
1243+
input Integer idepth "depth of tmp var";
12401244
output DAE.Exp x;
12411245
output DAE.Exp y;
12421246
output Boolean new_x;
12431247
output list<BackendDAE.Equation> eqnForNewVars "eqn for tmp vars";
12441248
output list<DAE.ComponentRef> newVarsCrefs;
1249+
output Integer odepth;
12451250
algorithm
1246-
(x, y, new_x, eqnForNewVars, newVarsCrefs) := matchcontinue(inExp1, inExp2)
1251+
(x, y, new_x, eqnForNewVars, newVarsCrefs, odepth) := matchcontinue(inExp1, inExp2)
12471252
local DAE.Exp e1, e_1, e, e2, exP, lhs;
1248-
list<DAE.Exp> lhsF, singCall;
12491253
DAE.ComponentRef cr;
12501254
DAE.Type tp;
12511255
BackendDAE.Equation eqn;
@@ -1257,43 +1261,36 @@ algorithm
12571261
case(DAE.CALL(path = Absyn.IDENT(name = "abs"),expLst = {e1}), _)
12581262
equation
12591263
tp = Expression.typeof(e1);
1260-
cr = ComponentReference.makeCrefIdent("$TMP_VAR_SOLVE_ABS_FOR_EQN_" + intString(uniqueEqIndex), tp , {});
1264+
cr = ComponentReference.makeCrefIdent("$TMP_VAR_SOLVE_ABS_FOR_EQN_" + intString(uniqueEqIndex) + "_" + intString(idepth), tp , {});
12611265
eqn = BackendDAE.SOLVED_EQUATION(cr, e1, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);
12621266
e = Expression.crefExp(cr);
1263-
exP = Expression.makePureBuiltinCall("$_initialGuess",{inExp3},tp);
1264-
e_1 = Expression.makePureBuiltinCall("$_signNoNull",{exP},tp);
1265-
lhsF = Expression.factors(inExp2);
1266-
(singCall,_) = List.split1OnTrue(lhsF, Expression.isFunCall, "$_signNoNull");
1267-
b = listLength(singCall) == 0;
1268-
lhs = if b then Expression.expMul(e_1,inExp2) else inExp2;
1269-
eqnForNewVars_ = if b then eqn::ieqnForNewVars else ieqnForNewVars;
1270-
newVarsCrefs_ = if b then cr ::inewVarsCrefs else inewVarsCrefs;
1271-
then(e1, lhs, true, eqnForNewVars_, newVarsCrefs_);
1267+
exP = Expression.makePureBuiltinCall("$_initialGuess", {e}, tp);
1268+
e_1 = Expression.makePureBuiltinCall("$_signNoNull", {exP}, tp);
1269+
lhs = Expression.expMul(e_1, inExp2);
1270+
eqnForNewVars_ = eqn::ieqnForNewVars;
1271+
newVarsCrefs_ = cr::inewVarsCrefs;
1272+
then(e1, lhs, true, eqnForNewVars_, newVarsCrefs_, idepth + 1);
12721273

12731274
// x^n = y -> x = y^(1/n)
1274-
case(DAE.BINARY(e1,DAE.POW(tp),e2),_)
1275+
case(DAE.BINARY(e1, DAE.POW(tp), e2),_)
12751276
equation
1276-
cr = ComponentReference.makeCrefIdent("$TMP_VAR_SOLVE_POW_FOR_EQN_" + intString(uniqueEqIndex), tp , {});
1277+
cr = ComponentReference.makeCrefIdent("$TMP_VAR_SOLVE_POW_FOR_EQN_" + intString(uniqueEqIndex) + "_" + intString(idepth), tp , {});
12771278
eqn = BackendDAE.SOLVED_EQUATION(cr, e1, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);
12781279
e = Expression.crefExp(cr);
12791280
exP = Expression.makePureBuiltinCall("$_initialGuess",{e},tp);
12801281
e_1 = Expression.makePureBuiltinCall("$_signNoNull",{exP},tp);
1281-
lhsF = Expression.factors(inExp2);
1282-
(singCall,_) = List.split1OnTrue(lhsF, Expression.isFunCall, "$_signNoNull");
1283-
b = listLength(singCall) == 0;
1284-
eqnForNewVars_ = if b then eqn::ieqnForNewVars else ieqnForNewVars;
1285-
newVarsCrefs_ = if b then cr ::inewVarsCrefs else inewVarsCrefs;
1282+
eqnForNewVars_ = eqn::ieqnForNewVars;
1283+
newVarsCrefs_ = cr ::inewVarsCrefs;
12861284
lhs = Expression.expPow(inExp2,DAE.BINARY(Expression.makeConstOne(tp),DAE.DIV(tp),e2));
1287-
lhs = if b then Expression.expMul(e_1,lhs) else lhs;
1288-
then(e1, lhs, true, eqnForNewVars_, newVarsCrefs_);
1285+
lhs = Expression.expMul(e_1, lhs);
1286+
then(e1, lhs, true, eqnForNewVars_, newVarsCrefs_, idepth + 1);
12891287

1290-
else (inExp1, inExp2, false, ieqnForNewVars, inewVarsCrefs);
1288+
else (inExp1, inExp2, false, ieqnForNewVars, inewVarsCrefs, idepth);
12911289
end matchcontinue;
12921290

12931291
end preprocessingSolveTmpVarsWork;
12941292

12951293

1296-
12971294
protected function solveIfExp
12981295
"
12991296
solve:

Compiler/FrontEnd/Expression.mo

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5949,8 +5949,6 @@ algorithm
59495949
then (inExp,false,inTpl);
59505950
case (DAE.CALL(path = Absyn.IDENT(name = "$_start")), _)
59515951
then (inExp,false,inTpl);
5952-
case (DAE.CALL(path = Absyn.IDENT(name = "$_initialGuess")), _)
5953-
then (inExp,false,inTpl);
59545952

59555953
case (DAE.CREF(componentRef = cr1), (cr,false))
59565954
equation

Compiler/Template/CodegenC.tpl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8886,7 +8886,8 @@ template daeExpCall(Exp call, Context context, Text &preExp, Text &varDecls, Tex
88868886
daeExpCallStart(arg, context, preExp, varDecls, &auxFunction)
88878887
// a $_initialGuess is used to get initial guess for nonlinear solver
88888888
case CALL(path=IDENT(name="$_initialGuess"), expLst={arg as CREF(__)}) then
8889-
'<%cref(arg.componentRef)%>'
8889+
let namestr = cref(arg.componentRef)
8890+
'_<%namestr%>(0)'
88908891
// if arg >= 0 then 1 else -1
88918892
case CALL(path=IDENT(name="$_signNoNull"), expLst={e1}) then
88928893
let var1 = daeExp(e1, context, &preExp, &varDecls, &auxFunction)

0 commit comments

Comments
 (0)