![]() |
OpenPFC
0.1.4
Phase Field Crystal simulation framework
|
K-space (Fourier space) helper functions for spectral methods. More...
#include <array>#include <cmath>#include <openpfc/constants.hpp>#include <openpfc/core/world.hpp>

Go to the source code of this file.
Functions | |
| template<typename T > | |
| std::array< double, 3 > | pfc::fft::kspace::k_frequency_scaling (const world::World< T > &world) noexcept |
| Compute frequency scaling factors for each dimension. | |
| double | pfc::fft::kspace::k_component (int index, int size, double freq_scale) noexcept |
| Compute k-space wave vector component with Nyquist folding. | |
| double | pfc::fft::kspace::k_laplacian_value (double ki, double kj, double kk) noexcept |
| Compute Laplacian operator value -k² in Fourier space. | |
| double | pfc::fft::kspace::k_squared_value (double ki, double kj, double kk) noexcept |
| Compute squared magnitude k² = k_x² + k_y² + k_z². | |
K-space (Fourier space) helper functions for spectral methods.
This file provides zero-cost helper functions for computing wave vector components in Fourier space for FFT-based spectral methods. These functions encapsulate the standard frequency scaling and Nyquist folding logic that is duplicated across 120+ lines in multiple examples.
The problem: Every spectral method example contains the same 30-line pattern:
The solution: Extract reusable helpers:
All functions are inline, noexcept, and zero-cost abstractions that compile to identical machine code as the manual implementation.
Compute k-space wave vector component with Nyquist folding.
Converts a grid index to the corresponding wave vector component, handling the Nyquist frequency folding that occurs in real-to-complex FFTs.
FFT convention:
| index | Grid index (0 to size-1) |
| size | Domain size in this dimension |
| freq_scale | Frequency scaling factor (from k_frequency_scaling) |
double ki = (i <= size / 2) ? i * fx : (i - size) * fx;Time complexity: O(1) Space complexity: O(1)


|
inlinenoexcept |
Compute frequency scaling factors for each dimension.
Calculates f = 2π / (spacing * size) for each dimension, which converts grid indices to wave vector components in Fourier space.
In spectral methods using FFT, derivatives in real space become multiplications by wave vectors in Fourier space. The frequency scaling establishes the relationship between grid indices and physical wave numbers.
| T | World coordinate system type (e.g., CartesianTag) |
| world | Simulation world containing grid size and spacing |
double fx = 2.0 * pi / (spacing[0] * size[0]);Time complexity: O(1) Space complexity: O(1)


Compute Laplacian operator value -k² in Fourier space.
Calculates -(k_x² + k_y² + k_z²), the Fourier representation of the Laplacian operator ∇². Used in diffusion, heat, and phase-field equations.
Mathematical relationship:
The negative sign is conventional, making the Laplacian negative definite.
| ki | Wave vector x-component |
| kj | Wave vector y-component |
| kk | Wave vector z-component |
double kLap = -(ki * ki + kj * kj + kk * kk);Time complexity: O(1) Space complexity: O(1)


Compute squared magnitude k² = k_x² + k_y² + k_z².
Calculates the positive squared magnitude of the wave vector, useful for higher-order operators, filtering, and wave number magnitudes.
Mathematical relationships:
| ki | Wave vector x-component |
| kj | Wave vector y-component |
| kk | Wave vector z-component |
double k2 = ki * ki + kj * kj + kk * kk;Time complexity: O(1) Space complexity: O(1)

