ExaDG
Loading...
Searching...
No Matches
verify_calculation_of_diagonal.h
1/* ______________________________________________________________________
2 *
3 * ExaDG - High-Order Discontinuous Galerkin for the Exa-Scale
4 *
5 * Copyright (C) 2021 by the ExaDG authors
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 * ______________________________________________________________________
20 */
21
22#ifndef EXADG_SOLVERS_AND_PRECONDITIONERS_UTILITIES_VERIFY_CALCULATION_OF_DIAGONAL_H_
23#define EXADG_SOLVERS_AND_PRECONDITIONERS_UTILITIES_VERIFY_CALCULATION_OF_DIAGONAL_H_
24
25// deal.II
26#include <deal.II/base/conditional_ostream.h>
27#include <deal.II/lac/la_parallel_vector.h>
28
29namespace ExaDG
30{
31/*
32 * To check the correctness of the efficient computation of the diagonal
33 * the result is compared to a naive calculation that simply applies the
34 * whole matrix-vector product N_dofs times. Accordingly, to call this
35 * function the Operator passed to this function has to implement a
36 * function called vmult() that calculates the matrix-vector product.
37 */
38template<typename Operator, typename value_type>
39void
40verify_calculation_of_diagonal(Operator & op,
41 dealii::LinearAlgebra::distributed::Vector<value_type> & diagonal,
42 MPI_Comm const & mpi_comm)
43{
44 dealii::ConditionalOStream pcout(std::cout,
45 dealii::Utilities::MPI::this_mpi_process(mpi_comm) == 0);
46 pcout << "Verify calculation of diagonal:" << std::endl;
47
48 typedef dealii::LinearAlgebra::distributed::Vector<value_type> VectorType;
49
50 VectorType diagonal_check(diagonal);
51 VectorType src(diagonal);
52 VectorType dst(diagonal);
53
54 diagonal_check = 0.0;
55 src = 0.0;
56 dst = 0.0;
57
58 /*
59 * Set dof-value i to 1.0, calculate matrix-vector
60 * product and store row i of the result in diagonal_check.
61 */
62 for(unsigned int i = 0; i < diagonal_check.size(); ++i)
63 {
64 if(diagonal_check.in_local_range(i))
65 {
66 src(i) = 1.0;
67 }
68
69 op.vmult(dst, src);
70
71 if(diagonal_check.in_local_range(i))
72 {
73 diagonal_check(i) = dst(i);
74 src(i) = 0.0;
75 }
76 }
77
78 value_type norm_diagonal = diagonal.l2_norm();
79 value_type norm_diagonal_check = diagonal_check.l2_norm();
80
81 pcout << std::endl
82 << "L2 norm diagonal - Variant 1: " << std::setprecision(10) << norm_diagonal << std::endl;
83 pcout << "L2 norm diagonal - Variant 2: " << std::setprecision(10) << norm_diagonal_check
84 << std::endl;
85
86 diagonal_check.add(-1.0, diagonal);
87 value_type norm_error = diagonal_check.l2_norm();
88
89 pcout << "L2 error diagonal: " << std::setprecision(10) << norm_error << std::endl << std::endl;
90}
91
92} // namespace ExaDG
93
94#endif /* EXADG_SOLVERS_AND_PRECONDITIONERS_UTILITIES_VERIFY_CALCULATION_OF_DIAGONAL_H_ */
Definition driver.cpp:33