Skip to content

Commit

Permalink
Handle deprecated post-optimization flags
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel committed Dec 14, 2015
1 parent 6c8bc92 commit e70366d
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 311 deletions.
162 changes: 77 additions & 85 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -3932,27 +3932,16 @@ end updateStatesVars;
//
// =============================================================================

public function addedScaledVars
public function addedScaledVars_states
"added var_norm = var/nominal, where var is state."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
if Flags.isSet(Flags.ADD_SCALED_VARS) or Flags.isSet(Flags.ADD_SCALED_VARS_INPUT) then
outDAE := addedScaledVarsWork(inDAE);
else
outDAE := inDAE;
end if;
end addedScaledVars;

protected function addedScaledVarsWork
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
protected
list<BackendDAE.EqSystem> systlst;
list<BackendDAE.EqSystem> osystlst = {};
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
list<BackendDAE.Var> kvarlst, lst_states, lst_inputs;
list<BackendDAE.Var> lst_states;
BackendDAE.Var tmpv;
DAE.ComponentRef cref;
DAE.Exp norm, y_norm, y, lhs;
Expand All @@ -3961,55 +3950,19 @@ protected
BackendDAE.EqSystem syst;
algorithm
BackendDAE.DAE(systlst, oshared) := inDAE;
kvarlst := BackendVariable.varList(oshared.knownVars);
lst_inputs := List.select(kvarlst, BackendVariable.isVarOnTopLevelAndInputNoDerInput);
// states
if Flags.isSet(Flags.ADD_SCALED_VARS) then
for syst in systlst loop
syst := match syst
local
BackendDAE.EqSystem syst1;
case syst1 as BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns)
algorithm
// get vars
lst_states := List.select(BackendVariable.varList(vars), BackendVariable.isStateVar);
//BackendDump.printVarList(lst_states);
for v in lst_states loop
cref := BackendVariable.varCref(v);
tmpv := BackendVariable.createVar(cref, "__OMC$scaled_state");
y := Expression.crefExp(cref);
norm := BackendVariable.getVarNominalValue(v);
y_norm := Expression.expDiv(y,norm);
(y_norm,_) := ExpressionSimplify.simplify(y_norm);
// lhs
cref := BackendVariable.varCref(tmpv);
lhs := Expression.crefExp(cref);
eqn := BackendDAE.EQUATION(lhs, y_norm, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
//print("\n" + BackendDump.equationString(eqn));
eqns := BackendEquation.addEquation(eqn, eqns);
vars := BackendVariable.addVar(tmpv, vars);
end for;
syst1.orderedVars := vars;
syst1.orderedEqs := eqns;
then BackendDAEUtil.clearEqSyst(syst1);
end match;
osystlst := syst::osystlst;
end for;
else
osystlst := systlst;
end if;
// inputs
if Flags.isSet(Flags.ADD_SCALED_VARS_INPUT) then
//BackendDump.printVarList(lst_inputs);
syst :: osystlst := osystlst;

for syst in systlst loop
syst := match syst
local
BackendDAE.EqSystem syst1;
case syst1 as BackendDAE.EQSYSTEM(orderedEqs=eqns, orderedVars=vars)
case syst1 as BackendDAE.EQSYSTEM(orderedVars=vars, orderedEqs=eqns)
algorithm
for v in lst_inputs loop
// get vars
lst_states := List.select(BackendVariable.varList(vars), BackendVariable.isStateVar);
//BackendDump.printVarList(lst_states);
for v in lst_states loop
cref := BackendVariable.varCref(v);
tmpv := BackendVariable.createVar(cref, "__OMC$scaled_input");
tmpv := BackendVariable.createVar(cref, "__OMC$scaled_state");
y := Expression.crefExp(cref);
norm := BackendVariable.getVarNominalValue(v);
y_norm := Expression.expDiv(y,norm);
Expand All @@ -4022,14 +3975,67 @@ algorithm
eqns := BackendEquation.addEquation(eqn, eqns);
vars := BackendVariable.addVar(tmpv, vars);
end for;
syst1.orderedEqs := eqns;
syst1.orderedVars := vars;
syst1.orderedEqs := eqns;
then BackendDAEUtil.clearEqSyst(syst1);
end match;
osystlst := syst::osystlst;
end if;
end for;

outDAE := BackendDAE.DAE(osystlst, oshared);
end addedScaledVarsWork;
end addedScaledVars_states;

public function addedScaledVars_inputs
"added var_norm = var/nominal, where var is state."
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
protected
list<BackendDAE.EqSystem> systlst;
list<BackendDAE.EqSystem> osystlst = {};
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
list<BackendDAE.Var> kvarlst, lst_inputs;
BackendDAE.Var tmpv;
DAE.ComponentRef cref;
DAE.Exp norm, y_norm, y, lhs;
BackendDAE.Equation eqn;
BackendDAE.Shared oshared;
BackendDAE.EqSystem syst;
algorithm
BackendDAE.DAE(systlst, oshared) := inDAE;
kvarlst := BackendVariable.varList(oshared.knownVars);
lst_inputs := List.select(kvarlst, BackendVariable.isVarOnTopLevelAndInputNoDerInput);

//BackendDump.printVarList(lst_inputs);
syst :: osystlst := systlst;
syst := match syst
local
BackendDAE.EqSystem syst1;
case syst1 as BackendDAE.EQSYSTEM(orderedEqs=eqns, orderedVars=vars)
algorithm
for v in lst_inputs loop
cref := BackendVariable.varCref(v);
tmpv := BackendVariable.createVar(cref, "__OMC$scaled_input");
y := Expression.crefExp(cref);
norm := BackendVariable.getVarNominalValue(v);
y_norm := Expression.expDiv(y,norm);
(y_norm,_) := ExpressionSimplify.simplify(y_norm);
// lhs
cref := BackendVariable.varCref(tmpv);
lhs := Expression.crefExp(cref);
eqn := BackendDAE.EQUATION(lhs, y_norm, DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_DYNAMIC);
//print("\n" + BackendDump.equationString(eqn));
eqns := BackendEquation.addEquation(eqn, eqns);
vars := BackendVariable.addVar(tmpv, vars);
end for;
syst1.orderedEqs := eqns;
syst1.orderedVars := vars;
then BackendDAEUtil.clearEqSyst(syst1);
end match;
osystlst := syst::osystlst;

outDAE := BackendDAE.DAE(osystlst, oshared);
end addedScaledVars_inputs;

// =============================================================================
// section for sortEqnsVars
Expand Down Expand Up @@ -4161,18 +4167,6 @@ end compWeightsEqns;
// =============================================================================

public function simplifyComplexFunction
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
algorithm
if Flags.isSet(Flags.DIS_SIMP_FUN) then
outDAE := inDAE;
else
outDAE := simplifyComplexFunction1(inDAE);
end if;
end simplifyComplexFunction;


public function simplifyComplexFunction1
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE = inDAE;
protected
Expand Down Expand Up @@ -4320,7 +4314,7 @@ algorithm
end for; // syst

outDAE.eqs := systlst;
end simplifyComplexFunction1;
end simplifyComplexFunction;

function simplifyComplexFunction2
input DAE.Exp e1;
Expand Down Expand Up @@ -5351,18 +5345,16 @@ protected
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 := BackendDAEUtil.createEqSystem(orderedVars, orderedEqs, {}, BackendDAE.CONTINUOUS_TIME_PARTITION());
outDAE := BackendDAE.DAE(eq::eqs, shared);
end if;
(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 := BackendDAEUtil.createEqSystem(orderedVars, orderedEqs, {}, BackendDAE.CONTINUOUS_TIME_PARTITION());
outDAE := BackendDAE.DAE(eq::eqs, shared);
end addTimeAsState;

protected function addTimeAsState1
Expand Down
60 changes: 27 additions & 33 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7102,13 +7102,14 @@ protected function allPostOptimizationModules
(BackendInline.lateInlineFunction, "lateInlineFunction"),
(DynamicOptimization.simplifyConstraints, "simplifyConstraints"),
(CommonSubExpression.wrapFunctionCalls, "wrapFunctionCalls"),
(CommonSubExpression.CSE, "CSE"),
(CommonSubExpression.cseBinary, "cseBinary"),
(OnRelaxation.relaxSystem, "relaxSystem"),
(InlineArrayEquations.inlineArrayEqn, "inlineArrayEqn"),
(SymbolicJacobian.constantLinearSystem, "constantLinearSystem"),
(BackendDAEOptimize.simplifysemiLinear, "simplifysemiLinear"),
(ResolveLoops.solveLinearSystem, "solveLinearSystem"),
(BackendDAEOptimize.addedScaledVars, "addScaledVars"),
(BackendDAEOptimize.addedScaledVars_states, "addScaledVars_states"),
(BackendDAEOptimize.addedScaledVars_inputs, "addScaledVars_inputs"),
(RemoveSimpleEquations.removeSimpleEquations, "removeSimpleEquations"),
(BackendDAEOptimize.simplifyComplexFunction, "simplifyComplexFunction"),
(BackendDAEOptimize.symEuler, "symEuler"),
Expand Down Expand Up @@ -7174,6 +7175,19 @@ algorithm
end if;
end deprecatedDebugFlag;

protected function deprecatedConfigFlag
input Flags.ConfigFlag inFlag;
input list<String> inModuleList;
input String inModule;
input String inPhase;
output list<String> outModuleList = inModuleList;
algorithm
if Flags.getConfigBool(inFlag) then
outModuleList := inModule::inModuleList;
Error.addCompilerWarning("Deprecated flag --" + Flags.configFlagName(inFlag) + " detected. Use --" + inPhase + "=" + inModule + " instead.");
end if;
end deprecatedConfigFlag;

protected function getPreOptModules
input Option<list<String>> inPreOptModules;
output list<tuple<BackendDAEFunc.optimizationModule, String>> outPreOptModules;
Expand Down Expand Up @@ -7245,38 +7259,24 @@ algorithm
enabledModules := "simplifyConstraints"::enabledModules;
end if;

if Flags.isSet(Flags.REDUCE_DYN_OPT) then
enabledModules := "reduceDynamicOptimization"::enabledModules;
end if;
enabledModules := deprecatedDebugFlag(Flags.REDUCE_DYN_OPT, enabledModules, "reduceDynamicOptimization", "postOptModules+");

if not Flags.getConfigString(Flags.LOOP2CON) == "none" then
enabledModules := "extendDynamicOptimization"::enabledModules;
end if;

if Flags.getConfigBool(Flags.CSE_BINARY) then
enabledModules := "CSE"::enabledModules;
end if;

if Flags.getConfigBool(Flags.CSE_CALL) or
Flags.getConfigBool(Flags.CSE_EACHCALL) then
enabledModules := "wrapFunctionCalls"::enabledModules;
end if;

if Flags.isSet(Flags.ON_RELAXATION) then
enabledModules := "relaxSystem"::enabledModules;
end if;

if Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_JACOBIAN) then
enabledModules := "generateSymbolicJacobian"::enabledModules;
end if;
enabledModules := deprecatedConfigFlag(Flags.CSE_BINARY, enabledModules, "cseBinary", "postOptModules+");
enabledModules := deprecatedConfigFlag(Flags.CSE_CALL, enabledModules, "wrapFunctionCalls", "postOptModules+");
enabledModules := deprecatedConfigFlag(Flags.CSE_EACHCALL, enabledModules, "wrapFunctionCalls", "postOptModules+");
enabledModules := deprecatedDebugFlag(Flags.ON_RELAXATION, enabledModules, "relaxSystem", "postOptModules+");
enabledModules := deprecatedConfigFlag(Flags.GENERATE_SYMBOLIC_JACOBIAN, enabledModules, "generateSymbolicJacobian", "postOptModules+");

if Flags.getConfigBool(Flags.GENERATE_SYMBOLIC_LINEARIZATION) then
enabledModules := "generateSymbolicLinearization"::enabledModules;
end if;

if Flags.isSet(Flags.ADD_SCALED_VARS) or Flags.isSet(Flags.ADD_SCALED_VARS_INPUT) then
enabledModules := "addScaledVars"::enabledModules;
end if;
enabledModules := deprecatedDebugFlag(Flags.ADD_SCALED_VARS, enabledModules, "addScaledVars_states", "postOptModules+");
enabledModules := deprecatedDebugFlag(Flags.ADD_SCALED_VARS_INPUT, enabledModules, "addScaledVars_inputs", "postOptModules+");

if Flags.getConfigBool(Flags.SYM_EULER) then
enabledModules := "symEuler"::enabledModules;
Expand All @@ -7290,9 +7290,7 @@ algorithm
enabledModules := "countOperations"::enabledModules;
end if;

if Flags.getConfigBool(Flags.ADD_TIME_AS_STATE) then
enabledModules := "addTimeAsState"::enabledModules;
end if;
enabledModules := deprecatedConfigFlag(Flags.ADD_TIME_AS_STATE, enabledModules, "addTimeAsState", "postOptModules+");

if 1 < Flags.getConfigInt(Flags.MAX_SIZE_FOR_SOLVE_LINIEAR_SYSTEM) then
enabledModules := "solveLinearSystem"::enabledModules;
Expand All @@ -7311,9 +7309,7 @@ algorithm
end if;

// handle special flags, which disable modules
if Flags.isSet(Flags.DIS_SIMP_FUN) then
disabledModules := "simplifyComplexFunction"::disabledModules;
end if;
disabledModules := deprecatedDebugFlag(Flags.DIS_SIMP_FUN, disabledModules, "simplifyComplexFunction", "postOptModules-");

if Flags.getConfigString(Flags.REMOVE_SIMPLE_EQUATIONS) == "none" or
Flags.getConfigString(Flags.REMOVE_SIMPLE_EQUATIONS) == "fastAcausal" or
Expand Down Expand Up @@ -7361,9 +7357,7 @@ algorithm
end if;

// handle special flags, which disable modules
if Flags.isSet(Flags.DIS_SIMP_FUN) then
disabledModules := "simplifyComplexFunction"::disabledModules;
end if;
disabledModules := deprecatedDebugFlag(Flags.DIS_SIMP_FUN, disabledModules, "simplifyComplexFunction", "initOptModules-");

if Config.getTearingMethod() == "noTearing" then
disabledModules := "tearingSystem"::disabledModules;
Expand Down

0 comments on commit e70366d

Please sign in to comment.