Skip to content

Commit

Permalink
Use nominal value to define threshold for param division
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaeuber authored and OpenModelica-Hudson committed Mar 29, 2017
1 parent 5351a6e commit e36b947
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 41 deletions.
69 changes: 45 additions & 24 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -5014,7 +5014,7 @@ protected function adjacencyRowEnhanced2
input BackendDAE.Variables globalKnownVars;
output BackendDAE.Solvability oSolvab;
algorithm
oSolvab := matchcontinue(cr,e,crlst,vars,globalKnownVars)
oSolvab := match(cr,e,crlst,vars,globalKnownVars)
local
Boolean b,b1,b2;
case(_,_,{},_,_)
Expand All @@ -5023,9 +5023,7 @@ algorithm
b2 = Expression.isConstOne(e) or Expression.isConstMinusOne(e);
then
if b2 then BackendDAE.SOLVABILITY_CONSTONE() else BackendDAE.SOLVABILITY_CONST(not b1);
case(_,_,_,_,_)
equation
true = List.isMemberOnTrue(cr,crlst,ComponentReference.crefEqualNoStringCompare);
case(_,_,_,_,_) guard List.isMemberOnTrue(cr,crlst,ComponentReference.crefEqualNoStringCompare)
then
BackendDAE.SOLVABILITY_NONLINEAR();
case(_,_,_,_,_)
Expand All @@ -5034,7 +5032,7 @@ algorithm
b2 = containAnyVar(crlst,vars);
then
adjacencyRowEnhanced3(b1,b2,cr,e,crlst,vars,globalKnownVars);
end matchcontinue;
end match;
end adjacencyRowEnhanced2;

protected function adjacencyRowEnhanced3
Expand All @@ -5050,13 +5048,13 @@ protected function adjacencyRowEnhanced3
input BackendDAE.Variables globalKnownVars;
output BackendDAE.Solvability oSolvab;
algorithm
oSolvab := matchcontinue(b1,b2,cr,e,crlst,vars,globalKnownVars)
oSolvab := match(b1,b2,cr,e,crlst,vars,globalKnownVars)
local
Boolean b,b_1;
DAE.Exp e1;
DAE.Exp e1, nominal;
case(true,true,_,_,_,_,_)
equation
(e1,_) = Expression.traverseExpBottomUp(e, replaceVartraverser, globalKnownVars);
(e1,_) = Expression.traverseExpBottomUp(e, replaceVarWithValue, globalKnownVars);
(e1,_) = ExpressionSimplify.simplify(e1);
b = not Expression.isZeroOrAlmostZero(e1);
then
Expand All @@ -5067,28 +5065,25 @@ algorithm
then
BackendDAE.SOLVABILITY_LINEAR(b);
case(true,_,_,_,_,_,_)
equation
(e1,_) = Expression.traverseExpBottomUp(e, replaceVartraverser, globalKnownVars);
(e1,_) = ExpressionSimplify.simplify(e1);
b = not Expression.isZeroOrAlmostZero(e1);
b_1 = Expression.isConst(e1);
algorithm
(nominal,_) := Expression.traverseExpBottomUp(e, replaceVarWithNominal, globalKnownVars);
(nominal,_) := ExpressionSimplify.simplify(nominal);
(e1,_) := Expression.traverseExpBottomUp(e, replaceVarWithValue, globalKnownVars);
(e1,_) := ExpressionSimplify.simplify(e1);
b := not Expression.isZeroOrAlmostZero(e1, nominal);
b_1 := Expression.isConst(e1);
then
if b_1 then BackendDAE.SOLVABILITY_PARAMETER(b) else BackendDAE.SOLVABILITY_LINEAR(b);
case(_,_,_,_,_,_,_)
equation
b = not Expression.isZeroOrAlmostZero(e);
then
BackendDAE.SOLVABILITY_LINEAR(b);
/* case(_,_,_,_,_,_,_)
equation
BackendDump.debugStrCrefStrExpStr(("Warning cannot calculate solvabilty for",cr," in ",e,"\n"));
then
BackendDAE.SOLVABILITY_LINEAR(true);
*/ end matchcontinue;
end match;
end adjacencyRowEnhanced3;

protected function replaceVartraverser
"Helper function to adjacencyRowEnhanced3. Traverser to replace variables(parameters) with there bind expression."
protected function replaceVarWithValue
"Helper function to adjacencyRowEnhanced3. Traverser to replace variables(parameters) with their bind expression."
input DAE.Exp inExp;
input BackendDAE.Variables inVars;
output DAE.Exp outExp;
Expand All @@ -5105,21 +5100,47 @@ algorithm
equation
(v::_,_) = BackendVariable.getVar(cr,vars);
e = BackendVariable.varBindExp(v);
(e,_) = Expression.traverseExpBottomUp(e, replaceVartraverser, vars);
(e,_) = Expression.traverseExpBottomUp(e, replaceVarWithValue, vars);
then (e, vars);

case (DAE.CREF(componentRef=cr),vars)
equation
(v::_,_) = BackendVariable.getVar(cr,vars);
true = BackendVariable.varFixed(v);
e = BackendVariable.varBindExpStartValue(v);
(e,_) = Expression.traverseExpBottomUp(e, replaceVartraverser, vars);
(e,_) = Expression.traverseExpBottomUp(e, replaceVarWithValue, vars);
then (e, vars);

else (inExp,inVars);

end matchcontinue;
end replaceVartraverser;
end replaceVarWithValue;

protected function replaceVarWithNominal
"Helper function to adjacencyRowEnhanced3. Traverser to replace variables(parameters) with their nominal values."
input DAE.Exp inExp;
input BackendDAE.Variables inVars;
output DAE.Exp outExp;
output BackendDAE.Variables outVars;
algorithm
(outExp,outVars) := matchcontinue (inExp,inVars)
local
DAE.ComponentRef cr;
BackendDAE.Variables vars;
BackendDAE.Var v;
DAE.Exp nom;

case (DAE.CREF(componentRef=cr),vars)
equation
(v::_,_) = BackendVariable.getVar(cr,vars);
nom = BackendVariable.getVarNominalValue(v);
(nom,_) = Expression.traverseExpBottomUp(nom, replaceVarWithNominal, vars);
then (nom, vars);

else (inExp,inVars);

end matchcontinue;
end replaceVarWithNominal;

protected function adjacencyRowExpEnhanced
"author: Frenkel TUD 2012-05
Expand Down
4 changes: 4 additions & 0 deletions Compiler/BackEnd/Tearing.mo
Expand Up @@ -185,6 +185,7 @@ algorithm
algorithm
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nTearing type: heuristic\n");
print("Tearing strictness: " + Flags.getConfigString(Flags.TEARING_STRICTNESS) + "\n");
end if;
(ocomp,outRunMatching) := omcTearing(isyst, ishared, eindex, vindx, ojac, jacType, mixedSystem);
if debug then execStat("Tearing.omcTearing"); end if;
Expand All @@ -194,6 +195,7 @@ algorithm
algorithm
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nTearing type: heuristic\n");
print("Tearing strictness: " + Flags.getConfigString(Flags.TEARING_STRICTNESS) + "\n");
end if;
(ocomp,outRunMatching) := CellierTearing(isyst, ishared, eindex, vindx, userTVars, ojac, jacType, mixedSystem, strongComponentIndex);
if debug then execStat("Tearing.CellierTearing"); end if;
Expand All @@ -203,6 +205,7 @@ algorithm
algorithm
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nTearing type: total\n");
print("Tearing strictness: " + Flags.getConfigString(Flags.TEARING_STRICTNESS) + "\n");
end if;
(ocomp,outRunMatching) := totalTearing(isyst, ishared, eindex, vindx, ojac, jacType, mixedSystem);
if debug then execStat("Tearing.totalTearing"); end if;
Expand All @@ -212,6 +215,7 @@ algorithm
algorithm
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nTearing type: user defined\n");
print("Tearing strictness: " + Flags.getConfigString(Flags.TEARING_STRICTNESS) + "\n");
end if;
(ocomp,outRunMatching) := userDefinedTearing(isyst, ishared, eindex, vindx, ojac, jacType, mixedSystem, userTVars, userResiduals);
if debug then execStat("Tearing.userDefinedTearing"); end if;
Expand Down
58 changes: 41 additions & 17 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -7511,17 +7511,26 @@ algorithm
list<DAE.Exp> ae;
list<list<DAE.Exp>> matrix;

case (DAE.ICONST(integer = ival)) then intEq(ival,0);
case (DAE.RCONST(real = rval)) then realEq(rval,0.0);
case (DAE.CAST(exp = e)) then isZero(e);
case (DAE.ICONST(integer = ival))
then intEq(ival,0);

case(DAE.UNARY(DAE.UMINUS(_),e)) then isZero(e);
case(DAE.ARRAY(array = ae)) then List.mapAllValueBool(ae,isZero,true);
case (DAE.RCONST(real = rval))
then realEq(rval,0.0);

case (DAE.CAST(exp = e))
then isZero(e);

case (DAE.UNARY(DAE.UMINUS(_),e))
then isZero(e);

case (DAE.ARRAY(array = ae))
then List.mapAllValueBool(ae,isZero,true);

case (DAE.MATRIX(matrix = matrix))
then List.mapListAllValueBool(matrix,isZero,true);
then List.mapListAllValueBool(matrix,isZero,true);

