| 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_SCHWARZ_HH |
| 6 |
#define DUNE_ISTL_SCHWARZ_HH |
| 7 |
|
| 8 |
#include <iostream> // for input/output to shell |
| 9 |
#include <fstream> // for input/output to files |
| 10 |
#include <vector> // STL vector class |
| 11 |
#include <sstream> |
| 12 |
|
| 13 |
#include <cmath> // Yes, we do some math here |
| 14 |
|
| 15 |
#include <dune/common/timer.hh> |
| 16 |
|
| 17 |
#include "io.hh" |
| 18 |
#include "bvector.hh" |
| 19 |
#include "vbvector.hh" |
| 20 |
#include "bcrsmatrix.hh" |
| 21 |
#include "io.hh" |
| 22 |
#include "gsetc.hh" |
| 23 |
#include "ilu.hh" |
| 24 |
#include "operators.hh" |
| 25 |
#include "solvers.hh" |
| 26 |
#include "preconditioners.hh" |
| 27 |
#include "scalarproducts.hh" |
| 28 |
#include "owneroverlapcopy.hh" |
| 29 |
|
| 30 |
namespace Dune { |
| 31 |
|
| 32 |
/** |
| 33 |
* @defgroup ISTL_Parallel Parallel Solvers |
| 34 |
* @ingroup ISTL_Solvers |
| 35 |
* Instead of using parallel data structures (matrices and vectors) that |
| 36 |
* (implicitly) know the data distribution and communication patterns, |
| 37 |
* there is a clear separation of the parallel data composition together |
| 38 |
* with the communication APIs from the data structures. This allows for |
| 39 |
* implementing overlapping and nonoverlapping domain decompositions as |
| 40 |
* well as data parallel parallelisation approaches. |
| 41 |
* |
| 42 |
* The \ref ISTL_Solvers "solvers" can easily be turned into parallel solvers |
| 43 |
* initializing them with matching parallel subclasses of the base classes |
| 44 |
* ScalarProduct, Preconditioner and LinearOperator. |
| 45 |
* |
| 46 |
* The information of the data distribution is provided by OwnerOverlapCopyCommunication |
| 47 |
* of \ref ISTL_Comm "communication API". |
| 48 |
*/ |
| 49 |
/** |
| 50 |
@addtogroup ISTL_Operators |
| 51 |
@{ |
| 52 |
*/ |
| 53 |
|
| 54 |
/** |
| 55 |
* \brief An overlapping Schwarz operator. |
| 56 |
* |
| 57 |
* This operator represents a parallel matrix product using |
| 58 |
* sequential data structures together with a parallel index set |
| 59 |
* describing an overlapping domain decomposition and the communication. |
| 60 |
* \tparam M The type of the sequential matrix to use, |
| 61 |
* e.g. BCRSMatrix or another matrix type fulfilling the |
| 62 |
* matrix interface of ISTL. |
| 63 |
* \tparam X The type of the sequential vector to use for the left hand side, |
| 64 |
* e.g. BlockVector or another type fulfilling the ISTL |
| 65 |
* vector interface. |
| 66 |
* \tparam Y The type of the sequential vector to use for the right hand side, |
| 67 |
* e..g. BlockVector or another type fulfilling the ISTL |
| 68 |
* vector interface. |
| 69 |
* \tparam C The type of the communication object. |
| 70 |
* This must either be OwnerOverlapCopyCommunication or a type |
| 71 |
* implementing the same interface. |
| 72 |
*/ |
| 73 |
template<class M, class X, class Y, class C> |
| 74 |
class OverlappingSchwarzOperator : public AssembledLinearOperator<M,X,Y> |
| 75 |
{ |
| 76 |
public: |
| 77 |
//! \brief The type of the matrix we operate on. |
| 78 |
//! |
| 79 |
//! E.g. BCRSMatrix or another matrix type fulfilling the |
| 80 |
//! matrix interface of ISTL |
| 81 |
typedef M matrix_type; |
| 82 |
//! \brief The type of the domain. |
| 83 |
//! |
| 84 |
//! E.g. BlockVector or another type fulfilling the ISTL |
| 85 |
//! vector interface. |
| 86 |
typedef X domain_type; |
| 87 |
//! \brief The type of the range. |
| 88 |
//! |
| 89 |
//! E.g. BlockVector or another type fulfilling the ISTL |
| 90 |
//! vector interface. |
| 91 |
typedef Y range_type; |
| 92 |
//! \brief The field type of the range |
| 93 |
typedef typename X::field_type field_type; |
| 94 |
//! \brief The type of the communication object. |
| 95 |
//! |
| 96 |
//! This must either be OwnerOverlapCopyCommunication or a type |
| 97 |
//! implementing the same interface. |
| 98 |
typedef C communication_type; |
| 99 |
|
| 100 |
/** |
| 101 |
* @brief constructor: just store a reference to a matrix. |
| 102 |
* |
| 103 |
* @param A The assembled matrix. |
| 104 |
* @param com The communication object for syncing overlap and copy |
| 105 |
* data points. (E.~g. OwnerOverlapCopyCommunication ) |
| 106 |
*/ |
| 107 |
OverlappingSchwarzOperator (const matrix_type& A, const communication_type& com) |
| 108 |
: _A_(stackobject_to_shared_ptr(A)), communication(com) |
| 109 |
{} |
| 110 |
|
| 111 |
OverlappingSchwarzOperator (const std::shared_ptr<matrix_type> A, const communication_type& com) |
| 112 |
: _A_(A), communication(com) |
| 113 |
{} |
| 114 |
|
| 115 |
//! apply operator to x: \f$ y = A(x) \f$ |
| 116 |
void apply (const X& x, Y& y) const override |
| 117 |
{ |
| 118 |
y = 0; |
| 119 |
_A_->umv(x,y); // result is consistent on interior+border |
| 120 |
communication.project(y); // we want this here to avoid it before the preconditioner |
| 121 |
// since there d is const! |
| 122 |
} |
| 123 |
|
| 124 |
//! apply operator to x, scale and add: \f$ y = y + \alpha A(x) \f$ |
| 125 |
void applyscaleadd (field_type alpha, const X& x, Y& y) const override |
| 126 |
{ |
| 127 |
_A_->usmv(alpha,x,y); // result is consistent on interior+border |
| 128 |
communication.project(y); // we want this here to avoid it before the preconditioner |
| 129 |
// since there d is const! |
| 130 |
} |
| 131 |
|
| 132 |
//! get the sequential assembled linear operator. |
| 133 |
const matrix_type& getmat () const override |
| 134 |
{ |
| 135 |
return *_A_; |
| 136 |
} |
| 137 |
|
| 138 |
//! Category of the linear operator (see SolverCategory::Category) |
| 139 |
SolverCategory::Category category() const override |
| 140 |
{ |
| 141 |
return SolverCategory::overlapping; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
//! Get the object responsible for communication |
| 146 |
const communication_type& getCommunication() const |
| 147 |
{ |
| 148 |
return communication; |
| 149 |
} |
| 150 |
private: |
| 151 |
const std::shared_ptr<const matrix_type>_A_; |
| 152 |
const communication_type& communication; |
| 153 |
}; |
| 154 |
|
| 155 |
/** @} */ |
| 156 |
|
| 157 |
/** |
| 158 |
* \addtogroup ISTL_Prec |
| 159 |
* @{ |
| 160 |
*/ |
| 161 |
//! \brief A parallel SSOR preconditioner. |
| 162 |
//! \tparam M The type of the sequential matrix to use, |
| 163 |
//! e.g. BCRSMatrix or another matrix type fulfilling the |
| 164 |
//! matrix interface of ISTL. |
| 165 |
//! \tparam X The type of the sequential vector to use for the left hand side, |
| 166 |
//! e.g. BlockVector or another type fulfilling the ISTL |
| 167 |
//! vector interface. |
| 168 |
//! \tparam Y The type of the sequential vector to use for the right hand side, |
| 169 |
//! e..g. BlockVector or another type fulfilling the ISTL |
| 170 |
//! vector interface. |
| 171 |
//! \tparam C The type of the communication object. |
| 172 |
//! This must either be OwnerOverlapCopyCommunication or a type |
| 173 |
//! implementing the same interface. |
| 174 |
template<class M, class X, class Y, class C> |
| 175 |
class ParSSOR : public Preconditioner<X,Y> { |
| 176 |
public: |
| 177 |
//! \brief The matrix type the preconditioner is for. |
| 178 |
typedef M matrix_type; |
| 179 |
//! \brief The domain type of the preconditioner. |
| 180 |
typedef X domain_type; |
| 181 |
//! \brief The range type of the preconditioner. |
| 182 |
typedef Y range_type; |
| 183 |
//! \brief The field type of the preconditioner. |
| 184 |
typedef typename X::field_type field_type; |
| 185 |
//! \brief The type of the communication object. |
| 186 |
typedef C communication_type; |
| 187 |
|
| 188 |
/*! \brief Constructor. |
| 189 |
|
| 190 |
constructor gets all parameters to operate the prec. |
| 191 |
\param A The matrix to operate on. |
| 192 |
\param n The number of iterations to perform. |
| 193 |
\param w The relaxation factor. |
| 194 |
\param c The communication object for syncing overlap and copy |
| 195 |
* data points. (E.~g. OwnerOverlapCopyCommunication ) |
| 196 |
*/ |
| 197 |
ParSSOR (const matrix_type& A, int n, field_type w, const communication_type& c) |
| 198 |
: _A_(A), _n(n), _w(w), communication(c) |
| 199 |
{ } |
| 200 |
|
| 201 |
/*! |
| 202 |
\brief Prepare the preconditioner. |
| 203 |
|
| 204 |
\copydoc Preconditioner::pre(X&,Y&) |
| 205 |
*/ |
| 206 |
void pre (X& x, [[maybe_unused]] Y& b) override |
| 207 |
{ |
| 208 |
communication.copyOwnerToAll(x,x); // make dirichlet values consistent |
| 209 |
} |
| 210 |
|
| 211 |
/*! |
| 212 |
\brief Apply the preconditioner |
| 213 |
|
| 214 |
\copydoc Preconditioner::apply(X&,const Y&) |
| 215 |
*/ |
| 216 |
void apply (X& v, const Y& d) override |
| 217 |
{ |
| 218 |
for (int i=0; i<_n; i++) { |
| 219 |
bsorf(_A_,v,d,_w); |
| 220 |
bsorb(_A_,v,d,_w); |
| 221 |
} |
| 222 |
communication.copyOwnerToAll(v,v); |
| 223 |
} |
| 224 |
|
| 225 |
/*! |
| 226 |
\brief Clean up. |
| 227 |
|
| 228 |
\copydoc Preconditioner::post(X&) |
| 229 |
*/ |
| 230 |
void post ([[maybe_unused]] X& x) override {} |
| 231 |
|
| 232 |
//! Category of the preconditioner (see SolverCategory::Category) |
| 233 |
SolverCategory::Category category() const override |
| 234 |
{ |
| 235 |
return SolverCategory::overlapping; |
| 236 |
} |
| 237 |
|
| 238 |
private: |
| 239 |
//! \brief The matrix we operate on. |
| 240 |
const matrix_type& _A_; |
| 241 |
//! \brief The number of steps to do in apply |
| 242 |
int _n; |
| 243 |
//! \brief The relaxation factor to use |
| 244 |
field_type _w; |
| 245 |
//! \brief the communication object |
| 246 |
const communication_type& communication; |
| 247 |
}; |
| 248 |
|
| 249 |
namespace Amg |
| 250 |
{ |
| 251 |
template<class T> struct ConstructionTraits; |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* @brief Block parallel preconditioner. |
| 256 |
* |
| 257 |
* This is essentially a wrapper that takes a sequential |
| 258 |
* preconditioner. In each step the sequential preconditioner |
| 259 |
* is applied and then all owner data points are updated on |
| 260 |
* all other processes. |
| 261 |
* \tparam M The type of the sequential matrix to use, |
| 262 |
* e.g. BCRSMatrix or another matrix type fulfilling the |
| 263 |
* matrix interface of ISTL. |
| 264 |
* \tparam X The type of the sequential vector to use for the left hand side, |
| 265 |
* e.g. BlockVector or another type fulfilling the ISTL |
| 266 |
* vector interface. |
| 267 |
* \tparam Y The type of the sequential vector to use for the right hand side, |
| 268 |
* e..g. BlockVector or another type fulfilling the ISTL |
| 269 |
* vector interface. |
| 270 |
* \tparam C The type of the communication object. |
| 271 |
* This must either be OwnerOverlapCopyCommunication or a type |
| 272 |
* implementing the same interface. |
| 273 |
* \tparam The type of the sequential preconditioner to use |
| 274 |
* for approximately solving the local matrix block consisting of unknowns |
| 275 |
* owned by the process. Has to implement the Preconditioner interface. |
| 276 |
*/ |
| 277 |
template<class X, class Y, class C, class P=Preconditioner<X,Y> > |
| 278 |
class BlockPreconditioner : public Preconditioner<X,Y> { |
| 279 |
friend struct Amg::ConstructionTraits<BlockPreconditioner<X,Y,C,P> >; |
| 280 |
public: |
| 281 |
//! \brief The domain type of the preconditioner. |
| 282 |
//! |
| 283 |
//! E.g. BlockVector or another type fulfilling the ISTL |
| 284 |
//! vector interface. |
| 285 |
typedef X domain_type; |
| 286 |
//! \brief The range type of the preconditioner. |
| 287 |
//! |
| 288 |
//! E.g. BlockVector or another type fulfilling the ISTL |
| 289 |
//! vector interface. |
| 290 |
typedef Y range_type; |
| 291 |
//! \brief The field type of the preconditioner. |
| 292 |
typedef typename X::field_type field_type; |
| 293 |
//! \brief The type of the communication object.. |
| 294 |
//! |
| 295 |
//! This must either be OwnerOverlapCopyCommunication or a type |
| 296 |
//! implementing the same interface. |
| 297 |
typedef C communication_type; |
| 298 |
|
| 299 |
/*! \brief Constructor. |
| 300 |
|
| 301 |
constructor gets all parameters to operate the prec. |
| 302 |
\param p The sequential preconditioner. |
| 303 |
\param c The communication object for syncing overlap and copy |
| 304 |
data points. (E.~g. OwnerOverlapCopyCommunication ) |
| 305 |
*/ |
| 306 |
BlockPreconditioner (P& p, const communication_type& c) |
| 307 |
: _preconditioner(stackobject_to_shared_ptr(p)), _communication(c) |
| 308 |
{ } |
| 309 |
|
| 310 |
/*! \brief Constructor. |
| 311 |
|
| 312 |
constructor gets all parameters to operate the prec. |
| 313 |
\param p The sequential preconditioner. |
| 314 |
\param c The communication object for syncing overlap and copy |
| 315 |
data points. (E.~g. OwnerOverlapCopyCommunication ) |
| 316 |
*/ |
| 317 |
BlockPreconditioner (const std::shared_ptr<P>& p, const communication_type& c) |
| 318 |
: _preconditioner(p), _communication(c) |
| 319 |
{ } |
| 320 |
|
| 321 |
/*! |
| 322 |
\brief Prepare the preconditioner. |
| 323 |
|
| 324 |
\copydoc Preconditioner::pre(X&,Y&) |
| 325 |
*/ |
| 326 |
void pre (X& x, Y& b) override |
| 327 |
{ |
| 328 |
_communication.copyOwnerToAll(x,x); // make dirichlet values consistent |
| 329 |
_preconditioner->pre(x,b); |
| 330 |
} |
| 331 |
|
| 332 |
/*! |
| 333 |
\brief Apply the preconditioner |
| 334 |
|
| 335 |
\copydoc Preconditioner::apply(X&,const Y&) |
| 336 |
*/ |
| 337 |
void apply (X& v, const Y& d) override |
| 338 |
{ |
| 339 |
_preconditioner->apply(v,d); |
| 340 |
_communication.copyOwnerToAll(v,v); |
| 341 |
} |
| 342 |
|
| 343 |
template<bool forward> |
| 344 |
void apply (X& v, const Y& d) |
| 345 |
{ |
| 346 |
_preconditioner->template apply<forward>(v,d); |
| 347 |
_communication.copyOwnerToAll(v,v); |
| 348 |
} |
| 349 |
|
| 350 |
/*! |
| 351 |
\brief Clean up. |
| 352 |
|
| 353 |
\copydoc Preconditioner::post(X&) |
| 354 |
*/ |
| 355 |
void post (X& x) override |
| 356 |
{ |
| 357 |
_preconditioner->post(x); |
| 358 |
} |
| 359 |
|
| 360 |
//! Category of the preconditioner (see SolverCategory::Category) |
| 361 |
SolverCategory::Category category() const override |
| 362 |
{ |
| 363 |
return SolverCategory::overlapping; |
| 364 |
} |
| 365 |
|
| 366 |
private: |
| 367 |
//! \brief a sequential preconditioner |
| 368 |
std::shared_ptr<P> _preconditioner; |
| 369 |
|
| 370 |
//! \brief the communication object |
| 371 |
const communication_type& _communication; |
| 372 |
}; |
| 373 |
|
| 374 |
/** @} end documentation */ |
| 375 |
|
| 376 |
} // end namespace |
| 377 |
|
| 378 |
#endif |