neuralop.losses.differentiation
.FourierDiff
- class neuralop.losses.differentiation.FourierDiff(dim, L=None, use_fc=False, fc_degree=4, fc_n_additional_pts=50, low_pass_filter_ratio=None)[source]
A unified class for computing Fourier/spectral derivatives in 1D, 2D, 3D.
This class provides comprehensive methods for computing derivatives using Fourier/spectral methods with support for both periodic and non-periodic functions through Fourier continuation: - Periodic functions: Direct Fourier differentiation using FFT - Non-periodic functions: Fourier continuation (FC) is used to extend functions to larger domain
on which the functions are periodic before applying Fourier differentiation with FFT.
The class also provides gradient, divergence, curl, and Laplacian operations.
- Parameters:
- dimint
Dimension of the input field. Must be 1, 2, or 3.
- Lfloat or tuple, optional
Length of the domain for Fourier differentiation. By default 2*pi for each dimension.
- use_fcstr, optional
Whether to use Fourier continuation for non-periodic functions. Options: False (no FC), ‘Legendre’, ‘Gram’. By default False.
- fc_degreeint, optional
Degree of the Fourier continuation polynomial matching. This is the number of matching points on the left and right boundaries used for the Fourier continuation procedure. By default 4.
- fc_n_additional_ptsint, optional
Number of additional points to add with the Fourier continuation layer. This extends the domain to handle non-periodic functions. By default 50.
- low_pass_filter_ratiofloat, optional
If not None, apply a low-pass filter to the Fourier coefficients to reduce high-frequency noise. Should be between 0 and 1. By default None.
- Available Methods
- —————-
- Derivative Methods:
- - dx(u, order=1): Compute derivative with respect to x
- - dy(u, order=1): Compute derivative with respect to y (2D/3D only)
- - dz(u, order=1): Compute derivative with respect to z (3D only)
- - derivative(u, order): Compute derivative with order tuple (e.g., (1,0) for ∂/∂x)
- Vector Calculus Operators:
- - laplacian(u): Compute the Laplacian ∇²f
- - gradient(u): Compute the gradient ∇f (returns vector field)
- - divergence(u): Compute the divergence ∇·u (for vector fields)
- - curl(u): Compute the curl ∇×u (for vector fields, 2D/3D only)
Methods
compute_multiple_derivatives
(u, derivatives)Compute multiple derivatives in a single FFT/IFFT call for better performance.
curl
(u)Compute the curl ∇×u (for vector fields, 2D/3D only).
derivative
(u, order)Compute Fourier derivative of a given tensor.
divergence
(u)Compute the divergence ∇·u (for vector fields).
dx
(u[, order])Compute derivative with respect to x.
dy
(u[, order])Compute derivative with respect to y (2D/3D only).
dz
(u[, order])Compute derivative with respect to z (3D only).
gradient
(u)Compute the gradient ∇f (returns vector field).
laplacian
(u)Compute the Laplacian ∇²f.
partial
(u[, direction, order])Compute partial Fourier derivative along a specific direction.
Examples
>>> # 1D Fourier derivatives >>> x = torch.linspace(0, 2*torch.pi, 100) >>> u = torch.sin(x) >>> fd1d = FourierDiff(dim=1, L=2*torch.pi, use_fc=False) >>> du_dx = fd1d.dx(u) # First derivative >>> d2u_dx2 = fd1d.dx(u, order=2) # Second derivative >>> >>> # 2D Fourier derivatives >>> fd2d = FourierDiff(dim=2, L=(2*torch.pi, 2*torch.pi), use_fc=False) >>> x = torch.linspace(0, 2*torch.pi, 50) >>> y = torch.linspace(0, 2*torch.pi, 50) >>> X, Y = torch.meshgrid(x, y, indexing='ij') >>> u = torch.sin(X) * torch.cos(Y) >>> du_dx = fd2d.dx(u) >>> du_dy = fd2d.dy(u) >>> grad = fd2d.gradient(u) # Returns [du_dx, du_dy] >>> >>> # 3D Fourier derivatives >>> fd3d = FourierDiff(dim=3, L=(2*torch.pi, 2*torch.pi, 2*torch.pi), use_fc=False) >>> x = torch.linspace(0, 2*torch.pi, 20) >>> y = torch.linspace(0, 2*torch.pi, 20) >>> z = torch.linspace(0, 2*torch.pi, 20) >>> X, Y, Z = torch.meshgrid(x, y, z, indexing='ij') >>> u = torch.sin(X) * torch.cos(Y) * torch.sin(Z) # 3D scalar field >>> du_dx = fd3d.dx(u) >>> du_dy = fd3d.dy(u) >>> du_dz = fd3d.dz(u) >>> laplacian = fd3d.laplacian(u) >>> >>> # Vector field operations >>> vx = torch.sin(X) * torch.cos(Y) * torch.sin(Z) >>> vy = torch.cos(X) * torch.sin(Y) * torch.cos(Z) >>> vz = torch.sin(X) * torch.sin(Y) * torch.cos(Z) >>> v = torch.stack([vx, vy, vz], dim=-4) >>> div_v = fd3d.divergence(v) >>> curl_v = fd3d.curl(v)
- compute_multiple_derivatives(u, derivatives)[source]
Compute multiple derivatives in a single FFT/IFFT call for better performance.
- Parameters:
- utorch.Tensor
Input tensor.
- derivativeslist
List of derivative specifications: - 1D: list of int (orders) - 2D: list of tuples (order_x, order_y) - 3D: list of tuples (order_x, order_y, order_z)
- Returns:
- list of torch.Tensor
List of computed derivatives in the same order as derivatives input
- derivative(u, order)[source]
Compute Fourier derivative of a given tensor.
- Parameters:
- utorch.Tensor
Input tensor
- ordertuple
Derivative orders: - 1D: (order_x,) - 2D: (order_x, order_y) - 3D: (order_x, order_y, order_z)
- Returns:
- torch.Tensor
The derivative of the input tensor
- partial(u, direction='x', order=1)[source]
Compute partial Fourier derivative along a specific direction.
- Parameters:
- utorch.Tensor
Input tensor
- directionstr, optional
Direction along which to compute the derivative, by default ‘x’ Options: ‘x’, ‘y’ (2D/3D only), ‘z’ (3D only)
- orderint, optional
Order of the derivative, by default 1
- Returns:
- torch.Tensor
The partial derivative of the input tensor
- dx(u, order=1)[source]
Compute derivative with respect to x.
- dy(u, order=1)[source]
Compute derivative with respect to y (2D/3D only).
- dz(u, order=1)[source]
Compute derivative with respect to z (3D only).
- laplacian(u)[source]
Compute the Laplacian ∇²f.
- gradient(u)[source]
Compute the gradient ∇f (returns vector field).
- divergence(u)[source]
Compute the divergence ∇·u (for vector fields).
- curl(u)[source]
Compute the curl ∇×u (for vector fields, 2D/3D only).