Skip to content

LIBSTELL: correct the right-edge curvature and slope of the Akima splines - #504

Merged
lazersos merged 1 commit into
PrincetonUniversity:developfrom
CharlesCNorton:akima-right-edge
Oct 4, 2026
Merged

lazersos merged 1 commit into
PrincetonUniversity:developfrom
CharlesCNorton:akima-right-edge

Conversation

@CharlesCNorton

@CharlesCNorton CharlesCNorton commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Problem

spline_akima.f and spline_akima_int.f in LIBSTELL/Sources/Miscel 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. hiddenSymmetries/VMEC2000#23 makes the same change in Sources/LIBSTELL_minimal, 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 LIBSTELL/Sources/Modules/stel_kinds.f LIBSTELL/Sources/Miscel/spline_akima.f \
  LIBSTELL/Sources/Miscel/spline_akima_int.f akima_check.f90 -o akima_check

Effect

profile_functions.f, in LIBSTELL and in VMEC2PIES, calls these routines for every profile of type akima_spline, akima_spline_i or akima_spline_ip, and THRIFT (thrift_equil_p.f90, thrift_equil_j.f90) and stellopt_prof_to_vmec.f90 pass their profiles to VMEC as an akima_spline pressure and an akima_spline_ip current. In the Akima profiles of BENCHMARKS/DIAGNO_TEST/input.DIIID_m20n0s128_nfp1_lasym, BENCHMARKS/DIAGNO_TEST/input.iter and BENCHMARKS/STELLOPT_TEST/RECON_TOK/input.RECON_TOK 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 the akima_spline_ip current to s = 1 changes by 5.1e-4 in input.iter and by 7.3e-5 in input.RECON_TOK; VMEC divides the current profile by that value to match CURTOR, so the current enclosed by every surface inside s = 0.989 changes by the same fraction.

@lazersos lazersos 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.

Appears legitimate. Effect overall should be small.

@lazersos
lazersos merged commit 8060f5e into PrincetonUniversity:develop Oct 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants