Skip to content

Commit

Permalink
added structure for der(input) for dyn. opt.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@24599 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Feb 16, 2015
1 parent 9822673 commit 7e8b29f
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -223,6 +223,8 @@ uniontype VarKind "variable kind"
record JAC_DIFF_VAR end JAC_DIFF_VAR;
record OPT_CONSTR end OPT_CONSTR;
record OPT_FCONSTR end OPT_FCONSTR;
record OPT_INPUT_WITH_DER end OPT_INPUT_WITH_DER;
record OPT_INPUT_DER end OPT_INPUT_DER;
end VarKind;

public uniontype TearingSelect
Expand Down
3 changes: 3 additions & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -76,6 +76,7 @@ protected import DAEDump;
protected import DAEUtil;
protected import Debug;
protected import Differentiate;
protected import DynamicOptimization;
protected import Error;
protected import EvaluateFunctions;
protected import EvaluateParameter;
Expand Down Expand Up @@ -7563,6 +7564,8 @@ algorithm
(BackendDAEOptimize.replaceEdgeChange, "replaceEdgeChange", false),
(BackendDAEOptimize.residualForm, "residualForm", false),
(ResolveLoops.resolveLoops, "resolveLoops", false),
(ResolveLoops.resolveLoops, "resolveLoops", false),
(DynamicOptimization.inputDerivativesForDynOpt, "inputDerivativesForDynOpt", false),
(EvaluateFunctions.evalFunctions, "evalFunc", false),
(UnitCheck.unitChecking, "unitChecking", true),
(CommonSubExpression.commonSubExpressionReplacement, "comSubExp", false),
Expand Down
2 changes: 2 additions & 0 deletions Compiler/BackEnd/BackendDump.mo
Expand Up @@ -2480,6 +2480,8 @@ algorithm
case BackendDAE.JAC_DIFF_VAR()then "JACOBIAN_DIFF_VAR";
case BackendDAE.OPT_CONSTR() then "OPT_CONSTR";
case BackendDAE.OPT_FCONSTR() then "OPT_FCONSTR";
case BackendDAE.OPT_INPUT_WITH_DER() then "OPT_INPUT_WITH_DER";
case BackendDAE.OPT_INPUT_DER() then "OPT_INPUT_DER";
end match;
end kindString;

Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendEquation.mo
Expand Up @@ -2117,6 +2117,7 @@ algorithm
dummyVar := BackendDAE.VAR(lhs, conKind, DAE.OUTPUT(), DAE.NON_PARALLEL(), DAE.T_REAL_DEFAULT, NONE(), NONE(), {}, DAE.emptyElementSource, NONE(), NONE(), NONE(), DAE.NON_CONNECTOR());
dummyVar := BackendVariable.mergeAliasVars(dummyVar, v, false, knvars);
eqn := BackendDAE.SOLVED_EQUATION(lhs, e1, Source, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN);

then ({eqn}, dummyVar);
else fail();
end match;
Expand Down
26 changes: 23 additions & 3 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -840,6 +840,8 @@ algorithm
case (BackendDAE.VAR(varKind=BackendDAE.STATE_DER())) then ();
case (BackendDAE.VAR(varKind=BackendDAE.OPT_CONSTR())) then ();
case (BackendDAE.VAR(varKind=BackendDAE.OPT_FCONSTR())) then ();
case (BackendDAE.VAR(varKind=BackendDAE.OPT_INPUT_WITH_DER())) then ();
case (BackendDAE.VAR(varKind=BackendDAE.OPT_INPUT_DER())) then ();
end match;
end failIfNonState;

Expand Down Expand Up @@ -948,6 +950,8 @@ algorithm
case ((BackendDAE.VAR(varKind=BackendDAE.DUMMY_STATE()) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.OPT_CONSTR()) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.OPT_FCONSTR()) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.OPT_INPUT_WITH_DER()) :: _)) then true;
case ((BackendDAE.VAR(varKind=BackendDAE.OPT_INPUT_DER()) :: _)) then true;
case ((_ :: vs)) then hasContinousVar(vs);
case ({}) then false;
end match;
Expand All @@ -965,7 +969,7 @@ algorithm

/* Real non discrete variable */
case (BackendDAE.VAR(varKind = kind, varType = DAE.T_REAL(_,_))) equation
kind_lst = {BackendDAE.VARIABLE(), BackendDAE.DUMMY_DER(), BackendDAE.DUMMY_STATE()};
kind_lst = {BackendDAE.VARIABLE(), BackendDAE.DUMMY_DER(), BackendDAE.DUMMY_STATE(), BackendDAE.OPT_INPUT_WITH_DER(), BackendDAE.OPT_INPUT_DER()};
then listMember(kind, kind_lst);

