From: Denis Davydov <davydden@gmail.com> Date: Tue, 1 Nov 2016 15:31:11 +0000 (+0100) Subject: add MatrixFreeOperators::LaplaceOperator X-Git-Tag: v8.5.0-rc1~511^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d56cd343cbe3313d290bb001a9d9f7ab5fd96190;p=dealii.git add MatrixFreeOperators::LaplaceOperator --- diff --git a/doc/news/changes.h b/doc/news/changes.h index ec9ca30ca0..03e342ca6d 100644 --- a/doc/news/changes.h +++ b/doc/news/changes.h @@ -405,6 +405,11 @@ inconvenience this causes. <h3>Specific improvements</h3> <ol> + <li> New: Add MatrixFreeOperators::LaplaceOperator representing a Laplace matrix. + <br> + (Denis Davydov, 2016/10/30) + </li> + <li> New: Add VectorTools::project() to do L2 projection of scalar-valued quadrature point data in parallel. <br> diff --git a/include/deal.II/matrix_free/operators.h b/include/deal.II/matrix_free/operators.h index adeeba9c5f..4e5a78c132 100644 --- a/include/deal.II/matrix_free/operators.h +++ b/include/deal.II/matrix_free/operators.h @@ -335,6 +335,125 @@ namespace MatrixFreeOperators + /** + * This class implements the operation of the action of a Laplace matrix, + * namely $ L_{ij} = \int_\Omega c(\mathbf x) \mathbf \nabla N_i(\mathbf x) \cdot \mathbf \nabla N_j(\mathbf x)\,d \mathbf x$, + * where $c(\mathbf x)$ is the scalar heterogeneity coefficient. + * + * @author Denis Davydov, 2016 + */ + template <int dim, int fe_degree, int n_q_points_1d = fe_degree+1, int n_components = 1, typename Number = double> + class LaplaceOperator : public Base<dim, Number> + { + public: + + /** + * Constructor. + */ + LaplaceOperator (); + + /** + * The diagonal is approximated by computing a local diagonal matrix per element + * and distributing it to the global diagonal. This will lead to wrong results + * on element with hanging nodes but is still an acceptable approximation + * to be used in preconditioners. + */ + virtual void compute_diagonal (); + + /** + * Set the heterogeneous scalar coefficient @p scalar_coefficient to be used at + * the quadrature points. The Table should be of correct size, consistent + * with the total number of quadrature points in <code>dim</code>-dimensions, + * controlled by the @p n_q_points_1d template parameter. Here, + * <code>(*scalar_coefficient)(cell,q)</code> corresponds to the value of the + * coefficient, where <code>cell</code> is an index into a set of cell + * batches as administered by the MatrixFree framework (which does not work + * on individual cells, but instead of batches of cells at once), and + * <code>q</code> is the number of the quadrature point within this batch. + * + * Such tables can be initialized by + * @code + * std_cxx11::shared_ptr<Table<2, VectorizedArray<double> > > coefficient; + * coefficient = std_cxx11::make_shared<Table<2, VectorizedArray<double> > >(); + * { + * FEEvaluation<dim,fe_degree,n_q_points_1d,1,double> fe_eval(mf_data); + * const unsigned int n_cells = mf_data.n_macro_cells(); + * const unsigned int n_q_points = fe_eval.n_q_points; + * coefficient->reinit(n_cells, n_q_points); + * for (unsigned int cell=0; cell<n_cells; ++cell) + * { + * fe_eval.reinit(cell); + * for (unsigned int q=0; q<n_q_points; ++q) + * (*coefficient)(cell,q) = function.value(fe_eval.quadrature_point(q)); + * } + * } + * @endcode + * where <code>mf_data</code> is a MatrixFree object and <code>function</code> + * is a function which provides the following method + * <code>VectorizedArray<double> value(const Point<dim, VectorizedArray<double> > &p_vec)</code>. + * + * If this function is not called, the coefficient is assumed to be unity. + * + * The argument to this function is a shared pointer to such a table. The + * class stores the shared pointer to this table, not a deep copy + * and uses it to form the Laplace matrix. Consequently, you can update the + * table and re-use the current object to obtain the action of a Laplace + * matrix with this updated coefficient. Alternatively, if the table values + * are only to be filled once, the original shared pointer can also go out + * of scope in user code and the clear() command or destructor of this class + * will delete the table. + */ + void set_coefficient(const std_cxx11::shared_ptr<Table<2, VectorizedArray<Number> > > &scalar_coefficient ); + + virtual void clear(); + + /** + * Read/Write access to coefficients to be used in Laplace operator. + * + * The function will throw an error if coefficients are not previously set + * by set_coefficient() function. + */ + std_cxx11::shared_ptr< Table<2, VectorizedArray<Number> > > get_coefficient(); + + private: + /** + * Applies the laplace matrix operation on an input vector. It is + * assumed that the passed input and output vector are correctly initialized + * using initialize_dof_vector(). + */ + virtual void apply_add (LinearAlgebra::distributed::Vector<Number> &dst, + const LinearAlgebra::distributed::Vector<Number> &src) const; + + /** + * Applies the Laplace operator on a cell. + */ + void local_apply_cell (const MatrixFree<dim,Number> &data, + LinearAlgebra::distributed::Vector<Number> &dst, + const LinearAlgebra::distributed::Vector<Number> &src, + const std::pair<unsigned int,unsigned int> &cell_range) const; + + /** + * Apply diagonal part of the Laplace operator on a cell. + */ + void local_diagonal_cell (const MatrixFree<dim,Number> &data, + LinearAlgebra::distributed::Vector<Number> &dst, + const unsigned int &, + const std::pair<unsigned int,unsigned int> &cell_range) const; + + /** + * Apply Laplace operator on a cell @p cell. + */ + void do_operation_on_cell(FEEvaluation<dim,fe_degree,n_q_points_1d,n_components,Number> &phi, + const unsigned int cell) const; + + /** + * User-provided heterogeneity coefficient. + */ + std_cxx11::shared_ptr< Table<2, VectorizedArray<Number> > > scalar_coefficient; + }; + + + // ------------------------------------ inline functions --------------------- template <int dim, int fe_degree, int n_components, typename Number> @@ -855,6 +974,182 @@ namespace MatrixFreeOperators } } + + //-----------------------------LaplaceOperator---------------------------------- + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + LaplaceOperator () + : + Base<dim, Number>() + { + } + + + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + void + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + clear () + { + Base<dim, Number>::clear(); + scalar_coefficient = NULL; + } + + + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + void + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + set_coefficient(const std_cxx11::shared_ptr<Table<2, VectorizedArray<Number> > > &scalar_coefficient_ ) + { + scalar_coefficient = scalar_coefficient_; + } + + + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + std_cxx11::shared_ptr< Table<2, VectorizedArray<Number> > > + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + get_coefficient() + { + Assert (scalar_coefficient.get(), + ExcNotInitialized()); + return scalar_coefficient; + } + + + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + void + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + compute_diagonal() + { + Assert((Base<dim, Number>::data != NULL), ExcNotInitialized()); + + unsigned int dummy = 0; + LinearAlgebra::distributed::Vector<Number> &inverse_diagonal_entries = Base<dim,Number>::inverse_diagonal_entries; + this->initialize_dof_vector(inverse_diagonal_entries); + Base<dim,Number>:: + data->cell_loop (&LaplaceOperator::local_diagonal_cell, + this, inverse_diagonal_entries, dummy); + + this->set_constrained_entries_to_one(inverse_diagonal_entries); + + for (unsigned int i=0; i<inverse_diagonal_entries.local_size(); ++i) + if (std::abs(inverse_diagonal_entries.local_element(i)) > std::sqrt(std::numeric_limits<Number>::epsilon())) + inverse_diagonal_entries.local_element(i) = 1./inverse_diagonal_entries.local_element(i); + else + inverse_diagonal_entries.local_element(i) = 1.; + + Base<dim, Number>::inverse_diagonal_entries.compress(VectorOperation::insert); + Base<dim, Number>::inverse_diagonal_entries.update_ghost_values(); + } + + + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + void + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + apply_add (LinearAlgebra::distributed::Vector<Number> &dst, + const LinearAlgebra::distributed::Vector<Number> &src) const + { + Base<dim, Number>::data->cell_loop (&LaplaceOperator::local_apply_cell, + this, dst, src); + } + + namespace + { + template<typename Number> + bool + non_negative(const VectorizedArray<Number> &n) + { + for (unsigned int v=0; v<VectorizedArray<Number>::n_array_elements; ++v) + if (n[v] < 0.) + return false; + + return true; + } + } + + + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + void + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + do_operation_on_cell(FEEvaluation<dim,fe_degree,n_q_points_1d,n_components,Number> &phi, + const unsigned int cell) const + { + phi.evaluate (false,true,false); + if (scalar_coefficient.get()) + { + for (unsigned int q=0; q<phi.n_q_points; ++q) + { + Assert (non_negative((*scalar_coefficient)(cell,q)), + ExcMessage("Coefficient must be non-negative")); + phi.submit_gradient ((*scalar_coefficient)(cell,q)*phi.get_gradient(q), q); + } + } + else + { + for (unsigned int q=0; q<phi.n_q_points; ++q) + { + phi.submit_gradient (phi.get_gradient(q), q); + } + } + phi.integrate (false,true); + } + + + + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + void + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + local_apply_cell (const MatrixFree<dim,Number> &data, + LinearAlgebra::distributed::Vector<Number> &dst, + const LinearAlgebra::distributed::Vector<Number> &src, + const std::pair<unsigned int,unsigned int> &cell_range) const + { + FEEvaluation<dim,fe_degree,n_q_points_1d,n_components,Number> phi (data); + for (unsigned int cell=cell_range.first; cell<cell_range.second; ++cell) + { + phi.reinit (cell); + phi.read_dof_values(src); + do_operation_on_cell(phi,cell); + phi.distribute_local_to_global (dst); + } + } + + + template <int dim, int fe_degree, int n_q_points_1d, int n_components, typename Number> + void + LaplaceOperator<dim, fe_degree, n_q_points_1d, n_components, Number>:: + local_diagonal_cell (const MatrixFree<dim,Number> &data, + LinearAlgebra::distributed::Vector<Number> &dst, + const unsigned int &, + const std::pair<unsigned int,unsigned int> &cell_range) const + { + FEEvaluation<dim,fe_degree,n_q_points_1d,n_components,Number> phi (data); + for (unsigned int cell=cell_range.first; cell<cell_range.second; ++cell) + { + phi.reinit (cell); + VectorizedArray<Number> local_diagonal_vector[phi.tensor_dofs_per_cell]; + for (unsigned int i=0; i<phi.dofs_per_cell; ++i) + { + for (unsigned int j=0; j<phi.dofs_per_cell; ++j) + phi.begin_dof_values()[j] = VectorizedArray<Number>(); + phi.begin_dof_values()[i] = 1.; + do_operation_on_cell(phi,cell); + local_diagonal_vector[i] = phi.begin_dof_values()[i]; + } + for (unsigned int i=0; i<phi.tensor_dofs_per_cell; ++i) + phi.begin_dof_values()[i] = local_diagonal_vector[i]; + phi.distribute_local_to_global (dst); + } + } + + } // end of namespace MatrixFreeOperators diff --git a/tests/matrix_free/laplace_operator_01.cc b/tests/matrix_free/laplace_operator_01.cc new file mode 100644 index 0000000000..9675dc7bf3 --- /dev/null +++ b/tests/matrix_free/laplace_operator_01.cc @@ -0,0 +1,219 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 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 is the same as mass_operator_01.cc, but tests Laplace operator instead. + +#include "../tests.h" + +#include <deal.II/base/logstream.h> +#include <deal.II/base/utilities.h> +#include <deal.II/base/function.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/matrix_free/operators.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> +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); + } + + MatrixFreeOperators::LaplaceOperator<dim,fe_degree,fe_degree+1, 1, number> mf; + mf.initialize(mf_data); + mf.compute_diagonal(); + LinearAlgebra::distributed::Vector<number> in, out, ref; + mf_data.initialize_dof_vector (in); + out.reinit (in); + ref.reinit (in); + + for (unsigned int i=0; i<in.local_size(); ++i) + { + const unsigned int glob_index = + owned_set.nth_index_in_set (i); + if (constraints.is_constrained(glob_index)) + continue; + in.local_element(i) = (double)Testing::rand()/RAND_MAX; + } + + mf.vmult (out, in); + + + // assemble trilinos sparse matrix with + // (v, u) for reference + TrilinosWrappers::SparseMatrix sparse_matrix; + { + TrilinosWrappers::SparsityPattern csp (owned_set, MPI_COMM_WORLD); + DoFTools::make_sparsity_pattern (dof, csp, constraints, true, + Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)); + csp.compress(); + sparse_matrix.reinit (csp); + } + { + QGauss<dim> quadrature_formula(fe_degree+1); + + FEValues<dim> fe_values (dof.get_fe(), quadrature_formula, + 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<types::global_dof_index> 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)) * + 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); + + sparse_matrix.vmult (ref, in); + out -= ref; + const double diff_norm = out.linfty_norm(); + + deallog << "Norm of difference: " << diff_norm << std::endl << std::endl; +} + + +int main (int argc, char **argv) +{ + Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, testing_max_num_threads()); + + 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"); + deallog.attach(logfile); + deallog << std::setprecision(4); + 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 + { + test<2,1>(); + test<2,2>(); + test<3,1>(); + test<3,2>(); + } +} + diff --git a/tests/matrix_free/laplace_operator_01.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=1.output b/tests/matrix_free/laplace_operator_01.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=1.output new file mode 100644 index 0000000000..25e13d5f8f --- /dev/null +++ b/tests/matrix_free/laplace_operator_01.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=1.output @@ -0,0 +1,13 @@ + +DEAL:0:2d::Testing FE_Q<2>(1) +DEAL:0:2d::Norm of difference: 0 +DEAL:0:2d:: +DEAL:0:2d::Testing FE_Q<2>(2) +DEAL:0:2d::Norm of difference: 0 +DEAL:0:2d:: +DEAL:0:3d::Testing FE_Q<3>(1) +DEAL:0:3d::Norm of difference: 0 +DEAL:0:3d:: +DEAL:0:3d::Testing FE_Q<3>(2) +DEAL:0:3d::Norm of difference: 0 +DEAL:0:3d:: diff --git a/tests/matrix_free/laplace_operator_01.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=4.output b/tests/matrix_free/laplace_operator_01.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=4.output new file mode 100644 index 0000000000..25e13d5f8f --- /dev/null +++ b/tests/matrix_free/laplace_operator_01.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=4.output @@ -0,0 +1,13 @@ + +DEAL:0:2d::Testing FE_Q<2>(1) +DEAL:0:2d::Norm of difference: 0 +DEAL:0:2d:: +DEAL:0:2d::Testing FE_Q<2>(2) +DEAL:0:2d::Norm of difference: 0 +DEAL:0:2d:: +DEAL:0:3d::Testing FE_Q<3>(1) +DEAL:0:3d::Norm of difference: 0 +DEAL:0:3d:: +DEAL:0:3d::Testing FE_Q<3>(2) +DEAL:0:3d::Norm of difference: 0 +DEAL:0:3d:: diff --git a/tests/matrix_free/laplace_operator_02.cc b/tests/matrix_free/laplace_operator_02.cc new file mode 100644 index 0000000000..0d3f396bf9 --- /dev/null +++ b/tests/matrix_free/laplace_operator_02.cc @@ -0,0 +1,283 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2016 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. +// +// --------------------------------------------------------------------- + + + +// the same as laplace_operator_01, but tests heterogeneous Laplace operator. + +#include "../tests.h" + +#include <deal.II/base/logstream.h> +#include <deal.II/base/utilities.h> +#include <deal.II/base/function.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/matrix_free/operators.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> +class F : public Function<dim> +{ +public: + F (const unsigned int q, + const unsigned int n_components) + : + Function<dim>(n_components), + q(q) + {} + + virtual double value (const Point<dim> &p, + const unsigned int component = 0) const + { + Assert ((component == 0) && (this->n_components == 1), + ExcInternalError()); + double val = 0; + for (unsigned int d=0; d<dim; ++d) + for (unsigned int i=0; i<=q; ++i) + val += (d+1)*(i+1)*std::pow (p[d], 1.*i); + return val; + } + + VectorizedArray<double> value(const Point<dim, VectorizedArray<double> > &p_vec) const + { + VectorizedArray<double> res = make_vectorized_array (0.); + Point<dim> p; + for (unsigned int v = 0; v < VectorizedArray<double>::n_array_elements; ++v) + { + for (unsigned int d = 0; d < dim; d++) + p[d] = p_vec[d][v]; + res[v] = value(p); + } + return res; + } + + +private: + const unsigned int q; +}; + + + + + +template <int dim, int fe_degree> +void test () +{ + typedef double number; + F<dim> function(3,1); + + 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; + data.mapping_update_flags = update_quadrature_points | update_gradients | update_JxW_values; + mf_data.reinit (dof, constraints, quad, data); + } + + std_cxx11::shared_ptr<Table<2, VectorizedArray<number> > > coefficient; + coefficient = std_cxx11::make_shared<Table<2, VectorizedArray<number> > >(); + { + FEEvaluation<dim,fe_degree,fe_degree+1,1,number> fe_eval(mf_data); + + const unsigned int n_cells = mf_data.n_macro_cells(); + const unsigned int n_q_points = fe_eval.n_q_points; + + coefficient->reinit(n_cells, n_q_points); + for (unsigned int cell=0; cell<n_cells; ++cell) + { + fe_eval.reinit(cell); + for (unsigned int q=0; q<n_q_points; ++q) + (*coefficient)(cell,q) = function.value(fe_eval.quadrature_point(q)); + } + } + + MatrixFreeOperators::LaplaceOperator<dim,fe_degree,fe_degree+1, 1, number> mf; + mf.initialize(mf_data); + mf.set_coefficient(coefficient); + mf.compute_diagonal(); + LinearAlgebra::distributed::Vector<number> in, out, ref; + mf_data.initialize_dof_vector (in); + out.reinit (in); + ref.reinit (in); + + for (unsigned int i=0; i<in.local_size(); ++i) + { + const unsigned int glob_index = + owned_set.nth_index_in_set (i); + if (constraints.is_constrained(glob_index)) + continue; + in.local_element(i) = (double)Testing::rand()/RAND_MAX; + } + + mf.vmult (out, in); + + + // assemble trilinos sparse matrix with + // (v, u) for reference + TrilinosWrappers::SparseMatrix sparse_matrix; + { + TrilinosWrappers::SparsityPattern csp (owned_set, MPI_COMM_WORLD); + DoFTools::make_sparsity_pattern (dof, csp, constraints, true, + Utilities::MPI::this_mpi_process(MPI_COMM_WORLD)); + csp.compress(); + sparse_matrix.reinit (csp); + } + { + QGauss<dim> quadrature_formula(fe_degree+1); + + FEValues<dim> fe_values (dof.get_fe(), quadrature_formula, + update_gradients | update_JxW_values | update_quadrature_points); + + 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<types::global_dof_index> 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)) * + function.value(fe_values.quadrature_point(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); + + sparse_matrix.vmult (ref, in); + out -= ref; + const double diff_norm = out.linfty_norm(); + + deallog << "Norm of difference: " << diff_norm << std::endl << std::endl; +} + + +int main (int argc, char **argv) +{ + Utilities::MPI::MPI_InitFinalize mpi_initialization (argc, argv, testing_max_num_threads()); + + 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"); + deallog.attach(logfile); + deallog << std::setprecision(4); + 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 + { + test<2,1>(); + test<2,2>(); + test<3,1>(); + test<3,2>(); + } +} + diff --git a/tests/matrix_free/laplace_operator_02.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=1.output b/tests/matrix_free/laplace_operator_02.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=1.output new file mode 100644 index 0000000000..25e13d5f8f --- /dev/null +++ b/tests/matrix_free/laplace_operator_02.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=1.output @@ -0,0 +1,13 @@ + +DEAL:0:2d::Testing FE_Q<2>(1) +DEAL:0:2d::Norm of difference: 0 +DEAL:0:2d:: +DEAL:0:2d::Testing FE_Q<2>(2) +DEAL:0:2d::Norm of difference: 0 +DEAL:0:2d:: +DEAL:0:3d::Testing FE_Q<3>(1) +DEAL:0:3d::Norm of difference: 0 +DEAL:0:3d:: +DEAL:0:3d::Testing FE_Q<3>(2) +DEAL:0:3d::Norm of difference: 0 +DEAL:0:3d:: diff --git a/tests/matrix_free/laplace_operator_02.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=4.output b/tests/matrix_free/laplace_operator_02.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=4.output new file mode 100644 index 0000000000..25e13d5f8f --- /dev/null +++ b/tests/matrix_free/laplace_operator_02.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=4.output @@ -0,0 +1,13 @@ + +DEAL:0:2d::Testing FE_Q<2>(1) +DEAL:0:2d::Norm of difference: 0 +DEAL:0:2d:: +DEAL:0:2d::Testing FE_Q<2>(2) +DEAL:0:2d::Norm of difference: 0 +DEAL:0:2d:: +DEAL:0:3d::Testing FE_Q<3>(1) +DEAL:0:3d::Norm of difference: 0 +DEAL:0:3d:: +DEAL:0:3d::Testing FE_Q<3>(2) +DEAL:0:3d::Norm of difference: 0 +DEAL:0:3d::