Skip to content

Commit 445af77

Browse files
committed
split LatticeReactiontests. Move spatial tests to the end of runtests.jl
1 parent aa81594 commit 445af77

File tree

3 files changed

+158
-145
lines changed

3 files changed

+158
-145
lines changed

test/runtests.jl

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ using SafeTestsets, Test
5050
@time @safetestset "MTK Structure Indexing" begin include("upstream/mtk_structure_indexing.jl") end
5151
@time @safetestset "MTK Problem Inputs" begin include("upstream/mtk_problem_inputs.jl") end
5252

53-
# Tests spatial modelling and simulations.
54-
@time @safetestset "PDE Systems Simulations" begin include("spatial_modelling/simulate_PDEs.jl") end
55-
@time @safetestset "Lattice Reaction Systems" begin include("spatial_modelling/lattice_reaction_systems.jl") end
56-
@time @safetestset "Spatial Lattice Variants" begin include("spatial_modelling/lattice_reaction_systems_lattice_types.jl") end
57-
@time @safetestset "ODE Lattice Systems Simulations" begin include("spatial_modelling/lattice_reaction_systems_ODEs.jl") end
58-
@time @safetestset "Jump Lattice Systems Simulations" begin include("spatial_modelling/lattice_reaction_systems_jumps.jl") end
59-
6053
# Tests network visualisation.
6154
@time @safetestset "Latexify" begin include("visualisation/latexify.jl") end
6255
# Disable on Macs as can't install GraphViz via jll
@@ -69,7 +62,15 @@ using SafeTestsets, Test
6962
@time @safetestset "HomotopyContinuation Extension" begin include("extensions/homotopy_continuation.jl") end
7063
@time @safetestset "Structural Identifiability Extension" begin include("extensions/structural_identifiability.jl") end
7164

72-
# test stability (uses HomotopyContinuation extension)
65+
# Transportest stability computation (uses HomotopyContinuation extension).
7366
@time @safetestset "Steady State Stability Computations" begin include("miscellaneous_tests/stability_computation.jl") end
7467

68+
# Tests spatial modelling and simulations.
69+
@time @safetestset "PDE Systems Simulations" begin include("spatial_modelling/simulate_PDEs.jl") end
70+
@time @safetestset "Spatial Reactions" begin include("spatial_modelling/spatial_reactions.jl") end
71+
@time @safetestset "Lattice Reaction Systems" begin include("spatial_modelling/lattice_reaction_systems.jl") end
72+
@time @safetestset "Spatial Lattice Variants" begin include("spatial_modelling/lattice_reaction_systems_lattice_types.jl") end
73+
@time @safetestset "ODE Lattice Systems Simulations" begin include("spatial_modelling/lattice_reaction_systems_ODEs.jl") end
74+
@time @safetestset "Jump Lattice Systems Simulations" begin include("spatial_modelling/lattice_reaction_systems_jumps.jl") end
75+
7576
end # @time

test/spatial_modelling/lattice_reaction_systems.jl

Lines changed: 13 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,9 @@ using Catalyst, Graphs, OrdinaryDiffEq, Test
66
# Fetch test networks.
77
include("../spatial_test_networks.jl")
88

9-
# Pre declares a grid.
9+
# Pre-declares a set of grids.
1010
grids = [very_small_2d_cartesian_grid, very_small_2d_masked_grid, very_small_2d_graph_grid]
1111

12-
### Test Spatial Reactions ###
13-
14-
# Test creation of TransportReaction with non-parameters in rate.
15-
# Tests that it works even when rate is highly nested.
16-
let
17-
@variables t
18-
@species X(t) Y(t)
19-
@parameters D1 D2 D3
20-
@test_throws ErrorException TransportReaction(D1 + D2*(D3 + Y), X)
21-
@test_throws ErrorException TransportReaction(Y, X)
22-
end
23-
24-
# Checks that the `hash` functions works for `TransportReaction`s.
25-
let
26-
tr1 = @transport_reaction D1 X
27-
tr2 = @transport_reaction D1 X
28-
tr3 = @transport_reaction D2 X
29-
hash(tr1, 0x0000000000000001) == hash(tr2, 0x0000000000000001)
30-
hash(tr2, 0x0000000000000001) != hash(tr3, 0x0000000000000001)
31-
end
3212

