CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   FFT in OpenFOAM (

Hisham November 3, 2011 04:56

Dear Foamers,

I have been trying to use the fft class. I have hard time understanding how the code checks for a 2^n number of samples. The code uses (I think) the bitwise "and" operator:

from fft.C

00048    forAll(nn, idim)
00049    {
00050        // Check for power of two
00051        unsigned int dimCount = nn[idim];
00052        if (!dimCount || (dimCount & (dimCount - 1)))
00053        {
00054            FatalErrorIn
00055            (
00056                  "fft::transform(complexField&, const labelList&, "
00057                  "transformDirection)"
00058            )  << "number of elements in direction " << idim
00059                << " is not a power of 2" << endl
00060                << "    Number of elements in each direction = " << nn
00061                << abort(FatalError);
00062        }
00063    }

So can I use that too to do zero padding?

I would appreciate it if the complementary code for using the fft class is shared. Is there any?

I mean zero-padding or whatever trick to prepare data for FFT (i.e. the 2^n). Also, it would be nice to look at post fft code.

Best regards,

Hisham November 3, 2011 07:57

I got it!



Comparing results from FOAM's fft class with Octave, there is something wrong. The results are not even close and Octave numbers look logical. Has anyone experienced problems with fft output??

Hisham November 13, 2011 18:13

Hi Foamers,

After using the fft class, it does not give the same results as Matlab or Octave. As a simple example I fft the following:
8.0000 ]

The results from Octave and Matlab are identical. OF is not near the results of either.

To do the fft, I make all imaginary components zeros. I pad zeros until 2^n. Then I make the call with the label list size = 1 and the only label set to the number of elements = 2^n.

Doing an ifft of the results from OF with the OF class, I get the original. Comparing ifft in OF with external sources. OF results are not good even when I construct the signal from cosine components.

I have some doubts about fft in OF. Can someone please help???

Best regards,

Matlab magnitude

OF's fft class magnitude

lishijie March 15, 2012 08:47

Hi,i am a student,i want to make the fft lib faster, but i don't know how can i run a program with fft lib, could you tell me which case will use fft lib?thanks

Hisham March 16, 2012 05:55

Hi lishijie

Unfortunately, I believe that the fft class in OpenFOAM is bugged because it gives different results compared to Matlab and Octave as I illustrated in my previous post.

To know how to use the class, please read the numerical recipes book (the fft part very clear indeed). Take care that the fft is the full implementation "for a complex data set".

Another option is to link an external fft class (e.g. fftw or fftw++). I have not done that because I am now involved with something else. Nevertheless, there are posts here about the topic.

Not many (any afaik) solvers use the fft class.

Good luck and please keep me updated with your progress in this area

Best regards

Edit: this post should be useful:

kmooney March 19, 2012 18:52

If your interested in fast, accurate FFTs, the FFTW lib is probably the best choice. I have some simple C code that loads and runs fftw if you would like to see some syntax. Just send me a PM with your email address and I can send it over if you want. I'm not sure if it would be difficult or not to call it from within foam.

Hisham March 19, 2012 19:32

An update: The dnsFoam solver uses the fft class.

@ kmooney: This is extremely nice of you :)

lishijie March 20, 2012 06:17

Oh~ it is so nice of you! I will try to make fft on GPU, thank you so much!!!

anishtain4 July 2, 2012 13:59

Hisham can you tell me how you test openfoam fft? you made a custom solver? or you are approaching classes directly?

Hisham July 6, 2012 06:58

Hi anishtain4

Sorry for the late reply .... Yes I made a test solver (say fftFoam) and tested some numbers on an fft object. I can email it to you if you would like


anishtain4 July 6, 2012 12:28

I would be grateful if you do that

raw17 October 23, 2013 11:51

I am tring to do the fft of channel flow velocity data in Openfoam. I use fftW library to perform that. This is the procedure

1. I do the fourier tranform of data.
2. To check whether its right or not I perform the inverse fft and cross check with the original velocity field of Openfoam. These two fields are not mathcing after 4th significan digit.
3. When I do the same for another code that is spectral I get very high accuracy with FFTw.

Can any one explain this to me. I am sure that my fftW subroutine is good. Then why is that fft and ifft velocity field of Openfoam does not match after 4th significant digit.

Bernhard October 24, 2013 01:48

You need to write the Fourier coefficients with higher precision. You probably truncate these, which is loss of information.

raw17 October 24, 2013 05:18


Originally Posted by Bernhard (Post 458659)
You need to write the Fourier coefficients with higher precision. You probably truncate these, which is loss of information.

Hello Bernhard,
Thanks for your reply. I use write precision 16 to write my velocity field in Openfoam. In my fortran code I read that file and just perform fft and ifft and write velocity field again without writing the fortier coefficients in a data file. This is a issue for me because I have to use newton method on the velocity field in fourier space and whatever correction I will do to my solution will be lost when I will perform ifft to write the data for Openfoam.


All times are GMT -4. The time now is 16:56.