Skip to content

Commit da912f2

Browse files
authored
Merge pull request #546 from isaacsas/parametric_composition_bug
Parametric composition bug
2 parents e1958d7 + b9e5353 commit da912f2

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "Catalyst"
22
uuid = "479239e8-5488-4da2-87a7-35f2df7eef83"
3-
version = "12.2.0"
3+
version = "12.2.1"
44

55
[deps]
66
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

src/reactionsystem.jl

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,27 @@ function Base.show(io::IO, rx::Reaction)
187187
print_rxside(io, rx.products, rx.prodstoich)
188188
end
189189

190+
function apply_if_nonempty(f, v)
191+
isempty(v) && return v
192+
s = similar(v)
193+
map!(f, s, v)
194+
s
195+
end
196+
190197
function ModelingToolkit.namespace_equation(rx::Reaction, name)
191-
subs = isempty(rx.substrates) ? rx.substrates :
192-
[namespace_expr(sub, name) for sub in rx.substrates]
193-
prods = isempty(rx.products) ? rx.products :
194-
[namespace_expr(prod, name) for prod in rx.products]
195-
Reaction(namespace_expr(rx.rate, name),
196-
subs, prods, rx.substoich, rx.prodstoich,
197-
[namespace_expr(n[1], name) => n[2] for n in rx.netstoich], rx.only_use_rate)
198+
f = Base.Fix2(namespace_expr, name)
199+
rate = f(rx.rate)
200+
subs = apply_if_nonempty(f, rx.substrates)
201+
prods = apply_if_nonempty(f, rx.products)
202+
substoich = apply_if_nonempty(f, rx.substoich)
203+
prodstoich = apply_if_nonempty(f, rx.prodstoich)
204+
netstoich = if isempty(rx.netstoich)
205+
rx.netstoich
206+
else
207+
ns = similar(rx.netstoich)
208+
map!(n -> f(n[1]) => f(n[2]), ns, rx.netstoich)
209+
end
210+
Reaction(rate, subs, prods, substoich, prodstoich, netstoich, rx.only_use_rate)
198211
end
199212

200213
netstoich_stoichtype(::Vector{Pair{S, T}}) where {S, T} = T

test/reactionsystem_components.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,25 @@ let
378378
@test haskey(ModelingToolkit.defaults(fullrn), b)
379379
@test ModelingToolkit.defaults(fullrn)[b] == 2.0
380380
end
381+
382+
# https://github.com/SciML/Catalyst.jl/issues/545
383+
let
384+
rn_AB = @reaction_network AB begin
385+
k1, A --> n*B
386+
end k1 n
387+
388+
rn_BC = @reaction_network BC begin
389+
k2, B --> C
390+
end k2
391+
392+
@variables t
393+
@named rs = ReactionSystem(t; systems = [rn_AB, rn_BC])
394+
sts = states(rs)
395+
@test issetequal(sts, (@variables AB₊A(t) AB₊B(t) BC₊B(t) BC₊C(t)))
396+
ps = parameters(rs)
397+
@test issetequal(ps, (@parameters AB₊k1 AB₊n BC₊k2))
398+
rxs = reactions(rs)
399+
@parameters AB₊n
400+
rxs2 = Reaction[(@reaction AB₊k1, AB₊A --> $(AB₊n)*AB₊B), (@reaction BC₊k2, BC₊B --> BC₊C)]
401+
@test (length(rxs) == length(rxs2)) && issubset(rxs, rxs2)
402+
end

0 commit comments

Comments
 (0)