From: Wolfgang Bangerth Date: Sun, 26 May 2013 15:16:12 +0000 (+0000) Subject: Add an argument to IndexSet::add_indices. Use this to make a function in BlockVector... X-Git-Tag: v8.0.0~430 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d240ed41b503ffec7b45b88b26f625156889bb95;p=dealii.git Add an argument to IndexSet::add_indices. Use this to make a function in BlockVector significantly faster. git-svn-id: https://svn.dealii.org/trunk@29607 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index a29684ee60..291868facf 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -130,6 +130,13 @@ this function.

Specific improvements

    +
  1. New: The IndexSet::add_indices function that takes another IndexSet +object now has an additional argument offset that can be used +to offset the indices of first argument. +
    +(Wolfgang Bangerth, 2013/05/25) +
  2. +
  3. New: ConstraintMatrix::distribute is now also implemented for arguments of type PETScWrappers::MPI::BlockVector.
    diff --git a/deal.II/include/deal.II/base/index_set.h b/deal.II/include/deal.II/base/index_set.h index aff873cba7..52a82c6ba1 100644 --- a/deal.II/include/deal.II/base/index_set.h +++ b/deal.II/include/deal.II/base/index_set.h @@ -128,8 +128,23 @@ public: * Add the given IndexSet @p other to the * current one, constructing the union of * *this and @p other. + * + * If the @p offset argument is nonzero, then every + * index in @p other is shifted by @p offset before being + * added to the current index set. This allows to construct, + * for example, one index set from several others that are + * supposed to represent index sets corresponding to + * different ranges (e.g., when constructing the set of + * nonzero entries of a block vector from the sets of nonzero + * elements of the individual blocks of a vector). + * + * This function will generate an exception if any of the + * (possibly shifted) indices of the @p other index set + * lie outside the range [0,size()) represented + * by the current object. */ - void add_indices(const IndexSet &other); + void add_indices(const IndexSet &other, + const unsigned int offset = 0); /** * Return whether the specified @@ -767,16 +782,17 @@ IndexSet::add_indices (const ForwardIterator &begin, inline void -IndexSet::add_indices(const IndexSet &other) +IndexSet::add_indices(const IndexSet &other, + const unsigned int offset) { - if (this == &other) + if ((this == &other) && (offset == 0)) return; for (std::vector::iterator range = other.ranges.begin(); range != other.ranges.end(); ++range) { - add_range(range->begin, range->end); + add_range(range->begin+offset, range->end+offset); } compress(); diff --git a/deal.II/include/deal.II/lac/block_vector_base.h b/deal.II/include/deal.II/lac/block_vector_base.h index a512e1aa44..9a2344c042 100644 --- a/deal.II/include/deal.II/lac/block_vector_base.h +++ b/deal.II/include/deal.II/lac/block_vector_base.h @@ -1810,16 +1810,12 @@ BlockVectorBase::locally_owned_elements () const { IndexSet is (size()); - // copy index sets from blocks into the global one + // copy index sets from blocks into the global one, shifted + // by the appropriate amount for each block for (unsigned int b=0; b +#include +#include +#include + +#include + +void testor(IndexSet & a, + IndexSet & other, + unsigned int offset, + bool verbose) +{ + IndexSet merged(a); + + merged.add_indices(other, offset); + + if (verbose) + { + deallog << "Original index set: " << std::endl; + a.print(deallog); + deallog << "other index set: " << std::endl; + other.print(deallog); + deallog << "merged index set: " << std::endl; + merged.print(deallog); + } + + for (unsigned int i=0;i=offset && other.is_element(i-offset))), + ExcInternalError()); + } +} + + + + +void test() +{ + const int size = 10; + + IndexSet empty(size); + IndexSet id(size); + + id.add_index(3); + id.add_index(4); + id.add_index(7); + + deallog << "* add empty: " << std::endl; + testor(id, empty, 2, true); + + deallog << "* add self: " << std::endl; + testor(id, id, 2, true); + + deallog << "* add id2: " << std::endl; + IndexSet id2(size); + id2.add_index(0); + id2.add_index(2); + id2.add_index(3); + testor(id, id2, 3, true); +} + + + + + + +int main() +{ + std::ofstream logfile("index_set_20_offset/output"); + deallog.attach(logfile); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + test (); +} diff --git a/tests/base/index_set_20_offset/cmp/generic b/tests/base/index_set_20_offset/cmp/generic new file mode 100644 index 0000000000..7d32353213 --- /dev/null +++ b/tests/base/index_set_20_offset/cmp/generic @@ -0,0 +1,22 @@ + +DEAL::* add empty: +DEAL::Original index set: +DEAL::{[3,4], 7} +DEAL::other index set: +DEAL::{} +DEAL::merged index set: +DEAL::{[3,4], 7} +DEAL::* add self: +DEAL::Original index set: +DEAL::{[3,4], 7} +DEAL::other index set: +DEAL::{[3,4], 7} +DEAL::merged index set: +DEAL::{[3,7], 9} +DEAL::* add id2: +DEAL::Original index set: +DEAL::{[3,4], 7} +DEAL::other index set: +DEAL::{0, [2,3]} +DEAL::merged index set: +DEAL::{[3,7]}