]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
Patch by Ben Thompson: copy petsc vector to deal.II parallel distributed vector
authorkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 31 Jan 2014 13:24:30 +0000 (13:24 +0000)
committerkronbichler <kronbichler@0785d39b-7218-0410-832d-ea1e28bc413d>
Fri, 31 Jan 2014 13:24:30 +0000 (13:24 +0000)
git-svn-id: https://svn.dealii.org/trunk@32355 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/doc/news/changes.h
deal.II/include/deal.II/lac/parallel_vector.h
deal.II/include/deal.II/lac/parallel_vector.templates.h

index 604fce026978c385732e18f62789677e3b34fe71..7e14d6133b7a40fc2cdf2104e1ba2f3b2ba4bd0c 100644 (file)
@@ -102,6 +102,11 @@ inconvenience this causes.
 <h3>Specific improvements</h3>
 
 <ol>
+  <li>New: There is now a method to copy the content from a
+  PETScWrappers::MPI::Vector to deal.II's parallel distributed vector.
+  <br>
+  (Ben Thompson, 2014/01/31)
+
   <li>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
index 7ccb1f97434a8b7bd5ec7924c88dcd23c0d17a59..9029db91c5fc1c636a4bbee47a2cbad9c0efa04c 100644 (file)
 
 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<Number> &
       operator = (const Vector<Number2> &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<Number> &
+      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.
index 624168b2e757e497fe45a7e9c43e31b1bca8af54..fbce1fb85dfd37fb07b8d7177fc138be4c6839de 100644 (file)
@@ -21,6 +21,8 @@
 #include <deal.II/base/config.h>
 #include <deal.II/lac/parallel_vector.h>
 
+#include <deal.II/lac/petsc_parallel_vector.h>
+
 DEAL_II_NAMESPACE_OPEN
 
 
@@ -204,6 +206,43 @@ namespace parallel
 
 
 
+#ifdef DEAL_II_WITH_PETSC
+
+    template <typename Number>
+    inline
+    Vector<Number> &
+    Vector<Number>::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<const Vec &>(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<const Vec &>(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 <typename Number>
     void
     Vector<Number>::copy_from (const Vector<Number> &c,

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.