Skip to content

Commit db07253

Browse files
committed
start on supporting hybrid jump systems
1 parent bef4566 commit db07253

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

Project.toml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
99
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
1010
DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
1111
DynamicQuantities = "06fc5a27-2a28-4c7c-a15d-362465fb6821"
12+
EnumX = "4e289a0a-7415-4d19-859d-a7e5c4648b56"
1213
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
1314
JumpProcesses = "ccbc3e58-028d-4f4c-8cd5-9ae44345cda5"
1415
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
@@ -32,14 +33,12 @@ BifurcationKit = "0f109fa4-8a5d-4b75-95aa-f515264e7665"
3233
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
3334
GraphMakie = "1ecd5474-83a3-4783-bb4f-06765db800d2"
3435
HomotopyContinuation = "f213a82b-91d6-5c5d-acf7-10f1c761b327"
35-
# StructuralIdentifiability = "220ca800-aa68-49bb-acd8-6037fa93a544"
3636

3737
[extensions]
3838
CatalystBifurcationKitExtension = "BifurcationKit"
3939
CatalystCairoMakieExtension = "CairoMakie"
4040
CatalystGraphMakieExtension = "GraphMakie"
4141
CatalystHomotopyContinuationExtension = "HomotopyContinuation"
42-
# CatalystStructuralIdentifiabilityExtension = "StructuralIdentifiability"
4342

4443
[compat]
4544
BifurcationKit = "0.3"
@@ -50,6 +49,7 @@ DiffEqBase = "6.83.0"
5049
DocStringExtensions = "0.8, 0.9"
5150
DynamicPolynomials = "0.5, 0.6"
5251
DynamicQuantities = "0.13.2, 1"
52+
EnumX = "1.0.4"
5353
GraphMakie = "0.5"
5454
Graphs = "1.4"
5555
HomotopyContinuation = "2.9"
@@ -64,7 +64,6 @@ Requires = "1.0"
6464
RuntimeGeneratedFunctions = "0.5.12"
6565
SciMLBase = "< 2.57.2"
6666
Setfield = "1"
67-
# StructuralIdentifiability = "0.5.8"
6867
SymbolicUtils = "2.1.2, 3.3.0"
6968
Symbolics = "5.30.1, 6"
7069
Unitful = "1.12.4"
@@ -93,7 +92,4 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
9392
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
9493

9594
[targets]
96-
test = ["DiffEqCallbacks", "DomainSets", "Graphviz_jll", "Logging", "NonlinearSolve",
97-
"OrdinaryDiffEq", "Pkg", "Plots", "Random", "SafeTestsets", "SciMLBase", "SciMLNLSolve",
98-
"StableRNGs", "StaticArrays", "Statistics", "SteadyStateDiffEq", "StochasticDiffEq",
99-
"Test", "Unitful"]
95+
test = ["DiffEqCallbacks", "DomainSets", "Graphviz_jll", "Logging", "NonlinearSolve", "OrdinaryDiffEq", "Pkg", "Plots", "Random", "SafeTestsets", "SciMLBase", "SciMLNLSolve", "StableRNGs", "StaticArrays", "Statistics", "SteadyStateDiffEq", "StochasticDiffEq", "Test", "Unitful"]

src/Catalyst.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ $(DocStringExtensions.README)
44
module Catalyst
55

66
using DocStringExtensions
7-
using SparseArrays, DiffEqBase, Reexport, Setfield
7+
using SparseArrays, DiffEqBase, Reexport, Setfield, EnumX
88
using LaTeXStrings, Latexify, Requires
99
using LinearAlgebra, Combinatorics
1010
using JumpProcesses: JumpProcesses, JumpProblem,
@@ -14,7 +14,7 @@ using JumpProcesses: JumpProcesses, JumpProblem,
1414
# ModelingToolkit imports and convenience functions we use
1515
using ModelingToolkit
1616
const MT = ModelingToolkit
17-
using DynamicQuantities#, Unitful # Having Unitful here as well currently gives an error.
17+
using DynamicQuantities #, Unitful # Having Unitful here as well currently gives an error.
1818

1919
@reexport using ModelingToolkit
2020
using Symbolics
@@ -95,7 +95,10 @@ end
9595
# The `Reaction` structure and its functions.
9696
include("reaction.jl")
9797
export isspecies
98-
export Reaction
98+
export Reaction, PhysicalScale
99+
100+
# Union type for `Reaction`s and `Equation`s.
101+
const CatalystEqType = Union{Reaction, Equation}
99102

100103
# The `ReactionSystem` structure and its functions.
101104
include("reactionsystem.jl")

src/reaction.jl

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,6 @@ function Reaction(rate, subs, prods; kwargs...)
244244
Reaction(rate, subs, prods, sstoich, pstoich; kwargs...)
245245
end
246246

