Skip to content

Commit 8aacf07

Browse files
authored
Merge pull request #1077 from isaacsas/collect_vars_support
collect_vars support
2 parents 7d2c223 + f13bf2e commit 8aacf07

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/Catalyst.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ using ModelingToolkit: Symbolic, value, get_unknowns, get_ps, get_iv, get_system
3131
import ModelingToolkit: get_variables, namespace_expr, namespace_equation, get_variables!,
3232
modified_unknowns!, validate, namespace_variables,
3333
namespace_parameters, rename, renamespace, getname, flatten,
34-
is_alg_equation, is_diff_equation
34+
is_alg_equation, is_diff_equation, collect_vars!,
35+
eqtype_supports_collect_vars
3536

3637
# internal but needed ModelingToolkit functions
3738
import ModelingToolkit: check_variables,

src/reaction.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,30 @@ end
348348
MT.is_diff_equation(rx::Reaction) = false
349349
MT.is_alg_equation(rx::Reaction) = false
350350

351+
# MTK functions for extracting variables within equation type object
352+
MT.eqtype_supports_collect_vars(rx::Reaction) = true
353+
function MT.collect_vars!(unknowns, parameters, rx::Reaction, iv; depth = 0,
354+
op = MT.Differential)
355+
MT.collect_vars!(unknowns, parameters, rx.rate, iv; depth, op)
356+
for sub in rx.substrates
357+
MT.collect_vars!(unknowns, parameters, sub, iv; depth, op)
358+
end
359+
for prod in rx.products
360+
MT.collect_vars!(unknowns, parameters, prod, iv; depth, op)
361+
end
362+
for substoich in rx.substoich
363+
MT.collect_vars!(unknowns, parameters, substoich, iv; depth, op)
364+
end
365+
for prodstoich in rx.prodstoich
366+
MT.collect_vars!(unknowns, parameters, prodstoich, iv; depth, op)
367+
end
368+
if hasnoisescaling(rx)
369+
ns = getnoisescaling(rx)
370+
MT.collect_vars!(unknowns, parameters, ns, iv; depth, op)
371+
end
372+
return nothing
373+
end
374+
351375
"""
352376
get_symbolics(set, rx::Reaction)
353377

test/reactionsystem_core/reaction.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Fetch packages.
44
using Catalyst, Test
55
using Catalyst: get_symbolics
6-
using ModelingToolkit: value, get_variables!
6+
using ModelingToolkit: value, get_variables!, collect_vars!, eqtype_supports_collect_vars
77

88
# Sets the default `t` to use.
99
t = default_t()
@@ -235,4 +235,20 @@ let
235235
@test Catalyst.hasmisc(r2)
236236
@test_throws Exception Catalyst.getmisc(r1)
237237
@test isequal(Catalyst.getmisc(r2), ('M', :M))
238+
end
239+
240+
# tests for collect_vars!
241+
let
242+
t = default_t()
243+
@variables E(t) F(t)
244+
@species A(t) B(t) C(t) D(t)
245+
@parameters k1, k2, η
246+
247+
rx = Reaction(k1*E, [A, B], [C], [k2*D, 3], [F], metadata = [:noise_scaling => η])
248+
us = Set()
249+
ps = Set()
250+
@test eqtype_supports_collect_vars(rx) == true
251+
collect_vars!(us, ps, rx, t)
252+
@test us == Set((A, B, C, D, E, F))
253+
@test ps == Set((k1, k2, η))
238254
end

0 commit comments

Comments
 (0)