deal.II version GIT relicensing-2167-g9622207b8f 2024-11-21 12:40: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
property_pool.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
15
17
19
21
22namespace Particles
23{
24 template <int dim, int spacedim>
25 const typename PropertyPool<dim, spacedim>::Handle
26 PropertyPool<dim, spacedim>::invalid_handle = static_cast<Handle>(-1);
27
28
29
30 template <int dim, int spacedim>
32 const unsigned int n_properties_per_slot)
33 : n_properties(n_properties_per_slot)
34 {}
35
36
37
38 template <int dim, int spacedim>
43
44
45
46 template <int dim, int spacedim>
47 void
49 {
50 const unsigned int n_open_handles =
51 locations.size() - currently_available_handles.size();
52 (void)n_open_handles;
53 Assert(n_open_handles == 0,
54 ExcMessage("This property pool currently still holds " +
55 std::to_string(n_open_handles) +
56 " open handles to memory that was allocated "
57 "via allocate_properties_array() but that has "
58 "not been returned via "
59 "deregister_particle()."));
60
61 // Clear vectors and ensure deallocation of memory
62 locations.clear();
63 locations.shrink_to_fit();
64
65 reference_locations.clear();
66 reference_locations.shrink_to_fit();
67
68 ids.clear();
69 ids.shrink_to_fit();
70
71 properties.clear();
72 properties.shrink_to_fit();
73
74 currently_available_handles.clear();
75 currently_available_handles.shrink_to_fit();
76 }
77
78
79
80 template <int dim, int spacedim>
83 {
84 Handle handle = invalid_handle;
85 if (currently_available_handles.size() > 0)
86 {
87 handle = currently_available_handles.back();
88 currently_available_handles.pop_back();
89 }
90 else
91 {
92 handle = locations.size();
93
94 locations.resize(locations.size() + 1);
95 reference_locations.resize(reference_locations.size() + 1);
96 ids.resize(ids.size() + 1);
97 properties.resize(properties.size() + n_properties);
98 }
99
100 // Then initialize whatever slot we have taken with invalid locations,
101 // but initialize properties with zero.
102 set_location(handle, numbers::signaling_nan<Point<spacedim>>());
103 set_reference_location(handle, numbers::signaling_nan<Point<dim>>());
104 set_id(handle, numbers::invalid_unsigned_int);
105 for (double &x : get_properties(handle))
106 x = 0;
107
108 return handle;
109 }
110
111
112
113 template <int dim, int spacedim>
114 void
116 {
117 Assert(
118 handle != invalid_handle,
120 "This handle is invalid and cannot be deallocated. This can happen if the "
121 "handle was deallocated already before calling this function."));
122
123 Assert(
124 currently_available_handles.size() < locations.size(),
126 "Trying to deallocate a particle when none are allocated. This can happen if all "
127 "handles were deallocated already before calling this function."));
128
129 currently_available_handles.push_back(handle);
130 handle = invalid_handle;
131
132 // If this was the last handle, resize containers to 0.
133 // This guarantees that all future properties
134 // are allocated in a sorted way without requiring reallocation.
135 if (currently_available_handles.size() == locations.size())
136 {
137 currently_available_handles.clear();
138 properties.clear();
139 locations.clear();
140 reference_locations.clear();
141 ids.clear();
142 }
144
145
146
147 template <int dim, int spacedim>
148 void
151 locations.reserve(size);
152 reference_locations.reserve(size);
153 properties.reserve(size * n_properties);
154 ids.reserve(size);
155 }
156
157
158
159 template <int dim, int spacedim>
160 unsigned int
162 {
163 return n_properties;
164 }
165
166
167
168 template <int dim, int spacedim>
169 unsigned int
171 {
172 Assert(locations.size() == reference_locations.size(),
173 ExcMessage("Number of registered locations is not equal to number "
174 "of registered reference locations."));
175
176 Assert(locations.size() == ids.size(),
177 ExcMessage("Number of registered locations is not equal to number "
178 "of registered ids."));
179
180 Assert(locations.size() * n_properties == properties.size(),
181 ExcMessage("Number of registered locations is not equal to number "
182 "of registered property slots."));
183
184 return locations.size() - currently_available_handles.size();
185 }
186
187
188
189 template <int dim, int spacedim>
190 void
192 const std::vector<Handle> &handles_to_sort)
193 {
194 std::vector<Point<spacedim>> sorted_locations;
195 std::vector<Point<dim>> sorted_reference_locations;
196 std::vector<types::particle_index> sorted_ids;
197 std::vector<double> sorted_properties;
198
199 sorted_locations.reserve(locations.size());
200 sorted_reference_locations.reserve(reference_locations.size());
201 sorted_ids.reserve(ids.size());
202 sorted_properties.reserve(properties.size());
203
204 for (const auto &handle : handles_to_sort)
205 {
206 Assert(handle != invalid_handle,
208 "Invalid handle detected during sorting particle memory."));
209
210 sorted_locations.push_back(locations[handle]);
211 sorted_reference_locations.push_back(reference_locations[handle]);
212 sorted_ids.push_back(ids[handle]);
213
214 for (unsigned int j = 0; j < n_properties; ++j)
215 sorted_properties.push_back(properties[handle * n_properties + j]);
216 }
217
218 Assert(sorted_locations.size() ==
219 locations.size() - currently_available_handles.size(),
220 ExcMessage("Number of sorted property handles is not equal to "
221 "number of currently registered handles: " +
222 std::to_string(sorted_locations.size()) + " vs " +
223 std::to_string(locations.size()) + " - " +
224 std::to_string(currently_available_handles.size())));
225
226 locations = std::move(sorted_locations);
227 reference_locations = std::move(sorted_reference_locations);
228 ids = std::move(sorted_ids);
229 properties = std::move(sorted_properties);
230
231 currently_available_handles.clear();
232 }
233
234
235 // Instantiate the class for all reasonable template arguments
236 template class PropertyPool<1, 1>;
237 template class PropertyPool<1, 2>;
238 template class PropertyPool<1, 3>;
239 template class PropertyPool<2, 2>;
240 template class PropertyPool<2, 3>;
241 template class PropertyPool<3, 3>;
242} // namespace Particles
unsigned int n_properties_per_slot() const
unsigned int n_registered_slots() const
void deregister_particle(Handle &handle)
void reserve(const std::size_t size)
PropertyPool(const unsigned int n_properties_per_slot)
void sort_memory_slots(const std::vector< Handle > &handles_to_sort)
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 & ExcMessage(std::string arg1)
std::size_t size
Definition mpi.cc:734
static const unsigned int invalid_unsigned_int
Definition types.h:220
T signaling_nan()