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