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 Total, S.A
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 GEOSX_LINEARALGEBRA_SOLVERS_PRECONDITIONERBLOCKJACOBI_HPP_
16 #define GEOSX_LINEARALGEBRA_SOLVERS_PRECONDITIONERBLOCKJACOBI_HPP_
17 
19 #include "linearAlgebra/solvers/PreconditionerBase.hpp"
21 
22 namespace geosx
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 compute( Matrix const & mat ) override
58  {
59  GEOSX_LAI_ASSERT( mat.ready() );
60  GEOSX_LAI_ASSERT_GT( m_blockSize, 0 );
61  GEOSX_LAI_ASSERT_EQ( mat.numLocalRows() % m_blockSize, 0 );
62  GEOSX_LAI_ASSERT_EQ( mat.numLocalCols() % m_blockSize, 0 );
63 
65 
66  m_blockDiag.createWithLocalSize( mat.numLocalRows(), mat.numLocalCols(), m_blockSize, mat.getComm() );
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.setValues< serialPolicy >( 0.0 );
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.globalRowLength( 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 
105  virtual void compute( Matrix const & mat,
106  DofManager const & dofManager ) override
107  {
108  GEOSX_UNUSED_VAR( dofManager );
109  compute( mat );
110  }
111 
123  virtual void clear() override
124  {
125  m_blockDiag.reset();
126  }
127 
134  virtual void apply( Vector const & src,
135  Vector & dst ) const override
136  {
137  GEOSX_LAI_ASSERT( m_blockDiag.ready() );
138  GEOSX_LAI_ASSERT_EQ( this->numGlobalRows(), dst.globalSize() );
139  GEOSX_LAI_ASSERT_EQ( this->numGlobalCols(), src.globalSize() );
140 
141  m_blockDiag.apply( src, dst );
142  }
143 
148  virtual bool hasPreconditionerMatrix() const override
149  {
150  GEOSX_LAI_ASSERT( m_blockDiag.ready() );
151  return true;
152  }
153 
158  virtual Matrix const & preconditionerMatrix() const override
159  {
160  GEOSX_LAI_ASSERT( m_blockDiag.ready() );
161  return m_blockDiag;
162  }
163 
164 private:
165 
167  Matrix m_blockDiag;
168 
170  localIndex m_blockSize = 0;
171 };
172 
173 }
174 
175 #endif //GEOSX_LINEARALGEBRA_SOLVERS_PRECONDITIONERBLOCKJACOBI_HPP_
PreconditionerBlockJacobi(localIndex const &blockSize=0)
Constructor.
virtual globalIndex numGlobalRows() const override
Get the number of global rows.
virtual void compute(Matrix const &mat) override
Compute the preconditioner from a matrix.
Common interface for preconditioning operators.
long long int globalIndex
Global index type (for indexing objects across MPI partitions).
Definition: DataTypes.hpp:128
virtual globalIndex numGlobalCols() const override
Get the number of global columns.
virtual void compute(Matrix const &mat)
Compute the preconditioner from a matrix.
The DoFManager is responsible for allocating global dofs, constructing sparsity patterns, and generally simplifying the interaction between PhysicsSolvers and linear algebra operations.
Definition: DofManager.hpp:42
static void matrixInverse(MatRowMajor< real64 const > const &A, MatRowMajor< real64 > const &Ainv, real64 &detA)
Computes the inverse matrix; Ainv = inverse(A).
virtual bool hasPreconditionerMatrix() const override
Whether the preconditioner is available in matrix form.
virtual void compute(Matrix const &mat, DofManager const &dofManager) override
Compute the preconditioner from a matrix.
virtual void clear() override
Clean up the preconditioner setup.
typename LAI::ParallelMatrix Matrix
Alias for matrix type.
#define GEOSX_LAI_ASSERT_EQ(lhs, rhs)
Definition: common.hpp:47
Common interface for identity preconditioning operator.
#define GEOSX_UNUSED_VAR(...)
Mark an unused variable and silence compiler warnings.
Definition: GeosxMacros.hpp:78
#define GEOSX_LAI_ASSERT_GT(lhs, rhs)
Definition: common.hpp:61
void resize(int const numDims, DIMS_TYPE const *const dims)
Resize the dimensions of the Array to match the given dimensions.
Definition: Array.hpp:277
std::ptrdiff_t localIndex
Local index type (for indexing objects within an MPI partition).
Definition: DataTypes.hpp:125
#define GEOSX_LAI_ASSERT(expr)
Definition: common.hpp:33
Abstract base class for linear operators.
typename Base::Vector Vector
Alias for vector type.
This class provides a fixed dimensional resizeable array interface in addition to an interface simila...
Definition: Array.hpp:55
virtual Matrix const & preconditionerMatrix() const override
Access the preconditioner in matrix form.
virtual void apply(Vector const &src, Vector &dst) const override
Apply operator to a vector.