case(DAE.UNARY(DAE.UMINUS_ARR(_),e)) then isZero(e);
case (DAE.UNARY(DAE.UMINUS_ARR(_),e))
then isZero(e);

else false;

Expand All @@ -7533,30 +7542,45 @@ public function isZeroOrAlmostZero
"Returns true if an expression is constant
and zero or near to zero, otherwise false"
input DAE.Exp inExp;
input DAE.Exp nominal = DAE.RCONST(1.0);
output Boolean outBoolean;
algorithm
outBoolean := match (inExp)
outBoolean := match (inExp, nominal)
local
Integer ival;
Real rval;
Type t;
DAE.Exp e,e1;
list<DAE.Exp> ae;
list<list<DAE.Exp>> matrix;
Real rNom;

case (DAE.ICONST(integer = ival)) then intEq(ival,0);
case (DAE.RCONST(real = rval)) then realLt(abs(rval),1e-15);
case (DAE.CAST(exp = e)) then isZeroOrAlmostZero(e);
case (DAE.ICONST(integer = ival),_)
then intEq(ival,0);

case(DAE.UNARY(DAE.UMINUS(_),e)) then isZeroOrAlmostZero(e);
case(DAE.ARRAY(array = ae)) then List.mapAllValueBool(ae,isZeroOrAlmostZero,true);
case (DAE.RCONST(real = rval), DAE.RCONST(real=rNom))
then realLt(abs(rval),1e-6*abs(rNom));

case (DAE.MATRIX(matrix = matrix))
then List.mapListAllValueBool(matrix,isZeroOrAlmostZero,true);
case (DAE.RCONST(real = rval),_)
then realLt(abs(rval),1e-6);

case (DAE.CAST(exp = e),_)
then isZeroOrAlmostZero(e, nominal);

case (DAE.UNARY(DAE.UMINUS(_),e),_)
then isZeroOrAlmostZero(e, nominal);

case (DAE.ARRAY(array = ae),_)
then List.map1AllValueBool(ae,isZeroOrAlmostZero,true,nominal);

case (DAE.MATRIX(matrix = matrix),_)
then List.map1ListAllValueBool(matrix,isZeroOrAlmostZero,true,nominal);

case(DAE.UNARY(DAE.UMINUS_ARR(_),e)) then isZeroOrAlmostZero(e);
case (DAE.UNARY(DAE.UMINUS_ARR(_),e),_)
then isZeroOrAlmostZero(e, nominal);

case(DAE.IFEXP(_,e,e1)) then (isZeroOrAlmostZero(e) or isZeroOrAlmostZero(e1));
case (DAE.IFEXP(_,e,e1),_)
then (isZeroOrAlmostZero(e, nominal) or isZeroOrAlmostZero(e1, nominal));

else false;

Expand Down
44 changes: 44 additions & 0 deletions Compiler/Util/List.mo
Expand Up @@ -2948,6 +2948,28 @@ algorithm
end try;
end mapAllValueBool;

public function map1AllValueBool<TI, TO, VT, ArgT1>
"Same as mapAllValueBool, but takes one extra argument."
input list<TI> inList;
input MapFunc inMapFunc;
input VT inValue;
input ArgT1 inArg1;
output Boolean outAllValue;

partial function MapFunc
input TI inElement;
input ArgT1 inArg1;
output TO outElement;
end MapFunc;
algorithm
try
map1AllValue(inList, inMapFunc, inValue, inArg1);
outAllValue := true;
else
outAllValue := false;
end try;
end map1AllValueBool;

public function map1AllValue<TI, TO, VT, ArgT1>
"Applies a function to all elements in the lists, and fails if not all
elements are equal to the given value. This function also takes an extra
Expand Down Expand Up @@ -3040,6 +3062,28 @@ algorithm
end for;
end mapListAllValueBool;

public function map1ListAllValueBool<TI, TO, VT, ArgT1>
"Same as mapListAllValueBool, but takes one extra argument."
input list<list<TI>> inList;
input MapFunc inMapFunc;
input VT inValue;
input ArgT1 inArg1;
output Boolean outAllValue = true;

partial function MapFunc
input TI inElement;
input ArgT1 inArg1;
output TO outElement;
end MapFunc;
algorithm
for lst in inList loop
if not map1AllValueBool(lst, inMapFunc, inValue, inArg1) then
outAllValue := false;
return;
end if;
end for;
end map1ListAllValueBool;

public function foldAllValue<TI, TO, ArgT1>
"Applies a function to all elements in the lists, and fails if not all
elements are equal to the given value. This function also takes an extra
Expand Down

0 comments on commit e36b947

Please sign in to comment.