A Julia package for numerical computation using collocation grids and spectral methods.
CollocationGrids.jl provides a comprehensive framework for working with various types of collocation grids commonly used in numerical analysis and spectral methods. The package supports multiple grid types, derivative calculations, interpolation, and multi-dimensional operations.
- Multiple Grid Types: Support for Chebyshev, Fourier, semi-infinite, and custom grids
- Multi-dimensional Grids: Tensor product grids for multi-dimensional problems
- Spectral Differentiation: Accurate computation of derivatives using spectral methods
- Interpolation: Cardinal function-based interpolation
- Grid Transformations: Linear rescaling and coordinate transformations
using Pkg
Pkg.add(url = "https://github.com/inokawazu/CollocationGrids")using CollocationGrids
# Create a Chebyshev-Lobatto grid with 10 points
grid = ChebyshevLobattoGrid{Float64}(10)
# Get grid points
points = collect(gridpoints(grid))
# Compute first derivative matrix
D1 = derivative_matrix(grid, 1)
# Interpolate a function
f(x) = sin(x)
coefs = f.(points)
interpolated_value = interpolate(grid, coefs, 0.5)ChebyshevLobattoGrid{T}(N): Chebyshev-Lobatto grid with N+1 points including boundariesChebyshevInteriorGrid{T}(N): Chebyshev grid with N interior points (excludes boundaries)FourierGrid{T}(N): Fourier grid with 2N points for periodic problemsSemiInfiniteGrid: Grid for semi-infinite domainsLinearRescaleGrid: Linearly rescaled version of any grid
# Create a 2D grid (tensor product)
grid1 = ChebyshevLobattoGrid{Float64}(10)
grid2 = FourierGrid{Float64}(8)
multigrid = MultiGrid(grid1, grid2)
# Get 2D derivative matrices
Dx = derivative_matrix(multigrid, (1, 0)) # ∂/∂x
Dy = derivative_matrix(multigrid, (0, 1)) # ∂/∂y
Dxy = derivative_matrix(multigrid, (1, 1)) # ∂²/∂x∂y# Get grid points
points = gridpoints(grid)
# Get domain boundaries
bounds = boundaries(grid)
# Get specific grid point
point_i = gridpoint(grid, i)# Derivative matrix for order-k derivatives
Dk = derivative_matrix(grid, k)
# Specific derivative element
d_ij = derivative(grid, i, j, order)
# For multi-dimensional grids
derivative_matrices = derivative_array_matrix(multigrid, max_order)# Cardinal function at point x for basis function j
card_val = cardinal(grid, j, x)
# Interpolate using coefficients
result = interpolate(grid, coefficients, x)# Lobatto grid (includes boundaries at ±1)
clob = ChebyshevLobattoGrid{Float64}(16)
# Interior grid (excludes boundaries)
cint = ChebyshevInteriorGrid{Float64}(16)# For periodic functions on [0, 2π]
fourier = FourierGrid{Float64}(32)# Rescale Chebyshev grid to [0, 5]
base_grid = ChebyshevLobattoGrid{Float64}(20)
custom_grid = LinearRescaleGrid(base_grid, 0.0, 5.0)
# Or use the convenience function
rescaler = linear_grid_rescale(0.0, 5.0)
custom_grid = rescaler(base_grid)# Grid for problems on [0, ∞)
base = ChebyshevLobattoGrid{Float64}(25)
semi = SemiInfiniteGrid(base, 1.0) # L = 1.0 is the mapping parameter# Get coordinate operators for a grid
X = coordinate_operators(grid) # Returns diagonal matrix
x_vec = coordinate_vectors(grid) # Returns vector
# For multi-dimensional grids
X, Y = coordinate_operators(multigrid) # Tuple of operators# Create operator from function
f(x) = x^2 + sin(x)
F_op = function_operator(f, grid) # Diagonal matrix
f_vec = function_vector(f, grid) # Vector evaluation# Get all derivative matrices up to given order
max_order = 3
all_derivatives = derivative_array_matrix(multigrid, max_order)All grids are parameterized by element type T (typically Float64 or BigFloat):
# High precision computation
grid_hp = ChebyshevLobattoGrid{BigFloat}(50)
# Standard precision
grid_std = ChebyshevLobattoGrid{Float64}(50)