Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
cell_id.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2015 - 2019 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 <sstream>
20 
21 DEAL_II_NAMESPACE_OPEN
22 
23 
25  : coarse_cell_id(numbers::invalid_unsigned_int)
26  , n_child_indices(numbers::invalid_unsigned_int)
27 {
28  // initialize the child indices to invalid values
29  // (the only allowed values are between zero and
30  // GeometryInfo<dim>::max_children_per_cell)
31  std::fill(child_indices.begin(),
32  child_indices.end(),
33  std::numeric_limits<char>::max());
34 }
35 
36 
37 
38 CellId::CellId(const unsigned int coarse_cell_id,
39  const std::vector<std::uint8_t> &id)
40  : coarse_cell_id(coarse_cell_id)
41  , n_child_indices(id.size())
42 {
44  std::copy(id.begin(), id.end(), child_indices.begin());
45 }
46 
47 
48 
49 CellId::CellId(const unsigned int coarse_cell_id,
50  const unsigned int n_child_indices,
51  const std::uint8_t *id)
52  : coarse_cell_id(coarse_cell_id)
53  , n_child_indices(n_child_indices)
54 {
56  memcpy(child_indices.data(), id, n_child_indices);
57 }
58 
59 
60 
61 CellId::CellId(const CellId::binary_type &binary_representation)
62 {
63  // The first entry stores the coarse cell id
64  coarse_cell_id = binary_representation[0];
65 
66  // The rightmost two bits of the second entry store the dimension,
67  // the rest stores the number of child indices.
68  const unsigned int two_bit_mask = (1 << 2) - 1;
69  const unsigned int dim = binary_representation[1] & two_bit_mask;
70  n_child_indices = (binary_representation[1] >> 2);
71 
73 
74  // Each child requires 'dim' bits to store its index
75  const unsigned int children_per_value =
76  sizeof(binary_type::value_type) * 8 / dim;
77  const unsigned int child_mask = (1 << dim) - 1;
78 
79  // Loop until all child indices have been read
80  unsigned int child_level = 0;
81  unsigned int binary_entry = 2;
82  while (child_level < n_child_indices)
83  {
84  for (unsigned int j = 0; j < children_per_value; ++j)
85  {
86  // Read the current child index by shifting to the current
87  // index's position and doing a bitwise-and with the child_mask.
88  child_indices[child_level] =
89  (binary_representation[binary_entry] >> (dim * j)) & child_mask;
90  ++child_level;
91  if (child_level == n_child_indices)
92  break;
93  }
94  ++binary_entry;
95  }
96 }
97 
98 
99 
100 template <int dim>
103 {
104  CellId::binary_type binary_representation;
105  binary_representation.fill(0);
106 
108 
109  // The first entry stores the coarse cell id
110  binary_representation[0] = coarse_cell_id;
111 
112  // The rightmost two bits of the second entry store the dimension,
113  // the rest stores the number of child indices.
114  binary_representation[1] = (n_child_indices << 2);
115  binary_representation[1] |= dim;
116 
117  // Each child requires 'dim' bits to store its index
118  const unsigned int children_per_value =
119  sizeof(binary_type::value_type) * 8 / dim;
120  unsigned int child_level = 0;
121  unsigned int binary_entry = 2;
122 
123  // Loop until all child indices have been written
124  while (child_level < n_child_indices)
125  {
126  Assert(binary_entry < binary_representation.size(), ExcInternalError());
127 
128  for (unsigned int j = 0; j < children_per_value; ++j)
129  {
130  const unsigned int child_index =
131  static_cast<unsigned int>(child_indices[child_level]);
132  // Shift the child index to its position in the unsigned int and store
133  // it
134  binary_representation[binary_entry] |= (child_index << (j * dim));
135  ++child_level;
136  if (child_level == n_child_indices)
137  break;
138  }
139  ++binary_entry;
140  }
141 
142  return binary_representation;
143 }
144 
145 
146 
147 std::string
149 {
150  std::ostringstream ss;
151  ss << *this;
152  return ss.str();
153 }
154 
155 
156 
157 template <int dim, int spacedim>
160 {
162  0,
164 
165  for (unsigned int i = 0; i < n_child_indices; ++i)
166  cell = cell->child(static_cast<unsigned int>(child_indices[i]));
167 
168  return cell;
169 }
170 
171 // explicit instantiations
172 #include "cell_id.inst"
173 
174 DEAL_II_NAMESPACE_CLOSE
Triangulation< dim, spacedim >::cell_iterator to_cell(const Triangulation< dim, spacedim > &tria) const
Definition: cell_id.cc:159
binary_type to_binary() const
Definition: cell_id.cc:102
CellId()
Definition: cell_id.cc:24
std::array< unsigned int, 4 > binary_type
Definition: cell_id.h:73
unsigned int coarse_cell_id
Definition: cell_id.h:166
#define Assert(cond, exc)
Definition: exceptions.h:1407
std::string to_string() const
Definition: cell_id.cc:148
unsigned int n_child_indices
Definition: cell_id.h:172
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition: cell_id.h:185
static ::ExceptionBase & ExcInternalError()