# SDE \`EnsembleProblem\` unhappy with \`trajectories\>=2\`?

**URL:** https://discourse.julialang.org/t/sde-ensembleproblem-unhappy-with-trajectories-2/82098
**Category:** Modelling & Simulations
**Tags:** sde, differentialequation
**Created:** [June 2, 2022, 1:20am UTC](https://discourse.julialang.org/t/sde-ensembleproblem-unhappy-with-trajectories-2/82098 "2022-06-02T01:20:20Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)
#### Post date: [June 2, 2022, 1:20am UTC](https://discourse.julialang.org/t/sde-ensembleproblem-unhappy-with-trajectories-2/82098/1 "2022-06-02T01:20:21Z")

</div>

The title is a bit generic, but I have trouble seeing a more specific version of my SDE problem. Consider the following problem:

```julia
using DifferentialEquations
using LinearAlgebra

function make_problem_2()
    m1 = rand(Complex{Float64}, 2, 2)
    m1 ./= norm(m1)
    m2 = rand(Complex{Float64}, 2, 2)
    m2 ./= norm(m2)

    u0 = rand(Complex{Float64}, 2, 2)

    W = WienerProcess(0.0,0.0)
    tspan = (0,10.)

    f = let
        m1 = m1
        f(du, u, p, t) = du .= m1 * u
    end
    g = let
        m2 = m2
        g(du, u, p, t) = du .= m2 * u
    end
    SDEProblem(f,g,u0,tspan,noise=W)
end
##
for i in 1:100
    prob = make_problem_2()
    eprob = EnsembleProblem(prob)
    @time esol = solve(eprob,
        #ImplicitRKMil(autodiff=false),
        #saveat=0.01,
        #dtmax=0.01,
        trajectories=2)
    @assert esol[1].retcode == :Success
end

```

If I set `trajectories=1` it works fine. If I set it to `>=2` then it breaks with

```julia
┌ Warning: dt <= dtmin. Aborting. There is either an error in your model specification or the true solution is unstable.
└ @ SciMLBase ~/.julia/packages/SciMLBase/YVSzo/src/integrator_interface.jl:345
  0.026924 seconds (67.83 k allocations: 4.377 MiB)
┌ Warning: Instability detected. Aborting
└ @ SciMLBase ~/.julia/packages/SciMLBase/YVSzo/src/integrator_interface.jl:351
┌ Warning: Instability detected. Aborting
└ @ SciMLBase ~/.julia/packages/SciMLBase/YVSzo/src/integrator_interface.jl:351
  0.007925 seconds (26.74 k allocations: 1.684 MiB)
ERROR: AssertionError: (esol[1]).retcode == :Success

```

Why is the instability showing up only with more than one trajectories? Seen both in 1.7.2 and in `1.9.0-DEV.700 Commit 53338ca3424 (2022-06-01 14:43 UTC)`.

`] st` gives

```julia
  [0c46a032] DifferentialEquations v7.1.0
  [f6369f11] ForwardDiff v0.10.30
  [91a5bcdd] Plots v1.29.0
  [6e0679c1] QuantumOptics v1.0.4

```

---

<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: [June 4, 2022, 11:19am UTC](https://discourse.julialang.org/t/sde-ensembleproblem-unhappy-with-trajectories-2/82098/2 "2022-06-04T11:19:17Z")

</div>

You’re reusing the same noise problem with both trajectories, so it’s not thread-safe. You want a noise per problem.

---

<div class="post-metadata">

### Author: ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)
#### Post date: [June 4, 2022, 5:01pm UTC](https://discourse.julialang.org/t/sde-ensembleproblem-unhappy-with-trajectories-2/82098/3 "2022-06-04T17:01:11Z")

</div>

Thanks! Could you provide the idiomatic way to create a new noise per problem? I have a few silly ways to do it, but I would like to make a PR to add the correct one to the documentation.

Currently the documentation makes you think this is automatically done because `deepcopy` is supposed to be used by default due to `safetycopy=true`: [Parallel Ensemble Simulations · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/features/ensemble/#Building-a-Problem)

Similarly, the tutorial makes it look as if nothing special is needed to copy a new noise instance: [Stochastic Differential Equations · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/tutorials/sde_example/#Ensemble-Simulations)

---

<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: [June 4, 2022, 5:56pm UTC](https://discourse.julialang.org/t/sde-ensembleproblem-unhappy-with-trajectories-2/82098/4 "2022-06-04T17:56:14Z")

</div>

> [@Krastanov](#):
>
> Thanks! Could you provide the idiomatic way to create a new noise per problem? I have a few silly ways to do it, but I would like to make a PR to add the correct one to the documentation

Make it as part of the prob func and add noise=.to the created prob there

---

<div class="post-metadata">

### Author: ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)
#### Post date: [June 7, 2022, 4:21am UTC](https://discourse.julialang.org/t/sde-ensembleproblem-unhappy-with-trajectories-2/82098/5 "2022-06-07T04:21:22Z")

</div>

Minor documentation update posted as a PR here [EnsembleProblem safetycopy clarification by Krastanov · Pull Request #561 · SciML/DiffEqDocs.jl · GitHub](https://github.com/SciML/DiffEqDocs.jl/pull/561)
