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

basic functions to work with mesh More...

#include "core_mesh.h"
Include dependency graph for core_mesh.cpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define ORDER   1
 

Functions

template<typename T >
static int sgn (T val)
 
void get_k_vec (size_t N, size_t index, int *k_vec)
 
void get_k_vec (size_t N, size_t index, Vec_3D< int > &k_vec)
 
double get_k_sq (size_t N, size_t index)
 
template<typename T >
static std::enable_if< std::is_integral< T >::value, T >::type get_per (T vec, size_t per)
 
template<typename T >
static std::enable_if<!std::is_integral< T >::value, T >::type get_per (T vec, size_t per)
 
template<typename T >
void get_per (Vec_3D< T > &position, size_t per)
 
template<typename T >
void get_per (Vec_3D< T > &position, size_t perx, size_t pery, size_t perz)
 
void get_per (std::vector< Particle_v< double >> &particles, const size_t per)
 
template<typename T >
get_distance_1D (const T x_1, const T x_2, const size_t per)
 
template<typename T >
get_distance_1D (const T x_1, const int x_2, const size_t per)
 
template<typename T >
get_sgn_distance_1D (T x_from, T x_to, size_t per)
 
double get_distance (const Vec_3D< double > &x_1, const Vec_3D< double > &x_2, int per)
 
Vec_3D< doubleget_sgn_distance (const Vec_3D< double > &x_from, const Vec_3D< double > &x_to, size_t per)
 
template<size_t N>
static double wgh_sch (const Vec_3D< double > &x, const Vec_3D< int > &y, size_t mesh_num)
 
template<>
double wgh_sch< 1 > (const Vec_3D< double > &x, const Vec_3D< int > &y, size_t mesh_num)
 
template<>
double wgh_sch< 2 > (const Vec_3D< double > &x, const Vec_3D< int > &y, size_t mesh_num)
 
void assign_to (Mesh &field, const Vec_3D< double > &position, const double value)
 
void assign_to (std::vector< Mesh > &field, const Vec_3D< double > &position, const Vec_3D< double > &value)
 
void assign_from (const Mesh &field, const Vec_3D< double > &position, double &value, double mod)
 
void assign_from (const std::vector< Mesh > &field, const Vec_3D< double > &position, Vec_3D< double > &value, double mod)
 
void fftw_execute_dft_r2c (const FFTW_PLAN_TYPE &p_F, Mesh &rho)
 compute forward (real to complex) FFT on mesh (inplace) More...
 
void fftw_execute_dft_c2r (const FFTW_PLAN_TYPE &p_B, Mesh &rho)
 compute backward (complex to real) FFT on mesh (inplace) More...
 
void fftw_execute_dft_r2c_triple (const FFTW_PLAN_TYPE &p_F, std::vector< Mesh > &rho)
 compute three forward (real to complex) FFTs on vector of meshes (inplace) More...
 
void fftw_execute_dft_c2r_triple (const FFTW_PLAN_TYPE &p_B, std::vector< Mesh > &rho)
 compute three backward (complex to real) FFTs on vector of meshes (inplace) More...
 
template void get_per (Vec_3D< int > &, size_t)
 
template void get_per (Vec_3D< int > &, size_t, size_t, size_t)
 
template void get_per (Vec_3D< size_t > &, size_t)
 
template void get_per (Vec_3D< size_t > &, size_t, size_t, size_t)
 
template void get_per (Vec_3D< double > &, size_t)
 
template void get_per (Vec_3D< double > &, size_t, size_t, size_t)
 

Detailed Description

basic functions to work with mesh

Author
Michal Vrastil
Date
2018-07-11

Definition in file core_mesh.cpp.

Macro Definition Documentation

#define ORDER   1

Definition at line 12 of file core_mesh.cpp.

Function Documentation

void assign_from ( const Mesh field,
const Vec_3D< double > &  position,
double value,
double  mod 
)

Definition at line 224 of file core_mesh.cpp.

References IT< points >::iter(), Mesh::N, and IT< points >::vec.

Referenced by kick_step_no_momentum(), App_Var_Chi::ChiImpl::kick_step_w_chi(), App_Var_Chi::ChiImpl::kick_step_w_chi_no_momentum(), kick_step_w_momentum(), kick_step_w_momentum_pm(), and anonymous_namespace{mod_frozen_potential.cpp}::kick_step_w_pp().

