| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception |
| 3 |
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 4 |
// vi: set et ts=4 sw=2 sts=2: |
| 5 |
#ifndef DUNE_ISTL_ILUSUBDOMAINSOLVER_HH |
| 6 |
#define DUNE_ISTL_ILUSUBDOMAINSOLVER_HH |
| 7 |
|
| 8 |
#include <map> |
| 9 |
#include <dune/common/typetraits.hh> |
| 10 |
#include <dune/istl/preconditioners.hh> |
| 11 |
#include "matrix.hh" |
| 12 |
#include <cmath> |
| 13 |
#include <cstdlib> |
| 14 |
|
| 15 |
namespace Dune { |
| 16 |
|
| 17 |
/** |
| 18 |
* @file |
| 19 |
* @brief Various local subdomain solvers based on ILU |
| 20 |
* for SeqOverlappingSchwarz. |
| 21 |
* @author Markus Blatt |
| 22 |
*/ |
| 23 |
/** |
| 24 |
* @addtogroup ISTL |
| 25 |
* @{ |
| 26 |
*/ |
| 27 |
/** |
| 28 |
* @brief base class encapsulating common algorithms of ILU0SubdomainSolver |
| 29 |
* and ILUNSubdomainSolver. |
| 30 |
* @tparam M The type of the matrix. |
| 31 |
* @tparam X The type of the vector for the domain. |
| 32 |
* @tparam X The type of the vector for the range. |
| 33 |
* |
| 34 |
*/ |
| 35 |
template<class M, class X, class Y> |
| 36 |
class ILUSubdomainSolver { |
| 37 |
public: |
| 38 |
//! \brief The matrix type the preconditioner is for. |
| 39 |
typedef typename std::remove_const<M>::type matrix_type; |
| 40 |
//! \brief The domain type of the preconditioner. |
| 41 |
typedef X domain_type; |
| 42 |
//! \brief The range type of the preconditioner. |
| 43 |
typedef Y range_type; |
| 44 |
|
| 45 |
/** |
| 46 |
* @brief Apply the subdomain solver. |
| 47 |
* |
| 48 |
* On entry v=? and d=b-A(x) (although this might not be |
| 49 |
* computed in that way. On exit v contains the update |
| 50 |
*/ |
| 51 |
virtual void apply (X& v, const Y& d) =0; |
| 52 |
|
| 53 |
virtual ~ILUSubdomainSolver() |
| 54 |
{} |
| 55 |
|
| 56 |
protected: |
| 57 |
/** |
| 58 |
* @brief Copy the local part of the global matrix to ILU. |
| 59 |
* @param A The global matrix. |
| 60 |
* @param rowset The global indices of the local problem. |
| 61 |
*/ |
| 62 |
template<class S> |
| 63 |
std::size_t copyToLocalMatrix(const M& A, S& rowset); |
| 64 |
|
| 65 |
//! \brief The ILU0 decomposition of the matrix, or the local matrix |
| 66 |
// for ILUN |
| 67 |
matrix_type ILU; |
| 68 |
}; |
| 69 |
|
| 70 |
/** |
| 71 |
* @brief Exact subdomain solver using ILU(p) with appropriate p. |
| 72 |
* @tparam M The type of the matrix. |
| 73 |
* @tparam X The type of the vector for the domain. |
| 74 |
* @tparam X The type of the vector for the range. |
| 75 |
*/ |
| 76 |
template<class M, class X, class Y> |
| 77 |
class ILU0SubdomainSolver |
| 78 |
: public ILUSubdomainSolver<M,X,Y>{ |
| 79 |
public: |
| 80 |
//! \brief The matrix type the preconditioner is for. |
| 81 |
typedef typename std::remove_const<M>::type matrix_type; |
| 82 |
typedef typename std::remove_const<M>::type rilu_type; |
| 83 |
//! \brief The domain type of the preconditioner. |
| 84 |
typedef X domain_type; |
| 85 |
//! \brief The range type of the preconditioner. |
| 86 |
typedef Y range_type; |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* @brief Apply the subdomain solver. |
| 91 |
* @copydoc ILUSubdomainSolver::apply |
| 92 |
*/ |
| 93 |
void apply (X& v, const Y& d) |
| 94 |
{ |
| 95 |
ILU::blockILUBacksolve(this->ILU,v,d); |
| 96 |
} |
| 97 |
/** |
| 98 |
* @brief Set the data of the local problem. |
| 99 |
* |
| 100 |
* @param A The global matrix. |
| 101 |
* @param rowset The global indices of the local problem. |
| 102 |
* @tparam S The type of the set with the indices. |
| 103 |
*/ |
| 104 |
template<class S> |
| 105 |
void setSubMatrix(const M& A, S& rowset); |
| 106 |
|
| 107 |
}; |
| 108 |
|
| 109 |
template<class M, class X, class Y> |
| 110 |
class ILUNSubdomainSolver |
| 111 |
: public ILUSubdomainSolver<M,X,Y>{ |
| 112 |
public: |
| 113 |
//! \brief The matrix type the preconditioner is for. |
| 114 |
typedef typename std::remove_const<M>::type matrix_type; |
| 115 |
typedef typename std::remove_const<M>::type rilu_type; |
| 116 |
//! \brief The domain type of the preconditioner. |
| 117 |
typedef X domain_type; |
| 118 |
//! \brief The range type of the preconditioner. |
| 119 |
typedef Y range_type; |
| 120 |
|
| 121 |
/** |
| 122 |
* @brief Apply the subdomain solver. |
| 123 |
* @copydoc ILUSubdomainSolver::apply |
| 124 |
*/ |
| 125 |
void apply (X& v, const Y& d) |
| 126 |
{ |
| 127 |
ILU::blockILUBacksolve(RILU,v,d); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @brief Set the data of the local problem. |
| 132 |
* |
| 133 |
* @param A The global matrix. |
| 134 |
* @param rowset The global indices of the local problem. |
| 135 |
* @tparam S The type of the set with the indices. |
| 136 |
*/ |
| 137 |
template<class S> |
| 138 |
void setSubMatrix(const M& A, S& rowset); |
| 139 |
|
| 140 |
private: |
| 141 |
/** |
| 142 |
* @brief Storage for the ILUN decomposition. |
| 143 |
*/ |
| 144 |
rilu_type RILU; |
| 145 |
}; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
template<class M, class X, class Y> |
| 150 |
template<class S> |
| 151 |
std::size_t ILUSubdomainSolver<M,X,Y>::copyToLocalMatrix(const M& A, S& rowSet) |
| 152 |
{ |
| 153 |
// Calculate consecutive indices for local problem |
| 154 |
// while preserving the ordering |
| 155 |
typedef typename M::size_type size_type; |
| 156 |
typedef std::map<typename S::value_type,size_type> IndexMap; |
| 157 |
typedef typename IndexMap::iterator IMIter; |
| 158 |
IndexMap indexMap; |
| 159 |
IMIter guess = indexMap.begin(); |
| 160 |
size_type localIndex=0; |
| 161 |
|
| 162 |
typedef typename S::const_iterator SIter; |
| 163 |
for(SIter rowIdx = rowSet.begin(), rowEnd=rowSet.end(); |
| 164 |
rowIdx!= rowEnd; ++rowIdx, ++localIndex) |
| 165 |
guess = indexMap.insert(guess, |
| 166 |
std::make_pair(*rowIdx,localIndex)); |
| 167 |
|
| 168 |
|
| 169 |
// Build Matrix for local subproblem |
| 170 |
ILU.setSize(rowSet.size(),rowSet.size()); |
| 171 |
ILU.setBuildMode(matrix_type::row_wise); |
| 172 |
|
| 173 |
// Create sparsity pattern |
| 174 |
typedef typename matrix_type::CreateIterator CIter; |
| 175 |
CIter rowCreator = ILU.createbegin(); |
| 176 |
std::size_t offset=0; |
| 177 |
for(SIter rowIdx = rowSet.begin(), rowEnd=rowSet.end(); |
| 178 |
rowIdx!= rowEnd; ++rowIdx, ++rowCreator) { |
| 179 |
// See which row entries are in our subset and add them to |
| 180 |
// the sparsity pattern |
| 181 |
guess = indexMap.begin(); |
| 182 |
|
| 183 |
for(typename matrix_type::ConstColIterator col=A[*rowIdx].begin(), |
| 184 |
endcol=A[*rowIdx].end(); col != endcol; ++col) { |
| 185 |
// search for the entry in the row set |
| 186 |
guess = indexMap.find(col.index()); |
| 187 |
if(guess!=indexMap.end()) { |
| 188 |
// add local index to row |
| 189 |
rowCreator.insert(guess->second); |
| 190 |
offset=std::max(offset,(std::size_t)std::abs((int)(guess->second-rowCreator.index()))); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
// Insert the matrix values for the local problem |
| 197 |
typename matrix_type::iterator iluRow=ILU.begin(); |
| 198 |
|
| 199 |
for(SIter rowIdx = rowSet.begin(), rowEnd=rowSet.end(); |
| 200 |
rowIdx!= rowEnd; ++rowIdx, ++iluRow) { |
| 201 |
// See which row entries are in our subset and add them to |
| 202 |
// the sparsity pattern |
| 203 |
typename matrix_type::ColIterator localCol=iluRow->begin(); |
| 204 |
for(typename matrix_type::ConstColIterator col=A[*rowIdx].begin(), |
| 205 |
endcol=A[*rowIdx].end(); col != endcol; ++col) { |
| 206 |
// search for the entry in the row set |
| 207 |
guess = indexMap.find(col.index()); |
| 208 |
if(guess!=indexMap.end()) { |
| 209 |
// set local value |
| 210 |
(*localCol)=(*col); |
| 211 |
++localCol; |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
return offset; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
template<class M, class X, class Y> |
| 220 |
template<class S> |
| 221 |
void ILU0SubdomainSolver<M,X,Y>::setSubMatrix(const M& A, S& rowSet) |
| 222 |
{ |
| 223 |
this->copyToLocalMatrix(A,rowSet); |
| 224 |
ILU::blockILU0Decomposition(this->ILU); |
| 225 |
} |
| 226 |
|
| 227 |
template<class M, class X, class Y> |
| 228 |
template<class S> |
| 229 |
void ILUNSubdomainSolver<M,X,Y>::setSubMatrix(const M& A, S& rowSet) |
| 230 |
{ |
| 231 |
std::size_t offset=copyToLocalMatrix(A,rowSet); |
| 232 |
RILU.setSize(rowSet.size(),rowSet.size(), (1+2*offset)*rowSet.size()); |
| 233 |
RILU.setBuildMode(matrix_type::row_wise); |
| 234 |
ILU::blockILUDecomposition(this->ILU, (offset+1)/2, RILU); |
| 235 |
} |
| 236 |
|
| 237 |
/** @} */ |
| 238 |
} // end name space DUNE |
| 239 |
|
| 240 |
|
| 241 |
#endif |