Skip to content

Commit

Permalink
new debuge flag: added new_var = var/nominal where var is state
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@25582 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Vitalij Ruge committed Apr 16, 2015
1 parent 36b0ad4 commit aa1e97b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 1 deletion.
103 changes: 103 additions & 0 deletions Compiler/BackEnd/BackendDAEOptimize.mo
Expand Up @@ -3991,6 +3991,109 @@ algorithm
end matchcontinue;
end updateStatesVars;

// =============================================================================
// section for addedScaledVars
//
// =============================================================================

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

protected function addedScaledVarsWork
input BackendDAE.BackendDAE inDAE;
output BackendDAE.BackendDAE outDAE;
protected
list<BackendDAE.EqSystem> systlst;
list<BackendDAE.EqSystem> osystlst = {};
BackendDAE.EqSystem osyst;

Option<BackendDAE.IncidenceMatrix> m, mT;
BackendDAE.Variables vars;
BackendDAE.EquationArray eqns;
BackendDAE.Matching matching;
BackendDAE.StateSets stateSets;
BackendDAE.BaseClockPartitionKind partitionKind;

BackendDAE.Variables vars;
BackendDAE.Variables knvars;
list<BackendDAE.Var> varlst, kvarlst, lst_states, lst_inputs, lst_normv, lst_new_var = {};
BackendDAE.Var tmpv;
DAE.ComponentRef cref;
DAE.Exp norm, y_norm, y, lhs;
BackendDAE.Equation eqn;
BackendDAE.Shared oshared ;

algorithm

BackendDAE.DAE(systlst, oshared) := inDAE;
BackendDAE.SHARED(knownVars=knvars) := oshared;
kvarlst := BackendVariable.varList(knvars);
lst_inputs := List.select(kvarlst, BackendVariable.isVarOnTopLevelAndInputNoDerInput);

for syst in systlst loop
BackendDAE.EQSYSTEM(vars, eqns, m, mT, matching, stateSets, partitionKind) := syst;
varlst := BackendVariable.varList(vars);
// get vars
lst_states := List.select(varlst, 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;
osyst := BackendDAE.EQSYSTEM(vars, eqns, NONE(), NONE(), BackendDAE.NO_MATCHING(), stateSets, partitionKind);
osystlst := osyst::osystlst;
end for;

// inputs
//BackendDump.printVarList(lst_inputs);
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;

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



// =============================================================================
// section for introduceDerAlias
//
Expand Down
1 change: 1 addition & 0 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -7710,6 +7710,7 @@ algorithm
(SymbolicJacobian.inputDerivativesUsed, "inputDerivativesUsed", false),
(BackendDAEOptimize.simplifysemiLinear, "simplifysemiLinear", false),
(BackendDAEOptimize.removeConstants, "removeConstants", false),
(BackendDAEOptimize.addedScaledVars, "addScaledVars", false),
(ExpressionSolve.solveSimpleEquations, "solveSimpleEquations", false),
(SymbolicJacobian.detectSparsePatternODE, "detectJacobianSparsePattern", false),
(Tearing.tearingSystem, "tearingSystem", false),
Expand Down
13 changes: 13 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -1673,6 +1673,19 @@ algorithm
NONE(), NONE(), DAE.NON_CONNECTOR(), DAE.NOT_INNER_OUTER(), false);
end createAliasDerVar;

public function createVar
"Creates a variable with <input> as cref"
input DAE.ComponentRef inCref;
input String prependStringCref;
output BackendDAE.Var outVar;
protected
BackendDAE.Var var;
DAE.ComponentRef cr;
algorithm
cr := ComponentReference.appendStringLastIdent(prependStringCref, inCref);
outVar := makeVar(cr);
end createVar;

public function createDummyVar
"Creates variable with $dummy."
output BackendDAE.Var outVar;
Expand Down
7 changes: 6 additions & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -436,6 +436,9 @@ constant DebugFlag REDUCE_DYN_OPT = DEBUG_FLAG(133, "reduceDynOpt", false,
Util.gettext("remove eqs which not need for the calculations of cost and constraints"));
constant DebugFlag VISUAL_XML = DEBUG_FLAG(134, "visxml", false,
Util.gettext("Outputs a xml-file that contains information for visualization."));
constant DebugFlag ADD_SCALED_VARS = DEBUG_FLAG(135, "addScaledVars", false,
Util.gettext("Adds an alias equation var_nrom = var/nominal where var is state"));


// 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 @@ -576,7 +579,8 @@ constant list<DebugFlag> allDebugFlags = {
CONSTJAC,
EXTENDS_DYN_OPT,
REDUCE_DYN_OPT,
VISUAL_XML
VISUAL_XML,
ADD_SCALED_VARS
};

public
Expand Down Expand Up @@ -733,6 +737,7 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
"constantLinearSystem",
"simplifysemiLinear",
"solveLinearSystem",
"addScaledVars",
"removeSimpleEquations",
"encapsulateWhenConditions", // must called after remove simple equations
"reshufflePost",
Expand Down

0 comments on commit aa1e97b

Please sign in to comment.