writePk.cc 5.73 KB
Newer Older
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
#include "Parser.hh"
#include "Class/MnClassEngine.hh"
#include"cxxsupport/cxxutils.h"
#include "Timer.hh"
//#include"cxxsupport/arr.h"
#include"cxxsupport/fitshandle.h"
#include <iostream>
#include<fstream>
#include<cassert>
#include<numeric>
#include<string>
#include<cmath>
using namespace std;


/*
Utlities to check the CLASS Pk spectrum: fixed nodes location + inetrpolated values

author : S. Plaszczynski, Aug.2017
*/



void logspace(double kmin,double kmax, int npks,vector<double>&v){

  double lmax=std::log(kmax);
  double lmin=std::log(kmin);
  double step=(lmax-lmin)/(npks-1);

  v.resize(npks);
  for (int i=0;i<npks;i++){
    double lval=lmin+step*i;
    v[i]=std::exp(lval);
  }
}


void linspace(double kmin,double kmax, int npks,vector<double>&v){

  double step=(kmax-kmin)/(npks-1);
  v.resize(npks);
  for (int i=0;i<npks;i++){
    double kval=kmin+step*i;
    v[i]=kval;
  }
}





int main(int argc,char** argv){

  planck_assert(argc==3 || argc==4,"Usage: writePk parfile(in) file.fits(out) z_pk(in)");

  assert_present(argv[1]);


  float z0 = 0.;
  if (argc==4) z0=atof(argv[3]);

  //decodage arguments
  Parser parser(argv[1]);

  //choose engine
  string engine= parser.params.find<string>("engine","CLASS");
  if (engine=="pico") {
    throw  Message_error("pico cannot be used for Pk !");
  }

  ClassParams classparms(parser.classparms);
  classparms.add("output","mPk"); //polar +lens+clphi
  
  classparms.add("z_pk",z0); //polar +lens+clphi
  

  //no lensing
  try{
    size_t i=classparms.findIndex("lensing");
    cout << "setting lensing to false" << endl;
    classparms.updateVal(i,str(false));
  }
  catch (Message_error&){
    classparms.add("lensing",false);
  }
  
  //define kmax and kvalues either from file either given by k_end

    double kmax=-1;
    vector<double>  kval;
    string kvfile=parser.params.find<string>("kval_file","");
    bool h_rescale=parser.params.find<bool>("h_rescale",false);
    //if file
    if (kvfile.size()!=0) {
      assert_present(kvfile);
      cout <<"using k values from file=" <<kvfile <<endl;
      //decode 1st column
      ifstream f(kvfile.c_str());
      string line;
      double kk;
      while (getline(f,line)) {
	//skip comments
	if (line.find("#",0) !=std:: string::npos) continue;
	std::istringstream str(line);
	if (str>>kk) {
	  kval.push_back(kk);
	}
      }
      kmax=kval.back();
    }
    else{
      kmax = parser.params.find<double>("kmax",1);
      const int npks = parser.params.find<int>("n_pks",1000);
      const double kmin = parser.params.find<double>("kmin",8.e-4);
      bool lin=parser.params.find<bool>("linspace",false);
      lin? linspace(kmin,kmax,npks,kval) : logspace(kmin,kmax,npks,kval) ; 
      
    }
    //modify class param accordingly
    string label=(h_rescale? "P_k_max_h/Mpc" : "P_k_max_1/Mpc");
    cout << label << " set to " << kmax <<endl;

    try{
      size_t i=classparms.findIndex(label);
      classparms.updateVal(i,dataToString(kmax));
    }
    catch (Message_error&){
      classparms.add(label,kmax);
    }




  //precision file
  string pre="";
  if (parser.params.param_present("precisionFile")){
    pre=Parser::CheckPath(parser.params.find<string>("precisionFile",""));
  }
  
  
  cout << "CLASS \t--> precision parameters taken from file=" <<  pre << endl;
  MnClassEngine* e=new MnClassEngine(parser.vars(),
		      classparms,
		      pre);
 

  cout << "Computing P(k) at z=" << z0 << endl;
  ClassEngine* klass=e->getClass();

  //output
  const std::string fileName(argv[2]);
  cout << "writing FITS file =" << fileName << endl;
  if (file_present(fileName)) {
    //std::cout << "the file " + fileName + " already exists: removing"<<endl;
    remove_file(fileName);
  }
  fitshandle fout(fileName,fitshandle::CREATE, READWRITE);

  bool do_nl =e->has_PkNL();
  
  //nodes
  {
    std::vector<fitscolumn> pkcols;
    pkcols.push_back(fitscolumn("knode", "1/Mpc",1,TDOUBLE));
    pkcols.push_back(fitscolumn("pknode", "(Mpc)^3" ,1,TDOUBLE));
    if (do_nl) pkcols.push_back(fitscolumn("pknodenl", "(Mpc)^3" ,1,TDOUBLE));
    fout.insert_bintab(pkcols);
    
    vector<double> knode, pknode,pknodeNL;
    klass->get_PkNodes(knode,pknode);
     if (do_nl) klass->get_PkNLNodes(knode,pknodeNL);

    for (size_t i=0;i<knode.size();i++){
      fout.write_column(1,knode[i],i);
      fout.write_column(2,pknode[i],i);
       if (do_nl) fout.write_column(3,pknodeNL[i],i);
    }



  }

  //interp values

   //test fast Pk
  /*
  Timer timer;
  cout <<"Pk timer test..." << endl;
  vector<double> pkvec(kval.size());
  for (size_t i=0;i<100;i++)
    e->get_Pkvec(kval,z0,pkvec);
  cout << "fast=" << timer.partial() <<endl;

  for (size_t i=0;i<100;i++){
    for (int k =0; k<kval.size() ; k++){
      double pk=e->get_Pklin(kval[k], 0);
    }
  }
  cout << "std=" << timer.partial() <<endl;
  */


  {
    std::vector<fitscolumn> pkcols;
    pkcols.push_back(fitscolumn("k", "1/Mpc",1,TDOUBLE));
    pkcols.push_back(fitscolumn("pklin", "(Mpc)^3" ,1,TDOUBLE));
    if (do_nl) pkcols.push_back(fitscolumn("pknl", "(Mpc)^3" ,1,TDOUBLE));

    fout.insert_bintab(pkcols);
    
    //optional rescale
    double h=e->get_H0()/100;
    //warning h_rescaling
    if (h_rescale)
      std::transform(kval.begin(),kval.end(),kval.begin(),bind1st(multiplies<double>(),h));
    cout <<"computing Pk from " << kval[0] << " to " << kval.back() << endl;

    for (size_t i=0;i<kval.size();i++){
      fout.write_column(1,kval[i],i);
      fout.write_column(2,klass->get_Pklin(kval[i],z0),i);
      if (do_nl) fout.write_column(3,klass->get_PkNL(kval[i],z0),i);
    }
    cout <<"Growth factor D(z=" << z0 << ")="<< klass->get_growthD(z0) << endl;
    fout.add_key("D",klass->get_growthD(z0),"growth factor");
    fout.add_key("f",klass->get_f(z0),"growth rate");

  }
  
  fout.close();




  delete e;

  return 0;
}