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

Discrete Events Behaving Unexpectedly #2528

Closed
timknab opened this issue Mar 7, 2024 · 2 comments · Fixed by SciML/DiffEqCallbacks.jl#210
Closed

Discrete Events Behaving Unexpectedly #2528

timknab opened this issue Mar 7, 2024 · 2 comments · Fixed by SciML/DiffEqCallbacks.jl#210
Labels
bug Something isn't working events

Comments

@timknab
Copy link

timknab commented Mar 7, 2024

There seem to be a couple of weird behaviors when using discrete_events in MTK9. For example, when setting a period of 24 hours for a periodic input into a state, the last event seems to double the input amount.

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using DifferentialEquations
using Plots
function MTK_func(;name)
    pars = @parameters begin
        input = 10.0
    end

    D = Differential(t)


    vars = @variables begin
        A(t) = 0.0
    end

        eqs = [D(A) ~ -A]
    ODESystem(eqs, t, vars, pars; discrete_events = [24.0 => [A ~ A + input]], name = name)
end

@mtkbuild mtkmod = MTK_func()

tspan = (0.0, 130.0);  # [hr], 28 days

prob = ODEProblem(mtkmod, Pair[], tspan);
sol = solve(prob);

Plots.plot(sol)

And if you use a vector of event times and happen to include 0.0, even though it should have no effect, it skips the first event...

using ModelingToolkit
using ModelingToolkit: t_nounits as t, D_nounits as D
using DifferentialEquations
using Plots
function MTK_func(input_times;name)
    pars = @parameters begin
        input = 10.0
    end

    D = Differential(t)


    vars = @variables begin
        A(t) = 0.0
    end

        eqs = [D(A) ~ -A]
    ODESystem(eqs, t, vars, pars; discrete_events = [input_times => [A ~ A + input]], name = name)
end

@mtkbuild mtk1 = PK_func([0.0, 24.0, 48.0, 96.0])
@mtkbuild mtk2 = PK_func([24.0, 48.0, 96.0])

tspan = (0.0, 130.0);  # [hr], 28 days

prob1 = ODEProblem(mtk1, Pair[], tspan);
prob2 = ODEProblem(mtk2, Pair[], tspan);
sol1 = solve(prob1, saveat=0.1);
sol2 = solve(prob2, saveat=0.1);

Plots.plot(sol1)
Plots.plot!(sol2)
Pkg.status()
[0c46a032] DifferentialEquations v7.13.0
[961ee093] ModelingToolkit v9.4.0
[91a5bcdd] Plots v1.40.1
@timknab timknab added the bug Something isn't working label Mar 7, 2024
@ChrisRackauckas
Copy link
Member

The missed event is fixed by SciML/DiffEqCallbacks.jl#210

But there's still the first issue that it seems a double event in the first.

@ChrisRackauckas
Copy link
Member

A fix for that behavior was added to that PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working events
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants