ClassEngine.cc 19.3 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
//--------------------------------------------------------------------------
//
// Description:
// 	class ClassEngine : see header file (ClassEngine.hh) for description.
//
//------------------------------------------------------------------------
//-----------------------
// This Class's Header --
//-----------------------
#include "ClassEngine.hh"
// ------------------------
// Collaborating classes --
//-------------------------
// C++
//--------------------
#include<iostream>
#include <iomanip>
#include<string>
#include<cmath>
#include <stdexcept>
#include<sstream>
#include<numeric>
#include<cassert>
#include"cxxutils.h"

//#define DBUG

using namespace std;

template<typename T> std::string str(const T &x){
  std::ostringstream os;
  os << x;
  return os.str();
};
//specilization
template<> std::string str (const float &x){
  std::ostringstream os;
  os << setprecision(8) << x;
  return os.str();
}
template<> std::string str (const double &x){
  std::ostringstream os;
  os << setprecision(16) << x;
  return os.str();
}
template<> std::string str (const bool &x){
  { return x ? "yes" : "no"; }
}

template<> std::string str (const std::string &x) {return x;}

std::string str (const char* s){return string(s);}

//instanciations
template string str(const int &x);
template string str(const signed char &x);
template string str(const unsigned char &x);
template string str(const short &x);
template string str(const unsigned short &x);
template string str(const unsigned int &x);
template string str(const long &x);
template string str(const unsigned long &x);
template string str(const long long &x);
template string str(const unsigned long long &x);


size_t
ClassParams::findIndex(const string& name){
  int index=-1;
  for (size_t i=0;i<size();i++){
    if (key(i)==name) {
      index=i; break;
    }
  }
75
  if (index<0) throw Message_error(string("key ")+name+ " not found in ClassParams");
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
76 77 78 79 80 81 82 83 84 85 86 87
  return static_cast<size_t>(index);
}

size_t ClassParams::size() const {return pars.size();}
string ClassParams::key(const unsigned& i) const {return pars[i].first;}
string ClassParams::value(const unsigned& i) const {return pars[i].second;}
void ClassParams::updateVal(const unsigned& i,const string& newval) {pars[i].second=newval;}


//---------------
// Constructors --
//----------------
88
ClassEngine::ClassEngine(const ClassParams& pars): cl(0),dofree(true),pvecback(0){
Plaszczynski Stephane's avatar
mieux  
Plaszczynski Stephane committed
89
 
90 91
  _lmax=-1; //default

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  cout << "Running CLASS version " << _VERSION_ << endl;

  //prepare fp structure
  size_t n=pars.size();
  //
  parser_init(&fc,n,"pipo",_errmsg);
  
  //config
  for (size_t i=0;i<pars.size();i++){
    strcpy(fc.name[i],pars.key(i).c_str());
    strcpy(fc.value[i],pars.value(i).c_str());
    //store
    parNames.push_back(pars.key(i));
    //identify lmax
    cout << pars.key(i) << "\t" << pars.value(i) <<endl;
    if (pars.key(i)=="l_max_scalars") {
      istringstream strstrm(pars.value(i));
      strstrm >> _lmax;
    }
111 112
    //store class output
    if (pars.key(i)=="output") _output=pars.value(i);
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
113

114
    //is nonlinear set
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
115
    //if (pars.key(i)=="non linear") _nonlin=true;
116 117

  }
Matthieu Tristram's avatar
Matthieu Tristram committed
118
  //input
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
119 120 121
  if (input_init(&fc,&pr,&ba,&th,&pt,&tr,&pm,&sp,&nl,&le,&op,_errmsg) == _FAILURE_) 
    throw invalid_argument(_errmsg);

Matthieu Tristram's avatar
Matthieu Tristram committed
122
  //protection parametres mal defini
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
123 124 125 126 127 128 129 130 131 132 133 134
  for (size_t i=0;i<pars.size();i++){
    if (fc.read[i] !=_TRUE_) throw invalid_argument(string("invalid CLASS parameter: ")+fc.name[i]);
  }

  //calcul class
  computeCls();
  
  //cout <<"creating " << sp.ct_size << " arrays" <<endl;
  cl=new double[sp.ct_size];

  //printFC();

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
135
  }
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
136

