Skip to content

Commit

Permalink
Add new post-opt module "addTimeAsState"
Browse files Browse the repository at this point in the history
This module introduces $time as state variable (+addTimeAsState).
  • Loading branch information
lochel committed May 17, 2015
1 parent 0bed963 commit 6c53415
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 7 deletions.
84 changes: 84 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -4628,5 +4628,89 @@ algorithm
end match;
end warnAboutVars;

public function addTimeAsState
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE = inDAE;
protected
BackendDAE.EqSystems eqs;
BackendDAE.EqSystem eq;
BackendDAE.Shared shared;
BackendDAE.Variables orderedVars "ordered Variables, only states and alg. vars";
BackendDAE.EquationArray orderedEqs "ordered Equations";
BackendDAE.Var var;
algorithm
if Flags.getConfigBool(Flags.ADD_TIME_AS_STATE) then
(BackendDAE.DAE(eqs, shared), _) := BackendDAEUtil.mapEqSystemAndFold(inDAE, addTimeAsState1, 0);
orderedVars := BackendVariable.emptyVars();
var := BackendDAE.VAR(DAE.crefTimeState, BackendDAE.STATE(1, NONE()), DAE.BIDIR(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), true);
var := BackendVariable.setVarFixed(var, true);
var := BackendVariable.setVarStartValue(var, DAE.CREF(DAE.crefTime, DAE.T_REAL_DEFAULT));
orderedVars := BackendVariable.addVar(var, orderedVars);
orderedEqs := BackendEquation.emptyEqns();
orderedEqs := BackendEquation.addEquation(BackendDAE.EQUATION(DAE.CALL(Absyn.IDENT("der"), {DAE.CREF(DAE.crefTimeState, DAE.T_REAL_DEFAULT)}, DAE.callAttrBuiltinReal), DAE.RCONST(1.0), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC), orderedEqs);
eq := BackendDAE.EQSYSTEM(orderedVars, orderedEqs, NONE(), NONE(), BackendDAE.NO_MATCHING(), {}, BackendDAE.CONTINUOUS_TIME_PARTITION());
outDAE := BackendDAE.DAE(eq::eqs, shared);
end if;
end addTimeAsState;

protected function addTimeAsState1
input BackendDAE.EqSystem inSystem;
input BackendDAE.Shared inShared;
input Integer inFoo;
output BackendDAE.EqSystem outSystem;
output BackendDAE.Shared outShared = inShared;
output Integer outFoo = inFoo;
algorithm
outSystem := matchcontinue(inSystem)
local
BackendDAE.Variables orderedVars;
BackendDAE.EquationArray orderedEqs;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;

case BackendDAE.EQSYSTEM(orderedVars, orderedEqs, _, _, _, stateSets, partitionKind) equation
(orderedEqs, _) = BackendEquation.traverseEquationArray_WithUpdate(orderedEqs, addTimeAsState2, inFoo);
then BackendDAE.EQSYSTEM(orderedVars, orderedEqs, NONE(), NONE(), BackendDAE.NO_MATCHING(), stateSets, partitionKind);

else inSystem;
end matchcontinue;
end addTimeAsState1;

protected function addTimeAsState2
input BackendDAE.Equation inEq;
input Integer inFoo;
output BackendDAE.Equation outEq;
output Integer outFoo = inFoo;
algorithm
(outEq, _) := BackendEquation.traverseExpsOfEquation(inEq, addTimeAsState3, inFoo);
end addTimeAsState2;

protected function addTimeAsState3
input DAE.Exp inExp;
input Integer inTuple;
output DAE.Exp outExp;
output Integer outTuple;
algorithm
(outExp, outTuple) := Expression.traverseExpTopDown(inExp, addTimeAsState4, inTuple);
end addTimeAsState3;

protected function addTimeAsState4
input DAE.Exp inExp;
input Integer inTuple;
output DAE.Exp outExp;
output Boolean cont = true;
output Integer outTuple = inTuple;
algorithm
outExp := match inExp
local
DAE.Type ty;

case DAE.CREF(componentRef=DAE.CREF_IDENT(ident="time"), ty=ty) equation
then DAE.CREF(DAE.crefTimeState, ty);

