From: Martin Kronbichler Date: Sun, 3 May 2020 09:06:55 +0000 (+0200) Subject: Test case X-Git-Tag: v9.2.0-rc1~137^2~2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41b8af51b755ff6777a6560784cb1b9af36313ea;p=dealii.git Test case --- diff --git a/tests/matrix_free/laplace_operator_03.cc b/tests/matrix_free/laplace_operator_03.cc new file mode 100644 index 0000000000..ef666ea2e6 --- /dev/null +++ b/tests/matrix_free/laplace_operator_03.cc @@ -0,0 +1,236 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2020 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.md at +// the top level directory of deal.II. +// +// --------------------------------------------------------------------- + + + +// the same as laplace_operator_01, but heterogeneous Laplace operator with a +// single constant coefficient per cell + +#include +#include + +#include + +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include + +#include + +#include + +#include + +#include "../tests.h" + + +template +void +test() +{ + typedef double number; + + parallel::distributed::Triangulation tria(MPI_COMM_WORLD); + GridGenerator::hyper_cube(tria); + tria.refine_global(1); + for (const auto &cell : tria.active_cell_iterators()) + 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(); + for (unsigned int i = 0; i < 10 - 3 * dim; ++i) + { + unsigned int counter = 0; + for (const auto &cell : tria.active_cell_iterators()) + 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); + + AffineConstraints constraints(relevant_set); + DoFTools::make_hanging_node_constraints(dof, constraints); + VectorTools::interpolate_boundary_values(dof, + 0, + Functions::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; + + std::shared_ptr> mf_data( + new MatrixFree()); + { + const QGauss<1> quad(fe_degree + 1); + typename MatrixFree::AdditionalData data; + 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::shared_ptr>> coefficient; + coefficient = std::make_shared>>(); + { + coefficient->reinit(mf_data->n_cell_batches(), 1); + for (unsigned int cell = 0; cell < mf_data->n_cell_batches(); ++cell) + for (unsigned int v = 0; + v < mf_data->n_active_entries_per_cell_batch(cell); + ++v) + (*coefficient)(cell, 0)[v] = + 1. + std::sqrt(static_cast( + mf_data->get_cell_iterator(cell, v)->active_cell_index())); + } + + MatrixFreeOperators::LaplaceOperator< + dim, + fe_degree, + fe_degree + 1, + 1, + LinearAlgebra::distributed::Vector> + 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 < 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) = random_value(); + } + + 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 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); + + const double coefficient = + 1. + std::sqrt(static_cast(cell->active_cell_index())); + + 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)) * + coefficient * 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, 1); + + mpi_initlog(); + + deallog << std::setprecision(4); + + deallog.push("2d"); + test<2, 1>(); + test<2, 2>(); + deallog.pop(); + + deallog.push("3d"); + test<3, 1>(); + test<3, 2>(); + deallog.pop(); +} diff --git a/tests/matrix_free/laplace_operator_03.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output b/tests/matrix_free/laplace_operator_03.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output new file mode 100644 index 0000000000..8099ecc45a --- /dev/null +++ b/tests/matrix_free/laplace_operator_03.with_trilinos=true.with_mpi=true.with_p4est=true.mpirun=3.output @@ -0,0 +1,13 @@ + +DEAL:2d::Testing FE_Q<2>(1) +DEAL:2d::Norm of difference: 2.593e-13 +DEAL:2d:: +DEAL:2d::Testing FE_Q<2>(2) +DEAL:2d::Norm of difference: 4.974e-14 +DEAL:2d:: +DEAL:3d::Testing FE_Q<3>(1) +DEAL:3d::Norm of difference: 3.553e-15 +DEAL:3d:: +DEAL:3d::Testing FE_Q<3>(2) +DEAL:3d::Norm of difference: 4.885e-15 +DEAL:3d::