From: Wolfgang Bangerth Date: Tue, 2 May 2000 16:38:41 +0000 (+0000) Subject: Re-organize block vectors. X-Git-Tag: v8.0.0~20620 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=98d204b4e6cf69c0fbb69a8f71b6b979b7edcd08;p=dealii.git Re-organize block vectors. git-svn-id: https://svn.dealii.org/trunk@2783 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/2000/c-3-0.html b/deal.II/doc/news/2000/c-3-0.html index b38c17af0f..bd633bd874 100644 --- a/deal.II/doc/news/2000/c-3-0.html +++ b/deal.II/doc/news/2000/c-3-0.html @@ -87,6 +87,12 @@ vector and without copying around data. Their run-time is therefore independent of the length of the vectors.

+ +
  • + New: BlockIndices: Class that + manages the conversion of global indices into a block + vector/matrix/... to the indices local to each of the blocks. +

    diff --git a/deal.II/lac/include/lac/block_indices.h b/deal.II/lac/include/lac/block_indices.h new file mode 100644 index 0000000000..dba295c088 --- /dev/null +++ b/deal.II/lac/include/lac/block_indices.h @@ -0,0 +1,207 @@ +//---------------------------- block_indices.h --------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2000 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//---------------------------- block_indices.h --------------------------- +#ifndef __deal2__block_indices_h +#define __deal2__block_indices_h + + +#include + + +/** + * Class that manages the conversion of global indices into a block + * vector or matrix to the local indices within this block. This is + * required when you address a global element in a block vector and + * want to know which element within which block this is. It is also + * useful if a matrix is composed of several blocks, where you have to + * translate global row and column indices to local ones. + * + * @author Wolfgang Bangerth, 2000 + */ +template +class BlockIndices +{ + public: + + /** + * Defaulut constructor. Set all + * indices denoting the start of + * the blocks to zero. + */ + BlockIndices (); + + /** + * Constructor. Initialize the + * number of indices within each + * block from the given + * argument. The size of the + * vector shall be equal to + * #n_blocks#. + */ + BlockIndices (const vector &n); + + /** + * Reset the number of indices + * within each block from the + * given argument. The size of + * the vector shall be equal to + * #n_blocks#. + */ + void reinit (const vector &n); + + /** + * Return the block and the + * index within that block + * for the global index #i#. The + * first element of the pair is + * the block, the second the + * index within that. + */ + pair global_to_local (const unsigned int i) const; + + /** + * Return the total number of + * indices accumulated over all + * blocks. + */ + unsigned int total_size () const; + + /** + * Copy operator. + */ + BlockIndices & operator = (const BlockIndices &b); + + /** + * Swap the contents of these two + * objects. + */ + void swap (BlockIndices &b); + + private: + /** + * Global starting index of each + * vector. The last and redundant + * value is the total number of + * entries. + */ + unsigned int start_indices[n_blocks+1]; +}; + + + +/* ---------------------- template and inline functions ------------------- */ + +template +inline +BlockIndices::BlockIndices () +{ + for (unsigned int i=0; i<=n_blocks; ++i) + start_indices[i] = 0; +}; + + + +template +inline +BlockIndices::BlockIndices (const vector &n) +{ + reinit (n); +}; + + + +template +inline +void +BlockIndices::reinit (const vector &n) +{ + Assert(n.size()==n_blocks, + ExcDimensionMismatch(n.size(), n_blocks)); + + start_indices[0] = 0; + for (unsigned int i=1; i<=n_blocks; ++i) + start_indices[i] = start_indices[i-1] + n[i]; +}; + + + +template +inline +pair +BlockIndices::find (const unsigned int i) const +{ + Assert (i(block, i-start_indices[block]); +}; + + + +template +inline +unsigned int +BlockIndices::total_size () const +{ + return start_indices[n_blocks]; +}; + + + +template +inline +BlockIndices & +BlockIndices::operator = (const BlockIndices &b) +{ + for (unsigned int i=0; i<=n_blocks; ++i) + start_indices[i] = b.start_indices[i]; + return *this; +}; + + + +template +inline +void +BlockIndices::swap (BlockIndices &b) +{ + for (unsigned int i=0; i<=n_blocks; ++i) + std::swap (start_indices[i], b.start_indices[i]); +}; + + + +/* ----------------- global functions ---------------------------- */ + + +/** + * Global function #swap# which overloads the default implementation + * of the C++ standard library which uses a temporary object. The + * function simply exchanges the data of the two objects. + * + * @author Wolfgang Bangerth, 2000 + */ +template +inline +void swap (BlockIndices &u, + BlockIndices &v) +{ + u.swap (v); +}; + + + + +#endif diff --git a/deal.II/lac/include/lac/block_vector.h b/deal.II/lac/include/lac/block_vector.h index 30d81f8bbf..d7e55fd8fe 100644 --- a/deal.II/lac/include/lac/block_vector.h +++ b/deal.II/lac/include/lac/block_vector.h @@ -15,17 +15,19 @@ #include +#include #include #include #include + /** * Several vectors of data. * * The BlockVector is a collection of normal LAC-#Vector#s. Each of * the vectors inside can have a different size. * - * The functionality of #BlockVector# includes everything, a #Vector# + * The functionality of #BlockVector# includes everything a #Vector# * can do, plus the access to a single #Vector# inside the * #BlockVector# by #block(i)#. * @@ -69,7 +71,7 @@ class BlockVector * Constructor. Set dimension to #n# and * initialize all elements with zero. */ - BlockVector (const vector& n); + BlockVector (const vector &n); /** * Destructor. Clears memory @@ -93,8 +95,8 @@ class BlockVector * On #fast==false#, the vector is filled by * zeros. */ - void reinit (const vector& N, - const bool fast=false); + void reinit (const vector &N, + const bool fast=false); /** * Change the dimension to that of the @@ -106,7 +108,7 @@ class BlockVector * #reinit (V.size(), fast)#. */ void reinit (const BlockVector &V, - const bool fast=false); + const bool fast=false); /** * Set all entries to zero. Equivalent to @@ -145,12 +147,12 @@ class BlockVector /** * Access to a single block. */ - Vector& block(unsigned int i); + Vector& block (const unsigned int i); /** * Read-only access to a single block. */ - const Vector& block(unsigned int i) const; + const Vector& block (const unsigned int i) const; /** * $U(0-N) = s$: fill all components. @@ -456,12 +458,13 @@ class BlockVector */ Vector components[n_blocks]; - /** - * Global starting index of each vector. - * The last andredundant value is the total - * number of entries. + /** + * Object managing the + * transformation between global + * indices and indices within the + * different blocks. */ - unsigned int start[n_blocks+1]; + BlockIndices block_indices; }; @@ -472,7 +475,7 @@ template inline unsigned int BlockVector::size () const { - return start[n_blocks]; + return block_indices.total_size(); } @@ -508,12 +511,9 @@ template inline Number BlockVector::operator() (const unsigned int i) const { - // Assert (ii) && (j!=0)) --j; - - return components[j](i-start[j]); + const pair local_index + = block_indices.global_to_local (i); + return components[local_index.first](local_index.second); } @@ -521,13 +521,12 @@ template inline Number& BlockVector::operator() (const unsigned int i) { - // Assert (i local_index + = block_indices.global_to_local (i); + return components[local_index.first](local_index.second); +} - int j=n_blocks-1; - while ((start[j]>i) && (j!=0)) --j; - return components[j](i-start[j]); -} template inline @@ -559,7 +558,8 @@ BlockVector::block(unsigned int i) const */ template inline -void swap (BlockVector &u, BlockVector &v) +void swap (BlockVector &u, + BlockVector &v) { u.swap (v); }; diff --git a/deal.II/lac/include/lac/block_vector.templates.h b/deal.II/lac/include/lac/block_vector.templates.h index e7c262d854..164ff2d602 100644 --- a/deal.II/lac/include/lac/block_vector.templates.h +++ b/deal.II/lac/include/lac/block_vector.templates.h @@ -26,19 +26,16 @@ static inline Number sqr (const Number x) }; + template BlockVector::BlockVector () -{ - for (unsigned int i=0;i<=n_blocks;++i) - start[n_blocks] = 0; -} +{} + template -BlockVector::BlockVector (const vector& n) +BlockVector::BlockVector (const vector &n) { - Assert(n.size()==n_blocks, ExcDimensionMismatch(n.size(), n_blocks)); - reinit (n, false); } @@ -46,12 +43,9 @@ BlockVector::BlockVector (const vector& n) template BlockVector::BlockVector (const BlockVector& v) { - for (unsigned int i=0;i::BlockVector (const BlockVector& v template -void BlockVector::reinit (const vector& n, - const bool fast) +void BlockVector::reinit (const vector &n, + const bool fast) { - Assert (n.size() == n_blocks, - ExcDimensionsDontMatch(n.size(), n_blocks)); - - unsigned int j=0; - start[0] = j; + block_indices.reinit (n); for (unsigned int i=0; i void BlockVector::reinit (const BlockVector& v, - const bool fast) + const bool fast) { + block_indices = v.block_indices; for (unsigned int i=0;i BlockVector::~BlockVector () -{ -} +{} @@ -116,11 +98,8 @@ template void BlockVector::swap (BlockVector &v) { for (unsigned int i=0; i::linfty_norm () const template -BlockVector& BlockVector::operator += (const BlockVector& v) +BlockVector& +BlockVector::operator += (const BlockVector& v) { add (v); return *this; @@ -225,11 +205,12 @@ BlockVector& BlockVector::operator += (const B template -BlockVector& BlockVector::operator -= (const BlockVector& v) +BlockVector& +BlockVector::operator -= (const BlockVector& v) { for (unsigned int i=0;i::add (const BlockVector& v) template void BlockVector::add (const Number a, - const BlockVector& v) + const BlockVector& v) { for (unsigned int i=0;i::add (const Number a, template void BlockVector::add (const Number a, - const BlockVector& v, - const Number b, - const BlockVector& w) + const BlockVector& v, + const Number b, + const BlockVector& w) { for (unsigned int i=0;i::add (const Number a, template void BlockVector::sadd (const Number x, - const BlockVector& v) + const BlockVector& v) { for (unsigned int i=0;i::sadd (const Number x, template void BlockVector::sadd (const Number x, const Number a, - const BlockVector& v) + const BlockVector& v) { for (unsigned int i=0;i::sadd (const Number x, const Number a, template void BlockVector::sadd (const Number x, const Number a, - const BlockVector& v, - const Number b, - const BlockVector& w) + const BlockVector& v, + const Number b, + const BlockVector& w) { for (unsigned int i=0;i::sadd (const Number x, const Number a, template void BlockVector::sadd (const Number x, const Number a, - const BlockVector& v, - const Number b, - const BlockVector& w, - const Number c, - const BlockVector& y) + const BlockVector& v, + const Number b, + const BlockVector& w, + const Number c, + const BlockVector& y) { for (unsigned int i=0;i::scale (const Number factor) template void BlockVector::equ (const Number a, - const BlockVector& v, - const Number b, - const BlockVector& w) + const BlockVector& v, + const Number b, + const BlockVector& w) { for (unsigned int i=0;i::equ (const Number a, template void BlockVector::equ (const Number a, - const BlockVector& v) + const BlockVector& v) { for (unsigned int i=0;i::operator = (const BlockVector& template void BlockVector::print (ostream &out, - unsigned int precision, - bool scientific, - bool across) const + unsigned int precision, + bool scientific, + bool across) const { for (unsigned int i=0;i