Splines Module
The Splines module provides cubic, bicubic, and fourier spline interpolation functionality, used for smooth representation of MHD equilibria.
Overview
The module includes:
- Cubic spline interpolation for 1D data
- Bicubic spline interpolation for 2D data
- Fourier spline interpolation for decomposed data
- Derivative evaluation capabilities
- Support for both real and complex-valued data
API Reference
Types
CubicSplineType
Represents a cubic spline interpolation object.
BicubicSplineType
Represents a bicubic spline interpolation object for 2D data.
FourierSplineType
Represents a Fourier spline interpolation object for decomposed data.
Functions
spline_setup
JPEC.SplinesMod.CubicSpline.spline_setup
— Functionspline_setup(xs, fs, bctype=1)
Set up a cubic spline interpolation.
Arguments
xs
: A vector of Float64 values representing the x-coordinates (must be monotonically increasing)fs
: A vector or matrix of Float64/ComplexF64 values representing the function values at the x-coordinatesbctype
: An integer specifying the boundary condition type (default is 1):- 1: Natural spline (zero second derivative at endpoints)
- 2: Periodic spline
- 3: Extrapolated spline
- 4: Not-a-knot spline
Returns
- A
CubicSplineType
object that can be used for evaluation
Examples
# 1D spline
xs = collect(range(0.0; stop=2π, length=21))
fs = sin.(xs)
spline = spline_setup(xs, fs, 1)
# Multi-quantity spline
fs_matrix = hcat(sin.(xs), cos.(xs))
spline = spline_setup(xs, fs_matrix, 1)
spline_eval
JPEC.SplinesMod.CubicSpline.spline_eval
— Functionspline_eval(spline, x, derivs=0)
Evaluate a cubic spline at given points.
Arguments
spline
: ACubicSplineType
object created byspline_setup
x
: A Float64 value or vector of Float64 values representing the x-coordinates to evaluatederivs
: Integer specifying derivative level (default is 0):- 0: Function values only
- 1: Function values and first derivatives
- 2: Function values, first and second derivatives
Returns
- If
derivs=0
: Matrix of function values (length(x) × nqty) - If
derivs=1
: Tuple of (values, first_derivatives) - If
derivs=2
: Tuple of (values, firstderivatives, secondderivatives)
Examples
# Evaluate at single point
spline = spline_setup(xs, fs, 1)
f_vals = spline_eval(spline, π / 2)
# Evaluate at multiple points
x_eval = [π / 4, π / 2, 3π / 4]
f_vals = spline_eval(spline, x_eval)
# Get derivatives
f_vals, f_derivs = spline_eval(spline, x_eval, 1)
bicube_setup
JPEC.SplinesMod.BicubicSpline.bicube_setup
— Functionbicube_setup(xs, ys, fs, bctypex=1, bctypey=1)
Set up a bicubic spline interpolation for 2D data.
Arguments
xs
: Vector of Float64 values representing the x-coordinates (must be monotonically increasing)ys
: Vector of Float64 values representing the y-coordinates (must be monotonically increasing)fs
: 3D array of Float64 values with dimensions (nx, ny, nqty) representing function valuesbctypex
: Boundary condition type for x-direction (default is 1)bctypey
: Boundary condition type for y-direction (default is 1)- 1: Natural spline (zero second derivative at boundaries)
- 2: Periodic spline
- 3: Extrapolated spline
- 4: Not-a-knot spline
Returns
- A
BicubicSplineType
object that can be used for 2D evaluation
Examples
# Create 2D grid
xs = collect(range(0.0; stop=2π, length=20))
ys = collect(range(0.0; stop=2π, length=20))
# Create 3D function data array
fs = zeros(20, 20, 1)
for i in 1:20, j in 1:20
fs[i, j, 1] = sin(xs[i]) * cos(ys[j])
end
# Set up bicubic spline
bcspline = bicube_setup(xs, ys, fs, 1, 1)
bicube_eval
JPEC.SplinesMod.BicubicSpline.bicube_eval
— Functionbicube_eval(bicube, x, y, derivs=0)
Evaluate a bicubic spline at given 2D points.
Arguments
bicube
: ABicubicSplineType
object created bybicube_setup
x
: Float64 value or vector of x-coordinates to evaluatey
: Float64 value or vector of y-coordinates to evaluatederivs
: Integer specifying derivative level (default is 0):- 0: Function values only
- 1: Function values and first derivatives (∂f/∂x, ∂f/∂y)
- 2: Function values, first and second derivatives (∂f/∂x, ∂f/∂y, ∂²f/∂x², ∂²f/∂x∂y, ∂²f/∂y²)
Returns
- If
derivs=0
: 3D array of function values (length(x) × length(y) × nqty) - If
derivs=1
: Tuple of (values, xderivatives, yderivatives) - If
derivs=2
: Tuple of (values, xderivatives, yderivatives, xxderivatives, xyderivatives, yy_derivatives)
Examples
# Evaluate at single point
bcspline = bicube_setup(xs, ys, fs, 1, 1)
f_vals = bicube_eval(bcspline, π / 2, π / 4)
# Evaluate on grid
x_eval = collect(range(0, 2π; length=50))
y_eval = collect(range(0, 2π; length=50))
f_vals = bicube_eval(bcspline, x_eval, y_eval)
# Get derivatives
f_vals, fx, fy = bicube_eval(bcspline, x_eval, y_eval, 1)
Example Usage
1D Cubic Spline
using JPEC
# Create data points
xs = collect(range(0.0, stop=2π, length=21))
fs = sin.(xs)
# Set up spline (1 quantity)
spline = JPEC.SplinesMod.spline_setup(xs, hcat(fs), 1)
# Evaluate at new points
xs_fine = collect(range(0.0, stop=2π, length=100))
fs_fine = JPEC.SplinesMod.spline_eval(spline, xs_fine)
2D Bicubic Spline
# Create 2D grid
xs = collect(range(0.0, stop=2π, length=20))
ys = collect(range(0.0, stop=2π, length=20))
# Create 2D function data
fs = zeros(20, 20, 1)
for i in 1:20, j in 1:20
fs[i, j, 1] = sin(xs[i]) * cos(ys[j])
end
# Set up bicubic spline
bcspline = JPEC.SplinesMod.bicube_setup(xs, ys, fs, 1, 1)
# Evaluate with derivatives
x_eval, y_eval = π/2, π/4
f, fx, fy = JPEC.SplinesMod.bicube_eval(bcspline, x_eval, y_eval, 1)