FFT Computations
Source Code: FFT
FFT.py
1# Copyright (C) 2025 Sukanta Basu
2#
3# This program is free software: you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation, either version 3 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16"""
17File: FFT.py
18==================
19
20:Author: Sukanta Basu
21:AI Assistance: Claude Code (Anthropic) and Codex (OpenAI) are used for documentation,
22 code restructuring, and performance optimization
23:Date: 2025-4-3
24:Description: performs FFT-based operations using JAX
25"""
26
27# ============================================================
28# Imports
29# ============================================================
30
31import jax
32import jax.numpy as jnp
33
34from ..initialization.Preprocess import Constant
35mx, my, nx_rfft, ny_rfft, mx_rfft, my_rfft = Constant()
36
37
38# ============================================================
39# Compute real fft of a variable F
40# ============================================================
41
42
43@jax.jit
44def FFT(F):
45 """
46 Parameters:
47 -----------
48 F : ndarray
49 3D input field with shape (nx, ny, nz)
50
51 Returns:
52 --------
53 F_fft : ndarray in Jax format
54 rfft2 of a field
55 """
56
57 F_fft = jnp.fft.rfft2(F, axes=(0, 1))
58
59 return F_fft
60
61
62# ============================================================
63# Compute real fft of a padded variable F_pad
64# ============================================================
65
66
67@jax.jit
68def FFT_pad(F_pad):
69 """
70 Parameters:
71 -----------
72 F_pad : ndarray with shape (mx=1.5*nx, my=1.5*ny, nz)
73 3D input field
74
75 Returns:
76 --------
77 F_pad_fft : ndarray in Jax format
78 rfft2 of a padded field
79 """
80
81 # Compute 2D real FFT along x and y axes
82 F_pad_fft = jnp.fft.rfft2(F_pad, axes=(0, 1))
83
84 return F_pad_fft