Reference documentation for deal.II version 9.6.0
\(\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
particle.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2017 - 2024 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_particles_particle_h
16#define dealii_particles_particle_h
17
18#include <deal.II/base/config.h>
19
21#include <deal.II/base/point.h>
22#include <deal.II/base/types.h>
23
25
26#include <boost/serialization/array.hpp>
27
28#include <cstdint>
29
31
36namespace Particles
37{
38 namespace internal
39 {
43 using LevelInd = std::pair<int, int>;
44 } // namespace internal
45
92 template <int dim, int spacedim = dim>
94 {
95 public:
100 Particle();
101
115 Particle(const Point<spacedim> &location,
116 const Point<dim> &reference_location,
117 const types::particle_index id);
118
126 Particle(const Particle<dim, spacedim> &particle);
127
152 Particle(const void *&begin_data,
154
159 Particle(Particle<dim, spacedim> &&particle) noexcept;
160
165 operator=(const Particle<dim, spacedim> &particle);
166
171 operator=(Particle<dim, spacedim> &&particle) noexcept;
172
179 ~Particle();
180
195 void *
196 write_particle_data_to_memory(void *data) const;
197
198
216 const void *
217 read_particle_data_from_memory(const void *data);
218
239 void
240 set_location(const Point<spacedim> &new_location);
241
247 const Point<spacedim> &
248 get_location() const;
249
272 get_location();
273
294 void
295 set_reference_location(const Point<dim> &new_reference_location);
296
300 const Point<dim> &
302
312 get_id() const;
313
342 void
343 set_id(const types::particle_index &new_id);
344
357 void
359
364 bool
365 has_properties() const;
366
387 void
388 set_properties(const ArrayView<const double> &new_properties);
389
402
410 get_properties() const;
411
417 std::size_t
419
425 template <class Archive>
426 void
427 save(Archive &ar, const unsigned int version) const;
428
438 template <class Archive>
439 void
440 load(Archive &ar, const unsigned int version);
441
445 void
447
448#ifdef DOXYGEN
454 template <class Archive>
455 void
456 serialize(Archive &archive, const unsigned int version);
457#else
458 // This macro defines the serialize() method that is compatible with
459 // the templated save() and load() method that have been implemented.
460 BOOST_SERIALIZATION_SPLIT_MEMBER()
461#endif
462
463 private:
469
475
480 };
481
482
483
484 /* ---------------------- inline and template functions ------------------ */
485
486 template <int dim, int spacedim>
487 template <class Archive>
488 inline void
489 Particle<dim, spacedim>::load(Archive &ar, const unsigned int)
490 {
491 unsigned int n_properties = 0;
492
493 Point<spacedim> location;
494 Point<dim> reference_location;
496 ar &location &reference_location &id &n_properties;
497
498 set_location(location);
499 set_reference_location(reference_location);
500 set_id(id);
501
502 if (n_properties > 0)
503 {
504 ArrayView<double> properties(get_properties());
505 Assert(
506 properties.size() == n_properties,
508 "This particle was serialized with " +
509 std::to_string(n_properties) +
510 " properties, but the new property handler provides space for " +
511 std::to_string(properties.size()) +
512 " properties. Deserializing a particle only works for matching property sizes."));
513
514 ar &boost::serialization::make_array(properties.data(), n_properties);
515 }
516 }
517
518
519
520 template <int dim, int spacedim>
521 template <class Archive>
522 inline void
523 Particle<dim, spacedim>::save(Archive &ar, const unsigned int) const
524 {
525 unsigned int n_properties = 0;
526 if ((property_pool != nullptr) &&
527 (property_pool_handle != PropertyPool<dim, spacedim>::invalid_handle))
528 n_properties = get_properties().size();
529
530 Point<spacedim> location = get_location();
531 Point<dim> reference_location = get_reference_location();
532 types::particle_index id = get_id();
533
534 ar &location &reference_location &id &n_properties;
535
536 if (n_properties > 0)
537 ar &boost::serialization::make_array(get_properties().data(),
538 n_properties);
539 }
540
541
542
543 template <int dim, int spacedim>
544 inline void
546 {
547 property_pool->set_location(property_pool_handle, new_loc);
548 }
549
550
551
552 template <int dim, int spacedim>
553 inline const Point<spacedim> &
555 {
556 return property_pool->get_location(property_pool_handle);
557 }
558
559
560
561 template <int dim, int spacedim>
562 inline Point<spacedim> &
564 {
565 return property_pool->get_location(property_pool_handle);
566 }
567
568
569
570 template <int dim, int spacedim>
571 inline void
573 {
574 property_pool->set_reference_location(property_pool_handle, new_loc);
575 }
576
577
578
579 template <int dim, int spacedim>
580 inline const Point<dim> &
582 {
583 return property_pool->get_reference_location(property_pool_handle);
584 }
585
586
587
588 template <int dim, int spacedim>
591 {
592 return property_pool->get_id(property_pool_handle);
593 }
594
595
596
597 template <int dim, int spacedim>
598 inline void
600 {
601 property_pool->set_id(property_pool_handle, new_id);
602 }
603
604
605
606 template <int dim, int spacedim>
607 inline void
609 PropertyPool<dim, spacedim> &new_property_pool)
610 {
611 // First, we do want to save any properties that may
612 // have previously been set, and copy them over to the memory allocated
613 // on the new pool.
614 //
615 // It is possible that a particle currently has no properties -- for
616 // example if it has been created without an associated property
617 // pool (i.e., uses the default global pool which does not store any
618 // properties) but that the new pool has properties. In that case,
619 // there is simply nothing to transfer -- but the register_particle()
620 // call here will make sure that the newly allocated properties are
621 // zero-initialized.
622 const typename PropertyPool<dim, spacedim>::Handle new_handle =
623 new_property_pool.register_particle();
624
625 const Point<spacedim> location = get_location();
626 const Point<dim> reference_location = get_reference_location();
627 const types::particle_index id = get_id();
628
629 if (/* old pool */ has_properties())
630 {
631 ArrayView<const double> old_properties = this->get_properties();
632 ArrayView<double> new_properties =
633 new_property_pool.get_properties(new_handle);
634 std::copy(old_properties.cbegin(),
635 old_properties.cend(),
636 new_properties.begin());
637 }
638
639 // Now release the old memory handle
640 property_pool->deregister_particle(property_pool_handle);
641
642
643 // Then set the pointer to the property pool we want to use. Also set the
644 // handle to any properties.
645 property_pool = &new_property_pool;
646 property_pool_handle = new_handle;
647
648 // Now also store the saved locations
649 set_location(location);
650 set_reference_location(reference_location);
651 set_id(id);
652 }
653
654
655
656 template <int dim, int spacedim>
659 {
660 if (has_properties() == false)
661 return {};
662 else
663 return property_pool->get_properties(property_pool_handle);
664 }
665
666
667
668 template <int dim, int spacedim>
669 inline bool
671 {
672 // Particles always have a property pool associated with them,
673 // but we can access properties only if there is a valid handle.
674 // The only way a particle can have no valid handle if it has
675 // been moved-from -- but that leaves an object in an invalid
676 // state, and so we can just assert that that can't be the case.
677 Assert((property_pool_handle !=
680 return (property_pool->n_properties_per_slot() > 0);
681 }
682
683} // namespace Particles
684
686
687
688namespace boost
689{
690 namespace geometry
691 {
692 namespace index
693 {
694 // Forward declaration of bgi::indexable
695 template <class T>
696 struct indexable;
697
701 template <int dim, int spacedim>
702 struct indexable<::Particles::Particle<dim, spacedim>>
703 {
708 using result_type = const ::Point<spacedim> &;
709
712 const ::Particles::Particle<dim, spacedim> &particle) const
713 {
714 return particle.get_location();
715 }
716 };
717
718 } // namespace index
719 } // namespace geometry
720} // namespace boost
721
722#endif
iterator begin() const
Definition array_view.h:702
const_iterator cend() const
Definition array_view.h:729
const_iterator cbegin() const
Definition array_view.h:720
value_type * data() const noexcept
Definition array_view.h:661
std::size_t size() const
Definition array_view.h:684
PropertyPool< dim, spacedim > * property_pool
Definition particle.h:474
void set_property_pool(PropertyPool< dim, spacedim > &property_pool)
Definition particle.h:608
const Point< dim > & get_reference_location() const
Definition particle.h:581
const Point< spacedim > & get_location() const
Definition particle.h:554
bool has_properties() const
Definition particle.h:670
static PropertyPool< dim, spacedim > global_property_pool
Definition particle.h:468
void set_reference_location(const Point< dim > &new_reference_location)
Definition particle.h:572
std::size_t serialized_size_in_bytes() const
Definition particle.cc:286
void save(Archive &ar, const unsigned int version) const
Definition particle.h:523
Particle< dim, spacedim > & operator=(const Particle< dim, spacedim > &particle)
Definition particle.cc:129
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:251
void * write_particle_data_to_memory(void *data) const
Definition particle.cc:218
types::particle_index get_id() const
Definition particle.h:590
ArrayView< double > get_properties()
Definition particle.cc:330
PropertyPool< dim, spacedim >::Handle property_pool_handle
Definition particle.h:479
void load(Archive &ar, const unsigned int version)
Definition particle.h:489
void set_id(const types::particle_index &new_id)
Definition particle.h:599
void serialize(Archive &archive, const unsigned int version)
void set_location(const Point< spacedim > &new_location)
Definition particle.h:545
const Point< spacedim > & get_location(const Handle handle) const
unsigned int n_properties_per_slot() const
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 set_location(const Handle handle, const Point< spacedim > &new_location)
const Point< dim > & get_reference_location(const Handle handle) const
ArrayView< double, ::MemorySpace::Host > get_properties(const Handle handle)
Definition point.h:111
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:503
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:504
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
std::pair< int, int > LevelInd
Definition particle.h:43
result_type operator()(const ::Particles::Particle< dim, spacedim > &particle) const
Definition particle.h:711