GEOS
PreconditionerBase.hpp
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2016-2024 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2024 TotalEnergies
7  * Copyright (c) 2018-2024 The Board of Trustees of the Leland Stanford Junior University
8  * Copyright (c) 2023-2024 Chevron
9  * Copyright (c) 2019- GEOS/GEOSX Contributors
10  * All rights reserved
11  *
12  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
13  * ------------------------------------------------------------------------------------------------------------
14  */
15 
16 #ifndef GEOS_LINEARALGEBRA_SOLVERS_PRECONDITIONERBASE_HPP_
17 #define GEOS_LINEARALGEBRA_SOLVERS_PRECONDITIONERBASE_HPP_
18 
21 
22 namespace geos
23 {
24 
25 class DofManager;
26 
31 template< typename LAI >
32 class PreconditionerBase : public LinearOperator< typename LAI::ParallelVector >
33 {
34 public:
35 
36  PreconditionerBase() = default;
37 
40 
42  using Vector = typename Base::Vector;
43 
45  using Matrix = typename LAI::ParallelMatrix;
46 
51  virtual void setup( Matrix const & mat )
52  {
53  GEOS_LAI_ASSERT( mat.ready() );
54  GEOS_LAI_ASSERT_MSG( mat.numLocalRows() == mat.numLocalCols(), "Matrix must be square" );
55  m_mat = &mat;
56  }
57 
69  virtual void clear()
70  {
71  m_mat = nullptr;
72  }
73 
78  virtual globalIndex numGlobalRows() const override
79  {
80  return m_mat->numGlobalRows();
81  }
82 
87  virtual globalIndex numGlobalCols() const override
88  {
89  return m_mat->numGlobalCols();
90  }
91 
96  virtual localIndex numLocalRows() const override
97  {
98  return m_mat->numLocalRows();
99  }
100 
105  virtual localIndex numLocalCols() const override
106  {
107  return m_mat->numLocalCols();
108  }
109 
117  virtual MPI_Comm comm() const override
118  {
119  return m_mat->comm();
120  }
121 
126  bool ready() const
127  {
128  return m_mat != nullptr;
129  }
130 
135  Matrix const & matrix() const
136  {
137  GEOS_LAI_ASSERT( ready() );
138  return *m_mat;
139  }
140 
145  virtual bool hasPreconditionerMatrix() const
146  {
147  return false;
148  }
149 
155  virtual Matrix const & preconditionerMatrix() const
156  {
157  GEOS_ERROR( "PreconditionerBase::preconditionerMatrix called. This is not supposed to happen."
158  "Check the value of hasPreconditionerMatrix() before accessing this function." );
159  // This is here just to be able to compile ...
160  return *m_mat;
161  }
162 
163 private:
164 
166  Matrix const * m_mat{};
167 };
168 
169 }
170 
171 #endif //GEOS_LINEARALGEBRA_SOLVERS_PRECONDITIONERBASE_HPP_
#define GEOS_ERROR(msg)
Raise a hard error and terminate the program.
Definition: Logger.hpp:157
Abstract base class for linear operators.
VECTOR Vector
Alias for template parameter.
Common interface for preconditioning operators.
virtual void clear()
Clean up the preconditioner setup.
virtual Matrix const & preconditionerMatrix() const
Access the preconditioner in matrix form (whenever available). It must be overridden by the specific ...
virtual MPI_Comm comm() const override
Get the MPI communicator the matrix was created with.
virtual localIndex numLocalCols() const override
Get the number of local columns.
virtual bool hasPreconditionerMatrix() const
Check whether the preconditioner is available in matrix (explicit) form.
bool ready() const
Chech if preconditioner is ready to use.
virtual void setup(Matrix const &mat)
Compute the preconditioner from a matrix.
virtual globalIndex numGlobalCols() const override
Get the number of global columns.
virtual localIndex numLocalRows() const override
Get the number of local rows.
Matrix const & matrix() const
Access the matrix the preconditioner was computed from.
virtual globalIndex numGlobalRows() const override
Get the number of global rows.
typename Base::Vector Vector
Alias for vector type.
typename LAI::ParallelMatrix Matrix
Alias for matrix type.
#define GEOS_LAI_ASSERT_MSG(expr, msg)
Definition: common.hpp:42
#define GEOS_LAI_ASSERT(expr)
Definition: common.hpp:35
GEOS_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:87
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:84
LAInterface::ParallelMatrix ParallelMatrix
Alias for ParallelMatrix.