Version: SMASH-3.1
particles.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2018,2020,2022-2023
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 #ifndef SRC_INCLUDE_SMASH_PARTICLES_H_
8 #define SRC_INCLUDE_SMASH_PARTICLES_H_
9 
10 #include <memory>
11 #include <type_traits>
12 #include <vector>
13 
14 #include "macros.h"
15 #include "particledata.h"
16 #include "particletype.h"
17 #include "pdgcode.h"
18 
19 namespace smash {
20 
33 class Particles {
34  public:
36  Particles();
37 
39  Particles(const Particles &) = delete;
41  Particles &operator=(const Particles &) = delete;
42 
44  ParticleList copy_to_vector() const {
45  if (dirty_.empty()) {
46  return {&data_[0], &data_[data_size_]};
47  }
48  return {begin(), end()};
49  }
50 
68  const ParticleData &insert(const ParticleData &p);
69 
76  void create(size_t n, PdgCode pdg);
77 
84  ParticleData &create(const PdgCode pdg);
85 
87  size_t size() const { return data_size_ - dirty_.size(); }
88 
90  bool is_empty() const { return data_size_ == 0; }
91 
100  double time() const {
101  assert(!is_empty());
102  return front().position().x0();
103  }
104 
109  void reset();
110 
120  bool is_valid(const ParticleData &copy) const {
121  if (data_size_ <= copy.index_) {
122  return false;
123  }
124  /* Check if the particles still exists. If it decayed
125  * or scattered inelastically it is gone. */
126  return data_[copy.index_].id() == copy.id()
127  /* If the particle has scattered
128  * elastically, its id_process has
129  * changed and we consider it invalid. */
130  && data_[copy.index_].id_process() == copy.id_process();
131  }
132 
141  void remove(const ParticleData &p);
142 
157  void replace(const ParticleList &to_remove, ParticleList &to_add);
158 
176  const ParticleData &new_state) {
177  assert(is_valid(p));
178  assert(p.type() == new_state.type());
179  ParticleData &original = data_[p.index_];
180  new_state.copy_to(original);
181  return original;
182  }
183 
200  void update(const ParticleList &old_state, ParticleList &new_state,
201  bool do_replace) {
202  if (do_replace) {
203  replace(old_state, new_state);
204  } else {
205  for (std::size_t i = 0; i < old_state.size(); ++i) {
206  new_state[i] = update_particle(old_state[i], new_state[i]);
207  }
208  }
209  }
210 
222  const ParticleData &lookup(const ParticleData &old_state) const {
223  assert(is_valid(old_state));
224  return data_[old_state.index_];
225  }
226 
244  template <typename T>
246  friend class Particles;
247 
248  public:
250  using iterator_category = std::bidirectional_iterator_tag;
252  using value_type = std::remove_const_t<T>;
254  using difference_type = std::ptrdiff_t;
256  using pointer = std::add_pointer_t<T>;
258  using reference = std::add_lvalue_reference_t<T>;
260  using const_pointer = std::add_const_t<pointer>;
262  using const_reference = std::add_const_t<reference>;
263 
264  private:
271  GenericIterator(pointer p) : ptr_(p) {} // NOLINT(runtime/explicit)
272 
275 
276  public:
287  do {
288  ++ptr_;
289  } while (ptr_->hole_);
290  return *this;
291  }
298  GenericIterator old = *this;
299  operator++();
300  return old;
301  }
302 
315  do {
316  --ptr_;
317  } while (ptr_->hole_);
318  return *this;
319  }
326  GenericIterator old = *this;
327  operator--();
328  return old;
329  }
330 
332  reference operator*() { return *ptr_; }
334  const_reference operator*() const { return *ptr_; }
335 
337  pointer operator->() { return ptr_; }
339  const_pointer operator->() const { return ptr_; }
340 
342  bool operator==(const GenericIterator &rhs) const {
343  return ptr_ == rhs.ptr_;
344  }
346  bool operator!=(const GenericIterator &rhs) const {
347  return ptr_ != rhs.ptr_;
348  }
350  bool operator<(const GenericIterator &rhs) const { return ptr_ < rhs.ptr_; }
352  bool operator>(const GenericIterator &rhs) const { return ptr_ > rhs.ptr_; }
357  bool operator<=(const GenericIterator &rhs) const {
358  return ptr_ <= rhs.ptr_;
359  }
364  bool operator>=(const GenericIterator &rhs) const {
365  return ptr_ >= rhs.ptr_;
366  }
367  };
372 
374  ParticleData &front() { return *begin(); }
380  const ParticleData &front() const { return *begin(); }
381 
383  ParticleData &back() { return *(--end()); }
389  const ParticleData &back() const { return *(--end()); }
390 
396  ParticleData *first = &data_[0];
397  while (first->hole_) {
398  ++first;
399  }
400  return first;
401  }
408  ParticleData *first = &data_[0];
409  while (first->hole_) {
410  ++first;
411  }
412  return first;
413  }
414 
419  iterator end() { return &data_[data_size_]; }
425  const_iterator end() const { return &data_[data_size_]; }
426 
428  const_iterator cbegin() const { return begin(); }
430  const_iterator cend() const { return end(); }
431 
438  friend std::ostream &operator<<(std::ostream &out,
439  const Particles &particles);
440 
441  private:
446  int id_max_ = -1;
447 
455  void increase_capacity(unsigned new_capacity);
464  inline void ensure_capacity(unsigned to_add);
480  inline void copy_in(ParticleData &to, const ParticleData &from);
481 
487  unsigned data_size_ = 0u;
492  unsigned data_capacity_ = 100u;
498  std::unique_ptr<ParticleData[]> data_;
499 
504  std::vector<unsigned> dirty_;
505 };
506 
507 } // namespace smash
508 
509 #endif // SRC_INCLUDE_SMASH_PARTICLES_H_
double x0() const
Definition: fourvector.h:313
ParticleData contains the dynamic information of a certain particle.
Definition: particledata.h:58
unsigned index_
Internal index in the Particles list.
Definition: particledata.h:430
const ParticleType & type() const
Get the type of the particle.
Definition: particledata.h:128
bool hole_
If true, the object is an entry in Particles::data_ and does not hold valid particle data.
Definition: particledata.h:450
uint32_t id_process() const
Get the id of the last action.
Definition: particledata.h:134
void copy_to(ParticleData &dst) const
Copies some information of the particle to the given particle dst.
Definition: particledata.h:402
int32_t id() const
Get the id of the particle.
Definition: particledata.h:76
const FourVector & position() const
Get the particle's position in Minkowski space.
Definition: particledata.h:204
bool operator>(const GenericIterator &rhs) const
Definition: particles.h:352
bool operator==(const GenericIterator &rhs) const
Definition: particles.h:342
std::add_const_t< pointer > const_pointer
add const qualification to a pointer
Definition: particles.h:260
std::add_lvalue_reference_t< T > reference
Required by STL: expose reference (lvalue)
Definition: particles.h:258
bool operator<(const GenericIterator &rhs) const
Definition: particles.h:350
std::ptrdiff_t difference_type
Required by STL: expose difference_type
Definition: particles.h:254
std::remove_const_t< T > value_type
Required by STL: expose value_type removing const qualification
Definition: particles.h:252
GenericIterator & operator--()
Advance the iterator to the previous valid (not a hole) entry in Particles.
Definition: particles.h:314
const_reference operator*() const
Definition: particles.h:334
GenericIterator operator--(int)
Postfix variant of the above prefix decrement operator.
Definition: particles.h:325
const_pointer operator->() const
Definition: particles.h:339
GenericIterator operator++(int)
Postfix variant of the above prefix increment operator.
Definition: particles.h:297
bool operator<=(const GenericIterator &rhs) const
Definition: particles.h:357
bool operator!=(const GenericIterator &rhs) const
Definition: particles.h:346
pointer ptr_
The entry in Particles this iterator points to.
Definition: particles.h:274
GenericIterator & operator++()
Advance the iterator to the next valid (not a hole) entry in Particles.
Definition: particles.h:286
GenericIterator(pointer p)
Constructs an iterator pointing to the ParticleData pointed to by p.
Definition: particles.h:271
std::add_pointer_t< T > pointer
Required by STL: expose pointer (of a reference)
Definition: particles.h:256
std::bidirectional_iterator_tag iterator_category
Required by STL: expose iterator_category
Definition: particles.h:250
bool operator>=(const GenericIterator &rhs) const
Definition: particles.h:364
std::add_const_t< reference > const_reference
add const qualification to a reference
Definition: particles.h:262
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
double time() const
Returns the time of the computational frame.
Definition: particles.h:100
std::unique_ptr< ParticleData[]> data_
Points to a dynamically allocated array of ParticleData objects.
Definition: particles.h:498
const ParticleData & update_particle(const ParticleData &p, const ParticleData &new_state)
Updates the particle identified by p with the state stored in new_state.
Definition: particles.h:175
size_t size() const
Definition: particles.h:87
void update(const ParticleList &old_state, ParticleList &new_state, bool do_replace)
Updates the Particles object, replacing the particles in old_state with the particles in new_state.
Definition: particles.h:200
const_iterator cend() const
Definition: particles.h:430
ParticleList copy_to_vector() const
Definition: particles.h:44
void reset()
Reset the state of the Particles object to an empty list and a new id counter.
Definition: particles.cc:139
bool is_valid(const ParticleData &copy) const
Check whether the ParticleData copy is still a valid copy of the one stored in the Particles object.
Definition: particles.h:120
std::vector< unsigned > dirty_
Stores the indexes in data_ that do not hold valid particle data and should be reused when new partic...
Definition: particles.h:504
const ParticleData & front() const
const overload of &front()
Definition: particles.h:380
ParticleData & front()
Definition: particles.h:374
unsigned data_size_
Definition: particles.h:487
Particles(const Particles &)=delete
Cannot be copied.
iterator begin()
Definition: particles.h:395
bool is_empty() const
Definition: particles.h:90
const_iterator end() const
const overload of end()
Definition: particles.h:425
void copy_in(ParticleData &to, const ParticleData &from)
Definition: particles.cc:44
Particles & operator=(const Particles &)=delete
Cannot be copied.
int id_max_
Highest id of a given particle.
Definition: particles.h:446
const ParticleData & lookup(const ParticleData &old_state) const
Returns the particle that is currently stored in this object given an old copy of that particle.
Definition: particles.h:222
void ensure_capacity(unsigned to_add)
Definition: particles.cc:23
Particles()
Creates a new (empty) Particles object.
Definition: particles.cc:17
ParticleData & back()
Definition: particles.h:383
void replace(const ParticleList &to_remove, ParticleList &to_add)
Replace the particles in to_remove with the particles in to_add in the list of current particles.
Definition: particles.cc:120
unsigned data_capacity_
Definition: particles.h:492
const ParticleData & insert(const ParticleData &p)
Inserts the particle into the list of particles.
Definition: particles.cc:50
void create(size_t n, PdgCode pdg)
Add n particles of the same type (pdg) to the list.
Definition: particles.cc:66
void increase_capacity(unsigned new_capacity)
Definition: particles.cc:30
const_iterator cbegin() const
Definition: particles.h:428
const_iterator begin() const
const overload of begin()
Definition: particles.h:407
const ParticleData & back() const
const overload of &back()
Definition: particles.h:389
void remove(const ParticleData &p)
Remove the given particle p from the list.
Definition: particles.cc:108
iterator end()
Definition: particles.h:419
PdgCode stores a Particle Data Group Particle Numbering Scheme particle type number.
Definition: pdgcode.h:111
friend std::ostream & operator<<(std::ostream &out, const Particles &particles)
Print effective mass and type name for all particles to the stream.
Definition: particles.cc:148
constexpr int p
Proton.
constexpr int n
Neutron.
Definition: action.h:24