Skip to content

Commit 550c218

Browse files
authored
Merge pull request #317 from JuliaDynamics/hw/callbacks
improve callback system
2 parents 4e296f1 + 428b3d7 commit 550c218

File tree

11 files changed

+544
-89
lines changed

11 files changed

+544
-89
lines changed

NEWS.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# NetworkDynamics Release Notes
22

3+
## v0.10.10 Changelog
4+
- [#317](https://github.com/JuliaDynamics/NetworkDynamics.jl/pull/317) Enhanced callback system with negative affect support and runtime callback injection:
5+
- Add `affect_neg!` parameter to `ContinuousComponentCallback` and `VectorContinuousComponentCallback` for handling downcrossing events
6+
- Add ability to inject additional callbacks at runtime via `get_callbacks(nw, additional_callbacks)` without storing them in component metadata
7+
- Add `NetworkDynamics.pretty_f()` debugging utility for pretty-printing MTK-generated functions
8+
- Improve MTK integration warnings for nested event systems
9+
- Better initialization error messages with specific variable information when NaN values are detected
10+
311
## v0.10.9 Changelog
412
- [#314](https://github.com/JuliaDynamics/NetworkDynamics.jl/pull/314) Consolidate deprecated functionality: all deprecated functionality moved to dedicated `src/deprecated.jl` file for easier maintenance
513

docs/src/API.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ has_callback
219219
get_callbacks(::NetworkDynamics.ComponentModel)
220220
set_callback!
221221
add_callback!
222+
delete_callbacks!
222223
```
223224

224225
## Sparsity Detection
@@ -253,6 +254,7 @@ ff_to_constraint
253254
Base.copy(::NetworkDynamics.ComponentModel)
254255
extract_nw
255256
implicit_output
257+
NetworkDynamics.pretty_f
256258
```
257259

258260
## NetworkDynamicsInspector API

ext/MTKExt_utils.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,28 @@ function generate_massmatrix(eqs::AbstractVector{Equation})
149149
end
150150

151151
function warn_missing_features(sys)
152-
cev = ModelingToolkit.get_continuous_events(sys)
153-
dev = ModelingToolkit.get_discrete_events(sys)
152+
cev = _collect_continuous_events(sys)
153+
dev = _collect_discrete_events(sys)
154154
if !isempty(cev) || !isempty(dev)
155-
@warn "Model has attached events, which is not supported."
155+
@warn "MTK-Model $(sys.name) contains events. They will be ignored by NetworkDynamics.jl. Use ComponentCallbacks on a component level for that!"
156156
end
157157

158158
if !isempty(ModelingToolkit.initialization_equations(sys))
159159
@warn "Model has explicit init equation. Those are currently ignored by NetworkDynamics.jl."
160160
end
161161
end
162+
function _collect_continuous_events(sys)
163+
vcat(
164+
ModelingToolkit.get_continuous_events(sys),
165+
[_collect_continuous_events(sys) for sys in ModelingToolkit.get_systems(sys)]...
166+
)
167+
end
168+
function _collect_discrete_events(sys)
169+
vcat(
170+
ModelingToolkit.get_discrete_events(sys),
171+
[_collect_discrete_events(sys) for sys in ModelingToolkit.get_systems(sys)]...
172+
)
173+
end
162174

163175
function check_metadata(exprs)
164176
nometadata = []

src/NetworkDynamics.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ using Random: Random
2323
using Static: Static, StaticInt
2424
using SciMLBase: VectorContinuousCallback, CallbackSet, DiscreteCallback
2525
using DiffEqCallbacks: DiffEqCallbacks
26+
using MacroTools: postwalk, @capture
2627

2728
@static if VERSION v"1.11.0-0"
2829
using Base: AnnotatedIOBuffer, AnnotatedString
@@ -79,7 +80,6 @@ export DiscreteComponentCallback, PresetTimeComponentCallback
7980
export SymbolicView
8081
include("callbacks.jl")
8182

82-
using MacroTools: postwalk
8383
export @initconstraint, InitConstraint
8484
export @initformula, InitFormula
8585
include("init_constraints.jl")
@@ -100,7 +100,7 @@ export has_init, get_init, set_init!, delete_init!, strip_inits!
100100
export has_bounds, get_bounds, set_bounds!, delete_bounds!, strip_bounds!
101101
export has_graphelement, get_graphelement, set_graphelement!
102102
export get_initial_state, dump_initial_state, dump_state
103-
export has_callback, get_callbacks, set_callback!, add_callback!
103+
export has_callback, get_callbacks, set_callback!, add_callback!, delete_callbacks!
104104
export has_initconstraint, get_initconstraints, set_initconstraint!, add_initconstraint!, delete_initconstraints!
105105
export has_initformula, get_initformulas, set_initformula!, add_initformula!, delete_initformulas!
106106
export has_position, get_position, set_position!

0 commit comments

Comments
 (0)