deal.II version GIT relicensing-6917-gb466a1809f 2026-10-16 06:30: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: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2015 - 2025 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
14#include <deal.II/grid/tria.h>
15
16#include <limits>
17#include <sstream>
18
20
21
23 : coarse_cell_id(numbers::invalid_coarse_cell_id)
24 , n_child_indices(std::numeric_limits<std::uint8_t>::max())
25{
26 // initialize the child indices to invalid values
27 std::fill(child_indices.begin(),
28 child_indices.end(),
29 std::numeric_limits<std::uint8_t>::max());
30}
31
32
33
35 const std::vector<std::uint8_t> &id)
36 : coarse_cell_id(coarse_cell_id)
37 , n_child_indices(id.size())
38{
40 std::copy(id.begin(), id.end(), child_indices.begin());
41}
42
43
44
46 const unsigned int n_child_indices,
47 const std::uint8_t *id)
48 : coarse_cell_id(coarse_cell_id)
49 , n_child_indices(n_child_indices)
50{
52 std::memcpy(child_indices.data(), id, n_child_indices);
53}
54
55
56
57CellId::CellId(const CellId::binary_type &binary_representation)
58 : coarse_cell_id(binary_representation[0])
59 , n_child_indices(static_cast<std::uint8_t>(binary_representation[2] & 0xFF))
60{
62 child_indices.fill(std::numeric_limits<std::uint8_t>::max());
63
64 // This is the reverse of to_binary()
65 unsigned int binary_entry_index = 1;
66 int index_within_binary_entry = 7;
67 for (std::uint8_t child_no = 0; child_no < n_child_indices; ++child_no)
68 {
69 binary_type::value_type mask = 0;
70 if (child_no % 2 == 0)
71 mask |= (0x0F << 4);
72 else
73 mask |= 0x0F;
74
75 AssertIndexRange(binary_entry_index, binary_representation.size());
76 auto value = (binary_representation[binary_entry_index] >>
77 (8 * index_within_binary_entry)) &
78 mask;
79 if (child_no % 2 == 0)
80 value >>= 4;
81
82 // We don't have dim available here, so make sure everything fits in the
83 // 3d limit instead
84 AssertIndexRange(value, ReferenceCells::max_n_children<3>());
85 child_indices[child_no] = static_cast<std::uint8_t>(value);
86
87 if (child_no % 2 == 1)
88 --index_within_binary_entry;
89 if (index_within_binary_entry == -1)
90 {
91 ++binary_entry_index;
92 index_within_binary_entry = 7;
93 }
94 }
95}
96
97
98
99CellId::CellId(const std::string &string_representation)
100{
101 std::istringstream ss(string_representation);
102 ss >> *this;
103}
104
105
106
107template <int dim>
110{
111 binary_type binary_representation;
112 binary_representation.fill(0);
113
114 binary_representation[0] = coarse_cell_id;
115 unsigned int binary_entry_index = 1;
116 int index_within_binary_entry = 7;
117 // This encoding only makes sense if we can use 7 as an index of sorts (see
118 // below) into that binary type, so future-proof a little by asserting that
119 // binary_type::value_type is big enough for this to work:
120 Assert(index_within_binary_entry < int(sizeof(binary_type::value_type)),
122 // Encode each child as a distinct power of 2 inside the binary
123 // representation. Rather than memcpy(), this representation is constructed
124 // from high values to low values so that we have a unique numerical value in
125 // the integers of binary_representation for any cell: that way we get results
126 // independent of the endianness of the machine (i.e., text_oarchive will have
127 // the same results across platforms). Even though we don't need it, this
128 // particular choice preserves lexical ordering (i.e., if cell_id_1 <
129 // cell_id_2, then cell_id_1.to_binary() < cell_id_2.to_binary()).
130 for (std::uint8_t child_no = 0; child_no < n_child_indices; ++child_no)
131 {
132 Assert(child_indices[child_no] < ReferenceCells::max_n_children<dim>(),
134 Assert(ReferenceCells::max_n_children<dim>() <= 0x0F, ExcInternalError());
135
136 binary_type::value_type entry = 0;
137
138 if (child_no % 2 == 0)
139 entry |= (child_indices[child_no] & 0x0F) << 4;
140 else
141 entry |= child_indices[child_no] & 0x0F;
142
143 AssertIndexRange(binary_entry_index, binary_representation.size());
144 binary_representation[binary_entry_index] |=
145 (entry << 8 * index_within_binary_entry);
146
147 if (child_no % 2 == 1)
148 --index_within_binary_entry;
149 if (index_within_binary_entry == -1)
150 {
151 ++binary_entry_index;
152 index_within_binary_entry = 7;
153 }
154 }
155
156 if (binary_entry_index == 2)
157 Assert(index_within_binary_entry > 0, ExcInternalError());
158 // finally store the number of children at the end:
159 binary_representation[2] |= n_child_indices;
160
161 return binary_representation;
162}
163
164
165
166std::string
168{
169 std::ostringstream ss;
170 ss << *this;
171 return ss.str();
172}
173
174
175// explicit instantiations
176#include "grid/cell_id.inst"
177
*  iterator end()
*  *  iterator begin()
std::uint8_t n_child_indices
Definition cell_id.h:209
std::array< std::uint8_t, numbers::max_n_levels - 1 > child_indices
Definition cell_id.h:222
binary_type to_binary() const
Definition cell_id.cc:109
std::string to_string() const
Definition cell_id.cc:167
CellId()
Definition cell_id.cc:22
std::array< std::uint64_t, 3 > binary_type
Definition cell_id.h:71
types::coarse_cell_id coarse_cell_id
Definition cell_id.h:203
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
std::size_t size
Definition mpi.cc:733
STL namespace.