Reference documentation for deal.II version GIT 9042b9283b 2023-12-02 14:50: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\}}\)
component_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_component_mask_h
17 #define dealii_fe_component_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 
82 {
83 public:
90  ComponentMask() = default;
91 
95  template <typename = void>
96  DEAL_II_DEPRECATED_EARLY_WITH_COMMENT(
97  "Implicit conversions from std::vector<bool> to ComponentMask are deprecated!")
98  ComponentMask(const std::vector<bool> &block_mask);
99 
109  explicit ComponentMask(const std::vector<bool> &component_mask);
110 
119  ComponentMask(const unsigned int n_components, const bool initializer);
120 
124  void
125  set(const unsigned int index, const bool value);
126 
135  unsigned int
136  size() const;
137 
151  bool
152  operator[](const unsigned int component_index) const;
153 
163  bool
164  represents_n_components(const unsigned int n) const;
165 
179  unsigned int
180  n_selected_components(const unsigned int overall_number_of_components =
182 
189  unsigned int
190  first_selected_component(const unsigned int overall_number_of_components =
192 
198  bool
200 
206  operator|(const ComponentMask &mask) const;
207 
213  operator&(const ComponentMask &mask) const;
214 
218  bool
219  operator==(const ComponentMask &mask) const;
220 
224  bool
225  operator!=(const ComponentMask &mask) const;
226 
231  std::size_t
232  memory_consumption() const;
233 
238  "The number of selected components in a mask "
239  "must be greater than zero.");
240 
241 private:
245  std::vector<bool> component_mask;
246 
247  // make the output operator a friend so it can access
248  // the component_mask array
249  friend std::ostream &
250  operator<<(std::ostream &out, const ComponentMask &mask);
251 };
252 
253 
264 std::ostream &
265 operator<<(std::ostream &out, const ComponentMask &mask);
266 
267 #ifndef DOXYGEN
268 // -------------------- inline functions ---------------------
269 
270 template <typename>
271 inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
273 {}
274 
275 
276 inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
277  : component_mask(component_mask)
278 {}
279 
280 
281 inline ComponentMask::ComponentMask(const unsigned int n_components,
282  const bool initializer)
283  : component_mask(n_components, initializer)
284 {}
285 
286 
287 inline unsigned int
288 ComponentMask::size() const
289 {
290  return component_mask.size();
291 }
292 
293 
294 inline void
295 ComponentMask::set(const unsigned int index, const bool value)
296 {
297  AssertIndexRange(index, component_mask.size());
299 }
300 
301 
302 inline bool
303 ComponentMask::operator[](const unsigned int component_index) const
304 {
305  // if the mask represents the all-component mask
306  // then always return true
307  if (component_mask.empty())
308  return true;
309  else
310  {
311  // otherwise check the validity of the index and
312  // return whatever is appropriate
313  AssertIndexRange(component_index, component_mask.size());
314  return component_mask[component_index];
315  }
316 }
317 
318 
319 inline bool
320 ComponentMask::represents_n_components(const unsigned int n) const
321 {
322  return ((component_mask.empty()) || (component_mask.size() == n));
323 }
324 
325 
326 inline unsigned int
327 ComponentMask::n_selected_components(const unsigned int n) const
328 {
329  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
330  AssertDimension(n, size());
331 
332  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
333  if (component_mask.empty())
334  return real_n;
335  else
336  {
337  AssertDimension(real_n, component_mask.size());
338  return std::count_if(component_mask.begin(),
339  component_mask.end(),
340  [](const bool selected) { return selected; });
341  }
342 }
343 
344 
345 inline unsigned int
346 ComponentMask::first_selected_component(const unsigned int n) const
347 {
348  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
349  AssertDimension(n, size());
350 
351  if (component_mask.empty())
352  return 0;
353  else
354  {
355  for (unsigned int c = 0; c < component_mask.size(); ++c)
356  if (component_mask[c] == true)
357  return c;
358 
359  Assert(false, ExcMessage("No component is selected at all!"));
361  }
362 }
363 
364 
365 
366 inline bool
368 {
369  return (component_mask.empty());
370 }
371 
372 
373 
374 inline ComponentMask
375 ComponentMask::operator|(const ComponentMask &mask) const
376 {
377  // if one of the two masks denotes the all-component mask,
378  // then return the other one
379  if (component_mask.empty())
380  return mask;
381  else if (mask.component_mask.empty())
382  return *this;
383  else
384  {
385  // if both masks have individual entries set, form
386  // the combination of the two
387  AssertDimension(component_mask.size(), mask.component_mask.size());
388  std::vector<bool> new_mask(component_mask.size());
389  for (unsigned int i = 0; i < component_mask.size(); ++i)
390  new_mask[i] = (component_mask[i] || mask.component_mask[i]);
391 
392  return ComponentMask(new_mask);
393  }
394 }
395 
396 
397 inline ComponentMask
398 ComponentMask::operator&(const ComponentMask &mask) const
399 {
400  // if one of the two masks denotes the all-component mask,
401  // then return the other one
402  if (component_mask.empty())
403  return mask;
404  else if (mask.component_mask.empty())
405  return *this;
406  else
407  {
408  // if both masks have individual entries set, form
409  // the combination of the two
410  AssertDimension(component_mask.size(), mask.component_mask.size());
411  std::vector<bool> new_mask(component_mask.size());
412  for (unsigned int i = 0; i < component_mask.size(); ++i)
413  new_mask[i] = (component_mask[i] && mask.component_mask[i]);
414 
415  return ComponentMask(new_mask);
416  }
417 }
418 
419 
420 inline bool
421 ComponentMask::operator==(const ComponentMask &mask) const
422 {
423  return component_mask == mask.component_mask;
424 }
425 
426 
427 inline bool
428 ComponentMask::operator!=(const ComponentMask &mask) const
429 {
430  return component_mask != mask.component_mask;
431 }
432 #endif // DOXYGEN
433 
434 
436 
437 #endif
bool operator[](const unsigned int component_index) const
bool represents_n_components(const unsigned int n) const
ComponentMask()=default
ComponentMask operator&(const ComponentMask &mask) const
void set(const unsigned int index, const bool value)
std::size_t memory_consumption() const
bool represents_the_all_selected_mask() const
bool operator!=(const ComponentMask &mask) const
std::vector< bool > component_mask
unsigned int size() const
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
ComponentMask operator|(const ComponentMask &mask) const
unsigned int first_selected_component(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
bool operator==(const ComponentMask &mask) 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
static ::ExceptionBase & ExcNoComponentSelected()
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1820
#define AssertIndexRange(index, range)
Definition: exceptions.h:1888
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:495
static ::ExceptionBase & ExcMessage(std::string arg1)
static const types::blas_int zero
static const unsigned int invalid_unsigned_int
Definition: types.h:221