% Generated by Sphinx.
\def\sphinxdocclass{report}
\documentclass[letterpaper,10pt,english]{sphinxmanual}
\usepackage[utf8x]{inputenc}
\usepackage{cmap}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage{times}
\usepackage[Bjarne]{fncychap}
\usepackage{longtable}
\usepackage{sphinx}
\usepackage{multirow}

\addto\captionsenglish{\renewcommand{\figurename}{Figure }}
\addto\captionsenglish{\renewcommand{\tablename}{Table }}
\floatname{literal-block}{Listing }



\title{Modelica Language Specification Documentation}
\date{June 09, 2015}
\release{3.3 Revision 1 (+ Sphinx conversion)}
\author{Modelica Association}
\newcommand{\sphinxlogo}{}
\renewcommand{\releasename}{Release}
\makeindex

\makeatletter
\def\PYG@reset{\let\PYG@it=\relax \let\PYG@bf=\relax%
    \let\PYG@ul=\relax \let\PYG@tc=\relax%
    \let\PYG@bc=\relax \let\PYG@ff=\relax}
\def\PYG@tok#1{\csname PYG@tok@#1\endcsname}
\def\PYG@toks#1+{\ifx\relax#1\empty\else%
    \PYG@tok{#1}\expandafter\PYG@toks\fi}
\def\PYG@do#1{\PYG@bc{\PYG@tc{\PYG@ul{%
    \PYG@it{\PYG@bf{\PYG@ff{#1}}}}}}}
\def\PYG#1#2{\PYG@reset\PYG@toks#1+\relax+\PYG@do{#2}}

\expandafter\def\csname PYG@tok@gd\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}}
\expandafter\def\csname PYG@tok@gu\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}}
\expandafter\def\csname PYG@tok@gt\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}}
\expandafter\def\csname PYG@tok@gs\endcsname{\let\PYG@bf=\textbf}
\expandafter\def\csname PYG@tok@gr\endcsname{\def\PYG@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}}
\expandafter\def\csname PYG@tok@cm\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}}
\expandafter\def\csname PYG@tok@vg\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}}
\expandafter\def\csname PYG@tok@m\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@mh\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@cs\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}\def\PYG@bc##1{\setlength{\fboxsep}{0pt}\colorbox[rgb]{1.00,0.94,0.94}{\strut ##1}}}
\expandafter\def\csname PYG@tok@ge\endcsname{\let\PYG@it=\textit}
\expandafter\def\csname PYG@tok@vc\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}}
\expandafter\def\csname PYG@tok@il\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@go\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.20,0.20,0.20}{##1}}}
\expandafter\def\csname PYG@tok@cp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@gi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}}
\expandafter\def\csname PYG@tok@gh\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}}
\expandafter\def\csname PYG@tok@ni\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.84,0.33,0.22}{##1}}}
\expandafter\def\csname PYG@tok@nl\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.13,0.44}{##1}}}
\expandafter\def\csname PYG@tok@nn\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.05,0.52,0.71}{##1}}}
\expandafter\def\csname PYG@tok@no\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.38,0.68,0.84}{##1}}}
\expandafter\def\csname PYG@tok@na\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@nb\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@nc\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.05,0.52,0.71}{##1}}}
\expandafter\def\csname PYG@tok@nd\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.33,0.33,0.33}{##1}}}
\expandafter\def\csname PYG@tok@ne\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@nf\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.02,0.16,0.49}{##1}}}
\expandafter\def\csname PYG@tok@si\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.44,0.63,0.82}{##1}}}
\expandafter\def\csname PYG@tok@s2\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@vi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}}
\expandafter\def\csname PYG@tok@nt\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.02,0.16,0.45}{##1}}}
\expandafter\def\csname PYG@tok@nv\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.38,0.84}{##1}}}
\expandafter\def\csname PYG@tok@s1\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@gp\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.78,0.36,0.04}{##1}}}
\expandafter\def\csname PYG@tok@sh\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@ow\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@sx\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.78,0.36,0.04}{##1}}}
\expandafter\def\csname PYG@tok@bp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@c1\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}}
\expandafter\def\csname PYG@tok@kc\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@c\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.50,0.56}{##1}}}
\expandafter\def\csname PYG@tok@mf\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@err\endcsname{\def\PYG@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}}
\expandafter\def\csname PYG@tok@mb\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@ss\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.32,0.47,0.09}{##1}}}
\expandafter\def\csname PYG@tok@sr\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.14,0.33,0.53}{##1}}}
\expandafter\def\csname PYG@tok@mo\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@kd\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@mi\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.13,0.50,0.31}{##1}}}
\expandafter\def\csname PYG@tok@kn\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@o\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}}
\expandafter\def\csname PYG@tok@kr\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@s\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@kp\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@w\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}}
\expandafter\def\csname PYG@tok@kt\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.56,0.13,0.00}{##1}}}
\expandafter\def\csname PYG@tok@sc\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@sb\endcsname{\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@k\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.00,0.44,0.13}{##1}}}
\expandafter\def\csname PYG@tok@se\endcsname{\let\PYG@bf=\textbf\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}
\expandafter\def\csname PYG@tok@sd\endcsname{\let\PYG@it=\textit\def\PYG@tc##1{\textcolor[rgb]{0.25,0.44,0.63}{##1}}}

\def\PYGZbs{\char`\\}
\def\PYGZus{\char`\_}
\def\PYGZob{\char`\{}
\def\PYGZcb{\char`\}}
\def\PYGZca{\char`\^}
\def\PYGZam{\char`\&}
\def\PYGZlt{\char`\<}
\def\PYGZgt{\char`\>}
\def\PYGZsh{\char`\#}
\def\PYGZpc{\char`\%}
\def\PYGZdl{\char`\$}
\def\PYGZhy{\char`\-}
\def\PYGZsq{\char`\'}
\def\PYGZdq{\char`\"}
\def\PYGZti{\char`\~}
% for compatibility with earlier versions
\def\PYGZat{@}
\def\PYGZlb{[}
\def\PYGZrb{]}
\makeatother

\renewcommand\PYGZsq{\textquotesingle}

\begin{document}

\maketitle
\tableofcontents
\phantomsection\label{index::doc}


Contents:


\chapter{Change Modelica language spec documentation format}
\label{MCP::doc}\label{MCP:welcome-to-modelica-language-specification-s-documentation}\label{MCP:change-modelica-language-spec-documentation-format}

\section{Abstract}
\label{MCP:abstract}
We want a version of the specification that:
\begin{itemize}
\item {} 
Can be easily modified.

\item {} 
Supports collaboration.

\item {} 
Can be converted to different formats easily.

\end{itemize}


\section{Copyright License}
\label{MCP:copyright-license}
This document is placed in public domain.


\section{Revisions}
\label{MCP:revisions}
\begin{tabulary}{\linewidth}{|L|L|L|}
\hline
\textsf{\relax 
Version
} & \textsf{\relax 
Date
} & \textsf{\relax 
Comments
}\\
\hline
v1
 & 
2015-06-08
 & 
Initial version
\\
\hline\end{tabulary}



\section{Proposed Changes in Specification}
\label{MCP:proposed-changes-in-specification}
The specification will need to be updated. The initial conversion
can be done by pandoc with manual changes of the specification to
make sure all formatting has been preserved, indexes updated, source
code using source code blocks, etc.


\subsection{Required changes in List of Keywords}
\label{MCP:required-changes-in-list-of-keywords}
Conversion to restructured text.


\subsection{Required changes in Grammar}
\label{MCP:required-changes-in-grammar}
Conversion to restructured text.
Actually write the grammar in EBNF (the old one does not follow the
grammar).


\subsection{Required changes in Specification Text}
\label{MCP:required-changes-in-specification-text}
All sections.


\section{Motivation}
\label{MCP:motivation}

\subsection{Benefits of using restructured text (Sphinx)}
\label{MCP:benefits-of-using-restructured-text-sphinx}

\subsubsection{It is a text-based representation}
\label{MCP:it-is-a-text-based-representation}
Equations are converted to nice vector graphics:
\begin{gather}
\begin{split}{\partial x \over \partial t} = 0.0\end{split}\notag
\end{gather}
Easy to get diff's for equations as they are also written in textual
markup (latex).

Easier to merge changes using a textual format than a binary one.
This makes collaboration much simpler, in particular when working
on making sure MCP's are up-to-date.

Easy to track individual changes (each commit is textual and easy to
see).

We could work on the specification directly during the design or web
meetings.


\subsubsection{Restructured text specifics}
\label{MCP:restructured-text-specifics}
Easier to reference particular subsections or items (a static, named,
link instead of an ever-changing section number).
That is, how to reference the specification from for example an e-mail
or forum discussion.

Automatic syntax highlighting for Modelica code snippets (no more
trying to remember which words to boldface):

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{model} \PYG{n+nc}{BouncingBall}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{e}\PYG{o}{=}\PYG{l+m+mf}{0.7} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{coefficient of restitution}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{g}\PYG{o}{=}\PYG{l+m+mf}{9.81} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{gravity acceleration}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{n+nb}{Real} \PYG{n}{h}\PYG{p}{(}\PYG{n}{start}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{height of ball}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{n+nb}{Real} \PYG{n}{v} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{velocity of ball}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{n+nb}{Boolean} \PYG{n}{flying}\PYG{p}{(}\PYG{n}{start}\PYG{o}{=}\PYG{k+kc}{true}\PYG{p}{)} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{true, if ball is flying}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{n+nb}{Boolean} \PYG{n}{impact}\PYG{p}{;}
  \PYG{n+nb}{Real} \PYG{n}{v\PYGZus{}new}\PYG{p}{;}
  \PYG{n+nb}{Integer} \PYG{n}{foo}\PYG{p}{;}
\PYG{k+kr}{equation}
  \PYG{n}{impact} \PYG{o}{=} \PYG{n}{h} \PYG{o}{\PYGZlt{}=} \PYG{l+m+mf}{0.0}\PYG{p}{;}
  \PYG{n}{foo} \PYG{o}{=} \PYG{k+kr}{if} \PYG{n}{impact} \PYG{k+kr}{then} \PYG{l+m+mi}{1} \PYG{k+kr}{else} \PYG{l+m+mi}{2}\PYG{p}{;}
  \PYG{k+kr}{der}\PYG{p}{(}\PYG{n}{v}\PYG{p}{)} \PYG{o}{=} \PYG{k+kr}{if} \PYG{n}{flying} \PYG{k+kr}{then} \PYG{o}{\PYGZhy{}}\PYG{n}{g} \PYG{k+kr}{else} \PYG{l+m+mi}{0}\PYG{p}{;}
  \PYG{k+kr}{der}\PYG{p}{(}\PYG{n}{h}\PYG{p}{)} \PYG{o}{=} \PYG{n}{v}\PYG{p}{;}

  \PYG{k+kr}{when} \PYG{p}{\PYGZob{}}\PYG{n}{h} \PYG{o}{\PYGZlt{}=} \PYG{l+m+mf}{0.0} \PYG{o+ow}{and} \PYG{n}{v} \PYG{o}{\PYGZlt{}=} \PYG{l+m+mf}{0.0}\PYG{p}{,}\PYG{n}{impact}\PYG{p}{\PYGZcb{}} \PYG{k+kr}{then}
    \PYG{n}{v\PYGZus{}new} \PYG{o}{=} \PYG{k+kr}{if} \PYG{n+nb}{edge}\PYG{p}{(}\PYG{n}{impact}\PYG{p}{)} \PYG{k+kr}{then} \PYG{o}{\PYGZhy{}}\PYG{n}{e}\PYG{o}{*}\PYG{n+nb}{pre}\PYG{p}{(}\PYG{n}{v}\PYG{p}{)} \PYG{k+kr}{else} \PYG{l+m+mi}{0}\PYG{p}{;}
    \PYG{n}{flying} \PYG{o}{=} \PYG{n}{v\PYGZus{}new} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{0}\PYG{p}{;}
    \PYG{n+nb}{reinit}\PYG{p}{(}\PYG{n}{v}\PYG{p}{,} \PYG{n}{v\PYGZus{}new}\PYG{p}{);}
  \PYG{k+kr}{end} \PYG{k+kr}{when}\PYG{p}{;}
\PYG{k+kr}{end} \PYG{n+nc}{BouncingBall}\PYG{p}{;}
\end{Verbatim}

Good search functionality (the JavaScript is able to search the html
even when the HTML is generated locally - no server required).

Google indexes the generated HTML easily.

It is also possible to host different versions of the documentation
on \href{https://readthedocs.org}{https://readthedocs.org}.

\href{http://sphinx-doc.org/}{http://sphinx-doc.org/} has good documentation on how to use
restructured text.

There are many useful features available:

\index{keyword!when}\index{keyword!if}\index{when}\begin{itemize}
\item {} 
Abbreviations like using :abbr: \textsc{MSL} (Modelica Standard Library) or as a short-hand name \textbar{}MLS\textbar{} \textsc{MLS} (Modelica Language Specification)

\item {} 
Citations like {\color{red}\bfseries{}:cite:{}`something{}`} (working with bibtex) is available as plugins.

\item {} 
Easy generation of an index, like \index{if}if and when.

\item {} 
Easy to theme the generated HTML.

\end{itemize}

Generation of the text into multiple formats:
\begin{itemize}
\item {} 
HTML (the primary format, web searchable and linkable)

\item {} 
PDF (via LaTeX)

\item {} 
epub (e-book readers)

\item {} 
qthelp (minor benefit; tools can use the built-in help and load the spec in it)

\end{itemize}


\subsubsection{Benefits of using git}
\label{MCP:benefits-of-using-git}
Easier to propose changes, for example by writing an MCP in restructured
text or creating a github pull request for fixing typo's.

Possible to work on the specification during meetings even when the
Wi-Fi is failing.


\subsection{Use Cases}
\label{MCP:impact}\label{MCP:use-cases}
See {\hyperref[MCP:impact]{\emph{\DUspan{}{Use Cases}}}}.


\section{Rationale}
\label{MCP:rationale}

\subsection{Design process}
\label{MCP:design-process}
Binary documentation formats are horrible.
ModelicaBook uses Sphinx, and OpenModelica documentation will as well
since the system mostly works.


\subsection{Alternative Designs}
\label{MCP:alternative-designs}
Investigated pandoc, docbook, ASCIIDoc.


\subsection{State of the Art}
\label{MCP:state-of-the-art}
It is used to document libraries, for example on \href{http://phpmyadmin.readthedocs.org/en/latest/}{http://phpmyadmin.readthedocs.org/en/latest/}.
It supports other languages as well, but the sphinx things allows you to
choose version and which version (HTML/PDF/etc) to download.


\section{Backwards Compatibility}
\label{MCP:backwards-compatibility}

\subsection{Corruption of existing models}
\label{MCP:corruption-of-existing-models}
No actual changes will be made.


\subsection{Means of mitigation}
\label{MCP:means-of-mitigation}
No actual changes will be made.


\section{Implementation Effort}
\label{MCP:implementation-effort}
No changes will be made that directly affect Modelica tools.


\subsection{Mandatory Parts}
\label{MCP:mandatory-parts}
No changes will be made that directly affect Modelica tools.


\subsection{Optional Parts}
\label{MCP:optional-parts}
No changes will be made that directly affect Modelica tools.


\subsection{Way of Implementation}
\label{MCP:way-of-implementation}
No changes will be made that directly affect Modelica tools.


\subsection{Required Patents}
\label{MCP:required-patents}
No changes will be made that affect patents.


\section{References}
\label{MCP:references}

\chapter{Introduction}
\label{introduction:introduction}\label{introduction::doc}

\section{Overview of Modelica}
\label{introduction:overview-of-modelica}
Modelica is a language for modeling of physical systems, designed to
support effective library development and model exchange. It is a modern
language built on acausal modeling with mathematical equations and
object-oriented constructs to facilitate reuse of modeling knowledge.


\section{Scope of the Specification}
\label{introduction:scope-of-the-specification}
The semantics of the Modelica language is specified by means of a set of
rules for translating any class described in the Modelica language to a
flat Modelica structure. A class must have additional properties in
order that its flat Modelica structure can be further transformed into a
set of differential, algebraic and discrete equations (= flat hybrid
DAE). Such classes are called simulation models.

The flat Modelica structure is also defined for other cases than
simulation models; including functions (can be used to provide
algorithmic contents), packages (used as a structuring mechanism), and
partial models (used as base-models). This allows correctness to be
verified before building the simulation model.

Modelica was designed to facilitate symbolic transformations of models,
especially by mapping basically every Modelica language construct to
continuous or instantaneous equations in the flat Modelica structure.
Many Modelica models, especially in the associated Modelica Standard
Library, are higher index systems, and can only be reasonably simulated
if symbolic index reduction is performed, i.e., equations are
differentiated and appropriate variables are selected as states, so that
the resulting system of equations can be transformed to state space form
(at least locally numerically), i.e., a hybrid DAE of index zero. The
Modelica specification does not define how to simulate a model. However,
it defines a set of equations that the simulation result should satisfy
as well as possible.

The key issues of the translation (or flattening) are:
\begin{itemize}
\item {} 
Expansion of inherited base classes

\item {} 
Parameterization of base classes, local classes and components

\item {} 
Generation of connection equations from connect-equations

\end{itemize}

The flat hybrid DAE form consists of:
\begin{itemize}
\item {} 
Declarations of variables with the appropriate basic types, prefixes
and attributes, such as \code{parameter Real v=5}.

\item {} 
Equations from equation sections.

\item {} 
Function invocations where an invocation is treated as a set of
equations which involves all input and all result variables (number
of equations = number of basic result variables).

\item {} 
Algorithm sections where every section is treated as a set of
equations which involves the variables occurring in the algorithm
section (number of equations = number of different assigned
variables).

\item {} 
When-clauses where every when-clause is treated as a set of
conditionally evaluated equations, also called instantaneous
equations, which are functions of the variables occurring in the
clause (number of equations = number of different assigned
variables).

\end{itemize}

Therefore, a flat hybrid DAE is seen as a set of equations where some of
the equations are only conditionally evaluated (e.g. instantaneous
equations are only evaluated when the corresponding when-condition
becomes true). Initial setup of the model is specified using
start-values and instantaneous equations that hold at the initial time
only.

A Modelica class may also contain annotations, i.e. formal comments,
which specify graphical representations of the class (icon and diagram),
documentation text for the class, and version information.


\section{Some Definitions}
\label{introduction:some-definitions}
The semantic specification should be read together with the Modelica
grammar. Non-normative text, i.e., examples and comments, are enclosed
in {[} {]}; comments are set \emph{in italics}. Additional terms are explained in
the glossary in \DUspan{xref,std,std-ref}{TODO: Appendix A}. Some important terms are:


\subsection{Component}
\label{introduction:component}
An element defined by the production component\_clause in the Modelica
grammar (basically a variable or an instance of a class).


\subsection{Element}
\label{introduction:element}
Class definitions, extends-clauses and component-clauses declared in a class
(basically a class reference or a component in a declaration).


\subsection{Flattening}
\label{introduction:flattening}
The translation of a model described in Modelica to the corresponding
model described as a hybrid DAE, involving expansion of inherited base
classes, parameterization of base classes, local classes and components,
and generation of connection equations from connect-equations (basically,
mapping the hierarchical structure of a model into a set of differential,
algebraic and discrete equations together with the corresponding variable
declarations and function definitions from the model).

\begin{notice}{note}{Todo}

Reference the glossary. Create the glossary.
\end{notice}


\section{Notation and Grammar}
\label{introduction:notation-and-grammar}
The following syntactic meta symbols are used (extended BNF \footnote{
\href{http://en.wikipedia.org/wiki/Extended\_Backus\%E2\%80\%93Naur\_Form}{ISO/IEC 14977 EBNF grammar}.
}):

\code{{[} {]}} optional

\code{\{ \}} repeat zero or more times

Boldface denotes keywords of the Modelica language. Keywords are
reserved words and may not be used as identifiers, with the exception of
initial which is a keyword in section headings, and der which is a
keyword for declaration functions, but it is also possible to call the
functions \code{initial()} and \code{der(...)}.

See \DUspan{xref,std,std-ref}{TODO: Appendix B} for a full lexical specification and grammar.


\chapter{Lexical Structure}
\label{lexical:lexical-structure}\label{lexical::doc}
This chapter describes several of the basic building blocks of Modelica
such as characters and lexical units including identifiers and literals.
Without question, the smallest building blocks in Modelica are single
characters belonging to a character set. Characters are combined to form
lexical units, also called tokens. These tokens are detected by the
lexical analysis part of the Modelica translator. Examples of tokens are
literal constants, identifiers, and operators. Comments are not really
lexical units since they are eventually discarded. On the other hand,
comments are detected by the lexical analyzer before being thrown away.

The information presented here is derived from the more formal
specification in Appendix B.


\section{Character Set}
\label{lexical:character-set}
The character set of the Modelica language is Unicode, but restricted to
the Unicode characters corresponding to 7-bit ASCII characters in
several places; for details see Appendix B.1.


\section{Comments}
\label{lexical:comments}
There are two kinds of comments in Modelica which are not lexical units
in the language and therefore are treated as whitespace by a Modelica
translator. The whitespace characters are space, tabulator, and line
separators (carriage return and line feed); and whitespace cannot occur
inside tokens, e.g., \textless{}= must be written as two characters without space
or comments between them. {[}\emph{The comment syntax is identical to that of
C++}{]}. The following comment variants are available:

\begin{tabulary}{\linewidth}{|L|L|}
\hline

\code{// comment}
 & 
Characters from // to the end of the line are ignored.
\\
\hline
\code{/* comment */}
 & 
Characters between /* and */ are ignored, including line terminators.
\\
\hline\end{tabulary}


Modelica comments do not nest, i.e., \code{/* */} cannot be embedded within
\code{/* */}. The following is \emph{invalid}:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{c+cm}{/* Commented out \PYGZhy{} erroneous comment, invalid nesting of comments!}
\PYG{c+cm}{/* This is an interesting model */}

\PYG{k+kr}{model} \PYG{n+nc}{interesting}
 \PYG{c+c1}{// ...}
\PYG{k+kr}{end} \PYG{n+nc}{interesting}\PYG{p}{;}
\PYG{o}{*}\PYG{o}{/}
\end{Verbatim}

There is also a kind of ``documentation comment,'' really a \emph{documentation
string} that is part of the Modelica language and therefore not ignored
by the Modelica translator. Such ``comments'' may occur at the ends of
declarations, equations, or statements or at the beginning of class
definitions. For example:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{model} \PYG{n+nc}{TempResistor} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Temperature dependent resistor}\PYG{l+s+s2}{\PYGZdq{}}
  \PYG{c+c1}{// ...}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{R} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Resistance for reference temp.}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{c+c1}{// ...}
\PYG{k+kr}{end} \PYG{n+nc}{TempResistor}\PYG{p}{;}
\end{Verbatim}


\section{Identifiers, Names, and Keywords}
\label{lexical:identifiers-names-and-keywords}
\emph{Identifiers} are sequences of letters, digits, and other characters
such as underscore, which are used for \emph{naming} various items in the
language. Certain combinations of letters are \emph{keywords} represented as
\emph{reserved} words in the Modelica grammar and are therefore not available
as identifiers.


\subsection{Identifiers}
\label{lexical:identifiers}
Modelica \emph{identifiers}, used for naming classes, variables, constants,
and other items, are of two forms. The first form always start with a
letter or underscore (\_), followed by any number of letters, digits, or
underscores. Case is significant, i.e., the names Inductor and inductor
are different. The second form (Q-IDENT) starts with a single quote,
followed by a sequence of any printable ASCII character, where
single-quote must be preceded by backslash, and terminated by a single
quote, e.g. `12H', `13\textbackslash{}'H', `+foo'. Control characters in quoted
identifiers have to use string escapes. The single quotes are part of
the identifier, i.e., `x' and x are distinct identifiers. The following
BNF-like rules define Modelica identifiers, where curly brackets \{\}
indicate repetition zero or more times, and vertical bar \textbar{} indicates
alternatives. A full BNF definition of the Modelica syntax and lexical
units is available in \DUspan{xref,std,std-ref}{old Appendix B}.

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k}{IDENT }\PYG{o}{=} \PYG{k}{NONDIGIT }\PYG{p}{\PYGZob{}} \PYG{k}{DIGIT }\PYG{p}{\textbar{}} \PYG{k}{NONDIGIT }\PYG{p}{\PYGZcb{}} \PYG{p}{\textbar{}} \PYG{k}{Q\PYGZhy{}IDENT }\PYG{p}{;}
\PYG{k}{Q\PYGZhy{}IDENT }\PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}\PYGZsq{}\PYGZdq{}} \PYG{p}{\PYGZob{}} \PYG{k}{Q\PYGZhy{}CHAR }\PYG{p}{\textbar{}} \PYG{k}{S\PYGZhy{}ESCAPE }\PYG{p}{\PYGZcb{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZsq{}\PYGZdq{}} \PYG{p}{;}
\PYG{k}{NONDIGIT }\PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}\PYGZus{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{k}{letters }\PYG{l+s+s2}{\PYGZdq{}a\PYGZdq{}} \PYG{k}{to }\PYG{l+s+s2}{\PYGZdq{}z\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{k}{letters }\PYG{l+s+s2}{\PYGZdq{}A\PYGZdq{}} \PYG{k}{to }\PYG{l+s+s2}{\PYGZdq{}Z\PYGZdq{}} \PYG{p}{;}
\PYG{k}{DIGIT }\PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}0\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}1\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}2\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}3\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}4\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}5\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}6\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}7\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}8\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}9\PYGZdq{}} \PYG{p}{;}
\PYG{k}{Q\PYGZhy{}CHAR }\PYG{o}{=} \PYG{k}{NONDIGIT }\PYG{p}{\textbar{}} \PYG{k}{DIGIT }\PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}!\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZsh{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZdl{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZpc{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZam{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}(\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{})\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZbs{}*\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}+\PYGZdq{}} \PYG{p}{\textbar{}}
  \PYG{l+s+s2}{\PYGZdq{},\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZhy{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}.\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}/\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}:\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{};\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZlt{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZgt{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}=\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}?\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}@\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}[\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}]\PYGZdq{}} \PYG{p}{\textbar{}}
  \PYG{l+s+s2}{\PYGZdq{}\PYGZca{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZob{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZcb{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\textbar{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZti{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{} \PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZus{}\PYGZdq{}} \PYG{p}{;}
\PYG{k}{S\PYGZhy{}ESCAPE }\PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}\PYGZbs{}\PYGZdq{}} \PYG{p}{(} \PYG{l+s+s2}{\PYGZdq{}\PYGZsq{}\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZdq{}}\PYG{l+s+s2}{\PYGZdq{}\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}?\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZbs{}\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}a\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}b\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}f\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}n\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}r\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}t\PYGZdq{}} \PYG{l+s+s2}{\PYGZdq{}v\PYGZdq{}} \PYG{p}{)} \PYG{p}{;}
\end{Verbatim}


\subsection{Names}
\label{lexical:names}
A \emph{name} is an identifier with a certain interpretation or meaning. For
example, a name may denote an Integer variable, a Real variable, a
function, a type, etc. A name may have different meanings in different
parts of the code, i.e., different scopes. The interpretation of
identifiers as names is described in more detail in \DUspan{xref,std,std-ref}{todo: Chapter 5}. The
meaning of package names is described in more detail in \DUspan{xref,std,std-ref}{todo: Chapter 13}.


\subsection{Modelica Keywords}
\label{lexical:modelica-keywords}
The following Modelica \emph{keywords} are reserved words and may not be used
as identifiers, except as listed in \DUspan{xref,std,std-ref}{TODO: Appendix B.1}:

\code{algorithm, and, annotation, block, break, class, connect, connector,
constant, constrainedby, der, discrete, each, else, elseif, elsewhen,
encapsulated, end, enumeration, equation, expandable, extends, external,
false, final, flow, for, function, if, import, impure, in, initial,
inner, input, loop, model, not, operator, or, outer, output, package,
parameter, partial, protected, public, pure, record, redeclare,
replaceable, return, stream, then, true, type, when, while, within}


\section{Literal Constants}
\label{lexical:literal-constants}
Literal constants are unnamed constants that have different forms
depending on their type. Each of the predefined types in Modelica has a
way of expressing unnamed constants of the corresponding type, which is
presented in the ensuing subsections. Additionally, array literals and
record literals can be expressed.


\subsection{Floating Point Numbers}
\label{lexical:floating-point-numbers}
A floating point number is expressed as a decimal number in the form of
a sequence of decimal digits optionally followed by a decimal point,
optionally followed by an exponent. At least one digit must be present.
The exponent is indicated by an E or e, followed by an optional sign (+
or -) and one or more decimal digits. The minimal recommended range is
that of IEEE double precision floating point numbers, for which the
largest representable positive number is \code{1.7976931348623157E+308} and the
smallest positive number is \code{2.2250738585072014E-308}. For example, the
following are floating point number literal constants:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{l+m+mf}{22.5}\PYG{p}{,} \PYG{l+m+mf}{3.141592653589793}\PYG{p}{,} \PYG{l+m+mf}{1.2E\PYGZhy{}35}
\end{Verbatim}

The same floating point number can be represented by different literals.
For example, all of the following literals denote the same number:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{l+m+mf}{13.}\PYG{p}{,} \PYG{l+m+mf}{13E0}\PYG{p}{,} \PYG{l+m+mf}{1.3e1}\PYG{p}{,} \PYG{l+m+mf}{0.13E2}
\end{Verbatim}


\subsection{Integer Literals}
\label{lexical:integer-literals}
Literals of type Integer are sequences of decimal digits, e.g. as in the
integer numbers \code{33, 0, 100, 30030044}. {[}\emph{Negative numbers are formed by
unary minus followed by an integer literal}{]}. The minimal recommended
number range is from \code{-2147483648} to \code{+2147483647} for a two’s-complement
32-bit integer implementation.


\subsection{Boolean Literals}
\label{lexical:boolean-literals}
The two Boolean literal values are \code{true} and \code{false}.


\subsection{Strings}
\label{lexical:strings}
String literals appear between double quotes as in ``between''. Any
character in the Modelica language character set (see appendix B.1 for
allowed characters) apart from double quote ('') and backslash (\textbackslash{}),
including new-line, can be \emph{directly} included in a string without using
an escape code. Certain characters in string literals can be represented
using escape codes, i.e., the character is preceded by a backslash (\textbackslash{})
within the string. Those characters are:

\begin{tabulary}{\linewidth}{|L|L|}
\hline

\code{"\textbackslash{}'"}
 & 
single quote may also appear without backslash in string constants.
\\
\hline
\code{"\textbackslash{}""}
 & 
double quote
\\
\hline
\code{"\textbackslash{}?"}
 & 
question-mark may also appear without backslash in string constants.
\\
\hline
\code{"\textbackslash{}\textbackslash{}"}
 & 
backslash itself
\\
\hline
\code{"\textbackslash{}a"}
 & 
alert (bell, code 7, ctrl-G)
\\
\hline
\code{"\textbackslash{}b"}
 & 
backspace (code 8, ctrl-H)
\\
\hline
\code{"\textbackslash{}f"}
 & 
form feed (code 12, ctrl-L)
\\
\hline
\code{"\textbackslash{}n"}
 & 
new-line (code 10, ctrl-J)
\\
\hline
\code{"\textbackslash{}r"}
 & 
return (code 13, ctrl-M)
\\
\hline
\code{"\textbackslash{}t"}
 & 
horizontal tab (code 9, ctrl-I)
\\
\hline
\code{"\textbackslash{}v"}
 & 
vertical tab (code 11, ctrl-K)
\\
\hline\end{tabulary}


For example, a string literal containing a tab, the words: This is,
double quote, space, the word: between, double quote, space, the word:
us, and new-line, would appear as follows:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+se}{\PYGZbs{}t}\PYG{l+s+s2}{This is}\PYG{l+s+se}{\PYGZbs{}\PYGZdq{}}\PYG{l+s+s2}{ between}\PYG{l+s+se}{\PYGZbs{}\PYGZdq{}}\PYG{l+s+s2}{ us}\PYG{l+s+se}{\PYGZbs{}n}\PYG{l+s+s2}{\PYGZdq{}}
\end{Verbatim}

Concatenation of string literals in certain situations (see the Modelica
grammar) is denoted by the + operator in Modelica, e.g. \code{"a" + "b"}
becomes \code{"ab"}. This is useful for expressing long string literals that
need to be written on several lines.

{[}\emph{Note, if the contents of a file is read into a Modelica string, it is
assumed that the reading function is responsible to handle the different
line ending symbols on file (e.g. on Linux systems to have a “newline”
character at the end of a line and on Windows systems to have a
“newline” and a “carriage return” character. As usual in programming
languages, the content of a file in a Modelica string only contains the
“newline” character.}

\emph{For long string comments, e.g., the “info” annotation to store the
documentation of a model, it would be very inconvenient, if the string
concatenation operator would have to be used for every line of
documentation. It is assumed that a Modelica tool supports the
non-printable “newline” character when browsing or editing a string
literal. For example, the following statement defines one string that
contains (non-printable) newline characters:}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n+nb}{assert}\PYG{p}{(}\PYG{n+nb}{noEvent}\PYG{p}{(}\PYG{n}{length} \PYG{o}{\PYGZgt{}} \PYG{n}{s\PYGZus{}small}\PYG{p}{),} \PYG{l+s+s2}{\PYGZdq{}}
\PYG{l+s+s2}{    The distance between the origin of frame\PYGZus{}a and the origin of frame\PYGZus{}b of a}
\PYG{l+s+s2}{    LineForceWithMass component became smaller as parameter s\PYGZus{}small}
\PYG{l+s+s2}{    (= a small number, defined in the }\PYG{l+s+se}{\PYGZbs{}\PYGZdq{}}\PYG{l+s+s2}{Advanced}\PYG{l+s+se}{\PYGZbs{}\PYGZdq{}}\PYG{l+s+s2}{ menu). The distance is}
\PYG{l+s+s2}{    set to s\PYGZus{}small, although it is smaller, to avoid a division by zero}
\PYG{l+s+s2}{    when computing the direction of the line force.}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,}
  \PYG{n}{level} \PYG{o}{=} \PYG{n+nb}{AssertionLevel}\PYG{o}{.}\PYG{n}{warning}\PYG{p}{);}
\end{Verbatim}

{]}


\section{Operator Symbols}
\label{lexical:operator-symbols}
The predefined operator symbols are formally defined on \DUspan{xref,std,std-ref}{todo: page 255} and
summarized in the table of operators in \hyperref[operators:table-operators]{Table \ref{operators:table-operators}}.


\chapter{Operators and Expressions}
\label{operators:operators-and-expressions}\label{operators::doc}
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 \DUspan{xref,std,std-ref}{TODO: Appendix B}.

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 Real, Integer, Boolean, String, and enumeration types which are
presented in more detail in Section \DUspan{xref,std,std-ref}{predefined-types}.
{[}\emph{The abbreviated predefined
type information below is given as background information for the rest
of the presentation.}{]}


\section{Expressions}
\label{operators:expressions}
Modelica equations, assignments and declaration equations contain
expressions.

Expressions can contain basic operations, \code{+, -, *, /, \textasciicircum{}}, etc. with
normal precedence as defined in \hyperref[operators:table-operators]{Table \ref{operators:table-operators}} and the grammar
in Section \DUspan{xref,std,std-ref}{modelica-concrete-syntax}. The semantics of the operations is defined for both
scalar and array arguments in \DUspan{xref,std,std-ref}{TODO: Section 10.6}.

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 \DUspan{xref,std,std-ref}{TODO: Section 12.4.1} and for vectorized calls in
\DUspan{xref,std,std-ref}{TODO: Section 12.4.4}. The built-in array functions are given in \DUspan{xref,std,std-ref}{TODO: Section 10.1.1}
and other built-in operators in Section {\hyperref[operators:builtin-with-function-syntax]{\emph{\DUspan{}{Built-in Intrinsic Operators with Function Syntax}}}}.


\section{Operator Precedence and Associativity}
\label{operators: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 from highest to lowest, as derived from the Modelica grammar
in \DUspan{xref,std,std-ref}{TODO: Appendix B}. All operators are binary except the postfix operators and
those shown as unary together with \emph{expr}, the conditional operator, the
array construction operator \code{\{\}} and concatenation operator \code{{[}{]}}, and the
array range constructor \code{:} which is either binary or ternary. Operators
with the same precedence occur at the same line of the table:


\begin{threeparttable}
\capstart\caption{Operators.}
\label{operators:table-operators}
\begin{tabulary}{\linewidth}{|L|L|L|}
\hline

\emph{Operator Group}
 & 
\emph{Operator Syntax}
 & 
\emph{Examples}
\\
\hline
postfix array index operator
 & 
\code{{[}{]}}
 & 
\code{arr{[}index{]}}
\\
\hline
postfix access operator
 & 
\code{.}
 & 
\code{a.b}
\\
\hline
postfix function call
 & 
\code{funcName(function-arguments)}
 & 
\code{sin(4.36)}
\\
\hline
array construct/concat
 & 
\code{\{expressions\}}
\code{{[}expressions{]}}
\code{{[}expressions; expressions, ...{]}}
 & 
\code{{[}2,3; 7,8{]}}
\code{\{2,3\}}
\code{{[}5,6{]}}
\\
\hline
exponentiation
 & 
\code{\textasciicircum{}}
 & 
\code{2\textasciicircum{}3}
\\
\hline
multiplicative and array
elementwise multiplicative
 & 
\code{* / .* ./}
 & 
\code{2*3 2/3}
\code{{[}1,2;3,4{]}.*{[}2,3;5,6{]}}
\\
\hline
additive and array
elementwise additive
 & 
\code{+ - +expr -expr .+ .-}
 & 
\code{a+b, a-b, +a, -a}
\code{{[}1,2;3,4{]}.+{[}2,3;5,6{]}}
\\
\hline
relational
 & 
\code{\textless{} \textless{}= \textgreater{} \textgreater{}= == \textless{}\textgreater{}}
 & 
\code{a\textless{}b, a\textless{}=b, a\textgreater{}b, ...}
\\
\hline
unary negation
 & 
\code{not expr}
 & 
\code{not b1}
\\
\hline
logical and
 & 
\code{and}
 & 
\code{b1 and b2}
\\
\hline
logical or
 & 
\code{or}
 & 
\code{b1 or b2}
\\
\hline
array range
 & 
\code{expr : expr}
\code{expr : expr : expr}
 & 
\code{1:5}
\code{start:step:stop}
\\
\hline
conditional
 & 
\code{if expr then expr else expr}
 & 
\code{if b then 3 else x}
\\
\hline
named argument
 & 
\code{ident = expr}
 & 
\code{x = 2.26}
\\
\hline\end{tabulary}

\end{threeparttable}


The conditional operator may also include elseif-clauses. Equality = and
assignment := 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.

{[}\emph{The unary minus and plus in Modelica is slightly different than in
Mathematica and in MATLAB} \footnote{
MATLAB is a registered trademark of MathWorks Inc.

Mathematica is a registered trademark of Wolfram Research Inc.
}\emph{, since the following expressions
are illegal (whereas in Mathematica and in MATLAB these are
valid expressions):}

\code{2*-2 // = -4} in Mathematica/MATLAB; is illegal in Modelica

\code{-{-}2 // = 2} in Mathematica/MATLAB; is illegal in Modelica

\code{++2 // = 2} in Mathematica/MATLAB; is illegal in Modelica

\code{2-{-}2 // = 4} in Mathematica/MATLAB; is illegal in Modelica

\emph{Non-associative exponentation and array range operator:}

\code{x\textasciicircum{}y\textasciicircum{}z} Not legal, use parenthesis to make it clear.

\code{a:b:c:d:e:f:g} Not legal, and scalar arguments gives no legal interpretation.

{]}


\section{Evaluation Order}
\label{operators:id2}\label{operators: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 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{operators:example-guarding-expressions-against-incorrect-evaluation}
{[}\emph{Example. If one wants to guard an expression against incorrect
evaluation, it should be guarded by an if:}

\begin{Verbatim}[commandchars=\\\{\}]
  \PYG{n+nb}{Boolean} \PYG{n}{v}\PYG{p}{[}\PYG{n}{n}\PYG{p}{];}
  \PYG{n+nb}{Boolean} \PYG{n}{b}\PYG{p}{;}
  \PYG{n+nb}{Integer} \PYG{n}{I}\PYG{p}{;}
\PYG{k+kr}{equation}
  \PYG{n}{x}\PYG{o}{=}\PYG{n}{v}\PYG{p}{[}\PYG{n}{I}\PYG{p}{]} \PYG{o+ow}{and} \PYG{p}{(}\PYG{n}{I}\PYG{o}{\PYGZgt{}=}\PYG{l+m+mi}{1} \PYG{o+ow}{and} \PYG{n}{I}\PYG{o}{\PYGZlt{}=}\PYG{n}{n}\PYG{p}{);} \PYG{c+c1}{// Invalid}
  \PYG{n}{x}\PYG{o}{=}\PYG{k+kr}{if} \PYG{p}{(}\PYG{n}{I}\PYG{o}{\PYGZgt{}=}\PYG{l+m+mi}{1} \PYG{o+ow}{and} \PYG{n}{I}\PYG{o}{\PYGZlt{}=}\PYG{n}{n}\PYG{p}{)} \PYG{k+kr}{then} \PYG{n}{v}\PYG{p}{[}\PYG{n}{I}\PYG{p}{]} \PYG{k+kr}{else} \PYG{k+kc}{false}\PYG{p}{;} \PYG{c+c1}{// Correct}
\end{Verbatim}

\emph{To guard square against square root of negative number use}
noEvent\emph{:}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{der}\PYG{p}{(}\PYG{n}{h}\PYG{p}{)}\PYG{o}{=}\PYG{k+kr}{if} \PYG{n}{h}\PYG{o}{\PYGZgt{}}\PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{o}{\PYGZhy{}}\PYG{n}{c}\PYG{o}{*}\PYG{n+nb}{sqrt}\PYG{p}{(}\PYG{n}{h}\PYG{p}{)} \PYG{k+kr}{else} \PYG{l+m+mi}{0}\PYG{p}{;} \PYG{c+c1}{// Incorrect}
\PYG{k+kr}{der}\PYG{p}{(}\PYG{n}{h}\PYG{p}{)}\PYG{o}{=}\PYG{k+kr}{if} \PYG{n+nb}{noEvent}\PYG{p}{(}\PYG{n}{h}\PYG{o}{\PYGZgt{}}\PYG{l+m+mi}{0}\PYG{p}{)} \PYG{k+kr}{then} \PYG{o}{\PYGZhy{}}\PYG{n}{c}\PYG{o}{*}\PYG{n+nb}{sqrt}\PYG{p}{(}\PYG{n}{h}\PYG{p}{)} \PYG{k+kr}{else} \PYG{l+m+mi}{0}\PYG{p}{;} \PYG{c+c1}{// Correct}
\end{Verbatim}

{]}


\section{Arithmetic Operators}
\label{operators:arithmetic-operators}
Modelica supports five binary arithmetic operators that operate on any
numerical type:

\begin{tabulary}{\linewidth}{|L|L|}
\hline

\code{\textasciicircum{}}
 & 
Exponentiation
\\
\hline
\code{*}
 & 
Multiplication
\\
\hline
\code{/}
 & 
Division
\\
\hline
\code{+}
 & 
Addition
\\
\hline
\code{-}
 & 
Subtraction
\\
\hline\end{tabulary}


Some of these operators can also be applied to a combination of a scalar
type and an array type, see Section \DUspan{xref,std,std-ref}{TODO: 10.6}.

The syntax of these operators is defined by the following rules from the
Modelica grammar:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k}{arithmetic\PYGZus{}expression }\PYG{o}{=}
  \PYG{p}{[} \PYG{k}{add\PYGZus{}op }\PYG{p}{]} \PYG{k}{term }\PYG{p}{\PYGZob{}} \PYG{k}{add\PYGZus{}op term }\PYG{p}{\PYGZcb{}} \PYG{p}{;}

\PYG{k}{add\PYGZus{}op }\PYG{o}{=}
  \PYG{l+s+s2}{\PYGZdq{}+\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZhy{}\PYGZdq{}} \PYG{p}{;}

\PYG{k}{term }\PYG{o}{=}
  \PYG{k}{factor }\PYG{p}{\PYGZob{}} \PYG{k}{mul\PYGZus{}op factor }\PYG{p}{\PYGZcb{}} \PYG{p}{;}

\PYG{k}{mul\PYGZus{}op }\PYG{o}{=}
  \PYG{l+s+s2}{\PYGZdq{}*\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}/\PYGZdq{}} \PYG{p}{;}

\PYG{k}{factor }\PYG{o}{=}
  \PYG{k}{primary }\PYG{p}{[} \PYG{l+s+s2}{\PYGZdq{}\PYGZca{}\PYGZdq{}} \PYG{k}{primary }\PYG{p}{]} \PYG{p}{;}
\end{Verbatim}


\section{Equality, Relational, and Logical Operators}
\label{operators:eq-relational-logic-operators}\label{operators:equality-relational-and-logical-operators}
Modelica supports the standard set of relational and logical operators,
all of which produce the standard boolean values true or false.

\begin{tabulary}{\linewidth}{|L|L|}
\hline

\code{\textgreater{}}
 & 
greater than
\\
\hline
\code{\textgreater{}=}
 & 
greater than or equal
\\
\hline
\code{\textless{}}
 & 
less than
\\
\hline
\code{\textless{}=}
 & 
less than or equal to
\\
\hline
\code{==}
 & 
equality within expressions
\\
\hline
\code{\textless{}\textgreater{}}
 & 
Inequality
\\
\hline\end{tabulary}


A single equals sign \code{=} is never used in relational expressions, only in
equations (\DUspan{xref,std,std-ref}{TODO: Chapter 8, Section 10.6.1}) and in function calls using named
parameter passing (\DUspan{xref,std,std-ref}{TODO: Section 12.4.1}).

The following logical operators are defined:

\begin{tabulary}{\linewidth}{|L|L|}
\hline

\code{not}
 & 
negation, unary operator
\\
\hline
\code{and}
 & 
logical and
\\
\hline
\code{or}
 & 
logical or
\\
\hline\end{tabulary}


The grammar rules define the syntax of the relational and logical
operators.

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k}{logical\PYGZus{}expression }\PYG{o}{=}
  \PYG{k}{logical\PYGZus{}term }\PYG{p}{\PYGZob{}} \PYG{k}{or logical\PYGZus{}term }\PYG{p}{\PYGZcb{}} \PYG{p}{;}
\PYG{k}{logical\PYGZus{}term }\PYG{o}{=}
  \PYG{k}{logical\PYGZus{}factor }\PYG{p}{\PYGZob{}} \PYG{k}{and logical\PYGZus{}factor }\PYG{p}{\PYGZcb{}} \PYG{p}{;}
\PYG{k}{logical\PYGZus{}factor }\PYG{o}{=}
  \PYG{p}{[} \PYG{k}{not }\PYG{p}{]} \PYG{k}{relation }\PYG{p}{;}
\PYG{k}{relation }\PYG{o}{=}
  \PYG{k}{arithmetic\PYGZus{}expression }\PYG{p}{[} \PYG{k}{rel\PYGZus{}op arithmetic\PYGZus{}expression }\PYG{p}{]} \PYG{p}{;}
\PYG{k}{rel\PYGZus{}op }\PYG{o}{=}
  \PYG{l+s+s2}{\PYGZdq{}\PYGZlt{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{p}{(} \PYG{l+s+s2}{\PYGZdq{}\PYGZlt{}=\PYGZdq{}} \PYG{p}{)} \PYG{p}{\textbar{}} \PYG{l+s+s2}{\PYGZdq{}\PYGZgt{}\PYGZdq{}} \PYG{p}{\textbar{}} \PYG{p}{(} \PYG{l+s+s2}{\PYGZdq{}\PYGZgt{}=\PYGZdq{}} \PYG{p}{)} \PYG{p}{\textbar{}} \PYG{p}{(} \PYG{l+s+s2}{\PYGZdq{}==\PYGZdq{}} \PYG{p}{)} \PYG{p}{\textbar{}} \PYG{p}{(} \PYG{l+s+s2}{\PYGZdq{}\PYGZlt{}\PYGZgt{}\PYGZdq{}} \PYG{p}{)} \PYG{p}{;}
\end{Verbatim}

The following holds for relational operators:
\begin{itemize}
\item {} 
Relational operators \code{\textless{}}, \code{\textless{}=}, \code{\textgreater{}}, \code{\textgreater{}=}, \code{==}, \textless{}\textgreater{}{}`{}`, are only defined for
scalar operands of simple types. The result is Boolean and is \code{true} or
\code{false} if the relation is fulfilled or not, respectively.

\item {} 
For operands of type String, str1 op str2 is for each relational
operator, op, defined in terms of the C-function strcmp as
strcmp(str1,str2) op 0.

\item {} 
For operands of type Boolean, \code{false\textless{}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 \code{v1 == v2} or \code{v1 \textless{}\textgreater{} v2}, \code{v1 or v2} shall, unless
used in a function, not be a subtype of Real. {[}\emph{The reason for this
rule is that relations with Real arguments are transformed to state
events (see Events, Section} 8.5\emph{) and this transformation becomes
unnecessarily complicated for the == and \textless{}\textgreater{} 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 Real variables is questionable on machines where the
number length in registers is different to number length in main
memory}{]}.

\item {} 
Relations of the form \code{v1 rel\_op v2}, with v1 and v2 variables and
rel\_op a relational operator are called elementary relations. If
either v1 or v2 or both variables are a subtype of Real, the relation
is called a Real elementary relation.

\end{itemize}


\section{Miscellaneous Operators and Variables}
\label{operators: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 time, which is a built-in variable, not an operator.


\subsection{String Concatenation}
\label{operators:string-concatenation}
Concatenation of strings (see the Modelica grammar) is denoted by the +
operator in Modelica {[}\emph{e.g.} \code{"a" + "b"} \emph{becomes} \code{"ab"}{]}.


\subsection{Array Constructor Operator}
\label{operators:array-constructor-operator}
The array constructor operator \code{\{ ... \}} is described in \DUspan{xref,std,std-ref}{TODO: Section 10.4}.


\subsection{Array Concatenation Operator}
\label{operators:array-concatenation-operator}
The array concatenation operator \code{{[} ... {]}} is described in \DUspan{xref,std,std-ref}{TODO: Section 10.4.2}.


\subsection{Array Range Operator}
\label{operators:array-range-operator}
The array range constructor operator \code{:} is described in \DUspan{xref,std,std-ref}{TODO: Section 10.4.3}.


\subsection{If-Expressions}
\label{operators:if-expressions}
An expression

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{if} \PYG{n}{expression1} \PYG{k+kr}{then} \PYG{n}{expression2} \PYG{k+kr}{else} \PYG{n}{expression3}
\end{Verbatim}

is one example of if-expression. First expression1, which must be
boolean expression, is evaluated. If expression1 is true expression2 is
evaluated and is the value of the if-expression, else expression3 is
evaluated and is the value of the if-expression. The two expressions,
expression2 and expression3, must be type compatible expressions
(\DUspan{xref,std,std-ref}{TODO: Section 6.6}) giving the type of the if-expression. If-expressions with
elseif are defined by replacing elseif by else if. {[}\emph{Note:} elseif \emph{has
been added for symmetry with if-clauses.}{]} For short-circuit evaluation
see Section {\hyperref[operators:evaluation-order]{\emph{\DUspan{}{Evaluation Order}}}}.

{[}\emph{Example}:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n+nb}{Integer} \PYG{n}{i}\PYG{p}{;}
\PYG{n+nb}{Integer} \PYG{n}{sign\PYGZus{}of\PYGZus{}i1}\PYG{o}{=}\PYG{k+kr}{if} \PYG{n}{i}\PYG{o}{\PYGZlt{}}\PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1} \PYG{k+kr}{elseif} \PYG{n}{i}\PYG{o}{==}\PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{l+m+mi}{0} \PYG{k+kr}{else} \PYG{l+m+mi}{1}\PYG{p}{;}
\PYG{n+nb}{Integer} \PYG{n}{sign\PYGZus{}of\PYGZus{}i2}\PYG{o}{=}\PYG{k+kr}{if} \PYG{n}{i}\PYG{o}{\PYGZlt{}}\PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1} \PYG{k+kr}{else} \PYG{k+kr}{if} \PYG{n}{i}\PYG{o}{==}\PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{l+m+mi}{0} \PYG{k+kr}{else} \PYG{l+m+mi}{1}\PYG{p}{;}
\end{Verbatim}

{]}


\subsection{Member Access Operator}
\label{operators:member-access-operator}
It is possible to access members of a class instance using dot notation,
i.e., the \code{.} operator.

{[}\emph{Example:} \code{R1.R} \emph{for accessing the resistance component} \code{R} \emph{of
resistor} \code{R1} \emph{. 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.}{]}


\subsection{Built-in Variable time}
\label{operators:built-in-variable-time}
All declared variables are functions of the independent variable time.
The variable 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{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{input} \PYG{n+nb}{Real} \PYG{n+nb}{time} \PYG{p}{(}\PYG{k+kr}{final} \PYG{n}{quantity} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Time}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{,} \PYG{k+kr}{final} \PYG{n}{unit} \PYG{o}{=} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{s}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{);}
\end{Verbatim}

The value of the start attribute of time is set to the time instant at
which the simulation is started.

{[}\emph{Example}:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{encapsulated} \PYG{k+kr}{model} \PYG{n+nc}{SineSource}
  \PYG{k+kr}{import} \PYG{n}{Modelica}\PYG{o}{.}\PYG{n}{Math}\PYG{o}{.}\PYG{n+nb}{sin}\PYG{p}{;}
  \PYG{k+kr}{connector} \PYG{n+nc}{OutPort}\PYG{o}{=}\PYG{k+kr}{output} \PYG{n+nb}{Real}\PYG{p}{;}
  \PYG{n}{OutPort} \PYG{n}{y}\PYG{o}{=}\PYG{n+nb}{sin}\PYG{p}{(}\PYG{n+nb}{time}\PYG{p}{);} \PYG{c+c1}{// Uses the built\PYGZhy{}in variable time.}
\PYG{k+kr}{end} \PYG{n+nc}{SineSource}\PYG{p}{;}
\end{Verbatim}

{]}


\section{Built-in Intrinsic Operators with Function Syntax}
\label{operators:builtin-with-function-syntax}\label{operators: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 Section {\hyperref[operators:numeric-and-conversion-functions]{\emph{\DUspan{}{Numeric Functions and Conversion Functions}}}}
below.

\item {} 
Derivative and special purpose operators with function syntax, see
Section {\hyperref[operators:derivative-special-purpose-operators]{\emph{\DUspan{}{Derivative and Special Purpose Operators with Function Syntax}}}} below.

\item {} 
Event-related operators with function syntax, see Section {\hyperref[operators:event-related-operators]{\emph{\DUspan{}{Event-Related Operators with Function Syntax}}}}
below.

\item {} 
Array operators/functions, see Section \DUspan{xref,std,std-ref}{TODO: 10.1.1}.

\end{itemize}

With exception of built-in operator \code{String(...)}, all operators in this
section can only be called with positional arguments.


\subsection{Numeric Functions and Conversion Functions}
\label{operators:numeric-and-conversion-functions}\label{operators:numeric-functions-and-conversion-functions}
The following mathematical operators and functions, also including some
conversion functions, are predefined in Modelica, and are vectorizable
according to Section 12.4.6, except for the String function. The
functions which do not trigger events are described in the table below,
whereas the event-triggering mathematical functions are described in
Section {\hyperref[operators:event-triggering-math-functions]{\emph{\DUspan{}{Event Triggering Mathematical Functions}}}}.

\begin{tabular}{|p{0.475\linewidth}|p{0.475\linewidth}|}
\hline

\code{abs(v)}
 & 
Is expanded into \code{noEvent(if v \textgreater{}= 0 then v else –v)}. Argument v needs to be an Integer or Real expression.
\\
\hline
\code{sign(v)}
 & 
Is expanded into \code{noEvent(if v\textgreater{}0 then 1 else if v\textless{}0 then –1 else 0)}. Argument v needs to be an Integer or Real expression.
\\
\hline
\code{sqrt(v)}
 & 
Returns the square root of \code{v} if \code{v\textgreater{}=0}, otherwise an error occurs. Argument \code{v} needs to be an Integer or Real expression.
\\
\hline
\code{Integer(e)}
 & 
Returns the ordinal number of the expression e of enumeration type that evaluates to the enumeration value E.enumvalue, where \code{Integer(E.e1){}`{}`=1, {}`{}`Integer(E.en){}`{}`=n, for an enumeration {}`{}`type E=enumeration(e1, ..., en)}. See also Section \DUspan{xref,std,std-ref}{TODO 4.8.5.2}.
\\
\hline
\begin{DUlineblock}{0em}
\item[] \code{String(b, \textless{}options\textgreater{})}
\item[] \code{String(i, \textless{}options\textgreater{})}
\item[] \code{String(r, significantDigits=d, \textless{}options\textgreater{})}
\end{DUlineblock}

\begin{DUlineblock}{0em}
\item[] \code{String(r, format=s)}
\item[] \code{String(e, \textless{}options\textgreater{})}
\end{DUlineblock}
 & 
Convert a scalar non-String expression to a String representation. The first argument may be a \code{Boolean b}, an \code{Integer i}, a \code{Real r} or an \code{Enumeration e} (Section \DUspan{xref,std,std-ref}{TODO: 4.8.5.2}). The other arguments must use named arguments. The optional \textless{}options\textgreater{} are:

Integer minimumLength=0: minimum length of the resulting string. If necessary, the blank character is used to fill up unused space.
Boolean leftJustified = true: if true, the converted result is left justified in the string; if false it is right justified in the string.
For Real expressions the output shall be according to the Modelica grammar. Integer significantDigits=6: defines the number of significant digits in the result string. {[}\emph{Examples:} \code{"12.3456"}, \code{"0.0123456"}, \code{"12345600"}, \code{"1.23456E-10"}{]}.

The format string corresponding to options is:
\begin{itemize}
\item {} 
for Reals: \code{(if leftJustified then "-" else "")+String(minimumLength)+"."+ String(signficantDigits)+"g"},

\item {} 
for Integers: \code{(if leftJustified then "-" else "")+String(minimumLength)+"d"}.

\end{itemize}

Format string: According to ANSI-C the format string specifies one conversion specifier (excluding the leading \%), may not contain length modifiers, and may not use ``*'' for width and/or precision. For all numeric values the format specifiers f, e, E, g, G are allowed. For integral values it is also allowed to use the d, i, o, x, X, u, and 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 x,X-formats (hexa-decimal) and c (character) for Integers does not lead to input that agrees with the Modelica-grammar.
\\
\hline\end{tabular}



\subsubsection{Event Triggering Mathematical Functions}
\label{operators:event-triggering-mathematical-functions}\label{operators:event-triggering-math-functions}
The built-in operators in this section trigger state events if used
outside of a when-clause and outside of a clocked discrete-time
partition (see Section 16.8.1). {[} \emph{If this is not desired, the} noEvent
\emph{function can be applied to them. E.g.} noEvent(integer(v)) {]}

\begin{tabulary}{\linewidth}{|L|L|}
\hline

\code{div(x,y)}
 & 
Returns the algebraic quotient x/y with any fractional part discarded (also known as truncation toward zero). {[}\emph{Note: this is defined for / in C99; in C89 the result for negative numbers is implementation-defined, so the standard function div() must be used.}{]}. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer.
\\
\hline
\code{mod(x,y)}
 & 
Returns the integer modulus of x/y, i.e. mod(x,y)=x-floor(x/y)*y. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously. Examples} mod(3,1.4)=0.2\emph{,} mod(-3,1.4)=1.2\emph{,} mod(3,-1.4)=-1.2{]}
\\
\hline
\code{rem(x,y)}
 & 
Returns the integer remainder of x/y, such that div(x,y)*y + rem(x, y) = x. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously. Examples} rem(3,1.4)=0.2\emph{,} rem(-3,1.4)=-0.2{]}
\\
\hline
\code{ceil(x)}
 & 
Returns the smallest integer not less than x. Result and argument shall have type Real. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}
\\
\hline
\code{floor(x)}
 & 
Returns the largest integer not greater than x. Result and argument shall have type Real. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}.
\\
\hline
\code{integer(x)}
 & 
Returns the largest integer not greater than x. The argument shall have type Real. The result has type Integer.
{[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}.
\\
\hline\end{tabulary}


\begin{notice}{note}{Todo}

This is using an alternative to using a table: headings. There are other alternatives (like definition lists).
Using headers makes it easy to get permanent links to the text.
\end{notice}


\paragraph{div(x,y)}
\label{operators:operator-div}\label{operators:div-x-y}
\code{div(x,y)} returns the algebraic quotient x/y with any fractional part discarded (also known as truncation toward zero). {[}\emph{Note: this is defined for / in C99; in C89 the result for negative numbers is implementation-defined, so the standard function div() must be used.}{]}. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer.


\paragraph{mod(x,y)}
\label{operators:mod-x-y}\label{operators:operator-mod}
\code{mod(x,y)} returns the integer modulus of x/y, i.e. mod(x,y)=x-floor(x/y)*y. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously. Examples} mod(3,1.4)=0.2\emph{,} mod(-3,1.4)=1.2\emph{,} mod(3,-1.4)=-1.2{]}.


\paragraph{rem(x,y)}
\label{operators:rem-x-y}\label{operators:operator-rem}
\code{rem(x,y)} returns the integer remainder of x/y, such that div(x,y)*y + rem(x, y) = x. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously. Examples} rem(3,1.4)=0.2\emph{,} rem(-3,1.4)=-0.2{]}.


\paragraph{ceil(x)}
\label{operators:ceil-x}\label{operators:operator-ceil}
\code{ceil(x)} returns the smallest integer not less than x. Result and argument shall have type Real. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}.


\paragraph{floor(x)}
\label{operators:floor-x}\label{operators:operator-floor}
\code{floor(x)} returns the largest integer not greater than x. Result and argument shall have type Real. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}.


\paragraph{integer(x)}
\label{operators:integer-x}\label{operators:operator-integer}
\code{integer(x)} returns the largest integer not greater than x. The argument shall have type Real. The result has type Integer.
{[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}.

\begin{notice}{note}{Todo}

This is using an alternative to using a table: definition lists.
\end{notice}
\begin{description}
\item[{\code{div(x,y)}}] \leavevmode
returns the algebraic quotient x/y with any fractional part discarded (also known as truncation toward zero). {[}\emph{Note: this is defined for / in C99; in C89 the result for negative numbers is implementation-defined, so the standard function div() must be used.}{]}. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer.

\item[{\code{mod(x,y)}}] \leavevmode
returns the integer modulus of x/y, i.e. mod(x,y)=x-floor(x/y)*y. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously. Examples} mod(3,1.4)=0.2\emph{,} mod(-3,1.4)=1.2\emph{,} mod(3,-1.4)=-1.2{]}.

\item[{\code{rem(x,y)}}] \leavevmode
returns the integer remainder of x/y, such that div(x,y)*y + rem(x, y) = x. Result and arguments shall have type Real or Integer. If either of the arguments is Real the result is Real otherwise Integer. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously. Examples} rem(3,1.4)=0.2\emph{,} rem(-3,1.4)=-0.2{]}.

\item[{\code{ceil(x)}}] \leavevmode
returns the smallest integer not less than x. Result and argument shall have type Real. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}.

\item[{\code{floor(x)}}] \leavevmode
returns the largest integer not greater than x. Result and argument shall have type Real. {[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}.

\item[{\code{integer(x)}}] \leavevmode
returns the largest integer not greater than x. The argument shall have type Real. The result has type Integer.
{[}\emph{Note, outside of a when-clause state events are triggered when the return value changes discontinuously.}{]}.

\end{description}


\subsubsection{Built-in Mathematical Functions and External Built-in Functions}
\label{operators:built-in-mathematical-functions-and-external-built-in-functions}
The following 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 Modelica.Math library.

\begin{tabulary}{\linewidth}{|L|L|}
\hline

\code{sin(x)}
 & 
sine
\\
\hline
\code{cos(x)}
 & 
cosine
\\
\hline
\code{tan(x)}
 & 
tangent (x shall not be: ..., -\(\pi\)/2, \(\pi\)/2, 3\(\pi\)/2, ...)
\\
\hline
\code{asin(x)}
 & 
inverse sine (-1 ≤ x ≤ 1)
\\
\hline
\code{acos(x)}
 & 
inverse cosine (-1 ≤ x ≤ 1)
\\
\hline
\code{atan(x)}
 & 
inverse tangent
\\
\hline
\code{atan2(y, x)}
 & 
the \code{atan2(y, x)} function calculates the principal value of the arc tangent of \code{y/x}, using the signs of the
two arguments to determine the quadrant of the result
\\
\hline
\code{sinh(x)}
 & 
hyperbolic sine
\\
\hline
\code{cosh(x)}
 & 
hyperbolic cosine
\\
\hline
\code{tanh(x)}
 & 
hyperbolic tangent
\\
\hline
\code{exp(x)}
 & 
exponential, base \emph{e}
\\
\hline
\code{log(x)}
 & 
natural (base \emph{e}) logarithm (\code{x \textgreater{} 0})
\\
\hline
\code{log10(x)}
 & 
base 10 logarithm (\code{x \textgreater{} 0})
\\
\hline\end{tabulary}



\subsection{Derivative and Special Purpose Operators with Function Syntax}
\label{operators:derivative-special-purpose-operators}\label{operators:derivative-and-special-purpose-operators-with-function-syntax}
The following derivative operator and special purpose operators with
function syntax are predefined:

\begin{notice}{note}{Todo}

This is using a definition list for the operators. There are other alternatives (tables are not that nice).
Definition lists can be referred to using links, like {\hyperref[operators:operator-der]{\emph{\DUspan{}{der(expr)}}}}, but the link will
not be visible in the HTML. Could possibly be themed into the HTML in some way.
\end{notice}
\phantomsection\label{operators:operator-der}\begin{description}
\item[{\code{der(expr)}}] \leavevmode
The time derivative of expr. If the expression expr is a scalar it
needs to be a subtype of Real. The expression and all its subexpressions
must be differentiable. If expr is an array, the operator is applied
to all elements of the array. For non-scalar arguments the function is
vectorized according to Section \DUspan{xref,std,std-ref}{TODO: 10.6.12}.
{[}\emph{For Real parameters and constants the result is a zero scalar or array of the same size as the variable.}{]}

\item[{\code{delay(expr,delayTime, delayMax)}, \code{delay(expr,delayTime)}}] \leavevmode
Returns: expr(time–delayTime) for time\textgreater{}time.start + delayTime and
expr(time.start) for time \textless{}= time.start + delayTime.
The arguments, i.e., expr, delayTime and delayMax, need to be subtypes of Real.
DelayMax needs to be additionally a parameter expression.
The following relation shall hold: 0 \textless{}= delayTime \textless{}= delayMax, otherwise an error occurs.
If delayMax is not supplied in the argument list, delayTime need to be a parameter expression.
See also Section {\hyperref[operators:operator-delay]{\emph{\DUspan{}{delay}}}}. For non-scalar arguments the function is vectorized according to Section \DUspan{xref,std,std-ref}{TODO: 10.6.12}.

\item[{\code{cardinality(c)}}] \leavevmode
{[}\emph{This is a deprecated operator. It should no longer be used, since it will be removed in one of the next Modelica releases.}{]}
Returns the number of (inside and outside) occurrences of connector instance c in a connect-equation as an Integer number. See also Section {\hyperref[operators:operator-cardinality]{\emph{\DUspan{}{cardinality (deprecated)}}}}.

\item[{\code{homotopy(actual=actual, simplified=simplified)}}] \leavevmode
The scalar expressions “actual” and “simplified” are subtypes of Real. A Modelica translator should map this operator into either of the two forms:
\begin{enumerate}
\item {} 
Returns “actual” \emph{{[}a 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, \emph{{[}e.g., actual*lambda + simplified*(1-lambda), where lambda is a homotopy parameter going from 0 to 1{]}.}

\end{enumerate}

The solution must fulfill the equations for homotopy returning \code{actual}.
See also Section {\hyperref[operators:operator-homotopy]{\emph{\DUspan{}{homotopy}}}}. For non-scalar arguments the function is vectorized according to Section \DUspan{xref,std,std-ref}{TODO: 12.4.6}.

\end{description}
\phantomsection\label{operators:operator-def-semilinear}\begin{description}
\item[{\code{semiLinear(x, positiveSlope, negativeSlope)}}] \leavevmode
Returns:
\code{if x\textgreater{}=0 then positiveSlope*x else negativeSlope*x}.
The result is of type Real. See Section {\hyperref[operators:operator-semilinear]{\emph{\DUspan{}{semiLinear}}}} {[}\emph{especially in the case when x = 0}{]}.
For non-scalar arguments the function is vectorized according to Section \DUspan{xref,std,std-ref}{TODO: 10.6.12}.

\item[{\code{inStream(v)}}] \leavevmode
The operator \code{inStream(v)} is only allowed on 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 more details see Section \DUspan{xref,std,std-ref}{TODO: 15.2}.

\item[{\code{actualStream(v)}}] \leavevmode
The actualStream(v) operator returns the actual value of the stream variable v for any flow direction. The operator is vectorizable. For more details, see Section \DUspan{xref,std,std-ref}{TODO: 15.3}.

\item[{\code{spatialDistribution(in0, in1, x, pv, iP, iV)}}] \leavevmode
The \code{spatialDistribution(...)} operator allows approximation of variable-speed transport of properties, see Section {\hyperref[operators:operator-spatialdistribution]{\emph{\DUspan{}{spatialDistribution}}}}.

\item[{\code{getInstanceName()}}] \leavevmode
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, see Section {\hyperref[operators:operator-instancename]{\emph{\DUspan{}{getInstanceName}}}}.

\end{description}

A few of these operators are described in more detail in the following.


\subsubsection{delay}
\label{operators:delay}\label{operators:operator-delay}
{[}\emph{The} \code{delay()} \emph{operator 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} \code{delayMax}\emph{.}

\emph{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, a} delay \emph{operator 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}.{]}


\subsubsection{spatialDistribution}
\label{operators:operator-spatialdistribution}\label{operators:spatialdistribution}
{[}\emph{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(y, 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.}{]}

The spatialDistribution() operator allows to approximate efficiently the
solution of the infinite-dimensional problem:
\begin{gather}
\begin{split}{\partial z(y,t) \over \partial t} + v(t) {\partial z(y,t) \over \partial y} = 0.0\end{split}\notag
\end{gather}\begin{gather}
\begin{split}z(0.0, t) = in_0(t) \text{ if } v >= 0\end{split}\notag
\end{gather}\begin{gather}
\begin{split}z(1.0, t) = in_1(t) \text{ if } v < 0\end{split}\notag
\end{gather}
where \emph{z(y, t)} is the transported quantity, \emph{y} is the normalized
spatial coordinate (0.0 ≤ \emph{y} ≤ 1.0), \emph{t} is the time,
\emph{v}(\emph{t})=\textbf{der}(\emph{x}) is the normalized transport velocity and the
boundary conditions are set at either \emph{y} = 0.0 or \emph{y} = 1.0, depending
on the sign of the velocity. The calling syntax is:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{p}{(}\PYG{n}{out0}\PYG{p}{,} \PYG{n}{out1}\PYG{p}{)} \PYG{o}{=} \PYG{n+nb}{spatialDistribution}\PYG{p}{(}\PYG{n}{in0}\PYG{p}{,} \PYG{n}{in1}\PYG{p}{,} \PYG{n}{x}\PYG{p}{,} \PYG{n}{positiveVelocity}\PYG{p}{,}
  \PYG{n}{initialPoints} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+m+mf}{0.0}\PYG{p}{,} \PYG{l+m+mf}{1.0}\PYG{p}{\PYGZcb{},}
  \PYG{n}{initialValues} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+m+mf}{0.0}\PYG{p}{,} \PYG{l+m+mf}{0.0}\PYG{p}{\PYGZcb{});}
\end{Verbatim}

where in0, in1, out0, out1, x, v are all subtypes of Real,
positiveVelocity is a Boolean, initialPoints and initialValues are
arrays of subtypes of Real of equal size, containing the y coordinates
and the \emph{z} values of a finite set of points describing the initial
distribution of \emph{z}(\emph{y, t0}). The out0 and out1 are given by the
solutions at \emph{z(0.0, t)} and \emph{z(1.0, t)}; and in0 and in1 are the
boundary conditions at \emph{z(0.0, t)} and \emph{z(1.0, t)} (at each point in
time only one of in0 and in1 is used). Elements in the initialPoints
array must be sorted in non-descending order. The operator can not be
vectorized according to the vectorization rules described in section
12.4.6. The operator can be vectorized only with respect to the
arguments in0 and in1 (which must have the same size), returning
vectorized outputs out0 and out1 of the same size; the arguments
initialPoints and initialValues are vectorized accordingly.

The solution, z(..), can be described in terms of characteristics:

= \emph{z}(\emph{y}, \emph{t}), for all, as long as staying inside the domain.

This allows to directly compute the solution based on interpolating the
boundary conditions.

The \code{spatialDistribution} operator can be described in terms of the
pseudo-code given as a block:

\begin{literal-block}
\caption{Pseudo-code for spatialDistribution in terms of a block.}
\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{block} \PYG{n+nc}{spatialDistribution}
  \PYG{k+kr}{input} \PYG{n+nb}{Real} \PYG{n}{in0}\PYG{p}{;}
  \PYG{k+kr}{input} \PYG{n+nb}{Real} \PYG{n}{in1}\PYG{p}{;}
  \PYG{k+kr}{input} \PYG{n+nb}{Real} \PYG{n}{x}\PYG{p}{;}
  \PYG{k+kr}{input} \PYG{n+nb}{Boolean} \PYG{n}{positiveVelocity}\PYG{p}{;}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{initialPoints}\PYG{p}{(}\PYG{k+kr}{each} \PYG{n+nb}{min}\PYG{o}{=}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{k+kr}{each} \PYG{n+nb}{max}\PYG{o}{=}\PYG{l+m+mi}{1}\PYG{p}{)[}\PYG{o}{:}\PYG{p}{]} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+m+mf}{0.0}\PYG{p}{,} \PYG{l+m+mf}{1.0}\PYG{p}{\PYGZcb{};}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{initialValues}\PYG{p}{[}\PYG{o}{:}\PYG{p}{]} \PYG{o}{=} \PYG{p}{\PYGZob{}}\PYG{l+m+mf}{0.0}\PYG{p}{,} \PYG{l+m+mf}{0.0}\PYG{p}{\PYGZcb{};}
  \PYG{k+kr}{output} \PYG{n+nb}{Real} \PYG{n}{out0}\PYG{p}{;}
  \PYG{k+kr}{output} \PYG{n+nb}{Real} \PYG{n}{out1}\PYG{p}{;}
  \PYG{k+kr}{protected}
  \PYG{n+nb}{Real} \PYG{n}{points}\PYG{p}{[}\PYG{o}{:}\PYG{p}{];}
  \PYG{n+nb}{Real} \PYG{n}{values}\PYG{p}{[}\PYG{o}{:}\PYG{p}{];}
  \PYG{n+nb}{Real} \PYG{n}{x0}\PYG{p}{;}
  \PYG{n+nb}{Integer} \PYG{n}{m}\PYG{p}{;}
\PYG{k+kr}{algorithm}
  \PYG{k+kr}{if} \PYG{n}{positiveVelocity} \PYG{k+kr}{then}
    \PYG{n}{out1}\PYG{o}{:=}\PYG{n}{interpolate}\PYG{p}{(}\PYG{n}{points}\PYG{p}{,} \PYG{n}{values}\PYG{p}{,} \PYG{l+m+mi}{1}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{));}
    \PYG{n}{out0}\PYG{o}{:=}\PYG{n}{values}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{];} \PYG{c+c1}{// similar to in0 but avoiding algebraic loop}
  \PYG{k+kr}{else}
    \PYG{n}{out0}\PYG{o}{:=}\PYG{n}{interpolate}\PYG{p}{(}\PYG{n}{points}\PYG{p}{,} \PYG{n}{values}\PYG{p}{,} \PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{));}
    \PYG{n}{out1}\PYG{o}{:=}\PYG{n}{values}\PYG{p}{[}\PYG{k+kr}{end}\PYG{p}{];} \PYG{c+c1}{// similar to in1 but avoiding algebraic loop}
  \PYG{k+kr}{end} \PYG{k+kr}{if}\PYG{p}{;}
  \PYG{k+kr}{when} \PYG{c+cm}{/* acceptedStep */} \PYG{k+kr}{then}
    \PYG{k+kr}{if} \PYG{n}{x}\PYG{o}{\PYGZgt{}}\PYG{n}{x0} \PYG{k+kr}{then}
      \PYG{n}{m}\PYG{o}{:=}\PYG{n+nb}{size}\PYG{p}{(}\PYG{n}{points}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{);}
      \PYG{k+kr}{while} \PYG{p}{(}\PYG{k+kr}{if} \PYG{n}{m}\PYG{o}{\PYGZgt{}}\PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{n}{points}\PYG{p}{[}\PYG{n}{m}\PYG{p}{]}\PYG{o}{+}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{)}\PYG{o}{\PYGZgt{}=}\PYG{l+m+mi}{1} \PYG{k+kr}{else} \PYG{k+kc}{false}\PYG{p}{)} \PYG{k+kr}{then}
        \PYG{n}{m}\PYG{o}{:=}\PYG{n}{m}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{;}
      \PYG{k+kr}{end} \PYG{k+kr}{while}\PYG{p}{;}
      \PYG{n}{values}\PYG{o}{:=}\PYG{n+nb}{cat}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{p}{\PYGZob{}}\PYG{n}{in0}\PYG{p}{\PYGZcb{},} \PYG{n}{values}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{o}{:}\PYG{n}{m}\PYG{p}{],} \PYG{p}{\PYGZob{}}\PYG{n}{interpolate}\PYG{p}{(}\PYG{n}{points}\PYG{p}{,} \PYG{n}{values}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{))\PYGZcb{}} \PYG{p}{);}
      \PYG{n}{points}\PYG{o}{:=}\PYG{n+nb}{cat}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{p}{\PYGZob{}}\PYG{l+m+mi}{0}\PYG{p}{\PYGZcb{},} \PYG{n}{points}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{o}{:}\PYG{n}{m}\PYG{p}{]} \PYG{o}{.+} \PYG{p}{(}\PYG{n}{x1}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{),} \PYG{p}{\PYGZob{}}\PYG{l+m+mi}{1}\PYG{p}{\PYGZcb{}} \PYG{p}{);}
    \PYG{k+kr}{elseif} \PYG{n}{x}\PYG{o}{\PYGZlt{}}\PYG{n}{x0} \PYG{k+kr}{then}
      \PYG{n}{m}\PYG{o}{:=}\PYG{l+m+mi}{1}\PYG{p}{;}
      \PYG{k+kr}{while} \PYG{p}{(}\PYG{k+kr}{if} \PYG{n}{m}\PYG{o}{\PYGZlt{}}\PYG{n+nb}{size}\PYG{p}{(}\PYG{n}{points}\PYG{p}{,}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{k+kr}{then} \PYG{n}{points}\PYG{p}{[}\PYG{n}{m}\PYG{p}{]}\PYG{o}{+}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{)}\PYG{o}{\PYGZlt{}=}\PYG{l+m+mi}{0} \PYG{k+kr}{else} \PYG{k+kc}{false}\PYG{p}{)} \PYG{k+kr}{then}
        \PYG{n}{m}\PYG{o}{:=}\PYG{n}{m}\PYG{o}{+}\PYG{l+m+mi}{1}\PYG{p}{;}
      \PYG{k+kr}{end} \PYG{k+kr}{while}\PYG{p}{;}
      \PYG{n}{values}\PYG{o}{:=}\PYG{n+nb}{cat}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{p}{\PYGZob{}}\PYG{n}{interpolate}\PYG{p}{(}\PYG{n}{points}\PYG{p}{,} \PYG{n}{values}\PYG{p}{,} \PYG{l+m+mi}{0}\PYG{o}{\PYGZhy{}}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{))\PYGZcb{},}\PYG{n}{values}\PYG{p}{[}\PYG{n}{m}\PYG{o}{:}\PYG{k+kr}{end}\PYG{p}{],\PYGZob{}}\PYG{n}{in1}\PYG{p}{\PYGZcb{});}
      \PYG{n}{points}\PYG{o}{:=}\PYG{n+nb}{cat}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{p}{\PYGZob{}}\PYG{l+m+mi}{0}\PYG{p}{\PYGZcb{},} \PYG{n}{points}\PYG{p}{[}\PYG{n}{m}\PYG{o}{:}\PYG{k+kr}{end}\PYG{p}{]} \PYG{o}{.+} \PYG{p}{(}\PYG{n}{x1}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{),} \PYG{p}{\PYGZob{}}\PYG{l+m+mi}{1}\PYG{p}{\PYGZcb{});}
    \PYG{k+kr}{end} \PYG{k+kr}{if}\PYG{p}{;}
    \PYG{n}{x0}\PYG{o}{:=}\PYG{n}{x}\PYG{p}{;}
  \PYG{k+kr}{end} \PYG{k+kr}{when}\PYG{p}{;}
\PYG{k+kr}{initial} \PYG{k+kr}{algorithm}
  \PYG{n}{x0}\PYG{o}{:=}\PYG{n}{x}\PYG{p}{;}
  \PYG{n}{points}\PYG{o}{:=}\PYG{n}{initialPoints}\PYG{p}{;}
  \PYG{n}{values}\PYG{o}{:=}\PYG{n}{initialValues}\PYG{p}{;}
\PYG{k+kr}{end} \PYG{n+nc}{spatialDistribution}\PYG{p}{;}
\end{Verbatim}
\phantomsection\label{operators:pseudo-code-for-spatialdistribution-in-terms-of-a-block}
\end{literal-block}

{[}\emph{The infinite-dimensional problem stated above can then be formulated
in the following way:}

\emph{Events are generated at the exact instants when the velocity changes
sign – if this is not needed, noEvent() can be used to suppress
event generation.}

\emph{If the velocity is known to be always positive, then out0 can be
omitted, e.g.:}

\emph{Technically relevant use cases for the use of the}
spatialDistribution\emph{() operator 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
v in the example above.}{]}


\subsubsection{cardinality (deprecated)}
\label{operators:cardinality-deprecated}\label{operators:operator-cardinality}
{[}\emph{The cardinality operator is deprecated for the following reasons and
will be removed in a future release:}
\begin{itemize}
\item {} 
\emph{Reflective operator may make early type checking more difficult.}

\item {} 
\emph{Almost always abused in strange ways}

\item {} 
\emph{Not used for Bond graphs even though it was originally introduced
for that purpose.}

\end{itemize}

{]}

{[}\emph{The} cardinality() \emph{operator allows the definition of connection
dependent equations in a model, for example}:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{connector} \PYG{n+nc}{Pin}
  \PYG{n+nb}{Real} \PYG{n}{v}\PYG{p}{;}
  \PYG{k+kr}{flow} \PYG{n+nb}{Real} \PYG{n}{i}\PYG{p}{;}
\PYG{k+kr}{end} \PYG{n+nc}{Pin}\PYG{p}{;}

\PYG{k+kr}{model} \PYG{n+nc}{Resistor}
  \PYG{n}{Pin} \PYG{n}{p}\PYG{p}{,} \PYG{n}{n}\PYG{p}{;}
\PYG{k+kr}{equation}
  \PYG{n+nb}{assert}\PYG{p}{(}\PYG{n+nb}{cardinality}\PYG{p}{(}\PYG{n}{p}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{0} \PYG{o+ow}{and} \PYG{n+nb}{cardinality}\PYG{p}{(}\PYG{n}{n}\PYG{p}{)} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{0}\PYG{p}{,} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Connectors p and n of Resistor must be connected}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{);}
  \PYG{c+c1}{// Equations of resistor ...}
\PYG{k+kr}{end} \PYG{n+nc}{Resistor}\PYG{p}{;}
\end{Verbatim}

{]}

The cardinality is counted after removing conditional components. and
may 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). The cardinality operator should only
be used in the condition of assert and if-statements – that do not
contain connect (and similar operators – see section 8.3.3).


\subsubsection{homotopy}
\label{operators:operator-homotopy}\label{operators:homotopy}
\emph{{[}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{Verbatim}[commandchars=\\\{\}]
\PYG{n}{lambda}\PYG{o}{*}\PYG{n}{actual} \PYG{o}{+} \PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{\PYGZhy{}}\PYG{n}{lambda}\PYG{p}{)}\PYG{o}{*}\PYG{n}{simplified}
\end{Verbatim}

\emph{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.}

\emph{The operator can be called with ordered arguments or preferably with
named arguments for improved readability.}

\emph{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{quote}

\begin{DUlineblock}{0em}
\item[] \textbf{w} = \textbf{f}$_{\text{1}}$(\textbf{x}) // has homotopy operator
\item[] \textbf{0} = \textbf{f}$_{\text{2}}$(der(\textbf{x}), \textbf{x}, \textbf{z}, \textbf{w})
\end{DUlineblock}
\end{quote}

\emph{Here, a non-linear equation system} \textbf{f}$_{\text{2}}$ \emph{is present. The
homotopy operator 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} \textbf{f}$_{\text{1}}$
\emph{and} \textbf{f}$_{\text{2}}$ \emph{together.}

\emph{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 a
homotopy operator is present and all equations up to the last BLT block
that describes a non-linear algebraic equation system.}

\emph{A trivial implementation of the homotopy operator is obtained by
defining the following function in the global scope:}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{function} \PYG{n+nc}{homotopy}
  \PYG{k+kr}{input} \PYG{n+nb}{Real} \PYG{n}{actual}\PYG{p}{;}
  \PYG{k+kr}{input} \PYG{n+nb}{Real} \PYG{n}{simplified}\PYG{p}{;}
  \PYG{k+kr}{output} \PYG{n+nb}{Real} \PYG{n}{y}\PYG{p}{;}
\PYG{k+kr}{algorithm}
  \PYG{n}{y} \PYG{o}{:=} \PYG{n}{actual}\PYG{p}{;}
\PYG{k+kr}{annotation}\PYG{p}{(}\PYG{n}{Inline} \PYG{o}{=} \PYG{k+kc}{true}\PYG{p}{);}
\PYG{k+kr}{end} \PYG{n+nc}{homotopy}\PYG{p}{;}
\end{Verbatim}

\emph{Example 1:}

\emph{In electrical systems it is often difficult to solve non-linear
algebraic equations if switches are part of the algebraic loop. An
idealized diode model might be implemented in the following way, by
starting with a “flat” diode characteristic and then move with the
homotopy operator to the desired “steep” characteristic:}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{model} \PYG{n+nc}{IdealDiode}
  \PYG{c+c1}{// ...}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{Goff} \PYG{o}{=} \PYG{l+m+mf}{1e\PYGZhy{}5}\PYG{p}{;}
  \PYG{k+kr}{protected}
  \PYG{n+nb}{Real} \PYG{n}{Goff\PYGZus{}flat} \PYG{o}{=} \PYG{n+nb}{max}\PYG{p}{(}\PYG{l+m+mf}{0.01}\PYG{p}{,} \PYG{n}{Goff}\PYG{p}{);}
  \PYG{n+nb}{Real} \PYG{n}{Goff2}\PYG{p}{;}
\PYG{k+kr}{equation}
  \PYG{n}{off} \PYG{o}{=} \PYG{n}{s} \PYG{o}{\PYGZlt{}} \PYG{l+m+mi}{0}\PYG{p}{;}
  \PYG{n}{Goff2} \PYG{o}{=} \PYG{n+nb}{homotopy}\PYG{p}{(}\PYG{n}{actual}\PYG{o}{=}\PYG{n}{Goff}\PYG{p}{,} \PYG{n}{simplified}\PYG{o}{=}\PYG{n}{Goff\PYGZus{}flat}\PYG{p}{);}
  \PYG{n}{u} \PYG{o}{=} \PYG{n}{s}\PYG{o}{*}\PYG{p}{(}\PYG{k+kr}{if} \PYG{n}{off} \PYG{k+kr}{then} \PYG{l+m+mi}{1} \PYG{k+kr}{else} \PYG{n}{Ron2}\PYG{p}{)} \PYG{o}{+} \PYG{n}{Vknee}\PYG{p}{;}
  \PYG{n}{i} \PYG{o}{=} \PYG{n}{s}\PYG{o}{*}\PYG{p}{(}\PYG{k+kr}{if} \PYG{n}{off} \PYG{k+kr}{then} \PYG{n}{Goff2} \PYG{k+kr}{else} \PYG{l+m+mi}{1} \PYG{p}{)} \PYG{o}{+} \PYG{n}{Goff2}\PYG{o}{*}\PYG{n}{Vknee}\PYG{p}{;}
  \PYG{c+c1}{// ...}
\PYG{k+kr}{end} \PYG{n+nc}{IdealDiode}\PYG{p}{;}
\end{Verbatim}

\emph{Example 2:}

\emph{In electrical systems it is often useful that all voltage sources start
with zero voltage and all current sources with zero current, since
steady state initialization with zero sources can be easily obtained. A
typical voltage source would then be defined as:}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{model} \PYG{n+nc}{ConstantVoltageSource}
  \PYG{k+kr}{extends} \PYG{n}{Modelica}\PYG{o}{.}\PYG{n}{Electrical}\PYG{o}{.}\PYG{n}{Analog}\PYG{o}{.}\PYG{n}{Interfaces}\PYG{o}{.}\PYG{n}{OnePort}\PYG{p}{;}
  \PYG{k+kr}{parameter} \PYG{n}{Modelica}\PYG{o}{.}\PYG{n}{SIunits}\PYG{o}{.}\PYG{n}{Voltage} \PYG{n}{V}\PYG{p}{;}
\PYG{k+kr}{equation}
  \PYG{n}{v} \PYG{o}{=} \PYG{n+nb}{homotopy}\PYG{p}{(}\PYG{n}{actual}\PYG{o}{=}\PYG{n}{V}\PYG{p}{,} \PYG{n}{simplified}\PYG{o}{=}\PYG{l+m+mf}{0.0}\PYG{p}{);}
\PYG{k+kr}{end} \PYG{n+nc}{ConstantVoltageSource}\PYG{p}{;}
\end{Verbatim}

\emph{Example 3:}

\emph{In fluid system modelling, the pressure/flowrate relationships are
highly nonlinear due to the quadratic terms and due to the dependency on
fluid properties. A simplified linear model, tuned on the nominal
operating point, can be used to make the overall model less nonlinear
and thus easier to solve without accurate start values. Named arguments
are used here in order to further improve the readability.}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{model} \PYG{n+nc}{PressureLoss}
  \PYG{k+kr}{import} \PYG{n}{SI} \PYG{o}{=} \PYG{n}{Modelica}\PYG{o}{.}\PYG{n}{SIunits}\PYG{p}{;}
  \PYG{c+c1}{// ...}
  \PYG{k+kr}{parameter} \PYG{n}{SI}\PYG{o}{.}\PYG{n}{MassFlowRate} \PYG{n}{m\PYGZus{}flow\PYGZus{}nominal} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Nominal mass flow rate}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{k+kr}{parameter} \PYG{n}{SI}\PYG{o}{.}\PYG{n}{Pressure} \PYG{n}{dp\PYGZus{}nominal} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Nominal pressure drop}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{n}{SI}\PYG{o}{.}\PYG{n}{Density} \PYG{n}{rho} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Upstream density}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
  \PYG{n}{SI}\PYG{o}{.}\PYG{n}{DynamicViscosity} \PYG{n}{lambda} \PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Upstream viscosity}\PYG{l+s+s2}{\PYGZdq{}}\PYG{p}{;}
\PYG{k+kr}{equation}
  \PYG{c+c1}{// ...}
  \PYG{n}{m\PYGZus{}flow} \PYG{o}{=} \PYG{n+nb}{homotopy}\PYG{p}{(}\PYG{n}{actual} \PYG{o}{=} \PYG{n}{turbulentFlow\PYGZus{}dp}\PYG{p}{(}\PYG{n}{dp}\PYG{p}{,} \PYG{n}{rho}\PYG{p}{,} \PYG{n}{lambda}\PYG{p}{),}
  \PYG{n}{simplified} \PYG{o}{=} \PYG{n}{dp}\PYG{o}{/}\PYG{n}{dp\PYGZus{}nominal}\PYG{o}{*}\PYG{n}{m\PYGZus{}flow\PYGZus{}nominal}\PYG{p}{);}
  \PYG{c+c1}{// ...}
\PYG{k+kr}{end} \PYG{n+nc}{PressureLoss}\PYG{p}{;}
\end{Verbatim}

\emph{Example 4:}

\emph{Note that the homotopy operator **shall not*} be used to combine
unrelated expressions, since this can generate singular systems from
combining two well-defined systems.*

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{model} \PYG{n+nc}{DoNotUse}
  \PYG{n+nb}{Real} \PYG{n}{x}\PYG{p}{;}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{x0} \PYG{o}{=} \PYG{l+m+mi}{0}\PYG{p}{;}
\PYG{k+kr}{equation}
  \PYG{k+kr}{der}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{\PYGZhy{}}\PYG{n}{x}\PYG{p}{;}
\PYG{k+kr}{initial} \PYG{k+kr}{equation}
  \PYG{l+m+mi}{0} \PYG{o}{=} \PYG{n+nb}{homotopy}\PYG{p}{(}\PYG{k+kr}{der}\PYG{p}{(}\PYG{n}{x}\PYG{p}{),} \PYG{n}{x} \PYG{o}{\PYGZhy{}} \PYG{n}{x0}\PYG{p}{);}
\PYG{k+kr}{end} \PYG{n+nc}{DoNotUse}\PYG{p}{;}
\end{Verbatim}

\emph{The initial equation is expanded into}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{l+m+mi}{0} \PYG{o}{=} \PYG{n}{lambda}\PYG{o}{*}\PYG{k+kr}{der}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)} \PYG{o}{+} \PYG{p}{(}\PYG{l+m+mi}{1}\PYG{o}{\PYGZhy{}}\PYG{n}{lambda}\PYG{p}{)}\PYG{o}{*}\PYG{p}{(}\PYG{n}{x}\PYG{o}{\PYGZhy{}}\PYG{n}{x0}\PYG{p}{)}
\end{Verbatim}

\emph{and you can solve the two equations to give}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n}{x} \PYG{o}{=} \PYG{p}{(}\PYG{n}{lambda}\PYG{o}{+}\PYG{p}{(}\PYG{n}{lambda}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x0}\PYG{p}{)}\PYG{o}{/}\PYG{p}{(}\PYG{l+m+mi}{2}\PYG{o}{*}\PYG{n}{lambda} \PYG{o}{\PYGZhy{}} \PYG{l+m+mi}{1}\PYG{p}{)}
\end{Verbatim}

\emph{which has the correct value of x0 at lambda = 0 and of 1 at lambda = 1,
but unfortunately has a singularity at lambda = 0.5.}

\emph{{]}}


\subsubsection{semiLinear}
\label{operators:semilinear}\label{operators:operator-semilinear}
(See definition of {\hyperref[operators:operator-def-semilinear]{\emph{\DUspan{}{semiLinear}}}}). In some situations,
equations with the semiLinear() function become underdetermined if the
first argument (x) becomes zero, i.e., there are an infinite number of
solutions. It is recommended that the following rules are used to
transform the equations during the translation phase in order to select
one meaningful solution in such cases:

\textbf{Rule 1}: The equations

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n}{y} \PYG{o}{=} \PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{sa}\PYG{p}{,} \PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]);}
\PYG{n}{y} \PYG{o}{=} \PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{],} \PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{]);}
\PYG{n}{y} \PYG{o}{=} \PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{],} \PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{]);}
\PYG{c+c1}{// ...}
\PYG{n}{y} \PYG{o}{=} \PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{s}\PYG{p}{[}\PYG{n}{n}\PYG{p}{],} \PYG{n}{sb}\PYG{p}{);}
\PYG{c+c1}{// ...}
\end{Verbatim}

may be replaced by

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{]} \PYG{o}{=} \PYG{k+kr}{if} \PYG{n}{x} \PYG{o}{\PYGZgt{}=} \PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{n}{sa} \PYG{k+kr}{else} \PYG{n}{sb}
\PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{]} \PYG{o}{=} \PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{1}\PYG{p}{];}
\PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{3}\PYG{p}{]} \PYG{o}{=} \PYG{n}{s}\PYG{p}{[}\PYG{l+m+mi}{2}\PYG{p}{];}
\PYG{c+c1}{// ...}
\PYG{n}{s}\PYG{p}{[}\PYG{n}{n}\PYG{p}{]} \PYG{o}{=} \PYG{n}{s}\PYG{p}{[}\PYG{n}{n}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{1}\PYG{p}{];}
\PYG{n}{y} \PYG{o}{=} \PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{sa}\PYG{p}{,} \PYG{n}{sb}\PYG{p}{);}
\end{Verbatim}

\textbf{Rule 2}: The equations

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n}{x} \PYG{o}{=} \PYG{l+m+mi}{0}\PYG{p}{;}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{0}\PYG{p}{;}
\PYG{n}{y} \PYG{o}{=} \PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{n}{x}\PYG{p}{,} \PYG{n}{sa}\PYG{p}{,} \PYG{n}{sb}\PYG{p}{);}
\end{Verbatim}

may be replaced by

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n}{x} \PYG{o}{=} \PYG{l+m+mi}{0}
\PYG{n}{y} \PYG{o}{=} \PYG{l+m+mi}{0}\PYG{p}{;}
\PYG{n}{sa} \PYG{o}{=} \PYG{n}{sb}\PYG{p}{;}
\end{Verbatim}

{[}\emph{For symbolic transformations, the following property is useful (this
follows from the definition):}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{n}{m\PYGZus{}flow}\PYG{p}{,} \PYG{n}{port\PYGZus{}h}\PYG{p}{,} \PYG{n}{h}\PYG{p}{);}
\end{Verbatim}

\emph{is identical to :}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{o}{\PYGZhy{}}\PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{o}{\PYGZhy{}}\PYG{n}{m\PYGZus{}flow}\PYG{p}{,} \PYG{n}{h}\PYG{p}{,} \PYG{n}{port\PYGZus{}h}\PYG{p}{);}
\end{Verbatim}

\emph{The} \code{semiLinear} \emph{function is designed to handle reversing flow in fluid
systems, such as}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n}{H\PYGZus{}flow}\PYG{o}{=}\PYG{n+nb}{semiLinear}\PYG{p}{(}\PYG{n}{m\PYGZus{}flow}\PYG{p}{,} \PYG{n}{port}\PYG{o}{.}\PYG{n}{h}\PYG{p}{,} \PYG{n}{h}\PYG{p}{);}
\end{Verbatim}

\emph{i.e., the enthalpy flow rate} \code{H\_flow} \emph{is computed from the mass flow
rate} \code{m\_flow} \emph{and the upstream specific enthalpy depending on the flow
direction.}

{]}


\subsubsection{getInstanceName}
\label{operators:operator-instancename}\label{operators:getinstancename}
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.

{[}\emph{Example:}

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{package} \PYG{n+nc}{MyLib}
  \PYG{k+kr}{model} \PYG{n+nc}{Vehicle}
  \PYG{n}{Engine} \PYG{n}{engine}\PYG{p}{;}
  \PYG{o}{.}\PYG{o}{.}\PYG{o}{.}
  \PYG{k+kr}{end} \PYG{n+nc}{Vehicle}\PYG{p}{;}

  \PYG{k+kr}{model} \PYG{n+nc}{Engine}
  \PYG{n}{Controller} \PYG{n}{controller}\PYG{p}{;}
  \PYG{o}{.}\PYG{o}{.}\PYG{o}{.}
  \PYG{k+kr}{end} \PYG{n+nc}{Engine}\PYG{p}{;}

  \PYG{k+kr}{model} \PYG{n+nc}{Controller}
  \PYG{k+kr}{equation}
  \PYG{n}{Modelica}\PYG{o}{.}\PYG{n}{Utilities}\PYG{o}{.}\PYG{n}{Streams}\PYG{o}{.}\PYG{n}{print}\PYG{p}{(}\PYG{l+s+s2}{\PYGZdq{}}\PYG{l+s+s2}{Info from: }\PYG{l+s+s2}{\PYGZdq{}} \PYG{o}{+} \PYG{n+nb}{getInstanceName}\PYG{p}{());}
  \PYG{k+kr}{end} \PYG{n+nc}{Controller}\PYG{p}{;}

\PYG{k+kr}{end} \PYG{n+nc}{MyLib}\PYG{p}{;}
\end{Verbatim}

\emph{If MyLib.Vehicle is simulated, the call of getInstanceName()
returns:''Vehicle.engine.controller''}

{]}

If this function is not called inside a model or block (e.g. the
function is called in a function or in a constant of a package), the
return value is not specified.

{[}\emph{The simulation result should not depend on the return value of this
function.} {]}


\subsection{Event-Related Operators with Function Syntax}
\label{operators:event-related-operators}\label{operators:event-related-operators-with-function-syntax}
The following event-related operators with function syntax are
supported. The operators noEvent, pre, edge, and change, are
vectorizable according to Section 12.4.6
\begin{description}
\item[{\code{initial()}}] \leavevmode
Returns true during the initialization phase and false otherwise {[}\emph{thereby triggering a time event at the beginning of a simulation}{]}.

\item[{\code{terminal()}}] \leavevmode
Returns true at the end of a successful analysis {[}\emph{thereby ensuring an event at the end of successful simulation}{]}.

\item[{\code{noEvent(expr)}}] \leavevmode
Real elementary relations within expr are taken literally, i.e., no state or time event is triggered. See also Section {\hyperref[operators:noevent-smooth]{\emph{\DUspan{}{noEvent and smooth}}}} and Section 8.5.

\item[{\code{smooth(p, expr)}}] \leavevmode
If p\textgreater{}=0 smooth(p,expr) returns expr and states that expr is p times
continuously differentiable, i.e.: expr is continuous in all real
variables appearing in the expression and all partial derivatives with
respect to all appearing real variables exist and are continuous up to
order p.
The argument p should be a scalar integer parameter expression.
The only allowed types for expr in smooth are: real expressions, arrays
of allowed expressions, and records containing only components of
allowed expressions. See also Section {\hyperref[operators:noevent-smooth]{\emph{\DUspan{}{noEvent and smooth}}}}.

\item[{\code{sample(start,interval)}}] \leavevmode
Returns true and triggers time events at time instants
\code{start + i*interval(i=0,1, ...)}.
During continuous integration the operator returns always false.
The starting time start and the sample interval interval need to be
parameter expressions and need to be a subtype of Real or Integer.                                                                                                                                                                                                                                                                                                                                                                                                                                           \textbar{}

\item[{\code{pre(y)}}] \leavevmode
Returns the ``left limit'' y(t$^{\text{pre}}$) of variable y(t) at a time
instant t. At an event instant, y(t$^{\text{pre}}$) is the value of y
after the last event iteration at time instant t (see comment below).
The pre() operator can be applied if the following three conditions
are fulfilled simultaneously:
\begin{enumerate}
\item {} 
variable y is either a subtype of a simple type or is a record component

\item {} 
y is a discrete-time expression

\item {} 
the operator is \emph{not} applied in a function class.

\end{enumerate}

{[}\emph{Note: This can be applied to continuous-time variables in when-clauses,
see Section :ref:{}`discrete-time-expressions{}` for the definition of discrete-time expression.}{]}
The first value of pre(y) is determined in the initialization phase. See also Section \DUspan{xref,std,std-ref}{operator-pre}.

\item[{\code{edge(b)}}] \leavevmode
Is expanded into \code{(b and not pre(b))} for Boolean variable \code{b}.
The same restrictions as for the \code{pre()} operator apply (e.g. not to be used in function classes).

\item[{\code{change(v)}}] \leavevmode
Is expanded into \code{(v\textless{}\textgreater{}pre(v))}.
The same restrictions as for the \code{pre()} operator apply.

\item[{\code{reinit(x, expr)}}] \leavevmode
In the body of a when clause, reinitializes \code{x} with \code{expr} at an event instant.
\code{x} is a Real variable (or an array of Real variables) that is
implicitly defined to have \code{StateSelect.always} {[}\emph{so must be selected as a state, and it is an error, if this is not possible}{]}.
expr needs to be type-compatible with x.
The \code{reinit} operator can only be applied once for the same variable
- either as an individual variable or as part of an array of variables.
It can only be applied in the body of a when clause in an equation section.
See also Section \DUspan{xref,std,std-ref}{TODO: 8.3.6}.

\end{description}

A few of these operators are described in more detail in the following.


\subsubsection{pre}
\label{operators:pre}
A new event is triggered if at least for one variable \code{v} \code{pre(v) \textless{}\textgreater{} v}
after the active model equations are evaluated at an event instant. In
this case the model is at once reevaluated. This evaluation sequence is
called “\emph{event iteration}”. The integration is restarted, if for all
v used in pre-operators the following condition holds: \code{pre(v) == v}.

{[}\emph{If} \code{v} \emph{and} \code{pre(v)} \emph{are only used in when-clauses, the translator
might mask event iteration for variable v since v cannot change during
event iteration. It is a “quality of implementation” to find the minimal
loops for event iteration, i.e., not all parts of the model need to be
reevaluated.}

\emph{The language allows mixed algebraic systems of equations where the
unknown variables are of type Real, Integer, Boolean, or an enumeration.
These systems of equations can be solved by a global fix point iteration
scheme, similarly to the event iteration, by fixing the Boolean,
Integer, and/or enumeration unknowns during one iteration. Again, it is
a quality of implementation to solve these systems more efficiently,
e.g., by applying the fix point iteration scheme to a subset of the
model equations.}{]}


\subsubsection{noEvent and smooth}
\label{operators:noevent-and-smooth}\label{operators:noevent-smooth}
The noEvent operator implies that real elementary expressions are taken
literally instead of generating crossing functions, Section \DUspan{xref,std,std-ref}{TODO: 8.5}.
The smooth operator should be used instead of noEvent, in order to avoid
events for efficiency reasons. A tool is free to not generate events for
expressions inside smooth. However, smooth does not guarantee that no
events will be generated, and thus it can be necessary to use noEvent
inside smooth. {[}\emph{Note that} smooth \emph{does not guarantee a smooth output
if any of the occurring variables change discontinuously.}{]}

{[}\emph{Example}:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{n+nb}{Real} \PYG{n}{x}\PYG{p}{,}\PYG{n}{y}\PYG{p}{,}\PYG{n}{z}\PYG{p}{;}
\PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{p}\PYG{p}{;}
\PYG{k+kr}{equation}
\PYG{n}{x} \PYG{o}{=} \PYG{k+kr}{if} \PYG{n+nb}{time}\PYG{o}{\PYGZlt{}}\PYG{l+m+mi}{1} \PYG{k+kr}{then} \PYG{l+m+mi}{2} \PYG{k+kr}{else} \PYG{n+nb}{time}\PYG{o}{\PYGZhy{}}\PYG{l+m+mi}{2}\PYG{p}{;}
\PYG{n}{z} \PYG{o}{=} \PYG{n+nb}{smooth}\PYG{p}{(}\PYG{l+m+mi}{0}\PYG{p}{,} \PYG{k+kr}{if} \PYG{n+nb}{time}\PYG{o}{\PYGZlt{}}\PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{l+m+mi}{0} \PYG{k+kr}{else} \PYG{n+nb}{time}\PYG{p}{);}
\PYG{n}{y} \PYG{o}{=} \PYG{n+nb}{smooth}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{,} \PYG{n+nb}{noEvent}\PYG{p}{(}\PYG{k+kr}{if} \PYG{n}{x}\PYG{o}{\PYGZlt{}}\PYG{l+m+mi}{0} \PYG{k+kr}{then} \PYG{l+m+mi}{0} \PYG{k+kr}{else} \PYG{n+nb}{sqrt}\PYG{p}{(}\PYG{n}{x}\PYG{p}{)}\PYG{o}{*}\PYG{n}{x}\PYG{p}{));}
\PYG{c+c1}{// noEvent is necessary.}
\end{Verbatim}

{]}


\section{Variability of Expressions}
\label{operators:variability-of-expressions}
The concept of variability of an expression indicates to what extent the
expression can vary over time. See also Section \DUspan{xref,std,std-ref}{TODO: 4.4.4}
regarding the concept of variability.
There are four levels of variability of expressions, starting from the
least variable:
\begin{itemize}
\item {} 
constant variability

\item {} 
parameter variability

\item {} 
discrete-time variability

\item {} 
continuous-time variability

\end{itemize}

For an assignment \code{v:=expr} or binding equation \code{v=expr}, \code{v} must be declared
to be at least as variable as \code{expr}.
\begin{itemize}
\item {} 
The right-hand side expression in a binding equation {[}\emph{that is},
\code{expr} {]} of a parameter component and of the base type attributes
{[}\emph{such as} \code{start}{]} needs to be a parameter or constant expression.

\item {} 
If \code{v} is a discrete-time component then \code{expr} needs to be a
discrete-time expression.

\end{itemize}


\subsection{Constant Expressions}
\label{operators:constant-expressions}
Constant expressions are:
\begin{itemize}
\item {} 
Real, Integer, Boolean, String, and enumeration literals.

\item {} 
Variables declared as constant.

\item {} 
Except for the special built-in operators initial, terminal, der,
edge, change, sample, and pre, a function or operator with constant
subexpressions as argument (and no parameters defined in the
function) is a constant expression.

\end{itemize}

Components declared as constant shall have an associated declaration
equation with a constant expression, if the constant is directly in the
simulation model, or used in the simulation model. The value of a
constant can be modified after it has been given a value, unless the
constant is declared final or modified with a final modifier. A constant
without an associated declaration equation can be given one by using a
modifier.


\subsection{Parameter Expressions}
\label{operators:parameter-expressions}
Parameter expressions are:
\begin{itemize}
\item {} 
Constant expressions.

\item {} 
Variables declared as parameter.

\item {} 
Except for the special built-in operators initial, terminal, der,
edge, change, sample, and pre, a function or operator with parameter
subexpressions is a parameter expression.

\end{itemize}


\subsection{Discrete-Time Expressions}
\label{operators:discrete-time-expressions}\label{operators:id3}
Discrete-time expressions are:
\begin{itemize}
\item {} 
Parameter expressions.

\item {} 
Discrete-time variables, i.e., Integer, Boolean, String variables and
enumeration variables, as well as Real variables assigned in
when-clauses

\item {} 
Function calls where all input arguments of the function are
discrete-time expressions.

\item {} 
Expressions where all the subexpressions are discrete-time
expressions.

\item {} 
Expressions in the body of a when-clause, initial equation, or
initial algorithm.

\item {} 
Unless inside noEvent: Ordered relations (\code{\textgreater{}}, \code{\textless{}}, \code{\textgreater{}=}, \code{\textless{}=}) if at least one
operand is a subtype of Real (i.e. Real elementary relations, see
Section {\hyperref[operators:eq-relational-logic-operators]{\emph{\DUspan{}{Equality, Relational, and Logical Operators}}}}) and the functions \code{ceil},
\code{floor}, \code{div}, \code{mod}, \code{rem}.
These will generate events if at least one subexpression is not a
discrete-time expression. {[}\emph{In other words, relations inside} \code{noEvent()} \emph{,
such as} \code{noEvent(x\textgreater{}1)}\emph{, are not discrete-time expressions}{]}.

\item {} 
The functions \code{pre}, \code{edge}, and \code{change} result in discrete-time
expressions.

\item {} 
Expressions in functions behave as though they were discrete-time
expressions.

\end{itemize}

For an equation \code{expr1 = expr2} where neither expression is of base type
Real, both expressions must be discrete-time expressions. For record
equations the equation is split into basic types before applying this
test. {[}\emph{This restriction guarantees that the} \code{noEvent()} \emph{operator cannot
be applied to} \code{Boolean}\emph{,} \code{Integer}\emph{,} \code{String}\emph{, or enumeration
equations outside of a when-clause, because then one of the two
expressions is not discrete-time}{]}

Inside an if-expression, if-clause, while-statement or for-clause, that
is controlled by a non-discrete-time (that is continuous-time, but not
discrete-time) switching expression and not in the body of a
when-clause, it is not legal to have assignments to discrete variables,
equations between discrete-time expressions, or real elementary
relations/functions that should generate events. {[}\emph{This restriction is
necessary in order to guarantee that there all equations for discrete
variable are discrete-time expressions, and to ensure that crossing
functions do not become active between events.}{]}

{[}\emph{Example}:

\begin{Verbatim}[commandchars=\\\{\}]
\PYG{k+kr}{model} \PYG{n+nc}{Constants}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{p1} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{p}{;}
  \PYG{k+kr}{constant} \PYG{n+nb}{Real} \PYG{n}{c1} \PYG{o}{=} \PYG{n}{p1} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{;} \PYG{c+c1}{// error, no constant expression}
  \PYG{k+kr}{parameter} \PYG{n+nb}{Real} \PYG{n}{p2} \PYG{o}{=} \PYG{n}{p1} \PYG{o}{+} \PYG{l+m+mi}{2}\PYG{p}{;} \PYG{c+c1}{// fine}
\PYG{k+kr}{end} \PYG{n+nc}{Constants}\PYG{p}{;}

\PYG{k+kr}{model} \PYG{n+nc}{Test}
  \PYG{n}{Constants} \PYG{n}{c1}\PYG{p}{(}\PYG{n}{p1}\PYG{o}{=}\PYG{l+m+mi}{3}\PYG{p}{);} \PYG{c+c1}{// fine}
  \PYG{n}{Constants} \PYG{n}{c2}\PYG{p}{(}\PYG{n}{p2}\PYG{o}{=}\PYG{l+m+mi}{7}\PYG{p}{);} \PYG{c+c1}{// fine, declaration equation can be modified}
  \PYG{n+nb}{Boolean} \PYG{n}{b}\PYG{p}{;}
  \PYG{n+nb}{Real} \PYG{n}{x}\PYG{p}{;}
\PYG{k+kr}{equation}
  \PYG{n}{b} \PYG{o}{=} \PYG{n+nb}{noEvent}\PYG{p}{(}\PYG{n}{x} \PYG{o}{\PYGZgt{}} \PYG{l+m+mi}{1}\PYG{p}{)} \PYG{c+c1}{// error, since b is a discrete\PYGZhy{}time expr. and}
  \PYG{c+c1}{// noEvent(x \PYGZgt{} 1) is not a discrete\PYGZhy{}time expr.}
\PYG{k+kr}{end} \PYG{n+nc}{Test}\PYG{p}{;}
\end{Verbatim}

{]}


\subsection{Continuous-Time Expressions}
\label{operators:continuous-time-expressions}
All expressions are continuous-time expressions including constant,
parameter and discrete expressions. The term “non-discrete-time
expression” refers to expressions that are not constant, parameter or
discrete expressions.


\chapter{Indices and tables}
\label{index:indices-and-tables}\begin{itemize}
\item {} 
\DUspan{xref,std,std-ref}{genindex}

\item {} 
\DUspan{xref,std,std-ref}{modindex}

\item {} 
\DUspan{xref,std,std-ref}{search}

\end{itemize}



\renewcommand{\indexname}{Index}
\printindex
\end{document}
