Reference documentation for deal.II version 8.5.1
block_indices.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2000 - 2016 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 at
12 // the top level of the deal.II distribution.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii__block_indices_h
17 #define dealii__block_indices_h
18 
19 
20 #include <deal.II/base/config.h>
21 #include <deal.II/base/subscriptor.h>
22 #include <deal.II/base/exceptions.h>
23 #include <deal.II/base/logstream.h>
24 #include <deal.II/base/utilities.h>
25 #include <cstddef>
26 #include <vector>
27 
28 DEAL_II_NAMESPACE_OPEN
29 
30 
57 class BlockIndices : public Subscriptor
58 {
59 public:
64 
68  BlockIndices ();
69 
75  BlockIndices (const std::vector<size_type> &block_sizes);
76 
77 #ifdef DEAL_II_WITH_CXX11
78 
86 
90  BlockIndices (const BlockIndices &) = default;
91 #endif
92 
96  explicit BlockIndices(const unsigned int n_blocks,
97  const size_type block_size = 0);
98 
103  void reinit (const unsigned int n_blocks,
104  const size_type n_elements_per_block);
105 
112  void reinit (const std::vector<size_type> &block_sizes);
113 
117  void push_back(const size_type size);
118 
123 
127  unsigned int size () const;
128 
133  size_type total_size () const;
134 
138  size_type block_size (const unsigned int i) const;
139 
145  std::string to_string () const;
146 
148 
158 
164  std::pair<unsigned int,size_type>
165  global_to_local (const size_type i) const;
166 
170  size_type local_to_global (const unsigned int block,
171  const size_type index) const;
172 
176  size_type block_start (const unsigned int i) const;
178 
183 
184 #ifdef DEAL_II_WITH_CXX11
185 
190 #endif
191 
196  bool operator == (const BlockIndices &b) const;
197 
201  void swap (BlockIndices &b);
202 
207  std::size_t memory_consumption () const;
208 
209 private:
214  unsigned int n_blocks;
215 
220  std::vector<size_type> start_indices;
221 };
222 
223 
232 inline
233 LogStream &
234 operator << (LogStream &s, const BlockIndices &bi)
235 {
236  const unsigned int n = bi.size();
237  s << n << ":[";
238  // Write first size without leading space
239  if (n>0)
240  s << bi.block_size(0);
241  // Write all other sizes
242  for (unsigned int i=1; i<n; ++i)
243  s << ' ' << bi.block_size(i);
244  s << "]->" << bi.total_size();
245  return s;
246 }
247 
248 
249 
250 /* ---------------------- template and inline functions ------------------- */
251 
252 inline
253 void
254 BlockIndices::reinit (const unsigned int nb,
255  const size_type block_size)
256 {
257  n_blocks = nb;
258  start_indices.resize(n_blocks+1);
259  for (size_type i=0; i<=n_blocks; ++i)
260  start_indices[i] = i * block_size;
261 }
262 
263 
264 
265 inline
266 void
267 BlockIndices::reinit (const std::vector<size_type> &block_sizes)
268 {
269  if (start_indices.size() != block_sizes.size()+1)
270  {
271  n_blocks = static_cast<unsigned int>(block_sizes.size());
272  start_indices.resize(n_blocks+1);
273  }
274  start_indices[0] = 0;
275  for (size_type i=1; i<=n_blocks; ++i)
276  start_indices[i] = start_indices[i-1] + block_sizes[i-1];
277 }
278 
279 
280 inline
282  :
283  n_blocks(0),
284  start_indices(1, 0)
285 {}
286 
287 
288 
289 inline
290 BlockIndices::BlockIndices (const unsigned int n_blocks,
291  const size_type block_size)
292  :
293  n_blocks(n_blocks),
294  start_indices(n_blocks+1)
295 {
296  for (size_type i=0; i<=n_blocks; ++i)
297  start_indices[i] = i * block_size;
298 }
299 
300 
301 
302 inline
303 BlockIndices::BlockIndices (const std::vector<size_type> &block_sizes)
304  :
305  n_blocks(static_cast<unsigned int>(block_sizes.size())),
306  start_indices(block_sizes.size()+1)
307 {
308  reinit (block_sizes);
309 }
310 
311 
312 
313 #ifdef DEAL_II_WITH_CXX11
314 
315 inline
317  :
318  n_blocks(b.n_blocks),
319  start_indices(std::move(b.start_indices))
320 {
321  b.n_blocks = 0;
322  b.start_indices = std::vector<size_type>(1, 0);
323 }
324 
325 #endif
326 
327 
328 
329 inline
330 void
332 {
333  start_indices.push_back(start_indices[n_blocks]+sz);
334  ++n_blocks;
336 }
337 
338 
339 inline
340 std::pair<unsigned int,BlockIndices::size_type>
342 {
343  Assert (i<total_size(), ExcIndexRangeType<size_type>(i, 0, total_size()));
344  Assert (n_blocks > 0, ExcLowerRangeType<size_type>(i, size_type(1)));
345 
346  unsigned int block = n_blocks-1;
347  while (i < start_indices[block])
348  --block;
349 
350  return std::pair<unsigned int,size_type>(block,
351  i-start_indices[block]);
352 }
353 
354 
355 inline
357 BlockIndices::local_to_global (const unsigned int block,
358  const size_type index) const
359 {
360  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
361  Assert (index < start_indices[block+1]-start_indices[block],
362  ExcIndexRangeType<size_type> (index, 0, start_indices[block+1]-start_indices[block]));
363 
364  return start_indices[block]+index;
365 }
366 
367 
368 inline
369 unsigned int
371 {
372  return n_blocks;
373 }
374 
375 
376 
377 inline
380 {
381  if (n_blocks == 0) return 0;
382  return start_indices[n_blocks];
383 }
384 
385 
386 
387 inline
389 BlockIndices::block_size (const unsigned int block) const
390 {
391  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
392  return start_indices[block+1]-start_indices[block];
393 }
394 
395 
396 
397 inline
398 std::string
400 {
401  std::string result = "[" + Utilities::int_to_string(n_blocks) + "->";
402  for (unsigned int i=0; i<n_blocks; ++i)
403  {
404  if (i>0)
405  result += ',';
406  result += Utilities::to_string(block_size(i));
407  }
408  result += "|" + Utilities::to_string(total_size()) + ']';
409  return result;
410 }
411 
412 
413 
414 inline
416 BlockIndices::block_start (const unsigned int block) const
417 {
418  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
419  return start_indices[block];
420 }
421 
422 
423 
424 inline
425 BlockIndices &
427 {
428  start_indices = b.start_indices;
429  n_blocks = b.n_blocks;
430  return *this;
431 }
432 
433 
434 
435 #ifdef DEAL_II_WITH_CXX11
436 inline
437 BlockIndices &
439 {
440  start_indices = std::move(b.start_indices);
441  n_blocks = b.n_blocks;
442 
443  b.start_indices = std::vector<size_type>(1, 0);
444  b.n_blocks = 0;
445 
446  return *this;
447 }
448 #endif
449 
450 
451 
452 inline
453 bool
455 {
456  if (n_blocks != b.n_blocks)
457  return false;
458 
459  for (size_type i=0; i<=n_blocks; ++i)
460  if (start_indices[i] != b.start_indices[i])
461  return false;
462 
463  return true;
464 }
465 
466 
467 
468 inline
469 void
471 {
472  std::swap(n_blocks, b.n_blocks);
473  std::swap(start_indices, b.start_indices);
474 }
475 
476 
477 
478 inline
479 std::size_t
481 {
482  return (sizeof(*this) +
483  start_indices.size() * sizeof(start_indices[0]));
484 }
485 
486 
487 
488 /* ----------------- global functions ---------------------------- */
489 
490 
499 inline
501 {
502  u.swap (v);
503 }
504 
505 
506 
507 
508 DEAL_II_NAMESPACE_CLOSE
509 
510 #endif
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1146
STL namespace.
size_type block_size(const unsigned int i) const
static ::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:92
size_type total_size() const
unsigned int n_blocks
void swap(BlockIndices &u, BlockIndices &v)
bool operator==(const BlockIndices &b) const
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:313
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
types::global_dof_index size_type
Definition: block_indices.h:63
std::string to_string() const
size_type local_to_global(const unsigned int block, const size_type index) const
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:85
std::vector< size_type > start_indices
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
BlockIndices & operator=(const BlockIndices &b)
std::size_t memory_consumption() const
size_type block_start(const unsigned int i) const
void push_back(const size_type size)
void swap(BlockIndices &b)
unsigned int size() const
std::pair< unsigned int, size_type > global_to_local(const size_type i) const