6 #include <gsl/gsl_integration.h>
7 #include <gsl/gsl_interp.h>
8 #include <bigfile-mpi.h>
18 static double sigma2_int(
double k,
void * params);
20 static double tk_eh(
double k);
43 typedef void (*
_parse_fn)(
int i,
double k,
char * line,
struct table *,
int *InputInLog10,
const double InitTime,
int NumCol);
50 static const char *
tnames[
MAXCOLS] = {
"DELTA_BAR",
"DELTA_CDM",
"DELTA_NU",
"DELTA_CB",
"VEL_BAR",
"VEL_CDM",
"VEL_NU",
"VEL_CB",
"VEL_TOT"};
73 const double logk = log10(k*scale);
86 logD += 1.5 * log10(scale);
87 double delta = (pow(10.0,
logD)-
NUGGET) * trans;
89 endrun(1,
"infinite delta or growth: %g for k = %g, Type = %d (tk = %g, logD = %g)\n",delta, k, Type, trans,
logD);
96 endrun(1,
"Velocity Type %d passed to Delta_Tabulated\n", Type);
104 if(Type < DELTA_BAR || Type >
DELTA_CB)
117 if(0 != big_file_mpi_create_block(bf, &btransfer, bname, NULL, 0, 0, 0, MPI_COMM_WORLD)) {
118 endrun(0,
"failed to create block %s:%s", bname,
119 big_file_get_error_message());
122 if ( (0 != big_block_set_attr(&btransfer,
"Nentry", &(ttable->
Nentry),
"u8", 1)) ) {
123 endrun(0,
"Failed to write table size %s\n",
124 big_file_get_error_message());
126 if(0 != big_block_mpi_close(&btransfer, MPI_COMM_WORLD)) {
127 endrun(0,
"Failed to close block %s\n",
128 big_file_get_error_message());
130 size_t dims[2] = {0 , 1};
138 snprintf(buf, 100,
"%s/logk", bname);
142 for(i = 0; i < ncol; i++)
144 snprintf(buf, 100,
"%s/%s", bname, colnames[i]);
152 const char * pname =
"DELTA_MAT";
158 void parse_power(
int i,
double k,
char * line,
struct table *out_tab,
int * InputInLog10,
const double InitTime,
int NumCol)
161 if((*InputInLog10) == 0) {
163 message(1,
"some input k is negative, guessing the file is in log10 units\n");
169 out_tab->
logk[i] = k;
170 retval = strtok(NULL,
" \t");
172 endrun(1,
"Incomplete line in power spectrum: %s\n",line);
173 double p = atof(retval);
174 if ((*InputInLog10) == 0)
177 out_tab->
logD[0][i] = p/2;
180 void parse_transfer(
int i,
double k,
char * line,
struct table *out_tab,
int * InputInLog10,
const double InitTime,
int NumCol)
183 int ncols = NumCol - 1;
184 int nnu = round((ncols - 15)/2);
185 double * transfers = (
double *)
mymalloc(
"transfers",
sizeof(
double) * ncols);
187 out_tab->
logk[i] = k;
196 for(j = 0; j< ncols; j++) {
197 char * retval = strtok(NULL,
" \t");
200 endrun(1,
"Incomplete line in power spectrum: only %d columns, expecting %d. Did you remember to set extra metric transfer functions=y?\n",j, ncols);
201 transfers[j] = atof(retval);
222 for(j=0; j < nnu; j++)
229 out_tab->
logD[
VEL_CDM][i] = transfers[8+nnu] * 0.5;
231 for(j=0; j < nnu; j++)
242 int InputInLog10 = 0;
245 if(!(fd = fopen(inputfile,
"r")))
246 endrun(1,
"can't read input spectrum in file '%s' on task %d\n", inputfile,
ThisTask);
252 char * retval = fgets(buffer, 1024, fd);
256 retval = strtok(buffer,
" \t");
257 if(!retval || retval[0] ==
'#')
264 MPI_Bcast(&(out_tab->
Nentry), 1, MPI_INT, 0, MPI_COMM_WORLD);
267 endrun(1,
"Input spectrum too short\n");
268 out_tab->
logk = (
double *)
mymalloc(
"Powertable", (ncols+1)*out_tab->
Nentry *
sizeof(double));
269 for(j=0; j<ncols; j++)
277 while(fgets(line1,1024,fd))
279 char * content = strtok(line1,
" \t");
280 if(content[0] !=
'#')
288 c = strtok(NULL,
" \t");
293 message(0,
"Detected %d columns in file '%s'. \n", Ncolumns, inputfile);
299 char * line = fgets(buffer, 1024, fd);
303 char * retval = strtok(line,
" \t");
304 if(!retval || retval[0] ==
'#')
306 double k = atof(retval);
307 parse_line(i, k, line, out_tab, &InputInLog10, InitTime, Ncolumns);
315 MPI_Bcast(out_tab->
logk, (ncols+1)*out_tab->
Nentry, MPI_DOUBLE, 0, MPI_COMM_WORLD);
316 for(j=0; j<ncols; j++) {
317 out_tab->
mat_intp[j] = gsl_interp_alloc(gsl_interp_cspline,out_tab->
Nentry);
325 const int nnu = (
CP->
MNu[0] > 0) + (
CP->
MNu[1] > 0) + (
CP->
MNu[2] > 0);
389 message(0,
"Scale-dependent growth calculated. Mean = %g %g %g %g %g\n",meangrowth[0], meangrowth[1], meangrowth[2], meangrowth[3], meangrowth[4]);
417 if(isfinite(res) && res > 0)
420 endrun(1,
"Could not normalize P(k) to Sigma8=%g! Measured Sigma8^2 is %g\n", ppar->
Sigma8, res);
425 message(0,
"Growth factor from z=%g (InputPowerRedshift) to z=%g (Init): %g \n", ppar->
InputPowerRedshift, 1. / InitTime - 1, Dplus);
427 message(0,
"Normalization adjusted to Sigma8=%g (at z=0) (Normfac=%g). \n", sqrt(
TopHatSigma2(R8))/Dplus,
Norm);
440 double q, theta, ommh2, a, s, gamma, L0, C0;
442 double omegam, ombh2, hubble;
456 ommh2 = omegam * hubble * hubble;
457 s = 44.5 * log(9.83 / ommh2) / sqrt(1. + 10. * exp(0.75 * log(ombh2))) * hubble;
458 a = 1. - 0.328 * log(431. * ommh2) * ombh2 / ommh2
459 + 0.380 * log(22.3 * ommh2) * (ombh2 / ommh2) * (ombh2 / ommh2);
460 gamma = a + (1. - a) / (1. + exp(4 * log(0.43 * k * s)));
461 gamma *= omegam * hubble;
462 q = k * theta * theta / gamma;
463 L0 = log(2. * exp(1.) + 1.8 * q);
464 C0 = 14.2 + 731. / (1. + 62.5 * q);
465 tmp = L0 / (L0 + C0 * q * q);
472 gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
473 double result,abserr;
479 gsl_integration_qags (&F, 0, 500. / R, 0, 1e-4,1000,w,&result, &abserr);
481 gsl_integration_workspace_free (w);
490 double r_tophat = *(
double *) params;
491 const double kr = r_tophat * k;
492 const double kr2 = kr * kr;
496 w = 1./3. - kr2/30. +kr2*kr2/840.;
498 w = 3 * (sin(kr) / kr - cos(kr)) / kr2;
499 x = 4 * M_PI / (2 * M_PI * 2 * M_PI * 2 * M_PI) * k * k * w * w * pow(
DeltaSpec(k,
DELTA_TOT),2);
double GrowthFactor(Cosmology *CP, double astart, double aend)
double hubble_function(const Cosmology *CP, double a)
void message(int where, const char *fmt,...)
void endrun(int where, const char *fmt,...)
#define mymalloc(name, size)
double omega_nu_single(const _omega_nu *const omnu, const double a, int i)
double get_omega_nu(const _omega_nu *const omnu, const double a)
double DeltaSpec(double k, enum TransferType Type)
static double sigma2_int(double k, void *params)
int init_powerspectrum(int ThisTask, double InitTime, double UnitLength_in_cm_in, Cosmology *CPin, struct power_params *ppar)
int init_transfer_table(int ThisTask, double InitTime, const struct power_params *const ppar)
static double get_Tabulated(double k, enum TransferType Type, double oobval)
double dlogGrowth(double kmag, enum TransferType Type)
static double PrimordialIndex
static double Delta_Tabulated(double k, enum TransferType Type)
void save_all_transfer_tables(BigFile *bf, int ThisTask)
void(* _parse_fn)(int i, double k, char *line, struct table *, int *InputInLog10, const double InitTime, int NumCol)
static struct table power_table
static const char * tnames[MAXCOLS]
static struct table transfer_table
void read_power_table(int ThisTask, const char *inputfile, const int ncols, struct table *out_tab, const double InitTime, _parse_fn parse_line)
static double tk_eh(double k)
void parse_transfer(int i, double k, char *line, struct table *out_tab, int *InputInLog10, const double InitTime, int NumCol)
void parse_power(int i, double k, char *line, struct table *out_tab, int *InputInLog10, const double InitTime, int NumCol)
static double Delta_EH(double k)
static void save_transfer(BigFile *bf, int ncol, struct table *ttable, const char *bname, int ThisTask, const char *colnames[])
static double UnitLength_in_cm
static double TopHatSigma2(double R)
void _bigfile_utils_create_block_from_c_array(BigFile *bf, void *baseptr, const char *name, const char *dtype, size_t dims[], ptrdiff_t elsize, int NumFiles, int NumWriters, MPI_Comm comm)
char * FileWithTransferFunction
int DifferentTransferFunctions
char * FileWithInputSpectrum
double InputPowerRedshift
gsl_interp * mat_intp[MAXCOLS]