137
ClassEngine::ClassEngine(const ClassParams& pars,const string & precision_file): cl(0),dofree(true),pvecback(0){
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
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

  cout << "Running CLASS version " << _VERSION_ << endl;

  struct file_content fc_precision;
  fc_precision.size = 0;
  //decode pre structure
  if (parser_read_file(const_cast<char*>(precision_file.c_str()),&fc_precision,_errmsg) == _FAILURE_){
    throw invalid_argument(_errmsg);
  }

  //pars
  struct file_content fc_input;
  fc_input.size = 0;
  fc_input.filename=new char[1];
 //prepare fc par structure
  size_t n=pars.size();
  parser_init(&fc_input,n,"pipo",_errmsg);
  //config
  for (size_t i=0;i<pars.size();i++){
    strcpy(fc_input.name[i],pars.key(i).c_str());
    strcpy(fc_input.value[i],pars.value(i).c_str());
    //store
    parNames.push_back(pars.key(i));
    if (pars.key(i)=="l_max_scalars") {
      istringstream strstrm(pars.value(i));
      strstrm >> _lmax;
    }
165 166 167
    //store class output
    if (pars.key(i)=="output") _output=pars.value(i);
    //is nonlinear set?
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
168
    //if (pars.key(i)=="non linear") _nonlin=true;
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
169

170
  }
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
171 172 173 174 175 176 177 178 179 180 181

  //concatenate both
  if (parser_cat(&fc_input,&fc_precision,&fc,_errmsg) == _FAILURE_) throw invalid_argument(_errmsg);

  //parser_free(&fc_input);
  parser_free(&fc_precision);
  
  //input
  if (input_init(&fc,&pr,&ba,&th,&pt,&tr,&pm,&sp,&nl,&le,&op,_errmsg) == _FAILURE_) 
    throw invalid_argument(_errmsg);

Matthieu Tristram's avatar
Matthieu Tristram committed
182
  //protection parametres mal defini
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
183 184 185 186 187 188 189 190 191 192
  for (size_t i=0;i<pars.size();i++){
    if (fc.read[i] !=_TRUE_) throw invalid_argument(string("invalid CLASS parameter: ")+fc.name[i]);
  }

  //calcul class
  computeCls();
  
  //cout <<"creating " << sp.ct_size << " arrays" <<endl;
  cl=new double[sp.ct_size];

193
  printFC();
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
194
 
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
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 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 263 264 265 266 267 268 269 270 271 272 273
}


//--------------
// Destructor --
//--------------
ClassEngine::~ClassEngine()
{

  //printFC();
  dofree && freeStructs();

  delete [] cl;

}

//-----------------
// Member functions --
//-----------------
bool ClassEngine::updateParValues(const std::vector<double>& par){
  dofree && freeStructs();
  for (size_t i=0;i<par.size();i++) {
    double val=par[i];
    strcpy(fc.value[i],str(val).c_str());
    strcpy(fc.name[i],parNames[i].c_str());
#ifdef DBUG
    cout << "update par values #" <<  parNames[i].c_str() << "\t" <<  val << endl;
#endif
  }
  int status=computeCls();
#ifdef DBUG
  cout << "update par status=" << status << " succes=" << _SUCCESS_ << endl;
#endif

  return (status==_SUCCESS_);
}


bool ClassEngine::updateParValues(const ClassParams& pars){
  dofree && freeStructs();
  for (size_t i=0;i<pars.size();i++) {
    strcpy(fc.name[i],pars.key(i).c_str());
    strcpy(fc.value[i],pars.value(i).c_str());
  }
  int status=computeCls();
  
  return (status==_SUCCESS_);

}


