void TestMatrix::runSubTest18(double& res, double& expected, std::string& subTestName) { expected = 1; subTestName = "simple_symmetric_invert"; #ifdef COSMO_LAPACK Math::SymmetricMatrix<double> mat(2, 2); mat(0, 0) = 2; mat(1, 1) = 3; mat(1, 0) = 1; mat.writeIntoTextFile("test_files/matrix_test_18_original.txt"); Math::SymmetricMatrix<double> invMat = mat; invMat.invert(); invMat.writeIntoTextFile("test_files/matrix_test_18_inverse.txt"); Math::Matrix<double> prod = mat; prod *= invMat; prod.writeIntoTextFile("test_files/matrix_test_18_product.txt"); res = 1; for(int i = 0; i < prod.rows(); ++i) { for(int j = 0; j < prod.rows(); ++j) { if(i == j) { if(!Math::areEqual(prod(i, j), 1.0, 1e-5)) { output_screen("FAIL! Diagonal element " << i << " must be 1 but it is " << prod(i, j) << std::endl); res = 0; } } else { if(!Math::areEqual(prod(i, j), 0.0, 1e-5)) { output_screen("FAIL! Non-diagonal element " << i << " " << j << " must be 0 but it is " << prod(i, j) << std::endl); res = 0; } } } } #else output_screen_clean("This test (below) is skipped because Cosmo++ has not been linked to lapack" << std::endl); res = 1; #endif }
/******************************************************************** * [函数名]: screen * [描述]: 输出信息至屏幕 * [修改记录]: * 2015-05-20,littledj: create ********************************************************************/ void GLogger::screen(const tstring format, ...) { va_list ap; va_start(ap, format); tstring msg = formatMsg_v(PRINT_TYPE::RAW, format, ap); va_end(ap); if (m_enableColor) output_screen(msg, m_defaultColor); else output_screen(msg); // 保存当前消息 saveToMessagePool(PRINT_TYPE::RAW, msg); }
/******************************************************************** * [函数名]: output * [描述]: 打印信息 * [修改记录]: * 2015-05-20,littledj: create ********************************************************************/ void GLogger::output(const tstring& msg, PRINT_COLOR color) { if (msg.empty()) output_screen(TEXT("<empty message>"), color); // 判断是否输出到屏幕 if (m_wpTarget == PRINT_TARGET::SCREEN) output_screen(msg, color); // 判断是否输出到日志文件 else if (m_wpTarget == PRINT_TARGET::FILE) output_file(msg); // 都输出 else if (m_wpTarget == PRINT_TARGET::BOTH) { output_screen(msg, color); output_file(msg); } }
int main(int argc, char *argv[]) { int nPar = 6; // Starting values for cosmological parameters // From Planck 2015 (http://arxiv.org/abs/1502.01589), Table 3, Column 4 const double h = 0.6727; const double omBH2 = 0.02225; const double omCH2 = 0.1198; const double tau = 0.079; const double ns = 0.9645; const double as = 3.094; // ln(10^10*as) const double pivot = 0.05; LambdaCDMParams params(omBH2, omCH2, h, tau, ns, std::exp(as)/1e10, pivot); Cosmo cosmo; cosmo.preInitialize(5000, false, false, false, 0, 100, 1e-6, 1.0); std::string file_name = "/Volumes/Data1/ncanac/cosmopp_neutrinos/completed_runs/standard_LCDM_mn_build/LCDM_planckposterior.txt"; std::ifstream datafile(file_name); std::string line; while(getline(datafile, line)) { std::istringstream iss(line); std::vector<double> vec; double dummy; while(iss >> dummy) vec.push_back(dummy); std::vector<double> v(&vec[0], &vec[nPar]); params.setAllParameters(v); cosmo.initialize(params, true, false, false, true); for(int i = 0; i < vec.size(); ++i) output_screen(vec[i] << " "); output_screen(cosmo.sigma8() << std::endl); } datafile.close(); return 0; }
int main(int argc, char *argv[]) { std::string root = "mh"; int nChains = 4; int burnin = 1000; // Read the resulting chain(s) with thinning const unsigned int thin = 1; MarkovChain chain(nChains, root.c_str(), burnin, thin); // Get the one dimensional marginalized posterior distributions, Gaussian smoothed with a scale of 0.3 Posterior1D* px = chain.posterior(0, Posterior1D::GAUSSIAN_SMOOTHING); Posterior1D* py = chain.posterior(1, Posterior1D::GAUSSIAN_SMOOTHING); // Get the two dimensional posterior distribution, gaussian smoothed with a scale of 0.25 Posterior2D* pxy = chain.posterior(0, 1); // Write the distributions into text files output_screen("Writing the distributions into text files..." << std::endl); px->writeIntoFile("mh_px.txt"); py->writeIntoFile("mh_py.txt"); pxy->writeIntoFile("mh_pxy.txt"); output_screen("OK" << std::endl); // Write the contour levels for the 2D distribution into a text file. This can be used later to make contour plots std::ofstream out("mh_contour_levels.txt"); out << pxy->get1SigmaLevel() << std::endl; //out << pxy->get2SigmaLevel() << std::endl; out.close(); // Delete the posterior distributions delete px; delete py; delete pxy; return 0; }
PlanckLikelihood::PlanckLikelihood(bool useCommander, bool useCamspec, bool useLensing, bool usePol, bool useActSpt, bool includeTensors, double kPerDecade, bool useOwnCmb) : spectraNames_(6), haveCommander_(false), havePol_(false), haveLens_(false), commander_(NULL), camspec_(NULL), lens_(NULL), pol_(NULL), actspt_(NULL), cmb_(NULL), modelParams_(NULL) { check(useCommander || useCamspec || useLensing || usePol || useActSpt, "at least one likelihood must be specified"); std::string planckLikeDir = PLANCK_DATA_DIR_STR; output_screen("Planck data dir = " << planckLikeDir << std::endl); int hasCl[6], lMax[6]; parname* names; spectraNames_[0] = std::string("TT"); spectraNames_[1] = std::string("EE"); spectraNames_[2] = std::string("BB"); spectraNames_[3] = std::string("TE"); spectraNames_[4] = std::string("TB"); spectraNames_[5] = std::string("EB"); lMax_ = 0; commanderLMax_ = 0; camspecLMax_ = 0; polLMax_ = 0; lensingLMax_ = 0; actSptLMax_ = 0; static PlanckLikelihoodContainer cont; if(useCommander) { std::stringstream commanderPath; commanderPath << planckLikeDir << "/commander_v4.1_lm49.clik"; char commanderPathCStr[100]; std::strcpy(commanderPathCStr, commanderPath.str().c_str()); if(!cont.commander) cont.commander = clik_init(commanderPathCStr, NULL); commander_ = cont.commander; clik_get_has_cl(commander_, hasCl, NULL); clik_get_lmax(commander_, lMax, NULL); for(int i = 0; i < 6; ++i) { output_screen1("Commander has " << spectraNames_[i] << " = " << hasCl[i] << " with l_max = " << lMax[i] << std::endl); if(lMax[i] > commanderLMax_) commanderLMax_ = lMax[i]; } if(commanderLMax_ > lMax_) lMax_ = commanderLMax_; /* int extraParams = clik_get_extra_parameter_names(commander_, &names, NULL); output_screen1("Commander has " << extraParams << " extra parameters. Their names are as follows:" << std::endl); for(int i = 0; i < extraParams; ++i) { output_screen1(names[i] << std::endl); } output_screen1(std::endl); delete names; */ } if(useCamspec) { std::stringstream camspecPath; camspecPath << planckLikeDir << "/CAMspec_v6.2TN_2013_02_26_dist.clik"; char camspecPathCStr[100]; std::strcpy(camspecPathCStr, camspecPath.str().c_str()); if(!cont.camspec) cont.camspec = clik_init(camspecPathCStr, NULL); camspec_ = cont.camspec; clik_get_has_cl(camspec_, hasCl, NULL); clik_get_lmax(camspec_, lMax, NULL); for(int i = 0; i < 6; ++i) { output_screen1("Camspec has " << spectraNames_[i] << " = " << hasCl[i] << " with l_max = " << lMax[i] << std::endl); if(lMax[i] > camspecLMax_) camspecLMax_ = lMax[i]; } if(camspecLMax_ > lMax_) lMax_ = camspecLMax_; /* int extraParams = clik_get_extra_parameter_names(camspec_, &names, NULL); output_screen1("Camspec has " << extraParams << " extra parameters. Their names are as follows:" << std::endl); for(int i = 0; i < extraParams; ++i) { output_screen1(names[i] << std::endl); } output_screen1(std::endl); delete names; */ } if(usePol) { std::stringstream polPath; polPath << planckLikeDir << "/lowlike_v222.clik"; char polPathCStr[100]; std::strcpy(polPathCStr, polPath.str().c_str()); if(!cont.pol) cont.pol = clik_init(polPathCStr, NULL); pol_ = cont.pol; clik_get_has_cl(pol_, hasCl, NULL); clik_get_lmax(pol_, lMax, NULL); for(int i = 0; i < 6; ++i) { output_screen1("Pol has " << spectraNames_[i] << " = " << hasCl[i] << " with l_max = " << lMax[i] << std::endl); if(lMax[i] > polLMax_) polLMax_ = lMax[i]; } if(polLMax_ > lMax_) lMax_ = polLMax_; /* int extraParams = clik_get_extra_parameter_names(pol_, &names, NULL); output_screen1("Pol has " << extraParams << " extra parameters. Their names are as follows:" << std::endl); for(int i = 0; i < extraParams; ++i) { output_screen1(names[i] << std::endl); } output_screen1(std::endl); delete names; */ } if(useLensing) { std::stringstream lensPath; lensPath << planckLikeDir << "/lensing_likelihood_v4_ref.clik_lensing"; char lensPathCStr[100]; std::strcpy(lensPathCStr, lensPath.str().c_str()); if(!cont.lens) cont.lens = clik_lensing_init(lensPathCStr, NULL); lens_ = cont.lens; lensingLMax_ = clik_lensing_get_lmax(lens_,NULL); if(lensingLMax_ > lMax_) lMax_ = lensingLMax_; output_screen1("Lensing has l_max = " << lensingLMax_ << std::endl); /* int extraParams = clik_lensing_get_extra_parameter_names(lens_, &names, NULL); output_screen1("Lensing has " << extraParams << " extra parameters. Their names are as follows:" << std::endl); for(int i = 0; i < extraParams; ++i) { output_screen1(names[i] << std::endl); } output_screen1(std::endl); delete names; */ } if(useActSpt) { std::stringstream actSptPath; actSptPath << planckLikeDir << "/actspt_2013_01.clik"; char actSptPathCStr[100]; std::strcpy(actSptPathCStr, actSptPath.str().c_str()); if(!cont.actspt) cont.actspt = clik_init(actSptPathCStr, NULL); actspt_ = cont.actspt; clik_get_has_cl(actspt_, hasCl, NULL); clik_get_lmax(actspt_, lMax, NULL); for(int i = 0; i < 6; ++i) { output_screen1("ActSpt has " << spectraNames_[i] << " = " << hasCl[i] << " with l_max = " << lMax[i] << std::endl); if(lMax[i] > actSptLMax_) actSptLMax_ = lMax[i]; } if(actSptLMax_ > lMax_) lMax_ = actSptLMax_; /* int extraParams = clik_get_extra_parameter_names(actspt_, &names, NULL); output_screen1("ActSpt has " << extraParams << " extra parameters. Their names are as follows:" << std::endl); for(int i = 0; i < extraParams; ++i) { output_screen1(names[i] << std::endl); } output_screen1(std::endl); delete names; */ } output_screen1("Total l_max = " << lMax_ << std::endl); if(useOwnCmb) { cmb_ = new CMB; cmb_->preInitialize(lMax_ + 1000, false, true, includeTensors, lMax_ + 1000, kPerDecade); } }
PlanckLikelihood::PlanckLikelihood(bool lowT, bool lowP, bool highT, bool highP, bool highLikeLite, bool lensingT, bool lensingP, bool includeTensors, double kPerDecade, bool useOwnCmb) : spectraNames_(6), lensSpectraNames_(7), low_(NULL), high_(NULL), lens_(NULL), lowT_(lowT), lowP_(lowP), highT_(highT), highP_(highP), highLikeLite_(highLikeLite), lensingT_(lensingT), lensingP_(lensingP), cmb_(NULL), modelParams_(NULL), aPlanck_(1), aPol_(1), szPrior_(false) { check(!lowP || lowT, "cannot include lowP without lowT"); check(!highP || highT, "cannot include highP without highT"); check(!lensingP || lensingT, "cannot include lensingP wihtout lensingT"); check(lowT || highT || lensingT, "at least one likelihood must be specified"); std::string planckLikeDir = PLANCK_DATA_DIR_STR; output_screen("Planck data dir = " << planckLikeDir << std::endl); spectraNames_[0] = std::string("TT"); spectraNames_[1] = std::string("EE"); spectraNames_[2] = std::string("BB"); spectraNames_[3] = std::string("TE"); spectraNames_[4] = std::string("TB"); spectraNames_[5] = std::string("EB"); lensSpectraNames_[0] = std::string("PP"); lensSpectraNames_[1] = std::string("TT"); lensSpectraNames_[2] = std::string("EE"); lensSpectraNames_[3] = std::string("BB"); lensSpectraNames_[4] = std::string("TE"); lensSpectraNames_[5] = std::string("TB"); lensSpectraNames_[6] = std::string("EB"); int lMax[6]; int lensLMax[7]; lMax_ = 0; lowLMax_ = 0; highLMax_ = 0; lensLMax_ = 0; parname* names; static PlanckLikelihoodContainer planckLikelihoodContainer; if(lowT) { if(lowP) low_ = planckLikelihoodContainer.getLowTP(); else low_ = planckLikelihoodContainer.getLowT(); clik_get_lmax(low_, lMax, NULL); for(int i = 0; i < 6; ++i) { output_screen1("Low likelihood has " << spectraNames_[i] << " with l_max = " << lMax[i] << std::endl); if(lMax[i] > lowLMax_) lowLMax_ = lMax[i]; } if(lowLMax_ > lMax_) lMax_ = lowLMax_; /* int extraParams = clik_get_extra_parameter_names(low_, &names, NULL); output_screen1("Low likelihood has " << extraParams << " extra parameters. Their names are as follows:" << std::endl); for(int i = 0; i < extraParams; ++i) { output_screen1(names[i] << std::endl); } output_screen1(std::endl); delete names; */ } if(highT) { if(highLikeLite) { if(highP) high_ = planckLikelihoodContainer.getHighTPLite(); else high_ = planckLikelihoodContainer.getHighTLite(); } else { if(highP) high_ = planckLikelihoodContainer.getHighTP(); else high_ = planckLikelihoodContainer.getHighT(); } clik_get_lmax(high_, lMax, NULL); for(int i = 0; i < 6; ++i) { output_screen1("High likelihood has " << spectraNames_[i] << " with l_max = " << lMax[i] << std::endl); if(lMax[i] > highLMax_) highLMax_ = lMax[i]; } if(highLMax_ > lMax_) lMax_ = highLMax_; /* int extraParams = clik_get_extra_parameter_names(high_, &names, NULL); output_screen1("High likelihood has " << extraParams << " extra parameters. Their names are as follows:" << std::endl); for(int i = 0; i < extraParams; ++i) { output_screen1(names[i] << std::endl); } output_screen1(std::endl); delete names; */ } if(lensingT) { if(lensingP) lens_ = planckLikelihoodContainer.getLensTP(); else lens_ = planckLikelihoodContainer.getLensT(); clik_lensing_get_lmaxs(static_cast<clik_lensing_object*>(lens_), lensLMax, NULL); for(int i = 0; i < 7; ++i) { output_screen1("Lensing likelihood has " << lensSpectraNames_[i] << " with l_max = " << lensLMax[i] << std::endl); if(lensLMax[i] > lensLMax_) lensLMax_ = lensLMax[i]; } if(lensLMax_ > lMax_) lMax_ = lensLMax_; /* int extraParams = clik_lensing_get_extra_parameter_names(static_cast<clik_lensing_object*>(lens_), &names, NULL); output_screen1("Lensing likelihood has " << extraParams << " extra parameters. Their names are as follows:" << std::endl); for(int i = 0; i < extraParams; ++i) { output_screen1(names[i] << std::endl); } output_screen1(std::endl); delete names; */ } output_screen1("Low l_max = " << lowLMax_ << std::endl); output_screen1("High l_max = " << highLMax_ << std::endl); output_screen1("Lensing l_max = " << lensLMax_ << std::endl); output_screen1("Total l_max = " << lMax_ << std::endl); if(useOwnCmb) { cmb_ = new CMB; cmb_->preInitialize(lMax_ + 1000, false, true, includeTensors, lMax_ + 1000, kPerDecade); } input_.resize(3 * lMax_ + 10000); if(highT) { highExtra_.resize(highP ? 32 : 15, 0); if(highP) beamExtra_.resize(60, 0); } }
int main(int argc, char *argv[]) { try { StandardException exc; if(argc < 3) { std::string exceptionString = "Need to specify the spline type (linear or cubic) and the number of (internal) knots."; exc.set(exceptionString); throw exc; } bool isLinear = true; if(argv[1][0] == 'c') isLinear = false; const int nKnots = std::atoi(argv[2]); if(nKnots < 0 || nKnots > 100) { std::stringstream exceptionStr; exceptionStr << "The number of knots " << nKnots << " is invalid. Needs to be positive and not larger than 100."; exc.set(exceptionStr.str()); throw exc; } bool varyNEff = false; bool varySumMNu = false; for(int i = 3; i < argc; ++i) { if(std::string(argv[i]) == "neff") varyNEff = true; if(std::string(argv[i]) == "sum_mnu") varySumMNu = true; } int nPar = 4 + 2 * (nKnots + 2) - 2; if(varyNEff) { ++nPar; output_screen("Varying N_eff!" << std::endl); } else { output_screen("N_eff not being varied. To vary it give \"neff\" as an extra argument." << std::endl); } if(varySumMNu) { ++nPar; output_screen("Varying sum_mnu!" << std::endl); } else { output_screen("sum_mnu not being varied. To vary it give \"sum_mnu\" as an extra argument." << std::endl); } // for A_planck ++nPar; const double kMin = 0.8e-6; const double kMax = 1.2; const double aMin = -2; const double aMax = 4; std::vector<double> kVals(nKnots + 2); std::vector<double> amplitudes(nKnots + 2, 2e-9); kVals[0] = kMin; kVals.back() = kMax; const double deltaLogK = (std::log(kMax) - std::log(kMin)) / (nKnots + 1); for(int i = 1; i < kVals.size() - 1; ++i) kVals[i] = std::exp(std::log(kMin) + i * deltaLogK); SplineWithDegenerateNeutrinosParams params(isLinear, 0.022, 0.12, 0.7, 0.1, kVals, amplitudes, 3.046, 0, 0, varyNEff, varySumMNu); #ifdef COSMO_PLANCK_15 PlanckLikelihood planckLike(true, true, true, false, true, false, false, false, 100); #else ERROR NOT IMPLEMENTED; #endif planckLike.setModelCosmoParams(¶ms); std::stringstream root; root << "knotted_"; if(isLinear) root << "linear_"; else root << "cubic_"; root << nKnots; if(varyNEff) root << "_neff"; if(varySumMNu) root << "_summnu"; PolyChord pc(nPar, planckLike, 300, root.str(), 3 * (4 + (varyNEff ? 1 : 0) + (varySumMNu ? 1 : 0))); int paramIndex = 0; pc.setParam(paramIndex++, "ombh2", 0.02, 0.025, 1); pc.setParam(paramIndex++, "omch2", 0.1, 0.2, 1); pc.setParam(paramIndex++, "h", 0.55, 0.85, 1); pc.setParam(paramIndex++, "tau", 0.02, 0.20, 1); if(varyNEff) pc.setParam(paramIndex++, "n_eff", 2.0, 5.0, 1); if(varySumMNu) pc.setParam(paramIndex++, "sum_mnu", 0.001, 3.0, 1); for(int i = 1; i < kVals.size() - 1; ++i) { std::stringstream paramName; paramName << "k_" << i; pc.setParamSortedUniform(paramIndex++, paramName.str(), std::log(kMin), std::log(kMax), 0, 2); } for(int i = 0; i < amplitudes.size(); ++i) { std::stringstream paramName; paramName << "a_" << i; pc.setParam(paramIndex++, paramName.str(), aMin, aMax, 2); } #ifdef COSMO_PLANCK_15 pc.setParamGauss(paramIndex++, "A_planck", 1, 0.0025, 3); #else ERROR NOT IMPLEMENTED; #endif check(paramIndex == nPar, ""); const std::vector<double> fracs{0.7, 0.25, 0.05}; pc.setParameterHierarchy(fracs); pc.run(true); } catch (std::exception& e) { output_screen("EXCEPTION CAUGHT!!! " << std::endl << e.what() << std::endl); output_screen("Terminating!" << std::endl); return 1; } return 0; }
int main(int argc, char *argv[]) { try { StandardException exc; // Choose values of the cosmological parameters const double h = 0.6704; const double omBH2 = 0.022032; const double omCH2 = 0.12038; const double tau = 0.0925; const double ns = 0.9619; const double as = 2.2154e-9; const double pivot = 0.05; const double r = 1e-10; const double nt = 0; const double nEff = 3.046; const int nMassive = 1; const double sumMNu = 0.5; // Create an instance of CosmologicalParams. Can use other examples below (just uncomment the appropriate line and comment this line) LCDMWithTensorParams params(omBH2, omCH2, h, tau, ns, as, pivot, r, nt, pivot); //LCDMWithDegenerateNeutrinosParams params(omBH2, omCH2, h, tau, ns, as, pivot, nEff, nMassive, sumMNu); //LambdaCDMParams params(omBH2, omCH2, h, tau, ns, as, pivot); // Choose lMax and create vectors where the resulting cl values will be written int lMax = 3000; std::vector<double> clTT, clEE, clTE, clPP, clTP, clEP, clBB, clLensedTT, clLensedEE, clLensedTE, clLensedBB; // Create a CMB instance. CMB cmb; output_screen("Pre-initializing CLASS..." << std::endl); // pre-initialize cmb cmb.preInitialize(lMax, false, false, true, lMax); output_screen("OK" << std::endl); output_screen("Initializing CLASS..." << std::endl); // initialize cmb cmb.initialize(params, true, true, true); output_screen("OK" << std::endl); // get different cl values cmb.getCl(&clTT, &clEE, &clTE, &clPP, &clTP, &clEP, &clBB); // get different lensed cl values cmb.getLensedCl(&clLensedTT, &clLensedEE, &clLensedTE, &clLensedBB); // write results into output files std::ofstream out("example_files/cl.txt"); std::ofstream outLensed("example_files/cl_lensed.txt"); for(int l = 0; l <= lMax; ++l) { out << l << ' ' << clTT[l] << ' ' << clEE[l] << ' ' << clTE[l] << ' ' << clPP[l] << ' ' << clTP[l] << ' ' << clEP[l] << ' ' << clBB[l] << std::endl; } for(int l = 0; l < clLensedTT.size(); ++l) outLensed << l << ' ' << clLensedTT[l] << ' ' << clLensedEE[l] << ' ' << clLensedTE[l] << ' ' << clLensedBB[l] << std::endl; outLensed.close(); out.close(); } catch (std::exception& e) { output_screen("EXCEPTION CAUGHT!!! " << std::endl << e.what() << std::endl); output_screen("Terminating!" << std::endl); return 1; } return 0; }
int main(int argc, char *argv[]) { try { StandardException exc; if(argc < 5) { std::string exceptionStr = "Input chain file, the indices of the two parameters for the contour plot (starting from 1), the output file, and the posterior resolution (i.e. number of points, optional, default = 100) must be specified."; exc.set(exceptionStr); throw exc; } std::stringstream paramsStr; paramsStr << argv[2] << ' ' << argv[3]; int p1, p2; paramsStr >> p1 >> p2; if(p1 <= 0) { std::stringstream exceptionStr; exceptionStr << "Invalid parameter index " << argv[2] << ". Needs to be a positive integer."; exc.set(exceptionStr.str()); throw exc; } if(p2 <= 0) { std::stringstream exceptionStr; exceptionStr << "Invalid parameter index " << argv[3] << ". Needs to be a positive integer."; exc.set(exceptionStr.str()); throw exc; } int res = 100; if(argc > 5) { std::stringstream resStr; resStr << argv[5]; resStr >> res; if(res <= 0) { std::stringstream exceptionStr; exceptionStr << "Invalid resolution " << argv[5] << ". Needs to be a positive integer."; exc.set(exceptionStr.str()); throw exc; } } std::vector<ChainElement> chain; std::ifstream in(argv[1]); if(!in) { std::stringstream exceptionStr; exceptionStr << "Input chain file " << argv[1] << " cannot be read."; exc.set(exceptionStr.str()); throw exc; } const int n = (p1 > p2 ? p1 : p2); output_screen("Reading the chain..." << std::endl); double maxP = 0; while(!in.eof()) { std::string s; std::getline(in, s); if(s == "") break; ChainElement e; chain.push_back(e); std::stringstream str(s); str >> e.p >> e.like; e.params.resize(n); for(int i = 0; i < n; ++i) str >> e.params[i]; if(e.p > maxP) maxP = e.p; chain.push_back(e); } in.close(); output_screen("OK" << std::endl); const double minP = maxP * 1e-6; output_screen("Creating the posterior distributions..." << std::endl); double min1 = 1e100, max1 = -1e100, min2 = 1e100, max2 = -1e100; for(unsigned long i = 0; i < chain.size(); ++i) { const ChainElement& e = chain[i]; if(e.p < minP) continue; if(e.params[p1] < min1) min1 = e.params[p1]; if(e.params[p1] > max1) max1 = e.params[p1]; if(e.params[p2] < min2) min2 = e.params[p2]; if(e.params[p2] > max2) max2 = e.params[p2]; } const double d1 = (max1 - min1) / res; const double d2 = (max2 - min2) / res; std::vector<double> x(res), y(res); for(int i = 0; i < res; ++i) { x[i] = min1 + d1 * i + d1 / 2; y[i] = min2 + d2 * i + d2 / 2; } std::vector<std::vector<double> > z(res); for(int i = 0; i < res; ++i) z[i].resize(res, 0.0); double totalP = 0; for(unsigned long i = 0; i < chain.size(); ++i) { const ChainElement& e = chain[i]; if(e.p < minP) continue; const double param1 = e.params[p1]; check(param1 >= min1, ""); int j1 = (int)std::floor((param1 - min1) / d1); check(j1 >= 0, ""); if(j1 >= res) j1 = res - 1; const double param2 = e.params[p2]; check(param2 >= min2, ""); int j2 = (int)std::floor((param2 - min2) / d2); check(j2 >= 0, ""); if(j2 >= res) j2 = res - 1; z[j1][j2] += e.p; totalP += e.p; } output_screen("OK" << std::endl); output_screen("Writing the output..." << std::endl); std::ofstream out(argv[4]); if(!out) { std::stringstream exceptionStr; exceptionStr << "Cannot write into the output file " << argv[4] << "."; exc.set(exceptionStr.str()); throw exc; } double prob = 0; unsigned long i = 0; while(prob < 1 - 1e-5 && i < chain.size()) { if(chain[i].p > minP) { out << chain[i].params[p1 - 1] << ' ' << chain[i].params[p2 - 1] << ' ' << prob << std::endl; } prob += chain[i].p; ++i; } out.close(); output_screen("OK" << std::endl); } catch (std::exception& e)
int main(int argc, char *argv[]) { //bool isLinear = true; //if(argv[1][0] == 'c') // isLinear = false; //const int nKnots = std::atoi(argv[2]); //bool varyNEff = false; //bool varySumMNu = false; //for(int i = 3; i < argc; ++i) //{ // if(std::string(argv[i]) == "neff") // varyNEff = true; // if(std::string(argv[i]) == "sum_mnu") // varySumMNu = true; //} //int nPar = 4 + 2 * (nKnots + 2) - 2; // //if(isLinear) // output_screen("Using a linear spline primordial power spectrum!" << std::endl); //if(varyNEff) //{ // ++nPar; // output_screen("Varying N_eff!" << std::endl); //} // //if(varySumMNu) //{ // ++nPar; // output_screen("Varying sum_mnu!" << std::endl); //} // Choose values of the cosmological parameters const double h = 0.702; const double omBH2 = 0.02262; const double omCH2 = 0.1138; const double tau = 0.088; const double ns = 0.9655; const double as = 2.1955e-9; const double pivot = 0.05; const int nMassive = 0; const double nEff = 3.046; const double sumMNu = 0.0; //const double kMin = 0.8e-6; //const double kMax = 1.2; //const double aMin = 2.9; //const double aMax = 3.6; //std::vector<double> kVals(nKnots + 2); //std::vector<double> amplitudes(nKnots + 2); //kVals[0] = kMin; //kVals.back() = kMax; //const double deltaLogK = (std::log(kMax) - std::log(kMin)) / (nKnots + 1); //for(int i = 1; i < kVals.size() - 1; ++i) // kVals[i] = std::exp(std::log(kMin) + i * deltaLogK); // //for(int i = 0; i < amplitudes.size(); ++i) // amplitudes[i] = as * pow(kVals[i]/pivot, ns - 1.0); //const double h = ; //const double omBH2 = ; //const double omCH2 = ; //const double tau = ; //std::vector<double> kVals {0.8e-6, std::exp(), std:exp(), 1.2}; //std::vector<double> amplitudes {std::exp()/1e10, std::exp()/1e10, std::exp()/1e10, std::exp()/1e10}; //const int nMassive = 1; //const double nEff = ; //const double sumMNu = ; // Does not work: //const double h = 0.694561; //const double omBH2 = 0.0246605; //const double omCH2 = 0.115974; //const double tau = 0.0650107; //std::vector<double> kVals {0.8e-6, std::exp(-12.0654), std:exp(-5.22511), 1.2}; //std::vector<double> amplitudes {std::exp(3.30985)/1e10, std::exp(3.0059)/1e10, std::exp(3.19711)/1e10, std::exp(2.91141)/1e10}; //const int nMassive = 1; //const double nEff = 3.99409; //const double sumMNu = 0.796424; //const double h = 0.695958; //const double omBH2 = 0.0207389; //const double omCH2 = 0.121261; //const double tau = 0.0866845; //std::vector<double> kVals {0.8e-6, std::exp(-5.38061), std:exp(-1.61162), 1.2}; //std::vector<double> amplitudes {std::exp(3.99405)/1e10, std::exp(3.1774)/1e10, std::exp(3.13913)/1e10, std::exp(3.64804)/1e10}; //const int nMassive = 1; //const double nEff = 2.40787; //const double sumMNu = 0.329839; output_screen("Creating instance of cosmological parameters..." << std::endl); // Create cosmological params LambdaCDMParams params(omBH2, omCH2, h, tau, ns, as, pivot); //SplineWithDegenerateNeutrinosParams params(isLinear, omBH2, omCH2, h, tau, kVals, amplitudes, nEff, nMassive, sumMNu, varyNEff, varySumMNu); output_screen("Initializing LRG DR7 likelihood..." << std::endl); //Cosmo cosmo; //cosmo.preInitialize(3500, false, true, false, 0, 100, 1e-6, 1); std::string datapath = "/Volumes/Data1/ncanac/cosmopp_neutrinos"; // Create likelihood CombinedLikelihood lrgLike(datapath, false, false, false, true, false); //LRGDR7Likelihood lrgLike(datapath, cosmo); output_screen("Setting the cosmological parameters for likelihood calculation..." << std::endl); // Set the cosmological parameters lrgLike.setCosmoParams(params); output_screen("Calculating the likelihood..." << std::endl); // Calculate likelihood double lnlike = lrgLike.likelihood(); // Output the likelihood output_screen("Likelihood = " << lnlike/2.0 << std::endl); return 0; }