Dune Core Modules (2.9.0)

partitionset.hh
1 // SPDX-FileCopyrightText: Copyright (C) DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 #ifndef DUNE_GRID_COMMON_PARTITIONSET_HH
6 #define DUNE_GRID_COMMON_PARTITIONSET_HH
7 
10 #include <dune/grid/common/gridenums.hh>
11 
12 namespace Dune {
13 
19  namespace {
20 
21  // Simple TMP to deduce partition iterator type from set of partitions.
22  template<unsigned int partitions>
23  struct derive_partition_iterator_type
24  {
25  // We did not match any specialization, bail out...
26  static_assert(AlwaysFalse<std::integral_constant<unsigned int,partitions> >::value,
27  "There is no partition iterator for this combination of entity partitions");
28  };
29 
30 
31  // specializations of derive_partition_iterator_type for existing PartitionIteratorTypes
32 
33  template<>
34  struct derive_partition_iterator_type<
35  (1 << InteriorEntity)
36  >
37  : public std::integral_constant<PartitionIteratorType,Interior_Partition>
38  {};
39 
40  template<>
41  struct derive_partition_iterator_type<
42  (1 << InteriorEntity) |
43  (1 << BorderEntity)
44  >
45  : public std::integral_constant<PartitionIteratorType,InteriorBorder_Partition>
46  {};
47 
48  template<>
49  struct derive_partition_iterator_type<
50  (1 << InteriorEntity) |
51  (1 << BorderEntity) |
52  (1 << OverlapEntity)
53  >
54  : public std::integral_constant<PartitionIteratorType,Overlap_Partition>
55  {};
56 
57  template<>
58  struct derive_partition_iterator_type<
59  (1 << InteriorEntity) |
60  (1 << BorderEntity) |
61  (1 << OverlapEntity) |
62  (1 << FrontEntity)
63  >
64  : public std::integral_constant<PartitionIteratorType,OverlapFront_Partition>
65  {};
66 
67  template<>
68  struct derive_partition_iterator_type<
69  (1 << InteriorEntity) |
70  (1 << BorderEntity) |
71  (1 << OverlapEntity) |
72  (1 << FrontEntity) |
73  (1 << GhostEntity)
74  >
75  : public std::integral_constant<PartitionIteratorType,All_Partition>
76  {};
77 
78  template<>
79  struct derive_partition_iterator_type<
80  (1 << GhostEntity)
81  >
82  : public std::integral_constant<PartitionIteratorType,Ghost_Partition>
83  {};
84 
85 
86  // Simple TMP to deduce set of partitions from partition iterator type.
87 
88  template<PartitionIteratorType pitype>
89  struct derive_partition_set;
90 
91 
92  // specializations of derive_partition_set for existing PartitionIteratorTypes
93 
94  template<>
95  struct derive_partition_set<Interior_Partition>
96  : std::integral_constant<unsigned int, (1 << InteriorEntity)>
97  {};
98 
99  template<>
100  struct derive_partition_set<InteriorBorder_Partition>
101  : std::integral_constant<unsigned int, (1 << InteriorEntity) | (1 << BorderEntity)>
102  {};
103 
104  template<>
105  struct derive_partition_set<Overlap_Partition>
106  : std::integral_constant<unsigned int, (1 << InteriorEntity) | (1 << BorderEntity) | (1 << OverlapEntity)>
107  {};
108 
109  template<>
110  struct derive_partition_set<OverlapFront_Partition>
111  : std::integral_constant<unsigned int, (1 << InteriorEntity) | (1 << BorderEntity) | (1 << OverlapEntity) | (1 << FrontEntity)>
112  {};
113 
114  template<>
115  struct derive_partition_set<Ghost_Partition>
116  : std::integral_constant<unsigned int, (1 << GhostEntity)>
117  {};
118 
119  template<>
120  struct derive_partition_set<All_Partition>
121  : std::integral_constant<unsigned int, (1 << InteriorEntity) | (1 << BorderEntity) | (1 << OverlapEntity) | (1 << FrontEntity) | (1 << GhostEntity)>
122  {};
123 
124  } // anonymous namespace
125 
126 
128 
136  template<unsigned int partitions>
137  struct PartitionSet
138  {
140  static constexpr unsigned int value = partitions;
141 
142 
144  template<unsigned int p>
145  struct PartitionSet<partitions | p>
146  constexpr operator+(const PartitionSet<p>&) const
147  {
148  return PartitionSet<partitions | p>();
149  }
150 
152  template<unsigned int p>
153  struct PartitionSet<partitions & ~p>
154  constexpr operator-(const PartitionSet<p>&) const
155  {
156  return PartitionSet<partitions & ~p>();
157  }
158 
160  friend std::ostream& operator<<(std::ostream& os, const PartitionSet&)
161  {
162  unsigned int set = partitions;
163  os << "partition set {";
164  bool first = true;
165  for (unsigned int p = 0; set; set &= ~(1 << p++))
166  {
167  if (!(set & (1 << p)))
168  continue;
169  if (!first)
170  os << ",";
171  first = false;
172  os << static_cast<PartitionType>(p);
173  }
174  os << "}";
175  return os;
176  }
177 
179 
183  static constexpr PartitionIteratorType partitionIterator()
184  {
185  return derive_partition_iterator_type<partitions>::value;
186  }
187 
189  static constexpr bool contains(PartitionType pt)
190  {
191  return partitions & (1 << pt);
192  }
193 
195  template<unsigned int contained_partitions>
196  static constexpr bool contains(PartitionSet<contained_partitions>)
197  {
198  return (partitions & contained_partitions) == contained_partitions;
199  }
200 
202  template<unsigned int p2>
203  constexpr bool operator==(PartitionSet<p2>) const
204  {
205  return partitions == p2;
206  }
207 
209  template<unsigned int p2>
210  constexpr bool operator!=(PartitionSet<p2>) const
211  {
212  return partitions != p2;
213  }
214 
215  };
216 
218 
221  template<PartitionType p>
222  PartitionSet<(1 << p)> partitionSet()
223  {
224  return PartitionSet<(1 << p)>();
225  }
226 
231  template<PartitionIteratorType pitype>
232  constexpr PartitionSet<derive_partition_set<pitype>::value> partitionSet()
233  {
234  return PartitionSet<derive_partition_set<pitype>::value>();
235  }
236 
238  namespace Partitions {
239 
240 
241 #ifdef DOXYGEN
242 
244  typedef PartitionSet<...> Interior;
245 
247  typedef PartitionSet<...> Border;
248 
250  typedef PartitionSet<...> Overlap;
251 
253  typedef PartitionSet<...> Front;
254 
256  typedef PartitionSet<...> Ghost;
257 
259  typedef PartitionSet<...> InteriorBorder;
260 
262  typedef PartitionSet<...> InteriorBorderOverlap;
263 
265  typedef PartitionSet<...> InteriorBorderOverlapFront;
266 
268  typedef PartitionSet<...> All;
269 
270 
272  constexpr Interior interior;
273 
275  constexpr Border border;
276 
278  constexpr Overlap overlap;
279 
281  constexpr Front front;
282 
284  constexpr Ghost ghost;
285 
287  constexpr InteriorBorder interiorBorder;
288 
290  constexpr InteriorBorderOverlap interiorBorderOverlap;
291 
293  constexpr InteriorBorderOverlapFront interiorBorderOverlapFront;
294 
296  constexpr All all;
297 
298 #else // DOXYGEN
299 
300  // First declare the types and objects for individual partitions
301 
302  typedef decltype(partitionSet<InteriorEntity>()) Interior;
303  typedef decltype(partitionSet<BorderEntity>()) Border;
304  typedef decltype(partitionSet<OverlapEntity>()) Overlap;
305  typedef decltype(partitionSet<FrontEntity>()) Front;
306  typedef decltype(partitionSet<GhostEntity>()) Ghost;
307 
308 #ifndef __cpp_inline_variables
309  namespace {
310 #endif
311 
312  // place global objects in anonymous namespace to ensure that visibility is
313  // restricted to the current translation unit, making it easier for the compiler
314  // to eliminate the actual objects and to avoid linking problems
315 
316  DUNE_INLINE_VARIABLE constexpr Interior interior = {};
317  DUNE_INLINE_VARIABLE constexpr Border border = {};
318  DUNE_INLINE_VARIABLE constexpr Overlap overlap = {};
319  DUNE_INLINE_VARIABLE constexpr Front front = {};
320  DUNE_INLINE_VARIABLE constexpr Ghost ghost = {};
321 
322 #ifndef __cpp_inline_variables
323  }
324 #endif
325 
326  // Now we can declare the partition sets that are a result of combining partitions
327 
328  typedef decltype(interior + border) InteriorBorder;
329  typedef decltype(interior + border + overlap) InteriorBorderOverlap;
330  typedef decltype(interior + border + overlap + front) InteriorBorderOverlapFront;
331  typedef decltype(interior + border + overlap + front + ghost) All;
332 
333 #ifndef __cpp_inline_variables
334  namespace {
335 #endif
336 
337  // again, place the global objects in an anonymous namespace
338 
339  DUNE_INLINE_VARIABLE constexpr InteriorBorder interiorBorder = {};
340  DUNE_INLINE_VARIABLE constexpr InteriorBorderOverlap interiorBorderOverlap = {};
341  DUNE_INLINE_VARIABLE constexpr InteriorBorderOverlapFront interiorBorderOverlapFront = {};
342  DUNE_INLINE_VARIABLE constexpr All all = {};
343 
344 #ifndef __cpp_inline_variables
345  }
346 #endif
347 
348 #endif // DOXYGEN
349 
350  } // namespace Partitions
351 
356 } // namespace Dune
357 
358 #endif // DUNE_GRID_COMMON_PARTITIONSET_HH
Traits for type conversions and type information.
@ Interior_Partition
only interior entities
Definition: gridenums.hh:137
@ FrontEntity
on boundary between overlap and ghost
Definition: gridenums.hh:34
@ InteriorEntity
all interior entities
Definition: gridenums.hh:31
@ GhostEntity
ghost entities
Definition: gridenums.hh:35
@ BorderEntity
on boundary between interior and overlap
Definition: gridenums.hh:32
@ OverlapEntity
all entities lying in the overlap zone
Definition: gridenums.hh:33
Definitions of several macros that conditionally make C++ syntax available.
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (Apr 30, 22:37, 2024)