deal.II version GIT relicensing-2055-gd703fda1ad 2024-10-30 20:00:00+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
block_mask.h
Go to the documentation of this file.
1// ------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: LGPL-2.1-or-later
4// Copyright (C) 2012 - 2023 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Part of the source code is dual licensed under Apache-2.0 WITH
9// LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10// governing the source code and code contributions can be found in
11// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12//
13// ------------------------------------------------------------------------
14
15#ifndef dealii_fe_block_mask_h
16#define dealii_fe_block_mask_h
17
18#include <deal.II/base/config.h>
19
22
23#include <algorithm>
24#include <iosfwd>
25#include <vector>
26
28
29
30
72{
73public:
80 BlockMask() = default;
81
85 template <typename = void>
87 "Implicit conversions from std::vector<bool> to BlockMask are deprecated!")
88 BlockMask(const std::vector<bool> &block_mask);
89
99 explicit BlockMask(const std::vector<bool> &block_mask);
100
109 BlockMask(const unsigned int n_blocks, const bool initializer);
110
119 unsigned int
120 size() const;
121
135 bool
136 operator[](const unsigned int block_index) const;
137
147 bool
148 represents_n_blocks(const unsigned int n) const;
149
163 unsigned int
164 n_selected_blocks(const unsigned int overall_number_of_blocks =
165 numbers::invalid_unsigned_int) const;
166
173 unsigned int
174 first_selected_block(const unsigned int overall_number_of_blocks =
175 numbers::invalid_unsigned_int) const;
176
182 bool
184
190 operator|(const BlockMask &mask) const;
191
197 operator&(const BlockMask &mask) const;
198
202 bool
203 operator==(const BlockMask &mask) const;
204
208 bool
209 operator!=(const BlockMask &mask) const;
210
215 std::size_t
216 memory_consumption() const;
217
218private:
222 std::vector<bool> block_mask;
223
224 // make the output operator a friend so it can access
225 // the block_mask array
226 friend std::ostream &
227 operator<<(std::ostream &out, const BlockMask &mask);
228};
229
230
241std::ostream &
242operator<<(std::ostream &out, const BlockMask &mask);
243
244
245#ifndef DOXYGEN
246// -------------------- inline functions ---------------------
247
248template <typename>
249inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
251{}
252
253
254inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
255 : block_mask(block_mask)
256{}
257
258
259inline BlockMask::BlockMask(const unsigned int n_blocks, const bool initializer)
260 : block_mask(n_blocks, initializer)
261{}
262
263
264inline unsigned int
265BlockMask::size() const
266{
267 return block_mask.size();
268}
269
270
271inline bool
272BlockMask::operator[](const unsigned int block_index) const
273{
274 // if the mask represents the all-block mask
275 // then always return true
276 if (block_mask.empty())
277 return true;
278 else
279 {
280 // otherwise check the validity of the index and
281 // return whatever is appropriate
282 AssertIndexRange(block_index, block_mask.size());
283 return block_mask[block_index];
284 }
285}
286
287
288inline bool
289BlockMask::represents_n_blocks(const unsigned int n) const
290{
291 return ((block_mask.empty()) || (block_mask.size() == n));
292}
293
294
295inline unsigned int
296BlockMask::n_selected_blocks(const unsigned int n) const
297{
298 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
299 AssertDimension(n, size());
300
301 const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
302 if (block_mask.empty())
303 return real_n;
304 else
305 {
306 AssertDimension(real_n, block_mask.size());
307 return std::count_if(block_mask.begin(),
308 block_mask.end(),
309 [](const bool selected) { return selected; });
310 }
311}
312
313
314inline unsigned int
315BlockMask::first_selected_block(const unsigned int n) const
316{
317 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
318 AssertDimension(n, size());
319
320 if (block_mask.empty())
321 return 0;
322 else
323 {
324 for (unsigned int c = 0; c < block_mask.size(); ++c)
325 if (block_mask[c] == true)
326 return c;
327
328 Assert(false, ExcMessage("No block is selected at all!"));
330 }
331}
332
333
334
335inline bool
337{
338 return (block_mask.empty());
339}
340
341
342
343inline BlockMask
344BlockMask::operator|(const BlockMask &mask) const
345{
346 // if one of the two masks denotes the all-block mask,
347 // then return the other one
348 if (block_mask.empty())
349 return mask;
350 else if (mask.block_mask.empty())
351 return *this;
352 else
353 {
354 // if both masks have individual entries set, form
355 // the combination of the two
356 AssertDimension(block_mask.size(), mask.block_mask.size());
357 std::vector<bool> new_mask(block_mask.size());
358 for (unsigned int i = 0; i < block_mask.size(); ++i)
359 new_mask[i] = (block_mask[i] || mask.block_mask[i]);
360
361 return BlockMask(new_mask);
362 }
363}
364
365
366inline BlockMask
367BlockMask::operator&(const BlockMask &mask) const
368{
369 // if one of the two masks denotes the all-block mask,
370 // then return the other one
371 if (block_mask.empty())
372 return mask;
373 else if (mask.block_mask.empty())
374 return *this;
375 else
376 {
377 // if both masks have individual entries set, form
378 // the combination of the two
379 AssertDimension(block_mask.size(), mask.block_mask.size());
380 std::vector<bool> new_mask(block_mask.size());
381 for (unsigned int i = 0; i < block_mask.size(); ++i)
382 new_mask[i] = (block_mask[i] && mask.block_mask[i]);
383
384 return BlockMask(new_mask);
385 }
386}
387
388
389inline bool
390BlockMask::operator==(const BlockMask &mask) const
391{
392 return block_mask == mask.block_mask;
393}
394
395
396inline bool
397BlockMask::operator!=(const BlockMask &mask) const
398{
399 return block_mask != mask.block_mask;
400}
401#endif // DOXYGEN
402
404
405#endif
bool operator==(const BlockMask &mask) const
BlockMask operator&(const BlockMask &mask) const
BlockMask operator|(const BlockMask &mask) const
std::size_t memory_consumption() const
Definition block_mask.cc:46
bool represents_the_all_selected_mask() const
unsigned int size() const
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
unsigned int first_selected_block(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
BlockMask()=default
bool operator[](const unsigned int block_index) const
bool operator!=(const BlockMask &mask) const
std::vector< bool > block_mask
Definition block_mask.h:222
bool represents_n_blocks(const unsigned int n) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:498
#define DEAL_II_DEPRECATED_WITH_COMMENT(comment)
Definition config.h:206
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:499
#define Assert(cond, exc)
#define AssertDimension(dim1, dim2)
#define AssertIndexRange(index, range)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::enable_if_t< IsBlockVector< VectorType >::value, unsigned int > n_blocks(const VectorType &vector)
Definition operators.h:49
static const unsigned int invalid_unsigned_int
Definition types.h:220
STL namespace.