From: Martin Kronbichler Date: Sat, 18 Feb 2017 14:07:38 +0000 (+0100) Subject: Test accessing block vectors with a component offset. X-Git-Tag: v8.5.0-rc1~107^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=85ee7008fce29126231672ed6506f33af0008b43;p=dealii.git Test accessing block vectors with a component offset. --- diff --git a/tests/matrix_free/matrix_vector_20.cc b/tests/matrix_free/matrix_vector_20.cc new file mode 100644 index 0000000000..d08a59f642 --- /dev/null +++ b/tests/matrix_free/matrix_vector_20.cc @@ -0,0 +1,279 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2013 - 2015 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + + +// this tests the correctness of matrix free matrix-vector products for two +// vectors on the same DoFHandler. Similar to matrix_vector_12.cc but using +// BlockVector instead of std::vector. + +#include "../tests.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +template +void +helmholtz_operator (const MatrixFree &data, + parallel::distributed::BlockVector &dst, + const parallel::distributed::BlockVector &src, + const std::pair &cell_range) +{ + FEEvaluation phi0 (data); + FEEvaluation phi1 (data); + const unsigned int n_q_points = phi0.n_q_points; + + for (unsigned int cell=cell_range.first; cell +class MatrixFreeTest +{ +public: + typedef VectorizedArray vector_t; + static const std::size_t n_vectors = VectorizedArray::n_array_elements; + + MatrixFreeTest(const MatrixFree &data_in): + data (data_in) + {}; + + void vmult (parallel::distributed::BlockVector &dst, + const parallel::distributed::BlockVector &src) const + { + for (unsigned int i=0; i &, + parallel::distributed::BlockVector &, + const parallel::distributed::BlockVector &, + const std::pair &)> + wrap = helmholtz_operator; + data.cell_loop (wrap, dst, src); + }; + +private: + const MatrixFree &data; +}; + + + + + +template +void test () +{ + typedef double number; + + Triangulation tria; + GridGenerator::hyper_cube (tria); + tria.refine_global(1); + typename Triangulation::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 (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 fe (fe_degree); + DoFHandler dof (tria); + dof.distribute_dofs(fe); + + ConstraintMatrix constraints; + DoFTools::make_hanging_node_constraints(dof, constraints); + VectorTools::interpolate_boundary_values (dof, 0, ZeroFunction(), + 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 mf_data; + { + const QGauss<1> quad (fe_degree+1); + typename MatrixFree::AdditionalData data; + data.mpi_communicator = MPI_COMM_WORLD; + data.tasks_parallel_scheme = + MatrixFree::AdditionalData::partition_color; + data.tasks_block_size = 7; + mf_data.reinit (dof, constraints, quad, data); + } + + MatrixFreeTest mf (mf_data); + parallel::distributed::Vector ref; + parallel::distributed::BlockVector 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 sparse_matrix(sparsity); + { + QGauss quadrature_formula(fe_degree+1); + + FEValues 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 cell_matrix (dofs_per_cell, dofs_per_cell); + std::vector local_dof_indices (dofs_per_cell); + + typename DoFHandler::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_pointget_dof_indices(local_dof_indices); + constraints.distribute_local_to_global (cell_matrix, + local_dof_indices, + sparse_matrix); + } + } + + 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) +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog << std::setprecision(4); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + deallog.push("2d"); + test<2,1>(); + deallog.pop(); + + deallog.push("3d"); + test<3,1>(); + deallog.pop(); +} diff --git a/tests/matrix_free/matrix_vector_20.output b/tests/matrix_free/matrix_vector_20.output new file mode 100644 index 0000000000..111efc484a --- /dev/null +++ b/tests/matrix_free/matrix_vector_20.output @@ -0,0 +1,7 @@ + +DEAL:2d::Testing FE_Q<2>(1) +DEAL:2d::Norm of difference (component 1/2): 0 0 +DEAL:2d:: +DEAL:3d::Testing FE_Q<3>(1) +DEAL:3d::Norm of difference (component 1/2): 0 0 +DEAL:3d:: diff --git a/tests/matrix_free/matrix_vector_21.cc b/tests/matrix_free/matrix_vector_21.cc new file mode 100644 index 0000000000..19e51fbbe6 --- /dev/null +++ b/tests/matrix_free/matrix_vector_21.cc @@ -0,0 +1,270 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2013 - 2015 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + + +// this tests the correctness of matrix free matrix-vector products for two +// vectors on the same DoFHandler. Similar to matrix_vector_20.cc but using +// a single FEEvaluation rather than two. + +#include "../tests.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +template +void +helmholtz_operator (const MatrixFree &data, + parallel::distributed::BlockVector &dst, + const parallel::distributed::BlockVector &src, + const std::pair &cell_range) +{ + FEEvaluation fe_eval (data); + const unsigned int n_q_points = fe_eval.n_q_points; + + for (unsigned int cell=cell_range.first; cell +class MatrixFreeTest +{ +public: + typedef VectorizedArray vector_t; + static const std::size_t n_vectors = VectorizedArray::n_array_elements; + + MatrixFreeTest(const MatrixFree &data_in): + data (data_in) + {}; + + void vmult (parallel::distributed::BlockVector &dst, + const parallel::distributed::BlockVector &src) const + { + for (unsigned int i=0; i &, + parallel::distributed::BlockVector &, + const parallel::distributed::BlockVector &, + const std::pair &)> + wrap = helmholtz_operator; + data.cell_loop (wrap, dst, src); + }; + +private: + const MatrixFree &data; +}; + + + + + +template +void test () +{ + typedef double number; + + Triangulation tria; + GridGenerator::hyper_cube (tria); + tria.refine_global(1); + typename Triangulation::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 (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 fe (fe_degree); + DoFHandler dof (tria); + dof.distribute_dofs(fe); + + ConstraintMatrix constraints; + DoFTools::make_hanging_node_constraints(dof, constraints); + VectorTools::interpolate_boundary_values (dof, 0, ZeroFunction(), + 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 mf_data; + { + const QGauss<1> quad (fe_degree+1); + typename MatrixFree::AdditionalData data; + data.mpi_communicator = MPI_COMM_WORLD; + data.tasks_parallel_scheme = + MatrixFree::AdditionalData::partition_color; + data.tasks_block_size = 7; + mf_data.reinit (dof, constraints, quad, data); + } + + MatrixFreeTest mf (mf_data); + parallel::distributed::Vector ref; + parallel::distributed::BlockVector 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 sparse_matrix(sparsity); + { + QGauss quadrature_formula(fe_degree+1); + + FEValues 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 cell_matrix (dofs_per_cell, dofs_per_cell); + std::vector local_dof_indices (dofs_per_cell); + + typename DoFHandler::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_pointget_dof_indices(local_dof_indices); + constraints.distribute_local_to_global (cell_matrix, + local_dof_indices, + sparse_matrix); + } + } + + 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) +{ + std::ofstream logfile("output"); + deallog.attach(logfile); + deallog << std::setprecision(4); + deallog.depth_console(0); + deallog.threshold_double(1.e-10); + + deallog.push("2d"); + test<2,1>(); + deallog.pop(); + + deallog.push("3d"); + test<3,1>(); + deallog.pop(); +} diff --git a/tests/matrix_free/matrix_vector_21.output b/tests/matrix_free/matrix_vector_21.output new file mode 100644 index 0000000000..111efc484a --- /dev/null +++ b/tests/matrix_free/matrix_vector_21.output @@ -0,0 +1,7 @@ + +DEAL:2d::Testing FE_Q<2>(1) +DEAL:2d::Norm of difference (component 1/2): 0 0 +DEAL:2d:: +DEAL:3d::Testing FE_Q<3>(1) +DEAL:3d::Norm of difference (component 1/2): 0 0 +DEAL:3d::