Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
core_out.h File Reference

functions handling output of the program More...

#include "stdafx.h"
#include <fstream>
#include "params.hpp"
#include "core_power.h"
#include "class_data_vec.hpp"
#include "class_mesh.hpp"
Include dependency graph for core_out.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Ofstream
 
class  Ifstream
 

Functions

std::string currentDateTime ()
 
std::string std_out_dir (const std::string &pre_subdir, const Sim_Param &sim)
 
void create_dir (const std::string &out_dir)
 
void remove_dir (const std::string &out_dir)
 
void remove_all_files (const std::string &out_dir)
 
void print_pow_spec (const Data_Vec< double, 2 > &pwr_spec_binned, std::string out_dir, std::string suffix)
 
void print_vel_pow_spec (const Data_Vec< double, 2 > &pwr_spec_binned, 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_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_pow_spec_diff (const Data_Vec< double, 2 > &pwr_spec_binned, const Interp_obj &pwr_spec_input, double growth, std::string out_dir, std::string suffix)
 
void print_pow_spec_diff (const Data_Vec< double, 2 > &pwr_spec_binned, const Data_Vec< double, 2 > &pwr_spec_binned_0, const Interp_obj &pwr_spec_input, double growth_now, double growth_init, 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 b, std::string out_dir, std::string suffix)
 
template<class T >
void print_par_pos_cut_small (const std::vector< T > &particles, const Sim_Param &sim, std::string out_dir, std::string suffix)
 
void print_rho_map (const Mesh &rho, const Sim_Param &sim, 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)
 
void print_dens_bin (const std::vector< size_t > &dens_binned, std::string out_dir, std::string suffix)
 

Detailed Description

functions handling output of the program

Author
Michal Vrastil
Date
2018-07-11

Definition in file core_out.h.

Function Documentation

void create_dir ( const std::string &  out_dir)

Definition at line 74 of file core_out.cpp.

References ccl_test_distances::path.

Referenced by App_Var< T >::Impl< T >::create_work_dir(), and TEST_CASE().

75 {
76  const fs::path dir(out_dir.c_str());
77  if(fs::create_directories(dir))
78  {
79  BOOST_LOG_TRIVIAL(debug) << "Directory created: "<< out_dir;
80  }
81 }
std::string currentDateTime ( )

Definition at line 41 of file core_out.cpp.

Referenced by std_out_dir().

42 {
43  const time_t now = time(0);
44  struct tm tstruct;
45  char buf[80];
46  gmtime_r(&now, &tstruct);
47  strftime(buf, sizeof(buf), "%y%m%d_%H%M%S", &tstruct);
48 
49  std::string returnval(buf);
50  return returnval;
51 }
void print_corr_func ( const Data_Vec< double, 2 > &  pwr_spec_binned,
std::string  out_dir,
std::string  suffix 
)

Definition at line 174 of file core_out.cpp.

References Data_Vec< T, N >::size().

Referenced by App_Var< T >::Impl< T >::print_corr().

175 {
176  out_dir += "corr_func/";
177  std::string file_name = out_dir + "corr_func" + suffix + ".dat";
178  Ofstream File(file_name);
179 
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";
183 
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";
187  }
188 }
size_t size() const noexcept
void print_dens_bin ( const std::vector< size_t > &  dens_binned,
std::string  out_dir,
std::string  suffix 
)

Definition at line 402 of file core_out.cpp.

References print_par_pos_cut_small().

Referenced by App_Var< T >::Impl< T >::print_density().

402  {
403  out_dir += "rho_bin/";
404  std::string file_name = out_dir + "rho_bin" + suffix + ".dat";
405  Ofstream File(file_name);
406 
407  BOOST_LOG_TRIVIAL(debug) << "Writing binned density into file " << file_name;
408  File << "# This file contains binned density field.\n"
409  "# dens\tbin_num\n";
410  FTYPE_t dens;
411  const size_t N = dens_binned.size();
412  for (size_t j = 0; j < N; j++)
413  {
414  dens = j*0.1-0.9;
415  File << dens << "\t" << dens_binned[j] << "\n";
416  }
417 }
template<class T >
void print_par_pos_cut_small ( const std::vector< T > &  particles,
const Sim_Param sim,
std::string  out_dir,
std::string  suffix 
)

Definition at line 109 of file core_out.cpp.

References Sim_Param::box_opt, Box_Opt::mesh_num, Box_Opt::par_num, x, Sim_Param::x_0(), and z.

Referenced by print_dens_bin(), and App_Var< T >::Impl< T >::print_position().

110 {
111  out_dir += "par_cut/";
112  std::string file_name = out_dir + "par_cut" + suffix + ".dat";
113  Ofstream File(file_name);
114 
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";
117  FTYPE_t x, y, z, dx;
118  const FTYPE_t x_0 = sim.x_0();
119  const size_t N = sim.box_opt.par_num;
120  for(size_t i=0; i < N; i++)
121  {
122  x = particles[i].position[0];
123  y = particles[i].position[1];
124  z = particles[i].position[2];
125  dx = std::abs(y - sim.box_opt.mesh_num/2);
126  if ((dx < 0.5) && (x < sim.box_opt.mesh_num/4.) && (z < sim.box_opt.mesh_num/4.))
127  {
128  // cut (L/4 x L/4 x 0.5)
129  File << x*x_0 << "\t" << z*x_0 << "\t" << y*x_0 << "\n";
130  }
131  }
132 }
size_t par_num
Definition: params.hpp:61
Box_Opt box_opt
Definition: params.hpp:202
static CCL_BEGIN_DECLS double x[111][8]
static double z[8]
double x_0() const
Definition: params.hpp:216
size_t mesh_num
Definition: params.hpp:58
void print_pow_spec ( const Data_Vec< double, 2 > &  pwr_spec_binned,
std::string  out_dir,
std::string  suffix 
)

Definition at line 134 of file core_out.cpp.

References Data_Vec< T, N >::size().

Referenced by App_Var< T >::Impl< T >::print_extrap_pwr(), App_Var< T >::Impl< T >::print_input_realisation(), App_Var_Chi::print_output(), and App_Var< T >::Impl< T >::print_power_spec().

135 {
136  out_dir += "pwr_spec/";
137  std::string file_name = out_dir + "pwr_spec" + suffix + ".dat";
138  Ofstream File(file_name);
139 
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";
143 
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";
149  }
150  File << "\n";
151  }
152 }
size_t size() const noexcept
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 
)

Definition at line 211 of file core_out.cpp.

References is_err(), pow2(), and Data_Vec< T, N >::size().

Referenced by App_Var< T >::Impl< T >::print_power_spec().

213 {
214  out_dir += "pwr_diff/";
215  std::string file_name = out_dir + "pwr_spec_diff" + suffix + ".dat";
216  Ofstream File(file_name);
217 
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";
223 
224  FTYPE_t P_k, P_lin;
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";
232  }
233 }
size_t size() const noexcept
T pow2(T base)
Definition: precision.hpp:52
bool is_err(const std::vector< T > &vec1, const std::vector< T > &vec2, size_t bin)
Definition: core_out.cpp:197
void print_pow_spec_diff ( const Data_Vec< double, 2 > &  pwr_spec_binned,
const Interp_obj pwr_spec_input,
double  growth,
std::string  out_dir,
std::string  suffix 
)

Definition at line 235 of file core_out.cpp.

References pow2(), Data_Vec< T, N >::size(), Interp_obj::x_max, and Interp_obj::x_min.

237 {
238  out_dir += "pwr_diff/";
239  std::string file_name = out_dir + "pwr_spec_diff" + suffix + ".dat";
240  Ofstream File(file_name);
241 
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";
247 
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;
254  else
255  {
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";
259  }
260  }
261 }
size_t size() const noexcept
T pow2(T base)
Definition: precision.hpp:52
double x_min
Definition: core_power.h:104
double x_max
Definition: core_power.h:104
void print_pow_spec_diff ( const Data_Vec< double, 2 > &  pwr_spec_binned,
const Data_Vec< double, 2 > &  pwr_spec_binned_0,
const Interp_obj pwr_spec_input,
double  growth_now,
double  growth_init,
std::string  out_dir,
std::string  suffix 
)

Definition at line 263 of file core_out.cpp.

References is_err(), pow2(), Data_Vec< T, N >::size(), Interp_obj::x_max, and Interp_obj::x_min.

