From: kronbichler Date: Fri, 31 Jan 2014 13:24:30 +0000 (+0000) Subject: Patch by Ben Thompson: copy petsc vector to deal.II parallel distributed vector X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=42051be2f58ba46348caa975b46269bcc885a55e;p=dealii-svn.git Patch by Ben Thompson: copy petsc vector to deal.II parallel distributed vector git-svn-id: https://svn.dealii.org/trunk@32355 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/doc/news/changes.h b/deal.II/doc/news/changes.h index 604fce0269..7e14d6133b 100644 --- a/deal.II/doc/news/changes.h +++ b/deal.II/doc/news/changes.h @@ -102,6 +102,11 @@ inconvenience this causes.

Specific improvements

    +
  1. New: There is now a method to copy the content from a + PETScWrappers::MPI::Vector to deal.II's parallel distributed vector. +
    + (Ben Thompson, 2014/01/31) +
  2. Fixed: The SolutionTransfer class had all sorts of problems when used with hp::DoFHandler that made its results at least questionable. Several parts of this class have been rewritten to make the results diff --git a/deal.II/include/deal.II/lac/parallel_vector.h b/deal.II/include/deal.II/lac/parallel_vector.h index 7ccb1f9743..9029db91c5 100644 --- a/deal.II/include/deal.II/lac/parallel_vector.h +++ b/deal.II/include/deal.II/lac/parallel_vector.h @@ -33,6 +33,17 @@ DEAL_II_NAMESPACE_OPEN +#ifdef DEAL_II_WITH_PETSC +namespace PETScWrappers +{ + namespace MPI + { + class Vector; + } +} +#endif + + namespace parallel { namespace distributed @@ -275,6 +286,18 @@ namespace parallel Vector & operator = (const Vector &in_vector); +#ifdef DEAL_II_WITH_PETSC + /** + * Copy the content of a PETSc vector into a vector. This function + * assumes that the vectors layouts have already been initialized to + * match. + * + * This operator is only available if deal.II was configured with PETSc. + */ + Vector & + operator = (const PETScWrappers::MPI::Vector &petsc_vec); +#endif + /** * This method copies the local range from another vector with the same * local range, but possibly different layout of ghost indices. diff --git a/deal.II/include/deal.II/lac/parallel_vector.templates.h b/deal.II/include/deal.II/lac/parallel_vector.templates.h index 624168b2e7..fbce1fb85d 100644 --- a/deal.II/include/deal.II/lac/parallel_vector.templates.h +++ b/deal.II/include/deal.II/lac/parallel_vector.templates.h @@ -21,6 +21,8 @@ #include #include +#include + DEAL_II_NAMESPACE_OPEN @@ -204,6 +206,43 @@ namespace parallel +#ifdef DEAL_II_WITH_PETSC + + template + inline + Vector & + Vector::operator = (const PETScWrappers::MPI::Vector &petsc_vec) + { + Assert(petsc_vec.locally_owned_elements() == locally_owned_elements(), + StandardExceptions::ExcInvalidState()); + + // get a representation of the vector and copy it + PetscScalar *start_ptr; + int ierr = VecGetArray (static_cast(petsc_vec), &start_ptr); + AssertThrow (ierr == 0, ExcPETScError(ierr)); + + const size_type vec_size = local_size(); + std::copy (start_ptr, start_ptr + vec_size, begin()); + + // restore the representation of the vector + ierr = VecRestoreArray (static_cast(petsc_vec), &start_ptr); + AssertThrow (ierr == 0, ExcPETScError(ierr)); + + // spread ghost values between processes? + bool must_update_ghost_values = vector_is_ghosted || + petsc_vec.has_ghost_elements(); + if (must_update_ghost_values) + update_ghost_values(); + + // return a pointer to this object per normal c++ operator overloading + // semantics + return *this; + } + +#endif + + + template void Vector::copy_from (const Vector &c,