Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
test_chameleon.cpp File Reference
#include <catch.hpp>
#include "../test.hpp"
#include "chameleon.cpp"
Include dependency graph for test_chameleon.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{test_chameleon.cpp}
 

Functions

template<typename T >
anonymous_namespace{test_chameleon.cpp}::mean (const std::vector< T > &data)
 
double anonymous_namespace{test_chameleon.cpp}::mean (const Mesh &data)
 
void anonymous_namespace{test_chameleon.cpp}::get_neighbor_gridindex (std::vector< size_t > &index_list, size_t i, size_t N)
 
void anonymous_namespace{test_chameleon.cpp}::init_overdensity (Sim_Param &sim, Mesh &rho, MultiGrid< 3, CHI_PREC_t > &rho_grid)
 
void anonymous_namespace{test_chameleon.cpp}::get_grav_pot (Mesh &rho, const FFTW_PLAN_TYPE &p_F, const FFTW_PLAN_TYPE &p_B, double box_size, double phi_prefactor)
 
template<typename T >
static int anonymous_namespace{test_chameleon.cpp}::sgn (T val)
 
void anonymous_namespace{test_chameleon.cpp}::print_mesh (const std::string &file_name, const Mesh &pot, const double mod=-1-CHI_MIN)
 
 TEST_CASE ("UNIT TEST: create Multigrid and copy data to/from Mesh","[multigrid]")
 
 TEST_CASE ("UNIT TEST: create and initialize ChiSolver, check bulk field","[chameleon]")
 
 TEST_CASE ("UNIT TEST: create and initialize ChiSolver, solve sphere","[chameleon]")
 

Function Documentation

TEST_CASE ( "UNIT TEST: create Multigrid and copy data to/from Mesh ,
""  [multigrid] 
)

Definition at line 111 of file test_chameleon.cpp.

References CHECK, anonymous_namespace{chameleon.cpp}::min(), anonymous_namespace{chameleon.cpp}::transform_Mesh_to_MultiGrid(), and anonymous_namespace{chameleon.cpp}::transform_MultiGrid_to_Mesh().

111  : create Multigrid and copy data to/from Mesh", "[multigrid]" )
112 {
113  print_unit_msg("create Multigrid and copy data to/from Mesh");
114 
115  constexpr size_t N = 32;
116  srand(time(0));
117  MultiGrid<3, FTYPE_t> grid(N);
118  Mesh mesh_from(N);
119  Mesh mesh_to(N);
120 
121  const std::vector<size_t> some_indices = // Mesh indices = N*N*(N+2)
122  {4, // (0, 0, 4)
123  (N+2)*2+5, // (0, 2, 5)
124  N*(N+2)*4+8*(N+2)+5}; // (4, 8, 5)
125  for(size_t i : some_indices) mesh_from[i] = 2.0*rand()/RAND_MAX - 1;
126 
127  transform_Mesh_to_MultiGrid(mesh_from, grid);
128  CHECK(min(mesh_from) == min(grid));
129  transform_MultiGrid_to_Mesh(mesh_to, grid);
130 
131  for(size_t i : some_indices) CHECK( mesh_from[i] == mesh_to[i] );
132  CHECK(min(mesh_from) == min(grid));
133  CHECK(min(mesh_to) == min(grid));
134 }
: creates a mesh of N*N*(N+2) cells
Definition: class_mesh.hpp:95
TEST_CASE ( "UNIT TEST: create and initialize  ChiSolver,
check bulk field"  ,
""  [chameleon] 
)

Definition at line 136 of file test_chameleon.cpp.

References ccl_test_power::argv, Mesh_base< T >::assign(), Chi_Opt::beta, CHECK, CHECK_THROWS_AS, Sim_Param::chi_opt, Sim_Param::cosmo, anonymous_namespace{test_chameleon.cpp}::get_neighbor_gridindex(), ccl_test_distances::h, Chi_Opt::n, REQUIRE, sqrt(), and anonymous_namespace{chameleon.cpp}::transform_Mesh_to_MultiGrid().

136  : create and initialize ChiSolver, check bulk field", "[chameleon]" )
137 {
138  print_unit_msg("create and initialize ChiSolver, check bulk field");
139 
140  constexpr size_t N = 32;
141  constexpr FTYPE_t a = 0.5;
142  constexpr FTYPE_t rho_0 = 0.3;
143  const std::vector<size_t> some_indices = // Mesh indices = N*N*(N+2)
144  {4, // (0, 0, 4)
145  (N+2)*2+5, // (0, 2, 5)
146  N*(N+2)*4+8*(N+2)+5}; // (4, 8, 5)
147 
148  // initialize Sim_Param
149  const int argc = 1;
150  const char* const argv[1] = {"test"};
151  Sim_Param sim(argc, argv);
152 
153  // check thowing of exception with unphysical values of chameleon parameters
154  auto constructor = [&sim](){ ChiSolver<FTYPE_t> _sol(N, sim, false); };
155  sim.chi_opt.n = -1;
156  CHECK_THROWS_AS( constructor(), std::out_of_range );
157  sim.chi_opt.n = 0.5;
158  sim.chi_opt.beta = -1;
159  CHECK_THROWS_AS( constructor(), std::out_of_range );
160 
161  // initialize ChiSolver
162  sim.chi_opt.beta = 1/sqrt(6.);
163  ChiSolver<FTYPE_t> sol(N, sim, false);
164 
165  // check thowing of exceptions in uninitialised state
166  CHECK_THROWS_AS( sol.set_bulk_field(), std::out_of_range );
167 
168  // initialize overdensity -- constant density
169  MultiGrid<3, FTYPE_t> rho_grid(N);
170  {
171  Mesh rho(N);
172  rho.assign(rho_0);
173  transform_Mesh_to_MultiGrid(rho, rho_grid);
174  }
175 
176  // check density
177  for(size_t i : some_indices) CHECK( rho_grid[0][i] == rho_0 );
178 
179  // set ChiSolver -- bulk field
180  sol.set_time(a, sim.cosmo);
181  sol.add_external_grid(&rho_grid);
182  sol.set_bulk_field();
183 
184  // check bulk field
185  {
186  const FTYPE_t chi_bulk = sol.chi_min(rho_0);
187  FTYPE_t const* const chi = sol.get_y();
188  for(size_t i : some_indices) REQUIRE( chi[i] == Approx(chi_bulk));
189  }
190 
191  // check that EOM is satisfied
192  std::vector<size_t> index_list;
193  size_t level = 0;
194  const FTYPE_t h = 1.0/FTYPE_t( sol.get_N(level) );
195  for(size_t i : some_indices)
196  {
197  get_neighbor_gridindex(index_list, i, N);
198  CHECK( sol.l_operator(level, index_list, true, h) == Approx(0.));
199  }
200 }
TEST_CASE ( "UNIT TEST: create and initialize  ChiSolver,
solve sphere"  ,
""  [chameleon] 
)

Definition at line 202 of file test_chameleon.cpp.

References ccl_test_power::argv, Sim_Param::box_opt, Box_Opt::box_size, Test_Opt::coarse_sweeps, Mesh::complex(), Sim_Param::cosmo, create_dir(), FFTW_DEST_PLAN, FFTW_PLAN_C2R, FFTW_PLAN_OMP, FFTW_PLAN_OMP_CLEAN, FFTW_PLAN_OMP_INIT, FFTW_PLAN_R2C, FFTW_PLAN_TYPE, Test_Opt::fine_sweeps, anonymous_namespace{test_chameleon.cpp}::get_grav_pot(), anonymous_namespace{test_chameleon.cpp}::init_overdensity(), Test_Opt::max_steps, Test_Opt::N_grid, Test_Opt::N_min, Run_Opt::nt, Out_Opt::out_dir, Sim_Param::out_opt, Sim_Param::print_info(), anonymous_namespace{test_chameleon.cpp}::print_mesh(), Mesh_base< T >::real(), remove_all_files(), Sim_Param::run_opt, Test_Opt::step_per_iter, Sim_Param::test_opt, anonymous_namespace{chameleon.cpp}::transform_MultiGridSolver_to_Mesh(), and Test_Opt::verbose.

