Reference documentation for deal.II version 9.4.1
\(\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\}}\)
Loading...
Searching...
No Matches
cell_id.cc
Go to the documentation of this file.
1// ---------------------------------------------------------------------
2//
3// Copyright (C) 2015 - 2020 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
17#include <deal.II/grid/tria.h>
18
19#include <sstream>
20
22
23
25 : coarse_cell_id(numbers::invalid_coarse_cell_id)
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
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{
56 memcpy(child_indices.data(), id, n_child_indices);
57}
58
59
60
61CellId::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
100CellId::CellId(const std::string &string_representation)
101{
102 std::istringstream ss(string_representation);
103 ss >> *this;
104}
105
106
107
108template <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
155std::string
157{
158 std::ostringstream ss;
159 ss << *this;
160 return ss.str();
161}
162
163
164
165template <int dim, int spacedim>
168{
169 return tria.create_cell_iterator(*this);
170}
171
172
173// explicit instantiations
174#include "cell_id.inst"
175
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition: cell_id.h:240
Triangulation< dim, spacedim >::cell_iterator to_cell(const Triangulation< dim, spacedim > &tria) const
Definition: cell_id.cc:167
unsigned int n_child_indices
Definition: cell_id.h:227
binary_type to_binary() const
Definition: cell_id.cc:110
std::string to_string() const
Definition: cell_id.cc:156
CellId()
Definition: cell_id.cc:24
types::coarse_cell_id coarse_cell_id
Definition: cell_id.h:221
std::array< unsigned int, 4 > binary_type
Definition: cell_id.h:80
cell_iterator create_cell_iterator(const CellId &cell_id) const
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:442
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:443
#define Assert(cond, exc)
Definition: exceptions.h:1473
static ::ExceptionBase & ExcInternalError()
const ::Triangulation< dim, spacedim > & tria