ExaDG
Loading...
Searching...
No Matches
wrapper_elementwise_solvers.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_SOLVERS_WRAPPER_ELEMENTWISE_SOLVERS_H_
23#define EXADG_SOLVERS_AND_PRECONDITIONERS_SOLVERS_WRAPPER_ELEMENTWISE_SOLVERS_H_
24
25// deal.II
26#include <deal.II/lac/la_parallel_vector.h>
27#include <deal.II/matrix_free/operators.h>
28
29// ExaDG
30#include <exadg/matrix_free/integrators.h>
31#include <exadg/solvers_and_preconditioners/preconditioners/elementwise_preconditioners.h>
32#include <exadg/solvers_and_preconditioners/solvers/elementwise_krylov_solvers.h>
33#include <exadg/solvers_and_preconditioners/solvers/enum_types.h>
34#include <exadg/solvers_and_preconditioners/solvers/iterative_solvers_dealii_wrapper.h>
35
36namespace ExaDG
37{
38namespace Elementwise
39{
40/*
41 * Solver data
42 */
43struct IterativeSolverData
44{
45 IterativeSolverData() : solver_type(Elementwise::Solver::CG), solver_data(SolverData())
46 {
47 }
48
49 Solver solver_type;
50
51 SolverData solver_data;
52};
53
54template<int dim,
55 int number_of_equations,
56 typename Number,
57 typename Operator,
58 typename Preconditioner>
59class IterativeSolver
60 : public Krylov::SolverBase<dealii::LinearAlgebra::distributed::Vector<Number>>
61{
62public:
63 typedef dealii::LinearAlgebra::distributed::Vector<Number> VectorType;
64
65 typedef IterativeSolver<dim, number_of_equations, Number, Operator, Preconditioner> THIS;
66
67 IterativeSolver(Operator & operator_in,
68 Preconditioner & preconditioner_in,
69 IterativeSolverData const solver_data_in)
70 : op(operator_in), preconditioner(preconditioner_in), iterative_solver_data(solver_data_in)
71 {
72 }
73
74 virtual ~IterativeSolver()
75 {
76 }
77
78 void
79 update_preconditioner(bool const update_preconditioner) const override
80 {
81 if(preconditioner.needs_update() or update_preconditioner)
82 {
83 preconditioner.update();
84 }
85 }
86
90 unsigned int
91 solve(VectorType & dst, VectorType const & src) const override
92 {
93 op.get_matrix_free().cell_loop(&THIS::solve_elementwise, this, dst, src);
94
95 return 0;
96 }
97
98private:
99 void
100 solve_elementwise(dealii::MatrixFree<dim, Number> const & matrix_free,
101 VectorType & dst,
102 VectorType const & src,
103 std::pair<unsigned int, unsigned int> const & cell_range) const
104 {
105 CellIntegrator<dim, number_of_equations, Number> integrator(matrix_free,
106 op.get_dof_index(),
107 op.get_quad_index());
108
109 unsigned int const dofs_per_cell = integrator.dofs_per_cell;
110
111 dealii::AlignedVector<dealii::VectorizedArray<Number>> solution(
112 dofs_per_cell, dealii::make_vectorized_array<Number>(0.0));
113
114 // setup elementwise solver
115 if(iterative_solver_data.solver_type == Solver::CG)
116 {
117 solver = std::make_shared<
119 dofs_per_cell, iterative_solver_data.solver_data);
120 }
121 else if(iterative_solver_data.solver_type == Solver::GMRES)
122 {
123 solver = std::make_shared<
125 dofs_per_cell, iterative_solver_data.solver_data);
126 }
127 else
128 {
129 AssertThrow(false, dealii::ExcMessage("Not implemented."));
130 }
131
132 // loop over all cells and solve local problem iteratively on each cell
133 for(unsigned int cell = cell_range.first; cell < cell_range.second; ++cell)
134 {
135 integrator.reinit(cell);
136 integrator.read_dof_values(src, 0);
137
138 // initialize operator and preconditioner for current cell
139 op.setup(cell, dofs_per_cell);
140 preconditioner.setup(cell);
141
142 // call iterative solver and solve on current cell
143 solver->solve(&op, solution.begin(), integrator.begin_dof_values(), &preconditioner);
144
145 // write solution on current element to global dof vector
146 for(unsigned int j = 0; j < dofs_per_cell; ++j)
147 integrator.begin_dof_values()[j] = solution[j];
148 integrator.set_dof_values(dst, 0);
149 }
150 }
151
152 mutable std::shared_ptr<
153 Elementwise::SolverBase<dealii::VectorizedArray<Number>, Operator, Preconditioner>>
154 solver;
155
156 Operator & op;
157
158 Preconditioner & preconditioner;
159
160 IterativeSolverData const iterative_solver_data;
161};
162
163} // namespace Elementwise
164} // namespace ExaDG
165
166#endif /* EXADG_SOLVERS_AND_PRECONDITIONERS_SOLVERS_WRAPPER_ELEMENTWISE_SOLVERS_H_ \
167 */
unsigned int solve(VectorType &dst, VectorType const &src) const override
Definition wrapper_elementwise_solvers.h:91
Definition elementwise_krylov_solvers.h:231
Definition elementwise_krylov_solvers.h:356
Definition iterative_solvers_dealii_wrapper.h:40
Definition driver.cpp:33
Definition wrapper_elementwise_solvers.h:44
Definition solver_data.h:34