Sure, Here you go. The following example programmatically generate a vector of species using same reactants A and B.
using Catalyst,OrdinaryDiffEq,Plots, JumpProcesses
function generate_creation(n)
# Creates a vector `X` with n species.
@species X(t)[1:n]
@species A(t),B(t)
@parameters k
# @parameters α,K,m,k
rxs = [
[Reaction(k, [A,B], [X[i]]) for i in 1:n];
]
# Assembly and return a complete `ReactionSystem`
@named lp_n = ReactionSystem(rxs, t)
return complete(lp_n)
end
function condition1(u, t, integrator) # Event when condition(u,t,integrator) == 0
u[1]-100
end
function condition2(u, t, integrator) # Event when condition(u,t,integrator) == 0
u[2]-100
end
function affect1!(integrator)
integrator.u[1] = 1000
reset_aggregated_jumps!(integrator)
end
function affect2!(integrator)
integrator.u[2] = 1000
reset_aggregated_jumps!(integrator)
end
begin
cb1 = ContinuousCallback(condition1, affect1!)
cb2 = ContinuousCallback(condition2, affect2!)
cbs = CallbackSet(cb1,cb2)
end
N=3
params = (:k =>0.1)
X0 = fill(0.0, N)
X0[1] = n * 1.0
prob1=generate_creation(N)
u_0 = [prob1.X => X0,prob1.A => 1000, prob1.B => 1000]
odeprob1=ODEProblem(prob1, u_0, (0.0, 0.5), params)
sol1=solve(odeprob1)
plot(sol1)
Final plot commands produced expected result. Where X1,X2 and X3 increases untill they saturate at same value as A and B goes to zero.
Adding Callback and solving gives us:
sol2=solve(odeprob1;callback=cbs)
plot(sol2)
So callbacks are working fine that lets the system generate X1,X2 and X3 well.
Now on to the Jump Process stochastic Equivalent:
begin
jumpinputs = JumpInputs(prob1, u_0, (0,0.05), params)
jumpprob = JumpProblem(jumpinputs)
# now, let's solve and plot the jump process:
soljump = solve(jumpprob,callback=cbs)
plot(soljump)
end
As you can see, it shows the right behaviour but the callbacks are not working. My instinct tells me that the condition u==0
is not working out at any time. But since, this is a stochastic simulation, the values are jumping by one. It should work.
P.S. I have adapted these examples from the SciMl documentation. Except the callback for jumpprocess. The documentation seems to be lacking there.