forked from modelica/ModelicaSpecification
-
Notifications
You must be signed in to change notification settings - Fork 2
/
operatorsandexpressions.tex
1632 lines (1429 loc) · 86.5 KB
/
operatorsandexpressions.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\chapter{Operators and Expressions}\label{operators-and-expressions}
The lexical units are combined to form even larger building blocks such as \firstuse[expression]{expressions} according to the rules given by the \lstinline[language=grammar]!expression! part of the Modelica grammar in \cref{modelica-concrete-syntax}.
For example, they can be built from operators, function references, components, or component references (referring to components) and literals.
Each expression has a type and a variability.
This chapter describes the evaluation rules for expressions, the concept of expression variability, built-in mathematical operators and functions, and the built-in special Modelica operators with function syntax.
Expressions can contain variables and constants, which have types, predefined or user defined.
The predefined built-in types of Modelica are \lstinline!Real!, \lstinline!Integer!, \lstinline!Boolean!, \lstinline!String!, and enumeration types which are presented in more detail in \cref{predefined-types-and-classes}.
\section{Expressions}\label{expressions}
Modelica equations, assignments and declaration equations contain expressions.
Expressions can contain basic operations, \lstinline!+!, \lstinline!-!, \lstinline!*!, \lstinline!/!, \lstinline!^!, etc.\ with normal precedence as defined in \cref{tab:operator-precedence} in \cref{operator-precedence-and-associativity} and the grammar in \cref{modelica-concrete-syntax}.
The semantics of the operations is defined for both scalar and array arguments in \cref{scalar-vector-matrix-and-array-operator-functions}.
It is also possible to define functions and call them in a normal fashion.
The function call syntax for both positional and named arguments is described in \cref{positional-or-named-input-arguments-of-functions} and for vectorized calls in \cref{initialization-and-binding-equations-of-components-in-functions}.
The built-in array functions are given in \cref{array-dimension-lower-and-upper-index-bounds} and other built-in operators in \cref{built-in-intrinsic-operators-with-function-syntax}.
\section{Operator Precedence and Associativity}\label{operator-precedence-and-associativity}
Operator precedence determines the implicit subexpression structure of expressions with operators.
(Explicit subexpression structure can be expressed by wrapping the subexpression in parentheses.)
An operator with higher precedence ties harder to its operands than an operator with lower precedence.
For example, `\lstinline!*!' having higher precedence than `\lstinline!+!' means that \lstinline!1 + 2 * 3! is implicitly structured as \lstinline!1 + (2 * 3)!.
Precedence group associativity is used to determine the implicit subexpression structure when operators belong to the same group of equal precedence.
Left associativity means that subexpressions are formed from left to right.
For example, left associativity of binary additive operators means that \lstinline!1 - 2 - 3! is implicitly structured as \lstinline!(1 - 2) - 3!.
A precedence group may also be non-associative, meaning that there is no implicit subexpression structure defined based on associativity.
For example, non-associativity of relational operators means that \lstinline!1 < 2 < 3! is an invalid expression.
Note that the operators don't need to be identical for associativity to matter; also \lstinline!1 == 2 < 3! is invalid, and \lstinline!1 - 2 + 3! is implicitly structured as \lstinline!(1 - 2) + 3!.
Also note that the non-associative array range in Modelica can be used with either two or three operands separated by `\lstinline!:!', meaning that \lstinline!1 : 2 : 5! is one valid ternary use of the operator rather than two invalid binary uses of the operator.
At the parsing stage -- which is where the here defined operator precedence and associativity matters -- the subexpression structure is fixed.
Since Modelica tools have the freedom to symbolically manipulate expressions, this subexpression structure cannot be expected to reflect order of evaluation, compare \cref{evaluation-order}.
The following table presents the precedence and associativity of all the expression operators, consistent with and complementing information that can be derived from the Modelica grammar in \cref{modelica-concrete-syntax}.
\begin{table}[H]
\caption{%
Operators in order of precedence from highest to lowest.
Operators with different precedence are separated by horizontal lines.
All operators are binary except the postfix operators and those shown as unary together with \emph{expr}, the conditional operator, the array construction operator
% Beware that the array construction operator, normally expressed as \lstinline!{ }! needs escaped braces inside \caption.
% This isn't handled correctly by LaTeXML, as reported here:
% - https://github.com/brucemiller/LaTeXML/issues/1377
\ifpdf
\lstinline!\{ \}! % Intentional space at end of line.
\else
\lstinline!{ }! % Intentional space at end of line.
\fi
and concatenation operator \lstinline![ ]!, and the array range constructor which is either binary or ternary.\\
$^{\dagger}$ The associativity of array construction and concatenation refers to the separator (`\lstinline!,!' or `\lstinline!;!'), not the enclosing delimiters.
}\label{tab:operator-precedence}
\begin{center}
\begin{tabular}{l c l l}
\hline
\tablehead{Operator group} & \tablehead{Assoc.} & \tablehead{Operator syntax} & \tablehead{Examples}\\
\hline
\hline
Postfix array index & left & {\lstinline![]!} & {\lstinline!arr[index]!}\\
\hline
Postfix access & left & {\lstinline!.!} & {\lstinline!a.b!}\\
\hline
Postfix function call & none & {\lstinline!$\mathit{funcName}$($\mathit{args}$)!} & {\lstinline!sin(4.36)!}\\
\hline
Array construction & left$^{\dagger}$ & {\lstinline!{$\mathit{expr}$, $\mathit{expr}$, $\ldots$}!} & {\lstinline!{2, 3}!}\\
Horizontal concatenation & left$^{\dagger}$ & {\lstinline![$\mathit{expr}$, $\mathit{expr}$, $\ldots$]!} & {\lstinline![5, 6]!}\\
\hline
Vertical concatenation & left$^{\dagger}$ & {\lstinline![$\mathit{expr}$; $\mathit{expr}$; $\ldots$]!} & {\lstinline![2, 3; 7, 8]!}\\
\hline
Exponentiation & none & {\lstinline!^!} & {\lstinline!2 ^ 3!}\\
\hline
Multiplicative & left & {\lstinline!* /!} & {\lstinline!2 * 3!}, {\lstinline!2 / 3!}\\
Elementwise multiplicative & left & {\lstinline!.* ./!} & {\lstinline!{2, 3} .* {4, 5}!}\\
\hline
Additive unary & none & {\lstinline!+$\mathit{expr}$ -$\mathit{expr}$!} & {\lstinline!-0.5!}\\
\hline
Additive & left & {\lstinline!+ -!} & {\lstinline!1 + 2!}\\
Elementwise additive & left & {\lstinline!.+ .-!} & {\lstinline!{2, 3} .+ {4, 5}!}\\
\hline
Relational & none & {\lstinline!< <= > >= == <>!} & {\lstinline!a < b!}, {\lstinline!a <= b!}, {\lstinline!a > b!}\\
\hline
Unary negation & none & {\lstinline!not $\mathit{expr}$!} & {\lstinline!not b1!}\\
\hline
Logical and & left & {\lstinline!and!} & {\lstinline!b1 and b2!}\\
\hline
Logical or & left & {\lstinline!or!} & {\lstinline!b1 or b2!}\\
\hline
\multirow{2}{*}{Array range} & none & {\lstinline!$\mathit{expr}$ : $\mathit{expr}$!} & {\lstinline!1 : 5!}\\
& none & {\lstinline!$\mathit{expr}$ : $\mathit{expr}$ : $\mathit{expr}$!} & {\lstinline!start : step : stop!}\\
\hline
Conditional & none & {\lstinline!if $\mathit{expr}$ then $\mathit{expr}$ else $\mathit{expr}$!} & {\lstinline!if b then 3 else x!}\\
\hline
Named argument & none & {\lstinline!$\mathit{ident}$ = $\mathit{expr}$!} & {\lstinline!x = 2.26!}\\
\hline
\end{tabular}
\end{center}
\end{table}
The postfix array index and postfix access operators are not expression operators in the normal sense that \lstinline!a.b[1]! can be treated as \lstinline!a.(b[1])!.
Instead, these operators need to be considered jointly to identify an entire \lstinline[language=grammar]!component-reference! (one of the alternative productions for \lstinline[language=grammar]!primary! in the grammar) which is the smallest unit that can be seen as an expression in itself.
Postfix array index and postifx access can only be applied immediately to a \lstinline[language=grammar]!component-reference!; not even parentheses around the left operand are allowed.
\begin{example}
Relative precedence of postfix array index and postfix access.
Consider the following definition of the array variable \lstinline!a!:
\begin{lstlisting}[language=modelica]
record R
Real[2] x;
end R;
R[3] a;
\end{lstlisting}
These are some valid as well as invalid ways to using postfix array index and postfix access:
\begin{lstlisting}[language=modelica]
a[3].x[2] // OK: Component reference of type Real
a[3].x // OK: Component reference of type Real[2]
a.x[2] // OK: Component reference of type Real[3]
a.x // OK: Component reference of type Real[3, 2]
(a.x)[2] // Error: Invalid use of parentheses
a[3] // OK: Component reference of type R
(a[3]).x // Error: Invalid use of parentheses
\end{lstlisting}
The relation between \lstinline!a.x!, \lstinline!a.x[2]!, and \lstinline!(a.x)[2]! illustrates the effect of giving higher precedence to array index than postfix access.
Had the precedence been equal, this would have changed the meaning of \lstinline!a.x[2]! to the same thing that \lstinline!(a.x)[2]! tries to express, being a component reference of type \lstinline!Real[2]!.
\end{example}
\begin{example}
Non-associative exponentiation and array range operator (note that the array range operator only takes scalar operands):
\begin{lstlisting}[language=modelica]
x ^ y ^ z // Not legal, use parentheses to make it clear.
a : b : c : d // Not legal, and parentheses cannot make it legal.
\end{lstlisting}
\end{example}
The additive unary expressions are only allowed in the first term of a sum, that is, not immediately to the right of any of the additive or elementwise additive operators.
For example, \lstinline!1 + -1 + 1! is an invalid expression (not parseable according to \cref{modelica-concrete-syntax}), whereas both \lstinline!1 + (-1) + 1! and \lstinline!-1 + 1 + 1! are fine.
\begin{example}
The unary minus and plus in Modelica is slightly different than in Mathematica\footnote{\emph{Mathematica} is a registered trademark of Wolfram Research Inc.} and in MATLAB\footnote{\emph{MATLAB} is a registered trademark of MathWorks Inc.}, since the following expressions are illegal (whereas in Mathematica and in MATLAB these are valid expressions):
% Using $\hspace{0pt}$ below to work around https://github.com/brucemiller/LaTeXML/issues/1399
\begin{lstlisting}[language=modelica]
2*$\hspace{0pt}$-2 // = -4 in Mathematica/MATLAB; is illegal in Modelica
-$\hspace{0pt}$-2 // = 2 in Mathematica/MATLAB; is illegal in Modelica
+$\hspace{0pt}$+2 // = 2 in Mathematica/MATLAB; is illegal in Modelica
2-$\hspace{0pt}$-2 // = 4 in Mathematica/MATLAB; is illegal in Modelica
\end{lstlisting}
\end{example}
The conditional operator may also include \lstinline!elseif!-branches.
Equality \lstinline!=! and assignment \lstinline!:=! are not expression operators since they are allowed only in equations and in assignment statements respectively.
\begin{nonnormative}
The operator precedence table is useful when generating textual representations of Modelica expression trees.
When doing this, attention must be paid to the rule that the unary additive operators are only allowed for the first term in a sum.
A naive implementation might not produce all the required parentheses for an expression tree such as \lstinline!1 + (-1)!, as it might think that the higher precedence of the unary operator makes the parentheses redundant.
A trick that solves this problem is to instead treat the additive unary operators as left associative with the same precedence as the binary additive operators.
\end{nonnormative}
\section{Evaluation Order}\label{evaluation-order}
A tool is free to solve equations, reorder expressions and to not evaluate expressions if their values do not influence the result (e.g.\ short-circuit evaluation of \lstinline!Boolean! expressions).
\lstinline!if!-statements and \lstinline!if!-expressions guarantee that their branches are only evaluated if the appropriate condition is true, but relational operators generating state or time events will during continuous integration have the value from the most recent event.
If a numeric operation overflows the result is undefined.
For literals it is recommended to automatically convert the number to another type with greater precision.
\begin{example}
If one wants to guard an expression against incorrect evaluation, it should be guarded by an \lstinline!if!:
\begin{lstlisting}[language=modelica]
Boolean v[n];
Boolean b;
Integer I;
equation
b = (I >= 1 and I <= n) and v[I]; // Unsafe, may result in error
b = if (I >= 1 and I <= n) then v[I] else false; // Safe
\end{lstlisting}
To guard square against square root of negative number use \lstinline!noEvent!:
\begin{lstlisting}[language=modelica]
der(h) = if h > 0 then -c * sqrt(h) else 0; // Incorrect
der(h) = if noEvent(h > 0) then -c * sqrt(h) else 0; // Correct
\end{lstlisting}
\end{example}
\section{Arithmetic Operators}\label{arithmetic-operators}
Modelica supports five binary arithmetic operators that operate on any numerical type:
\begin{center}
\begin{tabular}{c|l}
\tablehead{Operator} & \tablehead{Description} \\
\hline
\hline
{\lstinline!^!} & Exponentiation\\
{\lstinline!*!} & Multiplication\\
{\lstinline!/!} & Division\\
{\lstinline!+!} & Addition\\
{\lstinline!-!} & Subtraction\\
\hline
\end{tabular}
\end{center}
Some of these operators can also be applied to a combination of a scalar type and an array type, see \cref{scalar-vector-matrix-and-array-operator-functions}.
The syntax of these operators is defined by the following rules from the Modelica grammar:
\begin{lstlisting}[language=grammar]
arithmetic-expression :
[ add-operator ] term { add-operator term }
add-operator :
"+" | "-"
term :
factor { mul-operator factor }
mul-operator :
"*" | "/"
factor :
primary [ "^" primary ]
\end{lstlisting}
\section{Equality, Relational, and Logical Operators}\label{equality-relational-and-logical-operators}
Modelica supports the standard set of relational and logical operators, all of which produce the standard boolean values \lstinline!true! or \lstinline!false!:
\begin{center}
\begin{tabular}{c|l}
\tablehead{Operator} & \tablehead{Description} \\
\hline
\hline
{\lstinline!>!} & Greater than\\
{\lstinline!>=!} & Greater than or equal\\
{\lstinline!<!} & Less than\\
{\lstinline!<=!} & Less than or equal to\\
{\lstinline!==!} & Equality within expressions\\
{\lstinline!<>!} & Inequality\\
\hline
\end{tabular}
\end{center}
A single equals sign \lstinline!=! is never used in relational expressions, only in equations (\cref{equations}, \cref{equality-and-assignment}) and in function calls using named parameter passing (\cref{positional-or-named-input-arguments-of-functions}).
The following logical operators are defined:
\begin{center}
\begin{tabular}{c|l}
\tablehead{Operator} & \tablehead{Description} \\
\hline
\hline
{\lstinline!not!}\indexinline{not} & Logical negation (unary operator)\\
{\lstinline!and!}\indexinline{and} & Logical \emph{and} (conjunction)\\
{\lstinline!or!}\indexinline{or} & Logical \emph{or} (disjunction)\\
\hline
\end{tabular}
\end{center}
The grammar rules define the syntax of the relational and logical operators.
\begin{lstlisting}[language=grammar]
logical-expression :
logical-term { or logical-term }
logical-term :
logical-factor { and logical-factor }
logical-factor :
[ not ] relation
relation :
arithmetic-expression [ relational-operator arithmetic-expression ]
relational-operator :
"<" | "<=" | ">" | ">=" | "==" | "<>"
\end{lstlisting}
The following holds for relational operators:
\begin{itemize}
\item
Relational operators \lstinline!<!, \lstinline!<=!,\lstinline!>!, \lstinline!>=!, \lstinline!==!, \lstinline!<>!, are only defined for scalar operands of simple types.
The result is \lstinline!Boolean! and is true or false if the relation is fulfilled or not, respectively.
\item
For operands of type \lstinline!String!, \lstinline!str1 $\mathit{op}$ str2! is for each relational operator, $\mathit{op}$, defined in terms of the C function \lstinline[language=C]!strcmp! as \lstinline[language=C]!strcmp(str1, str2) $\mathit{op}$ 0!.
\item
For operands of type \lstinline!Boolean!, \lstinline!false < true!.
\item
For operands of enumeration types, the order is given by the order of declaration of the enumeration literals.
\item
In relations of the form \lstinline!v1 == v2 or v1 <> v2!, \lstinline!v1! or \lstinline!v2! shall, unless used in a function, not be a subtype of \lstinline!Real!.
\begin{nonnormative}
The reason for this rule is that relations with \lstinline!Real! arguments are transformed to state events (see \cref{events-and-synchronization}) and this transformation becomes unnecessarily complicated for the \lstinline!==! and \lstinline!<>! relational operators (e.g.\ two crossing functions instead of one crossing function needed, epsilon strategy needed even at event instants).
Furthermore, testing on equality of \lstinline!Real! variables is questionable on machines where the number length in registers is different to number length in main memory.
\end{nonnormative}
\item
Relational operators can generate events, see \cref{discrete-time-expressions}.
\end{itemize}
\section{Miscellaneous Operators and Variables}\label{miscellaneous-operators-and-variables}
Modelica also contains a few built-in operators which are not standard arithmetic, relational, or logical operators.
These are described below, including \lstinline!time!, which is a built-in variable, not an operator.
\subsection{String Concatenation}\label{string-concatenation}
Concatenation of strings (see the Modelica grammar) is denoted by the \lstinline!+! operator in Modelica.
\begin{example}
\lstinline!"a" + "b"! becomes \lstinline!"ab"!.
\end{example}
\subsection{Array Constructor Operator}\label{array-constructor-operator}
The array constructor operator \lstinline!{ $\ldots$ }! is described in \cref{vector-matrix-and-array-constructors}.
\subsection{Array Concatenation Operator}\label{array-concatenation-operator}
The array concatenation operator \lstinline![ $\ldots$ ]! is described in \cref{array-concatenation}.
\subsection{Array Range Operator}\label{array-range-operator}
The array range constructor operator \lstinline!:! is described in \cref{vector-construction}.
\subsection{If-Expressions}\label{if-expressions}
An expression
\begin{lstlisting}[language=modelica]
if expression1 then expression2 else expression3
\end{lstlisting}%
\index{if@\robustinline{if}!expression}\index{then@\robustinline{then}!if-expression@\robustinline{if}-expression}\index{else@\robustinline{else}!if-expression@\robustinline{if}-expression} is one example of \lstinline!if!-expression.
First \lstinline!expression1!, which must be \lstinline!Boolean! expression, is evaluated.
If \lstinline!expression1! is true \lstinline!expression2! is evaluated and is the value of the \lstinline!if!-expression, else \lstinline!expression3! is evaluated and is the value of the \lstinline!if!-expression.
The two expressions, \lstinline!expression2! and \lstinline!expression3!, must be type compatible expressions (\cref{type-compatible-expressions}) giving the type of the \lstinline!if!-expression.
The \lstinline!if!-expressions with \lstinline!elseif!\index{elseif@\robustinline{elseif}!if-expression} are defined by replacing \lstinline!elseif! by \lstinline!else if!.
For short-circuit evaluation see \cref{evaluation-order}.
\begin{nonnormative}
\lstinline!elseif! in expressions has been added to the Modelica language for symmetry with \lstinline!if!-equations.
\end{nonnormative}
\begin{example}
\begin{lstlisting}[language=modelica]
Integer i;
Integer sign_of_i1 = if i < 0 then -1 elseif i == 0 then 0 else 1;
Integer sign_of_i2 = if i < 0 then -1 else if i == 0 then 0 else 1;
\end{lstlisting}
\end{example}
\subsection{Member Access Operator}\label{member-access-operator}
It is possible to access members of a class instance using dot notation, i.e., the \lstinline!.! operator.
\begin{example}
\lstinline!R1.R! for accessing the resistance component \lstinline!R! of resistor \lstinline!R1!.
Another use of dot notation: local classes which are members of a class can of course also be accessed using dot notation on the name of the class, not on instances of the class.
\end{example}
\subsection{Built-in Variable time}\label{built-in-variable-time}\indexinline{time}
All declared variables are functions of the independent variable \lstinline!time!.
The variable \lstinline!time! is a built-in variable available in all models and blocks, which is treated as an input variable.
It is implicitly defined as:
\begin{lstlisting}[language=modelica]
input Real time (final quantity = "Time",
final unit = "s");
\end{lstlisting}
The value of the \lstinline!start!-attribute of \lstinline!time! is set to the time instant at which the simulation is started.
\begin{example}
\begin{lstlisting}[language=modelica]
encapsulated model SineSource
import Modelica.Math.sin;
connector OutPort = output Real;
OutPort y = sin(time); // Uses the built-in variable time.
end SineSource;
\end{lstlisting}
\end{example}
\section{Built-in Intrinsic Operators with Function Syntax}\label{built-in-intrinsic-operators-with-function-syntax}
Certain built-in operators of Modelica have the same syntax as a function call.
However, they do not behave as a mathematical function, because the result depends not only on the input arguments but also on the status of the simulation.
There are also built-in functions that depend only on the input argument, but also may trigger events in addition to returning a value.
Intrinsic means that they are defined at the Modelica language level, not in the Modelica library.
The following built-in intrinsic operators/functions are available:
\begin{itemize}
\item
Mathematical functions and conversion functions, see \cref{numeric-functions-and-conversion-functions} below.
\item
Derivative and special purpose operators with function syntax, see \cref{derivative-and-special-purpose-operators-with-function-syntax} below.
\item
Event-related operators with function syntax, see \cref{event-related-operators-with-function-syntax} below.
\item
Array operators/functions, see \cref{array-dimension-lower-and-upper-index-bounds}.
\end{itemize}
Note that when the specification references a function having the name of a built-in function it references the built-in function, not a user-defined function having the same name, see also \cref{built-in-functions}.
With exception of the built-in \lstinline!String! operator, all operators in this section can only be called with positional arguments.
\subsection{Numeric Functions and Conversion Functions}\label{numeric-functions-and-conversion-functions}
The mathematical functions and conversion operators are listed below do not generate events.
\begin{center}
\begin{tabular}{l|l l}
\hline
\tablehead{Expression} & \tablehead{Description} & \tablehead{Details}\\
\hline
\hline
{\lstinline!abs($v$)!} & Absolute value (event-free) & \Cref{modelica:abs} \\
{\lstinline!sign($v$)!} & Sign of argument (event-free) & \Cref{modelica:sign} \\
{\lstinline!sqrt($v$)!} & Square root & \Cref{modelica:sqrt} \\
{\lstinline!Integer($e$)!} & Conversion from enumeration to {\lstinline!Integer!} & \Cref{modelica:integer-of-enumeration} \\
{\lstinline!EnumTypeName($i$)!} & Conversion from {\lstinline!Integer!} to enumeration & \Cref{modelica:enumeration-of-integer} \\
{\lstinline!String($\ldots$)!} & Conversion to {\lstinline!String!} & \Cref{modelica:to-String} \\
\hline
\end{tabular}
\end{center}
All of these except for the \lstinline!String! conversion operator are vectorizable according to \cref{scalar-functions-applied-to-array-arguments}.
Additional non-event generating mathematical functions are described in \cref{built-in-mathematical-functions-and-external-built-in-functions}, whereas the event-triggering mathematical functions are described in \cref{event-triggering-mathematical-functions}.
\begin{functiondefinition}[abs]
\begin{synopsis}\begin{lstlisting}
abs($v$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Expands into \lstinline!noEvent(if $v$ >= 0 then $v$ else -$v$)!.
Argument $v$ needs to be an \lstinline!Integer! or \lstinline!Real! expression.
\end{semantics}
\begin{nonnormative}
By not generating events the property \lstinline!abs($x$)! $\geq 0$ for all $x$ is ensured at the cost of sometimes having a derivative that changes discontinuously between events.
A typical case requiring the event-free semantics is a flow equation of the form \lstinline!abs(x) * x = y!.
With event generation, the equation would switch between the two forms \lstinline!x^2 = y! and \lstinline!-x^2 = y! at the events, where the events would not be coinciding exactly with the sign changes of \lstinline!y!.
When \lstinline!y! passes through zero, neither form of the equation would have a solution in an open neighborhood of \lstinline!y! $= 0$, and hence solving the equation would have to fail at some point sufficiently close to \lstinline!y! $= 0$.
Without event generation, on the other hand, the equation can be solved easily for \lstinline!x!, also as \lstinline!y! passes through zero.
Note that without event generation the derivative of \lstinline!abs(x) * x! never changes discontinuously, despite \lstinline!abs(x)! having a discontinuous derivative.
In inverted form this equation is \lstinline!x = sign(y) * sqrt(abs(y))!.
With event generation, the call to \lstinline!sqrt! would fail when applied to a negative number during root finding of the zero crossing for \lstinline!abs(y)!, compare \cref{events-and-synchronization}.
Without event generation, on the other hand, evaluating \lstinline!sqrt(abs(y))! will never fail.
\end{nonnormative}
\end{functiondefinition}
\begin{functiondefinition}[sign]
\begin{synopsis}\begin{lstlisting}
sign($v$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Expands into \lstinline!noEvent(if $v$ > 0 then 1 else if $v$ < 0 then -1 else 0)!.
Argument $v$ needs to be an \lstinline!Integer! or \lstinline!Real! expression.
\end{semantics}
\end{functiondefinition}
\begin{functiondefinition}[sqrt]
\begin{synopsis}\begin{lstlisting}
sqrt($v$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Square root of $v$ if $v \geq 0$, otherwise an error occurs.
Argument $v$ needs to be an \lstinline!Integer! or \lstinline!Real! expression.
\end{semantics}
\end{functiondefinition}
\begin{operatordefinition*}[Integer]\label{modelica:integer-of-enumeration}\index{Integer@\robustinline{Integer}!conversion operator}
\begin{synopsis}\begin{lstlisting}
Integer($e$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Ordinal number of the expression $e$ of enumeration type that evaluates to the enumeration value \lstinline!E.enumvalue!, where \lstinline!Integer(E.e1) = 1!, \lstinline!Integer(E.en) = n!, for an enumeration type \lstinline!E = enumeration(e1, $\ldots$, en)!.
See also \cref{type-conversion-of-enumeration-values-to-string-or-integer}.
\end{semantics}
\end{operatordefinition*}
\begin{operatordefinition*}[<EnumTypeName>]\label{modelica:enumeration-of-integer}\index{enumeration@\robustinline{enumeration}!conversion operator}
\begin{synopsis}\begin{lstlisting}
EnumTypeName($i$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
For any enumeration type \lstinline!EnumTypeName!, returns the enumeration value \lstinline!EnumTypeName.e! such that $\text{\lstinline!Integer(EnumTypeName.e)!} = i$.
Refer to the definition of \lstinline!Integer! above.
It is an error to attempt to convert values of $i$ that do not correspond to values of the enumeration type.
See also \cref{type-conversion-of-integer-to-enumeration-values}.
\end{semantics}
\end{operatordefinition*}
\begin{operatordefinition*}[String]\label{modelica:to-String}\index{String@\robustinline{String}!conversion operator}
\begin{synopsis}\begin{lstlisting}
String($b$, $\langle$$\mbox{\emph{options}}$$\rangle$)
String($i$, $\langle$$\mbox{\emph{options}}$$\rangle$)
String($i$, format = $s$)
String($r$, $\langle$$\mbox{\emph{options}}$$\rangle$)
String($r$, format = $s$)
String($e$, $\langle$$\mbox{\emph{options}}$$\rangle$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Convert a scalar non-\lstinline!String! expression to a \lstinline!String! representation.
The first argument may be a \lstinline!Boolean! $b$, an \lstinline!Integer! $i$, a \lstinline!Real! $r$, or an enumeration value $e$ (\cref{type-conversion-of-enumeration-values-to-string-or-integer}).
The $\langle\mathit{options}\rangle$ represent zero or more of the following named arguments (that cannot be passed as positional arguments):
\begin{itemize}
\item
\lstinline!Integer minimumLength = 0!: Minimum length of the resulting string.
If necessary, the blank character is used to fill up unused space.
\item
\lstinline!Boolean leftJustified = true!: If true, the converted result is left justified in the string; if false it is right justified in the string.
\item
\lstinline!Integer significantDigits = 6!: Number of significant digits in the result string.
Only allowed when formatting a \lstinline!Real! value.
\end{itemize}
The standard type coercion described in \cref{standard-type-coercion} shall not be applied for the first argument of \lstinline!String!.
Hence, specifying \lstinline!significantDigits! is an error when the first argument of \lstinline!String! is an \lstinline!Integer! expression.
For \lstinline!Real! expressions the output shall be according to the Modelica grammar.
\begin{nonnormative}
Examples of \lstinline!Real! values formatted with 6 significant digits: \emph{12.3456}, \emph{0.0123456}, \emph{12345600}, \emph{1.23456E-10}.
\end{nonnormative}
The \lstinline!format! string corresponding to $\langle\mathit{options}\rangle$ is:
\begin{itemize}
\item
For \lstinline!Real!:\\
\lstinline!(if leftJustified then "-" else "") + String(minimumLength)!\\
\lstinline! + "." + String(signficantDigits) + "g"!
\item
For \lstinline!Integer!:\\
\lstinline!(if leftJustified then "-" else "") + String(minimumLength) + "d"!
\end{itemize}
The ANSI-C style \lstinline!format! string (which cannot be combined with any of the other named arguments) consists of a single conversion specification without the leading \%.
It shall not contain a length modifier, and shall not use `\lstinline!*!' for width and/or precision.
For both \lstinline!Real! and \lstinline!Integer! values, the conversion specifiers `\lstinline!f!', `\lstinline!e!', `\lstinline!E!', `\lstinline!g!', `\lstinline!G!' are allowed.
For \lstinline!Integer! values it is also allowed to use the `\lstinline!d!', `\lstinline!i!', `\lstinline!o!', `\lstinline!x!', `\lstinline!X!', `\lstinline!u!', and `\lstinline!c!' conversion specifiers (for \lstinline!Real! values a tool may round, truncate or use a different format if the integer conversion specifiers are used).
The `\lstinline!x!'/`\lstinline!X!' formats (hexa-decimal) and \lstinline!c! (character) for \lstinline!Integer! values give results that do not agree with the Modelica grammar.
\begin{example}
Some situations worth a remark:
\begin{itemize}
\item
\lstinline!String(4.0, format = "g")! produces \emph{4} which is not a valid \lstinline!Real! literal.
However, it is an \lstinline!Integer! literal that can be used almost anywhere in Modelica code instead of the \lstinline!Real! literal \lstinline{4.0} (with the first argument to \lstinline!String! being a notable exception here).
\item
\lstinline!String(4, format = ".3f")! uses the \lstinline!Integer! case of \lstinline!String! since no automatic type coerction takes place for the first argument.
An implementation may internally convert the value to floating point and then fall back on the \lstinline!Real! case implementation of \lstinline!format = ".3f"!.
\item
\lstinline!String(4611686018427387648, format = ".0f")! (a valid \lstinline!Integer! value in an implementation with 64 bit \lstinline!IntegerType!) may produce \emph{4611686018427387904} (not equal to input value), in case internal conversion to a 64 bit \lstinline[language=C]!double! is applied.
\end{itemize}
\end{example}
\end{semantics}
\end{operatordefinition*}
\subsection{Event Triggering Mathematical Functions}\label{event-triggering-mathematical-functions}
The operators listed below trigger events if used outside of a \lstinline!when!-clause and outside of a clocked discrete-time partition (see \cref{clocked-discrete-time-and-clocked-discretized-continuous-time-partition}).
\begin{center}
\begin{tabular}{l|l l}
\hline
\tablehead{Expression} & \tablehead{Description} & \tablehead{Details}\\
\hline
\hline
{\lstinline!div($x$, $y$)!} & Division with truncation toward zero & \Cref{modelica:div} \\
{\lstinline!mod($x$, $y$)!} & Integer modulus & \Cref{modelica:mod} \\
{\lstinline!rem($x$, $y$)!} & Integer remainder & \Cref{modelica:rem} \\
{\lstinline!ceil($x$)!} & Smallest integer {\lstinline!Real!} not less than $x$ & \Cref{modelica:ceil} \\
{\lstinline!floor($x$)!} & Largest integer {\lstinline!Real!} not greater than $x$ & \Cref{modelica:floor} \\
{\lstinline!integer($x$)!} & Largest {\lstinline!Integer!} not greater than $x$ & \Cref{modelica:integer} \\
\hline
\end{tabular}
\end{center}
These expression for \lstinline!div!, \lstinline!ceil!, \lstinline!floor!, and \lstinline!integer! are event generating expression.
The event generating expression for \lstinline!mod(x,y)! is \lstinline!floor(x/y)!, and for \lstinline!rem(x,y)! it is \lstinline!div(x,y)! -- i.e.\ events are not generated when \lstinline!mod! or \lstinline!rem! changes continuously in an interval, but when they change discontinuously from one interval to the next.
\begin{nonnormative}
If this is not desired, the \lstinline!noEvent! operator can be applied to them.
E.g., \lstinline!noEvent(integer(v))!.
\end{nonnormative}
\begin{operatordefinition}[div]
\begin{synopsis}\begin{lstlisting}
div($x$, $y$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Algebraic quotient $x / y$ with any fractional part discarded (also known as truncation toward zero).
\begin{nonnormative}
This is defined for \lstinline!/! in C99; in C89 the result for negative numbers is implementation-defined, so the standard function \lstinline[language=C]!div! must be used.
\end{nonnormative}
Result and arguments shall have type \lstinline!Real! or \lstinline!Integer!.
If either of the arguments is \lstinline!Real! the result is \lstinline!Real! otherwise \lstinline!Integer!.
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[mod]
\begin{synopsis}\begin{lstlisting}
mod($x$, $y$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Integer modulus of $x / y$, i.e., \lstinline!mod($x$, $y$) = $x$ - floor($x$ / $y$) * $y$!.
Result and arguments shall have type \lstinline!Real! or \lstinline!Integer!.
If either of the arguments is \lstinline!Real! the result is \lstinline!Real! otherwise \lstinline!Integer!.
\begin{nonnormative}
Note, outside of a \lstinline!when!-clause state events are triggered when the return value changes discontinuously.
Examples: \lstinline!mod(3, 1.4) = 0.2!, \lstinline!mod(-3, 1.4) = 1.2!, \lstinline!mod(3, -1.4) = -1.2!.
\end{nonnormative}
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[rem]
\begin{synopsis}\begin{lstlisting}
rem($x$, $y$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Integer remainder of $x / y$, such that \lstinline!div($x$, $y$) * $y$ + rem($x$, $y$) = $x$!.
Result and arguments shall have type \lstinline!Real! or \lstinline!Integer!.
If either of the arguments is \lstinline!Real! the result is \lstinline!Real! otherwise \lstinline!Integer!.
\begin{nonnormative}
Note, outside of a \lstinline!when!-clause state events are triggered when the return value changes discontinuously.
Examples: \lstinline!rem(3, 1.4) = 0.2!, \lstinline!rem(-3, 1.4) = -0.2!.
\end{nonnormative}
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[ceil]
\begin{synopsis}\begin{lstlisting}
ceil($x$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Smallest integer not less than $x$.
Result and argument shall have type \lstinline!Real!.
\begin{nonnormative}
Note, outside of a \lstinline!when!-clause state events are triggered when the return value changes discontinuously.
\end{nonnormative}
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[floor]
\begin{synopsis}\begin{lstlisting}
floor($x$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Largest integer not greater than $x$.
Result and argument shall have type \lstinline!Real!.
\begin{nonnormative}
Note, outside of a \lstinline!when!-clause state events are triggered when the return value changes discontinuously.
\end{nonnormative}
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[integer]
\begin{synopsis}\begin{lstlisting}
integer($x$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Largest integer not greater than $x$.
The argument shall have type \lstinline!Real!.
The result has type \lstinline!Integer!.
\begin{nonnormative}
Note, outside of a \lstinline!when!-clause state events are triggered when the return value changes discontinuously.
\end{nonnormative}
\end{semantics}
\end{operatordefinition}
\subsection{Elementary Mathematical Functions}\label{built-in-mathematical-functions-and-external-built-in-functions}
The functions listed below are elementary mathematical functions.
Tools are expected to utilize well known properties of these functions (derivatives, inverses, etc) for symbolic processing of expressions and equations.
\begin{center}
\begin{tabular}{l|l l}
\hline
\tablehead{Expression} & \tablehead{Description} & \tablehead{Details}\\
\hline
\hline
{\lstinline!sin($x$)!} \indexinline{sin} & Sine & \\
{\lstinline!cos($x$)!} \indexinline{cos} & Cosine & \\
{\lstinline!tan($x$)!} \indexinline{tan} & Tangent ($x$ shall not be: $\ldots$, -$\pi/2$, $\pi/2$, $3\pi/2$, $\ldots$) & \\
{\lstinline!asin($x$)!} \indexinline{asin} & Inverse sine ($-1 \le x \le 1$) & \\
{\lstinline!acos($x$)!} \indexinline{acos} & Inverse cosine ($-1 \le x \le 1$) & \\
{\lstinline!atan($x$)!} \indexinline{atan} & Inverse tangent & \\
{\lstinline!atan2($y$, $x$)!} \indexinline{atan2} & Principal value of the arc tangent of $y/x$ & \Cref{modelica:atan2} \\
{\lstinline!sinh($x$)!} \indexinline{sinh} & Hyperbolic sine & \\
{\lstinline!cosh($x$)!} \indexinline{cosh} & Hyperbolic cosine & \\
{\lstinline!tanh($x$)!} \indexinline{tanh} & Hyperbolic tangent & \\
{\lstinline!exp($x$)!} \indexinline{exp} & Exponential, base $\mathrm{e}$ & \\
{\lstinline!log($x$)!} \indexinline{log} & Natural (base $\mathrm{e}$) logarithm ($x > 0$) & \\
{\lstinline!log10($x$)!} \indexinline{log10} & Base 10 logarithm ($x > 0$) & \\
\hline
\end{tabular}
\end{center}
These functions are the only ones that can also be called using the deprecated \lstinline!"builtin"! external language, see \cref{external-function-interface}.
\begin{nonnormative}
End user oriented information about the elementary mathematical functions can be found for the corresponding functions in the \lstinline!Modelica.Math! package.
\end{nonnormative}
\begin{functiondefinition}[atan2]
\begin{synopsis}\begin{lstlisting}
atan2($y$, $x$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Principal value of the arc tangent of $y/x$, using the signs of the two arguments to determine the quadrant of the result.
The result $\varphi$ is in the interval $\left[-\pi,\, \pi\right]$ and satisfies:
\begin{equation*}
\begin{aligned}
\abs{(x,\, y)}\, \cos(\varphi) &= x\\
\abs{(x,\, y)}\, \sin(\varphi) &= y
\end{aligned}
\end{equation*}
\end{semantics}
\end{functiondefinition}
\subsection{Derivative and Special Purpose Operators with Function Syntax}\label{derivative-and-special-purpose-operators-with-function-syntax}
The operators listed below include the derivative operator and special purpose operators with function syntax.
\begin{center}
\begin{tabular}{l|l l}
\hline
\tablehead{Expression} & \tablehead{Description} & \tablehead{Details}\\
\hline
\hline
{\lstinline!der($\mathit{expr}$)!} & Time derivative & \Cref{modelica:der} \\
{\lstinline!delay($\mathit{expr}$, $\ldots$)!} & Time delay & \Cref{modelica:delay} \\
{\lstinline!cardinality($c$)!} & Number of occurrences in {\lstinline!connect!}-equations & \Cref{modelica:cardinality} \\
{\lstinline!homotopy($\mathit{actual}$, $\mathit{simplified}$)!} & Homotopy initialization & \Cref{modelica:homotopy} \\
{\lstinline!semiLinear($x$, $k^{+}$, $k^{-}$)!} & Sign-dependent slope & \Cref{modelica:semiLinear} \\
{\lstinline!inStream($v$)!} & Stream variable flow into component & \Cref{modelica:inStream} \\
{\lstinline!actualStream($v$)!} & Actual value of stream variable & \Cref{modelica:actualStream} \\
{\lstinline!spatialDistribution($\ldots$)!} & Variable-speed transport & \Cref{modelica:spatialDistribution} \\
{\lstinline!getInstanceName()!} & Name of instance at call site & \Cref{modelica:getInstanceName} \\
\hline
\end{tabular}
\end{center}
The special purpose operators with function syntax where the call below uses named arguments can be called with named arguments (with the specified names), or with positional arguments (the inputs of the functions are in the order given in the calls below).
\begin{operatordefinition}[der]
\begin{synopsis}\begin{lstlisting}
der($\mathit{expr}$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
The time derivative of $\mathit{expr}$.
If the expression $\mathit{expr}$ is a scalar it needs to be a subtype of \lstinline!Real!.
The expression and all its time-varying subexpressions must be continuous and semi-differentiable.
If $\mathit{expr}$ is an array, the operator is applied to all elements of the array.
For non-scalar arguments the function is vectorized according to \cref{vectorized-calls-of-functions}.
\begin{nonnormative}
For \lstinline!Real! parameters and constants the result is a zero scalar or array of the same size as the variable.
\end{nonnormative}
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[delay]
\begin{synopsis}\begin{lstlisting}
delay($\mathit{expr}$, $\mathit{delayTime}$, $\mathit{delayMax}$)
delay($\mathit{expr}$, $\mathit{delayTime}$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Evaluates to \lstinline!$\mathit{expr}$(time - $\mathit{delayTime}$)! for $\text{\lstinline!time!} > \text{\lstinline!time.start!} + \mathit{delayTime}$ and \lstinline!$\mathit{expr}$(time.start)! for $\text{\lstinline!time!} \leq \text{\lstinline!time.start!} + \mathit{delayTime}$.
The arguments, i.e., $\mathit{expr}$, $\mathit{delayTime}$ and $\mathit{delayMax}$, need to be subtypes of \lstinline!Real!.
$\mathit{delayMax}$ needs to be additionally a parameter expression.
The following relation shall hold: $0 \leq \mathit{delayTime} \leq \mathit{delayMax}$, otherwise an error occurs.
If $\mathit{delayMax}$ is not supplied in the argument list, $\mathit{delayTime}$ needs to be a parameter expression.
For non-scalar arguments the function is vectorized according to \cref{vectorized-calls-of-functions}.
For further details, see \cref{delay}.
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[cardinality]
\begin{synopsis}\begin{lstlisting}
cardinality($c$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
\begin{nonnormative}
This is a deprecated operator.
It should no longer be used, since it will be removed in one of the next Modelica releases.
\end{nonnormative}
Returns the number of (inside and outside) occurrences of connector instance $c$ in a \lstinline!connect!-equation as an \lstinline!Integer! number.
For further details, see \cref{cardinality-deprecated}.
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[homotopy]
\begin{synopsis}\begin{lstlisting}
homotopy(actual = $\mathit{actual}$, simplified = $\mathit{simplified}$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
The scalar expressions $\mathit{actual}$ and $\mathit{simplified}$ are subtypes of \lstinline!Real!.
A Modelica translator should map this operator into either of the two forms:
\begin{enumerate}
\item
Returns $\mathit{actual}$ (trivial implementation).
\item
In order to solve algebraic systems of equations, the operator might during the solution process return a combination of the two arguments, ending at actual.
\begin{example}
$\mathit{actual} \cdot \lambda + \mathit{simplified} \cdot (1 - \lambda)$, where $\lambda$ is a homotopy parameter going from 0 to 1.
\end{example}
The solution must fulfill the equations for \lstinline!homotopy! returning $\mathit{actual}$.
\end{enumerate}
For non-scalar arguments the function is vectorized according to \cref{scalar-functions-applied-to-array-arguments}.
For further details, see \cref{homotopy}.
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[semiLinear]
\begin{synopsis}\begin{lstlisting}
semiLinear($x$, $k^{+}$, $k^{-}$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
Returns: \lstinline!smooth(0, if $x$ >= 0 then $k^{+}$ * $x$ else $k^{-}$ * $x$)!.
The result is of type \lstinline!Real!.
For non-scalar arguments the function is vectorized according to \cref{vectorized-calls-of-functions}.
For further details, see \cref{semilinear} (especially in the case when $x = 0$).
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[inStream]
\begin{synopsis}\begin{lstlisting}
inStream($v$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
\lstinline!inStream($v$)! is only allowed for stream variables $v$ defined in stream connectors, and is the value of the stream variable $v$ close to the connection point assuming that the flow is from the connection point into the component.
This value is computed from the stream connection equations of the flow variables and of the stream variables.
The operator is vectorizable.
For further details, see \cref{stream-operator-instream-and-connection-equations}.
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[actualStream]
\begin{synopsis}\begin{lstlisting}
actualStream($v$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
\lstinline!actualStream($v$)! returns the actual value of the stream variable $v$ for any flow direction.
The operator is vectorizable.
For further details, see \cref{stream-operator-actualstream}.
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[spatialDistribution]
\begin{synopsis}\begin{lstlisting}
spatialDistribution(
in0 = $\mathit{in0}$, in1 = $\mathit{in1}$, x = $x$,
positiveVelocity = $\ldots$,
initialPoints = $\ldots$,
initialValues = $\ldots$)
\end{lstlisting}\end{synopsis}
\begin{semantics}
\lstinline!spatialDistribution! allows approximation of variable-speed transport of properties.
For further details, see \cref{spatialdistribution}.
\end{semantics}
\end{operatordefinition}
\begin{operatordefinition}[getInstanceName]
\begin{synopsis}\begin{lstlisting}
getInstanceName()
\end{lstlisting}\end{synopsis}
\begin{semantics}
Returns a string with the name of the model/block that is simulated, appended with the fully qualified name of the instance in which this function is called.
For further details, see \cref{getinstancename}.
\end{semantics}
\end{operatordefinition}
A few of these operators are described in more detail in the following.
\subsubsection{delay}\label{delay}
\begin{nonnormative}
\lstinline!delay! allows a numerical sound implementation by interpolating in the (internal) integrator polynomials, as well as a more simple realization by interpolating linearly in a buffer containing past values of expression $\mathit{expr}$.
Without further information, the complete time history of the delayed signals needs to be stored, because the delay time may change during simulation.
To avoid excessive storage requirements and to enhance efficiency, the maximum allowed delay time has to be given via $\mathit{delayMax}$.
This gives an upper bound on the values of the delayed signals which have to be stored.
For real-time simulation where fixed step size integrators are used, this information is sufficient to allocate the necessary storage for the internal buffer before the simulation starts.
For variable step size integrators, the buffer size is dynamic during integration.
In principle, \lstinline!delay! could break algebraic loops.
For simplicity, this is not supported because the minimum delay time has to be given as additional argument to be fixed at compile time.
Furthermore, the maximum step size of the integrator is limited by this minimum delay time in order to avoid extrapolation in the delay buffer.
\end{nonnormative}
\subsubsection{spatialDistribution}\label{spatialdistribution}
\begin{nonnormative}
Many applications involve the modelling of variable-speed transport of properties.
One option to model this infinite-dimensional system is to approximate it by an ODE, but this requires a large number of state variables and might introduce either numerical diffusion or numerical oscillations.
Another option is to use a built-in operator that keeps track of the spatial distribution of $z(x, t)$, by suitable sampling, interpolation, and shifting of the stored distribution.
In this case, the internal state of the operator is hidden from the ODE solver.
\end{nonnormative}
\lstinline!spatialDistribution! allows the infinite-dimensional problem below to be solved efficiently with good accuracy
\begin{align*}
\frac{\partial z(x,t)}{\partial t}+v(t)\frac{\partial z(x,t)}{\partial x} &= 0.0\\
z(0.0, t) &= \mathrm{in}_0(t) \text{ if $v\geq 0$}\\
z(1.0, t) &= \mathrm{in}_1(t) \text{ if $v<0$}
\end{align*}
where $z(x, t)$ is the transported quantity, $x$ is the normalized spatial coordinate ($0.0 \le x \le 1.0$), $t$ is the time, $v(t)=\mathrm{der}(x)$ is the normalized transport velocity and the boundary conditions are set at either $x=0.0$ or $x=1.0$, depending on the sign of the velocity.
The calling syntax is:
\begin{lstlisting}[language=modelica]
(out0, out1) = spatialDistribution(in0, in1, x, positiveVelocity,
initialPoints = {0.0, 1.0},
initialValues = {0.0, 0.0});
\end{lstlisting}
where \lstinline!in0!, \lstinline!in1!, \lstinline!out0!, \lstinline!out1!, and \lstinline!x! are all subtypes of \lstinline!Real!, \lstinline!positiveVelocity! is a \lstinline!Boolean!, \lstinline!initialPoints! and \lstinline!initialValues! are arrays of subtypes of \lstinline!Real! of equal size, containing the x coordinates and the $z$ values of a finite set of points describing the initial distribution of $z(x, \mathit{t0})$.
The \lstinline!out0! and \lstinline!out1! are given by the solutions at $z(0.0, t)$ and $z(1.0, t)$; and \lstinline!in0! and \lstinline!in1! are the boundary conditions at $z(0.0, t)$ and $z(1.0, t)$ (at each point in time only one of \lstinline!in0! and \lstinline!in1! is used).
Elements in the \lstinline!initialPoints! array must be sorted in non-descending order.
The operator can not be vectorized according to the vectorization rules described in \cref{scalar-functions-applied-to-array-arguments}.
The operator can be vectorized only with respect to the arguments \lstinline!in0! and \lstinline!in1! (which must have the same size), returning vectorized outputs \lstinline!out0! and \lstinline!out1! of the same size; the arguments \lstinline!initialPoints! and \lstinline!initialValues! are vectorized accordingly.
The solution, $z$, can be described in terms of characteristics:
\begin{equation*}
z(x+\int_{t}^{t+\beta} v(\alpha) \mathrm{d}\alpha, t+\beta) = z(x, t),\quad\text{for all $\beta$ as long as staying inside the domain}
\end{equation*}
This allows the direct computation of the solution based on interpolating the boundary conditions.
\lstinline!spatialDistribution! can be described in terms of the pseudo-code given as a block:
\begin{lstlisting}[language=modelica]
block spatialDistribution
input Real in0;
input Real in1;
input Real x;
input Boolean positiveVelocity;
parameter Real initialPoints(each min=0, each max=1)[:] = {0.0, 1.0};
parameter Real initialValues[:] = {0.0, 0.0};
output Real out0;
output Real out1;
protected
Real points[:];
Real values[:];
Real x0;
Integer m;
algorithm
/* The notation
* x <and then> y
* is used below as a shorthand for
* if x then y else false
* also known as "short-circuit evaluation of x and y".
*/
if positiveVelocity then
out1 := interpolate(points, values, 1 - (x - x0));
out0 := values[1]; // Similar to in0 but avoiding algebraic loop.
else
out0 := interpolate(points, values, 0 - (x - x0));
out1 := values[end]; // Similar to in1 but avoiding algebraic loop.
end if;
when <acceptedStep> then
if x > x0 then
m := size(points, 1);
while m > 0 <and then> points[m] + (x - x0) >= 1 loop
m := m - 1;
end while;
values := cat(1,
{in0},
values[1:m],
{interpolate(points, values, 1 - (x - x0))});
points := cat(1, {0}, points[1:m] .+ (x-x0), {1});
elseif x < x0 then
m := 1;
while m < size(points, 1) <and then> points[m] + (x - x0) <= 0 loop
m := m + 1;
end while;
values := cat(1,
{interpolate(points, values, 0 - (x - x0))},
values[m:end],
{in1});
points := cat(1, {0}, points[m:end] .+ (x - x0), {1});
end if;
x0 := x;
end when;
initial algorithm
x0 := x;
points := initialPoints;
values := initialValues;
end spatialDistribution;
\end{lstlisting}
\begin{nonnormative}
Note that the implementation has an internal state and thus cannot be described as a function in Modelica; \lstinline!initialPoints! and \lstinline!initialValues! are declared as parameters to indicate that they are only used during initialization.
The infinite-dimensional problem stated above can then be formulated in the following way:
\begin{lstlisting}[language=modelica]
der(x) = v;
(out0, out1) = spatialDistribution(in0, in1, x, v >= 0,
initialPoints, initialValues);
\end{lstlisting}
Events are generated at the exact instants when the velocity changes sign -- if this is not needed, \lstinline!noEvent! can be used to suppress event generation.
If the velocity is known to be always positive, then \lstinline!out0! can be omitted, e.g.:
\begin{lstlisting}[language=modelica]
der(x) = v;