Reference documentation for deal.II version GIT 35969cdc9b 2023-12-09 01:10:02+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\}}\)
block_mask.h
Go to the documentation of this file.
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2021 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_fe_block_mask_h
17 #define dealii_fe_block_mask_h
18 
19 #include <deal.II/base/config.h>
20 
23 
24 #include <algorithm>
25 #include <iosfwd>
26 #include <vector>
27 
29 
30 
31 
72 class BlockMask
73 {
74 public:
81  BlockMask() = default;
82 
86  template <typename = void>
87  DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
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 =
167 
174  unsigned int
175  first_selected_block(const unsigned int overall_number_of_blocks =
177 
183  bool
185 
190  BlockMask
191  operator|(const BlockMask &mask) const;
192 
197  BlockMask
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 
219 private:
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 
242 std::ostream &
243 operator<<(std::ostream &out, const BlockMask &mask);
244 
245 
246 #ifndef DOXYGEN
247 // -------------------- inline functions ---------------------
248 
249 template <typename>
250 inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
252 {}
253 
254 
255 inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
256  : block_mask(block_mask)
257 {}
258 
259 
260 inline BlockMask::BlockMask(const unsigned int n_blocks, const bool initializer)
261  : block_mask(n_blocks, initializer)
262 {}
263 
264 
265 inline unsigned int
266 BlockMask::size() const
267 {
268  return block_mask.size();
269 }
270 
271 
272 inline bool
273 BlockMask::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 
289 inline bool
290 BlockMask::represents_n_blocks(const unsigned int n) const
291 {
292  return ((block_mask.empty()) || (block_mask.size() == n));
293 }
294 
295 
296 inline unsigned int
297 BlockMask::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 
315 inline unsigned int
316 BlockMask::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 
336 inline bool
338 {
339  return (block_mask.empty());
340 }
341 
342 
343 
344 inline BlockMask
345 BlockMask::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 
367 inline BlockMask
368 BlockMask::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 
390 inline bool
391 BlockMask::operator==(const BlockMask &mask) const
392 {
393  return block_mask == mask.block_mask;
394 }
395 
396 
397 inline bool
398 BlockMask::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:47
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:477
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:478
#define Assert(cond, exc)
Definition: exceptions.h:1631
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1820
#define AssertIndexRange(index, range)
Definition: exceptions.h:1888
static ::ExceptionBase & ExcMessage(std::string arg1)
std::enable_if_t< IsBlockVector< VectorType >::value, unsigned int > n_blocks(const VectorType &vector)
Definition: operators.h:50
static const unsigned int invalid_unsigned_int
Definition: types.h:221