Skip to content
Merged
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: 4 additions & 2 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
all parameters of a system and all `ReactionSystem` subsystems. The latter
correspond to those parameters used within `Reaction`s.)
- Added a custom `hash` for `Reaction`s to ensure they work in `Dict`s and
`Set`s properly, and set-type comparisons between collections of `Reaction`s
work.
`Set`s properly, ensuring set-type comparisons between collections of
`Reaction`s work.
- `ReactionSystem(rxs::Vector{Reaction}, t)` should now work and will infer the
species and parameters.

## Catalyst 9.0
*1.* **BREAKING:** `netstoichmat`, `prodstoichmat` and `substoichmat` are now
Expand Down
49 changes: 37 additions & 12 deletions src/reactionsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ function get_netstoich(subs, prods, sstoich, pstoich)
ns
end


"""
$(TYPEDEF)

Expand Down Expand Up @@ -185,11 +186,11 @@ struct ReactionSystem <: ModelingToolkit.AbstractTimeDependentSystem
"""Type of the system"""
connection_type::Any

function ReactionSystem(eqs, iv, states, ps, observed, name, systems, defaults, connection_type; checks::Bool = true)

iv′ = value(iv)
states′ = value.(states)
ps′ = value.(ps)
function ReactionSystem(eqs, iv, states, ps, observed, name, systems, defaults, connection_type;
checks::Bool=true, skipvalue=false)
iv′ = value(iv)
states′ = skipvalue ? states : value.(MT.scalarize(states))
ps′ = skipvalue ? ps : value.(MT.scalarize(ps))

if checks
check_variables(states′, iv′)
Expand All @@ -198,7 +199,6 @@ struct ReactionSystem <: ModelingToolkit.AbstractTimeDependentSystem
end
rs = new(collect(eqs), iv′, states′, ps′, observed, name, systems, defaults, connection_type)
checks && validate(rs)

rs
end
end
Expand All @@ -207,17 +207,42 @@ function ReactionSystem(eqs, iv, species, ps;
observed = [],
systems = [],
name = nothing,
default_u0=Dict(),
default_p=Dict(),
defaults=_merge(Dict(default_u0), Dict(default_p)),
connection_type=nothing,
checks = true)
default_u0 = Dict(),
default_p = Dict(),
defaults =_merge(Dict(default_u0), Dict(default_p)),
connection_type = nothing,
checks = true,
skipvalue = false)
name === nothing && throw(ArgumentError("The `name` keyword must be provided. Please consider using the `@named` macro"))

ReactionSystem(eqs, iv, species, ps, observed, name, systems, defaults, connection_type,
checks = checks)
checks = checks, skipvalue = skipvalue)
end

function ReactionSystem(rxs::Vector{<:Reaction}, iv; kwargs...)
t = value(iv)
sts = Set(spec for rx in rxs for spec in rx.substrates)
foreach(v -> push!(sts,v), (prod for rx in rxs for prod in rx.products))

ps = Set()
vars = Set()
for rx in rxs
MT.get_variables!(vars, rx.rate)
for var in vars
isequal(t,var) && continue
if MT.isparameter(var)
push!(ps, var)
else
push!(sts, var)
end
end
empty!(vars)
end

ReactionSystem(rxs, t, collect(sts), collect(ps); skipvalue=true, kwargs...)
end


function ReactionSystem(iv; kwargs...)
ReactionSystem(Reaction[], iv, [], []; kwargs...)
end
Expand Down
11 changes: 11 additions & 0 deletions test/reactionsystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ rxs = [Reaction(k[1], nothing, [A]), # 0 -> A
odesys = convert(ODESystem,rs)
sdesys = convert(SDESystem,rs)

# test equation only constructor
@named rs2 = ReactionSystem(rxs,t)
@test Catalyst.isequal_ignore_names(rs,rs2)

# test show
io = IOBuffer()
show(io, rs)
Expand Down Expand Up @@ -272,3 +276,10 @@ js = convert(JumpSystem, rs)
@test isequal2(equations(js)[1].scaled_rates, k1/12)
js = convert(JumpSystem,rs; combinatoric_ratelaws=false)
@test isequal2(equations(js)[1].scaled_rates, k1)

# test building directly from rxs
@parameters x,y
rxs = [Reaction(x*t*A*B+y, [A], nothing)]
@named rs1 = ReactionSystem(rxs, t, [A,B], [x,y])
@named rs2 = ReactionSystem(rxs, t)
@test Catalyst.isequal_ignore_names(rs1,rs2)