Reference documentation for deal.II version 9.2.0
\(\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 
84 {
85 public:
92  ComponentMask() = default;
93 
103  ComponentMask(const std::vector<bool> &component_mask);
104 
113  ComponentMask(const unsigned int n_components, const bool initializer);
114 
118  void
119  set(const unsigned int index, const bool value);
120 
129  unsigned int
130  size() const;
131 
145  bool operator[](const unsigned int component_index) const;
146 
156  bool
157  represents_n_components(const unsigned int n) const;
158 
172  unsigned int
173  n_selected_components(const unsigned int overall_number_of_components =
175 
182  unsigned int
183  first_selected_component(const unsigned int overall_number_of_components =
185 
191  bool
193 
199  operator|(const ComponentMask &mask) const;
200 
205  ComponentMask 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 ComponentMask::operator[](const unsigned int component_index) const
289 {
290  // if the mask represents the all-component mask
291  // then always return true
292  if (component_mask.size() == 0)
293  return true;
294  else
295  {
296  // otherwise check the validity of the index and
297  // return whatever is appropriate
298  AssertIndexRange(component_index, component_mask.size());
299  return component_mask[component_index];
300  }
301 }
302 
303 
304 inline bool
305 ComponentMask::represents_n_components(const unsigned int n) const
306 {
307  return ((component_mask.size() == 0) || (component_mask.size() == n));
308 }
309 
310 
311 inline unsigned int
312 ComponentMask::n_selected_components(const unsigned int n) const
313 {
314  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
315  AssertDimension(n, size());
316 
317  const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
318  if (component_mask.size() == 0)
319  return real_n;
320  else
321  {
322  AssertDimension(real_n, component_mask.size());
323  return std::count_if(component_mask.begin(),
324  component_mask.end(),
325  [](const bool selected) { return selected; });
326  }
327 }
328 
329 
330 inline unsigned int
331 ComponentMask::first_selected_component(const unsigned int n) const
332 {
333  if ((n != numbers::invalid_unsigned_int) && (size() > 0))
334  AssertDimension(n, size());
335 
336  if (component_mask.size() == 0)
337  return 0;
338  else
339  {
340  for (unsigned int c = 0; c < component_mask.size(); ++c)
341  if (component_mask[c] == true)
342  return c;
343 
344  Assert(false, ExcMessage("No component is selected at all!"));
346  }
347 }
348 
349 
350 
351 inline bool
353 {
354  return (component_mask.size() == 0);
355 }
356 
357 
358 
359 inline ComponentMask
360 ComponentMask::operator|(const ComponentMask &mask) const
361 {
362  // if one of the two masks denotes the all-component mask,
363  // then return the other one
364  if (component_mask.size() == 0)
365  return mask;
366  else if (mask.component_mask.size() == 0)
367  return *this;
368  else
369  {
370  // if both masks have individual entries set, form
371  // the combination of the two
372  AssertDimension(component_mask.size(), mask.component_mask.size());
373  std::vector<bool> new_mask(component_mask.size());
374  for (unsigned int i = 0; i < component_mask.size(); ++i)
375  new_mask[i] = (component_mask[i] || mask.component_mask[i]);
376 
377  return new_mask;
378  }
379 }
380 
381 
382 inline ComponentMask ComponentMask::operator&(const ComponentMask &mask) const
383 {
384  // if one of the two masks denotes the all-component mask,
385  // then return the other one
386  if (component_mask.size() == 0)
387  return mask;
388  else if (mask.component_mask.size() == 0)
389  return *this;
390  else
391  {
392  // if both masks have individual entries set, form
393  // the combination of the two
394  AssertDimension(component_mask.size(), mask.component_mask.size());
395  std::vector<bool> new_mask(component_mask.size());
396  for (unsigned int i = 0; i < component_mask.size(); ++i)
397  new_mask[i] = (component_mask[i] && mask.component_mask[i]);
398 
399  return new_mask;
400  }
401 }
402 
403 
404 inline bool
405 ComponentMask::operator==(const ComponentMask &mask) const
406 {
407  return component_mask == mask.component_mask;
408 }
409 
410 
411 inline bool
412 ComponentMask::operator!=(const ComponentMask &mask) const
413 {
414  return component_mask != mask.component_mask;
415 }
416 #endif // DOXYGEN
417 
418 
420 
421 #endif
DeclExceptionMsg
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:496
ComponentMask::represents_n_components
bool represents_n_components(const unsigned int n) const
ComponentMask::operator&
ComponentMask operator&(const ComponentMask &mask) const
ComponentMask::n_selected_components
unsigned int n_selected_components(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
ComponentMask::represents_the_all_selected_mask
bool represents_the_all_selected_mask() const
memory_consumption.h
AssertIndexRange
#define AssertIndexRange(index, range)
Definition: exceptions.h:1649
DoFHandler::n_components
unsigned int n_components(const DoFHandler< dim, spacedim > &dh)
ComponentMask
Definition: component_mask.h:83
ComponentMask::operator|
ComponentMask operator|(const ComponentMask &mask) const
DoFTools::n_components
unsigned int n_components(const DoFHandler< dim, spacedim > &dh)
ComponentMask::operator<<
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)
Definition: component_mask.cc:25
ComponentMask::memory_consumption
std::size_t memory_consumption() const
Definition: component_mask.cc:47
ComponentMask::size
unsigned int size() const
StandardExceptions::ExcMessage
static ::ExceptionBase & ExcMessage(std::string arg1)
ComponentMask::operator!=
bool operator!=(const ComponentMask &mask) const
DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_OPEN
Definition: config.h:358
ComponentMask::set
void set(const unsigned int index, const bool value)
AssertDimension
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1579
ComponentMask::first_selected_component
unsigned int first_selected_component(const unsigned int overall_number_of_components=numbers::invalid_unsigned_int) const
exceptions.h
value
static const bool value
Definition: dof_tools_constraints.cc:433
ComponentMask::ExcNoComponentSelected
static ::ExceptionBase & ExcNoComponentSelected()
ComponentMask::operator==
bool operator==(const ComponentMask &mask) const
Assert
#define Assert(cond, exc)
Definition: exceptions.h:1419
Algorithms::OutputOperator::operator<<
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:173
ComponentMask::operator[]
bool operator[](const unsigned int component_index) const
numbers::invalid_unsigned_int
static const unsigned int invalid_unsigned_int
Definition: types.h:191
config.h
DEAL_II_NAMESPACE_CLOSE
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:359
ComponentMask::ComponentMask
ComponentMask()=default
ComponentMask::component_mask
std::vector< bool > component_mask
Definition: component_mask.h:237