ClassEngine.hh 7.81 KB
Newer Older
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
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
//--------------------------------------------------------------------------
//
// Description:
// 	class ClassEngine :
// encapsulation of class calls
//
//
// Author List:
//	Stephane Plaszczynski (plaszczy@lal.in2p3.fr)
//
// History (add to end):
//	creation:   ven. nov. 4 11:02:20 CET 2011 
//
//-----------------------------------------------------------------------

#ifndef ClassEngine_hh
#define ClassEngine_hh

//CLASS
extern  "C"{
#include"class.h"
}
#include"Engine.hh"
//STD
#include<string>
#include<vector>
#include<utility>
#include<ostream>
#include<sstream>

using std::string;

//general utility to convert safely numerical types to string
template<typename T> std::string str(const T &x);
//specialisations
template<> std::string str (const float &x);
template<> std::string str (const double &x);
template<> std::string str (const bool &x); //converts to "yes" or "no"
template<> std::string str (const std::string &x);

std::string str(const char* x);
//////////////////////////////////////////////////////////////////////////
//class to encapsulate CLASS parameters from any type (numerical or string)
class ClassParams{
public:

  ClassParams(){};
  ClassParams( const ClassParams& o):pars(o.pars){};

  //add class parameter with single value
  template<typename T> unsigned inline 
  add(const string& key,const T& val){
  pars.push_back(make_pair(key,str(val)));
  return pars.size();
  }

  //returs a class string from a list of values (assumed not empty)
  template<typename T> inline string classList(const std::vector<T>& vals){
    string valname=str(vals[0]);
    for (size_t i=1;i<vals.size();i++){
      valname+=",";
      valname+=str(vals[i]);
    }
    return valname;
  }

  //add a class parameter with a list of values
  template<typename T> inline unsigned addList(const string& key,const std::vector<T>& vals){
    string s=classList(vals);
    pars.push_back(make_pair(key,s));
    return pars.size();
  }


  //accesors
  size_t size() const;
  size_t findIndex(const string& keyname);
  string key(const unsigned& i) const;
  string value(const unsigned& i) const;
  //modifier
  void updateVal(const unsigned& i,const string& newval);


private:
  //ordered list of key-value pairs
  std::vector<std::pair<string,string> > pars;
};

///////////////////////////////////////////////////////////////////////////
class ClassEngine : public Engine
{

  friend class ClassParams;

public:
  //constructors
  ClassEngine(const ClassParams& pars);
  //with a class .pre file
  ClassEngine(const ClassParams& pars,const string & precision_file);
  

  // destructor
  ~ClassEngine();

  //obsolote: to be removed
  bool updateParValues(const std::vector<double>& par);

  //update from ClassParams class (should be same size than in constructor)
  bool updateParValues(const ClassParams& par);

111 112
  //services
  virtual bool has_Background() const {return true;}
113 114 115 116 117 118 119 120 121 122 123 124
//   //virtual bool has_CMB_Cl() const {return (_output.find("tCl")!=std::string::npos);}
//   virtual bool has_CMB_ClPol() const {return (_output.find("pCl")!=std::string::npos);}
//   virtual bool has_CMB_Lensing() const {return (_output.find("lCl")!=std::string::npos);}
//   virtual bool has_Pklin() const {return (_output.find("mPk")!=std::string::npos);}
//   virtual bool has_PkNL() const {return _nonlin;}

  //class based
  inline bool has_CMB_Cl() const {return (pt.has_cl_cmb_temperature==_TRUE_);}
  inline bool has_CMB_ClPol() const {return (pt.has_cl_cmb_polarization==_TRUE_);}
  inline bool has_CMB_Lensing() const {return (pt.has_cl_lensing_potential==_TRUE_);}
  inline bool has_Pklin() const {return (pt.has_pk_matter==_TRUE_);}
  inline bool has_PkNL() const {return (pt.has_nl_corrections_based_on_delta_m==_TRUE_);}
125 126


Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
127 128 129
  //get value at l ( 2<l<lmax): in units = (micro-K)^2
  //don't call if FAILURE returned previously
  //throws std::execption if pb
130
  virtual std::string name() const {return "ClassEngine";}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

  double getCl(Engine::cltype t,const long &l);  
  void getCls(const std::vector<unsigned>& lVec, //input 
	      std::vector<double>& cltt, 
	      std::vector<double>& clte, 
	      std::vector<double>& clee, 
	      std::vector<double>& clbb);

  
  bool getLensing(const std::vector<unsigned>& lVec, //input 
	      std::vector<double>& clphiphi, 
	      std::vector<double>& cltphi, 
	      std::vector<double>& clephi);


146 147 148 149 150
  //recombination
  inline double z_rec() const {return th.z_rec;}
  inline double rs_rec() const {return th.rs_rec;} 

 //baryon drag 
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
151 152 153 154 155 156 157 158 159 160 161
  inline double z_drag() const {return th.z_d;}
  inline double rs_drag() const {return th.rs_d;} 

  double get_Dv(double z);
  double get_Da(double z);
  double get_sigma8(double z);
  double get_f(double z);

  double get_Fz(double z);
  double get_H(double z);
  double get_DMod(double z);
162

163 164

  //PK related stuuff
165
  double get_Pklin(double k, double z);
166
  double get_PkNL(double k, double z);
167

168 169 170 171 172 173 174
  //nodes used by class to perfrom spline interpolation (at z=0)
  //knodes/pknodes resized
  bool get_PklinNodes(std::vector<double>& knodes,std::vector<double>& Pknodes);
  bool get_PkNLNodes(std::vector<double>& knodes,std::vector<double>& Pknodes);
  // the followingdepends on switch (prefer previous methods)
  inline bool get_PkNodes(std::vector<double>& knodes,std::vector<double>& Pknodes) 
  {return (has_PkNL()? get_PkNLNodes(knodes,Pknodes) : get_PklinNodes(knodes,Pknodes));} 
175

176 177 178 179 180
#ifdef FASTPK  
  //vectorized access (if macro defined): with switch according to has_NL 
  // outout vector pks must be properly sized (see Engine.hh)
  bool get_Pkvec(const std::vector<double>& kin, const std::vector<double>& z,std::vector<double>& pks);  
  inline bool get_Pkvec(const std::vector<double>& kvec,const double& z, std::vector<double>& pks){
181 182 183
    std::vector<double> zvec(1,z);
    return  this->get_Pkvec(kvec,zvec,pks);
  }
184
#endif
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
185

186 187


188
  //combile distance
189 190
  double com_distance(double z);

191 192 193
  //linear growth density term
  double get_growthD(double z);

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
194 195
  //expansion
  inline double get_H0() const {return ba.H0*_c_/1e3;}
196
  inline double get_h() const {return get_H0()/100.;}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
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
  inline double theta() const {return th.rs_rec/th.ra_rec;}

  inline double Omega_m() const {return ba.Omega0_b+ba.Omega0_cdm+ba.Omega0_ncdm_tot;}
  //reio
  inline double get_tau_reio() const {return th.tau_reio;}
  double get_z_reio() const {return th.z_reio;}


  inline double YHe() const { return th.YHe;}

  //may need that
  inline int numCls() const {return sp.ct_size;};
  inline double Tcmb() const {return ba.T_cmb;}

  inline int l_max_scalars() const {return _lmax;}

  //print content of file_content
  void printFC();


  //access to class content:dangerous to use (but sometimes mandatory...)
  struct file_content fc;

  //structures class en commun
  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 */

232 233
private:

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
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
  ErrorMsg _errmsg;            /* for error messages */
  double * cl;

  //helpers
  bool dofree;
  int freeStructs();

  //call once /model
  int computeCls();

  int class_main(
		 struct file_content *pfc,
		 struct precision * ppr,
		 struct background * pba,
		 struct thermo * pth,
		 struct perturbs * ppt,
		 struct transfers * ptr,
		 struct primordial * ppm,
		 struct spectra * psp,
		 struct nonlinear * pnl,
		 struct lensing * ple,
		 struct output * pop,
		 ErrorMsg errmsg);
  //parnames
  std::vector<std::string> parNames;


protected:
 
263
  std::string _output;
264
  double* pvecback; // helper to avoid too much allocations
265 266


Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
267 268 269 270
};

#endif