Skip to content

Commit

Permalink
- code simplification of function getMinMaxAsserts
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24589 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
lochel committed Feb 16, 2015
1 parent 53ee5b5 commit 6ae1d9a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 63 deletions.
2 changes: 0 additions & 2 deletions Compiler/BackEnd/BackendDAECreate.mo
Expand Up @@ -605,7 +605,6 @@ algorithm
b = DAEUtil.boolVarVisibility(protection);
dae_var_attr = DAEUtil.setProtectedAttr(dae_var_attr, b);
dae_var_attr = setMinMaxFromEnumeration(t, dae_var_attr);
_ = BackendVariable.getMinMaxAsserts(dae_var_attr, name, source, kind_1, tp, {});
_ = BackendVariable.getNominalAssert(dae_var_attr, name, source, kind_1, tp, {});
(dae_var_attr, source, _) = Inline.inlineStartAttribute(dae_var_attr, source, (SOME(functionTree), {DAE.NORM_INLINE()}));
ts = BackendDAEUtil.setTearingSelectAttribute(comment);
Expand Down Expand Up @@ -666,7 +665,6 @@ algorithm
b = DAEUtil.boolVarVisibility(protection);
dae_var_attr = DAEUtil.setProtectedAttr(dae_var_attr, b);
dae_var_attr = setMinMaxFromEnumeration(t, dae_var_attr);
_ = BackendVariable.getMinMaxAsserts(dae_var_attr, name, source, kind_1, tp, {});
_ = BackendVariable.getNominalAssert(dae_var_attr, name, source, kind_1, tp, {});
fnstpl = (SOME(functionTree), {DAE.NORM_INLINE()});
// build algorithms for the inlined asserts
Expand Down
68 changes: 33 additions & 35 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -1960,46 +1960,44 @@ algorithm
end getVarType;

public function getMinMaxAsserts "author: Frenkel TUD 2011-03"
input Option<DAE.VariableAttributes> attr;
input DAE.ComponentRef name;
input DAE.ElementSource source;
input BackendDAE.VarKind kind;
input BackendDAE.Type vartype;
input list<DAE.Algorithm> iMinmax;
output list<DAE.Algorithm> oMinmax;
input BackendDAE.Var inVar;
input list<DAE.Algorithm> inAsserts;
output BackendDAE.Var outVar := inVar;
output list<DAE.Algorithm> outAsserts;
algorithm
oMinmax :=
matchcontinue (attr,name,source,kind,vartype,iMinmax)
outAsserts := matchcontinue(inVar)
local
DAE.Exp e,cond,msg;
list<Option<DAE.Exp>> ominmax;
DAE.Exp e, cond, msg;
list<Option<DAE.Exp>> minmax;
String str, format;
DAE.Type tp;
DAE.ComponentRef name;
Option<DAE.VariableAttributes> attr;
BackendDAE.Type varType;
DAE.ElementSource source;

case(_,_,_,BackendDAE.CONST(),_,_) then iMinmax;
case (_,_,_,_,_,_)
equation
ominmax = DAEUtil.getMinMax(attr);
str = ComponentReference.printComponentRefStr(name);
str = stringAppendList({"Variable ",str," out of [min, max] interval: "});
e = Expression.crefExp(name);
tp = BackendDAEUtil.makeExpType(vartype);
cond = getMinMaxAsserts1(ominmax,e,tp);
(cond,_) = ExpressionSimplify.simplify(cond);
// do not add if const true
false = Expression.isConstTrue(cond);
str = str + ExpressionDump.printExpStr(cond) + " has value: ";
// if is real use %g otherwise use %d (ints and enums)
format = if Types.isRealOrSubTypeReal(tp) then "g" else "d";
msg = DAE.BINARY(
DAE.SCONST(str),
DAE.ADD(DAE.T_STRING_DEFAULT),
DAE.CALL(Absyn.IDENT("String"), {e, DAE.SCONST(format)}, DAE.callAttrBuiltinString)
);
BackendDAEUtil.checkAssertCondition(cond,msg,DAE.ASSERTIONLEVEL_WARNING,DAEUtil.getElementSourceFileInfo(source));
then
DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond,msg,DAE.ASSERTIONLEVEL_WARNING,source)})::iMinmax;
else iMinmax;
case BackendDAE.VAR(varKind=BackendDAE.CONST())
then inAsserts;