265 {
266  out_dir += "pwr_diff/";
267  std::string file_name = out_dir + "pwr_spec_diff" + suffix + ".dat";
268  Ofstream File(file_name);
269 
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";
275 
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;
284  else
285  {
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";
290  }
291  }
292 }
size_t size() const noexcept
T pow2(T base)
Definition: precision.hpp:52
double x_min
Definition: core_power.h:104
bool is_err(const std::vector< T > &vec1, const std::vector< T > &vec2, size_t bin)
Definition: core_out.cpp:197
double x_max
Definition: core_power.h:104
void print_projected_rho ( const Mesh delta,
const Sim_Param sim,
std::string  out_dir,
std::string  suffix 
)

Definition at line 376 of file core_out.cpp.

References Sim_Param::box_opt, Box_Opt::mesh_num_pwr, and Sim_Param::x_0_pwr().

377 {
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";
381  Ofstream File(file_name);
382 
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;
387  const size_t N = sim.box_opt.mesh_num_pwr;
388  for (size_t i = 0; i < N; i++){
389  for (size_t j = 0; j < N; j++){
390  rho = 0;
391  for (size_t k = 0; k < N; k++){
392  rho_tmp = delta(i, k, j);
393  // if (rho_tmp != -1) printf("Density in (%i, %i, %i) = %f\n", i, j, k, rho_tmp);
394  rho+=rho_tmp + 1;
395  }
396  File << i*x_0 << "\t" << j*x_0 << "\t" << rho -1 << "\n";
397  }
398  File << "\n";
399  }
400 }
Box_Opt box_opt
Definition: params.hpp:202
size_t mesh_num_pwr
Definition: params.hpp:58
double x_0_pwr() const
Definition: params.hpp:217
void print_rho_map ( const Mesh rho,
const Sim_Param sim,
std::string  out_dir,
std::string  suffix 
)

Definition at line 317 of file core_out.cpp.

References Sim_Param::box_opt, HALF_THICKNESS, MAX_SIZE, Box_Opt::mesh_num_pwr, and Sim_Param::x_0_pwr().

Referenced by App_Var< T >::Impl< T >::print_density().

318 {
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";
322  Ofstream File(file_name);
323 
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";
327 
328  #define HALF_THICKNESS 2
329  #define MAX_SIZE 512
330  // create buffer into which we can write in parallel
331  // smear density field if we have very hight accuracy
332  const size_t N = sim.box_opt.mesh_num_pwr;
333  const size_t N_step = N > MAX_SIZE ? N / MAX_SIZE : 1;
334  std::vector<FTYPE_t> buffer(N/N_step*N/N_step);
335 
336  // go throught one side (in parallel)
337  #pragma omp parallel for
338  for (size_t i = 0; i < N; i += N_step)
339  {
340  for (size_t j = 0; j < N; j += N_step)
341  {
342  // average over one side, add up 3rd dimension
343  FTYPE_t proj_delta = 0;
344  for (size_t i_xx = 0; i_xx < N_step; i_xx++)
345  {
346  for (size_t j_xx = 0; j_xx < N_step; j_xx++)
347  {
348  for (int k = -HALF_THICKNESS; k < HALF_THICKNESS; k++)
349  {
350  // we are at [i+i_xx, N/2 + k, j+j_xx]
351  proj_delta += delta(i+i_xx, sim.box_opt.mesh_num_pwr/2 + k, j+j_xx) + 1;
352  }
353  }
354  }
355  // average and save
356  proj_delta /= N_step*N_step;
357  proj_delta--;
358  size_t id = (i/N_step) * (N/N_step) + j/N_step;
359  buffer[id] = proj_delta;
360  }
361  }
362 
363  // write into file
364  for (size_t i = 0; i < N; i += N_step)
365  {
366  for (size_t j = 0; j < N; j += N_step)
367  {
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";
371  }
372  File << "\n";
373  }
374 }
Box_Opt box_opt
Definition: params.hpp:202
size_t mesh_num_pwr
Definition: params.hpp:58
#define HALF_THICKNESS
double x_0_pwr() const
Definition: params.hpp:217
#define MAX_SIZE
void print_vel_pow_spec ( const Data_Vec< double, 2 > &  pwr_spec_binned,
std::string  out_dir,
std::string  suffix 
)