//print content of file_content
void ClassEngine::printFC() {
  printf("FILE_CONTENT SIZE=%d\n",fc.size);
  for (int i=0;i<fc.size;i++) printf("%d : %s = %s\n",i,fc.name[i],fc.value[i]);


}
int ClassEngine::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) {
  

  if (input_init(pfc,ppr,pba,pth,ppt,ptr,ppm,psp,pnl,ple,pop,errmsg) == _FAILURE_) {
    printf("\n\nError running input_init_from_arguments \n=>%s\n",errmsg);
    dofree=false;
    return _FAILURE_;
  }

274
  delete [] pvecback;
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
275 276 277 278 279
  if (background_init(ppr,pba) == _FAILURE_) {
    printf("\n\nError running background_init \n=>%s\n",pba->error_message);
    dofree=false;
    return _FAILURE_;
  }
280 281
  pvecback=new double[ba.bg_size];

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

  if (thermodynamics_init(ppr,pba,pth) == _FAILURE_) {
    printf("\n\nError in thermodynamics_init \n=>%s\n",pth->error_message);
    background_free(&ba);
    dofree=false;
    return _FAILURE_;
  }

  if (perturb_init(ppr,pba,pth,ppt) == _FAILURE_) {
    printf("\n\nError in perturb_init \n=>%s\n",ppt->error_message);
    thermodynamics_free(&th);
    background_free(&ba);
    dofree=false;
    return _FAILURE_;
  }

  if (primordial_init(ppr,ppt,ppm) == _FAILURE_) {
    printf("\n\nError in primordial_init \n=>%s\n",ppm->error_message);
    perturb_free(&pt);
    thermodynamics_free(&th);
    background_free(&ba);
    dofree=false;
    return _FAILURE_;
  }

  if (nonlinear_init(ppr,pba,pth,ppt,ppm,pnl) == _FAILURE_)  {
    printf("\n\nError in nonlinear_init \n=>%s\n",pnl->error_message);
    primordial_free(&pm);
    perturb_free(&pt);
    thermodynamics_free(&th);
    background_free(&ba);
    dofree=false;
    return _FAILURE_;
  }

  if (transfer_init(ppr,pba,pth,ppt,pnl,ptr) == _FAILURE_) {
    printf("\n\nError in transfer_init \n=>%s\n",ptr->error_message);
    nonlinear_free(&nl);
    primordial_free(&pm);
    perturb_free(&pt);
    thermodynamics_free(&th);
    background_free(&ba);
    dofree=false;
    return _FAILURE_;
  }

  if (spectra_init(ppr,pba,ppt,ppm,pnl,ptr,psp) == _FAILURE_) {
    printf("\n\nError in spectra_init \n=>%s\n",psp->error_message);
    transfer_free(&tr);
    nonlinear_free(&nl);
    primordial_free(&pm);
    perturb_free(&pt);
    thermodynamics_free(&th);
    background_free(&ba);
    dofree=false;
    return _FAILURE_;
  }

  if (lensing_init(ppr,ppt,psp,pnl,ple) == _FAILURE_) {
    printf("\n\nError in lensing_init \n=>%s\n",ple->error_message);
    spectra_free(&sp);
    transfer_free(&tr);
    nonlinear_free(&nl);
    primordial_free(&pm);
    perturb_free(&pt);
    thermodynamics_free(&th);
    background_free(&ba);
    dofree=false;
    return _FAILURE_;
  }


  dofree=true;
  return _SUCCESS_;
}


int
ClassEngine::freeStructs(){
  
  
  if (lensing_free(&le) == _FAILURE_) {
    printf("\n\nError in spectra_free \n=>%s\n",le.error_message);
    return _FAILURE_;
  }

  if (spectra_free(&sp) == _FAILURE_) {
    printf("\n\nError in spectra_free \n=>%s\n",sp.error_message);
    return _FAILURE_;
  }
  
  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_;
}

