Chapter 19 Unit Expressions

Unless otherwise stated, the syntax and semantics of unit expressions in Modelica (for example, section 4.9.1 or section 18.5.2.2) conform with the international standards International System of Units (SI) by BIPM superseding parts of ISO 31/0-1992 General principles concerning quantities, units and symbols and ISO 1000-1992 SI units and recommendations for the use of their multiples and of certain other units. Unfortunately, these standards do not define a formal syntax for unit expressions. There are recommendations and Modelica exploits them.

Note that this document uses the American spelling meter, whereas the SI specification from BIPM uses the British spelling metre.

Examples for the syntax of unit expressions used in Modelica: "N.m", "kg.m/s2", "kg.m.s-2", "1/rad", "mm/s".

19.1 The Syntax of Unit Expressions

The Modelica unit string syntax allows neither comments nor white-space, and a unit string shall match the unit-expression rule:

unit-expression :

unit-numerator [ "/" unit-denominator ]

unit-numerator :

"1" | unit-factors | "(" unit-expression ")"

unit-denominator :

unit-factor | "(" unit-expression ")"

The unit of measure of a dimension free quantity is denoted by "1". The SI standard does not define any precedence between multiplications and divisions. The SI standard does not allow multiple units to the right of the division-symbol (/) since the result is ambiguous; either the divisor shall be enclosed in parentheses, or negative exponents used instead of division, for example, "J/(kg.K)" may be written as "J.kg-1.K-1".

unit-factors :

unit-factor [ "." unit-factors ]

The SI standard specifies that a multiplication operator symbol is written as space or as a dot. The SI standard requires that this dot is a bit above the base line: ‘·’, which is not part of ASCII. The ISO standard also prefers ‘·’, but Modelica supports the ISO alternative ‘.’, which is an ordinary dot on the base line.

For example, Modelica does not support "Nm" for newton-meter, but requires it to be written as "N.m".

unit-factor :

unit-operand [ unit-exponent ]

unit-exponent :

[ "+" | "-" ] ( UNSIGNED-INTEGER | "(" UNSIGNED-INTEGER "/" UNSIGNED-INTEGER ")" )

The SI standard uses super-script for the exponentation, and does thus not define any operator symbol for exponentiation. A unit-factor consists of a unit-operand possibly suffixed by a possibly signed integer or rational number, which is interpreted as an exponent. There must be no spacing between the unit-operand and a possible unit-exponent. It is recommended to use the simplest representation of exponents, meaning that the explicit + sign should be avoided, that leading zeros should be avoided, that rational exponents are reduced to not have common factors in the numerator and denominator, that rational exponents with denominator 1 should be avoided in favor of plain integer exponents, that the exponent 1 is omitted, and that entire factors with exponent 0 are omitted.

unit-operand :

unit-symbol | unit-prefix unit-symbol

unit-prefix :

"Q" | "R" | "Y" | "Z" | "E" | "P" | "T" | "G" | "M" | "k" | "h" | "da"
| "d" | "c" | "m" | "u" | "n" | "p" | "f" | "a" | "z" | "y" | "r" | "q"

unit-symbol :

unit-char { unit-char }

unit-char :

NON-DIGIT

The units required to be recognized are the basic and derived units of the SI system, as well as some units compatible with the SI system listed below, but tools are allowed to additionally support user-defined unit symbols. The required unit symbols do not make use of Greek letters, but a unit such as Ω is spelled out as "Ohm". Similarly degree is spelled out as "deg", both on its own (for angles) and as part of "degC", "degF" and "degRk" for temperatures (Celsius, Fahrenheit and Rankine).

It is recommended that non-SI units are only used for the displayUnit-attribute in order to reduce impact of unrecognized unit symbols when using another Modelica tool.

The following are the units required to be recognized in addition to the SI system:

  • minute "min" (1 minute = 60 s)

  • hour "h" (1 hour = 3600 s)

  • day "d" (1 day = 86400 s)

  • liter "l" and "L" (1 liter = 1 dm3)

  • electronvolt "eV" (1 electronvolt = 1.602176634e-19 J)

  • degree "deg" (1 degree = π/180 rad)

  • debye "debye" (1 debye = 1e-21 / 299792458 Cm)

The first 7 are listed in the SI standard as non-SI units that are acceptable to use with the SI system.

A unit-operand should first be interpreted as a unit-symbol and only if not successful the second alternative assuming a prefixed operand should be exploited. There must be no spacing between the unit-symbol and a possible unit-prefix. The values of the prefixes are according to the ISO standard. The letter u is used as a symbol for the prefix micro.

[A tool may present "Ohm" as Ω and the prefix "u" as μ. Exponents such as "m2" may be presented as m2. Degrees may be presented as , both for "deg" on its own (for angles) and for temperatures – e.g., "degC" can be presented as C. Note that BIPM have specific recommendations for formatting using these symbols. ]

[Example : The unit expression "m" means meter and not milli (10-3), since prefixes cannot be used in isolation. For millimeter use "mm" and for square meter, m2, write "m2".

The expression "mm2" means (10-3m)2 = 10-6m2. Note that exponentiation includes the prefix.

The unit expression "T" means tesla, but note that the letter T is also the symbol for the prefix tera which has a multiplier value of 1012. ]

19.2 Unitful Literals

The unsigned-number in the grammar allows a unit to be attached to a numeric value. In this case, the unit is given as a Q-IDENT rather than a string, since the use of a string could be ambiguously interpreted as description-string in some contexts.

[Example : An acceleration of 9.8 meters per square second can be expressed as 9.8'm/s2'. ]

When the modified component of a binding equation has a unit-attribute, a binding equation consisting of a unitful literal or a negated unitful literal is implicitly converted to the component’s unit.

[A unit system is coherent when every derived unit can be built from the base units purely by multiplying and dividing powers of them, with no numerical factor other than 1 anywhere in the construction. A coherent unit system ensures that the equations between the numerical values of quantities take exactly the same form as the equations between the quantities themselves. In the context of a coherent unit system, coherent unit refers to a unit belonging to the unit system, while non-coherent unit refers to any other unit.

Within the SI unit system, the set of coherent SI units form a coherent unit system. Prefixed SI units such as the centimeter are not coherent, since they are derived units where the scaling relative to the base units is not 1. The exception is mass where the SI base unit is kilogram, meaning that the kilogram (prefixed) is coherent, while the gram (not prefixed) is not. Other examples of non-coherent units include the minute (accepted for use with SI) and the inch (not accepted for use with SI).

The predominant use of coherent SI units in unit-attributes in libraries (as this is a good way to avoid unit inconsistency without tool assistance to verify unit consistency) should be kept in mind when using unitful literals where there is no implicit unit conversion. That is, using non-coherent units for those unitful literals is likely to be a mistake. The mistake can be detected by tools that perform (tool-dependent) analysis of unit consistency, while tools that don’t analyze unit consistency are likely to just drop the unit and produce incorrect results, see example below. ]

[Example : Implicit unit conversion in binding equations:

// Implicit unit conversion resolves inconsistent units:
Real x(unit = "m") = -5'cm'; // OK, same as -0.05'm'
// No implicit unit conversion since not (signed) literal:
Real y(unit = "m") = 0'cm' - 5'cm'; // Unit conflict
// No implicit unit conversion since not (signed) literal:
Real z(unit = "cm") = x; // Unit conflict
// No implicit unit conversion since w has no unit-attribute:
Real w = 5'cm'; // Not recommended, but accepted

There is no implicit unit conversion in equations:

Real x(unit = "m");
Real y(unit = "m");
equation
x = 5'cm' // Unit conflict
y = 0.05'm' // OK

A tool which does not detect the unit conflict might give the unexpected result of x being 5 meters. ]

A unitful literal may represent either an absolute or a relative quantity, depending on context. If an implicit unit conversion would be different for absolute and relative quantities (due to unit offsets), the implicit unit conversion is only allowed if the modified component (or its type) has an absoluteValue annotation (annotation 18.17).

[Example : Meanings of the unitful literal 20'degC'. Some valid uses in binding equations:

// Absolute temperature literal, will display as 293.15 K:
Real tCAbs(unit = "degC", displayUnit = "K") = 20'degC'
annotation(absoluteValue = true);
// Relative temperature literal, will display as 20 K:
Real tCRel(unit = "degC", displayUnit = "K") = 20'degC'
annotation(absoluteValue = false);
// Unclear temperature literal, but no need for conversion:
Real tC(unit = "degC") = 20'degC'; // OK, same as unitless literal ”20”
// Implicit unit conversion with offset, will display as 293.15 K:
Real tKAbs(unit = "K", displayUnit = "K") = 20'degC'
annotation(absoluteValue = true);
// Implicit unit conversion without offset, will display as 20 K:
Real tKRel(unit = "K", displayUnit = "K") = 20'degC'
annotation(absoluteValue = false);

Absence of absoluteValue annotation can prevent use of a unitful literal:

// No implicit conversion since no absoluteValue annotation:
Real tK(unit = "K") = 20'degC'; // Unit conflict

Adding a unitful temperature literal to a relative temperature leads to the same ambiguity as when adding a temperature variable without absoluteValue annotation. That is, since 20'degC' could be either absolute or relative, a tool cannot infer the absoluteValue status of the sum (compare annotation 18.17):

// Two variants with disallowed displayUnit conversion:
Real x(unit = "degC", displayUnit = "K") = tCRel + tC;
Real y(unit = "degC", displayUnit = "K") = tCRel + 20'degC';

]

[Example : Adding 2 centimeters to a length. A user has set x to 10 centimeters with the use of displayUnit, and intends to make y 2 centimeters bigger than x:

parameter Real x(unit = "m", displayUnit = "cm") = 0.1;
equation
y = x + 2; // Intention: Add 2 centimeters to x.

The result is that 2 meters instead of 2 centimeters is being added to x.

Using a unitful literal makes the intention clear:

y = x + 2'cm'; // Unit conflict

Since implicit unit conversion is not allowed in equations, there is a unit inconsistency in the addition that tools can detect and report. The unit inconsistency can then be resolved by using a unit compatible with the unit of x:

y = x + 0.02'm';

]