Skip to content

Commit a569190

Browse files
committed
rewors
1 parent cfa6b25 commit a569190

27 files changed

+624
-464
lines changed

test/dsl/custom_functions.jl

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
### Fetch Packages and Set Global Variables ###
32
using DiffEqBase, Catalyst, Random, Symbolics, Test
43
using ModelingToolkit: get_unknowns, get_ps
@@ -7,6 +6,9 @@ t = default_t()
76
using StableRNGs
87
rng = StableRNG(12345)
98

9+
# Fetch test functions.
10+
include("../test_functions.jl")
11+
1012
### Tests Custom Functions ###
1113
let
1214
new_hill(x, v, k, n) = v * x^n / (k^n + x^n)
@@ -33,32 +35,13 @@ let
3335
v5 * (X7^2) / (K5^2 + X7^2 + Y7^2), X7 + Y7 --> Z7
3436
end
3537

36-
function permute_ps(pvals, rn1, rn2)
37-
ps1 = parameters(rn1)
38-
ps2 = parameters(rn2)
39-
pvals2 = similar(pvals)
40-
for (i, p) in enumerate(ps2)
41-
pidx = findfirst(isequal(p), ps1)
42-
pvals2[i] = pvals[pidx]
43-
end
44-
pvals2
45-
end
46-
47-
f1 = ODEFunction(convert(ODESystem, custom_function_network_1), jac = true)
48-
f2 = ODEFunction(convert(ODESystem, custom_function_network_2), jac = true)
49-
g1 = SDEFunction(convert(SDESystem, custom_function_network_1))
50-
g2 = SDEFunction(convert(SDESystem, custom_function_network_2))
5138
for factor in [1e-2, 1e-1, 1e0, 1e1, 1e2]
52-
u0 = factor * rand(rng, length(get_unknowns(custom_function_network_1)))
53-
p = factor * rand(rng, length(get_ps(custom_function_network_2)))
54-
55-
# needed as this code assumes an ordering of the parameters and species...
56-
p2 = permute_ps(p, custom_function_network_1, custom_function_network_2)
57-
39+
u0 = rnd_u0(custom_function_network_1, rng; factor)
40+
ps = rnd_ps(custom_function_network_1, rng; factor)
5841
t = rand(rng)
59-
@test all(abs.(f1(u0, p, t) .- f2(u0, p2, t)) .< 10e-10)
60-
@test all(abs.(f1.jac(u0, p, t) .- f2.jac(u0, p2, t)) .< 10e-10)
61-
@test all(abs.(g1(u0, p, t) .- g2(u0, p2, t)) .< 10e-10)
42+
@test f_eval(custom_function_network_1, u0, ps, t) f_eval(custom_function_network_2, u0, ps, t)
43+
@test jac_eval(custom_function_network_1, u0, ps, t) jac_eval(custom_function_network_2, u0, ps, t)
44+
@test g_eval(custom_function_network_1, u0, ps, t) g_eval(custom_function_network_2, u0, ps, t)
6245
end
6346
end
6447

test/dsl/dsl_basics.jl

Lines changed: 66 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22

33
### Fetch Packages and Set Global Variables ###
44

5+
# Fetch packages.
56
using Catalyst, ModelingToolkit
7+
8+
# Set creates the `t` independent variable.
69
t = default_t()
710

811
### Naming Tests ###
912

13+
# Test that the correct name is generated.
1014
let
1115
@parameters k
1216
@species A(t)
@@ -63,63 +67,58 @@ end
6367

6468
### Test Interpolation Within the DSL ###
6569

66-
@parameters α k k1 k2
67-
@species A(t) B(t) C(t) D(t)
68-
70+
# Tests basic interpolation cases.
6971
let
70-
AA = A
71-
AAA = A^2 + B
72-
rn = @reaction_network rn begin
73-
@parameters k
74-
@species A(t) B(t) C(t) D(t)
75-
k*$AAA, C --> D
76-
end
77-
rn2 = ReactionSystem([Reaction(k*AAA, [C], [D])], t; name=:rn)
78-
@test rn == rn2
72+
# Declares parameters and species used across the test.
73+
@parameters α k k1 k2
74+
@species A(t) B(t) C(t) D(t)
7975

