Teko Version of the Day
Loading...
Searching...
No Matches
Teko_DiagonalPreconditionerOp.cpp
1// @HEADER
2// *****************************************************************************
3// Teko: A package for block and physics based preconditioning
4//
5// Copyright 2010 NTESS and the Teko contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#include "Teko_DiagonalPreconditionerOp.hpp"
11
12#include "TpetraExt_PointToBlockDiagPermute_decl.hpp"
13#include "Thyra_TpetraThyraWrappers.hpp"
14#include "Tpetra_MultiVector.hpp"
15
16using Teuchos::RCP;
17using Teuchos::rcp_const_cast;
18using Teuchos::rcp_dynamic_cast;
19using Teuchos::rcpFromRef;
20
21using Thyra::MultiVectorBase;
22
23namespace Teko {
24
25DiagonalPreconditionerOp::DiagonalPreconditionerOp(
26 Teuchos::RCP<Tpetra::Ext::PointToBlockDiagPermute<ST, LO, GO, NT> > BDP,
27 const VectorSpace range, const VectorSpace domain)
28 : BDP_(BDP), range_(range), domain_(domain) {}
29
30void DiagonalPreconditionerOp::implicitApply(const MultiVector& x, MultiVector& y,
31 const double alpha, const double beta) const {
32 TEUCHOS_TEST_FOR_EXCEPTION(BDP_ == Teuchos::null, std::runtime_error,
33 "DiagonalPreconditionerOp::implicitApply: null BDP_");
34
35 RCP<Tpetra::CrsMatrix<ST, LO, GO, NT> > H = BDP_->createCrsMatrix();
36 TEUCHOS_TEST_FOR_EXCEPTION(H == Teuchos::null, std::runtime_error,
37 "DiagonalPreconditionerOp::implicitApply: null block diagonal matrix");
38
39 RCP<const Tpetra::MultiVector<ST, LO, GO, NT> > x_ =
40 Thyra::TpetraOperatorVectorExtraction<ST, LO, GO, NT>::getConstTpetraMultiVector(x);
41 RCP<Tpetra::MultiVector<ST, LO, GO, NT> > y_ =
42 Thyra::TpetraOperatorVectorExtraction<ST, LO, GO, NT>::getTpetraMultiVector(y);
43
44 TEUCHOS_ASSERT(x_ != Teuchos::null);
45 TEUCHOS_ASSERT(y_ != Teuchos::null);
46
47 // Apply the explicit block-diagonal matrix.
48 // NOTE: this mirrors the original structure, but uses Apply rather than ApplyInverse
49 // because the Tpetra block-diagonal object materializes the matrix explicitly.
50 if (beta == 0.0) {
51 H->apply(*x_, *y_);
52 scale(alpha, y);
53 } else {
54 MultiVector y0 = deepcopy(y);
55 H->apply(*x_, *y_);
56 update(alpha, y, beta, y0);
57 }
58}
59
60void DiagonalPreconditionerOp::describe(Teuchos::FancyOStream& out_arg,
61 const Teuchos::EVerbosityLevel verbLevel) const {
62 using Teuchos::OSTab;
63
64 switch (verbLevel) {
65 case Teuchos::VERB_DEFAULT:
66 case Teuchos::VERB_LOW: out_arg << this->description() << std::endl; break;
67 case Teuchos::VERB_MEDIUM:
68 case Teuchos::VERB_HIGH:
69 case Teuchos::VERB_EXTREME: {
70 if (BDP_ != Teuchos::null) {
71 RCP<Tpetra::CrsMatrix<ST, LO, GO, NT> > H = BDP_->createCrsMatrix();
72 if (H != Teuchos::null) H->describe(out_arg, verbLevel);
73 }
74 break;
75 }
76 default: TEUCHOS_TEST_FOR_EXCEPT(true);
77 }
78}
79
80} // end namespace Teko
void scale(const double alpha, MultiVector &x)
Scale a multivector by a constant.
MultiVector deepcopy(const MultiVector &v)
Perform a deep copy of the vector.