202  : create and initialize ChiSolver, solve sphere", "[chameleon]" )
203 {
204  print_unit_msg("create and initialize ChiSolver, solve sphere");
205 
206  // initialize Sim_Param
207  const char* const argv[1] = {"test"};
208  Sim_Param sim(1, argv);
209  const size_t N = sim.test_opt.N_grid;
210  const size_t N_min = sim.test_opt.N_min;
211  const bool verbose = sim.test_opt.verbose;
212 
213  // initialize ChiSolver
214  ChiSolver<CHI_PREC_t> sol(N, N_min, sim, verbose);
215 
216  // initialize overdensity -- constant density in sphere of radius R, center at x0, y0, z0
217  MultiGrid<3, CHI_PREC_t> rho_grid(N);
218  Mesh rho(N);
219  init_overdensity(sim, rho, rho_grid);
220 
221  // FFTW preparation
222  if (!FFTW_PLAN_OMP_INIT()){
223  throw std::runtime_error("Errors during multi-thread initialization");
224  }
225  FFTW_PLAN_OMP(sim.run_opt.nt);
226  const FFTW_PLAN_TYPE p_F = FFTW_PLAN_R2C(N, N, N, rho.real(), rho.complex(), FFTW_ESTIMATE);
227  const FFTW_PLAN_TYPE p_B = FFTW_PLAN_C2R(N, N, N, rho.complex(), rho.real(), FFTW_ESTIMATE);
228 
229  // set ChiSolver
230  sol.set_time(1, sim.cosmo);
231  sol.set_def_convergence();
232  sol.add_external_grid(&rho_grid);
233  sol.set_ngs_sweeps(sim.test_opt.fine_sweeps, sim.test_opt.coarse_sweeps); //< fine, coarse
234 
235  // compute gravitational potential
236  Mesh phi_pot(rho); //< copy density
237  get_grav_pot(phi_pot, p_F, p_B, sim.box_opt.box_size, sol.get_phi_prefactor());
238 
239  // get linear prediction
240  sol.set_linear(rho, p_F, p_B);
241  sol.set_screened();
242 
243  // full solution on Mesh
244  Mesh chi_full(N);
245 
246  // create directory structure
247  std::string out_dir = sim.out_opt.out_dir + "test_ChiSolver/";
248  remove_all_files(out_dir);
249  create_dir(out_dir);
250  sim.print_info(out_dir, "test");
251 
252  // print gravitational potential
253  print_mesh(out_dir + "grav_pot.dat", phi_pot, 0);
254 
255  // Solve the equation -- full V-cycles
256  BOOST_LOG_TRIVIAL(info) << "Starting V-cycles with intermediate output...\n";
257 
258  int istep = 0;
259  const size_t step_per_iter = sim.test_opt.step_per_iter;
260  while(1)
261  {
262  // istep, max_step (use int for the last iteration -- negative max_step)
263  istep += sol.get_istep();
264  int max_step = istep + step_per_iter > sim.test_opt.max_steps ? sim.test_opt.max_steps - istep : step_per_iter;
265 
266  // print chi_full
267  transform_MultiGridSolver_to_Mesh(chi_full, sol);
268  print_mesh(out_dir + "chi_istep_" + std::to_string(istep) + ".dat", chi_full);
269 
270  // check max_step and convergence
271  if ((max_step <= 0) || ((istep != 0) && (sol.get_istep() < step_per_iter))) break;
272 
273  // set & solve
274  sol.set_maxsteps(max_step);
275  sol.solve();
276  }
277 
278  // FFTW CLEANUP
279  FFTW_DEST_PLAN(p_F);
280  FFTW_DEST_PLAN(p_B);
281  FFTW_PLAN_OMP_CLEAN();
282 }