225 {
226  IT<ORDER+1> it(position);
227  do{
228  #pragma omp atomic
229  value += field(it.vec) * mod * wgh_sch<ORDER>(position, it.vec, field.N);
230  } while( it.iter() );
231 }
: class for effective iteration of cube of mesh cells
Definition: core_mesh.h:66
size_t N
Definition: class_mesh.hpp:102
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
void assign_from ( const std::vector< Mesh > &  field,
const Vec_3D< double > &  position,
Vec_3D< double > &  value,
double  mod 
)

< reuse the same weigh for every field in std::vector

Definition at line 233 of file core_mesh.cpp.

References IT< points >::iter(), IT< points >::vec, and w.

234 {
235  IT<ORDER+1> it(position);
236  FTYPE_t w;
237  do{
238  w = mod * wgh_sch<ORDER>(position, it.vec, field[0].N);
239  for (unsigned int i = 0; i < 3; i++)
240  {
241  #pragma omp atomic
242  value[i] += field[i](it.vec) * w;
243  }
244  } while( it.iter() );
245 }
: class for effective iteration of cube of mesh cells
Definition: core_mesh.h:66
static double w[2][28][111]
Definition: ccl_emu17.c:33
void assign_to ( Mesh field,
const Vec_3D< double > &  position,
const double  value 
)

Definition at line 201 of file core_mesh.cpp.

References IT< points >::iter(), Mesh::N, and IT< points >::vec.

Referenced by get_rho_from_par(), and get_vel_from_par().

202 {
203  IT<ORDER+1> it(position);
204  do{
205  #pragma omp atomic
206  field(it.vec) += value * wgh_sch<ORDER>(position, it.vec, field.N);
207  } while( it.iter() );
208 }
: class for effective iteration of cube of mesh cells
Definition: core_mesh.h:66
size_t N
Definition: class_mesh.hpp:102
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
void assign_to ( std::vector< Mesh > &  field,
const Vec_3D< double > &  position,
const Vec_3D< double > &  value 
)

< reuse the same weigh for every field in std::vector

Definition at line 210 of file core_mesh.cpp.

References IT< points >::iter(), IT< points >::vec, and w.

211 {
212  IT<ORDER+1> it(position);
213  FTYPE_t w;
214  do{
215  w = wgh_sch<ORDER>(position, it.vec, field[0].N);
216  for (size_t i = 0; i < 3; i++)
217  {
218  #pragma omp atomic
219  field[i](it.vec) += value[i] * w;
220  }
221  } while( it.iter() );
222 }
: class for effective iteration of cube of mesh cells
Definition: core_mesh.h:66
static double w[2][28][111]
Definition: ccl_emu17.c:33
void fftw_execute_dft_c2r ( const FFTW_PLAN_TYPE p_B,
Mesh rho 
)

compute backward (complex to real) FFT on mesh (inplace)

Parameters
p_Bplan for backward transformation
rhomesh upon which the transformation is performed

Definition at line 253 of file core_mesh.cpp.

References Mesh::complex(), FFTW_EXEC_C2R, and Mesh_base< T >::real().

Referenced by fftw_execute_dft_c2r_triple(), anonymous_namespace{test_chameleon.cpp}::get_grav_pot(), App_Var_AA::pot_corr(), and anonymous_namespace{chameleon.cpp}::ChiSolver< T >::set_linear_sol_at_level().

254 {
255  FFTW_EXEC_C2R(p_B, rho.complex(), rho.real());
256 }
FFTW_COMPLEX_TYPE * complex()
get fftw_complex pointer to data
Definition: class_mesh.hpp:111
T * real()
Definition: class_mesh.hpp:36
#define FFTW_EXEC_C2R
Definition: precision.hpp:35
void fftw_execute_dft_c2r_triple ( const FFTW_PLAN_TYPE p_B,
std::vector< Mesh > &  rho 
)

compute three backward (complex to real) FFTs on vector of meshes (inplace)

Parameters
p_Bplan for backward transformations
rhovector of meshes upon which the transformations are performed

Definition at line 263 of file core_mesh.cpp.

References fftw_execute_dft_c2r(), and get_per().

Referenced by App_Var_AA::AAImpl::aa_convolution(), App_Var_Chi::ChiImpl::get_chi_force(), App_Var_FP_mod::pot_corr(), App_Var< T >::pot_corr(), and App_Var< T >::Impl< T >::set_init_cond().

