-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscalarps.c
More file actions
193 lines (135 loc) · 3.72 KB
/
Copy pathscalarps.c
File metadata and controls
193 lines (135 loc) · 3.72 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
/** @file scalarps.c
*
* Calculates power spectrum of a scalar field.
*/
#include "hydro.h"
#ifdef FFT
/** Compute and bin the power spectrum of the supplied scalar field in
* momentum space, and write the power spectrum to a file.
*
* Computes the power spectrum of supplied field in fourier space, and
* stores it binned and labelled with the timestep in a file.
*
* See fft_func.c for the `fft_field` functions to transform fields
* into fourier space. The momentum space fields are decomposed in pencils.
*
* For the calculation of vector field power spectra, see vectorps.c
* For tensor power spetra, see tensorps.c
*
*/
void scalarps(hydro_params p, fftwf_complex *field, int step, char *label) {
MPI_Status status;
float modsq;
long ksitesq;
int x, y, z;
int i;
float start = clock();
ptrdiff_t alloc_local, x_thickness, x_start;
ptrdiff_t n0 = p.Lx;
ptrdiff_t n1 = p.Ly;
ptrdiff_t n2 = p.Lz;
MPI_Comm fftw_comm;
int stride = (p.size > p.Lx) ? ((int)(p.size/p.Lx)) : 1;
int color = p.rank%stride;
MPI_Comm_split(MPI_COMM_WORLD, color, p.rank,
&fftw_comm);
if(color == 0) {
alloc_local = fftwf_mpi_local_size_3d(n0, n1, n2,
fftw_comm,
&x_thickness,
&x_start);
} else{
alloc_local = 0;
x_thickness = 0;
x_start = 0;
}
// Now we perform binning
float fft_norm = (1.0/(((float)p.Lx)*((float)p.Ly)*((float)p.Lz)));
int nbins = minof3_int(p.Lx, p.Ly, p.Lz);
float mink = 0.0;
float maxk = 2.0*M_PI;
float dk = (maxk-mink)/((float)nbins);
float *bins = (float *)malloc(nbins*sizeof(float));
int *counts = (int *)malloc(nbins*sizeof(int));
for(i=0;i<nbins;i++) {
bins[i] = 0.0;
counts[i] = 0;
}
int whichbin;
int true_x, true_y, true_z;
for(x=0;x<x_thickness;x++) {
for(y=0;y<p.Ly;y++) {
for(z=0;z<p.Lz;z++) {
/*
if(((x+((int)x_start))>p.Lx/2) || (y> p.Ly/2) || (z>p.Lz/2))
continue;
*/
if(x+x_start > p.Lx/2)
true_x = p.Lx - (x+x_start);
else
true_x = x+x_start;
if(y > p.Ly/2)
true_y = p.Ly - y;
else
true_y = y;
if(z > p.Lz/2)
true_z = p.Lz - z;
else
true_z = z;
// For binning we use momentum space index
ksitesq = true_x*true_x + true_y*true_y + true_z*true_z;
modsq = field[x*p.Ly*p.Lz + y*p.Lz + z][0]
*field[x*p.Ly*p.Lz + y*p.Lz + z][0]
+ field[x*p.Ly*p.Lz + y*p.Lz + z][1]
*field[x*p.Ly*p.Lz + y*p.Lz + z][1];
whichbin = (int)sqrt(ksitesq);
bins[whichbin] += modsq;
counts[whichbin]++;
}
}
}
float red_value;
int red_count;
for(i=0;i<nbins;i++) {
red_value = reduce_sum(bins[i], p);
red_count = reduce_sum_int(counts[i], p);
// Do normalisation here because we don't do it during fft_scalar...
bins[i] = red_value*(fft_norm*fft_norm);
counts[i] = red_count;
}
float thisk = dk/2.0;
if(!p.rank) {
char fftdest[200];
if(label != NULL){
if(*label){
sprintf(fftdest,"%s-ps-step%d.txt",label,step);
}
}
else{
sprintf(fftdest,"ps-step%d.txt",step);
}
FILE *fp = fopen(fftdest,"w");
for(i=0;i<nbins;i++) {
char fftdest[200];
fprintf(fp, "%lf %g %d\n",
thisk/(p.dx), bins[i], counts[i]);
thisk = thisk + dk;
}
fclose(fp);
}
free(bins);
free(counts);
MPI_Comm_free(&fftw_comm);
float end = clock();
if(label != NULL){
if(*label){
printf0(p, "%s scalar power spectrum calculation took %lf\n", label,
((float) (end - start)) / CLOCKS_PER_SEC);
}
}
else{
printf0(p, "scalar power spectrum calculation took %lf\n",
((float) (end - start)) / CLOCKS_PER_SEC);
}
}
#endif // FFT