2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2003 David van der Spoel, Erik Lindahl, University of Groningen.
5 * Copyright (c) 2013,2014,2015,2016,2017,2019, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
45 #include "external/fftpack/fftpack.h"
47 #include "gromacs/fft/fft.h"
48 #include "gromacs/utility/fatalerror.h"
49 #include "gromacs/utility/real.h"
53 * Contents of the FFTPACK fft datatype.
55 * Note that this is one of several possible implementations of gmx_fft_t.
57 * FFTPACK only does 1d transforms, so we use a pointers to another fft for
58 * the transform in the next dimension.
59 * Thus, a 3d-structure contains a pointer to a 2d one, which in turns contains
60 * a pointer to a 1d. The 1d structure has next==NULL.
63 struct gmx_fft_fftpack
68 int ndim; /**< Dimensions, including our subdimensions. */
69 int n; /**< Number of points in this dimension. */
70 int ifac[15]; /**< 15 bytes needed for cfft and rfft */
71 struct gmx_fft* next; /**< Pointer to next dimension, or NULL. */
72 real* work; /**< 1st 4n reserved for cfft, 1st 2n for rfft */
75 int gmx_fft_init_1d(gmx_fft_t* pfft, int nx, int gmx_unused flags)
81 gmx_fatal(FARGS, "Invalid FFT opaque type pointer.");
86 if ((fft = static_cast<struct gmx_fft*>(malloc(sizeof(struct gmx_fft)))) == nullptr)
94 /* Need 4*n storage for 1D complex FFT */
95 if ((fft->work = static_cast<real*>(malloc(sizeof(real) * (4 * nx)))) == nullptr)
103 fftpack_cffti1(nx, fft->work, fft->ifac);
111 int gmx_fft_init_1d_real(gmx_fft_t* pfft, int nx, int gmx_unused flags)
117 gmx_fatal(FARGS, "Invalid FFT opaque type pointer.");
122 if ((fft = static_cast<struct gmx_fft*>(malloc(sizeof(struct gmx_fft)))) == nullptr)
130 /* Need 2*n storage for 1D real FFT */
131 if ((fft->work = static_cast<real*>(malloc(sizeof(real) * (2 * nx)))) == nullptr)
139 fftpack_rffti1(nx, fft->work, fft->ifac);
146 int gmx_fft_init_2d_real(gmx_fft_t* pfft, int nx, int ny, int flags)
149 int nyc = (ny / 2 + 1);
154 gmx_fatal(FARGS, "Invalid FFT opaque type pointer.");
159 /* Create the X transform */
160 if ((fft = static_cast<struct gmx_fft*>(malloc(sizeof(struct gmx_fft)))) == nullptr)
167 /* Need 4*nx storage for 1D complex FFT, and another
168 * 2*nx*nyc elements for complex-to-real storage in our high-level routine.
170 if ((fft->work = static_cast<real*>(malloc(sizeof(real) * (4 * nx + 2 * nx * nyc)))) == nullptr)
175 fftpack_cffti1(nx, fft->work, fft->ifac);
177 /* Create real Y transform as a link from X */
178 if ((rc = gmx_fft_init_1d_real(&(fft->next), ny, flags)) != 0)
189 int gmx_fft_1d(gmx_fft_t fft, enum gmx_fft_direction dir, void* in_data, void* out_data)
199 p1 = static_cast<real*>(in_data);
200 p2 = static_cast<real*>(out_data);
205 /* FFTPACK only does in-place transforms, so emulate out-of-place
206 * by copying data to the output array first.
208 if (in_data != out_data)
210 p1 = static_cast<real*>(in_data);
211 p2 = static_cast<real*>(out_data);
213 /* n complex = 2*n real elements */
214 for (i = 0; i < 2 * n; i++)
220 /* Elements 0 .. 2*n-1 in work are used for ffac values,
221 * Elements 2*n .. 4*n-1 are internal FFTPACK work space.
224 if (dir == GMX_FFT_FORWARD)
226 fftpack_cfftf1(n, static_cast<real*>(out_data), fft->work + 2 * n, fft->work, fft->ifac, -1);
228 else if (dir == GMX_FFT_BACKWARD)
230 fftpack_cfftf1(n, static_cast<real*>(out_data), fft->work + 2 * n, fft->work, fft->ifac, 1);
234 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
242 int gmx_fft_1d_real(gmx_fft_t fft, enum gmx_fft_direction dir, void* in_data, void* out_data)
252 p1 = static_cast<real*>(in_data);
253 p2 = static_cast<real*>(out_data);
255 if (dir == GMX_FFT_REAL_TO_COMPLEX)
261 if (dir == GMX_FFT_REAL_TO_COMPLEX)
263 /* FFTPACK only does in-place transforms, so emulate out-of-place
264 * by copying data to the output array first. This works fine, since
265 * the complex array must be larger than the real.
267 if (in_data != out_data)
269 p1 = static_cast<real*>(in_data);
270 p2 = static_cast<real*>(out_data);
272 for (i = 0; i < 2 * (n / 2 + 1); i++)
278 /* Elements 0 .. n-1 in work are used for ffac values,
279 * Elements n .. 2*n-1 are internal FFTPACK work space.
281 fftpack_rfftf1(n, static_cast<real*>(out_data), fft->work + n, fft->work, fft->ifac);
284 * FFTPACK has a slightly more compact storage than we, time to
285 * convert it: ove most of the array one step up to make room for
286 * zero imaginary parts.
288 p2 = static_cast<real*>(out_data);
289 for (i = n - 1; i > 0; i--)
293 /* imaginary zero freq. */
302 else if (dir == GMX_FFT_COMPLEX_TO_REAL)
304 /* FFTPACK only does in-place transforms, and we cannot just copy
305 * input to output first here since our real array is smaller than
306 * the complex one. However, since the FFTPACK complex storage format
307 * is more compact than ours (2 reals) it will fit, so compact it
308 * and copy on-the-fly to the output array.
310 p1 = static_cast<real*>(in_data);
311 p2 = static_cast<real*>(out_data);
314 for (i = 1; i < n; i++)
318 fftpack_rfftb1(n, static_cast<real*>(out_data), fft->work + n, fft->work, fft->ifac);
322 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
330 int gmx_fft_2d_real(gmx_fft_t fft, enum gmx_fft_direction dir, void* in_data, void* out_data)
332 int i, j, nx, ny, nyc;
340 /* Number of complex elements in y direction */
343 work = fft->work + 4 * nx;
345 if (dir == GMX_FFT_REAL_TO_COMPLEX)
347 /* If we are doing an in-place transform the 2D array is already
348 * properly padded by the user, and we are all set.
350 * For out-of-place there is no array padding, but FFTPACK only
351 * does in-place FFTs internally, so we need to start by copying
352 * data from the input to the padded (larger) output array.
354 if (in_data != out_data)
356 p1 = static_cast<real*>(in_data);
357 p2 = static_cast<real*>(out_data);
359 for (i = 0; i < nx; i++)
361 for (j = 0; j < ny; j++)
363 p2[i * nyc * 2 + j] = p1[i * ny + j];
367 data = static_cast<t_complex*>(out_data);
369 /* y real-to-complex FFTs */
370 for (i = 0; i < nx; i++)
372 gmx_fft_1d_real(fft->next, GMX_FFT_REAL_TO_COMPLEX, data + i * nyc, data + i * nyc);
375 /* Transform to get X data in place */
376 gmx_fft_transpose_2d(data, data, nx, nyc);
378 /* Complex-to-complex X FFTs */
379 for (i = 0; i < nyc; i++)
381 gmx_fft_1d(fft, GMX_FFT_FORWARD, data + i * nx, data + i * nx);
385 gmx_fft_transpose_2d(data, data, nyc, nx);
387 else if (dir == GMX_FFT_COMPLEX_TO_REAL)
389 /* An in-place complex-to-real transform is straightforward,
390 * since the output array must be large enough for the padding to fit.
392 * For out-of-place complex-to-real transforms we cannot just copy
393 * data to the output array, since it is smaller than the input.
394 * In this case there's nothing to do but employing temporary work data,
395 * starting at work+4*nx and using nx*nyc*2 elements.
397 if (in_data != out_data)
399 memcpy(work, in_data, sizeof(t_complex) * nx * nyc);
400 data = reinterpret_cast<t_complex*>(work);
405 data = reinterpret_cast<t_complex*>(out_data);
408 /* Transpose to get X arrays */
409 gmx_fft_transpose_2d(data, data, nx, nyc);
412 for (i = 0; i < nyc; i++)
414 gmx_fft_1d(fft, GMX_FFT_BACKWARD, data + i * nx, data + i * nx);
417 /* Transpose to get Y arrays */
418 gmx_fft_transpose_2d(data, data, nyc, nx);
421 for (i = 0; i < nx; i++)
423 gmx_fft_1d_real(fft->next, GMX_FFT_COMPLEX_TO_REAL, data + i * nyc, data + i * nyc);
426 if (in_data != out_data)
428 /* Output (pointed to by data) is now in padded format.
429 * Pack it into out_data if we were doing an out-of-place transform.
431 p1 = reinterpret_cast<real*>(data);
432 p2 = static_cast<real*>(out_data);
434 for (i = 0; i < nx; i++)
436 for (j = 0; j < ny; j++)
438 p2[i * ny + j] = p1[i * nyc * 2 + j];
445 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
452 void gmx_fft_destroy(gmx_fft_t fft)
457 if (fft->next != nullptr)
459 gmx_fft_destroy(fft->next);
465 void gmx_fft_cleanup() {}