Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit bf2e0a4

Browse files
lochelOpenModelica-Hudson
authored andcommitted
Don't rename outputs
Belonging to [master]: - #2899 - OpenModelica/OpenModelica-testsuite#1114
1 parent 7eaf6bc commit bf2e0a4

File tree

5 files changed

+88
-37
lines changed

5 files changed

+88
-37
lines changed

Compiler/BackEnd/BackendDAE.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ public constant Integer SymbolicJacobianDIndex = 4;
739739
public constant String derivativeNamePrefix = "$DERAlias";
740740
public constant String partialDerivativeNamePrefix = "$pDER";
741741
public constant String functionDerivativeNamePrefix = "$funDER";
742-
public constant String outputStateAliasPrefix = "$outputStateAlias_";
742+
public constant String outputAliasPrefix = "$outputAlias_";
743743

744744
public constant String optimizationMayerTermName = "$OMC$objectMayerTerm";
745745
public constant String optimizationLagrangeTermName = "$OMC$objectLagrangeTerm";

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 67 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,55 +1865,90 @@ algorithm
18651865
end match;
18661866
end reduceEqSystem;
18671867

1868-
public function createAliasVarsForOutputStates
1869-
" creates for every state that is also output an
1870-
alias variable $outputStateAlias.stateName and
1871-
the corresponding equation"
1872-
input BackendDAE.BackendDAE inBDAE;
1873-
output BackendDAE.BackendDAE outBDAE = inBDAE;
1868+
public function introduceOutputAliases
1869+
input output BackendDAE.BackendDAE dae;
18741870
protected
18751871
BackendDAE.EqSystems systems, returnSysts = {};
1876-
BackendDAE.Variables vars;
1872+
BackendDAE.Variables vars, newVars;
18771873
BackendDAE.EquationArray eqs;
1878-
list<BackendDAE.Var> states;
1879-
DAE.ComponentRef newCref;
1874+
list<BackendDAE.Equation> newEqns;
1875+
DAE.ComponentRef newCref, cref;
18801876
BackendDAE.Var newVar;
18811877
BackendDAE.Equation newEqn;
1878+
HashSet.HashSet topLevelOutputs = HashSet.emptyHashSet();
18821879
algorithm
1883-
systems := inBDAE.eqs;
1880+
systems := dae.eqs;
18841881
for system in systems loop
1885-
eqs := system.orderedEqs;
1882+
eqs := system.orderedEqs;
18861883
vars := system.orderedVars;
1887-
states := BackendVariable.getAllStateVarFromVariables(vars);
1888-
if Config.languageStandardAtLeast(Config.LanguageStandard.'3.3') then
1889-
states := listAppend(states, BackendVariable.getAllClockedStatesFromVariables(vars));
1890-
end if;
1891-
for v in states loop
1892-
if BackendVariable.isVarOnTopLevelAndOutput(v) then
1893-
// generate new output var and add it
1894-
newCref := ComponentReference.prependStringCref(BackendDAE.outputStateAliasPrefix, BackendVariable.varCref(v));
1884+
newVars := BackendVariable.emptyVarsSized(realInt(intReal(BackendVariable.varsSize(vars)) * 1.4));
1885+
newEqns := {};
1886+
1887+
for v in BackendVariable.varList(vars) loop
1888+
if not BackendVariable.isVarOnTopLevelAndOutput(v) then
1889+
newVars := BackendVariable.addVar(v, newVars);
1890+
else
1891+
cref := BackendVariable.varCref(v);
1892+
topLevelOutputs := BaseHashSet.add(cref, topLevelOutputs);
1893+
1894+
// generate new internal alias var
1895+
newCref := ComponentReference.prependStringCref(BackendDAE.outputAliasPrefix, cref);
18951896
newVar := BackendVariable.copyVarNewName(newCref, v);
1896-
newVar := BackendVariable.setVarKind(newVar, BackendDAE.VARIABLE());
1897-
vars := BackendVariable.addVar(newVar, vars);
1897+
newVar := BackendVariable.setVarDirection(newVar, DAE.BIDIR());
1898+
newVars := BackendVariable.addVar(newVar, newVars);
18981899

