Skip to content

Commit 37a591d

Browse files
hstreyhelmutstreyMasonProtter
authored
Add comprehensive docstrings for key functions and types (#33)
* Add comprehensive docstrings for key functions and types - apply_subsystem_noise\!: Stochastic noise application - discrete_event_condition: Event trigger conditions - apply_discrete_event\!: Event handling and state modification - get_tag, get_params, get_states: Accessor functions - ConnectionMatrices: Multi-type connection container with diagram - ConnectionMatrix: Block matrix structure with ASCII visualization - graph_ode\!: Core simulation function with detailed flow diagram * Update src/GraphDynamics.jl * Update src/GraphDynamics.jl * Update src/GraphDynamics.jl * Update src/GraphDynamics.jl * Update src/GraphDynamics.jl * Update src/graph_solve.jl * Update src/graph_solve.jl --------- Co-authored-by: Helmut Strey <[email protected]> Co-authored-by: Mason Protter <[email protected]>
1 parent ab44088 commit 37a591d

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

src/GraphDynamics.jl

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,55 @@ struct Subsystem{T, Eltype, States, Params}
153153
params::SubsystemParams{T, Params}
154154
end
155155

156+
"""
157+
get_tag(subsystem::Subsystem{T}) = T
158+
get_tag(states::SubsystemStates{T}) = T
159+
get_tag(params::SubsystemParams{T}) = T
160+
get_tag(::Type{<:Subsystem{T}}) = T
161+
162+
Extract the type tag `T` from a subsystem or its components.
163+
164+
The tag identifies the subsystem type and is used for dispatch and organization.
165+
166+
# Examples
167+
```julia
168+
particle = Subsystem{Particle}(states=(x=1.0, v=0.0), params=(m=2.0,))
169+
get_tag(particle) # returns Particle
170+
get_tag(typeof(particle)) # returns Particle
171+
```
172+
"""
156173
function get_tag end
174+
"""
175+
get_params(subsystem::Subsystem) -> SubsystemParams
176+
177+
Extract the parameters from a subsystem.
178+
179+
Returns a `SubsystemParams` object containing the non-dynamical parameters
180+
(constants like mass, charge, spring constants, etc.) of the subsystem.
181+
182+
# Example
183+
```julia
184+
sys = Subsystem{Oscillator}(states=(x=1.0, v=0.0), params=(k=2.0, m=1.0))
185+
params = get_params(sys) # SubsystemParams{Oscillator}(k=2.0, m=1.0)
186+
params.k # 2.0
187+
```
188+
"""
157189
function get_params end
190+
"""
191+
get_states(subsystem::Subsystem) -> SubsystemStates
192+
193+
Extract the state variables from a subsystem.
194+
195+
Returns a `SubsystemStates` object containing the dynamical state variables
196+
(position, velocity, concentrations, etc.) that evolve over time.
197+
198+
# Example
199+
```julia
200+
sys = Subsystem{Oscillator}(states=(x=1.0, v=0.0), params=(k=2.0, m=1.0))
201+
states = get_states(sys) # SubsystemStates{Oscillator}(x=1.0, v=0.0)
202+
states.x # 1.0
203+
```
204+
"""
158205
function get_states end
159206

160207
"""
@@ -220,6 +267,27 @@ defaults to false, but users may add methods to this function if they have subsy
220267
subsystem_differential_requires_inputs(::Type{T}) where {T} = true
221268
subsystem_differential_requires_inputs(::Subsystem{T}) where {T} = subsystem_differential_requires_inputs(T)
222269

270+
"""
271+
apply_subsystem_noise!(vstate, subsystem, t)
272+
273+
Apply stochastic noise to a subsystem's state at time `t`.
274+
275+
This function modifies the noise terms for a subsystem's stochastic differential equation.
276+
By default, it does nothing (no noise). Override this for stochastic subsystems.
277+
278+
# Arguments
279+
- `vstate`: A view into the noise vector to be modified
280+
- `subsystem`: The `Subsystem` instance whose noise is being computed
281+
- `t`: Current time
282+
283+
# Example
284+
```julia
285+
function GraphDynamics.apply_subsystem_noise!(vstate, sys::Subsystem{BrownianParticle}, t)
286+
# No noise in position, so we don't modify vstate[:x]
287+
vstate[:v] = sys.σ # White noise in velocity with amplitude σ
288+
end
289+
```
290+
"""
223291
function apply_subsystem_noise!(vstate, subsystem, t)
224292
nothing
225293
end
@@ -235,6 +303,32 @@ end
235303
function continuous_event_condition end
236304
function apply_continuous_event! end
237305
function discrete_event_condition end
306+
"""
307+
apply_discrete_event!(integrator, sview, pview, subsystem, F[, input])
308+
apply_discrete_event!(integrator, sview_src, pview_src, sview_dst, pview_dst,
309+
connection_rule, sys_src, sys_dst[, input_src, input_dst])
310+
311+
Apply a discrete event to modify subsystem states or parameters.
312+
313+
This function is called when `discrete_event_condition` returns `true`.
314+
315+
# Arguments
316+
- `integrator`: The ODE/SDE integrator
317+
- `sview`/`pview`: Views into states/params to be modified
318+
- `subsystem`: The subsystem experiencing the event
319+
- `F`: `ForeachConnectedSubsystem` for accessing connected subsystems
320+
- `input`: Optional input from connected subsystems
321+
- For connection events: views and systems for both source and destination
322+
323+
# Example
324+
```julia
325+
function GraphDynamics.apply_discrete_event!(integrator, sview, pview,
326+
sys::Subsystem{Ball}, F)
327+
# Bounce: reverse velocity
328+
sview[:v] = -sys.restitution * sview[:v]
329+
end
330+
```
331+
"""
238332
function apply_discrete_event! end
239333

240334

src/graph_solve.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
"""
2+
graph_ode!(du, u, p, t)
3+
4+
Core ODE function for graph dynamics simulation.
5+
6+
Computes derivatives for all subsystems in the graph, handling:
7+
1. Partitioning states by subsystem type
8+
2. Computing inputs from connections
9+
3. Applying subsystem differential equations
10+
11+
# Arguments
12+
- `du`: Derivative vector (modified in-place)
13+
- `u`: Current state vector
14+
- `p`: Parameters with fields:
15+
- `params_partitioned`: Parameters grouped by subsystem type
16+
- `connection_matrices`: Connection structure
17+
- `scheduler`: Parallelization scheduler
18+
- `partition_plan`: Information for how to partition u and du into vectors of SubsystemStates grouped by subsystem type
19+
- `t`: Current time
20+
21+
# Flow Diagram
22+
```
23+
u (flat state vector)
24+
25+
├─── partitioned ──→ states[Type1][1..n]
26+
│ states[Type2][1..m]
27+
│ states[Type3][1..k]
28+
29+
├─── for each subsystem i of type T:
30+
│ │
31+
│ ├── calculate_inputs from connections
32+
│ │ └── Σ connection_rule(src → dst)
33+
│ │
34+
│ └── subsystem_differential(sys, input, t)
35+
│ └── writes to du[i]
36+
37+
└─── du (flat derivative vector)
38+
```
39+
"""
140
#----------------------------------------------------------
241
function graph_ode!(du,
342
u,

0 commit comments

Comments
 (0)