80-
rn = @reaction_network rn begin
81-
@parameters k
82-
@species A(t) C(t) D(t)
83-
k, $AA + C --> D
84-
end
85-
rn2 = ReactionSystem([Reaction(k, [AA,C], [D])], t; name=:rn)
86-
@test rn == rn2
87-
end
76+
let
77+
AA = A
78+
AAA = A^2 + B
79+
rn = @reaction_network rn begin
80+
@parameters k
81+
@species A(t) B(t) C(t) D(t)
82+
k*$AAA, C --> D
83+
end
84+
rn2 = ReactionSystem([Reaction(k*AAA, [C], [D])], t; name=:rn)
85+
@test rn == rn2
8886

89-
let
90-
BB = B; A2 = A
91-
rn = @reaction_network rn begin
92-
@parameters k1 k2
93-
(k1,k2), C + $A2 + $BB + $A2 <--> $BB + $BB
87+
rn = @reaction_network rn begin
88+
@parameters k
89+
@species A(t) C(t) D(t)
90+
k, $AA + C --> D
91+
end
92+
rn2 = ReactionSystem([Reaction(k, [AA,C], [D])], t; name=:rn)
93+
@test rn == rn2
9494
end
95-
rn2 = ReactionSystem([Reaction(k1, [C, A, B], [B], [1,2,1],[2]),
96-
Reaction(k2, [B], [C, A, B], [2], [1,2,1])],
97-
t; name=:rn)
98-
@test rn == rn2
99-
end
10095

101-
let
102-
AA = A
103-
kk1 = k^2*A
104-
kk2 = k1+k2
105-
rn = @reaction_network rn begin
106-
@parameters α k k1 k2
107-
α+$kk1*$kk2*$AA, 2*$AA + B --> $AA
96+
let
97+
BB = B; A2 = A
98+
rn = @reaction_network rn begin
99+
@parameters k1 k2
100+
(k1,k2), C + $A2 + $BB + $A2 <--> $BB + $BB
101+
end
102+
rn2 = ReactionSystem([Reaction(k1, [C, A, B], [B], [1,2,1],[2]),
103+
Reaction(k2, [B], [C, A, B], [2], [1,2,1])],
104+
t; name=:rn)
105+
@test rn == rn2
108106
end
109-
rn2 = ReactionSystem([Reaction+kk1*kk2*AA, [A, B], [A], [2, 1], [1])], t; name=:rn)
110-
@test rn == rn2
111-
end
112107

113-
@testset "make_reaction_system can be called from another module" begin
114-
ex = quote
115-
(Ka, Depot --> Central)
116-
(CL / Vc, Central --> 0)
108+
let
109+
AA = A
110+
kk1 = k^2*A
111+
kk2 = k1+k2
112+
rn = @reaction_network rn begin
113+
@parameters α k k1 k2
114+
α+$kk1*$kk2*$AA, 2*$AA + B --> $AA
115+
end
116+
rn2 = ReactionSystem([Reaction+kk1*kk2*AA, [A, B], [A], [2, 1], [1])], t; name=:rn)
117+
@test rn == rn2
117118
end
118-
# Line number nodes aren't ignored so have to be manually removed
119-
Base.remove_linenums!(ex)
120-
@test eval(Catalyst.make_reaction_system(ex)) isa ReactionSystem
121119
end
122120

121+
# Miscellaneous interpolation tests. Unsure what they do here (not related to DSL).
123122
let
124123
rx = @reaction k*h, A + 2*B --> 3*C + D
125124
@parameters k h
@@ -133,10 +132,21 @@ let
133132
@test rx == Reaction(b+ex, [A,C], nothing, [2,1], nothing)
134133
end
135134

