Skip to content

Commit

Permalink
do not export <DiscreteStates> in <ModelStructure> for FMU's (#11288)
Browse files Browse the repository at this point in the history
* do not export <DiscreteStates> in <ModelStructure> for FMU's

* add test

* always use getVariableFMIIndex() in modeldescription.xml

* expected output
  • Loading branch information
arun3688 committed Oct 2, 2023
1 parent 9235af4 commit d1144cf
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 17 deletions.
21 changes: 13 additions & 8 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -14063,17 +14063,17 @@ else
// create empty model structure
try
// create empty derivatives dependencies
derivatives := list(SimCode.FMIUNKNOWN(getVariableIndex(v), {}, {})
derivatives := list(SimCode.FMIUNKNOWN(getVariableFMIIndex(v), {}, {})
for v in getScalarVars(inModelInfo.vars.derivativeVars));

// create empty output dependencies
varsA := List.filterOnTrue(inModelInfo.vars.algVars, isOutputSimVar);
outputs := list(SimCode.FMIUNKNOWN(getVariableIndex(v), {}, {})
outputs := list(SimCode.FMIUNKNOWN(getVariableFMIIndex(v), {}, {})
for v in getScalarVars(varsA));

// create empty clockedStates dependencies
clockedStates := List.filterOnTrue(inModelInfo.vars.algVars, isClockedStateSimVar);
discreteStates := list(SimCode.FMIUNKNOWN(getVariableIndex(v), {}, {})
discreteStates := list(SimCode.FMIUNKNOWN(getVariableFMIIndex(v), {}, {})
for v in getScalarVars(clockedStates));

contPartSimDer := NONE();
Expand Down Expand Up @@ -14319,11 +14319,16 @@ protected
SimCodeVar.SimVar simVar;
algorithm
for cr in crefs loop
var := BackendVariable.getVarSingle(cr, orderedVars);
simVar := BaseHashTable.get(cr, crefSimVarHT);
// Filter only Real vars that match the --fmiFilter flag
if BackendVariable.isRealVar(var) and isSome(simVar.exportVar) then
outVar := var :: outVar;
// skip CLOCK related vars $CLKPRE.Ts and also check for previous, we do not generate partial derivatives for cpp runtime,
// but still some models (e.g) /testsuite/openmodelica/fmi/ModelExchange/2.0/fmi_attributes_20.mos, clock related variables
// pop up in partial derivatives and we should skip those variables
if (not ComponentReference.isPreviousCref(cr) and not ComponentReference.isPreCref(cr)) then
var := BackendVariable.getVarSingle(cr, orderedVars);
simVar := BaseHashTable.get(cr, crefSimVarHT);
// Filter only Real vars that match the --fmiFilter flag
if BackendVariable.isRealVar(var) and isSome(simVar.exportVar) then
outVar := var :: outVar;
end if;
end if;
end for;
end getDependentAndIndepentVarsForJacobian;
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Template/CodegenFMUCommon.tpl
Expand Up @@ -316,7 +316,7 @@ case SOME(fmistruct as FMIMODELSTRUCTURE(__)) then
<ModelStructure>
<%ModelStructureOutputs(fmistruct.fmiOutputs)%>
<%ModelStructureDerivatives(fmistruct.fmiDerivatives)%>
<%ModelStructureDiscreteStates(fmistruct.fmiDiscreteStates)%>
<% if Flags.getConfigBool(Flags.EXPORT_CLOCKS_IN_MODELDESCRIPTION) then ModelStructureDiscreteStates(fmistruct.fmiDiscreteStates) else "" %>
<%ModelStructureInitialUnknowns(fmistruct.fmiInitialUnknowns)%>
</ModelStructure>
>>
Expand Down
Expand Up @@ -945,12 +945,16 @@ readFile("modelDescription.tmp.xml");
// </ModelVariables>
// <ModelStructure>
// <Outputs>
// <Unknown index=\"105\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"106\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"107\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"108\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"101\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"102\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"103\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"104\" dependencies=\"\" dependenciesKind=\"\" />
// </Outputs>
// <DiscreteStates>
// <Unknown index=\"61\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"62\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"63\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"64\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"65\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"66\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"67\" dependencies=\"\" dependenciesKind=\"\" />
Expand Down Expand Up @@ -987,10 +991,6 @@ readFile("modelDescription.tmp.xml");
// <Unknown index=\"98\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"99\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"100\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"101\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"102\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"103\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"104\" dependencies=\"\" dependenciesKind=\"\" />
// </DiscreteStates>
// </ModelStructure>
// </fmiModelDescription>
Expand Down
1 change: 1 addition & 0 deletions testsuite/openmodelica/fmi/ModelExchange/2.0/Makefile
Expand Up @@ -20,6 +20,7 @@ fmi_attributes_16.mos \
fmi_attributes_17.mos \
fmi_attributes_18.mos \
fmi_attributes_19.mos \
fmi_attributes_20.mos \
FMUResourceTest.mos \
testBug2764.mos \
testBug2765.mos \
Expand Down
133 changes: 133 additions & 0 deletions testsuite/openmodelica/fmi/ModelExchange/2.0/fmi_attributes_20.mos
@@ -0,0 +1,133 @@
// name: fmi_attributes_20.mos
// keywords: FMI 2.0 export
// status: correct
// teardown_command: rm -rf fmi_attributes_20.fmu fmi_attributes_20.log fmi_attributes_20.xml fmi_attributes_20_tmp.xml

loadString("
model fmi_attributes_20
parameter Real T = 0.1;
parameter Real k = 1;
Real u;
Real a;
Real x(start = 0);
output Real y;
Real Ts = interval(u);
equation
a = sin(time);
when Clock(0.1) then
u = sample(a);
x = previous(x) + previous(Ts) / T * u;
end when;
y = k * hold(x);
end fmi_attributes_20;
"); getErrorString();

buildModelFMU(fmi_attributes_20); getErrorString();

// unzip to console, quiet, extra quiet
system("unzip -cqq fmi_attributes_20.fmu modelDescription.xml > fmi_attributes_20_tmp.xml"); getErrorString();
system("sed -n \"/<ModelVariables>/,/<\\/ModelVariables>/p\" fmi_attributes_20_tmp.xml > fmi_attributes_20.xml"); getErrorString();
readFile("fmi_attributes_20.xml"); getErrorString();

system("sed -n \"/<ModelStructure>/,/<\\/ModelStructure>/p\" fmi_attributes_20_tmp.xml > fmi_attributes_20.xml"); getErrorString();
readFile("fmi_attributes_20.xml"); getErrorString();

// Result:
// true
// ""
// "fmi_attributes_20.fmu"
// ""
// 0
// ""
// 0
// ""
// " <ModelVariables>
// <!-- Index of variable = \"1\" -->
// <ScalarVariable
// name=\"previous(Ts)\"
// valueReference=\"0\"
// variability=\"discrete\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"2\" -->
// <ScalarVariable
// name=\"previous(x)\"
// valueReference=\"1\"
// variability=\"discrete\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"3\" -->
// <ScalarVariable
// name=\"Ts\"
// valueReference=\"2\"
// variability=\"discrete\"
// initial=\"exact\">
// <Real start=\"0.0\"/>
// </ScalarVariable>
// <!-- Index of variable = \"4\" -->
// <ScalarVariable
// name=\"a\"
// valueReference=\"3\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"5\" -->
// <ScalarVariable
// name=\"u\"
// valueReference=\"4\"
// variability=\"discrete\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"6\" -->
// <ScalarVariable
// name=\"x\"
// valueReference=\"5\"
// variability=\"discrete\"
// initial=\"exact\">
// <Real start=\"0.0\"/>
// </ScalarVariable>
// <!-- Index of variable = \"7\" -->
// <ScalarVariable
// name=\"y\"
// valueReference=\"6\"
// causality=\"output\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"8\" -->
// <ScalarVariable
// name=\"T\"
// valueReference=\"7\"
// variability=\"fixed\"
// causality=\"parameter\"
// >
// <Real start=\"0.1\"/>
// </ScalarVariable>
// <!-- Index of variable = \"9\" -->
// <ScalarVariable
// name=\"k\"
// valueReference=\"8\"
// variability=\"fixed\"
// causality=\"parameter\"
// >
// <Real start=\"1.0\"/>
// </ScalarVariable>
// </ModelVariables>
// "
// ""
// 0
// ""
// " <ModelStructure>
// <Outputs>
// <Unknown index=\"7\" dependencies=\"1 2\" dependenciesKind=\"dependent dependent\" />
// </Outputs>
// <InitialUnknowns>
// <Unknown index=\"7\" dependencies=\"1\" dependenciesKind=\"dependent\" />
// </InitialUnknowns>
// </ModelStructure>
// "
// ""
// endResult

0 comments on commit d1144cf

Please sign in to comment.