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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 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 | |
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
biharmonic(u, spectral=False)
¶
Compute the biharmonic operator: d^4u/dx^4.
In Fourier space: k^4 * u_hat.
Source code in spectraldiffx/_src/fourier/operators.py
hyperviscosity(u, nu, order=2, spectral=False)
¶
Compute the hyperviscosity term: (-1)^{n+1} * nu * d^{2n}u/dx^{2n}.
This is always dissipative: the spectral multiplier is
-nu * |k|^{2n}, regardless of order.
Parameters¶
u : Array [N] Input field. nu : float Hyperviscosity coefficient. order : int Order n of the hyperviscosity (n=1 is regular diffusion, n=2 is biharmonic diffusion). Default: 2. spectral : bool Whether u is in Fourier space.
Raises¶
ValueError
If order < 1 or nu < 0.
Source code in spectraldiffx/_src/fourier/operators.py
inverse_laplacian(u, spectral=False)
¶
Compute the inverse Laplacian: nabla^{-2} u (1D Poisson solve).
On a periodic domain, d^2 psi / dx^2 = u has a solution only if u has zero spatial mean. This implementation removes the k=0 component of u and returns the unique zero-mean solution psi, i.e. it solves d^2 psi / dx^2 = u - mean(u) with psi_hat[k=0] = 0.
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
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 241 242 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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 | |
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
biharmonic(u, spectral=False)
¶
Compute the 2D biharmonic: nabla^4 u = (kx^2 + ky^2)^2 * u_hat.
Source code in spectraldiffx/_src/fourier/operators.py
hyperviscosity(u, nu, order=2, spectral=False)
¶
Compute the 2D hyperviscosity: (-1)^{n+1} * nu * nabla^{2n} u.
This is always dissipative: the spectral multiplier is
-nu * |k|^{2n}, regardless of order.
Parameters¶
u : Array [Ny, Nx] Input field. nu : float Hyperviscosity coefficient. order : int Order n (n=1 is diffusion, n=2 is biharmonic). Default: 2. spectral : bool Whether u is in Fourier space.
Raises¶
ValueError
If order < 1 or nu < 0.
Source code in spectraldiffx/_src/fourier/operators.py
inverse_laplacian(u, spectral=False)
¶
Compute the 2D inverse Laplacian: nabla^{-2} u (Poisson solve).
On a periodic domain, nabla^2 psi = u is solvable only if u has zero spatial mean. This implementation removes the (0,0) Fourier mode of u and returns the unique zero-mean solution psi.
Source code in spectraldiffx/_src/fourier/operators.py
velocity_from_streamfunction(psi, spectral=False)
¶
Compute velocity from a streamfunction: u = -dpsi/dy, v = dpsi/dx.
Source code in spectraldiffx/_src/fourier/operators.py
jacobian(f, g, spectral=False)
¶
Compute the Jacobian: J(f,g) = df/dx * dg/dy - df/dy * dg/dx.
Uses the pseudo-spectral method: derivatives in Fourier space, products in physical space, with dealiasing.
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
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 | |
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
biharmonic(u, spectral=False)
¶
Compute the 3D biharmonic: nabla^4 u.
Source code in spectraldiffx/_src/fourier/operators.py
hyperviscosity(u, nu, order=2, spectral=False)
¶
Compute the 3D hyperviscosity: (-1)^{n+1} * nu * nabla^{2n} u.
Raises¶
ValueError
If order < 1 or nu < 0.
Source code in spectraldiffx/_src/fourier/operators.py
inverse_laplacian(u, spectral=False)
¶
Compute the 3D inverse Laplacian: nabla^{-2} u (Poisson solve).
On a periodic domain, nabla^2 psi = u is solvable only if u has zero spatial mean. This implementation removes the (0,0,0) Fourier mode of u and returns the unique zero-mean solution psi.
Source code in spectraldiffx/_src/fourier/operators.py
velocity_from_streamfunction(psi, spectral=False)
¶
Compute horizontal velocity from a streamfunction.
Returns (u, v) where u = -dpsi/dy, v = dpsi/dx (horizontal components).
Source code in spectraldiffx/_src/fourier/operators.py
jacobian(f, g, spectral=False)
¶
Compute the horizontal Jacobian: J(f,g) = df/dx * dg/dy - df/dy * dg/dx.
Operates on the horizontal (x, y) components only — the z direction is treated as a batch dimension.
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.