-
Notifications
You must be signed in to change notification settings - Fork 41
/
operatorsandexpressions.tex
1508 lines (1328 loc) · 72.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 expressions according to the rules given by the expression part of
the Modelica grammar in \cref{modelica-concrete-syntax}.
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 the Table 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 order of evaluation of operators in
an expression. An operator with higher precedence is evaluated before an
operator with lower precedence in the same expression.
The following table presents all the expression operators in order of precedence.
\begin{table}[H]
% 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
\caption{Operators in order of precedence from highest to lowest, as derived from the Modelica grammar in \cref{modelica-concrete-syntax}. All operators are binary except the postfix operators and those shown as unary together with \emph{expr}, the conditional operator, the array construction operator \lstinline!\{ \}! and concatenation operator \lstinline![ ]!, and the array range constructor which is either binary or ternary. Operators with the same precedence occur at the same table row.}\label{tab:operator-precedence}
\begin{center}
\begin{tabular}{l l l}
\hline
\tablehead{Operator group} & \tablehead{Operator syntax} & \tablehead{Examples}\\
\hline
\hline
Postfix array index operator & \lstinline![]! & \lstinline!arr[index]!\\
\hline
Postfix access operator & \lstinline!.! & \lstinline!a.b!\\
\hline
Postfix function call & \lstinline!$\mathit{funcName}$($\mathit{functionArguments}$)! & \lstinline!sin(4.36)!\\
\hline
Array construction & \lstinline!{$\mathit{expressions}$}! & \lstinline!{2, 3}!\\
Horizontal concatenation & \lstinline![$\mathit{expressions}$]! & \lstinline![5, 6]!\\
Vertical concatenation & \lstinline![$\mathit{expressions}$; $\mathit{expressions}\ldots$]! & \lstinline![2, 3; 7, 8]!\\
\hline
Exponentiation & \ \lstinline!^! & \lstinline!2 ^ 3!\\
\hline
Multiplicative & \lstinline!* /! & \lstinline!2 * 3!, \lstinline!2 / 3!\\
Elementwise multiplicative & \lstinline!.* ./! & \lstinline![1, 2; 3, 4] .* [2, 3; 5, 6]!\\
\hline
Additive & \lstinline!+ -! & \lstinline!1 + 2!\\
Additive unary & \lstinline!+$\mathit{expr}$ -$\mathit{expr}$! & \lstinline!-0.5!\\
Array elementwise additive & \lstinline!.+ .-! & \lstinline![1, 2; 3, 4] .+ [2, 3; 5, 6]!\\
\hline
Relational & \lstinline!< <= > >= == <>! & \lstinline!a < b!, \lstinline!a <= b!, \lstinline!a > b!, \ldots\\
\hline
Unary negation & \lstinline!not $\mathit{expr}$! & \lstinline!not b1!\\
\hline
Logical and & \lstinline!and! & \lstinline!b1 and b2!\\
\hline
Logical or & \lstinline!or! & \lstinline!b1 or b2!\\
\hline
\multirow{2}{*}{Array range} & \lstinline!$\mathit{expr}$ : $\mathit{expr}$! & \lstinline!1 : 5!\\
& \lstinline!$\mathit{expr}$ : $\mathit{expr}$ : $\mathit{expr}$! & \lstinline!start : step : stop!\\
\hline
Conditional & \lstinline!if $\mathit{expr}$ then $\mathit{expr}$ else $\mathit{expr}$! & \lstinline!if b then 3 else x!\\
\hline
Named argument & \lstinline!$\mathit{ident}$ = $\mathit{expr}$! & \lstinline!x = 2.26!\\
\hline
\end{tabular}
\end{center}
\end{table}
The conditional operator may also include elseif-clauses. Equality \lstinline!=! and
assignment \lstinline!:=! are not expression operators since they are allowed only
in equations and in assignment statements respectively. All binary
expression operators are left associative, except exponentiation which
is non-associative. The array range operator is non-associative.
\begin{nonnormative}
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):
\begin{lstlisting}[language=modelica]
2*-2 // = -4 in Mathematica/MATLAB; is illegal in Modelica
--2 // = 2 in Mathematica/MATLAB; is illegal in Modelica
++2 // = 2 in Mathematica/MATLAB; is illegal in Modelica
2--2 // = 4 in Mathematica/MATLAB; is illegal in Modelica
\end{lstlisting}
Non-associative exponentiation and array range operator:
\begin{lstlisting}[language=modelica]
x^y^z // Not legal, use parenthesis to make it clear
a:b:c:d:e:f:g // Not legal, and scalar arguments gives no legal interpretation.
\end{lstlisting}
\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). If-statements and if-expressions guarantee that their clauses 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.
\subsection{Example: Guarding Expressions Against Incorrect Evaluation}\label{example-guarding-expressions-against-incorrect-evaluation}
\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]; // Invalid
b=if (I>=1 and I<=n) then v[I] else false; // Correct
\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 Events, \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}\index{else@\robustinline{else}!if-expression}
is one example of 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 if-expression, else \lstinline!expression3! is evaluated and is the value of the if-expression.
The two expressions, \lstinline!expression2! and \lstinline!expression3!, must be type compatible expressions (\cref{type-compatible-expressions}) giving the type of the if-expression.
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 if-clauses.
\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}
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}
\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, ..., 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$, <options>)
String($i$, <options>)
String($r$, significantDigits=$d$, <options>)
String($r$, format=$s$)
String($e$, <options>)
\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 other arguments must use named arguments. For \lstinline!Real! expressions the output shall be according to the Modelica grammar.
The optional \lstinline!<options>! are:
\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.
\end{itemize}
\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 \lstinline!<options>! 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}
Form of the \lstinline!format! string: According to ANSI-C the format string specifies one conversion specifier (excluding the leading \%), shall not contain length modifiers, and shall not use `\lstinline!*!' for width and/or precision. For all numeric values the format specifiers `\lstinline!f!', `\lstinline!e!', `\lstinline!E!', `\lstinline!g!', `\lstinline!G!' are allowed. For integral values it is also allowed to use the `\lstinline!d!', `\lstinline!i!', `\lstinline!o!', `\lstinline!x!', `\lstinline!X!', `\lstinline!u!', and `\lstinline!c!' format specifiers (for non-integral values a tool may round, truncate or use a different format if the integer conversion characters 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.
\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 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 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 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 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 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 when-clause state events are triggered when the return value changes discontinuously.
\end{nonnormative}
\end{semantics}
\end{operatordefinition}
\subsection{Built-in Mathematical Functions and External Built-in Functions}\label{built-in-mathematical-functions-and-external-built-in-functions}
The functions listed below are built-in mathematical functions are available in Modelica
and can be called directly without any package prefix added to the
function name. They are also available as external built-in functions in
the \lstinline!Modelica.Math! library.
\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}
\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 connect-equations & \Cref{modelica:cardinality} \\
\lstinline!homotopy($\mathit{actual}$, $\mathit{simplified}$)! & Homotpy 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 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 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 give 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(y,t)}{\partial t}+v(t)\frac{\partial z(y,t)}{\partial y} &= 0.0\\
z(0.0, t) &= \mathrm{in}_0(t) \text{ if $v\ge 0$}\\
z(1.0, t) &= \mathrm{in}_1(t) \text{ if $v<0$}
\end{align*}
where $z(y, t)$ is the transported quantity, $y$ is the
normalized spatial coordinate ($0.0 \le y \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
$y=0.0$ or $y=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!, \lstinline!x!, \lstinline!v! 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 y coordinates and the $z$ values of a finite set of points describing the initial distribution of $z(y, \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(y+\int_{t}^{t+\beta} v(\alpha) \mathrm{d}\alpha, t+\beta) = z(y, 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;
(, out1) = spatialDistribution(in0, 0, x, true, initialPoints, initialValues);
\end{lstlisting}
Technically relevant use cases for the use of \lstinline!spatialDistribution! are modeling of electrical transmission lines, pipelines and pipeline networks for gas, water and district heating, sprinkler systems, impulse propagation in elongated bodies, conveyor belts, and hydraulic systems. Vectorization is needed for pipelines where more than one quantity is transported with velocity \lstinline!v! in the example above.
\end{nonnormative}
\subsubsection{cardinality (deprecated)}\label{cardinality-deprecated}
\begin{nonnormative}
\lstinline!cardinality! is deprecated for the following reasons and will be removed in a future release:
\begin{itemize}
\item
Reflective operator may make early type checking more difficult.
\item
Almost always abused in strange ways
\item
Not used for Bond graphs even though it was originally introduced for that purpose.
\end{itemize}
\end{nonnormative}
\begin{nonnormative}
\lstinline!cardinality! allows the definition of connection dependent equations in a model, for example:
\begin{lstlisting}[language=modelica]
connector Pin
Real v;
flow Real i;
end Pin;
model Resistor
Pin p, n;
equation
assert(cardinality(p) > 0 and cardinality(n) > 0,
"Connectors p and n of Resistor must be connected");
// Equations of resistor
...
end Resistor;
\end{lstlisting}
\end{nonnormative}
The cardinality is counted after removing conditional components, and shall not be applied to expandable connectors, elements in expandable connectors, or to arrays of connectors (but can be applied to
the scalar elements of array of connectors). \lstinline!cardinality! should only be used in the condition of assert and if-statements that do not contain \lstinline!connect! and similar operators,
see \cref{clocked-discrete-time-and-clocked-discretized-continuous-time-partition}).
\subsubsection{homotopy}\label{homotopy}
\begin{nonnormative}
During the initialization phase of a dynamic simulation
problem, it often happens that large nonlinear systems of equations must
be solved by means of an iterative solver. The convergence of such
solvers critically depends on the choice of initial guesses for the
unknown variables. The process can be made more robust by providing an
alternative, simplified version of the model, such that convergence is
possible even without accurate initial guess values, and then by
continuously transforming the simplified model into the actual model.
This transformation can be formulated using expressions of this kind:
\begin{equation*}
\lambda\cdot\text{\lstinline!actual!} + (1-\lambda)\cdot\text{\lstinline!simplified!}
\end{equation*}
in the formulation of the system equations, and is usually called
a homotopy transformation. If the simplified expression is chosen
carefully, the solution of the problem changes continuously with $\lambda$,
so by taking small enough steps it is possible to eventually obtain the
solution of the actual problem.
The operator can be called with ordered arguments or preferably
with named arguments for improved readability.
It is recommended to perform (conceptually) one homotopy iteration
over the whole model, and not several homotopy iterations over the
respective non-linear algebraic equation systems. The reason is that the
following structure can be present:
\begin{lstlisting}[language=modelica]
w = $f_1$(x) // has homotopy
0 = $f_2$(der(x), x, z, w)
\end{lstlisting}
Here, a non-linear equation system $f_2$
is present. \lstinline!homotopy! is, however used on a variable
that is an ``input'' to the non-linear algebraic equation system, and
modifies the characteristics of the non-linear algebraic equation
system. The only useful way is to perform the homotopy iteration over
$f_1$ and $f_2$ together.
The suggested approach is ``conceptual'', because more efficient
implementations are possible, e.g.\ by determining the smallest iteration
loop, that contains the equations of the first BLT block in which
\lstinline!homotopy! is present and all equations up to the last BLT block
that describes a non-linear algebraic equation system.
A trivial implementation of \lstinline!homotopy! is obtained by
defining the following function in the global scope:
\begin{lstlisting}[language=modelica]
function homotopy
input Real actual;
input Real simplified;