Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
block_mask.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2009 - 2019 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 
21 #include <deal.II/base/exceptions.h>
22 #include <deal.II/base/memory_consumption.h>
23 
24 #include <algorithm>
25 #include <iosfwd>
26 #include <vector>
27 
28 DEAL_II_NAMESPACE_OPEN
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 // -------------------- inline functions ---------------------
239 
240 inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
241  : block_mask(block_mask)
242 {}
243 
244 
245 inline BlockMask::BlockMask(const unsigned int n_blocks, const bool initializer)
246  : block_mask(n_blocks, initializer)
247 {}
248 
249 
250 inline unsigned int
252 {
253  return block_mask.size();
254 }
255 
256 
257 inline bool BlockMask::operator[](const unsigned int block_index) const
258 {
259  // if the mask represents the all-block mask
260  // then always return true
261  if (block_mask.size() == 0)
262  return true;
263  else
264  {
265  // otherwise check the validity of the index and
266  // return whatever is appropriate
267  Assert(block_index < block_mask.size(),
268  ExcIndexRange(block_index, 0, 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
376 {
377  return block_mask == mask.block_mask;
378 }
379 
380 
381 inline bool
383 {
384  return block_mask != mask.block_mask;
385 }
386 
387 
388 DEAL_II_NAMESPACE_CLOSE
389 
390 #endif
bool represents_the_all_selected_mask() const
Definition: block_mask.h:322
static const unsigned int invalid_unsigned_int
Definition: types.h:173
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
friend std::ostream & operator<<(std::ostream &out, const BlockMask &mask)
Definition: block_mask.cc:25
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
bool operator==(const BlockMask &mask) const
Definition: block_mask.h:375
BlockMask()=default
static ::ExceptionBase & ExcMessage(std::string arg1)
BlockMask operator|(const BlockMask &mask) const
Definition: block_mask.h:330
#define Assert(cond, exc)
Definition: exceptions.h:1407
bool represents_n_blocks(const unsigned int n) const
Definition: block_mask.h:275
BlockMask operator &(const BlockMask &mask) const
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:170
std::vector< bool > block_mask
Definition: block_mask.h:215
unsigned int first_selected_block(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:301
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:282
bool operator[](const unsigned int block_index) const
Definition: block_mask.h:257
unsigned int size() const
Definition: block_mask.h:251
bool operator!=(const BlockMask &mask) const
Definition: block_mask.h:382
std::size_t memory_consumption() const
Definition: block_mask.cc:47