double
ClassEngine::getCl(Engine::cltype t,const long &l){

  if (!dofree) throw out_of_range("no Cl available because CLASS failed");

  if (output_total_cl_at_l(&sp,&le,&op,static_cast<double>(l),cl) == _FAILURE_){
    cerr << ">>>fail getting Cl type=" << (int)t << " @l=" << l <<endl; 
    throw out_of_range(sp.error_message);
  }

  double zecl=-1;

  double tomuk=1e6*Tcmb();
  double tomuk2=tomuk*tomuk;

  switch(t)
    {
    case TT:
      (sp.has_tt==_TRUE_) ? zecl=tomuk2*cl[sp.index_ct_tt] : throw invalid_argument("no ClTT available");
      break;
    case TE:
      (sp.has_te==_TRUE_) ? zecl=tomuk2*cl[sp.index_ct_te] : throw invalid_argument("no ClTE available");
      break; 
    case EE:
      (sp.has_ee==_TRUE_) ? zecl=tomuk2*cl[sp.index_ct_ee] : throw invalid_argument("no ClEE available");
      break;
    case BB:
      (sp.has_bb==_TRUE_) ? zecl=tomuk2*cl[sp.index_ct_bb] : throw invalid_argument("no ClBB available");
      break;
    case PP:
      (sp.has_pp==_TRUE_) ? zecl=cl[sp.index_ct_pp] : throw invalid_argument("no ClPhi-Phi available");
      break;
    case TP:
      (sp.has_tp==_TRUE_) ? zecl=tomuk*cl[sp.index_ct_tp] : throw invalid_argument("no ClT-Phi available");
      break;
    case EP:
      (sp.has_ep==_TRUE_) ? zecl=tomuk*cl[sp.index_ct_ep] : throw invalid_argument("no ClE-Phi available");
      break;
    }
  
  return zecl;

}


int ClassEngine::computeCls(){

#ifdef DBUG
  cout <<"call computecls" << endl;
  printFC();
#endif

  int status=this->class_main(&fc,&pr,&ba,&th,&pt,&tr,&pm,&sp,&nl,&le,&op,_errmsg);
#ifdef DBUG
  cout <<"status=" << status << endl;
#endif
  return status;

}


void 
ClassEngine::getCls(const std::vector<unsigned>& lvec, //input 
		      std::vector<double>& cltt, 
		      std::vector<double>& clte, 
		      std::vector<double>& clee, 
		      std::vector<double>& clbb)
{
  cltt.resize(lvec.size());
  clte.resize(lvec.size());
  clee.resize(lvec.size());
  clbb.resize(lvec.size());
  
  for (size_t i=0;i<lvec.size();i++){
    try{
      cltt[i]=getCl(ClassEngine::TT,lvec[i]);
      clte[i]=getCl(ClassEngine::TE,lvec[i]);
      clee[i]=getCl(ClassEngine::EE,lvec[i]);
      clbb[i]=getCl(ClassEngine::BB,lvec[i]);
    }
    catch(exception &e){
      throw e;
    }
  }

}
 
bool 
ClassEngine::getLensing(const std::vector<unsigned>& lvec, //input 
		std::vector<double>& clpp    , 
		std::vector<double>& cltp  , 
		std::vector<double>& clep  ){
 

  clpp.resize(lvec.size());
  cltp.resize(lvec.size());
  clep.resize(lvec.size());
  
  for (size_t i=0;i<lvec.size();i++){
    try{
508
      /*
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
509 510 511
      clpp[i]=getCl(ClassEngine::PP,lvec[i])/tr.lcmb_rescale/tr.lcmb_rescale;
      cltp[i]=getCl(ClassEngine::TP,lvec[i])/tr.lcmb_rescale;
      clep[i]=getCl(ClassEngine::EP,lvec[i])/tr.lcmb_rescale;
512 513 514 515 516
      */
      clpp[i]=getCl(ClassEngine::PP,lvec[i]);
      cltp[i]=getCl(ClassEngine::TP,lvec[i]);
      clep[i]=getCl(ClassEngine::EP,lvec[i]);

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
    }
    catch(exception &e){
      cout << "plantage!" << endl;
      cout << __FILE__ << e.what() << endl;
      return false;
    }
  }
  return true;
}

