Lethe Open-Source Library version 1.2.0
Developer Guide
Loading...
Searching...
No Matches
cls_linear_subequations_solver.cc
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) 2024-2026 The Lethe Authors
2// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
3
5
6#include <deal.II/dofs/dof_renumbering.h>
7
8#include <deal.II/grid/grid_tools.h>
9
10#include <deal.II/lac/solver_control.h>
11#include <deal.II/lac/sparsity_tools.h>
12#include <deal.II/lac/trilinos_solver.h>
13
14
15template <int dim>
16void
18{
19 // Get MPI communicator
20 auto mpi_communicator = this->triangulation->get_mpi_communicator();
21
22 // Distribute and renumber DoFs
23 this->dof_handler->distribute_dofs(*this->fe);
24 DoFRenumbering::Cuthill_McKee(*this->dof_handler);
25
26 // Get locally owned and relevant DoFs
27 this->locally_owned_dofs = this->dof_handler->locally_owned_dofs();
29 DoFTools::extract_locally_relevant_dofs(*this->dof_handler);
30
31 // Constraints
32 this->constraints.clear();
33 this->constraints.reinit(this->locally_owned_dofs,
35 DoFTools::make_hanging_node_constraints(*this->dof_handler,
36 this->constraints);
37
38 // Add periodic boundary conditions for the linear subequation
39 for (auto const &[id, type] :
40 this->simulation_parameters.cls_boundary_conditions.type)
41 {
43 {
44 const auto &periodic_boundary =
45 this->simulation_parameters.cls_boundary_conditions
46 .periodic_boundaries.at(id);
47 DoFTools::make_periodicity_constraints(*this->dof_handler,
48 id,
49 periodic_boundary.neighbor_id,
50 periodic_boundary.direction,
51 this->constraints);
52 }
53 }
54
55 this->constraints.close();
56
57 // Sparsity pattern
58 DynamicSparsityPattern dsp(this->locally_relevant_dofs);
59 DoFTools::make_sparsity_pattern(*this->dof_handler,
60 dsp,
61 this->constraints,
62 false);
63 SparsityTools::distribute_sparsity_pattern(dsp,
65 mpi_communicator,
67
68 // Reinitialize system matrix and right-hand side (rhs)
69 this->system_matrix.reinit(this->locally_owned_dofs,
71 dsp,
72 mpi_communicator);
73 this->system_rhs.reinit(this->locally_owned_dofs, mpi_communicator);
74
75 // Reinitialize solution vectors
76 this->present_solution->reinit(this->locally_owned_dofs,
78 mpi_communicator);
80
81
83 {
84 std::string subequation_string =
85 this->subequations_interface.get_subequation_string(
86 this->subequation_id);
87 this->pcout << " Number of " << subequation_string
88 << " degrees of freedom: " << this->dof_handler->n_dofs()
89 << std::endl;
90 }
91
92 // Provide DoFHandler and solutions to the subequations interface
93 this->subequations_interface.set_dof_handler(this->subequation_id,
94 this->dof_handler);
95 this->subequations_interface.set_solution(this->subequation_id,
96 this->present_solution);
97}
98
99
100template <int dim>
101void
103{
104 auto mpi_communicator = this->triangulation->get_mpi_communicator();
105
106 const AffineConstraints<double> &constraints_used = this->constraints;
107
108 const bool verbose(
111
112 if (verbose)
113 {
114 std::string subequation_string =
115 this->subequations_interface.get_subequation_string(
116 this->subequation_id);
117
118 this->pcout << " -Solving " << subequation_string << ":" << std::endl;
119 }
120
121 // Set tolerance
122 const double rescale_metric =
123 simulation_parameters.linear_solver.at(PhysicsID::CLS)
124 .rescale_residual_by_volume ?
125 std::sqrt(GridTools::volume(*this->triangulation, *this->mapping)) :
126 1.0;
127 const double linear_solver_tolerance =
128 this->simulation_parameters.linear_solver.at(PhysicsID::CLS)
129 .minimum_residual /
130 rescale_metric;
131
132 const double non_rescaled_linear_solver_tolerance =
133 linear_solver_tolerance * rescale_metric;
134
135 // Solution vector
136 GlobalVectorType completely_distributed_solution(this->locally_owned_dofs,
137 mpi_communicator);
138
139 if (verbose)
140 {
141 this->pcout << " -Tolerance of iterative solver is: "
142 << linear_solver_tolerance << std::endl;
143 }
144
145 // ILU preconditioner
146 const unsigned int ilu_fill =
147 this->simulation_parameters.linear_solver.at(PhysicsID::CLS)
148 .ilu_precond_fill;
149 const double ilu_atol =
150 this->simulation_parameters.linear_solver.at(PhysicsID::CLS)
151 .ilu_precond_atol;
152 const double ilu_rtol =
153 this->simulation_parameters.linear_solver.at(PhysicsID::CLS)
154 .ilu_precond_rtol;
155 TrilinosWrappers::PreconditionILU::AdditionalData preconditionerOptions(
156 ilu_fill, ilu_atol, ilu_rtol, 0);
157
158 this->ilu_preconditioner =
159 std::make_shared<TrilinosWrappers::PreconditionILU>();
160
161 this->ilu_preconditioner->initialize(this->system_matrix,
162 preconditionerOptions);
163
164 // CG solver
165 SolverControl solver_control(
166 this->simulation_parameters.linear_solver.at(PhysicsID::CLS).max_iterations,
167 non_rescaled_linear_solver_tolerance,
168 true,
169 true);
170
171 TrilinosWrappers::SolverCG solver(solver_control);
172
173 solver.solve(this->system_matrix,
174 completely_distributed_solution,
175 this->system_rhs,
176 *this->ilu_preconditioner);
177
178 if (verbose)
179 {
180 this->pcout << " -Iterative solver took " << solver_control.last_step()
181 << " steps to reach a residual norm of "
182 << solver_control.last_value() / rescale_metric << std::endl;
183 }
184
185 // Update constraints vector
186 constraints_used.distribute(completely_distributed_solution);
187
188 // Update solution vectors
189 *this->present_solution = completely_distributed_solution;
190 this->evaluation_point = *this->present_solution;
191}
192
193
194template <int dim>
195void
203
204
205template class CLSLinearSubequationsSolver<2>;
206template class CLSLinearSubequationsSolver<3>;
Generalized solver for CLS linear subequations.
const Parameters::Verbosity subequation_verbosity
virtual void assemble_system_matrix_and_rhs()=0
Assemble the matrix and the right-hand side (rhs) associated with the solver.
std::shared_ptr< FiniteElement< dim > > fe
std::shared_ptr< parallel::DistributedTriangulationBase< dim > > triangulation
std::shared_ptr< TrilinosWrappers::PreconditionILU > ilu_preconditioner
std::shared_ptr< Mapping< dim > > mapping
std::shared_ptr< DoFHandler< dim > > dof_handler
CLSSubequationsInterface< dim > & subequations_interface
void setup_dofs() override
Set up the DofHandler and the degree of freedom associated with the subequation.
void solve() override
Assemble and solve linear system when the equation to solve is linear without using the non-linear so...
const SimulationParameters< dim > & simulation_parameters
std::shared_ptr< GlobalVectorType > present_solution
TrilinosWrappers::SparseMatrix system_matrix
const Parameters::Verbosity linear_solver_verbosity
virtual void check_dependencies_validity()=0
Check if the solutions on which the subequation depends on are valid.
void solve_linear_system_and_update_solution() override
Solve linear system of equation using a strategy appropriate for the partial differential equation.
@ CLS
dealii::TrilinosWrappers::MPI::Vector GlobalVectorType
Definition vector.h:17