135+
# Creates a reaction network using `eval` and internal function.
136+
let
137+
ex = quote
138+
(Ka, Depot --> Central)
139+
(CL / Vc, Central --> 0)
140+
end
141+
# Line number nodes aren't ignored so have to be manually removed
142+
Base.remove_linenums!(ex)
143+
@test eval(Catalyst.make_reaction_system(ex)) isa ReactionSystem
144+
end
145+
136146
### Tests Reaction Metadata ###
137147

138-
# Tests construction for various types of metadata.
139-
# Tests accessor functions.
148+
# Tests construction for various types of reaction metadata.
149+
# Tests reaction metadata accessor functions.
140150
let
141151
# Creates reactions directly.
142152
@variables t
@@ -252,7 +262,7 @@ let
252262
@test isequal(rn1,rn2)
253263
end
254264

255-
### Other tests ###
265+
### Other Tests ###
256266

257267
# Test floating point stoichiometry work.
258268
let
@@ -272,7 +282,7 @@ let
272282
@test rn == mixedsys
273283
end
274284

275-
# Test variables that appear only in rates and aren't ps
285+
# Test that variables that appear only in rates and aren't ps
276286
# are categorized as species.
277287
let
278288
rn = @reaction_network begin
@@ -312,7 +322,7 @@ let
312322
@test length(equations(osys2)) == 2
313323
end
314324

315-
# test @variables in DSL
325+
# Test @variables in DSL.
316326
let
317327
rn = @reaction_network tester begin
318328
@parameters k1
@@ -341,7 +351,7 @@ let
341351
end
342352
end
343353

344-
# ivs test
354+
# Test ivs in DSL.
345355
let
346356
rn = @reaction_network ivstest begin
347357
@ivs s x
@@ -350,19 +360,21 @@ let
350360
@species A(s,x) B(s) C(x)
351361
k*k2*D, E*A +B --> F*C + C2
352362
end
363+
353364
@parameters k k2
354365
@variables s x D(x) E(s) F(s,x)
355366
@species A(s,x) B(s) C(x) C2(s,x)
356367
rx = Reaction(k*k2*D, [A, B], [C, C2], [E, 1], [F, 1])
357368
@named ivstest = ReactionSystem([rx], s; spatial_ivs = [x])
369+
358370
@test ivstest == rn
359371
@test issetequal(unknowns(rn), [D, E, F, A, B, C, C2])
360372
@test issetequal(species(rn), [A, B, C, C2])
361373
@test isequal(ModelingToolkit.get_iv(rn), s)
362374
@test issetequal(Catalyst.get_sivs(rn), [x])
363375
end
364376

365-
# array variables test
377+
# Array variables test.
366378
let
367379
rn = @reaction_network arrtest begin
368380
@parameters k[1:2] a

test/dsl/dsl_model_construction.jl

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ let
7676
basic_test(reaction_networks_weird[2], 4, [:X, :Y, :Z], [:k1, :k2, :k3, :k4])
7777
end
7878

79-
# Tries making various systems.
79+
# Compares networks to networks created using different arrow types.
8080
let
8181
identical_networks_1 = Vector{Pair}()
8282

@@ -137,7 +137,7 @@ let
137137
end
138138
end
139139

140-
# Tests that networks expressed in different ways are identical.
140+
# Compares networks to networks written in different ways.
141141
let
142142
identical_networks_2 = Vector{Pair}()
143143

@@ -203,7 +203,7 @@ let
203203
end
204204
end
205205

206-
# Test networks without parameters.
206+
# Compares networks to networks written without parameters
207207
let
208208
identical_networks_3 = Vector{Pair}()
209209
parameter_sets = []
@@ -217,8 +217,8 @@ let
217217
(sqrt(3.7), exp(1.9)), X4 X1 + X2
218218
end
219219
push!(identical_networks_3, reaction_networks_standard[9] => no_parameters_9)
220-
push!(parameter_sets,
221-
[1.5, 1, 2, 0.01, 2.3, 1001, π, 42, 19.9, 999.99, sqrt(3.7), exp(1.9)])
220+
push!(parameter_sets, [:p1 => 1.5, :p2 => 1, :p3 => 2, :d1 => 0.01, :d2 => 2.3, :d3 => 1001,
221+
:k1 => π, :k2 => 42, :k3 => 19.9, :k4 => 999.99, :k5 => sqrt(3.7), :k6 => exp(1.9)])
222222

