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
particle.cc
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 - 2025 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
14
16
18
19namespace Particles
20{
21 template <int dim, int spacedim>
22 PropertyPool<dim, spacedim> Particle<dim, spacedim>::global_property_pool = {
23 /* no properties: */ 0};
24
25
26 template <int dim, int spacedim>
28 : property_pool(&global_property_pool)
29 , property_pool_handle(property_pool->register_particle())
30 {}
31
32
33
34 template <int dim, int spacedim>
36 const Point<dim> &reference_location,
37 const types::particle_index id)
38 : property_pool(&global_property_pool)
39 , property_pool_handle(property_pool->register_particle())
40 {
41 set_location(location);
42 set_reference_location(reference_location);
43 set_id(id);
44 }
45
46
47
48 template <int dim, int spacedim>
50 : property_pool(particle.property_pool)
51 , property_pool_handle(property_pool->register_particle())
52 {
53 set_location(particle.get_location());
55 set_id(particle.get_id());
56
57 if (particle.has_properties())
58 {
59 const ArrayView<const double> their_properties =
60 particle.get_properties();
61 const ArrayView<double> my_properties =
63
64 std::copy(their_properties.begin(),
65 their_properties.end(),
66 my_properties.begin());
67 }
68 }
69
70
71
72 template <int dim, int spacedim>
74 const void *&data,
75 PropertyPool<dim, spacedim> *const new_property_pool)
76 : property_pool(new_property_pool != nullptr ? new_property_pool :
77 &global_property_pool)
78 , property_pool_handle(property_pool->register_particle())
79 {
80 const types::particle_index *id_data =
81 static_cast<const types::particle_index *>(data);
82 set_id(*id_data++);
83 const double *pdata = reinterpret_cast<const double *>(id_data);
84
85 Point<spacedim> location;
86 for (unsigned int i = 0; i < spacedim; ++i)
87 location[i] = *pdata++;
88 set_location(location);
89
90 Point<dim> reference_location;
91 for (unsigned int i = 0; i < dim; ++i)
92 reference_location[i] = *pdata++;
93 set_reference_location(reference_location);
94
95 // See if there are properties to load
96 if (has_properties())
97 {
98 const ArrayView<double> particle_properties = this->get_properties();
99 const unsigned int size = particle_properties.size();
100 for (unsigned int i = 0; i < size; ++i)
101 particle_properties[i] = *pdata++;
102 }
103
104 data = static_cast<const void *>(pdata);
105 }
106
107
108
109 template <int dim, int spacedim>
111 : property_pool(std::move(particle.property_pool))
112 , property_pool_handle(std::move(particle.property_pool_handle))
113 {
114 // There is no need to copy locations, properties, and id -- we simply
115 // inherit them from the object we move from.
116
117 // We stole the rhs's properties, so we need to invalidate
118 // the handle the rhs holds lest it releases the memory that
119 // we still reference here.
120 particle.property_pool_handle = PropertyPool<dim, spacedim>::invalid_handle;
121 }
122
123
124
125 template <int dim, int spacedim>
128 {
129 if (this != &particle)
130 {
131 Assert(this->property_pool->n_properties_per_slot() ==
134
135 set_location(particle.get_location());
136 set_reference_location(particle.get_reference_location());
137 set_id(particle.get_id());
138
139 if (particle.has_properties())
140 {
141 const ArrayView<const double> their_properties =
142 particle.get_properties();
143 const ArrayView<double> my_properties =
144 property_pool->get_properties(property_pool_handle);
145
146 std::copy(their_properties.begin(),
147 their_properties.end(),
148 my_properties.begin());
149 }
150 }
151
152 return *this;
153 }
154
155
156
157 template <int dim, int spacedim>
160 Particle<dim, spacedim> &&particle) noexcept
161 {
162 if (this != &particle)
163 {
164 // If we currently hold a handle, release the memory. (The only way a
165 // particle can end up not holding a valid handle is if it has been
166 // moved from.)
167 if (property_pool_handle != PropertyPool<dim, spacedim>::invalid_handle)
168 property_pool->deregister_particle(property_pool_handle);
169
170 property_pool = std::move(particle.property_pool);
171 property_pool_handle = std::move(particle.property_pool_handle);
172
173 // No need to copy locations, properties, and id -- we just get them
174 // by taking over the property pool handle.
175
176 // We stole the rhs's properties, so we need to invalidate
177 // the handle the rhs holds lest it releases the memory that
178 // we still reference here.
179 particle.property_pool_handle =
181 }
182 return *this;
183 }
184
185
186
187 template <int dim, int spacedim>
189 {
190 // If we still hold a handle, release the memory. The only way a
191 // particle can end up not holding a valid handle is if it has been
192 // moved from.
193 if (property_pool_handle != PropertyPool<dim, spacedim>::invalid_handle)
194 property_pool->deregister_particle(property_pool_handle);
195 }
196
197
198
199 template <int dim, int spacedim>
200 void
202 {
203 // If we still hold a handle, release the memory. The only way a
204 // particle can end up not holding a valid handle is if it has been
205 // moved from.
206 //
207 // deregister_particle() automatically invalidates its argument.
208 if (property_pool_handle != PropertyPool<dim, spacedim>::invalid_handle)
209 property_pool->deregister_particle(property_pool_handle);
210 }
211
212
213
214 template <int dim, int spacedim>
215 void *
217 void *data_pointer) const
218 {
219 types::particle_index *id_data =
220 static_cast<types::particle_index *>(data_pointer);
221 *id_data = get_id();
222 ++id_data;
223 double *pdata = reinterpret_cast<double *>(id_data);
224
225 // Write location
226 for (unsigned int i = 0; i < spacedim; ++i, ++pdata)
227 *pdata = get_location()[i];
228
229 // Write reference location
230 for (unsigned int i = 0; i < dim; ++i, ++pdata)
231 *pdata = get_reference_location()[i];
232
233 // Write properties
234 if (has_properties())
235 {
236 const ArrayView<double> particle_properties =
237 property_pool->get_properties(property_pool_handle);
238 for (unsigned int i = 0; i < particle_properties.size(); ++i, ++pdata)
239 *pdata = particle_properties[i];
240 }
241
242 return static_cast<void *>(pdata);
243 }
244
245
246
247 template <int dim, int spacedim>
248 const void *
250 const void *data_pointer)
251 {
252 const types::particle_index *id_data =
253 static_cast<const types::particle_index *>(data_pointer);
254 set_id(*id_data++);
255 const double *pdata = reinterpret_cast<const double *>(id_data);
256
257 Point<spacedim> location;
258 for (unsigned int i = 0; i < spacedim; ++i)
259 location[i] = *pdata++;
260 set_location(location);
261
262 Point<dim> reference_location;
263 for (unsigned int i = 0; i < dim; ++i)
264 reference_location[i] = *pdata++;
265 set_reference_location(reference_location);
266
267 // See if there are properties to load
268 if (has_properties())
269 {
270 const ArrayView<double> particle_properties =
271 property_pool->get_properties(property_pool_handle);
272 const unsigned int size = particle_properties.size();
273 for (unsigned int i = 0; i < size; ++i)
274 particle_properties[i] = *pdata++;
275 }
276
277 return static_cast<const void *>(pdata);
278 }
279
280
281
282 template <int dim, int spacedim>
283 std::size_t
285 {
286 std::size_t size = sizeof(get_id()) +
287 sizeof(double) * spacedim + // get_location()
288 sizeof(double) * dim; // get_reference_location()
289
290 if (has_properties())
291 {
292 const ArrayView<double> particle_properties =
293 property_pool->get_properties(property_pool_handle);
294 size += sizeof(double) * particle_properties.size();
295 }
296 return size;
297 }
298
299
300
301 template <int dim, int spacedim>
302 void
304 const ArrayView<const double> &new_properties)
305 {
306 const ArrayView<double> property_values =
307 property_pool->get_properties(property_pool_handle);
308
309 Assert(new_properties.size() == property_values.size(),
311 "You are trying to assign properties with an incompatible length. "
312 "The particle has space to store " +
313 std::to_string(property_values.size()) +
314 " properties, but you are trying to assign " +
315 std::to_string(new_properties.size()) +
316 " properties. This is not allowed."));
317
318 if (property_values.size() > 0)
319 std::copy(new_properties.begin(),
320 new_properties.end(),
321 property_values.begin());
322 }
323
324
325
326 template <int dim, int spacedim>
329 {
330 return property_pool->get_properties(property_pool_handle);
331 }
332} // namespace Particles
333
334#include "particles/particle.inst"
335
iterator begin() const
Definition array_view.h:755
iterator end() const
Definition array_view.h:764
std::size_t size() const
Definition array_view.h:737
PropertyPool< dim, spacedim > * property_pool
Definition particle.h:474
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:284
Particle< dim, spacedim > & operator=(const Particle< dim, spacedim > &particle)
Definition particle.cc:127
void set_properties(const ArrayView< const double > &new_properties)
Definition particle.cc:303
const void * read_particle_data_from_memory(const void *data)
Definition particle.cc:249
void * write_particle_data_to_memory(void *data) const
Definition particle.cc:216
types::particle_index get_id() const
Definition particle.h:590
ArrayView< double > get_properties()
Definition particle.cc:328
PropertyPool< dim, spacedim >::Handle property_pool_handle
Definition particle.h:479
void set_id(const types::particle_index &new_id)
Definition particle.h:599
void set_location(const Point< spacedim > &new_location)
Definition particle.h:545
unsigned int n_properties_per_slot() const
void deregister_particle(Handle &handle)
ArrayView< double, ::MemorySpace::Host > get_properties(const Handle handle)
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 & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
std::vector< index_type > data
Definition mpi.cc:734
std::size_t size
Definition mpi.cc:733