264 {
265  for (unsigned int i = 0; i < 3; i++) fftw_execute_dft_c2r(p_B, rho[i]);
266 }
void fftw_execute_dft_c2r(const FFTW_PLAN_TYPE &p_B, Mesh &rho)
compute backward (complex to real) FFT on mesh (inplace)
Definition: core_mesh.cpp:253
void fftw_execute_dft_r2c ( const FFTW_PLAN_TYPE p_F,
Mesh rho 
)

compute forward (real to complex) FFT on mesh (inplace)

Parameters
p_Fplan for forward transformation
rhomesh upon which the transformation is performed

Definition at line 247 of file core_mesh.cpp.

References Mesh::complex(), FFTW_EXEC_R2C, Mesh::N, pow(), and Mesh_base< T >::real().

Referenced by App_Var_AA::AAImpl::aa_convolution(), fftw_execute_dft_r2c_triple(), App_Var_Chi::ChiImpl::gen_pow_spec_binned(), gen_rho_dist_k(), App_Var< T >::Impl< T >::get_binned_power_spec(), App_Var_Chi::ChiImpl::get_chi_force(), anonymous_namespace{test_chameleon.cpp}::get_grav_pot(), anonymous_namespace{chameleon.cpp}::ChiSolver< T >::set_linear_sol_at_level(), and App_Var_PM::upd_pos().

248 {
249  FFTW_EXEC_R2C(p_F, rho.real(), rho.complex());
250  rho /= pow((FTYPE_t)rho.N, 3); //< normalization
251 }
FFTW_COMPLEX_TYPE * complex()
get fftw_complex pointer to data
Definition: class_mesh.hpp:111
T * real()
Definition: class_mesh.hpp:36
size_t N
Definition: class_mesh.hpp:102
#define FFTW_EXEC_R2C
Definition: precision.hpp:34
float pow(float base, unsigned long int exp)
Definition: precision.hpp:39
void fftw_execute_dft_r2c_triple ( const FFTW_PLAN_TYPE p_F,
std::vector< Mesh > &  rho 
)

compute three forward (real to complex) FFTs on vector of meshes (inplace)

Parameters
p_Fplan for forward transformations
rhovector of meshes upon which the transformations are performed

Definition at line 258 of file core_mesh.cpp.

References fftw_execute_dft_r2c().

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

259 {
260  for (unsigned int i = 0; i < 3; i++) fftw_execute_dft_r2c(p_F, rho[i]);
261 }
void fftw_execute_dft_r2c(const FFTW_PLAN_TYPE &p_F, Mesh &rho)
compute forward (real to complex) FFT on mesh (inplace)
Definition: core_mesh.cpp:247
double get_distance ( const Vec_3D< double > &  x_1,
const Vec_3D< double > &  x_2,
int  per 
)

Definition at line 115 of file core_mesh.cpp.

References get_distance_1D(), pow2(), and sqrt().

116 {
117  FTYPE_t dst = 0;
118  for (unsigned int i = 0; i < 3; i++) dst+= pow2(get_distance_1D(x_1[i], x_2[i], per));
119  return sqrt(dst);
120 }
T get_distance_1D(const T x_1, const T x_2, const size_t per)
Definition: core_mesh.cpp:90
T pow2(T base)
Definition: precision.hpp:52
Grid< NDIM, T > sqrt(Grid< NDIM, T > lhs)
Definition: grid.h:231
template<typename T >
T get_distance_1D ( const T  x_1,
const T  x_2,
const size_t  per 
)
inline

Definition at line 90 of file core_mesh.cpp.

References growth_allz::T.

Referenced by get_distance(), get_distance_1D(), wgh_sch< 1 >(), and wgh_sch< 2 >().

91 {
92  T d = std::abs(x_2 - x_1);
93  if (d <= per / 2) return d; // most probable, first condition
94  else if (d <= per) return per - d;
95  else d = fmod(d, T(per)); // fmod unlikely to evaluate, speed up code
96  if (d <= per / 2) return d;
97  else return per - d;
98 }
template<typename T >
T get_distance_1D ( const T  x_1,
const int  x_2,
const size_t  per 
)
inline

Definition at line 101 of file core_mesh.cpp.

References get_distance_1D(), and growth_allz::T.

101 { return get_distance_1D(x_1, (T)x_2, per); }
T get_distance_1D(const T x_1, const T x_2, const size_t per)
Definition: core_mesh.cpp:90
double get_k_sq ( size_t  N,
size_t  index 
)

