Skip to content

Commit 5399592

Browse files
committed
repeat test for SDEProblem as well
1 parent cd36fdd commit 5399592

File tree

1 file changed

+66
-60
lines changed

1 file changed

+66
-60
lines changed

test/network_analysis/conservation_laws.jl

Lines changed: 66 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,10 @@ let
244244
Reaction(k2, [X2], [X1])
245245
]
246246
@named rs = ReactionSystem(rxs, t)
247-
osys = convert(ODESystem, complete(rs); remove_conserved = true)
248-
osys = complete(osys)
247+
osys = complete(convert(ODESystem, complete(rs); remove_conserved = true))
249248
@unpack Γ = osys
250249

251-
# Creates an `ODEProblem`.
250+
# Creates the various problem types.
252251
u0 = [X1 => 1.0, X2 => 2.0]
253252
ps = [k1 => 0.1, k2 => 0.2]
254253
oprob = ODEProblem(osys, u0, (0.0, 1.0), ps)
@@ -294,65 +293,72 @@ let
294293
@unpack X1, X2, X3 = rn
295294
u0 = [X1 => 1.0, X2 => 1.0, X3 => 1.0]
296295
ps = [:k1 => 0.1, :k2 => 0.2, :k3 => 0.3, :k4 => 0.4]
297-
prob_old = ODEProblem(rn, u0, 1.0, ps; remove_conserved = true)
298296
conserved_quantity = conservationlaw_constants(rn)[1].rhs
299297

