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 INCLUDE_SOLVERS_AND_PRECONDITIONERS_VERIFY_CALCULATION_OF_DIAGONAL_H_
23#define INCLUDE_SOLVERS_AND_PRECONDITIONERS_VERIFY_CALCULATION_OF_DIAGONAL_H_
24
25#include <deal.II/base/conditional_ostream.h>
26#include <deal.II/lac/la_parallel_vector.h>
27
28namespace ExaDG
29{
30/*
31 * To check the correctness of the efficient computation of the diagonal
32 * the result is compared to a naive calculation that simply applies the
33 * whole matrix-vector product N_dofs times. Accordingly, to call this
34 * function the Operator passed to this function has to implement a
35 * function called vmult() that calculates the matrix-vector product.
36 */
37template<typename Operator, typename value_type>
38void
39verify_calculation_of_diagonal(Operator & op,
40 dealii::LinearAlgebra::distributed::Vector<value_type> & diagonal,
41 MPI_Comm const & mpi_comm)
42{
43 dealii::ConditionalOStream pcout(std::cout,
44 dealii::Utilities::MPI::this_mpi_process(mpi_comm) == 0);
45 pcout << "Verify calculation of diagonal:" << std::endl;
46
47 typedef dealii::LinearAlgebra::distributed::Vector<value_type> VectorType;
48
49 VectorType diagonal_check(diagonal);
50 VectorType src(diagonal);
51 VectorType dst(diagonal);
52
53 diagonal_check = 0.0;
54 src = 0.0;
55 dst = 0.0;
56
57 /*
58 * Set dof-value i to 1.0, calculate matrix-vector
59 * product and store row i of the result in diagonal_check.
60 */
61 for(unsigned int i = 0; i < diagonal_check.size(); ++i)
62 {
63 if(diagonal_check.in_local_range(i))
64 {
65 src(i) = 1.0;
66 }
67
68 op.vmult(dst, src);
69
70 if(diagonal_check.in_local_range(i))
71 {
72 diagonal_check(i) = dst(i);
73 src(i) = 0.0;
74 }
75 }
76
77 value_type norm_diagonal = diagonal.l2_norm();
78 value_type norm_diagonal_check = diagonal_check.l2_norm();
79
80 pcout << std::endl
81 << "L2 norm diagonal - Variant 1: " << std::setprecision(10) << norm_diagonal << std::endl;
82 pcout << "L2 norm diagonal - Variant 2: " << std::setprecision(10) << norm_diagonal_check
83 << std::endl;
84
85 diagonal_check.add(-1.0, diagonal);
86 value_type norm_error = diagonal_check.l2_norm();
87
88 pcout << "L2 error diagonal: " << std::setprecision(10) << norm_error << std::endl << std::endl;
89}
90
91} // namespace ExaDG
92
93
94#endif /* INCLUDE_SOLVERS_AND_PRECONDITIONERS_VERIFY_CALCULATION_OF_DIAGONAL_H_ */
Definition driver.cpp:33