]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Support BlockVector in matrix free class: access individual blocks of these vectors...
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Sat, 20 Apr 2013 16:29:05 +0000 (16:29 +0000)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Sat, 20 Apr 2013 16:29:05 +0000 (16:29 +0000)
git-svn-id: https://svn.dealii.org/trunk@29350 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/include/deal.II/matrix_free/fe_evaluation.h
deal.II/include/deal.II/matrix_free/matrix_free.h
tests/matrix_free/matrix_vector_curl.cc
tests/matrix_free/matrix_vector_stokes_noflux.cc
tests/mpi/matrix_free_01.cc
tests/mpi/matrix_free_02.cc
tests/mpi/matrix_free_03.cc
tests/mpi/matrix_free_04.cc [new file with mode: 0644]
tests/mpi/matrix_free_04/ncpu_1/cmp/generic [new file with mode: 0644]
tests/mpi/matrix_free_04/ncpu_2/cmp/generic [new file with mode: 0644]

index 3a5b84c092f577d77fb3aa68728b38ffd7ad39f8..b3387178f9fd803d7239ffa1f324b1c4eda6793d 100644 (file)
@@ -1781,6 +1781,36 @@ namespace internal
     }
   };
 
+  // allows to select between block vectors and non-block vectors, which
+  // allows to use a unified interface for extracting blocks on block vectors
+  // and doing nothing on usual vectors
+  template <typename VectorType, bool>
+  struct BlockVectorSelector {};
+
+  template <typename VectorType>
+  struct BlockVectorSelector<VectorType,true>
+  {
+    typedef typename VectorType::BlockType BaseVectorType;
+
+    static BaseVectorType* get_vector_component (VectorType &vec,
+                                                 const unsigned int component)
+    {
+      AssertIndexRange (component, vec.n_blocks());
+      return &vec.block(component);
+    }
+  };
+
+  template <typename VectorType>
+  struct BlockVectorSelector<VectorType,false>
+  {
+    typedef VectorType BaseVectorType;
+
+    static BaseVectorType* get_vector_component (VectorType &vec,
+                                                 const unsigned int)
+    {
+      return &vec;
+    }
+  };
 }
 
 