300-
# For a couple of iterations, updates the problem, ensuring that when a species is updated:
301-
# - Only that species and the conservation constant have their values updated.
302-
# The `≈` is because sometimes the computed values will not be fully exact.
303-
for _ = 1:3
304-
# Updates X2, checks the values of all species and Γ, then sets which is the old problem.
305-
X2_new = rand(rng, 1.0:10.0)
306-
prob_new = remake(prob_old; u0 = [:X2 => X2_new])
307-
@test prob_old[:X1] prob_new[:X1]
308-
@test X2_new prob_new[:X2]
309-
@test prob_old[:X3] prob_new[:X3]
310-
@test substitute(conserved_quantity, Dict([X1 => prob_old[X1], X2 => X2_new, X3 => prob_old[X3]])) prob_new.ps[][1]
311-
prob_old = prob_new
312-
313-
# Updates X3, checks the values of all species and Γ, then sets which is the old problem.
314-
X3_new = rand(rng, 1.0:10.0)
315-
prob_new = remake(prob_old; u0 = [:X3 => X3_new])
316-
@test prob_old[:X1] prob_new[:X1]
317-
@test prob_old[:X2] prob_new[:X2]
318-
@test X3_new prob_new[:X3]
319-
@test substitute(conserved_quantity, Dict([X1 => prob_old[X1], X2 => prob_old[X2], X3 => X3_new])) prob_new.ps[][1]
320-
prob_old = prob_new
321-
end
322-
323-
# Similarly, but now also updates the conservation constant. Here, once Γ has been updated:
324-
# - The conservation law constant will be kept fixed, and secondary updates are made to the
325-
# eliminated species.
326-
# Assumes that X3 is the eliminated species.
327-
# The random Γ is ensured to be large enough not to generate negative values in the eliminated species.
328-
for _ in 1:3
329-
# Updates Γ, checks the values of all species and Γ, then sets which is the old problem.
330-
Γ_new = substitute(conserved_quantity, Dict([X1 => prob_old[X1], X2 => prob_old[X2], X3 => 0])) + rand(rng, 0.0:5.0)
331-
prob_new = remake(prob_old; p = [ => [Γ_new]], warn_initialize_determined = false)
332-
@test prob_old[:X1] prob_new[:X1]
333-
@test prob_old[:X2] prob_new[:X2]
334-
@test Γ_new prob_new.ps[][1]
335-
@test substitute(conserved_quantity, Dict([X1 => prob_old[X1], X2 => prob_old[X2], X3 => prob_new[X3]])) prob_new.ps[][1]
336-
prob_old = prob_new
337-
338-
# Updates X1 (non-eliminated species), checks the values of all species and Γ, then sets which is the old problem.
339-
# Note that now, `X3` will have its value modified (not and `Γ` remains unchanged).
340-
X1_new = rand(rng, 1.0:10.0)
341-
prob_new = remake(prob_old; u0 = [:X1 => X1_new])
342-
@test X1_new prob_new[:X1]
343-
@test prob_old[:X2] prob_new[:X2]
344-
@test prob_old.ps[][1] prob_new.ps[][1]
345-
@test substitute(conserved_quantity, Dict([X1 => X1_new, X2 => prob_old[X2], X3 => prob_new[X3]])) prob_new.ps[][1]
346-
prob_old = prob_new
347-
348-
# Updates X3 (the eliminated species). Right now, this will have no effect on `X3` (or the system).
349-
X3_new = rand(rng, 1.0:10.0)
350-
prob_new = remake(prob_old; u0 = [:X3 => X3_new], warn_initialize_determined = false)
351-
@test prob_old[:X1] prob_new[:X1]
352-
@test prob_old[:X2] prob_new[:X2]
353-
@test prob_old[:X3] prob_new[:X3]
354-
@test prob_old.ps[][1] prob_new.ps[][1]
355-
prob_old = prob_new
298+
# Loops through the tests for different problem types.
299+
oprob_old = ODEProblem(rn, u0, 1.0, ps; remove_conserved = true)
300+
sprob_old = SDEProblem(rn, u0, 1.0, ps; remove_conserved = true)
301+
302+
for prob in [oprob_old, sprob_old]
303+
prob_old = prob
304+
305+
# For a couple of iterations, updates the problem, ensuring that when a species is updated:
306+
# - Only that species and the conservation constant have their values updated.
307+
# The `≈` is because sometimes the computed values will not be fully exact.
308+
for _ = 1:3
309+
# Updates X2, checks the values of all species and Γ, then sets which is the old problem.
310+
X2_new = rand(rng, 1.0:10.0)
311+
prob_new = remake(prob_old; u0 = [:X2 => X2_new])
312+
@test prob_old[:X1] prob_new[:X1]
313+
@test X2_new prob_new[:X2]
314+
@test prob_old[:X3] prob_new[:X3]
315+
@test substitute(conserved_quantity, Dict([X1 => prob_old[X1], X2 => X2_new, X3 => prob_old[X3]])) prob_new.ps[][1]
316+
prob_old = prob_new
317+
318+
# Updates X3, checks the values of all species and Γ, then sets which is the old problem.
319+
X3_new = rand(rng, 1.0:10.0)
320+
prob_new = remake(prob_old; u0 = [:X3 => X3_new])
321+
@test prob_old[:X1] prob_new[:X1]
322+
@test prob_old[:X2] prob_new[:X2]
323+
@test X3_new prob_new[:X3]
324+
@test substitute(conserved_quantity, Dict([X1 => prob_old[X1], X2 => prob_old[X2], X3 => X3_new])) prob_new.ps[][1]
325+
prob_old = prob_new
326+
end
327+
328+
# Similarly, but now also updates the conservation constant. Here, once Γ has been updated:
329+
# - The conservation law constant will be kept fixed, and secondary updates are made to the
330+
# eliminated species.
331+
# Assumes that X3 is the eliminated species.
332+
# The random Γ is ensured to be large enough not to generate negative values in the eliminated species.
333+
for _ in 1:3
334+
# Updates Γ, checks the values of all species and Γ, then sets which is the old problem.
335+
Γ_new = substitute(conserved_quantity, Dict([X1 => prob_old[X1], X2 => prob_old[X2], X3 => 0])) + rand(rng, 0.0:5.0)
336+
prob_new = remake(prob_old; p = [ => [Γ_new]], warn_initialize_determined = false)
337+
@test prob_old[:X1] prob_new[:X1]
338+
@test prob_old[:X2] prob_new[:X2]
339+
@test Γ_new prob_new.ps[][1]
340+
@test substitute(conserved_quantity, Dict([X1 => prob_old[X1], X2 => prob_old[X2], X3 => prob_new[X3]])) prob_new.ps[][1]
341+
prob_old = prob_new
342+
343+
# Updates X1 (non-eliminated species), checks the values of all species and Γ, then sets which is the old problem.
344+
# Note that now, `X3` will have its value modified (not and `Γ` remains unchanged).
345+
X1_new = rand(rng, 1.0:10.0)
346+
prob_new = remake(prob_old; u0 = [:X1 => X1_new])
347+
@test X1_new prob_new[:X1]
348+
@test prob_old[:X2] prob_new[:X2]
349+
@test prob_old.ps[][1] prob_new.ps[][1]
350+
@test substitute(conserved_quantity, Dict([X1 => X1_new, X2 => prob_old[X2], X3 => prob_new[X3]])) prob_new.ps[][1]
351+
prob_old = prob_new
352+
353+
# Updates X3 (the eliminated species). Right now, this will have no effect on `X3` (or the system).
354+
X3_new = rand(rng, 1.0:10.0)
355+
prob_new = remake(prob_old; u0 = [:X3 => X3_new], warn_initialize_determined = false)
356+
@test prob_old[:X1] prob_new[:X1]
357+
@test prob_old[:X2] prob_new[:X2]
358+
@test prob_old[:X3] prob_new[:X3]
359+
@test prob_old.ps[][1] prob_new.ps[][1]
360+
prob_old = prob_new
361+
end
356362
end
357363
end
358364

0 commit comments

Comments
 (0)