Skip to content

example in docs using @reaction_network #27

@gszep

Description

@gszep

Would be good to show users that its possible to use this library with DiffEqBiological

using DiffEqBiological,BifurcationKit
using Setfield,LinearAlgebra,Plots

system = @reaction_network begin
     k₁,		Y  2X
     2k₂,		2X  X + Y
     k₃,		X + Y  Y
     k₄,		X  0
end k₁ k₂ k₃ k₄

# get rate function and jacobian, for use in continuation
F! = oderhsfun(system)
function F(u::Vector{T},p::NamedTuple) where T<:Number
	f = similar(u)
	F!(f,u,[ p[key] for (key, value)  paramsmap(system) ],Inf)
	return f
end

∂Fu! = jacfun(system)
function ∂Fu(u::Vector{T},p::NamedTuple) where T<:Number
	J = zeros(T,length(u),length(u))
	∂Fu!(J,u,[ p[key] for (key, value)  paramsmap(system) ],Inf)
	return J
end

# define the parameter direction that you want to explore
pRange = range(0,12,length=200)
parameter_name = (@lens _.k₁)
p = (k₁=0.0, k₂=1.0, k₃=1.0, k₄=1.5)

# options for branch continuation along parameter direction
options = ContinuationPar( maxSteps = 5000,
	dsmax=10*step(pRange), dsmin=step(pRange), ds=step(pRange),
	pMin = minimum(pRange), pMax=maximum(pRange),
	newtonOptions = NewtonPar(tol = 1e-8),
	saveEigenvectors = false
)

# options for detecting multiple branches
deflation = DeflationOperator(2.0, dot, 1.0, [ zeros(length(species(system))) ])
termination_condition(x, f, J, residual, iteration, niter, options; kwargs...) = residual <1e3
root_search(x,p,id) = x .+ rand(length(x))

##################################### main continuation method
@assert( det(∂Fu(randn(length(species(system))),p))  0, "species must be linearly independent")
branches, = continuation( F, ∂Fu, p, parameter_name, options, deflation; showplot=false,
	callbackN = termination_condition, perturbSolution = root_search )

#################################### display results
plot(size=(500,500),ylabel="steady states", xlabel="parameter $(first(typeof(parameter_name).parameters))")
plot!(branches,lw=3,label="")

Screenshot from 2020-11-05 10-28-51

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions