Skip to content

Commit

Permalink
Undefined variable units when using FMUs (ticket:6002, #975)
Browse files Browse the repository at this point in the history
* Unknown units will be exported to the UnitDefinitions but without a BaseUnit representation
* Define unit bar
  • Loading branch information
lochel committed Jun 18, 2020
1 parent e374ec0 commit 69e586a
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 93 deletions.
11 changes: 6 additions & 5 deletions OMCompiler/Compiler/BackEnd/Unit.mo
Expand Up @@ -102,21 +102,22 @@ public constant list<tuple<String, Unit>> LU_COMPLEXUNITS = {
//("var", UNIT(1e3, 0, 0, 2,-3, 0, 0, 1)), //Var=Watt
("Hz", UNIT(1e0, 0, 0, 0,-1, 0, 0, 0)), //Hertz
("Ohm", UNIT(1e3, 0, 0, 2,-3,-2, 0, 1)), //Ohm
("F", UNIT(1e-3, 0, 0,-2, 4, 2, 0,-1)), //Farad
("F", UNIT(1e-3, 0, 0,-2, 4, 2, 0,-1)), //Farad
("H", UNIT(1e3, 0, 0, 2,-2,-2, 0, 1)), //Henry
("C", UNIT(1e0, 0, 0, 0, 1, 1, 0, 0)), //Coulomb
("T", UNIT(1e3, 0, 0, 0,-2,-1, 0, 1)), //Tesla
("S", UNIT(1e-3, 0, 0,-2, 3, 2, 0,-1)), //Siemens
("S", UNIT(1e-3, 0, 0,-2, 3, 2, 0,-1)), //Siemens
("Wb", UNIT(1e3, 0, 0, 2,-2,-1, 0, 1)), //Weber
//("lm", UNIT(1e0, 0, 1, 0, 0, 0, 0, 0)), //Lumen=Candela
//("lx", UNIT(1e0, 0, 1,-2, 0, 0, 0, 0)), //Lux=lm/m^2
("N", UNIT(1e3, 0, 0, 1,-2, 0, 0, 1)), //Newton
("Pa", UNIT(1e3, 0, 0,-1,-2, 0, 0, 1)), //Pascal; displayUnit ="bar"
("bar", UNIT(1e8, 0, 0,-1,-2, 0, 0, 1)), //bar
("J", UNIT(1e3, 0, 0, 2,-2, 0, 0, 1)), //Joule=N*m
("min", UNIT(6e1, 0, 0, 0, 1, 0, 0, 0)), //Minute
("h", UNIT(3.6e3, 0, 0, 0, 1, 0, 0, 0)), //Stunde
("d", UNIT(8.64e4, 0, 0, 0, 1, 0, 0, 0)), //Tag
("l", UNIT(1e-3, 0, 0, 3, 0, 0, 0, 0)), //Liter
("h", UNIT(3.6e3, 0, 0, 0, 1, 0, 0, 0)), //Stunde
("d", UNIT(8.64e4, 0, 0, 0, 1, 0, 0, 0)), //Tag
("l", UNIT(1e-3, 0, 0, 3, 0, 0, 0, 0)), //Liter
("kg", UNIT(1e3, 0, 0, 0, 0, 0, 0, 1)), //Kilogramm
//("Bq", UNIT(1e0, 0, 0, 0,-1, 0, 0, 0)), //Becquerel = Hertz
//("Gy", UNIT(1e0, 0, 0, 2,-2, 0, 0, 1)), //Gray
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/SimCode/SimCode.mo
Expand Up @@ -228,6 +228,9 @@ uniontype BaseUnit
Real factor "prefix";
Real offset "offset";
end BASEUNIT;

record NOBASEUNIT "no baseunit definition available"
end NOBASEUNIT;
end BaseUnit;

uniontype ModelInfo "Container for metadata about a Modelica model."
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/Compiler/SimCode/SimCodeUtil.mo
Expand Up @@ -9150,6 +9150,7 @@ algorithm
unitDefinitions := SimCode.UNITDEFINITION(var.unit, transformUnitToBaseUnit(unit)) :: unitDefinitions;
else
// catch the units which are not calculated
unitDefinitions := SimCode.UNITDEFINITION(var.unit, SimCode.NOBASEUNIT()) :: unitDefinitions;
end try;
end if;
end if;
Expand Down
29 changes: 15 additions & 14 deletions OMCompiler/Compiler/Template/CodegenFMUCommon.tpl
Expand Up @@ -682,18 +682,18 @@ template UnitDefinitionsHelper1(UnitDefinition unitDefinition)
"helper function to generates code for UnitDefinition for FMU target."
::=
match unitDefinition
case UNITDEFINITION(name = name, baseUnit = baseUnit) then
case UNITDEFINITION(name=name, baseUnit=baseUnit) then
<<
<Unit <%unitDefinitionAttribute(name)%>>
<BaseUnit <%baseUnitAttributes(baseUnit)%>/>
<%baseUnitAttributes(baseUnit)%>
</Unit>
>>
end UnitDefinitionsHelper1;

template unitDefinitionAttribute(String unitName)
"Generates code for UnitDefinition Attribute for FMU target."
::=
let unitString = if unitName then 'name ="<%unitName%>"'
let unitString = if unitName then 'name="<%unitName%>"'
<<
<%unitString%>
>>
Expand All @@ -703,19 +703,20 @@ template baseUnitAttributes(BaseUnit baseUnit)
"Generates code for BaseUnit for FMU target."
::=
match baseUnit
case (BASEUNIT(mol = mol, cd = cd, m = m, s = s, A = A, K = K, kg = kg, factor = factor, offset = offset)) then
let mol_Value = if not intEq(mol, 0) then ' mol="<% mol %>"' else ""
let cd_Value = if not intEq(cd, 0) then ' cd="<% cd %>"' else ""
let m_Value = if not intEq(m, 0) then ' m="<% m %>"' else ""
let s_Value = if not intEq(s, 0) then ' s="<% s %>"' else ""
let A_Value = if not intEq(A, 0) then ' A="<% A %>"' else ""
let K_Value = if not intEq(K, 0) then ' K="<% K %>"' else ""
let kg_Value = if not intEq(kg, 0) then ' kg="<% kg %>"' else ""
let factor_Value = if not realAlmostEq(factor, 1.0, 1e-6) then ' factor="<% factor %>"' else ""
let offset_Value = if not realAlmostEq(offset, 0.0, 1e-6) then ' offset="<% offset %>"' else ""
case (BASEUNIT(mol=mol, cd=cd, m=m, s=s, A=A, K=K, kg=kg, factor=factor, offset=offset)) then
let mol_Value = if not intEq(mol, 0) then 'mol="<% mol %>" ' else ""
let cd_Value = if not intEq(cd, 0) then 'cd="<% cd %>" ' else ""
let m_Value = if not intEq(m, 0) then 'm="<% m %>" ' else ""
let s_Value = if not intEq(s, 0) then 's="<% s %>" ' else ""
let A_Value = if not intEq(A, 0) then 'A="<% A %>" ' else ""
let K_Value = if not intEq(K, 0) then 'K="<% K %>" ' else ""
let kg_Value = if not intEq(kg, 0) then 'kg="<% kg %>" ' else ""
let factor_Value = if not realAlmostEq(factor, 1.0, 1e-6) then 'factor="<% factor %>" ' else ""
let offset_Value = if not realAlmostEq(offset, 0.0, 1e-6) then 'offset="<% offset %>" ' else ""
<<
<%mol_Value%><%cd_Value%><%m_Value%><%s_Value%><%A_Value%><%K_Value%><%kg_Value%><%factor_Value%><%offset_Value%>
<BaseUnit <%mol_Value%><%cd_Value%><%m_Value%><%s_Value%><%A_Value%><%K_Value%><%kg_Value%><%factor_Value%><%offset_Value%>/>
>>
case (NOBASEUNIT()) then ""
end baseUnitAttributes;

template fmiTypeDefinitions(SimCode simCode, String FMUVersion)
Expand Down
3 changes: 3 additions & 0 deletions OMCompiler/Compiler/Template/SimCodeTV.mo
Expand Up @@ -736,6 +736,9 @@ package SimCode
Real factor "prefix";
Real offset "offset";
end BASEUNIT;

record NOBASEUNIT "no baseunit definition available"
end NOBASEUNIT;
end BaseUnit;

uniontype VarInfo
Expand Down
Expand Up @@ -67,41 +67,41 @@ getErrorString();
// modelIdentifier=\"CSTRModel\" providesDirectionalDerivative=\"true\">
// </ModelExchange>
// <UnitDefinitions>
// <Unit name =\"s\">
// <BaseUnit s=\"1\"/>
// <Unit name=\"s\">
// <BaseUnit s=\"1\" />
// </Unit>
// <Unit name =\"kg/m3\">
// <BaseUnit m=\"-3\" kg=\"1\"/>
// <Unit name=\"kg/m3\">
// <BaseUnit m=\"-3\" kg=\"1\" />
// </Unit>
// <Unit name =\"kg\">
// <BaseUnit kg=\"1\"/>
// <Unit name=\"kg\">
// <BaseUnit kg=\"1\" />
// </Unit>
// <Unit name =\"W/(m2.K)\">
// <BaseUnit s=\"-3\" K=\"-1\" kg=\"1\"/>
// <Unit name=\"W/(m2.K)\">
// <BaseUnit s=\"-3\" K=\"-1\" kg=\"1\" />
// </Unit>
// <Unit name =\"m3\">
// <BaseUnit m=\"3\"/>
// <Unit name=\"m3\">
// <BaseUnit m=\"3\" />
// </Unit>
// <Unit name =\"J/mol\">
// <BaseUnit mol=\"-1\" m=\"2\" s=\"-2\" kg=\"1\"/>
// <Unit name=\"J/mol\">
// <BaseUnit mol=\"-1\" m=\"2\" s=\"-2\" kg=\"1\" />
// </Unit>
// <Unit name =\"J/(kg.K)\">
// <BaseUnit m=\"2\" s=\"-2\" K=\"-1\"/>
// <Unit name=\"J/(kg.K)\">
// <BaseUnit m=\"2\" s=\"-2\" K=\"-1\" />
// </Unit>
// <Unit name =\"m2\">
// <BaseUnit m=\"2\"/>
// <Unit name=\"m2\">
// <BaseUnit m=\"2\" />
// </Unit>
// <Unit name =\"1/h\">
// <BaseUnit s=\"-1\" factor=\"0.0002777777777777778\"/>
// <Unit name=\"1/h\">
// <BaseUnit s=\"-1\" factor=\"0.0002777777777777778\" />
// </Unit>
// <Unit name =\"kJ/h\">
// <BaseUnit m=\"2\" s=\"-3\" kg=\"1\" factor=\"0.2777777777777779\"/>
// <Unit name=\"kJ/h\">
// <BaseUnit m=\"2\" s=\"-3\" kg=\"1\" factor=\"0.2777777777777779\" />
// </Unit>
// <Unit name =\"mol/l\">
// <BaseUnit mol=\"1\" m=\"-3\" factor=\"1000.0\"/>
// <Unit name=\"mol/l\">
// <BaseUnit mol=\"1\" m=\"-3\" factor=\"1000.0\" />
// </Unit>
// <Unit name =\"degC\">
// <BaseUnit K=\"1\"/>
// <Unit name=\"degC\">
// <BaseUnit K=\"1\" />
// </Unit>
// </UnitDefinitions>
// <TypeDefinitions>
Expand Down
Expand Up @@ -28,7 +28,7 @@ readFile("modelDescription.tmp.xml");
importFMU("DrumBoilerModel.fmu"); getErrorString();
loadFile("DrumBoilerModel_me_FMU.mo"); getErrorString();

setCommandLineOptions("+simCodeTarget=C"); getErrorString();
setCommandLineOptions("--simCodeTarget=C"); getErrorString();
simulate(DrumBoilerModel_me_FMU, stopTime=100.0, simflags="-override=q_F=10,Y_Valve=1"); getErrorString();

// inputs
Expand Down Expand Up @@ -67,74 +67,77 @@ val(evaporator_qm_S, 100);
// modelIdentifier=\"DrumBoilerModel\" providesDirectionalDerivative=\"true\">
// </ModelExchange>
// <UnitDefinitions>
// <Unit name =\"m/s2\">
// <BaseUnit m=\"1\" s=\"-2\"/>
// <Unit name=\"m/s2\">
// <BaseUnit m=\"1\" s=\"-2\" />
// </Unit>
// <Unit name =\"1/K\">
// <BaseUnit K=\"-1\"/>
// <Unit name=\"1/K\">
// <BaseUnit K=\"-1\" />
// </Unit>
// <Unit name =\"s\">
// <BaseUnit s=\"1\"/>
// <Unit name=\"s\">
// <BaseUnit s=\"1\" />
// </Unit>
// <Unit name =\"kg/(s.Pa)\">
// <BaseUnit m=\"1\" s=\"1\"/>
// <Unit name=\"kg/(s.Pa)\">
// <BaseUnit m=\"1\" s=\"1\" />
// </Unit>
// <Unit name =\"1\">
// <Unit name=\"1\">
// <BaseUnit />
// </Unit>
// <Unit name =\"N/mm2\">
// <BaseUnit m=\"-1\" s=\"-2\" kg=\"1\" factor=\"1000000.0\"/>
// <Unit name=\"N/mm2\">
// <BaseUnit m=\"-1\" s=\"-2\" kg=\"1\" factor=\"1000000.0\" />
// </Unit>
// <Unit name =\"MW\">
// <BaseUnit m=\"2\" s=\"-3\" kg=\"1\" factor=\"1000000.0\"/>
// <Unit name=\"MW\">
// <BaseUnit m=\"2\" s=\"-3\" kg=\"1\" factor=\"1000000.0\" />
// </Unit>
// <Unit name =\"kg/kg\">
// <Unit name=\"bar\">
// <BaseUnit m=\"-1\" s=\"-2\" kg=\"1\" factor=\"100000.0\" />
// </Unit>
// <Unit name=\"kg/kg\">
// <BaseUnit />
// </Unit>
// <Unit name =\"degC\">
// <BaseUnit K=\"1\"/>
// <Unit name=\"degC\">
// <BaseUnit K=\"1\" />
// </Unit>
// <Unit name =\"J/(kg.K)\">
// <BaseUnit m=\"2\" s=\"-2\" K=\"-1\"/>
// <Unit name=\"J/(kg.K)\">
// <BaseUnit m=\"2\" s=\"-2\" K=\"-1\" />
// </Unit>
// <Unit name =\"kg/mol\">
// <BaseUnit mol=\"-1\" kg=\"1\"/>
// <Unit name=\"kg/mol\">
// <BaseUnit mol=\"-1\" kg=\"1\" />
// </Unit>
// <Unit name =\"kg/s\">
// <BaseUnit s=\"-1\" kg=\"1\"/>
// <Unit name=\"kg/s\">
// <BaseUnit s=\"-1\" kg=\"1\" />
// </Unit>
// <Unit name =\"W\">
// <BaseUnit m=\"2\" s=\"-3\" kg=\"1\"/>
// <Unit name=\"W\">
// <BaseUnit m=\"2\" s=\"-3\" kg=\"1\" />
// </Unit>
// <Unit name =\"kg\">
// <BaseUnit kg=\"1\"/>
// <Unit name=\"kg\">
// <BaseUnit kg=\"1\" />
// </Unit>
// <Unit name =\"J/kg\">
// <BaseUnit m=\"2\" s=\"-2\"/>
// <Unit name=\"J/kg\">
// <BaseUnit m=\"2\" s=\"-2\" />
// </Unit>
// <Unit name =\"J\">
// <BaseUnit m=\"2\" s=\"-2\" kg=\"1\"/>
// <Unit name=\"J\">
// <BaseUnit m=\"2\" s=\"-2\" kg=\"1\" />
// </Unit>
// <Unit name =\"kg/m3\">
// <BaseUnit m=\"-3\" kg=\"1\"/>
// <Unit name=\"kg/m3\">
// <BaseUnit m=\"-3\" kg=\"1\" />
// </Unit>
// <Unit name =\"m3/s\">
// <BaseUnit m=\"3\" s=\"-1\"/>
// <Unit name=\"m3/s\">
// <BaseUnit m=\"3\" s=\"-1\" />
// </Unit>
// <Unit name =\"K\">
// <BaseUnit K=\"1\"/>
// <Unit name=\"K\">
// <BaseUnit K=\"1\" />
// </Unit>
// <Unit name =\"km-1.s-3.g\">
// <BaseUnit m=\"-1\" s=\"-3\" kg=\"1\" factor=\"1e-006\"/>
// <Unit name=\"km-1.s-3.g\">
// <BaseUnit m=\"-1\" s=\"-3\" kg=\"1\" factor=\"1e-006\" />
// </Unit>
// <Unit name =\"m3.s-1\">
// <BaseUnit m=\"3\" s=\"-1\"/>
// <Unit name=\"m3.s-1\">
// <BaseUnit m=\"3\" s=\"-1\" />
// </Unit>
// <Unit name =\"Pa\">
// <BaseUnit m=\"-1\" s=\"-2\" kg=\"1\"/>
// <Unit name=\"Pa\">
// <BaseUnit m=\"-1\" s=\"-2\" kg=\"1\" />
// </Unit>
// <Unit name =\"m3\">
// <BaseUnit m=\"3\"/>
// <Unit name=\"m3\">
// <BaseUnit m=\"3\" />
// </Unit>
// </UnitDefinitions>
// <TypeDefinitions>
Expand Down
29 changes: 26 additions & 3 deletions testsuite/openmodelica/fmi/ModelExchange/2.0/fmi_attributes_15.mos
Expand Up @@ -6,8 +6,12 @@
loadString("
model fmi_attributes_15
Real x(unit = \"kg2\");
Real y(unit = \"bar\");
Real z(unit = \"xyz\");
equation
x=10;
y=20;
z=30;
end fmi_attributes_15;
"); getErrorString();

Expand Down Expand Up @@ -103,8 +107,13 @@ readFile("fmi_attributes_15_tmp.xml")
// </SourceFiles>
// </ModelExchange>
// <UnitDefinitions>
// <Unit name =\"kg2\">
// <BaseUnit kg=\"2\"/>
// <Unit name=\"xyz\">
// </Unit>
// <Unit name=\"bar\">
// <BaseUnit m=\"-1\" s=\"-2\" kg=\"1\" factor=\"100000.0\" />
// </Unit>
// <Unit name=\"kg2\">
// <BaseUnit kg=\"2\" />
// </Unit>
// </UnitDefinitions>
// <LogCategories>
Expand All @@ -120,7 +129,7 @@ readFile("fmi_attributes_15_tmp.xml")
// <Category name=\"logAll\" />
// <Category name=\"logFmi2Call\" />
// </LogCategories>
// <DefaultExperiment startTime=\"0.0\" stopTime=\"1.0\" tolerance=\"1e-006\"/>
// <DefaultExperiment startTime=\"0.0\" stopTime=\"1.0\" tolerance=\"1e-06\"/>
// <ModelVariables>
// <!-- Index of variable = \"1\" -->
// <ScalarVariable
Expand All @@ -129,6 +138,20 @@ readFile("fmi_attributes_15_tmp.xml")
// initial=\"exact\">
// <Real start=\"0.0\" unit=\"kg2\"/>
// </ScalarVariable>
// <!-- Index of variable = \"2\" -->
// <ScalarVariable
// name=\"y\"
// valueReference=\"1\"
// initial=\"exact\">
// <Real start=\"0.0\" unit=\"bar\"/>
// </ScalarVariable>
// <!-- Index of variable = \"3\" -->
// <ScalarVariable
// name=\"z\"
// valueReference=\"2\"
// initial=\"exact\">
// <Real start=\"0.0\" unit=\"xyz\"/>
// </ScalarVariable>
// </ModelVariables>
// <ModelStructure>
// </ModelStructure>
Expand Down
4 changes: 2 additions & 2 deletions testsuite/openmodelica/fmi/ModelExchange/2.0/testBug3049.mos
Expand Up @@ -106,8 +106,8 @@ readFile("modelDescription.tmp.xml");
// </SourceFiles>
// </ModelExchange>
// <UnitDefinitions>
// <Unit name =\"Pa\">
// <BaseUnit m=\"-1\" s=\"-2\" kg=\"1\"/>
// <Unit name=\"Pa\">
// <BaseUnit m=\"-1\" s=\"-2\" kg=\"1\" />
// </Unit>
// </UnitDefinitions>
// <LogCategories>
Expand Down

0 comments on commit 69e586a

Please sign in to comment.