Skip to content

Commit

Permalink
Keep start values of simvars but do not print them to modelDescriptio…
Browse files Browse the repository at this point in the history
…n.xml. (#10850)

Co-authored-by: arun3688 <arun3688@users.noreply.github.com>
  • Loading branch information
mahge and arun3688 committed Jun 20, 2023
1 parent 18f104f commit 9166c1a
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 140 deletions.
52 changes: 28 additions & 24 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -8442,28 +8442,36 @@ algorithm
end match;
end getDefaultFmiInitialAttribute;

protected function clearUpDefaultFmiAttributes
"Replaces default values of the following attributes with NONE(): initial, causality, variability"
input output SimCodeVar.SimVar simVar;
public function getFmiInitialAttributeStr
"This function is called from CodegenFMUCommon.tpl. It compares a variable's initial_ fmi attriute
with the default expected (based on teh variability and causality of the variable). If it turns out
to be the same as the default then it will return an empty string so that the value is not
printed to the modelDescription.xml file. However, if the flag DUMP_FORCE_FMI_ATTRIBUTES is set,
it will always print the attrbute whether it is equal to the defaul or not."
input SimCodeVar.SimVar simVar;
output String out_string = "";
protected
SimCodeVar.Causality default_causality = SimCodeVar.LOCAL();
SimCodeVar.Variability default_variability = SimCodeVar.CONTINUOUS();
SimCodeVar.Initial default_initial;
SimCodeVar.Initial var_initial, default_initial;
algorithm
default_initial := getDefaultFmiInitialAttribute(Util.getOptionOrDefault(simVar.variability, default_variability), Util.getOptionOrDefault(simVar.causality, default_causality));

if isSome(simVar.initial_) and valueEq(Util.getOption(simVar.initial_), default_initial) then
simVar.initial_ := NONE();
if isNone(simVar.initial_) then
return;
end if;

if isSome(simVar.causality) and valueEq(Util.getOption(simVar.causality), default_causality) then
simVar.causality := NONE();
end if;
SOME(var_initial) := simVar.initial_;
default_initial := getDefaultFmiInitialAttribute(Util.getOptionOrDefault(simVar.variability, SimCodeVar.CONTINUOUS())
, Util.getOptionOrDefault(simVar.causality, SimCodeVar.LOCAL()));

if isSome(simVar.variability) and valueEq(Util.getOption(simVar.variability), default_variability) then
simVar.variability := NONE();
if valueEq(var_initial, default_initial) and not Flags.isSet(Flags.DUMP_FORCE_FMI_ATTRIBUTES) then
var_initial := SimCodeVar.NONE_INITIAL(); // Set it to NONE_INITIAL here so the case below turns it to ""
end if;
end clearUpDefaultFmiAttributes;

out_string := match var_initial
case SimCodeVar.EXACT(__) then "exact";
case SimCodeVar.APPROX(__) then "approx";
case SimCodeVar.CALCULATED(__) then "calculated";
case SimCodeVar.NONE_INITIAL(__) then "";
end match;
end getFmiInitialAttributeStr;

// one dlow var can result in multiple simvars: input and output are a subset
// of algvars for example
Expand Down Expand Up @@ -8589,12 +8597,6 @@ algorithm
derivSimvar := simVar; // Just in case
end if;

// clear up default values to improve readability of modelDescription.xml
if not Flags.isSet(Flags.DUMP_FORCE_FMI_ATTRIBUTES) then
simVar := clearUpDefaultFmiAttributes(simVar);
derivSimvar := clearUpDefaultFmiAttributes(derivSimvar) "just in case";
end if;

//print("\n name :" + ComponentReference.printComponentRefStr(simVar.name) + "===>" + anyString(simVar.varKind) + "\n");
// If it is an input variable, we give it an index
if (not isalias) and BackendVariable.isVarOnTopLevelAndInputNoDerInput(dlowVar) then
Expand Down Expand Up @@ -10243,7 +10245,9 @@ protected function startValueIsConstOrDefault
input DAE.Type type_;
output Option<DAE.Exp> outstart_value;
algorithm
if Expression.isConstValue(Util.getOption(start_value)) then
if Util.isNone(start_value) then
outstart_value := NONE();
elseif Expression.isConstValue(Util.getOption(start_value)) then
outstart_value := start_value;
else
outstart_value := setDefaultStartValue(type_);
Expand All @@ -10265,7 +10269,7 @@ algorithm
case (NONE()) guard isInitialExactOrApprox(initial_) then setDefaultStartValue(var.varType);
case (SOME(_)) guard isCausalityInput(causality) then startValueIsConstOrDefault(startValue, var.varType);
case (NONE()) guard isCausalityInput(causality) then setDefaultStartValue(var.varType);
else NONE();
else startValueIsConstOrDefault(startValue, var.varType);
end match;
end if;
end updateStartValue;
Expand Down
35 changes: 19 additions & 16 deletions OMCompiler/Compiler/Template/CodegenFMUCommon.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ match simVar
let clockIndex = getClockIndex(simVar, simCode)
let previous = match varKind case CLOCKED_STATE(__) then '<%getVariableIndex(cref2simvar(previousName, simCode))%>'
let caus = getCausality2(causality)
let initial = getInitialType2(initial_)
let initial = getFmiInitialAttributeStr(simVar)
<<
name="<%System.stringReplace(crefStrNoUnderscore(Util.getOption(exportVar)),"$", "_D_")%>"
valueReference="<%valueReference%>"
Expand All @@ -502,7 +502,7 @@ match variability
case SOME(DISCRETE(__)) then "discrete"
case SOME(FIXED(__)) then "fixed"
case SOME(CONSTANT(__)) then "constant"
case SOME(CONTINUOUS(__)) then "continuous" // default
case SOME(CONTINUOUS(__)) then if Flags.isSet(Flags.DUMP_FORCE_FMI_ATTRIBUTES) then "continuous" else "" // default
case SOME(TUNABLE(__)) then "tunable"
else ""
end getVariability2;
Expand All @@ -514,7 +514,7 @@ match c
case SOME(NONECAUS(__)) then "none"
case SOME(OUTPUT(__)) then "output"
case SOME(INPUT(__)) then "input"
case SOME(LOCAL(__)) then "local" // same as INTERNAL() see FMI-2.0 specification
case SOME(LOCAL(__)) then if Flags.isSet(Flags.DUMP_FORCE_FMI_ATTRIBUTES) then "local" else "" // same as INTERNAL() see FMI-2.0 specification
case SOME(PARAMETER(__)) then "parameter"
case SOME(CALCULATED_PARAMETER(__)) then "calculatedParameter"
else ""
Expand All @@ -534,16 +534,6 @@ match simCode
else ""
end getNumberOfEventIndicators;

template getInitialType2(Option<Initial> initial_)
"Returns the Initial Attribute for fmiexport"
::=
match initial_
case SOME(EXACT(__)) then "exact"
case SOME(APPROX(__)) then "approx"
case SOME(CALCULATED(__)) then "calculated"
else ""
end getInitialType2;

template ScalarVariableType2(SimVar simvar, list<SimVar> stateVars)
"Generates code for ScalarVariable Type file for FMU 2.0 target."
::=
Expand Down Expand Up @@ -579,13 +569,26 @@ case SIMVAR(varKind = varKind, index = index) then
else ''
end DerivativeVarIndex;

// template StartString2(SimVar simvar)
// ::=
// match simvar
// case SIMVAR(aliasvar = SimCodeVar.ALIAS(__)) then ''
// case SIMVAR(initialValue = initialValue) then
// match initialValue
// case SOME(initialValue) then ' start="<%initValXml(initialValue)%>"'
// else ''
// end StartString2;

template StartString2(SimVar simvar)
::=
match simvar
case SIMVAR(aliasvar = SimCodeVar.ALIAS(__)) then ''
case SIMVAR(initialValue = initialValue) then
match initialValue
case SOME(initialValue) then ' start="<%initValXml(initialValue)%>"'
case SIMVAR(initialValue = NONE()) then ''
case SIMVAR(initialValue = SOME(initialValue), causality = SOME(SimCodeVar.INPUT())) then ' start="<%initValXml(initialValue)%>"'
case SIMVAR(initialValue = SOME(initialValue), initial_ = initial_) then
match initial_
case SOME(SimCodeVar.EXACT()) then ' start="<%initValXml(initialValue)%>"'
case SOME(SimCodeVar.APPROX()) then ' start="<%initValXml(initialValue)%>"'
else ''
end StartString2;

Expand Down
6 changes: 6 additions & 0 deletions OMCompiler/Compiler/Template/SimCodeTV.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,11 @@ package SimCodeUtil
input list<BackendDAE.SimIterator> iters;
output Integer size ;
end getSimIteratorSize;

function getFmiInitialAttributeStr
input SimCodeVar.SimVar simVar;
output String out_string;
end getFmiInitialAttributeStr;
end SimCodeUtil;

package SimCodeFunctionUtil
Expand Down Expand Up @@ -4039,6 +4044,7 @@ package Flags
constant ConfigFlag ZEROMQ_SERVER_ID;
constant ConfigFlag ZEROMQ_CLIENT_ID;
constant ConfigFlag FMI_FILTER;
constant DebugFlag DUMP_FORCE_FMI_ATTRIBUTES;
constant ConfigFlag EXPORT_CLOCKS_IN_MODELDESCRIPTION;
constant ConfigFlag OBFUSCATE;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ readFile("fmi_attributes_06.xml"); getErrorString();
// name=\"_D_STATESET1.A[1]\"
// valueReference=\"0\"
// variability=\"discrete\"
// causality=\"local\"
// initial=\"exact\">
// <Integer start=\"1\"/>
// </ScalarVariable>
Expand All @@ -177,7 +176,6 @@ readFile("fmi_attributes_06.xml"); getErrorString();
// name=\"_D_STATESET1.A[2]\"
// valueReference=\"1\"
// variability=\"discrete\"
// causality=\"local\"
// initial=\"exact\">
// <Integer start=\"0\"/>
// </ScalarVariable>
Expand All @@ -186,7 +184,6 @@ readFile("fmi_attributes_06.xml"); getErrorString();
// name=\"_D_STATESET2.A[1]\"
// valueReference=\"2\"
// variability=\"discrete\"
// causality=\"local\"
// initial=\"exact\">
// <Integer start=\"1\"/>
// </ScalarVariable>
Expand All @@ -195,7 +192,6 @@ readFile("fmi_attributes_06.xml"); getErrorString();
// name=\"_D_STATESET2.A[2]\"
// valueReference=\"3\"
// variability=\"discrete\"
// causality=\"local\"
// initial=\"exact\">
// <Integer start=\"0\"/>
// </ScalarVariable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ readFile("fmi_attributes_08.xml"); getErrorString();
// <ScalarVariable
// name=\"_D_TMP_D_VAR_D_2_D_0PREX_D_TAN\"
// valueReference=\"2\"
// causality=\"local\"
// >
// <Real/>
// </ScalarVariable>
Expand Down

0 comments on commit 9166c1a

Please sign in to comment.