Finite Difference Suite
Einstein.FiniteDifferenceSuite.LocalBarycentricInterpolation
Einstein.FiniteDifferenceSuite.fdm_boundary_weights
Einstein.FiniteDifferenceSuite.fdm_boundary_width
Einstein.FiniteDifferenceSuite.fdm_central_weights
Einstein.FiniteDifferenceSuite.fdm_central_width
Einstein.FiniteDifferenceSuite.fdm_derivative_operator
Einstein.FiniteDifferenceSuite.fdm_dissipation_boundary_weights
Einstein.FiniteDifferenceSuite.fdm_dissipation_boundary_width
Einstein.FiniteDifferenceSuite.fdm_dissipation_operator
Einstein.FiniteDifferenceSuite.fdm_dissipation_order
Einstein.FiniteDifferenceSuite.fdm_dissipation_weights
Einstein.FiniteDifferenceSuite.fdm_dissipation_width
Einstein.FiniteDifferenceSuite.fdm_extrapolation_weights
Einstein.FiniteDifferenceSuite.fdm_hermite_boundary_weights
Einstein.FiniteDifferenceSuite.fdm_hermite_boundary_width
Einstein.FiniteDifferenceSuite.fdm_hermite_weights
Einstein.FiniteDifferenceSuite.fdm_hermite_width
Einstein.FiniteDifferenceSuite.fdm_integrate_simpson
Einstein.FiniteDifferenceSuite.fdm_operator_matrix
Einstein.FiniteDifferenceSuite.fdm_weights_fornberg
Einstein.FiniteDifferenceSuite.LocalBarycentricInterpolation
— TypeLocalBarycentricInterpolation(points, values; degree=4)
Construct a local barycentric Lagrange interpolant for equispaced data points.
Creates a polynomial approximation of degree degree
using the data values
at uniformly spaced points points
. The interpolation is performed locally using degree + 1
points nearest to the evaluation point.
Arguments
points::StepRangeLen{TF}
: Equispaced points for interpolationvalues::AbstractVector{TF}
: Function values at the pointsdegree::Integer=4
: Degree of the local polynomial interpolant
Returns
- An interpolant function that can be evaluated at any point within
[minimum(points), maximum(points)]
Notes
- Requires
length(points) >= degree + 1
points
andvalues
must have the same length- Uses barycentric Lagrange interpolation for numerical stability
References
Einstein.FiniteDifferenceSuite.fdm_boundary_weights
— Methodfdm_boundary_weights([TR=Rational{TI}], derivative_order::TI, accuracy_order::TI) where {TR<:Real, TI<:Integer}
Generate finite difference coefficients for shifted boundary conditions.
Arguments
derivative_order::Integer
: The order of the derivative to approximateaccuracy_order::Integer
: The desired order of accuracy
Returns
Tuple of left and right shifted boundary finite difference coefficients The coefficients are stored in a matrix with the rows representing the different grid points. The rows are ordered from the leftmost grid point to the rightmost grid point.
Einstein.FiniteDifferenceSuite.fdm_boundary_width
— Methodfdm_boundary_width(derivative_order::Integer, accuracy_order::Integer)
Calculate the number of coefficients needed for shifted boundary FDM stencil.
Einstein.FiniteDifferenceSuite.fdm_central_weights
— Methodfdm_central_weights([TR=Rational{TI}], derivative_order::TI, accuracy_order::TI) where {TR<:Real, TI<:Integer}
Generate central finite difference coefficients for a given derivative and accuracy order.
Arguments
derivative_order::Integer
: The order of the derivative to approximateaccuracy_order::Integer
: The desired order of accuracy (must be even)
Returns
Vector of rational coefficients for the finite difference stencil
Einstein.FiniteDifferenceSuite.fdm_central_width
— Methodfdm_central_width(derivative_order::Integer, accuracy_order::Integer)
Calculate the number of coefficients needed for central FDM stencil.
References
Einstein.FiniteDifferenceSuite.fdm_derivative_operator
— Methodfdm_derivative_operator([TR=Float64], derivative_order::Integer, accuracy_order::Integer, dx::TR) -> FiniteDifferenceDerivativeOperator{TR}
Create a finite difference derivative operator with specified derivative and accuracy orders.
Arguments
TR
: The element type of the operatorderivative_order::Integer
: The order of the derivativeaccuracy_order::Integer
: The order of accuracydx::TR
: The grid spacing
Einstein.FiniteDifferenceSuite.fdm_dissipation_boundary_weights
— Methodfdm_dissipation_boundary_weights([TR=Rational{Int}], dissipation_order::Integer)
Calculate the weights for Kreiss-Oliger dissipation of given order at the boundary [B+08].
Einstein.FiniteDifferenceSuite.fdm_dissipation_boundary_width
— Methodfdm_dissipation_boundary_width(dissipation_order::Integer)
Calculate the number of coefficients needed for Kreiss-Oliger dissipation (boundary).
Einstein.FiniteDifferenceSuite.fdm_dissipation_operator
— Methodfdm_dissipation_operator([TR=Float64], dissipation_order::TI, σ::TR, dx::TR) -> FiniteDifferenceDerivativeOperator{TR}
Create a finite difference dissipation operator with specified dissipation order.
Arguments
TR
: The element type of the operatordissipation_order::Integer
: The order of the dissipation operatorσ::TR
: The dissipation coefficientdx::TR
: The grid spacing
Einstein.FiniteDifferenceSuite.fdm_dissipation_order
— Methodfdm_dissipation_order(accuracy_order::Integer)
Calculate the order of dissipation needed for a given finite difference accuracy order [B+08]. For a scheme of accuracy order 2r-2, returns dissipation order 2r.
Einstein.FiniteDifferenceSuite.fdm_dissipation_weights
— Methodfdm_dissipation_weights([TR=Rational{Int}], dissipation_order::Integer)
Calculate the weights for Kreiss-Oliger dissipation of given order [B+08].
Einstein.FiniteDifferenceSuite.fdm_dissipation_width
— Methodfdm_dissipation_width(dissipation_order::Integer)
Calculate the number of coefficients needed for Kreiss-Oliger dissipation (interior).
Einstein.FiniteDifferenceSuite.fdm_extrapolation_weights
— Methodfdm_extrapolation_weights(extrapolation_order::Int, direction::Symbol)
Generate weights for left or right extrapolation.
Arguments
extrapolation_order::Int
: Order of extrapolationdirection::Symbol
: Direction of extrapolation (:left or :right)
Returns
Vector of Integer coefficients for the extrapolation weights.
Einstein.FiniteDifferenceSuite.fdm_hermite_boundary_weights
— Methodfdm_hermite_boundary_weights([TR=Rational{TI}], derivative_order::TI, accuracy_order::TI) where {TR<:Real, TI<:Integer}
Generate Hermite-type finite difference coefficients for boundary conditions.
Einstein.FiniteDifferenceSuite.fdm_hermite_boundary_width
— Methodfdm_hermite_boundary_width(derivative_order::Integer, accuracy_order::Integer)
Calculate the number of coefficients needed for Hermite FDM stencil on boundary.
Einstein.FiniteDifferenceSuite.fdm_hermite_weights
— Methodfdm_hermite_weights([TR=Rational{TI}], derivative_order::TI, accuracy_order::TI) where {TR<:Real, TI<:Integer}
Generate Hermite-type finite difference coefficients that include function value and derivative information.
Arguments
derivative_order::Integer
: The order of the derivative to approximate (must be ≥ 2)accuracy_order::Integer
: The desired order of accuracy- For derivativeorder 2,3,6,7,10,11...: accuracyorder must be 4,8,12...
- For derivativeorder 4,5,8,9,12...: accuracyorder must be 2,6,10...
Returns
Vector of rational coefficients for the Hermite-type finite difference stencil
Einstein.FiniteDifferenceSuite.fdm_hermite_width
— Methodfdm_hermite_width(derivative_order::Integer, accuracy_order::Integer)
Calculate the number of coefficients needed for Hermite FDM stencil.
References
Einstein.FiniteDifferenceSuite.fdm_integrate_simpson
— Methodfdm_integrate_simpson(f::AbstractVector{T}, dx::T) where {T<:AbstractFloat}
Integrate a function f
using Simpson's rule, given the grid spacing dx
.
Einstein.FiniteDifferenceSuite.fdm_operator_matrix
— Methodfdm_operator_matrix(op::AbstractFiniteDifferenceOperator{TR}; boundary::Bool=false, transpose::Bool=false) -> BandedMatrix{TR}
Create a banded matrix representation of the finite difference operator.
Arguments
op::AbstractFiniteDifferenceOperator{TR}
: The finite difference operatorboundary::Bool=true
: Whether to include boundary weightstranspose::Bool=false
: Whether to transpose the matrix
Einstein.FiniteDifferenceSuite.fdm_weights_fornberg
— Methodfdm_weights_fornberg([TR=Float64], order::Integer, x0::Real, x::AbstractVector;
hermite::Bool=false)
Calculate finite difference weights for arbitrary-order derivatives using the Fornberg algorithm. Taken from SciML/MethodOfLines.jl.
Arguments
TR
: Type parameter for the weights (defaults to type of x0)order
: Order of the derivative to approximatex0
: Point at which to approximate the derivativex
: Grid points to use in the approximationhermite
: Whether to include first derivative values (Hermite finite differences)
Returns
If hermite == false
:
Vector{TR}
: Weights for standard finite differences
If hermite == true
:
Tuple{Vector{TR}, Vector{TR}}
: Weights for Hermite finite differences
Requirements
- For standard finite differences: N > order
- For Hermite finite differences: N > order/2 + 1
where N is the length of x
Examples
# Standard central difference for first derivative
x = [-1.0, 0.0, 1.0]
w = fdm_weights_fornberg(1, 0.0, x)
# Returns approximately [-0.5, 0.0, 0.5]
# Forward difference for second derivative
x = [0.0, 1.0, 2.0, 3.0]
w = fdm_weights_fornberg(2, 0.0, x)
# Hermite finite difference for third derivative
x = [-1.0, 0.0, 1.0]
w_f, w_d = fdm_weights_fornberg(3, 0.0, x, hermite=true)
References