Skip to content

Commit

Permalink
- some linearization bugfixes
Browse files Browse the repository at this point in the history
 - add testcase for numerical derivative


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@6943 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Nov 10, 2010
1 parent e163a6c commit 6c5ddac
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 341 deletions.
4 changes: 2 additions & 2 deletions Compiler/DAELow.mo
Expand Up @@ -18441,7 +18441,7 @@ algorithm
nArgs1 = listLength(varExpListTotal);
nArgs2 = listLength(restDerVar);
varExpList1Added = Util.listReplaceAtWithFill(DAE.RCONST(0.0),nArgs1 + nDerArgs - 1, varExpListTotal ,DAE.RCONST(0.0));
varExpList1Added = Util.listReplaceAtWithFill(DAE.RCONST(1.0),nArgs1 + nDerArgs - nArgs2 + 1, varExpList1Added,DAE.RCONST(0.0));
varExpList1Added = Util.listReplaceAtWithFill(DAE.RCONST(1.0),nArgs1 + nDerArgs - (nArgs2 + 1), varExpList1Added,DAE.RCONST(0.0));
derFun = DAE.CALL(derFname, varExpList1Added, tuple_, builtin, et, inlineType);
then DAE.BINARY(e, DAE.ADD(DAE.ET_REAL()), DAE.BINARY(derFun, DAE.MUL(DAE.ET_REAL()), currDerVar));
end matchcontinue;
Expand Down Expand Up @@ -18473,7 +18473,7 @@ algorithm
delta = DAE.IFEXP( absCurr, DAE.BINARY(currVar,DAE.MUL(DAE.ET_REAL()),DAE.RCONST(1e-8)), DAE.RCONST(1e-8));
nArgs1 = listLength(varExpListTotal);
nArgs2 = listLength(restVar);
varExpListHAdded = Util.listReplaceAtWithFill(DAE.BINARY(currVar, DAE.ADD(DAE.ET_REAL()),delta),nArgs1-nArgs2+1, varExpListTotal,DAE.RCONST(0.0));
varExpListHAdded = Util.listReplaceAtWithFill(DAE.BINARY(currVar, DAE.ADD(DAE.ET_REAL()),delta),nArgs1-(nArgs2+1), varExpListTotal,DAE.RCONST(0.0));
derFun = DAE.BINARY(DAE.BINARY(DAE.CALL(fname, varExpListHAdded, tuple_, builtin, et, inlineType), DAE.SUB(DAE.ET_REAL()), DAE.CALL(fname, varExpListTotal, tuple_, builtin, et, inlineType)), DAE.DIV(DAE.ET_REAL()), delta);
then DAE.BINARY(e, DAE.ADD(DAE.ET_REAL()), DAE.BINARY(derFun, DAE.MUL(DAE.ET_REAL()), currDerVar));
end matchcontinue;
Expand Down
72 changes: 37 additions & 35 deletions Compiler/Exp.mo
Expand Up @@ -10359,7 +10359,7 @@ algorithm
// solving linear equation system using newton iteration ( converges directly )
case (lhs,rhs,(cr as DAE.CREF(componentRef = _)))
equation
res = solve2(lhs, rhs, cr);
res = solve2(lhs, rhs, cr, false);
res_1 = simplify1(res);
then
res_1;
Expand Down Expand Up @@ -10391,7 +10391,7 @@ algorithm
end solve;

public function solveLin
"function: solve
"function: solve linear equation
Solves an equation consisting of a right hand side (rhs) and a
left hand side (lhs), with respect to the expression given as
third argument, usually a variable."
Expand Down Expand Up @@ -10436,22 +10436,22 @@ algorithm
res_1 = simplify1(lhs);
then
res_1;

// solving linear equation system using newton iteration ( converges directly )
case (lhs,rhs,(cr as DAE.CREF(componentRef = _)))
equation
true = hasOnlyFactors(lhs,rhs);
lhs = DAE.BINARY(lhs,DAE.ADD(DAE.ET_REAL()),DAE.RCONST(1.0));
rhs = DAE.BINARY(rhs,DAE.ADD(DAE.ET_REAL()),DAE.RCONST(1.0));
res = solve2(lhs, rhs, cr);
res = solve2(lhs, rhs, cr, true);
res_1 = simplify1(res);
then
res_1;
res_1;

