GEOS
PreconditionerBlockJacobi.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 Total, S.A
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_PRECONDITIONERBLOCKJACOBI_HPP_
17 #define GEOS_LINEARALGEBRA_SOLVERS_PRECONDITIONERBLOCKJACOBI_HPP_
18 
20 #include "linearAlgebra/common/PreconditionerBase.hpp"
21 #include "denseLinearAlgebra/interfaces/blaslapack/BlasLapackLA.hpp"
22 
23 namespace geos
24 {
25 
30 template< typename LAI >
32 {
33 public:
34 
37 
39  using Vector = typename Base::Vector;
40 
42  using Matrix = typename Base::Matrix;
43 
48  PreconditionerBlockJacobi( localIndex const & blockSize = 0 )
49  : m_blockDiag{}
50  {
51  m_blockSize = blockSize;
52  }
53 
58  virtual void setup( Matrix const & mat ) override
59  {
60  GEOS_LAI_ASSERT( mat.ready() );
61  GEOS_LAI_ASSERT_GT( m_blockSize, 0 );
62  GEOS_LAI_ASSERT_EQ( mat.numLocalRows() % m_blockSize, 0 );
63  GEOS_LAI_ASSERT_EQ( mat.numLocalCols() % m_blockSize, 0 );
64 
66 
67  m_blockDiag.createWithLocalSize( mat.numLocalRows(), mat.numLocalCols(), m_blockSize, mat.comm() );
68  m_blockDiag.open();
69 
70  array1d< globalIndex > idxBlk( m_blockSize );
71  array2d< real64 > values( m_blockSize, m_blockSize );
72  array2d< real64 > valuesInv( m_blockSize, m_blockSize );
74  array1d< real64 > vals;
75  for( globalIndex i = mat.ilower(); i < mat.iupper(); i += m_blockSize )
76  {
77  values.zero();
78  for( localIndex j = 0; j < m_blockSize; ++j )
79  {
80  globalIndex const iRow = i + LvArray::integerConversion< globalIndex >( j );
81  idxBlk[j] = iRow;
82  localIndex const rowLength = mat.rowLength( iRow );
83  cols.resize( rowLength );
84  vals.resize( rowLength );
85  mat.getRowCopy( iRow, cols, vals );
86  for( localIndex k = 0; k < rowLength; ++k )
87  {
88  localIndex const jCol = LvArray::integerConversion< localIndex >( cols[k]-i );
89  if( cols[k] >= i && cols[k] < i+LvArray::integerConversion< globalIndex >( m_blockSize ) )
90  {
91  values( j, jCol ) = vals[k];
92  }
93  }
94  }
95  BlasLapackLA::matrixInverse( values, valuesInv );
96  m_blockDiag.insert( idxBlk, idxBlk, valuesInv );
97  }
98  m_blockDiag.close();
99  }
100 
112  virtual void clear() override
113  {
114  m_blockDiag.reset();
115  }
116 
123  virtual void apply( Vector const & src,
124  Vector & dst ) const override
125  {
126  GEOS_LAI_ASSERT( m_blockDiag.ready() );
127  GEOS_LAI_ASSERT_EQ( this->numGlobalRows(), dst.globalSize() );
128  GEOS_LAI_ASSERT_EQ( this->numGlobalCols(), src.globalSize() );
129 
130  m_blockDiag.apply( src, dst );
131  }
132 
137  virtual bool hasPreconditionerMatrix() const override
138  {
139  GEOS_LAI_ASSERT( m_blockDiag.ready() );
140  return true;
141  }
142 
147  virtual Matrix const & preconditionerMatrix() const override
148  {
149  GEOS_LAI_ASSERT( m_blockDiag.ready() );
150  return m_blockDiag;
151  }
152 
153 private:
154 
156  Matrix m_blockDiag;
157 
159  localIndex m_blockSize = 0;
160 };
161 
162 }
163 
164 #endif //GEOS_LINEARALGEBRA_SOLVERS_PRECONDITIONERBLOCKJACOBI_HPP_
Abstract base class for linear operators.
Common interface for preconditioning operators.
virtual void setup(Matrix const &mat)
Compute the preconditioner from a matrix.
virtual globalIndex numGlobalCols() const override
Get the number of global columns.
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.
Common interface for identity preconditioning operator.
virtual void apply(Vector const &src, Vector &dst) const override
Apply operator to a vector.
virtual void clear() override
Clean up the preconditioner setup.
virtual void setup(Matrix const &mat) override
Compute the preconditioner from a matrix.
PreconditionerBlockJacobi(localIndex const &blockSize=0)
Constructor.
virtual bool hasPreconditionerMatrix() const override
Whether the preconditioner is available in matrix form.
typename Base::Matrix Matrix
Alias for matrix type.
virtual Matrix const & preconditionerMatrix() const override
Access the preconditioner in matrix form.
#define GEOS_LAI_ASSERT_EQ(lhs, rhs)
Definition: common.hpp:49
#define GEOS_LAI_ASSERT_GT(lhs, rhs)
Definition: common.hpp:63
#define GEOS_LAI_ASSERT(expr)
Definition: common.hpp:35
Array< T, 2, PERMUTATION > array2d
Alias for 2D array.
Definition: DataTypes.hpp:192
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
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:176