Skip to content

Commit 7b9f77e

Browse files
committed
WIP: MTK v10 compatibility changes
1 parent 8d2537a commit 7b9f77e

File tree

6 files changed

+51
-37
lines changed

6 files changed

+51
-37
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Catalyst"
22
uuid = "479239e8-5488-4da2-87a7-35f2df7eef83"
3-
version = "15.0.8"
3+
version = "16"
44

55
[deps]
66
Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
@@ -62,7 +62,7 @@ LaTeXStrings = "1.3.0"
6262
Latexify = "0.16.6"
6363
MacroTools = "0.5.5"
6464
Makie = "0.22.1"
65-
ModelingToolkit = "9.73"
65+
ModelingToolkit = "10"
6666
NetworkLayout = "0.4.7"
6767
Parameters = "0.12"
6868
Reexport = "1.0"

src/Catalyst.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import ModelingToolkit: get_variables, namespace_expr, namespace_equation, get_v
3636

3737
# internal but needed ModelingToolkit functions
3838
import ModelingToolkit: check_variables,
39-
check_parameters, _iszero, _merge, check_units,
39+
check_parameters, _iszero, check_units,
4040
get_unit, check_equations, iscomplete
4141

4242
import Base: (==), hash, size, getindex, setindex, isless, Sort.defalg, length, show

src/reactionsystem.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ Notes:
276276
units). Unit checking can be disabled by passing the keyword argument `checks=false`.
277277
"""
278278
struct ReactionSystem{V <: NetworkProperties} <:
279-
MT.AbstractTimeDependentSystem
279+
MT.AbstractSystem
280280
"""The equations (reactions and algebraic/differential) defining the system."""
281281
eqs::Vector{CatalystEqType}
282282
"""The Reactions defining the system. """
@@ -356,7 +356,7 @@ struct ReactionSystem{V <: NetworkProperties} <:
356356
check_parameters(ps, iv)
357357
nonrx_eqs = Equation[eq for eq in eqs if eq isa Equation]
358358
!isempty(nonrx_eqs) && check_equations(nonrx_eqs, iv)
359-
!isempty(cevs) && check_equations(equations(cevs), iv)
359+
!isnothing(cevs) && !isempty(cevs) && check_equations(equations(cevs), iv)
360360
end
361361

362362
if isempty(sivs) && (checks == true || (checks & MT.CheckUnits) > 0)
@@ -480,14 +480,10 @@ function ReactionSystem(eqs, iv, unknowns, ps;
480480
networkproperties
481481
end
482482

483-
# Creates the continuous and discrete callbacks.
484-
ccallbacks = MT.SymbolicContinuousCallbacks(continuous_events)
485-
dcallbacks = MT.SymbolicDiscreteCallbacks(discrete_events)
486-
487483
ReactionSystem(
488484
eqs′, rxs, iv′, sivs′, unknowns′, spcs, ps′, var_to_name, observed, name,
489485
systems, defaults, connection_type, nps, combinatoric_ratelaws,
490-
ccallbacks, dcallbacks, metadata; checks = checks)
486+
continuous_events, discrete_events, metadata; checks = checks)
491487
end
492488

493489
# Two-argument constructor (reactions/equations and time variable).

src/reactionsystem_conversions.jl

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ function assemble_jumps(rs; combinatoric_ratelaws = true, physical_scales = noth
386386
error("Must have at least one reaction that will be represented as a jump when constructing a JumpSystem.")
387387

388388
# note isvrjvec indicates which reactions are not constant rate jumps
389-
# it may be that a given jump has isvrjvec[i] = true but has a physical
389+
# it may be that a given jump has isvrjvec[i] = true but has a physical
390390
isvrjvec = classify_vrjs(rs, physcales)
391391

392392
rxvars = []
@@ -505,9 +505,16 @@ COMPLETENESS_ERROR = "A ReactionSystem must be complete before it can be convert
505505

506506
### System Conversions ###
507507

508+
abstract type SystemType end
509+
510+
struct ReactionRateSystem <: SystemType end
511+
struct ChemicalLangevinSystem <: SystemType end
512+
struct StochasticChemicalKineticSystem <: SystemType end
513+
struct NonlinearSystem <: SystemType end
514+
508515
"""
509516
```julia
510-
Base.convert(::Type{<:ODESystem},rs::ReactionSystem)
517+
Base.convert(::Type{<:System},rs::ReactionSystem)
511518
```
512519
Convert a [`ReactionSystem`](@ref) to an `ModelingToolkit.ODESystem`.
513520
@@ -524,7 +531,7 @@ Keyword args and default values:
524531
with their rational function representation when converting to another system type. Set to
525532
`false`` to disable.
526533
"""
527-
function Base.convert(::Type{<:ODESystem}, rs::ReactionSystem; name = nameof(rs),
534+
function Base.convert(::Type{<:ReactionRateSystem}, rs::ReactionSystem; name = nameof(rs),
528535
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
529536
include_zero_odes = true, remove_conserved = false, checks = false,
530537
default_u0 = Dict(), default_p = Dict(),
@@ -541,7 +548,7 @@ function Base.convert(::Type{<:ODESystem}, rs::ReactionSystem; name = nameof(rs)
541548
include_zero_odes, expand_catalyst_funs)
542549
eqs, us, ps, obs, defs = addconstraints!(eqs, fullrs, ists, ispcs; remove_conserved)
543550

544-
ODESystem(eqs, get_iv(fullrs), us, ps;
551+
System(eqs, get_iv(fullrs), us, ps;
545552
observed = obs,
546553
name,
547554
defaults = _merge(defaults, defs),
@@ -622,7 +629,7 @@ function Base.convert(::Type{<:NonlinearSystem}, rs::ReactionSystem; name = name
622629
all_differentials_permitted || nonlinear_convert_differentials_check(rs)
623630
eqs = [remove_diffs(eq.lhs) ~ remove_diffs(eq.rhs) for eq in eqs]
624631

625-
NonlinearSystem(eqs, us, ps;
632+
System(eqs, us, ps;
626633
name,
627634
observed = obs, initialization_eqs = initeqs,
628635
defaults = _merge(defaults, defs),
@@ -679,7 +686,7 @@ Notes:
679686
with their rational function representation when converting to another system type. Set to
680687
`false`` to disable.
681688
"""
682-
function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
689+
function Base.convert(::Type{<:ChemicalLangevinSystem}, rs::ReactionSystem;
683690
name = nameof(rs), combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
684691
include_zero_odes = true, checks = false, remove_conserved = false,
685692
default_u0 = Dict(), default_p = Dict(),
@@ -704,7 +711,7 @@ function Base.convert(::Type{<:SDESystem}, rs::ReactionSystem;
704711
@info "Boundary condition species detected. As constraint equations are not currently supported when converting to SDESystems, the resulting system will be undetermined. Consider using constant species instead."
705712
end
706713

707-
SDESystem(eqs, noiseeqs, get_iv(flatrs), us, ps;
714+
System([eqs; noiseeqs], get_iv(flatrs), us, ps;
708715
observed = obs,
709716
name,
710717
defaults = _merge(defaults, defs),
@@ -728,7 +735,7 @@ Merge physical scales for a set of reactions.
728735
function merge_physical_scales(rxs, physical_scales, default)
729736
scales = get_physical_scale.(rxs)
730737

731-
# override metadata attached scales
738+
# override metadata attached scales
732739
if physical_scales !== nothing
733740
for (key, scale) in physical_scales
734741
scales[key] = scale
@@ -769,13 +776,13 @@ Notes:
769776
`VariableRateJump` to save the solution before and/or after the jump occurs. Defaults to
770777
true for both.
771778
"""
772-
function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs),
779+
function Base.convert(::Type{<:StochasticChemicalKineticSystem}, rs::ReactionSystem; name = nameof(rs),
773780
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
774781
remove_conserved = nothing, checks = false, default_u0 = Dict(), default_p = Dict(),
775782
defaults = _merge(Dict(default_u0), Dict(default_p)), expand_catalyst_funs = true,
776783
save_positions = (true, true), physical_scales = nothing, kwargs...)
777784
iscomplete(rs) || error(COMPLETENESS_ERROR)
778-
spatial_convert_err(rs::ReactionSystem, JumpSystem)
785+
spatial_convert_err(rs::ReactionSystem, StochasticChemicalKineticSystem)
779786
(remove_conserved !== nothing) &&
780787
throw(ArgumentError("Catalyst does not support removing conserved species when converting to JumpSystems."))
781788

@@ -794,7 +801,7 @@ function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs
794801
physical_scales, save_positions)
795802
ists, ispcs = get_indep_sts(flatrs)
796803

