40 using namespace catalogue;
41 using namespace lognormal;
49 if (i<m_catalogue.size())
50 return m_catalogue[i];
53 ErrorCBL(
"The log-normal catalogue "+
conv(i+1,
par::fINT)+
" does not exist!",
"LogNormal_catalogue",
"LogNormal.cpp");
64 if (n_lognormal_mocks==0)
65 ErrorCBL(
"set number of log-normal realizations first!",
"generate",
"LogNormal.cpp");
72 const double minX_random = m_random.Min(Var::_X_);
73 const double minY_random = m_random.Min(Var::_Y_);
74 const double minZ_random = m_random.Min(Var::_Z_);
76 const double DeltaX = (m_random.Max(Var::_X_)-minX_random);
77 const double DeltaY = (m_random.Max(Var::_Y_)-minY_random);
78 const double DeltaZ = (m_random.Max(Var::_Z_)-minZ_random);
80 const int nTot = m_random.nObjects();
82 int nx = DeltaX/m_cell_size;
83 nx = (nx%2==0) ? nx : nx+1;
84 const double cell_size_x = DeltaX/nx;
86 int ny = DeltaY/m_cell_size;
87 ny = (ny%2==0) ? ny : ny+1;
88 const double cell_size_y = DeltaY/ny;
90 int nz = DeltaZ/m_cell_size;
91 nz = (nz%2==0) ? nz : nz+1;
92 const double cell_size_z = DeltaZ/nz;
94 const int nzp = nz*0.5+1;
96 const int nRtot = nx*ny*nz;
97 const int nKtot = nx*ny*nzp;
101 grid = fftw_alloc_real(nRtot);
102 xxii = fftw_alloc_real(nRtot);
103 ppkk = fftw_alloc_complex(nKtot);
105 for (
int i=0; i<nRtot; i++) {
110 for (
int i=0; i<nKtot; i++) {
115 for (
int i=0; i<nTot; i++) {
116 int i1 = min(
int((m_random.xx(i)-minX_random)/cell_size_x), nx-1);
117 int j1 = min(
int((m_random.yy(i)-minY_random)/cell_size_y), ny-1);
118 int z1 = min(
int((m_random.zz(i)-minZ_random)/cell_size_z), nz-1);
119 long int index = z1+nz*(j1+ny*i1);
120 grid[index] += 1./nTot;
123 const double fact = (m_real) ? pow(m_bias, 2) : pow(m_bias, 2)*
xi_ratio(m_cosmology.linear_growth_rate(m_redshift, 0.)/m_bias);
126 vector<double> PkG = m_cosmology.Pk_matter(kG, m_method_Pk, m_NL, m_redshift);
128 for (
size_t i=0; i<kG.size(); i++)
129 PkG[i] = fact*PkG[i];
134 const double xfact = 2.*
par::pi/(nx*cell_size_x);
135 const double yfact = 2.*
par::pi/(ny*cell_size_y);
136 const double zfact = 2.*
par::pi/(nz*cell_size_z);
138 const double Volume = nRtot*cell_size_x*cell_size_y*cell_size_z;
141 for (
int i=0; i<nx; i++) {
142 const double kx = (i<=nx/2) ? xfact*i : -(nx-i)*xfact;
143 for (
int j=0; j<ny; j++) {
144 const double ky = (j<=ny/2) ? yfact*j : -(ny-j)*yfact;
145 for (
int k=0; k<nzp; k++) {
146 const double kz = zfact*k;
147 const double kk = pow(kx*kx+ky*ky+kz*kz, 0.5);
148 const long int kindex = k+nzp*(j+ny*i);
149 ppkk[kindex][0] = interpPk(kk)/Volume;
154 coutCBL <<
"I'm computing Fourier transforms..."<<endl;
158 fftw_plan plan_xi_from_pk = fftw_plan_dft_c2r_3d(nx, ny, nz, ppkk, xxii, FFTW_ESTIMATE);
159 fftw_execute(plan_xi_from_pk);
160 fftw_destroy_plan(plan_xi_from_pk);
162 for (
int i=0; i<nRtot; i++)
163 xxii[i] = log(1+xxii[i]);
166 fftw_plan plan_pk_from_xi = fftw_plan_dft_r2c_3d(nx, ny, nz, xxii, ppkk, FFTW_ESTIMATE);
167 fftw_execute(plan_pk_from_xi);
168 fftw_destroy_plan(plan_pk_from_xi);
171 for (
int nn=0; nn<n_lognormal_mocks; nn++) {
173 coutCBL <<
"I'm constructing the log-normal mock: " << nn+1 <<
"..." << endl;
177 densK = fftw_alloc_complex(nKtot);
178 densX = fftw_alloc_real(nRtot);
180 for (
int i=0; i<nRtot; i++)
183 for (
int i=0; i<nKtot; i++) {
193 for (
int i=0; i<nx; i++)
194 for (
int j=0; j<ny; j++)
195 for (
int k=0; k<nzp; k++) {
197 const int kindex = k+nzp*(j+ny*i);
198 const double dk = sqrt(max(0., ppkk[kindex][0]/nRtot)*0.5);
200 const double v1 = dk*rang();
201 const double v2 = dk*rang();
203 if (i==0 && j==0 && k==0) {
204 densK[kindex][0] = 0;
205 densK[kindex][1] = 0;
207 else if (i==nx/2 || j==ny/2){
208 densK[kindex][0] = v1;
209 densK[kindex][1] = 0.;
212 densK[kindex][0] = v1;
213 densK[kindex][1] = v2;
219 fftw_plan plan_dr_from_dk = fftw_plan_dft_c2r_3d(nx, ny, nz, densK, densX, FFTW_ESTIMATE);
220 fftw_execute(plan_dr_from_dk);
221 fftw_destroy_plan(plan_dr_from_dk);
225 for (
int i=0; i<nx; i++)
226 for (
int j=0; j<ny; j++)
227 for (
int k=0; k<nz; k++)
228 ff.push_back(densX[k+nz*(j+ny*i)]);
230 const double sigma =
Sigma(ff);
237 default_random_engine generator;
238 vector<shared_ptr<Object>> mock_sample;
240 for (
int i=0; i<nx; i++) {
241 for (
int j=0; j<ny; j++) {
242 for (
int k=0; k<nz; k++) {
243 long int index = k+nz*(j+ny*i);
246 const double nObjects = m_nObjects*grid[index]*(exp(densX[index]-sigma*sigma*0.5));
252 for (
int nnoo=0; nnoo<no; nnoo++) {
253 comovingCoordinates coord;
254 coord.xx = cell_size_x*(i+ran())+minX_random;
255 coord.yy = cell_size_y*(j+ran())+minY_random;
256 coord.zz = cell_size_z*(k+ran())+minZ_random;
257 shared_ptr<Galaxy> SMP(
new Galaxy(coord));
258 mock_sample.push_back(SMP);
265 shared_ptr<Catalogue> p_cat(
new Catalogue{mock_sample});
267 p_cat->write_comoving_coordinates(output_dir+filename+
"_"+
conv(nn+start,
par::fINT)+
".dat");
269 m_catalogue.emplace_back(p_cat);
Wrapper for fftlog wripper.
#define coutCBL
CBL print message.
Implementation of the log-normal data structure.
void generate(const int n_lognormal_mocks, const std::string output_dir, const std::string filename="lognormal", const int start=1, const int seed=3213)
generate the log-normal mock catalogues
std::shared_ptr< catalogue::Catalogue > catalogue(const size_t i)
get the private member LogNormal::m_LNCat[i]
The class NormalRandomNumbers.
static const char fINT[]
conversion symbol for: int -> std::string
static const double pi
: the ratio of a circle's circumference to its diameter
The global namespace of the CosmoBolognaLib
void distribution(std::vector< double > &xx, std::vector< double > &fx, std::vector< double > &err, const std::vector< double > FF, const std::vector< double > WW, const int nbin, const bool linear=true, const std::string file_out=par::defaultString, const double fact=1., const double V1=par::defaultDouble, const double V2=par::defaultDouble, const std::string bin_type="Linear", const bool conv=false, const double sigma=0.)
derive and store the number distribution of a given std::vector
std::string conv(const T val, const char *fact)
convert a number to a std::string
std::vector< T > logarithmic_bin_vector(const size_t nn, const T min, const T max)
fill a std::vector with logarithmically spaced values
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
double xi_ratio(const double beta)
the ratio between the redshift-space and real-space correlation functions
double Sigma(const std::vector< double > vect)
the standard deviation of a std::vector