-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFFT_potential.c
More file actions
201 lines (156 loc) · 6.55 KB
/
Copy pathFFT_potential.c
File metadata and controls
201 lines (156 loc) · 6.55 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/********************************************************************
NAME: potential
FUNCTION: Calculates the potential of the contrast density in the k-space (from the FFT)in the grid[m] order, and then with an IFFT calculates the potential in the real space
INPUT: density contrast in the k-space
RETURN: File with the input and outputs (sorted in the grid[m] order)
**********************************************************************/
int potential()
{
int m, i, j, k;
double factor;
FILE *pf=NULL;
/*+++++ Computing the potential in k-space +++++*/
printf("Computing potential in k-space\n");
printf("-----------------------------------------\n");
/*----- Factor of the potential -----*/
factor = (-3.0/2.0) * (GV.H0*GV.H0) * GV.Omega_M0 / GV.a_SF;
for(m=0; m<GV.NTOTK; m++)
{
if( gp[m].k_module > GV.ZERO )
{
gp[m].poten_k[0] = factor * gp[m].DenCon_K[0]/(gp[m].k_module * gp[m].k_module); //Re()
gp[m].poten_k[1] = factor * gp[m].DenCon_K[1]/(gp[m].k_module * gp[m].k_module); //Im()
}//if
else
{
gp[m].poten_k[0] = 0.0; //Re()
gp[m].poten_k[1] = 0.0; //Im()
}//else
}//for m
printf("Potential in k-space saved!\n");
printf("-----------------------------------------\n");
#ifdef OUTOFPLACE
/*+++++ Definitions +++++*/
fftw_complex *in=NULL;
double *out=NULL;
fftw_complex *in2=NULL;
fftw_plan plan_k2r; // FFTW from k-space to r-space
fftw_plan plan_r2k; // FFTW from r-space to k-space
/*----- Creating input/output arrays -----*/
in = (fftw_complex *) fftw_malloc( sizeof( fftw_complex ) * GV.NTOTK );
out = (double *) fftw_malloc( sizeof( double ) * GV.NTOTALCELLS );
/*----- Saving the potential input of the FFT -----*/
for(m=0; m<GV.NTOTK; m++)
{
in[m][0] = gp[m].poten_k[0]; //Re()
in[m][1] = gp[m].poten_k[1]; //Im()
}//for m
/*----- Making the FFT -----*/
//plan_k2r = fftw_plan_dft_c2r_3d( GV.NCELLS, GV.NCELLS, GV.NCELLS, in, out, FFTW_ESTIMATE ); //out-of-place transform
plan_k2r = fftw_plan_dft_c2r_3d( GV.NCELLS, GV.NCELLS, GV.NCELLS, in, out, FFTW_MEASURE ); //out-of-place transform
fftw_execute( plan_k2r );
printf("FFT potential k2r finished!\n");
printf("---------------------------------------\n");
/*----- Saving the output data for an out-of-place transform -----*/
pf = fopen("./../Potential_r_out.dat", "w");
fprintf(pf, "%s%14s %15s\n", "#", "ID", "Poten(r)");
for( m=0; m<GV.NTOTALCELLS; m++ )
{
gp[m].poten_r = out[m]/GV.NORM; //Re()
fprintf(pf,
"%10d %16.8lf\n",
m, gp[m].poten_r );
}//for m
fclose(pf);
printf("---------------------------------------\n");
printf("Potential in r space from an out-of-place transform saved!\n");
printf("---------------------------------------\n");
/*----- Recreating input array -----*/
in2 = (fftw_complex *) fftw_malloc( sizeof( fftw_complex ) * GV.NTOTK );
plan_r2k = fftw_plan_dft_r2c_3d( GV.NCELLS, GV.NCELLS, GV.NCELLS, out, in2, FFTW_ESTIMATE );
fftw_execute( plan_r2k );
printf("Recreated input of potential in k-space from out-of-place transform performed\n");
printf("---------------------------------------\n");
/*+++++ Saving recreated data +++++*/
pf = fopen("./../Testing_potential_k_space.dat", "w");
fprintf(pf, "%s%14s %15s %15s %15s\n", "#", "Re_Pot(k)", "Im_Pot(k)", "Reconst_Re", "Reconst_Im");
for(m=0; m<GV.NTOTK; m++)
{
fprintf(pf,
"%10d %20.8lf %20.8lf %20.8lf %20.8lf\n",
m,
gp[m].poten_k[0], gp[m].poten_k[1],
in2[m][0]/(GV.NORM*GV.NORM), in2[m][1]/(GV.NORM*GV.NORM));
}//for m
fclose(pf),
printf("Recreated input of potential in k-space from out-of-place transform saved\n");
printf("---------------------------------------\n");
fftw_destroy_plan( plan_k2r );
fftw_destroy_plan( plan_r2k );
fftw_free( in );
fftw_free( in2 );
fftw_free( out );
#endif
#ifdef INPLACE
/*+++++ Definitions +++++*/
fftw_complex *in=NULL;
fftw_plan plan_k2r; // FFTW from k-space to r-space
fftw_plan plan_r2k; // FFTW from r-space to k-space
/*----- Creating input/output arrays -----*/
in = (fftw_complex *) fftw_malloc( sizeof( fftw_complex ) * GV.NTOTALCELLS );
/*----- Saving the potential input of the FFT -----*/
for(m=0; m<GV.NTOTK; m++)
{
in[m][0] = gp[m].poten_k[0]; //Re()
in[m][1] = gp[m].poten_k[1]; //Im()
}//for m
/*----- Making the FFT -----*/
//plan_k2r = fftw_plan_dft_c2r_3d( GV.NCELLS, GV.NCELLS, GV.NCELLS, in, (double *)in, FFTW_ESTIMATE ); //in-place transform
plan_k2r = fftw_plan_dft_c2r_3d( GV.NCELLS, GV.NCELLS, GV.NCELLS, in, (double *)in, FFTW_MEASURE ); //in-place transform
//plan_k2r = fftw_plan_dft_c2r_3d( GV.NCELLS, GV.NCELLS, GV.NCELLS, in, (double *)in, FFTW_IN_PLACE ); //in-place transform
fftw_execute( plan_k2r );
/*----- Saving the output data for an in-place transform -----*/
for( m=0; m<GV.NTOTALCELLS/2; m++ )
{
gp[2*m].poten_r = in[m][0]/GV.NORM;
gp[2*m+1].poten_r = in[m][1]/GV.NORM;
}//for m
pf = fopen("./../Potential_r_in.dat", "w");
fprintf(pf, "%s%14s %15s\n", "#", "ID", "Poten(r)");
for( m=0; m<GV.NTOTALCELLS; m++ )
{
fprintf(pf,
"%10d %16.8lf\n",
m, gp[m].poten_r );
}//for m
fclose(pf);
//fftw_free( in );
/*----- Recreating input array -----*/
//in = (double *) fftw_malloc( sizeof( double ) * GV.PADDING );
plan_r2k = fftw_plan_dft_r2c_3d( GV.NCELLS, GV.NCELLS, GV.NCELLS, (double*) in, in, FFTW_ESTIMATE );
//plan_r2k = fftw_plan_dft_r2c_3d( GV.NCELLS, GV.NCELLS, GV.NCELLS, in, (fftw_complex*)in, FFTW_IN_PLACE );
fftw_execute( plan_r2k );
printf("Recreated input of potential in k-space from out-of-place transform performed\n");
printf("---------------------------------------\n");
/*+++++ Saving recreated data +++++*/
pf = fopen("./../Testing_potential_k_space_in-place.dat", "w");
fprintf(pf, "%s%14s %15s %15s %15s\n", "#", "Re_Pot(k)", "Im_Pot(k)", "Reconst_Re", "Reconst_Im");
for(m=0; m<GV.NTOTK; m++)
{
fprintf(pf,
"%10d %20.8lf %20.8lf %20.8lf %20.8lf\n",
m,
gp[m].poten_k[0], gp[m].poten_k[1],
in[m][0]/(GV.NORM*GV.NORM), in[m][1]/(GV.NORM*GV.NORM));
}//for m
fclose(pf),
printf("Recreated input of potential in k-space from out-of-place transform saved\n");
printf("---------------------------------------\n");
fftw_free( in );
fftw_destroy_plan( plan_k2r );
fftw_destroy_plan( plan_r2k );
#endif
printf("FFT_potential code finished!\n");
printf("----------------------------\n");
return 0;
}//potential