}
};
+ // 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;
+ }
+ };
}
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);
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);
}
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);
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);
#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>
* 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;
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
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);
}
// 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
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);
};
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);
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
{
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
{
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;
}
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)
}
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));
}
}
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);
};
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);
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)
{
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
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;
}
{
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)
sparse_matrix);
}
}
- sparse_matrix.compress();
+ sparse_matrix.compress(VectorOperation::add);
sparse_matrix.vmult (ref, in);
out -= ref;
{
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)
{
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)
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)
--- /dev/null
+//------------------ 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>();
+ }
+}
+
--- /dev/null
+
+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::
--- /dev/null
+
+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::