FML
Wrapper.cpp
Go to the documentation of this file.
1#include <FML/Spline/Spline.h>
3
9
10// Allocate memory
12 HaloModel **hm = new HaloModel*();
13 *hm = new HaloModel();
14 return hm;
15}
16
17void get_deltac(HaloModel **hmp, int nz, double *z, int ndeltac, double *deltac){
18 HaloModel *hm = *hmp;
19 for(int i = 0; i < nz; i++){
20 double x = std::log(1.0/(1+z[i]));
21 deltac[i] = hm->hmcode ? hm->get_deltac_hmcode(x) : hm->deltac_of_x_spline(x);
22 }
23}
24
25void get_DeltaVir(HaloModel **hmp, int nz, double *z, int nDeltaVir, double *DeltaVir){
26 HaloModel *hm = *hmp;
27 for(int i = 0; i < nz; i++){
28 double x = std::log(1.0/(1+z[i]));
29 DeltaVir[i] = hm->hmcode ? hm->get_DeltaVir_hmcode(x) : hm->DeltaVir_of_x_spline(x);
30 }
31}
32
33// Free up memory
34void free(HaloModel **hm){
35 delete *hm;
36}
37
38// Fetch computed P(k,z) and return it
39void get_pofk(HaloModel **hmp, int nk, double * k, int npofk_lin, double * pofk_lin, int npofk, double *pofk){
40 HaloModel *hm = *hmp;
41 if(nk != npofk_lin or nk != npofk)
42 throw std::runtime_error("Arrays k and pofk must be compatible\n");
43 for(int i = 0; i < nk; i++){
44 const double factor = (2.0*M_PI*M_PI)/std::pow(k[i],3);
45 const double scaling = std::pow(hm->growthfactor_of_x_spline(hm->xcollapse) / hm->growthfactor_of_x_spline(hm->xinput_pofk), 2);
46 pofk[i] = std::exp(hm->logDeltaHM_full_of_logk_spline(std::log(k[i])))*factor;
47 pofk_lin[i] = scaling * std::exp(hm->logDelta_of_logk_spline(std::log(k[i])))*factor;
48 }
49}
50
51void get_pofk_1h_2h(HaloModel **hmp, int nk, double * k, int npofk_1h, double * pofk_1h, int npofk_2h, double *pofk_2h){
52 HaloModel *hm = *hmp;
53 if(nk != npofk_1h or nk != npofk_2h)
54 throw std::runtime_error("Arrays k and pofk must be compatible\n");
55 for(int i = 0; i < nk; i++){
56 const double factor = (2.0*M_PI*M_PI)/std::pow(k[i],3);
57 pofk_1h[i] = std::exp(hm->logDeltaHM_onehalo_of_logk_spline(std::log(k[i])))*factor;
58 pofk_2h[i] = std::exp(hm->logDeltaHM_twohalo_of_logk_spline(std::log(k[i])))*factor;
59 }
60}
61
62// Fetch massfunction dlogndlogM and n
63void get_nofM(HaloModel **hmp, int nm, double * M, int ndnofm, double *dnofm, int nnofm, double * nofm){
64 HaloModel *hm = *hmp;
65 if(nm != nnofm or nm != ndnofm)
66 throw std::runtime_error("Arrays M and nofM must be compatible\n");
67 for(int i = 0; i < nm; i++){
68 dnofm[i] = hm->dndlogM_of_logM_spline(std::log(M[i]));
69 nofm[i] = hm->n_of_logM_spline(std::log(M[i]));
70 }
71}
72
73// Compute P(k,z)
74void calc(HaloModel **hmp, double z){
75 HaloModel *hm = *hmp;
76 const double zcollapse = std::exp(-hm->xcollapse)-1.0;
77 if(std::fabs(z-zcollapse) < 1e-8)
78 return;
80}
81
82// Show some info
83void info(HaloModel **hmp){
84 HaloModel *hm = *hmp;
85 hm->info();
86}
87
88// Output to file
89void output(HaloModel **hmp, std::string label){
90 HaloModel *hm = *hmp;
91 const double z = std::exp(-hm->xcollapse)-1.0;
92 hm->output_pofk("pofk_" + label + "_z" + std::to_string(z) + ".txt");
93 hm->output_nofM("nofM_" + label + "_z" + std::to_string(z) + ".txt");
94 hm->output_deltac("sph_" + label + "_z" + std::to_string(z) + ".txt");
95}
96
97void init(
98 HaloModel **hmp,
99 std::string filename_pofk,
100 double OmegaM,
101 double w0,
102 double wa,
103 double mu0,
104 double mua,
105 bool verbose,
106 bool hmcode,
107 bool sigma8norm,
108 double deltac_multiplier,
109 double DeltaVir_multiplier,
110 double cofM_multiplier){
111
112 HaloModel *hm = *hmp;
113 const double OmegaLambda = 1.0-OmegaM;
114 const double xini_spherical_collapse = std::log(1e-5);
115 const double xpofk = 0.0;
116 const int npts_spherical_collapse = 1000;
117
118 // Cosmology functions E = H(x)/H0, x = log(a)
119 const Func1 Eofx = [OmegaM,OmegaLambda,w0,wa](double x) -> double {
120 const double a = std::exp(x);
121 return std::sqrt(
122 OmegaM * std::exp(-3.0*x) +
123 OmegaLambda * std::exp(3.0 * wa * (a - 1) - 3 * (1 + w0 + wa) * x)
124 );
125 };
126 const Func1 logEprimeofx = [OmegaM,OmegaLambda,w0,wa,Eofx](double x) -> double {
127 const double E = Eofx(x);
128 const double a = std::exp(x);
129 return 1.0 / (2.0 * E * E) * (-3.0 * OmegaM / (a * a * a) +
130 OmegaLambda * std::exp(3.0 * wa * (a - 1) - 3 * (1 + w0 + wa) * x) *
131 (3.0 * wa * a - 3.0 * (1 + w0 + wa)));
132 };
133 const Func1 muofx = [mu0,mua,Eofx](double x) -> double {
134 const double E = Eofx(x);
135 const double a = std::exp(x);
136 return 1.0 + (mu0 + mua*(1.0-a)) / (E*E);
137 };
138 const Func1 muofx_lcdm = []([[maybe_unused]] double x) -> double {
139 return 1.0;
140 };
141 const Func1 wofx = [w0,wa]([[maybe_unused]] double x) -> double {
142 const double a = std::exp(x);
143 return w0 + wa*(1-a);
144 };
145 const Func1 OmegaMofx = [OmegaM,Eofx](double x) -> double {
146 const double E = Eofx(x);
147 return OmegaM*std::exp(-3*x)/(E*E);
148 };
149
150 // Set up the model
152 Eofx,
153 OmegaMofx,
154 logEprimeofx,
155 muofx,
156 wofx);
157
158 SphericalCollapseModel lcdmmodel(
159 Eofx,
160 OmegaMofx,
161 logEprimeofx,
162 muofx_lcdm,
163 wofx);
164
165 // We read in P(k,z=zi) in LCDM and then scale it back
166 // and forward again to get the linear P(k,z=zi) of our mu-sims
167 Spline Dofx_spline, fofx_spline;
168 Spline Dlcdmofx_spline, flcdmofx_spline;
170 FML::COSMOLOGY::SPHERICALCOLLAPSE::compute_growthfactor(lcdmmodel, Dlcdmofx_spline, flcdmofx_spline);
171 const double xlinearpofk = 0.0;
172 const double xscaleback = std::log(1.0/50.0);
173 const double pofk_normalization = (sigma8norm ? 1.0 :
174 std::pow(Dlcdmofx_spline(xscaleback) / Dlcdmofx_spline(xlinearpofk), 2) /
175 std::pow(Dofx_spline(xscaleback) / Dofx_spline(xlinearpofk), 2));
176 Spline logDelta_of_logk_spline;
178 filename_pofk,
179 logDelta_of_logk_spline,
180 pofk_normalization);
181
182 // Set cosmology and Plin
183 hm->spcmodel = model;
184 hm->OmegaM = OmegaM;
185 hm->fnu = 0.0;
186 hm->logDelta_of_logk_spline = logDelta_of_logk_spline;
187 hm->xinput_pofk = xpofk;
188 hm->xini_spherical_collapse = xini_spherical_collapse;
189 hm->npts_spherical_collapse = npts_spherical_collapse;
190 hm->hmcode = hmcode;
191 hm->verbose = verbose;
192
193 // Initialize
194 hm->init();
195 hm->deltac_multiplier = deltac_multiplier;
196 hm->DeltaVir_multiplier = DeltaVir_multiplier;
197 hm->cofM_multiplier = cofM_multiplier;
198}
199
200//================================================
201// Read a file with (a, mu(a)) and spline up mu(x)
202//================================================
204 const std::string filename_mu,
205 Spline & mu_of_x_spline){
206
207 const auto mudata = FML::FILEUTILS::read_regular_ascii(filename_mu, 2, {0,1}, 0);
208
209 DVector xmu, mu;
210 bool negativemu = false;
211 for (auto & line : mudata) {
212 xmu.push_back(std::log(line[0]));
213 mu.push_back(line[1]);
214 if(line[1] < 0.0) negativemu = true;
215 }
216
217 if(negativemu){
218 std::cout << "Error: mu is negative, could be a problem\n";
219 exit(1);
220 }
221
222 mu_of_x_spline = Spline(xmu,mu);
223}
FML::COSMOLOGY::SPHERICALCOLLAPSE::Func1 Func1
Definition: Main.cpp:6
const double OmegaM
Definition: Main.cpp:65
FML::INTERPOLATION::SPLINE::DVector DVector
Definition: Simulation.h:39
FML::COSMOLOGY::SPHERICALCOLLAPSE::SphericalCollapseModel SphericalCollapseModel
Definition: Wrapper.cpp:4
void get_pofk_1h_2h(HaloModel **hmp, int nk, double *k, int npofk_1h, double *pofk_1h, int npofk_2h, double *pofk_2h)
Definition: Wrapper.cpp:51
FML::INTERPOLATION::SPLINE::Spline Spline
Definition: Wrapper.cpp:7
void output(HaloModel **hmp, std::string label)
Definition: Wrapper.cpp:89
void get_nofM(HaloModel **hmp, int nm, double *M, int ndnofm, double *dnofm, int nnofm, double *nofm)
Definition: Wrapper.cpp:63
void init(HaloModel **hmp, std::string filename_pofk, double OmegaM, double w0, double wa, double mu0, double mua, bool verbose, bool hmcode, bool sigma8norm, double deltac_multiplier, double DeltaVir_multiplier, double cofM_multiplier)
Definition: Wrapper.cpp:97
FML::COSMOLOGY::HALOMODEL::HaloModel HaloModel
Definition: Wrapper.cpp:5
HaloModel ** get()
Definition: Wrapper.cpp:11
void get_deltac(HaloModel **hmp, int nz, double *z, int ndeltac, double *deltac)
Definition: Wrapper.cpp:17
void get_DeltaVir(HaloModel **hmp, int nz, double *z, int nDeltaVir, double *DeltaVir)
Definition: Wrapper.cpp:25
void info(HaloModel **hmp)
Definition: Wrapper.cpp:83
void free(HaloModel **hm)
Definition: Wrapper.cpp:34
void get_pofk(HaloModel **hmp, int nk, double *k, int npofk_lin, double *pofk_lin, int npofk, double *pofk)
Definition: Wrapper.cpp:39
void calc(HaloModel **hmp, double z)
Definition: Wrapper.cpp:74
void read_and_spline_mu_file(const std::string filename_mu, Spline &mu_of_x_spline)
Definition: Wrapper.cpp:203
Class for computing the standard halomodel predictions for plus or just doing spherical collapse.
Definition: Halomodel.h:62
void output_nofM(std::string filename, double Mmin=1e8, double Mmax=1e16, int npts_M=100) const
Definition: Halomodel.cpp:1080
void compute_at_redshift(double zcollapse)
Definition: Halomodel.cpp:752
double get_DeltaVir_hmcode(double x) const
Definition: Halomodel.cpp:1219
void output_deltac(std::string filename, double zmin=0.0, double zmax=10.0, int npts_z=100) const
Definition: Halomodel.cpp:1038
double get_deltac_hmcode(double x) const
Definition: Halomodel.cpp:1209
void output_pofk(std::string filename, double kmin=1e-5, double kmax=100, int npts_k=1000) const
Definition: Halomodel.cpp:1110
SphericalCollapseModel spcmodel
Definition: Halomodel.h:65
The class contains all the info about the model needed to run the spherical collapse and halomodel Co...
This is a wrapper class for easy use of GSL splines It is (OpenMP) thread safe as long as splines are...
Definition: Spline.h:80
void read_pofk_file(const std::string filename_pofk, Spline &logDelta_of_logk_spline, double normalization, int col_k, int col_pofk)
Definition: Halomodel.cpp:61
std::function< double(double)> Func1
Definition: Halomodel.h:31
void compute_growthfactor(const SphericalCollapseModel &model, Spline &growthfactor_of_x_spline, Spline &growthrate_of_x_spline, const double xini, const double xend, const int npts)
Computes the growth factor (not normalized) and growth rate for a given model and returns splines wit...
DVector2D read_regular_ascii(std::string filename, int ncols, std::vector< int > cols_to_keep, int nskip, size_t nestimated_lines)
Read a regular ascii files with nskip header lines and containing ncol collums nestimated_lines is th...
Definition: FileUtils.cpp:14
std::vector< double > DVector
Definition: ODESolver.h:29
const double exp
Definition: Global.h:259
pofk_1h
Definition: example.py:65
deltac
Definition: example.py:67
float wa
Definition: example.py:11
bool verbose
Definition: example.py:19
bool sigma8norm
Definition: example.py:17
float mua
Definition: example.py:13
pofk_lin
Definition: example.py:64
float z
Definition: example.py:56
float w0
Definition: example.py:10
bool hmcode
Definition: example.py:18
pofk_2h
Definition: example.py:65
float mu0
Definition: example.py:12
x
Definition: test.py:18