Skip to content

Continious Callback does not work when condition is not properly written as a function of time #3621

@TorkelE

Description

@TorkelE

This came up in SBMLImporter, think the bug only appeared this week, so was likely introduced relative recently.

Here the callback should be triggered, but never is.

using ModelingToolkit, OrdinaryDiffEq, Plots
using ModelingToolkit: t_nounits as t, D_nounits as D

# Create ODEProblem
@parameters k1 k2 C
@variables S1(t) S2(t) S3(t)
eqs = [
    D(S1) ~ - (k1 / C) * S1 * S2 + k2 * S3,
    D(S2) ~ - (k1 / C) * S1 * S2 + k2 * S3,
    D(S3) ~ (k1 / C) * S1 * S2 - k2 * S3,
]
@mtkbuild model = ODESystem(eqs, t)
u0 = [S1 => 1.0, S2 => 2.0, S3 => 1.0]
ps = [k1 => 0.75, k2 => 0.25, C => 1.0]
ode = ODEProblem(model, u0, (0.0, 2.0), ps)

# Create callabck.
function myaffect!(integrator)
    println("Activated")
    integrator[S2] = 1 * integrator.ps[C]
    return nothing
end
function mycond(u, t, integrator)
    return integrator[S1] / integrator.ps[C] - 0.75
end
cb = ContinuousCallback(mycond, myaffect!)

# Solve problem.
sol = solve(ode; callback = cb)
plot(sol; idxs = S1/C - 0.75)

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions