Skip to content

Commit a94a05a

Browse files
committed
improve callback docs
1 parent 277275e commit a94a05a

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

docs/make.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ using ModelingToolkit
88
using DocumenterInterLinks
99

1010
links = InterLinks(
11-
"diffeq" => "https://diffeq.sciml.ai/stable/",
12-
"mtk" => "https://mtk.sciml.ai/stable/",
13-
"sii" => "https://docs.sciml.ai/SymbolicIndexingInterface/stable/",
11+
"DiffEq" => "https://docs.sciml.ai/DiffEqDocs/stable/",
12+
"MTK" => "https://docs.sciml.ai/ModelingToolkit/stable/",
13+
"SymbolicIndexingInterface" => "https://docs.sciml.ai/SymbolicIndexingInterface/stable/",
1414
)
1515

1616
# generate examples

docs/src/API.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ set_bounds!
127127
## Callbacks API
128128
### Define Callbacks
129129
```@docs
130-
ComponentCallback
130+
NetworkDynamics.ComponentCallback
131131
ContinousComponentCallback
132132
VectorContinousComponentCallback
133133
ComponentCondition

docs/src/callbacks.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
# Callbacks and Events (@id Callbacks)
1+
# [Callbacks and Events](@id Callbacks)
22

33
Callback-functions are a way of handling discontinuities in differential equations.
44
In a nutshell, the solver checks for some "condition" (i.e. a zero crossing of some variable)
55
and calls some "affect" if the condition is fulfilled.
6-
In the affect it is safe to modify the integrator, e.g. changing some state or some parameter.
6+
Within the affect function, it is safe to modify the integrator, e.g. changing some state or some parameter.
77

88
Since `NetworkDynamics.jl` provides nothing more than a RHS for DifferentialEquations.jl, please check
99
[their docs on event handling](https://docs.sciml.ai/DiffEqDocs/stable/features/callback_functions/)
1010
as a general reference.
11-
1211
This page at introducing the general concepts, for a hands on example of a simulation with callbacks
1312
refer to the [Cascading Failure](@ref) example.
1413

@@ -84,7 +83,7 @@ add_callback!(vert, vccb)
8483

8584

8685
### Extracting the Callback
87-
In order to use the callback during simulation, we need to generate a [`SciMLBase.CallbackSet`](@extref) which contains the conditions and affects of all the component based callbacks in the network. For that we use [`get_callbacks(::Network)`](@ref `get_callbacks(::NetworkDynamics.Network)`):
86+
In order to use the callback during simulation, we need to generate a [`SciMLBase.CallbackSet`](@extref) which contains the conditions and affects of all the component based callbacks in the network. For that we use [`get_callbacks(::Network)`](@ref):
8887
```julia
8988
u0 = NWState(u0)
9089
cbs = get_callbacks(nw)
@@ -105,7 +104,7 @@ To access states and parameters of specific components, we havily rely on the [S
105104
using SymbolicIndexingInterface as SII
106105
nw = Network(#= some network =#)
107106

108-
condition = let getvalue = SII.getu(nw, VIndex(1:5, :some_state))
107+
condition = let getvalue = SII.getsym(nw, VIndex(1:5, :some_state))
109108
function(out, u, t, integrator)
110109
s = NWState(integrator, u, integrator.p, t)
111110
some_state = getvalue(s)
@@ -115,7 +114,7 @@ end
115114
```
116115
Please not a few important things here:
117116
- Symbolic indexing can be costly, and the condition function gets called very
118-
often. By using [`SII.getu`](@extref `SymbolicIndexingInterface.getu`) we did
117+
often. By using [`SII.getsym`](@extref `SymbolicIndexingInterface.getsym`) we did
119118
some of the work *before* the callback by creating the accessor function.
120119
When handling with "normal states" and parameters consider using
121120
[`SII.variable_index`](@ref `SymbolicIndexingInterface.variable_index`) and
@@ -150,4 +149,4 @@ Once the `condition` and `affect!` is defined, you can use the [`SciMLBase.Conti
150149
To extract or plot timeseries of observed states under *time variant
151150
parameters* (i.e. parameters that are changed in a callback), those changes
152151
need to be recorded using the [`save_parameters!`](@ref) function whenever `p` is changed.
153-
When using [ComponentCallback](@ref), NetworkDynamics will automaticially check for changes in `p` and save them if necessary.
152+
When using [ComponentCallback](@ref NetworkDynamics.ComponentCallback), NetworkDynamics will automaticially check for changes in `p` and save them if necessary.

src/NetworkDynamics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ include("metadata.jl")
7777

7878
export ComponentCondition, ComponentAffect
7979
export ContinousComponentCallback, VectorContinousComponentCallback
80-
export SymbolicView, get_callbacks
80+
export SymbolicView
8181
include("callbacks.jl")
8282

8383
using NonlinearSolve: AbstractNonlinearSolveAlgorithm, NonlinearFunction

src/callbacks.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Creates a callback condition for a [`ComponentCallback`].
6666
- `event_idx`: The current event index, i.e. which `out` element triggerd in case of [`VectorContinousComponentCallback`](@ref).
6767
- `ctx::NamedTuple` a named tuple with context variables.
6868
- `ctx.model`: a referenc to the ocmponent model
69-
- `ctx.vidx`/ctx.eidx: The index of the vertex/edge model.
69+
- `ctx.vidx`/`ctx.eidx`: The index of the vertex/edge model.
7070
- `ctx.src`/`ctx.dst`: src and dst indices (only for edge models).
7171
- `ctx.integrator`: The integrator object.
7272
- `ctx.t=ctx.integrator.t`: The current simulation time.
@@ -155,6 +155,7 @@ Returns a `CallbackSet` composed of all the "component-based" callbacks in the m
155155
Network components.
156156
"""
157157
function get_callbacks(nw::Network)
158+
aliased_changed(nw; warn=true)
158159
cbbs = collect_callbackbatches(nw)
159160
if isempty(cbbs)
160161
return nothing
@@ -432,7 +433,8 @@ _sym_to_int(x::SymbolicView, idx) = _sym_to_int.(Ref(x), idx)
432433
####
433434
assert_cb_compat(comp::ComponentModel, t::Tuple) = assert_cb_compat.(Ref(comp), t)
434435
function assert_cb_compat(comp::ComponentModel, cb)
435-
all_obssym = Set(sym(comp)) Set(comp.obssym) insym_all(comp) outsym_flat(comp)
436+
insym = hasinsym(comp) ? insym_all(comp) : []
437+
all_obssym = Set(sym(comp)) Set(comp.obssym) insym outsym_flat(comp)
436438
pcond = s -> s in comp.psym
437439
ucond_cond = s -> s in all_obssym
438440
ucond_affect = s -> s in comp.sym

0 commit comments

Comments
 (0)