From: Denis Davydov 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.

Specific improvements

    +
  1. New: Add MatrixFreeOperators::LaplaceOperator representing a Laplace matrix. +
    + (Denis Davydov, 2016/10/30) +
  2. +
  3. New: Add VectorTools::project() to do L2 projection of scalar-valued quadrature point data in parallel.
    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 + class LaplaceOperator : public Base + { + 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 dim-dimensions, + * controlled by the @p n_q_points_1d template parameter. Here, + * (*scalar_coefficient)(cell,q) corresponds to the value of the + * coefficient, where cell 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 + * q is the number of the quadrature point within this batch. + * + * Such tables can be initialized by + * @code + * std_cxx11::shared_ptr > > coefficient; + * coefficient = std_cxx11::make_shared > >(); + * { + * FEEvaluation 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; cellmf_data is a MatrixFree object and function + * is a function which provides the following method + * VectorizedArray value(const Point > &p_vec). + * + * 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 > > &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 > > 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 &dst, + const LinearAlgebra::distributed::Vector &src) const; + + /** + * Applies the Laplace operator on a cell. + */ + void local_apply_cell (const MatrixFree &data, + LinearAlgebra::distributed::Vector &dst, + const LinearAlgebra::distributed::Vector &src, + const std::pair &cell_range) const; + + /** + * Apply diagonal part of the Laplace operator on a cell. + */ + void local_diagonal_cell (const MatrixFree &data, + LinearAlgebra::distributed::Vector &dst, + const unsigned int &, + const std::pair &cell_range) const; + + /** + * Apply Laplace operator on a cell @p cell. + */ + void do_operation_on_cell(FEEvaluation &phi, + const unsigned int cell) const; + + /** + * User-provided heterogeneity coefficient. + */ + std_cxx11::shared_ptr< Table<2, VectorizedArray > > scalar_coefficient; + }; + + + // ------------------------------------ inline functions --------------------- template @@ -855,6 +974,182 @@ namespace MatrixFreeOperators } } + + //-----------------------------LaplaceOperator---------------------------------- + + template + LaplaceOperator:: + LaplaceOperator () + : + Base() + { + } + + + + template + void + LaplaceOperator:: + clear () + { + Base::clear(); + scalar_coefficient = NULL; + } + + + + template + void + LaplaceOperator:: + set_coefficient(const std_cxx11::shared_ptr > > &scalar_coefficient_ ) + { + scalar_coefficient = scalar_coefficient_; + } + + + + template + std_cxx11::shared_ptr< Table<2, VectorizedArray > > + LaplaceOperator:: + get_coefficient() + { + Assert (scalar_coefficient.get(), + ExcNotInitialized()); + return scalar_coefficient; + } + + + + template + void + LaplaceOperator:: + compute_diagonal() + { + Assert((Base::data != NULL), ExcNotInitialized()); + + unsigned int dummy = 0; + LinearAlgebra::distributed::Vector &inverse_diagonal_entries = Base::inverse_diagonal_entries; + this->initialize_dof_vector(inverse_diagonal_entries); + Base:: + 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 std::sqrt(std::numeric_limits::epsilon())) + inverse_diagonal_entries.local_element(i) = 1./inverse_diagonal_entries.local_element(i); + else + inverse_diagonal_entries.local_element(i) = 1.; + + Base::inverse_diagonal_entries.compress(VectorOperation::insert); + Base::inverse_diagonal_entries.update_ghost_values(); + } + + + + template + void + LaplaceOperator:: + apply_add (LinearAlgebra::distributed::Vector &dst, + const LinearAlgebra::distributed::Vector &src) const + { + Base::data->cell_loop (&LaplaceOperator::local_apply_cell, + this, dst, src); + } + + namespace + { + template + bool + non_negative(const VectorizedArray &n) + { + for (unsigned int v=0; v::n_array_elements; ++v) + if (n[v] < 0.) + return false; + + return true; + } + } + + + + template + void + LaplaceOperator:: + do_operation_on_cell(FEEvaluation &phi, + const unsigned int cell) const + { + phi.evaluate (false,true,false); + if (scalar_coefficient.get()) + { + for (unsigned int q=0; q + void + LaplaceOperator:: + local_apply_cell (const MatrixFree &data, + LinearAlgebra::distributed::Vector &dst, + const LinearAlgebra::distributed::Vector &src, + const std::pair &cell_range) const + { + FEEvaluation phi (data); + for (unsigned int cell=cell_range.first; cell + void + LaplaceOperator:: + local_diagonal_cell (const MatrixFree &data, + LinearAlgebra::distributed::Vector &dst, + const unsigned int &, + const std::pair &cell_range) const + { + FEEvaluation phi (data); + for (unsigned int cell=cell_range.first; cell local_diagonal_vector[phi.tensor_dofs_per_cell]; + for (unsigned int i=0; i(); + 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + + + +template +void test () +{ + typedef double number; + + parallel::distributed::Triangulation tria (MPI_COMM_WORLD); + 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 (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 fe (fe_degree); + DoFHandler 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(), + 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::none; + data.tasks_block_size = 7; + mf_data.reinit (dof, constraints, quad, data); + } + + MatrixFreeOperators::LaplaceOperator mf; + mf.initialize(mf_data); + mf.compute_diagonal(); + LinearAlgebra::distributed::Vector in, out, ref; + mf_data.initialize_dof_vector (in); + out.reinit (in); + ref.reinit (in); + + for (unsigned int i=0; i quadrature_formula(fe_degree+1); + + FEValues 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 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); + } + } + 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + + +template +class F : public Function +{ +public: + F (const unsigned int q, + const unsigned int n_components) + : + Function(n_components), + q(q) + {} + + virtual double value (const Point &p, + const unsigned int component = 0) const + { + Assert ((component == 0) && (this->n_components == 1), + ExcInternalError()); + double val = 0; + for (unsigned int d=0; d value(const Point > &p_vec) const + { + VectorizedArray res = make_vectorized_array (0.); + Point p; + for (unsigned int v = 0; v < VectorizedArray::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 +void test () +{ + typedef double number; + F function(3,1); + + parallel::distributed::Triangulation tria (MPI_COMM_WORLD); + 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 (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 fe (fe_degree); + DoFHandler 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(), + 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::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 > > coefficient; + coefficient = std_cxx11::make_shared > >(); + { + FEEvaluation 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 mf; + mf.initialize(mf_data); + mf.set_coefficient(coefficient); + mf.compute_diagonal(); + LinearAlgebra::distributed::Vector in, out, ref; + mf_data.initialize_dof_vector (in); + out.reinit (in); + ref.reinit (in); + + for (unsigned int i=0; i quadrature_formula(fe_degree+1); + + FEValues 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 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); + } + } + 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::