else false;
Expand Down Expand Up @@ -1354,7 +1358,7 @@ public function isRealOptimizeConstraintsVars
algorithm
outBoolean := match (inVar)
case (BackendDAE.VAR(varKind = BackendDAE.OPT_CONSTR())) then true;
case (_) then false;
else false;
end match;
end isRealOptimizeConstraintsVars;

Expand All @@ -1365,10 +1369,21 @@ public function isRealOptimizeFinalConstraintsVars
algorithm
outBoolean := match (inVar)
case (BackendDAE.VAR(varKind = BackendDAE.OPT_FCONSTR())) then true;
case (_) then false;
else false;
end match;
end isRealOptimizeFinalConstraintsVars;

public function isRealOptimizeDerInput
"Return true if variable replaced der(Input)"
input BackendDAE.Var inVar;
output Boolean outBoolean;
algorithm
outBoolean := match (inVar)
case (BackendDAE.VAR(varKind = BackendDAE.OPT_INPUT_DER())) then true;
else false;
end match;
end isRealOptimizeDerInput;

public function hasMayerTermAnno
"author: Vitalij Ruge
Return true if variable has isMayer=true annotation"
Expand Down Expand Up @@ -1863,6 +1878,11 @@ algorithm
end match;
end isVarOnTopLevelAndInput;

public function isVarOnTopLevelAndInputNoDerInput
input BackendDAE.Var inVar;
output Boolean outBoolean := isVarOnTopLevelAndInput(inVar) and not isRealOptimizeDerInput(inVar);
end isVarOnTopLevelAndInputNoDerInput;

public function topLevelInput "author: PA
Succeds if variable is input declared at the top level of the model,
or if it is an input in a connector instance at top level."
Expand Down
115 changes: 113 additions & 2 deletions Compiler/BackEnd/DynamicOptimization.mo
Expand Up @@ -40,15 +40,16 @@
public import DAE;
public import BackendDAE;

/*
protected import BackendDump;
protected import ExpressionDump;
*/

protected import BackendEquation;
protected import BackendDAEUtil;


protected import BackendVariable;
protected import ComponentReference;
protected import Config;

protected import Expression;
protected import Flags;
Expand Down Expand Up @@ -342,5 +343,115 @@ algorithm

end addConstraints2;

// =============================================================================
// section for preOptModule >>inputDerivativesForDynOpt<<
//
// check for derivatives of inputs and replace (only for dyn. optimization)
// =============================================================================

public function inputDerivativesForDynOpt "
checks if der(input) is used and replace for dyn. optimization"
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
if Config.acceptOptimicaGrammar() or Flags.getConfigBool(Flags.GENERATE_DYN_OPTIMIZATION_PROBLEM) then
(outDAE, _) := BackendDAEUtil.mapEqSystemAndFold(inDAE, inputDerivativesForDynOptWork, false);
else
outDAE := inDAE;
end if;
end inputDerivativesForDynOpt;

protected function inputDerivativesForDynOptWork "author: "
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared inShared;
input Boolean inChanged;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared outShared := inShared;
output Boolean outChanged;

algorithm

(osyst, outChanged) := matchcontinue(isyst)
local
BackendDAE.Variables orderedVars "ordered Variables, only states and alg. vars";
BackendDAE.EquationArray orderedEqs "ordered Equations";
Option<BackendDAE.IncidenceMatrix> m;
Option<BackendDAE.IncidenceMatrixT> mT;
BackendDAE.Matching matching;
list<DAE.ComponentRef> idercr:={}, icr:={};
DAE.ComponentRef cr;
String s;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
list<BackendDAE.Var> varLst:={};
BackendDAE.Variables vars;

case BackendDAE.EQSYSTEM(orderedVars, orderedEqs, m, mT, matching, stateSets, partitionKind) algorithm
vars := BackendVariable.daeKnVars(outShared);

