Skip to content

Commit

Permalink
Add experimental initOptModule inlineHomotopy
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaeuber authored and OpenModelica-Hudson committed Apr 20, 2017
1 parent f0af412 commit 17e8841
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 5 deletions.
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -720,6 +720,7 @@ public constant String functionDerivativeNamePrefix = "$funDER";
public constant String optimizationMayerTermName = "$OMC$objectMayerTerm";
public constant String optimizationLagrangeTermName = "$OMC$objectLagrangeTerm";
public constant String symSolverDT = "__OMC_DT";
public constant String homotopyLambda = "__HOM_LAMBDA";

type FullJacobian = Option<list<tuple<Integer, Integer, Equation>>>;

Expand Down
60 changes: 60 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -5767,5 +5767,65 @@ algorithm
b := intLt(arrayGet(varArr,idx),0);
end stateVarIsNotVisited;


// =============================================================================
// section for initOptModule >>inlineHomotopy<<
//
// =============================================================================

public function inlineHomotopy
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE = inDAE;
protected
BackendDAE.EquationArray orderedEqs;
BackendDAE.Variables orderedVars;
Boolean foundHomotopy;
algorithm
for syst in inDAE.eqs loop
orderedEqs := syst.orderedEqs;
(orderedEqs, foundHomotopy) := BackendEquation.traverseEquationArray_WithUpdate(orderedEqs, inlineHomotopy2, false);
syst.orderedEqs := orderedEqs;
end for;
end inlineHomotopy;

public function inlineHomotopy2
input BackendDAE.Equation inEq;
input Boolean inFoundHomotopy;
output BackendDAE.Equation outEq;
output Boolean outFoundHomotopy = inFoundHomotopy;
algorithm
(outEq, outFoundHomotopy) := BackendEquation.traverseExpsOfEquation(inEq, inlineHomotopy3, inFoundHomotopy);
end inlineHomotopy2;

protected function inlineHomotopy3
input DAE.Exp inExp;
input Boolean inFoundHomotopy;
output DAE.Exp outExp;
output Boolean outFoundHomotopy = inFoundHomotopy;
algorithm
(outExp, outFoundHomotopy) := Expression.traverseExpTopDown(inExp, replaceHomotopyWithLambdaExpression, inFoundHomotopy);
end inlineHomotopy3;

protected function replaceHomotopyWithLambdaExpression
input DAE.Exp inExp;
input Boolean inFoundHomotopy;
output DAE.Exp outExp = inExp;
output Boolean cont = true;
output Boolean outFoundHomotopy;
algorithm
outFoundHomotopy := match(inExp)
local
DAE.Exp actual, simplified, lambda;

case DAE.CALL(path=Absyn.IDENT("homotopy"), expLst={actual,simplified})
algorithm
lambda := Expression.crefExp(ComponentReference.makeCrefIdent(BackendDAE.homotopyLambda, DAE.T_REAL_DEFAULT, {}));
outExp := DAE.BINARY(DAE.BINARY(simplified, DAE.MUL(DAE.T_REAL_DEFAULT), DAE.BINARY(DAE.RCONST(1.0), DAE.SUB(DAE.T_REAL_DEFAULT), lambda)), DAE.ADD(DAE.T_REAL_DEFAULT), DAE.BINARY(actual, DAE.MUL(DAE.T_REAL_DEFAULT), lambda));
then true;

else inFoundHomotopy;
end match;
end replaceHomotopyWithLambdaExpression;

annotation(__OpenModelica_Interface="backend");
end BackendDAEOptimize;
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7544,6 +7544,7 @@ protected function allInitOptimizationModules
"This list contains all back end init-optimization modules."
output list<tuple<BackendDAEFunc.optimizationModule, String>> allInitOptimizationModules = {
(SymbolicJacobian.constantLinearSystem, "constantLinearSystem"),
(BackendDAEOptimize.inlineHomotopy, "inlineHomotopy"),
(BackendDAEOptimize.inlineFunctionInLoops, "forceInlineFunctionInLoops"), // before simplifyComplexFunction
(BackendDAEOptimize.simplifyComplexFunction, "simplifyComplexFunction"),
(DynamicOptimization.reduceDynamicOptimization, "reduceDynamicOptimization"), // before tearing
Expand Down
28 changes: 23 additions & 5 deletions Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -3513,17 +3513,35 @@ protected
BackendDAE.Variables vars;
Boolean b;
BackendVarTransform.VariableReplacements repl;
DAE.Exp crExp;
algorithm
(outExp, outHomotopy) := match(inExp, inHomotopy)
(outExp, outHomotopy) := Expression.traverseExpTopDown(inExp, containsHomotopyCall2, inHomotopy);
end containsHomotopyCall;

protected function containsHomotopyCall2
input DAE.Exp inExp;
input Boolean inHomotopy;
output DAE.Exp outExp = inExp;
output Boolean cont;
output Boolean outHomotopy;
protected
BackendDAE.Variables vars;
Boolean b;
BackendVarTransform.VariableReplacements repl;
algorithm
(outExp, outHomotopy, cont) := match(inExp, inHomotopy)
case (_, true)
then (inExp, true);
then (inExp, true, false);

case (DAE.CALL(path=Absyn.IDENT(name="homotopy")), _)
then (inExp, true);
then (inExp, true, false);

case (DAE.CREF(componentRef=DAE.CREF_IDENT(ident=BackendDAE.homotopyLambda)), _)
then (inExp, true, false);

else (inExp, inHomotopy);
else (inExp, inHomotopy, true);
end match;
end containsHomotopyCall;
end containsHomotopyCall2;

protected function checkLinearSystem
input Integer info;
Expand Down
1 change: 1 addition & 0 deletions Compiler/Template/CodegenCFunctions.tpl
Expand Up @@ -4043,6 +4043,7 @@ end contextIteratorName;
case CREF_IDENT(ident = "xloc") then crefStr(cr)
case CREF_IDENT(ident = "time") then "data->localData[0]->timeValue"
case CREF_IDENT(ident = "__OMC_DT") then "data->simulationInfo->inlineData->dt"
case CREF_IDENT(ident = "__HOM_LAMBDA") then "data->simulationInfo->lambda"
case WILD(__) then ''
else crefToCStr(cr, 0, false, false)
end cref;
Expand Down
1 change: 1 addition & 0 deletions Compiler/Util/Flags.mo
Expand Up @@ -1209,6 +1209,7 @@ constant ConfigFlag INIT_OPT_MODULES = CONFIG_FLAG(77, "initOptModules",
("calculateStrongComponentJacobians", Util.gettext("Generates analytical jacobian for torn linear and non-linear strong components. By default non-linear components with user-defined function calls are skipped. See also debug flags: NLSanalyticJacobian and forceNLSanalyticJacobian")),
("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.")),
("extendDynamicOptimization", Util.gettext("Move loops to constraints.")),
("inlineHomotopy", Util.gettext("Experimental: Inlines the homotopy expression to allow symbolic simplifications.")),
("inputDerivativesUsed", Util.gettext("Checks if derivatives of inputs are need to calculate the model.")),
("recursiveTearing", Util.notrans("inline and repeat tearing")),
("reduceDynamicOptimization", Util.notrans("Removes equations which are not needed for the calculations of cost and constraints. This module requires +d=reduceDynOpt.")),
Expand Down

0 comments on commit 17e8841

Please sign in to comment.