Reference documentation for deal.II version 9.1.1
\(\newcommand{\dealcoloneq}{\mathrel{\vcenter{:}}=}\)
component_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_component_mask_h
17 #define dealii_fe_component_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 
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 
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
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
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
406 {
407  return component_mask == mask.component_mask;
408 }
409 
410 
411 inline bool
413 {
414  return component_mask != mask.component_mask;
415 }
416 
417 
418 
419 DEAL_II_NAMESPACE_CLOSE
420 
421 #endif
static const unsigned int invalid_unsigned_int
Definition: types.h:173
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1567
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:1637
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:1407
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:496
static ::ExceptionBase & ExcNoComponentSelected()
unsigned int size() const
std::size_t memory_consumption() const
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:170
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
unsigned int n_components(const DoFHandler< dim, spacedim > &dh)
ComponentMask()=default
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)
ComponentMask operator &(const ComponentMask &mask) const