Reference documentation for deal.II version Git a1a4ec9101 2020-05-28 10:32:28 +0200
\(\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\}}\)
cell_id.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 1998 - 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 
16 #ifndef dealii_cell_id_h
17 #define dealii_cell_id_h
18 
19 #include <deal.II/base/config.h>
20 
22 
23 #include <array>
24 #include <cstdint>
25 #include <iostream>
26 #include <vector>
27 
28 #ifdef DEAL_II_WITH_P4EST
30 #endif
31 
33 
34 // Forward declarations
35 #ifndef DOXYGEN
36 template <int, int>
37 class Triangulation;
38 #endif
39 
69 class CellId
70 {
71 public:
79  using binary_type = std::array<unsigned int, 4>;
80 
92  const std::vector<std::uint8_t> &child_indices);
93 
106  const unsigned int n_child_indices,
107  const std::uint8_t * child_indices);
108 
113  CellId(const binary_type &binary_representation);
114 
118  CellId();
119 
132  std::string
133  to_string() const;
134 
138  template <int dim>
140  to_binary() const;
141 
145  template <int dim, int spacedim>
147  to_cell(const Triangulation<dim, spacedim> &tria) const;
148 
152  bool
153  operator==(const CellId &other) const;
154 
158  bool
159  operator!=(const CellId &other) const;
160 
166  bool
167  operator<(const CellId &other) const;
168 
172  bool
173  is_parent_of(const CellId &other) const;
174 
178  bool
179  is_ancestor_of(const CellId &other) const;
180 
184  template <class Archive>
185  void
186  serialize(Archive &ar, const unsigned int version);
187 
192  get_coarse_cell_id() const;
193 
194 private:
200 
205  unsigned int n_child_indices;
206 
216 #ifdef DEAL_II_WITH_P4EST
217  std::array<std::uint8_t, internal::p4est::functions<2>::max_level>
219 #else
220  std::array<std::uint8_t, 30> child_indices;
221 #endif
222 
223  friend std::istream &
224  operator>>(std::istream &is, CellId &cid);
225  friend std::ostream &
226  operator<<(std::ostream &os, const CellId &cid);
227 };
228 
229 
230 
234 inline std::ostream &
235 operator<<(std::ostream &os, const CellId &cid)
236 {
237  os << cid.coarse_cell_id << '_' << cid.n_child_indices << ':';
238  for (unsigned int i = 0; i < cid.n_child_indices; ++i)
239  // write the child indices. because they are between 0 and 2^dim-1, they all
240  // just have one digit, so we could write them as one character
241  // objects. it's probably clearer to write them as one-digit characters
242  // starting at '0'
243  os << static_cast<unsigned char>('0' + cid.child_indices[i]);
244  return os;
245 }
246 
247 
248 
252 template <class Archive>
253 void
254 CellId::serialize(Archive &ar, const unsigned int /*version*/)
255 {
256  ar &coarse_cell_id;
257  ar &n_child_indices;
258  ar &child_indices;
259 }
260 
264 inline std::istream &
265 operator>>(std::istream &is, CellId &cid)
266 {
267  unsigned int cellid;
268  is >> cellid;
269  if (is.eof())
270  return is;
271 
272  cid.coarse_cell_id = cellid;
273  char dummy;
274  is >> dummy;
275  Assert(dummy == '_', ExcMessage("invalid CellId"));
276  is >> cid.n_child_indices;
277  is >> dummy;
278  Assert(dummy == ':', ExcMessage("invalid CellId"));
279 
280  unsigned char value;
281  for (unsigned int i = 0; i < cid.n_child_indices; ++i)
282  {
283  // read the one-digit child index (as an integer number) and
284  // convert it back into unsigned integer type
285  is >> value;
286  cid.child_indices[i] = value - '0';
287  }
288  return is;
289 }
290 
291 
292 
293 inline bool
294 CellId::operator==(const CellId &other) const
295 {
296  if (this->coarse_cell_id != other.coarse_cell_id)
297  return false;
298  if (n_child_indices != other.n_child_indices)
299  return false;
300 
301  for (unsigned int i = 0; i < n_child_indices; ++i)
302  if (child_indices[i] != other.child_indices[i])
303  return false;
304 
305  return true;
306 }
307 
308 
309 
310 inline bool
311 CellId::operator!=(const CellId &other) const
312 {
313  return !(*this == other);
314 }
315 
316 
317 
318 inline bool
319 CellId::operator<(const CellId &other) const
320 {
321  if (this->coarse_cell_id != other.coarse_cell_id)
322  return this->coarse_cell_id < other.coarse_cell_id;
323 
324  unsigned int idx = 0;
325  while (idx < n_child_indices)
326  {
327  if (idx >= other.n_child_indices)
328  return false;
329 
330  if (child_indices[idx] != other.child_indices[idx])
331  return child_indices[idx] < other.child_indices[idx];
332 
333  ++idx;
334  }
335 
336  if (n_child_indices == other.n_child_indices)
337  return false;
338  return true; // other.id is longer
339 }
340 
341 
342 
343 inline bool
344 CellId::is_parent_of(const CellId &other) const
345 {
346  if (this->coarse_cell_id != other.coarse_cell_id)
347  return false;
348 
349  if (n_child_indices + 1 != other.n_child_indices)
350  return false;
351 
352  for (unsigned int idx = 0; idx < n_child_indices; ++idx)
353  if (child_indices[idx] != other.child_indices[idx])
354  return false;
355 
356  return true; // other.id is longer
357 }
358 
359 
360 
361 inline bool
362 CellId::is_ancestor_of(const CellId &other) const
363 {
364  if (this->coarse_cell_id != other.coarse_cell_id)
365  return false;
366 
367  if (n_child_indices >= other.n_child_indices)
368  return false;
369 
370  for (unsigned int idx = 0; idx < n_child_indices; ++idx)
371  if (child_indices[idx] != other.child_indices[idx])
372  return false;
373 
374  return true; // other.id is longer
375 }
376 
377 
380 {
381  return coarse_cell_id;
382 }
383 
384 
386 
387 #endif
void serialize(Archive &ar, const unsigned int version)
Definition: cell_id.h:254
types::coarse_cell_id get_coarse_cell_id() const
Definition: cell_id.h:379
Triangulation< dim, spacedim >::cell_iterator to_cell(const Triangulation< dim, spacedim > &tria) const
Definition: cell_id.cc:159
binary_type to_binary() const
Definition: cell_id.cc:102
bool is_parent_of(const CellId &other) const
Definition: cell_id.h:344
CellId()
Definition: cell_id.cc:24
std::array< unsigned int, 4 > binary_type
Definition: cell_id.h:79
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition: cell_id.h:235
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1419
bool operator<(const CellId &other) const
Definition: cell_id.h:319
bool operator!=(const CellId &other) const
Definition: cell_id.h:311
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:362
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition: cell_id.h:265
bool operator==(const CellId &other) const
Definition: cell_id.h:294
std::string to_string() const
Definition: cell_id.cc:148
const types::global_dof_index * dummy()
Definition: dof_handler.cc:59
Definition: cell_id.h:69
bool is_ancestor_of(const CellId &other) const
Definition: cell_id.h:362
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:361
unsigned int n_child_indices
Definition: cell_id.h:205
types::coarse_cell_id coarse_cell_id
Definition: cell_id.h:199
static const bool value
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition: cell_id.h:218