Skip to content

Commit

Permalink
Return default start value for enumerations (#8569)
Browse files Browse the repository at this point in the history
  • Loading branch information
hkiel committed Feb 18, 2022
1 parent edbf694 commit e907ba7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 33 deletions.
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/BackEnd/BackendVariable.mo
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions OMCompiler/Compiler/FrontEnd/DAEUtil.mo
Expand Up @@ -903,20 +903,24 @@ end setMinMax;
public function getStartAttr "
Return the start attribute."
input Option<DAE.VariableAttributes> 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<String> 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;
Expand Down
14 changes: 7 additions & 7 deletions OMCompiler/Compiler/FrontEnd/StateMachineFlatten.mo
Expand Up @@ -417,7 +417,6 @@ protected
list<DAE.Element> varLst1, varLst2, assignedVarLst, stateVarLst, otherLst1, equationLst1, equationLst2, otherLst2, flatSmLst, otherLst3;
DAE.ComponentRef componentRef;
list<DAE.ComponentRef> stateVarCrefs;
list<Option<DAE.VariableAttributes>> variableAttributesOptions;
list<Option<DAE.Exp>> startValuesOpt;
list<tuple<DAE.ComponentRef, Option<DAE.Exp>>> varCrefStartVal;
list<DAE.Element> dAElist "a component with subelements";
Expand All @@ -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
Expand Down Expand Up @@ -1039,20 +1037,22 @@ end traversingSubsPreviousCrefs;
protected function getStartAttrOption "
Helper function to smCompToDataFlow
"
input Option<DAE.VariableAttributes> inVarAttrOpt;
input DAE.Element inElt;
output Option<DAE.Exp> outExpOpt;
protected
DAE.Exp start;
DAE.Type ty;
Option<DAE.VariableAttributes> 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
Expand Down
48 changes: 33 additions & 15 deletions testsuite/simulation/modelica/initialization/parameters.mos
Expand Up @@ -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();
Expand All @@ -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
Expand All @@ -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;
// "[<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>:12:5-12:45:writable] Warning: The parameter b2 has fixed = false and a binding equation b2 = true, which is probably redundant.
// "[<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>:11:5-11:42:writable] Warning: The parameter i2 has fixed = false and a binding equation i2 = 2, which is probably redundant.
// [<interactive>: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.
// [<interactive>:10:5-10:41:writable] Warning: The parameter r2 has fixed = false and a binding equation r2 = 2.0, which is probably redundant.
// [<interactive>: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.
// [<interactive>: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.
// [<interactive>: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

0 comments on commit e907ba7

Please sign in to comment.