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\}}\)
block_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_block_mask_h
17#define dealii_fe_block_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
73{
74public:
81 BlockMask() = default;
82
92 BlockMask(const std::vector<bool> &block_mask);
93
102 BlockMask(const unsigned int n_blocks, const bool initializer);
103
112 unsigned int
113 size() const;
114
128 bool operator[](const unsigned int block_index) const;
129
139 bool
140 represents_n_blocks(const unsigned int n) const;
141
155 unsigned int
156 n_selected_blocks(const unsigned int overall_number_of_blocks =
158
165 unsigned int
166 first_selected_block(const unsigned int overall_number_of_blocks =
168
174 bool
176
182 operator|(const BlockMask &mask) const;
183
188 BlockMask operator&(const BlockMask &mask) const;
189
193 bool
194 operator==(const BlockMask &mask) const;
195
199 bool
200 operator!=(const BlockMask &mask) const;
201
206 std::size_t
207 memory_consumption() const;
208
209private:
213 std::vector<bool> block_mask;
214
215 // make the output operator a friend so it can access
216 // the block_mask array
217 friend std::ostream &
218 operator<<(std::ostream &out, const BlockMask &mask);
219};
220
221
232std::ostream &
233operator<<(std::ostream &out, const BlockMask &mask);
234
235
236#ifndef DOXYGEN
237// -------------------- inline functions ---------------------
238
239inline BlockMask::BlockMask(const std::vector<bool> &block_mask)
240 : block_mask(block_mask)
241{}
242
243
244inline BlockMask::BlockMask(const unsigned int n_blocks, const bool initializer)
245 : block_mask(n_blocks, initializer)
246{}
247
248
249inline unsigned int
250BlockMask::size() const
251{
252 return block_mask.size();
253}
254
255
256inline bool BlockMask::operator[](const unsigned int block_index) const
257{
258 // if the mask represents the all-block mask
259 // then always return true
260 if (block_mask.size() == 0)
261 return true;
262 else
263 {
264 // otherwise check the validity of the index and
265 // return whatever is appropriate
266 AssertIndexRange(block_index, block_mask.size());
267 return block_mask[block_index];
268 }
269}
270
271
272inline bool
273BlockMask::represents_n_blocks(const unsigned int n) const
274{
275 return ((block_mask.size() == 0) || (block_mask.size() == n));
276}
277
278
279inline unsigned int
280BlockMask::n_selected_blocks(const unsigned int n) const
281{
282 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
283 AssertDimension(n, size());
284
285 const unsigned int real_n = (n != numbers::invalid_unsigned_int ? n : size());
286 if (block_mask.size() == 0)
287 return real_n;
288 else
289 {
290 AssertDimension(real_n, block_mask.size());
291 return std::count_if(block_mask.begin(),
292 block_mask.end(),
293 [](const bool selected) { return selected; });
294 }
295}
296
297
298inline unsigned int
299BlockMask::first_selected_block(const unsigned int n) const
300{
301 if ((n != numbers::invalid_unsigned_int) && (size() > 0))
302 AssertDimension(n, size());
303
304 if (block_mask.size() == 0)
305 return 0;
306 else
307 {
308 for (unsigned int c = 0; c < block_mask.size(); ++c)
309 if (block_mask[c] == true)
310 return c;
311
312 Assert(false, ExcMessage("No block is selected at all!"));
314 }
315}
316
317
318
319inline bool
321{
322 return (block_mask.size() == 0);
323}
324
325
326
327inline BlockMask
328BlockMask::operator|(const BlockMask &mask) const
329{
330 // if one of the two masks denotes the all-block mask,
331 // then return the other one
332 if (block_mask.size() == 0)
333 return mask;
334 else if (mask.block_mask.size() == 0)
335 return *this;
336 else
337 {
338 // if both masks have individual entries set, form
339 // the combination of the two
340 AssertDimension(block_mask.size(), mask.block_mask.size());
341 std::vector<bool> new_mask(block_mask.size());
342 for (unsigned int i = 0; i < block_mask.size(); ++i)
343 new_mask[i] = (block_mask[i] || mask.block_mask[i]);
344
345 return new_mask;
346 }
347}
348
349
350inline BlockMask BlockMask::operator&(const BlockMask &mask) const
351{
352 // if one of the two masks denotes the all-block mask,
353 // then return the other one
354 if (block_mask.size() == 0)
355 return mask;
356 else if (mask.block_mask.size() == 0)
357 return *this;
358 else
359 {
360 // if both masks have individual entries set, form
361 // the combination of the two
362 AssertDimension(block_mask.size(), mask.block_mask.size());
363 std::vector<bool> new_mask(block_mask.size());
364 for (unsigned int i = 0; i < block_mask.size(); ++i)
365 new_mask[i] = (block_mask[i] && mask.block_mask[i]);
366
367 return new_mask;
368 }
369}
370
371
372inline bool
373BlockMask::operator==(const BlockMask &mask) const
374{
375 return block_mask == mask.block_mask;
376}
377
378
379inline bool
380BlockMask::operator!=(const BlockMask &mask) const
381{
382 return block_mask != mask.block_mask;
383}
384#endif // DOXYGEN
385
387
388#endif
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:165
bool operator==(const BlockMask &mask) const
BlockMask operator&(const BlockMask &mask) const
BlockMask(const unsigned int n_blocks, const bool initializer)
friend std::ostream & operator<<(std::ostream &out, const BlockMask &mask)
Definition: block_mask.cc:25
BlockMask operator|(const BlockMask &mask) const
std::size_t memory_consumption() const
Definition: block_mask.cc:47
bool represents_the_all_selected_mask() const
unsigned int size() const
unsigned int n_selected_blocks(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
unsigned int first_selected_block(const unsigned int overall_number_of_blocks=numbers::invalid_unsigned_int) const
BlockMask()=default
bool operator[](const unsigned int block_index) const
bool operator!=(const BlockMask &mask) const
std::vector< bool > block_mask
Definition: block_mask.h:213
bool represents_n_blocks(const unsigned int n) const
BlockMask(const std::vector< bool > &block_mask)
#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
static ::ExceptionBase & ExcMessage(std::string arg1)
std::enable_if< IsBlockVector< VectorType >::value, unsignedint >::type n_blocks(const VectorType &vector)
Definition: operators.h:49
static const unsigned int invalid_unsigned_int
Definition: types.h:196