797-
# handle coupled ODEs and BC species
804+
# handle coupled ODEs and BC species
798805
if (PhysicalScale.ODE in unique_scales) || has_nonreactions(flatrs)
799806
odeeqs = assemble_drift(flatrs, ispcs; combinatoric_ratelaws,
800807
remove_conserved = false, physical_scales, include_zero_odes = true)
@@ -811,7 +818,7 @@ function Base.convert(::Type{<:JumpSystem}, rs::ReactionSystem; name = nameof(rs
811818
defs = MT.defaults(flatrs)
812819
end
813820

814-
JumpSystem(eqs, get_iv(flatrs), us, ps;
821+
System(eqs, get_iv(flatrs), us, ps;
815822
observed = obs,
816823
name,
817824
defaults = _merge(defaults, defs),
@@ -850,8 +857,8 @@ end
850857
DiffEqBase.NonlinearProblem(rs::ReactionSystem, u0,
851858
p = DiffEqBase.NullParameters(), args...;
852859
name = nameof(rs), combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
853-
remove_conserved = false, checks = false, check_length = false,
854-
structural_simplify = remove_conserved, all_differentials_permitted = false,
860+
remove_conserved = false, checks = false, check_length = false,
861+
structural_simplify = remove_conserved, all_differentials_permitted = false,
855862
kwargs...)
856863
```
857864
@@ -923,7 +930,7 @@ Inputs for a JumpProblem from a given `ReactionSystem`.
923930
# Fields
924931
$(FIELDS)
925932
"""
926-
struct JumpInputs{S <: MT.JumpSystem, T <: SciMLBase.AbstractODEProblem}
933+
struct JumpInputs{S <: MT.System, T <: SciMLBase.AbstractODEProblem}
927934
"""The `JumpSystem` to define the problem over"""
928935
sys::S
929936
"""The problem the JumpProblem should be defined over, for example DiscreteProblem"""
@@ -936,8 +943,8 @@ JumpInputs(rs::ReactionSystem, u0, tspan,
936943
p = DiffEqBase.NullParameters;
937944
name = nameof(rs),
938945
combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
939-
checks = false, physical_scales = nothing,
940-
expand_catalyst_funs = true,
946+
checks = false, physical_scales = nothing,
947+
expand_catalyst_funs = true,
941948
save_positions = (true, true),
942949
remake_warn = true, kwargs...)
943950
```
@@ -988,7 +995,7 @@ function JumpInputs(rs::ReactionSystem, u0, tspan, p = DiffEqBase.NullParameters
988995
name = nameof(rs), combinatoric_ratelaws = get_combinatoric_ratelaws(rs),
989996
checks = false, physical_scales = nothing, expand_catalyst_funs = true,
990997
save_positions = (true, true), remake_warn = true, kwargs...)
991-
jsys = complete(convert(JumpSystem, rs; name, combinatoric_ratelaws, checks,
998+
jsys = complete(convert(StochasticChemicalKineticSystem, rs; name, combinatoric_ratelaws, checks,
992999
physical_scales, expand_catalyst_funs, save_positions))
9931000

9941001
if MT.has_variableratejumps(jsys) || MT.has_equations(jsys) ||
@@ -1025,7 +1032,7 @@ function DiffEqBase.DiscreteProblem(rs::ReactionSystem, u0, tspan::Tuple,
10251032
Base.depwarn("DiscreteProblem(rn::ReactionSystem, ...) is deprecated and will be \
10261033
removed in Catalyst 16. Use JumpInputs(rn, ...) instead.",
10271034
:DiscreteProblem)
1028-
jsys = convert(JumpSystem, rs; name, combinatoric_ratelaws, checks,
1035+
jsys = convert(StochasticChemicalKineticSystem, rs; name, combinatoric_ratelaws, checks,
10291036
expand_catalyst_funs)
10301037
jsys = complete(jsys)
10311038
return DiscreteProblem(jsys, u0, tspan, p, args...; kwargs...)
@@ -1040,7 +1047,7 @@ function JumpProcesses.JumpProblem(rs::ReactionSystem, prob::SciMLBase.AbstractD
10401047
Base.depwarn("JumpProblem(rn::ReactionSystem, prob, ...) is \
10411048
deprecated and will be removed in Catalyst 16. Use \
10421049
JumpProblem(JumpInputs(rn, ...), ...) instead.", :JumpProblem)
1043-
jsys = convert(JumpSystem, rs; name, combinatoric_ratelaws,
1050+
jsys = convert(StochasticChemicalKineticSystem, rs; name, combinatoric_ratelaws,
10441051
expand_catalyst_funs, checks)
10451052
jsys = complete(jsys)
10461053
return JumpProblem(jsys, prob, aggregator; kwargs...)

src/spatial_reaction_systems/lattice_reaction_systems.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ continuous space systems with them is possible, but requires the user to determi
5757
(the lattice). Better support for continuous space models is a work in progress.
5858
- Catalyst contains extensive documentation on spatial modelling, which can be found [here](https://docs.sciml.ai/Catalyst/stable/spatial_modelling/lattice_reaction_systems/).
5959
"""
60-
struct LatticeReactionSystem{Q, R, S, T} <: MT.AbstractTimeDependentSystem
60+
struct LatticeReactionSystem{Q, R, S, T} <: MT.AbstractSystem
6161
# Input values.
6262
"""The (non-spatial) reaction system within each vertex."""
6363
reactionsystem::ReactionSystem{Q}

src/spatial_reaction_systems/utility.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function lattice_process_p(ps_in, ps_vertex_syms::Vector,
4040
# For uniform parameters these have size 1/(1,1). Else, they have size num_verts/(num_verts,num_verts).
4141
ps = lattice_process_input(ps_in, [ps_vertex_syms; ps_edge_syms])
4242

43-
# Split the parameter vector into one for vertex parameters and one for edge parameters.
43+
# Split the parameter vector into one for vertex parameters and one for edge parameters.
4444
# Next, convert their values to the correct form (vectors for vert_ps and sparse matrices for edge_ps).
4545
vert_ps, edge_ps = split_parameters(ps, ps_vertex_syms, ps_edge_syms)
4646
vert_ps = vertex_value_map(vert_ps, lrs)
@@ -164,13 +164,13 @@ function edge_value_form(values, lrs::LatticeReactionSystem, sym)
164164
throw(ArgumentError("Values was not provided for some edges for edge parameter $sym."))
165165
end
166166

167-
# Unlike initial conditions/vertex parameters, (unless uniform) edge parameters' values are
167+
# Unlike initial conditions/vertex parameters, (unless uniform) edge parameters' values are
168168
# always provided in the same (sparse matrix) form.
169169
return values
170170
end
171171

172172
# Creates a map, taking each species (with transportation) to its transportation rate.
173-
# The species is represented by its index (in species(lrs).
173+
# The species is represented by its index (in species(lrs).
174174
# If the rate is uniform across all edges, the transportation rate will be a size (1,1) sparse matrix.
175175
# Else, the rate will be a size (num_verts,num_verts) sparse matrix.
176176
function make_sidxs_to_transrate_map(vert_ps::Vector{Pair{R, Vector{T}}},
@@ -190,7 +190,7 @@ end
190190
# Computes the transport rates for all species with transportation rates. Output is a map
191191
# taking each species' symbolics form to its transportation rates across all edges.
192192
function compute_all_transport_rates(p_val_dict, lrs::LatticeReactionSystem)
193-
# For all species with transportation, compute their transportation rate (across all edges).
193+
# For all species with transportation, compute their transportation rate (across all edges).
194194
# This is a vector, pairing each species to these rates.
195195
unsorted_rates = [s => compute_transport_rates(s, p_val_dict, lrs)
196196
for s in spatial_species(lrs)]
@@ -199,7 +199,7 @@ function compute_all_transport_rates(p_val_dict, lrs::LatticeReactionSystem)
199199
return sort(unsorted_rates; by = rate -> findfirst(isequal(rate[1]), species(lrs)))
200200
end
201201

202-
# For the expression describing the rate of transport (likely only a single parameter, e.g. `D`),
202+
# For the expression describing the rate of transport (likely only a single parameter, e.g. `D`),
203203
# and the values of all our parameters, compute the transport rate(s).
204204
# If all parameters that the rate depends on are uniform across all edges, this becomes a length-1 vector.
205205
# Else it becomes a vector where each value corresponds to the rate at one specific edge.
@@ -304,10 +304,21 @@ end
304304

305305
### System Property Checks ###
306306

307-
# For a Symbolic expression, and a parameter set, check if any relevant parameters have a
307+
# For a Symbolic expression, and a parameter set, check if any relevant parameters have a
308308
# spatial component. Filters out any parameters that are edge parameters.
309309
function has_spatial_vertex_component(exp, ps)
310310
relevant_syms = Symbolics.get_variables(exp)
311311
value_dict = Dict(filter(p -> p[2] isa Vector, ps))
312312
return any(length(value_dict[sym]) > 1 for sym in relevant_syms)
313313
end
314+
315+
### Utilities previously in ModelingToolkit.jl ###
316+
317+
function todict(d)
318+
eltype(d) <: Pair || throw(ArgumentError("The variable-value mapping must be a Dict."))
319+
d isa Dict ? d : Dict(d)
320+
end
321+
322+
_merge(d1, d2) = merge(todict(d1), todict(d2))
323+
324+
MT.refreshed_metadata(::Nothing) = MT.MetadataT() # FIXME: Type piracy

0 commit comments

Comments
 (0)