| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2017 - 2025 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_accessor_h |
| 16 |
#define dealii_particles_particle_accessor_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#include <deal.II/base/array_view.h> |
| 21 |
|
| 22 |
#include <deal.II/grid/tria.h> |
| 23 |
#include <deal.II/grid/tria_iterator_base.h> |
| 24 |
|
| 25 |
#include <deal.II/particles/particle.h> |
| 26 |
#include <deal.II/particles/property_pool.h> |
| 27 |
|
| 28 |
#include <boost/geometry/index/indexable.hpp> |
| 29 |
#include <boost/serialization/array_wrapper.hpp> |
| 30 |
|
| 31 |
#include <list> |
| 32 |
|
| 33 |
|
| 34 |
DEAL_II_NAMESPACE_OPEN |
| 35 |
|
| 36 |
namespace Particles |
| 37 |
{ |
| 38 |
// Forward declarations |
| 39 |
#ifndef DOXYGEN |
| 40 |
template <int, int> |
| 41 |
class ParticleIterator; |
| 42 |
template <int, int> |
| 43 |
class ParticleHandler; |
| 44 |
#endif |
| 45 |
|
| 46 |
/** |
| 47 |
* Accessor class used by ParticleIterator to access particle data. |
| 48 |
*/ |
| 49 |
template <int dim, int spacedim = dim> |
| 50 |
class ParticleAccessor |
| 51 |
{ |
| 52 |
public: |
| 53 |
/** |
| 54 |
* Data structure to describe the particles in a given cell. This is used |
| 55 |
* inside an std::list in `particle_container`. The storage of this field |
| 56 |
* is typically handled by ParticleHandler, using an std::list of this |
| 57 |
* structure. |
| 58 |
* |
| 59 |
* There are four main reasons for the present design: |
| 60 |
* <ul> |
| 61 |
* <li>The list represents a compact storage for all particles on a cell, |
| 62 |
* handling e.g. the case where only a few cells hold particles without |
| 63 |
* storage overhead.</li> |
| 64 |
* <li>The particles on a cell are identified by their handle object, |
| 65 |
* i.e., a single integer, again ensuring only 4 byte storage cost for the |
| 66 |
* handling of the particles for the case of sufficiently many particles |
| 67 |
* per cell.</li> |
| 68 |
* <li>Combined with a cache object that holds an iterator to the |
| 69 |
* `std::list` of particles (8 bytes per cell), or the `std::list::end` in |
| 70 |
* case no particles are present on a cell, there is a fast access from a |
| 71 |
* ParticleAccessor -> surrounding cell (by access to |
| 72 |
* ParticlesInCell::cell_iterator), as well as fast access from cell -> |
| 73 |
* all particles (through the cache). It also allows for a fast iteration |
| 74 |
* through all particles, by incrementing either the index of particles |
| 75 |
* within a cell, or, if at the end of the cell, to the next element in |
| 76 |
* the outer list. The cache is simple to keep consistent because the |
| 77 |
* iterators into `std::list` remain valid upon insertion or deletion of |
| 78 |
* entries in the list, as specified by `std::list`'s API. |
| 79 |
* <li>In order to detect the start and end of all cells with particles, |
| 80 |
* the std::list contains some dummy entry with a `cell_iterator` past the |
| 81 |
* end of valid cells, which is used as a criterion to terminate the loops |
| 82 |
* of ParticleAccessor, again minimizing the computational cost of handling |
| 83 |
* the loop over particles.</li> |
| 84 |
* </ul> |
| 85 |
*/ |
| 86 |
struct ParticlesInCell |
| 87 |
{ |
| 88 |
/** |
| 89 |
* Default constructor. |
| 90 |
*/ |
| 91 |
ParticlesInCell() = default; |
| 92 |
|
| 93 |
/** |
| 94 |
* Construct from a vector of particles and a cell iterator. |
| 95 |
*/ |
| 96 |
ParticlesInCell( |
| 97 |
const std::vector<typename PropertyPool<dim, spacedim>::Handle> |
| 98 |
&particles, |
| 99 |
const typename Triangulation<dim, spacedim>::active_cell_iterator &cell) |
| 100 |
: particles(particles) |
| 101 |
, cell(cell) |
| 102 |
{} |
| 103 |
|
| 104 |
/** |
| 105 |
* A vector of particles on a cell. |
| 106 |
*/ |
| 107 |
std::vector<typename PropertyPool<dim, spacedim>::Handle> particles; |
| 108 |
|
| 109 |
/** |
| 110 |
* The underlying cell. |
| 111 |
*/ |
| 112 |
typename Triangulation<dim, spacedim>::active_cell_iterator cell; |
| 113 |
}; |
| 114 |
|
| 115 |
/** |
| 116 |
* A type for the storage container for particles. |
| 117 |
*/ |
| 118 |
using particle_container = std::list<ParticlesInCell>; |
| 119 |
|
| 120 |
/** |
| 121 |
* @copydoc Particle::write_particle_data_to_memory |
| 122 |
*/ |
| 123 |
void * |
| 124 |
write_particle_data_to_memory(void *data) const; |
| 125 |
|
| 126 |
|
| 127 |
/** |
| 128 |
* @copydoc Particle::read_particle_data_from_memory |
| 129 |
*/ |
| 130 |
const void * |
| 131 |
read_particle_data_from_memory(const void *data); |
| 132 |
|
| 133 |
/** |
| 134 |
* Set the location of this particle. Note that this does not check |
| 135 |
* whether this is a valid location in the simulation domain. |
| 136 |
* |
| 137 |
* @param [in] new_location The new location for this particle. |
| 138 |
* |
| 139 |
* @note In parallel programs, the ParticleHandler class stores particles |
| 140 |
* on both the locally owned cells, as well as on ghost cells. The |
| 141 |
* particles on the latter are *copies* of particles owned on other |
| 142 |
* processors, and should therefore be treated in the same way as |
| 143 |
* ghost entries in |
| 144 |
* @ref GlossGhostedVector "vectors with ghost elements" |
| 145 |
* or |
| 146 |
* @ref GlossGhostCell "ghost cells": |
| 147 |
* In both cases, one should |
| 148 |
* treat the ghost elements or cells as `const` objects that shouldn't |
| 149 |
* be modified even if the objects allow for calls that modify |
| 150 |
* properties. Rather, properties should only be modified on processors |
| 151 |
* that actually *own* the particle. |
| 152 |
*/ |
| 153 |
void |
| 154 |
set_location(const Point<spacedim> &new_location); |
| 155 |
|
| 156 |
/** |
| 157 |
* Get read-access to the location of this particle. |
| 158 |
* |
| 159 |
* @return The location of this particle. |
| 160 |
*/ |
| 161 |
const Point<spacedim> & |
| 162 |
get_location() const; |
| 163 |
|
| 164 |
/** |
| 165 |
* Get read- and write-access to the location of this particle. |
| 166 |
* Note that changing the location does not check |
| 167 |
* whether this is a valid location in the simulation domain. |
| 168 |
* |
| 169 |
* @note In parallel programs, the ParticleHandler class stores particles |
| 170 |
* on both the locally owned cells, as well as on ghost cells. The |
| 171 |
* particles on the latter are *copies* of particles owned on other |
| 172 |
* processors, and should therefore be treated in the same way as |
| 173 |
* ghost entries in |
| 174 |
* @ref GlossGhostedVector "vectors with ghost elements" |
| 175 |
* or |
| 176 |
* @ref GlossGhostCell "ghost cells": |
| 177 |
* In both cases, one should |
| 178 |
* treat the ghost elements or cells as `const` objects that shouldn't |
| 179 |
* be modified even if the objects allow for calls that modify |
| 180 |
* properties. Rather, properties should only be modified on processors |
| 181 |
* that actually *own* the particle. |
| 182 |
* |
| 183 |
* @return The location of this particle. |
| 184 |
*/ |
| 185 |
Point<spacedim> & |
| 186 |
get_location(); |
| 187 |
|
| 188 |
/** |
| 189 |
* Set the reference location of this particle. |
| 190 |
* |
| 191 |
* @param [in] new_reference_location The new reference location for |
| 192 |
* this particle. |
| 193 |
* |
| 194 |
* @note In parallel programs, the ParticleHandler class stores particles |
| 195 |
* on both the locally owned cells, as well as on ghost cells. The |
| 196 |
* particles on the latter are *copies* of particles owned on other |
| 197 |
* processors, and should therefore be treated in the same way as |
| 198 |
* ghost entries in |
| 199 |
* @ref GlossGhostedVector "vectors with ghost elements" |
| 200 |
* or |
| 201 |
* @ref GlossGhostCell "ghost cells": |
| 202 |
* In both cases, one should |
| 203 |
* treat the ghost elements or cells as `const` objects that shouldn't |
| 204 |
* be modified even if the objects allow for calls that modify |
| 205 |
* properties. Rather, properties should only be modified on processors |
| 206 |
* that actually *own* the particle. |
| 207 |
*/ |
| 208 |
void |
| 209 |
set_reference_location(const Point<dim> &new_reference_location); |
| 210 |
|
| 211 |
/** |
| 212 |
* Return the reference location of this particle in its current cell. |
| 213 |
*/ |
| 214 |
const Point<dim> & |
| 215 |
get_reference_location() const; |
| 216 |
|
| 217 |
/** |
| 218 |
* Set the ID number of this particle. |
| 219 |
*/ |
| 220 |
void |
| 221 |
set_id(const types::particle_index &new_id); |
| 222 |
|
| 223 |
/** |
| 224 |
* Return the ID number of this particle. |
| 225 |
*/ |
| 226 |
types::particle_index |
| 227 |
get_id() const; |
| 228 |
|
| 229 |
/** |
| 230 |
* Return a particle ID number local to each MPI process. This number |
| 231 |
* enables the direct array access (similar to |
| 232 |
* LinearAlgebra::distributed::Vector::local_element()) to quantities used |
| 233 |
* for local computations. Use |
| 234 |
* ParticleHandler::get_max_local_particle_index() to query suitable array |
| 235 |
* sizes. |
| 236 |
* |
| 237 |
* @note The number returned by this function is not stable between calls |
| 238 |
* of ParticleHandler::sort_particles_into_subdomains_and_cells() and |
| 239 |
* therefore it cannot be used to track quantities across those |
| 240 |
* calls. Furthermore, the numbers returned by this function are typically |
| 241 |
* not refreshed in case individual particles are removed from the |
| 242 |
* underlying ParticleHandler object, which means that the returned |
| 243 |
* indices of all particles on an MPI process typically do not form a |
| 244 |
* contiguous interval of numbers. Use particle properties for storing |
| 245 |
* persistent information and checkpointing the actual data. |
| 246 |
*/ |
| 247 |
types::particle_index |
| 248 |
get_local_index() const; |
| 249 |
|
| 250 |
/** |
| 251 |
* Return whether this particle has a valid property pool and a valid |
| 252 |
* handle to properties. |
| 253 |
*/ |
| 254 |
bool |
| 255 |
has_properties() const; |
| 256 |
|
| 257 |
/** |
| 258 |
* Set the properties of this particle. |
| 259 |
* |
| 260 |
* @param [in] new_properties A vector containing the |
| 261 |
* new properties for this particle. |
| 262 |
* |
| 263 |
* @note In parallel programs, the ParticleHandler class stores particles |
| 264 |
* on both the locally owned cells, as well as on ghost cells. The |
| 265 |
* particles on the latter are *copies* of particles owned on other |
| 266 |
* processors, and should therefore be treated in the same way as |
| 267 |
* ghost entries in |
| 268 |
* @ref GlossGhostedVector "vectors with ghost elements" |
| 269 |
* or |
| 270 |
* @ref GlossGhostCell "ghost cells": |
| 271 |
* In both cases, one should |
| 272 |
* treat the ghost elements or cells as `const` objects that shouldn't |
| 273 |
* be modified even if the objects allow for calls that modify |
| 274 |
* properties. Rather, properties should only be modified on processors |
| 275 |
* that actually *own* the particle. |
| 276 |
*/ |
| 277 |
void |
| 278 |
set_properties(const std::vector<double> &new_properties); |
| 279 |
|
| 280 |
/** |
| 281 |
* Set the properties of this particle. |
| 282 |
* |
| 283 |
* @param [in] new_properties An ArrayView pointing to memory locations |
| 284 |
* containing the new properties for this particle. |
| 285 |
* |
| 286 |
* @note In parallel programs, the ParticleHandler class stores particles |
| 287 |
* on both the locally owned cells, as well as on ghost cells. The |
| 288 |
* particles on the latter are *copies* of particles owned on other |
| 289 |
* processors, and should therefore be treated in the same way as |
| 290 |
* ghost entries in |
| 291 |
* @ref GlossGhostedVector "vectors with ghost elements" |
| 292 |
* or |
| 293 |
* @ref GlossGhostCell "ghost cells": |
| 294 |
* In both cases, one should |
| 295 |
* treat the ghost elements or cells as `const` objects that shouldn't |
| 296 |
* be modified even if the objects allow for calls that modify |
| 297 |
* properties. Rather, properties should only be modified on processors |
| 298 |
* that actually *own* the particle. |
| 299 |
*/ |
| 300 |
void |
| 301 |
set_properties(const ArrayView<const double> &new_properties); |
| 302 |
|
| 303 |
/** |
| 304 |
* Set the properties of this particle, assuming that the properties |
| 305 |
* stored on this particle correspond to a rank-1 Tensor object. In |
| 306 |
* particular, this means that the number of properties stored on |
| 307 |
* the particle must equal `dim`. |
| 308 |
* |
| 309 |
* @param [in] new_properties A Tensor containing the new properties for |
| 310 |
* this particle. |
| 311 |
* |
| 312 |
* @note In parallel programs, the ParticleHandler class stores particles |
| 313 |
* on both the locally owned cells, as well as on ghost cells. The |
| 314 |
* particles on the latter are *copies* of particles owned on other |
| 315 |
* processors, and should therefore be treated in the same way as |
| 316 |
* ghost entries in |
| 317 |
* @ref GlossGhostedVector "vectors with ghost elements" |
| 318 |
* or |
| 319 |
* @ref GlossGhostCell "ghost cells": |
| 320 |
* In both cases, one should |
| 321 |
* treat the ghost elements or cells as `const` objects that shouldn't |
| 322 |
* be modified even if the objects allow for calls that modify |
| 323 |
* properties. Rather, properties should only be modified on processors |
| 324 |
* that actually *own* the particle. |
| 325 |
*/ |
| 326 |
void |
| 327 |
set_properties(const Tensor<1, dim> &new_properties); |
| 328 |
|
| 329 |
/** |
| 330 |
* Get write-access to properties of this particle. |
| 331 |
* |
| 332 |
* @return An ArrayView of the properties of this particle. |
| 333 |
*/ |
| 334 |
ArrayView<double> |
| 335 |
get_properties() |
| 336 |
{ |
| 337 |
// The implementation is up here inside the class declaration because |
| 338 |
// NVCC (at least in 12.5 and 12.6) otherwise produce a compile error: |
| 339 |
// |
| 340 |
// error: no declaration matches ‘dealii::ArrayView<__remove_cv(const |
| 341 |
// double)> dealii::Particles::ParticleAccessor<dim, |
| 342 |
// spacedim>::get_properties()’ |
| 343 |
// |
| 344 |
// See https://github.com/dealii/dealii/issues/17148 |
| 345 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 346 |
|
| 347 |
return property_pool->get_properties(get_handle()); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* Get read-access to properties of this particle. |
| 353 |
* |
| 354 |
* @return An ArrayView of the properties of this particle. |
| 355 |
*/ |
| 356 |
ArrayView<const double> |
| 357 |
get_properties() const; |
| 358 |
|
| 359 |
/** |
| 360 |
* Return the size in bytes this particle occupies if all of its data is |
| 361 |
* serialized (i.e. the number of bytes that is written by the write_data |
| 362 |
* function of this class). |
| 363 |
*/ |
| 364 |
std::size_t |
| 365 |
serialized_size_in_bytes() const; |
| 366 |
|
| 367 |
/** |
| 368 |
* Get a cell iterator to the cell surrounding the current particle. |
| 369 |
*/ |
| 370 |
const typename Triangulation<dim, spacedim>::cell_iterator & |
| 371 |
get_surrounding_cell() const; |
| 372 |
|
| 373 |
/** |
| 374 |
* Write the data of this object to a stream for the purpose of |
| 375 |
* serialization using the [BOOST serialization |
| 376 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 377 |
*/ |
| 378 |
template <class Archive> |
| 379 |
void |
| 380 |
save(Archive &ar, const unsigned int version) const; |
| 381 |
|
| 382 |
/** |
| 383 |
* Read the data of this object from a stream for the purpose of |
| 384 |
* serialization using the [BOOST serialization |
| 385 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 386 |
*/ |
| 387 |
template <class Archive> |
| 388 |
void |
| 389 |
load(Archive &ar, const unsigned int version); |
| 390 |
|
| 391 |
#ifdef DOXYGEN |
| 392 |
/** |
| 393 |
* Write and read the data of this object from a stream for the purpose |
| 394 |
* of serialization using the [BOOST serialization |
| 395 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 396 |
*/ |
| 397 |
template <class Archive> |
| 398 |
void |
| 399 |
serialize(Archive &archive, const unsigned int version); |
| 400 |
#else |
| 401 |
// This macro defines the serialize() method that is compatible with |
| 402 |
// the templated save() and load() method that have been implemented. |
| 403 |
BOOST_SERIALIZATION_SPLIT_MEMBER() |
| 404 |
#endif |
| 405 |
|
| 406 |
/** |
| 407 |
* Advance the ParticleAccessor to the next particle. |
| 408 |
*/ |
| 409 |
void |
| 410 |
next(); |
| 411 |
|
| 412 |
/** |
| 413 |
* Move the ParticleAccessor to the previous particle. |
| 414 |
*/ |
| 415 |
void |
| 416 |
prev(); |
| 417 |
|
| 418 |
/** |
| 419 |
* Inequality operator. |
| 420 |
*/ |
| 421 |
bool |
| 422 |
operator!=(const ParticleAccessor<dim, spacedim> &other) const; |
| 423 |
|
| 424 |
/** |
| 425 |
* Equality operator. |
| 426 |
*/ |
| 427 |
bool |
| 428 |
operator==(const ParticleAccessor<dim, spacedim> &other) const; |
| 429 |
|
| 430 |
/** |
| 431 |
* Return the state of the accessor. |
| 432 |
*/ |
| 433 |
IteratorState::IteratorStates |
| 434 |
state() const; |
| 435 |
|
| 436 |
private: |
| 437 |
/** |
| 438 |
* Construct an invalid accessor. Such an object is not usable. |
| 439 |
*/ |
| 440 |
ParticleAccessor(); |
| 441 |
|
| 442 |
/** |
| 443 |
* Construct an accessor from a reference to a container, an iterator to the |
| 444 |
* current cell, and the particle index within that cell. |
| 445 |
* This constructor is `private` so that it can only be accessed by |
| 446 |
* friend classes. |
| 447 |
*/ |
| 448 |
ParticleAccessor( |
| 449 |
const typename particle_container::iterator particles_in_cell, |
| 450 |
const PropertyPool<dim, spacedim> &property_pool, |
| 451 |
const unsigned int particle_index_within_cell); |
| 452 |
|
| 453 |
/** |
| 454 |
* Returns a reference to the current Particle. Because the |
| 455 |
* internal structure may change this is not intended for public use and |
| 456 |
* only a convenience function for internal purposes. |
| 457 |
*/ |
| 458 |
const typename PropertyPool<dim, spacedim>::Handle & |
| 459 |
get_handle() const; |
| 460 |
|
| 461 |
/** |
| 462 |
* Non-@p const version of the function with the same name above. |
| 463 |
*/ |
| 464 |
typename PropertyPool<dim, spacedim>::Handle & |
| 465 |
get_handle(); |
| 466 |
|
| 467 |
/** |
| 468 |
* An iterator to the particles in the current cell within the |
| 469 |
* particle_container object. |
| 470 |
*/ |
| 471 |
typename particle_container::iterator particles_in_cell; |
| 472 |
|
| 473 |
/** |
| 474 |
* A pointer to the property pool that stores the actual particle data. |
| 475 |
*/ |
| 476 |
PropertyPool<dim, spacedim> *property_pool; |
| 477 |
|
| 478 |
/** |
| 479 |
* Local index of the particle within its current cell. |
| 480 |
*/ |
| 481 |
unsigned int particle_index_within_cell; |
| 482 |
|
| 483 |
// Make ParticleIterator a friend to allow it constructing |
| 484 |
// ParticleAccessors. |
| 485 |
template <int, int> |
| 486 |
friend class ParticleIterator; |
| 487 |
template <int, int> |
| 488 |
friend class ParticleHandler; |
| 489 |
}; |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
template <int dim, int spacedim> |
| 494 |
template <class Archive> |
| 495 |
inline void |
| 496 |
ParticleAccessor<dim, spacedim>::load(Archive &ar, const unsigned int) |
| 497 |
{ |
| 498 |
unsigned int n_properties = 0; |
| 499 |
|
| 500 |
Point<spacedim> location; |
| 501 |
Point<dim> reference_location; |
| 502 |
types::particle_index id; |
| 503 |
ar &location &reference_location &id &n_properties; |
| 504 |
|
| 505 |
set_location(location); |
| 506 |
set_reference_location(reference_location); |
| 507 |
set_id(id); |
| 508 |
|
| 509 |
if (n_properties > 0) |
| 510 |
{ |
| 511 |
ArrayView<double> properties(get_properties()); |
| 512 |
Assert( |
| 513 |
properties.size() == n_properties, |
| 514 |
ExcMessage( |
| 515 |
"This particle was serialized with " + |
| 516 |
std::to_string(n_properties) + |
| 517 |
" properties, but the new property handler provides space for " + |
| 518 |
std::to_string(properties.size()) + |
| 519 |
" properties. Deserializing a particle only works for matching property sizes.")); |
| 520 |
|
| 521 |
ar &boost::serialization::make_array(properties.data(), n_properties); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
template <int dim, int spacedim> |
| 528 |
template <class Archive> |
| 529 |
inline void |
| 530 |
ParticleAccessor<dim, spacedim>::save(Archive &ar, const unsigned int) const |
| 531 |
{ |
| 532 |
unsigned int n_properties = 0; |
| 533 |
if ((property_pool != nullptr) && |
| 534 |
(get_handle() != PropertyPool<dim, spacedim>::invalid_handle)) |
| 535 |
n_properties = get_properties().size(); |
| 536 |
|
| 537 |
Point<spacedim> location = get_location(); |
| 538 |
Point<dim> reference_location = get_reference_location(); |
| 539 |
types::particle_index id = get_id(); |
| 540 |
|
| 541 |
ar &location &reference_location &id &n_properties; |
| 542 |
|
| 543 |
if (n_properties > 0) |
| 544 |
ar &boost::serialization::make_array(get_properties().data(), |
| 545 |
n_properties); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
// ------------------------- inline functions ------------------------------ |
| 550 |
|
| 551 |
template <int dim, int spacedim> |
| 552 |
inline ParticleAccessor<dim, spacedim>::ParticleAccessor() |
| 553 |
: particles_in_cell(typename particle_container::iterator()) |
| 554 |
, property_pool(nullptr) |
| 555 |
, particle_index_within_cell(numbers::invalid_unsigned_int) |
| 556 |
{} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
template <int dim, int spacedim> |
| 561 |
inline ParticleAccessor<dim, spacedim>::ParticleAccessor( |
| 562 |
const typename particle_container::iterator particles_in_cell, |
| 563 |
const PropertyPool<dim, spacedim> &property_pool, |
| 564 |
const unsigned int particle_index_within_cell) |
| 565 |
: particles_in_cell(particles_in_cell) |
| 566 |
, property_pool(const_cast<PropertyPool<dim, spacedim> *>(&property_pool)) |
| 567 |
, particle_index_within_cell(particle_index_within_cell) |
| 568 |
{} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
template <int dim, int spacedim> |
| 573 |
inline const void * |
| 574 |
ParticleAccessor<dim, spacedim>::read_particle_data_from_memory( |
| 575 |
const void *data) |
| 576 |
{ |
| 577 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 578 |
|
| 579 |
const types::particle_index *id_data = |
| 580 |
static_cast<const types::particle_index *>(data); |
| 581 |
set_id(*id_data++); |
| 582 |
const double *pdata = reinterpret_cast<const double *>(id_data); |
| 583 |
|
| 584 |
Point<spacedim> location; |
| 585 |
for (unsigned int i = 0; i < spacedim; ++i) |
| 586 |
location[i] = *pdata++; |
| 587 |
set_location(location); |
| 588 |
|
| 589 |
Point<dim> reference_location; |
| 590 |
for (unsigned int i = 0; i < dim; ++i) |
| 591 |
reference_location[i] = *pdata++; |
| 592 |
set_reference_location(reference_location); |
| 593 |
|
| 594 |
// See if there are properties to load |
| 595 |
if (has_properties()) |
| 596 |
{ |
| 597 |
const ArrayView<double> particle_properties = |
| 598 |
property_pool->get_properties(get_handle()); |
| 599 |
const unsigned int size = particle_properties.size(); |
| 600 |
for (unsigned int i = 0; i < size; ++i) |
| 601 |
particle_properties[i] = *pdata++; |
| 602 |
} |
| 603 |
|
| 604 |
return static_cast<const void *>(pdata); |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
template <int dim, int spacedim> |
| 610 |
inline void * |
| 611 |
ParticleAccessor<dim, spacedim>::write_particle_data_to_memory( |
| 612 |
void *data) const |
| 613 |
{ |
| 614 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 615 |
|
| 616 |
types::particle_index *id_data = static_cast<types::particle_index *>(data); |
| 617 |
*id_data = get_id(); |
| 618 |
++id_data; |
| 619 |
double *pdata = reinterpret_cast<double *>(id_data); |
| 620 |
|
| 621 |
// Write location |
| 622 |
for (unsigned int i = 0; i < spacedim; ++i, ++pdata) |
| 623 |
*pdata = get_location()[i]; |
| 624 |
|
| 625 |
// Write reference location |
| 626 |
for (unsigned int i = 0; i < dim; ++i, ++pdata) |
| 627 |
*pdata = get_reference_location()[i]; |
| 628 |
|
| 629 |
// Write properties |
| 630 |
if (has_properties()) |
| 631 |
{ |
| 632 |
const ArrayView<double> particle_properties = |
| 633 |
property_pool->get_properties(get_handle()); |
| 634 |
for (unsigned int i = 0; i < particle_properties.size(); ++i, ++pdata) |
| 635 |
*pdata = particle_properties[i]; |
| 636 |
} |
| 637 |
|
| 638 |
return static_cast<void *>(pdata); |
| 639 |
} |
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
template <int dim, int spacedim> |
| 644 |
inline void |
| 645 |
ParticleAccessor<dim, spacedim>::set_location(const Point<spacedim> &new_loc) |
| 646 |
{ |
| 647 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 648 |
|
| 649 |
property_pool->set_location(get_handle(), new_loc); |
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
template <int dim, int spacedim> |
| 655 |
inline const Point<spacedim> & |
| 656 |
ParticleAccessor<dim, spacedim>::get_location() const |
| 657 |
{ |
| 658 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 659 |
|
| 660 |
return property_pool->get_location(get_handle()); |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
template <int dim, int spacedim> |
| 666 |
inline Point<spacedim> & |
| 667 |
ParticleAccessor<dim, spacedim>::get_location() |
| 668 |
{ |
| 669 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 670 |
|
| 671 |
return property_pool->get_location(get_handle()); |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
template <int dim, int spacedim> |
| 677 |
inline void |
| 678 |
ParticleAccessor<dim, spacedim>::set_reference_location( |
| 679 |
const Point<dim> &new_loc) |
| 680 |
{ |
| 681 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 682 |
|
| 683 |
property_pool->set_reference_location(get_handle(), new_loc); |
| 684 |
} |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
template <int dim, int spacedim> |
| 689 |
inline const Point<dim> & |
| 690 |
ParticleAccessor<dim, spacedim>::get_reference_location() const |
| 691 |
{ |
| 692 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 693 |
|
| 694 |
return property_pool->get_reference_location(get_handle()); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
template <int dim, int spacedim> |
| 700 |
inline types::particle_index |
| 701 |
ParticleAccessor<dim, spacedim>::get_id() const |
| 702 |
{ |
| 703 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 704 |
|
| 705 |
return property_pool->get_id(get_handle()); |
| 706 |
} |
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
template <int dim, int spacedim> |
| 711 |
inline types::particle_index |
| 712 |
ParticleAccessor<dim, spacedim>::get_local_index() const |
| 713 |
{ |
| 714 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 715 |
|
| 716 |
return get_handle(); |
| 717 |
} |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
template <int dim, int spacedim> |
| 722 |
inline void |
| 723 |
ParticleAccessor<dim, spacedim>::set_id(const types::particle_index &new_id) |
| 724 |
{ |
| 725 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 726 |
|
| 727 |
property_pool->set_id(get_handle(), new_id); |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
template <int dim, int spacedim> |
| 733 |
inline bool |
| 734 |
ParticleAccessor<dim, spacedim>::has_properties() const |
| 735 |
{ |
| 736 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 737 |
|
| 738 |
// Particles always have a property pool associated with them, |
| 739 |
// but we can access properties only if there is a valid handle. |
| 740 |
// The only way a particle can have no valid handle if it has |
| 741 |
// been moved-from -- but that leaves an object in an invalid |
| 742 |
// state, and so we can just assert that that can't be the case. |
| 743 |
Assert((get_handle() != PropertyPool<dim, spacedim>::invalid_handle), |
| 744 |
ExcInternalError()); |
| 745 |
return (property_pool->n_properties_per_slot() > 0); |
| 746 |
} |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
template <int dim, int spacedim> |
| 751 |
inline void |
| 752 |
ParticleAccessor<dim, spacedim>::set_properties( |
| 753 |
const std::vector<double> &new_properties) |
| 754 |
{ |
| 755 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 756 |
|
| 757 |
set_properties( |
| 758 |
ArrayView<const double>(new_properties.data(), new_properties.size())); |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
template <int dim, int spacedim> |
| 764 |
inline void |
| 765 |
ParticleAccessor<dim, spacedim>::set_properties( |
| 766 |
const ArrayView<const double> &new_properties) |
| 767 |
{ |
| 768 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 769 |
|
| 770 |
const ArrayView<double> property_values = |
| 771 |
property_pool->get_properties(get_handle()); |
| 772 |
|
| 773 |
Assert(new_properties.size() == property_values.size(), |
| 774 |
ExcMessage( |
| 775 |
"You are trying to assign properties with an incompatible length. " |
| 776 |
"The particle has space to store " + |
| 777 |
std::to_string(property_values.size()) + |
| 778 |
" properties, but you are trying to assign " + |
| 779 |
std::to_string(new_properties.size()) + |
| 780 |
" properties. This is not allowed.")); |
| 781 |
|
| 782 |
if (property_values.size() > 0) |
| 783 |
std::copy(new_properties.begin(), |
| 784 |
new_properties.end(), |
| 785 |
property_values.begin()); |
| 786 |
} |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
template <int dim, int spacedim> |
| 791 |
inline void |
| 792 |
ParticleAccessor<dim, spacedim>::set_properties( |
| 793 |
const Tensor<1, dim> &new_properties) |
| 794 |
{ |
| 795 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 796 |
|
| 797 |
// A Tensor object is not an array, so we cannot just create an |
| 798 |
// ArrayView object for it. Rather, copy the data into a true |
| 799 |
// array and make the ArrayView from that. |
| 800 |
double array[dim]; |
| 801 |
for (unsigned int d = 0; d < dim; ++d) |
| 802 |
array[d] = new_properties[d]; |
| 803 |
|
| 804 |
set_properties(make_array_view(array)); |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
template <int dim, int spacedim> |
| 810 |
inline ArrayView<const double> |
| 811 |
ParticleAccessor<dim, spacedim>::get_properties() const |
| 812 |
{ |
| 813 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 814 |
|
| 815 |
return property_pool->get_properties(get_handle()); |
| 816 |
} |
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
template <int dim, int spacedim> |
| 821 |
inline const typename Triangulation<dim, spacedim>::cell_iterator & |
| 822 |
ParticleAccessor<dim, spacedim>::get_surrounding_cell() const |
| 823 |
{ |
| 824 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 825 |
Assert(particles_in_cell->cell.state() == IteratorState::valid, |
| 826 |
ExcInternalError()); |
| 827 |
|
| 828 |
return particles_in_cell->cell; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
// template <int dim, int spacedim> |
| 834 |
// inline ArrayView<double> |
| 835 |
// ParticleAccessor<dim, spacedim>::get_properties() |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
template <int dim, int spacedim> |
| 840 |
inline std::size_t |
| 841 |
ParticleAccessor<dim, spacedim>::serialized_size_in_bytes() const |
| 842 |
{ |
| 843 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 844 |
|
| 845 |
std::size_t size = sizeof(get_id()) + |
| 846 |
sizeof(double) * spacedim + // get_location() |
| 847 |
sizeof(double) * dim; // get_reference_location() |
| 848 |
|
| 849 |
if (has_properties()) |
| 850 |
{ |
| 851 |
size += sizeof(double) * get_properties().size(); |
| 852 |
} |
| 853 |
return size; |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
template <int dim, int spacedim> |
| 859 |
inline void |
| 860 |
ParticleAccessor<dim, spacedim>::next() |
| 861 |
{ |
| 862 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 863 |
|
| 864 |
++particle_index_within_cell; |
| 865 |
|
| 866 |
if (particle_index_within_cell >= particles_in_cell->particles.size()) |
| 867 |
{ |
| 868 |
particle_index_within_cell = 0; |
| 869 |
++particles_in_cell; |
| 870 |
} |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
template <int dim, int spacedim> |
| 876 |
inline void |
| 877 |
ParticleAccessor<dim, spacedim>::prev() |
| 878 |
{ |
| 879 |
Assert(state() == IteratorState::valid, ExcInternalError()); |
| 880 |
|
| 881 |
if (particle_index_within_cell > 0) |
| 882 |
--particle_index_within_cell; |
| 883 |
else |
| 884 |
{ |
| 885 |
--particles_in_cell; |
| 886 |
particle_index_within_cell = particles_in_cell->particles.empty() ? |
| 887 |
0 : |
| 888 |
particles_in_cell->particles.size() - 1; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
template <int dim, int spacedim> |
| 895 |
inline bool |
| 896 |
ParticleAccessor<dim, spacedim>::operator!=( |
| 897 |
const ParticleAccessor<dim, spacedim> &other) const |
| 898 |
{ |
| 899 |
return !(*this == other); |
| 900 |
} |
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
template <int dim, int spacedim> |
| 905 |
inline bool |
| 906 |
ParticleAccessor<dim, spacedim>::operator==( |
| 907 |
const ParticleAccessor<dim, spacedim> &other) const |
| 908 |
{ |
| 909 |
return (property_pool == other.property_pool) && |
| 910 |
(particles_in_cell == other.particles_in_cell) && |
| 911 |
(particle_index_within_cell == other.particle_index_within_cell); |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
template <int dim, int spacedim> |
| 917 |
inline IteratorState::IteratorStates |
| 918 |
ParticleAccessor<dim, spacedim>::state() const |
| 919 |
{ |
| 920 |
if (property_pool != nullptr && |
| 921 |
particles_in_cell->cell.state() == IteratorState::valid && |
| 922 |
particle_index_within_cell < particles_in_cell->particles.size()) |
| 923 |
return IteratorState::valid; |
| 924 |
else if (property_pool != nullptr && |
| 925 |
particles_in_cell->cell.state() == IteratorState::past_the_end && |
| 926 |
particle_index_within_cell == 0) |
| 927 |
return IteratorState::past_the_end; |
| 928 |
else |
| 929 |
return IteratorState::invalid; |
| 930 |
|
| 931 |
return IteratorState::invalid; |
| 932 |
} |
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
template <int dim, int spacedim> |
| 937 |
inline typename PropertyPool<dim, spacedim>::Handle & |
| 938 |
ParticleAccessor<dim, spacedim>::get_handle() |
| 939 |
{ |
| 940 |
return particles_in_cell->particles[particle_index_within_cell]; |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
template <int dim, int spacedim> |
| 946 |
inline const typename PropertyPool<dim, spacedim>::Handle & |
| 947 |
ParticleAccessor<dim, spacedim>::get_handle() const |
| 948 |
{ |
| 949 |
return particles_in_cell->particles[particle_index_within_cell]; |
| 950 |
} |
| 951 |
|
| 952 |
} // namespace Particles |
| 953 |
|
| 954 |
DEAL_II_NAMESPACE_CLOSE |
| 955 |
|
| 956 |
namespace boost |
| 957 |
{ |
| 958 |
namespace geometry |
| 959 |
{ |
| 960 |
namespace index |
| 961 |
{ |
| 962 |
/** |
| 963 |
* Make sure we can construct an RTree from Particles::ParticleAccessor |
| 964 |
* objects. |
| 965 |
*/ |
| 966 |
template <int dim, int spacedim> |
| 967 |
struct indexable<dealii::Particles::ParticleAccessor<dim, spacedim>> |
| 968 |
{ |
| 969 |
/** |
| 970 |
* boost::rtree expects a const reference to an indexable object. For |
| 971 |
* a Particles::Particle object, this is its reference location. |
| 972 |
*/ |
| 973 |
using result_type = const dealii::Point<spacedim> &; |
| 974 |
|
| 975 |
result_type |
| 976 |
operator()(const dealii::Particles::ParticleAccessor<dim, spacedim> |
| 977 |
&accessor) const |
| 978 |
{ |
| 979 |
return accessor.get_location(); |
| 980 |
} |
| 981 |
}; |
| 982 |
} // namespace index |
| 983 |
} // namespace geometry |
| 984 |
} // namespace boost |
| 985 |
|
| 986 |
#endif |