1899-
//update states to remove the output direction
1900-
v := BackendVariable.setVarDirection(v, DAE.BIDIR());
1901-
vars := BackendVariable.addVar(v, vars);
1900+
// update output to ordinary variable
1901+
v := BackendVariable.setVarKind(v, BackendDAE.VARIABLE());
1902+
v := BackendVariable.removeFixedAttribute(v);
1903+
v := BackendVariable.removeStartAttribute(v);
1904+
newVars := BackendVariable.addVar(v, newVars);
19021905

19031906
// generate new equation and add it
1904-
newEqn := BackendEquation.generateEquation(Expression.crefToExp(newCref), Expression.crefToExp(BackendVariable.varCref(v)), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_BINDING);
1905-
eqs := BackendEquation.add(newEqn, eqs);
1907+
newEqn := BackendEquation.generateEquation(Expression.crefToExp(cref), Expression.crefToExp(newCref), DAE.emptyElementSource, BackendDAE.EQ_ATTR_DEFAULT_BINDING);
1908+
newEqns := newEqn::newEqns;
19061909
end if;
19071910
end for;
1908-
system.orderedVars := vars;
1911+
1912+
_ := traverseBackendDAEExpsEqns(eqs, introduceOutputAliases_eqs, topLevelOutputs);
1913+
eqs := BackendEquation.addList(newEqns, eqs);
1914+
system.orderedVars := newVars;
19091915
system.orderedEqs := eqs;
1910-
system := BackendDAEUtil.clearEqSyst(system);
19111916
returnSysts := system::returnSysts;
19121917
end for;
1918+
19131919
returnSysts := listReverse(returnSysts);
1914-
outBDAE.eqs := returnSysts;
1915-
outBDAE := BackendDAEUtil.transformBackendDAE(outBDAE, SOME((BackendDAE.NO_INDEX_REDUCTION(), BackendDAE.EXACT())), NONE(), NONE());
1916-
end createAliasVarsForOutputStates;
1920+
dae.eqs := returnSysts;
1921+
end introduceOutputAliases;
1922+
1923+
protected function introduceOutputAliases_eqs
1924+
input DAE.Exp inExp;
1925+
input HashSet.HashSet inStates;
1926+
output DAE.Exp outExp;
1927+
output HashSet.HashSet outStates;
1928+
algorithm
1929+
(outExp, outStates) := Expression.traverseExpBottomUp(inExp, introduceOutputAliases_eqs2, inStates);
1930+
end introduceOutputAliases_eqs;
1931+
1932+
protected function introduceOutputAliases_eqs2
1933+
input DAE.Exp inExp;
1934+
input HashSet.HashSet inStates;
1935+
output DAE.Exp outExp;
1936+
output HashSet.HashSet outStates = inStates;
1937+
algorithm
1938+
outExp := match inExp
1939+
local
1940+
DAE.Exp e1;
1941+
DAE.ComponentRef cr, newCref;
1942+
1943+
// replace der(cr) with der(<outputAliasPrefix> + cr)
1944+
case e1 as DAE.CREF(componentRef=cr) guard BaseHashSet.has(cr, inStates) algorithm
1945+
newCref := ComponentReference.prependStringCref(BackendDAE.outputAliasPrefix, cr);
1946+
e1.componentRef := newCref;
1947+
then e1;
1948+
1949+
else inExp;
1950+
end match;
1951+
end introduceOutputAliases_eqs2;
19171952

