Reference documentation for deal.II version 9.3.3
\(\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.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_particle_h
17#define dealii_particles_particle_h
18
19#include <deal.II/base/config.h>
20
22#include <deal.II/base/point.h>
23#include <deal.II/base/types.h>
24
26
27#include <cstdint>
28
30
35namespace Particles
36{
37 namespace internal
38 {
42 using LevelInd = std::pair<int, int>;
43 } // namespace internal
44
93 template <int dim, int spacedim = dim>
95 {
96 public:
101 Particle();
102
116 Particle(const Point<spacedim> & location,
117 const Point<dim> & reference_location,
118 const types::particle_index id);
119
127 Particle(const Particle<dim, spacedim> &particle);
128
153 Particle(const void *& begin_data,
155
160 Particle(Particle<dim, spacedim> &&particle) noexcept;
161
166 operator=(const Particle<dim, spacedim> &particle);
167
172 operator=(Particle<dim, spacedim> &&particle) noexcept;
173
180 ~Particle();
181
196 void *
197 write_particle_data_to_memory(void *data) const;
198
199
217 const void *
218 read_particle_data_from_memory(const void *data);
219
237 void
238 set_location(const Point<spacedim> &new_location);
239
245 const Point<spacedim> &
247
265 void
266 set_reference_location(const Point<dim> &new_reference_location);
267
271 const Point<dim> &
273
283 get_id() const;
284
310 void
312
325 void
327
332 bool
334
352 void
353 set_properties(const ArrayView<const double> &new_properties);
354
367
376
382 std::size_t
384
390 template <class Archive>
391 void
392 save(Archive &ar, const unsigned int version) const;
393
403 template <class Archive>
404 void
405 load(Archive &ar, const unsigned int version);
406
410 void
412
413#ifdef DOXYGEN
419 template <class Archive>
420 void
421 serialize(Archive &archive, const unsigned int version);
422#else
423 // This macro defines the serialize() method that is compatible with
424 // the templated save() and load() method that have been implemented.
425 BOOST_SERIALIZATION_SPLIT_MEMBER()
426#endif
427
428 private:
434
440
445 };
446
447
448
449 /* ---------------------- inline and template functions ------------------ */
450
451 template <int dim, int spacedim>
452 template <class Archive>
453 inline void
454 Particle<dim, spacedim>::load(Archive &ar, const unsigned int)
455 {
456 unsigned int n_properties = 0;
457
458 Point<spacedim> location;
459 Point<dim> reference_location;
461 ar &location &reference_location &id &n_properties;
462
463 set_location(location);
464 set_reference_location(reference_location);
465 set_id(id);
466
467 if (n_properties > 0)
468 {
469 ArrayView<double> properties(get_properties());
470 Assert(
471 properties.size() == n_properties,
473 "This particle was serialized with " +
474 std::to_string(n_properties) +
475 " properties, but the new property handler provides space for " +
476 std::to_string(properties.size()) +
477 " properties. Deserializing a particle only works for matching property sizes."));
478
479 ar &boost::serialization::make_array(properties.data(), n_properties);
480 }
481 }
482
483
484
485 template <int dim, int spacedim>
486 template <class Archive>
487 inline void
488 Particle<dim, spacedim>::save(Archive &ar, const unsigned int) const
489 {
490 unsigned int n_properties = 0;
491 if ((property_pool != nullptr) &&
492 (property_pool_handle != PropertyPool<dim, spacedim>::invalid_handle))
493 n_properties = get_properties().size();
494
495 Point<spacedim> location = get_location();
496 Point<dim> reference_location = get_reference_location();
497 types::particle_index id = get_id();
498
499 ar &location &reference_location &id &n_properties;
500
501 if (n_properties > 0)
502 ar &boost::serialization::make_array(get_properties().data(),
503 n_properties);
504 }
505
506
507
508 template <int dim, int spacedim>
509 inline void
511 {
512 property_pool->set_location(property_pool_handle, new_loc);
513 }
514
515
516
517 template <int dim, int spacedim>
518 inline const Point<spacedim> &
520 {
521 return property_pool->get_location(property_pool_handle);
522 }
523
524
525
526 template <int dim, int spacedim>
527 inline void
529 {
530 property_pool->set_reference_location(property_pool_handle, new_loc);
531 }
532
533
534
535 template <int dim, int spacedim>
536 inline const Point<dim> &
538 {
539 return property_pool->get_reference_location(property_pool_handle);
540 }
541
542
543
544 template <int dim, int spacedim>
547 {
548 return property_pool->get_id(property_pool_handle);
549 }
550
551
552
553 template <int dim, int spacedim>
554 inline void
556 {
557 property_pool->set_id(property_pool_handle, new_id);
558 }
559
560
561
562 template <int dim, int spacedim>
563 inline void
565 PropertyPool<dim, spacedim> &new_property_pool)
566 {
567 // First, we do want to save any properties that may
568 // have previously been set, and copy them over to the memory allocated
569 // on the new pool.
570 //
571 // It is possible that a particle currently has no properties -- for
572 // example if it has been created without an associated property
573 // pool (i.e., uses the default global pool which does not store any
574 // properties) but that the new pool has properties. In that case,
575 // there is simply nothing to transfer -- but the register_particle()
576 // call here will make sure that the newly allocated properties are
577 // zero-initialized.
578 const typename PropertyPool<dim, spacedim>::Handle new_handle =
579 new_property_pool.register_particle();
580
581 const Point<spacedim> location = get_location();
582 const Point<dim> reference_location = get_reference_location();
583 const types::particle_index id = get_id();
584
585 if (/* old pool */ has_properties())
586 {
587 ArrayView<const double> old_properties = this->get_properties();
588 ArrayView<double> new_properties =
589 new_property_pool.get_properties(new_handle);
590 std::copy(old_properties.cbegin(),
591 old_properties.cend(),
592 new_properties.begin());
593 }
594
595 // Now release the old memory handle
596 property_pool->deregister_particle(property_pool_handle);
597
598
599 // Then set the pointer to the property pool we want to use. Also set the
600 // handle to any properties.
601 property_pool = &new_property_pool;
602 property_pool_handle = new_handle;
603
604 // Now also store the saved locations
605 set_location(location);
606 set_reference_location(reference_location);
607 set_id(id);
608 }
609
610
611
612 template <int dim, int spacedim>
613 inline const ArrayView<const double>
615 {
616 if (has_properties() == false)
617 return {};
618 else
619 return property_pool->get_properties(property_pool_handle);
620 }
621
622
623
624 template <int dim, int spacedim>
625 inline bool
627 {
628 // Particles always have a property pool associated with them,
629 // but we can access properties only if there is a valid handle.
630 // The only way a particle can have no valid handle if it has
631 // been moved-from -- but that leaves an object in an invalid
632 // state, and so we can just assert that that can't be the case.
633 Assert((property_pool_handle !=
636 return (property_pool->n_properties_per_slot() > 0);
637 }
638
639} // namespace Particles
640
642
643
644namespace boost
645{
646 namespace geometry
647 {
648 namespace index
649 {
650 // Forward declaration of bgi::indexable
651 template <class T>
652 struct indexable;
653
657 template <int dim, int spacedim>
658 struct indexable<::Particles::Particle<dim, spacedim>>
659 {
664 using result_type = const ::Point<spacedim> &;
665
668 const ::Particles::Particle<dim, spacedim> &particle) const
669 {
670 return particle.get_location();
671 }
672 };
673
674 } // namespace index
675 } // namespace geometry
676} // namespace boost
677
678#endif
iterator begin() const
Definition: array_view.h:583
const_iterator cend() const
Definition: array_view.h:610
const_iterator cbegin() const
Definition: array_view.h:601
value_type * data() const noexcept
Definition: array_view.h:551
std::size_t size() const
Definition: array_view.h:574
PropertyPool< dim, spacedim > * property_pool
Definition: particle.h:439
void set_property_pool(PropertyPool< dim, spacedim > &property_pool)
Definition: particle.h:564
const Point< dim > & get_reference_location() const
Definition: particle.h:537
const Point< spacedim > & get_location() const
Definition: particle.h:519
bool has_properties() const
Definition: particle.h:626
static PropertyPool< dim, spacedim > global_property_pool
Definition: particle.h:433
void set_reference_location(const Point< dim > &new_reference_location)
Definition: particle.h:528
std::size_t serialized_size_in_bytes() const
Definition: particle.cc:287
void save(Archive &ar, const unsigned int version) const
Definition: particle.h:488
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
const ArrayView< const double > get_properties() const
Definition: particle.h:614
void * write_particle_data_to_memory(void *data) const
Definition: particle.cc:219
types::particle_index get_id() const
Definition: particle.h:546
PropertyPool< dim, spacedim >::Handle property_pool_handle
Definition: particle.h:444
void load(Archive &ar, const unsigned int version)
Definition: particle.h:454
void free_properties()
Definition: particle.cc:204
void set_id(const types::particle_index &new_id)
Definition: particle.h:555
void serialize(Archive &archive, const unsigned int version)
void set_location(const Point< spacedim > &new_location)
Definition: particle.h:510
ArrayView< double > get_properties(const Handle handle)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:402
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:403
#define Assert(cond, exc)
Definition: exceptions.h:1465
std::string to_string(const T &t)
Definition: patterns.h:2329
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
std::pair< int, int > LevelInd
Definition: particle.h:42
void copy(const T *begin, const T *end, U *dest)
result_type operator()(const ::Particles::Particle< dim, spacedim > &particle) const
Definition: particle.h:667