Reference documentation for deal.II version 9.3.3
\(\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{
83public:
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 operator[](const unsigned int component_index) const;
144
154 bool
155 represents_n_components(const unsigned int n) const;
156
170 unsigned int
171 n_selected_components(const unsigned int overall_number_of_components =
173
180 unsigned int
181 first_selected_component(const unsigned int overall_number_of_components =
183
189 bool
191
197 operator|(const ComponentMask &mask) const;
198
204
208 bool
209 operator==(const ComponentMask &mask) const;
210
214 bool
215 operator!=(const ComponentMask &mask) const;
216
221 std::size_t
222 memory_consumption() const;
223
228 "The number of selected components in a mask "
229 "must be greater than zero.");
230
231private:
235 std::vector<bool> component_mask;
236
237 // make the output operator a friend so it can access
238 // the component_mask array
239 friend std::ostream &
240 operator<<(std::ostream &out, const ComponentMask &mask);
241};
242
243
254std::ostream &
255operator<<(std::ostream &out, const ComponentMask &mask);
256
257#ifndef DOXYGEN
258// -------------------- inline functions ---------------------
259
260inline ComponentMask::ComponentMask(const std::vector<bool> &component_mask)
261 : component_mask(component_mask)
262{}
263
264
265inline ComponentMask::ComponentMask(const unsigned int n_components,
266 const bool initializer)
267 : component_mask(n_components, initializer)
268{}
269
270
271inline unsigned int
273{
274 return component_mask.size();
275}
276
277
278inline void
279ComponentMask::set(const unsigned int index, const bool value)
280{
281 AssertIndexRange(index, component_mask.size());
282 component_mask[index] = value;
283}
284
285
286inline bool ComponentMask::operator[](const unsigned int component_index) const
287{
288 // if the mask represents the all-component mask
289 // then always return true
290 if (component_mask.size() == 0)
291 return true;
292 else
293 {
294 // otherwise check the validity of the index and
295 // return whatever is appropriate
296 AssertIndexRange(component_index, component_mask.size());
297 return component_mask[component_index];
298 }
299}
300
301
302inline bool
303ComponentMask::represents_n_components(const unsigned int n) const
304{
305 return ((component_mask.size() == 0) || (component_mask.size() == n));
306}
307
308
309inline unsigned int
310ComponentMask::n_selected_components(const unsigned int n) const
311{
312 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
313 AssertDimension(n, size());
314
315 const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
316 if (component_mask.size() == 0)
317 return real_n;
318 else
319 {
320 AssertDimension(real_n, component_mask.size());
321 return std::count_if(component_mask.begin(),
322 component_mask.end(),
323 [](const bool selected) { return selected; });
324 }
325}
326
327
328inline unsigned int
329ComponentMask::first_selected_component(const unsigned int n) const
330{
331 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
332 AssertDimension(n, size());
333
334 if (component_mask.size() == 0)
335 return 0;
336 else
337 {
338 for (unsigned int c = 0; c < component_mask.size(); ++c)
339 if (component_mask[c] == true)
340 return c;
341
342 Assert(false, ExcMessage("No component is selected at all!"));
344 }
345}
346
347
348
349inline bool
351{
352 return (component_mask.size() == 0);
353}
354
355
356
357inline ComponentMask
359{
360 // if one of the two masks denotes the all-component mask,
361 // then return the other one
362 if (component_mask.size() == 0)
363 return mask;
364 else if (mask.component_mask.size() == 0)
365 return *this;
366 else
367 {
368 // if both masks have individual entries set, form
369 // the combination of the two
370 AssertDimension(component_mask.size(), mask.component_mask.size());
371 std::vector<bool> new_mask(component_mask.size());
372 for (unsigned int i = 0; i < component_mask.size(); ++i)
373 new_mask[i] = (component_mask[i] || mask.component_mask[i]);
374
375 return new_mask;
376 }
377}
378
379
381{
382 // if one of the two masks denotes the all-component mask,
383 // then return the other one
384 if (component_mask.size() == 0)
385 return mask;
386 else if (mask.component_mask.size() == 0)
387 return *this;
388 else
389 {
390 // if both masks have individual entries set, form
391 // the combination of the two
392 AssertDimension(component_mask.size(), mask.component_mask.size());
393 std::vector<bool> new_mask(component_mask.size());
394 for (unsigned int i = 0; i < component_mask.size(); ++i)
395 new_mask[i] = (component_mask[i] && mask.component_mask[i]);
396
397 return new_mask;
398 }
399}
400
401
402inline bool
404{
405 return component_mask == mask.component_mask;
406}
407
408
409inline bool
411{
412 return component_mask != mask.component_mask;
413}
414#endif // DOXYGEN
415
416
418
419#endif
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:165
bool operator[](const unsigned int component_index) const
ComponentMask(const unsigned int n_components, const bool initializer)
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
friend std::ostream & operator<<(std::ostream &out, const ComponentMask &mask)
ComponentMask(const std::vector< bool > &component_mask)
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:402
#define DEAL_II_NAMESPACE_CLOSE
Definition: config.h:403
#define Assert(cond, exc)
Definition: exceptions.h:1465
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1622
#define AssertIndexRange(index, range)
Definition: exceptions.h:1690
#define DeclExceptionMsg(Exception, defaulttext)
Definition: exceptions.h:493
static ::ExceptionBase & ExcNoComponentSelected()
static ::ExceptionBase & ExcMessage(std::string arg1)
static const unsigned int invalid_unsigned_int
Definition: types.h:196