Skip to content

Correct the right-edge curvature and slope of the Akima splines - #23

Merged
mbkumar merged 1 commit into
hiddenSymmetries:masterfrom
CharlesCNorton:akima-right-edge
Sep 24, 2026
Merged

mbkumar merged 1 commit into
hiddenSymmetries:masterfrom
CharlesCNorton:akima-right-edge

Conversation

@CharlesCNorton

Copy link
Copy Markdown
Contributor

Problem

spline_akima.f and spline_akima_int.f in Sources/LIBSTELL_minimal extend the data by two phantom points at each end, extrapolated along the parabola through the three end knots. The left end takes the curvature of that parabola and its slope at xloc(1),

cl = (m(2)-m(1))/(xloc(3)-xloc(1))
bl = m(1) - cl*(xloc(2)-xloc(1))

and the right end forms

cr = (m(iv-2)-m(iv-1))/(xloc(iv)-xloc(iv-2))
br = m(iv-2) - cr*(xloc(iv-1)-xloc(iv-2))

for the expansion yloc(iv) + br*(x-xloc(iv)) + cr*(x-xloc(iv))**2 that places yloc(iv+1) and yloc(iv+2). This cr has the wrong sign, and br is the slope of the parabola at xloc(iv-1) instead of xloc(iv). The phantom secants therefore turn back, m(iv) = m(iv-2) and m(iv+1) = 2*m(iv-2) - m(iv-1), where at the left end they continue the trend of the data, m(0) = 2*m(1) - m(2) and m(-1) = 3*m(1) - 2*m(2). The Akima slope at the last knot comes out as (m(iv-2)+m(iv-1))/2 instead of (3*m(iv-1)-m(iv-2))/2, the mirror of the slope (3*m(1)-m(2))/2 at the first knot, and every other knot keeps its slope. Only the last interval changes: with h = xx(npts)-xx(npts-1), the interpolant there is off by up to 4/27*|m(iv-1)-m(iv-2)|*h and its integral by |m(iv-1)-m(iv-2)|*h**2/12.

Fix

Form cr and br as the mirror of cl and bl, in both files:

cr = (m(iv-1)-m(iv-2))/(xloc(iv)-xloc(iv-2))
br = m(iv-1) - cr*(xloc(iv-1)-xloc(iv))

VMEC++ forms them this way, and jonathanschilling/educational_VMEC#3 made the same correction to cr and br in educational_VMEC. PrincetonUniversity/STELLOPT#504 makes the same change in LIBSTELL/Sources/Miscel, whose two files are identical to these.

Test

The program below interpolates a data set and its mirror image, and samples the parabola 1 + x/2 - x**2 on 11 uniformly spaced knots. Before this change the interpolant of the mirrored data, mirrored back, differs from the original by 1.8e-1 in value and 2.2e-2 in the integral, and the spline misses the parabola by 3.0e-3 in value and 1.7e-4 in the integral, all of it on the last interval; with the change all four are below 5e-16. On 48 random knot sets the fixed routines agree with the Akima profiles of VMEC++ to 6e-16, where the unfixed ones differed by up to 21% of the profile maximum.

akima_check.f90
program akima_check
  use stel_kinds, only: rprec
  implicit none
  integer, parameter :: n = 5, nk = 11, nq = 1001
  real(rprec), parameter :: xk(n) = &
    [0.0_rprec, 0.2_rprec, 0.45_rprec, 0.7_rprec, 1.0_rprec]
  real(rprec), parameter :: yk(n) = &
    [0.3_rprec, 0.7_rprec, 0.55_rprec, 0.9_rprec, 0.1_rprec]
  real(rprec) :: xr(n), yr(n), s(nk), y(nk), x, f, g, fi, gi, g1
  real(rprec) :: asym, asym_int, dev, dev_int
  integer :: i, iflag

  ! the mirror image of the data must give the mirrored interpolant
  xr = 1 - xk(n:1:-1)
  yr = yk(n:1:-1)
  call spline_akima_int(1.0_rprec, g1, xr, yr, n, iflag)
  asym = 0; asym_int = 0
  do i = 0, nq - 1
    x = real(i, rprec)/(nq - 1)
    call spline_akima(x, f, xk, yk, n, iflag)
    call spline_akima(1 - x, g, xr, yr, n, iflag)
    call spline_akima_int(x, fi, xk, yk, n, iflag)
    call spline_akima_int(1 - x, gi, xr, yr, n, iflag)
    asym = max(asym, abs(f - g))
    asym_int = max(asym_int, abs(fi - (g1 - gi)))
  end do

  ! uniformly spaced samples of a parabola must be reproduced exactly
  s = [(real(i, rprec)/(nk - 1), i = 0, nk - 1)]
  y = 1 + s/2 - s**2
  dev = 0; dev_int = 0
  do i = 0, nq - 1
    x = real(i, rprec)/(nq - 1)
    call spline_akima(x, f, s, y, nk, iflag)
    call spline_akima_int(x, fi, s, y, nk, iflag)
    dev = max(dev, abs(f - (1 + x/2 - x**2)))
    dev_int = max(dev_int, abs(fi - (x + x**2/4 - x**3/3)))
  end do

  print '(a, 2es10.2)', 'reflection, value and integral:', asym, asym_int
  print '(a, 2es10.2)', 'parabola,   value and integral:', dev, dev_int
end program akima_check
gfortran Sources/LIBSTELL_minimal/stel_kinds.f Sources/LIBSTELL_minimal/spline_akima.f \
  Sources/LIBSTELL_minimal/spline_akima_int.f akima_check.f90 -o akima_check

Effect

profile_functions.f calls these routines for every profile of type akima_spline, akima_spline_i or akima_spline_ip, and none of the inputs under python/tests uses one. In the Akima profiles of the benchmark inputs in STELLOPT the change is confined to the last knot interval, s > 0.989, and is at most 7.1e-4 of the profile maximum. The integral of an akima_spline_ip current to s = 1 changes there by up to 5.1e-4; VMEC divides the current profile by that value to match CURTOR, so the current enclosed by every surface with s < xx(npts-1) changes by the same fraction.

@mbkumar mbkumar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the fix

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