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 
38  virtual ~PreconditionerBase() = default;
39 
42 
44  using Vector = typename Base::Vector;
45 
47  using Matrix = typename LAI::ParallelMatrix;
48 
53  virtual void setup( Matrix const & mat )
54  {
55  GEOS_LAI_ASSERT( mat.ready() );
56  GEOS_LAI_ASSERT_MSG( mat.numLocalRows() == mat.numLocalCols(), "Matrix must be square" );
57  m_mat = &mat;
58  }
59 
71  virtual void clear()
72  {
73  m_mat = nullptr;
74  }
75 
80  virtual globalIndex numGlobalRows() const override
81  {
82  return m_mat->numGlobalRows();
83  }
84 
89  virtual globalIndex numGlobalCols() const override
90  {
91  return m_mat->numGlobalCols();
92  }
93 
98  virtual localIndex numLocalRows() const override
99  {
100  return m_mat->numLocalRows();
101  }
102 
107  virtual localIndex numLocalCols() const override
108  {
109  return m_mat->numLocalCols();
110  }
111 
119  virtual MPI_Comm comm() const override
120  {
121  return m_mat->comm();
122  }
123 
128  bool ready() const
129  {
130  return m_mat != nullptr;
131  }
132 
137  Matrix const & matrix() const
138  {
139  GEOS_LAI_ASSERT( ready() );
140  return *m_mat;
141  }
142 
147  virtual bool hasPreconditionerMatrix() const
148  {
149  return false;
150  }
151 
157  virtual Matrix const & preconditionerMatrix() const
158  {
159  GEOS_ERROR( "PreconditionerBase::preconditionerMatrix called. This is not supposed to happen."
160  "Check the value of hasPreconditionerMatrix() before accessing this function." );
161  // This is here just to be able to compile ...
162  return *m_mat;
163  }
164 
165 private:
166 
168  Matrix const * m_mat{};
169 };
170 
171 }
172 
173 #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:88
GEOS_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:85
LAInterface::ParallelMatrix ParallelMatrix
Alias for ParallelMatrix.