OpenFOAM logo
The Open Source CFD Toolbox
  Source Guide OpenCFD Solutions Contact OpenFOAM

fft.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 \*---------------------------------------------------------------------------*/
00026 
00027 #include "fft.H"
00028 #include "fftRenumber.H"
00029 
00030 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00031 
00032 namespace Foam
00033 {
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 #define SWAP(a,b) tempr=(a); (a)=(b); (b)=tempr
00038 #define TWOPI 6.28318530717959
00039 
00040 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00041 
00042 void fft::transform
00043 (
00044     complexField& field,
00045     const labelList& nn,
00046     transformDirection isign
00047 )
00048 {
00049     forAll(nn, idim)
00050     {
00051         // Check for power of two
00052         unsigned int dimCount = nn[idim];
00053         if (!dimCount || (dimCount & (dimCount - 1)))
00054         {
00055             FatalErrorIn
00056             (
00057                  "fft::transform(complexField&, const labelList&, "
00058                  "transformDirection)"
00059             )   << "number of elements in direction " << idim
00060                 << " is not a power of 2" << endl
00061                 << "    Number of elements in each direction = " << nn
00062                 << abort(FatalError);
00063         }
00064     }
00065 
00066     const label ndim = nn.size();
00067 
00068     label i1, i2, i3, i2rev, i3rev, ip1, ip2, ip3, ifp1, ifp2;
00069     label ibit, k1, k2, n, nprev, nrem, idim;
00070     scalar tempi, tempr;
00071     scalar theta, wi, wpi, wpr, wr, wtemp;
00072     scalar* data = reinterpret_cast<scalar*>(field.begin()) - 1;
00073 
00074 
00075     // if inverse transform : renumber before transform
00076 
00077     if (isign == REVERSE_TRANSFORM)
00078     {
00079         fftRenumber(field, nn);
00080     }
00081 
00082 
00083     label ntot = 1;
00084     forAll(nn, idim)
00085     {
00086         ntot *= nn[idim];
00087     }
00088 
00089 
00090     nprev = 1;
00091 
00092     for (idim=ndim; idim>=1; idim--)
00093     {
00094         n = nn[idim-1];
00095         nrem = ntot/(n*nprev);
00096         ip1 = nprev << 1;
00097         ip2 = ip1*n;
00098         ip3 = ip2*nrem;
00099         i2rev = 1;
00100 
00101         for (i2=1; i2<=ip2; i2+=ip1)
00102         {
00103             if (i2 < i2rev)
00104             {
00105                 for (i1=i2; i1<=i2 + ip1 - 2; i1+=2)
00106                 {
00107                     for (i3=i1; i3<=ip3; i3+=ip2)
00108                     {
00109                         i3rev = i2rev + i3 - i2;
00110                         SWAP(data[i3], data[i3rev]);
00111                         SWAP(data[i3 + 1], data[i3rev + 1]);
00112                     }
00113                 }
00114             }
00115 
00116             ibit = ip2 >> 1;
00117             while (ibit >= ip1 && i2rev > ibit)
00118             {
00119                 i2rev -= ibit;
00120                 ibit >>= 1;
00121             }
00122 
00123             i2rev += ibit;
00124         }
00125 
00126         ifp1 = ip1;
00127 
00128         while (ifp1 < ip2)
00129         {
00130             ifp2 = ifp1 << 1;
00131             theta = isign*TWOPI/(ifp2/ip1);
00132             wtemp = sin(0.5*theta);
00133             wpr = -2.0*wtemp*wtemp;
00134             wpi = sin(theta);
00135             wr = 1.0;
00136             wi = 0.0;
00137 
00138             for (i3 = 1; i3 <= ifp1; i3 += ip1)
00139             {
00140                 for (i1 = i3; i1 <= i3 + ip1 - 2; i1 += 2)
00141                 {
00142                     for (i2 = i1; i2 <= ip3; i2 += ifp2)
00143                     {
00144                         k1 = i2;
00145                         k2 = k1 + ifp1;
00146                         tempr = scalar(wr*data[k2]) - scalar(wi*data[k2 + 1]);
00147                         tempi = scalar(wr*data[k2 + 1]) + scalar(wi*data[k2]);
00148                         data[k2] = data[k1] - tempr;
00149                         data[k2 + 1] = data[k1 + 1] - tempi;
00150                         data[k1] += tempr;
00151                         data[k1 + 1] += tempi;
00152                     }
00153                 }
00154 
00155                 wr = (wtemp = wr)*wpr - wi*wpi + wr;
00156                 wi = wi*wpr + wtemp*wpi + wi;
00157             }
00158             ifp1 = ifp2;
00159         }
00160         nprev *= n;
00161     }
00162 
00163 
00164     // if forward transform : renumber after transform
00165 
00166     if (isign == FORWARD_TRANSFORM)
00167     {
00168         fftRenumber(field, nn);
00169     }
00170 
00171 
00172     // scale result (symmetric scale both forward and inverse transform)
00173 
00174     scalar recRootN = 1.0/sqrt(scalar(ntot));
00175 
00176     forAll(field, i)
00177     {
00178         field[i] *= recRootN;
00179     }
00180 }
00181 
00182 
00183 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00184 
00185 #undef SWAP
00186 #undef TWOPI
00187 
00188 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00189 
00190 tmp<complexField> fft::forwardTransform
00191 (
00192     const tmp<complexField>& tfield,
00193     const labelList& nn
00194 )
00195 {
00196     tmp<complexField> tfftField(new complexField(tfield));
00197 
00198     transform(tfftField(), nn, FORWARD_TRANSFORM);
00199 
00200     tfield.clear();
00201 
00202     return tfftField;
00203 }
00204 
00205 
00206 tmp<complexField> fft::reverseTransform
00207 (
00208     const tmp<complexField>& tfield,
00209     const labelList& nn
00210 )
00211 {
00212     tmp<complexField> tifftField(new complexField(tfield));
00213 
00214     transform(tifftField(), nn, REVERSE_TRANSFORM);
00215 
00216     tfield.clear();
00217 
00218     return tifftField;
00219 }
00220 
00221 
00222 tmp<complexVectorField> fft::forwardTransform
00223 (
00224     const tmp<complexVectorField>& tfield,
00225     const labelList& nn
00226 )
00227 {
00228     tmp<complexVectorField> tfftVectorField
00229     (
00230         new complexVectorField
00231         (
00232             tfield().size()
00233         )
00234     );
00235 
00236     for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00237     {
00238         tfftVectorField().replace
00239         (
00240             cmpt,
00241             forwardTransform(tfield().component(cmpt), nn)
00242         );
00243     }
00244 
00245     tfield.clear();
00246 
00247     return tfftVectorField;
00248 }
00249 
00250 
00251 tmp<complexVectorField> fft::reverseTransform
00252 (
00253     const tmp<complexVectorField>& tfield,
00254     const labelList& nn
00255 )
00256 {
00257     tmp<complexVectorField> tifftVectorField
00258     (
00259         new complexVectorField
00260         (
00261             tfield().size()
00262         )
00263     );
00264 
00265     for (direction cmpt=0; cmpt<vector::nComponents; cmpt++)
00266     {
00267         tifftVectorField().replace
00268         (
00269             cmpt,
00270             reverseTransform(tfield().component(cmpt), nn)
00271         );
00272     }
00273 
00274     tfield.clear();
00275 
00276     return tifftVectorField;
00277 }
00278 
00279 
00280 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00281 
00282 } // End namespace Foam
00283 
00284 // ************************************************************************* //
Copyright © 2000-2009 OpenCFD Ltd