Skip to content

Commit 9db0fd5

Browse files
[BE] Fix output filter for states (#10251)
If a state is removed, der-calls also have to be replaced by new parameter crefs. Fixes #8271 Co-authored-by: Karim Abdelhak <karim.abdelhak@fh-bielefeld.de> --------- Co-authored-by: kabdelhak <karim.abdelhak@fh-bielefeld.de>
1 parent 9dca89d commit 9db0fd5

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

OMCompiler/Compiler/BackEnd/BackendDAEOptimize.mo

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import SCode;
8787
import SynchronousFeatures;
8888
import Tearing;
8989
import Types;
90+
import UnorderedMap;
9091
import Util;
9192
import Values;
9293

@@ -5777,6 +5778,8 @@ protected
57775778
Integer systemNumber=0, numberOfSystems;
57785779

57795780
list<Integer> eqIndLst, eqIndexLst = {};
5781+
UnorderedMap<DAE.ComponentRef, DAE.Exp> der_replacement;
5782+
BackendDAE.Var derVar;
57805783

57815784
constant Boolean debug = false;
57825785
algorithm
@@ -5897,8 +5900,21 @@ algorithm
58975900
if debug then print("No output variables in this system ("+intString(systemNumber)+"/"+intString(numberOfSystems)+")\n"); end if;
58985901
end if;
58995902

5903+
// make unneeded state derivatives and add them to unneeded vars
5904+
der_replacement := UnorderedMap.new<DAE.Exp>(ComponentReference.hashComponentRef, ComponentReference.crefEqual);
5905+
for state in BackendVariable.varList(vars) loop
5906+
if BackendVariable.isStateVar(state) then
5907+
derVar := BackendVariable.makeVar(ComponentReference.prependStringCref("$DER_REM_", state.varName));
5908+
UnorderedMap.add(state.varName, Expression.crefExp(derVar.varName), der_replacement);
5909+
vars := BackendVariable.addVar(derVar, vars);
5910+
end if;
5911+
end for;
5912+
5913+
// replace unneeded der() calls with derivative crefs
5914+
eqs := BackendEquation.traverseEquationArray_WithUpdate(eqs, function BackendEquation.traverseExpsOfEquation(inFunc = replaceDerCallOutputsOnly), der_replacement);
5915+
59005916
// make unneeded vars parameters and equations initial equations
5901-
(vars, _) := BackendVariable.traverseBackendDAEVarsWithUpdate(vars, BackendVariable.makeParamFixed, false);
5917+
(vars, _) := BackendVariable.traverseBackendDAEVarsWithUpdate(vars, BackendVariable.makeParamOutputsOnly, false);
59025918
(eqs, _) := BackendEquation.traverseEquationArray_WithUpdate(eqs,BackendEquation.setEquationKind, BackendDAE.INITIAL_EQUATION());
59035919

59045920
// add unneeded variables and equations
@@ -5919,6 +5935,18 @@ algorithm
59195935
b := intLt(arrayGet(varArr,idx),0);
59205936
end stateVarIsNotVisited;
59215937

5938+
protected function replaceDerCallOutputsOnly
5939+
input output DAE.Exp exp;
5940+
input output UnorderedMap<DAE.ComponentRef, DAE.Exp> der_replacement;
5941+
algorithm
5942+
exp := match exp
5943+
local
5944+
DAE.ComponentRef cr;
5945+
case DAE.CALL(path = Absyn.IDENT("der"), expLst = {DAE.CREF(cr)})
5946+
then UnorderedMap.getOrDefault(cr, der_replacement, exp);
5947+
else exp;
5948+
end match;
5949+
end replaceDerCallOutputsOnly;
59225950

59235951
// =============================================================================
59245952
// section for initOptModule >>inlineHomotopy<<

OMCompiler/Compiler/BackEnd/BackendVariable.mo

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,14 +1248,18 @@ algorithm
12481248
var.varKind := BackendDAE.PARAM();
12491249
end makeParam;
12501250

1251-
public function makeParamFixed
1251+
public function makeParamOutputsOnly
12521252
"Change variable to parameter"
12531253
input output BackendDAE.Var var;
12541254
input output Boolean fixed; // also output for traversing
12551255
algorithm
12561256
var.varKind := BackendDAE.PARAM();
12571257
var := setVarFixed(var, fixed);
1258-
end makeParamFixed;
1258+
var.values := if isSome(var.values) then var.values else SOME(getVariableAttributefromType(var.varType));
1259+
if isNone(DAEUtil.getFixedAttr(var.values)) then
1260+
var.values := DAEUtil.setFixedAttr(var.values, SOME(DAE.BCONST(fixed)));
1261+
end if;
1262+
end makeParamOutputsOnly;
12591263

12601264
public function isParamOrConstant
12611265
"Return true if variable is parameter or constant"

OMCompiler/Compiler/FrontEnd/DAEUtil.mo

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,21 @@ algorithm
12961296
end match;
12971297
end setFixedAttr;
12981298

1299+
public function getFixedAttr "
1300+
retrieves the protected attribute form VariableAttributes."
1301+
input Option<DAE.VariableAttributes> attr;
1302+
output Option<DAE.Exp> isFixed;
1303+
algorithm
1304+
isFixed := match(attr)
1305+
case (SOME(DAE.VAR_ATTR_REAL(fixed=isFixed))) then isFixed;
1306+
case (SOME(DAE.VAR_ATTR_INT(fixed=isFixed))) then isFixed;
1307+
case (SOME(DAE.VAR_ATTR_BOOL(fixed=isFixed))) then isFixed;
1308+
case (SOME(DAE.VAR_ATTR_STRING(fixed=isFixed))) then isFixed;
1309+
case (SOME(DAE.VAR_ATTR_ENUMERATION(fixed=isFixed))) then isFixed;
1310+
else NONE();
1311+
end match;
1312+
end getFixedAttr;
1313+
12991314
public function setFinalAttr "
13001315
sets the start attribute. If NONE(), assumes Real attributes."
13011316
input Option<DAE.VariableAttributes> attr;

0 commit comments

Comments
 (0)