From e907ba7a81a9ff808aa8ada9914c70d9c1ef680d Mon Sep 17 00:00:00 2001 From: Henning Kiel Date: Fri, 18 Feb 2022 16:33:13 +0100 Subject: [PATCH] Return default start value for enumerations (#8569) --- .../Compiler/BackEnd/BackendVariable.mo | 2 +- OMCompiler/Compiler/FrontEnd/DAEUtil.mo | 24 ++++++---- .../Compiler/FrontEnd/StateMachineFlatten.mo | 14 +++--- .../modelica/initialization/parameters.mos | 48 +++++++++++++------ 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/OMCompiler/Compiler/BackEnd/BackendVariable.mo b/OMCompiler/Compiler/BackEnd/BackendVariable.mo index 61147704c94..54449d845c7 100644 --- a/OMCompiler/Compiler/BackEnd/BackendVariable.mo +++ b/OMCompiler/Compiler/BackEnd/BackendVariable.mo @@ -195,7 +195,7 @@ public function varStartValue "author: PA input BackendDAE.Var inVar; output DAE.Exp sv; algorithm - sv := DAEUtil.getStartAttr(inVar.values); + sv := DAEUtil.getStartAttr(inVar.values, inVar.varType); end varStartValue; public function varUnreplaceable "author: lochel diff --git a/OMCompiler/Compiler/FrontEnd/DAEUtil.mo b/OMCompiler/Compiler/FrontEnd/DAEUtil.mo index 68822f4193c..8ca024991bb 100644 --- a/OMCompiler/Compiler/FrontEnd/DAEUtil.mo +++ b/OMCompiler/Compiler/FrontEnd/DAEUtil.mo @@ -903,20 +903,24 @@ end setMinMax; public function getStartAttr " Return the start attribute." input Option inVariableAttributesOption; + input DAE.Type inType; output DAE.Exp start; algorithm - start := match(inVariableAttributesOption) + start := match(inVariableAttributesOption, inType) local DAE.Exp r; - case (SOME(DAE.VAR_ATTR_REAL(start = SOME(r)))) then r; - case (SOME(DAE.VAR_ATTR_REAL(start = NONE()))) then DAE.RCONST(0.0); - case (SOME(DAE.VAR_ATTR_INT(start = SOME(r)))) then r; - case (SOME(DAE.VAR_ATTR_INT(start = NONE()))) then DAE.ICONST(0); - case (SOME(DAE.VAR_ATTR_BOOL(start = SOME(r)))) then r; - case (SOME(DAE.VAR_ATTR_BOOL(start = NONE()))) then DAE.BCONST(false); - case (SOME(DAE.VAR_ATTR_STRING(start = SOME(r)))) then r; - case (SOME(DAE.VAR_ATTR_STRING(start = NONE()))) then DAE.SCONST(""); - case (SOME(DAE.VAR_ATTR_ENUMERATION(start = SOME(r)))) then r; + Absyn.Path path; + list names; + case (SOME(DAE.VAR_ATTR_REAL(start = SOME(r))), _) then r; + case (SOME(DAE.VAR_ATTR_REAL(start = NONE())), _) then DAE.RCONST(0.0); + case (SOME(DAE.VAR_ATTR_INT(start = SOME(r))), _) then r; + case (SOME(DAE.VAR_ATTR_INT(start = NONE())), _) then DAE.ICONST(0); + case (SOME(DAE.VAR_ATTR_BOOL(start = SOME(r))), _) then r; + case (SOME(DAE.VAR_ATTR_BOOL(start = NONE())), _) then DAE.BCONST(false); + case (SOME(DAE.VAR_ATTR_STRING(start = SOME(r))), _) then r; + case (SOME(DAE.VAR_ATTR_STRING(start = NONE())), _) then DAE.SCONST(""); + case (SOME(DAE.VAR_ATTR_ENUMERATION(start = SOME(r))), _) then r; + case (SOME(DAE.VAR_ATTR_ENUMERATION(start = NONE())), DAE.T_ENUMERATION(path = path, names = names)) then DAE.ENUM_LITERAL(AbsynUtil.joinPaths(path, Absyn.IDENT(listHead(names))), 1); else DAE.RCONST(0.0); end match; end getStartAttr; diff --git a/OMCompiler/Compiler/FrontEnd/StateMachineFlatten.mo b/OMCompiler/Compiler/FrontEnd/StateMachineFlatten.mo index 27029f07a4f..5551668161d 100644 --- a/OMCompiler/Compiler/FrontEnd/StateMachineFlatten.mo +++ b/OMCompiler/Compiler/FrontEnd/StateMachineFlatten.mo @@ -417,7 +417,6 @@ protected list varLst1, varLst2, assignedVarLst, stateVarLst, otherLst1, equationLst1, equationLst2, otherLst2, flatSmLst, otherLst3; DAE.ComponentRef componentRef; list stateVarCrefs; - list> variableAttributesOptions; list> startValuesOpt; list>> varCrefStartVal; list dAElist "a component with subelements"; @@ -439,8 +438,7 @@ algorithm //print("StateMachineFlatten.smCompToDataFlow: stateVarLst:\n" + DAEDump.dumpElementsStr(stateVarLst) +"\n"); stateVarCrefs := List.map(stateVarLst, DAEUtil.varCref); - variableAttributesOptions := List.map(stateVarLst, DAEUtil.getVariableAttributes); - startValuesOpt := List.map(variableAttributesOptions, getStartAttrOption); + startValuesOpt := List.map(stateVarLst, getStartAttrOption); varCrefStartVal := List.zip(stateVarCrefs, startValuesOpt); crToExpOpt := HashTableCrToExpOption.emptyHashTableSized(listLength(varCrefStartVal) + 1); // create table that maps the cref of a variable to its start value @@ -1039,20 +1037,22 @@ end traversingSubsPreviousCrefs; protected function getStartAttrOption " Helper function to smCompToDataFlow " - input Option inVarAttrOpt; + input DAE.Element inElt; output Option outExpOpt; protected DAE.Exp start; + DAE.Type ty; + Option varAttrOpt; algorithm - if isSome(inVarAttrOpt) then - start := DAEUtil.getStartAttr(inVarAttrOpt); + DAE.VAR(variableAttributesOption=varAttrOpt, ty=ty) := inElt; + if isSome(varAttrOpt) then + start := DAEUtil.getStartAttr(varAttrOpt, ty); outExpOpt := SOME(start); else outExpOpt := NONE(); end if; end getStartAttrOption; - protected function addPropagationEquations " Author: BTH Add activation and reset propagation related equation and variables to flat state machine diff --git a/testsuite/simulation/modelica/initialization/parameters.mos b/testsuite/simulation/modelica/initialization/parameters.mos index 1955f2b7383..d6b67b577a7 100644 --- a/testsuite/simulation/modelica/initialization/parameters.mos +++ b/testsuite/simulation/modelica/initialization/parameters.mos @@ -8,20 +8,25 @@ loadString(" within ; package initializationTests + type E = enumeration(E1, E2, E3, En); model parameters parameter Real r1; parameter Integer i1; parameter Boolean b1; parameter String s1; + parameter E e1; parameter Real r2(fixed=false) = 2.0; parameter Integer i2(fixed=false) = 2; parameter Boolean b2(fixed=false) = true; + parameter String s2(fixed=false) = \"two\"; + parameter E e2(fixed=false) = E.E2; parameter Real r3(start=3.0); parameter Integer i3(start=3); parameter Boolean b3(start=true); parameter String s3(start=\"three\"); + parameter E e3 (start=E.E3); end parameters; end initializationTests; "); getErrorString(); @@ -36,6 +41,9 @@ simulate(initializationTests.parameters, simflags="-lv=LOG_INIT_V"); getErrorStr // resultFile = "initializationTests.parameters_res.mat", // simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'initializationTests.parameters', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = '-lv=LOG_INIT_V'", // messages = "LOG_INIT | info | ### START INITIALIZATION ### +// assert | warning | The following assertion has been violated at time 0.000000 +// | | | | e1 >= initializationTests.E.E1 and e1 <= initializationTests.E.En +// assert | warning | Variable violating min/max constraint: initializationTests.E.E1 <= e1 <= initializationTests.E.En, has value: 0 // LOG_INIT | info | updating min-values // LOG_INIT | info | updating max-values // LOG_INIT | info | updating nominal-values @@ -48,35 +56,45 @@ simulate(initializationTests.parameters, simflags="-lv=LOG_INIT_V"); getErrorStr // | | | | | | [2] parameter Real r2(start=2, fixed=false) = 2 // | | | | | | [3] parameter Real r3(start=3, fixed=true) = 3 // | | | | | integer parameters -// | | | | | | [1] parameter Integer i1(start=0, fixed=true) = 0 -// | | | | | | [2] parameter Integer i2(start=2, fixed=false) = 2 -// | | | | | | [3] parameter Integer i3(start=3, fixed=true) = 3 +// | | | | | | [1] parameter Integer e1(start=0, fixed=true) = 0 +// | | | | | | [2] parameter Integer e2(start=2, fixed=false) = 2 +// | | | | | | [3] parameter Integer e3(start=3, fixed=true) = 3 +// | | | | | | [4] parameter Integer i1(start=0, fixed=true) = 0 +// | | | | | | [5] parameter Integer i2(start=2, fixed=false) = 2 +// | | | | | | [6] parameter Integer i3(start=3, fixed=true) = 3 // | | | | | boolean parameters // | | | | | | [1] parameter Boolean b1(start=false, fixed=true) = false // | | | | | | [2] parameter Boolean b2(start=true, fixed=false) = true // | | | | | | [3] parameter Boolean b3(start=true, fixed=true) = true // | | | | | string parameters // | | | | | | [1] parameter String s1(start=\"\") = \"\" -// | | | | | | [2] parameter String s3(start=\"three\") = \"three\" +// | | | | | | [2] parameter String s2(start=\"two\") = \"two\" +// | | | | | | [3] parameter String s3(start=\"three\") = \"three\" // LOG_SOTI | info | ### SOLUTION OF THE INITIALIZATION ### // LOG_INIT | info | ### END INITIALIZATION ### // LOG_SUCCESS | info | The initialization finished successfully without homotopy method. // LOG_SUCCESS | info | The simulation finished successfully. // " // end SimulationResult; -// "[:17:5-17:39:writable] Warning: Parameter s3 has no value, and is fixed during initialization (fixed=true), using available start value (start=\"three\") as default value. -// [:16:5-16:37:writable] Warning: Parameter b3 has no value, and is fixed during initialization (fixed=true), using available start value (start=true) as default value. -// [:15:5-15:34:writable] Warning: Parameter i3 has no value, and is fixed during initialization (fixed=true), using available start value (start=3) as default value. -// [:14:5-14:33:writable] Warning: Parameter r3 has no value, and is fixed during initialization (fixed=true), using available start value (start=3.0) as default value. -// [:8:5-8:24:writable] Warning: Parameter s1 has no value, and is fixed during initialization (fixed=true), using available start value (start=\"\") as default value. -// [:7:5-7:25:writable] Warning: Parameter b1 has no value, and is fixed during initialization (fixed=true), using available start value (start=false) as default value. -// [:6:5-6:25:writable] Warning: Parameter i1 has no value, and is fixed during initialization (fixed=true), using available start value (start=0) as default value. -// [:5:5-5:22:writable] Warning: Parameter r1 has no value, and is fixed during initialization (fixed=true), using available start value (start=0.0) as default value. -// [:12:5-12:45:writable] Warning: The parameter b2 has fixed = false and a binding equation b2 = true, which is probably redundant. +// "[:22:5-22:32:writable] Warning: Parameter e3 has no value, and is fixed during initialization (fixed=true), using available start value (start=initializationTests.E.E3) as default value. +// [:21:5-21:39:writable] Warning: Parameter s3 has no value, and is fixed during initialization (fixed=true), using available start value (start=\"three\") as default value. +// [:20:5-20:37:writable] Warning: Parameter b3 has no value, and is fixed during initialization (fixed=true), using available start value (start=true) as default value. +// [:19:5-19:34:writable] Warning: Parameter i3 has no value, and is fixed during initialization (fixed=true), using available start value (start=3) as default value. +// [:18:5-18:33:writable] Warning: Parameter r3 has no value, and is fixed during initialization (fixed=true), using available start value (start=3.0) as default value. +// [:10:5-10:19:writable] Warning: Parameter e1 has no value, and is fixed during initialization (fixed=true), using available start value (start=initializationTests.E.E1) as default value. +// [:9:5-9:24:writable] Warning: Parameter s1 has no value, and is fixed during initialization (fixed=true), using available start value (start=\"\") as default value. +// [:8:5-8:25:writable] Warning: Parameter b1 has no value, and is fixed during initialization (fixed=true), using available start value (start=false) as default value. +// [:7:5-7:25:writable] Warning: Parameter i1 has no value, and is fixed during initialization (fixed=true), using available start value (start=0) as default value. +// [:6:5-6:22:writable] Warning: Parameter r1 has no value, and is fixed during initialization (fixed=true), using available start value (start=0.0) as default value. +// [:16:5-16:39:writable] Warning: The parameter e2 has fixed = false and a binding equation e2 = initializationTests.E.E2, which is probably redundant. // Setting fixed = false usually means there is an additional initial equation to determine the parameter value. The binding was ignored by old Modelica tools, but this is not according to the Modelica specification. Please remove the parameter binding, or bind the parameter to another parameter with fixed = false and no binding. -// [:11:5-11:42:writable] Warning: The parameter i2 has fixed = false and a binding equation i2 = 2, which is probably redundant. +// [:15:5-15:45:writable] Warning: The parameter s2 has fixed = false and a binding equation s2 = \"two\", which is probably redundant. // Setting fixed = false usually means there is an additional initial equation to determine the parameter value. The binding was ignored by old Modelica tools, but this is not according to the Modelica specification. Please remove the parameter binding, or bind the parameter to another parameter with fixed = false and no binding. -// [:10:5-10:41:writable] Warning: The parameter r2 has fixed = false and a binding equation r2 = 2.0, which is probably redundant. +// [:14:5-14:45:writable] Warning: The parameter b2 has fixed = false and a binding equation b2 = true, which is probably redundant. +// Setting fixed = false usually means there is an additional initial equation to determine the parameter value. The binding was ignored by old Modelica tools, but this is not according to the Modelica specification. Please remove the parameter binding, or bind the parameter to another parameter with fixed = false and no binding. +// [:13:5-13:42:writable] Warning: The parameter i2 has fixed = false and a binding equation i2 = 2, which is probably redundant. +// Setting fixed = false usually means there is an additional initial equation to determine the parameter value. The binding was ignored by old Modelica tools, but this is not according to the Modelica specification. Please remove the parameter binding, or bind the parameter to another parameter with fixed = false and no binding. +// [:12:5-12:41:writable] Warning: The parameter r2 has fixed = false and a binding equation r2 = 2.0, which is probably redundant. // Setting fixed = false usually means there is an additional initial equation to determine the parameter value. The binding was ignored by old Modelica tools, but this is not according to the Modelica specification. Please remove the parameter binding, or bind the parameter to another parameter with fixed = false and no binding. // " // endResult