Skip to content

Commit

Permalink
- changed merge of nominal values for alias variables
Browse files Browse the repository at this point in the history
   - don't calculate arithmetic mean instead choose via componentReference depth (like for start values)
   - also don't negate nominal values
 - one testcase fails for some different reasons(non-linear solver), so deactived for now(Modelica.Fluid.Examples.BranchingDynamicPipes.mos).
   



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@19458 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Willi Braun committed Mar 5, 2014
1 parent 3995d2c commit 13f27fb
Showing 1 changed file with 59 additions and 61 deletions.
120 changes: 59 additions & 61 deletions Compiler/BackEnd/RemoveSimpleEquations.mo
Expand Up @@ -121,10 +121,10 @@ end SimpleContainer;
protected type AccTuple = tuple<BackendDAE.Variables, BackendDAE.Shared, list<BackendDAE.Equation>, list<SimpleContainer>, Integer, array<list<Integer>>, Boolean>;

protected type VarSetAttributes =
tuple<Boolean, tuple<Integer, list<tuple<Option<DAE.Exp>, DAE.ComponentRef>>>, Option<DAE.Exp>, Integer, tuple<Option<DAE.Exp>, Option<DAE.Exp>>>
"fixed, list<startvalue, origin, cr>, nominal, nNominals, min, max";
tuple<Boolean, tuple<Integer, list<tuple<Option<DAE.Exp>, DAE.ComponentRef>>>, list<tuple<DAE.Exp, DAE.ComponentRef>>, tuple<Option<DAE.Exp>, Option<DAE.Exp>>>
"fixed, list<startvalue, origin, cr>, nominal, (min, max)";

protected constant VarSetAttributes EMPTYVARSETATTRIBUTES = (false, (-1, {}), NONE(), 0, (NONE(), NONE()));
protected constant VarSetAttributes EMPTYVARSETATTRIBUTES = (false, (-1, {}), {}, (NONE(), NONE()));

