Loading [MathJax]/extensions/TeX/newcommand.js
 deal.II version GIT relicensing-2684-gc61376a70f 2025-02-22 15:30: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\}}
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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#include <deal.II/base/types.h>
23
24#include <algorithm>
25#include <iosfwd>
26#include <vector>
27
29
30
31
73{
74public:
81 BlockMask() = default;
82
86 template <typename = void>
88 "Implicit conversions from std::vector<bool> to BlockMask are deprecated!")
89 BlockMask(const std::vector<bool> &block_mask);
90
100 explicit BlockMask(const std::vector<bool> &block_mask);
101
110 BlockMask(const unsigned int n_blocks, const bool initializer);
111
120 unsigned int
121 size() const;
122
136 bool
137 operator[](const unsigned int block_index) const;
138
148 bool
149 represents_n_blocks(const unsigned int n) const;
150
164 unsigned int
165 n_selected_blocks(const unsigned int overall_number_of_blocks =
166 numbers::invalid_unsigned_int) const;
167
174 unsigned int
175 first_selected_block(const unsigned int overall_number_of_blocks =
176 numbers::invalid_unsigned_int) const;
177
183 bool
185
191 operator|(const BlockMask &mask) const;
192
198 operator&(const BlockMask &mask) const;
199
203 bool
204 operator==(const BlockMask &mask) const;
205
209 bool
210 operator!=(const BlockMask &mask) const;
211
216 std::size_t
217 memory_consumption() const;
218
219private:
223 std::vector<bool> block_mask;
224
225 // make the output operator a friend so it can access
226 // the block_mask array
227 friend std::ostream &
228 operator<<(std::ostream &out, const BlockMask &mask);
229};
230
231
242std::ostream &
243operator<<(std::ostream &out, const BlockMask &mask);
244
245
246#ifndef DOXYGEN
247// -------------------- inline functions ---------------------
248
249template <typename>
250inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
252{}
253
254
255inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
256 : block_mask(block_mask)
257{}
258
259
260inline BlockMask::BlockMask(const unsigned int n_blocks, const bool initializer)
261 : block_mask(n_blocks, initializer)
262{}
263
264
265inline unsigned int
266BlockMask::size() const
267{
268 return block_mask.size();
269}
270
271
272inline bool
273BlockMask::operator[](const unsigned int block_index) const
274{
275 // if the mask represents the all-block mask
276 // then always return true
277 if (block_mask.empty())
278 return true;
279 else
280 {
281 // otherwise check the validity of the index and
282 // return whatever is appropriate
283 AssertIndexRange(block_index, block_mask.size());
284 return block_mask[block_index];
285 }
286}
287
288
289inline bool
290BlockMask::represents_n_blocks(const unsigned int n) const
291{
292 return ((block_mask.empty()) || (block_mask.size() == n));
293}
294
295
296inline unsigned int
297BlockMask::n_selected_blocks(const unsigned int n) const
298{
299 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
300 AssertDimension(n, size());
301
302 const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
303 if (block_mask.empty())
304 return real_n;
305 else
306 {
307 AssertDimension(real_n, block_mask.size());
308 return std::count_if(block_mask.begin(),
309 block_mask.end(),
310 [](const bool selected) { return selected; });
311 }
312}
313
314
315inline unsigned int
316BlockMask::first_selected_block(const unsigned int n) const
317{
318 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
319 AssertDimension(n, size());
320
321 if (block_mask.empty())
322 return 0;
323 else
324 {
325 for (unsigned int c = 0; c < block_mask.size(); ++c)
326 if (block_mask[c] == true)
327 return c;
328
329 Assert(false, ExcMessage("No block is selected at all!"));
331 }
332}
333
334
335
336inline bool
338{
339 return (block_mask.empty());
340}
341
342
343
344inline BlockMask
345BlockMask::operator|(const BlockMask &mask) const
346{
347 // if one of the two masks denotes the all-block mask,
348 // then return the other one
349 if (block_mask.empty())
350 return mask;
351 else if (mask.block_mask.empty())
352 return *this;
353 else
354 {
355 // if both masks have individual entries set, form
356 // the combination of the two
357 AssertDimension(block_mask.size(), mask.block_mask.size());
358 std::vector<bool> new_mask(block_mask.size());
359 for (unsigned int i = 0; i < block_mask.size(); ++i)
360 new_mask[i] = (block_mask[i] || mask.block_mask[i]);
361
362 return BlockMask(new_mask);
363 }
364}
365
366
367inline BlockMask
368BlockMask::operator&(const BlockMask &mask) const
369{
370 // if one of the two masks denotes the all-block mask,
371 // then return the other one
372 if (block_mask.empty())
373 return mask;
374 else if (mask.block_mask.empty())
375 return *this;
376 else
377 {
378 // if both masks have individual entries set, form
379 // the combination of the two
380 AssertDimension(block_mask.size(), mask.block_mask.size());
381 std::vector<bool> new_mask(block_mask.size());
382 for (unsigned int i = 0; i < block_mask.size(); ++i)
383 new_mask[i] = (block_mask[i] && mask.block_mask[i]);
384
385 return BlockMask(new_mask);
386 }
387}
388
389
390inline bool
391BlockMask::operator==(const BlockMask &mask) const
392{
393 return block_mask == mask.block_mask;
394}
395
396
397inline bool
398BlockMask::operator!=(const BlockMask &mask) const
399{
400 return block_mask != mask.block_mask;
401}
402#endif // DOXYGEN
403
405
406#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:48
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:223
bool represents_n_blocks(const unsigned int n) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:522
#define DEAL_II_DEPRECATED_WITH_COMMENT(comment)
Definition config.h:233
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:523
#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
constexpr unsigned int invalid_unsigned_int
Definition types.h:232
STL namespace.