Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Jump representations

Preserve `MassActionJump` in `JumpProblem`. Each solver owns any conversion or
specialization of reaction data. Do not normalize mass-action jumps into
`RegularJump` in the problem constructor; keep general-rate support as a separate
solver path.
4 changes: 3 additions & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
PointProcesses = "af0b7596-9bb0-472a-a012-63904f2b4c55"
StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3"
StochasticDiffEq = "789caeaf-c7a9-5a7d-9973-96adeb23e2a0"
StochasticDiffEqLeaping = "aefaaa88-39f2-4e89-b162-d61b7e8cc81b"

[compat]
Catalyst = "16"
Expand All @@ -22,7 +23,7 @@ DifferentialEquations = "7.11, 8.0"
Distributions = "0.25"
Documenter = "1.1"
Graphs = "1.9"
JumpProcesses = "9.13.2"
JumpProcesses = "9.33"
SciMLBase = "3"
OrdinaryDiffEq = "6.59, 7"
OrdinaryDiffEqFunctionMap = "1, 2"
Expand All @@ -31,3 +32,4 @@ Plots = "1.39"
PointProcesses = "0.5"
StableRNGs = "1.0"
StochasticDiffEq = "6.63, 7"
StochasticDiffEqLeaping = "2.2"
2 changes: 2 additions & 0 deletions docs/pages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
pages = ["index.md",
"Tutorials" => Any["tutorials/simple_poisson_process.md",
"tutorials/discrete_stochastic_example.md",
"tutorials/tau_leaping.md",
"tutorials/gpu.md",
"tutorials/point_process_simulation.md",
"tutorials/jump_diffusion.md",
"tutorials/spatial.md"],
Expand Down
74 changes: 59 additions & 15 deletions docs/src/jump_solve.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ faster as they only simulate the total number of jumps over each leap interval,
and thus do not need to simulate the realization of every single jump. Jumps for
use with exact simulation methods can be defined as `ConstantRateJump`s,
`MassActionJump`s, and/or `VariableRateJump`. Jumps for use with inexact
Ο„-leaping methods should be defined as `RegularJump`s.
Ο„-leaping methods can be defined as `RegularJump`s or `MassActionJump`s, with
`PureLeaping()` as the common mass-action interface (see the table below).

There are special algorithms available for efficiently simulating an exact, pure
`JumpProblem` (i.e., a `JumpProblem` over a `DiscreteProblem`). `SSAStepper()`
Expand All @@ -33,10 +34,12 @@ usually faster, it only supports discrete events (`DiscreteCallback`s), for pure
jump problems requiring continuous events (`ContinuousCallback`s) the less
performant `FunctionMap` time-stepper can be used.

If there is a `RegularJump`, then inexact Ο„-leaping methods must be used. The
current recommended method is `TauLeaping` if one needs adaptivity, events, etc.
If one only needs the most barebones fixed time-step leaping method, then
`SimpleTauLeaping` can have performance benefits.
For a pure mass-action model, use `SimpleExplicitTauLeaping` for adaptive explicit
leaping, or one of the implicit methods below for stiff systems. For custom
`RegularJump` rates and updates, use `SimpleTauLeaping` for fixed steps or
StochasticDiffEq's `TauLeaping` for adaptive steps. See the
[tau-leaping tutorial](@ref tau_leaping_tutorial) for examples and the
[GPU tutorial](@ref gpu_ensembles) for ensemble execution.

## Special Methods for Pure Jump Problems

Expand All @@ -63,20 +66,61 @@ algorithms are optimized for pure jump problems.
interpolation, and can therefore exactly evaluate the sampled solution
path at any time when only saving the post-jump state for each jump.

## RegularJump Compatible Methods
## Tau-Leaping Methods

All tau-leaping methods below accept the same
`JumpProblem(prob, PureLeaping(), mass_action_jump)`, with `prob` a
`DiscreteProblem`. Some also accept a `RegularJump` for more general rates and
count-based updates.

| Package | Algorithm | Jump representation | Step selection |
|:--------|:----------|:--------------------|:---------------|
| JumpProcesses | [`SimpleTauLeaping`](@ref) | `MassActionJump` or `RegularJump` | Fixed; requires `dt` |
| JumpProcesses | [`SimpleExplicitTauLeaping`](@ref) | `MassActionJump` | Adaptive explicit; `epsilon` |
| JumpProcesses | [`SimpleImplicitTauLeaping`](@ref) | `MassActionJump` | Adaptive implicit; `epsilon` |
| JumpProcesses | [`SimpleTrapezoidalLeaping`](@ref) | `MassActionJump` | Adaptive implicit trapezoidal; `epsilon` |
| JumpProcesses | [`SimpleAdaptiveTauLeaping`](@ref) | `MassActionJump` | Switches explicit/implicit; `epsilon` |
| StochasticDiffEq | `TauLeaping` | `MassActionJump` or `RegularJump` | Adaptive with post-leap estimates |
| StochasticDiffEq | `CaoTauLeaping` | `MassActionJump` or `RegularJump` | Fixed steps with `dt`, `adaptive = false`; adaptive selector incomplete |
| StochasticDiffEq | `ImplicitTauLeaping` | `MassActionJump` or `RegularJump` | Implicit; fixed steps with `dt`, `adaptive = false` |
| StochasticDiffEq | `ThetaTrapezoidalTauLeaping` | `MassActionJump` or `RegularJump` | Implicit theta-trapezoidal; fixed steps with `dt`, `adaptive = false` |

### StochasticDiffEq.jl

These methods support mixing with event handling, other jump types, and all of
the features of the normal differential equation solvers.
### JumpProcesses.jl

- `TauLeaping`: an adaptive tau-leaping algorithm with post-leap estimates.
The `Simple*` methods listed above are streamlined solvers for pure jump problems:
construct a `DiscreteProblem` and use `PureLeaping()` as the aggregator. The
adaptive mass-action methods require a `MassActionJump`. Each solver uses the stored rate constants and
stoichiometry directly. They support `saveat`, `save_start`, and `save_end`.

`SimpleImplicitTauLeaping` takes the deterministic part of a leap implicitly.
`SimpleTrapezoidalLeaping` averages current and new-state propensities in that
implicit solve. `SimpleAdaptiveTauLeaping` switches between explicit and implicit
steps based on a stiffness estimate. Its default implicit method is
`SimpleImplicitTauLeaping()`; select the trapezoidal formulation with
`implicit_alg = SimpleTrapezoidalLeaping()`. The default stiffness test uses the
spread of propensities; `eigenvalue_check = true` uses the drift Jacobian instead.
See the algorithm docstrings for the remaining tuning parameters.

Smaller `epsilon` values generally select smaller leaps. This is a step-selection
parameter, not a guarantee of a specified error in an observable or distribution.
Check convergence of the quantities of interest as the leap size is reduced.
Fixed-step `SimpleTauLeaping` does not reject leaps that produce negative
populations, so choosing an appropriate `dt` is especially important.

### JumpProcesses.jl
### StochasticDiffEq.jl

- `SimpleTauLeaping`: a tau-leaping algorithm for pure `RegularJump` `JumpProblem`s.
Requires a choice of `dt`.
- `RegularSSA`: a version of SSA for pure `RegularJump` `JumpProblem`s.
Load `StochasticDiffEq` to use its algorithms. These methods accept a
`MassActionJump` through `PureLeaping()`, or the same
[`RegularJump`](@ref) rate/count-update interface as `SimpleTauLeaping`, with
ordinary differential-equation integrator facilities such as callbacks. When
mixing regular jumps with separately aggregated jumps, select an aggregator such
as `Direct()` for those additional jumps; `PureLeaping()` bypasses aggregation.

The similarly named implicit methods in the two packages are separate algorithms
with different formulations and options. In particular,
`StochasticDiffEq.ImplicitTauLeaping` is not an alias for
`JumpProcesses.SimpleImplicitTauLeaping`, and `ThetaTrapezoidalTauLeaping` is not
`SimpleTrapezoidalLeaping`.

## Regular Jump Diffusion Compatible Methods

Expand Down
19 changes: 17 additions & 2 deletions docs/src/jump_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ These methods can be much faster as they do not need to simulate the realization
of every individual jump event. Ο„-leaping methods trade accuracy for speed, and
are best used when a set of jumps do not make significant changes to the
processes' state and/or rates over the course of one time-step (i.e., during a
leap interval). A single [`RegularJump`](@ref) is used to encode jumps for
Ο„-leaping algorithms. While Ο„-leaping methods can be proven to converge in the
leap interval). A single [`RegularJump`](@ref) encodes custom rates and count-based
updates for Ο„-leaping algorithms. All tau-leaping methods accept a
[`MassActionJump`](@ref) directly through `PureLeaping()`; see
[Tau-Leaping Methods](@ref jump_solve). While Ο„-leaping methods can be proven to converge in the
limit that the time-step approaches zero, their accuracy can be highly dependent
on the chosen time-step. As a rule of thumb, if changes to the state variable
`u` during a time-step (i.e., leap interval) are "minimal" compared to the size of
Expand Down Expand Up @@ -505,3 +507,16 @@ as will trying to update either `p` or `tspan` while passing a new
COEVOLVE: a joint point process model for information diffusion and network
evolution, Journal of Machine Learning Research 18(1), 1305–1353 (2017). doi:
10.5555/3122009.3122050.

## Mass-action operations for solver authors

`JumpProblem` preserves `MassActionJump` data. Solvers can use the rate constants
and stoichiometry directly and choose their own storage and evaluation strategy.
The following operations support CPU solver implementations; GPU solvers should
prepare a device-compatible representation during solver initialization.

```@docs
massaction_rates!
massaction_stoichiometry_mul!
massaction_drift!
```
5 changes: 5 additions & 0 deletions docs/src/tutorials/discrete_stochastic_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -936,3 +936,8 @@ we use as follows:
sol = solve(jump_prob, SimpleTauLeaping(); dt = 0.001)
plot(sol; label = ["S(t)" "I(t)" "R(t)"])
```

For adaptive explicit, implicit, and switching methods using `MassActionJump`,
and for the StochasticDiffEq solvers using this same `RegularJump` interface,
continue with the [tau-leaping tutorial](@ref tau_leaping_tutorial).
The [GPU tutorial](@ref gpu_ensembles) covers running ensembles on a GPU.
117 changes: 117 additions & 0 deletions docs/src/tutorials/gpu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
# [GPU ensembles](@id gpu_ensembles)

JumpProcesses provides [`EnsembleGPUKernel`](@ref) to run independent pure-jump
trajectories in parallel, with one trajectory per device thread. It uses
KernelAbstractions and Adapt; load both packages as well as your GPU backend.
The examples below use CUDA and require a working CUDA installation. They are
not executed during the documentation build.

## Supported solvers and inputs

| Solver | Jump representation | Required solve options |
|:-------|:--------------------|:-----------------------|
| `SSAStepper()` | `MassActionJump` | `trajectories`, `saveat` |
| `SimpleExplicitTauLeaping()` | `MassActionJump` | `trajectories`, `saveat` |
| `SimpleTauLeaping()` | `MassActionJump` or `RegularJump` | `trajectories`, `dt` |

All three paths require a `DiscreteProblem`. `SSAStepper` simulates individual
reaction events exactly; the other two introduce tau-leaping error. There are no
`EnsembleGPUKernel` implementations for `SimpleImplicitTauLeaping`,
`SimpleTrapezoidalLeaping`, `SimpleAdaptiveTauLeaping`, or StochasticDiffEq's
tau-leaping algorithms.

## Mass-action ensembles

Construct the problem using ordinary host arrays. The ensemble implementation
transfers the reaction data to the device and returns solutions that can be
inspected on the CPU.

```julia
using JumpProcesses, KernelAbstractions, Adapt, CUDA, Statistics

