# StackOverflowError for large set of Jumps in JumpProcesses.jl with high-dimensional set of ODEs

**URL:** https://discourse.julialang.org/t/stackoverflowerror-for-large-set-of-jumps-in-jumpprocesses-jl-with-high-dimensional-set-of-odes/112969
**Category:** General Usage
**Tags:** question, numerics, differentialequation
**Created:** [April 15, 2024, 3:27pm UTC](https://discourse.julialang.org/t/stackoverflowerror-for-large-set-of-jumps-in-jumpprocesses-jl-with-high-dimensional-set-of-odes/112969 "2024-04-15T15:27:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![flmuk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/flmuk/32/204871_2.png) [@flmuk](https://discourse.julialang.org/u/flmuk)
#### Post date: [April 15, 2024, 3:27pm UTC](https://discourse.julialang.org/t/stackoverflowerror-for-large-set-of-jumps-in-jumpprocesses-jl-with-high-dimensional-set-of-odes/112969/1 "2024-04-15T15:27:57Z")

</div>

Hello,  
I have to simulate a large (n=1000) set of simple ODEs, and each ODE gets an independent realisation of Poisson input.  
When trying to set up the JumpProblem as in [the documentation](https://docs.sciml.ai/JumpProcesses/stable/tutorials/discrete_stochastic_example/#Adding-Jumps-to-a-Differential-Equation).  
However, when the number of dynamical variables is large somehow a StackOverflowError is raised when constructing the problem:

```julia
using DifferentialEquations, JumpProcesses
using StatsBase

# Define the ODE system
function large_ode_system!(du, u, p, t)
    n = length(u)
    for i in 1:n
        du[i] = - u[i] 
    end
end

# Define initial conditions and parameters
n = 1000 #n=50 works
u0 = ones(n)
tspan = (0.0, 1.0)

rate(u, p, t) = 1. #in Hz
function create_jump_affect(affected_id)

    function jump_affect_by_id!(integrator)
        refractory = sample(1:n, 10)
        if !(affected_id in refractory)
            integrator.u[affected_id] += 1.
        end
        nothing
    end
    return jump_affect_by_id!
end

# Define the problem
prob = ODEProblem(large_ode_system!, u0, tspan, p)

# Generate individual jumps
jumps = ConstantRateJump[]
for i in 1:n
    push!(jumps,ConstantRateJump(rate, create_jump_affect(i)))
end

# Create jump set
try
    jumpset_1 = JumpSet(jumps...);#gives StackOverflowError (because of recursion)?
catch error
    println(error)
finally
    nothing
end

########################################################
#alternative: create jump set 'manually':
function create_jump_set(constant_jumps::Vector{ConstantRateJump})
    # Create a JumpSet with the provided constant rate jumps and empty/nil for other jump types
    return JumpSet((), tuple(constant_jumps...), nothing, nothing)
end

jumpset_2 = create_jump_set(jumps);#works fine

# Manual handling of multiple jumps (conceptual, adjust based on actual usage)
jump_prob = JumpProblem(prob, Direct(), jumpset_2)

# Solve the problem
sol = solve(jump_prob, ImplicitEuler(), saveat=0.1) #now this gives a StackOverflowError ('Internal error: stack overflow in type inference of solve!')

```

How can one get around this? Any suggestion is more than welcome!

---

<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: [April 15, 2024, 4:16pm UTC](https://discourse.julialang.org/t/stackoverflowerror-for-large-set-of-jumps-in-jumpprocesses-jl-with-high-dimensional-set-of-odes/112969/2 "2024-04-15T16:16:41Z")

</div>

Try `DirectFW()` for large numbers of jumps.
