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

Support remake to update individual variables/species in new MTK version #2396

Closed
TorkelE opened this issue Dec 26, 2023 · 7 comments
Closed

Comments

@TorkelE
Copy link
Member

TorkelE commented Dec 26, 2023

Currently, to mutate e.g. an ODEProblem to change a variable, you can do

prob[x] = 1.0

Using remake is a bit different, in that it leaves the original problem unchanged, but creates a new one. Bar various bugs, you can do e.g.

oprob_new = remake(prob; u0 = [x => 1.0, y => 2.0, z => 3.0])

to create a new problem wit ha new variable vector. However, if I want to update only a subset of the variables, e.g. only x, is there a plan for supporting this using the new symbolic indexing update?

@ChrisRackauckas
Copy link
Member

what about oprob_new = remake(prob; u0 = [x => 1.0])?

@TorkelE
Copy link
Member Author

TorkelE commented Dec 26, 2023

I think this notation is good. If the users provide a symbol not existing in u0 an error can be thrown, instead, only those that are provided are updated.

The only potential problem is if the user writes an entirely new u0 vector, but forgets to add some variable (in which case this is not caught). I think people even going about this approach will be rare though, even when they update a full u0 vector they will likely just ctrl+c the original one and change the values. And when they don' they actually need to miss something etc.

@ChrisRackauckas
Copy link
Member

I believe that already works? We setup symbolic remake awhile ago.

@TorkelE
Copy link
Member Author

TorkelE commented Dec 26, 2023

I think it is supposed to, but I think generally the remake function is the biggest culprit of various things that should work for non-integer indexing doesn't work. Either case, what does/doesn't work currently is not important given the remake, I just wanted to ensure that this case was caught (and tested for) in the new MTK version.

@ChrisRackauckas
Copy link
Member

I think it does work and is tested? It's worth double checking.

@TorkelE
Copy link
Member Author

TorkelE commented Dec 26, 2023

Don't know, e.g. this gives errors for me:

using Catalyst
using DifferentialEquations

begin
    @parameters σ ρ β
    @variables t x(t) y(t) z(t)
    D = Differential(t)

    eqs = [D(x) ~ σ * (y - x),
        D(y) ~ x *- z) - y,
        D(z) ~ x * y - β * z]

    @named sys = ODESystem(eqs)
    sys = structural_simplify(sys)

    u0 = [x => 1.0,
        y => 0.0,
        z => 0.0]

    p ==> 28.0,
        ρ => 10.0,
        β => 8 / 3]

    tspan = (0.0, 100.0)
    prob = ODEProblem(sys, u0, tspan, p)
end

prob[x] # 1.0

prob_new = remake(prob; u0 = [x => 1.0])
prob_new[u0] # Error

prob_new = remake(prob; u0 = [x => 1.0, y => 0.0, z => 0.0])
prob_new[u0] # Error

@ChrisRackauckas
Copy link
Member

It worked fine.

using Catalyst

@parameters σ ρ β
@variables t x(t) y(t) z(t)
D = Differential(t)

eqs = [D(x) ~ σ * (y - x),
    D(y) ~ x *- z) - y,
    D(z) ~ x * y - β * z]

@named sys = ODESystem(eqs)
sys = structural_simplify(sys)

u0 = [x => 1.0,
    y => 0.0,
    z => 0.0]

p ==> 28.0,
    ρ => 10.0,
    β => 8 / 3]

tspan = (0.0, 100.0)
prob = ODEProblem(sys, u0, tspan, p)

prob[x] # 1.0

prob_new = remake(prob; u0 = [x => 2.0])
prob_new[x]
prob_new[z]

prob_new = remake(prob; u0 = [x => 2.0, y => 0.0, z => 0.0])
prob_new[x]
prob_new[z]

prob_new[u0] isn't meaningful.

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

2 participants