223223
no_parameters_10 = @reaction_network begin
224224
0.01, ∅ X1
@@ -229,19 +229,17 @@ let
229229
1.0, X5
230230
end
231231
push!(identical_networks_3, reaction_networks_standard[10] => no_parameters_10)
232-
push!(parameter_sets, [0.01, 3.1, 3.2, 0.0, 2.1, 901.0, 63.5, 7, 8, 1.0])
232+
push!(parameter_sets, [:p => 0.01, :k1 => 3.1, :k2 => 3.2, :k3 => 0.0, :k4 => 2.1, :k5 => 901.0,
233+
:k6 => 63.5, :k7 => 7, :k8 => 8, :d => 1.0])
233234

234-
for (i, networks) in enumerate(identical_networks_3)
235+
for (networks, p_1) in zip(identical_networks_3, parameter_sets)
235236
for factor in [1e-2, 1e-1, 1e0, 1e1, 1e2, 1e3]
236-
u0_1 = rnd_u0(networks[1], rng; factor)
237-
p_1 = Pair.(parameters(networks[1]), parameter_sets[i])
238-
u0_2 = Pair.(unknowns(networks[2]), last.(u0_1))
239-
p_2 = []
237+
u0 = rnd_u0(networks[1], rng; factor)
240238
t = rand(rng)
241239

242-
@test f_eval(networks[1], u0_1, p_1, t) f_eval(networks[2], u0_2, p_2, t)
243-
@test jac_eval(networks[1], u0_1, p_1, t) jac_eval(networks[2], u0_2, p_2, t)
244-
@test g_eval(networks[1], u0_1, p_1, t) g_eval(networks[2], u0_2, p_2, t)
240+
@test f_eval(networks[1], u0, p_1, t) f_eval(networks[2], u0, [], t)
241+
@test jac_eval(networks[1], u0, p_1, t) jac_eval(networks[2], u0, [], t)
242+
@test g_eval(networks[1], u0, p_1, t) g_eval(networks[2], u0, [], t)
245243
end
246244
end
247245
end
@@ -318,7 +316,7 @@ let
318316
end
319317
end
320318

321-
# Test various names as varriables.
319+
# CHeck that various symbols can be used as species/parameter names.
322320
let
323321
@reaction_network begin
324322
(a, A), n N
@@ -359,7 +357,7 @@ let
359357
end
360358
end
361359

362-
# Test that I works.
360+
# Test that I works as a name.
363361
let
364362
rn = @reaction_network begin
365363
k1, S + I --> 2I
@@ -370,16 +368,7 @@ let
370368
@test any(isequal(I), unknowns(rn))
371369
end
372370

373-
# Test names work.
374-
let
375-
rn = @reaction_network SIR1 begin
376-
k1, S + I --> 2I
377-
k2, I --> R
378-
end
379-
@test nameof(rn) == :SIR1
380-
end
381-
382-
# Tests some arrow variants.
371+
# Tests backwards and double arrows.
383372
let
384373
rn1 = @reaction_network arrowtest begin
385374
(a1, a2), C <--> 0
@@ -406,7 +395,7 @@ let
406395
@test isequal((@reaction k, 0 --> X), (@reaction k, 0 X))
407396
end
408397

409-
# Test forbidden and special symbols.
398+
# Test that symbols with special mean, or that are forbidden, are handled properly.
410399
let
411400
test_network = @reaction_network begin t * k, X -->end
412401
@test length(species(test_network)) == 1

0 commit comments

Comments
 (0)