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
JPEC.SplinesMod.BicubicSpline — MethodBicubicSpline(xs, ys, fs; bctypex::Union{String, Int}="not-a-knot", bctypey::Union{String, Int}="not-a-knot")
## Arguments:
- `xs`: A vector of Float64 values representing the x-coordinates.
- `ys`: A vector of Float64 values representing the y-coordinates.
- `fs`: A 3D array of Float64 values representing the function values at the (x,y) coordinates.
## Keyword Arguments:
- `bctypex`: An integer specifying the boundary condition type for x (Default is 4, not a knot)
- `bctypey`: An integer specifying the boundary condition type for y (Default is 4, not a knot)
## Returns:
- A `BicubicSpline` object containing the spline handle, x-coordinates, y-coordinates,
function values, number of x-coordinates, number of y-coordinates, number of quantities,
and boundary condition types.JPEC.SplinesMod.CubicSpline — MethodCubicSpline(xs, fs; bctype="not-a-knot")
Arguments:
- `xs`: A vector of Float64 values representing the x-coordinates.
- `fs`: A vector or matrix of Float64/ComplexF64 values representing the function values at the x-coordinates.
Keyword Arguments:
- `bctype`: Boundary condition type for the cubic spline in `x`.
- 1: Natural spline (default)
- 2: Periodic spline
- 3: Extrapolated spline
- 4: "Not-a-knot" spline
Returns:
- A `CubicSpline` object containing the spline handle, x-coordinates, function values,
number of x-coordinates, number of quantities, and index of x position in the spline.JPEC.SplinesMod.FourierSpline — MethodFourierSpline(xs::Vector{Float64}, ys::Vector{Float64}, fs::Array{Float64, 3}, mband::Int; bctype::Union{String, Int}="not-a-knot", fitmethod::Int=1, fitflag::Bool=true)
Creates and fits a function of two variables, f(x, y), to a cubic spline
in the x-direction and a Fourier series in the y-direction. The y-direction
is assumed to be periodic.
## Arguments:
- `xs`: Vector of x-coordinates (length `mx`+1).
- `ys`: Vector of y-coordinates (length `my`+1, periodic direction).
- `fs`: 3D array of function values with dimensions (`mx`+1, `my`+1, `nqty`).
- `mband`: Number of Fourier modes (harmonics) to keep, from 0 to `mband`.
## Keyword Arguments:
- `bctype`: Boundary condition type for the cubic spline in `x`.
- 1: Natural spline (default)
- 2: Periodic spline
- 3: Extrapolated spline
- 4: "Not-a-knot" spline
- `fit_method`: Algorithm for computing Fourier coefficients.
- 1: Integration method (for non-uniform `y` grids).
- 2: Fast Fourier Transform (FFT) method (requires `length(ys)-1` to be a power of 2).
## Returns:
- A `FourierSpline` object ready for evaluation.JPEC.SplinesMod.ReadOnlyArray — TypeReadOnlyArray{T,N,A}A thin wrapper that provides a read-only view.
JPEC.SplinesMod.bicube_eval — Functionbicube_eval(bicube::BicubicSpline, x, y, derivs::Int=0) ## Arguments: - bicube: A BicubicSpline object. - x: A Float64 value or a vector of Float64 values representing the x-coordinates to evaluate the bicubic spline at. - y: A Float64 value or a vector of Float64 values representing the y-coordinates to evaluate the bicubic spline at. ## Returns: - If x and y are single Float64 values, returns a vector of Float64 values representing the function values at that (x,y) coordinate. - If x and y are vectors of Float64 values, returns a 3D array of Float64 values where each slice corresponds to the function values at the respective (x,y) coordinates in x and y.
JPEC.SplinesMod.fspline_eval — Functionfspline_eval(fspline::FourierSpline, x::Float64, y::Float64, derivs::Int=0)
Evaluates a fitted Fourier-Spline at given coordinates.
## Arguments:
- `fspline`: A `FourierSpline` object.
- `x`: A `Float64` x-coordinate.
- `y`: A `Float64` y-coordinate.
## Returns:
- If `x`, `y` are scalars: A tuple containing the function value(s) and any requested derivatives. If nqty=1, the results are scalars, otherwise they are vectors.JPEC.SplinesMod.fspline_eval — Functionfspline_eval(fspline::FourierSpline, xs::Vector{Float64}, ys::Vector{Float64}, derivs::Int=0)
Evaluates a fitted Fourier-Spline at given coordinates.
## Arguments:
- `fspline`: A `FourierSpline` object.
- `x`: A `Vector{Float64}` of x-coordinates.
- `y`: A `Vector{Float64}` of y-coordinates.
## Returns:
- If `x`, `y` are vectors: A tuple of 3D arrays for the function values and derivatives on the grid defined by `x` and `y`.JPEC.SplinesMod.parse_bctype — Methodparse_bctype(bctype)Internal helper to parse a boundary condition into a validated integer code.
Arguments:
bctype: The boundary condition as aStringorInt. Valid options are:"natural"or1"periodic"or2"extrap"or3"not-a-knot"or4
Returns:
- A validated
Intcode (1-4).
JPEC.SplinesMod.spline_eval! — Methodspline_eval!(f, spline, x; derivs=0, f1=nothing, f2=nothing, f3=nothing)In-place evaluation of CubicSpline at a single point x.
Arguments:
f: preallocated vector for the function values (length = spline.nqty).spline: the cubic spline object.x: Float64 input point.derivs: number of derivatives to evaluate (0–3).f1,f2,f3: optional preallocated vectors for first, second, third derivatives.
Results are written into f (and optionally f1, f2, f3).
JPEC.SplinesMod.spline_eval — Methodspline_eval(spline::CubicSpline{T}, x, derivs::Int=0) where {T<:Union{Float64, ComplexF64}} ## Arguments: - spline: A Spline object created by CubicSpline. - x: A Float64 value or a vector of Float64 values representing the x-coordinates to evaluate the spline at. ## Returns: - If x is a single Float64 value, returns a vector of Float64 values representing the function values at that x-coordinate. - If x is a vector of Float64 values, returns a matrix of Float64 values where each row corresponds to the function values at the respective x-coordinate in x. - Depending on the derivatives requested, it may return additional vectors for the first, second, or third derivatives.
JPEC.SplinesMod.spline_integrate! — Methodspline_integrate!(spline::CubicSpline{T}) where {T<:Union{Float64, ComplexF64}}
## Arguments:
- `spline`: A mutable `Spline` object".
## Returns:
- Nothing. Updates `spline._fsi` in place so that
`spline._fsi[i, :]` equals `∫_{xs[1]}^{xs[i]} f(x) dx` for each component.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
CubicSpline
JPEC.SplinesMod.CubicSpline — TypeCubicSpline(xs, fs; bctype="not-a-knot")
Arguments:
- `xs`: A vector of Float64 values representing the x-coordinates.
- `fs`: A vector or matrix of Float64/ComplexF64 values representing the function values at the x-coordinates.
Keyword Arguments:
- `bctype`: Boundary condition type for the cubic spline in `x`.
- 1: Natural spline (default)
- 2: Periodic spline
- 3: Extrapolated spline
- 4: "Not-a-knot" spline
Returns:
- A `CubicSpline` object containing the spline handle, x-coordinates, function values,
number of x-coordinates, number of quantities, and index of x position in the spline.spline_eval
JPEC.SplinesMod.spline_eval — Functionspline_eval(spline::CubicSpline{T}, x, derivs::Int=0) where {T<:Union{Float64, ComplexF64}} ## Arguments: - spline: A Spline object created by CubicSpline. - x: A Float64 value or a vector of Float64 values representing the x-coordinates to evaluate the spline at. ## Returns: - If x is a single Float64 value, returns a vector of Float64 values representing the function values at that x-coordinate. - If x is a vector of Float64 values, returns a matrix of Float64 values where each row corresponds to the function values at the respective x-coordinate in x. - Depending on the derivatives requested, it may return additional vectors for the first, second, or third derivatives.
BicubicSpline
JPEC.SplinesMod.BicubicSpline — TypeBicubicSpline(xs, ys, fs; bctypex::Union{String, Int}="not-a-knot", bctypey::Union{String, Int}="not-a-knot")
## Arguments:
- `xs`: A vector of Float64 values representing the x-coordinates.
- `ys`: A vector of Float64 values representing the y-coordinates.
- `fs`: A 3D array of Float64 values representing the function values at the (x,y) coordinates.
## Keyword Arguments:
- `bctypex`: An integer specifying the boundary condition type for x (Default is 4, not a knot)
- `bctypey`: An integer specifying the boundary condition type for y (Default is 4, not a knot)
## Returns:
- A `BicubicSpline` object containing the spline handle, x-coordinates, y-coordinates,
function values, number of x-coordinates, number of y-coordinates, number of quantities,
and boundary condition types.bicube_eval
JPEC.SplinesMod.bicube_eval — Functionbicube_eval(bicube::BicubicSpline, x, y, derivs::Int=0) ## Arguments: - bicube: A BicubicSpline object. - x: A Float64 value or a vector of Float64 values representing the x-coordinates to evaluate the bicubic spline at. - y: A Float64 value or a vector of Float64 values representing the y-coordinates to evaluate the bicubic spline at. ## Returns: - If x and y are single Float64 values, returns a vector of Float64 values representing the function values at that (x,y) coordinate. - If x and y are vectors of Float64 values, returns a 3D array of Float64 values where each slice corresponds to the function values at the respective (x,y) coordinates in x and y.
FourierSpline
JPEC.SplinesMod.FourierSpline — TypeFourierSpline(xs::Vector{Float64}, ys::Vector{Float64}, fs::Array{Float64, 3}, mband::Int; bctype::Union{String, Int}="not-a-knot", fitmethod::Int=1, fitflag::Bool=true)
Creates and fits a function of two variables, f(x, y), to a cubic spline
in the x-direction and a Fourier series in the y-direction. The y-direction
is assumed to be periodic.
## Arguments:
- `xs`: Vector of x-coordinates (length `mx`+1).
- `ys`: Vector of y-coordinates (length `my`+1, periodic direction).
- `fs`: 3D array of function values with dimensions (`mx`+1, `my`+1, `nqty`).
- `mband`: Number of Fourier modes (harmonics) to keep, from 0 to `mband`.
## Keyword Arguments:
- `bctype`: Boundary condition type for the cubic spline in `x`.
- 1: Natural spline (default)
- 2: Periodic spline
- 3: Extrapolated spline
- 4: "Not-a-knot" spline
- `fit_method`: Algorithm for computing Fourier coefficients.
- 1: Integration method (for non-uniform `y` grids).
- 2: Fast Fourier Transform (FFT) method (requires `length(ys)-1` to be a power of 2).
## Returns:
- A `FourierSpline` object ready for evaluation.fspline_eval
JPEC.SplinesMod.fspline_eval — Functionfspline_eval(fspline::FourierSpline, x::Float64, y::Float64, derivs::Int=0)
Evaluates a fitted Fourier-Spline at given coordinates.
## Arguments:
- `fspline`: A `FourierSpline` object.
- `x`: A `Float64` x-coordinate.
- `y`: A `Float64` y-coordinate.
## Returns:
- If `x`, `y` are scalars: A tuple containing the function value(s) and any requested derivatives. If nqty=1, the results are scalars, otherwise they are vectors.fspline_eval(fspline::FourierSpline, xs::Vector{Float64}, ys::Vector{Float64}, derivs::Int=0)
Evaluates a fitted Fourier-Spline at given coordinates.
## Arguments:
- `fspline`: A `FourierSpline` object.
- `x`: A `Vector{Float64}` of x-coordinates.
- `y`: A `Vector{Float64}` of y-coordinates.
## Returns:
- If `x`, `y` are vectors: A tuple of 3D arrays for the function values and derivatives on the grid defined by `x` and `y`.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.CubicSpline(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.BicubicSpline(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)