StochasticDiffEq with Acausal modeling

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

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:

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.

Using SDESystem was deprecated awhile ago for System. See:

1 Like

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

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.

inshort without using @mtkbuild

open an issue

1 Like