double ClassEngine::get_f(double z)
{
  double tau;
  int index;
  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);

  //call to fill pvecback
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);

  double f_z=pvecback[ba.index_bg_f];
#ifdef DBUG
  cout << "f_of_z= "<< f_z <<endl;
#endif
  return f_z;
}

544 545 546 547 548 549 550 551 552 553 554 555 556 557
double ClassEngine::get_growthD(double z)
{
  double tau;
  int index;
  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);

  //call to fill pvecback
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);
  double D=pvecback[ba.index_bg_D];

  return D;
}

558
double ClassEngine::get_Pklin(double k, double z){
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
  double tau;
  int index;
  double * pk_ic = NULL;


  double mypk =0.;
  background_tau_of_z(&ba,z,&tau);
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);

  int ret=spectra_pk_at_k_and_z(&ba,&pm,&sp,k,z,&mypk,pk_ic);
  return mypk;
}

double ClassEngine::get_PkNL(double k, double z){
  double tau;
  int index;
  double mypk;
  background_tau_of_z(&ba,z,&tau);
577
  //cout << "pknl  after bkg "<<tau<< endl;
578
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);
579
  //cout << "pknl  after bkg @tau "<<tau<< " z "<<z<< " k "<< k << endl;
580 581 582 583
  int ret = spectra_pk_nl_at_k_and_z(&ba,&pm,&sp,k,z,&mypk);

  return mypk;
}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611

double ClassEngine::get_sigma8(double z)
{
  double tau;
  int index;
  double sigma8 = -99.;

  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);

  //call to fill pvecback
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);
  //background_at_tau(pba,tau,pba->long_info,pba->inter_normal,&last_index,pvecback);
  spectra_sigma(&ba,&pm,&sp,8./ba.h,z,&sigma8);
#ifdef DBUG
  cout << "sigma_8= " << sigma8 <<" at "<<z<<" tau "<<tau<<" bah "<<ba.h<< "size " << ba.bg_size << " index "<< index<<endl;
#endif
  planck_assert( sigma8 != -99. , " sigma8 could not be computed at " +str(z) +  "  \n 1) check z_pk and z_max_pk parameters for class \n 2)  that you have enabled the Pk computation with : do_mPk = true  \n "); 

  return sigma8;
}

double ClassEngine::get_Dv(double z)
{
  double tau;
  int index;
  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);
Matthieu Tristram's avatar
Matthieu Tristram committed
612
//   cout << " dv : tau= "<< tau <<endl;
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665

  //call to fill pvecback
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);


  double H_z=pvecback[ba.index_bg_H];
  double D_ang=pvecback[ba.index_bg_ang_distance];
#ifdef DBUG
  cout << "H_z= "<< H_z <<endl;
  cout << "D_ang= "<< D_ang <<endl;
#endif
  double D_v;

  D_v=pow(D_ang*(1+z),2)*z/H_z;
  D_v=pow(D_v,1./3.);
#ifdef DBUG
  cout << D_v << endl;
#endif
  return D_v;
}

double ClassEngine::get_Fz(double z)
{
  double tau;
  int index;
  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);

  //call to fill pvecback
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);


  double H_z=pvecback[ba.index_bg_H];
  double D_ang=pvecback[ba.index_bg_ang_distance];
#ifdef DBUG
  cout << "H_z= "<< H_z <<endl;
  cout << "D_ang= "<< D_ang <<endl;
