GEOSX
PreconditionerBlockJacobi.hpp
1 /*
2  * ------------------------------------------------------------------------------------------------------------
3  * SPDX-License-Identifier: LGPL-2.1-only
4  *
5  * Copyright (c) 2018-2019 Lawrence Livermore National Security LLC
6  * Copyright (c) 2018-2019 The Board of Trustees of the Leland Stanford Junior University
7  * Copyright (c) 2018-2019 TotalEnergies
8  * Copyright (c) 2019- GEOSX Contributors
9  * All right reserved
10  *
11  * See top level LICENSE, COPYRIGHT, CONTRIBUTORS, NOTICE, and ACKNOWLEDGEMENTS files for details.
12  * ------------------------------------------------------------------------------------------------------------
13  */
14 
15 #ifndef GEOS_LINEARALGEBRA_SOLVERS_PRECONDITIONERBLOCKJACOBI_HPP_
16 #define GEOS_LINEARALGEBRA_SOLVERS_PRECONDITIONERBLOCKJACOBI_HPP_
17 
19 #include "linearAlgebra/common/PreconditionerBase.hpp"
20 #include "denseLinearAlgebra/interfaces/blaslapack/BlasLapackLA.hpp"
21 
22 namespace geos
23 {
24 
29 template< typename LAI >
31 {
32 public:
33 
36 
38  using Vector = typename Base::Vector;
39 
41  using Matrix = typename Base::Matrix;
42 
47  PreconditionerBlockJacobi( localIndex const & blockSize = 0 )
48  : m_blockDiag{}
49  {
50  m_blockSize = blockSize;
51  }
52 
57  virtual void setup( Matrix const & mat ) override
58  {
59  GEOS_LAI_ASSERT( mat.ready() );
60  GEOS_LAI_ASSERT_GT( m_blockSize, 0 );
61  GEOS_LAI_ASSERT_EQ( mat.numLocalRows() % m_blockSize, 0 );
62  GEOS_LAI_ASSERT_EQ( mat.numLocalCols() % m_blockSize, 0 );
63 
65 
66  m_blockDiag.createWithLocalSize( mat.numLocalRows(), mat.numLocalCols(), m_blockSize, mat.comm() );
67  m_blockDiag.open();
68 
69  array1d< globalIndex > idxBlk( m_blockSize );
70  array2d< real64 > values( m_blockSize, m_blockSize );
71  array2d< real64 > valuesInv( m_blockSize, m_blockSize );
73  array1d< real64 > vals;
74  for( globalIndex i = mat.ilower(); i < mat.iupper(); i += m_blockSize )
75  {
76  values.zero();
77  for( localIndex j = 0; j < m_blockSize; ++j )
78  {
79  globalIndex const iRow = i + LvArray::integerConversion< globalIndex >( j );
80  idxBlk[j] = iRow;
81  localIndex const rowLength = mat.rowLength( iRow );
82  cols.resize( rowLength );
83  vals.resize( rowLength );
84  mat.getRowCopy( iRow, cols, vals );
85  for( localIndex k = 0; k < rowLength; ++k )
86  {
87  localIndex const jCol = LvArray::integerConversion< localIndex >( cols[k]-i );
88  if( cols[k] >= i && cols[k] < i+LvArray::integerConversion< globalIndex >( m_blockSize ) )
89  {
90  values( j, jCol ) = vals[k];
91  }
92  }
93  }
94  BlasLapackLA::matrixInverse( values, valuesInv );
95  m_blockDiag.insert( idxBlk, idxBlk, valuesInv );
96  }
97  m_blockDiag.close();
98  }
99 
111  virtual void clear() override
112  {
113  m_blockDiag.reset();
114  }
115 
122  virtual void apply( Vector const & src,
123  Vector & dst ) const override
124  {
125  GEOS_LAI_ASSERT( m_blockDiag.ready() );
126  GEOS_LAI_ASSERT_EQ( this->numGlobalRows(), dst.globalSize() );
127  GEOS_LAI_ASSERT_EQ( this->numGlobalCols(), src.globalSize() );
128 
129  m_blockDiag.apply( src, dst );
130  }
131 
136  virtual bool hasPreconditionerMatrix() const override
137  {
138  GEOS_LAI_ASSERT( m_blockDiag.ready() );
139  return true;
140  }
141 
146  virtual Matrix const & preconditionerMatrix() const override
147  {
148  GEOS_LAI_ASSERT( m_blockDiag.ready() );
149  return m_blockDiag;
150  }
151 
152 private:
153 
155  Matrix m_blockDiag;
156 
158  localIndex m_blockSize = 0;
159 };
160 
161 }
162 
163 #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:47
#define GEOS_LAI_ASSERT_GT(lhs, rhs)
Definition: common.hpp:61
#define GEOS_LAI_ASSERT(expr)
Definition: common.hpp:33
Array< T, 2, PERMUTATION > array2d
Alias for 2D array.
Definition: DataTypes.hpp:232
GEOSX_GLOBALINDEX_TYPE globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
GEOSX_LOCALINDEX_TYPE localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
Array< T, 1 > array1d
Alias for 1D array.
Definition: DataTypes.hpp:216