| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 1998 - 2025 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_solver_cg_h |
| 16 |
#define dealii_solver_cg_h |
| 17 |
|
| 18 |
|
| 19 |
#include <deal.II/base/config.h> |
| 20 |
|
| 21 |
#include <deal.II/base/enable_observer_pointer.h> |
| 22 |
#include <deal.II/base/exceptions.h> |
| 23 |
#include <deal.II/base/logstream.h> |
| 24 |
#include <deal.II/base/template_constraints.h> |
| 25 |
#include <deal.II/base/vectorization.h> |
| 26 |
|
| 27 |
#include <deal.II/lac/solver.h> |
| 28 |
#include <deal.II/lac/solver_control.h> |
| 29 |
#include <deal.II/lac/tridiagonal_matrix.h> |
| 30 |
|
| 31 |
#include <boost/signals2.hpp> |
| 32 |
|
| 33 |
#include <cmath> |
| 34 |
|
| 35 |
DEAL_II_NAMESPACE_OPEN |
| 36 |
|
| 37 |
// forward declaration |
| 38 |
#ifndef DOXYGEN |
| 39 |
class PreconditionIdentity; |
| 40 |
namespace LinearAlgebra |
| 41 |
{ |
| 42 |
namespace distributed |
| 43 |
{ |
| 44 |
template <typename, typename> |
| 45 |
class Vector; |
| 46 |
} |
| 47 |
} // namespace LinearAlgebra |
| 48 |
#endif |
| 49 |
|
| 50 |
|
| 51 |
/** @addtogroup Solvers */ |
| 52 |
/** @{ */ |
| 53 |
|
| 54 |
/** |
| 55 |
* This class implements the preconditioned Conjugate Gradients (CG) |
| 56 |
* method that can be used to solve linear systems with a symmetric positive |
| 57 |
* definite matrix. This |
| 58 |
* class is used first in step-3 and step-4, but is used in many other |
| 59 |
* tutorial programs as well. Like all other solver classes, it can work on |
| 60 |
* any kind of vector and matrix as long as they satisfy certain requirements |
| 61 |
* (for the requirements on matrices and vectors in order to work with this |
| 62 |
* class, see the documentation of the Solver base class). The type of the |
| 63 |
* solution vector must be passed as template argument, and defaults to |
| 64 |
* dealii::Vector<double>. The AdditionalData structure allows to control the |
| 65 |
* type of residual for the stopping condition. |
| 66 |
* |
| 67 |
* @note The CG method requires a symmetric preconditioner (i.e., for example, |
| 68 |
* SOR is not a possible choice). There is a variant of the solver, |
| 69 |
* SolverFlexibleCG, that allows to use a variable preconditioner or a |
| 70 |
* preconditioner with some slight non-symmetry (like weighted Schwarz |
| 71 |
* methods), by using a different formula for the step length in the |
| 72 |
* computation of the next search direction. |
| 73 |
* |
| 74 |
* |
| 75 |
* <h3>Eigenvalue computation</h3> |
| 76 |
* |
| 77 |
* The cg-method performs an orthogonal projection of the original |
| 78 |
* preconditioned linear system to another system of smaller dimension. |
| 79 |
* Furthermore, the projected matrix @p T is tri-diagonal. Since the |
| 80 |
* projection is orthogonal, the eigenvalues of @p T approximate those of the |
| 81 |
* original preconditioned matrix @p PA. In fact, after @p n steps, where @p n |
| 82 |
* is the dimension of the original system, the eigenvalues of both matrices |
| 83 |
* are equal. But, even for small numbers of iteration steps, the condition |
| 84 |
* number of @p T is a good estimate for the one of @p PA. |
| 85 |
* |
| 86 |
* After @p m steps the matrix T_m can be written in terms of the coefficients |
| 87 |
* @p alpha and @p beta as the tri-diagonal matrix with diagonal elements |
| 88 |
* <tt>1/alpha_0</tt>, <tt>1/alpha_1 + beta_0/alpha_0</tt>, ..., |
| 89 |
* <tt>1/alpha_{m-1}+beta_{m-2}/alpha_{m-2}</tt> and off-diagonal elements |
| 90 |
* <tt>sqrt(beta_0)/alpha_0</tt>, ..., <tt>sqrt(beta_{m-2})/alpha_{m-2}</tt>. |
| 91 |
* The eigenvalues of this matrix can be computed by postprocessing. |
| 92 |
* |
| 93 |
* @see Y. Saad: "Iterative methods for Sparse Linear Systems", section 6.7.3 |
| 94 |
* for details. |
| 95 |
* |
| 96 |
* The coefficients, eigenvalues and condition number (computed as the ratio |
| 97 |
* of the largest over smallest eigenvalue) can be obtained by connecting a |
| 98 |
* function as a slot to the solver using one of the functions @p |
| 99 |
* connect_coefficients_slot, @p connect_eigenvalues_slot and @p |
| 100 |
* connect_condition_number_slot. These slots will then be called from the |
| 101 |
* solver with the estimates as argument. |
| 102 |
* |
| 103 |
* <h3>Observing the progress of linear solver iterations</h3> |
| 104 |
* |
| 105 |
* The solve() function of this class uses the mechanism described in the |
| 106 |
* Solver base class to determine convergence. This mechanism can also be used |
| 107 |
* to observe the progress of the iteration. |
| 108 |
* |
| 109 |
* <h4>Optimized operations with specific `MatrixType` argument</h4> |
| 110 |
* |
| 111 |
* This class enables to embed the vector updates into the matrix-vector |
| 112 |
* product in case the `MatrixType` and `PreconditionerType` support such a |
| 113 |
* mode of operation. To this end, the `VectorType` needs to be |
| 114 |
* LinearAlgebra::distributed::Vector, the class `MatrixType` needs to provide |
| 115 |
* a function with the signature |
| 116 |
* @code |
| 117 |
* void MatrixType::vmult( |
| 118 |
* VectorType &, |
| 119 |
* const VectorType &, |
| 120 |
* const std::function<void(const unsigned int, const unsigned int)> &, |
| 121 |
* const std::function<void(const unsigned int, const unsigned int)> &) const |
| 122 |
* @endcode |
| 123 |
* where the two given functions run before and after the matrix-vector |
| 124 |
* product, respectively, and the `PreconditionerType` needs to provide a |
| 125 |
* function either the signature |
| 126 |
* @code |
| 127 |
* Number PreconditionerType::apply(unsigned int index, const Number src) const |
| 128 |
* @endcode |
| 129 |
* to apply the action of the preconditioner on a single element (effectively |
| 130 |
* being a diagonal preconditioner), or the signature |
| 131 |
* @code |
| 132 |
* void PreconditionerType::apply_to_subrange(unsigned int start_range, |
| 133 |
* unsigned int end_range, |
| 134 |
* const Number* src_ptr_to_subrange, |
| 135 |
* Number* dst_ptr_to_subrange) |
| 136 |
* @endcode |
| 137 |
* where the pointers `src_ptr_to_subrange` and `dst_ptr_to_subrange` point to |
| 138 |
* the location in the vector where the operation should be applied to. If both |
| 139 |
* functions are given, the more optimized `apply` path is selected. The |
| 140 |
* functions passed to `MatrixType::vmult` take as arguments a sub-range among |
| 141 |
* the locally owned elements of the vector, defined as half-open |
| 142 |
* intervals. The intervals are designed to be scheduled close to the time the |
| 143 |
* matrix-vector product touches those entries in the `src` and `dst` vectors, |
| 144 |
* respectively, with the requirement that |
| 145 |
* <ul> |
| 146 |
* <li> the matrix-vector product may only access an entry in `src` or `dst` |
| 147 |
* once the `operation_before_matrix_vector_product` has been run on that |
| 148 |
* vector entry; </li> |
| 149 |
* <li> `operation_after_matrix_vector_product` may run on a range of entries |
| 150 |
* `[i,j)` once the matrix-vector product does not access the entries `[i,j)` |
| 151 |
* in `src` and `dst` any more. </li> |
| 152 |
* </ul> |
| 153 |
* The motivation for this function is to increase data locality and hence |
| 154 |
* cache usage. For the example of a class similar to the one in the step-37 |
| 155 |
* tutorial program, the implementation is |
| 156 |
* @code |
| 157 |
* void |
| 158 |
* vmult(LinearAlgebra::distributed::Vector<number> & dst, |
| 159 |
* const LinearAlgebra::distributed::Vector<number> &src, |
| 160 |
* const std::function<void(const unsigned int, const unsigned int)> |
| 161 |
* &operation_before_matrix_vector_product, |
| 162 |
* const std::function<void(const unsigned int, const unsigned int)> |
| 163 |
* &operation_after_matrix_vector_product) const |
| 164 |
* { |
| 165 |
* data.cell_loop(&LaplaceOperator::local_apply, |
| 166 |
* this, |
| 167 |
* dst, |
| 168 |
* src, |
| 169 |
* operation_before_matrix_vector_product, |
| 170 |
* operation_after_matrix_vector_product); |
| 171 |
* } |
| 172 |
* @endcode |
| 173 |
* |
| 174 |
* In terms of the SolverCG implementation, the operation before the loop will |
| 175 |
* run the updates on the vectors according to a variant presented in |
| 176 |
* Algorithm 2.2 of @cite Chronopoulos1989 (but for a preconditioner), whereas |
| 177 |
* the operation after the loop performs a total of 7 reductions in parallel. |
| 178 |
* |
| 179 |
* <h3>Preconditioned residual</h3> |
| 180 |
* |
| 181 |
* @p AdditionalData allows you to choose between using the explicit |
| 182 |
* or implicit residual as a stopping condition for the iterative |
| 183 |
* solver. This behavior can be overridden by using the flag |
| 184 |
* AdditionalData::use_default_residual. A <tt>true</tt> value refers to the |
| 185 |
* implicit residual, while <tt>false</tt> reverts |
| 186 |
* it. The former uses the result of the matrix-vector |
| 187 |
* product already computed in other algorithm steps to derive the residual by |
| 188 |
* a mere vector update, whereas the latter explicitly calculates the system |
| 189 |
* residual with an additional matrix-vector product. |
| 190 |
* More information on explicit and implicit residual stopping |
| 191 |
* criteria can be found |
| 192 |
* <a |
| 193 |
* href="https://en.wikipedia.org/wiki/Conjugate_gradient_method#Explicit_residual_calculation">link |
| 194 |
* here</a>. |
| 195 |
*/ |
| 196 |
template <typename VectorType = Vector<double>> |
| 197 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 198 |
class SolverCG : public SolverBase<VectorType> |
| 199 |
{ |
| 200 |
public: |
| 201 |
/** |
| 202 |
* Declare type for container size. |
| 203 |
*/ |
| 204 |
using size_type = types::global_dof_index; |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* Standardized data struct to pipe additional data to the solver. |
| 209 |
*/ |
| 210 |
struct AdditionalData |
| 211 |
{ |
| 212 |
/** |
| 213 |
* Constructor. By default, set the residual of the stopping criterion |
| 214 |
* to the implicit residual. A <tt>true</tt> value of |
| 215 |
* AdditionalData::use_default_residual refers to the |
| 216 |
* implicit residual, while <tt>false</tt> reverts |
| 217 |
* it. The former uses the result of the matrix-vector |
| 218 |
* product already computed in other algorithm steps to derive the residual |
| 219 |
* by a mere vector update, whereas the latter explicitly calculates the |
| 220 |
* system residual with an additional matrix-vector product. More |
| 221 |
* information on explicit and implicit residual stopping criteria can be |
| 222 |
* found <a |
| 223 |
* href="https://en.wikipedia.org/wiki/Conjugate_gradient_method#Explicit_residual_calculation">link |
| 224 |
* here</a>. |
| 225 |
*/ |
| 226 |
explicit AdditionalData(const bool use_default_residual = true); |
| 227 |
|
| 228 |
/** |
| 229 |
* Flag for the default residual that is used to measure convergence. |
| 230 |
*/ |
| 231 |
bool use_default_residual; |
| 232 |
}; |
| 233 |
|
| 234 |
|
| 235 |
/** |
| 236 |
* Constructor. |
| 237 |
*/ |
| 238 |
SolverCG(SolverControl &cn, |
| 239 |
VectorMemory<VectorType> &mem, |
| 240 |
const AdditionalData &data = AdditionalData()); |
| 241 |
|
| 242 |
/** |
| 243 |
* Constructor. Use an object of type GrowingVectorMemory as a default to |
| 244 |
* allocate memory. |
| 245 |
*/ |
| 246 |
SolverCG(SolverControl &cn, const AdditionalData &data = AdditionalData()); |
| 247 |
|
| 248 |
/** |
| 249 |
* Virtual destructor. |
| 250 |
*/ |
| 251 |
virtual ~SolverCG() override = default; |
| 252 |
|
| 253 |
/** |
| 254 |
* Solve the linear system $Ax=b$ for x. |
| 255 |
*/ |
| 256 |
template <typename MatrixType, typename PreconditionerType> |
| 257 |
DEAL_II_CXX20_REQUIRES( |
| 258 |
(concepts::is_linear_operator_on<MatrixType, VectorType> && |
| 259 |
concepts::is_linear_operator_on<PreconditionerType, VectorType>)) |
| 260 |
void solve(const MatrixType &A, |
| 261 |
VectorType &x, |
| 262 |
const VectorType &b, |
| 263 |
const PreconditionerType &preconditioner); |
| 264 |
|
| 265 |
/** |
| 266 |
* Connect a slot to retrieve the CG coefficients. The slot will be called |
| 267 |
* with alpha as the first argument and with beta as the second argument, |
| 268 |
* where alpha and beta follow the notation in Y. Saad: "Iterative methods |
| 269 |
* for Sparse Linear Systems", section 6.7. Called once per iteration |
| 270 |
*/ |
| 271 |
boost::signals2::connection |
| 272 |
connect_coefficients_slot( |
| 273 |
const std::function<void(typename VectorType::value_type, |
| 274 |
typename VectorType::value_type)> &slot); |
| 275 |
|
| 276 |
/** |
| 277 |
* Connect a slot to retrieve the estimated condition number. Called on each |
| 278 |
* iteration if every_iteration=true, otherwise called once when iterations |
| 279 |
* are ended (i.e., either because convergence has been achieved, or because |
| 280 |
* divergence has been detected). |
| 281 |
*/ |
| 282 |
boost::signals2::connection |
| 283 |
connect_condition_number_slot(const std::function<void(double)> &slot, |
| 284 |
const bool every_iteration = false); |
| 285 |
|
| 286 |
/** |
| 287 |
* Connect a slot to retrieve the estimated eigenvalues. Called on each |
| 288 |
* iteration if every_iteration=true, otherwise called once when iterations |
| 289 |
* are ended (i.e., either because convergence has been achieved, or because |
| 290 |
* divergence has been detected). |
| 291 |
*/ |
| 292 |
boost::signals2::connection |
| 293 |
connect_eigenvalues_slot( |
| 294 |
const std::function<void(const std::vector<double> &)> &slot, |
| 295 |
const bool every_iteration = false); |
| 296 |
|
| 297 |
protected: |
| 298 |
/** |
| 299 |
* Interface for derived class. This function gets the current iteration |
| 300 |
* vector, the residual and the update vector in each step. It can be used |
| 301 |
* for graphical output of the convergence history. |
| 302 |
*/ |
| 303 |
virtual void |
| 304 |
print_vectors(const unsigned int step, |
| 305 |
const VectorType &x, |
| 306 |
const VectorType &r, |
| 307 |
const VectorType &d) const; |
| 308 |
|
| 309 |
/** |
| 310 |
* Estimates the eigenvalues from diagonal and offdiagonal. Uses these |
| 311 |
* estimate to compute the condition number. Calls the signals |
| 312 |
* eigenvalues_signal and cond_signal with these estimates as arguments. |
| 313 |
*/ |
| 314 |
static void |
| 315 |
compute_eigs_and_cond( |
| 316 |
const std::vector<typename VectorType::value_type> &diagonal, |
| 317 |
const std::vector<typename VectorType::value_type> &offdiagonal, |
| 318 |
const boost::signals2::signal<void(const std::vector<double> &)> |
| 319 |
&eigenvalues_signal, |
| 320 |
const boost::signals2::signal<void(double)> &cond_signal); |
| 321 |
|
| 322 |
/** |
| 323 |
* Additional parameters. |
| 324 |
*/ |
| 325 |
AdditionalData additional_data; |
| 326 |
|
| 327 |
/** |
| 328 |
* Signal used to retrieve the CG coefficients. Called on each iteration. |
| 329 |
*/ |
| 330 |
boost::signals2::signal<void(typename VectorType::value_type, |
| 331 |
typename VectorType::value_type)> |
| 332 |
coefficients_signal; |
| 333 |
|
| 334 |
/** |
| 335 |
* Signal used to retrieve the estimated condition number. Called once when |
| 336 |
* all iterations are ended. |
| 337 |
*/ |
| 338 |
boost::signals2::signal<void(double)> condition_number_signal; |
| 339 |
|
| 340 |
/** |
| 341 |
* Signal used to retrieve the estimated condition numbers. Called on each |
| 342 |
* iteration. |
| 343 |
*/ |
| 344 |
boost::signals2::signal<void(double)> all_condition_numbers_signal; |
| 345 |
|
| 346 |
/** |
| 347 |
* Signal used to retrieve the estimated eigenvalues. Called once when all |
| 348 |
* iterations are ended. |
| 349 |
*/ |
| 350 |
boost::signals2::signal<void(const std::vector<double> &)> eigenvalues_signal; |
| 351 |
|
| 352 |
/** |
| 353 |
* Signal used to retrieve the estimated eigenvalues. Called on each |
| 354 |
* iteration. |
| 355 |
*/ |
| 356 |
boost::signals2::signal<void(const std::vector<double> &)> |
| 357 |
all_eigenvalues_signal; |
| 358 |
|
| 359 |
/** |
| 360 |
* Flag to indicate whether the classical Fletcher--Reeves update formula |
| 361 |
* for the parameter $\beta_k$ (standard CG algorithm, minimal storage |
| 362 |
* needs) or the flexible conjugate gradient method with Polak-Ribiere |
| 363 |
* formula for $\beta_k$ should be used. This base class implementation of |
| 364 |
* SolverCG will always use the former method, whereas the derived class |
| 365 |
* SolverFlexibleCG will use the latter. |
| 366 |
*/ |
| 367 |
bool determine_beta_by_flexible_formula; |
| 368 |
}; |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
/** |
| 373 |
* This class implements a flexible variant of the conjugate gradient method, |
| 374 |
* which is based on a different formula to compute $\beta_k$ in the process |
| 375 |
* of constructing a new search direction that is A-orthogonal against the |
| 376 |
* previous one. Rather than using the Fletcher--Reeves update formula with |
| 377 |
* $\beta_k = \frac{\mathbf{r}^T_{k+1} \mathbf{z}_{k+1}}{\mathbf{r}^T_{k} |
| 378 |
* \mathbf{z}_{k}}$ for computing the new search direction (here |
| 379 |
* $\mathbf{r}_{k+1}$ is the residual in step $k+1$ and $\mathbf{z}_{k+1} = |
| 380 |
* P^{-1} \mathbf{r}_{k+1}$) as in the classical conjugate gradient algorithm, |
| 381 |
* this class selects the Polak-Ribiere formula $\beta_k = |
| 382 |
* \frac{\mathbf{r}^T_{k+1} \left(\mathbf{z}_{k+1} - |
| 383 |
* \mathbf{z}_{k}\right)}{\mathbf{r}^T_{k} \mathbf{z}_{k}}$. The |
| 384 |
* additional term $\mathbf{r}^T_{k+1} \mathbf{z}_{k}$ is zero for linear |
| 385 |
* symmetric-positive definite preconditioners due to the construction of the |
| 386 |
* search directions, so the behavior of SolverFlexibleCG is equivalent for |
| 387 |
* those kinds of situations and merely increases costs by requiring an |
| 388 |
* additional stored vector and associated vector operations. While there are |
| 389 |
* no theoretical guarantees for convergence as in the classical CG algorithm, |
| 390 |
* the current class has been documented to be much more robust for variable |
| 391 |
* preconditioners (e.g., involving some iterative inverse that is not fully |
| 392 |
* converged) or a preconditioner with some slight non-symmetry (like weighted |
| 393 |
* Schwarz methods), which results from the local optimality of the search |
| 394 |
* direction with at least as good progress as the locally optimal steepest |
| 395 |
* descent method. |
| 396 |
*/ |
| 397 |
template <typename VectorType = Vector<double>> |
| 398 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 399 |
class SolverFlexibleCG : public SolverCG<VectorType> |
| 400 |
{ |
| 401 |
public: |
| 402 |
/** |
| 403 |
* Declare type for container size. |
| 404 |
*/ |
| 405 |
using size_type = types::global_dof_index; |
| 406 |
|
| 407 |
|
| 408 |
/** |
| 409 |
* Standardized data struct to pipe additional data to the solver. |
| 410 |
*/ |
| 411 |
struct AdditionalData |
| 412 |
{ |
| 413 |
/** |
| 414 |
* Constructor. By default, set the residual of the stopping criterion |
| 415 |
* to the implicit residual. A <tt>true</tt> value of |
| 416 |
* AdditionalData::use_default_residual refers to the |
| 417 |
* implicit residual, while <tt>false</tt> reverts |
| 418 |
* it. The former uses the result of the matrix-vector |
| 419 |
* product already computed in other algorithm steps to derive the residual |
| 420 |
* by a mere vector update, whereas the latter explicitly calculates the |
| 421 |
* system residual with an additional matrix-vector product. More |
| 422 |
* information on explicit and implicit residual stopping criteria can be |
| 423 |
* found <a |
| 424 |
* href="https://en.wikipedia.org/wiki/Conjugate_gradient_method#Explicit_residual_calculation">link |
| 425 |
* here</a>. |
| 426 |
*/ |
| 427 |
explicit AdditionalData(const bool use_default_residual = true); |
| 428 |
|
| 429 |
/** |
| 430 |
* Flag for the default residual that is used to measure convergence. |
| 431 |
*/ |
| 432 |
bool use_default_residual; |
| 433 |
}; |
| 434 |
|
| 435 |
|
| 436 |
/** |
| 437 |
* Constructor. |
| 438 |
*/ |
| 439 |
SolverFlexibleCG(SolverControl &cn, |
| 440 |
VectorMemory<VectorType> &mem, |
| 441 |
const AdditionalData &data = AdditionalData()); |
| 442 |
|
| 443 |
/** |
| 444 |
* Constructor. Use an object of type GrowingVectorMemory as a default to |
| 445 |
* allocate memory. |
| 446 |
*/ |
| 447 |
SolverFlexibleCG(SolverControl &cn, |
| 448 |
const AdditionalData &data = AdditionalData()); |
| 449 |
|
| 450 |
protected: |
| 451 |
/** |
| 452 |
* Additional parameters. |
| 453 |
*/ |
| 454 |
AdditionalData additional_data; |
| 455 |
}; |
| 456 |
|
| 457 |
|
| 458 |
/** @} */ |
| 459 |
|
| 460 |
/*------------------------- Implementation ----------------------------*/ |
| 461 |
|
| 462 |
#ifndef DOXYGEN |
| 463 |
|
| 464 |
|
| 465 |
template <typename VectorType> |
| 466 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 467 |
inline SolverCG<VectorType>::AdditionalData::AdditionalData( |
| 468 |
const bool use_default_residual) |
| 469 |
: use_default_residual(use_default_residual) |
| 470 |
{} |
| 471 |
|
| 472 |
template <typename VectorType> |
| 473 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 474 |
SolverCG<VectorType>::SolverCG(SolverControl &cn, |
| 475 |
VectorMemory<VectorType> &mem, |
| 476 |
const AdditionalData &data) |
| 477 |
: SolverBase<VectorType>(cn, mem) |
| 478 |
, additional_data(data) |
| 479 |
, determine_beta_by_flexible_formula(false) |
| 480 |
{} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
template <typename VectorType> |
| 485 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 486 |
SolverCG<VectorType>::SolverCG(SolverControl &cn, const AdditionalData &data) |
| 487 |
: SolverBase<VectorType>(cn) |
| 488 |
, additional_data(data) |
| 489 |
, determine_beta_by_flexible_formula(false) |
| 490 |
{} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
template <typename VectorType> |
| 495 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 496 |
void SolverCG<VectorType>::print_vectors(const unsigned int, |
| 497 |
const VectorType &, |
| 498 |
const VectorType &, |
| 499 |
const VectorType &) const |
| 500 |
{} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
template <typename VectorType> |
| 505 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 506 |
inline void SolverCG<VectorType>::compute_eigs_and_cond( |
| 507 |
const std::vector<typename VectorType::value_type> &diagonal, |
| 508 |
const std::vector<typename VectorType::value_type> &offdiagonal, |
| 509 |
const boost::signals2::signal<void(const std::vector<double> &)> |
| 510 |
&eigenvalues_signal, |
| 511 |
const boost::signals2::signal<void(double)> &cond_signal) |
| 512 |
{ |
| 513 |
// Avoid computing eigenvalues unless they are needed. |
| 514 |
if (!cond_signal.empty() || !eigenvalues_signal.empty()) |
| 515 |
{ |
| 516 |
TridiagonalMatrix<typename VectorType::value_type> T(diagonal.size(), |
| 517 |
true); |
| 518 |
for (size_type i = 0; i < diagonal.size(); ++i) |
| 519 |
{ |
| 520 |
T(i, i) = diagonal[i]; |
| 521 |
if (i < diagonal.size() - 1) |
| 522 |
T(i, i + 1) = offdiagonal[i]; |
| 523 |
} |
| 524 |
T.compute_eigenvalues(); |
| 525 |
// Need two eigenvalues to estimate the condition number. |
| 526 |
if (diagonal.size() > 1) |
| 527 |
{ |
| 528 |
auto condition_number = T.eigenvalue(T.n() - 1) / T.eigenvalue(0); |
| 529 |
// Condition number is real valued and nonnegative; simply take |
| 530 |
// the absolute value: |
| 531 |
cond_signal(std::abs(condition_number)); |
| 532 |
} |
| 533 |
// Avoid copying the eigenvalues of T to a vector unless a signal is |
| 534 |
// connected. |
| 535 |
if (!eigenvalues_signal.empty()) |
| 536 |
{ |
| 537 |
std::vector<double> eigenvalues(T.n()); |
| 538 |
for (unsigned int j = 0; j < T.n(); ++j) |
| 539 |
{ |
| 540 |
// for a hermitian matrix, all eigenvalues are real-valued |
| 541 |
// and non-negative, simply return the absolute value: |
| 542 |
eigenvalues[j] = std::abs(T.eigenvalue(j)); |
| 543 |
} |
| 544 |
eigenvalues_signal(eigenvalues); |
| 545 |
} |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
namespace internal |
| 552 |
{ |
| 553 |
|
| 554 |
namespace SolverCG |
| 555 |
{ |
| 556 |
// This base class is used to select different variants of the conjugate |
| 557 |
// gradient solver. The default variant is used for standard matrix and |
| 558 |
// preconditioner arguments, as provided by the derived class |
| 559 |
// IterationWork below, but there is also a specialized variant further |
| 560 |
// down that uses SFINAE to identify whether matrices and preconditioners |
| 561 |
// support special operations on sub-ranges of the vectors. |
| 562 |
template <typename VectorType, |
| 563 |
typename MatrixType, |
| 564 |
typename PreconditionerType> |
| 565 |
struct IterationWorkerBase |
| 566 |
{ |
| 567 |
using Number = typename VectorType::value_type; |
| 568 |
|
| 569 |
const MatrixType &A; |
| 570 |
const PreconditionerType &preconditioner; |
| 571 |
const bool flexible; |
| 572 |
VectorType &x; |
| 573 |
const VectorType &b; |
| 574 |
|
| 575 |
typename VectorMemory<VectorType>::Pointer r_pointer; |
| 576 |
typename VectorMemory<VectorType>::Pointer p_pointer; |
| 577 |
typename VectorMemory<VectorType>::Pointer v_pointer; |
| 578 |
typename VectorMemory<VectorType>::Pointer z_pointer; |
| 579 |
typename VectorMemory<VectorType>::Pointer explicit_r_pointer; |
| 580 |
|
| 581 |
// Define some aliases for simpler access, using the variables 'r' for |
| 582 |
// the residual b - A*x, 'p' for the search direction, and 'v' for the |
| 583 |
// auxiliary vector. This naming convention is used e.g. by the |
| 584 |
// description on |
| 585 |
// https://en.wikipedia.org/wiki/Conjugate_gradient_method. The variable |
| 586 |
// 'z' gets only used for the flexible variant of the CG method. |
| 587 |
VectorType &r; |
| 588 |
VectorType &p; |
| 589 |
VectorType &v; |
| 590 |
VectorType &z; |
| 591 |
VectorType &explicit_r; |
| 592 |
|
| 593 |
Number r_dot_preconditioner_dot_r; |
| 594 |
Number alpha; |
| 595 |
Number beta; |
| 596 |
double residual_norm; |
| 597 |
Number previous_alpha; |
| 598 |
const bool use_default_residual; |
| 599 |
|
| 600 |
|
| 601 |
IterationWorkerBase(const MatrixType &A, |
| 602 |
const PreconditionerType &preconditioner, |
| 603 |
const bool flexible, |
| 604 |
VectorMemory<VectorType> &memory, |
| 605 |
VectorType &x, |
| 606 |
const VectorType &b, |
| 607 |
const bool &use_default_residual) |
| 608 |
: A(A) |
| 609 |
, preconditioner(preconditioner) |
| 610 |
, flexible(flexible) |
| 611 |
, x(x) |
| 612 |
, b(b) |
| 613 |
, r_pointer(memory) |
| 614 |
, p_pointer(memory) |
| 615 |
, v_pointer(memory) |
| 616 |
, z_pointer(memory) |
| 617 |
, explicit_r_pointer(memory) |
| 618 |
, r(*r_pointer) |
| 619 |
, p(*p_pointer) |
| 620 |
, v(*v_pointer) |
| 621 |
, z(*z_pointer) |
| 622 |
, explicit_r(*explicit_r_pointer) |
| 623 |
, r_dot_preconditioner_dot_r(Number()) |
| 624 |
, alpha(Number()) |
| 625 |
, beta(Number()) |
| 626 |
, residual_norm(0.0) |
| 627 |
, previous_alpha(Number()) |
| 628 |
, use_default_residual(use_default_residual) |
| 629 |
{} |
| 630 |
|
| 631 |
void |
| 632 |
startup() |
| 633 |
{ |
| 634 |
// Initialize without setting the vector entries, as those would soon |
| 635 |
// be overwritten anyway |
| 636 |
r.reinit(x, true); |
| 637 |
p.reinit(x, true); |
| 638 |
v.reinit(x, true); |
| 639 |
if (flexible) |
| 640 |
z.reinit(x, true); |
| 641 |
if (!use_default_residual) |
| 642 |
explicit_r.reinit(x, true); |
| 643 |
|
| 644 |
// compute residual. if vector is zero, then short-circuit the full |
| 645 |
// computation |
| 646 |
if (!x.all_zero()) |
| 647 |
{ |
| 648 |
A.vmult(r, x); |
| 649 |
r.sadd(-1., 1., b); |
| 650 |
} |
| 651 |
else |
| 652 |
r.equ(1., b); |
| 653 |
|
| 654 |
residual_norm = r.l2_norm(); |
| 655 |
} |
| 656 |
}; |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
// Implementation of a conjugate gradient operation with matrices and |
| 661 |
// preconditioners without special capabilities |
| 662 |
template <typename VectorType, |
| 663 |
typename MatrixType, |
| 664 |
typename PreconditionerType, |
| 665 |
typename = int> |
| 666 |
struct IterationWorker |
| 667 |
: public IterationWorkerBase<VectorType, MatrixType, PreconditionerType> |
| 668 |
{ |
| 669 |
using BaseClass = |
| 670 |
IterationWorkerBase<VectorType, MatrixType, PreconditionerType>; |
| 671 |
|
| 672 |
|
| 673 |
IterationWorker(const MatrixType &A, |
| 674 |
const PreconditionerType &preconditioner, |
| 675 |
const bool flexible, |
| 676 |
VectorMemory<VectorType> &memory, |
| 677 |
VectorType &x, |
| 678 |
const VectorType &b, |
| 679 |
const bool &use_default_residual) |
| 680 |
: BaseClass(A, |
| 681 |
preconditioner, |
| 682 |
flexible, |
| 683 |
memory, |
| 684 |
x, |
| 685 |
b, |
| 686 |
use_default_residual) |
| 687 |
{} |
| 688 |
|
| 689 |
using BaseClass::A; |
| 690 |
using BaseClass::alpha; |
| 691 |
using BaseClass::b; |
| 692 |
using BaseClass::beta; |
| 693 |
using BaseClass::explicit_r; |
| 694 |
using BaseClass::p; |
| 695 |
using BaseClass::preconditioner; |
| 696 |
using BaseClass::r; |
| 697 |
using BaseClass::r_dot_preconditioner_dot_r; |
| 698 |
using BaseClass::residual_norm; |
| 699 |
using BaseClass::use_default_residual; |
| 700 |
using BaseClass::v; |
| 701 |
using BaseClass::x; |
| 702 |
using BaseClass::z; |
| 703 |
|
| 704 |
void |
| 705 |
do_iteration(const unsigned int iteration_index) |
| 706 |
{ |
| 707 |
using Number = typename VectorType::value_type; |
| 708 |
|
| 709 |
const Number previous_r_dot_preconditioner_dot_r = |
| 710 |
r_dot_preconditioner_dot_r; |
| 711 |
|
| 712 |
if (std::is_same_v<PreconditionerType, PreconditionIdentity> == false) |
| 713 |
{ |
| 714 |
preconditioner.vmult(v, r); |
| 715 |
r_dot_preconditioner_dot_r = r * v; |
| 716 |
} |
| 717 |
else |
| 718 |
r_dot_preconditioner_dot_r = residual_norm * residual_norm; |
| 719 |
|
| 720 |
const VectorType &direction = |
| 721 |
std::is_same_v<PreconditionerType, PreconditionIdentity> ? r : v; |
| 722 |
|
| 723 |
if (iteration_index > 1) |
| 724 |
{ |
| 725 |
Assert(std::abs(previous_r_dot_preconditioner_dot_r) != 0., |
| 726 |
ExcDivideByZero()); |
| 727 |
beta = |
| 728 |
r_dot_preconditioner_dot_r / previous_r_dot_preconditioner_dot_r; |
| 729 |
if (this->flexible) |
| 730 |
beta -= (r * z) / previous_r_dot_preconditioner_dot_r; |
| 731 |
p.sadd(beta, 1., direction); |
| 732 |
} |
| 733 |
else |
| 734 |
p.equ(1., direction); |
| 735 |
|
| 736 |
if (this->flexible) |
| 737 |
z.swap(v); |
| 738 |
|
| 739 |
A.vmult(v, p); |
| 740 |
|
| 741 |
const Number p_dot_A_dot_p = p * v; |
| 742 |
Assert(std::abs(p_dot_A_dot_p) != 0., ExcDivideByZero()); |
| 743 |
|
| 744 |
this->previous_alpha = alpha; |
| 745 |
alpha = r_dot_preconditioner_dot_r / p_dot_A_dot_p; |
| 746 |
|
| 747 |
x.add(alpha, p); |
| 748 |
|
| 749 |
// compute the residual norm with implicit residual |
| 750 |
if (use_default_residual) |
| 751 |
{ |
| 752 |
residual_norm = std::sqrt(std::abs(r.add_and_dot(-alpha, v, r))); |
| 753 |
} |
| 754 |
// compute the residual norm with the explicit residual, i.e. |
| 755 |
// compute l2 norm of Ax - b. |
| 756 |
else |
| 757 |
{ |
| 758 |
// compute the residual conjugate gradient update |
| 759 |
r.add(-alpha, v); |
| 760 |
// compute explicit residual |
| 761 |
A.vmult(explicit_r, x); |
| 762 |
explicit_r.add(-1, b); |
| 763 |
residual_norm = explicit_r.l2_norm(); |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
void |
| 768 |
finalize_after_convergence(const unsigned int) |
| 769 |
{} |
| 770 |
}; |
| 771 |
|
| 772 |
|
| 773 |
// In the following, we provide a specialization of the above |
| 774 |
// IterationWorker class that picks up particular features in the matrix |
| 775 |
// and preconditioners. |
| 776 |
|
| 777 |
// a helper type-trait that leverage SFINAE to figure out if MatrixType has |
| 778 |
// ... MatrixType::vmult(VectorType &, const VectorType&, |
| 779 |
// std::function<...>, std::function<...>) const |
| 780 |
template <typename MatrixType, typename VectorType> |
| 781 |
using vmult_functions_t = decltype(std::declval<const MatrixType>().vmult( |
| 782 |
std::declval<VectorType &>(), |
| 783 |
std::declval<const VectorType &>(), |
| 784 |
std::declval< |
| 785 |
const std::function<void(const unsigned int, const unsigned int)> &>(), |
| 786 |
std::declval<const std::function<void(const unsigned int, |
| 787 |
const unsigned int)> &>())); |
| 788 |
|
| 789 |
template <typename MatrixType, typename VectorType> |
| 790 |
constexpr bool has_vmult_functions = |
| 791 |
is_supported_operation<vmult_functions_t, MatrixType, VectorType>; |
| 792 |
|
| 793 |
// a helper type-trait that leverage SFINAE to figure out if |
| 794 |
// PreconditionerType has ... PreconditionerType::apply_to_subrange(const |
| 795 |
// unsigned int, const unsigned int, const Number*, Number*) const |
| 796 |
template <typename PreconditionerType> |
| 797 |
using apply_to_subrange_t = |
| 798 |
decltype(std::declval<const PreconditionerType>() |
| 799 |
.apply_to_subrange(0U, 0U, nullptr, nullptr)); |
| 800 |
|
| 801 |
template <typename PreconditionerType> |
| 802 |
constexpr bool has_apply_to_subrange = |
| 803 |
is_supported_operation<apply_to_subrange_t, PreconditionerType>; |
| 804 |
|
| 805 |
// a helper type-trait that leverage SFINAE to figure out if |
| 806 |
// PreconditionerType has ... PreconditionerType::apply(const |
| 807 |
// unsigned int, const Number) const |
| 808 |
template <typename PreconditionerType> |
| 809 |
using apply_t = |
| 810 |
decltype(std::declval<const PreconditionerType>().apply(0U, 0.0)); |
| 811 |
|
| 812 |
template <typename PreconditionerType> |
| 813 |
constexpr bool has_apply = |
| 814 |
is_supported_operation<apply_t, PreconditionerType>; |
| 815 |
|
| 816 |
|
| 817 |
// Internal function to run one iteration of the conjugate gradient solver |
| 818 |
// for matrices and preconditioners that support interleaving the vector |
| 819 |
// updates with the matrix-vector product. |
| 820 |
template <typename VectorType, |
| 821 |
typename MatrixType, |
| 822 |
typename PreconditionerType> |
| 823 |
struct IterationWorker< |
| 824 |
VectorType, |
| 825 |
MatrixType, |
| 826 |
PreconditionerType, |
| 827 |
std::enable_if_t<has_vmult_functions<MatrixType, VectorType> && |
| 828 |
(has_apply_to_subrange<PreconditionerType> || |
| 829 |
has_apply<PreconditionerType>)&&std:: |
| 830 |
is_same_v<VectorType, |
| 831 |
LinearAlgebra::distributed::Vector< |
| 832 |
typename VectorType::value_type, |
| 833 |
MemorySpace::Host>>, |
| 834 |
int>> |
| 835 |
: public IterationWorkerBase<VectorType, MatrixType, PreconditionerType> |
| 836 |
{ |
| 837 |
using Number = typename VectorType::value_type; |
| 838 |
|
| 839 |
Number next_r_dot_preconditioner_dot_r; |
| 840 |
Number previous_beta; |
| 841 |
|
| 842 |
IterationWorker(const MatrixType &A, |
| 843 |
const PreconditionerType &preconditioner, |
| 844 |
const bool flexible, |
| 845 |
VectorMemory<VectorType> &memory, |
| 846 |
VectorType &x, |
| 847 |
const VectorType &b, |
| 848 |
const bool &use_default_residual) |
| 849 |
: IterationWorkerBase<VectorType, MatrixType, PreconditionerType>( |
| 850 |
A, |
| 851 |
preconditioner, |
| 852 |
flexible, |
| 853 |
memory, |
| 854 |
x, |
| 855 |
b, |
| 856 |
use_default_residual) |
| 857 |
, next_r_dot_preconditioner_dot_r(0.) |
| 858 |
, previous_beta(0.) |
| 859 |
{} |
| 860 |
|
| 861 |
// This is the main iteration function, that will use some of the |
| 862 |
// specialized functions below |
| 863 |
void |
| 864 |
do_iteration(const unsigned int iteration_index) |
| 865 |
{ |
| 866 |
if (iteration_index > 1) |
| 867 |
{ |
| 868 |
previous_beta = this->beta; |
| 869 |
this->beta = next_r_dot_preconditioner_dot_r / |
| 870 |
this->r_dot_preconditioner_dot_r; |
| 871 |
} |
| 872 |
|
| 873 |
std::array<VectorizedArray<Number>, 7> vectorized_sums = {}; |
| 874 |
|
| 875 |
this->A.vmult( |
| 876 |
this->v, |
| 877 |
this->p, |
| 878 |
[&](const unsigned int begin, const unsigned int end) { |
| 879 |
operation_before_loop(iteration_index, begin, end); |
| 880 |
}, |
| 881 |
[&](const unsigned int begin, const unsigned int end) { |
| 882 |
operation_after_loop(begin, end, vectorized_sums); |
| 883 |
}); |
| 884 |
|
| 885 |
std::array<Number, 7> scalar_sums; |
| 886 |
for (unsigned int i = 0; i < 7; ++i) |
| 887 |
scalar_sums[i] = vectorized_sums[i][0]; |
| 888 |
for (unsigned int l = 1; l < VectorizedArray<Number>::size(); ++l) |
| 889 |
for (unsigned int i = 0; i < 7; ++i) |
| 890 |
scalar_sums[i] += vectorized_sums[i][l]; |
| 891 |
|
| 892 |
Utilities::MPI::sum(dealii::ArrayView<const Number>(scalar_sums.data(), |
| 893 |
7), |
| 894 |
this->r.get_mpi_communicator(), |
| 895 |
dealii::ArrayView<Number>(scalar_sums.data(), 7)); |
| 896 |
|
| 897 |
this->r_dot_preconditioner_dot_r = scalar_sums[6]; |
| 898 |
|
| 899 |
const Number p_dot_A_dot_p = scalar_sums[0]; |
| 900 |
Assert(std::abs(p_dot_A_dot_p) != 0., ExcDivideByZero()); |
| 901 |
|
| 902 |
this->previous_alpha = this->alpha; |
| 903 |
this->alpha = this->r_dot_preconditioner_dot_r / p_dot_A_dot_p; |
| 904 |
|
| 905 |
// Round-off errors near zero might yield negative values, so take |
| 906 |
// the absolute value in the next two formulas |
| 907 |
this->residual_norm = std::sqrt(std::abs( |
| 908 |
scalar_sums[3] + |
| 909 |
this->alpha * (-2. * scalar_sums[2] + this->alpha * scalar_sums[1]))); |
| 910 |
|
| 911 |
next_r_dot_preconditioner_dot_r = std::abs( |
| 912 |
this->r_dot_preconditioner_dot_r + |
| 913 |
this->alpha * (-2. * scalar_sums[4] + this->alpha * scalar_sums[5])); |
| 914 |
} |
| 915 |
|
| 916 |
// Function that we use if the PreconditionerType implements an apply() |
| 917 |
// function |
| 918 |
template <typename U = void> |
| 919 |
std::enable_if_t<has_apply<PreconditionerType>, U> |
| 920 |
operation_before_loop(const unsigned int iteration_index, |
| 921 |
const unsigned int start_range, |
| 922 |
const unsigned int end_range) const |
| 923 |
{ |
| 924 |
Number *x = this->x.begin(); |
| 925 |
Number *r = this->r.begin(); |
| 926 |
Number *p = this->p.begin(); |
| 927 |
Number *v = this->v.begin(); |
| 928 |
const Number alpha = this->alpha; |
| 929 |
const Number beta = this->beta; |
| 930 |
constexpr unsigned int n_lanes = VectorizedArray<Number>::size(); |
| 931 |
const unsigned int end_regular = |
| 932 |
start_range + (end_range - start_range) / n_lanes * n_lanes; |
| 933 |
if (iteration_index == 1) |
| 934 |
{ |
| 935 |
// Vectorize by hand since compilers are often pretty bad at |
| 936 |
// doing these steps automatically even with |
| 937 |
// DEAL_II_OPENMP_SIMD_PRAGMA |
| 938 |
for (unsigned int j = start_range; j < end_regular; j += n_lanes) |
| 939 |
{ |
| 940 |
VectorizedArray<Number> rj, pj; |
| 941 |
rj.load(r + j); |
| 942 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 943 |
for (unsigned int l = 0; l < n_lanes; ++l) |
| 944 |
pj[l] = this->preconditioner.apply(j + l, rj[l]); |
| 945 |
pj.store(p + j); |
| 946 |
rj = VectorizedArray<Number>(); |
| 947 |
rj.store(v + j); |
| 948 |
} |
| 949 |
for (unsigned int j = end_regular; j < end_range; ++j) |
| 950 |
{ |
| 951 |
p[j] = this->preconditioner.apply(j, r[j]); |
| 952 |
v[j] = Number(); |
| 953 |
} |
| 954 |
} |
| 955 |
else if (iteration_index % 2 == 0 && beta != Number()) |
| 956 |
{ |
| 957 |
for (unsigned int j = start_range; j < end_regular; j += n_lanes) |
| 958 |
{ |
| 959 |
VectorizedArray<Number> rj, vj, pj, prec_rj; |
| 960 |
rj.load(r + j); |
| 961 |
vj.load(v + j); |
| 962 |
rj -= alpha * vj; |
| 963 |
rj.store(r + j); |
| 964 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 965 |
for (unsigned int l = 0; l < n_lanes; ++l) |
| 966 |
prec_rj[l] = this->preconditioner.apply(j + l, rj[l]); |
| 967 |
pj.load(p + j); |
| 968 |
pj = beta * pj + prec_rj; |
| 969 |
pj.store(p + j); |
| 970 |
rj = VectorizedArray<Number>(); |
| 971 |
rj.store(v + j); |
| 972 |
} |
| 973 |
for (unsigned int j = end_regular; j < end_range; ++j) |
| 974 |
{ |
| 975 |
r[j] -= alpha * v[j]; |
| 976 |
p[j] = beta * p[j] + this->preconditioner.apply(j, r[j]); |
| 977 |
v[j] = Number(); |
| 978 |
} |
| 979 |
} |
| 980 |
else if (iteration_index % 2 == 0 && beta == Number()) |
| 981 |
{ |
| 982 |
// Case where beta is zero: we cannot reconstruct p_{j-1} in the |
| 983 |
// next iteration, and must hence compute x here. This can happen |
| 984 |
// before termination |
| 985 |
for (unsigned int j = start_range; j < end_regular; j += n_lanes) |
| 986 |
{ |
| 987 |
VectorizedArray<Number> rj, xj, vj, pj, prec_rj; |
| 988 |
rj.load(r + j); |
| 989 |
vj.load(v + j); |
| 990 |
xj.load(x + j); |
| 991 |
pj.load(p + j); |
| 992 |
xj += alpha * pj; |
| 993 |
xj.store(x + j); |
| 994 |
rj -= alpha * vj; |
| 995 |
rj.store(r + j); |
| 996 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 997 |
for (unsigned int l = 0; l < n_lanes; ++l) |
| 998 |
prec_rj[l] = this->preconditioner.apply(j + l, rj[l]); |
| 999 |
prec_rj.store(p + j); |
| 1000 |
rj = VectorizedArray<Number>(); |
| 1001 |
rj.store(v + j); |
| 1002 |
} |
| 1003 |
for (unsigned int j = end_regular; j < end_range; ++j) |
| 1004 |
{ |
| 1005 |
r[j] -= alpha * v[j]; |
| 1006 |
x[j] += alpha * p[j]; |
| 1007 |
p[j] = this->preconditioner.apply(j, r[j]); |
| 1008 |
v[j] = Number(); |
| 1009 |
} |
| 1010 |
} |
| 1011 |
else |
| 1012 |
{ |
| 1013 |
const Number alpha_plus_previous_alpha_over_beta = |
| 1014 |
alpha + this->previous_alpha / this->previous_beta; |
| 1015 |
const Number previous_alpha_over_beta = |
| 1016 |
this->previous_alpha / this->previous_beta; |
| 1017 |
for (unsigned int j = start_range; j < end_regular; j += n_lanes) |
| 1018 |
{ |
| 1019 |
VectorizedArray<Number> rj, vj, pj, xj, prec_rj, prec_vj; |
| 1020 |
xj.load(x + j); |
| 1021 |
pj.load(p + j); |
| 1022 |
xj += alpha_plus_previous_alpha_over_beta * pj; |
| 1023 |
rj.load(r + j); |
| 1024 |
vj.load(v + j); |
| 1025 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1026 |
for (unsigned int l = 0; l < n_lanes; ++l) |
| 1027 |
{ |
| 1028 |
prec_rj[l] = this->preconditioner.apply(j + l, rj[l]); |
| 1029 |
prec_vj[l] = this->preconditioner.apply(j + l, vj[l]); |
| 1030 |
} |
| 1031 |
xj -= previous_alpha_over_beta * prec_rj; |
| 1032 |
xj.store(x + j); |
| 1033 |
rj -= alpha * vj; |
| 1034 |
rj.store(r + j); |
| 1035 |
prec_rj -= alpha * prec_vj; |
| 1036 |
pj = beta * pj + prec_rj; |
| 1037 |
pj.store(p + j); |
| 1038 |
rj = VectorizedArray<Number>(); |
| 1039 |
rj.store(v + j); |
| 1040 |
} |
| 1041 |
for (unsigned int j = end_regular; j < end_range; ++j) |
| 1042 |
{ |
| 1043 |
x[j] += alpha_plus_previous_alpha_over_beta * p[j]; |
| 1044 |
x[j] -= previous_alpha_over_beta * |
| 1045 |
this->preconditioner.apply(j, r[j]); |
| 1046 |
r[j] -= alpha * v[j]; |
| 1047 |
p[j] = beta * p[j] + this->preconditioner.apply(j, r[j]); |
| 1048 |
v[j] = Number(); |
| 1049 |
} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
|
| 1053 |
// Function that we use if the PreconditionerType implements an apply() |
| 1054 |
// function |
| 1055 |
template <typename U = void> |
| 1056 |
std::enable_if_t<has_apply<PreconditionerType>, U> |
| 1057 |
operation_after_loop( |
| 1058 |
const unsigned int start_range, |
| 1059 |
const unsigned int end_range, |
| 1060 |
std::array<VectorizedArray<Number>, 7> &vectorized_sums) const |
| 1061 |
{ |
| 1062 |
const Number *r = this->r.begin(); |
| 1063 |
const Number *p = this->p.begin(); |
| 1064 |
const Number *v = this->v.begin(); |
| 1065 |
std::array<VectorizedArray<Number>, 7> my_sums = {}; |
| 1066 |
constexpr unsigned int n_lanes = VectorizedArray<Number>::size(); |
| 1067 |
const unsigned int end_regular = |
| 1068 |
start_range + (end_range - start_range) / n_lanes * n_lanes; |
| 1069 |
for (unsigned int j = start_range; j < end_regular; j += n_lanes) |
| 1070 |
{ |
| 1071 |
VectorizedArray<Number> pj, vj, rj, prec_vj, prec_rj; |
| 1072 |
pj.load(p + j); |
| 1073 |
vj.load(v + j); |
| 1074 |
rj.load(r + j); |
| 1075 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1076 |
for (unsigned int l = 0; l < n_lanes; ++l) |
| 1077 |
{ |
| 1078 |
prec_vj[l] = this->preconditioner.apply(j + l, vj[l]); |
| 1079 |
prec_rj[l] = this->preconditioner.apply(j + l, rj[l]); |
| 1080 |
} |
| 1081 |
my_sums[0] += pj * vj; |
| 1082 |
my_sums[1] += vj * vj; |
| 1083 |
my_sums[2] += rj * vj; |
| 1084 |
my_sums[3] += rj * rj; |
| 1085 |
my_sums[4] += rj * prec_vj; |
| 1086 |
my_sums[5] += vj * prec_vj; |
| 1087 |
my_sums[6] += rj * prec_rj; |
| 1088 |
} |
| 1089 |
for (unsigned int j = end_regular; j < end_range; ++j) |
| 1090 |
{ |
| 1091 |
const Number prec_v = this->preconditioner.apply(j, v[j]); |
| 1092 |
const Number prec_r = this->preconditioner.apply(j, r[j]); |
| 1093 |
my_sums[0][0] += p[j] * v[j]; |
| 1094 |
my_sums[1][0] += v[j] * v[j]; |
| 1095 |
my_sums[2][0] += r[j] * v[j]; |
| 1096 |
my_sums[3][0] += r[j] * r[j]; |
| 1097 |
my_sums[4][0] += r[j] * prec_v; |
| 1098 |
my_sums[5][0] += v[j] * prec_v; |
| 1099 |
my_sums[6][0] += r[j] * prec_r; |
| 1100 |
} |
| 1101 |
for (unsigned int i = 0; i < vectorized_sums.size(); ++i) |
| 1102 |
vectorized_sums[i] += my_sums[i]; |
| 1103 |
} |
| 1104 |
|
| 1105 |
// Function that we use if the PreconditionerType implements an apply() |
| 1106 |
// function |
| 1107 |
template <typename U = void> |
| 1108 |
std::enable_if_t<has_apply<PreconditionerType>, U> |
| 1109 |
finalize_after_convergence(const unsigned int iteration_index) |
| 1110 |
{ |
| 1111 |
if (iteration_index % 2 == 1 || this->beta == Number()) |
| 1112 |
this->x.add(this->alpha, this->p); |
| 1113 |
else |
| 1114 |
{ |
| 1115 |
using Number = typename VectorType::value_type; |
| 1116 |
const unsigned int end_range = this->x.locally_owned_size(); |
| 1117 |
|
| 1118 |
Number *x = this->x.begin(); |
| 1119 |
Number *r = this->r.begin(); |
| 1120 |
Number *p = this->p.begin(); |
| 1121 |
|
| 1122 |
// Note that we use 'beta' here rather than 'previous_beta' in the |
| 1123 |
// formula above, which is because the shift in beta -> |
| 1124 |
// previous_beta has not been applied at this stage, allowing us |
| 1125 |
// to recover the previous search direction |
| 1126 |
const Number alpha_plus_previous_alpha_over_beta = |
| 1127 |
this->alpha + this->previous_alpha / this->beta; |
| 1128 |
const Number previous_alpha_over_beta = |
| 1129 |
this->previous_alpha / this->beta; |
| 1130 |
|
| 1131 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1132 |
for (unsigned int j = 0; j < end_range; ++j) |
| 1133 |
{ |
| 1134 |
x[j] += alpha_plus_previous_alpha_over_beta * p[j] - |
| 1135 |
previous_alpha_over_beta * |
| 1136 |
this->preconditioner.apply(j, r[j]); |
| 1137 |
} |
| 1138 |
} |
| 1139 |
} |
| 1140 |
|
| 1141 |
// Function that we use if the PreconditionerType does not implement an |
| 1142 |
// apply() function, where we instead need to choose the |
| 1143 |
// apply_to_subrange function |
| 1144 |
template <typename U = void> |
| 1145 |
std::enable_if_t<!has_apply<PreconditionerType>, U> |
| 1146 |
operation_before_loop(const unsigned int iteration_index, |
| 1147 |
const unsigned int start_range, |
| 1148 |
const unsigned int end_range) const |
| 1149 |
{ |
| 1150 |
Number *x = this->x.begin() + start_range; |
| 1151 |
Number *r = this->r.begin() + start_range; |
| 1152 |
Number *p = this->p.begin() + start_range; |
| 1153 |
Number *v = this->v.begin() + start_range; |
| 1154 |
const Number alpha = this->alpha; |
| 1155 |
const Number beta = this->beta; |
| 1156 |
constexpr unsigned int grain_size = 128; |
| 1157 |
std::array<Number, grain_size> prec_r; |
| 1158 |
if (iteration_index == 1) |
| 1159 |
{ |
| 1160 |
for (unsigned int j = start_range; j < end_range; j += grain_size) |
| 1161 |
{ |
| 1162 |
const unsigned int length = std::min(grain_size, end_range - j); |
| 1163 |
this->preconditioner.apply_to_subrange(j, |
| 1164 |
j + length, |
| 1165 |
r, |
| 1166 |
prec_r.data()); |
| 1167 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1168 |
for (unsigned int i = 0; i < length; ++i) |
| 1169 |
{ |
| 1170 |
p[i] = prec_r[i]; |
| 1171 |
v[i] = Number(); |
| 1172 |
} |
| 1173 |
p += length; |
| 1174 |
r += length; |
| 1175 |
v += length; |
| 1176 |
} |
| 1177 |
} |
| 1178 |
else if (iteration_index % 2 == 0 && beta != Number()) |
| 1179 |
{ |
| 1180 |
for (unsigned int j = start_range; j < end_range; j += grain_size) |
| 1181 |
{ |
| 1182 |
const unsigned int length = std::min(grain_size, end_range - j); |
| 1183 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1184 |
for (unsigned int i = 0; i < length; ++i) |
| 1185 |
r[i] -= this->alpha * v[i]; |
| 1186 |
this->preconditioner.apply_to_subrange(j, |
| 1187 |
j + length, |
| 1188 |
r, |
| 1189 |
prec_r.data()); |
| 1190 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1191 |
for (unsigned int i = 0; i < length; ++i) |
| 1192 |
{ |
| 1193 |
p[i] = this->beta * p[i] + prec_r[i]; |
| 1194 |
v[i] = Number(); |
| 1195 |
} |
| 1196 |
p += length; |
| 1197 |
r += length; |
| 1198 |
v += length; |
| 1199 |
} |
| 1200 |
} |
| 1201 |
else if (iteration_index % 2 == 0 && beta == Number()) |
| 1202 |
{ |
| 1203 |
// Case where beta is zero: We cannot reconstruct p_{j-1} in the |
| 1204 |
// next iteration, and must hence compute x here. This can happen |
| 1205 |
// before termination |
| 1206 |
for (unsigned int j = start_range; j < end_range; j += grain_size) |
| 1207 |
{ |
| 1208 |
const unsigned int length = std::min(grain_size, end_range - j); |
| 1209 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1210 |
for (unsigned int i = 0; i < length; ++i) |
| 1211 |
r[i] -= this->alpha * v[i]; |
| 1212 |
this->preconditioner.apply_to_subrange(j, |
| 1213 |
j + length, |
| 1214 |
r, |
| 1215 |
prec_r.data()); |
| 1216 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1217 |
for (unsigned int i = 0; i < length; ++i) |
| 1218 |
{ |
| 1219 |
x[i] += this->alpha * p[i]; |
| 1220 |
p[i] = prec_r[i]; |
| 1221 |
v[i] = Number(); |
| 1222 |
} |
| 1223 |
p += length; |
| 1224 |
r += length; |
| 1225 |
v += length; |
| 1226 |
} |
| 1227 |
} |
| 1228 |
else |
| 1229 |
{ |
| 1230 |
const Number alpha_plus_previous_alpha_over_beta = |
| 1231 |
this->alpha + this->previous_alpha / this->previous_beta; |
| 1232 |
const Number previous_alpha_over_beta = |
| 1233 |
this->previous_alpha / this->previous_beta; |
| 1234 |
for (unsigned int j = start_range; j < end_range; j += grain_size) |
| 1235 |
{ |
| 1236 |
const unsigned int length = std::min(grain_size, end_range - j); |
| 1237 |
this->preconditioner.apply_to_subrange(j, |
| 1238 |
j + length, |
| 1239 |
r, |
| 1240 |
prec_r.data()); |
| 1241 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1242 |
for (unsigned int i = 0; i < length; ++i) |
| 1243 |
{ |
| 1244 |
x[i] += alpha_plus_previous_alpha_over_beta * p[i] - |
| 1245 |
previous_alpha_over_beta * prec_r[i]; |
| 1246 |
r[i] -= this->alpha * v[i]; |
| 1247 |
} |
| 1248 |
this->preconditioner.apply_to_subrange(j, |
| 1249 |
j + length, |
| 1250 |
r, |
| 1251 |
prec_r.data()); |
| 1252 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1253 |
for (unsigned int i = 0; i < length; ++i) |
| 1254 |
{ |
| 1255 |
p[i] = this->beta * p[i] + prec_r[i]; |
| 1256 |
v[i] = Number(); |
| 1257 |
} |
| 1258 |
p += length; |
| 1259 |
r += length; |
| 1260 |
v += length; |
| 1261 |
x += length; |
| 1262 |
} |
| 1263 |
} |
| 1264 |
} |
| 1265 |
|
| 1266 |
// Function that we use if the PreconditionerType does not implement an |
| 1267 |
// apply() function and where we instead need to use the |
| 1268 |
// apply_to_subrange function |
| 1269 |
template <typename U = void> |
| 1270 |
std::enable_if_t<!has_apply<PreconditionerType>, U> |
| 1271 |
operation_after_loop( |
| 1272 |
const unsigned int start_range, |
| 1273 |
const unsigned int end_range, |
| 1274 |
std::array<VectorizedArray<Number>, 7> &vectorized_sums) const |
| 1275 |
{ |
| 1276 |
const Number *r = this->r.begin(); |
| 1277 |
const Number *p = this->p.begin(); |
| 1278 |
const Number *v = this->v.begin(); |
| 1279 |
std::array<VectorizedArray<Number>, 7> my_sums = {}; |
| 1280 |
constexpr unsigned int grain_size = 128; |
| 1281 |
Assert(grain_size % VectorizedArray<Number>::size() == 0, |
| 1282 |
ExcNotImplemented()); |
| 1283 |
const unsigned int end_regular = |
| 1284 |
start_range + (end_range - start_range) / grain_size * grain_size; |
| 1285 |
std::array<Number, grain_size> prec_r; |
| 1286 |
std::array<Number, grain_size> prec_v; |
| 1287 |
for (unsigned int j = start_range; j < end_regular; j += grain_size) |
| 1288 |
{ |
| 1289 |
this->preconditioner.apply_to_subrange(j, |
| 1290 |
j + grain_size, |
| 1291 |
r + j, |
| 1292 |
prec_r.data()); |
| 1293 |
this->preconditioner.apply_to_subrange(j, |
| 1294 |
j + grain_size, |
| 1295 |
v + j, |
| 1296 |
prec_v.data()); |
| 1297 |
VectorizedArray<Number> pj, vj, rj, prec_vj, prec_rj; |
| 1298 |
for (unsigned int i = 0; i < grain_size; |
| 1299 |
i += VectorizedArray<Number>::size()) |
| 1300 |
{ |
| 1301 |
pj.load(p + j + i); |
| 1302 |
vj.load(v + j + i); |
| 1303 |
rj.load(r + j + i); |
| 1304 |
prec_rj.load(prec_r.data() + i); |
| 1305 |
prec_vj.load(prec_v.data() + i); |
| 1306 |
|
| 1307 |
my_sums[0] += pj * vj; |
| 1308 |
my_sums[1] += vj * vj; |
| 1309 |
my_sums[2] += rj * vj; |
| 1310 |
my_sums[3] += rj * rj; |
| 1311 |
my_sums[4] += rj * prec_vj; |
| 1312 |
my_sums[5] += vj * prec_vj; |
| 1313 |
my_sums[6] += rj * prec_rj; |
| 1314 |
} |
| 1315 |
} |
| 1316 |
const unsigned int length = end_range - end_regular; |
| 1317 |
AssertIndexRange(length, grain_size); |
| 1318 |
this->preconditioner.apply_to_subrange(end_regular, |
| 1319 |
end_regular + length, |
| 1320 |
r + end_regular, |
| 1321 |
prec_r.data()); |
| 1322 |
this->preconditioner.apply_to_subrange(end_regular, |
| 1323 |
end_regular + length, |
| 1324 |
v + end_regular, |
| 1325 |
prec_v.data()); |
| 1326 |
for (unsigned int j = end_regular; j < end_range; ++j) |
| 1327 |
{ |
| 1328 |
my_sums[0][0] += p[j] * v[j]; |
| 1329 |
my_sums[1][0] += v[j] * v[j]; |
| 1330 |
my_sums[2][0] += r[j] * v[j]; |
| 1331 |
my_sums[3][0] += r[j] * r[j]; |
| 1332 |
my_sums[4][0] += r[j] * prec_v[j - end_regular]; |
| 1333 |
my_sums[5][0] += v[j] * prec_v[j - end_regular]; |
| 1334 |
my_sums[6][0] += r[j] * prec_r[j - end_regular]; |
| 1335 |
} |
| 1336 |
for (unsigned int i = 0; i < vectorized_sums.size(); ++i) |
| 1337 |
vectorized_sums[i] += my_sums[i]; |
| 1338 |
} |
| 1339 |
|
| 1340 |
// Function that we use if the PreconditionerType does not implement an |
| 1341 |
// apply() function, where we instead need to choose the |
| 1342 |
// apply_to_subrange function |
| 1343 |
template <typename U = void> |
| 1344 |
std::enable_if_t<!has_apply<PreconditionerType>, U> |
| 1345 |
finalize_after_convergence(const unsigned int iteration_index) |
| 1346 |
{ |
| 1347 |
if (iteration_index % 2 == 1 || this->beta == Number()) |
| 1348 |
this->x.add(this->alpha, this->p); |
| 1349 |
else |
| 1350 |
{ |
| 1351 |
const unsigned int end_range = this->x.locally_owned_size(); |
| 1352 |
|
| 1353 |
Number *x = this->x.begin(); |
| 1354 |
Number *r = this->r.begin(); |
| 1355 |
Number *p = this->p.begin(); |
| 1356 |
const Number alpha_plus_previous_alpha_over_beta = |
| 1357 |
this->alpha + this->previous_alpha / this->beta; |
| 1358 |
const Number previous_alpha_over_beta = |
| 1359 |
this->previous_alpha / this->beta; |
| 1360 |
|
| 1361 |
constexpr unsigned int grain_size = 128; |
| 1362 |
std::array<Number, grain_size> prec_r; |
| 1363 |
for (unsigned int j = 0; j < end_range; j += grain_size) |
| 1364 |
{ |
| 1365 |
const unsigned int length = std::min(grain_size, end_range - j); |
| 1366 |
this->preconditioner.apply_to_subrange(j, |
| 1367 |
j + length, |
| 1368 |
r, |
| 1369 |
prec_r.data()); |
| 1370 |
DEAL_II_OPENMP_SIMD_PRAGMA |
| 1371 |
for (unsigned int i = 0; i < length; ++i) |
| 1372 |
x[i] += alpha_plus_previous_alpha_over_beta * p[i] - |
| 1373 |
previous_alpha_over_beta * prec_r[i]; |
| 1374 |
|
| 1375 |
x += length; |
| 1376 |
r += length; |
| 1377 |
p += length; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
} |
| 1381 |
}; |
| 1382 |
} // namespace SolverCG |
| 1383 |
} // namespace internal |
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
template <typename VectorType> |
| 1388 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 1389 |
template <typename MatrixType, typename PreconditionerType> |
| 1390 |
DEAL_II_CXX20_REQUIRES( |
| 1391 |
(concepts::is_linear_operator_on<MatrixType, VectorType> && |
| 1392 |
concepts::is_linear_operator_on<PreconditionerType, VectorType>)) |
| 1393 |
void SolverCG<VectorType>::solve(const MatrixType &A, |
| 1394 |
VectorType &x, |
| 1395 |
const VectorType &b, |
| 1396 |
const PreconditionerType &preconditioner) |
| 1397 |
{ |
| 1398 |
using number = typename VectorType::value_type; |
| 1399 |
|
| 1400 |
SolverControl::State solver_state = SolverControl::iterate; |
| 1401 |
|
| 1402 |
LogStream::Prefix prefix("cg"); |
| 1403 |
|
| 1404 |
// Should we build the matrix for eigenvalue computations? |
| 1405 |
const bool do_eigenvalues = |
| 1406 |
!condition_number_signal.empty() || !all_condition_numbers_signal.empty() || |
| 1407 |
!eigenvalues_signal.empty() || !all_eigenvalues_signal.empty(); |
| 1408 |
|
| 1409 |
// vectors used for eigenvalue computations |
| 1410 |
std::vector<typename VectorType::value_type> diagonal; |
| 1411 |
std::vector<typename VectorType::value_type> offdiagonal; |
| 1412 |
|
| 1413 |
typename VectorType::value_type eigen_beta_alpha = 0; |
| 1414 |
|
| 1415 |
int it = 0; |
| 1416 |
|
| 1417 |
internal::SolverCG:: |
| 1418 |
IterationWorker<VectorType, MatrixType, PreconditionerType> |
| 1419 |
worker(A, |
| 1420 |
preconditioner, |
| 1421 |
determine_beta_by_flexible_formula, |
| 1422 |
this->memory, |
| 1423 |
x, |
| 1424 |
b, |
| 1425 |
additional_data.use_default_residual); |
| 1426 |
|
| 1427 |
worker.startup(); |
| 1428 |
|
| 1429 |
solver_state = this->iteration_status(0, worker.residual_norm, x); |
| 1430 |
if (solver_state != SolverControl::iterate) |
| 1431 |
return; |
| 1432 |
|
| 1433 |
while (solver_state == SolverControl::iterate) |
| 1434 |
{ |
| 1435 |
++it; |
| 1436 |
|
| 1437 |
worker.do_iteration(it); |
| 1438 |
|
| 1439 |
print_vectors(it, x, worker.r, worker.p); |
| 1440 |
|
| 1441 |
if (it > 1) |
| 1442 |
{ |
| 1443 |
this->coefficients_signal(worker.previous_alpha, worker.beta); |
| 1444 |
// set up the vectors containing the diagonal and the off diagonal |
| 1445 |
// of the projected matrix. |
| 1446 |
if (do_eigenvalues) |
| 1447 |
{ |
| 1448 |
diagonal.push_back(number(1.) / worker.previous_alpha + |
| 1449 |
eigen_beta_alpha); |
| 1450 |
eigen_beta_alpha = worker.beta / worker.previous_alpha; |
| 1451 |
offdiagonal.push_back(std::sqrt(worker.beta) / |
| 1452 |
worker.previous_alpha); |
| 1453 |
} |
| 1454 |
compute_eigs_and_cond(diagonal, |
| 1455 |
offdiagonal, |
| 1456 |
all_eigenvalues_signal, |
| 1457 |
all_condition_numbers_signal); |
| 1458 |
} |
| 1459 |
|
| 1460 |
solver_state = this->iteration_status(it, worker.residual_norm, x); |
| 1461 |
} |
| 1462 |
|
| 1463 |
worker.finalize_after_convergence(it); |
| 1464 |
|
| 1465 |
compute_eigs_and_cond(diagonal, |
| 1466 |
offdiagonal, |
| 1467 |
eigenvalues_signal, |
| 1468 |
condition_number_signal); |
| 1469 |
|
| 1470 |
AssertThrow(solver_state == SolverControl::success, |
| 1471 |
SolverControl::NoConvergence(it, worker.residual_norm)); |
| 1472 |
} |
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
template <typename VectorType> |
| 1477 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 1478 |
boost::signals2::connection SolverCG<VectorType>::connect_coefficients_slot( |
| 1479 |
const std::function<void(typename VectorType::value_type, |
| 1480 |
typename VectorType::value_type)> &slot) |
| 1481 |
{ |
| 1482 |
return coefficients_signal.connect(slot); |
| 1483 |
} |
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
template <typename VectorType> |
| 1488 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 1489 |
boost::signals2::connection SolverCG<VectorType>::connect_condition_number_slot( |
| 1490 |
const std::function<void(double)> &slot, |
| 1491 |
const bool every_iteration) |
| 1492 |
{ |
| 1493 |
if (every_iteration) |
| 1494 |
{ |
| 1495 |
return all_condition_numbers_signal.connect(slot); |
| 1496 |
} |
| 1497 |
else |
| 1498 |
{ |
| 1499 |
return condition_number_signal.connect(slot); |
| 1500 |
} |
| 1501 |
} |
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
template <typename VectorType> |
| 1506 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 1507 |
boost::signals2::connection SolverCG<VectorType>::connect_eigenvalues_slot( |
| 1508 |
const std::function<void(const std::vector<double> &)> &slot, |
| 1509 |
const bool every_iteration) |
| 1510 |
{ |
| 1511 |
if (every_iteration) |
| 1512 |
{ |
| 1513 |
return all_eigenvalues_signal.connect(slot); |
| 1514 |
} |
| 1515 |
else |
| 1516 |
{ |
| 1517 |
return eigenvalues_signal.connect(slot); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
template <typename VectorType> |
| 1524 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 1525 |
SolverFlexibleCG<VectorType>::AdditionalData::AdditionalData( |
| 1526 |
const bool use_default_residual) |
| 1527 |
: use_default_residual(use_default_residual) |
| 1528 |
{} |
| 1529 |
|
| 1530 |
|
| 1531 |
template <typename VectorType> |
| 1532 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 1533 |
SolverFlexibleCG<VectorType>::SolverFlexibleCG(SolverControl &cn, |
| 1534 |
VectorMemory<VectorType> &mem, |
| 1535 |
const AdditionalData &data) |
| 1536 |
: SolverCG<VectorType>(cn, mem) |
| 1537 |
{ |
| 1538 |
this->determine_beta_by_flexible_formula = true; |
| 1539 |
this->additional_data = data; |
| 1540 |
} |
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
template <typename VectorType> |
| 1545 |
DEAL_II_CXX20_REQUIRES(concepts::is_vector_space_vector<VectorType>) |
| 1546 |
SolverFlexibleCG<VectorType>::SolverFlexibleCG(SolverControl &cn, |
| 1547 |
const AdditionalData &data) |
| 1548 |
: SolverCG<VectorType>(cn) |
| 1549 |
{ |
| 1550 |
this->determine_beta_by_flexible_formula = true; |
| 1551 |
this->additional_data = data; |
| 1552 |
} |
| 1553 |
|
| 1554 |
|
| 1555 |
#endif // DOXYGEN |
| 1556 |
|
| 1557 |
DEAL_II_NAMESPACE_CLOSE |
| 1558 |
|
| 1559 |
#endif |