((_, idercr, icr, varLst)) := BackendDAEUtil.traverseBackendDAEExpsEqnsWithUpdate(orderedEqs, traverserinputDerivativesForDynOpt, (vars, idercr, icr, varLst));
if List.isEmpty(idercr) then
fail();
end if;
// der(u) -> u has der
varLst := BackendVariable.setVarsKind(varLst, BackendDAE.OPT_INPUT_WITH_DER());
for v in varLst loop
outShared := BackendVariable.addKnVarDAE(v, outShared);
end for;
// der(u) -> new input var
varLst := List.map(idercr, BackendVariable.makeVar);
varLst := List.map1(varLst, BackendVariable.setVarDirection, DAE.INPUT());
for v in varLst loop
v := BackendVariable.setVarKind(v,BackendDAE.OPT_INPUT_DER());
outShared := BackendVariable.addKnVarDAE(v, outShared);
end for;
vars := BackendVariable.daeKnVars(outShared);
//BackendDump.printVariables(vars);
then (BackendDAE.EQSYSTEM(orderedVars, orderedEqs, NONE(),NONE(),BackendDAE.NO_MATCHING(), stateSets, partitionKind), true);

else (isyst, inChanged);
end matchcontinue;
end inputDerivativesForDynOptWork;

protected function traverserinputDerivativesForDynOpt "author:"
input DAE.Exp inExp;
input tuple<BackendDAE.Variables, list<DAE.ComponentRef>,list<DAE.ComponentRef> ,list<BackendDAE.Var>> itpl;
output DAE.Exp e;
output tuple<BackendDAE.Variables,list<DAE.ComponentRef>,list<DAE.ComponentRef>,list<BackendDAE.Var>> tpl;
algorithm
(e,tpl) := Expression.traverseExpTopDown(inExp,traverserExpinputDerivativesForDynOpt,itpl);
end traverserinputDerivativesForDynOpt;

protected function traverserExpinputDerivativesForDynOpt
input DAE.Exp inExp;
input tuple<BackendDAE.Variables,list<DAE.ComponentRef>,list<DAE.ComponentRef>,list<BackendDAE.Var>> tpl;
output DAE.Exp outExp;
output Boolean cont;
output tuple<BackendDAE.Variables,list<DAE.ComponentRef>,list<DAE.ComponentRef>,list<BackendDAE.Var>> outTpl;
algorithm
(outExp,cont,outTpl) := matchcontinue (inExp,tpl)
local
BackendDAE.Variables vars;
DAE.Type tp;
DAE.Exp e;
DAE.ComponentRef cr, cr1;
BackendDAE.Var var;
list<DAE.ComponentRef> lst, lst1;
list<BackendDAE.Var> varLst;

case (DAE.CALL(path=Absyn.IDENT(name = "der"),expLst={DAE.CREF(componentRef=cr)}),(vars,lst,lst1,varLst))
equation
(var::{},_) = BackendVariable.getVar(cr, vars);
true = BackendVariable.isVarOnTopLevelAndInput(var);
cr1 = ComponentReference.prependStringCref("$TMP$DER$P", cr);
//cr1 = ComponentReference.crefPrefixDer(cr);
e = Expression.crefExp(cr1);
then (e,true,(vars, List.unionElt(cr1,lst), List.unionElt(cr,lst1), List.unionElt(var,varLst)));

else (inExp,true,tpl);
end matchcontinue;
end traverserExpinputDerivativesForDynOpt;


annotation(__OpenModelica_Interface="backend");
end DynamicOptimization;
2 changes: 1 addition & 1 deletion Compiler/BackEnd/SymbolicJacobian.mo
Expand Up @@ -1228,7 +1228,7 @@ algorithm
states = BackendVariable.getAllStateVarFromVariables(v);
inputvars = List.select(knvarlst,BackendVariable.isInput);
paramvars = List.select(knvarlst, BackendVariable.isParam);
inputvars2 = List.select(knvarlst,BackendVariable.isVarOnTopLevelAndInput);
inputvars2 = List.select(knvarlst,BackendVariable.isVarOnTopLevelAndInputNoDerInput); // without der(u)
outputvars = List.select(varlst, BackendVariable.isVarOnTopLevelAndOutput);
conVarsList = List.select(varlst, BackendVariable.isRealOptimizeConstraintsVars);
fconVarsList = List.select(varlst, BackendVariable.isRealOptimizeFinalConstraintsVars); // ToDo: FinalCon
Expand Down
8 changes: 8 additions & 0 deletions Compiler/SimCode/SerializeModelInfo.mo
Expand Up @@ -734,6 +734,14 @@ algorithm
equation
File.write(file,"final constraint");
then ();
case BackendDAE.OPT_INPUT_WITH_DER()
equation
File.write(file,"use derivation of input");
then ();
case BackendDAE.OPT_INPUT_DER()
equation
File.write(file,"derivation of input");
then ();
else
equation
Error.addMessage(Error.INTERNAL_ERROR, {"serializeVarKind failed"});
Expand Down
3 changes: 2 additions & 1 deletion Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -7272,7 +7272,7 @@ algorithm
boolAlgVars = List.consOnTrue((not isalias) and
BackendVariable.isVarBoolAlg(dlowVar), simvar, boolAlgVars);
inputVars = List.consOnTrue((not isalias) and
BackendVariable.isVarOnTopLevelAndInput(dlowVar), simvar, inputVars);
BackendVariable.isVarOnTopLevelAndInputNoDerInput(dlowVar), simvar, inputVars);
outputVars = List.consOnTrue((not isalias) and
BackendVariable.isVarOnTopLevelAndOutput(dlowVar), simvar, outputVars);
paramVars = List.consOnTrue((not isalias) and
Expand Down Expand Up @@ -7307,6 +7307,7 @@ algorithm
BackendVariable.isRealOptimizeConstraintsVars(dlowVar), simvar, realOptimizeConstraintsVars);
realOptimizeFinalConstraintsVars = List.consOnTrue((not isalias) and
BackendVariable.isRealOptimizeFinalConstraintsVars(dlowVar), simvar, realOptimizeFinalConstraintsVars);

then
SimCodeVar.SIMVARS(stateVars, derivativeVars, algVars, discreteAlgVars, intAlgVars, boolAlgVars, inputVars, outputVars,
aliasVars, intAliasVars, boolAliasVars, paramVars, intParamVars, boolParamVars,
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Template/CodegenC.tpl
Expand Up @@ -912,7 +912,7 @@ template variableDefinitions(ModelInfo modelInfo, list<BackendDAE.TimeEvent> tim
::=
let () = System.tmpTickReset(1000)
match modelInfo
case MODELINFO(varInfo=VARINFO(numStateVars=numStateVars, numAlgVars= numAlgVars, numDiscreteReal=numDiscreteReal, numOptimizeConstraints=numOptimizeConstraints), vars=SIMVARS(__)) then
case MODELINFO(varInfo=VARINFO(numStateVars=numStateVars, numAlgVars= numAlgVars, numDiscreteReal=numDiscreteReal, numOptimizeConstraints=numOptimizeConstraints, numOptimizeFinalConstraints=numOptimizeFinalConstraints), vars=SIMVARS(__)) then
<<
#define time data->localData[0]->timeValue

Expand Down
2 changes: 2 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Expand Up @@ -936,6 +936,8 @@ package BackendDAE
record JAC_DIFF_VAR end JAC_DIFF_VAR;
record OPT_CONSTR end OPT_CONSTR;
record OPT_FCONSTR end OPT_FCONSTR;
record OPT_INPUT_WITH_DER end OPT_INPUT_WITH_DER;
record OPT_INPUT_DER end OPT_INPUT_DER;
end VarKind;

uniontype ZeroCrossing
Expand Down
2 changes: 2 additions & 0 deletions Compiler/Util/Flags.mo
Expand Up @@ -621,6 +621,7 @@ constant ConfigFlag PRE_OPT_MODULES = CONFIG_FLAG(12, "preOptModules",
"expandDerOperator",
"findStateOrder",
"introduceDerAlias",
"inputDerivativesForDynOpt", // only for dyn. opt.
"replaceEdgeChange",
"inlineArrayEqn",
"removeSimpleEquations",
Expand Down Expand Up @@ -651,6 +652,7 @@ constant ConfigFlag PRE_OPT_MODULES = CONFIG_FLAG(12, "preOptModules",
("expandDerOperator", Util.notrans("DESCRIBE ME")),
("findStateOrder", Util.notrans("DESCRIBE ME")),
("introduceDerAlias", Util.notrans("Adds for every der-call an alias equation e.g. dx = der(x).")),
("inputDerivativesForDynOpt", Util.gettext("Allowed derivatives of inputs in dyn. optimization.")),
("simplifyIfEquations", Util.gettext("Tries to simplify if equations by use of information from evaluated parameters.")),
("replaceEdgeChange", Util.gettext("Replace edge(b) = b and not pre(b) and change(b) = v <> pre(v).")),
("residualForm", Util.gettext("Transforms simple equations x=y to zero-sum equations 0=y-x.")),
Expand Down

0 comments on commit 7e8b29f

Please sign in to comment.