# Gillespie simulations: don't deepcopy(JumpProblem)

**URL:** https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073
**Category:** Modelling & Simulations
**Tags:** diffeq
**Created:** [March 18, 2022, 9:37am UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073 "2022-03-18T09:37:30Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)
#### Post date: [March 18, 2022, 9:37am UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/1 "2022-03-18T09:37:30Z")

</div>

Example from the docs. It seems that deepcopying a JumpProblem object causes each copy to be solved with the same seed. Is this known?

```julia
using DifferentialEquations
using Catalyst
sir_model = @reaction_network begin
    β, S + I --> 2I
    ν, I --> R
end β ν

p = (0.1 / 1000, 0.01)
u₀ = [999, 1, 0]
tspan = (0.0, 250.0)
prob = DiscreteProblem(sir_model, u₀, tspan, p)
jump_prob = JumpProblem(sir_model, prob, Direct())

for _ in 1:5
    sol = solve(jump_prob, SSAStepper())
    print(sol.u[end])
end
# [0, 194, 806][0, 175, 825][1, 174, 825][0, 190, 810][0, 214, 786]
for _ in 1:5
    sol = solve(deepcopy(jump_prob), SSAStepper())
    print(sol.u[end])
end
# [1, 196, 803][1, 196, 803][1, 196, 803][1, 196, 803][1, 196, 803]

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [March 18, 2022, 1:23pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/2 "2022-03-18T13:23:43Z")

</div>

This is probably because pre Julia v1.7 releases use an RNG object per jump problem since the global RNG was not thread-safe.

@isaacsas is this fixed in later versions by using the new global Julia RNG?

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [March 18, 2022, 1:46pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/3 "2022-03-18T13:46:45Z")

</div>

> [@levasco](#):
>
> ```julia
> for _ in 1:5
> sol = solve(deepcopy(jump_prob), SSAStepper())
> print(sol.u[end])
> end
> 
> ```

Yeah, the second loop gives different results on 1.8.

@levasco what Julia version are you on?

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [March 18, 2022, 1:49pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/4 "2022-03-18T13:49:50Z")

</div>

Also, why do you want to deepcopy the `JumpProblem`? That is potentially a very expensive operation since it will deepcopy all the underlying SSA data structures and data too.

---

<div class="post-metadata">

### Author: ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)
#### Post date: [March 18, 2022, 2:06pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/5 "2022-03-18T14:06:14Z")

</div>

Thanks for the reply. I’m on 1.7.0 indeed.

As for the why, often out of habit/precaution as I’ve been surprised by Julia’s pass by reference in the past. But sometimes it’s actually needed e.g. if I update/store quantities in `prob.p` during integration using callbacks, I can’t reuse the same object for a new simulation.

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [March 18, 2022, 2:28pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/6 "2022-03-18T14:28:04Z")

</div>

Have you looked at [Jump Problems · DifferentialEquations.jl](https://diffeq.sciml.ai/latest/types/jump_types/#Remaking-JumpProblems)? Generally I’d suggest using `remake` over deepcopying yourself if possible. (And if it doesn’t work right please do let us know / open a DiffEqJump issue!)

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [March 18, 2022, 2:30pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/7 "2022-03-18T14:30:50Z")

</div>

Also, if you are changing parameters during a simulation, make sure to look at [this](https://diffeq.sciml.ai/latest/tutorials/discrete_stochastic_example/#*3.-How-do-I-use-callbacks-with-ConstantRateJump-or-MassActionJump-systems?*) as you need to call a special function to ensure `MassActionJump`s, which Catalyst generates, are updated appropriately.

Basically, you need to always call `reset_aggregated_jumps!(integrator)` after changing `p` during a simulation to ensure the SSAs update for the changes. Otherwise they may have internal data structures that are no longer valid. Changing `p` between simulations you can just use `remake`.

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [March 18, 2022, 2:32pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/8 "2022-03-18T14:32:28Z")

</div>

It is strange you are seeing issues on 1.7 as that should have the new random number generator that is on 1.8 too. What DiffEqJump version are you using?

---

<div class="post-metadata">

### Author: ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)
#### Post date: [March 18, 2022, 2:52pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/9 "2022-03-18T14:52:29Z")

</div>

Just learned about `remake()`, seems like a much better way indeed. And thanks for the heads up regarding `reset_aggregated_jumps!()`, I had only used callbacks with classical ODEs so far but it was bound to bite me at some point. Version is 8.0, updating now to see if it still happens.

EDIT: Updating to 8.3 fixed it (didn’t update Julia, just the pkg)

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [March 18, 2022, 4:10pm UTC](https://discourse.julialang.org/t/gillespie-simulations-dont-deepcopy-jumpproblem/78073/10 "2022-03-18T16:10:04Z")

</div>

Perfect!
