178 |
kaklik |
1 |
#include <fftw3.h> |
|
|
2 |
#include <boost/array.hpp> |
|
|
3 |
|
|
|
4 |
namespace mimas { |
|
|
5 |
|
|
|
6 |
// Helping function-object for fft and invfft. |
|
|
7 |
template< typename Real > |
|
|
8 |
struct dft_ |
|
|
9 |
{ |
|
|
10 |
void operator()( int rank, const int *n, const std::complex< Real > *in, |
|
|
11 |
std::complex< Real > *out, int sign ); |
|
|
12 |
}; |
|
|
13 |
|
|
|
14 |
template< typename Real, std::size_t Dim, typename TPtr > |
|
|
15 |
boost::multi_array< std::complex< Real >, Dim > fft_t< Real, Dim, TPtr >::operator() |
|
|
16 |
( const boost::const_multi_array_ref< std::complex< Real >, Dim, TPtr > &field ) |
|
|
17 |
{ |
|
|
18 |
boost::array< int, Dim > shape; |
|
|
19 |
std::copy( field.shape(), field.shape() + Dim, shape.begin() ); |
|
|
20 |
|
|
|
21 |
boost::multi_array< std::complex< Real >, Dim > retVal( shape ); |
|
|
22 |
|
|
|
23 |
dft_< Real > dft; |
|
|
24 |
dft( Dim, shape.begin(), field.data(), retVal.data(), -1 ); |
|
|
25 |
|
|
|
26 |
return retVal; |
|
|
27 |
} |
|
|
28 |
|
|
|
29 |
template< typename Real, std::size_t Dim, typename TPtr > |
|
|
30 |
boost::multi_array< std::complex< Real >, Dim > invfft_t< Real, Dim, TPtr >::operator() |
|
|
31 |
( const boost::const_multi_array_ref< std::complex< Real >, Dim, TPtr > &field ) |
|
|
32 |
{ |
|
|
33 |
boost::array< int, Dim > shape; |
|
|
34 |
std::copy( field.shape(), field.shape() + Dim, shape.begin() ); |
|
|
35 |
|
|
|
36 |
boost::multi_array< std::complex< Real >, Dim > retVal( shape ); |
|
|
37 |
|
|
|
38 |
dft_< Real > dft; |
|
|
39 |
dft( Dim, shape.begin(), field.data(), retVal.data(), +1 ); |
|
|
40 |
|
|
|
41 |
return retVal * std::complex< Real >( 1.0 / retVal.num_elements() ); |
|
|
42 |
} |
|
|
43 |
|
|
|
44 |
// Helping function-object for rfft. |
|
|
45 |
template< typename Real > |
|
|
46 |
struct dft_r2c_ |
|
|
47 |
{ |
|
|
48 |
void operator()( int rank, const int *n, const Real *in, |
|
|
49 |
std::complex< Real > *out ); |
|
|
50 |
}; |
|
|
51 |
|
|
|
52 |
template< typename Real, std::size_t Dim, typename TPtr > |
|
|
53 |
boost::multi_array< std::complex< Real >, Dim > rfft_t< Real, Dim, TPtr >::operator() |
|
|
54 |
( const boost::const_multi_array_ref< Real, Dim, TPtr > &field ) |
|
|
55 |
{ |
|
|
56 |
assert( field.shape()[ Dim - 1 ] % 2 == 0 ); |
|
|
57 |
|
|
|
58 |
boost::array< int, Dim > shape1, shape2; |
|
|
59 |
std::copy( field.shape(), field.shape() + Dim, shape1.begin() ); |
|
|
60 |
std::copy( field.shape(), field.shape() + Dim, shape2.begin() ); |
|
|
61 |
shape1[ Dim - 1 ] = shape1[ Dim - 1 ] / 2 + 1; |
|
|
62 |
|
|
|
63 |
boost::multi_array< std::complex< Real >, Dim > retVal( shape1 ); |
|
|
64 |
|
|
|
65 |
dft_r2c_< Real > dft_r2c; |
|
|
66 |
dft_r2c( Dim, shape2.begin(), field.data(), retVal.data() ); |
|
|
67 |
|
|
|
68 |
return retVal; |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
// Helping function-object for invrfft. |
|
|
72 |
template< typename Real > |
|
|
73 |
struct dft_c2r_ |
|
|
74 |
{ |
|
|
75 |
void operator()( int rank, const int *n, const std::complex< Real > *in, |
|
|
76 |
Real *out ); |
|
|
77 |
}; |
|
|
78 |
|
|
|
79 |
template< typename Real, std::size_t Dim, typename TPtr > |
|
|
80 |
boost::multi_array< Real, Dim > invrfft_t< Real, Dim, TPtr >::operator() |
|
|
81 |
( const boost::const_multi_array_ref< std::complex< Real >, Dim, TPtr > &field ) |
|
|
82 |
{ |
|
|
83 |
// Doesn't preserve input. |
|
|
84 |
boost::multi_array< std::complex< Real >, Dim > copy( field ); |
|
|
85 |
|
|
|
86 |
boost::array< int, Dim > shape; |
|
|
87 |
std::copy( field.shape(), field.shape() + Dim, shape.begin() ); |
|
|
88 |
shape[ Dim - 1 ] = ( shape[ Dim - 1 ] - 1 ) * 2; |
|
|
89 |
|
|
|
90 |
boost::multi_array< Real, Dim > retVal( shape ); |
|
|
91 |
|
|
|
92 |
dft_c2r_< Real > dft_c2r; |
|
|
93 |
dft_c2r( Dim, shape.begin(), copy.data(), retVal.data() ); |
|
|
94 |
|
|
|
95 |
return retVal * (Real)( 1.0 / retVal.num_elements() ); |
|
|
96 |
} |
|
|
97 |
|
|
|
98 |
} |