Reference documentation for deal.II version GIT 6dd81a2340 2022-11-29 05:55:02+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\}}\)
cell_id.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2021 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
37 template <int, int>
38 class Triangulation;
39 #endif
40 
70 class CellId
71 {
72 public:
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 
207 private:
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 
247 inline std::ostream &
248 operator<<(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 
265 template <class Archive>
266 void
267 CellId::serialize(Archive &ar, const unsigned int /*version*/)
268 {
269  ar &coarse_cell_id;
270  ar &n_child_indices;
271  ar &child_indices;
272 }
273 
277 inline std::istream &
278 operator>>(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 
306 inline bool
307 CellId::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 
323 inline bool
324 CellId::operator!=(const CellId &other) const
325 {
326  return !(*this == other);
327 }
328 
329 
330 
331 inline bool
332 CellId::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 
356 inline bool
357 CellId::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 
374 inline bool
375 CellId::is_ancestor_of(const CellId &other) const
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 
402 {
403  return {child_indices.data(), n_child_indices};
404 }
405 
406 
408 
409 #endif
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:165
Definition: cell_id.h:71
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:111
ArrayView< const std::uint8_t > get_child_indices() const
Definition: cell_id.h:401
bool operator!=(const CellId &other) const
Definition: cell_id.h:324
std::string to_string() const
Definition: cell_id.cc:157
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition: cell_id.h:248
CellId()
Definition: cell_id.cc:25
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
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition: cell_id.h:278
types::coarse_cell_id coarse_cell_id
Definition: cell_id.h:212
std::array< unsigned int, 4 > binary_type
Definition: cell_id.h:80
bool is_parent_of(const CellId &other) const
Definition: cell_id.h:357
std::istream & operator>>(std::istream &in, Point< dim, Number > &p)
Definition: point.h:685
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:458
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:459
#define Assert(cond, exc)
Definition: exceptions.h:1501
static ::ExceptionBase & ExcMessage(std::string arg1)
global_cell_index coarse_cell_id
Definition: types.h:119