int main(int argc, char **argv) { struct precision pr; /* for precision parameters */ struct background ba; /* for cosmological background */ struct thermo th; /* for thermodynamics */ struct perturbs pt; /* for source functions */ struct transfers tr; /* for transfer functions */ struct primordial pm; /* for primordial spectra */ struct spectra sp; /* for output spectra */ struct nonlinear nl; /* for non-linear spectra */ struct lensing le; /* for lensed spectra */ struct output op; /* for output files */ ErrorMsg errmsg; /* for error messages */ if (input_init_from_arguments(argc, argv,&pr,&ba,&th,&pt,&tr,&pm,&sp,&nl,&le,&op,errmsg) == _FAILURE_) { printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg); return _FAILURE_; } if (background_init(&pr,&ba) == _FAILURE_) { printf("\n\nError running background_init \n=>%s\n",ba.error_message); return _FAILURE_; } if (thermodynamics_init(&pr,&ba,&th) == _FAILURE_) { printf("\n\nError in thermodynamics_init \n=>%s\n",th.error_message); return _FAILURE_; } if (perturb_init(&pr,&ba,&th,&pt) == _FAILURE_) { printf("\n\nError in perturb_init \n=>%s\n",pt.error_message); return _FAILURE_; } if (pt.has_perturbations == _TRUE_) { /*********************************************************************/ /* here you can output the source function S(k,tau) of your choice */ /*********************************************************************/ FILE * output; int index_k,index_tau; /* choose a mode (scalar, tensor, ...) */ int index_md=pt.index_md_scalars; /* choose a type (temperature, polarization, grav. pot., ...) */ int index_type=pt.index_tp_t0; /* choose an initial condition (ad, bi, cdi, nid, niv, ...) */ int index_ic=pt.index_ic_ad; output=fopen("output/source.dat","w"); fprintf(output,"# k tau S\n"); for (index_k=0; index_k < pt.k_size[index_md]; index_k++) { for (index_tau=0; index_tau < pt.tau_size; index_tau++) { fprintf(output,"%e %e %e\n", pt.k[index_md][index_k], pt.tau_sampling[index_tau], pt.sources[index_md] [index_ic * pt.tp_size[index_md] + index_type] [index_tau * pt.k_size[index_md] + index_k] ); } fprintf(output,"\n"); } fclose(output); } /****** all calculations done, now free the structures ******/ if (perturb_free(&pt) == _FAILURE_) { printf("\n\nError in perturb_free \n=>%s\n",pt.error_message); return _FAILURE_; } if (thermodynamics_free(&th) == _FAILURE_) { printf("\n\nError in thermodynamics_free \n=>%s\n",th.error_message); return _FAILURE_; } if (background_free(&ba) == _FAILURE_) { printf("\n\nError in background_free \n=>%s\n",ba.error_message); return _FAILURE_; } return _SUCCESS_; }
/* Benchmark certain kernel operations */ void time_kernels ( struct vtx_data **A, /* matrix/graph being analyzed */ int n, /* number of rows/columns in matrix */ double *vwsqrt /* square roots of vertex weights */ ) { extern int DEBUG_PERTURB; /* debug flag for matrix perturbation */ extern int PERTURB; /* randomly perturb to break symmetry? */ extern int NPERTURB; /* number of edges to perturb */ extern int DEBUG_TRACE; /* trace main execution path */ extern double PERTURB_MAX; /* maximum size of perturbation */ int i, beg, end; double *dvec1, *dvec2, *dvec3; float *svec1, *svec2, *svec3, *vwsqrt_float; double norm_dvec, norm_svec; double dot_dvec, dot_svec; double time, time_dvec, time_svec; double diff; double factor, fac; float factor_float, fac_float; int loops; double min_time, target_time; double *mkvec(); float *mkvec_float(); void frvec(), frvec_float(); void vecran(); double ch_norm(), dot(); double norm_float(), dot_float(); double seconds(); void scadd(), scadd_float(), update(), update_float(); void splarax(), splarax_float(); void perturb_init(), perturb_clear(); if (DEBUG_TRACE > 0) { printf("<Entering time_kernels>\n"); } beg = 1; end = n; dvec1 = mkvec(beg, end); dvec2 = mkvec(beg, end); dvec3 = mkvec(beg - 1, end); svec1 = mkvec_float(beg, end); svec2 = mkvec_float(beg, end); svec3 = mkvec_float(beg - 1, end); if (vwsqrt == NULL) { vwsqrt_float = NULL; } else { vwsqrt_float = mkvec_float(beg - 1, end); for (i = beg - 1; i <= end; i++) { vwsqrt_float[i] = vwsqrt[i]; } } vecran(dvec1, beg, end); vecran(dvec2, beg, end); vecran(dvec3, beg, end); for (i = beg; i <= end; i++) { svec1[i] = dvec1[i]; svec2[i] = dvec2[i]; svec3[i] = dvec3[i]; } /* Set number of loops so that ch_norm() takes about one second. This should insulate against inaccurate timings on faster machines. */ loops = 1; time_dvec = 0; min_time = 0.5; target_time = 1.0; while (time_dvec < min_time) { time = seconds(); for (i = loops; i; i--) { norm_dvec = ch_norm(dvec1, beg, end); } time_dvec = seconds() - time; if (time_dvec < min_time) { loops = 10 * loops; } } loops = (target_time / time_dvec) * loops; if (loops < 1) loops = 1; printf(" Kernel benchmarking\n"); printf("Time (in seconds) for %d loops of each operation:\n\n", loops); printf("Routine Double Float Discrepancy Description\n"); printf("------- ------ ----- ----------- -----------\n"); /* Norm operation */ time = seconds(); for (i = loops; i; i--) { norm_dvec = ch_norm(dvec1, beg, end); } time_dvec = seconds() - time; time = seconds(); for (i = loops; i; i--) { norm_svec = norm_float(svec1, beg, end); } time_svec = seconds() - time; diff = norm_dvec - norm_svec; printf("norm %6.2f %6.2f %14.5e", time_dvec, time_svec, diff); printf(" 2 norm\n"); /* Dot operation */ time = seconds(); for (i = loops; i; i--) { dot_dvec = dot(dvec1, beg, end, dvec2); } time_dvec = seconds() - time; time = seconds(); for (i = loops; i; i--) { dot_svec = dot_float(svec1, beg, end, svec2); } time_svec = seconds() - time; diff = dot_dvec - dot_svec; printf("dot %6.2f %6.2f %14.5e", time_dvec, time_svec, diff); printf(" scalar product\n"); /* Scadd operation */ factor = 1.01; factor_float = factor; fac = factor; time = seconds(); for (i = loops; i; i--) { scadd(dvec1, beg, end, fac, dvec2); fac = -fac; /* to keep things in scale */ } time_dvec = seconds() - time; fac_float = factor_float; time = seconds(); for (i = loops; i; i--) { scadd_float(svec1, beg, end, fac_float, svec2); fac_float = -fac_float; /* to keep things in scale */ } time_svec = seconds() - time; diff = checkvec(dvec1, beg, end, svec1); printf("scadd %6.2f %6.2f %14.5e", time_dvec, time_svec, diff); printf(" vec1 <- vec1 + alpha*vec2\n"); /* Update operation */ time = seconds(); for (i = loops; i; i--) { update(dvec1, beg, end, dvec2, factor, dvec3); } time_dvec = seconds() - time; time = seconds(); for (i = loops; i; i--) { update_float(svec1, beg, end, svec2, factor_float, svec3); } time_svec = seconds() - time; diff = checkvec(dvec1, beg, end, svec1); printf("update %6.2f %6.2f %14.2g", time_dvec, time_svec, diff); printf(" vec1 <- vec2 + alpha*vec3\n"); /* splarax operation */ if (PERTURB) { if (NPERTURB > 0 && PERTURB_MAX > 0.0) { perturb_init(n); if (DEBUG_PERTURB > 0) { printf("Matrix being perturbed with scale %e\n", PERTURB_MAX); } } else if (DEBUG_PERTURB > 0) { printf("Matrix not being perturbed\n"); } } time = seconds(); for (i = loops; i; i--) { splarax(dvec1, A, n, dvec2, vwsqrt, dvec3); } time_dvec = seconds() - time; time = seconds(); for (i = loops; i; i--) { splarax_float(svec1, A, n, svec2, vwsqrt_float, svec3); } time_svec = seconds() - time; diff = checkvec(dvec1, beg, end, svec1); printf("splarax %6.2f %6.2f %14.5e", time_dvec, time_svec, diff); printf(" sparse matrix vector multiply\n"); if (PERTURB && NPERTURB > 0 && PERTURB_MAX > 0.0) { perturb_clear(); } printf("\n"); /* Free memory */ frvec(dvec1, 1); frvec(dvec2, 1); frvec(dvec3, 0); frvec_float(svec1, 1); frvec_float(svec2, 1); frvec_float(svec3, 0); if (vwsqrt_float != NULL) { frvec_float(vwsqrt_float, beg - 1); } }
int main(int argc, char **argv) { struct precision pr; /* for precision parameters */ struct background ba; /* for cosmological background */ struct thermo th; /* for thermodynamics */ struct perturbs pt; /* for source functions */ struct bessels bs; /* for bessel functions */ struct transfers tr; /* for transfer functions */ struct primordial pm; /* for primordial spectra */ struct spectra sp; /* for output spectra */ struct lensing le; /* for lensing spectra */ struct output op; /* for output files */ struct spectra_nl nl; /* for calculation of non-linear spectra */ ErrorMsg errmsg; if (input_init_from_arguments(argc, argv,&pr,&ba,&th,&pt,&bs,&tr,&pm,&sp,&le,&op,&nl,errmsg) == _FAILURE_) { printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg); return _FAILURE_; } if (background_init(&pr,&ba) == _FAILURE_) { printf("\n\nError running background_init \n=>%s\n",ba.error_message); return _FAILURE_; } if (thermodynamics_init(&pr,&ba,&th) == _FAILURE_) { printf("\n\nError in thermodynamics_init \n=>%s\n",th.error_message); return _FAILURE_; } if (perturb_init(&pr,&ba,&th,&pt) == _FAILURE_) { printf("\n\nError in perturb_init \n=>%s\n",pt.error_message); return _FAILURE_; } if (bessel_init(&pr,&bs) == _FAILURE_) { printf("\n\nError in bessel_init \n =>%s\n",bs.error_message); return _FAILURE_; } if (transfer_init(&pr,&ba,&th,&pt,&bs,&tr) == _FAILURE_) { printf("\n\nError in transfer_init \n=>%s\n",tr.error_message); return _FAILURE_; } if (primordial_init(&pr,&pt,&pm) == _FAILURE_) { printf("\n\nError in primordial_init \n=>%s\n",pm.error_message); return _FAILURE_; } if (spectra_init(&ba,&pt,&tr,&pm,&sp) == _FAILURE_) { printf("\n\nError in spectra_init \n=>%s\n",sp.error_message); return _FAILURE_; } if (output_init(&ba,&pt,&sp,&op) == _FAILURE_) { printf("\n\nError in output_init \n=>%s\n",op.error_message); return _FAILURE_; } /****** done ******/ int index_mode=0; int index_eta; int index_ic=0; int index_k; double delta_rho_bc,rho_bc; double delta_i,rho_i; double P_bc; int last_index_back; double * pvecback_long; double k,pk; FILE * output; double z,eta; double * tki; if(pt.has_matter_transfers == _FALSE_) { printf("You need to switch on mTk calculation for this code\n"); return _FAILURE_; } output=fopen("output/Pcb.dat","w"); class_alloc(pvecback_long,sizeof(double)*ba.bg_size,errmsg); class_alloc(tki,sizeof(double)*sp.ln_k_size*sp.tr_size,errmsg); z=0.; if(background_eta_of_z(&ba,z,&eta) == _FAILURE_) { printf("\n\nError running background_eta_of_z \n=>%s\n",ba.error_message); return _FAILURE_; } if(background_at_eta(&ba, eta, long_info, normal, &last_index_back, pvecback_long) == _FAILURE_) { printf("\n\nError running background_at_eta \n=>%s\n",ba.error_message); return _FAILURE_; } if (spectra_tk_at_z(&ba,&sp,z,tki) == _FAILURE_) { printf("\n\nError in spectra_tk_at_z \n=>%s\n",sp.error_message); return _FAILURE_; } for (index_k=0; index_k<sp.ln_k_size; index_k++) { k=exp(sp.ln_k[index_k]); delta_rho_bc=0.; rho_bc=0.; /* T_b(k,eta) */ delta_i = tki[index_k*sp.tr_size+sp.index_tr_b]; rho_i = pvecback_long[ba.index_bg_rho_b]; delta_rho_bc += rho_i * delta_i; rho_bc += rho_i; /* T_cdm(k,eta) */ if (ba.has_cdm == _TRUE_) { delta_i = tki[index_k*sp.tr_size+sp.index_tr_cdm]; rho_i = pvecback_long[ba.index_bg_rho_cdm]; delta_rho_bc += rho_i * delta_i; rho_bc += rho_i; } if (primordial_spectrum_at_k(&pm,index_mode,linear,k,&pk) == _FAILURE_) { printf("\n\nError in primordial_spectrum_at_k \n=>%s\n",pm.error_message); return _FAILURE_; } P_bc=pk*pow(delta_rho_bc/rho_bc,2)*2.*_PI_*_PI_/k/k/k; fprintf(output,"%e %e\n",k,P_bc); } /******************/ if (spectra_free(&sp) == _FAILURE_) { printf("\n\nError in spectra_free \n=>%s\n",sp.error_message); return _FAILURE_; } if (primordial_free(&pm) == _FAILURE_) { printf("\n\nError in primordial_free \n=>%s\n",pm.error_message); return _FAILURE_; } if (transfer_free(&tr) == _FAILURE_) { printf("\n\nError in transfer_free \n=>%s\n",tr.error_message); return _FAILURE_; } if (bessel_free(&bs) == _FAILURE_) { printf("\n\nError in bessel_free \n=>%s\n",bs.error_message); return _FAILURE_; } if (perturb_free(&pt) == _FAILURE_) { printf("\n\nError in perturb_free \n=>%s\n",pt.error_message); return _FAILURE_; } if (thermodynamics_free(&th) == _FAILURE_) { printf("\n\nError in thermodynamics_free \n=>%s\n",th.error_message); return _FAILURE_; } if (background_free(&ba) == _FAILURE_) { printf("\n\nError in background_free \n=>%s\n",ba.error_message); return _FAILURE_; } return _SUCCESS_; }
int main(int argc, char **argv) { struct precision pr; /* for precision parameters */ struct background ba; /* for cosmological background */ struct thermo th; /* for thermodynamics */ struct perturbs pt; /* for source functions */ struct transfers tr; /* for transfer functions */ struct primordial pm; /* for primordial spectra */ struct spectra sp; /* for output spectra */ struct nonlinear nl; /* for non-linear spectra */ struct lensing le; /* for lensed spectra */ struct output op; /* for output files */ ErrorMsg errmsg; /* for error messages */ if (input_init_from_arguments(argc, argv,&pr,&ba,&th,&pt,&tr,&pm,&sp,&nl,&le,&op,errmsg) == _FAILURE_) { printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg); return _FAILURE_; } if (background_init(&pr,&ba) == _FAILURE_) { printf("\n\nError running background_init \n=>%s\n",ba.error_message); return _FAILURE_; } if (thermodynamics_init(&pr,&ba,&th) == _FAILURE_) { printf("\n\nError in thermodynamics_init \n=>%s\n",th.error_message); return _FAILURE_; } if (perturb_init(&pr,&ba,&th,&pt) == _FAILURE_) { printf("\n\nError in perturb_init \n=>%s\n",pt.error_message); return _FAILURE_; } if (primordial_init(&pr,&pt,&pm) == _FAILURE_) { printf("\n\nError in primordial_init \n=>%s\n",pm.error_message); return _FAILURE_; } if (nonlinear_init(&pr,&ba,&th,&pt,&pm,&nl) == _FAILURE_) { printf("\n\nError in nonlinear_init \n=>%s\n",nl.error_message); return _FAILURE_; } if (transfer_init(&pr,&ba,&th,&pt,&nl,&tr) == _FAILURE_) { printf("\n\nError in transfer_init \n=>%s\n",tr.error_message); return _FAILURE_; } /****** output the transfer functions ******/ printf("Output of transfer functions (l, q, k, nu, Delta)\n"); printf("(in flat space, q=k and nu=inf) \n"); /* 1) select the mode, initial condition, type and multipole of the function you want to plot: */ int index_mode=pt.index_md_scalars; int index_ic =pt.index_ic_ad; int index_type=tr.index_tt_t0; /* 2) here is an illustration of how to output the transfer functions at some (k,l)'s of your choice */ /* int index_l = 0; double q=3.6e-4; double transfer; if (transfer_functions_at_q(&tr, index_mode, index_ic, index_type, index_l, q, &transfer ) == _FAILURE_) { printf("\n\nError in transfer_function_at_k \n=>%s\n",tr.error_message); return _FAILURE_; } printf("%d %e %e\n",tr.l[index_l],q,transfer); */ /* 3) here you can output the full tabulated arrays for all k and l's*/ int index_q; int index_l; double transfer; FILE * output; output=fopen("output/test.trsf","w"); for (index_l=0; index_l<tr.l_size[index_mode]; index_l++) { for (index_q=0; index_q<tr.q_size; index_q++) { /* use this to plot a single type : */ transfer = tr.transfer[index_mode] [((index_ic * tr.tt_size[index_mode] + index_type) * tr.l_size[index_mode] + index_l) * tr.q_size + index_q]; /* or use this to plot the full temperature transfer function: */ /* transfer = tr.transfer[index_mode][((index_ic * tr.tt_size[index_mode] + tr.index_tt_t0) * tr.l_size[index_mode] + index_l) * tr.q_size + index_q] + tr.transfer[index_mode][((index_ic * tr.tt_size[index_mode] + tr.index_tt_t1) * tr.l_size[index_mode] + index_l) * tr.q_size + index_q] + tr.transfer[index_mode][((index_ic * tr.tt_size[index_mode] + tr.index_tt_t2) * tr.l_size[index_mode] + index_l) * tr.q_size + index_q]; */ if (transfer != 0.) { fprintf(output,"%d %e %e %e %e\n", tr.l[index_l], tr.q[index_q], tr.k[index_mode][index_q], tr.q[index_q]/sqrt(ba.sgnK*ba.K), transfer); } } fprintf(output,"\n\n"); //} } fclose(output); /****** all calculations done, now free the structures ******/ if (transfer_free(&tr) == _FAILURE_) { printf("\n\nError in transfer_free \n=>%s\n",tr.error_message); return _FAILURE_; } if (nonlinear_free(&nl) == _FAILURE_) { printf("\n\nError in nonlinear_free \n=>%s\n",nl.error_message); return _FAILURE_; } if (primordial_free(&pm) == _FAILURE_) { printf("\n\nError in primordial_free \n=>%s\n",pm.error_message); return _FAILURE_; } if (perturb_free(&pt) == _FAILURE_) { printf("\n\nError in perturb_free \n=>%s\n",pt.error_message); return _FAILURE_; } if (thermodynamics_free(&th) == _FAILURE_) { printf("\n\nError in thermodynamics_free \n=>%s\n",th.error_message); return _FAILURE_; } if (background_free(&ba) == _FAILURE_) { printf("\n\nError in background_free \n=>%s\n",ba.error_message); return _FAILURE_; } return _SUCCESS_; }
int class_assuming_bessels_computed( struct file_content *pfc, struct precision * ppr, struct background * pba, struct thermo * pth, struct perturbs * ppt, struct bessels * pbs, struct transfers * ptr, struct primordial * ppm, struct spectra * psp, struct nonlinear * pnl, struct lensing * ple, struct output * pop, double z, double * psCl, int lmin, int lmax, ErrorMsg errmsg) { /*local variables*/ double pvecback[100]; double T21; int l; double tau; /* conformal age, in unit of Mpc */ double pk_tmp; double k; double * pk_ic; if (input_init(pfc,ppr,pba,pth,ppt,pbs,ptr,ppm,psp,pnl,ple,pop,errmsg) == _FAILURE_) { printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg); return _FAILURE_; } if (background_init(ppr,pba) == _FAILURE_) { printf("\n\nError running background_init \n=>%s\n",pba->error_message); return _FAILURE_; } if (thermodynamics_init(ppr,pba,pth) == _FAILURE_) { printf("\n\nError in thermodynamics_init \n=>%s\n",pth->error_message); return _FAILURE_; } if (perturb_init(ppr,pba,pth,ppt) == _FAILURE_) { printf("\n\nError in perturb_init \n=>%s\n",ppt->error_message); return _FAILURE_; } if (transfer_init(ppr,pba,pth,ppt,pbs,ptr) == _FAILURE_) { printf("\n\nError in transfer_init \n=>%s\n",ptr->error_message); return _FAILURE_; } if (primordial_init(ppr,ppt,ppm) == _FAILURE_) { printf("\n\nError in primordial_init \n=>%s\n",ppm->error_message); return _FAILURE_; } if (spectra_init(ppr,pba,ppt,ptr,ppm,psp) == _FAILURE_) { printf("\n\nError in spectra_init \n=>%s\n",psp->error_message); return _FAILURE_; } if (nonlinear_init(ppr,pba,pth,ppt,pbs,ptr,ppm,psp,pnl) == _FAILURE_) { printf("\n\nError in nonlinear_init \n=>%s\n",pnl->error_message); return _FAILURE_; } if (lensing_init(ppr,ppt,psp,pnl,ple) == _FAILURE_) { printf("\n\nError in lensing_init \n=>%s\n",ple->error_message); return _FAILURE_; } if (output_init(pba,ppt,psp,pnl,ple,pop) == _FAILURE_) { printf("\n\nError in output_init \n=>%s\n",pop->error_message); return _FAILURE_; } background_functions(pba, 1/(1.+z), 1, pvecback); /* T21 := 7.59*10^-2 h (1+\delta) (1+z)^2 / E(z); * E[z]:= Sqrt[Omega_m(1+z)^3+Omega_l exp(-3 Integrate[(1+w)/a, a])] mK */ T21 = 7.59 * 0.01 * pba->h * pow(1.+z,2) / (pvecback[pba->index_bg_H]/pba->H0); background_tau_of_z(pba, z, &tau); for (l=lmin; l<=lmax; l+=1) { k = l/(pba->conformal_age - tau); /* tau in Mpc; k in spectra_pk_at_k_and_z is in [1/Mpc] */ spectra_pk_at_k_and_z(pba, ppm, psp, k, z, &pk_tmp, pk_ic); /* 3-D vs 2-D: l(l+1)Cl/2PI = k^3 P21(k)/2PI^2, P21(k) = (T21*Y)^2 * P(k) */ /* psCl[l]'s are in mK^2 */ if(l%100==0) printf("z is %e, l is %d, tau is %e, k is %e, pk is %e\n", z, l, tau, k, pk_tmp); psCl[l] = 2*M_PI/(l*1.0*(l+1)) * T21*T21 * pow(k, 3) * pk_tmp/(2*M_PI*M_PI); } /****** all calculations done, now free the structures ******/ if (lensing_free(ple) == _FAILURE_) { printf("\n\nError in spectra_free \n=>%s\n",ple->error_message); return _FAILURE_; } if (nonlinear_free(pnl) == _FAILURE_) { printf("\n\nError in nonlinear_free \n=>%s\n",pnl->error_message); return _FAILURE_; } if (spectra_free(psp) == _FAILURE_) { printf("\n\nError in spectra_free \n=>%s\n",psp->error_message); return _FAILURE_; } if (primordial_free(ppm) == _FAILURE_) { printf("\n\nError in primordial_free \n=>%s\n",ppm->error_message); return _FAILURE_; } if (transfer_free(ptr) == _FAILURE_) { printf("\n\nError in transfer_free \n=>%s\n",ptr->error_message); return _FAILURE_; } if (perturb_free(ppt) == _FAILURE_) { printf("\n\nError in perturb_free \n=>%s\n",ppt->error_message); return _FAILURE_; } if (thermodynamics_free(pth) == _FAILURE_) { printf("\n\nError in thermodynamics_free \n=>%s\n",pth->error_message); return _FAILURE_; } if (background_free(pba) == _FAILURE_) { printf("\n\nError in background_free \n=>%s\n",pba->error_message); return _FAILURE_; } return _SUCCESS_; }
int main(int argc, char **argv) { struct precision pr; /* for precision parameters */ struct background ba; /* for cosmological background */ struct thermo th; /* for thermodynamics */ struct perturbs pt; /* for source functions */ struct bessels bs; /* for bessel functions */ struct transfers tr; /* for transfer functions */ struct primordial pm; /* for primordial spectra */ struct spectra sp; /* for output spectra */ struct nonlinear nl; /* for non-linear spectra */ struct lensing le; /* for lensed spectra */ struct output op; /* for output files */ ErrorMsg errmsg; /* for error messages */ if (input_init_from_arguments(argc, argv,&pr,&ba,&th,&pt,&bs,&tr,&pm,&sp,&nl,&le,&op,errmsg) == _FAILURE_) { printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg); return _FAILURE_; } if (background_init(&pr,&ba) == _FAILURE_) { printf("\n\nError running background_init \n=>%s\n",ba.error_message); return _FAILURE_; } if (thermodynamics_init(&pr,&ba,&th) == _FAILURE_) { printf("\n\nError in thermodynamics_init \n=>%s\n",th.error_message); return _FAILURE_; } if (perturb_init(&pr,&ba,&th,&pt) == _FAILURE_) { printf("\n\nError in perturb_init \n=>%s\n",pt.error_message); return _FAILURE_; } if (bessel_init(&pr,&bs) == _FAILURE_) { printf("\n\nError in bessel_init \n =>%s\n",bs.error_message); return _FAILURE_; } if (transfer_init(&pr,&ba,&th,&pt,&bs,&tr) == _FAILURE_) { printf("\n\nError in transfer_init \n=>%s\n",tr.error_message); return _FAILURE_; } /****** output the transfer functions ******/ printf("Output of transfer functions (l, k, Delta)\n"); /* 1) select the mode, initial condition, type and multipole of the function you want to plot: */ int index_mode=pt.index_md_scalars; int index_ic =pt.index_ic_ad; int index_type=tr.index_tt_lcmb; //int index_type=tr.index_tt_density+2; /* 2) here is an illustration of how to output the transfer functions at some (k,l)'s of your choice */ /* int index_l = 0; double k=3.6e-4; double transfer; if (transfer_functions_at_k(&tr, index_mode, index_ic, index_type, index_l, k, &transfer ) == _FAILURE_) { printf("\n\nError in transfer_function_at_k \n=>%s\n",tr.error_message); return _FAILURE_; } printf("%d %e %e\n",tr.l[index_l],k,transfer); */ /* 3) here you can output the full tabulated arrays for all k and l's*/ int index_k; int index_l; double transfer; for (index_l=0; index_l<tr.l_size[index_mode]; index_l++) { //for (index_l=20; index_l<21; index_l++) { for (index_k=0; index_k<tr.k_size[index_mode]; index_k++) { transfer=tr.transfer[index_mode] [((index_ic * tr.tt_size[index_mode] + index_type) * tr.l_size[index_mode] + index_l) * tr.k_size[index_mode] + index_k]; if (transfer != 0.) { printf("%d %e %e\n",tr.l[index_l],tr.k[index_mode][index_k],transfer); } } printf("\n\n"); } /****** all calculations done, now free the structures ******/ if (transfer_free(&tr) == _FAILURE_) { printf("\n\nError in transfer_free \n=>%s\n",tr.error_message); return _FAILURE_; } if (bessel_free(&bs) == _FAILURE_) { printf("\n\nError in bessel_free \n=>%s\n",bs.error_message); return _FAILURE_; } if (perturb_free(&pt) == _FAILURE_) { printf("\n\nError in perturb_free \n=>%s\n",pt.error_message); return _FAILURE_; } if (thermodynamics_free(&th) == _FAILURE_) { printf("\n\nError in thermodynamics_free \n=>%s\n",th.error_message); return _FAILURE_; } if (background_free(&ba) == _FAILURE_) { printf("\n\nError in background_free \n=>%s\n",ba.error_message); return _FAILURE_; } return _SUCCESS_; }
int main(int argc, char **argv) { struct precision pr; /* precision parameters (1st-order) */ struct precision2 pr2; /* precision parameters (2nd-order) */ struct background ba; /* cosmological background */ struct thermo th; /* thermodynamics */ struct perturbs pt; /* source functions (1st-order) */ struct perturbs2 pt2; /* source functions (2nd-order) */ struct transfers tr; /* transfer functions (1st-order) */ struct bessels bs; /* bessel functions (1st-order) */ struct bessels2 bs2; /* bessel functions (2nd-order) */ struct transfers2 tr2; /* transfer functions (2nd-order) */ struct primordial pm; /* primordial spectra */ struct spectra sp; /* output spectra (1st-order) */ struct nonlinear nl; /* non-linear spectra */ struct lensing le; /* lensed spectra */ struct bispectra bi; /* bispectra */ struct fisher fi; /* fisher matrix */ struct output op; /* output files */ ErrorMsg errmsg; /* error messages */ // =================================================================================== // = Parse arguments = // =================================================================================== /* We introduce the n_args variable to differentiate between CLASS arguments and the arguments for this function */ int n_args = 1; int index_k; /* CLASS/SONG can accept either one argument... */ if (argc == 2 + n_args) { struct stat st; stat (argv[1], &st); int is_dir = (S_ISDIR (st.st_mode) != 0); if (!is_dir) { printf ("ERROR: when giving two arguments, the first one ('%s') should be a run directory\n", argv[1]); return _FAILURE_; } index_k = atoi(argv[2]); } /* ... or two arguments */ else if (argc == 3 + n_args) { index_k = atoi(argv[3]); } else { printf ("usage: %s <ini file> <pre file> <index k>\n", argv[0]); printf (" %s <run_directory> <index k>\n", argv[0]); return _FAILURE_; } // =================================================================================== // = Compute perturbations = // =================================================================================== /* Decrease the argument counter. The reason is that CLASS should be fed only its default arguments, that is the parameter files and, optionally, the run directory */ argc -= n_args; if (input_init_from_arguments(argc,argv,&pr,&ba,&th,&pt,&tr,&pm, &sp,&nl,&le,&bs,&bi,&fi,&op,errmsg) == _FAILURE_) { printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg); return _FAILURE_; } if (pt.has_perturbations2 == _TRUE_) { if (input2_init_from_arguments(argc,argv,&pr,&pr2,&ba,&th,&pt,&pt2,&tr,&bs,&bs2,&tr2,&pm, &sp,&nl,&le,&bi,&fi,&op,errmsg) == _FAILURE_) { printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg); return _FAILURE_; } /* Compute only the first-order early transfer functions, no matter what is specified in the input files */ pt2.has_early_transfers1_only = _TRUE_; } if (background_init(&pr,&ba) == _FAILURE_) { printf("\n\nError running background_init \n=>%s\n",ba.error_message); return _FAILURE_; } if (thermodynamics_init(&pr,&ba,&th) == _FAILURE_) { printf("\n\nError in thermodynamics_init \n=>%s\n",th.error_message); return _FAILURE_; } if (pt.has_perturbations2 == _FALSE_) { if (perturb_init(&pr,&ba,&th,&pt) == _FAILURE_) { printf("\n\nError in perturb_init \n=>%s\n",pt.error_message); return _FAILURE_; } } else { if (perturb2_init(&pr,&pr2,&ba,&th,&pt,&pt2) == _FAILURE_) { printf("\n\nError in perturb2_init \n=>%s\n",pt2.error_message); return _FAILURE_; } } /* Run some checks */ if (pt.has_scalars == _FALSE_) { printf ("ERROR: only scalar modes supported by this function\n"); return _FAILURE_; } int index_md = pt.index_md_scalars; if (pt.has_ad == _FALSE_) { printf ("ERROR: only adiabatic modes supported by this function\n"); return _FAILURE_; } int index_ic = pt.index_ic_ad; if ((index_k<0) || (index_k>=pt.k_size[index_md])) { printf ("ERROR: index_k should be between index_k=%d (k=%g) and index_k=%d (k=%g)\n", 0, pt.k[index_md][0], pt.k_size[index_md]-1, pt.k[index_md][pt.k_size[index_md]-1]); return _FAILURE_; } // =================================================================================== // = First or second order? = // =================================================================================== /* Should we show the line-of-sight sources or the quadratic sources? */ short tabulate_los_sources = _FALSE_; /* If we are in standard 1st-order mode, we can only access the line-of-sight sources */ if (pt2.has_perturbations2 == _FALSE_) tabulate_los_sources = _TRUE_; /* Information about the cosmological model */ double h = ba.h; double a_equality = ba.a_eq; fprintf (stderr, "# Cosmological parameters:\n"); fprintf (stderr, "# tau0 = %g, a_equality = %g, Omega_b = %g, Tcmb = %g, Omega_cdm = %g\n", ba.conformal_age, ba.a_eq, ba.Omega0_b, ba.T_cmb, ba.Omega0_cdm); fprintf (stderr, "# omega_lambda = %g, Omega_ur = %g, Omega_fld = %g, h = %g, tau0 = %g\n", ba.Omega0_lambda, ba.Omega0_ur, ba.Omega0_fld, ba.h, ba.conformal_age); fprintf (stderr, "# omega_b = %g, omega_cdm = %g, omega_lambda = %g, omega_ur = %g, omega_fld = %g\n", ba.Omega0_b*h*h, ba.Omega0_cdm*h*h, ba.Omega0_lambda*h*h, ba.Omega0_ur*h*h, ba.Omega0_fld*h*h); /* Info about the used gauge at first order */ fprintf (stderr, "# gauge = "); if (pt.gauge == newtonian) fprintf (stderr, "Newtonian gauge\n"); if (pt.gauge == synchronous) fprintf (stderr, "synchronous gauge\n"); /* Sizes associated to the non-running indices in the ***sources table */ int k_size = pt.k_size[index_md]; int tp_size = pt.tp_size[index_md]; int qs_size; if (pt.has_perturbations2 == _TRUE_) qs_size = pt.qs_size[index_md]; /* Account for overshooting of 'k' */ if(index_k > k_size-1) index_k = k_size-1; if(index_k < 0) index_k = 0; // =================================================================================== // = Print sources = // =================================================================================== /* Print information on 'k' */ double k = pt.k[index_md][index_k]; printf("# k = %g (index_k=%d)\n", pt.k[index_md][index_k], index_k); fprintf (stderr, "# k = %g (index_k=%d)\n", pt.k[index_md][index_k], index_k); /* Vector that will contain background quantities (we are only interested in 'a') */ double * pvecback = malloc(ba.bg_size_short*sizeof(double)); /* Number of rows that will be printed */ int tau_size; double tau_ini, tau_end; if (tabulate_los_sources == _TRUE_) { tau_size = pt.tau_size; tau_ini = pt.tau_sampling[0]; tau_end = pt.tau_sampling[pt.tau_size-1]; } else { tau_size = pt.tau_size_quadsources; tau_ini = pt.tau_sampling_quadsources[0]; tau_end = pt.tau_sampling_quadsources[pt.tau_size_quadsources-1]; }; /* Some debug info */ if (tabulate_los_sources == _TRUE_) fprintf (stderr, "# Number of source types tp_size=%d\n", tp_size); else fprintf (stderr, "# Number of source types qs_size=%d\n", qs_size); fprintf (stderr, "# Will print %d rows from tau=%g to %g\n", tau_size, tau_ini, tau_end); fprintf (stderr, "# Considered mode: index_md=%d\n", index_md); fprintf (stderr, "# Considered initial condition: index_ic=%d\n", index_ic); /* Running index used to number the columns */ int index_print=1; /* First row contains the labels of the different types */ fprintf (stderr, "%11s(%03d) ", "tau", index_print++); // Conformal time fprintf (stderr, "%11s(%03d) ", "a", index_print++); // Scale factor fprintf (stderr, "%11s(%03d) ", "y", index_print++); // Scale factor normalized to equality fprintf (stderr, "%11s(%03d) ", "k_tau", index_print++); // Scale times Conformal time /* Build labels for the line-of-sight sources */ char label[32]; if (tabulate_los_sources == _TRUE_) { for (int index_tp = 0; index_tp < tp_size; ++index_tp) { sprintf(label, "S_%d", index_tp); fprintf (stderr, "%11s(%03d) ", label, index_print++); } } /* Labels for the second-order quadsources */ else { for (int index_tp = 0; index_tp < qs_size; ++index_tp) fprintf (stderr, "%11s(%03d) ", pt.qs_labels[index_md][index_tp], index_print++); } fprintf (stderr, "\n"); // =================================================================================== // = Loop on time = // =================================================================================== for (int index_tau = 0; index_tau < tau_size; ++index_tau) { double tau = (tabulate_los_sources==_TRUE_ ? pt.tau_sampling[index_tau] : pt.tau_sampling_quadsources[index_tau]); /* Extract value of the scale factor */ int dump; background_at_tau( &ba, tau, ba.short_info, ba.inter_normal, &dump, pvecback ); double a = pvecback[ba.index_bg_a]; fprintf (stderr, "%+16g ", tau); fprintf (stderr, "%+16e ", a); fprintf (stderr, "%+16e ", log10(a/a_equality)); fprintf (stderr, "%+16e ", tau*k); /* Line of sight sources */ if (tabulate_los_sources == _TRUE_) { for (int index_tp = 0; index_tp < tp_size; ++index_tp) { double var = pt.sources[index_md][index_ic*tp_size + index_tp][index_tau*k_size + index_k]; fprintf (stderr, "%+16e ", var); } } /* Sources for the second-order system */ else { for (int index_tp = 0; index_tp < qs_size; ++index_tp) { double var = pt.quadsources[index_md][index_ic*qs_size + index_tp][index_tau*k_size + index_k]; fprintf (stderr, "%+16e ", var); } } fprintf (stderr, "\n"); } // end of for(index_tau) // ===================================================================================== // = Free memory = // ===================================================================================== if (pt.has_perturbations2 == _TRUE_) { if (perturb2_free(&pr2, &pt2) == _FAILURE_) { printf("\n\nError in perturb2_free \n=>%s\n",pt2.error_message); return _FAILURE_; } } if (perturb_free(&pt) == _FAILURE_) { printf("\n\nError in perturb_free \n=>%s\n",pt.error_message); return _FAILURE_; } if (thermodynamics_free(&th) == _FAILURE_) { printf("\n\nError in thermodynamics_free \n=>%s\n",th.error_message); return _FAILURE_; } if (background_free(&ba) == _FAILURE_) { printf("\n\nError in background_free \n=>%s\n",ba.error_message); return _FAILURE_; } return _SUCCESS_; }
main(int argc, char **argv) { struct precision pr; /* for precision parameters */ struct background ba; /* for cosmological background */ struct thermo th; /* for thermodynamics */ struct perturbs pt; /* for source functions */ struct bessels bs; /* for bessel functions */ struct transfers tr; /* for transfer functions */ struct primordial pm; /* for primordial spectra */ struct output op; struct lensing le; struct spectra sp; /* for output spectra */ struct spectra_nl nl; /* for calculation of non-linear spectra */ ErrorMsg errmsg; if (input_init_from_arguments(argc, argv,&pr,&ba,&th,&pt,&bs,&tr,&pm,&sp,&le,&op,&nl,errmsg) == _FAILURE_) { printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg); return _FAILURE_; } if (background_init(&pr,&ba) == _FAILURE_) { printf("\n\nError running background_init \n=>%s\n",ba.error_message); return _FAILURE_; } if (thermodynamics_init(&pr,&ba,&th) == _FAILURE_) { printf("\n\nError in thermodynamics_init \n=>%s\n",th.error_message); return _FAILURE_; } if (perturb_init(&pr,&ba,&th,&pt) == _FAILURE_) { printf("\n\nError in perturb_init \n=>%s\n",pt.error_message); return _FAILURE_; } if (bessel_init(&pr,&bs) == _FAILURE_) { printf("\n\nError in bessel_init \n =>%s\n",bs.error_message); return _FAILURE_; } if (transfer_init(&pr,&ba,&th,&pt,&bs,&tr) == _FAILURE_) { printf("\n\nError in transfer_init \n=>%s\n",tr.error_message); return _FAILURE_; } if (primordial_init(&pr,&pt,&pm) == _FAILURE_) { printf("\n\nError in transfer_init \n=>%s\n",pm.error_message); return _FAILURE_; } if (spectra_init(&ba,&pt,&tr,&pm,&sp) == _FAILURE_) { printf("\n\nError in spectra_init \n=>%s\n",sp.error_message); return _FAILURE_; } if (trg_init(&pr,&ba,&th,&pm,&sp,&nl) == _FAILURE_) { printf("\n\nError in trg_init \n=>%s\n",nl.error_message); return _FAILURE_; } /****** done ******/ if (trg_free(&nl) == _FAILURE_) { printf("\n\nError in trg_free \n=>%s\n",nl.error_message); return _FAILURE_; } if (spectra_free(&sp) == _FAILURE_) { printf("\n\nError in spectra_free \n=>%s\n",sp.error_message); return _FAILURE_; } if (primordial_free(&pm) == _FAILURE_) { printf("\n\nError in primordial_free \n=>%s\n",pm.error_message); return _FAILURE_; } if (transfer_free(&tr) == _FAILURE_) { printf("\n\nError in transfer_free \n=>%s\n",tr.error_message); return _FAILURE_; } if (bessel_free(&bs) == _FAILURE_) { printf("\n\nError in bessel_free \n=>%s\n",bs.error_message); return _FAILURE_; } if (perturb_free(&pt) == _FAILURE_) { printf("\n\nError in perturb_free \n=>%s\n",pt.error_message); return _FAILURE_; } if (thermodynamics_free(&th) == _FAILURE_) { printf("\n\nError in thermodynamics_free \n=>%s\n",th.error_message); return _FAILURE_; } if (background_free(&ba) == _FAILURE_) { printf("\n\nError in background_free \n=>%s\n",ba.error_message); return _FAILURE_; } return _SUCCESS_; }