3313
### Tests LatticeReactionSystem Getters Correctness ###
3414

@@ -179,110 +159,6 @@ let
179159
@test isequal(independent_variables(lrs), [t])
180160
end
181161

182-
### Tests Spatial Reactions Getters Correctness ###
183-
184-
# Test case 1.
185-
let
186-
tr_1 = @transport_reaction dX X
187-
tr_2 = @transport_reaction dY1*dY2 Y
188-
189-
# @test ModelingToolkit.getname.(species(tr_1)) == ModelingToolkit.getname.(spatial_species(tr_1)) == [:X] # species(::TransportReaction) currently not supported.
190-
# @test ModelingToolkit.getname.(species(tr_2)) == ModelingToolkit.getname.(spatial_species(tr_2)) == [:Y]
191-
@test ModelingToolkit.getname.(spatial_species(tr_1)) == [:X]
192-
@test ModelingToolkit.getname.(spatial_species(tr_2)) == [:Y]
193-
@test ModelingToolkit.getname.(parameters(tr_1)) == [:dX]
194-
@test ModelingToolkit.getname.(parameters(tr_2)) == [:dY1, :dY2]
195-
196-
# @test issetequal(species(tr_1), [tr_1.species])
197-
# @test issetequal(species(tr_2), [tr_2.species])
198-
@test issetequal(spatial_species(tr_1), [tr_1.species])
199-
@test issetequal(spatial_species(tr_2), [tr_2.species])
200-
end
201-
202-
# Test case 2.
203-
let
204-
rs = @reaction_network begin
205-
@species X(t) Y(t)
206-
@parameters dX dY1 dY2
207-
end
208-
@unpack X, Y, dX, dY1, dY2 = rs
209-
tr_1 = TransportReaction(dX, X)
210-
tr_2 = TransportReaction(dY1*dY2, Y)
211-
# @test isequal(species(tr_1), [X])
212-
# @test isequal(species(tr_1), [X])
213-
@test issetequal(spatial_species(tr_2), [Y])
214-
@test issetequal(spatial_species(tr_2), [Y])
215-
@test issetequal(parameters(tr_1), [dX])
216-
@test issetequal(parameters(tr_2), [dY1, dY2])
217-
end
218-
219-
### Tests Spatial Reactions Generation ###
220-
221-
# Tests TransportReaction with non-trivial rate.
222-
let
223-
rs = @reaction_network begin
224-
@parameters dV dE [edgeparameter=true]
225-
(p,1), 0 <--> X
226-
end
227-
@unpack dV, dE, X = rs
228-
229-
tr = TransportReaction(dV*dE, X)
230-
@test isequal(tr.rate, dV*dE)
231-
end
232-
233-
# Tests transport_reactions function for creating TransportReactions.
234-
let
235-
rs = @reaction_network begin
236-
@parameters d
237-
(p,1), 0 <--> X
238-
end
239-
@unpack d, X = rs
240-
trs = TransportReactions([(d, X), (d, X)])
241-
@test isequal(trs[1], trs[2])
242-
end
243-
244-
# Test reactions with constants in rate.
245-
let
246-
@variables t
247-
@species X(t) Y(t)
248-
249-
tr_1 = TransportReaction(1.5, X)
250-
tr_1_macro = @transport_reaction 1.5 X
251-
@test isequal(tr_1.rate, tr_1_macro.rate)
252-
@test isequal(tr_1.species, tr_1_macro.species)
253-
254-
tr_2 = TransportReaction(π, Y)
255-
tr_2_macro = @transport_reaction π Y
256-
@test isequal(tr_2.rate, tr_2_macro.rate)
257-
@test isequal(tr_2.species, tr_2_macro.species)
258-
end
259-
260-
### Test Interpolation ###
261-
262-
# Does not currently work. The 3 tr_macro_ lines generate errors.
263-
# Test case 1.
264-
let
265-
rs = @reaction_network begin
266-
@species X(t) Y(t) Z(t)
267-
@parameters dX dY1 dY2 dZ
268-
end
269-
@unpack X, Y, Z, dX, dY1, dY2, dZ = rs
270-
rate1 = dX
271-
rate2 = dY1*dY2
272-
species3 = Z
273-
tr_1 = TransportReaction(dX, X)
274-
tr_2 = TransportReaction(dY1*dY2, Y)
275-
tr_3 = TransportReaction(dZ, Z)
276-
tr_macro_1 = @transport_reaction $dX X
277-
tr_macro_2 = @transport_reaction $(rate2) Y
278-
@test_broken false
279-
# tr_macro_3 = @transport_reaction dZ $species3 # Currently does not work, something with meta programming.
280-
281-
@test isequal(tr_1, tr_macro_1)
282-
@test isequal(tr_2, tr_macro_2)
283-
# @test isequal(tr_3, tr_macro_3)
284-
end
285-
286162
### Tests Error generation ###
287163

288164
# Network where diffusion species is not declared in non-spatial network.
@@ -375,13 +251,13 @@ end
375251
# Tests for hierarchical input system.
376252
let
377253
t = default_t()
378-
@parameters d
254+
@parameters d D
379255
@species X(t)
380256
rxs = [Reaction(d, [X], [])]
381257
@named rs1 = ReactionSystem(rxs, t)
382258
@named rs2 = ReactionSystem(rxs, t; systems = [rs1])
383259
rs2 = complete(rs2)
384-
@test_throws ArgumentError LatticeReactionSystem(rs2, [tr], short_path)
260+
@test_throws ArgumentError LatticeReactionSystem(rs2, [TransportReaction(D, X)], CartesianGrid((2,2)))
385261
end
386262

387263
# Tests for non-complete input `ReactionSystem`.
@@ -390,7 +266,7 @@ let
390266
rs = @network_component begin
391267
(p,d), 0 <--> X
392268
end
393-
@test_throws ArgumentError LatticeReactionSystem(rs1, [tr], short_path)
269+
@test_throws ArgumentError LatticeReactionSystem(rs, [tr], CartesianGrid((2,2)))
394270
end
395271

396272
### Tests Grid Vertex and Edge Number Computation ###
@@ -409,14 +285,14 @@ let
409285
random_1d_masked_grid, random_2d_masked_grid, random_3d_masked_grid]
410286
lrs1 = LatticeReactionSystem(SIR_system, SIR_srs_1, lattice)
411287
lrs2 = LatticeReactionSystem(SIR_system, SIR_srs_1, lattice; diagonal_connections=true)
412-
@test lrs1.num_edges == iterator_count(edge_iterator(lrs1))
413-
@test lrs2.num_edges == iterator_count(edge_iterator(lrs2))
288+
@test num_edges(lrs1) == iterator_count(edge_iterator(lrs1))
289+
@test num_edges(lrs2) == iterator_count(edge_iterator(lrs2))
414290
end
415291

416292
# Graph grids (cannot test diagonal connections).
417293
for lattice in [small_2d_graph_grid, small_3d_graph_grid, undirected_cycle, small_directed_cycle, unconnected_graph]
418294
lrs1 = LatticeReactionSystem(SIR_system, SIR_srs_1, lattice)
419-
@test lrs1.num_edges == iterator_count(edge_iterator(lrs1))
295+
@test num_edges(lrs1) == iterator_count(edge_iterator(lrs1))
420296
end
421297
end
422298

@@ -462,33 +338,33 @@ let
462338
lrs = LatticeReactionSystem(rn, [tr], CartesianGrid(n))
463339
ps = [:D => make_directed_edge_values(lrs, (10.0, 0.0))]
464340
oprob = ODEProblem(lrs, u0, tspan, ps)
465-
@test isapprox(solve(oprob, Tsit5())[end][5], n, rtol=1e-6)
341+
@test isapprox(solve(oprob, Tsit5()).u[end][5], n, rtol=1e-6)
466342

467343
# Checks the 2d case (both with 1d and 2d flow).
468344
lrs = LatticeReactionSystem(rn, [tr], CartesianGrid((n,n)))
469345

470346
ps = [:D => make_directed_edge_values(lrs, (1.0, 0.0), (0.0, 0.0))]
471347
oprob = ODEProblem(lrs, u0, tspan, ps)
472-
@test all(isapprox.(solve(oprob, Tsit5())[end][5:5:25], n, rtol=1e-6))
348+
@test all(isapprox.(solve(oprob, Tsit5()).u[end][5:5:25], n, rtol=1e-6))
473349

474350
ps = [:D => make_directed_edge_values(lrs, (1.0, 0.0), (1.0, 0.0))]
475351
oprob = ODEProblem(lrs, u0, tspan, ps)
476-
@test isapprox(solve(oprob, Tsit5())[end][25], n^2, rtol=1e-6)
352+
@test isapprox(solve(oprob, Tsit5()).u[end][25], n^2, rtol=1e-6)
477353

478354
# Checks the 3d case (both with 1d and 2d flow).
479355
lrs = LatticeReactionSystem(rn, [tr], CartesianGrid((n,n,n)))
480356

481357
ps = [:D => make_directed_edge_values(lrs, (1.0, 0.0), (0.0, 0.0), (0.0, 0.0))]
482358
oprob = ODEProblem(lrs, u0, tspan, ps)
483-
@test all(isapprox.(solve(oprob, Tsit5())[end][5:5:125], n, rtol=1e-6))
359+
@test all(isapprox.(solve(oprob, Tsit5()).u[end][5:5:125], n, rtol=1e-6))
484360

485361
ps = [:D => make_directed_edge_values(lrs, (1.0, 0.0), (1.0, 0.0), (0.0, 0.0))]
486362
oprob = ODEProblem(lrs, u0, tspan, ps)
487-
@test all(isapprox.(solve(oprob, Tsit5())[end][25:25:125], n^2, rtol=1e-6))
363+
@test all(isapprox.(solve(oprob, Tsit5()).u[end][25:25:125], n^2, rtol=1e-6))
488364

489365
ps = [:D => make_directed_edge_values(lrs, (1.0, 0.0), (1.0, 0.0), (1.0, 0.0))]
490366
oprob = ODEProblem(lrs, u0, tspan, ps)
491-
@test isapprox(solve(oprob, Tsit5())[end][125], n^3, rtol=1e-6)
367+
@test isapprox(solve(oprob, Tsit5()).u[end][125], n^3, rtol=1e-6)
492368
end
493369

