Skip to content

Commit

Permalink
add new FMI config flag to filter modelDescription vars (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
arun3688 committed May 19, 2020
1 parent 95a4210 commit dffda8e
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 39 deletions.
10 changes: 10 additions & 0 deletions OMCompiler/Compiler/FrontEnd/ComponentReference.mo
Expand Up @@ -2101,6 +2101,16 @@ algorithm
outCref := makeCrefQual(DAE.preNamePrefix, DAE.T_UNKNOWN_DEFAULT, {}, inCref);
end crefPrefixPre;

public function getConcealedCref "public function getConcealedCref
Replaces a cref to concealed so a => $concealed1"
output DAE.ComponentRef outCref;
protected
String ident;
algorithm
ident := "$concealed" + intString(System.tmpTick()+1);
outCref := makeCrefIdent(ident, DAE.T_UNKNOWN_DEFAULT, {});
end getConcealedCref;

public function crefPrefixPrevious "public function crefPrefixPrevious
Appends $CLKPRE to a cref, so a => $CLKPRE.a"
input DAE.ComponentRef inCref;
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/SimCode/ReduceDAE.mo
Expand Up @@ -2191,14 +2191,14 @@ algorithm
//create simVar for label_1

simVar_1 = SimCodeVar.SIMVAR(DAE.CREF_IDENT(name1,DAE.T_REAL_DEFAULT,{}),BackendDAE.PARAM(),"","","",p,NONE(),NONE(),SOME(DAE.RCONST(1.0)),NONE(),
true,DAE.T_REAL_DEFAULT,false,NONE(),SimCodeVar.NOALIAS(),DAE.emptyElementSource,SOME(SimCodeVar.LOCAL()),NONE(),NONE(),{},false,false,false,NONE(),NONE(),NONE(),NONE(),false);
true,DAE.T_REAL_DEFAULT,false,NONE(),SimCodeVar.NOALIAS(),DAE.emptyElementSource,SOME(SimCodeVar.LOCAL()),NONE(),NONE(),{},false,false,false,NONE(),NONE(),NONE(),NONE(),NONE());
param=listReverse(param);
//add simVar_1 to parameter list
param_1=simVar_1::param;
p=p+1;
//create simVar_2 to parameter list
simVar_2 = SimCodeVar.SIMVAR(DAE.CREF_IDENT(name2,DAE.T_REAL_DEFAULT,{}),BackendDAE.PARAM(),"","","",p,NONE(),NONE(),SOME(DAE.RCONST(0.0)),NONE(),
true,DAE.T_REAL_DEFAULT,false,NONE(),SimCodeVar.NOALIAS(),DAE.emptyElementSource,SOME(SimCodeVar.LOCAL()),NONE(),NONE(),{},false,false,false,NONE(),NONE(),NONE(),NONE(),false);
true,DAE.T_REAL_DEFAULT,false,NONE(),SimCodeVar.NOALIAS(),DAE.emptyElementSource,SOME(SimCodeVar.LOCAL()),NONE(),NONE(),{},false,false,false,NONE(),NONE(),NONE(),NONE(),NONE());
//add simVar_2 to parameter list
param_2=simVar_2::param_1;
param_2=listReverse(param_2);
Expand Down
76 changes: 49 additions & 27 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion OMCompiler/Compiler/SimCode/SimCodeVar.mo
Expand Up @@ -105,7 +105,7 @@ public uniontype SimVar "Information about a variable in a Modelica model."
Option<String> matrixName "if the varibale is a jacobian var, this is the corresponding matrix";
Option<Variability> variability "FMI-2.0 variabilty attribute";
Option<Initial> initial_ "FMI-2.0 initial attribute";
Boolean exportVar "variables will only be exported to the modelDescription.xml if this attribute is true";
Option<DAE.ComponentRef> exportVar "variables will only be exported to the modelDescription.xml if this attribute is SOME(cref) and this cref is only used in ModelDescription.xml for FMI-2.0 export";
end SIMVAR;
end SimVar;

Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/Template/CodegenFMUCommon.tpl
Expand Up @@ -156,7 +156,7 @@ case SIMVAR(__) then
else if stringEq(crefStr(name),"der($dummy)") then
<<>>
else if isFMIVersion20(FMUVersion) then
if exportVar then
if isSome(exportVar) then
<<
<!-- Index of variable = "<%getVariableFMIIndex(simVar)%>" -->
<ScalarVariable
Expand Down Expand Up @@ -484,7 +484,7 @@ match simVar
let caus = getCausality2(causality)
let initial = getInitialType2(initial_)
<<
name="<%System.stringReplace(crefStrNoUnderscore(name),"$", "_D_")%>"
name="<%System.stringReplace(crefStrNoUnderscore(Util.getOption(exportVar)),"$", "_D_")%>"
valueReference="<%valueReference%>"
<%description%>
<%if boolNot(stringEq(variability_, "")) then 'variability="'+variability_+'"' %>
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/Template/SimCodeTV.mo
Expand Up @@ -270,7 +270,7 @@ package SimCodeVar
Option<String> matrixName;
Option<Variability> variability "FMI-2.0 variabilty attribute";
Option<Initial> initial_ "FMI-2.0 initial attribute";
Boolean exportVar "true for internal variables that are introduced in the symbolic transformation process";
Option<DAE.ComponentRef> exportVar "variables will only be exported to the modelDescription.xml if this attribute is SOME(cref)";
end SIMVAR;
end SimVar;