Definition at line 38 of file core_mesh.cpp.

References get_k_vec(), and pow2().

Referenced by gen_pot_k(), gen_rho_w_pow_k(), and anonymous_namespace{chameleon.cpp}::ChiSolver< T >::get_chi_k().

39 {
40  int k_vec[3];
41  FTYPE_t tmp = 0;
42  get_k_vec(N, index, k_vec);
43  for (unsigned int i = 0; i<3; i++) tmp += pow2(k_vec[i]);
44  return tmp;
45 }
T pow2(T base)
Definition: precision.hpp:52
void get_k_vec(size_t N, size_t index, int *k_vec)
Definition: core_mesh.cpp:20
void get_k_vec ( size_t  N,
size_t  index,
int *  k_vec 
)

Definition at line 20 of file core_mesh.cpp.

Referenced by gen_displ_k_S2(), get_k_sq(), pwr_spec_k(), pwr_spec_k_init(), and vel_pwr_spec_k().

21 {
22  k_vec[0] = index / ((N/2 + 1)*N);
23  k_vec[1] = (index / (N/2 + 1)) % N;
24  k_vec[2] = index % (N/2 + 1);
25 
26  for (unsigned int i = 0; i<2; i++) if (k_vec[i] > N/2) k_vec[i] -= N; // k_vec[2] is ALWAYS less or equal than N/2 (real FFTW)
27 }
void get_k_vec ( size_t  N,
size_t  index,
Vec_3D< int > &  k_vec 
)

Definition at line 29 of file core_mesh.cpp.

30 {
31  k_vec[0] = index / ((N/2 + 1)*N);
32  k_vec[1] = (index / (N/2 + 1)) % N;
33  k_vec[2] = index % (N/2 + 1);
34 
35  for (unsigned int i = 0; i<2; i++) if (k_vec[i] > N/2) k_vec[i] -= N; // k_vec[2] is ALWAYS less or equal than N/2 (real FFTW)
36 }
template<typename T >
static std::enable_if<std::is_integral<T>::value, T>::type get_per ( vec,
size_t  per 
)
static

Definition at line 48 of file core_mesh.cpp.

References Catch::Generators::value().

Referenced by fftw_execute_dft_c2r_triple(), get_per(), set_pert_pos(), stream_kick_stream(), and TEST_CASE().

49 {
50  static_assert(std::is_integral<T>::value, "Integral required.");
51  if ((vec >= (int)per) || (vec < 0) ){
52  vec %= (int)per;
53  return (vec < 0) ? vec + (int)per : vec;
54  }
55  else return vec;
56 }
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename T >
static std::enable_if<!std::is_integral<T>::value, T>::type get_per ( vec,
size_t  per 
)
static

Definition at line 59 of file core_mesh.cpp.

References Catch::Generators::value().

60 {
61  static_assert(!std::is_integral<T>::value, "Non-integral required.");
62  return ((vec >= per) || (vec < 0) ) ? vec - per * floor( vec / per ) : vec;
63 }
auto value(T const &val) -> Generator< T >
Definition: catch.hpp:3177
template<typename T >
void get_per ( Vec_3D< T > &  position,
size_t  per 
)

Definition at line 66 of file core_mesh.cpp.

References get_per(), and growth_allz::T.

Referenced by Mesh_base< size_t >::operator()(), and Mesh::operator()().

67 {
68  for (T& pos : position) pos = get_per(pos, per);
69 }
static std::enable_if< std::is_integral< T >::value, T >::type get_per(T vec, size_t per)
Definition: core_mesh.cpp:48
template<typename T >
void get_per ( Vec_3D< T > &  position,
size_t  perx,
size_t  pery,
size_t  perz 
)

Definition at line 72 of file core_mesh.cpp.

References get_per().

73 {
74  position[0] = get_per(position[0], perx);
75  position[1] = get_per(position[1], pery);
76  position[2] = get_per(position[2], perz);
77 }
static std::enable_if< std::is_integral< T >::value, T >::type get_per(T vec, size_t per)
Definition: core_mesh.cpp:48
void get_per ( std::vector< Particle_v< double >> &  particles,
const size_t  per 
)

Definition at line 79 of file core_mesh.cpp.

References get_per().

