Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify variability of functions. #2924

Merged
merged 20 commits into from Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions chapters/functions.tex
Expand Up @@ -116,6 +116,29 @@ \subsection{Inheritance of Functions}\label{inheritance-of-functions}
For example, it is possible to modify and extend a \lstinline!function! class to add default values for input variables.
\end{nonnormative}

A special case is defining a \lstinline!function! as a short-class definition with modifiers for inputs inside a model.
These default values, unless overridden in the function call, will then be considered for variability similarly as if they were given in the function call.
henrikt-ma marked this conversation as resolved.
Show resolved Hide resolved

\begin{example}
Demonstrating the variability implications.
Note that functions cannot directly use non-constants in enclosing scopes, so we cannot write \lstinline!input Real x1=x;! directly in \lstinline!foo!.
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
\begin{lstlisting}[language=modelica]
model M
function foo
input Real x1;
input Real x2=2;
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
output Real y;
algorithm
y:=x1+x2;
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
end foo;
Real x=time;
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
function f1=foo(x1=x);
constant Real z=f1(x2=1); // Illegal since 'x' is seen as argument
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
constant Real z2=f1(x1=2); // Legal, since 'x1' has a new value.
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
end M;
\end{lstlisting}
\end{example}

\section{Function as a Specialized Class}\label{function-as-a-specialized-class}

The function concept in Modelica is a specialized class (\cref{specialized-classes}).
Expand Down
12 changes: 11 additions & 1 deletion chapters/interface.tex
Expand Up @@ -537,6 +537,11 @@ \section{Function-Compatibility or Function-Subtyping for Functions}\label{funct
constraining interface of the function being redeclared.
\end{itemize}

Note that function-compatibilty does currently not ensure that the function call has the correct variability, and that must then be checked after re-declaration - due the possibility of default arguments with higher variability.
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
\begin{nonnormative}
Thus any call of a function that is replaceable will need to be checked after redeclarations.
\end{nonnormative}

henrikt-ma marked this conversation as resolved.
Show resolved Hide resolved
\begin{example}
Demonstrating a redeclaration using a function-compatible function
\begin{lstlisting}[language=modelica]
Expand All @@ -559,14 +564,16 @@ \section{Function-Compatibility or Function-Subtyping for Functions}\label{funct
end UseDriveLine;
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
Modelica.Mechanics.MultiBody.Interface.Frame_a frame_a;
replaceable function gravity = GravityInterface;
constant Real failed[:]=gravity({1,0,0}); // May fail
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
equation
frame_a.f = gravity(frame_a.r0);
// or gravity(position=frame_a.r0);
frame_a.t = zeros(3);
end Body;

model PlanetSimulation
function sunGravity = PointMassGravity (m=2e30);
parameter Modelica.Units.SI.Mass mSun=2e30;
function sunGravity = PointMassGravity (m=mSun);
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved
Body planet1(redeclare function gravity = sunGravity);
Body planet2(redeclare function gravity = PointMassGravity (m=2e30));
$\ldots$
Expand All @@ -577,6 +584,9 @@ \section{Function-Compatibility or Function-Subtyping for Functions}\label{funct
\lstinline!GravityInterface! (no default for \lstinline!m!), but \lstinline!sunGravity!
inside \lstinline!PlanetSimulation! is function-compatible with
\lstinline!GravityInterface!.

The constant failed in \lstinline!planet1!, will violate variability constraints, whereas it will work in \lstinline!planet2!.
The call \lstinline!gravity(frame_a.r0)! will work in both of them.
\end{example}

\section{Type Compatible Expressions}\label{type-compatible-expressions}
Expand Down
1 change: 1 addition & 0 deletions chapters/operatorsandexpressions.tex
Expand Up @@ -1378,6 +1378,7 @@ \section{Variability of Expressions}\label{variability-of-expressions}
If \lstinline!v! is a discrete-time component then \lstinline!expr! needs to be a
discrete-time expression.
\end{itemize}
Note that for a function call it is also necessary to consider the variability of any used default arguments, especially if the function is redeclared, see \cref{function-compatibility}.
HansOlsson marked this conversation as resolved.
Show resolved Hide resolved

\begin{example}
The (underdetermined) model \lstinline!Test! below illustrates two kinds of consequences due to variability constraints.
Expand Down