GEOSX
KrylovSolver.hpp
1 /*
2  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3  * Copyright (c) 2019, Lawrence Livermore National Security, LLC.
4  *
5  * Produced at the Lawrence Livermore National Laboratory
6  *
7  * LLNL-CODE-746361
8  *
9  * All rights reserved. See COPYRIGHT for details.
10  *
11  * This file is part of the GEOSX Simulation Framework.
12  *
13  * GEOSX is a free software; you can redistribute it and/or modify it under
14  * the terms of the GNU Lesser General Public License (as published by the
15  * Free Software Foundation) version 2.1 dated February 1999.
16  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17  */
18 
19 #ifndef GEOSX_LINEARALGEBRA_SOLVERS_KRYLOVSOLVER_HPP_
20 #define GEOSX_LINEARALGEBRA_SOLVERS_KRYLOVSOLVER_HPP_
21 
27 
28 namespace geosx
29 {
30 
31 template< typename Vector > class LinearOperator;
32 template< typename Vector > class BlockVectorView;
33 
38 template< typename VECTOR >
39 class KrylovSolver : public LinearOperator< VECTOR >
40 {
41 public:
42 
45 
47  using Vector = typename Base::Vector;
48 
56  static std::unique_ptr< KrylovSolver< VECTOR > > Create( LinearSolverParameters const & parameters,
57  LinearOperator< VECTOR > const & matrix,
58  LinearOperator< VECTOR > const & precond );
59 
68  KrylovSolver( LinearOperator< Vector > const & matrix,
69  LinearOperator< Vector > const & precond,
70  real64 const tolerance,
71  localIndex const maxIterations,
72  integer const verbosity );
73 
77  virtual ~KrylovSolver() override = default;
78 
84  virtual void solve( Vector const & b, Vector & x ) const = 0;
85 
86 
93  virtual void apply( Vector const & src, Vector & dst ) const override final
94  {
95  solve( src, dst );
96  }
97 
98  virtual globalIndex numGlobalRows() const override final
99  {
100  return m_operator.numGlobalRows();
101  }
102 
103  virtual globalIndex numGlobalCols() const override final
104  {
105  return m_operator.numGlobalCols();
106  }
107 
112  LinearSolverResult const & result() const
113  {
114  return m_result;
115  }
116 
122  {
123  return m_residualNorms;
124  }
125 
131  {
132  return m_logLevel;
133  }
134 
139  virtual string methodName() const = 0;
140 
141 private:
142 
144 
145  template< typename VEC >
146  struct VectorStorageHelper
147  {
148  using type = VEC;
149 
150  static VEC createFrom( VEC const & src )
151  {
152  VEC v;
153  v.createWithLocalSize( src.localSize(), src.getComm() );
154  return v;
155  }
156  };
157 
158  template< typename VEC >
159  struct VectorStorageHelper< BlockVectorView< VEC > >
160  {
161  using type = BlockVector< VEC >;
162 
163  static BlockVector< VEC > createFrom( BlockVectorView< VEC > const & src )
164  {
165  BlockVector< VEC > v( src.blockSize() );
166  for( localIndex i = 0; i < src.blockSize(); ++i )
167  {
168  v.block( i ).createWithLocalSize( src.block( i ).localSize(), src.block( i ).getComm() );
169  }
170  return v;
171  }
172  };
173 
175 
176 protected:
177 
179  using VectorTemp = typename VectorStorageHelper< VECTOR >::type;
180 
188  static VectorTemp createTempVector( Vector const & src )
189  {
190  return VectorStorageHelper< VECTOR >::createFrom( src );
191  }
192 
198  void logProgress( localIndex const iter, real64 const rnorm ) const
199  {
200  m_residualNorms[iter] = rnorm;
201  GEOSX_LOG_LEVEL_RANK_0( 2, methodName() << " iteration " << iter << ": residual = " << rnorm );
202  }
203 
207  void logResult() const
208  {
209  GEOSX_LOG_LEVEL_RANK_0( 1, methodName() << ' ' <<
210  ( m_result.success() ? "converged" : "failed to converge" ) <<
211  " in " << m_result.numIterations << " iterations " <<
212  "(" << m_result.solveTime << " s)" );
213  }
214 
217 
220 
223 
226 
229 
232 
235 };
236 
237 } //namespace geosx
238 
239 #endif //GEOSX_LINEARALGEBRA_SOLVERS_KRYLOVSOLVER_HPP_
virtual void apply(Vector const &src, Vector &dst) const override final
Apply operator to a vector.
Concrete representation of a block vector.
Definition: BlockVector.hpp:35
typename VectorStorageHelper< VECTOR >::type VectorTemp
Alias for vector type that can be used for temporaries.
virtual globalIndex numGlobalRows() const override final
Get the number of global rows.
long long int globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
#define GEOSX_LOG_LEVEL_RANK_0(minLevel, msg)
Output messages (only on rank 0) based on current Group&#39;s log level.
Definition: Logger.hpp:326
virtual globalIndex numGlobalCols() const =0
Get the number of global columns.
virtual string methodName() const =0
Get name of the Krylov subspace method.
integer m_logLevel
solver verbosity level
LinearOperator< Vector > const & m_operator
reference to the operator to be solved
LinearOperator< Vector > const & m_precond
reference to the preconditioning operator
void logProgress(localIndex const iter, real64 const rnorm) const
Output iteration progress (called by implementations).
integer numIterations
Number of solver iterations performed.
virtual globalIndex numGlobalCols() const override final
Get the number of global columns.
Set of parameters for a linear solver or preconditioner.
virtual ~KrylovSolver() override=default
Virtual destructor.
Results/stats of a linear solve.
real64 solveTime
Solve time (in seconds) exclusive of setup costs.
static VectorTemp createTempVector(Vector const &src)
Helper function to create temporary vectors based on a source vector.
This class serves to provide a "view" of a multidimensional array.
Definition: ArrayView.hpp:67
KrylovSolver(LinearOperator< Vector > const &matrix, LinearOperator< Vector > const &precond, real64 const tolerance, localIndex const maxIterations, integer const verbosity)
Constructor.
double real64
64-bit floating point type.
Definition: DataTypes.hpp:136
localIndex blockSize() const
Get block size.
array1d< real64 > m_residualNorms
Absolute residual norms at each iteration (if available)
typename Base::Vector Vector
Alias for template parameter.
virtual globalIndex numGlobalRows() const =0
Get the number of global rows.
real64 m_tolerance
relative residual norm reduction tolerance
std::int32_t integer
Signed integer type.
Definition: DataTypes.hpp:122
arrayView1d< real64 const > history() const
Get convergence history of a linear solve.
static std::unique_ptr< KrylovSolver< VECTOR > > Create(LinearSolverParameters const &parameters, LinearOperator< VECTOR > const &matrix, LinearOperator< VECTOR > const &precond)
Factory method for instantiating Krylov solver objects.
localIndex m_maxIterations
maximum number of Krylov iterations
std::ptrdiff_t localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
LinearSolverResult const & result() const
Get result of a linear solve.
VECTOR const & block(localIndex const blockIndex) const
Get a reference to the vector corresponding to block blockRowIndex.
integer getLogLevel() const
Get log level.
Abstract view of a block vector.
void logResult() const
Output convergence result (called by implementations).
Abstract base class for linear operators.
VECTOR Vector
Alias for template parameter.
LinearSolverResult m_result
results of a solve
Base class for Krylov solvers.
bool success() const
Check whether the last solve was successful.
This class provides a fixed dimensional resizeable array interface in addition to an interface simila...
Definition: Array.hpp:55
virtual void solve(Vector const &b, Vector &x) const =0
Solve preconditioned system.