80 {
81  const size_t Np = particles.size();
82  #pragma omp parallel for
83  for (size_t i = 0; i < Np; i++)
84  {
85  get_per(particles[i].position, per);
86  }
87 }
static std::enable_if< std::is_integral< T >::value, T >::type get_per(T vec, size_t per)
Definition: core_mesh.cpp:48
template void get_per ( Vec_3D< int > &  ,
size_t   
)
template void get_per ( Vec_3D< int > &  ,
size_t  ,
size_t  ,
size_t   
)
template void get_per ( Vec_3D< size_t > &  ,
size_t   
)
template void get_per ( Vec_3D< size_t > &  ,
size_t  ,
size_t  ,
size_t   
)
template void get_per ( Vec_3D< double > &  ,
size_t   
)
template void get_per ( Vec_3D< double > &  ,
size_t  ,
size_t  ,
size_t   
)
Vec_3D<double> get_sgn_distance ( const Vec_3D< double > &  x_from,
const Vec_3D< double > &  x_to,
size_t  per 
)

Definition at line 122 of file core_mesh.cpp.

References get_sgn_distance_1D(), wgh_sch(), and x.

Referenced by anonymous_namespace{mod_frozen_potential.cpp}::force_short().

123 {
124  Vec_3D<FTYPE_t> dst;
125  for (unsigned int i = 0; i < 3; i++) dst[i] = get_sgn_distance_1D(x_from[i], x_to[i], per);
126  return dst;
127 }
T get_sgn_distance_1D(T x_from, T x_to, size_t per)
Definition: core_mesh.cpp:104
template<typename T >
T get_sgn_distance_1D ( x_from,
x_to,
size_t  per 
)
inline

Definition at line 104 of file core_mesh.cpp.

References sgn(), and growth_allz::T.

Referenced by get_sgn_distance().

105 { // return signed (oriented) distance, e.g. x_to - x_from (no periodicity)
106  // periodicity makes this a little bit trickier
107  T d = x_from - x_to;
108  if (std::abs(d) <= per / 2) return d; // most probable, first condition
109  else if (std::abs(d) <= per) return d-sgn(d)*per;
110  else d = fmod(d, T(per)); // fmod unlikely to evaluate, speed up code
111  if (std::abs(d) <= per / 2) return d;
112  else return d-sgn(d)*per;
113 }
static int sgn(T val)
Definition: core_mesh.cpp:15
template<typename T >
static int sgn ( val)
static

Definition at line 15 of file core_mesh.cpp.

References growth_allz::T.

Referenced by get_sgn_distance_1D(), and TEST_CASE().

16 {
17  return (T(0) < val) - (val < T(0));
18 }
template<size_t N>
static double wgh_sch ( const Vec_3D< double > &  x,
const Vec_3D< int > &  y,
size_t  mesh_num 
)
static

Referenced by get_sgn_distance().

template<>
double wgh_sch< 1 > ( const Vec_3D< double > &  x,
const Vec_3D< int > &  y,
size_t  mesh_num 
)

Definition at line 140 of file core_mesh.cpp.

References get_distance_1D(), and w.

141 { // CIC: Cloud in cells
142  FTYPE_t dx, w = 1;
143  for (unsigned int i = 0; i < 3; i++)
144  {
145  dx = get_distance_1D(x[i], y[i], mesh_num);
146  if (dx > 1) return 0;
147  else w *= 1 - dx;
148  }
149  return w;
150 }
T get_distance_1D(const T x_1, const T x_2, const size_t per)
Definition: core_mesh.cpp:90
static double w[2][28][111]
Definition: ccl_emu17.c:33
template<>
double wgh_sch< 2 > ( const Vec_3D< double > &  x,
const Vec_3D< int > &  y,
size_t  mesh_num 
)

Definition at line 152 of file core_mesh.cpp.

References get_distance_1D(), pow2(), and w.

153 { // TSC: Triangular shaped clouds
154  FTYPE_t dx, w = 1;
155  for (unsigned int i = 0; i < 3; i++)
156  {
157  dx = get_distance_1D(x[i], y[i], mesh_num);
158  if (dx > 1.5) return 0;
159  else if (dx > 0.5) w *= pow2(1.5 - dx) / 2;
160  else w *= 3 / FTYPE_t(4) - dx*dx;
161  }
162  return w;
163 }
T get_distance_1D(const T x_1, const T x_2, const size_t per)
Definition: core_mesh.cpp:90
T pow2(T base)
Definition: precision.hpp:52
static double w[2][28][111]
Definition: ccl_emu17.c:33