Skip to content

Commit 518e54f

Browse files
committed
more tests
1 parent 784f754 commit 518e54f

File tree

3 files changed

+88
-32
lines changed

3 files changed

+88
-32
lines changed

test/dsl/dsl_advanced_model_construction.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,3 +360,25 @@ let
360360
@named twostate = ReactionSystem([rx1, rx2], t)
361361
@test twostate == rn
362362
end
363+
364+
############## tests related to hybrid systems ###################
365+
let
366+
t = default_t()
367+
D = default_time_deriv()
368+
@parameters λ k
369+
@variables V(t)
370+
@species A(t)
371+
rx = Reaction(k*V, [], [A])
372+
eq = D(V) ~ λ*V
373+
cevents = [[V ~ 2.0] => [V ~ V/2, A ~ A/2]]
374+
@named hybrid = ReactionSystem([rx, eq], t; continuous_events = cevents)
375+
rn = @reaction_network hybrid begin
376+
@parameters λ
377+
k*V, 0 --> A
378+
@equations D(V) ~ λ*V
379+
@continuous_events begin
380+
[V ~ 2.0] => [V ~ V/2, A ~ A/2]
381+
end
382+
end
383+
@test hybrid == rn
384+
end

test/reactionsystem_core/reactionsystem.jl

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,37 +1002,28 @@ let
10021002
@test size(nps.incidencemat) == (3,3)
10031003
end
10041004

1005-
# test JumpInputs function auto problem selection
1005+
########## tests related to hybrid systems ##########
10061006
let
1007-
rn = @reaction_network begin
1008-
k*(1 + sin(t)), 0 --> A
1009-
end
1010-
jinput = JumpInputs(rn, [:A => 0], (0.0, 10.0), [:k => .5])
1011-
@test jinput.prob isa ODEProblem
1012-
jprob = JumpProblem(jinput; rng)
1013-
sol = solve(jprob, Tsit5())
1014-
@test sol(10.0; idxs = :A) > 0
1015-
1016-
rn = @reaction_network begin
1017-
k, 0 --> A
1018-
end
1019-
jinput = JumpInputs(rn, [:A => 0], (0.0, 10.0), [:k => .5])
1020-
@test jinput.prob isa DiscreteProblem
1021-
jprob = JumpProblem(jinput; rng)
1022-
sol = solve(jprob)
1023-
@test sol(10.0; idxs = :A) > 0
1024-
1025-
rn = @reaction_network begin
1026-
@parameters λ
1027-
k*V, 0 --> A
1028-
@equations D(V) ~ λ*V
1029-
@continuous_events begin
1030-
[V ~ 2.0] => [V ~ V/2, A ~ rand()*A]
1031-
end
1032-
end
1033-
jinput = JumpInputs(rn, [:A => 0, :V => 1.0], (0.0, 10.0), [:k => 1.0, => .2])
1007+
t = default_t()
1008+
D = default_time_deriv()
1009+
@parameters λ k
1010+
@variables V(t)
1011+
@species A(t) B(t) C(t)
1012+
rxs = [Reaction(k*V, [], [A]), Reaction*A, [B], nothing),
1013+
Reaction(k, [A, B], nothing), Reaction(λ, [C], [A])]
1014+
eqs = [D(V) ~ λ*V*C]
1015+
cevents = [[V ~ 2.0] => [V ~ V/2, A ~ A/2]]
1016+
@named rs = ReactionSystem(vcat(rxs, eqs), t; continuous_events = cevents)
1017+
rs = complete(rs)
1018+
jinput = JumpInputs(rn, [:A => 0, :V => 1.0], (0.0, 10.0), [:k => 1.0, => .4])
10341019
@test jinput.prob isa ODEProblem
1035-
jprob = JumpProblem(jinput; rng)
1036-
sol = solve(jprob, Tsit5())
1037-
1038-
end
1020+
sys = jinput.sys
1021+
@test sys isa JumpSystem
1022+
@test MT.has_equations(sys)
1023+
eqs = MT.equations(sys)
1024+
@test isempty(eqs.x[1])
1025+
@test isempty(eqs.x[2])
1026+
@test length(eqs.x[3]) == 1
1027+
@test length(eqs.x[4]) == 2 # dA/dt and dV/dt
1028+
@test length(continuous_events(sys)) == 1
1029+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Fetch packages.
2+
using Catalyst, JumpProcesses, OrdinaryDiffEqTsit5, Statistics, Test
3+
4+
# Sets stable rng number.
5+
using StableRNGs
6+
rng = StableRNG(12345)
7+
seed = rand(rng, 1:100)
8+
9+
# test JumpInputs function auto problem selection
10+
let
11+
rn = @reaction_network begin
12+
k*(1 + sin(t)), 0 --> A
13+
end
14+
jinput = JumpInputs(rn, [:A => 0], (0.0, 10.0), [:k => .5])
15+
@test jinput.prob isa ODEProblem
16+
jprob = JumpProblem(jinput; rng)
17+
sol = solve(jprob, Tsit5())
18+
@test sol(10.0; idxs = :A) > 0
19+
20+
rn = @reaction_network begin
21+
k, 0 --> A
22+
end
23+
jinput = JumpInputs(rn, [:A => 0], (0.0, 10.0), [:k => .5])
24+
@test jinput.prob isa DiscreteProblem
25+
jprob = JumpProblem(jinput; rng)
26+
sol = solve(jprob)
27+
@test sol(10.0; idxs = :A) > 0
28+
29+
rn = @reaction_network begin
30+
@parameters λ
31+
k*V, 0 --> A
32+
@equations D(V) ~ λ*V
33+
@continuous_events begin
34+
[V ~ 2.0] => [V ~ V/2, A ~ A/2]
35+
end
36+
end
37+
jinput = JumpInputs(rn, [:A => 0, :V => 1.0], (0.0, 10.0), [:k => 1.0, => .4])
38+
@test jinput.prob isa ODEProblem
39+
jprob = JumpProblem(jinput; rng)
40+
sol = solve(jprob, Tsit5())
41+
end
42+
43+

0 commit comments

Comments
 (0)