case BackendDAE.VAR(varName=name, values=attr, varType=varType, source=source) equation
minmax = DAEUtil.getMinMax(attr);
e = Expression.crefExp(name);
tp = BackendDAEUtil.makeExpType(varType);

// do not add if const true
cond = getMinMaxAsserts1(minmax, e, tp);
(cond, _) = ExpressionSimplify.simplify(cond);
false = Expression.isConstTrue(cond);

str = "Variable " + ComponentReference.printComponentRefStr(name) + " out of [min, max] interval: ";
str = str + ExpressionDump.printExpStr(cond) + " has value: ";
// if is real use %g otherwise use %d (ints and enums)
format = if Types.isRealOrSubTypeReal(tp) then "g" else "d";
msg = DAE.BINARY(DAE.SCONST(str), DAE.ADD(DAE.T_STRING_DEFAULT), DAE.CALL(Absyn.IDENT("String"), {e, DAE.SCONST(format)}, DAE.callAttrBuiltinString));
BackendDAEUtil.checkAssertCondition(cond, msg, DAE.ASSERTIONLEVEL_WARNING, DAEUtil.getElementSourceFileInfo(source));
then DAE.ALGORITHM_STMTS({DAE.STMT_ASSERT(cond, msg, DAE.ASSERTIONLEVEL_WARNING, source)})::inAsserts;

else inAsserts;
end matchcontinue;
end getMinMaxAsserts;

Expand Down
28 changes: 2 additions & 26 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -5564,7 +5564,7 @@ algorithm
case (BackendDAE.EQSYSTEM(orderedVars = vars), BackendDAE.SHARED(), (uniqueEqIndex, simeqns))
equation
// get minmax and nominal asserts
res = BackendVariable.traverseBackendDAEVars(vars, createVarMinMaxAssert, {});
res = BackendVariable.traverseBackendDAEVars(vars, BackendVariable.getMinMaxAsserts, {});
(result, uniqueEqIndex) = List.mapFold(res, dlowAlgToSimEqSystem, uniqueEqIndex);
result = listAppend(result, simeqns);
then ((uniqueEqIndex, result));
Expand Down Expand Up @@ -6928,7 +6928,7 @@ protected function createVarAsserts
input BackendDAE.Var inVar;
output list<DAE.Algorithm> outAlgs;
algorithm
(_, outAlgs) := createVarMinMaxAssert(inVar, {});
(_, outAlgs) := BackendVariable.getMinMaxAsserts(inVar, {});
(_, outAlgs) := createVarNominalAssert(inVar, outAlgs);
end createVarAsserts;

Expand Down Expand Up @@ -6956,30 +6956,6 @@ algorithm
end matchcontinue;
end createVarNominalAssert;

protected function createVarMinMaxAssert
input BackendDAE.Var inVar;
input list<DAE.Algorithm> inAsserts;
output BackendDAE.Var outVar;
output list<DAE.Algorithm> asserts;
algorithm
(outVar,asserts) := matchcontinue (inVar,inAsserts)
local
BackendDAE.Var var;
DAE.ComponentRef name;
DAE.ElementSource source;
BackendDAE.VarKind kind;
Option<DAE.VariableAttributes> attr;
BackendDAE.Type varType;

case (var as BackendDAE.VAR(varName=name, varKind=kind, values = attr, varType=varType, source = source), asserts)
equation
asserts = BackendVariable.getMinMaxAsserts(attr, name, source, kind, varType, asserts);
then (var, asserts);

else (inVar,inAsserts);
end matchcontinue;
end createVarMinMaxAssert;

public function createModelInfo
input Absyn.Path class_;
input BackendDAE.BackendDAE dlow;
Expand Down

0 comments on commit 6ae1d9a

Please sign in to comment.