You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/basics/Events.md
+87-21
Original file line number
Diff line number
Diff line change
@@ -25,6 +25,67 @@ the event occurs). These can both be specified symbolically, but a more [general
25
25
functional affect](@ref func_affects) representation is also allowed, as described
26
26
below.
27
27
28
+
## Symbolic Callback Semantics
29
+
30
+
In callbacks, there is a distinction between values of the unknowns and parameters
31
+
*before* the callback, and the desired values *after* the callback. In MTK, this
32
+
is provided by the `Pre` operator. For example, if we would like to add 1 to an
33
+
unknown `x` in a callback, the equation would look like the following:
34
+
35
+
```julia
36
+
x ~Pre(x) +1
37
+
```
38
+
39
+
Non `Pre`-d values will be interpreted as values *after* the callback. As such,
40
+
writing
41
+
42
+
```julia
43
+
x ~ x +1
44
+
```
45
+
46
+
will be interpreted as an algebraic equation to be satisfied after the callback.
47
+
Since this equation obviously cannot be satisfied, an error will result.
48
+
49
+
Callbacks must maintain the consistency of DAEs, meaning that they must satisfy
50
+
all the algebraic equations of the system after their update. However, the affect
51
+
equations often do not fully specify which unknowns/parameters should be modified
52
+
to maintain consistency. To make this clear, MTK uses the following rules:
53
+
54
+
1. All unknowns are treated as modifiable by the callback. In order to enforce that an unknown `x` remains the same, one can add `x ~ Pre(x)` to the affect equations.
55
+
2. All parameters are treated as un-modifiable, *unless* they are declared as `discrete_parameters` to the callback. In order to be a discrete parameter, the parameter must be time-dependent (the terminology *discretes* here means [discrete variables](@ref save_discretes)).
56
+
57
+
For example, consider the following system.
58
+
59
+
```julia
60
+
@variablesx(t) y(t)
61
+
@parametersp(t)
62
+
@mtkbuild sys =ODESystem([x * y ~ p, D(x) ~0], t)
63
+
event = [t ==1] => [x ~Pre(x) +1]
64
+
```
65
+
66
+
By default what will happen is that `x` will increase by 1, `p` will remain constant,
67
+
and `y` will change in order to compensate the increase in `x`. But what if we
68
+
wanted to keep `y` constant and change `p` instead? We could use the callback
0 commit comments