Reference documentation for deal.II version GIT defb42778c 2022-12-05 01:15:02+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
particle.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2021 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
17 
19 
21 
22 namespace Particles
23 {
24  template <int dim, int spacedim>
25  PropertyPool<dim, spacedim> Particle<dim, spacedim>::global_property_pool = {
26  /* no properties: */ 0};
27 
28 
29  template <int dim, int spacedim>
31  : property_pool(&global_property_pool)
32  , property_pool_handle(property_pool->register_particle())
33  {}
34 
35 
36 
37  template <int dim, int spacedim>
39  const Point<dim> & reference_location,
40  const types::particle_index id)
41  : property_pool(&global_property_pool)
42  , property_pool_handle(property_pool->register_particle())
43  {
44  set_location(location);
45  set_reference_location(reference_location);
46  set_id(id);
47  }
48 
49 
50 
51  template <int dim, int spacedim>
53  : property_pool(particle.property_pool)
54  , property_pool_handle(property_pool->register_particle())
55  {
56  set_location(particle.get_location());
58  set_id(particle.get_id());
59 
60  if (particle.has_properties())
61  {
62  const ArrayView<const double> their_properties =
63  particle.get_properties();
64  const ArrayView<double> my_properties =
66 
67  std::copy(their_properties.begin(),
68  their_properties.end(),
69  my_properties.begin());
70  }
71  }
72 
73 
74 
75  template <int dim, int spacedim>
77  const void *& data,
78  PropertyPool<dim, spacedim> *const new_property_pool)
79  : property_pool(new_property_pool != nullptr ? new_property_pool :
80  &global_property_pool)
81  , property_pool_handle(property_pool->register_particle())
82  {
83  const types::particle_index *id_data =
84  static_cast<const types::particle_index *>(data);
85  set_id(*id_data++);
86  const double *pdata = reinterpret_cast<const double *>(id_data);
87 
88  Point<spacedim> location;
89  for (unsigned int i = 0; i < spacedim; ++i)
90  location(i) = *pdata++;
91  set_location(location);
92 
93  Point<dim> reference_location;
94  for (unsigned int i = 0; i < dim; ++i)
95  reference_location(i) = *pdata++;
96  set_reference_location(reference_location);
97 
98  // See if there are properties to load
99  if (has_properties())
100  {
101  const ArrayView<double> particle_properties = this->get_properties();
102  const unsigned int size = particle_properties.size();
103  for (unsigned int i = 0; i < size; ++i)
104  particle_properties[i] = *pdata++;
105  }
106 
107  data = static_cast<const void *>(pdata);
108  }
109 
110 
111 
112  template <int dim, int spacedim>
114  : property_pool(std::move(particle.property_pool))
115  , property_pool_handle(std::move(particle.property_pool_handle))
116  {
117  // There is no need to copy locations, properties, and id -- we simply
118  // inherit them from the object we move from.
119 
120  // We stole the rhs's properties, so we need to invalidate
121  // the handle the rhs holds lest it releases the memory that
122  // we still reference here.
123  particle.property_pool_handle = PropertyPool<dim, spacedim>::invalid_handle;
124  }
125 
126 
127 
128  template <int dim, int spacedim>
131  {
132  if (this != &particle)
133  {
134  Assert(this->property_pool->n_properties_per_slot() ==
136  ExcInternalError());
137 
138  set_location(particle.get_location());
139  set_reference_location(particle.get_reference_location());
140  set_id(particle.get_id());
141 
142  if (particle.has_properties())
143  {
144  const ArrayView<const double> their_properties =
145  particle.get_properties();
146  const ArrayView<double> my_properties =
147  property_pool->get_properties(property_pool_handle);
148 
149  std::copy(their_properties.begin(),
150  their_properties.end(),
151  my_properties.begin());
152  }
153  }
154 
155  return *this;
156  }
157 
158 
159 
160  template <int dim, int spacedim>
163  Particle<dim, spacedim> &&particle) noexcept
164  {
165  if (this != &particle)
166  {
167  // If we currently hold a handle, release the memory. (The only way a
168  // particle can end up not holding a valid handle is if it has been
169  // moved from.)
170  if (property_pool_handle != PropertyPool<dim, spacedim>::invalid_handle)
171  property_pool->deregister_particle(property_pool_handle);
172 
173  property_pool = std::move(particle.property_pool);
174  property_pool_handle = std::move(particle.property_pool_handle);
175 
176  // No need to copy locations, properties, and id -- we just get them
177  // by taking over the property pool handle.
178 
179  // We stole the rhs's properties, so we need to invalidate
180  // the handle the rhs holds lest it releases the memory that
181  // we still reference here.
182  particle.property_pool_handle =
184  }
185  return *this;
186  }
187 
188 
189 
190  template <int dim, int spacedim>
192  {
193  // If we still hold a handle, release the memory. The only way a
194  // particle can end up not holding a valid handle is if it has been
195  // moved from.
196  if (property_pool_handle != PropertyPool<dim, spacedim>::invalid_handle)
197  property_pool->deregister_particle(property_pool_handle);
198  }
199 
200 
201 
202  template <int dim, int spacedim>
203  void
205  {
206  // If we still hold a handle, release the memory. The only way a
207  // particle can end up not holding a valid handle is if it has been
208  // moved from.
209  //
210  // deregister_particle() automatically invalidates its argument.
211  if (property_pool_handle != PropertyPool<dim, spacedim>::invalid_handle)
212  property_pool->deregister_particle(property_pool_handle);
213  }
214 
215 
216 
217  template <int dim, int spacedim>
218  void *
220  void *data_pointer) const
221  {
222  types::particle_index *id_data =
223  static_cast<types::particle_index *>(data_pointer);
224  *id_data = get_id();
225  ++id_data;
226  double *pdata = reinterpret_cast<double *>(id_data);
227 
228  // Write location
229  for (unsigned int i = 0; i < spacedim; ++i, ++pdata)
230  *pdata = get_location()[i];
231 
232  // Write reference location
233  for (unsigned int i = 0; i < dim; ++i, ++pdata)
234  *pdata = get_reference_location()[i];
235 
236  // Write properties
237  if (has_properties())
238  {
239  const ArrayView<double> particle_properties =
240  property_pool->get_properties(property_pool_handle);
241  for (unsigned int i = 0; i < particle_properties.size(); ++i, ++pdata)
242  *pdata = particle_properties[i];
243  }
244 
245  return static_cast<void *>(pdata);
246  }
247 
248 
249 
250  template <int dim, int spacedim>
251  const void *
253  const void *data_pointer)
254  {
255  const types::particle_index *id_data =
256  static_cast<const types::particle_index *>(data_pointer);
257  set_id(*id_data++);
258  const double *pdata = reinterpret_cast<const double *>(id_data);
259 
260  Point<spacedim> location;
261  for (unsigned int i = 0; i < spacedim; ++i)
262  location(i) = *pdata++;
263  set_location(location);
264 
265  Point<dim> reference_location;
266  for (unsigned int i = 0; i < dim; ++i)
267  reference_location(i) = *pdata++;
268  set_reference_location(reference_location);
269 
270  // See if there are properties to load
271  if (has_properties())
272  {
273  const ArrayView<double> particle_properties =
274  property_pool->get_properties(property_pool_handle);
275  const unsigned int size = particle_properties.size();
276  for (unsigned int i = 0; i < size; ++i)
277  particle_properties[i] = *pdata++;
278  }
279 
280  return static_cast<const void *>(pdata);
281  }
282 
283 
284 
285  template <int dim, int spacedim>
286  std::size_t
288  {
289  std::size_t size = sizeof(get_id()) + sizeof(get_location()) +
290  sizeof(get_reference_location());
291 
292  if (has_properties())
293  {
294  const ArrayView<double> particle_properties =
295  property_pool->get_properties(property_pool_handle);
296  size += sizeof(double) * particle_properties.size();
297  }
298  return size;
299  }
300 
301 
302 
303  template <int dim, int spacedim>
304  void
306  const ArrayView<const double> &new_properties)
307  {
308  const ArrayView<double> property_values =
309  property_pool->get_properties(property_pool_handle);
310 
311  Assert(new_properties.size() == property_values.size(),
312  ExcMessage(
313  "You are trying to assign properties with an incompatible length. "
314  "The particle has space to store " +
315  std::to_string(property_values.size()) +
316  " properties, but you are trying to assign " +
317  std::to_string(new_properties.size()) +
318  " properties. This is not allowed."));
319 
320  if (property_values.size() > 0)
321  std::copy(new_properties.begin(),
322  new_properties.end(),
323  property_values.begin());
324  }
325 
326 
327 
328  template <int dim, int spacedim>
329  const ArrayView<double>
331  {
332  return property_pool->get_properties(property_pool_handle);
333  }
334 } // namespace Particles
335 
337 
339 
340 #include "particle.inst"
341 
iterator begin() const
Definition: array_view.h:585
iterator end() const
Definition: array_view.h:594
std::size_t size() const
Definition: array_view.h:576
PropertyPool< dim, spacedim > * property_pool
Definition: particle.h:453
const Point< dim > & get_reference_location() const
Definition: particle.h:551
const Point< spacedim > & get_location() const
Definition: particle.h:533
bool has_properties() const
Definition: particle.h:640
static PropertyPool< dim, spacedim > global_property_pool
Definition: particle.h:447
void set_reference_location(const Point< dim > &new_reference_location)
Definition: particle.h:542
std::size_t serialized_size_in_bytes() const
Definition: particle.cc:287
Particle< dim, spacedim > & operator=(const Particle< dim, spacedim > &particle)
Definition: particle.cc:130
const ArrayView< double > get_properties()
Definition: particle.cc:330
void set_properties(const ArrayView< const double > &new_properties)
Definition: particle.cc:305
const void * read_particle_data_from_memory(const void *data)
Definition: particle.cc:252
void * write_particle_data_to_memory(void *data) const
Definition: particle.cc:219
types::particle_index get_id() const
Definition: particle.h:560
PropertyPool< dim, spacedim >::Handle property_pool_handle
Definition: particle.h:458
void free_properties()
Definition: particle.cc:204
void set_id(const types::particle_index &new_id)
Definition: particle.h:569
void set_location(const Point< spacedim > &new_location)
Definition: particle.h:524
unsigned int n_properties_per_slot() const
ArrayView< double > get_properties(const Handle handle)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:458
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:459
static ::ExceptionBase & ExcInternalError()
#define Assert(cond, exc)
Definition: exceptions.h:1501
static ::ExceptionBase & ExcMessage(std::string arg1)
std::string to_string(const T &t)
Definition: patterns.h:2394
void copy(const T *begin, const T *end, U *dest)
unsigned int particle_index
Definition: property_pool.h:65