Definition at line 154 of file core_out.cpp.

References Data_Vec< T, N >::size().

Referenced by App_Var< T >::Impl< T >::print_vel_pwr().

155 {
156  out_dir += "vel_pwr_spec/";
157  std::string file_name = out_dir + "vel_pwr_spec" + suffix + ".dat";
158  Ofstream File(file_name);
159 
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";
163 
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";
169  }
170  File << "\n";
171  }
172 }
size_t size() const noexcept
void print_vel_pow_spec_diff ( const Data_Vec< double, 2 > &  pwr_spec_binned,
const Data_Vec< double, 2 > &  pwr_spec_binned_0,
double  b,
std::string  out_dir,
std::string  suffix 
)

Definition at line 294 of file core_out.cpp.

References is_err(), pow2(), and Data_Vec< T, N >::size().

Referenced by App_Var< T >::Impl< T >::print_vel_pwr().

296 {
297  out_dir += "vel_pwr_diff/";
298  std::string file_name = out_dir + "vel_pwr_spec_diff" + suffix + ".dat";
299  Ofstream File(file_name);
300 
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";
305 
306  FTYPE_t P_k, P_ZA;
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";
314  }
315 }
size_t size() const noexcept
T pow2(T base)
Definition: precision.hpp:52
bool is_err(const std::vector< T > &vec1, const std::vector< T > &vec2, size_t bin)
Definition: core_out.cpp:197
void remove_all_files ( const std::string &  out_dir)

Definition at line 92 of file core_out.cpp.

References p, and ccl_test_distances::path.

Referenced by TEST_CASE().

93 {
94  const fs::path dir(out_dir.c_str());
95  size_t i = 0;
96 
97  for(auto & p : fs::directory_iterator(dir))
98  {
99  if (fs::is_regular_file(p))
100  {
101  fs::remove(p);
102  ++i;
103  }
104  }
105  BOOST_LOG_TRIVIAL(debug) << "Removed " << i << " file(s) in directory: "<< out_dir;
106 }
static int p
Definition: ccl_emu17.c:25
void remove_dir ( const std::string &  out_dir)

Definition at line 83 of file core_out.cpp.

References ccl_test_distances::path.

84 {
85  const fs::path dir(out_dir.c_str());
86  if (fs::remove_all(dir))
87  {
88  BOOST_LOG_TRIVIAL(debug) << "Directory removed: "<< out_dir;
89  }
90 }
std::string std_out_dir ( const std::string &  pre_subdir,
const Sim_Param sim 
)

directory name = YYMMDD_HHMMSS_m_p_M_b

check if directory exists

try appending numbers starting at 2

Definition at line 53 of file core_out.cpp.

References Sim_Param::box_opt, Box_Opt::box_size, currentDateTime(), exists(), Box_Opt::mesh_num, Box_Opt::mesh_num_pwr, Box_Opt::Ng, Out_Opt::out_dir, Sim_Param::out_opt, and ccl_test_distances::path.

54 {
56  const std::string out_dir = sim.out_opt.out_dir + pre_subdir + currentDateTime() + "_" + std::to_string(sim.box_opt.mesh_num) +"m_" +
57  std::to_string(sim.box_opt.Ng) + "p_" + std::to_string(sim.box_opt.mesh_num_pwr) +"M_" +
58  std::to_string((size_t)sim.box_opt.box_size) + "b";
59 
61  if (!fs::exists(fs::path(out_dir.c_str()))) return out_dir + "/";
62 
64  else
65  {
66  for(size_t i = 2; ; ++i)
67  {
68  const std::string out_dir_new = out_dir + "_" + std::to_string(i);
69  if (!fs::exists(fs::path(out_dir_new.c_str()))) return out_dir_new + "/";
70  }
71  }
72 }
double box_size
Definition: params.hpp:59
std::string currentDateTime()
Definition: core_out.cpp:41
Box_Opt box_opt
Definition: params.hpp:202
size_t mesh_num_pwr
Definition: params.hpp:58
size_t Ng
Definition: params.hpp:61
Out_Opt out_opt
Definition: params.hpp:204
size_t mesh_num
Definition: params.hpp:58
std::string out_dir
Definition: params.hpp:86
bool exists(std::string filename)
Definition: continuity.cpp:195