gvec.fourier#

pyGVEC postprocessing - Fourier representation

This module provides functions for computing the Fourier transform in 1D and 2D. In this context, the Fourier series is of the form \(x(\theta, \zeta) = \sum_{m, n} c_{m, n} \cos(m \theta - n \zeta) + s_{m, n} \sin(m \theta - n \zeta)\).

gvec.fourier.eval2d(c: ndarray, s: ndarray, theta: ndarray, zeta: ndarray, deriv: str | None = None, nfp: int = 1)#

Evaluate a 2D Fourier series at given poloidal and toroidal angles.

Parameters:
  • c (numpy.ndarray) – Cosine coefficients of the Fourier series.

  • s (numpy.ndarray) – Sine coefficients of the Fourier series.

  • theta (numpy.ndarray) – Poloidal angles at which to evaluate the series.

  • zeta (numpy.ndarray) – Toroidal angles at which to evaluate the series.

  • deriv (str, optional) – Derivative to evaluate, by default None. Specified as ‘theta’, ‘zeta’ or any string of ‘t’ & ‘z’, e.g. ‘t’, ‘tz’, ‘ttz’, …

  • nfp (int, optional) – Number of field periods, by default 1.

Returns:

x – The values of the series at the given angles.

Return type:

numpy.ndarray

gvec.fourier.fft1d(y: Iterable, x0=0.0, axis=None)#

Compute the Fourier transform in 1d from data values at equidistant points \(y(x_i)\), along a given axis of an array.

Parameters:
  • y – Input array to transform \(y(x_i)\), assumed to be sampled on x=x0+np.linspace(0,2*np.pi,len(x),endpoint=False). y is allowed to have multiple dimensions, but the fft will be applied only along the given axis.

  • x0 (starting value of angle, where x was sampled, in [0,2pi] , defaults to 0.)

  • axis (int) – Axis along which to compute the fft. None by default. Becomes mandatory if y is multi-dimensional.

Returns:

  • c (numpy.ndarray) – Cosine coefficients of the Fourier series.

  • s (numpy.ndarray) – Sine coefficients of the Fourier series.

Notes

The function uses the real-input fast Fourier transform (rfft) from numpy.

gvec.fourier.fft2d(x: ndarray, theta0=0.0, zeta0=0.0)#

Compute the Fourier transform of a 2D array.

The Fourier series is of the form \(x(\theta, \zeta) = \sum_{m, n} c_{m, n} \cos(m \theta - n \zeta) + s_{m, n} \sin(m \theta - n \zeta)\). The coefficients are given as arrays of shape (M + 1, 2 * N + 1), where M and N are the maximum poloidal and toroidal harmonics, respectively. The coefficients with toroidal indices \(n > N\) are to be interpreted negatively, counted from the end of the array.

Parameters:
  • x (numpy.ndarray) – Input array of shape (ntheta, nzeta) to transform, assumed to be sampled on theta=theta0+np.linspace(0,2*np.pi,ntheta,endpoint=False) and zeta=zeta0+np.linspace(0,2*np.pi,nzeta*nfp,endpoint=False)

  • theta0 (float) – starting value of theta, where x was sampled , defaults to 0.0.

  • zeta0 (float) – starting value of zeta, where x was sampled, defaults to 0.0.

Returns:

  • c (numpy.ndarray) – Cosine coefficients of the double-angle Fourier series.

  • s (numpy.ndarray) – Sine coefficients of the double-angle Fourier series.

gvec.fourier.fft2d_modes(M: int, N: int, grid: bool = False)#

Generate the modenumbers for a 2D FFT, as performed by fft2d.

Parameters:
  • M (int) – The maximum poloidal modenumber.

  • N (int) – The maximum toroidal modenumber.

Returns:

  • m (numpy.ndarray) – The poloidal modenumbers.

  • n (numpy.ndarray) – The toroidal modenumbers.

gvec.fourier.fft2d_to_parameters(coefficients: ndarray) dict[tuple[int, int], float]#

Convert Fourier coefficients to a parameter vector.

Parameters:

coefficients (numpy.ndarray) – Sine or cosine coefficients of the Fourier series.

Returns:

params – Dictionary mapping (m, n) mode numbers to their corresponding coefficient values, excluding zero values.

Return type:

dict[tuple[int, int], float]

gvec.fourier.get_B_dft(x_out, deriv, nfp, modes)#

Function to be used together with real_dft_mat, a direct Fourier transform in 1D. Get the the matrix for the backward direct fourier transform.

Parameters:
  • x_out (numpy.ndarray) – array of output point positions, can be chosen anywhere.

  • deriv (int,optional) – build the output matrix for the derivative of the function.

  • nfp (int,optional) – number of field periods, all modes are multiples of nfp, default is 1

  • modes (numpy.ndarray, int) – modes to be considered in the 1D Fourier transform, on one field period.

Returns:

modes_back – Matrix for Fourier transform from modes to points, with derivative

Return type:

numpy.ndarray

gvec.fourier.ifft1d(c: ndarray, s: ndarray, npoints: int | None = None, deriv: str | None = None, nfp: int = 1, axis=None) ndarray#

Compute the inverse Fast-Fourier-Transform of a 1D Fourier series.

Parameters:
  • c (numpy.ndarray) – Cosine coefficients of the Fourier series.

  • s (numpy.ndarray) – Sine coefficients of the Fourier series.

  • npoints (int) – Number of points at which to evaluate the series. If None, n=2*m_max+1 is used

  • deriv (int, optional) – Derivative to evaluate, by default None.

  • nfp (int, optional) – Number of field periods, by default 1. Only used for derivatives, the data itself is always assumed to be in a single field period.

  • axis (int, optional) – Axis along which to compute the ifft, by default None. Becomes mandatory if c,s are multi-dimensional.

Returns:

y – The values of the series evaluated at x=np.linspace(0,2*np.pi,len(x),endpoint=False).

Return type:

numpy.ndarray

gvec.fourier.ifft2d(c: ndarray, s: ndarray, deriv: str | None = None, nfp: int = 1) ndarray#

Inverse Fast-Fourier-Transform of a 2D Fourier series.

Parameters:
  • c (numpy.ndarray) – Cosine coefficients of the Fourier series.

  • s (numpy.ndarray) – Sine coefficients of the Fourier series.

  • deriv (str, optional) – Derivative to evaluate, by default None. Specified as 'theta', 'zeta' or any string of 't' ``  and ``'z', e.g. 't', 'tz', 'ttz', …

  • nfp (int, optional) – Number of field periods, by default 1. Only used for derivatives, the data itself is always assumed to be in a single field period.

Returns:

x – The values of the series evaluated at theta=np.linspace(0,2*np.pi,2*M+1,endpoint=False) and zeta=np.linspace(0,2*np.pi,N*nfp+1,endpoint=False).

Return type:

numpy.ndarray

gvec.fourier.real_dft_mat(x_in, x_out, nfp=1, modes=None, deriv=0)#

Precompute matrices for flexible Direct Fourier Transform for real data in 1D.

Takes an input array of equidistant points in [0,2pi/nfp[ (exclude endpoint!), evaluate the discrete fourier transform with the given 1d mode vector (all >=0) using the input points x_in, then evaluate the inverse transform (or its derivative deriv>0) on the output points x_out anywhere.

Parameters:
  • x_in (numpy.ndarray) – equidistant points in one field period, excluding the periodic endpoint len(x_in) must be > 2*max(modes)

  • x_out (numpy.ndarray) – array of output point positions, can be chosen anywhere.

  • nfp (int,optional) – number of field periods, all modes are multiples of nfp, default is 1

  • modes (ndarray, int, optional) – modes to be considered in the 1D Fourier transform, on one field period. Default is None, which sets the modes to np.arange((len(x_in) - 1) // 2 + 1)

  • deriv (int,optional) – build the output matrix for the derivative of the function.

Returns:

out – output is a dictionary, storing the input data and the forward-backward matrix "FB", that transforms real function to real function [derivative]: f^deriv(x_out) = dict["BF"]  @ f(x_in) (can then be used to do 2d transforms with matmul!)

Return type:

dict

gvec.fourier.scale_modes2d(c, M, N)#

Scale/Cutoff the coefficients of a 2D Fourier series to a new maximum poloidal and toroidal harmonics.

Parameters:
  • c (numpy.ndarray) – The coefficients of the original Fourier series, with poloidal mode numbers in the first dimension and toroidal mode numbers in the second. second dimension must be odd.

  • M (int) – The new maximum poloidal harmonic.

  • N (int) – The new maximum toroidal harmonic.

Returns:

c2 – The coefficients of the scaled Fourier series.

Return type:

numpy.ndarray

gvec.fourier.shift_1d(y: ndarray, x0, axis, newshape=None)#

shift periodic data along one given axis (and upsample):

from \(y(x_i)\) with \(x_i=x_0+2\pi i/N\), \(i=0,..N-1,\) N=len(y)

to \(y(x_j)\) with \(x_j=0+2\pi j/M\), \(j=0,..M-1\)

with M>=N. can be used to upsample, by setting x0=0.

Parameters:
  • y (numpy.ndarray) – periodic data

  • x0 (float) – origin position where y[0] was evaluated. If zero, no shift is applied.

  • axis (int) – axis along which to shift

  • newshape (int) – output shape along the shifted axis >= input shape. Defaults to input shape

Returns:

yshft – shifted periodic data y(x-x0)

Return type:

ndarray