@@ -1865,55 +1865,90 @@ algorithm
18651865 end match;
18661866end 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;
18741870protected
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();
18821879algorithm
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
19181953protected function translateArrayList
19191954 input Integer inElement;
@@ -7623,6 +7658,7 @@ end selectMatchingAlgorithm;
76237658public 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;
76637699public 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" ),
0 commit comments