else inExp;
end match;
end addTimeAsState4;

annotation(__OpenModelica_Interface="backend");
end BackendDAEOptimize;
3 changes: 2 additions & 1 deletion Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7768,7 +7768,8 @@ algorithm
(ResolveLoops.solveLinearSystem, "solveLinearSystem", false),
(CommonSubExpression.CSE, "CSE", false),
(BackendDump.dumpDAE, "dumpDAE", false),
(XMLDump.dumpDAEXML, "dumpDAEXML", false)
(XMLDump.dumpDAEXML, "dumpDAEXML", false),
(BackendDAEOptimize.addTimeAsState, "addTimeAsState", false)
};

strpostOptModules := getPostOptModulesString();
Expand Down
3 changes: 2 additions & 1 deletion Compiler/FrontEnd/DAE.mo
Expand Up @@ -1751,7 +1751,8 @@ uniontype ComponentRef "- Component references

end ComponentRef;

public constant ComponentRef crefTime = CREF_IDENT("time",T_REAL_DEFAULT,{});
public constant ComponentRef crefTime = CREF_IDENT("time", T_REAL_DEFAULT, {});
public constant ComponentRef crefTimeState = CREF_IDENT("$time", T_REAL_DEFAULT, {});

public
uniontype Subscript "The `Subscript\' and `ComponentRef\' datatypes are simple
Expand Down
18 changes: 13 additions & 5 deletions Compiler/Util/Flags.mo
Expand Up @@ -756,16 +756,17 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
"countOperations",
"inputDerivativesUsed",
"extendDynamicOptimization",
"addTimeAsState",
"calculateStrongComponentJacobians",
"calculateStateSetsJacobians",
"detectJacobianSparsePattern",
"generateSymbolicJacobian",
"generateSymbolicLinearization",
"removeUnusedFunctions",
"removeConstants"
//"solveSimpleEquations"
// "partitionIndependentBlocks",
// "addInitialStmtsToAlgorithms"
//"solveSimpleEquations",
//"partitionIndependentBlocks",
//"addInitialStmtsToAlgorithms",
}),
SOME(STRING_DESC_OPTION({
("encapsulateWhenConditions", Util.gettext("Replace each condition/relation with a boolean variable.")),
Expand Down Expand Up @@ -801,7 +802,8 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
("reshufflePost", Util.gettext("Reshuffles algebraic loops.")),
("CSE", Util.gettext("Common Subexpression Elimination")),
("dumpDAE", Util.gettext("dumps the DAE representation of the current transformation state")),
("dumpDAEXML", Util.gettext("dumps the DAE as xml representation of the current transformation state"))
("dumpDAEXML", Util.gettext("dumps the DAE as xml representation of the current transformation state")),
("addTimeAsState", Util.gettext("Experimental feature: this repaces each occurrence of variable time with a new introduced state $time with equation der($time) = 1.0"))
})),
Util.gettext("Sets the post optimization modules to use in the back end. See --help=optmodules for more info."));

Expand Down Expand Up @@ -1054,6 +1056,11 @@ constant ConfigFlag SYM_EULER = CONFIG_FLAG(69, "symEuler",
NONE(), EXTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Rewritte the ode system for inplicit euler."));

constant ConfigFlag ADD_TIME_AS_STATE = CONFIG_FLAG(70,
"addTimeAsState", NONE(), INTERNAL(), BOOL_FLAG(false), NONE(),
Util.gettext("Experimental feature: this repaces each occurrence of variable time with a new introduced state $time with equation der($time) = 1.0"));


protected
// This is a list of all configuration flags. A flag can not be used unless it's
// in this list, and the list is checked at initialization so that all flags are
Expand Down Expand Up @@ -1127,7 +1134,8 @@ constant list<ConfigFlag> allConfigFlags = {
CPP_FLAGS,
REMOVE_SIMPLE_EQUATIONS,
DYNAMIC_TEARING,
SYM_EULER
SYM_EULER,
ADD_TIME_AS_STATE
};

public function new
Expand Down

0 comments on commit 6c53415

Please sign in to comment.