Skip to content

Commit a340ead

Browse files
committed
add new test
1 parent b9e46fb commit a340ead

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

test/network_analysis/conservation_laws.jl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ let
229229
@test all(sol2[osys.X1 + osys.X2] .== 4.0)
230230
end
231231

232+
### Problem `remake`ing and Updating Tests ###
233+
232234
# Tests system problem updating when conservation laws are eliminated.
233235
# Checks that the correct values are used after the conservation law species are updated.
234236
# Here is an issue related to the broken tests: https://github.com/SciML/Catalyst.jl/issues/952
@@ -278,6 +280,82 @@ let
278280
@test integrator.ps[k2] == 0.6
279281
end
280282

283+
# Goes through a chain of updating of conservation law constants/species, checking that
284+
# the values of relevant quantitites are correct after each step.
285+
# Generally, if `Γ` has not been explicitly updated, it will be updated to accomodate new species
286+
# values. If it has been explicitly udpated, the corresponding elimianted quantity will have its
287+
# vaue updated to accomodate new Γ/species values (also, the elimianted species's value can not longer be changed).
288+
let
289+
# Prepares an `ODEProblem` with conserved quantities eliminated. Computes the conservation equation.
290+
rn = @reaction_network begin
291+
(k1,k2), 2X1 <--> X2
292+
(k3,k4), X1 + X2 <--> X3
293+
end
294+
@unpack X1, X2, X3 = rn
295+
u0 = [X1 => 1.0, X2 => 1.0, X3 => 1.0]
296+
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)
298+
conserved_quantity = conservationlaw_constants(rn)[1].rhs
299+
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
356+
end
357+
end
358+
281359
### Other Tests ###
282360

283361
# Checks that `JumpSystem`s with conservation laws cannot be generated.

0 commit comments

Comments
 (0)