Spectral Derivative Operators¶
SpectralDerivative1D
¶
Bases: Module
1D Spectral derivative operator using the Fast Fourier Transform (FFT).
This class provides methods to differentiate fields periodically using spectral accuracy. It leverages the property that the Fourier Transform (FT) turns differentiation into multiplication by the wavenumber vector.
Mathematical Formulation:¶
Given a periodic field u(x) on a domain of length L with N points: u(x) = Σ u_hat(k) * exp(i * k * x)
The n-th derivative is
d^n u / dx^n = Σ (i * k)^n * u_hat(k) * exp(i * k * x)
where k are the discrete wavenumbers: k = 2 * pi * n / L.
grid : FourierGrid1D
The 1D grid object containing wavenumbers k [N].
Source code in spectraldiffx/_src/fourier/operators.py
Functions¶
__call__(u, order=1, spectral=False)
¶
Compute the n-th derivative of a field.
Parameters:¶
u : Array [N] The input field. If spectral=False, this is physical space. If spectral=True, this is complex Fourier coefficients. order : int, optional The order of the derivative (1=first, 2=second, etc.). Default is 1. spectral : bool, optional Whether the input 'u' is already in Fourier space. Default is False.
Returns:¶
du_dx : Array [N] The n-th derivative in physical space.
Source code in spectraldiffx/_src/fourier/operators.py
gradient(u, spectral=False)
¶
Compute the first derivative (gradient) du/dx.
Parameters:¶
u : Array [N] Input field. spectral : bool, optional If True, treats u as Fourier coefficients.
Source code in spectraldiffx/_src/fourier/operators.py
laplacian(u, spectral=False)
¶
Compute the second derivative (Laplacian) d^2u/dx^2.
Operation in Fourier Space
lap_hat = -(k^2) * u_hat
Source code in spectraldiffx/_src/fourier/operators.py
apply_dealias(u, spectral=False)
¶
Apply the 2/3 dealiasing rule mask to a field.
This zeros out the top 1/3 of the spectrum to prevent aliasing errors in nonlinear products (e.g., u * du/dx).
Source code in spectraldiffx/_src/fourier/operators.py
SpectralDerivative2D
¶
Bases: Module
2D Spectral derivative operators for doubly periodic rectangular domains.
Mathematical Formulation:¶
For a 2D field u(x, y): u(x, y) = ΣΣ u_hat(kx, ky) * exp(i * (kxx + kyy))
Partial derivatives
∂u/∂x ↔ ikx * u_hat ∂u/∂y ↔ iky * u_hat
Gradient vector: ∇u = (∂u/∂x, ∂u/∂y) Laplacian: ∇^2 u = ∂^2u/∂x^2 + ∂^2u/∂y^2 ↔ -(kx^2 + ky^2) * u_hat
Source code in spectraldiffx/_src/fourier/operators.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
Functions¶
gradient(u, spectral=False)
¶
Compute the gradient vector [du/dx, du/dy].
Source code in spectraldiffx/_src/fourier/operators.py
divergence(vx, vy, spectral=False)
¶
Compute the divergence of a 2D vector field: div(V) = ∂vx/∂x + ∂vy/∂y.
Source code in spectraldiffx/_src/fourier/operators.py
curl(vx, vy, spectral=False)
¶
Compute the 2D scalar curl (vorticity): ζ = ∂vy/∂x - ∂vx/∂y.
Source code in spectraldiffx/_src/fourier/operators.py
laplacian(u, spectral=False)
¶
Compute the 2D Laplacian: ∇^2 u = ∂^2u/∂x^2 + ∂^2u/∂y^2.
Source code in spectraldiffx/_src/fourier/operators.py
apply_dealias(u, spectral=False)
¶
Apply the 2D spectral dealiasing filter mask [Ny, Nx].
Source code in spectraldiffx/_src/fourier/operators.py
project_vector(vx, vy)
¶
Perform Leray projection to extract the divergence-free component.
Solves the decomposition: V = V_solenoidal + V_irrotational where div(V_solenoidal) = 0 and curl(V_irrotational) = 0.
Physics:¶
In incompressible flows, we project the velocity onto the divergence-free manifold by solving for a scalar potential φ: div(V - grad(φ)) = 0 => ∇^2 φ = div(V)
Then: V_solenoidal = V - grad(φ)
Source code in spectraldiffx/_src/fourier/operators.py
advection_scalar(vx, vy, q)
¶
Compute scalar advection (u·∇)q using the pseudo-spectral method.
This method computes derivatives in Fourier space for accuracy, then transforms back to physical space to perform the multiplication.
Out = vx * ∂q/∂x + vy * ∂q/∂y
Source code in spectraldiffx/_src/fourier/operators.py
SpectralDerivative3D
¶
Bases: Module
3D Spectral derivative operators for triply periodic domains.
Mathematical Framework:¶
Field shapes: (Nz, Ny, Nx) following 'ij' indexing. Wavenumbers: (kz, ky, kx).
Gradient vector: ∇u = (∂u/∂z, ∂u/∂y, ∂u/∂x)
Source code in spectraldiffx/_src/fourier/operators.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
Functions¶
gradient(u, spectral=False)
¶
Compute the 3D gradient vector field.
Source code in spectraldiffx/_src/fourier/operators.py
divergence(vz, vy, vx, spectral=False)
¶
Compute 3D divergence: ∂vz/∂z + ∂vy/∂y + ∂vx/∂x.
Source code in spectraldiffx/_src/fourier/operators.py
curl(vz, vy, vx, spectral=False)
¶
Compute the 3D curl vector: ω = ∇ x V.
Components
ωz = ∂vy/∂x - ∂vx/∂y ωy = ∂vx/∂z - ∂vz/∂x ωx = ∂vz/∂y - ∂vy/∂z
Source code in spectraldiffx/_src/fourier/operators.py
laplacian(u, spectral=False)
¶
Compute 3D Laplacian: ∇^2 u = ∂^2u/∂z^2 + ∂^2u/∂y^2 + ∂^2u/∂x^2.
Source code in spectraldiffx/_src/fourier/operators.py
apply_dealias(u, spectral=False)
¶
Apply the 3D periodic dealiasing filter mask [Nz, Ny, Nx].
Source code in spectraldiffx/_src/fourier/operators.py
project_vector(vz, vy, vx)
¶
Project a 3D vector field onto its solenoidal (divergence-free) component.
Source code in spectraldiffx/_src/fourier/operators.py
advection_scalar(vz, vy, vx, q)
¶
Compute the 3D scalar advection term: (u·∇)q.