10 #include <boost/filesystem.hpp> 14 namespace fs = boost::filesystem;
16 #define BUFF_SIZE 1024 * 1024 * 16 // 16 MB buffer 20 if (!this->is_open())
throw std::runtime_error(
"Error while opening '" + file_name +
"'");
21 this->rdbuf()->pubsetbuf(
buf,
sizeof(
buf));
27 if (this->is_open()) this->close();
32 if (!this->is_open())
throw std::runtime_error(
"Error while opening '" + file_name +
"'");
37 if (this->is_open()) this->close();
43 const time_t now = time(0);
46 gmtime_r(&now, &tstruct);
47 strftime(buf,
sizeof(buf),
"%y%m%d_%H%M%S", &tstruct);
49 std::string returnval(buf);
66 for(
size_t i = 2; ; ++i)
68 const std::string out_dir_new = out_dir +
"_" + std::to_string(i);
77 if(fs::create_directories(dir))
79 BOOST_LOG_TRIVIAL(debug) <<
"Directory created: "<< out_dir;
86 if (fs::remove_all(dir))
88 BOOST_LOG_TRIVIAL(debug) <<
"Directory removed: "<< out_dir;
97 for(
auto &
p : fs::directory_iterator(dir))
99 if (fs::is_regular_file(
p))
105 BOOST_LOG_TRIVIAL(debug) <<
"Removed " << i <<
" file(s) in directory: "<< out_dir;
111 out_dir +=
"par_cut/";
112 std::string file_name = out_dir +
"par_cut" + suffix +
".dat";
115 BOOST_LOG_TRIVIAL(debug) <<
"Writing small cut through the box of particles into file " << file_name;
116 File <<
"# This file contains positions of particles in units [Mpc/h].\n";
118 const FTYPE_t x_0 = sim.
x_0();
120 for(
size_t i=0; i < N; i++)
122 x = particles[i].position[0];
123 y = particles[i].position[1];
124 z = particles[i].position[2];
129 File << x*x_0 <<
"\t" << z*x_0 <<
"\t" << y*x_0 <<
"\n";
136 out_dir +=
"pwr_spec/";
137 std::string file_name = out_dir +
"pwr_spec" + suffix +
".dat";
140 BOOST_LOG_TRIVIAL(debug) <<
"Writing power spectrum into file " << file_name;
141 File <<
"# This file contains power spectrum P(k) in units [(Mpc/h)^3] depending on wavenumber k in units [h/Mpc].\n" 142 "# k [h/Mpc]\tP(k) [(Mpc/h)^3]\n";
144 File << std::scientific;
145 const size_t size = pwr_spec_binned.
size();
146 for (
size_t j = 0; j < size; j++){
147 for (
size_t i = 0; i < 2; i++){
148 File << pwr_spec_binned[i][j] <<
"\t";
156 out_dir +=
"vel_pwr_spec/";
157 std::string file_name = out_dir +
"vel_pwr_spec" + suffix +
".dat";
160 BOOST_LOG_TRIVIAL(debug) <<
"Writing velocity divergence power spectrum into file " << file_name;
161 File <<
"# This file contains velocity divergence power spectrum P(k) in units [(Mpc/h)^3] depending on wavenumber k in units [h/Mpc].\n" 162 "# k [h/Mpc]\tP(k) [(Mpc/h)^3]\n";
164 File << std::scientific;
165 const size_t size = pwr_spec_binned.
size();
166 for (
size_t j = 0; j < size; j++){
167 for (
size_t i = 0; i < 2; i++){
168 File << pwr_spec_binned[i][j] <<
"\t";
176 out_dir +=
"corr_func/";
177 std::string file_name = out_dir +
"corr_func" + suffix +
".dat";
180 BOOST_LOG_TRIVIAL(debug) <<
"Writing correlation function into file " << file_name;
181 File <<
"# This file contains correlation function depending on distance r in units [Mpc/h].\n" 182 "# x [Mpc/h]\txsi(r)\n";
184 const size_t N = pwr_spec_binned.
size();
185 for (
size_t j = 0; j < N; j++){
186 if (pwr_spec_binned[1][j]) File << pwr_spec_binned[0][j] <<
"\t" << pwr_spec_binned[1][j] <<
"\n";
193 return a ? fabs((a-b)/a) : fabs(a-b);
197 bool is_err(
const std::vector<T>& vec1,
const std::vector<T>& vec2,
size_t bin)
199 const T err =
rel_error( vec1[bin], vec2[bin]);
204 BOOST_LOG_TRIVIAL(error) <<
"ERROR! Different values of k in bin " << bin <<
"! Relative error = " << err;
207 else if (err > prec_war) BOOST_LOG_TRIVIAL(warning) <<
"WARNING! Different values of k in bin " << bin <<
"! Relative error = " << err;
212 FTYPE_t growth, std::string out_dir, std::string suffix)
214 out_dir +=
"pwr_diff/";
215 std::string file_name = out_dir +
"pwr_spec_diff" + suffix +
".dat";
218 BOOST_LOG_TRIVIAL(debug) <<
"Writing power spectrum difference into file " << file_name;
219 File <<
"# This file contains relative difference between power spectrum P(k)\n" 220 "# and lineary extrapolated power spectrum of initial position of particles\n" 221 "# depending on wavenumber k in units [h/Mpc].\n" 222 "# k [h/Mpc]\t(P(k, z)-P_lin(k, z))/P_lin(k, z)\n";
225 File << std::setprecision(10);
226 const size_t size = pwr_spec_binned.
size();
227 for (
size_t j = 0; j < size; j++){
228 if (
is_err(pwr_spec_binned[0], pwr_spec_binned_0[0], j))
continue;
229 P_k = pwr_spec_binned[1][j];
230 P_lin = pwr_spec_binned_0[1][j] *
pow2(growth);
231 File << std::scientific << pwr_spec_binned_0[0][j] <<
"\t" << std::fixed << (P_k-P_lin)/P_lin <<
"\n";
236 FTYPE_t growth, std::string out_dir, std::string suffix)
238 out_dir +=
"pwr_diff/";
239 std::string file_name = out_dir +
"pwr_spec_diff" + suffix +
".dat";
242 BOOST_LOG_TRIVIAL(debug) <<
"Writing power spectrum difference into file " << file_name;
243 File <<
"# This file contains relative difference between power spectrum P(k)\n" 244 "# and lineary extrapolated input power spectrum\n" 245 "# depending on wavenumber k in units [h/Mpc].\n" 246 "# k [h/Mpc]\t(P(k, z)-P_lin(k, z))/P_lin(k, z)\n";
248 FTYPE_t k, P_k, P_lin;
249 const size_t size = pwr_spec_binned.
size();
250 for (
size_t j = 0; j < size; j++){
251 k = pwr_spec_binned[0][j];
252 if (k < pwr_spec_input.
x_min)
continue;
253 else if(k > pwr_spec_input.
x_max)
break;
256 P_k = pwr_spec_binned[1][j];
257 P_lin = pwr_spec_input(k) *
pow2(growth);
258 File << std::scientific << k <<
"\t" << std::fixed << (P_k-P_lin)/P_lin <<
"\n";
264 const Interp_obj &pwr_spec_input, FTYPE_t growth_now, FTYPE_t growth_init, std::string out_dir, std::string suffix)
266 out_dir +=
"pwr_diff/";
267 std::string file_name = out_dir +
"pwr_spec_diff" + suffix +
".dat";
270 BOOST_LOG_TRIVIAL(debug) <<
"Writing power spectrum difference into file " << file_name;
271 File <<
"# This file contains relative difference between power spectrum P(k)\n" 272 "# and lineary extrapolated 'hybrid' power spectrum\n" 273 "# depending on wavenumber k in units [h/Mpc].\n" 274 "# k [h/Mpc]\t(P(k, z)-P_lin(k, z))/P_lin(k, z)\n";
276 FTYPE_t k, P_k, P_input, P_par;
277 File << std::setprecision(10);
278 const size_t size = pwr_spec_binned.
size();
279 for (
size_t j = 0; j < size; j++){
280 if (
is_err(pwr_spec_binned[0], pwr_spec_binned_0[0], j))
continue;
281 k = pwr_spec_binned_0[0][j];
282 if (k < pwr_spec_input.
x_min)
continue;
283 else if(k > pwr_spec_input.
x_max)
break;
286 P_k = pwr_spec_binned[1][j];
287 P_input = pwr_spec_input(k) *
pow2(growth_now);
288 P_par = pwr_spec_binned_0[1][j] *
pow2(growth_now / growth_init);
289 File << std::scientific << k <<
"\t" << std::fixed << P_k/P_input - P_k/P_par <<
"\n";
295 FTYPE_t dDda, std::string out_dir, std::string suffix)
297 out_dir +=
"vel_pwr_diff/";
298 std::string file_name = out_dir +
"vel_pwr_spec_diff" + suffix +
".dat";
301 BOOST_LOG_TRIVIAL(debug) <<
"Writing power velocity divergence spectrum difference into file " << file_name;
302 File <<
"# This file contains relative difference between velocity divergence power spectrum P(k)\n" 303 "# and lineary extrapolated velocity divergence power spectrum depending on wavenumber k in units [h/Mpc].\n" 304 "# k [h/Mpc]\t(P(k, z)-P_lin(k, z))/P_lin(k, z)\n";
307 File << std::setprecision(10);
308 const size_t size = pwr_spec_binned.
size();
309 for (
size_t j = 0; j < size; j++){
310 if (
is_err(pwr_spec_binned[0], pwr_spec_binned_0[0], j))
continue;
311 P_k = pwr_spec_binned[1][j];
312 P_ZA = pwr_spec_binned_0[1][j] *
pow2(dDda);
313 File << std::scientific << pwr_spec_binned_0[0][j] <<
"\t" << std::fixed << (P_k-P_ZA)/P_ZA <<
"\n";
319 out_dir +=
"rho_map/";
320 const FTYPE_t x_0 = sim.
x_0_pwr();
321 std::string file_name = out_dir +
"rho_map" + suffix +
".dat";
324 BOOST_LOG_TRIVIAL(debug) <<
"Writing density map into file " << file_name;
325 File <<
"# This file contains density map delta(x).\n";
326 File <<
"# x [Mpc/h]\tz [Mpc/h]\tdelta\n";
328 #define HALF_THICKNESS 2 334 std::vector<FTYPE_t> buffer(N/N_step*N/N_step);
337 #pragma omp parallel for 338 for (
size_t i = 0; i < N; i += N_step)
340 for (
size_t j = 0; j < N; j += N_step)
343 FTYPE_t proj_delta = 0;
344 for (
size_t i_xx = 0; i_xx < N_step; i_xx++)
346 for (
size_t j_xx = 0; j_xx < N_step; j_xx++)
356 proj_delta /= N_step*N_step;
358 size_t id = (i/N_step) * (N/N_step) + j/N_step;
359 buffer[id] = proj_delta;
364 for (
size_t i = 0; i < N; i += N_step)
366 for (
size_t j = 0; j < N; j += N_step)
368 size_t id = (i/N_step) * (N/N_step) + j/N_step;
369 FTYPE_t proj_delta = buffer[id];
370 File << i*x_0 <<
"\t" << j*x_0 <<
"\t" << proj_delta <<
"\n";
378 out_dir +=
"rho_map/";
379 const FTYPE_t x_0 = sim.
x_0_pwr();
380 std::string file_name = out_dir +
"rho_map_projected" + suffix +
".dat";
383 BOOST_LOG_TRIVIAL(debug) <<
"Writing density map into file " << file_name <<
"\n";
384 File <<
"# This file contains density map delta(x).\n" 385 "# x [Mpc/h]\tz [Mpc/h]\tdelta\n";
386 FTYPE_t rho, rho_tmp;
388 for (
size_t i = 0; i < N; i++){
389 for (
size_t j = 0; j < N; j++){
391 for (
size_t k = 0; k < N; k++){
392 rho_tmp = delta(i, k, j);
396 File << i*x_0 <<
"\t" << j*x_0 <<
"\t" << rho -1 <<
"\n";
402 void print_dens_bin(
const std::vector<size_t> &dens_binned, std::string out_dir, std::string suffix){
403 out_dir +=
"rho_bin/";
404 std::string file_name = out_dir +
"rho_bin" + suffix +
".dat";
407 BOOST_LOG_TRIVIAL(debug) <<
"Writing binned density into file " << file_name;
408 File <<
"# This file contains binned density field.\n" 411 const size_t N = dens_binned.size();
412 for (
size_t j = 0; j < N; j++)
415 File << dens <<
"\t" << dens_binned[j] <<
"\n";
void print_rho_map(const Mesh &delta, const Sim_Param &sim, std::string out_dir, std::string suffix)
void print_vel_pow_spec_diff(const Data_Vec< double, 2 > &pwr_spec_binned, const Data_Vec< double, 2 > &pwr_spec_binned_0, double dDda, std::string out_dir, std::string suffix)
Ifstream(std::string file_name)
std::string std_out_dir(const std::string &pre_subdir, const Sim_Param &sim)
functions handling output of the program
std::string currentDateTime()
: linear interpolation (Steffen) of data [x, y]
class handling particles (position only)
void remove_all_files(const std::string &out_dir)
: class storing simulation parameters
size_t size() const noexcept
void print_dens_bin(const std::vector< size_t > &dens_binned, std::string out_dir, std::string suffix)
: creates a mesh of N*N*(N+2) cells
void print_pow_spec(const Data_Vec< double, 2 > &pwr_spec_binned, std::string out_dir, std::string suffix)
define container Particle (with and without velocity)
void print_vel_pow_spec(const Data_Vec< double, 2 > &pwr_spec_binned, std::string out_dir, std::string suffix)
void print_projected_rho(const Mesh &delta, const Sim_Param &sim, std::string out_dir, std::string suffix)
bool is_err(const std::vector< T > &vec1, const std::vector< T > &vec2, size_t bin)
class handling particles (position only)
declaration in params.hpp
void create_dir(const std::string &out_dir)
static CCL_BEGIN_DECLS double x[111][8]
T rel_error(const T &a, const T &b)
void print_pow_spec_diff(const Data_Vec< double, 2 > &pwr_spec_binned, const Data_Vec< double, 2 > &pwr_spec_binned_0, double growth, std::string out_dir, std::string suffix)
void print_corr_func(const Data_Vec< double, 2 > &pwr_spec_binned, std::string out_dir, std::string suffix)
void print_par_pos_cut_small(const std::vector< T > &particles, const Sim_Param &sim, std::string out_dir, std::string suffix)
void remove_dir(const std::string &out_dir)
auto value(T const &val) -> Generator< T >
bool exists(std::string filename)
Ofstream(std::string file_name)