Skip to content

Commit 6313833

Browse files
committed
Conservation law error check.
1 parent 419edfc commit 6313833

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

ext/CatalystBifurcationKitExtension/bifurcation_kit_extension.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ function BK.BifurcationProblem(rs::ReactionSystem, u0_bif, ps, bif_par, args...;
1111
(u0 isa Vector{Pair{Symbol, Float64}}) && (u0 = symmap_to_varmap(rs, u0))
1212

1313
# Creates NonlinearSystem.
14-
#conservationlaw_errorcheck(rs, vcat(ps, u0))
14+
conservationlaw_errorcheck(rs, vcat(ps, u0))
1515
nsys = convert(NonlinearSystem, rs; remove_conserved=true, defaults=Dict(u0))
1616

1717
# Makes BifurcationProblem.
1818
return BK.BifurcationProblem(nsys, u0_bif, ps, bif_par, args...; plot_var=plot_var, record_from_solution=record_from_solution, jac=jac, kwargs...)
19-
end
20-
19+
end

src/miscellaneous.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# File containing code which I am currently unsure which file in which it belongs. Long-term functions here should be moved elsewhere.
2+
3+
4+
# If u0s are not given while conservation laws are present, throws an error.
5+
# Used in HomotopyContinuation and BifurcationKit extensions.
6+
function conservationlaw_errorcheck(rs, pre_varmap)
7+
vars_with_vals = Set(p[1] for p in pre_varmap)
8+
any(s -> s in vars_with_vals, species(rs)) && return
9+
isempty(conservedequations(rs)) ||
10+
error("The system has conservation laws but initial conditions were not provided for some species.")
11+
end

test/extensions/bifurcation_kit.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ rng = StableRNG(12345)
1313
# Checks that bifurcation diagrams can be computed for systems with conservation laws.
1414
# Checks that bifurcation diagrams can be computed for systems with default values.
1515
# Checks that bifurcation diagrams can be computed for systems with non-constant rate.
16+
# Checks that not providing conserved species throws and appropriate error.
1617
let
1718
# Create model
1819
extended_brusselator = @reaction_network begin
@@ -29,7 +30,7 @@ let
2930
p_start = [A => 1.0, B => 4.0, k1 => 0.1]
3031

3132
# Computes bifurcation diagram.
32-
ifurcationProblem(extended_brusselator, u0_guess, p_start, :B; plot_var=:V, u0 = [:V => 1.0])
33+
BifurcationProblem(extended_brusselator, u0_guess, p_start, :B; plot_var=:V, u0 = [:V => 1.0])
3334
p_span = (0.1, 6.0)
3435
opt_newton = NewtonPar(tol = 1e-9, max_iterations = 100)
3536
opts_br = ContinuationPar(dsmin = 0.0001, dsmax = 0.001, ds = 0.0001,
@@ -47,6 +48,9 @@ let
4748
@test length(bif_dia.γ.specialpoint) == 3 # Includes start and end point.
4849
hopf_bif_point = filter(sp -> sp.type == :hopf, bif_dia.γ.specialpoint)[1]
4950
@test isapprox(hopf_bif_point.param, 1.5, atol=1e-5)
51+
52+
# Tests that an error is thrown if information of conserved species is not fully provided.
53+
@test_throws Exception BifurcationProblem(extended_brusselator, u0_guess, p_start, :B; plot_var=:V, u0 = [:X => 1.0])
5054
end
5155

5256
# Bistable switch.
@@ -81,5 +85,3 @@ let
8185
@test bprob_BK.VF.F(u0, p) == bprob.VF.F(u0, p)
8286
end
8387
end
84-
85-
# Three-state system, tests that bifurcation diagrams works for systems with conserved quantities.

0 commit comments

Comments
 (0)