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
cell_id.h
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) 2013 - 2026 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
13#ifndef dealii_cell_id_h
14#define dealii_cell_id_h
15
16#include <deal.II/base/config.h>
17
20
21#include <array>
22#include <cstdint>
23#include <iostream>
24#include <vector>
25
27
28// Forward declarations
29#ifndef DOXYGEN
30template <int dim, int spacedim>
32class Triangulation;
33#endif
34
64class CellId
65{
66public:
71 using binary_type = std::array<std::uint64_t, 3>;
72
84 const std::vector<std::uint8_t> &child_indices);
85
98 const unsigned int n_child_indices,
99 const std::uint8_t *child_indices);
100
105 CellId(const binary_type &binary_representation);
106
111 explicit CellId(const std::string &string_representation);
112
116 CellId();
117
130 std::string
131 to_string() const;
132
136 template <int dim>
138 to_binary() const;
139
143 bool
144 operator==(const CellId &other) const;
145
149 bool
150 operator!=(const CellId &other) const;
151
157 bool
158 operator<(const CellId &other) const;
159
163 bool
164 is_parent_of(const CellId &other) const;
165
169 bool
170 is_ancestor_of(const CellId &other) const;
171
177 template <class Archive>
178 void
179 serialize(Archive &ar, const unsigned int version);
180
185 get_coarse_cell_id() const;
186
196 get_child_indices() const;
197
198private:
204
209 std::uint8_t n_child_indices;
210
222 std::array<std::uint8_t, numbers::max_n_levels - 1> child_indices;
223
224 friend std::istream &
225 operator>>(std::istream &is, CellId &cid);
226 friend std::ostream &
227 operator<<(std::ostream &os, const CellId &cid);
228};
229
230
231
235inline std::ostream &
236operator<<(std::ostream &os, const CellId &cid)
237{
238 os << cid.coarse_cell_id << '_' << int(cid.n_child_indices) << ':';
239 for (std::uint8_t i = 0; i < cid.n_child_indices; ++i)
240 // write the child indices. because they are between 0 and 2^dim-1, they all
241 // just have one digit, so we could write them as one character
242 // objects. it's probably clearer to write them as one-digit characters
243 // starting at '0'
244 os << static_cast<unsigned char>('0' + cid.child_indices[i]);
245 return os;
246}
247
248
249
253template <class Archive>
254void
255CellId::serialize(Archive &ar, const unsigned int /*version*/)
256{
257 ar &coarse_cell_id;
258 ar &n_child_indices;
259 ar &child_indices;
260}
261
265inline std::istream &
266operator>>(std::istream &is, CellId &cid)
267{
269 is >> cellid;
270 if (is.eof())
271 return is;
272
273 cid.coarse_cell_id = cellid;
274 char dummy;
275 is >> dummy;
276 Assert(dummy == '_', ExcMessage("invalid CellId"));
277 unsigned int n_child_indices;
278 is >> n_child_indices;
279 Assert(n_child_indices < cid.child_indices.size(),
280 ExcMessage("invalid CellId"));
281 cid.n_child_indices = static_cast<std::uint8_t>(n_child_indices);
282 is >> dummy;
283 Assert(dummy == ':', ExcMessage("invalid CellId"));
284
285 unsigned char value;
286 for (std::uint8_t i = 0; i < cid.n_child_indices; ++i)
287 {
288 // read the one-digit child index (as an integer number) and
289 // convert it back into unsigned integer type
290 is >> value;
291 cid.child_indices[i] = value - '0';
292 }
293 return is;
294}
295
296
297
298inline bool
299CellId::operator==(const CellId &other) const
300{
301 if (this->coarse_cell_id != other.coarse_cell_id)
302 return false;
303 if (n_child_indices != other.n_child_indices)
304 return false;
305
306 for (std::uint8_t i = 0; i < n_child_indices; ++i)
307 if (child_indices[i] != other.child_indices[i])
308 return false;
309
310 return true;
311}
312
313
314
315inline bool
316CellId::operator!=(const CellId &other) const
317{
318 return !(*this == other);
319}
320
321
322
323inline bool
324CellId::operator<(const CellId &other) const
325{
326 if (this->coarse_cell_id != other.coarse_cell_id)
327 return this->coarse_cell_id < other.coarse_cell_id;
328
329 std::uint8_t idx = 0;
330 while (idx < n_child_indices)
331 {
332 if (idx >= other.n_child_indices)
333 return false;
334
335 if (child_indices[idx] != other.child_indices[idx])
336 return child_indices[idx] < other.child_indices[idx];
337
338 ++idx;
339 }
340
341 if (n_child_indices == other.n_child_indices)
342 return false;
343 return true; // other.id is longer
344}
345
346
347
348inline bool
349CellId::is_parent_of(const CellId &other) const
350{
351 if (this->coarse_cell_id != other.coarse_cell_id)
352 return false;
353
354 if (n_child_indices + 1 != other.n_child_indices)
355 return false;
356
357 for (unsigned int idx = 0; idx < n_child_indices; ++idx)
358 if (child_indices[idx] != other.child_indices[idx])
359 return false;
360
361 return true; // other.id is longer
362}
363
364
365
366inline bool
368{
369 if (this->coarse_cell_id != other.coarse_cell_id)
370 return false;
371
372 if (n_child_indices >= other.n_child_indices)
373 return false;
374
375 for (std::uint8_t idx = 0; idx < n_child_indices; ++idx)
376 if (child_indices[idx] != other.child_indices[idx])
377 return false;
378
379 return true; // other.id is longer
380}
381
382
383
386{
387 return coarse_cell_id;
388}
389
390
391
394{
395 return {child_indices.data(), std::size_t(n_child_indices)};
396}
397
398
400
401#endif
std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition cell_id.h:236
std::istream & operator>>(std::istream &is, CellId &cid)
Definition cell_id.h:266
value_type * data() const noexcept
Definition array_view.h:714
bool operator==(const CellId &other) const
Definition cell_id.h:299
std::uint8_t n_child_indices
Definition cell_id.h:209
bool is_ancestor_of(const CellId &other) const
Definition cell_id.h:367
std::array< std::uint8_t, numbers::max_n_levels - 1 > child_indices
Definition cell_id.h:222
void serialize(Archive &ar, const unsigned int version)
Definition cell_id.h:255
binary_type to_binary() const
Definition cell_id.cc:109
ArrayView< const std::uint8_t > get_child_indices() const
Definition cell_id.h:393
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition cell_id.h:236
bool operator!=(const CellId &other) const
Definition cell_id.h:316
std::string to_string() const
Definition cell_id.cc:167
CellId()
Definition cell_id.cc:22
bool operator<(const CellId &other) const
Definition cell_id.h:324
types::coarse_cell_id get_coarse_cell_id() const
Definition cell_id.h:385
std::array< std::uint64_t, 3 > binary_type
Definition cell_id.h:71
types::coarse_cell_id coarse_cell_id
Definition cell_id.h:203
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition cell_id.h:266
bool is_parent_of(const CellId &other) const
Definition cell_id.h:349
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:249
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
constexpr std::uint8_t max_n_levels
Definition types.h:415
constexpr types::coarse_cell_id invalid_coarse_cell_id
Definition types.h:272