Expand Down
13 changes: 11 additions & 2 deletions OMCompiler/Compiler/Util/Flags.mo
Expand Up @@ -160,6 +160,13 @@ public constant Integer METAMODELICA = 2;
public constant Integer PARMODELICA = 3;
public constant Integer OPTIMICA = 4;
public constant Integer PDEMODELICA = 5;

// FMI-ModelDescription-ENUM-FLAGS
public constant Integer FMI_NONE = 1;
public constant Integer FMI_INTERNAL = 2;
public constant Integer FMI_PROTECTED = 3;
public constant Integer FMI_BLACKBOX = 4;

constant Gettext.TranslatableContent collapseArrayExpressionsText = Gettext.gettext("Simplifies {x[1],x[2],x[3]} → x for arrays of whole variable references (simplifies code generation).");

// DEBUG FLAGS
Expand Down Expand Up @@ -553,8 +560,6 @@ constant DebugFlag NF_DUMP_FLAT = DEBUG_FLAG(192, "nfDumpFlat", false,
Gettext.gettext("Dumps the flat model structure before generating the DAE."));
constant DebugFlag DUMP_FORCE_FMI_ATTRIBUTES = DEBUG_FLAG(193, "force-fmi-attributes", false,
Gettext.gettext("Force to export all fmi attributes to the modelDescription.xml, including those which have default values"));
constant DebugFlag DUMP_FORCE_FMI_INTERNAL_VARIABLES = DEBUG_FLAG(194, "force-fmi-internal-variables", false,
Gettext.gettext("Force to export all internal variables (eg: $CSE) to the modelDescription.xml"));

public
// CONFIGURATION FLAGS
Expand Down Expand Up @@ -1348,6 +1353,10 @@ constant ConfigFlag FMI_VERSION = CONFIG_FLAG(140,
constant ConfigFlag FLAT_MODELICA = CONFIG_FLAG(141, "flatModelica",
SOME("f"), EXTERNAL(), BOOL_FLAG(false), NONE(),
Gettext.gettext("Outputs experimental flat Modelica."));
constant ConfigFlag FMI_FILTER = CONFIG_FLAG(142, "fmiFilter", NONE(), EXTERNAL(),
ENUM_FLAG(FMI_INTERNAL, {("none", FMI_NONE), ("internal", FMI_INTERNAL), ("protected", FMI_PROTECTED), ("blackBox", FMI_BLACKBOX)}),
SOME(STRING_OPTION({"none", "internal", "protected", "blackBox"})),
Gettext.gettext("Filters the FMI-ModelDescription Vars in the ModelDescription.xml"));

function getFlags
"Loads the flags with getGlobalRoot. Assumes flags have been loaded."
Expand Down
7 changes: 3 additions & 4 deletions OMCompiler/Compiler/Util/FlagsUtil.mo
Expand Up @@ -249,8 +249,7 @@ constant list<Flags.DebugFlag> allDebugFlags = {
Flags.DUMP_ASSC,
Flags.SPLIT_CONSTANT_PARTS_SYMJAC,
Flags.NF_DUMP_FLAT,
Flags.DUMP_FORCE_FMI_ATTRIBUTES,
Flags.DUMP_FORCE_FMI_INTERNAL_VARIABLES
Flags.DUMP_FORCE_FMI_ATTRIBUTES
};

protected
Expand Down Expand Up @@ -398,8 +397,8 @@ constant list<Flags.ConfigFlag> allConfigFlags = {
Flags.ZEROMQ_SERVER_ID,
Flags.ZEROMQ_CLIENT_ID,
Flags.FMI_VERSION,
Flags.FLAT_MODELICA

Flags.FLAT_MODELICA,
Flags.FMI_FILTER
};

public function new
Expand Down
2 changes: 2 additions & 0 deletions testsuite/openmodelica/fmi/ModelExchange/2.0/Makefile
Expand Up @@ -14,6 +14,8 @@ fmi_attributes_07.mos \
fmi_attributes_08.mos \
fmi_attributes_09.mos \
fmi_attributes_10.mos \
fmi_attributes_11.mos \
fmi_attributes_12.mos \
FMIExercise.mos \
FMUResourceTest.mos \
HelloFMIWorld.mos \
Expand Down
97 changes: 97 additions & 0 deletions testsuite/openmodelica/fmi/ModelExchange/2.0/fmi_attributes_11.mos
@@ -0,0 +1,97 @@
// name: fmi_attributes_11.mos
// keywords: FMI 2.0 export
// status: correct
// teardown_command: rm -rf fmi_attributes_11.fmu fmi_attributes_11.log fmi_attributes_11.xml fmi_attributes_11_tmp.xml fmi_attributes_11.fmutmp/

loadString("
model fmi_attributes_11
input Real u;
Real x;
output Real y;
Real a, b;
parameter Real z=30;
initial equation
x = 0.0;
equation
a = 2.0*u;
b = 0.5*a;
der(x) = b+z;
y = x;
end fmi_attributes_11;
"); getErrorString();

setCommandLineOptions("--fmiFilter=blackBox"); getErrorString();
translateModelFMU(fmi_attributes_11); getErrorString();

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

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


// Result:
// true
// ""
// true
// ""
// "fmi_attributes_11.fmu"
// ""
// 0
// ""
// 0
// ""
// " <ModelVariables>
// <!-- Index of variable = \"1\" -->
// <ScalarVariable
// name=\"_D_concealed1\"
// valueReference=\"0\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"2\" -->
// <ScalarVariable
// name=\"_D_concealed2\"
// valueReference=\"1\"
// >
// <Real derivative=\"1\"/>
// </ScalarVariable>
// <!-- Index of variable = \"3\" -->
// <ScalarVariable
// name=\"u\"
// valueReference=\"4\"
// causality=\"input\"
// >
// <Real start=\"0.0\"/>
// </ScalarVariable>
// <!-- Index of variable = \"4\" -->
// <ScalarVariable
// name=\"y\"
// valueReference=\"5\"
// causality=\"output\"
// >
// <Real/>
// </ScalarVariable>
// </ModelVariables>
// "
// ""
// 0
// ""
// " <ModelStructure>
// <Outputs>
// <Unknown index=\"4\" dependencies=\"1\" dependenciesKind=\"dependent\" />
// </Outputs>
// <Derivatives>
// <Unknown index=\"2\" dependencies=\"3\" dependenciesKind=\"dependent\" />
// </Derivatives>
// <InitialUnknowns>
// <Unknown index=\"1\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"2\" dependencies=\"3\" dependenciesKind=\"dependent\" />
// <Unknown index=\"4\" dependencies=\"\" dependenciesKind=\"\" />
// </InitialUnknowns>
// </ModelStructure>
// "
// ""
// endResult
124 changes: 124 additions & 0 deletions testsuite/openmodelica/fmi/ModelExchange/2.0/fmi_attributes_12.mos
@@ -0,0 +1,124 @@
// name: fmi_attributes_12.mos
// keywords: FMI 2.0 export
// status: correct
// teardown_command: rm -rf fmi_attributes_12.fmu fmi_attributes_12.log fmi_attributes_12.xml fmi_attributes_12_tmp.xml fmi_attributes_12.fmutmp/

loadString("
model fmi_attributes_12
input Real u;
Real x;
output Real y;
Real a, b;
parameter Real z=30;
protected
Real m, n;
initial equation
x = 0.0;
equation
a = 2.0*u;
b = 0.5*a;
der(x) = b+z;
y = x;
m = z;
n = a;
end fmi_attributes_12;
"); getErrorString();

setCommandLineOptions("--fmiFilter=protected"); getErrorString();
translateModelFMU(fmi_attributes_12); getErrorString();

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

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


// Result:
// true
// ""
// true
// ""
// "fmi_attributes_12.fmu"
// ""
// 0
// ""
// 0
// ""
// " <ModelVariables>
// <!-- Index of variable = \"1\" -->
// <ScalarVariable
// name=\"x\"
// valueReference=\"0\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"2\" -->
// <ScalarVariable
// name=\"der(x)\"
// valueReference=\"1\"
// >
// <Real derivative=\"1\"/>
// </ScalarVariable>
// <!-- Index of variable = \"3\" -->
// <ScalarVariable
// name=\"a\"
// valueReference=\"2\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"4\" -->
// <ScalarVariable
// name=\"b\"
// valueReference=\"3\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"5\" -->
// <ScalarVariable
// name=\"u\"
// valueReference=\"4\"
// causality=\"input\"
// >
// <Real start=\"0.0\"/>
// </ScalarVariable>
// <!-- Index of variable = \"6\" -->
// <ScalarVariable
// name=\"y\"
// valueReference=\"5\"
// causality=\"output\"
// >
// <Real/>
// </ScalarVariable>
// <!-- Index of variable = \"7\" -->
// <ScalarVariable
// name=\"z\"
// valueReference=\"7\"
// variability=\"fixed\"
// causality=\"parameter\"
// >
// <Real start=\"30.0\"/>
// </ScalarVariable>
// </ModelVariables>
// "
// ""
// 0
// ""
// " <ModelStructure>
// <Outputs>
// <Unknown index=\"6\" dependencies=\"1\" dependenciesKind=\"dependent\" />
// </Outputs>
// <Derivatives>
// <Unknown index=\"2\" dependencies=\"5\" dependenciesKind=\"dependent\" />
// </Derivatives>
// <InitialUnknowns>
// <Unknown index=\"1\" dependencies=\"\" dependenciesKind=\"\" />
// <Unknown index=\"2\" dependencies=\"5 7\" dependenciesKind=\"dependent dependent\" />
// <Unknown index=\"6\" dependencies=\"\" dependenciesKind=\"\" />
// </InitialUnknowns>
// </ModelStructure>
// "
// ""
// endResult

0 comments on commit dffda8e

Please sign in to comment.