# JumpProblem hopping\_constants callback?

**URL:** https://discourse.julialang.org/t/jumpproblem-hopping-constants-callback/86045
**Category:** General Usage
**Tags:** sciml
**Created:** [August 19, 2022, 11:07pm UTC](https://discourse.julialang.org/t/jumpproblem-hopping-constants-callback/86045 "2022-08-19T23:07:59Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)
#### Post date: [August 19, 2022, 11:07pm UTC](https://discourse.julialang.org/t/jumpproblem-hopping-constants-callback/86045/1 "2022-08-19T23:07:59Z")

</div>

There’s a new feature in [JumpProcesses.jl](https://jump.sciml.ai/dev/) (formerly DiffEqJump.jl) where you can model spatial jump processes like in this example [post](https://tutorials.sciml.ai/html/jumps/spatial.html). One of the inputs is a `hopping_constants` matrix which controls how long a species will occupy a point in space. My question is: Can the `hopping_constants` matrix be modified in a callback?

Having different values in the `hopping_constants` matrix can mimic a boundary where the rate of diffusion differs on either side. By modifying the `hopping_constants` matrix you could simulate that boundary changing over time.

Here’s an example of some code I would like to add such a callback to:

```julia
using DifferentialEquations, Graphs, Plots, Printf

dims = (50,50)
numberOfSpecies = 1
numberOfNodes = prod(dims) # number of sites
grid = Graphs.grid(dims) # planar grid graph

#Put 25 molecules at each site less than 10 units from grid center
center = LinearIndices(dims)[dims .÷2...]
startingState = zeros(Int,numberOfSpecies, numberOfNodes)
startingState[gdistances(grid, center) .≤ 10] .= 25

#Create a Discrete problem 
tspan = (0.0, 10.0)
rates = [1.1, 1.0] 
prob = DiscreteProblem(startingState, tspan, rates)

#Encode the 2 reactions (could use Catalyst.jl to do this automatically)
reactstoch = [[1 => 1],[1 => 1]]
netstoch = [[1 => 1],[1 => -1]]
majumps = MassActionJump(rates, reactstoch, netstoch)

#Faster diffusion outside boundary
hopConstants = ones(numberOfSpecies, numberOfNodes)
hopConstants[gdistances(grid, center) .> 10] .= 2.0

alg = DirectCRDirect()
jump_prob = JumpProblem(prob,
                        alg,
                        majumps;
                        hopping_constants=hopConstants,
                        spatial_system = grid,
                        save_positions=(false, false))

sol = solve(jump_prob, SSAStepper(), saveat = tspan[2]/300)

```

Code to animate the solution

```julia
#maxium value obtained
maxu = mapreduce(maximum,max,sol.u)

anim = @animate for t in range(tspan..., 300)
    currTime = @sprintf "Time: %.2f" t
    heatmap(
        reshape(sol(t), dims),
        axis=nothing,
        clims = (0,maxu),
        framestyle = :box,
        title=currTime)
end

gif(anim, "output.gif", fps = 60)

```

![output](https://global.discourse-cdn.com/julialang/original/3X/f/3/f3cc7c7e1490eabdf38b441cf31dec1c26a1d991.gif)

---

<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: [August 19, 2022, 11:23pm UTC](https://discourse.julialang.org/t/jumpproblem-hopping-constants-callback/86045/2 "2022-08-19T23:23:34Z")

</div>

You could modify it in a callback, but we don’t have a version of `reset_aggregated_jumps!` that will communicate it has been changed and reset the internal data structures.

If you wouldn’t mind opening an issue on JumpProcesses we could look into adding that.

---

<div class="post-metadata">

### Author: ![RobertGregg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/robertgregg/32/22105_2.png) [@RobertGregg](https://discourse.julialang.org/u/RobertGregg)
#### Post date: [August 20, 2022, 12:33am UTC](https://discourse.julialang.org/t/jumpproblem-hopping-constants-callback/86045/3 "2022-08-20T00:33:48Z")

</div>

Oh for sure, I’ll open up an issue [here](https://github.com/SciML/JumpProcesses.jl/issues/258). Thanks!
