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

use b-splines to calculate moments; add spline keyword #3

Merged
merged 1 commit into from
Jun 12, 2023
Merged

Conversation

lstagner
Copy link
Contributor

@lstagner lstagner commented Jun 8, 2023

This is more accurate for higher mode numbers. default is to use trapz since splines are 5-10x slower.

import MillerExtendedHarmonic
using MXHEquilibrium
using PyPlot
using EFIT
g = readg("/home/lstagner/.julia/dev/MXHEquilibrium/test/g150219.03200")

M = efit(g,clockwise_phi=false)
bdry = plasma_boundary(M)

fig,ax = plt.subplots(ncols=5)
fig.set_size_inches(12,5)
for (i,N) in enumerate(2:2:10)
    @time mxh1 = MillerExtendedHarmonic.MXH(bdry.r,bdry.z,N; spline=false);
    @time mxh2 = MillerExtendedHarmonic.MXH(bdry.r,bdry.z,N; spline=true);
    ax[i].plot(bdry.r,bdry.z,color="black")

    x1,y1 = mxh1(100)
    ax[i].plot(x1,y1,color="b",ls="--",label="trapz")
    x2,y2 = mxh2(100)
    ax[i].plot(x2,y2,color="r",ls="--",label="spline")
    ax[i].legend(title="N=$N")
    ax[i].set_aspect("equal")
end
fig.tight_layout()

image

"""
function MXH(pr::AbstractVector{<:Real}, pz::AbstractVector{<:Real}, MXH_modes::Integer=5; θ=nothing, Δθᵣ=nothing, dθ=nothing, Fm=nothing, optimize_fit=false)
function MXH(pr::AbstractVector{<:Real}, pz::AbstractVector{<:Real}, MXH_modes::Integer=5; θ=nothing, Δθᵣ=nothing, dθ=nothing, Fm=nothing, optimize_fit=false, spline=false)
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 make spline=true the default? @lstagner do you have a sense of how the spline approach affects performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its a somewhat significant slowdown.

N = 2 spline = false:  0.000039 seconds (25 allocations: 12.375 KiB)
N = 2 spline = true:   0.000336 seconds (115 allocations: 347.688 KiB)
N = 4 spline = false:  0.000073 seconds (25 allocations: 12.406 KiB)
N = 4 spline = true:   0.000438 seconds (187 allocations: 615.969 KiB)
N = 6 spline = false:  0.000051 seconds (25 allocations: 12.438 KiB)
N = 6 spline = true:   0.000759 seconds (259 allocations: 884.250 KiB)
N = 8 spline = false:  0.000067 seconds (25 allocations: 12.469 KiB)
N = 8 spline = true:   0.000908 seconds (331 allocations: 1.126 MiB)
N = 10 spline = false:  0.000065 seconds (25 allocations: 12.500 KiB)
N = 10 spline = true:   0.000979 seconds (403 allocations: 1.388 MiB)

@orso82 orso82 merged commit dae2e20 into master Jun 12, 2023
@orso82 orso82 deleted the spline branch June 12, 2023 16:30
@orso82
Copy link
Member

orso82 commented Jun 12, 2023

Thank you for taking the time to do the benchmark @lstagner

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.

2 participants