Modelica® Language Specification version 3.6-dev

Chapter 19 Unit Expressions

Unless otherwise stated, the syntax and semantics of unit expressions in Modelica 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

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 number, which is interpreted as an exponent. There must be no spacing between the unit-operand and a possible unit-exponent.

unit-operand :
   unit-symbol | unit-prefix unit-symbol
unit-prefix :
   "Y" | "Z" | "E" | "P" | "T" | "G" | "M" | "k" | "h" | "da"
   | "d" | "c" | "m" | "u" | "n" | "p" | "f" | "a" | "z" | "y"
unit-symbol :
   unit-char { unit-char }
unit-char :
   NON-DIGIT

It is required that basic and derived units of the SI system are recognized, 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).

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 Unit Checking

How to verify that units are used in compatible ways is current not fully determined by the specification. This section gives rules for certain situations, but in general tools are expected to reason according to standard dimensional analysis.

19.2.1 Standard Dimensional Analysis

This section gives an incomplete characterization of “standard dimensional analysis”, also referred to as just dimensional analysis. What is described below applies to unit checking in Modelica – dimensional analysis could have additional meanings in other contexts. While Modelica has a concept of empty units (described in later sections), standard dimensional analysis only deals with non-empty units such as "m", "m/s", or "1". It consists of two parts:

  • Unit compatibility requirements.

  • Rules for deriving the unit of an expression.

Unit compatibility requirements that must be met in Modelica:

  • Dimensional homogeneity: The two sides of an equation or assignment statement must have the same unit.

  • The expression of a declaration equation must have the same unit as the component to which it belongs (special case of dimensional homogeneity).

  • The two operands of the additive operators + and - must have the same unit.

  • In a function call, the unit of an argument expression must match the unit (unless empty, see below) of the corresponding function input component.

  • Other situations where unit compatibility might seem natural are currently not covered by the specification, but could become additional unit compatibility requirements in the future.

Unit derivation in Modelica:

  • The result of additive operators has the same unit as the operands.

  • The result of multiplication has a unit obtained by multiplying the operands’ units.

  • Built-in operators such as pre and previous preserve units, while der changes the unit by dividing it by "s".

  • Other expressions are not yet covered by the specification.

19.2.2 Empty and Undefined Units

In situations where the specification does not prescribe how to determine the unit of an expression, the unit of that expression is said to be undefined. It is then not possible for a tool to reject or approve the equation (or other construct) with support in the specification, and options for the tool include silently not performing unit checking, or applying checks based on dimensional analysis.

The unit of an expression can also be defined as being empty, see below. In certain places (see below), expressions with empty unit can be implicitly cast to suitable units. When an expression with empty unit is implicitly cast to some unit, that unit is referred to as the inferred unit of the expression.

19.2.3 Unit Inference

Where the empty unit has no other defined meaning, it can be implicitly cast to unit "1".

In the following cases, the empty unit can be implicitly cast to an inferred unit:

  • In binding equations and modifications:

    • The entire expression of the binding or modification.

    • When the entire expression is an array construction, array concatenation and array range, then apply rules recursively for the direct subexpressions.

  • The entire argument expression in a function call.

19.2.4 Expressions with Empty Unit

This section describes conditions under which an expression has empty unit. Conditions not given here must not be interpreted as definitely not implying empty unit; instead, the unit may be currently undefined for some expressions, allowing the unit to be properly defined in future versions of the specification.

Basic expressions defined to have empty unit:

  • Real literals.

  • Integer expressions implicitly cast to Real.

Expressions defined to not propagate the empty unit, thereby forcing inference of unit "1":

  • Addition, subtraction, multiplication and division operators when either operand has non-empty unit.

  • Right operand of binary exponentiation.

  • Component references outside of functions, where the component’s declared unit-attribute is "" (possibly by not being specified). (Unit checking involving user-defined functions with empty unit on inputs and outputs is currently not defined.)

Built-in non-array operators, functions, and special expressions that propagate any unit (including empty):

  • When all operands have the same unit: negation, addition, and subtraction (scalar or element-wise).

Special situations in which the empty unit is propagated up the expression tree:

  • Multiplication and division when both operands have empty unit.

[Example: Consider unit checking of the following declaration equation:

Real y(unit = "m") = 1 + 2.5 * 3;

The unit of the declaration equation right-hand side is determined as follows:

  • The Real literal 2.5 has empty unit.

  • The Integer literals 1 and 3 are implicitly cast to Real, and therefore have empty unit.

  • The multiplication 2.5 * 3 has empty unit, as multiplication can propagate the empty unit of both operands.

  • The addition 1 + (2.5 * 3) has empty unit, as addition can propagate any unit as long as both operands have the same unit.

  • The entire right-hand side expression gets inferred unit "m" in order to be compatible with the component’s declared unit.

]

[Example: Consider unit checking of the following erroneous declaration equation for y:

Real x(unit = "m") = 1.0;
Real y(unit = "m") = x^2 / 2;

The unit of the declaration equation right-hand side is determined as follows:

  • The unit of x is "m", and dimensional analysis gives that x^2 has unit "m2"

  • The Real literal 1.0 has empty unit, and gets inferred unit "m".

  • As the left operand of x^2 / 2 is non-empty, the right operand cannot be empty, and hence empty unit of 2 is implicitly cast to "1".

  • Dimensional analysis then gives that the unit of x^2 / 2 is "m2", which is an error due to the required unit being "m".

]

[Example: Consider the potential consequences of an undefined unit in the following declaration equation:

Real y(unit = "m") = sin(1.57);

To see that the unit of the declaration equation right-hand side is undefined, note that:

  • The Real literal 1.57 has empty unit.

  • The expression sin(1.57) is not covered by the specification, and hence has undefined unit.

If a tool wants to proceed according to “standard dimensional analysis”, alternatives include:

  • Assume that sin is a mapping from unit "1" to unit "1". The unit of 1.57 then defaults to "1" (alternatively, the same unit could be obtained by inference). The unit of sin(1.57) is then found to be "1", which is an error due to the required unit being "m".

  • Assume that sin preserves both the unit "1" and the empty unit. The empty unit of 1.57 gets propagated to sin(1.57), which in turn gets inferred unit "m".

]