# StochasticDiffEq with Acausal modeling

**URL:** https://discourse.julialang.org/t/stochasticdiffeq-with-acausal-modeling/121998
**Category:** Modelling & Simulations
**Created:** [October 30, 2024, 3:30pm UTC](https://discourse.julialang.org/t/stochasticdiffeq-with-acausal-modeling/121998 "2024-10-30T15:30:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)
#### Post date: [October 30, 2024, 3:30pm UTC](https://discourse.julialang.org/t/stochasticdiffeq-with-acausal-modeling/121998/1 "2024-10-30T15:30:25Z")

</div>

Hello,  
I am trying to have a `SDESystem` instead of `ODESystem` for my acausal framework.  
The following block of code fails:

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

@connector function myconnect(;name) 
    vars = @variables begin
    x(t), [input = true]
    y(t), [input = true]
    z(t), [input = true]
    end
    noiseeqs = [

    ]
    SDESystem(Equation[], noiseeqs,t, vars, [];name=name)
end

function source(;name,x_,y_,z_)
    @named port = myconnect()
    para = @parameters begin

    end
    vars = @variables begin
        x(t)
        y(t)
        z(t)
     end

    eqs = [
        D(x) ~ 0
        D(y) ~ 0
        D(z) ~ 0
        port.x ~ x_ # Outflow is negative
        port.y ~ y_
        port.z ~ z_
    ]
    noiseeqs = [
        0.1*x,
        0.1*y,
        0.1*z,
        0,
        0,
        0    
    ]
    compose(SDESystem(eqs,noiseeqs ,t, vars, para;name),port)
end

function mycomp(;name)
    @named _in = myconnect()
    @named _out = myconnect()
    
    para = @parameters begin
        
    end

    vars = @variables begin
        x(t)
        y(t)
        z(t)
    end
    eqs = [
        D(x) ~ x*cos(-t) 
        _out.x ~ x
        D(y) ~ y*sin(-t)
        _out.y ~ y
        D(z) ~ Base.ifelse(t>10,z*(cos(t)),0)
        _out.z ~ z
    ]
    noiseeqs = [
        0.1*x,
        0,
        0.1*y,
        0,
        0.1*z,
        0
    ]
    compose(SDESystem(eqs,noiseeqs,t, vars, para;name),_in,_out)
end

function sink(;name)
    @named port = myconnect()
    para = @parameters begin

    end
    vars = @variables begin
        x(t)
        y(t)
        z(t)
     end    

    eqs = [
        port.x ~ x # Outflow is negative
        port.y ~ y
        port.z ~ z
    ]
    noiseeqs = [
        0,
        0,
        0
    ]
    compose(SDESystem(eqs,noiseeqs ,t, vars, para;name),port)
end

@named src = source(x_ = 1,y_=2,z_=3)
@named comp = mycomp()
@named snk = sink()

eqs = [
    connect(src.port,comp._in)
    connect(comp._out,snk.port)
]

systems = [src,comp,snk]
@named model = SDESystem(eqs,t, systems=systems)

```

with the error:

```julia
ERROR: MethodError: no method matching SDESystem(::Vector{Equation}, ::Num; systems::Vector{SDESystem}, name::Symbol)
The type `SDESystem` exists, but no method is defined for this combination of argument types when trying to construct it.

```

This is my first time trying acausal system with SDE so I am sure many things are wrong here.  
I am also not sure if an SDE can be made using the acausal framework.

Any guidance here would be very helpful.

---

<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: [November 3, 2024, 11:01am UTC](https://discourse.julialang.org/t/stochasticdiffeq-with-acausal-modeling/121998/2 "2024-11-03T11:01:05Z")

</div>

Using SDESystem was deprecated awhile ago for `System`. See:

> **[Modeling with Stochasticity · ModelingToolkit.jl](https://docs.sciml.ai/ModelingToolkit/stable/tutorials/stochastic_diffeq/)**
>
> Documentation for ModelingToolkit.jl.

---

<div class="post-metadata">

### Author: ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)
#### Post date: [November 3, 2024, 12:17pm UTC](https://discourse.julialang.org/t/stochasticdiffeq-with-acausal-modeling/121998/3 "2024-11-03T12:17:07Z")

</div>

Hello,  
Is there a way to use it like the following? I cannot find much docs on this

The following example will not compile but I wanted to know if I can pass `@brownian` variables to the `System` the way I can pass `eqs`, `@variables`, and `@parameters`

```julia
using ModelingToolkit, StochasticDiffEq
using ModelingToolkit: t_nounits as t, D_nounits as D
using Plots

function sto(;name)
    para = @parameters begin
        σ=10.0 
        ρ=28
        β=8/3
    end
    vars = @variables begin
        x(t)
        y(t)
        z(t)
    end 
    br = @brownian begin
        B
    end
    eqs = [D(x) ~ σ * (y - x) + B,
        D(y) ~ x * (ρ - z) - y ,
        D(z) ~ x * y - β * z ]
    System(eqs,t,vars,para,br;name)
end
@named de = sto()
sys = structural_simplify(de)
typeof(sys)

```

This example works for the `ODESystem` without using the `@brownian` variables.

---

<div class="post-metadata">

### Author: ![Sushrut\_Deshpande](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sushrut_deshpande/32/52754_2.png) [@Sushrut\_Deshpande](https://discourse.julialang.org/u/Sushrut_Deshpande)
#### Post date: [November 3, 2024, 12:18pm UTC](https://discourse.julialang.org/t/stochasticdiffeq-with-acausal-modeling/121998/4 "2024-11-03T12:18:02Z")

</div>

inshort without using `@mtkbuild`

---

<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: [November 3, 2024, 1:26pm UTC](https://discourse.julialang.org/t/stochasticdiffeq-with-acausal-modeling/121998/5 "2024-11-03T13:26:00Z")

</div>

open an issue
