-
Notifications
You must be signed in to change notification settings - Fork 41
/
unitexpressions.tex
259 lines (209 loc) · 13.9 KB
/
unitexpressions.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
\chapter{Unit Expressions}\label{unit-expressions}
Unless otherwise stated, the syntax and semantics of unit expressions in Modelica conform with the international standards \emph{International System of Units (SI)} by BIPM superseding parts of ISO 31/0-1992 \emph{General principles concerning quantities, units and symbols} and ISO 1000-1992 \emph{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 \emph{meter}, whereas the SI specification from BIPM uses the British spelling \emph{metre}.
Examples for the syntax of unit expressions used in Modelica: \lstinline!"N.m"!, \lstinline!"kg.m/s2"!, \lstinline!"kg.m.s-2"!, \lstinline!"1/rad"!, \lstinline!"mm/s"!.
\section{The Syntax of Unit Expressions}\label{the-syntax-of-unit-expressions}
The Modelica unit string syntax allows neither comments nor white-space, and a unit string shall match the \lstinline[language=grammar]!unit-expression! rule:
\begin{lstlisting}[language=grammar]
unit-expression :
unit-numerator [ "/" unit-denominator ]
unit-numerator :
"1" | unit-factors | "(" unit-expression ")"
unit-denominator:
unit-factor | "(" unit-expression ")"
\end{lstlisting}
The unit of measure of a dimension free quantity is denoted by \lstinline!"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 (\lstinline!/!) since the result is ambiguous; either the divisor shall be enclosed in parentheses, or negative exponents used instead of division, for example, \lstinline!"J/(kg.K)"! may be written as \lstinline!"J.kg-1.K-1"!.
\begin{lstlisting}[language=grammar]
unit-factors :
unit-factor [ "." unit-factors ]
\end{lstlisting}
The SI standard specifies that a multiplication operator symbol is written as space or as a dot.
The SI standard requires that this \emph{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 \emph{dot} on the base line.
For example, Modelica does not support \lstinline!"Nm"! for newton-meter, but requires it to be written as \lstinline!"N.m"!.
\begin{lstlisting}[language=grammar]
unit-factor :
unit-operand [ unit-exponent ]
unit-exponent :
[ "+" | "-" ] UNSIGNED-INTEGER
\end{lstlisting}
The SI standard uses super-script for the exponentation, and does thus not define any operator symbol for exponentiation.
A \lstinline[language=grammar]!unit-factor! consists of a \lstinline[language=grammar]!unit-operand! possibly suffixed by a possibly signed integer number, which is interpreted as an exponent.
There must be no spacing between the \lstinline[language=grammar]!unit-operand! and a possible \lstinline[language=grammar]!unit-exponent!.
\begin{lstlisting}[language=grammar]
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
\end{lstlisting}
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 $\Omega$ is spelled out as \lstinline!"Ohm"!.
Similarly degree is spelled out as \lstinline!"deg"!, both on its own (for angles) and as part of \lstinline!"degC"!, \lstinline!"degF"! and \lstinline!"degRk"! for temperatures (Celsius, Fahrenheit and Rankine).
A \lstinline[language=grammar]!unit-operand! should first be interpreted as a \lstinline[language=grammar]!unit-symbol! and only if not successful the second alternative assuming a prefixed operand should be exploited.
There must be no spacing between the \lstinline[language=grammar]!unit-symbol! and a possible \lstinline[language=grammar]!unit-prefix!.
The values of the prefixes are according to the ISO standard.
The letter \lstinline!u! is used as a symbol for the prefix \emph{micro}.
\begin{nonnormative}
A tool may present \lstinline!"Ohm"! as $\Omega$ and the prefix \lstinline!"u"! as $\mu$.
Exponents such as \lstinline!"m2"! may be presented as m\textsuperscript{2}.
Degrees may be presented as $^{\circ}$, both for \lstinline!"deg"! on its own (for angles) and for temperatures -- e.g., \lstinline!"degC"! can be presented as $^{\circ}$C.
Note that BIPM have specific recommendations for formatting using these symbols.
\end{nonnormative}
\begin{example}
The unit expression \lstinline!"m"! means meter and not milli (10\textsuperscript{-3}), since prefixes cannot be used in isolation.
For millimeter use \lstinline!"mm"! and for square meter, m\textsuperscript{2}, write \lstinline!"m2"!.
The expression \lstinline!"mm2"! means (10\textsuperscript{-3}m)\textsuperscript{2} = 10\textsuperscript{-6}m\textsuperscript{2}.
Note that exponentiation includes the prefix.
The unit expression \lstinline!"T"! means tesla, but note that the letter \lstinline!T! is also the symbol for the prefix tera which has a multiplier value of 10\textsuperscript{12}.
\end{example}
\section{Unit Checking}\label{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.
\subsection{Standard Dimensional Analysis}\label{standard-dimensional-analysis}
This section gives an incomplete characterization of ``standard dimensional analysis'', also referred to as just \firstuse{dimensional analysis}.
What is described below applies to unit checking in Modelica -- \emph{dimensional analysis} could have additional meanings in other contexts.
While Modelica has a concept of \willintroduce{empty units} (described in later sections), standard dimensional analysis only deals with non-empty units such as \lstinline!"m"!, \lstinline!"m/s"!, or \lstinline!"1"!.
It consists of two parts:
\begin{itemize}
\item
Unit compatibility requirements.
\item
Rules for deriving the unit of an expression.
\end{itemize}
Examples of unit compatibility requirements that must be met in Modelica:
\begin{itemize}
\item
Dimensional homogeneity: The two sides of an equation or assignment statement must have the same unit.
\item
The expression of a declaration equation must have the same unit as the component to which it belongs (special case of dimensional homogeneity).
\item
The two operands of the additive operators \lstinline!+! and \lstinline!-! must have the same unit.
\item
In a function call, the unit of an argument expression must match the unit (unless empty, see below) of the corresponding function input component.
\end{itemize}
% TODO: Replace these examples by giving unit semantics where operators and built-in functions are defined in the specification,
% and just include some likes to places where such semantics are given.
Examples of unit derivation in Modelica:
\begin{itemize}
\item
The result of additive operators has the same unit as the operands.
\item
The result of multiplication has a unit obtained by multiplying the operands' units.
\item
Built-in operators such as \lstinline!pre! and \lstinline!previous! preserve units, while \lstinline!der! changes the unit by dividing it by \lstinline!"s"!.
\end{itemize}
\subsection{Empty and Undefined Units}\label{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 \firstuse[undefined unit]{undefined}.
It is then not possible for a tool to reject 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 dimensional analysis.
The unit of an expression can also be defined as being \firstuse[empty unit]{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 \firstuse{inferred unit} of the expression.
\subsection{Unit Inference}\label{unit-inference}
Where the empty unit has no other defined meaning, it can be implicitly cast to unit \lstinline!"1"!.
In some cases the empty unit can be implicitly cast to an inferred unit:
\begin{itemize}
\item
In binding equations and modifications:
\begin{itemize}
\item The entire expression of the binding or modification.
\item When the entire expression is an array construction, array concatenation and array range, then apply rules recursively for the direct subexpressions.
\end{itemize}
\item
The entire argument expression in a function call.
% To keep the design close to the bare minimum, this part is currently excluded:
%\item
% For a translation-time constant with value 0.0:
% \begin{itemize}
% \item Expressions constituting the entire side of an equality or relation.
% \item The right hand side of an assignment.
% \item The direct subexpressions of array construction, array concatenation, and array range.
% \end{itemize}
\end{itemize}
\subsection{Expressions with Empty Unit}\label{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:
\begin{itemize}
\item
\lstinline!Real! literals.
\item
\lstinline!Integer! expressions implicitly cast to \lstinline!Real!.
\end{itemize}
Expressions defined to \emph{not} propagate the empty unit, thereby forcing inference of unit \lstinline!"1"!:
\begin{itemize}
\item
Addition, subtraction, multiplication and division operators when either operand has non-empty unit.
\item
Right operand of binary exponentiation.
\item
Component references outside of functions, where the component's declared \lstinline!unit!-attribute is \lstinline!""! (possibly by not being specified).
(Unit checking involving user-defined functions with empty unit on inputs and outputs is currently not defined.)
\end{itemize}
Built-in non-array operators, functions, and special expressions that propagate any unit (including empty):
\begin{itemize}
\item
When all operands have the same unit: negation, addition, and subtraction (scalar or element-wise).
\end{itemize}
Special situations in which the empty unit can be propagated up the expression tree:
\begin{itemize}
\item
Multiplication and division when both operands have empty unit.
\end{itemize}
\begin{example}
Consider unit checking of the following declaration equation:
\begin{lstlisting}[language=modelica]
Real y(unit = "m") = 1 + 2.5 * 3;
\end{lstlisting}
The unit of the declaration equation right-hand side is determined as follows:
\begin{itemize}
\item The \lstinline!Real! literal \lstinline!2.5! has empty unit.
\item The \lstinline!Integer! literals \lstinline!1! and \lstinline!3! are implicitly cast to \lstinline!Real!, and therefore have empty unit.
\item The multiplication \lstinline!2.5 * 3! has empty unit, as multiplication can propagate the empty unit of both operands.
\item The addition \lstinline!1 + (2.5 * 3)! has empty unit, as addition can propagate any unit as long as both operands have the same unit.
\item The entire right-hand side expression gets inferred unit \lstinline!"m"! in order to be compatible with the component's declared \lstinline!unit!.
\end{itemize}
\end{example}
\begin{example}
Consider unit checking of the following erroneous declaration equation for \lstinline!y!:
\begin{lstlisting}[language=modelica]
Real x(unit = "m") = 1.0;
Real y(unit = "m") = x^2 / 2;
\end{lstlisting}
The unit of the declaration equation right-hand side is determined as follows:
\begin{itemize}
\item The unit of \lstinline!x! is \lstinline!"m"!, and dimensional analysis gives that \lstinline!x^2! has unit \lstinline!"m2"!
\item The \lstinline!Real! literal \lstinline!1.0! has empty unit, and gets inferred unit \lstinline!"m"!.
\item As the left operand of \lstinline!x^2 / 2! is non-empty, the right operand cannot be empty, and hence empty unit of \lstinline!2! is implicitly cast to \lstinline!"1"!.
\item Dimensional analysis then gives that the unit of \lstinline!x^2 / 2! is \lstinline!"m2"!, which is an error due to the required unit being \lstinline!"m"!.
\end{itemize}
\end{example}
\begin{example}
Consider the potential consequences of an undefined unit in the following declaration equation:
\begin{lstlisting}[language=modelica]
Real y(unit = "m") = sin(1.57);
\end{lstlisting}
To see that the unit of the declaration equation right-hand side is undefined, note that:
\begin{itemize}
\item The \lstinline!Real! literal \lstinline!1.57! has empty unit.
\item The expression \lstinline!sin(1.57)! is not covered by the specification, and hence has undefined unit.
\end{itemize}
If a tool wants to proceed according to ``standard dimensional analysis'', alternatives include:
\begin{itemize}
\item
Assume that \lstinline!sin! is a mapping from unit \lstinline!"1"! to unit \lstinline!"1"!.
The unit of \lstinline!1.57! then defaults to \lstinline!"1"! (alternatively, the same unit could be obtained by inference).
The unit of \lstinline!sin(1.57)! is then found to be \lstinline!"1"!, which is an error due to the required unit being \lstinline!"m"!.
\item
Assume that \lstinline!sin! preserves both the unit \lstinline!"1"! and the empty unit.
The empty unit of \lstinline!1.57! gets propagated to \lstinline!sin(1.57)!, which in turn gets inferred unit \lstinline!"m"!.
\end{itemize}
\end{example}