deal.II version GIT relicensing-7182-g951947b1c4 2026-11-26 10: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
refinement_choice.h
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) 2026 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
13#ifndef dealii_grid_refinement_choice_h
14#define dealii_grid_refinement_choice_h
15
16
17#include <deal.II/base/config.h>
18
22
23#include <cstdint>
24#include <string>
25
26
28
29// Forward declarations
30template <int dim>
32
33namespace internal
34{
48 template <int dim>
49 constexpr RefinementChoice<dim>
50 make_refinement_choice_from_int(const std::uint8_t kind);
51} // namespace internal
52
53
54
63template <int dim>
65{
66public:
70 constexpr RefinementChoice();
71
84 constexpr
85 operator std::uint8_t() const;
86
90 constexpr bool
91 operator==(const RefinementChoice<dim> &other_choice) const;
92
96 constexpr bool
97 operator!=(const RefinementChoice<dim> &other_choice) const;
98
102 std::string
103 to_string() const;
104
113 constexpr RefinementChoice<dim>
114 operator|(const RefinementChoice<dim> &other_choice) const;
115
125 constexpr RefinementChoice<dim>
126 operator&(const RefinementChoice<dim> &other_choice) const;
127
137 constexpr RefinementChoice<dim>
138 operator~() const;
139
148 constexpr bool
149 is_anisotropic() const;
150
156 static constexpr RefinementChoice<dim>
157 cut_axis(const unsigned int i);
158
163 constexpr bool
164 is_tetrahedral() const;
165
171 constexpr unsigned int
172 new_tetrahedron_line() const;
173
174private:
178 static constexpr std::uint8_t isotropic_kind = dim == 0 ? 1 : (1 << dim) - 1;
179
183 std::uint8_t kind;
184
188 constexpr explicit RefinementChoice(const std::uint8_t kind);
189
194 template <int dim_>
195 friend constexpr RefinementChoice<dim_>
197};
198
199
200/*----------------------------- inline functions -----------------------------*/
201
202
203template <int dim>
205 : kind(0)
206{}
207
208
209
210template <int dim>
212 const std::uint8_t kind)
213 : kind(kind)
214{
215 static_assert(0 <= dim && dim <= 3, "Only implemented for 0d - 3d.");
216 // Prevent users from creating illegal combinations of dim and kind. These
217 // checks duplicate the values of 'kind' encoded in RefinementChoices.
218 if constexpr (dim == 0)
220 ExcMessage("The only valid RefinementChoices in 0d are None and "
221 "Isotropic."));
222 else if constexpr (dim == 1)
224 ExcMessage("The only valid RefinementChoices in 1d are None and "
225 "Isotropic."));
226 else if constexpr (dim == 2)
228 ExcMessage("The only valid RefinementChoices in 2d are None, "
229 "CutX, CutY, and Isotropic."));
230 else if constexpr (dim == 3)
231 Assert(kind <= isotropic_kind || (8 <= kind && kind <= 10),
232 ExcMessage("This RefinementChoice isn't implemented in 3d."));
233}
234
235namespace internal
236{
237 template <int dim>
238 inline constexpr RefinementChoice<dim>
239 make_refinement_choice_from_int(const std::uint8_t kind)
240 {
241 return RefinementChoice<dim>(kind);
242 }
243} // namespace internal
244
251{
255 template <int dim>
257 internal::make_refinement_choice_from_int<dim>(0);
258
265 template <int dim>
267 internal::make_refinement_choice_from_int<dim>(1);
268
274 template <int dim>
276 internal::make_refinement_choice_from_int<dim>(2);
277
284 template <int dim>
286 internal::make_refinement_choice_from_int<dim>(3);
287
291 template <int dim>
293 internal::make_refinement_choice_from_int<dim>(4);
294
298 template <int dim>
300 internal::make_refinement_choice_from_int<dim>(5);
301
305 template <int dim>
307 internal::make_refinement_choice_from_int<dim>(6);
308
317 template <int dim>
319 internal::make_refinement_choice_from_int<dim>(dim == 0 ? 1 :
320 (1 << dim) - 1);
321
326 template <int dim>
328 internal::make_refinement_choice_from_int<dim>(8);
329
334 template <int dim>
336 internal::make_refinement_choice_from_int<dim>(9);
337
342 template <int dim>
344 internal::make_refinement_choice_from_int<dim>(10);
345} // namespace RefinementChoices
346
347
348
349template <int dim>
350inline constexpr RefinementChoice<dim>::
351operator std::uint8_t() const
352{
353 return this->kind;
354}
355
356
357
358template <int dim>
359inline constexpr bool
361{
362 return this->kind == other.kind;
363}
364
365
366
367template <int dim>
368inline constexpr bool
370{
371 return !(*this == other);
372}
373
374
375
376template <int dim>
377inline constexpr RefinementChoice<dim>
379 const RefinementChoice<dim> &other_choice) const
380{
381 // Canonicalize by ensuring this->kind <= other_choice.kind
382 if (*this == other_choice)
383 return *this;
384 if (this->kind > other_choice.kind)
385 return other_choice.operator|(*this);
386
387 if (*this == RefinementChoices::None<dim>)
388 return other_choice;
389
390 // Hypercube refinements are stored as bit fields:
391 if (dim < 3 || !other_choice.is_tetrahedral())
392 return RefinementChoice<dim>(this->kind | other_choice.kind);
393
394 // Tetrahedral refinements can only be combined with None (handled above) or
395 // themselves (also handled above).
396 Assert(this->is_tetrahedral(),
397 ExcMessage("Tetrahedral refinements (e.g., Cut49) cannot be combined "
398 "with refinements of other cells (e.g., CutX or "
399 "Isotropic)."));
400 Assert(*this == other_choice,
401 ExcMessage("The union of distinct tetrahedral refinements, e.g., "
402 "Cut49 with Cut57, is not defined since these are mutually "
403 "exclusive operations."));
404 return RefinementChoices::None<dim>;
405}
406
407
408
409template <int dim>
410inline constexpr RefinementChoice<dim>
412 const RefinementChoice<dim> &other_choice) const
413{
414 // Like operator|(), canonicalize by ensuring this->kind <= other_choice.kind
415 if (*this == other_choice)
416 return *this;
417 if (this->kind > other_choice.kind)
418 return other_choice.operator&(*this);
419
420 if (*this == RefinementChoices::None<dim>)
421 return *this;
422
423 // Hypercube refinements are stored as bit fields:
424 if (dim < 3 || !other_choice.is_tetrahedral())
425 return RefinementChoice<dim>(this->kind & other_choice.kind);
426
427 // Tetrahedral refinements can only be combined with None (handled above) or
428 // other tetrahedral refinements. Since these are mutually exclusive their
429 // intersection is empty.
430 Assert(this->is_tetrahedral(),
431 ExcMessage("Tetrahedral refinements (e.g., Cut49) cannot be combined "
432 "with refinements of other cells (e.g., CutX or "
433 "Isotropic)."));
434 Assert(*this != other_choice, ExcInternalError());
435 return RefinementChoices::None<dim>;
436}
437
438
439
440template <int dim>
441inline constexpr RefinementChoice<dim>
443{
444 if (this->is_tetrahedral())
445 return RefinementChoices::None<dim>;
446 else
447 return RefinementChoice<dim>(~(this->kind) & isotropic_kind);
448}
449
450
451
452template <int dim>
453inline constexpr bool
455{
456 return !(*this == RefinementChoices::None<dim> ||
457 *this == RefinementChoices::Isotropic<dim> ||
458 this->is_tetrahedral());
459}
460
461
462
463template <int dim>
464inline constexpr RefinementChoice<dim>
466{
467 // Since there are no axes in 0d this also prevents calling this function
468 // in that case.
469 AssertIndexRange(i, dim);
470 // As with RefinementCase, cutting along an axis sets the bit corresponding to
471 // that axis.
472 return RefinementChoice<dim>(static_cast<std::uint8_t>(1u << i));
473}
474
475
476
477template <int dim>
478inline constexpr bool
480{
481 return dim == 3 && this->kind > isotropic_kind;
482}
483
484
485
486template <int dim>
487inline constexpr unsigned int
489{
490 Assert(is_tetrahedral(),
491 ExcMessage("This function may only be called on tetrahedral "
492 "refinement choices."));
493 switch (this->kind)
494 {
495 case RefinementChoices::Cut49<3>:
496 return 2;
497 case RefinementChoices::Cut57<3>:
498 return 1;
499 case RefinementChoices::Cut68<3>:
500 return 0;
501 default:
504 }
505}
506
507
509
510#endif
A class which enumerates refinement possibilities for all supported ReferenceCell objects.
constexpr RefinementChoice< dim > operator|(const RefinementChoice< dim > &other_choice) const
constexpr RefinementChoice< dim > operator&(const RefinementChoice< dim > &other_choice) const
static constexpr RefinementChoice< dim > cut_axis(const unsigned int i)
constexpr unsigned int new_tetrahedron_line() const
constexpr bool is_anisotropic() const
constexpr bool operator==(const RefinementChoice< dim > &other_choice) const
constexpr bool is_tetrahedral() const
constexpr bool operator!=(const RefinementChoice< dim > &other_choice) const
constexpr RefinementChoice()
std::string to_string() const
constexpr RefinementChoice< dim > operator~() const
static constexpr std::uint8_t isotropic_kind
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
#define DEAL_II_NOT_IMPLEMENTED()
#define Assert(cond, exc)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcMessage(std::string arg1)
constexpr RefinementChoice< dim > CutXZ
constexpr RefinementChoice< dim > CutZ
constexpr RefinementChoice< dim > Cut57
constexpr RefinementChoice< dim > CutXY
constexpr RefinementChoice< dim > CutX
constexpr RefinementChoice< dim > Isotropic
constexpr RefinementChoice< dim > CutYZ
constexpr RefinementChoice< dim > CutY
constexpr RefinementChoice< dim > Cut49
constexpr RefinementChoice< dim > Cut68
constexpr RefinementChoice< dim > None
constexpr RefinementChoice< dim > make_refinement_choice_from_int(const std::uint8_t kind)
constexpr unsigned int invalid_unsigned_int
Definition types.h:228