Skip to content

Commit

Permalink
Don't rename outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
lochel authored and OpenModelica-Hudson committed Jan 25, 2019
1 parent 7eaf6bc commit bf2e0a4
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Compiler/BackEnd/BackendDAE.mo
Expand Up @@ -739,7 +739,7 @@ public constant Integer SymbolicJacobianDIndex = 4;
public constant String derivativeNamePrefix = "$DERAlias";
public constant String partialDerivativeNamePrefix = "$pDER";
public constant String functionDerivativeNamePrefix = "$funDER";
public constant String outputStateAliasPrefix = "$outputStateAlias_";
public constant String outputAliasPrefix = "$outputAlias_";

public constant String optimizationMayerTermName = "$OMC$objectMayerTerm";
public constant String optimizationLagrangeTermName = "$OMC$objectLagrangeTerm";
Expand Down
99 changes: 67 additions & 32 deletions Compiler/BackEnd/BackendDAEUtil.mo
Expand Up @@ -1865,55 +1865,90 @@ algorithm
end match;
end reduceEqSystem;

public function createAliasVarsForOutputStates
" creates for every state that is also output an
alias variable $outputStateAlias.stateName and
the corresponding equation"
input BackendDAE.BackendDAE inBDAE;
output BackendDAE.BackendDAE outBDAE = inBDAE;
public function introduceOutputAliases
input output BackendDAE.BackendDAE dae;
protected
BackendDAE.EqSystems systems, returnSysts = {};
BackendDAE.Variables vars;
BackendDAE.Variables vars, newVars;
BackendDAE.EquationArray eqs;
list<BackendDAE.Var> states;
DAE.ComponentRef newCref;
list<BackendDAE.Equation> newEqns;
DAE.ComponentRef newCref, cref;
BackendDAE.Var newVar;
BackendDAE.Equation newEqn;
HashSet.HashSet topLevelOutputs = HashSet.emptyHashSet();
algorithm
systems := inBDAE.eqs;
systems := dae.eqs;
for system in systems loop
eqs := system.orderedEqs;
eqs := system.orderedEqs;
vars := system.orderedVars;
states := BackendVariable.getAllStateVarFromVariables(vars);
if Config.languageStandardAtLeast(Config.LanguageStandard.'3.3') then
states := listAppend(states, BackendVariable.getAllClockedStatesFromVariables(vars));
end if;
for v in states loop
if BackendVariable.isVarOnTopLevelAndOutput(v) then
// generate new output var and add it
newCref := ComponentReference.prependStringCref(BackendDAE.outputStateAliasPrefix, BackendVariable.varCref(v));
newVars := BackendVariable.emptyVarsSized(realInt(intReal(BackendVariable.varsSize(vars)) * 1.4));
newEqns := {};

for v in BackendVariable.varList(vars) loop
if not BackendVariable.isVarOnTopLevelAndOutput(v) then
newVars := BackendVariable.addVar(v, newVars);
else
cref := BackendVariable.varCref(v);
topLevelOutputs := BaseHashSet.add(cref, topLevelOutputs);

// generate new internal alias var
newCref := ComponentReference.prependStringCref(BackendDAE.outputAliasPrefix, cref);
newVar := BackendVariable.copyVarNewName(newCref, v);
newVar := BackendVariable.setVarKind(newVar, BackendDAE.VARIABLE());
vars := BackendVariable.addVar(newVar, vars);
newVar := BackendVariable.setVarDirection(newVar, DAE.BIDIR());
newVars := BackendVariable.addVar(newVar, newVars);

//update states to remove the output direction
v := BackendVariable.setVarDirection(v, DAE.BIDIR());
vars := BackendVariable.addVar(v, vars);
// update output to ordinary variable
v := BackendVariable.setVarKind(v, BackendDAE.VARIABLE());
v := BackendVariable.removeFixedAttribute(v);
v := BackendVariable.removeStartAttribute(v);
newVars := BackendVariable.addVar(v, newVars);

// generate new equation and add it
newEqn := BackendEquation.generateEquation(Expression.crefToExp(newCref), Expression.crefToExp(BackendVariable.varCref(v)), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_BINDING);
eqs := BackendEquation.add(newEqn, eqs);
newEqn := BackendEquation.generateEquation(Expression.crefToExp(cref), Expression.crefToExp(newCref), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_BINDING);
newEqns := newEqn::newEqns;
end if;
end for;
system.orderedVars := vars;

_ := traverseBackendDAEExpsEqns(eqs, introduceOutputAliases_eqs, topLevelOutputs);
eqs := BackendEquation.addList(newEqns, eqs);
system.orderedVars := newVars;
system.orderedEqs := eqs;
system := BackendDAEUtil.clearEqSyst(system);
returnSysts := system::returnSysts;
end for;

returnSysts := listReverse(returnSysts);
outBDAE.eqs := returnSysts;
outBDAE := BackendDAEUtil.transformBackendDAE(outBDAE, SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())), NONE(), NONE());
end createAliasVarsForOutputStates;
dae.eqs := returnSysts;
end introduceOutputAliases;

protected function introduceOutputAliases_eqs
input DAE.Exp inExp;
input HashSet.HashSet inStates;
output DAE.Exp outExp;
output HashSet.HashSet outStates;
algorithm
(outExp, outStates) := Expression.traverseExpBottomUp(inExp, introduceOutputAliases_eqs2, inStates);
end introduceOutputAliases_eqs;

