FML
Loading...
Searching...
No Matches
ReflectOnParticleMethods.h
Go to the documentation of this file.
1#ifndef PARTICLEREFLECTION_HEADER
2#define PARTICLEREFLECTION_HEADER
3
4#include <array>
5#include <cassert>
6#include <cstring>
7#include <iostream>
8#include <tuple>
9#include <type_traits>
10#include <utility>
11#include <vector>
12
13#include <FML/Global/Global.h>
14
15//=======================================================================
16//
17// This file contains a bit of meta programming nonsense we need in the library
18// It's a convoluted (because there is no other good) way of checking if a class
19// has a given method and return that. If it doesn't exist we need to provide a
20// fiducial method for it for the code to compile. In algorithms always test
21// if a method is available by calling FML::PARTICLE::has_##method##() and
22// only if this is true call FML::PARTICLE::Get##method##(p) to fetch the quantity
23//
24// The only methods that are truly needed are [get_pos] and [get_ndim].
25// We exit with an error if a method that doesn't exist is used
26// The exception so far is [get_mass] and [get_volume]. These return the
27// fiducial value 1.0 (same for all particles)
28//
29// Example:
30// struct Particle { double pos[3]; double *get_pos(){ return pos; } };
31// Particle t;
32// auto pos = FML::PARTICLE::GetPos(t); // Pointer to position
33// auto vel = FML::PARTICLE::GetVel(t); // nullptr (fiducial value)
34// auto mass = FML::PARTICLE::GetMass(t); // 1.0 (fiducial value)
35// auto vol = FML::PARTICLE::GetVol(t); // 1.0 (fiducial value)
36// std::cout << std::boolalpha << FML::PARTICLE::has_get_pos<Particle>() << " get_pos\n"; // true
37// std::cout << std::boolalpha << FML::PARTICLE::has_get_vel<Particle>() << " get_vel\n"; // false
38// std::cout << std::boolalpha << FML::PARTICLE::has_get_mass<Particle>() << " get_mass\n"; // false
39//
40// TODO: change the methods below and use static_assert so that it gives a compile error
41// instead of a runtime error if trying to use a method that do not exist, e.g. we can add on
42// the template name and use something silly like (false directly will not work as we need something
43// that only gets evaluated if instantiated and not when parsing the code):
44// static_assert(sizeof...(Args) < 0, "Trying to set Redshift from a particle that has no set_z method");
45//
46//=======================================================================
47
48// Get the type and the value of the Nth argument in a parameter pack
49template <int N, typename... Ts>
50using NthTypeOf = typename std::tuple_element<N, std::tuple<Ts...>>::type;
51template <int I, class... Ts>
52decltype(auto) get_NthArgOf(Ts &&... ts) {
53 return std::get<I>(std::forward_as_tuple(ts...));
54}
55
56//=======================================================================
57// Macro to generate a SFINAE test to figure out if Particle has a given method
58// and provide a Get method for that quantity. This is useful for making general
59// algorithms that don't need a quantity, but can do more stuff if it has that quantity
60//=======================================================================
61// The method counts as present if &C::method is well-formed (a single non-overloaded method) or if it can be
62// called with no arguments or with one argument (overloaded e.g. const and non-const methods, template methods)
63namespace FML {
64 namespace PARTICLE {
65 namespace SFINAE_DETAIL {
66 /// An argument convertible to anything (to detect methods that take one argument, e.g. setters)
67 struct AnyArgument {
68 template <class U>
69 operator U() const;
70 };
71 } // namespace SFINAE_DETAIL
72 } // namespace PARTICLE
73} // namespace FML
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< \
87 T, \
88 std::void_t<decltype(std::declval<T &>().getmethod(std::declval<SFINAE_DETAIL::AnyArgument>()))>> \
89 : std::true_type {}; \
90 template <typename T> \
91 struct Has##name { \
92 static constexpr bool value = \
93 Has##name##_address<T>::value or Has##name##_call0<T>::value or Has##name##_call1<T>::value; \
94 };
95
96#define SFINAE_HAS(name, getmethod) \
97 template <class T> \
98 constexpr bool has_##getmethod() { \
99 return Has##name<T>::value; \
100 }
101
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...); \
106 }
107
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...); \
112 }
113
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...); \
118 }
119
120#define SFINAE_TEST_GET(name, getmethod) \
121 SFINAE_STRUCT(name, getmethod) \
122 SFINAE_HAS(name, getmethod) \
123 SFINAE_GET(name, getmethod)
124
125#define SFINAE_CONSTEXPR_TEST_GET(name, getmethod) \
126 SFINAE_STRUCT(name, getmethod) \
127 SFINAE_HAS(name, getmethod) \
128 SFINAE_CONSTEXPR_GET(name, getmethod)
129
130#define SFINAE_TEST_SET(name, setmethod) \
131 SFINAE_STRUCT(name, setmethod) \
132 SFINAE_HAS(name, setmethod) \
133 SFINAE_SET(name, setmethod)
134
135namespace FML {
136 namespace PARTICLE {
137
138 //=====================================================================
139 // Position and velocity
140 // Return (non-const) pointer to first element so no set method needed
141 //=====================================================================
142 SFINAE_TEST_GET(GetPos, get_pos)
143 SFINAE_TEST_GET(GetVel, get_vel)
144 constexpr double * GetPos(...) {
145 assert_mpi(false, "Trying to get position from a particle that has no get_pos method");
146 return nullptr;
147 };
148 constexpr double * GetVel(...) {
149 assert_mpi(false, "Trying to get velocity from a particle that has no get_vel method");
150 return nullptr;
151 };
152
153 //=====================================================================
154 // ID of particle
155 //=====================================================================
156 SFINAE_TEST_GET(GetID, get_id)
157 SFINAE_TEST_GET(SetID, set_id)
158 constexpr int GetID(...) {
159 assert_mpi(false, "Trying to get id from a particle that has no get_id method");
160 return -1;
161 };
162 constexpr void SetID(...) { assert_mpi(false, "Trying to set id from a particle that has no set_id method"); };
163
164 //=====================================================================
165 // Mass of particle
166 //=====================================================================
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) {
171 // Optional to have this. All particles having equal mass is the fiducial case
172 // NB: not using ... to avoid copies of particle being made when this is called
173 return 1.0;
174 };
175 constexpr void SetMass(...) {
176 assert_mpi(false, "Trying to set mass from a particle that has no set_mass method");
177 };
178
179 //=====================================================================
180 // delta-f weight of a particle (e.g. neutrinos). If a particle has this then the density assignment
181 // (particles_to_grid) gives delta(x) = Nmesh^N / NumPartTot * sum_i w_i W(x - x_i), i.e. the particles
182 // only carry the perturbation (no mean density is subtracted)
183 //=====================================================================
184 SFINAE_TEST_GET(GetDeltafWeight, get_deltaf_weight)
185 template <class... Args>
186 constexpr double GetDeltafWeight([[maybe_unused]] Args &... args) {
187 return 1.0;
188 };
189
190 //=====================================================================
191 // Volume of particle
192 //=====================================================================
193 SFINAE_TEST_GET(GetVolume, get_volume)
194 SFINAE_TEST_SET(SetVolume, set_volume)
195 template <class... Args>
196 constexpr double GetVolume([[maybe_unused]] Args &... args) {
197 // Optional to have this. All particles having equal volume is the fiducial case
198 // NB: not using ... to avoid copies of particle being made when this is called
199 return 1.0;
200 };
201 constexpr void SetVolume(...) {
202 assert_mpi(false, "Trying to set volume for particle that has no set_volume method");
203 };
204
205 //=====================================================================
206 // Dimension we are working in
207 // This should be constexpr to allow for compiletime evaluation of it
208 //=====================================================================
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");
212 return 3;
213 };
214
215 //=====================================================================
216 // Fiducial methods for communication
217 // If you have dynamic allocated methods in a class then you must
218 // provide these methods yourself in the class
219 //=====================================================================
220 SFINAE_TEST_GET(GetSize, get_particle_byte_size)
221 // AppendToBuffer / AssignFromBuffer: use the particle's own method if it has one, otherwise copy the raw
222 // bytes (a single function each, so that a particle with custom methods gives no ambiguous overloads)
223 SFINAE_STRUCT(AppendToBuffer, append_to_buffer)
224 SFINAE_HAS(AppendToBuffer, append_to_buffer)
225 SFINAE_STRUCT(AssignFromBuffer, assign_from_buffer)
226 SFINAE_HAS(AssignFromBuffer, assign_from_buffer)
227 template <class... Args>
228 constexpr size_t GetSize([[maybe_unused]] Args &... args) {
229 // Fiducial method. Assumes no dynamic allocated memory in class
230 static_assert(sizeof...(args) == 1);
231 using ParticleType = NthTypeOf<0, Args...>;
232 return sizeof(ParticleType);
233 };
234 template <class T>
235 void AppendToBuffer(T & t, char * buffer) {
236 if constexpr (HasAppendToBuffer<T>::value) {
237 t.append_to_buffer(buffer);
238 } else {
239 // Fiducial method for communication: a raw copy of the bytes. A particle that owns memory (e.g. a
240 // std::vector member) must provide append_to_buffer, assign_from_buffer and get_particle_byte_size
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));
245 }
246 };
247 template <class T>
248 void AssignFromBuffer(T & t, char * buffer) {
249 if constexpr (HasAssignFromBuffer<T>::value) {
250 t.assign_from_buffer(buffer);
251 } else {
252 // Fiducial method for communication: a raw copy of the bytes (see AppendToBuffer)
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));
257 }
258 };
259
260 //=====================================================================
261 // Lagrangian perturbation theory (Displacement fields and Lagrangian coord)
262 // Returns (non-const) pointer to first element so no set method needed
263 //=====================================================================
264 SFINAE_TEST_GET(GetD_1LPT, get_D_1LPT)
265 SFINAE_TEST_GET(GetD_2LPT, get_D_2LPT)
266 SFINAE_TEST_GET(GetD_3LPTa, get_D_3LPTa)
267 SFINAE_TEST_GET(GetD_3LPTb, get_D_3LPTb)
268 SFINAE_TEST_GET(GetdDdloga_1LPT, get_dDdloga_1LPT)
269 SFINAE_TEST_GET(GetdDdloga_2LPT, get_dDdloga_2LPT)
270 SFINAE_TEST_GET(GetLagrangianPos, get_q)
271 constexpr double * GetD_1LPT(...) {
272 assert_mpi(false, "Trying to get D_1LPT from a particle that has no get_D_1LPT method");
273 return nullptr;
274 };
275 constexpr double * GetdDdloga_1LPT(...) {
276 assert_mpi(false, "Trying to get dDdloga_1LPT from a particle that has no get_dDdloga_1LPT method");
277 return nullptr;
278 };
279 constexpr double * GetD_2LPT(...) {
280 assert_mpi(false, "Trying to get D_2LPT from a particle that has no get_D_2LPT method");
281 return nullptr;
282 };
283 constexpr double * GetdDdloga_2LPT(...) {
284 assert_mpi(false, "Trying to get dDdloga_2LPT from a particle that has no get_dDdloga_2LPT method");
285 return nullptr;
286 };
287 constexpr double * GetD_3LPTa(...) {
288 assert_mpi(false, "Trying to get D_3LPTa from a particle that has no get_D_3LPTa method");
289 return nullptr;
290 };
291 constexpr double * GetD_3LPTb(...) {
292 assert_mpi(false, "Trying to get D_3LPTb from a particle that has no get_D_3LPTb method");
293 return nullptr;
294 };
295 constexpr double * GetLagrangianPos(...) {
296 assert_mpi(false, "Trying to get the Lagrangian coordinate q from a particle that has no get_q method");
297 return nullptr;
298 };
299
300 //=====================================================================
301 // Halo finding
302 //=====================================================================
303 SFINAE_TEST_GET(GetFoFID, get_fofid)
304 SFINAE_TEST_SET(SetFoFID, set_fofid)
305 constexpr size_t GetFoFID(...) {
306 assert_mpi(false, "Trying to get FoFID for particle that has no get_fofid method");
307 return 0;
308 }
309 constexpr void SetFoFID(...) {
310 assert_mpi(false, "Trying to set FoFID for particle that has no set_fofid method");
311 }
312
313 //=====================================================================
314 // Ramses related methods
315 //=====================================================================
316 SFINAE_TEST_GET(GetFamily, get_family)
317 SFINAE_TEST_SET(SetFamily, set_family)
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)
322 constexpr char GetFamily(...) {
323 assert_mpi(false, "Trying to get family for particle that has no get_family method");
324 return 0;
325 }
326 constexpr void SetFamily(...) {
327 assert_mpi(false, "Trying to set family for particle that has no set_family method");
328 }
329 constexpr char GetTag(...) {
330 assert_mpi(false, "Trying to get tag for particle that has no get_tag method");
331 return 0;
332 }
333 constexpr void SetTag(...) { assert_mpi(false, "Trying to set tag for particle that has no set_tag method"); }
334 constexpr int GetLevel(...) {
335 assert_mpi(false, "Trying to get level for particle that has no get_level method");
336 return 0;
337 }
338 constexpr void SetLevel(...) {
339 assert_mpi(false, "Trying to set level for particle that has no set_level method");
340 }
341
342 //=====================================================================
343 // Galaxies and paircounting
344 //=====================================================================
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)
349 SFINAE_TEST_GET(GetRedshift, get_z)
350 SFINAE_TEST_SET(SetRedshift, set_z)
351 SFINAE_TEST_GET(GetWeight, get_weight)
352 SFINAE_TEST_SET(SetWeight, set_weight)
353 constexpr double GetRA(...) {
354 assert_mpi(false, "Trying to get RA from a particle that has no get_RA method");
355 return 0.0;
356 }
357 constexpr double SetRA(...) {
358 assert_mpi(false, "Trying to set RA from a particle that has no set_RA method");
359 return 0.0;
360 }
361 constexpr double GetDEC(...) {
362 assert_mpi(false, "Trying to get DEC from a particle that has no get_DEC method");
363 return 0.0;
364 }
365 constexpr double SetDEC(...) {
366 assert_mpi(false, "Trying to set DEC from a particle that has no set_DEC method");
367 return 0.0;
368 }
369 constexpr double GetRedshift(...) {
370 assert_mpi(false, "Trying to get Redshift from a particle that has no get_z method");
371 return 0.0;
372 }
373 constexpr double SetRedshift(...) {
374 assert_mpi(false, "Trying to set Redshift from a particle that has no set_z method");
375 return 0.0;
376 }
377 template <class... Args>
378 constexpr double GetWeight([[maybe_unused]] Args &... args) {
379 // Optional to have this. All particles having the same weight is the fiducial case
380 // NB: not using ... to avoid copies of particle being made when this is called
381 return 1.0;
382 }
383 constexpr void SetWeight(...) {
384 assert_mpi(false, "Trying to set weight from a particle that has no set_weight method");
385 }
386
387 template <class T>
388 void info() {
389 if (FML::ThisTask == 0) {
390 T tmp{};
391 constexpr int N = FML::PARTICLE::GetNDIM(tmp);
392 std::cout << "\n";
393 std::cout << "#=====================================================\n";
394 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";
401 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";
404
405 if constexpr (FML::PARTICLE::has_append_to_buffer<T>())
406 std::cout << "# Particle has custom communication append_to_buffer method\n";
407 else
408 std::cout << "# Particle uses fiducial communication append_to_buffer method (assumes no dynamic "
409 "alloc inside class)\n";
410
411 if constexpr (FML::PARTICLE::has_assign_from_buffer<T>())
412 std::cout << "# Particle has custom communication assign_from_buffer method\n";
413 else
414 std::cout << "# Particle uses fiducial communication assign_from_buffer method (assumes no dynamic "
415 "alloc inside class)\n";
416
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";
420 else
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";
424
425 if constexpr (FML::PARTICLE::has_get_pos<T>())
426 std::cout << "# Particle has [Position] (" << sizeof(FML::PARTICLE::GetPos(tmp)[0]) * N
427 << " bytes)\n";
428
429 if constexpr (FML::PARTICLE::has_get_vel<T>())
430 std::cout << "# Particle has [Velocity] (" << sizeof(FML::PARTICLE::GetVel(tmp)[0]) * N
431 << " bytes)\n";
432
433 if constexpr (FML::PARTICLE::has_get_mass<T>())
434 std::cout << "# Particle has [Mass] (" << sizeof(FML::PARTICLE::GetMass(tmp)) << " bytes)\n";
435
436 if constexpr (FML::PARTICLE::has_set_id<T>())
437 std::cout << "# Particle has [ID] (" << sizeof(FML::PARTICLE::GetID(tmp)) << " bytes)\n";
438
439 if constexpr (FML::PARTICLE::has_set_volume<T>())
440 std::cout << "# Particle has [Volume] (" << sizeof(FML::PARTICLE::GetVolume(tmp)) << " bytes)\n";
441
442 // Ramses specific things
443 if constexpr (FML::PARTICLE::has_set_tag<T>())
444 std::cout << "# Particle has [Tag] (" << sizeof(FML::PARTICLE::GetTag(tmp)) << " bytes)\n";
445 if constexpr (FML::PARTICLE::has_set_family<T>())
446 std::cout << "# Particle has [Family] (" << sizeof(FML::PARTICLE::GetFamily(tmp)) << " bytes)\n";
447 if constexpr (FML::PARTICLE::has_set_level<T>())
448 std::cout << "# Particle has [Level] (" << sizeof(FML::PARTICLE::GetLevel(tmp)) << " bytes)\n";
449
450 // Galaxy and paircount specific things
451 if constexpr (FML::PARTICLE::has_set_RA<T>())
452 std::cout << "# Particle has [RA] (" << sizeof(FML::PARTICLE::GetRA(tmp)) << " bytes)\n";
453 if constexpr (FML::PARTICLE::has_set_DEC<T>())
454 std::cout << "# Particle has [DEC] (" << sizeof(FML::PARTICLE::GetDEC(tmp)) << " bytes)\n";
455 if constexpr (FML::PARTICLE::has_set_z<T>())
456 std::cout << "# Particle has [Redshift] (" << sizeof(FML::PARTICLE::GetRedshift(tmp))
457 << " bytes)\n";
458 if constexpr (FML::PARTICLE::has_set_weight<T>())
459 std::cout << "# Particle has [Weight] (" << sizeof(FML::PARTICLE::GetWeight(tmp)) << " bytes)\n";
460
461 // LPT specific things
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] ("
467 << sizeof(FML::PARTICLE::GetdDdloga_1LPT(tmp)[0]) * N << " bytes)\n";
468 if constexpr (FML::PARTICLE::has_get_D_2LPT<T>())
469 std::cout << "# Particle has [2LPT Displacement field] ("
470 << sizeof(FML::PARTICLE::GetD_2LPT(tmp)[0]) * N << " bytes)\n";
471 if constexpr (FML::PARTICLE::has_get_dDdloga_2LPT<T>())
472 std::cout << "# Particle has [2LPT Displacement field derivative] ("
473 << sizeof(FML::PARTICLE::GetdDdloga_2LPT(tmp)[0]) * N << " bytes)\n";
474 if constexpr (FML::PARTICLE::has_get_D_3LPTa<T>())
475 std::cout << "# Particle has [3LPTa Displacement field] ("
476 << sizeof(FML::PARTICLE::GetD_3LPTa(tmp)[0]) * N << " bytes)\n";
477 if constexpr (FML::PARTICLE::has_get_D_3LPTb<T>())
478 std::cout << "# Particle has [3LPTb Displacement field] ("
479 << sizeof(FML::PARTICLE::GetD_3LPTb(tmp)[0]) * N << " bytes)\n";
480 if constexpr (FML::PARTICLE::has_get_q<T>())
481 std::cout << "# Particle has [Lagrangian position] ("
482 << sizeof(FML::PARTICLE::GetLagrangianPos(tmp)[0]) * N << " bytes)\n";
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";
486
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";
491 } else {
492 std::cout << "# Particle is not compatible with COLA\n";
493 }
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";
500 } else {
501 std::cout << "# Particle is not compatible with scaledependent COLA\n";
502 }
503
504 std::cout << "#\n";
505 std::cout << "#=====================================================\n";
506 std::cout << "\n";
507 }
508 }
509
510 /// @brief Wrap a coordinate periodically into [0,1) assuming it is in [-1,2) (i.e. it has been shifted by
511 /// less than the boxsize). The result is always in [0,1): x + 1 can round to exactly 1.0 for tiny negative
512 /// x (e.g. -1e-17) and then we use 0.0 (the closest point of [0,1) in the periodic sense).
513 template <class U>
515 if (x >= U(1.0))
516 x -= U(1.0);
517 if (x < U(0.0))
518 x += U(1.0);
519 if (x == U(1.0))
520 x = U(0.0);
521 return x;
522 }
523
524 /// @brief Shift the positions of a set of particles: \f$ x \to x + {\rm sign}\cdot s \f$.
525 /// NB: a shift followed by the opposite shift does not in general give back exactly the same positions
526 /// (floating point round-off, wrapping). To restore the exact original positions save them first with
527 /// save_particle_positions and use restore_particle_positions instead of shifting back.
528 /// @param[in] part Pointer to the first particle
529 /// @param[in] NumPart Number of particles
530 /// @param[in] shift The shift (in units of the boxsize, |shift| < 1)
531 /// @param[in] sign +1 or -1 (to undo a shift)
532 /// @param[in] wrap_type 0: no periodic wrapping, 1: wrap all coordinates into [0,1), 2: wrap all but the
533 /// x-coordinate (so particles stay on the task they belong to; with MPI the grid then needs one extra
534 /// slice on the right if the shift is less than a cell)
535 template <class T, int NDIM>
536 void shift_particles(T * part,
537 size_t NumPart,
538 const std::array<double, NDIM> & shift,
539 double sign = 1.0,
540 int wrap_type = 1) {
541#ifdef USE_OMP
542#pragma omp parallel for
543#endif
544 for (size_t i = 0; i < NumPart; i++) {
545 auto * pos = FML::PARTICLE::GetPos(part[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);
549 if (wrap)
550 pos[idim] = wrap_coordinate_periodic(pos[idim]);
551 }
552 }
553 }
554
555 /// @brief Copy of the positions of a set of particles (NDIM values per particle) so that they can be
556 /// restored exactly with restore_particle_positions (e.g. after shift_particles)
557 template <class T, int NDIM>
558 auto save_particle_positions(T * part, size_t NumPart) {
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);
561#ifdef USE_OMP
562#pragma omp parallel for
563#endif
564 for (size_t i = 0; i < NumPart; i++) {
565 const auto * pos = FML::PARTICLE::GetPos(part[i]);
566 for (int idim = 0; idim < NDIM; idim++)
567 saved[i * NDIM + idim] = pos[idim];
568 }
569 return saved;
570 }
571
572 /// @brief Restore the positions saved by save_particle_positions (bit-for-bit)
573 template <class T, int NDIM, class U>
574 void restore_particle_positions(T * part, size_t NumPart, const std::vector<U> & saved) {
575 assert_mpi(saved.size() == NumPart * NDIM,
576 "[restore_particle_positions] The number of saved positions does not match NumPart\n");
577#ifdef USE_OMP
578#pragma omp parallel for
579#endif
580 for (size_t i = 0; i < NumPart; i++) {
581 auto * pos = FML::PARTICLE::GetPos(part[i]);
582 for (int idim = 0; idim < NDIM; idim++)
583 pos[idim] = saved[i * NDIM + idim];
584 }
585 }
586
587 // Swap positions with Lagrangian position (if that exists)
588 template <class T>
589 void swap_eulerian_and_lagrangian_positions(T * part, size_t NumPart) {
590 if constexpr (not FML::PARTICLE::has_get_q<T>()) {
591 (void)part;
592 (void)NumPart;
593 return;
594 } else {
595 T tmp{};
596 constexpr int N = FML::PARTICLE::GetNDIM(tmp);
597#ifdef USE_OMP
598#pragma omp parallel for
599#endif
600 for (size_t ind = 0; ind < NumPart; ind++) {
601 auto pos_eulerian = FML::PARTICLE::GetPos(part[ind]);
602 auto pos_lagrangian = FML::PARTICLE::GetLagrangianPos(part[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;
607 }
608 }
609 }
610 }
611
612 } // namespace PARTICLE
613} // namespace FML
614
615#undef SFINAE_STRUCT
616#undef SFINAE_HAS
617#undef SFINAE_GET
618#undef SFINAE_SET
619#undef SFINAE_TEST_GET
620#undef SFINAE_TEST_SET
621#undef SFINAE_CONSTEXPR_GET
622#undef SFINAE_CONSTEXPR_TEST_GET
623
624#endif
const int NDIM
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.
Definition Global.cpp:39
double wrap(double x)
Definition test_fof.cpp:43