Пример #1
0
int main() {
    count = 0;
    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 */

    FILE * fp;
    FILE * fp2;

    char f_tmp[50];
    double fbest[NREDSHIFT][LMAX];

    int i,j;
    int num_ct_max=7;

    int lmax, lmin;
    int l; /* l starts from 2 (quadrupole) in spectrum calculation, but
              starts from lmin in calculating Fisher matrix. */

    double * psCl;  /* power spectrum in 2-D */
    double * psCl_fid;
    double * dCldPara[NVARY]; /* first derivatives: for Fisher matrix */
    double * var_cl; /* uncertainty of Cl's */

    double z;
    int iz;
    double khmax; /* We used fitting formula to get *smallest* nonlinear kh, or 
                     the turning point from linear to nonlinear; see the paper or the
                     accompanying mathematica notebook (please email me for it) for detail */

    double zs[NREDSHIFT];
    double pvecback[100];

    double fisher[NVARY][NVARY]= {0};

    /* parameter of the telescope */
    double tele_res0 = 2.9; /* also FWHM, angular resolution in arc minute, 
                               will be in the following converted to radian */
    double tele_res; /* angular resolution at z: tele_res = tele_res0*(1+z) */
    double tele_temp_sys = 20.; /* system temperature in K; always mK in calculation */
    double tele_time_pix; /* observation time per pixel, in [sec] */
    double tele_freq = 0.2e6; /* delta_nu in Hz; [tele_] noise = temp_sys /
                                 sqrt(freq*time_pix) */

    //Two settings are considered in the paper. 
/*
    double tele_time_tot = 8. ;//survey time in [yr] 
    int    tele_num_beam=100;
    double tele_fsky = 0.575;
*/
    double tele_time_tot = 1. ;// survey time in [yr] 
    int    tele_num_beam=1;
    double tele_fsky = 0.0005;

    double noise_ins;
    double noise_fg = 0.; /* RMS of the residual foreground noise, in mK; currently 
                             set to zero, meaning the foreground are all effectively
                             cleaned */
    double deltaN;
    double tmp_cl_noise;
    double delta_N;
    double freqH = 1420.4;
    double freq_step, center_f;

    struct file_content fc;

    double tau;
    double deg2rad = M_PI/180.;
    double yr2sec = 31556926;
    tele_res0 = tele_res0/60.*deg2rad;
    freq_step = freqH*(1./(1.+ZMIN) - 1./(1.+ZMAX))/(NREDSHIFT-1.);

    center_f=freqH*(1./(1.+ZMIN));
    for(iz=0; iz<NREDSHIFT; iz++,center_f-=freq_step) zs[iz] = freqH/center_f-1;

for(iz=0;iz<NREDSHIFT; iz++) printf("one z is %f \n",zs[iz]);
    if (set_fiducial(&fc,errmsg) == _FAILURE_) {
        printf("\n\nError set fiducial parameters \n=>%s\n",errmsg);
        return _FAILURE_;
    }

    /* read input parameters and compute bessel functions once and for all */
    if (input_init(&fc,&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 (bessel_init(&pr,&bs) == _FAILURE_) {
        printf("\n\nError in bessel_init \n =>%s\n",bs.error_message);
        return _FAILURE_;
    }

fp2=fopen("fsky_best.dat", "w");
    for (iz = 0; iz<NREDSHIFT; iz++) {
        z = zs[iz];
        tele_res = tele_res0 * (1.+z); /* [minute deg] tele_res[minute degree] grows with z */
        khmax=0.170255158514253+0.13037369068045573*z-0.10832440336426165* pow(z,2)+0.046760120277751734*pow(z,3) - 0.005948516308501343* pow(z,4); /* fitting formula for minimum NL k-modes */
        background_tau_of_z(&ba, z, &tau);
        lmax = (int) (khmax*ba.h * (ba.conformal_age - tau));
        printf("lmax = %d\n", lmax);
        background_functions(&ba, 1/(1.+z), 1, pvecback);
        lmin = (int) (ba.conformal_age - tau) * 2*M_PI/(pow(1+z,2)/pvecback[ba.index_bg_H]*freq_step/freqH);
        printf("lmin = %d\n", lmin);
        psCl_fid = malloc((lmax+1)*sizeof(double));
        var_cl = malloc((lmax+1)*sizeof(double));
        for(i=0; i<NVARY; i++) dCldPara[i] = malloc((lmax+1)*sizeof(double));

        class_assuming_bessels_computed(&fc,&pr,&ba,&th,&pt,&bs,&tr,&pm,&sp,&nl,&le,&op, \
                                        z,psCl_fid,lmin,lmax,errmsg);
	for(i=lmin; i<=lmax; i+=10) printf("psCl_fid[%d] = %e\n", i, psCl_fid[i]); 

        for (i=0; i<NVARY; i++) {
            /* Vary each parameter and get dCldPara[ipara][il]'s at redshift z. Allocate
             * an array psCl first; psCl is used only inside the loop, so allocate
             * and free inside the loop. */
            psCl = malloc((lmax+1)*sizeof(double));
            vary_parameter(&fc,&pr,&ba,&th,&pt,&bs,&tr,&pm,&sp,&nl,&le,&op, \
                           z,psCl,psCl_fid,lmin,lmax,i,dCldPara[i],errmsg);
            free(psCl);
        }

        tele_res=tele_res0 * (1+z);
        tele_time_pix=tele_time_tot * yr2sec * tele_num_beam * pow(tele_res,2) / (4*M_PI*tele_fsky);
        /* Radio equation in mK, in consistence with signal
         * dimension. */
        noise_ins = tele_temp_sys*1000. / sqrt(tele_freq*tele_time_pix);

        delta_N = noise_ins + noise_fg;
        for (l=lmin; l<=lmax; l++) {
            tmp_cl_noise = pow(tele_res,2) * pow(delta_N,2) * exp(l*(l+1) * pow(tele_res,2)/(8*log(2)));
            var_cl[l] = 2./((2*l+1)*tele_fsky) * pow(psCl_fid[l]+tmp_cl_noise, 2); //variance of cl
            printf("noise vs signal: %e, %e\n", tmp_cl_noise,psCl_fid[l]);
            fbest[iz][l]=(tele_freq*tele_num_beam*tele_time_tot*yr2sec*psCl_fid[l])/(4*M_PI*pow(tele_temp_sys*1000.,2)*exp(l*(l+1) * pow(tele_res,2)/(8*log(2))));
        }

     if(NVARY != 0)
        for (i = 0; i<NVARY; i++)
            for (j = 0; j<NVARY; j++)
                for (l = lmin; l <= lmax; l++)
                    fisher[i][j] += dCldPara[i][l]*dCldPara[j][l]/var_cl[l];

        free(psCl_fid);
        free(var_cl);
        for(i=0; i<NVARY; i++) free(dCldPara[i]);
    }
    for (iz=0; iz<NREDSHIFT; iz++)
    {
      for (l=0; l<LMAX; l++) fprintf(fp2, "%f ", fbest[iz][l]);
      fprintf(fp2, "\n");
    } 
    fclose(fp2);

    fp = fopen("fisher.mat","w"); 

    for (i=0; i<NVARY; i++) {
        for (j=0; j<NVARY; j++) {
            printf("%20.10e ", fisher[i][j]);
	        fprintf(fp, "%20.10e ", fisher[i][j]);
        }
        printf("\n");
    	fprintf(fp, "\n");
    }
    for (i=0; i<NVARY; i++) fprintf(fp, "%s ", fc.name[i]);
    fprintf(fp, "\n");
    for (i=0; i<NVARY; i++) fprintf(fp, "%s ", fc.value[i]);
    fprintf(fp, "\n");

    fclose(fp);

    /* now free the bessel structure */
    if (bessel_free(&bs) == _FAILURE_)  {
        printf("\n\nError in bessel_free \n=>%s\n",bs.error_message);
        return _FAILURE_;
    }

    return _SUCCESS_;

}
Пример #2
0
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 sepctra */
  struct output op;           /* for output files */
  ErrorMsg errmsg;            /* for error message */

  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_;
  }

  /********************************************/
  /***** output thermodynamics quantities *****/
  /********************************************/
  
  int i;
  double tau;
  double z;
  int last_index;
  double pvecback[30];
  double pvecthermo[30];

  printf("#1: redshift z\n");
  printf("#2: conformal time tau\n");
  printf("#3: electron ionization fraction x_e\n");
  printf("#4: Thomson scattering rate kappa'\n");
  printf("#5: Thomson scattering rate derivative kappa''\n");
  printf("#6: Thomson scattering rate derivative kappa'''\n");
  printf("#7: exponential of optical depth e^-kappa\n");
  printf("#8: visibility function g = kappa' e^-kappa \n");
  printf("#9: derivative of visibility function g' \n");
  printf("#10: second derivative of visibility function g'' \n");
  printf("#11: squared baryon temperature\n");
  printf("#12: squared baryon sound speed c_b^2 \n");
  printf("#13: baryon drag optical depth tau_d \n");
  printf("#14: variation rate \n");

  /* first, quantities stored in table */

  for (i=0; i < th.tt_size; i++) {

    background_tau_of_z(&ba,th.z_table[i],&tau);

    printf("%.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e\n",
	   th.z_table[i],
	   tau,
	   th.thermodynamics_table[i*th.th_size+th.index_th_xe],
	   th.thermodynamics_table[i*th.th_size+th.index_th_dkappa],
	   th.thermodynamics_table[i*th.th_size+th.index_th_ddkappa],
	   th.thermodynamics_table[i*th.th_size+th.index_th_dddkappa],
	   th.thermodynamics_table[i*th.th_size+th.index_th_exp_m_kappa],
	   th.thermodynamics_table[i*th.th_size+th.index_th_g],
	   th.thermodynamics_table[i*th.th_size+th.index_th_dg],
	   th.thermodynamics_table[i*th.th_size+th.index_th_ddg],
	   th.thermodynamics_table[i*th.th_size+th.index_th_Tb],
	   th.thermodynamics_table[i*th.th_size+th.index_th_cb2],
	   th.thermodynamics_table[i*th.th_size+th.index_th_tau_d],
	   th.thermodynamics_table[i*th.th_size+th.index_th_rate]
	   );

  }

  /* the function thermodynamics_at_z knows how to extrapolate at
     redshifts above the maximum redshift in the table. Here we add to
     the previous output a few more points at higher redshift. */

  for (z = th.z_table[th.tt_size-1]; z < 1000*th.z_table[th.tt_size-1]; z *= 2.) {

    background_tau_of_z(&ba,z,&tau);
    
    background_at_tau(&ba,tau,ba.normal_info,ba.inter_normal,&last_index,pvecback);

    thermodynamics_at_z(&ba,&th,z,th.inter_normal,&last_index,pvecback,pvecthermo);
    
    printf("%.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e %.10e\n",
	   z,
	   tau,
	   pvecthermo[th.index_th_xe],
	   pvecthermo[th.index_th_dkappa],
	   pvecthermo[th.index_th_ddkappa],
	   pvecthermo[th.index_th_dddkappa],
	   pvecthermo[th.index_th_exp_m_kappa],
	   pvecthermo[th.index_th_g],
	   pvecthermo[th.index_th_dg],
	   pvecthermo[th.index_th_ddg],
	   pvecthermo[th.index_th_Tb],
	   pvecthermo[th.index_th_cb2],
	   pvecthermo[th.index_th_tau_d],
	   pvecthermo[th.index_th_rate]
	   );
    
  }

  /****** all calculations done, now free the structures ******/

  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_;

}
Пример #3
0
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_;

}