Reference documentation for deal.II version 9.1.0-pre
cell_id.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 2018 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 #ifndef dealii_cell_id_h
17 #define dealii_cell_id_h
18 
19 #include <deal.II/base/config.h>
20 
21 #include <deal.II/base/exceptions.h>
22 
23 #include <array>
24 #include <cstdint>
25 #include <iostream>
26 #include <vector>
27 
28 #ifdef DEAL_II_WITH_P4EST
29 # include <deal.II/distributed/p4est_wrappers.h>
30 #endif
31 
32 DEAL_II_NAMESPACE_OPEN
33 
34 template <int, int>
35 class Triangulation;
36 
63 class CellId
64 {
65 public:
73  using binary_type = std::array<unsigned int, 4>;
74 
85  CellId(const unsigned int coarse_cell_id,
86  const std::vector<std::uint8_t> &child_indices);
87 
99  CellId(const unsigned int coarse_cell_id,
100  const unsigned int n_child_indices,
101  const std::uint8_t *child_indices);
102 
107  CellId(const binary_type &binary_representation);
108 
112  CellId();
113 
117  std::string
118  to_string() const;
119 
123  template <int dim>
125  to_binary() const;
126 
130  template <int dim, int spacedim>
132  to_cell(const Triangulation<dim, spacedim> &tria) const;
133 
137  bool
138  operator==(const CellId &other) const;
139 
143  bool
144  operator!=(const CellId &other) const;
145 
151  bool
152  operator<(const CellId &other) const;
153 
157  template <class Archive>
158  void
159  serialize(Archive &ar, const unsigned int version);
160 
161 private:
166  unsigned int coarse_cell_id;
167 
172  unsigned int n_child_indices;
173 
183 #ifdef DEAL_II_WITH_P4EST
184  std::array<std::uint8_t, internal::p4est::functions<2>::max_level>
186 #else
187  std::array<std::uint8_t, 30> child_indices;
188 #endif
189 
190  friend std::istream &
191  operator>>(std::istream &is, CellId &cid);
192  friend std::ostream &
193  operator<<(std::ostream &os, const CellId &cid);
194 };
195 
196 
197 
201 inline std::ostream &
202 operator<<(std::ostream &os, const CellId &cid)
203 {
204  os << cid.coarse_cell_id << '_' << cid.n_child_indices << ':';
205  for (unsigned int i = 0; i < cid.n_child_indices; ++i)
206  // write the child indices. because they are between 0 and 2^dim-1, they all
207  // just have one digit, so we could write them as one character
208  // objects. it's probably clearer to write them as one-digit characters
209  // starting at '0'
210  os << static_cast<unsigned char>('0' + cid.child_indices[i]);
211  return os;
212 }
213 
214 
215 
219 template <class Archive>
220 void
221 CellId::serialize(Archive &ar, const unsigned int /*version*/)
222 {
223  ar &coarse_cell_id;
224  ar &n_child_indices;
225  ar &child_indices;
226 }
227 
231 inline std::istream &
232 operator>>(std::istream &is, CellId &cid)
233 {
234  unsigned int cellid;
235  is >> cellid;
236  if (is.eof())
237  return is;
238 
239  cid.coarse_cell_id = cellid;
240  char dummy;
241  is >> dummy;
242  Assert(dummy == '_', ExcMessage("invalid CellId"));
243  is >> cid.n_child_indices;
244  is >> dummy;
245  Assert(dummy == ':', ExcMessage("invalid CellId"));
246 
247  unsigned char value;
248  for (unsigned int i = 0; i < cid.n_child_indices; ++i)
249  {
250  // read the one-digit child index (as an integer number) and
251  // convert it back into unsigned integer type
252  is >> value;
253  cid.child_indices[i] = value - '0';
254  }
255  return is;
256 }
257 
258 
259 
260 inline bool
261 CellId::operator==(const CellId &other) const
262 {
263  if (this->coarse_cell_id != other.coarse_cell_id)
264  return false;
265  if (n_child_indices != other.n_child_indices)
266  return false;
267 
268  for (unsigned int i = 0; i < n_child_indices; ++i)
269  if (child_indices[i] != other.child_indices[i])
270  return false;
271 
272  return true;
273 }
274 
275 
276 
277 inline bool
278 CellId::operator!=(const CellId &other) const
279 {
280  return !(*this == other);
281 }
282 
283 
284 
285 inline bool
286 CellId::operator<(const CellId &other) const
287 {
288  if (this->coarse_cell_id != other.coarse_cell_id)
289  return this->coarse_cell_id < other.coarse_cell_id;
290 
291  unsigned int idx = 0;
292  while (idx < n_child_indices)
293  {
294  if (idx >= other.n_child_indices)
295  return false;
296 
297  if (child_indices[idx] != other.child_indices[idx])
298  return child_indices[idx] < other.child_indices[idx];
299 
300  ++idx;
301  }
302 
303  if (n_child_indices == other.n_child_indices)
304  return false;
305  return true; // other.id is longer
306 }
307 
308 DEAL_II_NAMESPACE_CLOSE
309 
310 #endif
void serialize(Archive &ar, const unsigned int version)
Definition: cell_id.h:221
bool operator<(const CellId &other) const
Definition: cell_id.h:286
CellId()
Definition: cell_id.cc:24
std::array< unsigned int, 4 > binary_type
Definition: cell_id.h:73
unsigned int coarse_cell_id
Definition: cell_id.h:166
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition: cell_id.h:202
static::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1227
std::string to_string() const
Definition: cell_id.cc:147
Triangulation< dim, spacedim >::cell_iterator to_cell(const Triangulation< dim, spacedim > &tria) const
Definition: cell_id.cc:158
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition: cell_id.h:232
binary_type to_binary() const
Definition: cell_id.cc:101
Definition: cell_id.h:63
unsigned int n_child_indices
Definition: cell_id.h:172
bool operator==(const CellId &other) const
Definition: cell_id.h:261
bool operator!=(const CellId &other) const
Definition: cell_id.h:278
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition: cell_id.h:185