Skip to content

Commit

Permalink
[BE] fix output filter module (#10247)
Browse files Browse the repository at this point in the history
- fixes ticket #8271
 - the initial system might be causalized differently, therefore:
   - add all removed variables to parameters
   - add all remved equations to initial equations
 - ToDo: if the variables and equations are also not needed for intialization, the could be removed entirely

Co-authored-by: phannebohm <philip.hannebohm@fh-bielefeld.de>
  • Loading branch information
kabdelhak and phannebohm committed Feb 22, 2023
1 parent 759188c commit 7954d04
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
17 changes: 16 additions & 1 deletion OMCompiler/Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -5776,6 +5776,8 @@ protected
array<Integer> mapIncRowEqn;
Integer systemNumber=0, numberOfSystems;

list<Integer> eqIndLst, eqIndexLst = {};

constant Boolean debug = false;
algorithm
daeOut := daeIn;
Expand Down Expand Up @@ -5856,9 +5858,10 @@ algorithm
eqLstNew := {};
varLstNew := {};
for comp in compsNew loop
(varLst,_,eqLst,_) := BackendDAEUtil.getStrongComponentVarsAndEquations(comp,vars,eqs);
(varLst,_,eqLst,eqIndLst) := BackendDAEUtil.getStrongComponentVarsAndEquations(comp,vars,eqs);
varLstNew := listAppend(varLst,varLstNew);
eqLstNew := listAppend(eqLst,eqLstNew);
eqIndexLst := listAppend(eqIndLst,eqIndexLst);
end for;

// causalize again
Expand Down Expand Up @@ -5886,9 +5889,21 @@ algorithm
syst.removedEqs := BackendEquation.emptyEqns();

systsNew := syst::systsNew;

// find unneeded vars and equations
vars := BackendVariable.deleteVars(syst.orderedVars, vars);
eqs := BackendEquation.deleteList(eqs, eqIndexLst);
else
if debug then print("No output variables in this system ("+intString(systemNumber)+"/"+intString(numberOfSystems)+")\n"); end if;
end if;

// make unneeded vars parameters and equations initial equations
(vars, _) := BackendVariable.traverseBackendDAEVarsWithUpdate(vars, BackendVariable.makeParamFixed, false);
(eqs, _) := BackendEquation.traverseEquationArray_WithUpdate(eqs,BackendEquation.setEquationKind, BackendDAE.INITIAL_EQUATION());

// add unneeded variables and equations
shared.globalKnownVars := BackendVariable.addVariables(vars, shared.globalKnownVars);
shared.initialEqs := BackendEquation.addList(BackendEquation.equationList(eqs), shared.initialEqs);
end for;

// alias vars are not necessary anymore
Expand Down
24 changes: 23 additions & 1 deletion OMCompiler/Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -1914,11 +1914,33 @@ algorithm
case BackendDAE.COMPLEX_EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(kind=kind)) then kind;
case BackendDAE.IF_EQUATION(attr=BackendDAE.EQUATION_ATTRIBUTES(kind=kind)) then kind;
else equation
Error.addInternalError("BackendEquation.equationKind failed!", sourceInfo());
Error.addInternalError(getInstanceName() + " failed!", sourceInfo());
then fail();
end match;
end equationKind;

public function setEquationKind
input output BackendDAE.Equation eq;
input output BackendDAE.EquationKind k;
algorithm
eq := match eq
local
BackendDAE.EquationAttributes a;
case BackendDAE.EQUATION(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
case BackendDAE.ARRAY_EQUATION(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
case BackendDAE.FOR_EQUATION(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
case BackendDAE.SOLVED_EQUATION(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
case BackendDAE.RESIDUAL_EQUATION(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
case BackendDAE.WHEN_EQUATION(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
case BackendDAE.ALGORITHM(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
case BackendDAE.COMPLEX_EQUATION(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
case BackendDAE.IF_EQUATION(attr=a) algorithm a.kind := k; eq.attr := a; then eq;
else algorithm
Error.addInternalError(getInstanceName() + " failed!", sourceInfo());
then fail();
end match;
end setEquationKind;

public function setEvalStageDynamic
input output BackendDAE.EvaluationStages evalStage;
algorithm
Expand Down
9 changes: 9 additions & 0 deletions OMCompiler/Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -1248,6 +1248,15 @@ algorithm
var.varKind := BackendDAE.PARAM();
end makeParam;

public function makeParamFixed
"Change variable to parameter"
input output BackendDAE.Var var;
input output Boolean fixed; // also output for traversing
algorithm
var.varKind := BackendDAE.PARAM();
var := setVarFixed(var, fixed);
end makeParamFixed;

public function isParamOrConstant
"Return true if variable is parameter or constant"
input BackendDAE.Var invar;
Expand Down

0 comments on commit 7954d04

Please sign in to comment.