deal.II version GIT relicensing-6834-g5b78e6bcdf 2026-10-01 11:20:01+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\}}\)
Loading...
Searching...
No Matches
property_pool.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2017 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_particles_property_pool_h
14#define dealii_particles_property_pool_h
15
16#include <deal.II/base/config.h>
17
19#include <deal.II/base/point.h>
20
21
23
24namespace types
25{
26 /* Type definitions */
27
28#ifdef DEAL_II_WITH_64BIT_INDICES
40 using particle_index = std::uint64_t;
41
42# ifdef DEAL_II_WITH_MPI
51# define DEAL_II_PARTICLE_INDEX_MPI_TYPE MPI_UINT64_T
52# endif
53
54#else
66 using particle_index = unsigned int;
67
68# ifdef DEAL_II_WITH_MPI
73# define DEAL_II_PARTICLE_INDEX_MPI_TYPE MPI_UNSIGNED
74# endif
75#endif
76} // namespace types
77
78namespace Particles
79{
102 template <int dim, int spacedim = dim>
104 {
105 public:
111 using Handle = unsigned int;
112
116 static const Handle invalid_handle;
117
121 PropertyPool(const unsigned int n_properties_per_slot);
122
129
136 void
137 clear();
138
144 Handle
146
151 void
153
158 const Point<spacedim> &
159 get_location(const Handle handle) const;
160
166 get_location(const Handle handle);
167
171 void
172 set_location(const Handle handle, const Point<spacedim> &new_location);
173
178 const Point<dim> &
179 get_reference_location(const Handle handle) const;
180
185 void
187 const Point<dim> &new_reference_location);
188
194 get_id(const Handle handle) const;
195
200 void
201 set_id(const Handle handle, const types::particle_index &new_id);
202
208 get_properties(const Handle handle)
209 {
210 // The implementation is up here inside the class declaration because
211 // NVCC (at least in 12.5 and 12.6) otherwise produce a compile error:
212 //
213 // error: no declaration matches ‘::ArrayView<__remove_cv(const
214 // double)> ::Particles::PropertyPool<dim,
215 // spacedim>::get_properties(Handle)’
216 //
217 // See https://github.com/dealii/dealii/issues/17148
218
219 const std::vector<double>::size_type data_index =
220 (handle != invalid_handle) ? handle * n_properties : 0;
221
222 // Ideally we would need to assert that 'handle' has not been deallocated
223 // by searching through 'currently_available_handles'. However, this
224 // is expensive and this function is performance critical, so instead
225 // just check against the array range, and rely on the fact
226 // that handles are invalidated when handed over to
227 // deallocate_properties_array().
228 Assert(data_index <= properties.size() - n_properties,
230 "Invalid property handle. This can happen if the "
231 "handle was duplicated and then one copy was deallocated "
232 "before trying to access the properties."));
233
234 return ArrayView<double>(properties.data() + data_index, n_properties);
235 }
236
237
242 void
243 reserve(const std::size_t size);
244
248 unsigned int
249 n_properties_per_slot() const;
250
255 unsigned int
256 n_slots() const;
257
261 unsigned int
262 n_registered_slots() const;
263
272 void
273 sort_memory_slots(const std::vector<Handle> &handles_to_sort);
274
275 private:
279 const unsigned int n_properties;
280
286 std::vector<Point<spacedim>> locations;
287
293 std::vector<Point<dim>> reference_locations;
294
300 std::vector<types::particle_index> ids;
301
307 std::vector<double> properties;
308
316 std::vector<Handle> currently_available_handles;
317 };
318
319
320
321 /* ---------------------- inline and template functions ------------------ */
322
323 template <int dim, int spacedim>
324 inline const Point<spacedim> &
326 {
327 const std::vector<double>::size_type data_index =
328 (handle != invalid_handle) ? handle : 0;
329
330 // Ideally we would need to assert that 'handle' has not been deallocated
331 // by searching through 'currently_available_handles'. However, this
332 // is expensive and this function is performance critical, so instead
333 // just check against the array range, and rely on the fact
334 // that handles are invalidated when handed over to
335 // deallocate_properties_array().
336 Assert(data_index <= locations.size() - 1,
337 ExcMessage("Invalid location handle. This can happen if the "
338 "handle was duplicated and then one copy was deallocated "
339 "before trying to access the properties."));
340
341 return locations[data_index];
342 }
343
344
345
346 template <int dim, int spacedim>
347 inline Point<spacedim> &
349 {
350 const std::vector<double>::size_type data_index =
351 (handle != invalid_handle) ? handle : 0;
352
353 // Ideally we would need to assert that 'handle' has not been deallocated
354 // by searching through 'currently_available_handles'. However, this
355 // is expensive and this function is performance critical, so instead
356 // just check against the array range, and rely on the fact
357 // that handles are invalidated when handed over to
358 // deallocate_properties_array().
359 Assert(data_index <= locations.size() - 1,
360 ExcMessage("Invalid location handle. This can happen if the "
361 "handle was duplicated and then one copy was deallocated "
362 "before trying to access the properties."));
363
364 return locations[data_index];
365 }
366
367
368
369 template <int dim, int spacedim>
370 inline void
372 const Point<spacedim> &new_location)
373 {
374 const std::vector<double>::size_type data_index =
375 (handle != invalid_handle) ? handle : 0;
376
377 // Ideally we would need to assert that 'handle' has not been deallocated
378 // by searching through 'currently_available_handles'. However, this
379 // is expensive and this function is performance critical, so instead
380 // just check against the array range, and rely on the fact
381 // that handles are invalidated when handed over to
382 // deallocate_properties_array().
383 Assert(data_index <= locations.size() - 1,
384 ExcMessage("Invalid location handle. This can happen if the "
385 "handle was duplicated and then one copy was deallocated "
386 "before trying to access the properties."));
387
388 locations[data_index] = new_location;
389 }
390
391
392
393 template <int dim, int spacedim>
394 inline const Point<dim> &
396 {
397 const std::vector<double>::size_type data_index =
398 (handle != invalid_handle) ? handle : 0;
399
400 // Ideally we would need to assert that 'handle' has not been deallocated
401 // by searching through 'currently_available_handles'. However, this
402 // is expensive and this function is performance critical, so instead
403 // just check against the array range, and rely on the fact
404 // that handles are invalidated when handed over to
405 // deallocate_properties_array().
406 Assert(data_index <= reference_locations.size() - 1,
407 ExcMessage("Invalid location handle. This can happen if the "
408 "handle was duplicated and then one copy was deallocated "
409 "before trying to access the properties."));
410
411 return reference_locations[data_index];
412 }
413
414
415
416 template <int dim, int spacedim>
417 inline void
419 const Handle handle,
420 const Point<dim> &new_reference_location)
421 {
422 const std::vector<double>::size_type data_index =
423 (handle != invalid_handle) ? handle : 0;
424
425 // Ideally we would need to assert that 'handle' has not been deallocated
426 // by searching through 'currently_available_handles'. However, this
427 // is expensive and this function is performance critical, so instead
428 // just check against the array range, and rely on the fact
429 // that handles are invalidated when handed over to
430 // deallocate_properties_array().
431 Assert(data_index <= reference_locations.size() - 1,
432 ExcMessage("Invalid location handle. This can happen if the "
433 "handle was duplicated and then one copy was deallocated "
434 "before trying to access the properties."));
435
436 reference_locations[data_index] = new_reference_location;
437 }
438
439
440
441 template <int dim, int spacedim>
444 {
445 const std::vector<double>::size_type data_index =
446 (handle != invalid_handle) ? handle : 0;
447
448 // Ideally we would need to assert that 'handle' has not been deallocated
449 // by searching through 'currently_available_handles'. However, this
450 // is expensive and this function is performance critical, so instead
451 // just check against the array range, and rely on the fact
452 // that handles are invalidated when handed over to
453 // deallocate_properties_array().
454 Assert(data_index <= ids.size() - 1,
455 ExcMessage("Invalid location handle. This can happen if the "
456 "handle was duplicated and then one copy was deallocated "
457 "before trying to access the properties."));
458
459 return ids[data_index];
460 }
461
462
463
464 template <int dim, int spacedim>
465 inline void
467 const types::particle_index &new_id)
468 {
469 const std::vector<double>::size_type data_index =
470 (handle != invalid_handle) ? handle : 0;
471
472 // Ideally we would need to assert that 'handle' has not been deallocated
473 // by searching through 'currently_available_handles'. However, this
474 // is expensive and this function is performance critical, so instead
475 // just check against the array range, and rely on the fact
476 // that handles are invalidated when handed over to
477 // deallocate_properties_array().
478 Assert(data_index <= ids.size() - 1,
479 ExcMessage("Invalid location handle. This can happen if the "
480 "handle was duplicated and then one copy was deallocated "
481 "before trying to access the properties."));
482
483 ids[data_index] = new_id;
484 }
485
486
487
488 // template <int dim, int spacedim>
489 // inline ArrayView<double, ::MemorySpace::Host>
490 // PropertyPool<dim, spacedim>::get_properties(const Handle handle)
491
492
493
494 template <int dim, int spacedim>
495 inline unsigned int
497 {
498 return locations.size();
499 }
500
501
502} // namespace Particles
503
505
506#endif
const Point< spacedim > & get_location(const Handle handle) const
std::vector< Point< dim > > reference_locations
unsigned int n_properties_per_slot() const
unsigned int n_registered_slots() const
unsigned int n_slots() const
const unsigned int n_properties
void deregister_particle(Handle &handle)
void set_id(const Handle handle, const types::particle_index &new_id)
types::particle_index get_id(const Handle handle) const
void set_reference_location(const Handle handle, const Point< dim > &new_reference_location)
void reserve(const std::size_t size)
void set_location(const Handle handle, const Point< spacedim > &new_location)
const Point< dim > & get_reference_location(const Handle handle) const
void sort_memory_slots(const std::vector< Handle > &handles_to_sort)
Point< spacedim > & get_location(const Handle handle)
std::vector< Point< spacedim > > locations
std::vector< types::particle_index > ids
ArrayView< double, ::MemorySpace::Host > get_properties(const Handle handle)
static const Handle invalid_handle
std::vector< Handle > currently_available_handles
std::vector< double > properties
Definition point.h:111
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::size_t size
Definition mpi.cc:733
Definition types.h:30
unsigned int particle_index