Reference documentation for deal.II version Git ac8d010384 2020-11-27 19:49:05 +0100
\(\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 CellId::CellId(const std::string &string_representation)
101 {
102  std::istringstream ss(string_representation);
103  ss >> *this;
104 }
105 
106 
107 
108 template <int dim>
111 {
112  CellId::binary_type binary_representation;
113  binary_representation.fill(0);
114 
116 
117  // The first entry stores the coarse cell id
118  binary_representation[0] = coarse_cell_id;
119 
120  // The rightmost two bits of the second entry store the dimension,
121  // the rest stores the number of child indices.
122  binary_representation[1] = (n_child_indices << 2);
123  binary_representation[1] |= dim;
124 
125  // Each child requires 'dim' bits to store its index
126  const unsigned int children_per_value =
127  sizeof(binary_type::value_type) * 8 / dim;
128  unsigned int child_level = 0;
129  unsigned int binary_entry = 2;
130 
131  // Loop until all child indices have been written
132  while (child_level < n_child_indices)
133  {
134  Assert(binary_entry < binary_representation.size(), ExcInternalError());
135 
136  for (unsigned int j = 0; j < children_per_value; ++j)
137  {
138  const unsigned int child_index =
139  static_cast<unsigned int>(child_indices[child_level]);
140  // Shift the child index to its position in the unsigned int and store
141  // it
142  binary_representation[binary_entry] |= (child_index << (j * dim));
143  ++child_level;
144  if (child_level == n_child_indices)
145  break;
146  }
147  ++binary_entry;
148  }
149 
150  return binary_representation;
151 }
152 
153 
154 
155 std::string
157 {
158  std::ostringstream ss;
159  ss << *this;
160  return ss.str();
161 }
162 
163 
164 
165 template <int dim, int spacedim>
168 {
171 
172  for (unsigned int i = 0; i < n_child_indices; ++i)
173  cell = cell->child(static_cast<unsigned int>(child_indices[i]));
174 
175  return cell;
176 }
177 
178 // explicit instantiations
179 #include "cell_id.inst"
180 
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:196
const types::coarse_cell_id invalid_coarse_cell_id
Definition: types.h:220
Triangulation< dim, spacedim >::cell_iterator to_cell(const Triangulation< dim, spacedim > &tria) const
Definition: cell_id.cc:167
binary_type to_binary() const
Definition: cell_id.cc:110
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:1466
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:372
VectorType::value_type * end(VectorType &V)
std::string to_string() const
Definition: cell_id.cc:156
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:371
VectorType::value_type * begin(VectorType &V)
unsigned int n_child_indices
Definition: cell_id.h:211
types::coarse_cell_id coarse_cell_id
Definition: cell_id.h:205
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:224
static ::ExceptionBase & ExcInternalError()