Skip to content

Commit

Permalink
improved r25421
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25471 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Apr 9, 2015
1 parent e33cf67 commit 74cdd70
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 111 deletions.
106 changes: 13 additions & 93 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -492,89 +492,6 @@ algorithm
(_, (_,cr_lst)) := traverseExpsOfEquation(e, Expression.traverseSubexpressionsHelper, (Expression.traversingComponentRefFinder, cr_lst));
end traversingEquationCrefFinder;


public function equationUnknownCrefsNotConst "author: Frenkel TUD 2012-05
From the equation and a variable array return all
variables in the equation an not in the variable array or not const"
input list<BackendDAE.Equation> inEquationLst;
input BackendDAE.Variables inVars;
input BackendDAE.Variables inKnVars;
output list<DAE.ComponentRef> cr_lst;
protected
HashTable.HashTable ht;
algorithm
ht := HashTable.emptyHashTable();
(_, (_, (_, _, ht))) := traverseExpsOfEquationList(inEquationLst, Expression.traverseSubexpressionsHelper, (checkEquationsUnknownCrefsExpNotConst, (inVars, inKnVars, ht)));
cr_lst := BaseHashTable.hashTableKeyList(ht);
end equationUnknownCrefsNotConst;

protected function checkEquationsUnknownCrefsExpNotConst
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, BackendDAE.Variables, HashTable.HashTable> inTuple;
output DAE.Exp outExp;
output tuple<BackendDAE.Variables, BackendDAE.Variables, HashTable.HashTable> outTuple;
algorithm
(outExp,outTuple) := matchcontinue (inExp,inTuple)
local
DAE.Exp e, e1;
BackendDAE.Variables vars, knvars;
HashTable.HashTable ht;
DAE.ComponentRef cr;
list<DAE.Exp> expl;
list<DAE.Var> varLst;
list<BackendDAE.Var> var_lst;

// special case for records
case (e as DAE.CREF(componentRef = cr, ty= DAE.T_COMPLEX(varLst=varLst, complexClassType=ClassInf.RECORD(_))), _)
equation
expl = List.map1(varLst, Expression.generateCrefsExpFromExpVar, cr);
(_, outTuple) = Expression.traverseExpList(expl, checkEquationsUnknownCrefsExp, inTuple);
then (e, outTuple);

// special case for arrays
case (e as DAE.CREF(ty = DAE.T_ARRAY()), _)
equation
(e1, true) = Expression.extendArrExp(e, false);
(_, outTuple) = Expression.traverseExpBottomUp(e1, checkEquationsUnknownCrefsExp, inTuple);
then (e, outTuple);

// case for function pointers
case (DAE.CREF(ty=DAE.T_FUNCTION_REFERENCE_FUNC()), _)
then (inExp, inTuple);

// already there
case (DAE.CREF(componentRef = cr), (_, _, ht))
equation
_ = BaseHashTable.get(cr, ht);
then (inExp, inTuple);

// known
case (DAE.CREF(componentRef = cr), (vars, _, _))
algorithm
(var_lst, _) := BackendVariable.getVar(cr, vars);
for var in var_lst loop
true := BackendVariable.isVarConst(var) or BackendVariable.isParam(var);
end for;
then (inExp, inTuple);

case (DAE.CREF(componentRef = cr), (_, knvars, _))
algorithm
(var_lst, _) := BackendVariable.getVar(cr, knvars);
for var in var_lst loop
false := BackendVariable.isInput(var);
end for;
then (inExp, inTuple);

// add it
case (DAE.CREF(componentRef = cr), (vars, knvars, ht))
equation
ht = BaseHashTable.add((cr, 0), ht);
then (inExp, (vars, knvars, ht));

else (inExp,inTuple);
end matchcontinue;
end checkEquationsUnknownCrefsExpNotConst;

public function equationUnknownCrefs "author: Frenkel TUD 2012-05
From the equation and a variable array return all
variables in the equation an not in the variable array."
Expand Down Expand Up @@ -2312,11 +2229,12 @@ public function makeTmpEqnForExp

protected
DAE.ComponentRef cr;
DAE.ComponentRef cr_time = ComponentReference.makeCrefIdent("time", DAE.T_REAL_DEFAULT , {});
BackendDAE.Var tmpvar;
String name_ = "OMC__HELPER__VAR" + intString(offset) + "$" + name;
DAE.Exp x, y;
BackendDAE.Equation eqn;
list<DAE.ComponentRef> cr_lst;
list<BackendDAE.Var> eqnVars, eqnKnVars, inputsKnVars, paramKnVars;
BackendDAE.Variables knowVars = BackendVariable.daeKnVars(oshared);
Boolean b;

Expand All @@ -2325,26 +2243,28 @@ algorithm
(y, _) := ExpressionSimplify.simplify(iExp);
b := Expression.isCref(y) or Expression.isConst(y);
if not b then

