Skip to content

Commit

Permalink
- correct a mistake from r7788
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7792 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Jan 27, 2011
1 parent 6e177f3 commit 99e9e86
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 8 deletions.
101 changes: 101 additions & 0 deletions Compiler/BackEnd/ExpressionSolve.mo
Expand Up @@ -138,6 +138,107 @@ algorithm
end matchcontinue;
end solve;

public function solveLin
"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."
input DAE.Exp inExp1;
input DAE.Exp inExp2;
input DAE.Exp inExp3;
output DAE.Exp outExp;
output list<DAE.Statement> outAsserts;
algorithm
outExp := matchcontinue (inExp1,inExp2,inExp3)
local
DAE.Exp crexp,crexp2,rhs,lhs,res,res_1,cr,e1,e2,e3;
DAE.ComponentRef cr1,cr2;
DAE.ExpType tp,tp1;
list<DAE.Statement> asserts,asserts1,asserts2;

// case(debuge1,debuge2,debuge3) // FOR DEBBUGING...
// local DAE.Exp debuge1,debuge2,debuge3;
// equation
// print("(Expression.mo debugging) To solve: rhs: " +&
// printExpStr(debuge1) +& " lhs: " +&
// printExpStr(debuge2) +& " with respect to: " +&
// printExpStr(debuge3) +& "\n");
// then
// fail();

// special case when already solved, cr1 = rhs, otherwise division by zero when dividing with derivative
case (crexp,rhs,crexp2)
equation
cr1 = crOrDerCr(crexp);
cr2 = crOrDerCr(crexp2);
true = ComponentReference.crefEqual(cr1, cr2);
false = Expression.expContains(rhs, crexp);
res_1 = ExpressionSimplify.simplify1(rhs);
then
(res_1,{});

// special case when already solved, lhs = cr1, otherwise division by zero when dividing with derivative
case (lhs,crexp ,crexp2)
equation
cr1 = crOrDerCr(crexp);
cr2 = crOrDerCr(crexp2);
true = ComponentReference.crefEqual(cr1, cr2);
false = Expression.expContains(lhs, crexp);
res_1 = ExpressionSimplify.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);
tp = Expression.typeof(lhs);
e1 = Expression.makeConstOne(tp);
lhs = Expression.makeSum({lhs,e1});
tp1 = Expression.typeof(rhs);
e2 = Expression.makeConstOne(tp1);
lhs = Expression.makeSum({rhs,e2});
(res,asserts) = solve2(lhs, rhs, cr, true);
res_1 = ExpressionSimplify.simplify1(res);
then
(res_1,asserts);

// solving linear equation system using newton iteration ( converges directly )
case (lhs,rhs,(cr as DAE.CREF(componentRef = _)))
equation
(res,asserts) = solve2(lhs, rhs, cr, true);
res_1 = ExpressionSimplify.simplify1(res);
then
(res_1,asserts);

case (lhs,DAE.IFEXP(e1,e2,e3),(cr as DAE.CREF(componentRef = _)))
equation
(rhs,asserts) = solveLin(lhs,e2,cr);
(res,asserts1) = solveLin(lhs,e3,cr);
res_1 = ExpressionSimplify.simplify1(DAE.IFEXP(e1,rhs,res));
asserts2 = listAppend(asserts,asserts1);
then
(res_1,asserts2);

case (DAE.IFEXP(e1,e2,e3),rhs,(cr as DAE.CREF(componentRef = _)))
equation
(rhs,asserts) = solveLin(rhs,e2,cr);
(res,asserts1) = solveLin(rhs,e3,cr);
res_1 = ExpressionSimplify.simplify1(DAE.IFEXP(e1,rhs,res));
asserts2 = listAppend(asserts,asserts1);
then
(res_1,asserts2);

case (e1,e2,e3)
equation
Debug.fprint("failtrace", "-Expression.solve failed\n");
//print("solve ");print(ExpressionDump.printExpStr(e1));print(" = ");print(ExpressionDump.printExpStr(e2));
//print(" w.r.t ");print(ExpressionDump.printExpStr(e3));print(" failed\n");
then
fail();
end matchcontinue;
end solveLin;

protected function solve2
"function: solve2
This function solves an equation e1 = e2 with
Expand Down
16 changes: 8 additions & 8 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -3213,7 +3213,7 @@ algorithm
equations = listAppend(equations1,equations);
then
equations;

// a linear system of equations
case (includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, true, dlow, ass1, ass2, (comp as (_ :: (_ :: _))) :: restComps, helpVarInfo)
equation
Expand All @@ -3224,19 +3224,19 @@ algorithm
equations1;

// single equation
case (includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, linearSystem, dlow, ass1, ass2, {index} :: restComps, helpVarInfo)
case (includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, false, dlow, ass1, ass2, {index} :: restComps, helpVarInfo)
equation
equations1 = createEquation(index, dlow, ass1, ass2, helpVarInfo, linearSystem, skipDiscInAlgorithm);
equations = createEquations(includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, linearSystem, dlow, ass1, ass2, restComps, helpVarInfo);
equations1 = createEquation(index, dlow, ass1, ass2, helpVarInfo, false, skipDiscInAlgorithm);
equations = createEquations(includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, false, dlow, ass1, ass2, restComps, helpVarInfo);
equations = listAppend(equations1,equations);
then
equations;

// multiple equations that must be solved together (algebraic loop)
case (includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, linearSystem, dlow, ass1, ass2, (comp as (_ :: (_ :: _))) :: restComps, helpVarInfo)
case (includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, false, dlow, ass1, ass2, (comp as (_ :: (_ :: _))) :: restComps, helpVarInfo)
equation
equations_ = createOdeSystem(genDiscrete, skipDiscInAlgorithm, linearSystem, dlow, ass1, ass2, comp, helpVarInfo);
equations = createEquations(includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, linearSystem, dlow, ass1, ass2, restComps, helpVarInfo);
equations_ = createOdeSystem(genDiscrete, skipDiscInAlgorithm, false, dlow, ass1, ass2, comp, helpVarInfo);
equations = createEquations(includeWhen, skipDiscInZc, genDiscrete, skipDiscInAlgorithm, false, dlow, ass1, ass2, restComps, helpVarInfo);
equations1 = listAppend(equations_,equations);
then
equations1;
Expand Down Expand Up @@ -3304,7 +3304,7 @@ algorithm
= getEquationAndSolvedVar(eqNum, eqns, vars, ass2);
eqStr = BackendDump.equationStr(eqn);
varexp = Expression.crefExp(cr);
(exp_,asserts) = solve(e1, e2, varexp);
(exp_,asserts) = ExpressionSolve.solveLin(e1, e2, varexp);
then
{SES_ALGORITHM(asserts),SES_SIMPLE_ASSIGN(cr, exp_)};

Expand Down

0 comments on commit 99e9e86

Please sign in to comment.