Version: SMASH-2.0
particles.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2020
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 
232  template <typename T>
234  : public std::iterator<std::bidirectional_iterator_tag, T> {
235  friend class Particles;
236 
237  public:
239  using value_type = typename std::remove_const<T>::type;
241  using pointer = typename std::add_pointer<T>::type;
243  using reference = typename std::add_lvalue_reference<T>::type;
245  using const_pointer = typename std::add_const<pointer>::type;
247  using const_reference = typename std::add_const<reference>::type;
248 
249  private:
256  GenericIterator(pointer p) : ptr_(p) {} // NOLINT(runtime/explicit)
257 
260 
261  public:
272  do {
273  ++ptr_;
274  } while (ptr_->hole_);
275  return *this;
276  }
283  GenericIterator old = *this;
284  operator++();
285  return old;
286  }
287 
300  do {
301  --ptr_;
302  } while (ptr_->hole_);
303  return *this;
304  }
311  GenericIterator old = *this;
312  operator--();
313  return old;
314  }
315 
317  reference operator*() { return *ptr_; }
319  const_reference operator*() const { return *ptr_; }
320 
322  pointer operator->() { return ptr_; }
324  const_pointer operator->() const { return ptr_; }
325 
327  bool operator==(const GenericIterator &rhs) const {
328  return ptr_ == rhs.ptr_;
329  }
331  bool operator!=(const GenericIterator &rhs) const {
332  return ptr_ != rhs.ptr_;
333  }
335  bool operator<(const GenericIterator &rhs) const { return ptr_ < rhs.ptr_; }
337  bool operator>(const GenericIterator &rhs) const { return ptr_ > rhs.ptr_; }
342  bool operator<=(const GenericIterator &rhs) const {
343  return ptr_ <= rhs.ptr_;
344  }
349  bool operator>=(const GenericIterator &rhs) const {
350  return ptr_ >= rhs.ptr_;
351  }
352  };
357 
359  ParticleData &front() { return *begin(); }
365  const ParticleData &front() const { return *begin(); }
366 
368  ParticleData &back() { return *(--end()); }
374  const ParticleData &back() const { return *(--end()); }
375 
381  ParticleData *first = &data_[0];
382  while (first->hole_) {
383  ++first;
384  }
385  return first;
386  }
393  ParticleData *first = &data_[0];
394  while (first->hole_) {
395  ++first;
396  }
397  return first;
398  }
399 
404  iterator end() { return &data_[data_size_]; }
410  const_iterator end() const { return &data_[data_size_]; }
411 
413  const_iterator cbegin() const { return begin(); }
415  const_iterator cend() const { return end(); }
416 
423  friend std::ostream &operator<<(std::ostream &out,
424  const Particles &particles);
425 
426  private:
431  int id_max_ = -1;
432 
440  void increase_capacity(unsigned new_capacity);
449  inline void ensure_capacity(unsigned to_add);
465  inline void copy_in(ParticleData &to, const ParticleData &from);
466 
472  unsigned data_size_ = 0u;
477  unsigned data_capacity_ = 100u;
483  std::unique_ptr<ParticleData[]> data_;
484 
489  std::vector<unsigned> dirty_;
490 };
491 
492 } // namespace smash
493 
494 #endif // SRC_INCLUDE_SMASH_PARTICLES_H_
smash::Particles::GenericIterator::operator->
pointer operator->()
Definition: particles.h:322
smash
Definition: action.h:24
smash::Particles::GenericIterator
Definition: particles.h:233
particledata.h
smash::Particles::front
const ParticleData & front() const
const overload of &front()
Definition: particles.h:365
smash::Particles::begin
const_iterator begin() const
const overload of begin()
Definition: particles.h:392
smash::Particles::size
size_t size() const
Definition: particles.h:87
smash::ParticleData
Definition: particledata.h:52
smash::Particles::reset
void reset()
Reset the state of the Particles object to an empty list and a new id counter.
Definition: particles.cc:139
smash::ParticleData::id
int32_t id() const
Get the id of the particle.
Definition: particledata.h:70
smash::Particles::begin
iterator begin()
Definition: particles.h:380
smash::Particles::GenericIterator::operator<
bool operator<(const GenericIterator &rhs) const
Definition: particles.h:335
smash::Particles::cbegin
const_iterator cbegin() const
Definition: particles.h:413
smash::Particles::GenericIterator::operator==
bool operator==(const GenericIterator &rhs) const
Definition: particles.h:327
macros.h
smash::ParticleData::id_process
uint32_t id_process() const
Get the id of the last action.
Definition: particledata.h:128
smash::Particles::GenericIterator::operator--
GenericIterator operator--(int)
Postfix variant of the above prefix decrement operator.
Definition: particles.h:310
smash::Particles::dirty_
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:489
smash::Particles::end
iterator end()
Definition: particles.h:404
smash::Particles::operator=
Particles & operator=(const Particles &)=delete
Cannot be copied.
smash::Particles::replace
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
smash::Particles::GenericIterator::operator>=
bool operator>=(const GenericIterator &rhs) const
Definition: particles.h:349
smash::Particles::create
void create(size_t n, PdgCode pdg)
Add n particles of the same type (pdg) to the list.
Definition: particles.cc:66
smash::Particles::GenericIterator::operator*
reference operator*()
Definition: particles.h:317
smash::Particles::GenericIterator::operator>
bool operator>(const GenericIterator &rhs) const
Definition: particles.h:337
smash::Particles::GenericIterator::operator*
const_reference operator*() const
Definition: particles.h:319
smash::Particles::is_empty
bool is_empty() const
Definition: particles.h:90
smash::Particles::update_particle
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
smash::Particles::end
const_iterator end() const
const overload of end()
Definition: particles.h:410
smash::Particles::copy_to_vector
ParticleList copy_to_vector() const
Definition: particles.h:44
smash::Particles::GenericIterator::GenericIterator
GenericIterator(pointer p)
Constructs an iterator pointing to the ParticleData pointed to by p.
Definition: particles.h:256
smash::Particles::operator<<
friend std::ostream & operator<<(std::ostream &out, const Particles &particles)
Definition: particles.cc:148
smash::Particles::data_size_
unsigned data_size_
Definition: particles.h:472
smash::Particles::GenericIterator::operator!=
bool operator!=(const GenericIterator &rhs) const
Definition: particles.h:331
smash::FourVector::x0
double x0() const
Definition: fourvector.h:303
smash::ParticleData::hole_
bool hole_
If true, the object is an entry in Particles::data_ and does not hold valid particle data.
Definition: particledata.h:438
smash::Particles::copy_in
void copy_in(ParticleData &to, const ParticleData &from)
Definition: particles.cc:44
smash::Particles::update
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
smash::ParticleData::position
const FourVector & position() const
Get the particle's position in Minkowski space.
Definition: particledata.h:198
smash::Particles::GenericIterator::pointer
typename std::add_pointer< T >::type pointer
provide the pointer of a reference
Definition: particles.h:241
smash::Particles::GenericIterator::reference
typename std::add_lvalue_reference< T >::type reference
creates a lvalue reference
Definition: particles.h:243
smash::Particles::id_max_
int id_max_
Highest id of a given particle.
Definition: particles.h:431
smash::Particles::GenericIterator::const_pointer
typename std::add_const< pointer >::type const_pointer
add const qualification to a pointer
Definition: particles.h:245
smash::Particles::data_capacity_
unsigned data_capacity_
Definition: particles.h:477
particletype.h
smash::ParticleData::copy_to
void copy_to(ParticleData &dst) const
Copies some information of the particle to the given particle dst.
Definition: particledata.h:391
smash::PdgCode
Definition: pdgcode.h:108
smash::Particles::GenericIterator::value_type
typename std::remove_const< T >::type value_type
remove const qualification
Definition: particles.h:239
smash::Particles::data_
std::unique_ptr< ParticleData[]> data_
Points to a dynamically allocated array of ParticleData objects.
Definition: particles.h:483
smash::Particles::lookup
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
smash::Particles::insert
const ParticleData & insert(const ParticleData &p)
Inserts the particle into the list of particles.
Definition: particles.cc:50
smash::Particles::back
const ParticleData & back() const
const overload of &back()
Definition: particles.h:374
smash::Particles::is_valid
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
smash::Particles
Definition: particles.h:33
smash::Particles::time
double time() const
Returns the time of the computational frame.
Definition: particles.h:100
smash::Particles::back
ParticleData & back()
Definition: particles.h:368
pdgcode.h
smash::Particles::ensure_capacity
void ensure_capacity(unsigned to_add)
Definition: particles.cc:23
smash::Particles::GenericIterator::operator<=
bool operator<=(const GenericIterator &rhs) const
Definition: particles.h:342
smash::Particles::front
ParticleData & front()
Definition: particles.h:359
smash::Particles::GenericIterator::operator->
const_pointer operator->() const
Definition: particles.h:324
smash::pdg::p
constexpr int p
Proton.
Definition: pdgcode_constants.h:28
smash::pdg::n
constexpr int n
Neutron.
Definition: pdgcode_constants.h:30
smash::Particles::cend
const_iterator cend() const
Definition: particles.h:415
smash::Particles::GenericIterator::operator++
GenericIterator & operator++()
Advance the iterator to the next valid (not a hole) entry in Particles.
Definition: particles.h:271
smash::Particles::GenericIterator::operator++
GenericIterator operator++(int)
Postfix variant of the above prefix increment operator.
Definition: particles.h:282
smash::Particles::GenericIterator::ptr_
pointer ptr_
The entry in Particles this iterator points to.
Definition: particles.h:259
smash::Particles::increase_capacity
void increase_capacity(unsigned new_capacity)
Definition: particles.cc:30
smash::Particles::GenericIterator::operator--
GenericIterator & operator--()
Advance the iterator to the previous valid (not a hole) entry in Particles.
Definition: particles.h:299
smash::ParticleData::type
const ParticleType & type() const
Get the type of the particle.
Definition: particledata.h:122
smash::Particles::Particles
Particles()
Creates a new (empty) Particles object.
Definition: particles.cc:17
smash::Particles::remove
void remove(const ParticleData &p)
Remove the given particle p from the list.
Definition: particles.cc:108
smash::ParticleData::index_
unsigned index_
Internal index in the Particles list.
Definition: particledata.h:418
smash::Particles::GenericIterator::const_reference
typename std::add_const< reference >::type const_reference
add const qualification to a reference
Definition: particles.h:247