Reference documentation for deal.II version GIT 5ac9d67d2d 2023-06-07 21:45:01+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 
92  BlockMask(const std::vector<bool> &block_mask);
93 
102  BlockMask(const unsigned int n_blocks, const bool initializer);
103 
112  unsigned int
113  size() const;
114 
128  bool
129  operator[](const unsigned int block_index) const;
130 
140  bool
141  represents_n_blocks(const unsigned int n) const;
142 
156  unsigned int
157  n_selected_blocks(const unsigned int overall_number_of_blocks =
159 
166  unsigned int
167  first_selected_block(const unsigned int overall_number_of_blocks =
169 
175  bool
177 
182  BlockMask
183  operator|(const BlockMask &mask) const;
184 
189  BlockMask
190  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
259 BlockMask::operator[](const unsigned int block_index) const
260 {
261  // if the mask represents the all-block mask
262  // then always return true
263  if (block_mask.size() == 0)
264  return true;
265  else
266  {
267  // otherwise check the validity of the index and
268  // return whatever is appropriate
269  AssertIndexRange(block_index, block_mask.size());
270  return block_mask[block_index];
271  }
272 }
273 
274 
275 inline bool
276 BlockMask::represents_n_blocks(const unsigned int n) const
277 {
278  return ((block_mask.size() == 0) || (block_mask.size() == n));
279 }
280 
281 
282 inline unsigned int
283 BlockMask::n_selected_blocks(const unsigned int n) const
284 {
285  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
286  AssertDimension(n, size());
287 
288  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
289  if (block_mask.size() == 0)
290  return real_n;
291  else
292  {
293  AssertDimension(real_n, block_mask.size());
294  return std::count_if(block_mask.begin(),
295  block_mask.end(),
296  [](const bool selected) { return selected; });
297  }
298 }
299 
300 
301 inline unsigned int
302 BlockMask::first_selected_block(const unsigned int n) const
303 {
304  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
305  AssertDimension(n, size());
306 
307  if (block_mask.size() == 0)
308  return 0;
309  else
310  {
311  for (unsigned int c = 0; c < block_mask.size(); ++c)
312  if (block_mask[c] == true)
313  return c;
314 
315  Assert(false, ExcMessage("No block is selected at all!"));
317  }
318 }
319 
320 
321 
322 inline bool
324 {
325  return (block_mask.size() == 0);
326 }
327 
328 
329 
330 inline BlockMask
331 BlockMask::operator|(const BlockMask &mask) const
332 {
333  // if one of the two masks denotes the all-block mask,
334  // then return the other one
335  if (block_mask.size() == 0)
336  return mask;
337  else if (mask.block_mask.size() == 0)
338  return *this;
339  else
340  {
341  // if both masks have individual entries set, form
342  // the combination of the two
343  AssertDimension(block_mask.size(), mask.block_mask.size());
344  std::vector<bool> new_mask(block_mask.size());
345  for (unsigned int i = 0; i < block_mask.size(); ++i)
346  new_mask[i] = (block_mask[i] || mask.block_mask[i]);
347 
348  return new_mask;
349  }
350 }
351 
352 
353 inline BlockMask
354 BlockMask::operator&(const BlockMask &mask) const
355 {
356  // if one of the two masks denotes the all-block mask,
357  // then return the other one
358  if (block_mask.size() == 0)
359  return mask;
360  else if (mask.block_mask.size() == 0)
361  return *this;
362  else
363  {
364  // if both masks have individual entries set, form
365  // the combination of the two
366  AssertDimension(block_mask.size(), mask.block_mask.size());
367  std::vector<bool> new_mask(block_mask.size());
368  for (unsigned int i = 0; i < block_mask.size(); ++i)
369  new_mask[i] = (block_mask[i] && mask.block_mask[i]);
370 
371  return new_mask;
372  }
373 }
374 
375 
376 inline bool
377 BlockMask::operator==(const BlockMask &mask) const
378 {
379  return block_mask == mask.block_mask;
380 }
381 
382 
383 inline bool
384 BlockMask::operator!=(const BlockMask &mask) const
385 {
386  return block_mask != mask.block_mask;
387 }
388 #endif // DOXYGEN
389 
391 
392 #endif
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:165
bool operator==(const BlockMask &mask) const
BlockMask operator&(const BlockMask &mask) const
friend std::ostream & operator<<(std::ostream &out, const BlockMask &mask)
Definition: block_mask.cc:25
BlockMask(const unsigned int n_blocks, const bool initializer)
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:215
bool represents_n_blocks(const unsigned int n) const
BlockMask(const std::vector< bool > &block_mask)
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:474
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:475
#define Assert(cond, exc)
Definition: exceptions.h:1614
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1787
#define AssertIndexRange(index, range)
Definition: exceptions.h:1855
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:213