maj = MassActionJump(
[0.1 / 1000.0, 0.01],
[[1 => 1, 2 => 1], [2 => 1]],
[[1 => -1, 2 => 1], [2 => -1, 3 => 1]]
)
prob = DiscreteProblem([999.0, 10.0, 0.0], (0.0, 250.0))
jprob = JumpProblem(prob, PureLeaping(), maj)

sol = solve(
EnsembleProblem(jprob), SimpleExplicitTauLeaping(; epsilon = 0.01),
EnsembleGPUKernel(CUDABackend()); trajectories = 10_000, saveat = 10.0
)
mean(solution.u[end][3] for solution in sol.u)
```

`epsilon` controls adaptive leap selection. `saveat` specifies the output grid,
not a fixed leap size; the kernel shortens leaps to land on the next save time.
The SSA and adaptive explicit kernels require `saveat` (an interval or collection
of times), and support `save_start` and `save_end`.

For an exact SSA ensemble of the same model, construct a problem with an SSA
aggregator and change the time-stepper:

```julia
ssa_prob = JumpProblem(prob, Direct(), maj)
ssa_sol = solve(
EnsembleProblem(ssa_prob), SSAStepper(), EnsembleGPUKernel(CUDABackend());
trajectories = 10_000, saveat = 10.0
)
```

The `SSAStepper` and `SimpleExplicitTauLeaping` GPU kernels do not accept
`RegularJump`, `ConstantRateJump`, `VariableRateJump`, user callbacks, or an
ensemble `prob_func`. They share the
same initial condition and reaction data across trajectories. Use a CPU ensemble
such as `EnsembleThreads()` if these restrictions do not fit the model.

## Fixed-step ensembles

Use the same mass-action `jprob` from the first example with fixed steps:

```julia
fixed_sol = solve(
EnsembleProblem(jprob), SimpleTauLeaping(), EnsembleGPUKernel(CUDABackend());
trajectories = 10_000, dt = 0.1
)
```

### More general rates with RegularJump

The `SimpleTauLeaping` GPU kernel additionally supports the same `RegularJump`
interface as its CPU counterpart. Its rate and update functions must compile for the GPU:
use device-compatible operations and parameters, and write updates in place.

```julia
using JumpProcesses, KernelAbstractions, Adapt, CUDA

function rate!(out, u, p, t)
out[1] = p * u[1] / (1 + u[1])
return nothing
end

function change!(du, u, p, t, counts, mark)
du[1] = -counts[1]
du[2] = counts[1]
return nothing
end

rj = RegularJump(rate!, change!, 1)
prob = DiscreteProblem([1000.0, 0.0], (0.0, 1.0), 100.0)
jprob = JumpProblem(prob, PureLeaping(), rj)
sol = solve(
EnsembleProblem(jprob), SimpleTauLeaping(), EnsembleGPUKernel(CUDABackend());
trajectories = 10_000, dt = 0.01
)
```

This fixed-step kernel saves the step grid; do not rely on the SSA and adaptive
explicit kernels' `saveat` controls here. Use a `dt` that divides the simulation interval.
It does not reject negative populations, and marked jumps, callbacks, and
per-trajectory problem customization are not supported by this path.

`EnsembleGPUKernel()` without an explicit backend currently selects
KernelAbstractions' CPU backend. Pass `CUDABackend()` to request CUDA execution.
Other KernelAbstractions backends also need compatible device random-number
support; backend availability alone does not establish solver support.
Loading
Loading