Skip to content

Commit 50312ad

Browse files
authored
Merge pull request #956 from SciML/new_default_prob_update_tests
[v14 - Awaiting Catalyst update] New default prob update tests
2 parents 3893ddc + 1204fcd commit 50312ad

File tree

3 files changed

+138
-3
lines changed

3 files changed

+138
-3
lines changed

test/network_analysis/conservation_laws.jl

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,55 @@ let
230230
@test all(sol2[osys.X1 + osys.X2] .== 4.0)
231231
end
232232

233+
# Tests system problem updating when conservation laws are eliminated.
234+
# Checks that the correct values are used after the conservation law species are updated.
235+
# Here is an issue related to the broken tests: https://github.com/SciML/Catalyst.jl/issues/952
236+
let
237+
# Create model and fetch the conservation parameter (Γ).
238+
t = default_t()
239+
@parameters k1 k2
240+
@species X1(t) X2(t)
241+
rxs = [
242+
Reaction(k1, [X1], [X2]),
243+
Reaction(k2, [X2], [X1])
244+
]
245+
@named rs = ReactionSystem(rxs, t)
246+
osys = convert(ODESystem, complete(rs); remove_conserved = true, remove_conserved_warn = false)
247+
osys = complete(osys)
248+
@unpack Γ = osys
249+
250+
# Creates an `ODEProblem`.
251+
u0 = [X1 => 1.0, X2 => 2.0]
252+
ps = [k1 => 0.1, k2 => 0.2]
253+
oprob = ODEProblem(osys, u0, (0.0, 1.0), ps)
254+
255+
# Check `ODEProblem` content.
256+
@test oprob[X1] == 1.0
257+
@test oprob[X2] == 2.0
258+
@test oprob.ps[k1] == 0.1
259+
@test oprob.ps[k2] == 0.2
260+
@test oprob.ps[Γ[1]] == 3.0
261+
262+
# Currently, any kind of updating of species or the conservation parameter(s) is not possible.
263+
264+
# Update problem parameters using `remake`.
265+
oprob_new = remake(oprob; p = [k1 => 0.3, k2 => 0.4])
266+
@test oprob_new.ps[k1] == 0.3
267+
@test oprob_new.ps[k2] == 0.4
268+
integrator = init(oprob_new, Tsit5())
269+
@test integrator.ps[k1] == 0.3
270+
@test integrator.ps[k2] == 0.4
271+
272+
# Update problem parameters using direct indexing.
273+
oprob.ps[k1] = 0.5
274+
oprob.ps[k2] = 0.6
275+
@test oprob.ps[k1] == 0.5
276+
@test oprob.ps[k2] == 0.6
277+
integrator = init(oprob, Tsit5())
278+
@test integrator.ps[k1] == 0.5
279+
@test integrator.ps[k2] == 0.6
280+
end
281+
233282
### Other Tests ###
234283

235284
# Checks that `JumpSystem`s with conservation laws cannot be generated.
@@ -288,4 +337,27 @@ let
288337
@test_nowarn XProblem(rn, u0, ps; remove_conserved_warn = false)
289338
@test_nowarn XProblem(rn, u0, ps; remove_conserved = true, remove_conserved_warn = false)
290339
end
291-
end
340+
end
341+
342+
# Conservation law simulations for vectorised species.
343+
let
344+
# Prepares the model.
345+
t = default_t()
346+
@species X(t)[1:2]
347+
@parameters k[1:2]
348+
rxs = [
349+
Reaction(k[1], [X[1]], [X[2]]),
350+
Reaction(k[2], [X[2]], [X[1]])
351+
]
352+
@named rs = ReactionSystem(rxs, t)
353+
rs = complete(rs)
354+
355+
# Checks that simulation reaches known equilibrium.
356+
@test_broken false # Currently broken on MTK .
357+
# u0 = [:X => [3.0, 9.0]]
358+
# ps = [:k => [1.0, 2.0]]
359+
# oprob = ODEProblem(rs, u0, (0.0, 1000.0), ps; remove_conserved = true)
360+
# sol = solve(oprob, Vern7())
361+
# @test sol[X[1]][end] ≈ 8.0
362+
# @test sol[X[2]][end] ≈ 4.0
363+
end

test/reactionsystem_core/reactionsystem.jl

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,70 @@ let
262262
end
263263
end
264264

