From d1144cfa8880aad8779e4e48fe62f122e95c38f7 Mon Sep 17 00:00:00 2001 From: arun3688 Date: Tue, 3 Oct 2023 00:18:50 +0200 Subject: [PATCH] do not export in for FMU's (#11288) * do not export in for FMU's * add test * always use getVariableFMIIndex() in modeldescription.xml * expected output --- OMCompiler/Compiler/SimCode/SimCodeUtil.mo | 21 +-- .../Compiler/Template/CodegenFMUCommon.tpl | 2 +- .../modelExchange/2.0/testArrayEquations.mos | 16 +-- .../fmi/ModelExchange/2.0/Makefile | 1 + .../ModelExchange/2.0/fmi_attributes_20.mos | 133 ++++++++++++++++++ 5 files changed, 156 insertions(+), 17 deletions(-) create mode 100644 testsuite/openmodelica/fmi/ModelExchange/2.0/fmi_attributes_20.mos diff --git a/OMCompiler/Compiler/SimCode/SimCodeUtil.mo b/OMCompiler/Compiler/SimCode/SimCodeUtil.mo index 979c54dc906..a44eaab84ff 100644 --- a/OMCompiler/Compiler/SimCode/SimCodeUtil.mo +++ b/OMCompiler/Compiler/SimCode/SimCodeUtil.mo @@ -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(); @@ -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; diff --git a/OMCompiler/Compiler/Template/CodegenFMUCommon.tpl b/OMCompiler/Compiler/Template/CodegenFMUCommon.tpl index 69674acc8c6..353c088a2c9 100644 --- a/OMCompiler/Compiler/Template/CodegenFMUCommon.tpl +++ b/OMCompiler/Compiler/Template/CodegenFMUCommon.tpl @@ -316,7 +316,7 @@ case SOME(fmistruct as FMIMODELSTRUCTURE(__)) then <%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)%> >> diff --git a/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/testArrayEquations.mos b/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/testArrayEquations.mos index 6e1e0afbb10..e22836231ac 100644 --- a/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/testArrayEquations.mos +++ b/testsuite/openmodelica/cppruntime/fmu/modelExchange/2.0/testArrayEquations.mos @@ -945,12 +945,16 @@ readFile("modelDescription.tmp.xml"); // // // -// -// -// -// +// +// +// +// // // +// +// +// +// // // // @@ -987,10 +991,6 @@ readFile("modelDescription.tmp.xml"); // // // -// -// -// -// // // // diff --git a/testsuite/openmodelica/fmi/ModelExchange/2.0/Makefile b/testsuite/openmodelica/fmi/ModelExchange/2.0/Makefile index e479c4a5d0f..85dee377d62 100644 --- a/testsuite/openmodelica/fmi/ModelExchange/2.0/Makefile +++ b/testsuite/openmodelica/fmi/ModelExchange/2.0/Makefile @@ -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 \ diff --git a/testsuite/openmodelica/fmi/ModelExchange/2.0/fmi_attributes_20.mos b/testsuite/openmodelica/fmi/ModelExchange/2.0/fmi_attributes_20.mos new file mode 100644 index 00000000000..cf12be10b1f --- /dev/null +++ b/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>/p\" fmi_attributes_20_tmp.xml > fmi_attributes_20.xml"); getErrorString(); +readFile("fmi_attributes_20.xml"); getErrorString(); + +system("sed -n \"//,/<\\/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 +// "" +// " +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// +// " +// "" +// 0 +// "" +// " +// +// +// +// +// +// +// +// " +// "" +// endResult