Reference documentation for deal.II version 9.2.0
\(\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 - 2020 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 
74 class BlockMask
75 {
76 public:
83  BlockMask() = default;
84 
94  BlockMask(const std::vector<bool> &block_mask);
95 
104  BlockMask(const unsigned int n_blocks, const bool initializer);
105 
114  unsigned int
115  size() const;
116 
130  bool operator[](const unsigned int block_index) const;
131 
141  bool
142  represents_n_blocks(const unsigned int n) const;
143 
157  unsigned int
158  n_selected_blocks(const unsigned int overall_number_of_blocks =
160 
167  unsigned int
168  first_selected_block(const unsigned int overall_number_of_blocks =
170 
176  bool
178 
183  BlockMask
184  operator|(const BlockMask &mask) const;
185 
190  BlockMask operator&(const BlockMask &mask) const;
191 
195  bool
196  operator==(const BlockMask &mask) const;
197 
201  bool
202  operator!=(const BlockMask &mask) const;
203 
208  std::size_t
209  memory_consumption() const;
210 
211 private:
215  std::vector<bool> block_mask;
216 
217  // make the output operator a friend so it can access
218  // the block_mask array
219  friend std::ostream &
220  operator<<(std::ostream &out, const BlockMask &mask);
221 };
222 
223 
234 std::ostream &
235 operator<<(std::ostream &out, const BlockMask &mask);
236 
237 
238 #ifndef DOXYGEN
239 // -------------------- inline functions ---------------------
240 
241 inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
242  : block_mask(block_mask)
243 {}
244 
245 
246 inline BlockMask::BlockMask(const unsigned int n_blocks, const bool initializer)
247  : block_mask(n_blocks, initializer)
248 {}
249 
250 
251 inline unsigned int
252 BlockMask::size() const
253 {
254  return block_mask.size();
255 }
256 
257 
258 inline bool BlockMask::operator[](const unsigned int block_index) const
259 {
260  // if the mask represents the all-block mask
261  // then always return true
262  if (block_mask.size() == 0)
263  return true;
264  else
265  {
266  // otherwise check the validity of the index and
267  // return whatever is appropriate
268  AssertIndexRange(block_index, block_mask.size());
269  return block_mask[block_index];
270  }
271 }
272 
273 
274 inline bool
275 BlockMask::represents_n_blocks(const unsigned int n) const
276 {
277  return ((block_mask.size() == 0) || (block_mask.size() == n));
278 }
279 
280 
281 inline unsigned int
282 BlockMask::n_selected_blocks(const unsigned int n) const
283 {
284  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
285  AssertDimension(n, size());
286 
287  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
288  if (block_mask.size() == 0)
289  return real_n;
290  else
291  {
292  AssertDimension(real_n, block_mask.size());
293  return std::count_if(block_mask.begin(),
294  block_mask.end(),
295  [](const bool selected) { return selected; });
296  }
297 }
298 
299 
300 inline unsigned int
301 BlockMask::first_selected_block(const unsigned int n) const
302 {
303  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
304  AssertDimension(n, size());
305 
306  if (block_mask.size() == 0)
307  return 0;
308  else
309  {
310  for (unsigned int c = 0; c < block_mask.size(); ++c)
311  if (block_mask[c] == true)
312  return c;
313 
314  Assert(false, ExcMessage("No block is selected at all!"));
316  }
317 }
318 
319 
320 
321 inline bool
323 {
324  return (block_mask.size() == 0);
325 }
326 
327 
328 
329 inline BlockMask
330 BlockMask::operator|(const BlockMask &mask) const
331 {
332  // if one of the two masks denotes the all-block mask,
333  // then return the other one
334  if (block_mask.size() == 0)
335  return mask;
336  else if (mask.block_mask.size() == 0)
337  return *this;
338  else
339  {
340  // if both masks have individual entries set, form
341  // the combination of the two
342  AssertDimension(block_mask.size(), mask.block_mask.size());
343  std::vector<bool> new_mask(block_mask.size());
344  for (unsigned int i = 0; i < block_mask.size(); ++i)
345  new_mask[i] = (block_mask[i] || mask.block_mask[i]);
346 
347  return new_mask;
348  }
349 }
350 
351 
352 inline BlockMask BlockMask::operator&(const BlockMask &mask) const
353 {
354  // if one of the two masks denotes the all-block mask,
355  // then return the other one
356  if (block_mask.size() == 0)
357  return mask;
358  else if (mask.block_mask.size() == 0)
359  return *this;
360  else
361  {
362  // if both masks have individual entries set, form
363  // the combination of the two
364  AssertDimension(block_mask.size(), mask.block_mask.size());
365  std::vector<bool> new_mask(block_mask.size());
366  for (unsigned int i = 0; i < block_mask.size(); ++i)
367  new_mask[i] = (block_mask[i] && mask.block_mask[i]);
368 
369  return new_mask;
370  }
371 }
372 
373 
374 inline bool
375 BlockMask::operator==(const BlockMask &mask) const
376 {
377  return block_mask == mask.block_mask;
378 }
379 
380 
381 inline bool
382 BlockMask::operator!=(const BlockMask &mask) const
383 {
384  return block_mask != mask.block_mask;
385 }
386 #endif // DOXYGEN
387 
389 
390 #endif
BlockMask
Definition: block_mask.h:74
memory_consumption.h
BlockMask::operator[]
bool operator[](const unsigned int block_index) const
BlockMask::block_mask
std::vector< bool > block_mask
Definition: block_mask.h:215
BlockMask::operator&
BlockMask operator&(const BlockMask &mask) const
AssertIndexRange
#define AssertIndexRange(index, range)
Definition: exceptions.h:1649
BlockMask::n_selected_blocks
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
BlockMask::operator!=
bool operator!=(const BlockMask &mask) const
MatrixFreeOperators::BlockHelper::n_blocks
std::enable_if< IsBlockVector< VectorType >::value, unsigned int >::type n_blocks(const VectorType &vector)
Definition: operators.h:49
StandardExceptions::ExcMessage
static ::ExceptionBase & ExcMessage(std::string arg1)
BlockMask::memory_consumption
std::size_t memory_consumption() const
Definition: block_mask.cc:47
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
BlockMask::BlockMask
BlockMask()=default
AssertDimension
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1579
BlockMask::represents_the_all_selected_mask
bool represents_the_all_selected_mask() const
exceptions.h
BlockMask::first_selected_block
unsigned int first_selected_block(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
BlockMask::represents_n_blocks
bool represents_n_blocks(const unsigned int n) const
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
Algorithms::OutputOperator::operator<<
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:173
numbers::invalid_unsigned_int
static const unsigned int invalid_unsigned_int
Definition: types.h:191
config.h
BlockMask::size
unsigned int size() const
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
BlockMask::operator==
bool operator==(const BlockMask &mask) const
BlockMask::operator|
BlockMask operator|(const BlockMask &mask) const
BlockMask::operator<<
friend std::ostream & operator<<(std::ostream &out, const BlockMask &mask)
Definition: block_mask.cc:25