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

BifurcationKit: Bad performance relative to user supplied vector field in a function #3468

Open
rveltz opened this issue Mar 18, 2025 · 2 comments
Assignees
Labels
question Further information is requested

Comments

@rveltz
Copy link
Contributor

rveltz commented Mar 18, 2025

Hi,

I was expecting MTK to be much faster than a user passed vector field and ForwardDiff jacobian. This is not the case. Note that in the latest version of BifurcationKit, you can pass the inplace vector field and the inplace jacobian.

Perhaps, it could be easier to define a new type like BifurcationProblemMTK which redefine residual, residual!, jacobian, jacobian! and the @optic functionality so that closures are avoided.

using BifurcationKit, ModelingToolkit

@variables t x(t) y(t)
@parameters μ
D = Differential(t)
eqs = [D(x) ~ μ * x - y - x * (x^2 + y^2),
       D(y) ~ x + μ * y - y * (x^2 + y^2)]
@named osys = ODESystem(eqs, t)
osys = complete(osys)

bif_par = μ
plot_var = x
p_start ==> 1.0]
u0_guess = [x => 0.0, y => 0.0]

bprob = BifurcationProblem(osys,
    u0_guess,
    p_start,
    bif_par;
    plot_var = plot_var,
    jac = false)

using BenchmarkTools
_out = zeros(2)
_J = zeros(2,2)
@btime BifurcationKit.residual($bprob, $(bprob.u0), $(bprob.params)) # 28.769 ns (1 allocation: 80 bytes)
@btime BifurcationKit.residual!($bprob, $_out, $(bprob.u0), $(bprob.params)) # 14.389 ns (0 allocations: 0 bytes)
@btime BifurcationKit.jacobian($bprob, $(bprob.u0), $(bprob.params)) #  323.101 ns (6 allocations: 432 bytes)
@btime BifurcationKit.jacobian!($bprob, $_J, $(bprob.u0), $(bprob.params)) #  308.194 ns (5 allocations: 336 bytes)

function Fsl!(out, X, p)
    (;μ, ) = p
    x, y = X
    ua = x^2 + y^2
    out[1] = μ * x - y - x * (x^2 + y^2)
    out[2] = x + μ * y - y * (x^2 + y^2)
    out
end

bprob2 = BifurcationProblem(Fsl!, zeros(2), (μ=0.1, b=0), (@optic _.μ))
@btime BifurcationKit.residual($bprob2, $(bprob2.u0), $(bprob2.params)) # 15.238 ns (1 allocation: 80 bytes)
@btime BifurcationKit.residual!($bprob2, $_out, $(bprob2.u0), $(bprob2.params)) # 1.916 ns (0 allocations: 0 bytes)
@btime BifurcationKit.jacobian($bprob2, $(bprob2.u0), $(bprob2.params)) #  289.651 ns (6 allocations: 432 bytes)
@btime BifurcationKit.jacobian!($bprob2, $_J, $(bprob2.u0), $(bprob2.params)) # 280.585 ns (5 allocations: 336 bytes)
@rveltz rveltz added the question Further information is requested label Mar 18, 2025
@ChrisRackauckas
Copy link
Member

It should just match in this case, not be any faster or slower. It would be good to figure out why it's not in this case.

@rveltz
Copy link
Contributor Author

rveltz commented Mar 18, 2025

The inplace jacobian should be much faster. If I code it myself, it is like 5ns

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants