forked from sbird/GenPK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen-pk.cpp
367 lines (358 loc) · 14 KB
/
gen-pk.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/* Copyright (c) 2009,2010 Simeon Bird <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
/** \mainpage
* \section intro_sec Introduction
* MatPow is a 3D matter power spectrum estimator, written in C++ and parallelised with OpenMP.
*
* \section feat_sec Features
*
* A matter power spectrum estimator, which can read most Gadget-I and II format files through the use of the
* GadgetReader library. Calculates the matter power spectrum quickly, as well as the expected error from
* cosmic variance.
*
* Outputs one file per particle type found.
*
* \section missing_sec Missing Features
*
* Should rebin to ensure enough modes per bin
*
* Use Volker Springel's fold-on-itself to calculate small-scale power below the Nyquist frequency
*
* \section usage_sec Usage
*
* See:
* ./gen-pk --help
*
* \section req_sec Requirements
* A C++ compiler with map, vector, set getopt, and stdint.h
*
* GadgetReader library, preferably installed in ../GadgetReader
* (edit the first line of the Makefile for other locations)
*
* FFTW3
*
* Boost::Test library >= 1.34 for the test suite
*/
#include "gen-pk.h"
#include <string.h>
#include <math.h>
//For getopt
#include <unistd.h>
//For omp_get_num_procs
#ifdef _OPENMP
#include <omp.h>
#else
#warning "You are compiling with OpenMP disabled (probably Apple clang). This will be slow! Try installing gcc."
#endif
#include <stdlib.h>
/** Maximal size of FFT grid.
* In practice 1024 means we need just over 4GB, as sizeof(float)=4*/
#define FIELD_DIMS 3072L
using namespace GadgetReader;
using namespace std;
/** \file
* File containing main() */
/** Main function. Accepts arguments, uses GadgetReader to open the snapshot, prints some header info,
* allocates FFT memory and creates a plan, calls read_fieldize and powerspectrum, prints the P(k)
* to a file, and then frees the memory*/
int main(int argc, char* argv[])
{
int64_t field_dims=0;
int type;
GENFLOAT *field;
double *power, *keffs;
int *count;
int64_t npart_total[N_TYPE];
double mass[N_TYPE];
string infiles(""),jinfiles(""),outdir("");
char c;
int crosstype = -1;
bool stars_are_baryons = false;
double box;
double Omega0;
GSnap * snap = NULL;
bool use_hdf5 = false;
bool use_bigfile = false;
fftw_plan pl;
fftw_complex *outfield;
while((c = getopt(argc, argv, "i:j:o:c:s:h")) !=-1){
switch(c){
case 'o':
outdir=static_cast<string>(optarg);
break;
case 'i':
infiles=static_cast<string>(optarg);
break;
case 'j':
jinfiles=static_cast<string>(optarg);
break;
case 'c':
crosstype = static_cast<int>(atoi(optarg));
break;
case 's':
stars_are_baryons = static_cast<bool>(atoi(optarg));
break;
case 'h':
default:
help();
return 0;
}
}
#ifndef NOHDF5
/*ffname is a copy of input filename for extension*/
/*First open first file to get header properties*/
std::vector<std::string> fnames = find_hdf_set(infiles);
if ( !fnames.empty() ){
fprintf(stderr, "Found %lu hdf5 files in snapshot\n",fnames.size());
/*See if we have been handed the first file of a set:
* our method for dealing with this closely mirrors
* HDF5s family mode, but we cannot use this, because
* our files may not all be the same size.*/
use_hdf5 = true;
double atime, redshift, h100;
//Get the header and print out some useful things
if(load_hdf5_header(fnames[0].c_str(), &atime, &redshift, &box, &h100, npart_total, mass)) {
fprintf(stderr, "Could not load header\n");
return 1;
}
}
else
#endif
#ifndef NOBIGFILE
if(is_bigfile(infiles.c_str())) {
use_bigfile = true;
double atime, redshift, h100;
//Get the header and print out some useful things
if(load_bigfile_header(infiles.c_str(), &atime, &redshift, &box, &h100, npart_total, mass, &Omega0)) {
fprintf(stderr, "Could not load header\n");
return 1;
}
}
else
#endif
{
//Open the snapshot
snap = new GSnap(infiles);
if(outdir.empty() || (!use_hdf5 && snap->GetNumFiles() < 1)){
help();
return 0;
}
for(type=0;type<N_TYPE;type++){
npart_total[type]=snap->GetNpart(type);
mass[type] = snap->GetHeader().mass[type];
}
//Get the header and print out some useful things
box=snap->GetHeader().BoxSize;
printf("Boxsize=%g, ",box);
printf("NPart=(%g,%g,%g,%g,%g,%g)**3\n",cbrt(npart_total[0]),cbrt(npart_total[1]),cbrt(npart_total[2]),cbrt(npart_total[3]),cbrt(npart_total[4]),cbrt(npart_total[5]));
printf("Masses=[%g %g %g ]\n",snap->GetHeader().mass[0],snap->GetHeader().mass[1],snap->GetHeader().mass[2]);
printf("redshift=%g, Ω_M=%g\n",snap->GetHeader().redshift,snap->GetHeader().Omega0);
}
//Work out how large a field we need
for(type=0;type<N_TYPE;type++){
int64_t tmp=2*nexttwo(cbrt(npart_total[type]));
field_dims=std::max(field_dims, std::min(tmp, FIELD_DIMS));
}
const int nrbins=field_dims;
//Memory for the field
printf("FFT grid dimension: %lu\n",field_dims);
const size_t field_size = 2*field_dims*(size_t) field_dims*(field_dims/2+1);
/* Allocating a bit more memory allows us to do in-place transforms.*/
if(!(field=(GENFLOAT *)fftw_malloc(field_size*sizeof(GENFLOAT)))){
fprintf(stderr,"Error allocating memory for field\n");
return 1;
}
string filename=outdir;
size_t last=infiles.find_last_of("/\\");
//Set up FFTW
outfield=(fftw_complex *) &field[0];
if(!fftw_init_threads()){
fprintf(stderr,"Error initialising fftw threads\n");
return 0;
}
#ifdef _OPENMP
fftw_plan_with_nthreads(omp_get_max_threads());
#endif
pl=fftw_plan_dft_r2c_3d(field_dims,field_dims,field_dims,&field[0],outfield, FFTW_ESTIMATE);
//Allocate memory for output
power=(double *) malloc(nrbins*sizeof(double));
count=(int *) malloc(nrbins*sizeof(int));
keffs=(double *) malloc(nrbins*sizeof(double));
if(!power || !count || !keffs){
fprintf(stderr,"Error allocating memory for power spectrum.\n");
return 1;
}
if ((crosstype < 0) && (jinfiles.size()==0)) {
/*Now make a power spectrum for each particle type*/
for(type=0; type<N_TYPE; type++){
//Zero field
if(npart_total[type] == 0)
continue;
memset(field, 0, field_size*sizeof(GENFLOAT));
double total_mass = 0;
if (use_hdf5){
for(unsigned fileno = 0; fileno < fnames.size(); ++fileno)
/*If we couldn't read the coordinate array for some reason, go on to the next type*/
if(read_fieldize_hdf5(field, fnames[fileno].c_str(), type, box, field_dims, &total_mass, fileno) < 0)
continue;
if(type == 0 && stars_are_baryons) {
for(unsigned fileno = 0; fileno < fnames.size(); ++fileno)
read_fieldize_hdf5(field, fnames[fileno].c_str(), STARS_TYPE, box, field_dims, &total_mass, fileno);
}
}
else if(use_bigfile) {
read_fieldize_bigfile(field, infiles.c_str(), type, box, field_dims, &total_mass, npart_total, mass, Omega0);
if(type == 0 && stars_are_baryons) {
read_fieldize_bigfile(field, infiles.c_str(), STARS_TYPE, box, field_dims, &total_mass, npart_total, mass, Omega0);
}
}
else {
read_fieldize(field,snap,type, box, field_dims, &total_mass);
if(type == 0 && stars_are_baryons) {
read_fieldize(field,snap,STARS_TYPE, box, field_dims, &total_mass);
}
}
printf("total_mass in type %d = %g\n", type, total_mass);
fftw_execute(pl);
if(powerspectrum(field_dims,outfield, outfield, nrbins, power,count,keffs, total_mass, total_mass))
continue;
filename=outdir;
filename+="/PK-"+type_str(type)+"-"+infiles.substr(last+1);
print_pk(filename,nrbins,keffs,power,count);
}
} else if (jinfiles.size()>0) {
// do cross correlation across two files
bool use_hdf52=false;
GSnap * snap2 = NULL;
#ifndef NOHDF5
std::vector<std::string> fnames2 = find_hdf_set(jinfiles);
if ( !fnames2.empty() ){
/*See if we have been handed the first file of a set:
* our method for dealing with this closely mirrors
* HDF5s family mode, but we cannot use this, because
* our files may not all be the same size.*/
use_hdf52 = true;
}
else
#endif
snap2 = new GSnap(jinfiles);
/*Now make a power spectrum for each particle type*/
for(type=0; type<N_TYPE; type++){
if(npart_total[type] == 0)
continue;
//Memory for the field
/* Allocating a bit more memory allows us to do in-place transforms.*/
GENFLOAT * field2;
if(!(field2=(GENFLOAT *)fftw_malloc(field_size*sizeof(GENFLOAT)))){
fprintf(stderr,"Error allocating memory for second field\n");
return 1;
}
memset(field, 0, field_size*sizeof(GENFLOAT));
memset(field2, 0, field_size*sizeof(GENFLOAT));
fftw_complex * outfield2=(fftw_complex *) &field2[0];
fftw_plan pl2=fftw_plan_dft_r2c_3d(field_dims,field_dims,field_dims,&field2[0],outfield2, FFTW_ESTIMATE);
fprintf(stderr,"Reading...\n");
//Get the new filename if HDF5
double total_mass = 0;
if (use_hdf52){
for(unsigned fileno = 0; fileno < fnames.size(); ++fileno)
read_fieldize_hdf5(field, fnames[fileno].c_str(), 1, box, field_dims, &total_mass, fileno);
}
else if(use_bigfile) {
read_fieldize_bigfile(field, infiles.c_str(), type, box, field_dims, &total_mass, npart_total, mass, Omega0);
}
else if(read_fieldize(field,snap,type, box, field_dims, &total_mass))
continue;
double total_mass2 = 0;
//Get the new filename if HDF5
if (use_hdf52){
for(unsigned fileno = 0; fileno < fnames2.size(); ++fileno)
read_fieldize_hdf5(field2, fnames2[fileno].c_str(), 1, box, field_dims, &total_mass2, fileno);
}
else if(use_bigfile) {
read_fieldize_bigfile(field, infiles.c_str(), type, box, field_dims, &total_mass, npart_total, mass, Omega0);
}
else if(read_fieldize(field2,snap2,type, box, field_dims, &total_mass2))
continue;
fftw_execute(pl);
fftw_execute(pl2);
if(powerspectrum(field_dims,outfield, outfield2, nrbins, power,count,keffs, total_mass, total_mass2))
continue;
filename=outdir;
filename+="/PX-"+type_str(type)+"-"+infiles.substr(last+1);
print_pk(filename,nrbins,keffs,power,count);
}
}
else
{
//Do a cross-correlation inside the file
if(crosstype > N_TYPE || npart_total[1] == 0 || npart_total[crosstype] == 0){
fprintf(stderr, "Can't cross-correlate types not present in snapshot\n");
return 1;
}
//Memory for the field
/* Allocating a bit more memory allows us to do in-place transforms.*/
GENFLOAT * field2;
if(!(field2=(GENFLOAT *)fftw_malloc(field_size*sizeof(GENFLOAT)))){
fprintf(stderr,"Error allocating memory for second field\n");
return 1;
}
memset(field, 0, field_size*sizeof(GENFLOAT));
memset(field2, 0, field_size*sizeof(GENFLOAT));
fftw_complex * outfield2=(fftw_complex *) &field2[0];
fftw_plan pl2=fftw_plan_dft_r2c_3d(field_dims,field_dims,field_dims,&field2[0],outfield2, FFTW_ESTIMATE);
//Get the DM
double total_mass = 0;
if (use_hdf5){
for(unsigned fileno = 0; fileno < fnames.size(); ++fileno)
read_fieldize_hdf5(field, fnames[fileno].c_str(), 1, box, field_dims, &total_mass, fileno);
}
else if(use_bigfile) {
read_fieldize_bigfile(field, infiles.c_str(), 1, box, field_dims, &total_mass, npart_total, mass, Omega0);
}
else
read_fieldize(field,snap,1, box, field_dims, &total_mass);
//Get the other species
double total_mass2 = 0;
if (use_hdf5){
for(unsigned fileno = 0; fileno < fnames.size(); ++fileno)
read_fieldize_hdf5(field2, fnames[fileno].c_str(), crosstype, box, field_dims, &total_mass2, fileno);
}
else if(use_bigfile) {
read_fieldize_bigfile(field2, infiles.c_str(), crosstype, box, field_dims, &total_mass2, npart_total, mass, Omega0);
}
else
read_fieldize(field2,snap,crosstype, box, field_dims, &total_mass2);
//Do FFT of DM
fftw_execute(pl);
//Do FFT of other species
fftw_execute(pl2);
powerspectrum(field_dims,outfield, outfield2, nrbins, power,count,keffs, total_mass, total_mass2);
filename=outdir;
filename+="/PK-DMx"+type_str(crosstype)+"-"+infiles.substr(last+1);
//Multiply by a mass factor
// for (int i=0; i< nrbins; i++) {
// power[i]*= mass[1]/mass[crosstype];
// }
print_pk(filename,nrbins,keffs,power,count);
}
//Free memory
delete snap;
free(power);
free(count);
free(keffs);
fftw_free(field);
fftw_destroy_plan(pl);
return 0;
}