Skip to content

Commit eec24cf

Browse files
committed
Document MutatingFunctionalAffect
1 parent b96cd2e commit eec24cf

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/systems/callbacks.jl

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,29 @@ function namespace_affect(affect::FunctionalAffect, s)
7272
end
7373

7474
"""
75-
`MutatingFunctionalAffect` differs from `FunctionalAffect` in two key ways:
76-
* First, insetad of the `u` vector passed to `f` being a vector of indices into `integ.u` it's instead the result of evaluating `obs` at the current state, named as specified in `obs_syms`. This allows affects to easily access observed states and decouples affect inputs from the system structure.
77-
* Second, it abstracts the assignment back to system states away. Instead of writing `integ.u[u.myvar] = [whatever]`, you instead declare in `mod_params` that you want to modify `myvar` and then either (out of place) return a named tuple with `myvar` or (in place) modify the associated element in the ComponentArray that's given.
78-
Initially, we only support "flat" states in `modified`; these states will be marked as irreducible in the overarching system and they will simply be bulk assigned at mutation. In the future, this will be extended to perform a nonlinear solve to further decouple the affect from the system structure.
75+
MutatingFunctionalAffect(f::Function; observed::NamedTuple, modified::NamedTuple, ctx)
76+
77+
`MutatingFunctionalAffect` is a helper for writing affect functions that will compute observed values and
78+
ensure that modified values are correctly written back into the system. The affect function `f` needs to have
79+
one of three signatures:
80+
* `f(observed::ComponentArray)` if the function only reads observed values back from the system,
81+
* `f(observed::ComponentArray, modified::ComponentArray)` if the function also writes values (unknowns or parameters) into the system,
82+
* `f(observed::ComponentArray, modified::ComponentArray, ctx)` if the function needs the user-defined context,
83+
* `f(observed::ComponentArray, modified::ComponentArray, ctx, integrator)` if the function needs the low-level integrator.
84+
85+
The function `f` will be called with `observed` and `modified` `ComponentArray`s that are derived from their respective `NamedTuple` definitions.
86+
Each `NamedTuple` should map an expression to a symbol; for example if we pass `observed=(; x = a + b)` this will alias the result of executing `a+b` in the system as `x`
87+
so the value of `a + b` will be accessible as `observed.x` in `f`. `modified` currently restricts symbolic expressions to only bare variables, so only tuples of the form
88+
`(; x = y)` or `(; x)` (which aliases `x` as itself) are allowed.
89+
90+
Both `observed` and `modified` will be automatically populated with the current values of their corresponding expressions on function entry.
91+
The values in `modified` will be written back to the system after `f` returns. For example, if we want to update the value of `x` to be the result of `x + y` we could write
92+
93+
MutatingFunctionalAffect(observed=(; x_plus_y = x + y), modified=(; x)) do o, m
94+
m.x = o.x_plus_y
95+
end
96+
97+
The affect function updates the value at `x` in `modified` to be the result of evaluating `x + y` as passed in the observed values.
7998
"""
8099
@kwdef struct MutatingFunctionalAffect
81100
f::Any
@@ -174,6 +193,7 @@ Affects (i.e. `affect` and `affect_neg`) can be specified as either:
174193
+ `read_parameters` is a vector of the parameters that are *used* by `f!`. Their indices are passed to `f` in `p` similarly to the indices of `unknowns` passed in `u`.
175194
+ `modified_parameters` is a vector of the parameters that are *modified* by `f!`. Note that a parameter will not appear in `p` if it only appears in `modified_parameters`; it must appear in both `parameters` and `modified_parameters` if it is used in the affect definition.
176195
+ `ctx` is a user-defined context object passed to `f!` when invoked. This value is aliased for each problem.
196+
* A [`MutatingFunctionalAffect`](@ref); refer to its documentation for details.
177197
"""
178198
struct SymbolicContinuousCallback
179199
eqs::Vector{Equation}

0 commit comments

Comments
 (0)