Reference documentation for deal.II version Git cf38156a17 2020-04-01 20:09:03 -0400
\(\newcommand{\vcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\vcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
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  AssertIndexRange(block_index, block_mask.size());
268  return block_mask[block_index];
269  }
270 }
271 
272 
273 inline bool
274 BlockMask::represents_n_blocks(const unsigned int n) const
275 {
276  return ((block_mask.size() == 0) || (block_mask.size() == n));
277 }
278 
279 
280 inline unsigned int
281 BlockMask::n_selected_blocks(const unsigned int n) const
282 {
283  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
284  AssertDimension(n, size());
285 
286  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
287  if (block_mask.size() == 0)
288  return real_n;
289  else
290  {
291  AssertDimension(real_n, block_mask.size());
292  return std::count_if(block_mask.begin(),
293  block_mask.end(),
294  [](const bool selected) { return selected; });
295  }
296 }
297 
298 
299 inline unsigned int
300 BlockMask::first_selected_block(const unsigned int n) const
301 {
302  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
303  AssertDimension(n, size());
304 
305  if (block_mask.size() == 0)
306  return 0;
307  else
308  {
309  for (unsigned int c = 0; c < block_mask.size(); ++c)
310  if (block_mask[c] == true)
311  return c;
312 
313  Assert(false, ExcMessage("No block is selected at all!"));
315  }
316 }
317 
318 
319 
320 inline bool
322 {
323  return (block_mask.size() == 0);
324 }
325 
326 
327 
328 inline BlockMask
329 BlockMask::operator|(const BlockMask &mask) const
330 {
331  // if one of the two masks denotes the all-block mask,
332  // then return the other one
333  if (block_mask.size() == 0)
334  return mask;
335  else if (mask.block_mask.size() == 0)
336  return *this;
337  else
338  {
339  // if both masks have individual entries set, form
340  // the combination of the two
341  AssertDimension(block_mask.size(), mask.block_mask.size());
342  std::vector<bool> new_mask(block_mask.size());
343  for (unsigned int i = 0; i < block_mask.size(); ++i)
344  new_mask[i] = (block_mask[i] || mask.block_mask[i]);
345 
346  return new_mask;
347  }
348 }
349 
350 
351 inline BlockMask BlockMask::operator&(const BlockMask &mask) const
352 {
353  // if one of the two masks denotes the all-block mask,
354  // then return the other one
355  if (block_mask.size() == 0)
356  return mask;
357  else if (mask.block_mask.size() == 0)
358  return *this;
359  else
360  {
361  // if both masks have individual entries set, form
362  // the combination of the two
363  AssertDimension(block_mask.size(), mask.block_mask.size());
364  std::vector<bool> new_mask(block_mask.size());
365  for (unsigned int i = 0; i < block_mask.size(); ++i)
366  new_mask[i] = (block_mask[i] && mask.block_mask[i]);
367 
368  return new_mask;
369  }
370 }
371 
372 
373 inline bool
375 {
376  return block_mask == mask.block_mask;
377 }
378 
379 
380 inline bool
382 {
383  return block_mask != mask.block_mask;
384 }
385 
386 
387 DEAL_II_NAMESPACE_CLOSE
388 
389 #endif
bool represents_the_all_selected_mask() const
Definition: block_mask.h:321
static const unsigned int invalid_unsigned_int
Definition: types.h:190
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1579
#define AssertIndexRange(index, range)
Definition: exceptions.h:1649
friend std::ostream & operator<<(std::ostream &out, const BlockMask &mask)
Definition: block_mask.cc:25
bool operator==(const BlockMask &mask) const
Definition: block_mask.h:374
BlockMask()=default
static ::ExceptionBase & ExcMessage(std::string arg1)
BlockMask operator|(const BlockMask &mask) const
Definition: block_mask.h:329
#define Assert(cond, exc)
Definition: exceptions.h:1419
bool represents_n_blocks(const unsigned int n) const
Definition: block_mask.h:274
BlockMask operator &(const BlockMask &mask) const
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:300
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
Definition: block_mask.h:281
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:381
std::size_t memory_consumption() const
Definition: block_mask.cc:47