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 should 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 binding 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.

  • The two connectors in a connect-equation must agree on all units inside the connectors (follows from dimensional homogeneity and additive operator rule).

  • In a function call, the unit of an argument expression must match the unit 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.

The requirements above apply to non-empty as well as empty units, but both unit propagation and unit inference (described below) need to be considered when verifying the requirements.

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 Propagation

The main work of unit checking is performed on the flattened model, with the exception that connect-equations need to be considered for unit propagation.

Unit propagation is the act of replacing the empty unit of an instantiated component by some other unit in order to fulfill some of the most obvious unit compatibility requirements. It is the first step of the unit checking procedure, being independent of both unit derivation and unit inference. Unit propagation takes place in the following situations:

  • Simple binding equations (section 8.1): When a component is declared with empty unit-attribute and has a binding equation with just a component reference on the right-hand side, the unit of the right-hand side replaces the empty unit.

  • connect-equations: Units can be propagated in both directions of a connect-equation.

Notation: The unit-attribute of a component refers to the attribute as given in the model. The unit of a component refers to the component’s unit after unit propagation has been carried out.

[The reason to not propagate units of non-simple binding equations is to avoid bootstrapping problems where unit propagation depends on unit derivation, and unit derivation depends on unit propagation. If the restrictions on unit propagation would be relaxed in the future, this would be a backwards compatible change as it only means that there would be less need to write out units explicitly.]

[For unit propagation in connect-equations, it is recommended to perform the propagation on the connection sets to avoid diagnostics involving two connectors where neither is declared with a non-empty unit. By considering a connection set, a diagnostic message can omit all the connectors where the unit is empty, and only report examples of connectors in the set with mismatched non-empty units.]

[Example: The following illustrates unit propagation and its limitations:

Real x(unit = "m") = 1.0; // OK: No unit propagation.
Real y = x;               // OK: Unit propagation assigns y the unit ”m”.
Real z = y;               // OK: Unit propagation assigns z the unit ”m”.
Real w = 2 * z;           // Error: No unit propagation as 2 * z isn't simple.

(The rules making the binding equation for x OK will be given in the sections below.)]

19.2.4 Bottom-Up Unit Derivation

After completed unit propagation, the unit of every expression shall be determined in order to be able to verify unit compatibility requirements. The derivation is a bottom-up analysis of the expression tree, involving expression-specific rules and a simple form of unit inference. Separate rules assign units to all expression tree leaves (such as variables and literals). For a general non-leaf expression 𝑜𝑝(e1,e2,,en) where 𝑜𝑝 symolizes the kind of expression and the ei represent the immediate children in the expression tree, unit derivation follows these steps:

  1. 1.

    Derive the unit of each subexpression ei. The so obtained unit of the expression might be empty as well as non-empty.

  2. 2.

    If there is a unit derivation rule for 𝑜𝑝 matching the units of the ei, apply that rule. Note that some expressions can handle subexpressions with empty unit, often assigning the empty unit to the entire 𝑜𝑝-expression as well.

  3. 3.

    Otherwise:

    1. a.

      Infer a non-empty unit for each ei that has empty unit, and let ei denote the subexpressions after unit inference. How the unit is inferred depends on the kind of expression, and is described in section 19.2.5.

    2. b.

      If there is a unit derivation rule for 𝑜𝑝(e1,e2,,en), apply that rule.

    3. c.

      Otherwise, the 𝑜𝑝-expression has a unit error.

19.2.5 Unit Inference

In Modelica unit checking, unit inference refers to the implicit casting of an expression with empty unit to a corresponding expression with non-empty unit. An expression having empty unit always gets an inferred non-empty unit when appearing in a context where the empty unit is not allowed.

When encountering the empty unit in the following situations, the inferred unit is (uniquely) determined by ensuring that unit compatibility requirements are fulfilled:

  • 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.

Otherwise, the inferred unit is "1".

[Example: Consider unit inference in the binding equation below:

Real y(unit = "m") = 1.5;

With the pseudo-code form unit(e, u) representing the expression e having empty unit being cast to unit u, the binding equation after unit inference could be expressed explicitly as:

Real y(unit = "m") = unit(1.5, "m"); // Using pseudo-code operator 'unit'.

Note that unit inference has not changed the empty unit of 1.5 itself, but that it has introduced an implicit unit cast around 1.5 in order to fulfill the unit compatibility requirement.]

19.2.6 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 up the expression tree, 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 unit (after unit propagation has been carried out) is empty (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) up the expression tree:

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

  • The abs function, see function 3.1.

Transcendental functions that propagate both unit "1" and the empty unit up the expression tree:

  • All of the elementary mathematical functions listed in section 3.7.3. (Whether some of these also accept other dimensionless units such as "rad" for the input argument is currently not defined.)

  • The only binary of these functions, atan2, requires both arguments to have the same unit, and accepts any unit. The result of atan2 has empty unit only when both arguments have empty unit; otherwise the unit of the result is "1".

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 binding equation:

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

The unit of the binding 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-attribute.

]

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

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

The unit of the binding 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: Difference between using a literal and a constant:

function f
  input Real u(unit = "m");
  output Real y(unit = "m") = u;
end f;
constant Real pi = 3.14;
Real x(unit = "m") = f(3.14); // OK.
Real y(unit = "m") = f(pi);   // Error.

The first call to f is OK due to unit inference making "m" the inferred unit of 3.14. The second call to f is an error because the component reference pi has an inferred unit of "1" by itself, which prevents inference of the unit required by the function call.]

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

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

To see that the unit of the binding 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".

]