cr := ComponentReference.makeCrefIdent(name_, DAE.T_REAL_DEFAULT , {});
tmpvar := BackendVariable.makeVar(cr);
x := Expression.crefExp(cr);
oExp := x;

eqn := BackendDAE.EQUATION(x, y, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);
//BackendDump.printEquation(eqn);
cr_lst := equationUnknownCrefsNotConst({eqn}, ivars, knowVars);
eqnVars := equationVars(eqn, ivars);
eqnKnVars := equationVars(eqn, knowVars);

(inputsKnVars,_) := List.splitOnTrue(eqnKnVars, BackendVariable.isInput);

b := match(cr_lst)
local DAE.ComponentRef cr1;
case({}) then true;
case({cr1}) then ComponentReference.crefEqualNoStringCompare(cr,cr1);
else false;
end match;
b := List.isEmpty(eqnVars) and List.isEmpty(inputsKnVars) and not Expression.expHasCref(y,cr_time);

if b then
tmpvar := BackendVariable.setBindExp(tmpvar, SOME(y));
tmpvar := BackendVariable.setVarKind(tmpvar, BackendDAE.CONST());
(paramKnVars,_) := List.splitOnTrue(eqnKnVars, BackendVariable.isParam);
if List.isEmpty(paramKnVars) then
tmpvar := BackendVariable.setVarKind(tmpvar, BackendDAE.CONST());
else
tmpvar := BackendVariable.setVarKind(tmpvar, BackendDAE.PARAM());
end if;
oshared := BackendVariable.addKnVarDAE(tmpvar, oshared);
else
oeqns := BackendEquation.addEquation(eqn, oeqns);
Expand Down
32 changes: 14 additions & 18 deletions Compiler/BackEnd/ResolveLoops.mo
Expand Up @@ -2246,10 +2246,10 @@ protected
array<DAE.Exp> b = arrayCreate(n,DAE.RCONST(0.0));
array<DAE.Exp> A = arrayCreate(n*n,DAE.RCONST(0.0));
array<DAE.Exp> ax = arrayCreate(n,DAE.RCONST(0.0));
array<DAE.Exp> solvedX = arrayCreate(n,DAE.RCONST(0.0));
array<DAE.Exp> scaled_x = arrayCreate(n,DAE.RCONST(0.0));
array<DAE.Exp> scaleA = arrayCreate(n,DAE.RCONST(0.0));

DAE.Exp a, x, eqn_exp, eqn_scalar;
DAE.Exp a, x;
Integer m, ii, jj, mm;
list<DAE.Exp> x_lst = List.map(cr_x, Expression.crefExp);
DAE.ComponentRef cr;
Expand Down Expand Up @@ -2311,30 +2311,26 @@ algorithm
end if;
m := m + 1;
end for;

//rescale x
// scale_x = x/factor
for i in 1:n loop
a := Expression.expMul(arrayGet(ax,i), arrayGet(scaleA,i));
(a, oeqns, ovars, oshared) := BackendEquation.makeTmpEqnForExp(a, "QR$sx$" + intString(i), offset, oeqns, ovars, oshared);
arrayUpdate(scaled_x,i,a);
end for;

//qrDecomposition3(ax, n, false, "x");

// A*x = b -> R*x = Q'b
(R, Qb, oeqns, ovars, oshared) := qrDecomposition(A, n, b, oeqns, ovars, offset, oshared);

// update R scaling
for i in 1:n loop
m := (i-1)*n;
for j in 1:n loop
a := arrayGet(R,j+m);
if not Expression.isZero(a) then
a := Expression.expMul(a, arrayGet(scaleA,j));
//(a, oeqns, ovars, oshared) := BackendEquation.makeTmpEqnForExp(a, "QR$sR$" + intString(i + (j-1)*n), offset, oeqns, ovars, oshared);
arrayUpdate(R, j+m, a);
end if;
end for;
end for;

// R*x = Q'*b
// R*x = Q'*b where x is scaled
for i in n:-1:1 loop
m := (i-1)*n;
a := Expression.makeSum1(list(Expression.expMul(arrayGet(R, m + j), arrayGet(ax, j)) for j in i:n));
a := Expression.makeSum1(list(Expression.expMul(arrayGet(R, m + j), arrayGet(scaled_x, j)) for j in i:n));
eqn := BackendDAE.EQUATION(a, arrayGet(Qb,i), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);
eqn := BackendEquation.solveEquation(eqn, arrayGet(ax,i), NONE());
eqn := BackendEquation.solveEquation(eqn, arrayGet(scaled_x,i), NONE());
oeqns := BackendEquation.addEquation(eqn, oeqns);
end for;

Expand Down

0 comments on commit 74cdd70

Please sign in to comment.