247-
# Union type for `Reaction`s and `Equation`s.
248-
const CatalystEqType = Union{Reaction, Equation}
249-
250247
### Base Function Dispatches ###
251248

252249
# Used by `Base.show`.
@@ -679,6 +676,45 @@ function getmisc(reaction::Reaction)
679676
end
680677
end
681678

679+
############## Metadata for the mathematical type of a reaction ##############
680+
681+
"""
682+
@enumx PhysicalScale
683+
684+
EnumX instance representing the physical scale of a reaction.
685+
686+
Notes: The following values are possible:
687+
- `Auto`: (DEFAULT) Lets Catalyst decide at the time of system conversion and/or
688+
problem generation at what physical scale to represent the reaction.
689+
- `ODE`: The reaction is to be treated via an ordinary differential equation term.
690+
- `SDE`: The reaction is to be treated via a stochastic differential equation (CLE) term.
691+
- `Jump`: The reaction is to be treated via a jump process (stochastic chemical kinetics)
692+
term, letting Catalyst decide the specific jump type.
693+
- `VariableRateJump`: The reaction is to be treated as a jump process (stochastic chemical
694+
kinetics) term, specifically assigning it to a VariableRateJump.
695+
"""
696+
@enumx PhysicalScale begin
697+
Auto # the default that lets Catalyst decide
698+
ODE
699+
SDE
700+
Jump # lets Catalyst decide the jump type
701+
VariableRateJump # forces a VariableRateJump
702+
end
703+
704+
"""
705+
has_physical_scale(rx::Reaction)
706+
707+
Returns `true` if the input reaction has the `physical_scale` metadata field assigned,
708+
else `false`.
709+
"""
710+
function has_physical_scale(rx::Reaction)
711+
return hasmetadata(rx, :physical_scale)
712+
end
713+
714+
function get_physical_scale(rx::Reaction)
715+
return has_physical_scale(rx) ? getmetadata(rx, :physical_scale) : PhysicalScale.Auto
716+
end
717+
682718
### Units Handling ###
683719

684720
"""

src/reactionsystem_conversions.jl

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs
673673
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
674674
remove_conserved = nothing, checks = false,
675675
default_u0 = Dict(), default_p = Dict(),
676-
defaults = _merge(Dict(default_u0), Dict(default_p)),
676+
defaults = _merge(Dict(default_u0), Dict(default_p)), physical_scales = nothing
677677
kwargs...)
678678
iscomplete(rs) || error(COMPLETENESS_ERROR)
679679
spatial_convert_err(rs::ReactionSystem, JumpSystem)
@@ -683,9 +683,7 @@ function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs
683683
flatrs = Catalyst.flatten(rs)
684684
error_if_constraints(JumpSystem, flatrs)
685685

686-
(length(MT.continuous_events(flatrs)) > 0) &&
687-
(@warn "continuous_events will be dropped as they are not currently supported by JumpSystems.")
688-
686+
physical_scales = merge_physical_scales(reactions(rs), physical_scales)
689687
eqs = assemble_jumps(flatrs; combinatoric_ratelaws)
690688

691689
# handle BC species
@@ -769,6 +767,16 @@ function DiffEqBase.SDEProblem(rs::ReactionSystem, u0, tspan,
769767
noise_rate_prototype = p_matrix, kwargs...)
770768
end
771769

770+
function merge_physical_scales(rxs, physical_scales)
771+
scales = get_physical_scale.(rxs)
772+
if physical_scales !== nothing
773+
for (idx, scale) in physical_scales
774+
scales[idx] = scale
775+
end
776+
end
777+
scales
778+
end
779+
772780
"""
773781
$(TYPEDEF)
774782
@@ -817,11 +825,14 @@ plot(sol, idxs = :A)
817825
"""
818826
function JumpInputs(rs::ReactionSystem, u0, tspan, p = DiffEqBase.NullParameters();
819827
name = nameof(rs), combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
820-
checks = false, kwargs...)
828+
checks = false, physical_scales = nothing, kwargs...)
821829
u0map = symmap_to_varmap(rs, u0)
822830
pmap = symmap_to_varmap(rs, p)
823-
jsys = complete(convert(JumpSystem, rs; name, combinatoric_ratelaws, checks))
824-
if MT.has_variableratejumps(jsys)
831+
jsys = complete(convert(JumpSystem, rs; name, combinatoric_ratelaws, checks,
832+
physical_scales))
833+
834+
if MT.has_variableratejumps(jsys) || MT.has_equations(jsys) ||
835+
!isempty(MT.continuous_events(jsys))
825836
prob = ODEProblem(jsys, u0map, tspan, pmap; kwargs...)
826837
else
827838
prob = DiscreteProblem(jsys, u0map, tspan, pmap; kwargs...)

0 commit comments

Comments
 (0)