Reference documentation for deal.II version Git 5a2787e538 2021-09-21 14:55:10 -0600
\(\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 - 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_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 
101  ComponentMask(const std::vector<bool> &component_mask);
102 
111  ComponentMask(const unsigned int n_components, const bool initializer);
112 
116  void
117  set(const unsigned int index, const bool value);
118 
127  unsigned int
128  size() const;
129 
143  bool
144  operator[](const unsigned int component_index) const;
145 
155  bool
156  represents_n_components(const unsigned int n) const;
157 
171  unsigned int
172  n_selected_components(const unsigned int overall_number_of_components =
174 
181  unsigned int
182  first_selected_component(const unsigned int overall_number_of_components =
184 
190  bool
192 
198  operator|(const ComponentMask &mask) const;
199 
205  operator&(const ComponentMask &mask) const;
206 
210  bool
211  operator==(const ComponentMask &mask) const;
212 
216  bool
217  operator!=(const ComponentMask &mask) const;
218 
223  std::size_t
224  memory_consumption() const;
225 
230  "The number of selected components in a mask "
231  "must be greater than zero.");
232 
233 private:
237  std::vector<bool> component_mask;
238 
239  // make the output operator a friend so it can access
240  // the component_mask array
241  friend std::ostream &
242  operator<<(std::ostream &out, const ComponentMask &mask);
243 };
244 
245 
256 std::ostream &
257 operator<<(std::ostream &out, const ComponentMask &mask);
258 
259 #ifndef DOXYGEN
260 // -------------------- inline functions ---------------------
261 
262 inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
263  : component_mask(component_mask)
264 {}
265 
266 
267 inline ComponentMask::ComponentMask(const unsigned int n_components,
268  const bool initializer)
269  : component_mask(n_components, initializer)
270 {}
271 
272 
273 inline unsigned int
274 ComponentMask::size() const
275 {
276  return component_mask.size();
277 }
278 
279 
280 inline void
281 ComponentMask::set(const unsigned int index, const bool value)
282 {
283  AssertIndexRange(index, component_mask.size());
284  component_mask[index] = value;
285 }
286 
287 
288 inline bool
289 ComponentMask::operator[](const unsigned int component_index) const
290 {
291  // if the mask represents the all-component mask
292  // then always return true
293  if (component_mask.size() == 0)
294  return true;
295  else
296  {
297  // otherwise check the validity of the index and
298  // return whatever is appropriate
299  AssertIndexRange(component_index, component_mask.size());
300  return component_mask[component_index];
301  }
302 }
303 
304 
305 inline bool
306 ComponentMask::represents_n_components(const unsigned int n) const
307 {
308  return ((component_mask.size() == 0) || (component_mask.size() == n));
309 }
310 
311 
312 inline unsigned int
313 ComponentMask::n_selected_components(const unsigned int n) const
314 {
315  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
316  AssertDimension(n, size());
317 
318  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
319  if (component_mask.size() == 0)
320  return real_n;
321  else
322  {
323  AssertDimension(real_n, component_mask.size());
324  return std::count_if(component_mask.begin(),
325  component_mask.end(),
326  [](const bool selected) { return selected; });
327  }
328 }
329 
330 
331 inline unsigned int
332 ComponentMask::first_selected_component(const unsigned int n) const
333 {
334  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
335  AssertDimension(n, size());
336 
337  if (component_mask.size() == 0)
338  return 0;
339  else
340  {
341  for (unsigned int c = 0; c < component_mask.size(); ++c)
342  if (component_mask[c] == true)
343  return c;
344 
345  Assert(false, ExcMessage("No component is selected at all!"));
347  }
348 }
349 
350 
351 
352 inline bool
354 {
355  return (component_mask.size() == 0);
356 }
357 
358 
359 
360 inline ComponentMask
361 ComponentMask::operator|(const ComponentMask &mask) const
362 {
363  // if one of the two masks denotes the all-component mask,
364  // then return the other one
365  if (component_mask.size() == 0)
366  return mask;
367  else if (mask.component_mask.size() == 0)
368  return *this;
369  else
370  {
371  // if both masks have individual entries set, form
372  // the combination of the two
373  AssertDimension(component_mask.size(), mask.component_mask.size());
374  std::vector<bool> new_mask(component_mask.size());
375  for (unsigned int i = 0; i < component_mask.size(); ++i)
376  new_mask[i] = (component_mask[i] || mask.component_mask[i]);
377 
378  return new_mask;
379  }
380 }
381 
382 
383 inline ComponentMask
384 ComponentMask::operator&(const ComponentMask &mask) const
385 {
386  // if one of the two masks denotes the all-component mask,
387  // then return the other one
388  if (component_mask.size() == 0)
389  return mask;
390  else if (mask.component_mask.size() == 0)
391  return *this;
392  else
393  {
394  // if both masks have individual entries set, form
395  // the combination of the two
396  AssertDimension(component_mask.size(), mask.component_mask.size());
397  std::vector<bool> new_mask(component_mask.size());
398  for (unsigned int i = 0; i < component_mask.size(); ++i)
399  new_mask[i] = (component_mask[i] && mask.component_mask[i]);
400 
401  return new_mask;
402  }
403 }
404 
405 
406 inline bool
407 ComponentMask::operator==(const ComponentMask &mask) const
408 {
409  return component_mask == mask.component_mask;
410 }
411 
412 
413 inline bool
414 ComponentMask::operator!=(const ComponentMask &mask) const
415 {
416  return component_mask != mask.component_mask;
417 }
418 #endif // DOXYGEN
419 
420 
422 
423 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:196
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1653
bool operator==(const ComponentMask &mask) const
bool operator[](const unsigned int component_index) const
bool operator!=(const ComponentMask &mask) const
std::vector< bool > component_mask
#define AssertIndexRange(index, range)
Definition: exceptions.h:1718
ComponentMask operator|(const ComponentMask &mask) const
bool represents_n_components(const unsigned int n) const
bool represents_the_all_selected_mask() const
void set(const unsigned int index, const bool value)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
Definition: exceptions.h:1461
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:487
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:401
static ::ExceptionBase & ExcNoComponentSelected()
unsigned int size() const
std::size_t memory_consumption() const
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:400
unsigned int first_selected_component(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
ComponentMask()=default
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)
ComponentMask operator &(const ComponentMask &mask) const