Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion chapters/equations.tex
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,9 @@ \section{Events and Synchronization}\label{events-and-synchronization}
An \firstuse{event} is something that occurs instantaneously at a specific time or when a specific condition occurs.
Events are for example defined by the condition occurring in a \lstinline!when!-clause, \lstinline!if!-equation, or \lstinline!if!-expression.

The integration is halted and an event occurs whenever an event generation expression, e.g., \lstinline!x > 2! or \lstinline!floor(x)!, changes its value.
The integration is halted and an event occurs whenever an event generating expression, e.g., \lstinline!x > 2! or \lstinline!floor(x)!, changes its value.
An event generating expression has an internal buffer, and the value of the expression can only be changed at event instants.
In an algorithm in a model or block each expression is compared to the previous value when executing the corresponding statement, where each iteration of statements inside for-loops are seen as different statements.
If the evaluated expression is inconsistent with the buffer, that will trigger an event and the buffer will be updated with a new value at the event instant.
During continuous integration event generation expression has the constant value of the expression from the last event instant.

Expand All @@ -610,6 +611,50 @@ \section{Events and Synchronization}\label{events-and-synchronization}
This requirement can be fulfilled if \lstinline!Real! elementary relations are not treated literally but as defined above, because discontinuous changes can only occur at event instants and no longer during continuous integration.
\end{example}

\begin{example}
Event generation inside algorithms.
\begin{lstlisting}[language=modelica]
model AlgorithmEvent1
Real x;
Integer i;
algorithm
i := 0;
x := sin(time);
if x < 0 then // <-- relation 1
x := 0;
i := 1;
end if;
x := cos(time);
if x < 0 then // <-- relation 2
x := 0;
i := i+2;
end if;
x := 3.5;
annotation(experiment(StopTime=10));
end AlgorithmEvent1;
\end{lstlisting}
This example works as if the first \lstinline!x < 0! was replaced by \lstinline!sin(time) < 0! and the second by \lstinline!cos(time) < 0!.
\end{example}

\begin{example}
Event generation and loops.
\begin{lstlisting}[language=modelica]
model AlgorithmEvent2
Real x[n](start = 2:4);
parameter Integer n = 3 annotation(Evaluate=true);
Boolean b[n];
equation
der(x) = -2*x;
algorithm
for i in 1:n loop
b[i] := x[i] > 1;
end for;
end AlgorithmEvent2;
\end{lstlisting}
Here we get one crossing function for each element of \lstinline!x!, three in total.
Comment on lines +641 to +654
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really make such a big point of how a for-statement works when we don't have an example with a for-equation? For me, it would make more sense to just split the example in two, and make the second one primarily about for-equations, possibly also mentioning that for-statements are similar.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see reasons for making a big point about for-statements, but I wouldn't mind a for-equation example.

The reason is that traditionally for-equations were normally expanded, and adding crossing functions to expanded equations seems kind of trivial. For-statements are different (as they are not expanded), so one "line" in the source-code leads to multiple crossing functions. Both as a user and implementor I find that significant enough to have a special example.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split into two example-sections, and add that for-equation text in second example-section.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

A corresponding \lstinline!for!-equation (which is normally expanded), would also generate three crossing functions.
\end{example}

\begin{nonnormative}
It is a quality of implementation issue that the following special relations
\begin{lstlisting}[language=modelica]
Expand Down