Reference documentation for deal.II version 9.0.0
particle.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2017 - 2018 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #include <deal.II/particles/particle.h>
17 
18 DEAL_II_NAMESPACE_OPEN
19 
20 namespace Particles
21 {
22  template <int dim, int spacedim>
24  :
25  location (),
26  reference_location(),
27  id (0),
28  property_pool(nullptr),
29  properties(PropertyPool::invalid_handle)
30  {
31  }
32 
33 
34 
35  template <int dim, int spacedim>
37  const Point<dim> &reference_location,
38  const types::particle_index id)
39  :
40  location (location),
41  reference_location (reference_location),
42  id (id),
43  property_pool(nullptr),
44  properties (PropertyPool::invalid_handle)
45  {
46  }
47 
48 
49 
50  template <int dim, int spacedim>
52  :
53  location (particle.get_location()),
54  reference_location (particle.get_reference_location()),
55  id (particle.get_id()),
56  property_pool(particle.property_pool),
57  properties ((particle.has_properties()) ? property_pool->allocate_properties_array() : PropertyPool::invalid_handle)
58  {
59  if (particle.has_properties())
60  {
62  const ArrayView<const double> their_properties = particle.get_properties();
63 
64  std::copy(their_properties.begin(), their_properties.end(), my_properties.begin());
65  }
66  }
67 
68 
69 
70  template <int dim, int spacedim>
72  PropertyPool *const new_property_pool)
73  {
74  const types::particle_index *id_data = static_cast<const types::particle_index *> (data);
75  id = *id_data++;
76  const double *pdata = reinterpret_cast<const double *> (id_data);
77 
78  for (unsigned int i = 0; i < spacedim; ++i)
79  location(i) = *pdata++;
80 
81  for (unsigned int i = 0; i < dim; ++i)
82  reference_location(i) = *pdata++;
83 
84  property_pool = new_property_pool;
85  if (property_pool != nullptr)
86  properties = property_pool->allocate_properties_array();
87  else
88  properties = PropertyPool::invalid_handle;
89 
90  // See if there are properties to load
91  if (has_properties())
92  {
93  const ArrayView<double> particle_properties = property_pool->get_properties(properties);
94  const unsigned int size = particle_properties.size();
95  for (unsigned int i = 0; i < size; ++i)
96  particle_properties[i] = *pdata++;
97  }
98 
99  data = static_cast<const void *> (pdata);
100  }
101 
102 
103 
104  template <int dim, int spacedim>
106 :
107  location (std::move(particle.location)),
108  reference_location(std::move(particle.reference_location)),
109  id (std::move(particle.id)),
110  property_pool(std::move(particle.property_pool)),
111  properties(std::move(particle.properties))
112  {
113  particle.properties = PropertyPool::invalid_handle;
114  }
115 
116 
117 
118  template <int dim, int spacedim>
121  {
122  if (this != &particle)
123  {
124  location = particle.location;
125  reference_location = particle.reference_location;
126  id = particle.id;
127  property_pool = particle.property_pool;
128 
129  if (particle.has_properties())
130  {
131  properties = property_pool->allocate_properties_array();
132  const ArrayView<const double> their_properties = particle.get_properties();
133  const ArrayView<double> my_properties = property_pool->get_properties(properties);
134 
135  std::copy(their_properties.begin(), their_properties.end(), my_properties.begin());
136  }
137  else
138  properties = PropertyPool::invalid_handle;
139  }
140  return *this;
141  }
142 
143 
144 
145  template <int dim, int spacedim>
148  {
149  if (this != &particle)
150  {
151  location = particle.location;
152  reference_location = particle.reference_location;
153  id = particle.id;
154  property_pool = particle.property_pool;
155  properties = particle.properties;
156  particle.properties = PropertyPool::invalid_handle;
157  }
158  return *this;
159  }
160 
161 
162 
163  template <int dim, int spacedim>
165  {
166  if (property_pool != nullptr && properties != PropertyPool::invalid_handle)
167  property_pool->deallocate_properties_array(properties);
168  }
169 
170 
171 
172  template <int dim, int spacedim>
173  void
175  {
176  types::particle_index *id_data = static_cast<types::particle_index *> (data);
177  *id_data = id;
178  ++id_data;
179  double *pdata = reinterpret_cast<double *> (id_data);
180 
181  // Write location data
182  for (unsigned int i = 0; i < spacedim; ++i,++pdata)
183  *pdata = location(i);
184 
185  // Write reference location data
186  for (unsigned int i = 0; i < dim; ++i,++pdata)
187  *pdata = reference_location(i);
188 
189  // Write property data
190  if (has_properties())
191  {
192  const ArrayView<double> particle_properties = property_pool->get_properties(properties);
193  for (unsigned int i = 0; i < particle_properties.size(); ++i,++pdata)
194  *pdata = particle_properties[i];
195  }
196 
197  data = static_cast<void *> (pdata);
198  }
199 
200 
201 
202  template <int dim, int spacedim>
203  std::size_t
205  {
206  std::size_t size = sizeof(types::particle_index)
207  + sizeof(location)
208  + sizeof(reference_location);
209 
210  if (has_properties())
211  {
212  const ArrayView<double> particle_properties = property_pool->get_properties(properties);
213  size += sizeof(double) * particle_properties.size();
214  }
215  return size;
216  }
217 
218 
219 
220  template <int dim, int spacedim>
221  void
223  {
224  location = new_loc;
225  }
226 
227 
228 
229  template <int dim, int spacedim>
230  const Point<spacedim> &
232  {
233  return location;
234  }
235 
236 
237 
238  template <int dim, int spacedim>
239  void
241  {
242  reference_location = new_loc;
243  }
244 
245 
246 
247  template <int dim, int spacedim>
248  const Point<dim> &
250  {
251  return reference_location;
252  }
253 
254 
255 
256  template <int dim, int spacedim>
259  {
260  return id;
261  }
262 
263 
264 
265  template <int dim, int spacedim>
266  void
268  {
269  property_pool = &new_property_pool;
270  }
271 
272 
273 
274  template <int dim, int spacedim>
275  void
277  {
278  if (properties == PropertyPool::invalid_handle)
279  properties = property_pool->allocate_properties_array();
280 
281  const ArrayView<double> old_properties = property_pool->get_properties(properties);
282 
283  Assert (new_properties.size() == old_properties.size(),
284  ExcMessage(std::string("You are trying to assign properties with an incompatible length. ")
285  + "The particle has space to store " + Utilities::to_string(old_properties.size()) + " properties, "
286  + "and this function tries to assign" + Utilities::to_string(new_properties.size()) + " properties. "
287  + "This is not allowed."));
288 
289  if (old_properties.size() > 0)
290  std::copy(new_properties.begin(), new_properties.end(), old_properties.begin());
291  }
292 
293 
294 
295  template <int dim, int spacedim>
298  {
299  Assert(property_pool != nullptr,
300  ExcInternalError());
301 
302  return property_pool->get_properties(properties);
303  }
304 
305 
306 
307  template <int dim, int spacedim>
308  const ArrayView<double>
310  {
311  Assert(property_pool != nullptr,
312  ExcInternalError());
313 
314  return property_pool->get_properties(properties);
315  }
316 
317 
318 
319  template <int dim, int spacedim>
320  bool
322  {
323  return (property_pool != nullptr)
324  && (properties != PropertyPool::invalid_handle);
325  }
326 }
327 
328 DEAL_II_NAMESPACE_CLOSE
329 
330 DEAL_II_NAMESPACE_OPEN
331 
332 #include "particle.inst"
333 
334 DEAL_II_NAMESPACE_CLOSE
PropertyPool::Handle properties
Definition: particle.h:322
Point< spacedim > location
Definition: particle.h:301
void set_property_pool(PropertyPool &property_pool)
Definition: particle.cc:267
Particle< dim, spacedim > & operator=(const Particle< dim, spacedim > &particle)
Definition: particle.cc:120
iterator begin() const
Definition: array_view.h:378
ArrayView< double > get_properties(const Handle handle)
void write_data(void *&data) const
Definition: particle.cc:174
unsigned int particle_index
Definition: particle.h:64
types::particle_index get_id() const
Definition: particle.cc:258
void set_properties(const ArrayView< const double > &new_properties)
Definition: particle.cc:276
types::particle_index id
Definition: particle.h:311
const Point< spacedim > & get_location() const
Definition: particle.cc:231
const ArrayView< double > get_properties()
Definition: particle.cc:309
void set_reference_location(const Point< dim > &new_reference_location)
Definition: particle.cc:240
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:107
std::size_t serialized_size_in_bytes() const
Definition: particle.cc:204
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1142
static const Handle invalid_handle
Definition: property_pool.h:54
iterator end() const
Definition: array_view.h:388
const Point< dim > & get_reference_location() const
Definition: particle.cc:249
Point< dim > reference_location
Definition: particle.h:306
std::size_t size() const
Definition: array_view.h:370
bool has_properties() const
Definition: particle.cc:321
void set_location(const Point< spacedim > &new_location)
Definition: particle.cc:222
PropertyPool * property_pool
Definition: particle.h:317
Handle allocate_properties_array()
static ::ExceptionBase & ExcInternalError()