FFT#

class fridom.framework.grid.cartesian.fft.FFT(periodic: tuple[bool])[source]#

Bases: object

Class for performing fourier transforms on a cartesian grid.

Description#

Model grids that have periodic boundary conditions in some directions, and non-periodic boundary conditions in other directions, require a combination of fast fourier transforms and discrete cosine transforms. This class provides a method to transform an array from physical space to spectral space and back. For the discrete cosine transform, the type 2 transform is used. This means that the variable must be located at the cell centers in that direction.

Parameters#

periodictuple[bool]

A list of booleans that indicate whether the axis is periodic. If True, the axis is periodic, if False, the axis is non-periodic.

Examples#

import numpy as np
from fridom.framework.grid.cartesian import FFT
fft = FFT(periodic=(True, True, False))
u = np.random.rand(*(32, 32, 8))
v = fft.forward(u)
w = fft.backward(v).real
assert np.allclose(u, w)
__init__(periodic: tuple[bool]) None[source]#

Methods

__init__(periodic)

backward(u_hat[, axes, bc_types, positions])

Backward transform from spectral space to physical space.

forward(u[, axes, bc_types, positions])

Forward transform from physical space to spectral space.

get_freq(shape, dx)

Get the frequencies for the given shape and dx.

get_freq(shape: tuple[int], dx: tuple[float]) tuple[ndarray][source]#

Get the frequencies for the given shape and dx.

Description#

This method calculates the frequencies for the given shape and dx. The returned frequencies could be used to construct wavenumber meshgrids.

Parameters#

shapetuple[int]

The global shape (number of grid points in each direction).

dxtuple[float]

The grid spacing in each direction.

Returns#

tuple[np.ndarray]

The frequencies in each direction.

Examples#

import numpy as np
from fridom.framework.grid.cartesian import FFT
fft = FFT(periodic=(True, True, False))
shape = (32, 32, 8)  # Number of grid points in x,y,z
dx = (0.1, 0.1, 0.1)  # Grid spacing in x,y,z
kx, ky, kz = fft.get_freq(shape, dx)
KX, KY, KZ = np.meshgrid(kx, ky, kz, indexing='ij')
forward(u: ndarray, axes: list[int] | None = None, bc_types: tuple[BCType] | None = None, positions: tuple[AxisPosition] | None = None) ndarray[source]#

Forward transform from physical space to spectral space.

Parameters#

unp.ndarray

The array to transform from physical space to spectral space.

axeslist[int] | None

The axes to transform. If None, all axes are transformed.

bc_typestuple[fr.grid.BCType] | None

The type of boundary conditions for each axis.

positionstuple[fr.grid.AxisPosition] | None

The position of the variable in each direction.

Returns#

np.ndarray

The transformed array in spectral space. If all dimensions are periodic, the obtained array is real, else it is complex.

backward(u_hat: ndarray, axes: list[int] | None = None, bc_types: tuple[BCType] | None = None, positions: tuple[AxisPosition] | None = None) ndarray[source]#

Backward transform from spectral space to physical space.

Parameters#

u_hatnp.ndarray

The array to transform from spectral space to physical space.

axeslist[int] | None

The axes to transform. If None, all axes are transformed.

bc_typestuple[fr.grid.BCType] | None

The type of boundary conditions for each axis.

positionstuple[fr.grid.AxisPosition] | None

The position of the variable in each direction.

Returns#

np.ndarray

The transformed array in physical space.