10#ifndef BELOS_SOLUTION_PROJECTION_HPP
11#define BELOS_SOLUTION_PROJECTION_HPP
50#include "Teuchos_ParameterList.hpp"
51#include "Teuchos_RCP.hpp"
52#include "Teuchos_ScalarTraits.hpp"
74template<
class ScalarType,
class MV,
class OP,
81 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
83 typedef Teuchos::ScalarTraits<MagnitudeType>
MT;
87 const int maxBasisSize) :
89 maxBasisSize_(maxBasisSize),
91 replacementStrategy_(
"Restart"),
93 reorthogonalizationSteps_(2),
102 const Teuchos::RCP<Teuchos::ParameterList>& params) :
106 replacementStrategy_(
"Restart"),
108 reorthogonalizationSteps_(2),
112 if (!params.is_null()) {
114 params->get(
"Maximum Basis Size", maxBasisSize_);
115 replacementStrategy_ =
116 params->get(
"Replacement Strategy", replacementStrategy_);
118 params->get(
"Projection Tolerance", projectionTol_);
119 reorthogonalizationSteps_ =
120 params->get(
"Reorthogonalization Steps", reorthogonalizationSteps_);
123 validateParameters();
143 return curBasisSize_;
148 return maxBasisSize_;
153 replacementStrategy_ = strategy;
154 validateParameters();
159 return replacementStrategy_;
164 projectionTol_ = tol;
165 validateParameters();
170 return projectionTol_;
175 reorthogonalizationSteps_ = steps;
176 validateParameters();
181 return reorthogonalizationSteps_;
186 if (curBasisSize_ == 0 || U_.is_null()) {
187 return Teuchos::null;
190 std::vector<int> ind(curBasisSize_);
191 for (
int i = 0; i < curBasisSize_; ++i) {
200 if (curBasisSize_ == 0 || C_.is_null()) {
201 return Teuchos::null;
204 std::vector<int> ind(curBasisSize_);
205 for (
int i = 0; i < curBasisSize_; ++i) {
214 TEUCHOS_TEST_FOR_EXCEPTION(
217 "Belos::SolutionProjection::addSolution(): operator is null.");
221 TEUCHOS_TEST_FOR_EXCEPTION(
223 std::invalid_argument,
224 "Belos::SolutionProjection::addSolution(): input multivector must contain at least one vector.");
237 TEUCHOS_TEST_FOR_EXCEPTION(
239 std::invalid_argument,
240 "Belos::SolutionProjection::addPair(): input multivector must contain at least one vector.");
242 TEUCHOS_TEST_FOR_EXCEPTION(
244 std::invalid_argument,
245 "Belos::SolutionProjection::addPair(): u and c must have the same number of vectors.");
247 TEUCHOS_TEST_FOR_EXCEPTION(
249 std::invalid_argument,
250 "Belos::SolutionProjection::addPair(): u and c must have the same global length.");
252 if (maxBasisSize_ == 0) {
260 for (
int j = 0; j < numVecs; ++j) {
261 std::vector<int> srcInd(1);
270 const bool ok = addOnePair(*uNew, *cNew);
281 if (curBasisSize_ == 0) {
285 validateProjectionInputs(x, r);
287 const ScalarType one = SCT::one();
291 std::vector<int> ind(curBasisSize_);
292 for (
int i = 0; i < curBasisSize_; ++i) {
299 Teuchos::RCP<DM> gamma =
DMT::Create(curBasisSize_, numRhs);
314 Teuchos::RCP<MV> projectedResidual = Teuchos::null)
const {
315 TEUCHOS_TEST_FOR_EXCEPTION(
318 "Belos::SolutionProjection::project(): operator is null.");
320 validateProjectionInputs(x, b);
322 if (!projectedResidual.is_null()) {
323 TEUCHOS_TEST_FOR_EXCEPTION(
325 std::invalid_argument,
326 "Belos::SolutionProjection::project(): projectedResidual must have the same number of vectors as b.");
328 TEUCHOS_TEST_FOR_EXCEPTION(
330 std::invalid_argument,
331 "Belos::SolutionProjection::project(): projectedResidual must have the same global length as b.");
341 if (!projectedResidual.is_null()) {
348 Teuchos::RCP<MV> projectedResidual = Teuchos::null)
const {
351 TEUCHOS_TEST_FOR_EXCEPTION(
354 "Belos::SolutionProjection::project(LinearProblem): current LHS is null. "
355 "Did you call setLSIndex() or otherwise set the current linear system?");
359 TEUCHOS_TEST_FOR_EXCEPTION(
362 "Belos::SolutionProjection::project(LinearProblem): current RHS is null. "
363 "Did you call setLSIndex() or otherwise set the current linear system?");
365 if (!projectedResidual.is_null()) {
366 TEUCHOS_TEST_FOR_EXCEPTION(
368 std::invalid_argument,
369 "Belos::SolutionProjection::project(LinearProblem): projectedResidual has wrong number of vectors.");
371 TEUCHOS_TEST_FOR_EXCEPTION(
373 std::invalid_argument,
374 "Belos::SolutionProjection::project(LinearProblem): projectedResidual has wrong global length.");
384 if (!projectedResidual.is_null()) {
390 void validateParameters()
const {
391 TEUCHOS_TEST_FOR_EXCEPTION(
393 std::invalid_argument,
394 "Belos::SolutionProjection: maximum basis size must be nonnegative.");
396 TEUCHOS_TEST_FOR_EXCEPTION(
397 projectionTol_ < MT::zero(),
398 std::invalid_argument,
399 "Belos::SolutionProjection: projection tolerance must be nonnegative.");
401 TEUCHOS_TEST_FOR_EXCEPTION(
402 reorthogonalizationSteps_ < 1,
403 std::invalid_argument,
404 "Belos::SolutionProjection: reorthogonalization steps must be at least one.");
406 TEUCHOS_TEST_FOR_EXCEPTION(
407 replacementStrategy_ !=
"Restart",
408 std::invalid_argument,
409 "Belos::SolutionProjection: currently only \"Restart\" replacement strategy is implemented.");
412 void validateProjectionInputs(
const MV& x,
const MV& r)
const {
413 TEUCHOS_TEST_FOR_EXCEPTION(
415 std::invalid_argument,
416 "Belos::SolutionProjection::project(): x and residual/right-hand side must have the same number of vectors.");
418 TEUCHOS_TEST_FOR_EXCEPTION(
420 std::invalid_argument,
421 "Belos::SolutionProjection::project(): x and residual/right-hand side must have the same global length.");
423 TEUCHOS_TEST_FOR_EXCEPTION(
425 std::invalid_argument,
426 "Belos::SolutionProjection::project(): x has incompatible global length with stored basis.");
428 TEUCHOS_TEST_FOR_EXCEPTION(
430 std::invalid_argument,
431 "Belos::SolutionProjection::project(): residual/right-hand side has incompatible global length with stored basis.");
435 void ensureStorage(
const MV& uPrototype,
const MV& cPrototype) {
440 Teuchos::RCP<const MV> tmp = U_;
449 Teuchos::RCP<const MV> tmp = C_;
455 bool addOnePair(MV& uNew, MV& cNew) {
456 const ScalarType one = SCT::one();
458 std::vector<MagnitudeType> initialNorm(1);
461 if (initialNorm[0] == MT::zero()) {
465 if (curBasisSize_ == maxBasisSize_) {
466 if (replacementStrategy_ ==
"Restart") {
472 TEUCHOS_TEST_FOR_EXCEPTION(
474 SolutionProjectionFailure,
475 "Belos::SolutionProjection: unsupported replacement strategy.");
479 for (
int pass = 0; pass < reorthogonalizationSteps_; ++pass) {
480 if (curBasisSize_ == 0) {
484 std::vector<int> ind(curBasisSize_);
485 for (
int i = 0; i < curBasisSize_; ++i) {
492 Teuchos::RCP<DM> h =
DMT::Create(curBasisSize_, 1);
506 std::vector<MagnitudeType> finalNorm(1);
509 if (finalNorm[0] <= projectionTol_ * initialNorm[0]) {
513 const ScalarType scale =
514 static_cast<ScalarType
>(MT::one() / finalNorm[0]);
519 std::vector<int> dstInd(1);
520 dstInd[0] = curBasisSize_;
531 Teuchos::RCP<const OP> A_;
536 std::string replacementStrategy_;
542 int reorthogonalizationSteps_;
Belos header file which uses auto-configuration information to include necessary C++ headers.
Full specialization of Belos::DenseMatTraits for Kokkos::DualView with arbitrary scalarType....
Class which describes the linear problem to be solved by the iterative solver.
Declaration of basic traits for the multivector type.
Class which defines basic traits for the operator type.
Full specialization of Belos::DenseMatTraits for Teuchos::SerialDenseMatrix with ordinal type int and...
Collection of types and exceptions used within the Belos solvers.
BelosError(const std::string &what_arg)
Virtual base class which defines basic traits for the multi-vector type.
static Teuchos::RCP< DM > Create()
Creates a new empty DM with no dimension.
virtual void computeCurrResVec(MV *R, const MV *X=0, const MV *B=0) const
Compute a residual R for this operator given a solution X, and right-hand side B.
Teuchos::RCP< const MV > getCurrRHSVec()
Get a pointer to the current right-hand side of the linear system.
Teuchos::RCP< MV > getCurrLHSVec()
Get a pointer to the current left-hand side (solution) of the linear system.
Traits class which defines basic operations on multivectors.
static void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const DM &B, const ScalarType beta, MV &mv)
static void Assign(const MV &A, MV &mv)
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, DM &B)
static ptrdiff_t GetGlobalLength(const MV &mv)
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
static void MvScale(MV &mv, const ScalarType alpha)
static int GetNumberVecs(const MV &mv)
Class which defines basic traits for the operator type.
static void Apply(const OP &Op, const MV &x, MV &y, ETrans trans=NOTRANS)
Apply Op to x, putting the result into y.
SolutionProjectionFailure(const std::string &what_arg)
std::string getReplacementStrategy() const
Get replacement strategy.
int addSolution(const MV &u)
Add solution vector(s) u. The class computes c = A u.
void reset()
Clear the stored projection basis.
MagnitudeType getProjectionTolerance() const
Get relative projection tolerance.
void project(MV &x, const MV &b, Teuchos::RCP< MV > projectedResidual=Teuchos::null) const
Compute r = b - A x, then apply projection to x and r.
void setOperator(const Teuchos::RCP< const OP > &A)
Set or replace the operator.
Teuchos::ScalarTraits< MagnitudeType > MT
Teuchos::RCP< const MV > getCorrectionBasis() const
Get correction basis U.
SCT::magnitudeType MagnitudeType
void setReplacementStrategy(const std::string &strategy)
Set replacement strategy.
void setProjectionTolerance(const MagnitudeType tol)
Set relative projection tolerance.
Teuchos::RCP< const MV > getImageBasis() const
Get image basis C.
int addPair(const MV &u, const MV &c)
Add pair(s) u and c, where c should equal A u.
int getBasisSize() const
Current number of stored basis vectors.
MultiVecTraits< ScalarType, MV, DM > MVT
void project(MV &x, MV &r) const
Apply projection to x and r, where r is assumed to be b - A x.
SolutionProjection(const Teuchos::RCP< const OP > &A, const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Constructor with parameter list.
SolutionProjection(const Teuchos::RCP< const OP > &A, const int maxBasisSize)
Constructor with explicit maximum basis size.
void project(LinearProblem< ScalarType, MV, OP, DM > &problem, Teuchos::RCP< MV > projectedResidual=Teuchos::null) const
Project the current solution in a LinearProblem.
Teuchos::ScalarTraits< ScalarType > SCT
void setReorthogonalizationSteps(const int steps)
Set number of reorthogonalization passes.
int getReorthogonalizationSteps() const
Get number of reorthogonalization passes.
OperatorTraits< ScalarType, MV, OP > OPT
DenseMatTraits< ScalarType, DM > DMT
int getMaxBasisSize() const
Maximum number of stored basis vectors.
Teuchos::SerialDenseMatrix< Ordinal, Scalar > DefaultDenseMatrix