Reference documentation for deal.II version Git 423cd11810 2020-05-28 01:07:25 +0200
\(\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 - 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 
22 
23 
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(),
34 }
35 
36 
37 
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 
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 {
55  Assert(n_child_indices < child_indices.size(), ExcInternalError());
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 {
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 
virtual unsigned int coarse_cell_id_to_coarse_cell_index(const types::coarse_cell_id coarse_cell_id) const
static const unsigned int invalid_unsigned_int
Definition: types.h:191
const types::coarse_cell_id invalid_coarse_cell_id
Definition: types.h:215
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:79
#define Assert(cond, exc)
Definition: exceptions.h:1419
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:362
VectorType::value_type * end(VectorType &V)
std::string to_string() const
Definition: cell_id.cc:148
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:361
VectorType::value_type * begin(VectorType &V)
unsigned int n_child_indices
Definition: cell_id.h:205
types::coarse_cell_id coarse_cell_id
Definition: cell_id.h:199
global_cell_index coarse_cell_id
Definition: types.h:114
void copy(const T *begin, const T *end, U *dest)
T max(const T &t, const MPI_Comm &mpi_communicator)
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition: cell_id.h:218
static ::ExceptionBase & ExcInternalError()