1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
27 #include "fftRenumber.H"
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 #define SWAP(a,b) tempr=(a); (a)=(b); (b)=tempr
37 #define TWOPI 6.28318530717959
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
45 transformDirection isign
50 // Check for power of two
51 unsigned int dimCount = nn[idim];
52 if (!dimCount || (dimCount & (dimCount - 1)))
56 "fft::transform(complexField&, const labelList&, "
58 ) << "number of elements in direction " << idim
59 << " is not a power of 2" << endl
60 << " Number of elements in each direction = " << nn
65 const label ndim = nn.size();
67 label i1, i2, i3, i2rev, i3rev, ip1, ip2, ip3, ifp1, ifp2;
68 label ibit, k1, k2, n, nprev, nrem, idim;
70 scalar theta, wi, wpi, wpr, wr, wtemp;
71 scalar* data = reinterpret_cast<scalar*>(field.begin()) - 1;
74 // if inverse transform : renumber before transform
76 if (isign == REVERSE_TRANSFORM)
78 fftRenumber(field, nn);
91 for (idim=ndim; idim>=1; idim--)
94 nrem = ntot/(n*nprev);
100 for (i2=1; i2<=ip2; i2+=ip1)
104 for (i1=i2; i1<=i2 + ip1 - 2; i1+=2)
106 for (i3=i1; i3<=ip3; i3+=ip2)
108 i3rev = i2rev + i3 - i2;
109 SWAP(data[i3], data[i3rev]);
110 SWAP(data[i3 + 1], data[i3rev + 1]);
116 while (ibit >= ip1 && i2rev > ibit)
130 theta = isign*TWOPI/(ifp2/ip1);
131 wtemp = sin(0.5*theta);
132 wpr = -2.0*wtemp*wtemp;
137 for (i3 = 1; i3 <= ifp1; i3 += ip1)
139 for (i1 = i3; i1 <= i3 + ip1 - 2; i1 += 2)
141 for (i2 = i1; i2 <= ip3; i2 += ifp2)
145 tempr = scalar(wr*data[k2]) - scalar(wi*data[k2 + 1]);
146 tempi = scalar(wr*data[k2 + 1]) + scalar(wi*data[k2]);
147 data[k2] = data[k1] - tempr;
148 data[k2 + 1] = data[k1 + 1] - tempi;
150 data[k1 + 1] += tempi;
154 wr = (wtemp = wr)*wpr - wi*wpi + wr;
155 wi = wi*wpr + wtemp*wpi + wi;
163 // if forward transform : renumber after transform
165 if (isign == FORWARD_TRANSFORM)
167 fftRenumber(field, nn);
171 // scale result (symmetric scale both forward and inverse transform)
173 scalar recRootN = 1.0/sqrt(scalar(ntot));
177 field[i] *= recRootN;
182 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
187 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
189 tmp<complexField> fft::forwardTransform
191 const tmp<complexField>& tfield,
195 tmp<complexField> tfftField(new complexField(tfield));
197 transform(tfftField(), nn, FORWARD_TRANSFORM);
205 tmp<complexField> fft::reverseTransform
207 const tmp<complexField>& tfield,
211 tmp<complexField> tifftField(new complexField(tfield));
213 transform(tifftField(), nn, REVERSE_TRANSFORM);
221 tmp<complexVectorField> fft::forwardTransform
223 const tmp<complexVectorField>& tfield,
227 tmp<complexVectorField> tfftVectorField
229 new complexVectorField
235 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
237 tfftVectorField().replace
240 forwardTransform(tfield().component(cmpt), nn)
246 return tfftVectorField;
250 tmp<complexVectorField> fft::reverseTransform
252 const tmp<complexVectorField>& tfield,
256 tmp<complexVectorField> tifftVectorField
258 new complexVectorField
264 for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
266 tifftVectorField().replace
269 reverseTransform(tfield().component(cmpt), nn)
275 return tifftVectorField;
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
281 } // End namespace Foam
283 // ************************************************************************* //