deal.II version GIT relicensing-2306-ga6fff1538f 2024-12-29 17:30: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
cell_id.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2013 - 2023 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#ifndef dealii_cell_id_h
16#define dealii_cell_id_h
17
18#include <deal.II/base/config.h>
19
22
23#include <array>
24#include <cstdint>
25#include <iostream>
26#include <vector>
27
28#ifdef DEAL_II_WITH_P4EST
30#endif
31
33
34// Forward declarations
35#ifndef DOXYGEN
36template <int dim, int spacedim>
38class Triangulation;
39#endif
40
70class CellId
71{
72public:
80 using binary_type = std::array<unsigned int, 4>;
81
93 const std::vector<std::uint8_t> &child_indices);
94
107 const unsigned int n_child_indices,
108 const std::uint8_t *child_indices);
109
114 CellId(const binary_type &binary_representation);
115
120 explicit CellId(const std::string &string_representation);
121
125 CellId();
126
139 std::string
140 to_string() const;
141
145 template <int dim>
147 to_binary() const;
148
152 bool
153 operator==(const CellId &other) const;
154
158 bool
159 operator!=(const CellId &other) const;
160
166 bool
167 operator<(const CellId &other) const;
168
172 bool
173 is_parent_of(const CellId &other) const;
174
178 bool
179 is_ancestor_of(const CellId &other) const;
180
186 template <class Archive>
187 void
188 serialize(Archive &ar, const unsigned int version);
189
194 get_coarse_cell_id() const;
195
205 get_child_indices() const;
206
207private:
213
218 unsigned int n_child_indices;
219
229#ifdef DEAL_II_WITH_P4EST
230 std::array<std::uint8_t, internal::p4est::functions<2>::max_level>
232#else
233 std::array<std::uint8_t, 30> child_indices;
234#endif
235
236 friend std::istream &
237 operator>>(std::istream &is, CellId &cid);
238 friend std::ostream &
239 operator<<(std::ostream &os, const CellId &cid);
240};
241
242
243
247inline std::ostream &
248operator<<(std::ostream &os, const CellId &cid)
249{
250 os << cid.coarse_cell_id << '_' << cid.n_child_indices << ':';
251 for (unsigned int i = 0; i < cid.n_child_indices; ++i)
252 // write the child indices. because they are between 0 and 2^dim-1, they all
253 // just have one digit, so we could write them as one character
254 // objects. it's probably clearer to write them as one-digit characters
255 // starting at '0'
256 os << static_cast<unsigned char>('0' + cid.child_indices[i]);
257 return os;
258}
259
260
261
265template <class Archive>
266void
267CellId::serialize(Archive &ar, const unsigned int /*version*/)
268{
269 ar &coarse_cell_id;
270 ar &n_child_indices;
271 ar &child_indices;
272}
273
277inline std::istream &
278operator>>(std::istream &is, CellId &cid)
279{
280 unsigned int cellid;
281 is >> cellid;
282 if (is.eof())
283 return is;
284
285 cid.coarse_cell_id = cellid;
286 char dummy;
287 is >> dummy;
288 Assert(dummy == '_', ExcMessage("invalid CellId"));
289 is >> cid.n_child_indices;
290 is >> dummy;
291 Assert(dummy == ':', ExcMessage("invalid CellId"));
292
293 unsigned char value;
294 for (unsigned int i = 0; i < cid.n_child_indices; ++i)
295 {
296 // read the one-digit child index (as an integer number) and
297 // convert it back into unsigned integer type
298 is >> value;
299 cid.child_indices[i] = value - '0';
300 }
301 return is;
302}
303
304
305
306inline bool
307CellId::operator==(const CellId &other) const
308{
309 if (this->coarse_cell_id != other.coarse_cell_id)
310 return false;
311 if (n_child_indices != other.n_child_indices)
312 return false;
313
314 for (unsigned int i = 0; i < n_child_indices; ++i)
315 if (child_indices[i] != other.child_indices[i])
316 return false;
317
318 return true;
319}
320
321
322
323inline bool
324CellId::operator!=(const CellId &other) const
325{
326 return !(*this == other);
327}
328
329
330
331inline bool
332CellId::operator<(const CellId &other) const
333{
334 if (this->coarse_cell_id != other.coarse_cell_id)
335 return this->coarse_cell_id < other.coarse_cell_id;
336
337 unsigned int idx = 0;
338 while (idx < n_child_indices)
339 {
340 if (idx >= other.n_child_indices)
341 return false;
342
343 if (child_indices[idx] != other.child_indices[idx])
344 return child_indices[idx] < other.child_indices[idx];
345
346 ++idx;
347 }
348
349 if (n_child_indices == other.n_child_indices)
350 return false;
351 return true; // other.id is longer
352}
353
354
355
356inline bool
357CellId::is_parent_of(const CellId &other) const
358{
359 if (this->coarse_cell_id != other.coarse_cell_id)
360 return false;
361
362 if (n_child_indices + 1 != other.n_child_indices)
363 return false;
364
365 for (unsigned int idx = 0; idx < n_child_indices; ++idx)
366 if (child_indices[idx] != other.child_indices[idx])
367 return false;
368
369 return true; // other.id is longer
370}
371
372
373
374inline bool
376{
377 if (this->coarse_cell_id != other.coarse_cell_id)
378 return false;
379
380 if (n_child_indices >= other.n_child_indices)
381 return false;
382
383 for (unsigned int idx = 0; idx < n_child_indices; ++idx)
384 if (child_indices[idx] != other.child_indices[idx])
385 return false;
386
387 return true; // other.id is longer
388}
389
390
391
394{
395 return coarse_cell_id;
396}
397
398
399
405
406
408
409#endif
std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition cell_id.h:248
std::istream & operator>>(std::istream &is, CellId &cid)
Definition cell_id.h:278
value_type * data() const noexcept
Definition array_view.h:661
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition cell_id.h:231
bool operator==(const CellId &other) const
Definition cell_id.h:307
bool is_ancestor_of(const CellId &other) const
Definition cell_id.h:375
unsigned int n_child_indices
Definition cell_id.h:218
void serialize(Archive &ar, const unsigned int version)
Definition cell_id.h:267
binary_type to_binary() const
Definition cell_id.cc:110
ArrayView< const std::uint8_t > get_child_indices() const
Definition cell_id.h:401
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition cell_id.h:248
bool operator!=(const CellId &other) const
Definition cell_id.h:324
std::string to_string() const
Definition cell_id.cc:156
CellId()
Definition cell_id.cc:24
bool operator<(const CellId &other) const
Definition cell_id.h:332
types::coarse_cell_id get_coarse_cell_id() const
Definition cell_id.h:393
types::coarse_cell_id coarse_cell_id
Definition cell_id.h:212
std::array< unsigned int, 4 > binary_type
Definition cell_id.h:80
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition cell_id.h:278
bool is_parent_of(const CellId &other) const
Definition cell_id.h:357
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:175
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)