Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@mtkmodel macro supporting DDEs #3274

Open
Filer1 opened this issue Dec 16, 2024 · 0 comments
Open

@mtkmodel macro supporting DDEs #3274

Filer1 opened this issue Dec 16, 2024 · 0 comments

Comments

@Filer1
Copy link

Filer1 commented Dec 16, 2024

I was wondering if it was somehow possible to use transport delay inside of a ModelingToolkit component. I tried modifing this minamal example using DDEs

using ModelingToolkit, DelayDiffEq
using ModelingToolkit: t_nounits as t, D_nounits as D

tau = 1;
@parameters p0=0.2 p1=0.2 q0=0.3 q1=0.3 v0=1 v1=1 d0=5 d1=1 d2=1 beta0=1 beta1=1
@variables x₀(t) x₁(t) x₂(..)
eqs = [D(x₀) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (p0 - q0) * x₀ - d0 * x₀
       D(x₁) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (1 - p0 + q0) * x₀ +
               (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (p1 - q1) * x₁ - d1 * x₁
       D(x₂(t)) ~ (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (1 - p1 + q1) * x₁ - d2 * x₂(t)]
@mtkbuild sys = System(eqs, t)

tspan = (0.0, 10.0)
prob = DDEProblem(sys,
    [x₀ => 1.0, x₁ => 1.0, x₂(t) => 1.0],
    tspan,
    constant_lags = [tau])

alg = MethodOfSteps(Tsit5())
sol = solve(prob, alg)

using Plots
plot(sol)

to

using ModelingToolkit, DelayDiffEq
using ModelingToolkit: t_nounits as t, D_nounits as D

using Plots

# this does not work
tau = 1 
@mtkmodel FOL begin
    @parameters begin
        p0=0.2 
        p1=0.2 
        q0=0.3 
        q1=0.3 
        v0=1 
        v1=1 
        d0=5 
        d1=1 
        d2=1 
        beta0=1 
        beta1=1
    end
    @variables begin
        x₀(t) 
        x₁(t) 
        x₂(..)
    end
    @equations begin
        D(x₀) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (p0 - q0) * x₀ - d0 * x₀
        D(x₁) ~ (v0 / (1 + beta0 * (x₂(t - tau)^2))) * (1 - p0 + q0) * x₀ +
               (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (p1 - q1) * x₁ - d1 * x₁
        D(x₂(t)) ~ (v1 / (1 + beta1 * (x₂(t - tau)^2))) * (1 - p1 + q1) * x₁ - d2 * x₂(t)
    end
end

tspan = (0.0, 10.0)
@mtkbuild sys = FOL()

prob = DDEProblem(sys,
    [x₀ => 1.0, x₁ => 1.0, x₂(t) => 1.0],
    tspan,
    constant_lags = [tau])

alg = MethodOfSteps(Tsit5())
sol = solve(prob, alg)

plot(sol)

and found it does not work as expected. It turned out that ModelingToolkit.jl does no support DDEs in the macro right now, but should be able to lower to System instead of ODESystem.

This issue is a result of a forum discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant