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