#endif
  double F_z = (1.+z) * D_ang * H_z /(3.e8) ; // is there speed of light somewhere ? 
  return F_z;
}

double ClassEngine::get_H(double z)
{
  double tau;
  int index;
  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);

  //call to fill pvecback
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);

  double H_z=pvecback[ba.index_bg_H]*_c_/1e3 ;
666

Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
667 668 669
  return(H_z);

}
670 671 672 673 674 675 676 677 678 679 680 681 682 683
double ClassEngine::com_distance(double z)
{
  double tau;
  int index;
  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);

  //call to fill pvecback
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);

  double r=pvecback[ba.index_bg_conf_distance];
  return(r);

}
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720


double ClassEngine::get_Da(double z)
{
  double tau;
  int index;
  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);

  //call to fill pvecback
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);


  double H_z=pvecback[ba.index_bg_H];
  double D_ang=pvecback[ba.index_bg_ang_distance];
#ifdef DBUG
  cout << "H_z= "<< H_z <<endl;
  cout << "D_ang= "<< D_ang <<endl;
#endif
  return D_ang;
}


double  ClassEngine::get_DMod(double z){
  // returns distance modulus at redshift z
  double tau;
  int index;
  //transform redshift in conformal time
  background_tau_of_z(&ba,z,&tau);
  //pvecback must be allocated 
  background_at_tau(&ba,tau,ba.long_info,ba.inter_normal, &index, pvecback);

  double mu = 5.*log10(pvecback[ba.index_bg_lum_distance]) + 25.;

  return(mu);

}
721 722 723

#ifdef FASTPK  
//fast access
724 725 726

#include "class_extra/spectra_extra.h"

727

728 729 730
bool
ClassEngine::get_Pkvec(const std::vector<double>& knodes, const std::vector<double>& zvec, std::vector<double>& pks)
{
Plaszczynski Stephane's avatar
Plaszczynski Stephane committed
731
  return ( has_PkNL() ? 
732 733
(spectra_fast_pk_nl_at_kvec_and_zvec(&ba,&sp,const_cast<double*>(&knodes[0]),knodes.size(),const_cast<double*>(&zvec[0]), zvec.size(),&pks[0])==_TRUE_) 
: (spectra_fast_pk_at_kvec_and_zvec(&ba,&sp,const_cast<double*>(&knodes[0]),knodes.size(),const_cast<double*>(&zvec[0]), zvec.size(),&pks[0])==_TRUE_) );
734 735 736 737

}

#endif
738 739

bool
740
ClassEngine::get_PklinNodes(std::vector<double>& knodes,std::vector<double>& pknodes){
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755

  knodes.resize(sp.ln_k_size);
  pknodes.resize(sp.ln_k_size);

  int index_mode=0;
  int index_ic1_ic2=0;
  int index_eta = sp.ln_tau_size-1;
  
  for (int index_k=0; index_k < sp.ln_k_size; index_k++){
    knodes[index_k]=std::exp(sp.ln_k[index_k]);
    pknodes[index_k]=std::exp(sp.ln_pk[(index_eta * sp.ln_k_size + index_k) * sp.ic_ic_size[index_mode] + index_ic1_ic2]);
  }
  return _SUCCESS_;

}
756 757 758 759 760 761 762 763 764 765 766

bool
ClassEngine::get_PkNLNodes(std::vector<double>& knodes,std::vector<double>& pknodes){

  knodes.resize(sp.ln_k_size);
  pknodes.resize(sp.ln_k_size);

  int index_eta = sp.ln_tau_size-1;
  
  for (int index_k=0; index_k < sp.ln_k_size; index_k++){
    knodes[index_k]=std::exp(sp.ln_k[index_k]);
767
    pknodes[index_k]=std::exp(sp.ln_pk_nl[index_eta * sp.ln_k_size + index_k]);
768 769 770 771
  }
  return _SUCCESS_;

}