1#ifndef PARTICLEREFLECTION_HEADER
2#define PARTICLEREFLECTION_HEADER
49template <
int N,
typename... Ts>
50using NthTypeOf =
typename std::tuple_element<N, std::tuple<Ts...>>::type;
51template <
int I,
class... Ts>
53 return std::get<I>(std::forward_as_tuple(ts...));
65 namespace SFINAE_DETAIL {
74#define SFINAE_STRUCT(name, getmethod) \
75 template <typename T, typename = void> \
76 struct Has##name##_address : std::false_type {}; \
77 template <typename T> \
78 struct Has##name##_address<T, std::void_t<decltype(&T::getmethod)>> : std::true_type {}; \
79 template <typename T, typename = void> \
80 struct Has##name##_call0 : std::false_type {}; \
81 template <typename T> \
82 struct Has##name##_call0<T, std::void_t<decltype(std::declval<T &>().getmethod())>> : std::true_type {}; \
83 template <typename T, typename = void> \
84 struct Has##name##_call1 : std::false_type {}; \
85 template <typename T> \
86 struct Has##name##_call1< \
88 std::void_t<decltype(std::declval<T &>().getmethod(std::declval<SFINAE_DETAIL::AnyArgument>()))>> \
89 : std::true_type {}; \
90 template <typename T> \
92 static constexpr bool value = \
93 Has##name##_address<T>::value or Has##name##_call0<T>::value or Has##name##_call1<T>::value; \
96#define SFINAE_HAS(name, getmethod) \
98 constexpr bool has_##getmethod() { \
99 return Has##name<T>::value; \
102#define SFINAE_GET(name, getmethod) \
103 template <typename T, typename B = typename std::enable_if<Has##name<T>::value>::type, class... Args> \
104 auto name(T & p, Args... args) { \
105 return p.getmethod(args...); \
108#define SFINAE_CONSTEXPR_GET(name, getmethod) \
109 template <typename T, typename B = typename std::enable_if<Has##name<T>::value>::type, class... Args> \
110 constexpr auto name(const T & p, Args... args) { \
111 return p.getmethod(args...); \
114#define SFINAE_SET(name, setmethod) \
115 template <typename T, typename B = typename std::enable_if<Has##name<T>::value>::type, class... Args> \
116 auto name(T & p, Args... args) { \
117 return p.setmethod(args...); \
120#define SFINAE_TEST_GET(name, getmethod) \
121 SFINAE_STRUCT(name, getmethod) \
122 SFINAE_HAS(name, getmethod) \
123 SFINAE_GET(name, getmethod)
125#define SFINAE_CONSTEXPR_TEST_GET(name, getmethod) \
126 SFINAE_STRUCT(name, getmethod) \
127 SFINAE_HAS(name, getmethod) \
128 SFINAE_CONSTEXPR_GET(name, getmethod)
130#define SFINAE_TEST_SET(name, setmethod) \
131 SFINAE_STRUCT(name, setmethod) \
132 SFINAE_HAS(name, setmethod) \
133 SFINAE_SET(name, setmethod)
142 SFINAE_TEST_GET(
GetPos, get_pos)
143 SFINAE_TEST_GET(
GetVel, get_vel)
145 assert_mpi(
false,
"Trying to get position from a particle that has no get_pos method");
149 assert_mpi(
false,
"Trying to get velocity from a particle that has no get_vel method");
156 SFINAE_TEST_GET(
GetID, get_id)
157 SFINAE_TEST_GET(
SetID, set_id)
159 assert_mpi(
false,
"Trying to get id from a particle that has no get_id method");
162 constexpr void SetID(...) { assert_mpi(
false,
"Trying to set id from a particle that has no set_id method"); };
167 SFINAE_TEST_GET(
GetMass, get_mass)
168 SFINAE_TEST_SET(
SetMass, set_mass)
169 template <
class... Args>
170 constexpr double GetMass([[maybe_unused]] Args &... args) {
176 assert_mpi(
false,
"Trying to set mass from a particle that has no set_mass method");
184 SFINAE_TEST_GET(GetDeltafWeight, get_deltaf_weight)
185 template <
class... Args>
186 constexpr double GetDeltafWeight([[maybe_unused]] Args &... args) {
195 template <
class... Args>
196 constexpr double GetVolume([[maybe_unused]] Args &... args) {
202 assert_mpi(
false,
"Trying to set volume for particle that has no set_volume method");
209 SFINAE_CONSTEXPR_TEST_GET(GetNDIM, get_ndim)
210 constexpr int GetNDIM(...) {
211 assert_mpi(
false,
"Particle must have a get_ndim method to signal the dimension");
220 SFINAE_TEST_GET(GetSize, get_particle_byte_size)
227 template <
class... Args>
228 constexpr size_t GetSize([[maybe_unused]] Args &... args) {
230 static_assert(
sizeof...(args) == 1);
231 using ParticleType =
NthTypeOf<0, Args...>;
232 return sizeof(ParticleType);
236 if constexpr (HasAppendToBuffer<T>::value) {
237 t.append_to_buffer(buffer);
241 static_assert(std::is_trivially_copyable_v<T>,
242 "Particle is not trivially copyable (e.g. has a std::vector member): it must provide "
243 "append_to_buffer, assign_from_buffer and get_particle_byte_size");
244 std::memcpy(buffer, &t, GetSize(t));
249 if constexpr (HasAssignFromBuffer<T>::value) {
250 t.assign_from_buffer(buffer);
253 static_assert(std::is_trivially_copyable_v<T>,
254 "Particle is not trivially copyable (e.g. has a std::vector member): it must provide "
255 "append_to_buffer, assign_from_buffer and get_particle_byte_size");
256 std::memcpy(&t, buffer, GetSize(t));
264 SFINAE_TEST_GET(GetD_1LPT, get_D_1LPT)
271 constexpr double * GetD_1LPT(...) {
272 assert_mpi(
false,
"Trying to get D_1LPT from a particle that has no get_D_1LPT method");
276 assert_mpi(
false,
"Trying to get dDdloga_1LPT from a particle that has no get_dDdloga_1LPT method");
280 assert_mpi(
false,
"Trying to get D_2LPT from a particle that has no get_D_2LPT method");
284 assert_mpi(
false,
"Trying to get dDdloga_2LPT from a particle that has no get_dDdloga_2LPT method");
288 assert_mpi(
false,
"Trying to get D_3LPTa from a particle that has no get_D_3LPTa method");
292 assert_mpi(
false,
"Trying to get D_3LPTb from a particle that has no get_D_3LPTb method");
296 assert_mpi(
false,
"Trying to get the Lagrangian coordinate q from a particle that has no get_q method");
303 SFINAE_TEST_GET(
GetFoFID, get_fofid)
304 SFINAE_TEST_SET(
SetFoFID, set_fofid)
306 assert_mpi(
false,
"Trying to get FoFID for particle that has no get_fofid method");
310 assert_mpi(
false,
"Trying to set FoFID for particle that has no set_fofid method");
318 SFINAE_TEST_GET(
GetTag, get_tag)
319 SFINAE_TEST_SET(
SetTag, set_tag)
320 SFINAE_TEST_GET(
GetLevel, get_level)
321 SFINAE_TEST_SET(
SetLevel, set_level)
323 assert_mpi(
false,
"Trying to get family for particle that has no get_family method");
327 assert_mpi(
false,
"Trying to set family for particle that has no set_family method");
330 assert_mpi(
false,
"Trying to get tag for particle that has no get_tag method");
333 constexpr void SetTag(...) { assert_mpi(
false,
"Trying to set tag for particle that has no set_tag method"); }
335 assert_mpi(
false,
"Trying to get level for particle that has no get_level method");
339 assert_mpi(
false,
"Trying to set level for particle that has no set_level method");
345 SFINAE_TEST_GET(
GetRA, get_RA)
346 SFINAE_TEST_SET(
SetRA, set_RA)
347 SFINAE_TEST_GET(
GetDEC, get_DEC)
348 SFINAE_TEST_SET(
SetDEC, set_DEC)
354 assert_mpi(
false,
"Trying to get RA from a particle that has no get_RA method");
358 assert_mpi(
false,
"Trying to set RA from a particle that has no set_RA method");
362 assert_mpi(
false,
"Trying to get DEC from a particle that has no get_DEC method");
366 assert_mpi(
false,
"Trying to set DEC from a particle that has no set_DEC method");
370 assert_mpi(
false,
"Trying to get Redshift from a particle that has no get_z method");
374 assert_mpi(
false,
"Trying to set Redshift from a particle that has no set_z method");
377 template <
class... Args>
378 constexpr double GetWeight([[maybe_unused]] Args &... args) {
384 assert_mpi(
false,
"Trying to set weight from a particle that has no set_weight method");
391 constexpr int N = FML::PARTICLE::GetNDIM(tmp);
393 std::cout <<
"#=====================================================\n";
395 std::cout <<
"# .___ _____ \n";
396 std::cout <<
"# | | _____/ ____\\____ \n";
397 std::cout <<
"# | |/ \\ __\\/ _ \\ \n";
398 std::cout <<
"# | | | \\ | ( <_> ) \n";
399 std::cout <<
"# |___|___| /__| \\____/ \n";
400 std::cout <<
"# \\/ \n";
402 std::cout <<
"# Information about (an empty) particle of the given type:\n";
403 std::cout <<
"# Below we only show info about methods we have implemented support for\n";
405 if constexpr (FML::PARTICLE::has_append_to_buffer<T>())
406 std::cout <<
"# Particle has custom communication append_to_buffer method\n";
408 std::cout <<
"# Particle uses fiducial communication append_to_buffer method (assumes no dynamic "
409 "alloc inside class)\n";
411 if constexpr (FML::PARTICLE::has_assign_from_buffer<T>())
412 std::cout <<
"# Particle has custom communication assign_from_buffer method\n";
414 std::cout <<
"# Particle uses fiducial communication assign_from_buffer method (assumes no dynamic "
415 "alloc inside class)\n";
417 if constexpr (FML::PARTICLE::has_get_particle_byte_size<T>())
418 std::cout <<
"# Particle has custom size method. Size of an empty particle is "
419 << FML::PARTICLE::GetSize(tmp) <<
" bytes\n";
421 std::cout <<
"# Particle uses fiducial size method. Size of particle is "
422 << FML::PARTICLE::GetSize(tmp) <<
" bytes\n";
423 std::cout <<
"# Dimension is " << N <<
"\n";
425 if constexpr (FML::PARTICLE::has_get_pos<T>())
429 if constexpr (FML::PARTICLE::has_get_vel<T>())
433 if constexpr (FML::PARTICLE::has_get_mass<T>())
436 if constexpr (FML::PARTICLE::has_set_id<T>())
439 if constexpr (FML::PARTICLE::has_set_volume<T>())
443 if constexpr (FML::PARTICLE::has_set_tag<T>())
445 if constexpr (FML::PARTICLE::has_set_family<T>())
447 if constexpr (FML::PARTICLE::has_set_level<T>())
451 if constexpr (FML::PARTICLE::has_set_RA<T>())
453 if constexpr (FML::PARTICLE::has_set_DEC<T>())
455 if constexpr (FML::PARTICLE::has_set_z<T>())
458 if constexpr (FML::PARTICLE::has_set_weight<T>())
462 if constexpr (FML::PARTICLE::has_get_D_1LPT<T>())
463 std::cout <<
"# Particle has [1LPT Displacement field] ("
464 <<
sizeof(FML::PARTICLE::GetD_1LPT(tmp)[0]) * N <<
" bytes)\n";
465 if constexpr (FML::PARTICLE::has_get_dDdloga_1LPT<T>())
466 std::cout <<
"# Particle has [1LPT Displacement field derivative] ("
468 if constexpr (FML::PARTICLE::has_get_D_2LPT<T>())
469 std::cout <<
"# Particle has [2LPT Displacement field] ("
471 if constexpr (FML::PARTICLE::has_get_dDdloga_2LPT<T>())
472 std::cout <<
"# Particle has [2LPT Displacement field derivative] ("
474 if constexpr (FML::PARTICLE::has_get_D_3LPTa<T>())
475 std::cout <<
"# Particle has [3LPTa Displacement field] ("
477 if constexpr (FML::PARTICLE::has_get_D_3LPTb<T>())
478 std::cout <<
"# Particle has [3LPTb Displacement field] ("
480 if constexpr (FML::PARTICLE::has_get_q<T>())
481 std::cout <<
"# Particle has [Lagrangian position] ("
483 if constexpr (FML::PARTICLE::has_get_D_1LPT<T>() and FML::PARTICLE::has_get_D_2LPT<T>() and
484 FML::PARTICLE::has_get_D_3LPTa<T>() and FML::PARTICLE::has_get_D_3LPTb<T>()) {
485 std::cout <<
"# Particle compatible with 3LPT COLA\n";
487 }
else if (FML::PARTICLE::has_get_D_1LPT<T>() and FML::PARTICLE::has_get_D_2LPT<T>()) {
488 std::cout <<
"# Particle compatible with 2LPT COLA\n";
489 }
else if (FML::PARTICLE::has_get_D_1LPT<T>()) {
490 std::cout <<
"# Particle compatible with 1LPT COLA\n";
492 std::cout <<
"# Particle is not compatible with COLA\n";
494 if constexpr (FML::PARTICLE::has_get_q<T>() and FML::PARTICLE::has_get_D_1LPT<T>() and
495 FML::PARTICLE::has_get_dDdloga_1LPT<T>()) {
496 std::cout <<
"# Particle compatible with 1LPT scaledependent COLA\n";
497 }
else if constexpr (FML::PARTICLE::has_get_q<T>() and FML::PARTICLE::has_get_D_1LPT<T>() and
498 FML::PARTICLE::has_get_D_2LPT<T>()) {
499 std::cout <<
"# Particle compatible with 2+ LPT scaledependent COLA\n";
501 std::cout <<
"# Particle is not compatible with scaledependent COLA\n";
505 std::cout <<
"#=====================================================\n";
535 template <
class T,
int NDIM>
538 const std::array<double, NDIM> & shift,
542#pragma omp parallel for
544 for (
size_t i = 0; i < NumPart; i++) {
546 for (
int idim = 0; idim <
NDIM; idim++) {
547 pos[idim] += sign * shift[idim];
548 const bool wrap = (wrap_type == 1) or (wrap_type == 2 and idim > 0);
557 template <
class T,
int NDIM>
559 using PosType = std::remove_cv_t<std::remove_reference_t<
decltype(
FML::PARTICLE::GetPos(part[0])[0])>>;
560 std::vector<PosType> saved(NumPart *
NDIM);
562#pragma omp parallel for
564 for (
size_t i = 0; i < NumPart; i++) {
566 for (
int idim = 0; idim <
NDIM; idim++)
567 saved[i *
NDIM + idim] = pos[idim];
573 template <
class T,
int NDIM,
class U>
575 assert_mpi(saved.size() == NumPart *
NDIM,
576 "[restore_particle_positions] The number of saved positions does not match NumPart\n");
578#pragma omp parallel for
580 for (
size_t i = 0; i < NumPart; i++) {
582 for (
int idim = 0; idim <
NDIM; idim++)
583 pos[idim] = saved[i *
NDIM + idim];
590 if constexpr (not FML::PARTICLE::has_get_q<T>()) {
596 constexpr int N = FML::PARTICLE::GetNDIM(tmp);
598#pragma omp parallel for
600 for (
size_t ind = 0; ind < NumPart; ind++) {
603 for (
int idim = 0; idim < N; idim++) {
604 auto tmp_pos = pos_eulerian[idim];
605 pos_eulerian[idim] = pos_lagrangian[idim];
606 pos_lagrangian[idim] = tmp_pos;
619#undef SFINAE_TEST_GET
620#undef SFINAE_TEST_SET
621#undef SFINAE_CONSTEXPR_GET
622#undef SFINAE_CONSTEXPR_TEST_GET
typename std::tuple_element< N, std::tuple< Ts... > >::type NthTypeOf
decltype(auto) get_NthArgOf(Ts &&... ts)
This namespace deals with interpolation and density assignment.
U wrap_coordinate_periodic(U x)
Wrap a coordinate periodically into [0,1) assuming it is in [-1,2) (i.e.
get_vel constexpr double * GetPos(...)
constexpr double GetDEC(...)
set_fofid constexpr size_t GetFoFID(...)
set_volume constexpr double GetVolume(Args &... args)
constexpr void SetWeight(...)
void AssignFromBuffer(T &t, char *buffer)
constexpr void SetTag(...)
constexpr double * GetLagrangianPos(...)
constexpr double SetRA(...)
constexpr double * GetD_3LPTa(...)
constexpr double * GetdDdloga_2LPT(...)
set_family set_tag set_level constexpr char GetFamily(...)
constexpr void SetLevel(...)
constexpr void SetFoFID(...)
constexpr void SetMass(...)
constexpr double * GetdDdloga_1LPT(...)
void restore_particle_positions(T *part, size_t NumPart, const std::vector< U > &saved)
Restore the positions saved by save_particle_positions (bit-for-bit)
constexpr void SetVolume(...)
auto save_particle_positions(T *part, size_t NumPart)
Copy of the positions of a set of particles (NDIM values per particle) so that they can be restored e...
constexpr double SetDEC(...)
set_id constexpr int GetID(...)
void AppendToBuffer(T &t, char *buffer)
constexpr double * GetVel(...)
set_RA set_DEC set_z set_weight constexpr double GetRA(...)
constexpr double * GetD_3LPTb(...)
constexpr void SetFamily(...)
constexpr double GetRedshift(...)
constexpr double * GetD_2LPT(...)
constexpr double SetRedshift(...)
set_mass constexpr double GetMass(Args &... args)
constexpr double GetWeight(Args &... args)
constexpr void SetID(...)
constexpr int GetLevel(...)
void swap_eulerian_and_lagrangian_positions(T *part, size_t NumPart)
void shift_particles(T *part, size_t NumPart, const std::array< double, NDIM > &shift, double sign=1.0, int wrap_type=1)
Shift the positions of a set of particles: .
constexpr char GetTag(...)
int ThisTask
The MPI task number.