@@ -2110,13 +2140,12 @@ void
 FEEvaluationBase<dim,dofs_per_cell_,n_q_points_,n_components_,Number>
 ::read_dof_values (const VectorType &src)
 {
-  AssertDimension (n_components_, n_fe_components);
-
-  // only need one component, but to silent compiler warnings, use
-  // n_components copies here (but these will not be used)
-  VectorType *src_data[n_components];
+  // select between block vectors and non-block vectors. Note that the number
+  // of components is checked in the internal data
+  typename internal::BlockVectorSelector<VectorType,
+    IsBlockVector<VectorType>::value>::BaseVectorType *src_data[n_components];
   for (unsigned int d=0; d<n_components; ++d)
-    src_data[d] = const_cast<VectorType *>(&src);
+    src_data[d] = internal::BlockVectorSelector<VectorType, IsBlockVector<VectorType>::value>::get_vector_component(const_cast<VectorType &>(src), d);
 
   internal::VectorReader<Number> reader;
   read_write_operation (reader, src_data);
@@ -2194,13 +2223,13 @@ void
 FEEvaluationBase<dim,dofs_per_cell_,n_q_points_,n_components_,Number>
 ::read_dof_values_plain (const VectorType &src)
 {
-  AssertDimension (n_components_, n_fe_components);
-
-  // only need one component, but to avoid compiler warnings, use n_components
-  // copies here (but these will not be used)
-  const VectorType *src_data[n_components];
+  // select between block vectors and non-block vectors. Note that the number
+  // of components is checked in the internal data
+  typename internal::BlockVectorSelector<const VectorType,
+    IsBlockVector<VectorType>::value>::BaseVectorType *src_data[n_components];
   for (unsigned int d=0; d<n_components; ++d)
-    src_data[d] = &src;
+    src_data[d] = internal::BlockVectorSelector<const VectorType, IsBlockVector<VectorType>::value>::get_vector_component(src, d);
+
   read_dof_values_plain (src_data);
 }
 
@@ -2258,15 +2287,15 @@ void
 FEEvaluationBase<dim,dofs_per_cell_,n_q_points_,n_components_,Number>
 ::distribute_local_to_global (VectorType &dst) const
 {
-  AssertDimension (n_components_, n_fe_components);
   Assert (dof_values_initialized==true,
           internal::ExcAccessToUninitializedField());
 
-  // only need one component, but to avoid compiler warnings, use n_components
-  // copies here (but these will not be used)
-  VectorType *dst_data [n_components];
+  // select between block vectors and non-block vectors. Note that the number
+  // of components is checked in the internal data
+  typename internal::BlockVectorSelector<VectorType,
+    IsBlockVector<VectorType>::value>::BaseVectorType *dst_data[n_components];
   for (unsigned int d=0; d<n_components; ++d)
-    dst_data[d] = &dst;
+    dst_data[d] = internal::BlockVectorSelector<VectorType, IsBlockVector<VectorType>::value>::get_vector_component(dst, d);
 
   internal::VectorDistributorLocalToGlobal<Number> distributor;
   read_write_operation (distributor, dst_data);
@@ -2336,15 +2365,15 @@ void
 FEEvaluationBase<dim,dofs_per_cell_,n_q_points_,n_components_,Number>
 ::set_dof_values (VectorType &dst) const
 {
-  AssertDimension (n_components_, n_fe_components);
   Assert (dof_values_initialized==true,
           internal::ExcAccessToUninitializedField());
 
-  // only need one component, but to avoid compiler warnings, use n_components
-  // copies here (but these will not be used)
-  VectorType *dst_data [n_components];
+  // select between block vectors and non-block vectors. Note that the number
+  // of components is checked in the internal data
+  typename internal::BlockVectorSelector<VectorType,
+    IsBlockVector<VectorType>::value>::BaseVectorType *dst_data[n_components];
   for (unsigned int d=0; d<n_components; ++d)
-    dst_data[d] = &dst;
+    dst_data[d] = internal::BlockVectorSelector<VectorType, IsBlockVector<VectorType>::value>::get_vector_component(dst, d);
 
   internal::VectorSetter<Number> setter;
   read_write_operation (setter, dst_data);
index 2ec5a1ac7fc38a255a02893995b5fabe056d4608..fa52dc196f9141bb874360de51d8c44cf78a28af 100644 (file)
 #include <deal.II/base/parallel.h>
 #include <deal.II/base/quadrature.h>
 #include <deal.II/base/vectorization.h>
+#include <deal.II/base/template_constraints.h>
 #include <deal.II/fe/fe.h>
 #include <deal.II/fe/mapping.h>
 #include <deal.II/fe/mapping_q1.h>
 #include <deal.II/lac/vector.h>
 #include <deal.II/lac/parallel_vector.h>
+#include <deal.II/lac/block_vector_base.h>
 #include <deal.II/lac/constraint_matrix.h>
 #include <deal.II/dofs/dof_handler.h>
 #include <deal.II/multigrid/mg_dof_handler.h>
@@ -616,7 +618,7 @@ public:
    * different MPI processors need to be accessed at a certain time when
    * accessing remote data and overlapping communication with computation.
    */
-  typename DoFHandler<dim>::active_cell_iterator
+  typename DoFHandler<dim>::cell_iterator
   get_cell_iterator (const unsigned int macro_cell_number,
                      const unsigned int vector_number,
                      const unsigned int fe_component = 0) const;
@@ -1191,7 +1193,7 @@ MatrixFree<dim,Number>::get_dof_handler (const unsigned int dof_index) const
 
 template <int dim, typename Number>
 inline
-typename DoFHandler<dim>::active_cell_iterator
+typename DoFHandler<dim>::cell_iterator
 MatrixFree<dim,Number>::get_cell_iterator(const unsigned int macro_cell_number,
                                           const unsigned int vector_number,
                                           const unsigned int dof_index) const
@@ -1222,7 +1224,7 @@ MatrixFree<dim,Number>::get_cell_iterator(const unsigned int macro_cell_number,
 
   std::pair<unsigned int,unsigned int> index =
     cell_level_index[macro_cell_number*vectorization_length+vector_number];
-  return typename DoFHandler<dim>::active_cell_iterator
+  return typename DoFHandler<dim>::cell_iterator
          (&dofh->get_tria(), index.first, index.second, dofh);
 }
 
@@ -1638,160 +1640,253 @@ reinit(const Mapping<dim>                         &mapping,
 // internal helper functions that define how to call MPI data exchange
 // functions: for generic vectors, do nothing at all. For distributed vectors,
 // can call update_ghost_values_start function and so on. If we have
-// collections of vectors, just do the individual functions of the components
+// collections of vectors, just do the individual functions of the
+// components. this is a bit messy for block vectors, which use some
+// additional helper functions to select the blocks
 namespace internal
 {
+  template<typename VectorStruct>
+  void update_ghost_values_start_block (const VectorStruct &vec,
+                                        const unsigned int channel,
+                                        internal::bool2type<true>);
+  template<typename VectorStruct>
+  void update_ghost_values_finish_block (const VectorStruct &vec,
+                                         internal::bool2type<true>);
+  template<typename VectorStruct>
+  void compress_start_block (const VectorStruct &vec,
+                             const unsigned int channel,
+                             internal::bool2type<true>);
+  template<typename VectorStruct>
+  void compress_finish_block (const VectorStruct &vec,
+                              internal::bool2type<true>);
+
+  template<typename VectorStruct>
+  void update_ghost_values_start_block (const VectorStruct &,
+                                        const unsigned int,
+                                        internal::bool2type<false>)
+  {}
+  template<typename VectorStruct>
+  void update_ghost_values_finish_block (const VectorStruct &,
+                                         internal::bool2type<false>)
+  {}
+  template<typename VectorStruct>
+  void compress_start_block (const VectorStruct &,
+                             const unsigned int,
+                             internal::bool2type<false>)
+  {}
+  template<typename VectorStruct>
+  void compress_finish_block (const VectorStruct &,
+                              internal::bool2type<false>)
+  {}
+
+
+
   template<typename VectorStruct>
   inline
-  void update_ghost_values_start (const VectorStruct &,
+  void update_ghost_values_start (const VectorStruct &vec,
                                   const unsigned int channel = 0)
   {
-    (void)channel;
+    update_ghost_values_start_block(vec, channel,
+                                    internal::bool2type<IsBlockVector<VectorStruct>::value>());
   }
 
 
 
   template<typename Number>
   inline
-  void update_ghost_values_start (const parallel::distributed::Vector<Number>  &src,
+  void update_ghost_values_start (const parallel::distributed::Vector<Number> &vec,
                                   const unsigned int                  channel = 0)
   {
-    src.update_ghost_values_start(channel);
+    vec.update_ghost_values_start(channel);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void update_ghost_values_start (const std::vector<VectorStruct>  &src)
+  void update_ghost_values_start (const std::vector<VectorStruct> &vec)
   {
-    for (unsigned int comp=0; comp<src.size(); comp++)
-      update_ghost_values_start(src[comp], comp);
+    for (unsigned int comp=0; comp<vec.size(); comp++)
+      update_ghost_values_start(vec[comp], comp);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void update_ghost_values_start (const std::vector<VectorStruct *>  &src)
+  void update_ghost_values_start (const std::vector<VectorStruct *> &vec)
   {
-    for (unsigned int comp=0; comp<src.size(); comp++)
-      update_ghost_values_start(*src[comp], comp);
+    for (unsigned int comp=0; comp<vec.size(); comp++)
+      update_ghost_values_start(*vec[comp], comp);
+  }
+
+
+
+  template<typename VectorStruct>
+  inline
+  void update_ghost_values_start_block (const VectorStruct &vec,
+                                        const unsigned int channel,
+                                        internal::bool2type<true>)
+  {
+    for (unsigned int i=0; i<vec.n_blocks(); ++i)
+      update_ghost_values_start(vec.block(i), channel+500*i);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void update_ghost_values_finish (const VectorStruct &)
-  {}
+  void update_ghost_values_finish (const VectorStruct &vec)
+  {
+    update_ghost_values_finish_block(vec,
+                                     internal::bool2type<IsBlockVector<VectorStruct>::value>());
+  }
 
 
 
   template <typename Number>
   inline
-  void update_ghost_values_finish (const parallel::distributed::Vector<Number>  &src)
+  void update_ghost_values_finish (const parallel::distributed::Vector<Number> &vec)
   {
-    src.update_ghost_values_finish();
+    vec.update_ghost_values_finish();
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void update_ghost_values_finish (const std::vector<VectorStruct>  &src)
+  void update_ghost_values_finish (const std::vector<VectorStruct> &vec)
   {
-    for (unsigned int comp=0; comp<src.size(); comp++)
-      update_ghost_values_finish(src[comp]);
+    for (unsigned int comp=0; comp<vec.size(); comp++)
+      update_ghost_values_finish(vec[comp]);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void update_ghost_values_finish (const std::vector<VectorStruct *>  &src)
+  void update_ghost_values_finish (const std::vector<VectorStruct *> &vec)
   {
-    for (unsigned int comp=0; comp<src.size(); comp++)
-      update_ghost_values_finish(*src[comp]);
+    for (unsigned int comp=0; comp<vec.size(); comp++)
+      update_ghost_values_finish(*vec[comp]);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void compress_start (VectorStruct &,
+  void update_ghost_values_finish_block (const VectorStruct &vec,
+                                         internal::bool2type<true>)
+  {
+    for (unsigned int i=0; i<vec.n_blocks(); ++i)
+      update_ghost_values_finish(vec.block(i));
+  }
+
+
+
+  template <typename VectorStruct>
+  inline
+  void compress_start (VectorStruct &vec,
                        const unsigned int channel = 0)
   {
-    (void)channel;
+    compress_start_block (vec, channel,
+                          internal::bool2type<IsBlockVector<VectorStruct>::value>());
   }
 
 
 
   template <typename Number>
   inline
-  void compress_start (parallel::distributed::Vector<Number> &dst,
+  void compress_start (parallel::distributed::Vector<Number> &vec,
                        const unsigned int           channel = 0)
   {
-    dst.compress_start(channel);
+    vec.compress_start(channel);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void compress_start (std::vector<VectorStruct> &dst)
+  void compress_start (std::vector<VectorStruct> &vec)
   {
-    for (unsigned int comp=0; comp<dst.size(); comp++)
-      compress_start (dst[comp], comp);
+    for (unsigned int comp=0; comp<vec.size(); comp++)
+      compress_start (vec[comp], comp);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void compress_start (std::vector<VectorStruct *> &dst)
+  void compress_start (std::vector<VectorStruct *> &vec)
   {
-    for (unsigned int comp=0; comp<dst.size(); comp++)
-      compress_start (*dst[comp], comp);
+    for (unsigned int comp=0; comp<vec.size(); comp++)
+      compress_start (*vec[comp], comp);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void compress_finish (VectorStruct &)
-  {}
+  void compress_start_block (VectorStruct      &vec,
+                             const unsigned int channel,
+                             internal::bool2type<true>)
+  {
+    for (unsigned int i=0; i<vec.n_blocks(); ++i)
+      compress_start(vec.block(i), channel + 500*i);
+  }
+
+
+
+  template <typename VectorStruct>
+  inline
+  void compress_finish (VectorStruct &vec)
+  {
+    compress_finish_block(vec,
+                          internal::bool2type<IsBlockVector<VectorStruct>::value>());
+  }
 
 
 
   template <typename Number>
   inline
-  void compress_finish (parallel::distributed::Vector<Number> &dst)
+  void compress_finish (parallel::distributed::Vector<Number> &vec)
+  {
+    vec.compress_finish();
+  }
+
+
+
+  template <typename VectorStruct>
+  inline
+  void compress_finish (std::vector<VectorStruct> &vec)
   {
-    dst.compress_finish();
+    for (unsigned int comp=0; comp<vec.size(); comp++)
+      compress_finish(vec[comp]);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void compress_finish (std::vector<VectorStruct> &dst)
+  void compress_finish (std::vector<VectorStruct *> &vec)
   {
-    for (unsigned int comp=0; comp<dst.size(); comp++)
-      compress_finish(dst[comp]);
+    for (unsigned int comp=0; comp<vec.size(); comp++)
+      compress_finish(*vec[comp]);
   }
 
 
 
   template <typename VectorStruct>
   inline
-  void compress_finish (std::vector<VectorStruct *> &dst)
+  void compress_finish_block (VectorStruct &vec,
+                              internal::bool2type<true>)
   {
-    for (unsigned int comp=0; comp<dst.size(); comp++)
-      compress_finish(*dst[comp]);
+    for (unsigned int i=0; i<vec.n_blocks(); ++i)
+      compress_finish(vec.block(i));
   }
 
 
+
 #ifdef DEAL_II_WITH_THREADS
 
   // This defines the TBB data structures that are needed to schedule the
index 6b0fcdd61f7d776b4db325a391d787b742f107dd..c7de4227ca8860bc8fe7fe3d16919bc9cf47c788 100644 (file)
@@ -80,9 +80,7 @@ class MatrixFreeTest
   void vmult (VectorType &dst,
               const VectorType &src) const
   {
-    AssertDimension (dst.size(), dim);
-    for (unsigned int d=0; d<dim; ++d)
-      dst[d] = 0;
+    dst = 0;
     data.cell_loop (&MatrixFreeTest<dim,degree,VectorType>::local_apply,
                     this, dst, src);
   };
@@ -128,9 +126,8 @@ void test ()
   BlockSparsityPattern      sparsity_pattern;
   BlockSparseMatrix<double> system_matrix;
 
-  BlockVector<double> solution;
+  BlockVector<double> solution, mf_solution;
   BlockVector<double> system_rhs;
-  std::vector<Vector<double> > vec1, vec2;
 
   dof_handler.distribute_dofs (fe);
   dof_handler_sca.distribute_dofs (fe_sca);
@@ -160,16 +157,7 @@ void test ()
   solution.collect_sizes ();
 
   system_rhs.reinit (solution);
-
-  vec1.resize (dim);
-  vec2.resize (dim);
-  vec1[0].reinit (dofs_per_block);
-  vec2[0].reinit (vec1[0]);
-  for (unsigned int i=1; i<dim; ++i)
-    {
-      vec1[i].reinit (vec1[0]);
-      vec2[i].reinit (vec1[0]);
-    }
+  mf_solution.reinit (solution);
 
                                 // assemble curl-curl operator
   {
@@ -244,8 +232,6 @@ void test ()
         system_rhs.block(i)(j) = val;
       }
   constraints.condense(system_rhs);
-  for (unsigned int i=0; i<dim; ++i)
-    vec1[i] = system_rhs.block(i);
 
                                 // setup matrix-free structure
   {
@@ -258,16 +244,13 @@ void test ()
 
   system_matrix.vmult (solution, system_rhs);
 
-  typedef std::vector<Vector<double> > VectorType;
-  MatrixFreeTest<dim,fe_degree,VectorType> mf (mf_data);
-  mf.vmult (vec2, vec1);
+  MatrixFreeTest<dim,fe_degree,BlockVector<double> > mf (mf_data);
+  mf.vmult (mf_solution, system_rhs);
 
                                 // Verification
-  double error = 0.;
-  for (unsigned int i=0; i<dim; ++i)
-    for (unsigned int j=0; j<system_rhs.block(i).size(); ++j)
-      error += std::fabs (solution.block(i)(j)-vec2[i](j));
-  double relative = solution.block(0).l1_norm();
+  mf_solution -= solution;
+  const double error = mf_solution.linfty_norm();
+  const double relative = solution.linfty_norm();
   deallog << "  Verification fe degree " << fe_degree  <<  ": "
           << error/relative << std::endl << std::endl;
 }
index 13b369fe3ae797d4bc0bde937576df352b8ed7e3..9b612114f23e63d794eaf98951394f6c3662b79d 100644 (file)
@@ -65,10 +65,10 @@ class MatrixFreeTest
     for(unsigned int cell=cell_range.first;cell<cell_range.second;++cell)
       {
         velocity.reinit (cell);
-        velocity.read_dof_values (src[0]);
+        velocity.read_dof_values (src.block(0));
         velocity.evaluate (false,true,false);
         pressure.reinit (cell);
-        pressure.read_dof_values (src[1]);
+        pressure.read_dof_values (src.block(1));
         pressure.evaluate (true,false,false);
 
         for (unsigned int q=0; q<velocity.n_q_points; ++q)
@@ -87,9 +87,9 @@ class MatrixFreeTest
           }
 
         velocity.integrate (false,true);
-        velocity.distribute_local_to_global (dst[0]);
+        velocity.distribute_local_to_global (dst.block(0));
         pressure.integrate (true,false);
-        pressure.distribute_local_to_global (dst[1]);
+        pressure.distribute_local_to_global (dst.block(1));
       }
   }
 
@@ -97,9 +97,7 @@ class MatrixFreeTest
   void vmult (VectorType &dst,
               const VectorType &src) const
   {
-    AssertDimension (dst.size(), 2);
-    for (unsigned int d=0; d<2; ++d)
-      dst[d] = 0;
+    dst = 0;
     data.cell_loop (&MatrixFreeTest<dim,degree_p,VectorType>::local_apply,
                     this, dst, src);
   };
@@ -144,7 +142,7 @@ void test ()
 
   BlockVector<double> solution;
   BlockVector<double> system_rhs;
-  std::vector<Vector<double> > vec1, vec2;
+  BlockVector<double> mf_solution;
 
   dof_handler.distribute_dofs (fe);
   dof_handler_u.distribute_dofs (fe_u);
@@ -268,14 +266,7 @@ void test ()
   solution.collect_sizes ();
 
   system_rhs.reinit (solution);
-
-  vec1.resize (2);
-  vec2.resize (2);
-  for (unsigned int d=0; d<2; ++d)
-    {
-      vec1[d].reinit (dofs_per_block[d]);
-      vec2[d].reinit (vec1[d]);
-    }
+  mf_solution.reinit (solution);
 
                                 // fill system_rhs with random numbers
   for (unsigned int j=0; j<system_rhs.block(0).size(); ++j)
@@ -283,14 +274,12 @@ void test ()
       {
         const double val = -1 + 2.*(double)rand()/double(RAND_MAX);
         system_rhs.block(0)(j) = val;
-        vec1[0](j) = val;
       }
   for (unsigned int j=0; j<system_rhs.block(1).size(); ++j)
     if (constraints_p.is_constrained(j) == false)
       {
         const double val = -1 + 2.*(double)rand()/double(RAND_MAX);
         system_rhs.block(1)(j) = val;
-        vec1[1](j) = val;
       }
 
                                 // setup matrix-free structure
@@ -311,16 +300,13 @@ void test ()
 
   system_matrix.vmult (solution, system_rhs);
 
-  typedef std::vector<Vector<double> > VectorType;
-  MatrixFreeTest<dim,fe_degree,VectorType> mf (mf_data);
-  mf.vmult (vec2, vec1);
+  MatrixFreeTest<dim,fe_degree,BlockVector<double> > mf (mf_data);
+  mf.vmult (mf_solution, system_rhs);
 
                                 // Verification
-  double error = 0.;
-  for (unsigned int i=0; i<2; ++i)
-    for (unsigned int j=0; j<solution.block(i).size(); ++j)
-      error += std::fabs (solution.block(i)(j)-vec2[i](j));
-  double relative = solution.l1_norm();
+  mf_solution -= solution;
+  const double error = mf_solution.linfty_norm();
+  const double relative = solution.linfty_norm();
   deallog << "Verification fe degree " << fe_degree  <<  ": "
           << error/relative << std::endl << std::endl;
 }
index 79c00d5063d9a1679ab5ffecfa3c591e39595ebf..38f30650f1489370a4c63be51be2d214c7d6613f 100644 (file)
@@ -52,8 +52,6 @@ helmholtz_operator (const MatrixFree<dim,Number>  &data,
     {
       fe_eval.reinit (cell);
 
-                                // compare values with the ones the FEValues
-                                // gives us. Those are seen as reference
       fe_eval.read_dof_values (src);
       fe_eval.evaluate (true, true, false);
       for (unsigned int q=0; q<n_q_points; ++q)
@@ -237,7 +235,7 @@ void test ()
                                                 sparse_matrix);
       }
   }
-  sparse_matrix.compress();
+  sparse_matrix.compress(VectorOperation::add);
 
   sparse_matrix.vmult (ref, in);
   out -= ref;
index 2578c07dece48236fb004f0a20a6510b25c34d47..af19dc5341ca995e8874bad109737221f49f518c 100644 (file)
@@ -44,8 +44,6 @@ helmholtz_operator (const MatrixFree<dim,Number>  &data,
     {
       fe_eval.reinit (cell);
 
-                                // compare values with the ones the FEValues
-                                // gives us. Those are seen as reference
       fe_eval.read_dof_values (src);
       fe_eval.evaluate (true, true, false);
       for (unsigned int q=0; q<n_q_points; ++q)
index 4e549b746c8604b2b490e970f230773bb09cf53d..ba449996ee2296564dc50363130359eb08f849ce 100644 (file)
@@ -46,8 +46,6 @@ helmholtz_operator (const MatrixFree<dim,Number>  &data,
     {
       fe_eval.reinit (cell);
 
-                                // compare values with the ones the FEValues
-                                // gives us. Those are seen as reference
       fe_eval.read_dof_values (src);
       fe_eval.evaluate (true, true, false);
       for (unsigned int q=0; q<n_q_points; ++q)
@@ -238,7 +236,7 @@ void test ()
                                                 sparse_matrix);
       }
   }
-  sparse_matrix.compress();
+  sparse_matrix.compress(VectorOperation::add);
 
   deallog << "Norm of difference (component 1/2): ";
   for (unsigned int i=0; i<2; ++i)
diff --git a/tests/mpi/matrix_free_04.cc b/tests/mpi/matrix_free_04.cc
new file mode 100644 (file)
index 0000000..ec1a701
--- /dev/null
@@ -0,0 +1,289 @@
+//------------------  matrix_free_04.cc  ------------------------
+//    $Id$
+//    Version: $Name$
+//
+//------------------  matrix_free_04.cc  ------------------------
+
+
+// this tests the correctness of matrix free matrix-vector products for
+// BlockVector using two vectors on the same DoFHandler. Otherwise the same as
+// matrix_free_01/03
+
+#include "../tests.h"
+
+#include <deal.II/matrix_free/matrix_free.h>
+#include <deal.II/matrix_free/fe_evaluation.h>
+
+#include <deal.II/base/logstream.h>
+#include <deal.II/base/utilities.h>
+#include <deal.II/base/function.h>
+#include <deal.II/lac/parallel_block_vector.h>
+#include <deal.II/distributed/tria.h>
+#include <deal.II/grid/grid_generator.h>
+#include <deal.II/grid/tria_boundary_lib.h>
+#include <deal.II/dofs/dof_tools.h>
+#include <deal.II/dofs/dof_handler.h>
+#include <deal.II/lac/constraint_matrix.h>
+#include <deal.II/lac/trilinos_sparse_matrix.h>
+#include <deal.II/lac/trilinos_sparsity_pattern.h>
+#include <deal.II/fe/fe_q.h>
+#include <deal.II/fe/fe_values.h>
+#include <deal.II/numerics/vector_tools.h>
+
+#include <iostream>
+
+
+template <int dim, int fe_degree, typename Number>
+void
+helmholtz_operator (const MatrixFree<dim,Number>  &data,
+                    parallel::distributed::BlockVector<Number> &dst,
+                    const parallel::distributed::BlockVector<Number> &src,
+                    const std::pair<unsigned int,unsigned int>  &cell_range)
+{
+  FEEvaluation<dim,fe_degree,fe_degree+1,2,Number> fe_eval (data);
+  const unsigned int n_q_points = fe_eval.n_q_points;
+
+  for(unsigned int cell=cell_range.first;cell<cell_range.second;++cell)
+    {
+      fe_eval.reinit (cell);
+
+      fe_eval.read_dof_values (src);
+      fe_eval.evaluate (true, true, false);
+      for (unsigned int q=0; q<n_q_points; ++q)
+        {
+          fe_eval.submit_value (make_vectorized_array(Number(10))*
+                                fe_eval.get_value(q), q);
+          fe_eval.submit_gradient (fe_eval.get_gradient(q),q);
+        }
+      fe_eval.integrate (true,true);
+      fe_eval.distribute_local_to_global (dst);
+    }
+}
+
+
+
+template <int dim, int fe_degree, typename Number>
+class MatrixFreeTest
+{
+ public:
+  typedef VectorizedArray<Number> vector_t;
+  static const std::size_t n_vectors = VectorizedArray<Number>::n_array_elements;
+
+  MatrixFreeTest(const MatrixFree<dim,Number> &data_in):
+    data (data_in)
+  {};
+
+  void vmult (parallel::distributed::BlockVector<Number>       &dst,
+              const parallel::distributed::BlockVector<Number> &src) const
+  {
+    dst = 0;
+    const std_cxx1x::function<void(const MatrixFree<dim,Number>  &,
+                                   parallel::distributed::BlockVector<Number> &,
+                                   const parallel::distributed::BlockVector<Number> &,
+                                   const std::pair<unsigned int,unsigned int>&)>
+      wrap = helmholtz_operator<dim,fe_degree,Number>;
+    data.cell_loop (wrap, dst, src);
+  };
+
+private:
+  const MatrixFree<dim,Number> &data;
+};
+
+
+
+
+
+template <int dim, int fe_degree>
+void test ()
+{
+  typedef double number;
+
+  parallel::distributed::Triangulation<dim> tria (MPI_COMM_WORLD);
+  GridGenerator::hyper_cube (tria);
+  tria.refine_global(1);
+  typename Triangulation<dim>::active_cell_iterator
+    cell = tria.begin_active (),
+    endc = tria.end();
+   cell = tria.begin_active ();
+  for (; cell!=endc; ++cell)
+    if (cell->is_locally_owned())
+      if (cell->center().norm()<0.2)
+        cell->set_refine_flag();
+  tria.execute_coarsening_and_refinement();
+  if (dim < 3 && fe_degree < 2)
+    tria.refine_global(2);
+  else
+    tria.refine_global(1);
+  if (tria.begin(tria.n_levels()-1)->is_locally_owned())
+    tria.begin(tria.n_levels()-1)->set_refine_flag();
+  if (tria.last()->is_locally_owned())
+    tria.last()->set_refine_flag();
+  tria.execute_coarsening_and_refinement();
+  cell = tria.begin_active ();
+  for (unsigned int i=0; i<10-3*dim; ++i)
+    {
+      cell = tria.begin_active ();
+      unsigned int counter = 0;
+      for (; cell!=endc; ++cell, ++counter)
+        if (cell->is_locally_owned())
+          if (counter % (7-i) == 0)
+            cell->set_refine_flag();
+      tria.execute_coarsening_and_refinement();
+    }
+
+  FE_Q<dim> fe (fe_degree);
+  DoFHandler<dim> dof (tria);
+  dof.distribute_dofs(fe);
+
+  IndexSet owned_set = dof.locally_owned_dofs();
+  IndexSet relevant_set;
+  DoFTools::extract_locally_relevant_dofs (dof, relevant_set);
+
+  ConstraintMatrix constraints (relevant_set);
+  DoFTools::make_hanging_node_constraints(dof, constraints);
+  VectorTools::interpolate_boundary_values (dof, 0, ZeroFunction<dim>(),
+                                            constraints);
+  constraints.close();
+
+  deallog << "Testing " << dof.get_fe().get_name() << std::endl;
+  //std::cout << "Number of cells: " << tria.n_global_active_cells() << std::endl;
+  //std::cout << "Number of degrees of freedom: " << dof.n_dofs() << std::endl;
+  //std::cout << "Number of constraints: " << constraints.n_constraints() << std::endl;
+
+  MatrixFree<dim,number> mf_data;
+  {
+    const QGauss<1> quad (fe_degree+1);
+    typename MatrixFree<dim,number>::AdditionalData data;
+    data.mpi_communicator = MPI_COMM_WORLD;
+    data.tasks_parallel_scheme =
+      MatrixFree<dim,number>::AdditionalData::none;
+    data.tasks_block_size = 7;
+    mf_data.reinit (dof, constraints, quad, data);
+  }
+
+  MatrixFreeTest<dim,fe_degree,number> mf (mf_data);
+  parallel::distributed::Vector<number> ref;
+  parallel::distributed::BlockVector<number> in(2), out(2);
+  for (unsigned int i=0; i<2; ++i)
+    {
+      mf_data.initialize_dof_vector (in.block(i));
+      mf_data.initialize_dof_vector (out.block(i));
+    }
+  in.collect_sizes();
+  out.collect_sizes();
+
+  mf_data.initialize_dof_vector (ref);
+
+  for (unsigned int i=0; i<in.block(0).local_size(); ++i)
+    {
+      const unsigned int glob_index =
+        owned_set.nth_index_in_set (i);
+      if(constraints.is_constrained(glob_index))
+        continue;
+      in.block(0).local_element(i) = (double)rand()/RAND_MAX;
+      in.block(1).local_element(i) = (double)rand()/RAND_MAX;
+    }
+
+  mf.vmult (out, in);
+
+
+                                // assemble trilinos sparse matrix with
+                                // (\nabla v, \nabla u) + (v, 10 * u) for
+                                // reference
+  TrilinosWrappers::SparseMatrix sparse_matrix;
+  {
+    TrilinosWrappers::SparsityPattern csp (owned_set, MPI_COMM_WORLD);
+    DoFTools::make_sparsity_pattern (dof, csp, constraints, true);
+    csp.compress();
+    sparse_matrix.reinit (csp);
+  }
+  {
+    QGauss<dim>  quadrature_formula(fe_degree+1);
+
+    FEValues<dim> fe_values (dof.get_fe(), quadrature_formula,
+                             update_values    |  update_gradients |
+                             update_JxW_values);
+
+    const unsigned int   dofs_per_cell = dof.get_fe().dofs_per_cell;
+    const unsigned int   n_q_points    = quadrature_formula.size();
+
+    FullMatrix<double>   cell_matrix (dofs_per_cell, dofs_per_cell);
+    std::vector<unsigned int> local_dof_indices (dofs_per_cell);
+
+    typename DoFHandler<dim>::active_cell_iterator
+      cell = dof.begin_active(),
+      endc = dof.end();
+    for (; cell!=endc; ++cell)
+      if (cell->is_locally_owned())
+      {
+        cell_matrix = 0;
+        fe_values.reinit (cell);
+
+        for (unsigned int q_point=0; q_point<n_q_points; ++q_point)
+          for (unsigned int i=0; i<dofs_per_cell; ++i)
+            {
+              for (unsigned int j=0; j<dofs_per_cell; ++j)
+                cell_matrix(i,j) += ((fe_values.shape_grad(i,q_point) *
+                                      fe_values.shape_grad(j,q_point)
+                                      +
+                                      10. *
+                                      fe_values.shape_value(i,q_point) *
+                                      fe_values.shape_value(j,q_point)) *
+                                     fe_values.JxW(q_point));
+            }
+
+        cell->get_dof_indices(local_dof_indices);
+        constraints.distribute_local_to_global (cell_matrix,
+                                                local_dof_indices,
+                                                sparse_matrix);
+      }
+  }
+  sparse_matrix.compress(VectorOperation::add);
+
+  deallog << "Norm of difference (component 1/2): ";
+  for (unsigned int i=0; i<2; ++i)
+    {
+      sparse_matrix.vmult (ref, in.block(i));
+      out.block(i) -= ref;
+      const double diff_norm = out.block(i).linfty_norm();
+      deallog << diff_norm << " ";
+    }
+  deallog << std::endl << std::endl;
+}
+
+
+int main (int argc, char ** argv)
+{
+  Utilities::System::MPI_InitFinalize mpi_initialization(argc, argv);
+
+  unsigned int myid = Utilities::MPI::this_mpi_process (MPI_COMM_WORLD);
+  deallog.push(Utilities::int_to_string(myid));
+
+  if (myid == 0)
+    {
+      std::ofstream logfile(output_file_for_mpi("matrix_free_04").c_str());
+      deallog.attach(logfile);
+      deallog << std::setprecision(4);
+      deallog.depth_console(0);
+      deallog.threshold_double(1.e-10);
+
+      deallog.push("2d");
+      test<2,1>();
+      test<2,2>();
+      deallog.pop();
+
+      deallog.push("3d");
+      test<3,1>();
+      test<3,2>();
+      deallog.pop();
+    }
+  else
+    {
+      deallog.depth_console(0);
+      test<2,1>();
+      test<2,2>();
+      test<3,1>();
+      test<3,2>();
+    }
+}
+
diff --git a/tests/mpi/matrix_free_04/ncpu_1/cmp/generic b/tests/mpi/matrix_free_04/ncpu_1/cmp/generic
new file mode 100644 (file)
index 0000000..c81a565
--- /dev/null
@@ -0,0 +1,13 @@
+
+DEAL:0:2d::Testing FE_Q<2>(1)
+DEAL:0:2d::Norm of difference (component 1/2): 0 0 
+DEAL:0:2d::
+DEAL:0:2d::Testing FE_Q<2>(2)
+DEAL:0:2d::Norm of difference (component 1/2): 0 0 
+DEAL:0:2d::
+DEAL:0:3d::Testing FE_Q<3>(1)
+DEAL:0:3d::Norm of difference (component 1/2): 0 0 
+DEAL:0:3d::
+DEAL:0:3d::Testing FE_Q<3>(2)
+DEAL:0:3d::Norm of difference (component 1/2): 0 0 
+DEAL:0:3d::
diff --git a/tests/mpi/matrix_free_04/ncpu_2/cmp/generic b/tests/mpi/matrix_free_04/ncpu_2/cmp/generic
new file mode 100644 (file)
index 0000000..c81a565
--- /dev/null
@@ -0,0 +1,13 @@
+
+DEAL:0:2d::Testing FE_Q<2>(1)
+DEAL:0:2d::Norm of difference (component 1/2): 0 0 
+DEAL:0:2d::
+DEAL:0:2d::Testing FE_Q<2>(2)
+DEAL:0:2d::Norm of difference (component 1/2): 0 0 
+DEAL:0:2d::
+DEAL:0:3d::Testing FE_Q<3>(1)
+DEAL:0:3d::Norm of difference (component 1/2): 0 0 
+DEAL:0:3d::
+DEAL:0:3d::Testing FE_Q<3>(2)
+DEAL:0:3d::Norm of difference (component 1/2): 0 0 
+DEAL:0:3d::

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.