Reference documentation for deal.II version GIT 741a3088b8 2023-04-01 07:05: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.cc
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2015 - 2020 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 #include <deal.II/grid/cell_id.h>
17 #include <deal.II/grid/tria.h>
18 
19 #include <limits>
20 #include <sstream>
21 
23 
24 
27  , n_child_indices(numbers::invalid_unsigned_int)
28 {
29  // initialize the child indices to invalid values
30  // (the only allowed values are between zero and
31  // GeometryInfo<dim>::max_children_per_cell)
32  std::fill(child_indices.begin(),
33  child_indices.end(),
35 }
36 
37 
38 
40  const std::vector<std::uint8_t> &id)
42  , n_child_indices(id.size())
43 {
45  std::copy(id.begin(), id.end(), child_indices.begin());
46 }
47 
48 
49 
51  const unsigned int n_child_indices,
52  const std::uint8_t * id)
54  , n_child_indices(n_child_indices)
55 {
57  memcpy(child_indices.data(), id, n_child_indices);
58 }
59 
60 
61 
62 CellId::CellId(const CellId::binary_type &binary_representation)
63 {
64  // The first entry stores the coarse cell id
65  coarse_cell_id = binary_representation[0];
66 
67  // The rightmost two bits of the second entry store the dimension,
68  // the rest stores the number of child indices.
69  const unsigned int two_bit_mask = (1 << 2) - 1;
70  const unsigned int dim = binary_representation[1] & two_bit_mask;
71  n_child_indices = (binary_representation[1] >> 2);
72 
74 
75  // Each child requires 'dim' bits to store its index
76  const unsigned int children_per_value =
77  sizeof(binary_type::value_type) * 8 / dim;
78  const unsigned int child_mask = (1 << dim) - 1;
79 
80  // Loop until all child indices have been read
81  unsigned int child_level = 0;
82  unsigned int binary_entry = 2;
83  while (child_level < n_child_indices)
84  {
85  for (unsigned int j = 0; j < children_per_value; ++j)
86  {
87  // Read the current child index by shifting to the current
88  // index's position and doing a bitwise-and with the child_mask.
89  child_indices[child_level] =
90  (binary_representation[binary_entry] >> (dim * j)) & child_mask;
91  ++child_level;
92  if (child_level == n_child_indices)
93  break;
94  }
95  ++binary_entry;
96  }
97 }
98 
99 
100 
101 CellId::CellId(const std::string &string_representation)
102 {
103  std::istringstream ss(string_representation);
104  ss >> *this;
105 }
106 
107 
108 
109 template <int dim>
112 {
113  CellId::binary_type binary_representation;
114  binary_representation.fill(0);
115 
117 
118  // The first entry stores the coarse cell id
119  binary_representation[0] = coarse_cell_id;
120 
121  // The rightmost two bits of the second entry store the dimension,
122  // the rest stores the number of child indices.
123  binary_representation[1] = (n_child_indices << 2);
124  binary_representation[1] |= dim;
125 
126  // Each child requires 'dim' bits to store its index
127  const unsigned int children_per_value =
128  sizeof(binary_type::value_type) * 8 / dim;
129  unsigned int child_level = 0;
130  unsigned int binary_entry = 2;
131 
132  // Loop until all child indices have been written
133  while (child_level < n_child_indices)
134  {
135  Assert(binary_entry < binary_representation.size(), ExcInternalError());
136 
137  for (unsigned int j = 0; j < children_per_value; ++j)
138  {
139  const unsigned int child_index =
140  static_cast<unsigned int>(child_indices[child_level]);
141  // Shift the child index to its position in the unsigned int and store
142  // it
143  binary_representation[binary_entry] |= (child_index << (j * dim));
144  ++child_level;
145  if (child_level == n_child_indices)
146  break;
147  }
148  ++binary_entry;
149  }
150 
151  return binary_representation;
152 }
153 
154 
155 
156 std::string
158 {
159  std::ostringstream ss;
160  ss << *this;
161  return ss.str();
162 }
163 
164 
165 // explicit instantiations
166 #include "cell_id.inst"
167 
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition: cell_id.h:232
unsigned int n_child_indices
Definition: cell_id.h:219
binary_type to_binary() const
Definition: cell_id.cc:111
std::string to_string() const
Definition: cell_id.cc:157
CellId()
Definition: cell_id.cc:25
types::coarse_cell_id coarse_cell_id
Definition: cell_id.h:213
std::array< unsigned int, 4 > binary_type
Definition: cell_id.h:81
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:474
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:475
static ::ExceptionBase & ExcInternalError()
#define Assert(cond, exc)
Definition: exceptions.h:1586
VectorType::value_type * begin(VectorType &V)
VectorType::value_type * end(VectorType &V)
void copy(const T *begin, const T *end, U *dest)
const types::coarse_cell_id invalid_coarse_cell_id
Definition: types.h:242
static const unsigned int invalid_unsigned_int
Definition: types.h:213
global_cell_index coarse_cell_id
Definition: types.h:127