group_particles
Loading...
Searching...
No Matches
callback.hpp
Go to the documentation of this file.
1
8#ifndef CALLBACK_HPP
9#define CALLBACK_HPP
10
11#include <cstddef>
12#include <string>
13#include <memory>
14#include <type_traits>
15
16#include "H5Cpp.h"
17
18#include "fields.hpp"
19#include "geom_utils.hpp"
20
30template<typename AFields>
32{
45 template<typename T>
47 {// {{{
48 // declare as char so we can advance byte-wise
49 // (char is guaranteed to be one byte wide)
50 char *data[T::Nfields];
51
52 template<typename Field>
53 static constexpr size_t get_field_idx ();
54
55 public :
56 // optional offset initializes at offset particles inside the data_in_memory pointers
57 BaseProperties (void *data_in_memory[T::Nfields], size_t offset=0UL);
58
59 // advances the internal pointers by the respective strides
60 // User should not call this!
61 void advance ();
62
63 // retrieves the internal pointers
64 // User should not need to call this
65 void *operator[] (size_t idx) const;
66
69 auto coord () const;
70
79 template<typename Field>
80 auto get () const;
81 };// }}}
82
84 class GrpProperties : public BaseProperties<typename AFields::GroupFields>
85 {
86 using BaseProperties<typename AFields::GroupFields>::BaseProperties;
87
88 public :
89 size_t chunk_idx;
90 GrpProperties (size_t chunk_idx_, void **data_in_memory, size_t offset=0UL);
91 };
92
94 struct PrtProperties : public BaseProperties<typename AFields::ParticleFields>
95 {
96 coord_t Bsize;
97
98 PrtProperties (coord_t Bsize_, void **data_in_memory, size_t offset=0UL);
99
100 // get around the name hiding issue
101 using BaseProperties<typename AFields::ParticleFields>::coord;
102
110 std::array<coord_t, 3> coord (const coord_t *grp_coord) const;
111 };
112
123 virtual bool grp_chunk (size_t chunk_idx, std::string &fname) const = 0;
124
135 virtual bool prt_chunk (size_t chunk_idx, std::string &fname) const = 0;
136
143 virtual std::string grp_name () const = 0;
144
151 virtual std::string prt_name () const = 0;
152
161 virtual void read_grp_meta_init (std::shared_ptr<H5::H5File> fptr) { return; }
162
171 virtual void read_prt_meta_init (std::shared_ptr<H5::H5File> fptr) { return; }
172
184 virtual void read_grp_meta (size_t chunk_idx, std::shared_ptr<H5::H5File> fptr,
185 size_t &Ngroups) const = 0;
186
202 virtual void read_prt_meta (size_t chunk_idx, std::shared_ptr<H5::H5File> fptr,
203 coord_t &Bsize, size_t &Nparts) const = 0;
204
215 virtual bool grp_select (const GrpProperties &grp) const { return true; }
216
231 virtual void grp_action (const GrpProperties &grp) = 0;
232
243 virtual coord_t grp_radius (const GrpProperties &grp) const = 0;
244
260 virtual void prt_action (size_t grp_idx, const GrpProperties &grp,
261 const PrtProperties &prt, coord_t Rsq) = 0;
262
272 virtual coord_t prt_coord_rescale ( ) const { return 1.0; }
273
281 virtual void prt_modify (PrtProperties &prt) { return; }
282};
283
284
285// --- Implementation of the BaseProperties struct ---
286
287template<typename AFields>
288template<typename T>
289Callback<AFields>::BaseProperties<T>::BaseProperties (void *data_in_memory[T::Nfields], size_t offset)
290{
291 for (size_t ii=0; ii != T::Nfields; ++ii)
292 data[ii] = (char *)(data_in_memory[ii]) + offset * T::strides_fcoord[ii];
293}
294
295template<typename AFields>
296template<typename T>
297inline void
299{
300 for (size_t ii=0; ii != T::Nfields; ++ii)
301 data[ii] += T::strides_fcoord[ii];
302}
303
304template<typename AFields>
305template<typename T>
306inline void *
308{
309 return data[idx];
310}
311
312template<typename AFields>
313template<typename T>
314inline auto
316{
317 return (coord_t *)data[0];
318}
319
320template<typename AFields>
321template<typename T>
322template<typename Field>
323constexpr size_t
325{
326 static_assert(T::field_type == Field::type);
327
328 if constexpr (T::field_type == FieldTypes::GrpFld)
329 return AFields::GroupFields::template idx<Field>;
330 else
331 return AFields::ParticleFields::template idx<Field>;
332}
333
334template<typename AFields>
335template<typename T>
336template<typename Field>
337inline auto
339{
340 constexpr size_t idx = get_field_idx<Field> ();
341
342 if constexpr (Field::dim == 1)
343 return (typename Field::value_type) *(typename Field::value_type *)data[idx];
344 else
345 if constexpr (Field::coord)
346 // need to use the global coord_t that this field has been converted to
347 return (coord_t *)data[idx];
348 else
349 return (typename Field::value_type *)data[idx];
350}
351
352template<typename AFields>
353Callback<AFields>::GrpProperties::GrpProperties (size_t chunk_idx_, void **data_in_memory, size_t offset) :
354 Callback<AFields>::template BaseProperties<typename AFields::GroupFields> { data_in_memory, offset },
355 chunk_idx { chunk_idx_ }
356{ }
357
358template<typename AFields>
359Callback<AFields>::PrtProperties::PrtProperties (coord_t Bsize_, void **data_in_memory, size_t offset) :
360 Callback<AFields>::template BaseProperties<typename AFields::ParticleFields> { data_in_memory, offset },
361 Bsize { Bsize_ }
362{ }
363
364template<typename AFields>
365inline std::array<coord_t, 3>
367{
368 std::array<coord_t, 3> out;
369
371
372 for (size_t ii=0; ii != 3; ++ii)
373 out[ii] = grp_prt_detail::GeomUtils::periodic_dist(grp_coord[ii], prt_coord[ii], Bsize);
374
375 return out;
376}
377
378#endif // CALLBACK_HPP
Type describing either a group or a particle.
Definition callback.hpp:47
auto get() const
Returns the individual properties.
Definition callback.hpp:338
auto coord() const
Definition callback.hpp:315
Specialization of the Callback::BaseProperties type to groups.
Definition callback.hpp:85
Templates to construct types that tell the code which data fields are to to be read from the group an...
float coord_t
Internal coordinate type.
Definition fields.hpp:28
Specialization of the Callback::BaseProperties type to particles.
Definition callback.hpp:95
The abstract base class the user should inherit from.
Definition callback.hpp:32
virtual bool grp_chunk(size_t chunk_idx, std::string &fname) const =0
Where to find the group files.
virtual void read_grp_meta_init(std::shared_ptr< H5::H5File > fptr)
Allows the user to read meta-data from the 0th group chunk.
Definition callback.hpp:161
virtual void read_grp_meta(size_t chunk_idx, std::shared_ptr< H5::H5File > fptr, size_t &Ngroups) const =0
Inform the code how many groups there are in a group chunk.
virtual coord_t grp_radius(const GrpProperties &grp) const =0
Inform the code how large this group is.
virtual bool prt_chunk(size_t chunk_idx, std::string &fname) const =0
Where to find the particle files.
virtual void prt_modify(PrtProperties &prt)
Modifications to particle properties.
Definition callback.hpp:281
virtual coord_t prt_coord_rescale() const
Rescaling of particle coordinates.
Definition callback.hpp:272
virtual void grp_action(const GrpProperties &grp)=0
Action to take for each group for which grp_select returned true.
virtual void read_prt_meta_init(std::shared_ptr< H5::H5File > fptr)
Allows the user to read meta-data from the 0th particle chunk.
Definition callback.hpp:171
virtual bool grp_select(const GrpProperties &grp) const
Inform the code whether a group should be considered.
Definition callback.hpp:215
virtual std::string prt_name() const =0
Where to find the particle fields in the hdf5 file.
virtual void read_prt_meta(size_t chunk_idx, std::shared_ptr< H5::H5File > fptr, coord_t &Bsize, size_t &Nparts) const =0
Inform the code how large the box is and how many particles there are in a particle chunk.
virtual void prt_action(size_t grp_idx, const GrpProperties &grp, const PrtProperties &prt, coord_t Rsq)=0
Action to take for each particle that falls within grp_radius from a group.
virtual std::string grp_name() const =0
Where to find the group fields in the hdf5 file.