1 /* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
4 * Gromacs 4.0 Copyright (c) 1991-2003
5 * David van der Spoel, Erik Lindahl, University of Groningen.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * To help us fund GROMACS development, we humbly ask that you cite
13 * the research papers on the package. Check out http://www.gromacs.org
16 * Gnomes, ROck Monsters And Chili Sauce
22 #ifdef GMX_FFT_FFTPACK
31 #include "gmx_fatal.h"
32 #include "external/fftpack/fftpack.h"
34 /** Contents of the FFTPACK fft datatype.
36 * FFTPACK only does 1d transforms, so we use a pointers to another fft for
37 * the transform in the next dimension.
38 * Thus, a 3d-structure contains a pointer to a 2d one, which in turns contains
39 * a pointer to a 1d. The 1d structure has next==NULL.
43 int ndim; /**< Dimensions, including our subdimensions. */
44 int n; /**< Number of points in this dimension. */
45 int ifac[15]; /**< 15 bytes needed for cfft and rfft */
46 struct gmx_fft *next; /**< Pointer to next dimension, or NULL. */
47 real * work; /**< 1st 4n reserved for cfft, 1st 2n for rfft */
55 gmx_fft_init_1d(gmx_fft_t * pfft,
63 gmx_fatal(FARGS, "Invalid FFT opaque type pointer.");
68 if ( (fft = (struct gmx_fft *)malloc(sizeof(struct gmx_fft))) == NULL)
76 /* Need 4*n storage for 1D complex FFT */
77 if ( (fft->work = (real *)malloc(sizeof(real)*(4*nx))) == NULL)
85 fftpack_cffti1(nx, fft->work, fft->ifac);
95 gmx_fft_init_1d_real(gmx_fft_t * pfft,
103 gmx_fatal(FARGS, "Invalid FFT opaque type pointer.");
108 if ( (fft = (struct gmx_fft *)malloc(sizeof(struct gmx_fft))) == NULL)
116 /* Need 2*n storage for 1D real FFT */
117 if ((fft->work = (real *)malloc(sizeof(real)*(2*nx))) == NULL)
125 fftpack_rffti1(nx, fft->work, fft->ifac);
133 gmx_fft_init_2d_real(gmx_fft_t * pfft,
139 int nyc = (ny/2 + 1);
144 gmx_fatal(FARGS, "Invalid FFT opaque type pointer.");
149 /* Create the X transform */
150 if ( (fft = (struct gmx_fft *)malloc(sizeof(struct gmx_fft))) == NULL)
157 /* Need 4*nx storage for 1D complex FFT, and another
158 * 2*nx*nyc elements for complex-to-real storage in our high-level routine.
160 if ( (fft->work = (real *)malloc(sizeof(real)*(4*nx+2*nx*nyc))) == NULL)
165 fftpack_cffti1(nx, fft->work, fft->ifac);
167 /* Create real Y transform as a link from X */
168 if ( (rc = gmx_fft_init_1d_real(&(fft->next), ny, flags)) != 0)
180 gmx_fft_1d (gmx_fft_t fft,
181 enum gmx_fft_direction dir,
193 p1 = (real *)in_data;
194 p2 = (real *)out_data;
199 /* FFTPACK only does in-place transforms, so emulate out-of-place
200 * by copying data to the output array first.
202 if (in_data != out_data)
204 p1 = (real *)in_data;
205 p2 = (real *)out_data;
207 /* n complex = 2*n real elements */
208 for (i = 0; i < 2*n; i++)
214 /* Elements 0 .. 2*n-1 in work are used for ffac values,
215 * Elements 2*n .. 4*n-1 are internal FFTPACK work space.
218 if (dir == GMX_FFT_FORWARD)
220 fftpack_cfftf1(n, (real *)out_data, fft->work+2*n, fft->work, fft->ifac, -1);
222 else if (dir == GMX_FFT_BACKWARD)
224 fftpack_cfftf1(n, (real *)out_data, fft->work+2*n, fft->work, fft->ifac, 1);
228 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
238 gmx_fft_1d_real (gmx_fft_t fft,
239 enum gmx_fft_direction dir,
251 p1 = (real *)in_data;
252 p2 = (real *)out_data;
254 if (dir == GMX_FFT_REAL_TO_COMPLEX)
260 if (dir == GMX_FFT_REAL_TO_COMPLEX)
262 /* FFTPACK only does in-place transforms, so emulate out-of-place
263 * by copying data to the output array first. This works fine, since
264 * the complex array must be larger than the real.
266 if (in_data != out_data)
268 p1 = (real *)in_data;
269 p2 = (real *)out_data;
271 for (i = 0; i < 2*(n/2+1); i++)
277 /* Elements 0 .. n-1 in work are used for ffac values,
278 * Elements n .. 2*n-1 are internal FFTPACK work space.
280 fftpack_rfftf1(n, (real *)out_data, fft->work+n, fft->work, fft->ifac);
283 * FFTPACK has a slightly more compact storage than we, time to
284 * convert it: ove most of the array one step up to make room for
285 * zero imaginary parts.
287 p2 = (real *)out_data;
288 for (i = n-1; i > 0; i--)
292 /* 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 = (real *) in_data;
311 p2 = (real *)out_data;
314 for (i = 1; i < n; i++)
318 fftpack_rfftb1(n, (real *)out_data, fft->work+n, fft->work, fft->ifac);
322 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
331 gmx_fft_2d_real (gmx_fft_t fft,
332 enum gmx_fft_direction dir,
336 int i, j, nx, ny, nyc;
344 /* Number of complex elements in y direction */
347 work = fft->work+4*nx;
349 if (dir == GMX_FFT_REAL_TO_COMPLEX)
351 /* If we are doing an in-place transform the 2D array is already
352 * properly padded by the user, and we are all set.
354 * For out-of-place there is no array padding, but FFTPACK only
355 * does in-place FFTs internally, so we need to start by copying
356 * data from the input to the padded (larger) output array.
358 if (in_data != out_data)
360 p1 = (real *)in_data;
361 p2 = (real *)out_data;
363 for (i = 0; i < nx; i++)
365 for (j = 0; j < ny; j++)
367 p2[i*nyc*2+j] = p1[i*ny+j];
371 data = (t_complex *)out_data;
373 /* y real-to-complex FFTs */
374 for (i = 0; i < nx; i++)
376 gmx_fft_1d_real(fft->next, GMX_FFT_REAL_TO_COMPLEX, data+i*nyc, data+i*nyc);
379 /* Transform to get X data in place */
380 gmx_fft_transpose_2d(data, data, nx, nyc);
382 /* Complex-to-complex X FFTs */
383 for (i = 0; i < nyc; i++)
385 gmx_fft_1d(fft, GMX_FFT_FORWARD, data+i*nx, data+i*nx);
389 gmx_fft_transpose_2d(data, data, nyc, nx);
392 else if (dir == GMX_FFT_COMPLEX_TO_REAL)
394 /* An in-place complex-to-real transform is straightforward,
395 * since the output array must be large enough for the padding to fit.
397 * For out-of-place complex-to-real transforms we cannot just copy
398 * data to the output array, since it is smaller than the input.
399 * In this case there's nothing to do but employing temporary work data,
400 * starting at work+4*nx and using nx*nyc*2 elements.
402 if (in_data != out_data)
404 memcpy(work, in_data, sizeof(t_complex)*nx*nyc);
405 data = (t_complex *)work;
410 data = (t_complex *)out_data;
413 /* Transpose to get X arrays */
414 gmx_fft_transpose_2d(data, data, nx, nyc);
417 for (i = 0; i < nyc; i++)
419 gmx_fft_1d(fft, GMX_FFT_BACKWARD, data+i*nx, data+i*nx);
422 /* Transpose to get Y arrays */
423 gmx_fft_transpose_2d(data, data, nyc, nx);
426 for (i = 0; i < nx; i++)
428 gmx_fft_1d_real(fft->next, GMX_FFT_COMPLEX_TO_REAL, data+i*nyc, data+i*nyc);
431 if (in_data != out_data)
433 /* Output (pointed to by data) is now in padded format.
434 * Pack it into out_data if we were doing an out-of-place transform.
437 p2 = (real *)out_data;
439 for (i = 0; i < nx; i++)
441 for (j = 0; j < ny; j++)
443 p2[i*ny+j] = p1[i*nyc*2+j];
450 gmx_fatal(FARGS, "FFT plan mismatch - bad plan or direction.");
458 gmx_fft_destroy(gmx_fft_t fft)
463 if (fft->next != NULL)
465 gmx_fft_destroy(fft->next);
471 void gmx_fft_cleanup()
474 #endif /* GMX_FFT_FFTPACK */