Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
main.cpp
Go to the documentation of this file.
1 
9 #include "stdafx.h"
10 
11 #include "params.hpp"
12 #include "adhesion.hpp"
13 #include "chameleon.hpp"
14 #include "frozen_flow.hpp"
15 #include "frozen_potential.hpp"
16 #include "mod_frozen_potential.hpp"
17 #include "zeldovich.hpp"
18 
19 #include <boost/log/core.hpp>
20 #include <boost/log/utility/setup/common_attributes.hpp>
21 #include <boost/log/utility/setup/console.hpp>
22 #include <boost/log/utility/setup/file.hpp>
23 #include <boost/timer/timer.hpp>
24 #include <boost/date_time/posix_time/posix_time_types.hpp>
25 
26 namespace logging = boost::log;
27 namespace sinks = boost::log::sinks;
28 namespace keywords = boost::log::keywords;
29 
30 typedef sinks::synchronous_sink< sinks::text_file_backend > sink_t;
31 typedef sinks::synchronous_sink< sinks::text_ostream_backend > sink_os_t;
32 
33 namespace
34 {
36 {
37  // set logging core to log everything
38  logging::core::get()->set_filter
39  (
40  logging::trivial::severity >= logging::trivial::trace
41  );
42 
43  // add atributes
44  boost::log::add_common_attributes();
45  boost::log::register_simple_formatter_factory< boost::log::trivial::severity_level, char >("Severity");
46 
47  // register 'std:cout' as sink
48  boost::shared_ptr< sink_os_t > g_file_sink = logging::add_console_log(std::cout);
49  g_file_sink->set_filter(logging::trivial::severity >= logging::trivial::info);
50 }
51 
52 class Logger
53 {
54 public:
55  Logger(const std::string& filename):
56  g_file_sink(logging::add_file_log(
57  keywords::file_name = filename,
58  keywords::time_based_rotation = sinks::file::rotation_at_time_interval(boost::posix_time::hours(1)),
59  keywords::format = "[%TimeStamp%] <%Severity%>: %Message%"
60  ))
61  {
62  g_file_sink->set_filter(logging::trivial::severity >= logging::trivial::debug);
63  }
65  {
66  // close the log file
67  logging::core::get()->remove_sink(g_file_sink);
68  g_file_sink.reset();
69  }
70 
71 private:
72  boost::shared_ptr< sink_t > g_file_sink;
73 };
74 
75 class Timer{
76 public:
77  Timer() = default;
78  ~Timer(){
79  stop();
80  }
81  void stop(bool print = true){
82  if (!_t.is_stopped()){
83  _t.stop();
84  if (print) BOOST_LOG_TRIVIAL(info) << "Time elapsed:" << _t.format();
85  }
86  }
87 private:
88  boost::timer::cpu_timer _t;
89 };
90 
91 template<class T>
93 {
94  // timer for wall time, CPU time
95  Timer t;
96 
97  // create approximation class and whether we have truncation in initial power spectrum
98  T APP(sim);
99  APP.update_cosmo(sim.cosmo);
100 
101  // register log file
102  Logger l(APP.get_out_dir() + "log/log" + "_%N.log");
103 
104  // run the simulation
105  APP.run_simulation();
106 
107  // stop timer manually so it is in the log file
108  t.stop();
109 }
110 
111 }
112 
120 int main(int argc, char* argv[]){
121  // initialize logging
122  init_logging();
123 
124  // timer --automatically displays timing information
125  Timer t;
126 
127  /* SIMULATION PARAMETERS
128  - read and handle command line options / config file
129  - compute power spectrum normalization
130  */
131  Sim_Param sim(argc, argv);
132  if (sim.is_ready())
133  {
134  sim.print_info();
135  do{
136  /* ZEL`DOVICH APPROXIMATION */
137  if(sim.comp_app.ZA) init_and_run_app<App_Var_ZA>(sim);
138 
139  /* TRUNCATED ZEL`DOVICH APPROXIMATION */
140  if(sim.comp_app.TZA) init_and_run_app<App_Var_TZA>(sim);
141 
142  /* FROZEN-FLOW APPROXIMATION */
143  if(sim.comp_app.FF) init_and_run_app<App_Var_FF>(sim);
144 
145  /* FROZEN-POTENTIAL APPROXIMATION */
146  if(sim.comp_app.FP) init_and_run_app<App_Var_FP>(sim);
147 
148  /* PARTICLE-MESH APPROXIMATION */
149  if(sim.comp_app.PM) init_and_run_app<App_Var_PM>(sim);
150 
151  /* ADHESION APPROXIMATION */
152  if(sim.comp_app.AA) init_and_run_app<App_Var_AA>(sim);
153 
154  /* MODIFIED FROZEN-POTENTIAL APPROXIMATION */
155  if(sim.comp_app.FP_pp) init_and_run_app<App_Var_FP_mod>(sim);
156 
157  /* CHAMELEON GRAVITY (FROZEN-POTENTIAL APPROXIMATION) */
158  if(sim.comp_app.chi) init_and_run_app<App_Var_Chi>(sim);
159 
160  /* CHAMELEON GRAVITY (FROZEN-FLOW APPROXIMATION) */
161  if(sim.comp_app.chi_ff) init_and_run_app<App_Var_Chi_FF>(sim);
162 
163  } while (sim.simulate());
164  BOOST_LOG_TRIVIAL(info) << "All simulations completed.";
165  }
166  else
167  {
168  t.stop(false);
169  }
170  return 0;
171 }
sinks::synchronous_sink< sinks::text_ostream_backend > sink_os_t
Definition: main.cpp:31
bool simulate()
Definition: params.hpp:218
bool TZA
Definition: params.hpp:100
sinks::synchronous_sink< sinks::text_file_backend > sink_t
Definition: main.cpp:30
: class storing simulation parameters
Definition: params.hpp:193
Comp_App comp_app
Definition: params.hpp:205
modified frozen-potential approximation interface
ahesion approximation interface
bool is_ready()
Definition: params.cpp:556
boost::shared_ptr< sink_t > g_file_sink
Definition: main.cpp:72
bool FF
Definition: params.hpp:100
std::ostream & cout()
Frozen-potential approximation interface.
system include files and for project-specific include files that are used frequently but are changed ...
Cosmo_Param cosmo
Definition: params.hpp:206
void stop(bool print=true)
Definition: main.cpp:81
bool FP_pp
Definition: params.hpp:100
bool chi_ff
Definition: params.hpp:102
frozen-flow approximation interface
bool FP
Definition: params.hpp:100
various simulation parameters
bool PM
Definition: params.hpp:101
void init_and_run_app(Sim_Param &sim)
Definition: main.cpp:92
bool ZA
Definition: params.hpp:100
bool chi
Definition: params.hpp:102
Logger(const std::string &filename)
Definition: main.cpp:55
Zel`dovich approximation interface.
bool AA
Definition: params.hpp:100
void print_info(std::string out, std::string app) const
Definition: params.cpp:497
int main(int argc, char *argv[])
< end of anonymous namespace
Definition: main.cpp:120
boost::timer::cpu_timer _t
Definition: main.cpp:88
chameleon model of gravity interface