// solving linear equation system using newton iteration ( converges directly )
case (lhs,rhs,(cr as DAE.CREF(componentRef = _)))
equation
res = solve2(lhs, rhs, cr);
res = solve2(lhs, rhs, cr, true);
res_1 = simplify1(res);
then
res_1;
Expand Down Expand Up @@ -10508,19 +10508,21 @@ protected function solve2
input Exp inExp1;
input Exp inExp2;
input Exp inExp3;
input Boolean linearExps;
output Exp outExp;
algorithm
outExp := matchcontinue (inExp1,inExp2,inExp3)
outExp := matchcontinue (inExp1,inExp2,inExp3,linearExps)
local
Exp lhs,lhsder,lhsder_1,lhszero,lhszero_1,rhs,rhs_1,e1,e2,crexp;
ComponentRef cr;
Boolean linExp;

// e1 e2 e3
case (e1,e2,(crexp as DAE.CREF(componentRef = cr)))
// e1 e2 e3
case (e1,e2,(crexp as DAE.CREF(componentRef = cr)),linExp)
equation
false = hasOnlyFactors(e1,e2);
lhs = DAE.BINARY(e1,DAE.SUB(DAE.ET_REAL()),e2);
lhsder = Derive.differentiateExpCont(lhs, cr);
lhsder = Derive.differentiateExp(lhs, cr,linExp);
lhsder_1 = simplify(lhsder);
false = isZero(lhsder_1);
false = expContains(lhsder_1, crexp);
Expand All @@ -10531,7 +10533,7 @@ algorithm
then
rhs_1;

case(e1,e2,(crexp as DAE.CREF(componentRef = cr)))
case(e1,e2,(crexp as DAE.CREF(componentRef = cr)),_)
local Exp invCr; list<Exp> factors;
equation
({invCr},factors) = Util.listSplitOnTrue1(listAppend(factors(e1),factors(e2)),isInverseCref,cr);
Expand All @@ -10540,40 +10542,40 @@ algorithm
then
rhs_1;

case (e1,e2,(crexp as DAE.CREF(componentRef = cr)))
case (e1,e2,(crexp as DAE.CREF(componentRef = cr)),linExp)
equation
lhs = DAE.BINARY(e1,DAE.SUB(DAE.ET_REAL()),e2);
lhsder = Derive.differentiateExpCont(lhs, cr);
lhsder = Derive.differentiateExp(lhs, cr, linExp);
lhsder_1 = simplify(lhsder);
true = expContains(lhsder_1, crexp);
/*print("solve2 failed: Not linear: ");
print(printExpStr(e1));
print(" = ");
print(printExpStr(e2));
print("\nsolving for: ");
print(printExpStr(crexp));
print("\n");
print("derivative: ");
print(printExpStr(lhsder));
print("\n");*/
Debug.fcall("failtrace",print, "solve2 failed: Not linear: ");
Debug.fcall("failtrace",print, printExpStr(e1));
Debug.fcall("failtrace",print," = ");
Debug.fcall("failtrace",print,printExpStr(e2));
Debug.fcall("failtrace",print,"\nsolving for: ");
Debug.fcall("failtrace",print,printExpStr(crexp));
Debug.fcall("failtrace",print,"\n");
Debug.fcall("failtrace",print,"derivative: ");
Debug.fcall("failtrace",print,printExpStr(lhsder));
Debug.fcall("failtrace",print,"\n");
then
fail();

case (e1,e2,(crexp as DAE.CREF(componentRef = cr)))
case (e1,e2,(crexp as DAE.CREF(componentRef = cr)),linExp)
equation
lhs = DAE.BINARY(e1,DAE.SUB(DAE.ET_REAL()),e2);
lhsder = Derive.differentiateExpCont(lhs, cr);
lhsder = Derive.differentiateExp(lhs, cr, linExp);
lhsder_1 = simplify(lhsder);
/*print("solve2 failed: ");
print(printExpStr(e1));
print(" = ");
print(printExpStr(e2));
print("\nsolving for: ");
print(printExpStr(crexp));
print("\n");
print("derivative: ");
print(printExpStr(lhsder_1));
print("\n");*/
Debug.fcall("failtrace",print,"solve2 failed: ");
Debug.fcall("failtrace",print,printExpStr(e1));
Debug.fcall("failtrace",print," = ");
Debug.fcall("failtrace",print,printExpStr(e2));
Debug.fcall("failtrace",print,"\nsolving for: ");
Debug.fcall("failtrace",print,printExpStr(crexp));
Debug.fcall("failtrace",print,"\n");
Debug.fcall("failtrace",print,"derivative: ");
Debug.fcall("failtrace",print,printExpStr(lhsder_1));
Debug.fcall("failtrace",print,"\n");
then
fail();
end matchcontinue;
Expand Down

0 comments on commit 6c5ddac

Please sign in to comment.