19181953
protected function translateArrayList
19191954
input Integer inElement;
@@ -7623,6 +7658,7 @@ end selectMatchingAlgorithm;
76237658
public function allPreOptimizationModules
76247659
"This list contains all back end pre-optimization modules."
76257660
output list<tuple<BackendDAEFunc.optimizationModule, String>> allPreOptimizationModules = {
7661+
(BackendDAEUtil.introduceOutputAliases, "introduceOutputAliases"),
76267662
(Uncertainties.dataReconciliation, "dataReconciliation"),
76277663
(UnitCheck.unitChecking, "unitChecking"),
76287664
(DynamicOptimization.createDynamicOptimization,"createDynamicOptimization"),
@@ -7663,7 +7699,6 @@ end allPreOptimizationModules;
76637699
public function allPostOptimizationModules
76647700
"This list contains all back end sim-optimization modules."
76657701
output list<tuple<BackendDAEFunc.optimizationModule, String>> allPostOptimizationModules = {
7666-
(BackendDAEUtil.createAliasVarsForOutputStates, "createAliasVarsForOutputStates"),
76677702
(BackendInline.lateInlineFunction, "lateInlineFunction"),
76687703
(DynamicOptimization.simplifyConstraints, "simplifyConstraints"),
76697704
(CommonSubExpression.wrapFunctionCalls, "wrapFunctionCalls"),

Compiler/BackEnd/BackendVariable.mo

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,22 @@ algorithm
102102
outVar.values := DAEUtil.setFixedAttr(oattr, SOME(DAE.BCONST(inBoolean)));
103103
end setVarFixed;
104104

105+
public function removeFixedAttribute
106+
input output BackendDAE.Var var;
107+
algorithm
108+
if isSome(var.values) then
109+
var.values := DAEUtil.setFixedAttr(var.values, NONE());
110+
end if;
111+
end removeFixedAttribute;
112+
113+
public function removeStartAttribute
114+
input output BackendDAE.Var var;
115+
algorithm
116+
if isSome(var.values) then
117+
var.values := DAEUtil.setStartAttrOption(var.values, NONE());
118+
end if;
119+
end removeStartAttribute;
120+
105121
public function varFixed "author: PA
106122
Extracts the fixed attribute of a variable.
107123
The default fixed value is used if not found. Default is true for parameters

Compiler/SimCode/SimCodeMain.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ algorithm
774774
list<BackendDAE.Equation> removedInitialEquationLst;
775775
Real fsize;
776776
Option<DAE.DAElist> odae;
777-
Option<list<String>> strOptPostOptModules;
777+
Option<list<String>> strPreOptModules;
778778
Boolean isFMI2;
779779
String fmiVersion;
780780
BackendDAE.SymbolicJacobians fmiDer;
@@ -844,10 +844,10 @@ algorithm
844844
else false;
845845
end match;
846846
// FMI 2.0: enable postOptModule to create alias variables for output states
847-
strOptPostOptModules := if isFMI2 then SOME("createAliasVarsForOutputStates"::BackendDAEUtil.getPostOptModulesString()) else NONE();
847+
strPreOptModules := if isFMI2 then SOME("introduceOutputAliases"::BackendDAEUtil.getPreOptModulesString()) else NONE();
848848

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

852852
// generate derivatives
853853
if isFMI2 and not Flags.isSet(Flags.FMI20_DEPENDENCIES) then

Compiler/Util/Flags.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ constant ConfigFlag PRE_OPT_MODULES = CONFIG_FLAG(12, "preOptModules",
807807
"encapsulateWhenConditions"
808808
}),
809809
SOME(STRING_DESC_OPTION({
810+
("introduceOutputAliases", Util.gettext("Introduces aliases for top-level outputs.")),
810811
("clockPartitioning", Util.gettext("Does the clock partitioning.")),
811812
("collapseArrayExpressions", collapseArrayExpressionsText),
812813
("comSubExp", Util.gettext("Introduces alias assignments for variables which are assigned to simple terms i.e. a = b/c; d = b/c; --> a=d")),
@@ -910,7 +911,6 @@ constant ConfigFlag POST_OPT_MODULES = CONFIG_FLAG(16, "postOptModules",
910911
("collapseArrayExpressions", collapseArrayExpressionsText),
911912
("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.")),
912913
("countOperations", Util.gettext("Count the mathematical operations of the system.")),
913-
("createAliasVarsForOutputStates", Util.gettext("Module creates alias variables for output states.")),
914914
("cseBinary", Util.gettext("Common Sub-expression Elimination")),
915915
("dumpComponentsGraphStr", Util.notrans("Dumps the assignment graph used to determine strong components to format suitable for Mathematica")),
916916
("dumpDAE", Util.gettext("dumps the DAE representation of the current transformation state")),

0 commit comments

Comments
 (0)