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

MTK allows you to create faulty events without throwing an error. #2612

Open
TorkelE opened this issue Apr 4, 2024 · 2 comments
Open

MTK allows you to create faulty events without throwing an error. #2612

TorkelE opened this issue Apr 4, 2024 · 2 comments
Labels
bug Something isn't working events

Comments

@TorkelE
Copy link
Member

TorkelE commented Apr 4, 2024

I have gone through the events in MTK. There are several cases where MTK allow your to build a system with a misformated event, and an error is not actually thrown until you create a problem or attempts to simulate it. Especially since it is a fit trick how to format the events, the errors should be thrown at the first possible time (when a system is created).

using ModelingToolkit, OrdinaryDiffEq

@parameters p d
@variables t X(t)
eqs = [Differential(t)(X) ~ p - d*X]

# Example 1: Discrete events with dosage given as a Tuple
discrete_events = (2.0, 5.0) => [X ~ X + 10.0]
@mtkbuild osys = ODESystem(eqs, t; discrete_events)
oprob = ODEProblem(osys, [X => 10], (0.0, 10.), [p => 2.0, d => 0.5]) # Error is thrown here.

# Example 2: Discrete events where single expression condition is given in a vector.
discrete_events = [X < 5.0] => [X ~ X + 10.0]
@mtkbuild osys = ODESystem(eqs, t; discrete_events)
oprob = ODEProblem(osys, [X => 10], (0.0, 10.), [p => 2.0, d => 0.5])
sol = solve(oprob, Tsit5()) # Error is thrown here.

# Example 3: Discrete events where multiple expression conditions are given in a vector.
discrete_events = [X < 5.0, X > 10.0] => [X ~ X + 10.0]
@mtkbuild osys = ODESystem(eqs, t; discrete_events)
oprob = ODEProblem(osys, [X => 10], (0.0, 10.), [p => 2.0, d => 0.5]) # Error is thrown here.

# Example 4: Discrete events that affects parameters and variables.
discrete_events = 5.0 => [X ~ X + 10.0, p ~ 2.0]
@mtkbuild osys = ODESystem(eqs, t; discrete_events)
oprob = ODEProblem(osys, [X => 10], (0.0, 10.), [p => 2.0, d => 0.5]) # Error is thrown here.

# Example 5: Discrete events with non-trivial affect right-hand side.
discrete_events = 5.0 => [X + 1.0 ~ X + 10.0]
@mtkbuild osys = ODESystem(eqs, t; discrete_events)
oprob = ODEProblem(osys, [X => 10], (0.0, 10.), [p => 2.0, d => 0.5]) # Error is thrown here.

# Example 6: Continuous events that affects parameters and variables.
continuous_events = [X ~ 5.0] => [X ~ X + 10.0, p ~ 2.0]
@mtkbuild osys = ODESystem(eqs, t; continuous_events)
oprob = ODEProblem(osys, [X => 10], (0.0, 10.), [p => 2.0, d => 0.5]) # Error is thrown here.

# Example 7: Continuous events with non-trivial affect right-hand side.
continuous_events = [X ~ 5.0] => [X + 1.0 ~ X + 10.0]
@mtkbuild osys = ODESystem(eqs, t; continuous_events)
oprob = ODEProblem(osys, [X => 10], (0.0, 10.), [p => 2.0, d => 0.5]) # Error is thrown here.
@TorkelE TorkelE added the bug Something isn't working label Apr 4, 2024
@baggepinnen
Copy link
Contributor

If we used the shift-index notation to describe the affect, we could actually handle cases like

discrete_events = 5.0 => [X + 1.0 ~ X + 10.0]

since it would be well-defined which X to solve for, and easy to solve

discrete_events = 5.0 => [X(k) + 1.0 ~ X(k-1) + 10.0]

for X(k).

@ChrisRackauckas
Copy link
Member

That's a pretty interesting proposal. We'd have to make validate that only a single shift is given but that would make that implicitness become explicit.

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

No branches or pull requests

3 participants