Skip to content

Commit

Permalink
added module for move loops as equal constrains in NLP
Browse files Browse the repository at this point in the history
 - cheaper,simple evaluation for jacobians, DAE
 - bigger NLP
open issues: initial guess.



git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25161 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Mar 19, 2015
1 parent 95593e3 commit 3392478
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -2879,7 +2879,7 @@ algorithm
end match;
end makeResidualIfExpLst;

protected function makeEquationToResidualExp ""
public function makeEquationToResidualExp ""
input BackendDAE.Equation eq;
output DAE.Exp oExp;
algorithm
Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7573,6 +7573,7 @@ algorithm
(ExpressionSolve.solveSimpleEquations, "solveSimpleEquations", false),
(SymbolicJacobian.detectSparsePatternODE, "detectJacobianSparsePattern", false),
(Tearing.tearingSystem, "tearingSystem", false),
(DynamicOptimization.removeLoops, "extendDynamicOptimization", false),
(HpcOmEqSystems.partitionLinearTornSystem, "partlintornsystem", false),
(BackendDAEOptimize.addInitialStmtsToAlgorithms, "addInitialStmtsToAlgorithms", false),
(SymbolicJacobian.calculateStrongComponentJacobians, "calculateStrongComponentJacobians", false),
Expand Down
178 changes: 178 additions & 0 deletions Compiler/BackEnd/DynamicOptimization.mo
Expand Up @@ -39,6 +39,7 @@
"
public import DAE;
public import BackendDAE;
public import BackendDAEOptimize;

protected import BackendDump;
protected import ExpressionDump;
Expand All @@ -52,6 +53,7 @@ protected import ComponentReference;
protected import Config;

protected import Expression;
protected import ExpressionSolve;
protected import Error;
protected import Flags;
protected import List;
Expand Down Expand Up @@ -481,5 +483,181 @@ algorithm
end traverserExpinputDerivativesForDynOpt;


// =============================================================================
// section for postOptModule >>extendDynamicOptimization<<
//
// transform loops from DAE in constraints for optimizer
// - bigger NLP
// - don't solve loop in each step
// - cheaper jacobians
// =============================================================================

public function removeLoops
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
if Flags.isSet(Flags.EXTENDS_DYN_OPT) and Flags.getConfigBool(Flags.GENERATE_DYN_OPTIMIZATION_PROBLEM) then
//BackendDump.bltdump("***", inDAE);
(outDAE, _) := BackendDAEUtil.mapEqSystemAndFold(inDAE, findLoops, false);
//BackendDump.bltdump("###", outDAE);
else
outDAE := inDAE;
end if;
end removeLoops;

protected function findLoops
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared inShared;
input Boolean inChanged;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared outShared;
output Boolean outChanged;
protected
BackendDAE.StrongComponents comps;
algorithm
BackendDAE.EQSYSTEM(matching=BackendDAE.MATCHING(comps=comps)) := isyst;
(osyst, outShared, outChanged) := findLoops1(isyst, inShared, comps, inChanged);
end findLoops;

protected function findLoops1
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input BackendDAE.StrongComponents inComps;
input Boolean inchanged;
output BackendDAE.EqSystem osyst = isyst;
output BackendDAE.Shared oshared = ishared;
output Boolean changed = inchanged "not used";
protected
Boolean b;
BackendDAE.StrongComponent c;
Integer i = 1;
BackendDAE.Variables vars;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
BackendDAE.EquationArray eqns;

algorithm
for comp in inComps loop
(osyst,oshared) := removeLoopsWork(osyst,oshared,comp);
end for;

end findLoops1;


protected function removeLoopsWork
"
author: Vitalij Ruge
"
input BackendDAE.EqSystem isyst;
input BackendDAE.Shared ishared;
input BackendDAE.StrongComponent icomp;
output BackendDAE.EqSystem osyst;
output BackendDAE.Shared oshared;
algorithm
(osyst,oshared):=
matchcontinue (isyst,ishared,icomp)
local
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;
list<Integer> eindex,vindx;
Integer eindex_,vindx_;
BackendDAE.Shared shared;
DAE.Exp e1, e2, varexp;
BackendDAE.Var v;
DAE.ComponentRef cr;
DAE.FunctionTree funcs;

case (BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns,stateSets=stateSets,partitionKind=partitionKind),shared,(BackendDAE.EQUATIONSYSTEM(eqns=eindex,vars=vindx)))
equation
(eqns,vars,shared) = res2Con(eqns, vars, eindex, vindx,shared);
then (BackendDAE.EQSYSTEM(vars, eqns, NONE(), NONE(), BackendDAE.NO_MATCHING(), stateSets, partitionKind), shared);

case (BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns,stateSets=stateSets,partitionKind=partitionKind),shared,(BackendDAE.TORNSYSTEM(residualequations=eindex,tearingvars=vindx)))
equation
(eqns,vars,shared) = res2Con(eqns, vars, eindex, vindx,shared);
then (BackendDAE.EQSYSTEM(vars, eqns, NONE(), NONE(), BackendDAE.NO_MATCHING(), stateSets, partitionKind), shared);

case (BackendDAE.EQSYSTEM(orderedVars=vars,orderedEqs=eqns,stateSets=stateSets,partitionKind=partitionKind),shared,BackendDAE.SINGLEEQUATION(eqn=eindex_,var=vindx_))
equation
BackendDAE.EQUATION(exp=e1, scalar=e2) = BackendEquation.equationNth1(eqns, eindex_);
(v as BackendDAE.VAR(varName = cr)) = BackendVariable.getVarAt(vars, vindx_);
varexp = Expression.crefExp(cr);
varexp = if BackendVariable.isStateVar(v) then Expression.expDer(varexp) else varexp;
BackendDAE.SHARED(functionTree = funcs) = shared;
failure(ExpressionSolve.solve2(e1, e2, varexp, SOME(funcs), NONE()));
(eqns,vars,shared) = res2Con(eqns, vars, {eindex_}, {vindx_},shared);
then (BackendDAE.EQSYSTEM(vars, eqns, NONE(), NONE(), BackendDAE.NO_MATCHING(), stateSets, partitionKind), shared);


else (isyst,ishared);

end matchcontinue;

end removeLoopsWork;


protected function res2Con
"
author: Vitalij Ruge
"
input BackendDAE.EquationArray ieqns;
input BackendDAE.Variables ivars;
input list<Integer> eindex;
input list<Integer> vindx;
input BackendDAE.Shared ishared;
output BackendDAE.EquationArray oeqns = ieqns;
output BackendDAE.Variables ovars = ivars;
output BackendDAE.Shared oshared = ishared;
protected
list<BackendDAE.Equation> eqn_lst = BackendEquation.getEqns(eindex,ieqns);
BackendDAE.Equation eqn;
list<BackendDAE.Var> var_lst = List.map1r(vindx, BackendVariable.getVarAt, ivars);
BackendDAE.Var var, var_;
list<DAE.ComponentRef> cr_lst = List.map(var_lst, BackendVariable.varCref);
DAE.ComponentRef cr, cr_var;
list<String> name_lst = List.map(cr_lst,ComponentReference.crefStr);
DAE.Exp e, res;
Integer ind;
list<Integer> ind_lst = vindx;
BackendDAE.Variables knvars;
algorithm
({},_) := List.splitOnTrue(var_lst, BackendVariable.isStateVar);
BackendDAE.SHARED(knownVars=knvars) := oshared;
for name in name_lst loop

// res -> con
var_::var_lst := var_lst;
cr_var :: cr_lst := cr_lst;
eqn :: eqn_lst := eqn_lst;
ind :: ind_lst := ind_lst;

cr := ComponentReference.makeCrefIdent("$OMC$con$" + name, DAE.T_REAL_DEFAULT , {});
e := Expression.crefExp(cr);

var := BackendVariable.makeVar(cr);
var := BackendVariable.setVarMinMax(var, SOME(DAE.RCONST(0.0)), SOME(DAE.RCONST(0.0)));
var := BackendVariable.setVarKind(var, BackendDAE.OPT_CONSTR());

ovars := BackendVariable.addNewVar(var, ovars);
res := BackendDAEOptimize.makeEquationToResidualExp(eqn);

//oeqns := BackendEquation.addEquation(BackendDAE.EQUATION(e, res, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN), oeqns);
oeqns := BackendEquation.setAtIndex(oeqns,ind, BackendDAE.EQUATION(e, res, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN));
// new input(resvar)
(cr,var) := makeVar("OMC$Input" + intString(ind));
var := BackendVariable.setVarDirection(var, DAE.INPUT());
var := BackendVariable.mergeAliasVars(var, var_, false, knvars);
oshared := BackendVariable.addKnVarDAE(var, oshared);
// resvar = new input(resvar)
oeqns := BackendEquation.addEquation(BackendDAE.EQUATION(Expression.crefExp(cr_var), Expression.crefExp(cr), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_UNKNOWN), oeqns);


end for;


end res2Con;

annotation(__OpenModelica_Interface="backend");
end DynamicOptimization;
8 changes: 7 additions & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -430,6 +430,9 @@ constant DebugFlag ADVANCE_TEARING = DEBUG_FLAG(130, "advanceTearing", false,
Util.gettext("Using ExpressionSolve in adjacencyRowEnhanced"));
constant DebugFlag CONSTJAC = DEBUG_FLAG(131, "constjac", false,
Util.gettext("solves linear systems with const jacobian and variable b-Vector symbolically"));
constant DebugFlag EXTENDS_DYN_OPT = DEBUG_FLAG(132, "extendsDynOpt", false,
Util.gettext("generat extends NLP, move loops in the optimization problem as constraints. hint: using intial guess from file!"));


// This is a list of all debug flags, to keep track of which flags are used. A
// flag can not be used unless it's in this list, and the list is checked at
Expand Down Expand Up @@ -567,7 +570,8 @@ constant list<DebugFlag> allDebugFlags = {
NO_START_CALC,
NO_PARTITIONING,
ADVANCE_TEARING,
CONSTJAC
CONSTJAC,
EXTENDS_DYN_OPT
};

public
Expand Down Expand Up @@ -731,6 +735,7 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
"partlintornsystem",
"countOperations",
"inputDerivativesUsed",
"extendDynamicOptimization",
"calculateStrongComponentJacobians",
"calculateStateSetsJacobians",
"detectJacobianSparsePattern",
Expand All @@ -756,6 +761,7 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
("removeEqualFunctionCalls", Util.notrans("DESCRIBE ME")),
("inlineArrayEqn", Util.gettext("This module expands all array equations to scalar equations.")),
("removeUnusedParameter", Util.gettext("Strips all parameter not present in the equations from the system.")),
("extendDynamicOptimization", Util.gettext("Move loops to constraints.")),
("constantLinearSystem", Util.gettext("Evaluates constant linear systems (a*x+b*y=c; d*x+e*y=f; a,b,c,d,e,f are constants) at compile-time.")),
("tearingSystem",Util.notrans("For method selection use flag tearingMethod.")),
("partlintornsystem",Util.notrans("partitions linear torn systems.")),
Expand Down

0 comments on commit 3392478

Please sign in to comment.