FML
Loading...
Searching...
No Matches
FoFBinning.h
Go to the documentation of this file.
1#ifndef FOFBINNING_HEADER
2#define FOFBINNING_HEADER
3#include <FML/Global/Global.h>
5#include <algorithm>
6#include <array>
7#include <cmath>
8#include <cassert>
9#include <vector>
10
11namespace FML {
12 namespace FOF {
13
14 //=========================================================================
15 /// This class defines how to compute the halos from the particles
16 ///
17 /// If you want to compute more things then make your own class like this one
18 /// and supply as template parameter to the FriendsOfFriends method.
19 ///
20 /// NB: The computation of the COM has to be done incrementally: we add up the mean
21 /// values as we go along instead of doing it in the end. For other quantities you
22 /// can do it as you wish.
23 ///
24 /// If you want access to all particles then one can add a vector of
25 /// pointers to the particles, push them in this vector during add and
26 /// then process them in finalize.
27 ///
28 //=========================================================================
29 template <class T, int NDIM>
30 class FoFHalo {
31 public:
32 /// Id of the halo
33 size_t id{0};
34 /// Number of particles in the halo
35 size_t np{0};
36 /// Mass of the halo (units is the same as those in the particles; get_mass)
37 double mass{0.0};
38 /// Center of the halo
39 std::array<double, NDIM> pos{};
40 /// Velocity of the halo
41 std::array<double, NDIM> vel{};
42 /// RMS velocity of the particles in the halo (velocity dispersion per dimension)
43 /// Contains <v^2> before finalizing
44 std::array<double, NDIM> vel_rms{};
45
46 // To be able to use this with MPIParticles we need these methods
47 constexpr int get_ndim() const { return NDIM; }
48 double * get_pos() { return pos.data(); }
49 double * get_vel() { return vel.data(); }
50
51 // NB: we don't add a get_mass as this will then be used if
52 // we compute density field of halos which is usually not what
53 // we want
54 double get_halo_mass() const { return mass; };
55
56 size_t get_npart() const { return np; };
57 size_t get_id() const { return id; };
58 std::array<double, NDIM> get_velocity_dispersion() const { return vel_rms; };
59
60#ifdef STORE_MEMBERS_PTRS
61 // If you want access to all particles in the end
62 std::vector<T *> member_particles;
63#endif
64
65 FoFHalo() = default;
66 FoFHalo(size_t _id) { id = _id; }
67
68 /// @brief Add a new particle to the FoF group. We update the center of mass
69 /// position to be that of the particles added so far.
70 /// @param[in] particle The particle we are to add to the halo
71 /// @param[in] periodic (Optional) Is the box periodic?
72 void add(T & particle, bool periodic = true) {
73 const double _mass = FML::PARTICLE::GetMass(particle);
74 static_assert(FML::PARTICLE::has_get_pos<T>());
75 const auto * _pos = FML::PARTICLE::GetPos(particle);
76
77 // Sanity check
78 assert_mpi(_mass > 0.0, "FoFBinning::add::Particle mass is negative or zero, this is not normal!");
79
80#ifdef STORE_MEMBERS_PTRS
81 // If you want access to all particles in finalize
82 member_particles.push_back(&particle);
83#endif
84
85 // Initialize
86 if (np == 0) {
87 for (int idim = 0; idim < NDIM; idim++) {
88 pos[idim] = 0.0;
89 vel[idim] = 0.0;
90 vel_rms[idim] = 0.0;
91 }
92 mass = 0.0;
93 }
94
95 // Update center of mass
96 std::array<double, NDIM> dx;
97 double weight = _mass / (mass + _mass);
98 for (int idim = 0; idim < NDIM; idim++) {
99 dx[idim] = _pos[idim] - pos[idim];
100 if (periodic) {
101 if (dx[idim] < -0.5)
102 dx[idim] += 1;
103 if (dx[idim] >= 0.5)
104 dx[idim] -= 1;
105 }
106 pos[idim] += dx[idim] * weight;
107 if (periodic) {
108 if (pos[idim] < 0.0)
109 pos[idim] += 1;
110 if (pos[idim] >= 1.0)
111 pos[idim] -= 1;
112 }
113 }
114
115 // Add velocity if particle has velocity
116 if constexpr (FML::PARTICLE::has_get_vel<T>()) {
117 auto _vel = FML::PARTICLE::GetVel(particle);
118 for (int idim = 0; idim < NDIM; idim++) {
119 vel[idim] = vel[idim] * (1 - weight) + _vel[idim] * weight;
120 vel_rms[idim] = vel_rms[idim] * (1 - weight) + _vel[idim] * _vel[idim] * weight;
121 }
122 }
123
124 // Update this last
125 np++;
126 mass += _mass;
127 }
128
129 /// @brief This method is called after all particles has been added to the halo
130 /// and we can do any normalization etc we want.
131 /// @param[in] periodic (Optional) Is the box periodic?
132 void finalize([[maybe_unused]] bool periodic = true) {
133
134 // Velocity dispersion. v_rms contains <v^2> at this point (<v^2> - <v>^2 can be slightly negative from
135 // round-off when all the particles have (nearly) the same velocity)
136 for (int idim = 0; idim < NDIM; idim++)
137 vel_rms[idim] = std::sqrt(std::max(0.0, vel_rms[idim] - vel[idim] * vel[idim]));
138
139#ifdef STORE_MEMBERS_PTRS
140 // Free up memory
141 member_particles = std::vector<T *>();
142#endif
143 }
144 };
145
146 } // namespace FOF
147} // namespace FML
148#endif
const int NDIM
This class defines how to compute the halos from the particles.
Definition FoFBinning.h:30
FoFHalo(size_t _id)
Definition FoFBinning.h:66
double mass
Mass of the halo (units is the same as those in the particles; get_mass)
Definition FoFBinning.h:37
void add(T &particle, bool periodic=true)
Add a new particle to the FoF group.
Definition FoFBinning.h:72
std::array< double, NDIM > pos
Center of the halo.
Definition FoFBinning.h:39
size_t get_npart() const
Definition FoFBinning.h:56
std::vector< T * > member_particles
Definition FoFBinning.h:62
size_t np
Number of particles in the halo.
Definition FoFBinning.h:35
std::array< double, NDIM > vel_rms
RMS velocity of the particles in the halo (velocity dispersion per dimension) Contains <v^2> before f...
Definition FoFBinning.h:44
void finalize(bool periodic=true)
This method is called after all particles has been added to the halo and we can do any normalization ...
Definition FoFBinning.h:132
size_t get_id() const
Definition FoFBinning.h:57
double * get_pos()
Definition FoFBinning.h:48
constexpr int get_ndim() const
Definition FoFBinning.h:47
std::array< double, NDIM > vel
Velocity of the halo.
Definition FoFBinning.h:41
double * get_vel()
Definition FoFBinning.h:49
std::array< double, NDIM > get_velocity_dispersion() const
Definition FoFBinning.h:58
double get_halo_mass() const
Definition FoFBinning.h:54
size_t id
Id of the halo.
Definition FoFBinning.h:33
get_vel constexpr double * GetPos(...)
constexpr double * GetVel(...)
set_mass constexpr double GetMass(Args &... args)
double periodic(double d)