Reference documentation for deal.II version 9.1.0-pre
cell_id.cc
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2015 - 2018 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  for (unsigned int i = 0; i < child_indices.size(); ++i)
32  child_indices[i] = std::numeric_limits<char>::max();
33 }
34 
35 
36 
37 CellId::CellId(const unsigned int coarse_cell_id,
38  const std::vector<std::uint8_t> &id)
39  : coarse_cell_id(coarse_cell_id)
40  , n_child_indices(id.size())
41 {
43  std::copy(id.begin(), id.end(), child_indices.begin());
44 }
45 
46 
47 
48 CellId::CellId(const unsigned int coarse_cell_id,
49  const unsigned int n_child_indices,
50  const std::uint8_t *id)
51  : coarse_cell_id(coarse_cell_id)
52  , n_child_indices(n_child_indices)
53 {
54  Assert(n_child_indices < child_indices.size(), ExcInternalError());
55  memcpy(&(child_indices[0]), id, n_child_indices);
56 }
57 
58 
59 
60 CellId::CellId(const CellId::binary_type &binary_representation)
61 {
62  // The first entry stores the coarse cell id
63  coarse_cell_id = binary_representation[0];
64 
65  // The rightmost two bits of the second entry store the dimension,
66  // the rest stores the number of child indices.
67  const unsigned int two_bit_mask = (1 << 2) - 1;
68  const unsigned int dim = binary_representation[1] & two_bit_mask;
69  n_child_indices = (binary_representation[1] >> 2);
70 
72 
73  // Each child requires 'dim' bits to store its index
74  const unsigned int children_per_value =
75  sizeof(binary_type::value_type) * 8 / dim;
76  const unsigned int child_mask = (1 << dim) - 1;
77 
78  // Loop until all child indices have been read
79  unsigned int child_level = 0;
80  unsigned int binary_entry = 2;
81  while (child_level < n_child_indices)
82  {
83  for (unsigned int j = 0; j < children_per_value; ++j)
84  {
85  // Read the current child index by shifting to the current
86  // index's position and doing a bitwise-and with the child_mask.
87  child_indices[child_level] =
88  (binary_representation[binary_entry] >> (dim * j)) & child_mask;
89  ++child_level;
90  if (child_level == n_child_indices)
91  break;
92  }
93  ++binary_entry;
94  }
95 }
96 
97 
98 
99 template <int dim>
102 {
103  CellId::binary_type binary_representation;
104  binary_representation.fill(0);
105 
107 
108  // The first entry stores the coarse cell id
109  binary_representation[0] = coarse_cell_id;
110 
111  // The rightmost two bits of the second entry store the dimension,
112  // the rest stores the number of child indices.
113  binary_representation[1] = (n_child_indices << 2);
114  binary_representation[1] |= dim;
115 
116  // Each child requires 'dim' bits to store its index
117  const unsigned int children_per_value =
118  sizeof(binary_type::value_type) * 8 / dim;
119  unsigned int child_level = 0;
120  unsigned int binary_entry = 2;
121 
122  // Loop until all child indices have been written
123  while (child_level < n_child_indices)
124  {
125  Assert(binary_entry < binary_representation.size(), ExcInternalError());
126 
127  for (unsigned int j = 0; j < children_per_value; ++j)
128  {
129  const unsigned int child_index =
130  static_cast<unsigned int>(child_indices[child_level]);
131  // Shift the child index to its position in the unsigned int and store
132  // it
133  binary_representation[binary_entry] |= (child_index << (j * dim));
134  ++child_level;
135  if (child_level == n_child_indices)
136  break;
137  }
138  ++binary_entry;
139  }
140 
141  return binary_representation;
142 }
143 
144 
145 
146 std::string
148 {
149  std::ostringstream ss;
150  ss << *this;
151  return ss.str();
152 }
153 
154 
155 
156 template <int dim, int spacedim>
159 {
161  0,
163 
164  for (unsigned int i = 0; i < n_child_indices; ++i)
165  cell = cell->child(static_cast<unsigned int>(child_indices[i]));
166 
167  return cell;
168 }
169 
170 // explicit instantiations
171 #include "cell_id.inst"
172 
173 DEAL_II_NAMESPACE_CLOSE
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:1227
std::string to_string() const
Definition: cell_id.cc:147
Triangulation< dim, spacedim >::cell_iterator to_cell(const Triangulation< dim, spacedim > &tria) const
Definition: cell_id.cc:158
binary_type to_binary() const
Definition: cell_id.cc:101
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()