494370
# Checks that erroneous input yields errors.
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
### Preparations ###
2+
3+
# Fetch packages.
4+
using Catalyst, Graphs, OrdinaryDiffEq, Test
5+
6+
# Fetch test networks.
7+
include("../spatial_test_networks.jl")
8+
9+
# Pre-declares a set of grids.
10+
grids = [very_small_2d_cartesian_grid, very_small_2d_masked_grid, very_small_2d_graph_grid]
11+
12+
13+
### TransportReaction Creation Tests ###
14+
15+
# Tests TransportReaction with non-trivial rate.
16+
let
17+
rs = @reaction_network begin
18+
@parameters dV dE [edgeparameter=true]
19+
(p,1), 0 <--> X
20+
end
21+
@unpack dV, dE, X = rs
22+
23+
tr = TransportReaction(dV*dE, X)
24+
@test isequal(tr.rate, dV*dE)
25+
end
26+
27+
# Tests transport_reactions function for creating TransportReactions.
28+
let
29+
rs = @reaction_network begin
30+
@parameters d
31+
(p,1), 0 <--> X
32+
end
33+
@unpack d, X = rs
34+
trs = TransportReactions([(d, X), (d, X)])
35+
@test isequal(trs[1], trs[2])
36+
end
37+
38+
# Test reactions with constants in rate.
39+
let
40+
@variables t
41+
@species X(t) Y(t)
42+
43+
tr_1 = TransportReaction(1.5, X)
44+
tr_1_macro = @transport_reaction 1.5 X
45+
@test isequal(tr_1.rate, tr_1_macro.rate)
46+
@test isequal(tr_1.species, tr_1_macro.species)
47+
48+
tr_2 = TransportReaction(π, Y)
49+
tr_2_macro = @transport_reaction π Y
50+
@test isequal(tr_2.rate, tr_2_macro.rate)
51+
@test isequal(tr_2.species, tr_2_macro.species)
52+
end
53+
54+
### Spatial Reactions Getters Correctness ###
55+
56+
# Test case 1.
57+
let
58+
tr_1 = @transport_reaction dX X
59+
tr_2 = @transport_reaction dY1*dY2 Y
60+
61+
# @test ModelingToolkit.getname.(species(tr_1)) == ModelingToolkit.getname.(spatial_species(tr_1)) == [:X] # species(::TransportReaction) currently not supported.
62+
# @test ModelingToolkit.getname.(species(tr_2)) == ModelingToolkit.getname.(spatial_species(tr_2)) == [:Y]
63+
@test ModelingToolkit.getname.(spatial_species(tr_1)) == [:X]
64+
@test ModelingToolkit.getname.(spatial_species(tr_2)) == [:Y]
65+
@test ModelingToolkit.getname.(parameters(tr_1)) == [:dX]
66+
@test ModelingToolkit.getname.(parameters(tr_2)) == [:dY1, :dY2]
67+
68+
# @test issetequal(species(tr_1), [tr_1.species])
69+
# @test issetequal(species(tr_2), [tr_2.species])
70+
@test issetequal(spatial_species(tr_1), [tr_1.species])
71+
@test issetequal(spatial_species(tr_2), [tr_2.species])
72+
end
73+
74+
# Test case 2.
75+
let
76+
rs = @reaction_network begin
77+
@species X(t) Y(t)
78+
@parameters dX dY1 dY2
79+
end
80+
@unpack X, Y, dX, dY1, dY2 = rs
81+
tr_1 = TransportReaction(dX, X)
82+
tr_2 = TransportReaction(dY1*dY2, Y)
83+
# @test isequal(species(tr_1), [X])
84+
# @test isequal(species(tr_1), [X])
85+
@test issetequal(spatial_species(tr_2), [Y])
86+
@test issetequal(spatial_species(tr_2), [Y])
87+
@test issetequal(parameters(tr_1), [dX])
88+
@test issetequal(parameters(tr_2), [dY1, dY2])
89+
end
90+
91+
### Error Tests ###
92+
93+
# Tests that creation of TransportReaction with non-parameters in rate yield errors.
94+
# Tests that errors are throw even when the rate is highly nested.
95+
let
96+
@variables t
97+
@species X(t) Y(t)
98+
@parameters D1 D2 D3
99+
@test_throws ErrorException TransportReaction(D1 + D2*(D3 + Y), X)
100+
@test_throws ErrorException TransportReaction(Y, X)
101+
end
102+
103+
### Other Tests ###
104+
105+
# Test Interpolation
106+
# Does not currently work. The 3 tr_macro_ lines generate errors.
107+
let
108+
rs = @reaction_network begin
109+
@species X(t) Y(t) Z(t)
110+
@parameters dX dY1 dY2 dZ
111+
end
112+
@unpack X, Y, Z, dX, dY1, dY2, dZ = rs
113+
rate1 = dX
114+
rate2 = dY1*dY2
115+
species3 = Z
116+
tr_1 = TransportReaction(dX, X)
117+
tr_2 = TransportReaction(dY1*dY2, Y)
118+
tr_3 = TransportReaction(dZ, Z)
119+
tr_macro_1 = @transport_reaction $dX X
120+
tr_macro_2 = @transport_reaction $(rate2) Y
121+
@test_broken false
122+
# tr_macro_3 = @transport_reaction dZ $species3 # Currently does not work, something with meta programming.
123+
124+
@test isequal(tr_1, tr_macro_1)
125+
@test isequal(tr_2, tr_macro_2)
126+
# @test isequal(tr_3, tr_macro_3)
127+
end
128+
129+
# Checks that the `hash` functions works for `TransportReaction`s.
130+
let
131+
tr1 = @transport_reaction D1 X
132+
tr2 = @transport_reaction D1 X
133+
tr3 = @transport_reaction D2 X
134+
hash(tr1, 0x0000000000000001) == hash(tr2, 0x0000000000000001)
135+
hash(tr2, 0x0000000000000001) != hash(tr3, 0x0000000000000001)
136+
end

0 commit comments

Comments
 (0)