# Is it possible to simulate SDDE with modeling toolkit?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-simulate-sdde-with-modeling-toolkit/125420
**Category:** Modelling & Simulations
**Tags:** modelingtoolkit
**Created:** [January 31, 2025, 8:09am UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-sdde-with-modeling-toolkit/125420 "2025-01-31T08:09:50Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![NinaOmejc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninaomejc/32/214448_2.png) [@NinaOmejc](https://discourse.julialang.org/u/NinaOmejc)
#### Post date: [January 31, 2025, 8:09am UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-sdde-with-modeling-toolkit/125420/1 "2025-01-31T08:09:50Z")

</div>

Hi,  
I’ve come across Chris Rackauckas post that it is possible to simulate DDEs using Modeling Toolkit ([ModelingToolkit: (Transport) delay? Example: Inflow to one water reservoir is a delayed outflow from another - #7 by filip.cerny](https://discourse.julialang.org/t/modelingtoolkit-transport-delay-example-inflow-to-one-water-reservoir-is-a-delayed-outflow-from-another/122654/7)).

Then I tried to simulate SDDE, and it worked, but the solution looked different from the solution when directly using the DifferentialEquations module. Have I made some mistake or is SDDEProblem not yet compatible with ModelingToolkit?  
Thank you! I have attached the simplified code below.

Simulating with MTK:

```julia
using DifferentialEquations
using StochasticDelayDiffEq
using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using Random 
using Plots

Random.seed!(1)

@parameters a b τ
@variables x₁(t) x₂(..)
@brownian w₁ 

eqs = [D(x₁) ~ a * x₁ - x₁ * x₂(t-τ) + 0.2*w₁, 
       D(x₂(t)) ~ b * x₂(t) + x₁ * x₂(t)] 

# Parameters and setup
params = [a => 1., b => -3.0, τ => 0.01]
inits = [x₁ => 1.0, x₂(t) => 1.0]
tspan = (0.0, 20.0)

sys = structural_simplify(System(eqs, t; name=Symbol("LotkaVolterra")))
prob = SDDEProblem(sys, inits, tspan, params; constant_lags=[τ]) 
sol = solve(prob, SRIW1())
plot(sol, xlims=(0, 20), ylims=(0, 10))

```

Output:

 ![Screenshot_3](https://global.discourse-cdn.com/julialang/original/3X/c/1/c1c9c620238887bafaee8c42b2929d3efc1ed6f6.png)

Simulating with differentialEquations:

```julia
using DifferentialEquations
using Plots
using StochasticDelayDiffEq
using StochasticDiffEq
using Random 

Random.seed!(1)

function eq!(du, u, h, p, t)
    a, b, τ = p
    hu2 = h(p, t - τ)[2]
    du[1] = a * u[1] - u[1] * hu2
    du[2] = b * u[2] + u[1] * u[2] # + h(p, t - τ)[1] # -3 * Vex + Ve * Vex
end

function noise!(du, u, h, p, t)
    du1 = 0.2
    du2 = 0.
    du.= [du1, du2]
end

# Parameters and setup
a, b, τ = 1.0, -3.0, 0.01
params = [a, b, τ]
inits = [1.0, 1.0]
tspan = (0.0, 20.0)
h(p, t) = inits

prob = SDDEProblem(eq!, noise!, inits, h, tspan, params; constant_lags=τ)
sol = solve(prob, SRIW1())
plot(sol, xlims=(0, 20), ylims=(0, 10))

```

Output:

 ![Screenshot_1](https://global.discourse-cdn.com/julialang/original/3X/8/5/8524ac315e976017733a89a37c266779cb679a3c.png)

---

<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: [January 31, 2025, 9:59am UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-sdde-with-modeling-toolkit/125420/2 "2025-01-31T09:59:48Z")

</div>

@cryptic.ax can you double check the codegen here?

---

<div class="post-metadata">

### Author: ![cryptic.ax](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cryptic.ax/32/220540_2.png) [@cryptic.ax](https://discourse.julialang.org/u/cryptic.ax)
#### Post date: [January 31, 2025, 11:22am UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-sdde-with-modeling-toolkit/125420/3 "2025-01-31T11:22:14Z")

</div>

Codegen all looks fine. I’ll try and dig further

---

<div class="post-metadata">

### Author: ![NinaOmejc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ninaomejc/32/214448_2.png) [@NinaOmejc](https://discourse.julialang.org/u/NinaOmejc)
#### Post date: [February 6, 2025, 1:31pm UTC](https://discourse.julialang.org/t/is-it-possible-to-simulate-sdde-with-modeling-toolkit/125420/4 "2025-02-06T13:31:24Z")

</div>

Thank you for looking into it! I would be happy to know the results of the search.
