| 1 |
|
| 2 |
// @HEADER |
| 3 |
// ***************************************************************************** |
| 4 |
// Belos: Block Linear Solvers Package |
| 5 |
// |
| 6 |
// Copyright 2004-2016 NTESS and the Belos contributors. |
| 7 |
// SPDX-License-Identifier: BSD-3-Clause |
| 8 |
// ***************************************************************************** |
| 9 |
// @HEADER |
| 10 |
|
| 11 |
#ifndef BELOS_LINEAR_MULTIPROBLEM_HPP |
| 12 |
#define BELOS_LINEAR_MULTIPROBLEM_HPP |
| 13 |
|
| 14 |
/// \file BelosLinearMultiShiftProblem.hpp |
| 15 |
/// \brief Class which describes the linear problem to be solved by |
| 16 |
/// the iterative solver. |
| 17 |
#include "BelosLinearProblem.hpp" |
| 18 |
#include "BelosMultiVecTraits.hpp" |
| 19 |
#include "BelosOperatorTraits.hpp" |
| 20 |
#include "Teuchos_ParameterList.hpp" |
| 21 |
#include "Teuchos_TimeMonitor.hpp" |
| 22 |
|
| 23 |
namespace Belos { |
| 24 |
|
| 25 |
//@} |
| 26 |
|
| 27 |
/// \class LinearMultiShiftProblem |
| 28 |
/// \brief A linear system to solve multiple matrix and right-hand sides. |
| 29 |
/// |
| 30 |
/// This class encapsulates the general information needed for |
| 31 |
/// solving a set of linear systems of equations using an iterative method. |
| 32 |
/// A_i*x_i = b_i for i=1,...,N, where A_i is A+i*I |
| 33 |
/// |
| 34 |
/// \tparam ScalarType The type of the entries in the matrix and |
| 35 |
/// vectors. |
| 36 |
/// \tparam MV The (multi)vector type. |
| 37 |
/// \tparam OP The operator type. (Operators are functions that |
| 38 |
/// take a multivector as input and compute a multivector as |
| 39 |
/// output.) |
| 40 |
template <class ScalarType, class MV, class OP> |
| 41 |
class LinearMultiShiftProblem : public LinearProblem<ScalarType, MV, OP> { |
| 42 |
public: |
| 43 |
|
| 44 |
//! @name Constructors/Destructor |
| 45 |
//@{ |
| 46 |
|
| 47 |
/// \brief Default constructor. |
| 48 |
/// |
| 49 |
/// Creates an empty Belos::LinearMultiShiftProblem instance. The operator |
| 50 |
/// A, left-hand-side X and right-hand-side B must be set using |
| 51 |
/// the \c setOperator(), \c setLHS(), and \c setRHS() methods |
| 52 |
/// respectively. |
| 53 |
LinearMultiShiftProblem (void); |
| 54 |
|
| 55 |
/// \brief Unpreconditioned linear system constructor. |
| 56 |
/// |
| 57 |
/// Creates an unpreconditioned LinearMultiShiftProblem instance with the |
| 58 |
/// operator (\c A), initial guess (\c X), and right hand side (\c |
| 59 |
/// B). Preconditioners can be set using the \c setLeftPrec() and |
| 60 |
/// \c setRightPrec() methods. |
| 61 |
LinearMultiShiftProblem (const Teuchos::RCP<const OP> &A, |
| 62 |
const Teuchos::RCP<MV> &X, |
| 63 |
const Teuchos::RCP<const MV> &B); |
| 64 |
|
| 65 |
//! Destructor (declared virtual for memory safety of derived classes). |
| 66 |
virtual ~LinearMultiShiftProblem (void) {} |
| 67 |
|
| 68 |
//@} |
| 69 |
|
| 70 |
//! @name Set / Reset method |
| 71 |
//@{ |
| 72 |
|
| 73 |
/// \brief Set up the linear problem manager. |
| 74 |
/// |
| 75 |
/// Call this method if you want to solve the linear system with a |
| 76 |
/// different left- or right-hand side, or if you want to prepare |
| 77 |
/// the linear problem to solve the linear system that was already |
| 78 |
/// passed in. (In the latter case, call this method with the |
| 79 |
/// default arguments.) The internal flags will be set as if the |
| 80 |
/// linear system manager was just initialized, and the initial |
| 81 |
/// residual will be computed. |
| 82 |
/// |
| 83 |
/// Many of Belos' solvers require that this method has been |
| 84 |
/// called on the linear problem, before they can solve it. |
| 85 |
/// |
| 86 |
/// \param newX [in/out] If you want to solve the linear system |
| 87 |
/// with a different left-hand side, pass it in here. |
| 88 |
/// Otherwise, set this to null (the default value). |
| 89 |
/// |
| 90 |
/// \param newB [in] If you want to solve the linear system with a |
| 91 |
/// different right-hand side, pass it in here. Otherwise, set |
| 92 |
/// this to null (the default value). |
| 93 |
/// |
| 94 |
/// \return Whether the linear problem was successfully set up. |
| 95 |
/// Successful setup requires at least that the matrix operator |
| 96 |
/// A, the left-hand side X, and the right-hand side B all be |
| 97 |
/// non-null. |
| 98 |
bool |
| 99 |
setProblem (const Teuchos::RCP<MV> &newX = Teuchos::null, |
| 100 |
const Teuchos::RCP<const MV> &newB = Teuchos::null); |
| 101 |
|
| 102 |
|
| 103 |
void |
| 104 |
setShift( bool addShift ) |
| 105 |
{ |
| 106 |
addShift_ = addShift; |
| 107 |
} |
| 108 |
|
| 109 |
//@} |
| 110 |
|
| 111 |
//! @name Apply / Compute methods |
| 112 |
//@{ |
| 113 |
|
| 114 |
/// \brief Apply ONLY the operator to \c x, returning \c y. |
| 115 |
/// |
| 116 |
/// This method only applies the linear problem's operator, |
| 117 |
/// without any preconditioners that may have been defined. |
| 118 |
/// Flexible variants of Krylov methods will use this method. If |
| 119 |
/// no operator has been defined, this method just copies x into |
| 120 |
/// y. |
| 121 |
void applyOp( const MV& x, MV& y ) const; |
| 122 |
|
| 123 |
//@} |
| 124 |
|
| 125 |
private: |
| 126 |
bool addShift_; |
| 127 |
|
| 128 |
using LinearProblem<ScalarType, MV, OP>::A_; |
| 129 |
using LinearProblem<ScalarType, MV, OP>::X_; |
| 130 |
using LinearProblem<ScalarType, MV, OP>::curX_; |
| 131 |
using LinearProblem<ScalarType, MV, OP>::B_; |
| 132 |
using LinearProblem<ScalarType, MV, OP>::curB_; |
| 133 |
using LinearProblem<ScalarType, MV, OP>::R0_; |
| 134 |
using LinearProblem<ScalarType, MV, OP>::PR0_; |
| 135 |
using LinearProblem<ScalarType, MV, OP>::R0_user_; |
| 136 |
using LinearProblem<ScalarType, MV, OP>::PR0_user_; |
| 137 |
using LinearProblem<ScalarType, MV, OP>::LP_; |
| 138 |
using LinearProblem<ScalarType, MV, OP>::RP_; |
| 139 |
using LinearProblem<ScalarType, MV, OP>::timerOp_; |
| 140 |
using LinearProblem<ScalarType, MV, OP>::timerPrec_; |
| 141 |
using LinearProblem<ScalarType, MV, OP>::blocksize_; |
| 142 |
using LinearProblem<ScalarType, MV, OP>::num2Solve_; |
| 143 |
using LinearProblem<ScalarType, MV, OP>::rhsIndex_; |
| 144 |
using LinearProblem<ScalarType, MV, OP>::lsNum_; |
| 145 |
using LinearProblem<ScalarType, MV, OP>::isSet_; |
| 146 |
using LinearProblem<ScalarType, MV, OP>::solutionUpdated_; |
| 147 |
using LinearProblem<ScalarType, MV, OP>::label_; |
| 148 |
|
| 149 |
typedef MultiVecTraits<ScalarType,MV> MVT; |
| 150 |
typedef OperatorTraits<ScalarType,MV,OP> OPT; |
| 151 |
}; |
| 152 |
|
| 153 |
//-------------------------------------------- |
| 154 |
// Constructor Implementations |
| 155 |
//-------------------------------------------- |
| 156 |
|
| 157 |
template <class ScalarType, class MV, class OP> |
| 158 |
LinearMultiShiftProblem<ScalarType,MV,OP>::LinearMultiShiftProblem(void) |
| 159 |
: LinearProblem<ScalarType,MV,OP>(), |
| 160 |
addShift_(true) |
| 161 |
{} |
| 162 |
|
| 163 |
template <class ScalarType, class MV, class OP> |
| 164 |
LinearMultiShiftProblem<ScalarType,MV,OP>::LinearMultiShiftProblem(const Teuchos::RCP<const OP> &A, |
| 165 |
const Teuchos::RCP<MV> &X, |
| 166 |
const Teuchos::RCP<const MV> &B |
| 167 |
) |
| 168 |
: LinearProblem<ScalarType,MV,OP>(A, X, B), |
| 169 |
addShift_(true) |
| 170 |
{ |
| 171 |
int numVecs = MVT::GetNumberVecs( *B ); |
| 172 |
rhsIndex_.resize(numVecs); |
| 173 |
for (int i=0; i<numVecs; ++i) |
| 174 |
rhsIndex_[i] = i; |
| 175 |
} |
| 176 |
|
| 177 |
template <class ScalarType, class MV, class OP> |
| 178 |
bool |
| 179 |
LinearMultiShiftProblem<ScalarType,MV,OP>:: |
| 180 |
setProblem (const Teuchos::RCP<MV> &newX, |
| 181 |
const Teuchos::RCP<const MV> &newB) |
| 182 |
{ |
| 183 |
// Create timers if the haven't been created yet. |
| 184 |
if (timerOp_ == Teuchos::null) { |
| 185 |
std::string opLabel = label_ + ": Operation Op*x"; |
| 186 |
#ifdef BELOS_TEUCHOS_TIME_MONITOR |
| 187 |
timerOp_ = Teuchos::TimeMonitor::getNewCounter( opLabel ); |
| 188 |
#endif |
| 189 |
} |
| 190 |
if (timerPrec_ == Teuchos::null) { |
| 191 |
std::string precLabel = label_ + ": Operation Prec*x"; |
| 192 |
#ifdef BELOS_TEUCHOS_TIME_MONITOR |
| 193 |
timerPrec_ = Teuchos::TimeMonitor::getNewCounter( precLabel ); |
| 194 |
#endif |
| 195 |
} |
| 196 |
|
| 197 |
// Set the linear system using the arguments newX and newB |
| 198 |
if (newX != Teuchos::null) |
| 199 |
X_ = newX; |
| 200 |
if (newB != Teuchos::null) |
| 201 |
B_ = newB; |
| 202 |
|
| 203 |
// Invalidate the current linear system indices and multivectors. |
| 204 |
curX_ = Teuchos::null; |
| 205 |
curB_ = Teuchos::null; |
| 206 |
|
| 207 |
// Temporarily set the rhsIndex_ to all the vectors of the linear system. |
| 208 |
// This is necessary for the applyOp to perform correctly in computing the |
| 209 |
// initial residual. It will be reset at the end of this method. |
| 210 |
int numVecs = MVT::GetNumberVecs( *B_ ); |
| 211 |
rhsIndex_.resize(numVecs); |
| 212 |
for (int i=0; i<numVecs; ++i) |
| 213 |
rhsIndex_[i] = i; |
| 214 |
|
| 215 |
// If we didn't set a matrix A, a left-hand side X, or a |
| 216 |
// right-hand side B, then we didn't set the problem. |
| 217 |
if (A_ == Teuchos::null || X_ == Teuchos::null || B_ == Teuchos::null) { |
| 218 |
isSet_ = false; |
| 219 |
return isSet_; |
| 220 |
} |
| 221 |
|
| 222 |
// Reset whether the solution has been updated. (We're just |
| 223 |
// setting the problem now, so of course we haven't updated the |
| 224 |
// solution yet.) |
| 225 |
solutionUpdated_ = false; |
| 226 |
|
| 227 |
// Compute the initial residuals. |
| 228 |
if(Teuchos::is_null(R0_user_)) { |
| 229 |
if (R0_==Teuchos::null || MVT::GetNumberVecs( *R0_ )!=MVT::GetNumberVecs( *B_ )) { |
| 230 |
R0_ = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) ); |
| 231 |
} |
| 232 |
this->computeCurrResVec( &*R0_, &*X_, &*B_ ); |
| 233 |
|
| 234 |
if (LP_!=Teuchos::null) { |
| 235 |
if (PR0_==Teuchos::null || (PR0_==R0_) || (MVT::GetNumberVecs(*PR0_)!=MVT::GetNumberVecs(*B_))) { |
| 236 |
PR0_ = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) ); |
| 237 |
} |
| 238 |
this->applyLeftPrec( *R0_, *PR0_ ); |
| 239 |
} |
| 240 |
else { |
| 241 |
PR0_ = R0_; |
| 242 |
} |
| 243 |
} |
| 244 |
else { // User specified the residuals |
| 245 |
// If the user did not specify the right sized residual, create one and set R0_user_ to null. |
| 246 |
if (MVT::GetNumberVecs( *R0_user_ )!=MVT::GetNumberVecs( *B_ )) { |
| 247 |
Teuchos::RCP<MV> helper = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) ); |
| 248 |
this->computeCurrResVec( &*helper, &*X_, &*B_ ); |
| 249 |
R0_user_ = Teuchos::null; |
| 250 |
R0_ = helper; |
| 251 |
} |
| 252 |
|
| 253 |
if (LP_!=Teuchos::null) { |
| 254 |
// If the user provided preconditioned residual is the wrong size or pointing at |
| 255 |
// the wrong object, create one and set the PR0_user_ to null. |
| 256 |
if (PR0_user_==Teuchos::null || (PR0_user_==R0_) || (PR0_user_==R0_user_) |
| 257 |
|| (MVT::GetNumberVecs(*PR0_user_)!=MVT::GetNumberVecs(*B_))) { |
| 258 |
Teuchos::RCP<MV> helper = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) ); |
| 259 |
|
| 260 |
// Get the initial residual from getInitResVec because R0_user_ may be null from above. |
| 261 |
this->applyLeftPrec( *(this->getInitResVec()), *helper ); |
| 262 |
PR0_user_ = Teuchos::null; |
| 263 |
PR0_ = helper; |
| 264 |
} |
| 265 |
} |
| 266 |
else { |
| 267 |
// The preconditioned initial residual vector is the residual vector. |
| 268 |
// NOTE: R0_user_ could be null if incompatible. |
| 269 |
if (R0_user_!=Teuchos::null) |
| 270 |
{ |
| 271 |
PR0_user_ = R0_user_; |
| 272 |
} |
| 273 |
else |
| 274 |
{ |
| 275 |
PR0_user_ = Teuchos::null; |
| 276 |
PR0_ = R0_; |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
// Resize the rhsIndex_ back to zero length to correspond with null pointers for curX_ and curB_. |
| 282 |
rhsIndex_.resize(0); |
| 283 |
|
| 284 |
// The problem has been set and is ready for use. |
| 285 |
isSet_ = true; |
| 286 |
|
| 287 |
// Return isSet. |
| 288 |
return isSet_; |
| 289 |
} |
| 290 |
|
| 291 |
template <class ScalarType, class MV, class OP> |
| 292 |
void LinearMultiShiftProblem<ScalarType,MV,OP>::applyOp( const MV& x, MV& y ) const { |
| 293 |
|
| 294 |
if (A_.get()) |
| 295 |
{ |
| 296 |
{ |
| 297 |
#ifdef BELOS_TEUCHOS_TIME_MONITOR |
| 298 |
Teuchos::TimeMonitor OpTimer(*timerOp_); |
| 299 |
#endif |
| 300 |
OPT::Apply( *A_,x, y); |
| 301 |
} |
| 302 |
// Now add in shift based on rhsIndex_ |
| 303 |
if (addShift_) |
| 304 |
{ |
| 305 |
std::vector<int> newIdx( 1 ); |
| 306 |
for (unsigned int i=0; i<rhsIndex_.size(); ++i) |
| 307 |
{ |
| 308 |
newIdx[0] = i; |
| 309 |
Teuchos::RCP<const MV> tmp_x = MVT::CloneView( x, newIdx ); |
| 310 |
Teuchos::RCP<MV> tmp_y = MVT::CloneViewNonConst( y, newIdx ); |
| 311 |
MVT::MvAddMv( rhsIndex_[i]*Teuchos::ScalarTraits<ScalarType>::one(), *tmp_x, |
| 312 |
Teuchos::ScalarTraits<ScalarType>::one(), *tmp_y, *tmp_y ); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
else { |
| 317 |
MVT::MvAddMv( Teuchos::ScalarTraits<ScalarType>::one(), x, |
| 318 |
Teuchos::ScalarTraits<ScalarType>::zero(), x, y ); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
} // end Belos namespace |
| 323 |
|
| 324 |
#endif /* BELOS_LINEAR_MULTIPROBLEM_HPP */ |
| 325 |
|
| 326 |
|