protected function introduceOutputAliases_eqs2
input DAE.Exp inExp;
input HashSet.HashSet inStates;
output DAE.Exp outExp;
output HashSet.HashSet outStates = inStates;
algorithm
outExp := match inExp
local
DAE.Exp e1;
DAE.ComponentRef cr, newCref;

// replace der(cr) with der(<outputAliasPrefix> + cr)
case e1 as DAE.CREF(componentRef=cr) guard BaseHashSet.has(cr, inStates) algorithm
newCref := ComponentReference.prependStringCref(BackendDAE.outputAliasPrefix, cr);
e1.componentRef := newCref;
then e1;

else inExp;
end match;
end introduceOutputAliases_eqs2;

protected function translateArrayList
input Integer inElement;
Expand Down Expand Up @@ -7623,6 +7658,7 @@ end selectMatchingAlgorithm;
public function allPreOptimizationModules
"This list contains all back end pre-optimization modules."
output list<tuple<BackendDAEFunc.optimizationModule, String>> allPreOptimizationModules = {
(BackendDAEUtil.introduceOutputAliases, "introduceOutputAliases"),
(Uncertainties.dataReconciliation, "dataReconciliation"),
(UnitCheck.unitChecking, "unitChecking"),
(DynamicOptimization.createDynamicOptimization,"createDynamicOptimization"),
Expand Down Expand Up @@ -7663,7 +7699,6 @@ end allPreOptimizationModules;
public function allPostOptimizationModules
"This list contains all back end sim-optimization modules."
output list<tuple<BackendDAEFunc.optimizationModule, String>> allPostOptimizationModules = {
(BackendDAEUtil.createAliasVarsForOutputStates, "createAliasVarsForOutputStates"),
(BackendInline.lateInlineFunction, "lateInlineFunction"),
(DynamicOptimization.simplifyConstraints, "simplifyConstraints"),
(CommonSubExpression.wrapFunctionCalls, "wrapFunctionCalls"),
Expand Down
16 changes: 16 additions & 0 deletions Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -102,6 +102,22 @@ algorithm
outVar.values := DAEUtil.setFixedAttr(oattr, SOME(DAE.BCONST(inBoolean)));
end setVarFixed;

public function removeFixedAttribute
input output BackendDAE.Var var;
algorithm
if isSome(var.values) then
var.values := DAEUtil.setFixedAttr(var.values, NONE());
end if;
end removeFixedAttribute;

public function removeStartAttribute
input output BackendDAE.Var var;
algorithm
if isSome(var.values) then
var.values := DAEUtil.setStartAttrOption(var.values, NONE());
end if;
end removeStartAttribute;

public function varFixed "author: PA
Extracts the fixed attribute of a variable.
The default fixed value is used if not found. Default is true for parameters
Expand Down
6 changes: 3 additions & 3 deletions Compiler/SimCode/SimCodeMain.mo
Expand Up @@ -774,7 +774,7 @@ algorithm
list<BackendDAE.Equation> removedInitialEquationLst;
Real fsize;
Option<DAE.DAElist> odae;
Option<list<String>> strOptPostOptModules;
Option<list<String>> strPreOptModules;
Boolean isFMI2;
String fmiVersion;
BackendDAE.SymbolicJacobians fmiDer;
Expand Down Expand Up @@ -844,10 +844,10 @@ algorithm
else false;
end match;
// FMI 2.0: enable postOptModule to create alias variables for output states
strOptPostOptModules := if isFMI2 then SOME("createAliasVarsForOutputStates"::BackendDAEUtil.getPostOptModulesString()) else NONE();
strPreOptModules := if isFMI2 then SOME("introduceOutputAliases"::BackendDAEUtil.getPreOptModulesString()) else NONE();

//BackendDump.printBackendDAE(dlow);
(dlow, initDAE, initDAE_lambda0, inlineData, removedInitialEquationLst) := BackendDAEUtil.getSolvedSystem(dlow,inFileNamePrefix,strPostOptModules=strOptPostOptModules);
(dlow, initDAE, initDAE_lambda0, inlineData, removedInitialEquationLst) := BackendDAEUtil.getSolvedSystem(dlow,inFileNamePrefix,strPreOptModules=strPreOptModules);

// generate derivatives
if isFMI2 and not Flags.isSet(Flags.FMI20_DEPENDENCIES) then
Expand Down
2 changes: 1 addition & 1 deletion Compiler/Util/Flags.mo
Expand Up @@ -807,6 +807,7 @@ constant ConfigFlag PRE_OPT_MODULES = CONFIG_FLAG(12, "preOptModules",
"encapsulateWhenConditions"
}),
SOME(STRING_DESC_OPTION({
("introduceOutputAliases", Util.gettext("Introduces aliases for top-level outputs.")),
("clockPartitioning", Util.gettext("Does the clock partitioning.")),
("collapseArrayExpressions", collapseArrayExpressionsText),
("comSubExp", Util.gettext("Introduces alias assignments for variables which are assigned to simple terms i.e. a = b/c; d = b/c; --> a=d")),
Expand Down Expand Up @@ -910,7 +911,6 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
("collapseArrayExpressions", collapseArrayExpressionsText),
("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.")),
("countOperations", Util.gettext("Count the mathematical operations of the system.")),
("createAliasVarsForOutputStates", Util.gettext("Module creates alias variables for output states.")),
("cseBinary", Util.gettext("Common Sub-expression Elimination")),
("dumpComponentsGraphStr", Util.notrans("Dumps the assignment graph used to determine strong components to format suitable for Mathematica")),
("dumpDAE", Util.gettext("dumps the DAE representation of the current transformation state")),
Expand Down

0 comments on commit bf2e0a4

Please sign in to comment.