neuralop.losses.differentiation.FiniteDiff

class neuralop.losses.differentiation.FiniteDiff(dim, h=1.0, periodic_in_x=True, periodic_in_y=True, periodic_in_z=True)[source]

A unified class for computing finite differences in 1D, 2D, or 3D.

This class provides comprehensive methods for computing derivatives using finite differences with support for both periodic and non-periodic boundary conditions.

It implements the following high-order finite difference schemes: - Interior points: Second-order central differences for optimal accuracy - Periodic boundaries: Uses torch.roll for seamless periodic wrapping. - Non-periodic boundaries: Uses third-order one-sided differences at boundary points.

Parameters:
dimint

Dimension of the input field. Must be 1, 2, or 3.

hfloat or tuple, optional

Grid spacing(s) for finite difference calculations, by default 1.0. - For 1D: single float or tuple with one element - For 2D: tuple (h_x, h_y) or single float for uniform spacing - For 3D: tuple (h_x, h_y, h_z) or single float for uniform spacing

periodic_in_xbool, optional

Whether to use periodic boundary conditions in x-direction, by default True. When True, uses torch.roll for efficient periodic wrapping. When False, uses high-order one-sided differences at boundaries.

periodic_in_ybool, optional

Whether to use periodic boundary conditions in y-direction, by default True. When True, uses torch.roll for efficient periodic wrapping. When False, uses high-order one-sided differences at boundaries. Only used for 2D and 3D fields.

periodic_in_zbool, optional

Whether to use periodic boundary conditions in z-direction, by default True. When True, uses torch.roll for efficient periodic wrapping. When False, uses high-order one-sided differences at boundaries. Only used for 3D fields.

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)
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

curl(u)

Compute the curl ∇×u for vector fields.

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.

dz(u[, order])

Compute derivative with respect to z.

gradient(u)

Compute the gradient ∇f for scalar fields.

laplacian(u)

Compute the Laplacian ∇²f.

Examples

>>> # 1D finite differences
>>> x = torch.linspace(0, 2*torch.pi, 100)
>>> u = torch.sin(x)
>>> fd1d = FiniteDiff(dim=1, h=0.1, periodic_in_x=True)
>>> du_dx = fd1d.dx(u)  # First derivative
>>> d2u_dx2 = fd1d.dx(u, order=2)  # Second derivative
>>>
>>> # 2D finite differences
>>> fd2d = FiniteDiff(dim=2, h=(0.1, 0.1), periodic_in_x=True, periodic_in_y=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 finite differences
>>> fd3d = FiniteDiff(dim=3, h=(0.1, 0.1, 0.1), periodic_in_x=True, periodic_in_y=True, periodic_in_z=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)  # Sum of all second derivatives
>>>
>>> # 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)  # 3D vector field
>>> div_v = fd3d.divergence(v)  # Scalar field
>>> curl_v = fd3d.curl(v)  # Vector field
dx(u, order=1)[source]

Compute derivative with respect to x.

Parameters:
utorch.Tensor

Input tensor

orderint, optional

Order of the derivative, by default 1

Returns:
torch.Tensor

Derivative with respect to x

dy(u, order=1)[source]

Compute derivative with respect to y.

Parameters:
utorch.Tensor

Input tensor

orderint, optional

Order of the derivative, by default 1

Returns:
torch.Tensor

Derivative with respect to y

dz(u, order=1)[source]

Compute derivative with respect to z.

Parameters:
utorch.Tensor

Input tensor

orderint, optional

Order of the derivative, by default 1

Returns:
torch.Tensor

Derivative with respect to z

laplacian(u)[source]

Compute the Laplacian ∇²f.

Parameters:
utorch.Tensor

Input tensor

Returns:
torch.Tensor

The Laplacian of the input tensor

gradient(u)[source]

Compute the gradient ∇f for scalar fields.

Parameters:
utorch.Tensor

Input scalar field

Returns:
torch.Tensor

The gradient of the scalar field

divergence(u)[source]

Compute the divergence ∇·u for vector fields.

Parameters:
utorch.Tensor

Input vector field

Returns:
torch.Tensor

The divergence of the vector field

curl(u)[source]

Compute the curl ∇×u for vector fields.

Parameters:
utorch.Tensor

Input vector field

Returns:
torch.Tensor

The curl of the vector field