# Stiff SDE and \`DiffEq\`

**URL:** <https://discourse.julialang.org/t/stiff-sde-and-diffeq/26851>\
**Category:** Numerics\
**Tags:** diffeq, sde\
**Created:** [July 26, 2019, 3:53pm UTC](https://discourse.julialang.org/t/stiff-sde-and-diffeq/26851 "2019-07-26T15:53:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [July 26, 2019, 3:53pm UTC](https://discourse.julialang.org/t/stiff-sde-and-diffeq/26851/1 "2019-07-26T15:53:50Z")

</div>

I’m trying to integrate an SDE with constant, additive, diagonal noise and a “stiff” drift component (the ODE component comes from a singularly perturbed DAE system…). I need to be able to integrate indefinitely (I’m interested in when the particle reaches some faraway region), so I think that adaptive time-stepping methods won’t work for this problem (but I may be wrong).

I’ve looked through the [SDE solvers page](https://docs.juliadiffeq.org/latest/solvers/sde_solve.html) and was thinking of `TangXiaoSROCK2` but see that it’s under development. So, I’m wondering what integrators are recommended for problems like this (ideally any explicit integrators). Since the diffusion term is constant, Milstein methods are identical to Euler-Maruyama (with strong order 1 convergence), and I’m wondering if I can just use a “better” (explicit) ODE method for the drift term (to deal with the stiffness) while handling the diffusion in the same way as Euler-Maruyama.

---

<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:** [July 28, 2019, 2:01am UTC](https://discourse.julialang.org/t/stiff-sde-and-diffeq/26851/2 "2019-07-28T02:01:44Z")

</div>

> [@jacob-roth](#):
>
> I need to be able to integrate indefinitely (I’m interested in when the particle reaches some faraway region), so I think that adaptive time-stepping methods won’t work for this problem (but I may be wrong).

This is precisely the case where adaptive methods perform better. The transition can be a big constraint to the time steps, so you want to adapt during transient behavior. Also, any implicit method needs adaptivity since Newton iterations can diverge. Anyways, any method can be set to fixed timesteps with `adaptive=false`.

> [@jacob-roth](#):
>
> I’m trying to integrate an SDE with constant, additive, diagonal noise and a “stiff” drift component

I would give SKenCarp a try. There’s something I want to try with its error estimator so give it a try and if it’s not doing well I might want to play with it.

> [@jacob-roth](#):
>
> I’m wondering if I can just use a “better” (explicit) ODE method for the drift term (to deal with the stiffness) while handling the diffusion in the same way as Euler-Maruyama.

For this style, SROCK2 might be the best bet? Those methods are brand new so we haven’t benchmarked between them yet, so I wouldn’t be so caught up in `TangXiaoSROCK2` (there seems to be an issue with the tableau in the paper, IIRC it doesn’t satisfy the order conditions).

And if you need to brute force it, `SOSRA` should get the job done, but not efficiently. Start giving these a try and let us know if you need more help.

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [July 31, 2019, 2:20am UTC](https://discourse.julialang.org/t/stiff-sde-and-diffeq/26851/3 "2019-07-31T02:20:17Z")

</div>

> [@ChrisRackauckas](#):
>
> SKenCarp

Thanks @ChrisRackauckas! By “indefinitely”, I mean that I can’t specify an end time in the problem, as I don’t know how long my simulation will take to reach this region. Am I able to handle such cases with `SKenCarp`? I tried looking through the documentation, but it appears that an end time is required for each `SDEProblem`.

---

<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:** [July 31, 2019, 2:43pm UTC](https://discourse.julialang.org/t/stiff-sde-and-diffeq/26851/4 "2019-07-31T14:43:32Z")

</div>

> [@jacob-roth](#):
>
> Thanks @ChrisRackauckas! By “indefinitely”, I mean that I can’t specify an end time in the problem, as I don’t know how long my simulation will take to reach this region. Am I able to handle such cases with `SKenCarp` ? I tried looking through the documentation, but it appears that an end time is required for each `SDEProblem` .

You’re looking for the `terminate!` command in the callbacks. An example is shown here:

[http://docs.juliadiffeq.org/latest/features/callback\_functions.html#Example-2:-Terminating-an-Integration-1](http://docs.juliadiffeq.org/latest/features/callback_functions.html#Example-2:-Terminating-an-Integration-1)

---

<div class="post-metadata">

**Author:** ![jacob-roth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacob-roth/32/1862_2.png) [@jacob-roth](https://discourse.julialang.org/u/jacob-roth)\
**Post date:** [July 31, 2019, 6:15pm UTC](https://discourse.julialang.org/t/stiff-sde-and-diffeq/26851/5 "2019-07-31T18:15:01Z")

</div>

Thanks, though this still requires an end time, right? My issue is that _a priori_ I don’t know how long the simulation should run. Is there an effective way to set the end time in `tspan` as something like infinity? I feel that perhaps I’m missing something…

---

<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:** [July 31, 2019, 7:20pm UTC](https://discourse.julialang.org/t/stiff-sde-and-diffeq/26851/6 "2019-07-31T19:20:59Z")

</div>

Yes, just use `tspan = (0.0,Inf)` or something like that.
