-
Notifications
You must be signed in to change notification settings - Fork 1
/
inplace_r2c_Poisson_example.cpp
78 lines (63 loc) · 1.77 KB
/
inplace_r2c_Poisson_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/// This program performs a 3d real-to-complex in place FFT,
/// and then solves the Poisson equation in Fourierspace and then backtransforms.
#include <cmath>
#include <iostream>
#include <fftw3.h>
#define SIZE 8
#define NXX SIZE
#define NYY SIZE
#define NZZ SIZE
int main()
{
double Pi = M_PI;
/// Grid size
int Nx=NXX,Ny=NYY,Nz=NZZ,Nzh=(Nz/2+1);
/// Declare FFTW components.
fftw_complex *mem;
fftw_complex *out;
double *in;
mem = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * Nx * Ny * Nzh);
out = mem;
in = mem[0];
fftw_plan fwrd = fftw_plan_dft_r2c_3d(Nx,Ny,Nz,in,out,FFTW_MEASURE);
fftw_plan bwrd = fftw_plan_dft_c2r_3d(Nx,Ny,Nz,out,in,FFTW_MEASURE);
double L0=0.0;
double L1=2*Pi;
double xlen = (L1-L0);
fftw_execute(fwrd);
int II,JJ;
double k1,k2,k3;
for (int i=0;i<Nx;i++)
{
if (2*i<Nx)
II = i;
else
II = Nx-i;
k1 = 2*Pi*II/xlen;
for (int j=0;j<Ny;j++)
{
if (2*j<Ny)
JJ = j;
else
JJ = Ny-j;
k2 = 2*Pi*JJ/xlen;
for (int k=0;k<Nzh;k++)
{
k3 = 2*Pi*k/xlen;
double fac = -1.0*(pow(k1,2)+pow(k2,2)+pow(k3,2));
if (fabs(fac) < 1e-14)
{
out[k+Nzh*(j+Ny*i)][0] = 0.0;
out[k+Nzh*(j+Ny*i)][1] = 0.0;
}
else
{
out[k+Nzh*(j+Ny*i)][0] /= fac;
out[k+Nzh*(j+Ny*i)][1] /= fac;
}
}
}
}
fftw_execute(bwrd);
return 0;
}