Reference documentation for deal.II version 9.0.0
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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_cell_id_h
17 #define dealii_cell_id_h
18 
19 #include <deal.II/base/config.h>
20 #include <deal.II/base/exceptions.h>
21 
22 #include <array>
23 #include <vector>
24 #include <iostream>
25 #include <cstdint>
26 
27 #ifdef DEAL_II_WITH_P4EST
28 #include <deal.II/distributed/p4est_wrappers.h>
29 #endif
30 
31 DEAL_II_NAMESPACE_OPEN
32 
33 template <int, int> class Triangulation;
34 
61 class CellId
62 {
63 public:
71  typedef std::array<unsigned int, 4> binary_type;
72 
83  CellId(const unsigned int coarse_cell_id,
84  const std::vector<std::uint8_t> &child_indices);
85 
97  CellId(const unsigned int coarse_cell_id,
98  const unsigned int n_child_indices,
99  const std::uint8_t *child_indices);
100 
105  CellId(const binary_type &binary_representation);
106 
110  CellId();
111 
115  std::string to_string() const;
116 
120  template <int dim>
121  binary_type to_binary() const;
122 
126  template <int dim, int spacedim>
128  to_cell(const Triangulation<dim,spacedim> &tria) const;
129 
133  bool operator== (const CellId &other) const;
134 
138  bool operator!= (const CellId &other) const;
139 
145  bool operator<(const CellId &other) const;
146 
150  template<class Archive>
151  void serialize(Archive &ar, const unsigned int version );
152 
153 private:
158  unsigned int coarse_cell_id;
159 
164  unsigned int n_child_indices;
165 
175 #ifdef DEAL_II_WITH_P4EST
176  std::array<std::uint8_t,internal::p4est::functions<2>::max_level> child_indices;
177 #else
178  std::array<std::uint8_t,30> child_indices;
179 #endif
180 
181  friend std::istream &operator>> (std::istream &is, CellId &cid);
182  friend std::ostream &operator<< (std::ostream &os, const CellId &cid);
183 };
184 
185 
186 
187 
191 inline
192 std::ostream &operator<< (std::ostream &os,
193  const CellId &cid)
194 {
195  os << cid.coarse_cell_id << '_' << cid.n_child_indices << ':';
196  for (unsigned int i=0; i<cid.n_child_indices; ++i)
197  // write the child indices. because they are between 0 and 2^dim-1, they all
198  // just have one digit, so we could write them as one character
199  // objects. it's probably clearer to write them as one-digit characters
200  // starting at '0'
201  os << static_cast<unsigned char>('0' + cid.child_indices[i]);
202  return os;
203 }
204 
205 
206 
210 template<class Archive>
211 void CellId::serialize(Archive &ar, const unsigned int /*version*/)
212 {
213  ar &coarse_cell_id;
214  ar &n_child_indices;
215  ar &child_indices;
216 }
217 
221 inline
222 std::istream &operator>> (std::istream &is,
223  CellId &cid)
224 {
225  unsigned int cellid;
226  is >> cellid;
227  if (is.eof())
228  return is;
229 
230  cid.coarse_cell_id = cellid;
231  char dummy;
232  is >> dummy;
233  Assert(dummy=='_', ExcMessage("invalid CellId"));
234  is >> cid.n_child_indices;
235  is >> dummy;
236  Assert(dummy==':', ExcMessage("invalid CellId"));
237 
238  unsigned char value;
239  for (unsigned int i=0; i<cid.n_child_indices; ++i)
240  {
241  // read the one-digit child index (as an integer number) and
242  // convert it back into unsigned integer type
243  is >> value;
244  cid.child_indices[i] = value-'0';
245  }
246  return is;
247 }
248 
249 
250 
251 inline bool
252 CellId::operator== (const CellId &other) const
253 {
254  if (this->coarse_cell_id != other.coarse_cell_id)
255  return false;
256  if (n_child_indices != other.n_child_indices)
257  return false;
258 
259  for (unsigned int i=0; i<n_child_indices; ++i)
260  if (child_indices[i] != other.child_indices[i])
261  return false;
262 
263  return true;
264 }
265 
266 
267 
268 inline bool
269 CellId::operator!= (const CellId &other) const
270 {
271  return !(*this == other);
272 }
273 
274 
275 
276 inline
277 bool CellId::operator<(const CellId &other) const
278 {
279  if (this->coarse_cell_id != other.coarse_cell_id)
280  return this->coarse_cell_id < other.coarse_cell_id;
281 
282  unsigned int idx = 0;
283  while (idx < n_child_indices)
284  {
285  if (idx>=other.n_child_indices)
286  return false;
287 
288  if (child_indices[idx] != other.child_indices[idx])
289  return child_indices[idx] < other.child_indices[idx];
290 
291  ++idx;
292  }
293 
294  if (n_child_indices == other.n_child_indices)
295  return false;
296  return true; // other.id is longer
297 }
298 
299 DEAL_II_NAMESPACE_CLOSE
300 
301 #endif
void serialize(Archive &ar, const unsigned int version)
Definition: cell_id.h:211
std::array< std::uint8_t, internal::p4est::functions< 2 >::max_level > child_indices
Definition: cell_id.h:176
Triangulation< dim, spacedim >::cell_iterator to_cell(const Triangulation< dim, spacedim > &tria) const
Definition: cell_id.cc:163
binary_type to_binary() const
Definition: cell_id.cc:107
CellId()
Definition: cell_id.cc:25
unsigned int coarse_cell_id
Definition: cell_id.h:158
friend std::ostream & operator<<(std::ostream &os, const CellId &cid)
Definition: cell_id.h:192
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1142
bool operator<(const CellId &other) const
Definition: cell_id.h:277
bool operator!=(const CellId &other) const
Definition: cell_id.h:269
friend std::istream & operator>>(std::istream &is, CellId &cid)
Definition: cell_id.h:222
bool operator==(const CellId &other) const
Definition: cell_id.h:252
std::string to_string() const
Definition: cell_id.cc:152
Definition: cell_id.h:61
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:163
unsigned int n_child_indices
Definition: cell_id.h:164
std::array< unsigned int, 4 > binary_type
Definition: cell_id.h:71
std::istream & operator>>(std::istream &in, Point< dim, Number > &p)
Definition: point.h:570