Dune Core Modules (unstable)

partitionset.hh
1// SPDX-FileCopyrightText: Copyright © 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
9#include <dune/grid/common/gridenums.hh>
10
11namespace Dune {
12
18 namespace {
19
20 // Simple TMP to deduce partition iterator type from set of partitions.
21 template<unsigned int partitions>
22 struct derive_partition_iterator_type
23 {
24 // We did not match any specialization, bail out...
25 static_assert(AlwaysFalse<std::integral_constant<unsigned int,partitions> >::value,
26 "There is no partition iterator for this combination of entity partitions");
27 };
28
29
30 // specializations of derive_partition_iterator_type for existing PartitionIteratorTypes
31
32 template<>
33 struct derive_partition_iterator_type<
34 (1 << InteriorEntity)
35 >
36 : public std::integral_constant<PartitionIteratorType,Interior_Partition>
37 {};
38
39 template<>
40 struct derive_partition_iterator_type<
41 (1 << InteriorEntity) |
42 (1 << BorderEntity)
43 >
44 : public std::integral_constant<PartitionIteratorType,InteriorBorder_Partition>
45 {};
46
47 template<>
48 struct derive_partition_iterator_type<
49 (1 << InteriorEntity) |
50 (1 << BorderEntity) |
51 (1 << OverlapEntity)
52 >
53 : public std::integral_constant<PartitionIteratorType,Overlap_Partition>
54 {};
55
56 template<>
57 struct derive_partition_iterator_type<
58 (1 << InteriorEntity) |
59 (1 << BorderEntity) |
60 (1 << OverlapEntity) |
61 (1 << FrontEntity)
62 >
63 : public std::integral_constant<PartitionIteratorType,OverlapFront_Partition>
64 {};
65
66 template<>
67 struct derive_partition_iterator_type<
68 (1 << InteriorEntity) |
69 (1 << BorderEntity) |
70 (1 << OverlapEntity) |
71 (1 << FrontEntity) |
72 (1 << GhostEntity)
73 >
74 : public std::integral_constant<PartitionIteratorType,All_Partition>
75 {};
76
77 template<>
78 struct derive_partition_iterator_type<
79 (1 << GhostEntity)
80 >
81 : public std::integral_constant<PartitionIteratorType,Ghost_Partition>
82 {};
83
84
85 // Simple TMP to deduce set of partitions from partition iterator type.
86
87 template<PartitionIteratorType pitype>
88 struct derive_partition_set;
89
90
91 // specializations of derive_partition_set for existing PartitionIteratorTypes
92
93 template<>
94 struct derive_partition_set<Interior_Partition>
95 : std::integral_constant<unsigned int, (1 << InteriorEntity)>
96 {};
97
98 template<>
99 struct derive_partition_set<InteriorBorder_Partition>
100 : std::integral_constant<unsigned int, (1 << InteriorEntity) | (1 << BorderEntity)>
101 {};
102
103 template<>
104 struct derive_partition_set<Overlap_Partition>
105 : std::integral_constant<unsigned int, (1 << InteriorEntity) | (1 << BorderEntity) | (1 << OverlapEntity)>
106 {};
107
108 template<>
109 struct derive_partition_set<OverlapFront_Partition>
110 : std::integral_constant<unsigned int, (1 << InteriorEntity) | (1 << BorderEntity) | (1 << OverlapEntity) | (1 << FrontEntity)>
111 {};
112
113 template<>
114 struct derive_partition_set<Ghost_Partition>
115 : std::integral_constant<unsigned int, (1 << GhostEntity)>
116 {};
117
118 template<>
119 struct derive_partition_set<All_Partition>
120 : std::integral_constant<unsigned int, (1 << InteriorEntity) | (1 << BorderEntity) | (1 << OverlapEntity) | (1 << FrontEntity) | (1 << GhostEntity)>
121 {};
122
123 } // anonymous namespace
124
125
127
135 template<unsigned int partitions>
136 struct PartitionSet
137 {
139 static constexpr unsigned int value = partitions;
140
141
143 template<unsigned int p>
144 struct PartitionSet<partitions | p>
145 constexpr operator+(const PartitionSet<p>&) const
146 {
147 return PartitionSet<partitions | p>();
148 }
149
151 template<unsigned int p>
152 struct PartitionSet<partitions & ~p>
153 constexpr operator-(const PartitionSet<p>&) const
154 {
155 return PartitionSet<partitions & ~p>();
156 }
157
159 friend std::ostream& operator<<(std::ostream& os, const PartitionSet&)
160 {
161 unsigned int set = partitions;
162 os << "partition set {";
163 bool first = true;
164 for (unsigned int p = 0; set; set &= ~(1 << p++))
165 {
166 if (!(set & (1 << p)))
167 continue;
168 if (!first)
169 os << ",";
170 first = false;
171 os << static_cast<PartitionType>(p);
172 }
173 os << "}";
174 return os;
175 }
176
178
182 static constexpr PartitionIteratorType partitionIterator()
183 {
184 return derive_partition_iterator_type<partitions>::value;
185 }
186
188 static constexpr bool contains(PartitionType pt)
189 {
190 return partitions & (1 << pt);
191 }
192
194 template<unsigned int contained_partitions>
195 static constexpr bool contains(PartitionSet<contained_partitions>)
196 {
197 return (partitions & contained_partitions) == contained_partitions;
198 }
199
201 template<unsigned int p2>
202 constexpr bool operator==(PartitionSet<p2>) const
203 {
204 return partitions == p2;
205 }
206
208 template<unsigned int p2>
209 constexpr bool operator!=(PartitionSet<p2>) const
210 {
211 return partitions != p2;
212 }
213
214 };
215
217
220 template<PartitionType p>
221 PartitionSet<(1 << p)> partitionSet()
222 {
223 return PartitionSet<(1 << p)>();
224 }
225
230 template<PartitionIteratorType pitype>
231 constexpr PartitionSet<derive_partition_set<pitype>::value> partitionSet()
232 {
233 return PartitionSet<derive_partition_set<pitype>::value>();
234 }
235
237 namespace Partitions {
238
239
240#ifdef DOXYGEN
241
243 typedef PartitionSet<...> Interior;
244
246 typedef PartitionSet<...> Border;
247
249 typedef PartitionSet<...> Overlap;
250
252 typedef PartitionSet<...> Front;
253
255 typedef PartitionSet<...> Ghost;
256
258 typedef PartitionSet<...> InteriorBorder;
259
261 typedef PartitionSet<...> InteriorBorderOverlap;
262
264 typedef PartitionSet<...> InteriorBorderOverlapFront;
265
267 typedef PartitionSet<...> All;
268
269
271 constexpr Interior interior;
272
274 constexpr Border border;
275
277 constexpr Overlap overlap;
278
280 constexpr Front front;
281
283 constexpr Ghost ghost;
284
286 constexpr InteriorBorder interiorBorder;
287
289 constexpr InteriorBorderOverlap interiorBorderOverlap;
290
292 constexpr InteriorBorderOverlapFront interiorBorderOverlapFront;
293
295 constexpr All all;
296
297#else // DOXYGEN
298
299 // First declare the types and objects for individual partitions
300
301 typedef decltype(partitionSet<InteriorEntity>()) Interior;
302 typedef decltype(partitionSet<BorderEntity>()) Border;
303 typedef decltype(partitionSet<OverlapEntity>()) Overlap;
304 typedef decltype(partitionSet<FrontEntity>()) Front;
305 typedef decltype(partitionSet<GhostEntity>()) Ghost;
306
307 inline constexpr Interior interior = {};
308 inline constexpr Border border = {};
309 inline constexpr Overlap overlap = {};
310 inline constexpr Front front = {};
311 inline constexpr Ghost ghost = {};
312
313 // Now we can declare the partition sets that are a result of combining partitions
314
315 typedef decltype(interior + border) InteriorBorder;
316 typedef decltype(interior + border + overlap) InteriorBorderOverlap;
317 typedef decltype(interior + border + overlap + front) InteriorBorderOverlapFront;
318 typedef decltype(interior + border + overlap + front + ghost) All;
319
320 inline constexpr InteriorBorder interiorBorder = {};
321 inline constexpr InteriorBorderOverlap interiorBorderOverlap = {};
322 inline constexpr InteriorBorderOverlapFront interiorBorderOverlapFront = {};
323 inline constexpr All all = {};
324
325#endif // DOXYGEN
326
327 } // namespace Partitions
328
333} // namespace Dune
334
335#endif // DUNE_GRID_COMMON_PARTITIONSET_HH
@ 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
Dune namespace.
Definition: alignedallocator.hh:13
Traits for type conversions and type information.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 13, 23:29, 2024)