Chebyshev Derivative Operators¶
ChebyshevDerivative1D
¶
Bases: Module
1D Chebyshev derivative operator using the precomputed differentiation matrix.
Mathematical Formulation
For a function u(x) sampled at Chebyshev nodes xⱼ on [−L, L]:
(du/dx)ⱼ = Σₖ D_{jk} uₖ
where D is the (N+1)×(N+1) (Gauss–Lobatto) or N×N (Gauss) differentiation
matrix precomputed in :class:ChebyshevGrid1D. Higher-order derivatives
are matrix powers:
d²u/dx² = D · D · u = D² · u
Two evaluation strategies are available via method:
"matrix" (default) : Dⁿ·u by dense mat-vecs — O(N²) per derivative,
fastest for small N on accelerators.
"fft" : DCT → coefficient recurrence → inverse DCT —
O(N log N). Asymptotically cheaper, but a
BLAS mat-vec usually wins in practice for
N ≲ 10³ (on CPU, "matrix" was ~4× faster at
N = 512 for a batch of 256 fields); benchmark
for your hardware and N.
Both agree to round-off (they are the same polynomial interpolant).
Attributes:
| Name | Type | Description |
|---|---|---|
grid |
ChebyshevGrid1D
|
1D Chebyshev grid carrying the differentiation matrix D. |
method |
{'matrix', 'fft'}
|
Evaluation strategy (static). |
Examples:
Derivative of sin(πx) on [−1, 1]:
>>> import jax.numpy as jnp
>>> grid = ChebyshevGrid1D.from_N_L(N=32, L=1.0)
>>> deriv = ChebyshevDerivative1D(grid=grid)
>>> u = jnp.sin(jnp.pi * grid.x)
>>> du_dx = deriv(u) # ≈ π cos(πx)
>>> d2u_dx2 = deriv(u, order=2) # ≈ −π² sin(πx)
Source code in spectraldiffx/_src/chebyshev/operators.py
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 | |
Functions¶
__call__(u, order=1)
¶
Apply the n-th derivative Dⁿ to a nodal field.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Float[Array, 'Npts']
|
Nodal values at Chebyshev nodes (Npts = N+1 for GL, N for Gauss). |
required |
order
|
int
|
Derivative order (≥ 0). |
1
|
Returns:
| Type | Description |
|---|---|
Float[Array, 'Npts']
|
n-th derivative at the Chebyshev nodes. |
Source code in spectraldiffx/_src/chebyshev/operators.py
gradient(u)
¶
laplacian(u)
¶
integrate(u)
¶
Definite integral ∫_{−L}^{L} u(x) dx.
Computed from the Chebyshev coefficients,
∫ u dx = L Σ_{k even} 2 aₖ / (1 − k²)
which on Gauss–Lobatto nodes is exactly Clenshaw–Curtis quadrature.
Source code in spectraldiffx/_src/chebyshev/operators.py
antiderivative(u)
¶
Indefinite integral U(x) = ∫_{−L}^{x} u(s) ds at the nodes.
See :func:chebyshev_antiderivative_coeffs for the recurrence.
Source code in spectraldiffx/_src/chebyshev/operators.py
ChebyshevDerivative2D
¶
Bases: Module
2D Chebyshev derivative operators on [−Lx, Lx] × [−Ly, Ly].
Mathematical Formulation
For u(x, y) on a (Nypts, Nxpts) grid with differentiation matrices Dx, Dy stored on the grid:
(∂u/∂x)[j, i] = (u · Dxᵀ)[j, i] # applied along axis 1 (x)
(∂u/∂y)[j, i] = (Dy · u)[j, i] # applied along axis 0 (y)
The scalar Laplacian and 2D divergence/curl follow directly:
∇²u = ∂²u/∂x² + ∂²u/∂y²
∇·V = ∂vₓ/∂x + ∂vᵧ/∂y
(∇×V)_z = ∂vᵧ/∂x − ∂vₓ/∂y
Attributes:
| Name | Type | Description |
|---|---|---|
grid |
ChebyshevGrid2D
|
2D Chebyshev grid carrying Dx, Dy and the precomputed Dx², Dy². |
method |
{'matrix', 'fft'}
|
Evaluation strategy (static). "matrix" contracts each axis with
the 1D differentiation matrix (O(N³) per 2D derivative); "fft"
uses DCT + coefficient recurrence along each axis
(O(N² log N)). See :class: |
Examples:
Laplacian of u(x, y) = sin(πx)·sin(πy) on [−1, 1]²:
>>> import jax.numpy as jnp
>>> grid = ChebyshevGrid2D.from_N_L(Nx=24, Ny=24, Lx=1.0, Ly=1.0)
>>> deriv = ChebyshevDerivative2D(grid=grid)
>>> X, Y = grid.X
>>> u = jnp.sin(jnp.pi * X) * jnp.sin(jnp.pi * Y)
>>> lap_u = deriv.laplacian(u) # ≈ −2π² u
Divergence of V = (y, −x) (should be ~0):
Source code in spectraldiffx/_src/chebyshev/operators.py
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 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 | |
Functions¶
gradient(u)
¶
Partial derivatives (∂u/∂x, ∂u/∂y) of a 2D nodal field.
laplacian(u)
¶
2D Laplacian ∇²u = ∂²u/∂x² + ∂²u/∂y².
With method="matrix" this uses the precomputed Dx² and Dy² from
the grid, so the per-call cost is two matrix–matrix multiplies and
an add (no O(N³) recomputation of D²).
Source code in spectraldiffx/_src/chebyshev/operators.py
divergence(vx, vy)
¶
Cartesian divergence ∇·V = ∂vₓ/∂x + ∂vᵧ/∂y.
curl(vx, vy)
¶
Scalar curl ζ = ∂vᵧ/∂x − ∂vₓ/∂y (z-component of ∇×V).
This is also the relative vorticity of the velocity field (vₓ, vᵧ).
Source code in spectraldiffx/_src/chebyshev/operators.py
advection_scalar(vx, vy, q)
¶
Scalar advection (V·∇)q = vₓ·∂q/∂x + vᵧ·∂q/∂y.
Source code in spectraldiffx/_src/chebyshev/operators.py
biharmonic(u)
¶
hyperviscosity(u, nu, order=2)
¶
Hyperviscous tendency (−1)ⁿ⁺¹ ν ∇²ⁿ u.
The sign makes the operator dissipative for every n (it matches
:meth:SpectralDerivative2D.hyperviscosity). No boundary
conditions are imposed: this is the raw collocation operator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
u
|
Num[Array, 'Nypts Nxpts']
|
Nodal field. |
required |
nu
|
float
|
Hyperviscosity coefficient (≥ 0). |
required |
order
|
int
|
n ≥ 1 (1 = Laplacian diffusion, 2 = biharmonic). |
2
|
Source code in spectraldiffx/_src/chebyshev/operators.py
vector_laplacian(vx, vy)
¶
Cartesian vector Laplacian ∇²V = (∇²vₓ, ∇²vᵧ).
Source code in spectraldiffx/_src/chebyshev/operators.py
velocity_from_streamfunction(psi)
¶
Non-divergent velocity from a streamfunction.
u = −∂ψ/∂y, v = ∂ψ/∂x
(same sign convention as :meth:SpectralDerivative2D.velocity_from_streamfunction),
so that ∇·(u, v) = 0 and ζ = ∂v/∂x − ∂u/∂y = ∇²ψ.
Source code in spectraldiffx/_src/chebyshev/operators.py
jacobian(f, g)
¶
Jacobian J(f, g) = ∂f/∂x·∂g/∂y − ∂f/∂y·∂g/∂x.
Derivatives are spectral; the products are taken pointwise at the
collocation nodes (no dealiasing — combine with
:func:cheb_dealias_product if aliasing matters). With ψ the
streamfunction, J(ψ, q) = u·∇q is the advection of q.
Source code in spectraldiffx/_src/chebyshev/operators.py
integrate(u)
¶
Definite integral ∫∫ u dx dy over [−Lx, Lx] × [−Ly, Ly].
Tensor-product Chebyshev quadrature from the 2D coefficients (Clenshaw–Curtis on Gauss–Lobatto nodes).
Source code in spectraldiffx/_src/chebyshev/operators.py
ChebyshevDerivative3D
¶
Bases: Module
3D Chebyshev derivative operators on [−Lz, Lz] × [−Ly, Ly] × [−Lx, Lx].
Arrays have shape (Nzpts, Nypts, Nxpts) with axis order (z, y, x),
mirroring :class:SpectralDerivative3D; vector-valued methods take and
return components in the same (z, y, x) order.
Mathematical Formulation
Each partial derivative contracts one axis with the 1D matrix:
∂u/∂x = u ×₂ Dx, ∂u/∂y = u ×₁ Dy, ∂u/∂z = u ×₀ Dz
so a derivative costs O(Nx·Ny·Nz·N) ("matrix") or O(Nx·Ny·Nz·log N) ("fft").
Attributes:
| Name | Type | Description |
|---|---|---|
grid |
ChebyshevGrid3D
|
3D Chebyshev grid. |
method |
{'matrix', 'fft'}
|
Evaluation strategy (static). |
Examples:
>>> import jax.numpy as jnp
>>> grid = ChebyshevGrid3D.from_N_L(Nx=12, Ny=12, Nz=12, Lx=1.0, Ly=1.0, Lz=1.0)
>>> deriv = ChebyshevDerivative3D(grid=grid)
>>> Z, Y, X = grid.X
>>> u = jnp.sin(X) * jnp.cos(Y) * jnp.exp(Z)
>>> lap = deriv.laplacian(u) # ≈ −u
Source code in spectraldiffx/_src/chebyshev/operators.py
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 397 398 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 | |
Functions¶
gradient(u)
¶
Gradient (∂u/∂z, ∂u/∂y, ∂u/∂x).
Source code in spectraldiffx/_src/chebyshev/operators.py
divergence(vz, vy, vx)
¶
Divergence ∇·V = ∂v_z/∂z + ∂vᵧ/∂y + ∂vₓ/∂x.
Source code in spectraldiffx/_src/chebyshev/operators.py
curl(vz, vy, vx)
¶
Curl ω = ∇×V returned as (ω_z, ω_y, ω_x).
ω_z = ∂vᵧ/∂x − ∂vₓ/∂y ω_y = ∂vₓ/∂z − ∂v_z/∂x ω_x = ∂v_z/∂y − ∂vᵧ/∂z
Source code in spectraldiffx/_src/chebyshev/operators.py
laplacian(u)
¶
Laplacian ∇²u = ∂²u/∂x² + ∂²u/∂y² + ∂²u/∂z².
biharmonic(u)
¶
hyperviscosity(u, nu, order=2)
¶
Hyperviscous tendency (−1)ⁿ⁺¹ ν ∇²ⁿ u (dissipative for every n ≥ 1).
Source code in spectraldiffx/_src/chebyshev/operators.py
vector_laplacian(vz, vy, vx)
¶
Cartesian vector Laplacian ∇²V = (∇²v_z, ∇²vᵧ, ∇²vₓ).
Source code in spectraldiffx/_src/chebyshev/operators.py
velocity_from_streamfunction(psi)
¶
Horizontal velocity (u, v) = (−∂ψ/∂y, ∂ψ/∂x) at every level.
Source code in spectraldiffx/_src/chebyshev/operators.py
jacobian(f, g)
¶
Horizontal Jacobian J(f, g) = ∂f/∂x·∂g/∂y − ∂f/∂y·∂g/∂x at every level.
Source code in spectraldiffx/_src/chebyshev/operators.py
integrate(u)
¶
Definite integral ∫∫∫ u dV over the box (tensor-product quadrature).