-
Notifications
You must be signed in to change notification settings - Fork 43
Add variability change for these functions. #3610
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
base: master
Are you sure you want to change the base?
Changes from all commits
5878d52
7b49a58
b760714
3acf849
b1cb03a
aa0e9ec
fb13f66
cefd7a5
54a38c3
a99f180
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -171,7 +171,7 @@ \section{Function as a Specialized Class}\label{function-as-a-specialized-class} | |||||
\item | ||||||
For initialization of local variables of a function see \cref{initialization-and-binding-equations-of-components-in-functions}). | ||||||
\item | ||||||
Components of a function will inside the function behave as though they had discrete-time variability. | ||||||
Components of a function will inside the function with some exceptions behave as though they had discrete-time variability, see \cref{discrete-time-expressions} for details. | ||||||
\end{itemize} | ||||||
|
||||||
Modelica functions have the following enhancements compared to a general Modelica \lstinline!class!: | ||||||
|
@@ -1762,6 +1762,72 @@ \section{Function Inlining and Event Generation}\label{function-inlining-and-eve | |||||
|
||||||
If the function is called in a context where events will not be generated (e.g., inside another function without {\lstinline!GenerateEvents = true!}) no special action is needed. | ||||||
\end{nonnormative} | ||||||
\begin{example} | ||||||
The annotation also impacts the variability, \cref{variability-of-expressions} consider a trivial case. | ||||||
\begin{lstlisting}[language=modelica] | ||||||
function greaterEqual | ||||||
input Real x1; | ||||||
input Real x2; | ||||||
output Boolean b; | ||||||
algorithm | ||||||
b := x1 >= x2; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excessive space:
Suggested change
|
||||||
annotation(GenerateEvents=true, LateInline=true); | ||||||
end greaterEqual; | ||||||
|
||||||
function greaterEqualWO | ||||||
input Real x1; | ||||||
input Real x2; | ||||||
output Boolean b; | ||||||
algorithm | ||||||
b := x1 >= x2; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Excessive space:
Suggested change
|
||||||
annotation(LateInline=true); | ||||||
end greaterEqualWO; | ||||||
|
||||||
model M | ||||||
Boolean b1 = greaterEqual(time, 1); | ||||||
Boolean b2 = greaterEqualWO(time, 1) "Illegal"; | ||||||
Boolean b3 = noEvent(greaterEqual(time, 1)) "Illegal"; | ||||||
Boolean b4 = greaterEqualWO(if b1 then 2 else 0, 1); | ||||||
Real z = if greaterEqualWO(time, 1) then 2 else 1; | ||||||
end M; | ||||||
|
||||||
function bad | ||||||
input Real x1; | ||||||
output Integer i; | ||||||
algorithm | ||||||
i := greaterEqualWO(x1, 1) then 2 else 1; | ||||||
annotation(GenerateEvents=true, LateInline=true); | ||||||
end bad; | ||||||
|
||||||
function saturatedSquare | ||||||
input Real x2; | ||||||
output Real y; | ||||||
algorithm | ||||||
if x2 > 1 then | ||||||
y := 1; | ||||||
else | ||||||
y := x2*x2; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have a nice operator for the square:
Suggested change
|
||||||
end if; | ||||||
end saturatedSquare; | ||||||
|
||||||
function isBelowSat | ||||||
input Real x1, x2; | ||||||
output Boolean b; | ||||||
algorithm | ||||||
b : = x1 < saturatedSquare(x2); | ||||||
annotation (GenerateEvents=true, LateInline=true); | ||||||
end isBelowSat; | ||||||
\end{lstlisting} | ||||||
In \lstinline!M! there will be an event when \lstinline!time! becomes greater or equal to \lstinline!1!, and thus the discrete-time variable \lstinline!b1! can be bound to this function call even though it involves the non-discrete time variable \lstinline!time!. | ||||||
Both \lstinline!b2! and \lstinline!b3! are illegal, since the right hand sides have non-discrete-time variability. | ||||||
The variable \lstinline!z! is legal, since a Real variable can have non-discrete time variability. | ||||||
The variable \lstinline!b4! is legal, since the function inputs have discrete-time variability. | ||||||
The function \lstinline!bad! demonstrates that similar restrictions (as for \lstinline!b2! and \lstinline!b3!) apply inside functions with {\lstinline!GenerateEvents = true!}. | ||||||
The function \lstinline!isBelowSat! demonstrates that it is possible to call functions that do not generate events (like \lstinline!saturatedSquare!) inside functions with {\lstinline!GenerateEvents = true!}. | ||||||
Combined the last two examples demonstrate that {\lstinline!GenerateEvents = true!} does not propagate to called functions. | ||||||
|
||||||
The annotation \lstinline!LateInline=true! is not needed, but makes the issue more obvious in tools that normally inline function before checking variability. | ||||||
\end{example} | ||||||
\end{semantics} | ||||||
\end{annotationdefinition} | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -1582,7 +1582,7 @@ \subsection{Evaluable Expressions}\label{evaluable-expressions} | |||
\item | ||||
Evaluable parameter variables, see \cref{component-variability}. | ||||
\item | ||||
Input variables in functions behave as though they were evaluable expressions. | ||||
Input variables in functions not having annotation \lstinline!GenerateEvents = true! (\Cref{modelica:GenerateEvents}) behave as though they were evaluable expressions. | ||||
\item | ||||
Except for the special built-in operators \lstinline!initial!, \lstinline!terminal!, \lstinline!der!, \lstinline!edge!, \lstinline!change!, \lstinline!sample!, and \lstinline!pre!, a function or operator with evaluable subexpressions is an evaluable expression. | ||||
\item | ||||
|
@@ -1593,7 +1593,7 @@ \subsection{Evaluable Expressions}\label{evaluable-expressions} | |||
\item | ||||
\lstinline!cardinality(c)!, see restrictions for use in \cref{cardinality-deprecated}. | ||||
\item | ||||
\lstinline!size(A)! (including \lstinline!size(A, j)! where \lstinline!j! is an evaluable expression) if \lstinline!A! is variable declared in a non-function class. | ||||
\lstinline!size(A)! (including \lstinline!size(A, j)! where \lstinline!j! is an evaluable expression) if \lstinline!A! is variable declared in a non-function class, or as input in a function having annotation \lstinline!GenerateEvents = true! (\Cref{modelica:GenerateEvents}). | ||||
\item | ||||
\lstinline!Connections.isRoot(A.R)! | ||||
\item | ||||
|
@@ -1644,10 +1644,14 @@ \subsection{Discrete-Time Expressions}\label{discrete-time-expressions} | |||
Note that \lstinline!rem! and \lstinline!mod! generate events but are not discrete-time expressions. | ||||
In other words, relations inside \lstinline!noEvent!, such as \lstinline!noEvent(x>1)!, are not discrete-time expressions. | ||||
\end{nonnormative} | ||||
\item | ||||
Unless inside \lstinline!noEvent!: Function calls where the function has annotation \lstinline!GenerateEvents = true! (\Cref{modelica:GenerateEvents}), the output does not contain a subtype of \lstinline!Real!, and any non-\lstinline!Real! inputs have discrete-time variability. | ||||
For a function call returning multiple return values (see \cref{output-formal-parameters-of-functions}) the variability is decided separately for each output. | ||||
These calls will generate events, \Cref{modelica:GenerateEvents}. | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this sentence is more of a distraction than helpful here:
Suggested change
|
||||
\item | ||||
The functions \lstinline!pre!, \lstinline!edge!, and \lstinline!change! result in discrete-time expressions. | ||||
\item | ||||
Expressions in functions behave as though they were discrete-time expressions. | ||||
Expressions in functions not having annotation \lstinline!GenerateEvents = true! (\Cref{modelica:GenerateEvents}), behave as though they were discrete-time expressions. | ||||
HansOlsson marked this conversation as resolved.
Show resolved
Hide resolved
|
||||
\end{itemize} | ||||
|
||||
Inside an \lstinline!if!-expression, \lstinline!if!-clause, \lstinline!while!-statement or \lstinline!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 \lstinline!when!-clause, it is not legal to have assignments to discrete-time variables, equations between discrete-time expressions, or real elementary relations/functions that should generate events. | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Below, it is said that inputs behave as if they were evaluable. Maybe we should just drop this item now that the rules are becoming more complicated?