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

feat!: use SciMLStructures and add new MTKParameters #2447

Merged
merged 12 commits into from
Feb 14, 2024

Conversation

AayushSabharwal
Copy link
Member

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Additional context

Add any other context about the problem here.

@AayushSabharwal AayushSabharwal marked this pull request as ready for review February 13, 2024 11:09
@AayushSabharwal AayushSabharwal changed the title [WIP] feat!: use SciMLStructures and add new MTKParameters feat!: use SciMLStructures and add new MTKParameters Feb 13, 2024
@AayushSabharwal
Copy link
Member Author

Also worth noting that this still scalarizes vector parameters. Not doing that won't be breaking, so it's not blocking the release.

@TorkelE
Copy link
Member

TorkelE commented Feb 13, 2024

You mean that parameters that are length-1 vectors become scalars? If so, would it be possible to not do that and let the user decide? I have a case which heavily depends on parameters that are length-1 vectors.

test/runtests.jl Outdated
@safetestset "Input Output Test" include("input_output_handling.jl")
@safetestset "Clock Test" include("clock.jl")
@safetestset "DiscreteSystem Test" include("discretesystem.jl")
@safetestset "Linearization Tests" include("linearize.jl") # !
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should remove the # !s.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b20236e.

@@ -60,22 +61,22 @@ for f in [
# iip
du = zeros(3)
u = collect(1:3)
p = collect(4:6)
p = ModelingToolkit.MTKParameters(de, [σ, ρ, β] .=> 4.0:6.0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we keep the original p to test the fallback case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the system has an IndexCache it will always use that for indexing, and expect that the parameter buffer is an MTKParameters. As such, collect(4:6) will be turned into MTKParameters(de, parameters(de) .=> collect(4:6))

# Creates F and J functions.
ofun = NonlinearFunction(nsys; jac = jac)
F = ofun.f
J = jac ? ofun.jac : nothing
F(u, p) = ofun.f(u, ModelingToolkit.MTKParameters(nsys, parameters(nsys) .=> p))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really the best way to do this? Doesn’t this mean every call is reallocating a vector of pairs? BifurcationKit will call this function many many times.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is not a good way to do this. We need to do the interface a bit differently here, but I don't want that to block the v9. It should build the problem and use the generated functions from that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't reviewed these changes, but I hope that condition/affect functions that are generated will have similar performance to the current MTK and don't have such allocations (which would be really bad for jump models).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(given how often those functions are called)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no allocations, and the split form is type stable so it should be faster for cases that have heterogeneous parameters.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't reviewed these changes, but I hope that condition/affect functions that are generated will have similar performance to the current MTK and don't have such allocations (which would be really bad for jump models).

This is a BifurcationKit-only hack, which I'm removing in favor of just not using MTKParameters here. Everywhere else in the codebase, this sort of thing does not happen. MTKParameters is only created once for a problem, and all of the codegen leverages it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AayushSabharwal
Copy link
Member Author

You mean that parameters that are length-1 vectors become scalars? If so, would it be possible to not do that and let the user decide? I have a case which heavily depends on parameters that are length-1 vectors.

No, this only means that parameter values need to be specified as [p[1] => 1.0, ...], similar to how get_u0_p expects them to be. Indexing array parameters still returns an array:

julia> @parameters p[1:1]
julia> @variables x(t)
julia> @mtkbuild sys = ODESystem([D(x) ~ sum(p) * t], t)
julia> prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p[1] => 1.0])
julia> prob.ps[p]
1-element Vector{Float64}:
 1.0

This is something that will be changed, to make specifying [p => [1.0]] possible, but that probably needs some changes outside of just IndexCache/MTKParameters and isn't blocking for MTKv9, so I've pushed it down the road a bit in the interest of releasing sooner.

@ChrisRackauckas
Copy link
Member

Open an issue on that.

@ChrisRackauckas ChrisRackauckas merged commit 3b4fb73 into SciML:master Feb 14, 2024
3 of 20 checks passed
@TorkelE
Copy link
Member

TorkelE commented Feb 14, 2024

You mean that parameters that are length-1 vectors become scalars? If so, would it be possible to not do that and let the user decide? I have a case which heavily depends on parameters that are length-1 vectors.

No, this only means that parameter values need to be specified as [p[1] => 1.0, ...], similar to how get_u0_p expects them to be. Indexing array parameters still returns an array:

julia> @parameters p[1:1]
julia> @variables x(t)
julia> @mtkbuild sys = ODESystem([D(x) ~ sum(p) * t], t)
julia> prob = ODEProblem(sys, [x => 1.0], (0.0, 1.0), [p[1] => 1.0])
julia> prob.ps[p]
1-element Vector{Float64}:
 1.0

This is something that will be changed, to make specifying [p => [1.0]] possible, but that probably needs some changes outside of just IndexCache/MTKParameters and isn't blocking for MTKv9, so I've pushed it down the road a bit in the interest of releasing sooner.

That makes sense, thanks

@AayushSabharwal AayushSabharwal deleted the as/param-splitting branch February 29, 2024 12:11
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

Successfully merging this pull request may close these issues.

5 participants