Reference documentation for deal.II version GIT relicensing-426-g7976cfd195 2024-04-18 21:10:01+00:00
\(\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// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2015 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
16#include <deal.II/grid/tria.h>
17
18#include <limits>
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// explicit instantiations
165#include "cell_id.inst"
166
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition cell_id.h:231
unsigned int n_child_indices
Definition cell_id.h:218
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:212
std::array< unsigned int, 4 > binary_type
Definition cell_id.h:80
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:502
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:503
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()