Skip to content

Commit

Permalink
- check for derivatives of inputs
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@13380 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Jens Frenkel committed Oct 14, 2012
1 parent 182ec2d commit 7b86774
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 7 deletions.
93 changes: 89 additions & 4 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -9086,25 +9086,27 @@ algorithm
DAE.Type tp;
DAE.Exp e,zero;
DAE.ComponentRef cr;
BackendDAE.Var var;
case((DAE.CALL(path=Absyn.IDENT(name = "der"),expLst={DAE.CREF(componentRef=cr,ty=tp)}),(vars,_)))
equation
(_,_) = BackendVariable.getVar(cr, vars);
(var::{},_) = BackendVariable.getVar(cr, vars);
false = BackendVariable.isVarOnTopLevelAndInput(var);
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
then
((zero,(vars,true)));
case((DAE.CALL(path=Absyn.IDENT(name = "pre"),expLst={e as DAE.CREF(componentRef=cr,ty=tp)}),(vars,_)))
equation
(_,_) = BackendVariable.getVar(cr, vars);
(_::{},_) = BackendVariable.getVar(cr, vars);
then
((e,(vars,true)));
case((DAE.CALL(path=Absyn.IDENT(name = "change"),expLst={e as DAE.CREF(componentRef=cr,ty=tp)}),(vars,_)))
equation
(_,_) = BackendVariable.getVar(cr, vars);
(_::{},_) = BackendVariable.getVar(cr, vars);
then
((DAE.BCONST(false),(vars,true)));
case((DAE.CALL(path=Absyn.IDENT(name = "edge"),expLst={e as DAE.CREF(componentRef=cr,ty=tp)}),(vars,_)))
equation
(_,_) = BackendVariable.getVar(cr, vars);
(_::{},_) = BackendVariable.getVar(cr, vars);
then
((DAE.BCONST(false),(vars,true)));
case _ then tpl;
Expand Down Expand Up @@ -12652,6 +12654,89 @@ algorithm
end matchcontinue;
end simplifysemiLinearFinder;

/*
* check for derivatives of inputs
*
*/
public function inputDerivativesUsed "function inputDerivativesUsed
checks if der(input) is used and report a warning/error.
author: Frenkel TUD 2012-10"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
(outDAE,_) := BackendDAEUtil.mapEqSystemAndFold(inDAE,inputDerivativesUsedWork,false);
end inputDerivativesUsed;

protected function inputDerivativesUsedWork "function inputDerivativesUsedWork
author: Frenkel TUD 2012-10"
input BackendDAE.EqSystem isyst;
input tuple<BackendDAE.Shared,Boolean> sharedChanged;
output BackendDAE.EqSystem osyst;
output tuple<BackendDAE.Shared,Boolean> osharedChanged;
algorithm
(osyst,osharedChanged) :=
matchcontinue(isyst,sharedChanged)
local
BackendDAE.Variables orderedVars "orderedVars ; ordered Variables, only states and alg. vars" ;
BackendDAE.EquationArray orderedEqs "orderedEqs ; ordered Equations" ;
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.Matching matching;
BackendDAE.Shared shared;
list<DAE.Exp> explst;
String s;
case (BackendDAE.EQSYSTEM(orderedVars,orderedEqs,m,mT,matching),(shared, _))
equation
((_,explst as _::_)) = BackendDAEUtil.traverseBackendDAEExpsEqnsWithUpdate(orderedEqs,traverserinputDerivativesUsed,(BackendVariable.daeKnVars(shared),{}));
s = stringDelimitList(List.map(explst,ExpressionDump.printExpStr),"\n");
Error.addMessage(Error.DERIVATIVE_INPUT,{s});
then
(BackendDAE.EQSYSTEM(orderedVars,orderedEqs,m,mT,matching),(shared,true));
else
(isyst,sharedChanged);
end matchcontinue;
end inputDerivativesUsedWork;

protected function traverserinputDerivativesUsed "function traverserinputDerivativesUsed
author: Frenkel TUD 2012-10"
input tuple<DAE.Exp,tuple<BackendDAE.Variables,list<DAE.Exp>>> itpl;
output tuple<DAE.Exp,tuple<BackendDAE.Variables,list<DAE.Exp>>> outTpl;
protected
DAE.Exp e;
tuple<BackendDAE.Variables,list<DAE.Exp>> tpl;
algorithm
(e,tpl) := itpl;
outTpl := Expression.traverseExpTopDown(e,traverserExpinputDerivativesUsed,tpl);
end traverserinputDerivativesUsed;

protected function traverserExpinputDerivativesUsed "function traverserExpinputDerivativesUsed
author: Frenkel TUD 2012-10"
input tuple<DAE.Exp,tuple<BackendDAE.Variables,list<DAE.Exp>>> tpl;
output tuple<DAE.Exp,Boolean,tuple<BackendDAE.Variables,list<DAE.Exp>>> outTpl;
algorithm
outTpl := matchcontinue(tpl)
local
BackendDAE.Variables vars;
DAE.Type tp;
DAE.Exp e,zero;
DAE.ComponentRef cr;
BackendDAE.Var var;
list<DAE.Exp> explst;
case((e as DAE.CALL(path=Absyn.IDENT(name = "der"),expLst={DAE.CALL(path=Absyn.IDENT(name = "der"),expLst={DAE.CREF(componentRef=cr,ty=tp)})}),(vars,explst)))
equation
(var::{},_) = BackendVariable.getVar(cr, vars);
true = BackendVariable.isVarOnTopLevelAndInput(var);
then
((e,false,(vars,e::explst)));
case((e as DAE.CALL(path=Absyn.IDENT(name = "der"),expLst={DAE.CREF(componentRef=cr,ty=tp)}),(vars,explst)))
equation
(var::{},_) = BackendVariable.getVar(cr, vars);
true = BackendVariable.isVarOnTopLevelAndInput(var);
then
((e,false,(vars,e::explst)));
case ((e,(vars,explst))) then ((e,true,(vars,explst)));
end matchcontinue;
end traverserExpinputDerivativesUsed;



Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -8787,6 +8787,7 @@ algorithm
(BackendDAEOptimize.collapseIndependentBlocks,"collapseIndependentBlocks",true),
(BackendDAEOptimize.removeUnusedFunctions,"removeUnusedFunctions",false),
(BackendDAEOptimize.simplifyTimeIndepFuncCalls,"simplifyTimeIndepFuncCalls",false),
(BackendDAEOptimize.inputDerivativesUsed,"inputDerivativesUsed",false),
(BackendDAEOptimize.simplifysemiLinear,"simplifysemiLinear",false),
(BackendDAEOptimize.optimizeInitialSystem,"optimizeInitialSystem",false),
(BackendDAEOptimize.detectSparsePatternODE,"detectJacobianSparsePattern",false)
Expand Down
6 changes: 4 additions & 2 deletions Compiler/BackEnd/Derive.mo
Expand Up @@ -334,6 +334,7 @@ algorithm
DAE.CallAttributes attr;
BackendDAE.VarKind kind;
Real r;
BackendDAE.Var var;

case (DAE.ICONST(integer = _),_) then DAE.RCONST(0.0);
case (DAE.RCONST(real = _),_) then DAE.RCONST(0.0);
Expand All @@ -355,10 +356,11 @@ algorithm
then
differentiateExpTime(e1,inVariables);

// Constants, known variables, parameters and discrete variables have a 0-derivative
// Constants, known variables, parameters and discrete variables have a 0-derivative, not the inputs
case ((e as DAE.CREF(componentRef = cr,ty = tp)),(_,BackendDAE.SHARED(knownVars=knvars)))
equation
(_::{},_) = BackendVariable.getVar(cr, knvars);
(var::{},_) = BackendVariable.getVar(cr, knvars);
false = BackendVariable.isVarOnTopLevelAndInput(var);
(zero,_) = Expression.makeZeroExpression(Expression.arrayDimension(tp));
then zero;

Expand Down
4 changes: 3 additions & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -586,6 +586,7 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
// "countOperations",
"removeUnusedFunctions",
"simplifyTimeIndepFuncCalls",
"inputDerivativesUsed",
// "detectJacobianSparsePattern",
"optimizeInitialSystem"
}),
Expand All @@ -608,14 +609,15 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
("collapseIndependentBlocks", Util.gettext("Collapses all equation systems back into one big system again (undo partitionIndependentBlocks)")),
("removeUnusedFunctions", Util.gettext("removed all unused functions from functionTree")),
("simplifyTimeIndepFuncCalls", Util.gettext("simplifies time independent built in function calls like pre(param) -> param, der(param) -> 0.0, change(param) -> false, edge(param) -> false")),
("inputDerivativesUsed", Util.gettext("checks if derivatives of inputs are need to calculate the model.")),
("simplifysemiLinear", Util.gettext("simplifies calls to semiLinear")),
("optimizeInitialSystem", Util.gettext("simplifies time initial system")),
("detectJacobianSparsePattern", Util.gettext("detects the sparse pattern for Jacobian A"))
})),
Util.gettext("Sets the post optimization modules to use in the back end. See +help=optmodules for more info."));
constant ConfigFlag SIMCODE_TARGET = CONFIG_FLAG(17, "simCodeTarget",
NONE(), EXTERNAL(), STRING_FLAG("C"),
SOME(STRING_OPTION({"CSharp", "Cpp", "Adevs", "QSS", "C", "c", "Dump", "XML", "Java"})),
SOME(STRING_OPTION({"CSharp", "Cpp", "Adevs", "QSS", "C", "c", "Dump", "XML", "Java","ResidualCMP"})),
Util.gettext("Sets the target language for the code generation"));
constant ConfigFlag ORDER_CONNECTIONS = CONFIG_FLAG(18, "orderConnections",
NONE(), EXTERNAL(), BOOL_FLAG(true), NONE(),
Expand Down

0 comments on commit 7b86774

Please sign in to comment.