/*
* fastAcausal
Expand Down Expand Up @@ -2599,12 +2599,12 @@ protected function addVarSetAttributes "author: Frenkel TUD 2012-12"
output VarSetAttributes oAttributes "fixed, list<startvalue, origin, cr>, nominal, min, max";
protected
Boolean fixed, fixedset;
Option<DAE.Exp> start, origin, nominalset;
Option<DAE.Exp> start, origin;
list<tuple<DAE.Exp, DAE.ComponentRef>> nominalset;
tuple<Option<DAE.Exp>, Option<DAE.Exp>> minmaxset;
Integer nNominal;
tuple<Integer, list<tuple<Option<DAE.Exp>, DAE.ComponentRef>>> startvalues;
algorithm
(fixedset, startvalues, nominalset, nNominal, minmaxset) := iAttributes;
(fixedset, startvalues, nominalset, minmaxset) := iAttributes;
// get attributes
// fixed
fixed := BackendVariable.varFixed(inVar);
Expand All @@ -2613,10 +2613,10 @@ algorithm
origin := BackendVariable.varStartOrigin(inVar);
(fixedset, startvalues) := addStartValue(fixed, fixedset, BackendVariable.varCref(inVar), start, origin, negate, mark, simpleeqnsarr, startvalues);
// nominal
(nominalset, nNominal) := addNominalValue(inVar, negate, nominalset, nNominal);
(nominalset) := addNominalValue(inVar, nominalset);
// minmax
minmaxset := addMinMaxAttribute(inVar, negate, mark, simpleeqnsarr, minmaxset);
oAttributes := (fixedset, startvalues, nominalset, nNominal, minmaxset);
oAttributes := (fixedset, startvalues, nominalset, minmaxset);
end addVarSetAttributes;

protected function addStartValue "author: Frenkel TUD 2012-12"
Expand Down Expand Up @@ -2680,30 +2680,21 @@ end addStartValue;

protected function addNominalValue "author: Frenkel TUD 2012-12"
input BackendDAE.Var inVar;
input Boolean negate;
input Option<DAE.Exp> iNominal;
input Integer iNNominals;
output Option<DAE.Exp> oNominal;
output Integer oNNominals;
input list<tuple<DAE.Exp, DAE.ComponentRef>> iNominal;
output list<tuple<DAE.Exp, DAE.ComponentRef>> oNominal;
algorithm
(oNominal, oNNominals) := matchcontinue(inVar, negate, iNominal, iNNominals)
(oNominal) := matchcontinue(inVar, iNominal)
local
DAE.Exp nominal, nominalset;

case(_, _, SOME(nominalset), _)
equation
nominal = BackendVariable.varNominalValue(inVar);
nominal = negateExpression(negate, nominal, nominal, " nominal_1 ");
nominalset = Expression.expAdd(nominal, nominalset);
then
(SOME(nominalset), iNNominals+1);
case(_, _, NONE(), _)
DAE.ComponentRef cr;

case(_, _)
equation
nominal = BackendVariable.varNominalValue(inVar);
nominal = negateExpression(negate, nominal, nominal, " nominal_2 ");
cr = BackendVariable.varCref(inVar);
then
(SOME(nominal), 1);
else then (iNominal, iNNominals);
((nominal, cr)::iNominal);
else then (iNominal);
end matchcontinue;
end addNominalValue;

Expand Down Expand Up @@ -2890,19 +2881,18 @@ algorithm
oVars := matchcontinue(iAttributes, inVar, i, iVars, ishared)
local
Boolean fixedset, isdiscrete;
Option<DAE.Exp> nominalset;
list<tuple<DAE.Exp, DAE.ComponentRef>> nominalset;
tuple<Option<DAE.Exp>, Option<DAE.Exp>> minmaxset;
Integer nNominal;
tuple<Integer, list<tuple<Option<DAE.Exp>, DAE.ComponentRef>>> startvalues;
BackendDAE.Var v;
BackendDAE.Variables vars;
case((fixedset, startvalues, nominalset, nNominal, minmaxset), _, _, _, _)
case((fixedset, startvalues, nominalset, minmaxset), _, _, _, _)
equation
isdiscrete = BackendVariable.isVarDiscrete(inVar);
// start and fixed
v = Debug.bcallret4(not isdiscrete, mergeStartFixedAttributes, inVar, fixedset, startvalues, ishared, inVar);
// nominal
v = mergeNominalAttribute(nominalset, nNominal, v);
v = mergeNominalAttribute(nominalset, v);
// min max
v = BackendVariable.setVarMinMax(v, minmaxset);
// update vars
Expand Down Expand Up @@ -2952,7 +2942,7 @@ algorithm
v = BackendVariable.setVarFixed(inVar, true);
// get all nonzero values
zerofreevalues = List.fold(values, getZeroFreeValues, {});
then selectFreeValue1(zerofreevalues, {}, "fixed Alias set with several free start values\n", v);
then selectFreeValue1(zerofreevalues, {}, "fixed Alias set with several free start values\n", "start", BackendVariable.setVarStartValue, v);

// fixed false only one start value -> nothing changed
case (_, false, (_, {(start, _)}), _)
Expand Down Expand Up @@ -3107,7 +3097,7 @@ protected function selectFreeValue "author: Frenkel TUD 2012-12"
algorithm
outVar := match(iZeroFreeValues, inVar)
case ({}, _) then inVar;
case (_, _) then selectFreeValue1(iZeroFreeValues, {}, "Alias set with several free start values\n", inVar);
case (_, _) then selectFreeValue1(iZeroFreeValues, {}, "Alias set with several free start values\n", "start", BackendVariable.setVarStartValue, inVar);
end match;
end selectFreeValue;

Expand Down Expand Up @@ -3147,10 +3137,17 @@ protected function selectFreeValue1 "author: Frenkel TUD 2012-12
input list<tuple<DAE.Exp, DAE.ComponentRef>> iZeroFreeValues;
input list<tuple<DAE.Exp, DAE.ComponentRef, Integer>> iFavorit;
input String iStr;
input String iAttributeName;
input FuncSetAttribute inFunc;
input BackendDAE.Var inVar;
output BackendDAE.Var outVar;
partial function FuncSetAttribute
input BackendDAE.Var iVar;
input DAE.Exp iExp;
output BackendDAE.Var oVar;
end FuncSetAttribute;
algorithm
outVar := matchcontinue(iZeroFreeValues, iFavorit, iStr, inVar)
outVar := matchcontinue(iZeroFreeValues, iFavorit, iStr, iAttributeName, inFunc, inVar)
local
DAE.Exp e, es;
DAE.ComponentRef cr, crs;
Expand All @@ -3161,38 +3158,38 @@ algorithm
list<tuple<DAE.Exp, DAE.ComponentRef, Integer>> rest;
String s;

case ({}, {}, _, _) then inVar;
case ({}, {}, _, _, _, _) then inVar;

// end of list analyse what we got
case ({}, rest, _, _) equation
case ({}, rest, _, _, _, _) equation
((e, cr, i)) = selectNonZeroExpression(rest);
s = iStr +& "=> select value from " +& ComponentReference.printComponentRefStr(cr) +& "(start = " +& ExpressionDump.printExpStr(e) +& ")\n" +&
s = iStr +& "=> select value from " +& ComponentReference.printComponentRefStr(cr) +& "(" +& iAttributeName +& " = " +& ExpressionDump.printExpStr(e) +& ")\n" +&
" because its component reference (or its binding component reference) is closer to the top level scope with depth: " +& intString(i) +& ".\n" +&
" If we have equal component reference depth for several components choose the one with non zero binding.";
Error.addMessage(Error.COMPILER_WARNING, {s});
v = BackendVariable.setVarStartValue(inVar, e);
v = inFunc(inVar, e);
then v;

// none, push it in
case ((e, cr)::zerofreevalues, {}, _, _) equation
s = iStr +& " * candidate: " +& ComponentReference.printComponentRefStr(cr) +& "(start = " +& ExpressionDump.printExpStr(e) +& ")\n";
case ((e, cr)::zerofreevalues, {}, _, _, _, _) equation
s = iStr +& " * candidate: " +& ComponentReference.printComponentRefStr(cr) +& "(" +& iAttributeName +& " = " +& ExpressionDump.printExpStr(e) +& ")\n";
i = selectMinDepth(ComponentReference.crefDepth(cr), e);
then selectFreeValue1(zerofreevalues, {(e, cr, i)}, s, inVar);
then selectFreeValue1(zerofreevalues, {(e, cr, i)}, s, iAttributeName, inFunc, inVar);

// equal, put it in
case ((e, cr)::zerofreevalues, (es, crs, is)::rest, _, _) equation
s = iStr +& " * candidate: " +& ComponentReference.printComponentRefStr(cr) +& "(start = " +& ExpressionDump.printExpStr(e) +& ")\n";
case ((e, cr)::zerofreevalues, (es, crs, is)::rest, _, _, _, _) equation
s = iStr +& " * candidate: " +& ComponentReference.printComponentRefStr(cr) +& "(" +& iAttributeName +& " = " +& ExpressionDump.printExpStr(e) +& ")\n";
i = selectMinDepth(ComponentReference.crefDepth(cr), e);
true = intEq(i, is);
favorit = (e, cr, i)::(es, crs, is)::rest;
then selectFreeValue1(zerofreevalues, favorit, s, inVar);
then selectFreeValue1(zerofreevalues, favorit, s, iAttributeName, inFunc, inVar);

// less than, remove all from list, return just this one
case ((e, cr)::zerofreevalues, (es, crs, is)::rest, _, _) equation
s = iStr +& " * candidate: " +& ComponentReference.printComponentRefStr(cr) +& "(start = " +& ExpressionDump.printExpStr(e) +& ")\n";
case ((e, cr)::zerofreevalues, (es, crs, is)::rest, _, _, _, _) equation
s = iStr +& " * candidate: " +& ComponentReference.printComponentRefStr(cr) +& "(" +& iAttributeName +& " = " +& ExpressionDump.printExpStr(e) +& ")\n";
i = selectMinDepth(ComponentReference.crefDepth(cr), e);
favorit = Util.if_(intLt(i, is), {(e, cr, i)}, iFavorit);
then selectFreeValue1(zerofreevalues, favorit, s, inVar);
then selectFreeValue1(zerofreevalues, favorit, s, iAttributeName, inFunc, inVar);
end matchcontinue;
end selectFreeValue1;

Expand Down Expand Up @@ -3221,28 +3218,29 @@ algorithm
end selectMinDepth;

protected function mergeNominalAttribute "author: Frenkel TUD 2012-12"
input Option<DAE.Exp> nominal;
input Integer n;
input list<tuple<DAE.Exp, DAE.ComponentRef>> nominalList;
input BackendDAE.Var inVar;
output BackendDAE.Var outVar;
algorithm
outVar := match (nominal, n, inVar)
outVar := matchcontinue (nominalList, inVar)
local
Real r;
DAE.Exp e;
String str;
DAE.ComponentRef varName;
list<DAE.Exp> allExp;


case (NONE(), _, _) then inVar;
case (SOME(e), _, BackendDAE.VAR(varName=varName)) equation
r = intReal(n);
e = Expression.expDiv(e, DAE.RCONST(r)); // Real is legal because only Reals have nominal attribute
(e, _) = ExpressionSimplify.simplify(e);
str = "Alias set with different nominal values\n=> select value for " +& ComponentReference.printComponentRefStr(varName) +& "(nominal = " +& ExpressionDump.printExpStr(e) +& ")";
Error.addMessage(Error.COMPILER_WARNING, {str});
then BackendVariable.setVarNominalValue(inVar, e);
end match;
case ({}, _) then inVar;

//check if all expressions are equal take just one
case (_, _)
equation
allExp = List.map(nominalList, Util.tuple21);
{e} = List.uniqueOnTrue(allExp, Expression.expEqual);
then BackendVariable.setVarNominalValue(inVar, e);

case (_, _)
then selectFreeValue1(nominalList, {}, "Alias set with different nominal values\n", "nominal", BackendVariable.setVarNominalValue, inVar);

end matchcontinue;
end mergeNominalAttribute;

/*
Expand Down

0 comments on commit 13f27fb

Please sign in to comment.