265+
### Nich Model Declarations ###
266+
267+
# Checks model with vector species and parameters.
268+
# Checks that it works for programmatic/dsl-based modelling.
269+
# Checks that all forms of model input (parameter/initial condition and vector/non-vector) are
270+
# handled properly.
271+
let
272+
# Declares programmatic model.
273+
@parameters p[1:2] k d1 d2
274+
@species (X(t))[1:2] Y1(t) Y2(t)
275+
rxs = [
276+
Reaction(p[1], [], [X[1]]),
277+
Reaction(p[2], [], [X[2]]),
278+
Reaction(k, [X[1]], [Y1]),
279+
Reaction(k, [X[2]], [Y2]),
280+
Reaction(d1, [Y1], []),
281+
Reaction(d2, [Y2], []),
282+
]
283+
rs_prog = complete(ReactionSystem(rxs, t; name = :rs))
284+
285+
# Declares DSL-based model.
286+
rs_dsl = @reaction_network rs begin
287+
@parameters p[1:2] k d1 d2
288+
@species (X(t))[1:2] Y1(t) Y2(t)
289+
(p[1],p[2]), 0 --> (X[1],X[2])
290+
k, (X[1],X[2]) --> (Y1,Y2)
291+
(d1,d2), (Y1,Y2) --> 0
292+
end
293+
294+
# Checks equivalence.
295+
rs_dsl == rs_prog
296+
297+
# Creates all possible initial conditions and parameter values.
298+
u0_alts = [
299+
[X => [2.0, 5.0], Y1 => 0.2, Y2 => 0.5],
300+
[X[1] => 2.0, X[2] => 5.0, Y1 => 0.2, Y2 => 0.5],
301+
[rs_dsl.X => [2.0, 5.0], rs_dsl.Y1 => 0.2, rs_dsl.Y2 => 0.5],
302+
[rs_dsl.X[1] => 2.0, X[2] => 5.0, rs_dsl.Y1 => 0.2, rs_dsl.Y2 => 0.5],
303+
[:X => [2.0, 5.0], :Y1 => 0.2, :Y2 => 0.5]
304+
]
305+
ps_alts = [
306+
[p => [1.0, 10.0], d1 => 5.0, d2 => 4.0, k => 2.0],
307+
[p[1] => 1.0, p[2] => 10.0, d1 => 5.0, d2 => 4.0, k => 2.0],
308+
[rs_dsl.p => [1.0, 10.0], rs_dsl.d1 => 5.0, rs_dsl.d2 => 4.0, rs_dsl.k => 2.0],
309+
[rs_dsl.p[1] => 1.0, p[2] => 10.0, rs_dsl.d1 => 5.0, rs_dsl.d2 => 4.0, rs_dsl.k => 2.0],
310+
[:p => [1.0, 10.0], :d1 => 5.0, :d2 => 4.0, :k => 2.0]
311+
]
312+
313+
# Loops through all inputs and check that the correct steady state is reached
314+
# Target steady state: (X1, X2, Y1, Y2) = (p1/k, p2/k, p1/d1, p2/d2).
315+
# Technically only one model needs to be check. However, "equivalent" models in MTK can still
316+
# have slight differences, so checking for both here to be certain.
317+
for rs in [rs_prog, rs_dsl]
318+
oprob = ODEProblem(rs, u0_alts[1], (0.0, 10000.), ps_alts[1])
319+
@test_broken false # Cannot currently `remake` this problem/
320+
# for rs in [rs_prog, rs_dsl], u0 in u0_alts, p in ps_alts
321+
# oprob_remade = remake(oprob; u0, p)
322+
# sol = solve(oprob_remade, Vern7(); abstol = 1e-8, reltol = 1e-8)
323+
# @test sol[end] ≈ [0.5, 5.0, 0.2, 2.5]
324+
# end
325+
end
326+
end
327+
328+
### Other Tests ###
265329

266330
### Test Show ###
267331

@@ -751,4 +815,4 @@ end
751815
# the code might need adaptation to take the updated reaction system into account.
752816
let
753817
@test_nowarn Catalyst.reactionsystem_uptodate_check()
754-
end
818+
end

test/upstream/mtk_problem_inputs.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ let
158158
end
159159
end
160160

161-
162161
### Checks Errors On Faulty Inputs ###
163162

164163
# Checks various erroneous problem inputs, ensuring that these throw errors.

0 commit comments

Comments
 (0)