Reference documentation for deal.II version 8.4.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 
54 class BlockIndices : public Subscriptor
55 {
56 public:
61 
65  BlockIndices ();
66 
72  BlockIndices (const std::vector<size_type> &block_sizes);
73 
77  explicit BlockIndices(const unsigned int n_blocks,
78  const size_type block_size = 0);
79 
84  void reinit (const unsigned int n_blocks,
85  const size_type n_elements_per_block);
86 
93  void reinit (const std::vector<size_type> &block_sizes);
94 
98  void push_back(const size_type size);
99 
104 
108  unsigned int size () const;
109 
114  size_type total_size () const;
115 
119  size_type block_size (const unsigned int i) const;
120 
126  std::string to_string () const;
127 
129 
139 
145  std::pair<unsigned int,size_type>
146  global_to_local (const size_type i) const;
147 
151  size_type local_to_global (const unsigned int block,
152  const size_type index) const;
153 
157  size_type block_start (const unsigned int i) const;
159 
164 
169  bool operator == (const BlockIndices &b) const;
170 
174  void swap (BlockIndices &b);
175 
180  std::size_t memory_consumption () const;
181 
182 private:
187  unsigned int n_blocks;
188 
193  std::vector<size_type> start_indices;
194 };
195 
196 
205 inline
206 LogStream &
207 operator << (LogStream &s, const BlockIndices &bi)
208 {
209  const unsigned int n = bi.size();
210  s << n << ":[";
211  // Write first size without leading space
212  if (n>0)
213  s << bi.block_size(0);
214  // Write all other sizes
215  for (unsigned int i=1; i<n; ++i)
216  s << ' ' << bi.block_size(i);
217  s << "]->" << bi.total_size();
218  return s;
219 }
220 
221 
222 
223 /* ---------------------- template and inline functions ------------------- */
224 
225 inline
226 void
227 BlockIndices::reinit (const unsigned int nb,
228  const size_type block_size)
229 {
230  n_blocks = nb;
231  start_indices.resize(n_blocks+1);
232  for (size_type i=0; i<=n_blocks; ++i)
233  start_indices[i] = i * block_size;
234 }
235 
236 
237 
238 inline
239 void
240 BlockIndices::reinit (const std::vector<size_type> &block_sizes)
241 {
242  if (start_indices.size() != block_sizes.size()+1)
243  {
244  n_blocks = static_cast<unsigned int>(block_sizes.size());
245  start_indices.resize(n_blocks+1);
246  }
247  start_indices[0] = 0;
248  for (size_type i=1; i<=n_blocks; ++i)
249  start_indices[i] = start_indices[i-1] + block_sizes[i-1];
250 }
251 
252 
253 inline
255  :
256  n_blocks(0),
257  start_indices(1, 0)
258 {}
259 
260 
261 
262 inline
263 BlockIndices::BlockIndices (const unsigned int n_blocks,
264  const size_type block_size)
265  :
266  n_blocks(n_blocks),
267  start_indices(n_blocks+1)
268 {
269  for (size_type i=0; i<=n_blocks; ++i)
270  start_indices[i] = i * block_size;
271 }
272 
273 
274 
275 inline
276 BlockIndices::BlockIndices (const std::vector<size_type> &block_sizes)
277  :
278  n_blocks(static_cast<unsigned int>(block_sizes.size())),
279  start_indices(block_sizes.size()+1)
280 {
281  reinit (block_sizes);
282 }
283 
284 
285 inline
286 void
288 {
289  start_indices.push_back(start_indices[n_blocks]+sz);
290  ++n_blocks;
292 }
293 
294 
295 inline
296 std::pair<unsigned int,BlockIndices::size_type>
298 {
299  Assert (i<total_size(), ExcIndexRangeType<size_type>(i, 0, total_size()));
300  Assert (n_blocks > 0, ExcLowerRangeType<size_type>(i, size_type(1)));
301 
302  unsigned int block = n_blocks-1;
303  while (i < start_indices[block])
304  --block;
305 
306  return std::pair<unsigned int,size_type>(block,
307  i-start_indices[block]);
308 }
309 
310 
311 inline
313 BlockIndices::local_to_global (const unsigned int block,
314  const size_type index) const
315 {
316  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
317  Assert (index < start_indices[block+1]-start_indices[block],
318  ExcIndexRangeType<size_type> (index, 0, start_indices[block+1]-start_indices[block]));
319 
320  return start_indices[block]+index;
321 }
322 
323 
324 inline
325 unsigned int
327 {
328  return n_blocks;
329 }
330 
331 
332 
333 inline
336 {
337  if (n_blocks == 0) return 0;
338  return start_indices[n_blocks];
339 }
340 
341 
342 
343 inline
345 BlockIndices::block_size (const unsigned int block) const
346 {
347  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
348  return start_indices[block+1]-start_indices[block];
349 }
350 
351 
352 
353 inline
354 std::string
356 {
357  std::string result = "[" + Utilities::int_to_string(n_blocks) + "->";
358  for (unsigned int i=0; i<n_blocks; ++i)
359  {
360  if (i>0)
361  result += ',';
362  result += Utilities::to_string(block_size(i));
363  }
364  result += "|" + Utilities::to_string(total_size()) + ']';
365  return result;
366 }
367 
368 
369 
370 inline
372 BlockIndices::block_start (const unsigned int block) const
373 {
374  Assert (block < n_blocks, ExcIndexRange(block, 0, n_blocks));
375  return start_indices[block];
376 }
377 
378 
379 
380 inline
381 BlockIndices &
383 {
385  n_blocks = b.n_blocks;
386  return *this;
387 }
388 
389 
390 
391 inline
392 bool
394 {
395  if (n_blocks != b.n_blocks)
396  return false;
397 
398  for (size_type i=0; i<=n_blocks; ++i)
399  if (start_indices[i] != b.start_indices[i])
400  return false;
401 
402  return true;
403 }
404 
405 
406 
407 inline
408 void
410 {
411  std::swap(n_blocks, b.n_blocks);
412  std::swap(start_indices, b.start_indices);
413 }
414 
415 
416 
417 inline
418 std::size_t
420 {
421  return (sizeof(*this) +
422  start_indices.size() * sizeof(start_indices[0]));
423 }
424 
425 
426 
427 /* ----------------- global functions ---------------------------- */
428 
429 
438 inline
440 {
441  u.swap (v);
442 }
443 
444 
445 
446 
447 DEAL_II_NAMESPACE_CLOSE
448 
449 #endif
bool operator==(const BlockIndices &b) const
#define AssertDimension(dim1, dim2)
Definition: exceptions.h:1052
std::string to_string() const
Auxiliary class aiding in the handling of block structures like in BlockVector or FESystem...
Definition: block_indices.h:54
std::size_t memory_consumption() const
std::string to_string(const number value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:89
unsigned int n_blocks
size_type block_start(const unsigned int i) const
void swap(BlockIndices &u, BlockIndices &v)
unsigned int global_dof_index
Definition: types.h:88
#define Assert(cond, exc)
Definition: exceptions.h:294
void reinit(const unsigned int n_blocks, const size_type n_elements_per_block)
types::global_dof_index size_type
Definition: block_indices.h:60
size_type total_size() const
std::pair< unsigned int, size_type > global_to_local(const size_type i) const
std::string int_to_string(const unsigned int value, const unsigned int digits=numbers::invalid_unsigned_int)
Definition: utilities.cc:82
size_type block_size(const unsigned int i) const
std::vector< size_type > start_indices
OutputOperator< VectorType > & operator<<(OutputOperator< VectorType > &out, unsigned int step)
Definition: operator.h:154
BlockIndices & operator=(const BlockIndices &b)
void push_back(const size_type size)
void swap(BlockIndices &b)
unsigned int size() const
size_type local_to_global(const unsigned int block, const size_type index) const