SciML / ModelingToolkit.jl

An acausal modeling framework for automatically parallelized scientific machine learning (SciML) in Julia. A computer algebra system for integrated symbolics for physics-informed machine learning and automated transformations of differential equations
https://mtk.sciml.ai/dev/
Other
1.43k stars 209 forks source link

making SDESystem without using @mtkbuild #3174

Closed Sush1090 closed 1 week ago

Sush1090 commented 1 week ago

Is your feature request related to a problem? Please describe.

I like to make my "acausal" components using the following format

function MyComp(;name,...)
    @named inport = ...connector..
    @named outport = ...connector..
    para = @parameters begin
        MyParas ...
    end
    vars = @variables begin
        MyVars ...
     end
   eqs = [  outport.mdot ~ abs(inport.mdot) 
            outport.p ~ eq1 ...
            outport.h ~ eq2 ...
            ..
   ]
   compose(ODESystem(eqs, t, vars, para;name), inport, outport)
end

This works very well for ODESystems I would like to know if I can have the same possibility of doing the same with SDESystem Describe the solution you’d like So it should look something like this:

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)

The current example of SDESystem works well as described in here but uses @mtkbuild which does not allow me to make my function. Describe alternatives you’ve considered The example in the tutorial is the only alternative I know but the "low-level" interface allows for more flexibility as I have mentioned in the example of ODESystem and SDESystem.

Additional context

Sush1090 commented 1 week ago

I found a working solution.

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

This already existed