Reference documentation for deal.II version 9.5.0
\(\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// Copyright (C) 1998 - 2023 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.md at
12// the top level directory of deal.II.
13//
14// ---------------------------------------------------------------------
15
16#ifndef dealii_cell_id_h
17#define dealii_cell_id_h
18
19#include <deal.II/base/config.h>
20
23
24#include <array>
25#include <cstdint>
26#include <iostream>
27#include <vector>
28
29#ifdef DEAL_II_WITH_P4EST
31#endif
32
34
35// Forward declarations
36#ifndef DOXYGEN
37template <int dim, int spacedim>
39class Triangulation;
40#endif
41
71class CellId
72{
73public:
81 using binary_type = std::array<unsigned int, 4>;
82
94 const std::vector<std::uint8_t> &child_indices);
95
108 const unsigned int n_child_indices,
109 const std::uint8_t * child_indices);
110
115 CellId(const binary_type &binary_representation);
116
121 explicit CellId(const std::string &string_representation);
122
126 CellId();
127
140 std::string
141 to_string() const;
142
146 template <int dim>
148 to_binary() const;
149
153 bool
154 operator==(const CellId &other) const;
155
159 bool
160 operator!=(const CellId &other) const;
161
167 bool
168 operator<(const CellId &other) const;
169
173 bool
174 is_parent_of(const CellId &other) const;
175
179 bool
180 is_ancestor_of(const CellId &other) const;
181
187 template <class Archive>
188 void
189 serialize(Archive &ar, const unsigned int version);
190
195 get_coarse_cell_id() const;
196
206 get_child_indices() const;
207
208private:
214
219 unsigned int n_child_indices;
220
230#ifdef DEAL_II_WITH_P4EST
231 std::array<std::uint8_t, internal::p4est::functions<2>::max_level>
233#else
234 std::array<std::uint8_t, 30> child_indices;
235#endif
236
237 friend std::istream &
238 operator>>(std::istream &is, CellId &cid);
239 friend std::ostream &
240 operator<<(std::ostream &os, const CellId &cid);
241};
242
243
244
248inline std::ostream &
249operator<<(std::ostream &os, const CellId &cid)
250{
251 os << cid.coarse_cell_id << '_' << cid.n_child_indices << ':';
252 for (unsigned int i = 0; i < cid.n_child_indices; ++i)
253 // write the child indices. because they are between 0 and 2^dim-1, they all
254 // just have one digit, so we could write them as one character
255 // objects. it's probably clearer to write them as one-digit characters
256 // starting at '0'
257 os << static_cast<unsigned char>('0' + cid.child_indices[i]);
258 return os;
259}
260
261
262
266template <class Archive>
267void
268CellId::serialize(Archive &ar, const unsigned int /*version*/)
269{
270 ar &coarse_cell_id;
271 ar &n_child_indices;
272 ar &child_indices;
273}
274
278inline std::istream &
279operator>>(std::istream &is, CellId &cid)
280{
281 unsigned int cellid;
282 is >> cellid;
283 if (is.eof())
284 return is;
285
286 cid.coarse_cell_id = cellid;
287 char dummy;
288 is >> dummy;
289 Assert(dummy == '_', ExcMessage("invalid CellId"));
290 is >> cid.n_child_indices;
291 is >> dummy;
292 Assert(dummy == ':', ExcMessage("invalid CellId"));
293
294 unsigned char value;
295 for (unsigned int i = 0; i < cid.n_child_indices; ++i)
296 {
297 // read the one-digit child index (as an integer number) and
298 // convert it back into unsigned integer type
299 is >> value;
300 cid.child_indices[i] = value - '0';
301 }
302 return is;
303}
304
305
306
307inline bool
308CellId::operator==(const CellId &other) const
309{
310 if (this->coarse_cell_id != other.coarse_cell_id)
311 return false;
312 if (n_child_indices != other.n_child_indices)
313 return false;
314
315 for (unsigned int i = 0; i < n_child_indices; ++i)
316 if (child_indices[i] != other.child_indices[i])
317 return false;
318
319 return true;
320}
321
322
323
324inline bool
325CellId::operator!=(const CellId &other) const
326{
327 return !(*this == other);
328}
329
330
331
332inline bool
333CellId::operator<(const CellId &other) const
334{
335 if (this->coarse_cell_id != other.coarse_cell_id)
336 return this->coarse_cell_id < other.coarse_cell_id;
337
338 unsigned int idx = 0;
339 while (idx < n_child_indices)
340 {
341 if (idx >= other.n_child_indices)
342 return false;
343
344 if (child_indices[idx] != other.child_indices[idx])
345 return child_indices[idx] < other.child_indices[idx];
346
347 ++idx;
348 }
349
350 if (n_child_indices == other.n_child_indices)
351 return false;
352 return true; // other.id is longer
353}
354
355
356
357inline bool
358CellId::is_parent_of(const CellId &other) const
359{
360 if (this->coarse_cell_id != other.coarse_cell_id)
361 return false;
362
363 if (n_child_indices + 1 != other.n_child_indices)
364 return false;
365
366 for (unsigned int idx = 0; idx < n_child_indices; ++idx)
367 if (child_indices[idx] != other.child_indices[idx])
368 return false;
369
370 return true; // other.id is longer
371}
372
373
374
375inline bool
377{
378 if (this->coarse_cell_id != other.coarse_cell_id)
379 return false;
380
381 if (n_child_indices >= other.n_child_indices)
382 return false;
383
384 for (unsigned int idx = 0; idx < n_child_indices; ++idx)
385 if (child_indices[idx] != other.child_indices[idx])
386 return false;
387
388 return true; // other.id is longer
389}
390
391
392
395{
396 return coarse_cell_id;
397}
398
399
400
403{
404 return {child_indices.data(), n_child_indices};
405}
406
407
409
410#endif
std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition cell_id.h:249
std::istream & operator>>(std::istream &is, CellId &cid)
Definition cell_id.h:279
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition cell_id.h:232
bool operator==(const CellId &other) const
Definition cell_id.h:308
bool is_ancestor_of(const CellId &other) const
Definition cell_id.h:376
unsigned int n_child_indices
Definition cell_id.h:219
void serialize(Archive &ar, const unsigned int version)
Definition cell_id.h:268
binary_type to_binary() const
Definition cell_id.cc:111
ArrayView< const std::uint8_t > get_child_indices() const
Definition cell_id.h:402
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition cell_id.h:249
bool operator!=(const CellId &other) const
Definition cell_id.h:325
std::string to_string() const
Definition cell_id.cc:157
CellId()
Definition cell_id.cc:25
bool operator<(const CellId &other) const
Definition cell_id.h:333
types::coarse_cell_id get_coarse_cell_id() const
Definition cell_id.h:394
types::coarse_cell_id coarse_cell_id
Definition cell_id.h:213
std::array< unsigned int, 4 > binary_type
Definition cell_id.h:81
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition cell_id.h:279
bool is_parent_of(const CellId &other) const
Definition cell_id.h:358
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:472
#define DEAL_II_CXX20_REQUIRES(condition)
Definition config.h:160
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:473
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)