Reference documentation for deal.II version Git 4e8af90f34 2022-01-22 18:32:44 +0100
\(\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\}}\)
property_pool.h
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 
16 #ifndef dealii_particles_property_pool_h
17 #define dealii_particles_property_pool_h
18 
19 #include <deal.II/base/config.h>
20 
22 #include <deal.II/base/point.h>
23 
24 
26 
27 namespace types
28 {
29  /* Type definitions */
30 
31 #ifdef DEAL_II_WITH_64BIT_INDICES
32 
43  using particle_index = uint64_t;
44 
45 # ifdef DEAL_II_WITH_MPI
46 
50 # define DEAL_II_PARTICLE_INDEX_MPI_TYPE MPI_UINT64_T
51 # endif
52 
53 #else
54 
65  using particle_index = unsigned int;
66 
67 # ifdef DEAL_II_WITH_MPI
68 
72 # define DEAL_II_PARTICLE_INDEX_MPI_TYPE MPI_UNSIGNED
73 # endif
74 #endif
75 } // namespace types
76 
77 namespace Particles
78 {
101  template <int dim, int spacedim = dim>
103  {
104  public:
110  using Handle = unsigned int;
111 
115  static const Handle invalid_handle;
116 
120  PropertyPool(const unsigned int n_properties_per_slot);
121 
127  ~PropertyPool();
128 
135  void
136  clear();
137 
143  Handle
144  register_particle();
145 
150  void
151  deregister_particle(Handle &handle);
152 
156  const Point<spacedim> &
157  get_location(const Handle handle) const;
158 
162  void
163  set_location(const Handle handle, const Point<spacedim> &new_location);
164 
169  const Point<dim> &
170  get_reference_location(const Handle handle) const;
171 
176  void
177  set_reference_location(const Handle handle,
178  const Point<dim> &new_reference_location);
179 
185  get_id(const Handle handle) const;
186 
191  void
192  set_id(const Handle handle, const types::particle_index &new_id);
193 
199  get_properties(const Handle handle);
200 
205  void
206  reserve(const std::size_t size);
207 
211  unsigned int
212  n_properties_per_slot() const;
213 
218  unsigned int
219  n_slots() const;
220 
224  unsigned int
225  n_registered_slots() const;
226 
235  void
236  sort_memory_slots(const std::vector<Handle> &handles_to_sort);
237 
238  private:
242  const unsigned int n_properties;
243 
249  std::vector<Point<spacedim>> locations;
250 
256  std::vector<Point<dim>> reference_locations;
257 
263  std::vector<types::particle_index> ids;
264 
270  std::vector<double> properties;
271 
279  std::vector<Handle> currently_available_handles;
280  };
281 
282 
283 
284  /* ---------------------- inline and template functions ------------------ */
285 
286  template <int dim, int spacedim>
287  inline const Point<spacedim> &
289  {
290  const std::vector<double>::size_type data_index =
291  (handle != invalid_handle) ? handle : 0;
292 
293  // Ideally we would need to assert that 'handle' has not been deallocated
294  // by searching through 'currently_available_handles'. However, this
295  // is expensive and this function is performance critical, so instead
296  // just check against the array range, and rely on the fact
297  // that handles are invalidated when handed over to
298  // deallocate_properties_array().
299  Assert(data_index <= locations.size() - 1,
300  ExcMessage("Invalid location handle. This can happen if the "
301  "handle was duplicated and then one copy was deallocated "
302  "before trying to access the properties."));
303 
304  return locations[data_index];
305  }
306 
307 
308 
309  template <int dim, int spacedim>
310  inline void
312  const Point<spacedim> &new_location)
313  {
314  const std::vector<double>::size_type data_index =
315  (handle != invalid_handle) ? handle : 0;
316 
317  // Ideally we would need to assert that 'handle' has not been deallocated
318  // by searching through 'currently_available_handles'. However, this
319  // is expensive and this function is performance critical, so instead
320  // just check against the array range, and rely on the fact
321  // that handles are invalidated when handed over to
322  // deallocate_properties_array().
323  Assert(data_index <= locations.size() - 1,
324  ExcMessage("Invalid location handle. This can happen if the "
325  "handle was duplicated and then one copy was deallocated "
326  "before trying to access the properties."));
327 
328  locations[data_index] = new_location;
329  }
330 
331 
332 
333  template <int dim, int spacedim>
334  inline const Point<dim> &
336  {
337  const std::vector<double>::size_type data_index =
338  (handle != invalid_handle) ? handle : 0;
339 
340  // Ideally we would need to assert that 'handle' has not been deallocated
341  // by searching through 'currently_available_handles'. However, this
342  // is expensive and this function is performance critical, so instead
343  // just check against the array range, and rely on the fact
344  // that handles are invalidated when handed over to
345  // deallocate_properties_array().
346  Assert(data_index <= reference_locations.size() - 1,
347  ExcMessage("Invalid location handle. This can happen if the "
348  "handle was duplicated and then one copy was deallocated "
349  "before trying to access the properties."));
350 
351  return reference_locations[data_index];
352  }
353 
354 
355 
356  template <int dim, int spacedim>
357  inline void
359  const Handle handle,
360  const Point<dim> &new_reference_location)
361  {
362  const std::vector<double>::size_type data_index =
363  (handle != invalid_handle) ? handle : 0;
364 
365  // Ideally we would need to assert that 'handle' has not been deallocated
366  // by searching through 'currently_available_handles'. However, this
367  // is expensive and this function is performance critical, so instead
368  // just check against the array range, and rely on the fact
369  // that handles are invalidated when handed over to
370  // deallocate_properties_array().
371  Assert(data_index <= reference_locations.size() - 1,
372  ExcMessage("Invalid location handle. This can happen if the "
373  "handle was duplicated and then one copy was deallocated "
374  "before trying to access the properties."));
375 
376  reference_locations[data_index] = new_reference_location;
377  }
378 
379 
380 
381  template <int dim, int spacedim>
382  inline types::particle_index
384  {
385  const std::vector<double>::size_type data_index =
386  (handle != invalid_handle) ? handle : 0;
387 
388  // Ideally we would need to assert that 'handle' has not been deallocated
389  // by searching through 'currently_available_handles'. However, this
390  // is expensive and this function is performance critical, so instead
391  // just check against the array range, and rely on the fact
392  // that handles are invalidated when handed over to
393  // deallocate_properties_array().
394  Assert(data_index <= ids.size() - 1,
395  ExcMessage("Invalid location handle. This can happen if the "
396  "handle was duplicated and then one copy was deallocated "
397  "before trying to access the properties."));
398 
399  return ids[data_index];
400  }
401 
402 
403 
404  template <int dim, int spacedim>
405  inline void
407  const types::particle_index &new_id)
408  {
409  const std::vector<double>::size_type data_index =
410  (handle != invalid_handle) ? handle : 0;
411 
412  // Ideally we would need to assert that 'handle' has not been deallocated
413  // by searching through 'currently_available_handles'. However, this
414  // is expensive and this function is performance critical, so instead
415  // just check against the array range, and rely on the fact
416  // that handles are invalidated when handed over to
417  // deallocate_properties_array().
418  Assert(data_index <= ids.size() - 1,
419  ExcMessage("Invalid location handle. This can happen if the "
420  "handle was duplicated and then one copy was deallocated "
421  "before trying to access the properties."));
422 
423  ids[data_index] = new_id;
424  }
425 
426 
427 
428  template <int dim, int spacedim>
429  inline ArrayView<double>
431  {
432  const std::vector<double>::size_type data_index =
433  (handle != invalid_handle) ? handle * n_properties : 0;
434 
435  // Ideally we would need to assert that 'handle' has not been deallocated
436  // by searching through 'currently_available_handles'. However, this
437  // is expensive and this function is performance critical, so instead
438  // just check against the array range, and rely on the fact
439  // that handles are invalidated when handed over to
440  // deallocate_properties_array().
441  Assert(data_index <= properties.size() - n_properties,
442  ExcMessage("Invalid property handle. This can happen if the "
443  "handle was duplicated and then one copy was deallocated "
444  "before trying to access the properties."));
445 
446  return ArrayView<double>(properties.data() + data_index, n_properties);
447  }
448 
449 
450 
451  template <int dim, int spacedim>
452  inline unsigned int
454  {
455  return locations.size();
456  }
457 
458 
459 } // namespace Particles
460 
462 
463 #endif
std::vector< types::particle_index > ids
types::global_dof_index size_type
Definition: cuda_kernels.h:45
const unsigned int n_properties
unsigned int particle_index
Definition: property_pool.h:65
static ::ExceptionBase & ExcMessage(std::string arg1)
Definition: types.h:31
#define Assert(cond, exc)
Definition: exceptions.h:1461
std::vector< Handle > currently_available_handles
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:407
static const Handle invalid_handle
std::vector< Point< dim > > reference_locations
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:406
std::vector< double > properties
std::vector< Point< spacedim > > locations