Debian Codegraph @

packages/belos/tpetra/src/BelosTpetraOperator.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Belos: Block Linear Solvers Package
4 //
5 // Copyright 2004-2016 NTESS and the Belos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /*! \file BelosTpetraOperator.h
11 \brief This file provides a Tpetra::Operator interface so Belos can be integrated into
12 other codes as an abstract operator.
13 */
14
15 #ifndef BELOS_TPETRA_OPERATOR_H
16 #define BELOS_TPETRA_OPERATOR_H
17
18 #include "Tpetra_MultiVector.hpp"
19 #include "Tpetra_Operator.hpp"
20 #include "Tpetra_Map.hpp"
21
22 #include "BelosLinearProblem.hpp"
23 #include "BelosStatusTest.hpp"
24 #include "BelosOutputManager.hpp"
25 #include "BelosTpetraAdapter.hpp"
26 #include "BelosSolverFactory.hpp"
27
28 #include "Teuchos_ParameterList.hpp"
29
30 /*! \class Belos::TpetraOperator
31 \brief This class provides an interface to the Tpetra::Operator class, so Belos can be
32 integrated into other codes as an abstract operator. It allows a Belos solver to be
33 called a Tpetra::Operator.
34 Thus, it can use itself as a preconditioner if need be. It can
35 also be used as the inner iteration of Anasazi :)
36 */
37
38 namespace Belos {
39
40 template<class Scalar = Tpetra::Operator<>::scalar_type,
41 class LocalOrdinal = Tpetra::Operator<>::local_ordinal_type,
42 class GlobalOrdinal = Tpetra::Operator<>::global_ordinal_type,
43 class Node = Tpetra::Operator<>::node_type>
44 class TpetraOperator : public Tpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node> {
45 private:
46
47 // Typedefs:
48 using Tpetra_MultiVector = Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
49 using Tpetra_Operator = Tpetra::Operator<Scalar, LocalOrdinal, GlobalOrdinal, Node>;
50 using Tpetra_Map = Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node>;
51
52 Teuchos::RCP<SolverManager<Scalar,Tpetra_MultiVector,Tpetra_Operator> > solver_;
53 Teuchos::RCP<LinearProblem<Scalar,Tpetra_MultiVector,Tpetra_Operator> > lp_;
54 Teuchos::RCP<Teuchos::ParameterList> plist_;
55
56 //! Name of solver to be passed into solver factory.
57 std::string solver_name_;
58
59 /// \brief True if TpetraOperator should initialize the solution vector to zero before
60 /// calling 'solve' in the 'apply' function.
61 bool initSolnVec_;
62
63 public:
64
65 //! @name Constructor / Destructor
66 //@{
67
68 /// \brief Create a Tpetra::Operator from the Belos solver \c solver_name generated by Belos' solver factory.
69 ///
70 /// The solver is initialized with a LinearProblem (\c lp) and ParameterList (\c plist).
71 ///
72 /// One should create a new linear problem to pass to the inner solver which is separate
73 /// from the linear problem passed to any outer solver. (Both problems may have the same
74 /// operator, but solution and right-hand side vectors need to be maintained separately.)
75 ///
76 /// When true, the bool \c initSolnVec causes the solution vector \c Y to be itialized to zero
77 /// when apply(X,Y) is called.
78
79 TpetraOperator( const Teuchos::RCP<LinearProblem<Scalar,Tpetra_MultiVector,Tpetra_Operator> >& lp,
80 const Teuchos::RCP<Teuchos::ParameterList>& plist,
81 const std::string solver_name,
82 bool initSolnVec = false )
83 : lp_(lp),
84 plist_(plist),
85 solver_name_(solver_name),
86 initSolnVec_(initSolnVec)
87 {
88 Belos::SolverFactory<Scalar,Tpetra_MultiVector,Tpetra_Operator> factory;
89 solver_ = factory.create( solver_name_, plist );
90 solver_->setProblem( lp_ );
91 }
92
93
94 //! Destructor
95 virtual ~TpetraOperator() = default;
96 //@}
97
98 //! @name Operator application methods
99 //@{
100
101 /// \brief Apply the operator. Performs a linear solve with right-hand side \c X.
102 /// Solution is stored in \c Y. If \c initSolnVec was set to true, the vector \c Y
103 /// will be initialized with zeros before calling solve. If \c initSolnVec is false,
104 /// the vector \c Y will be used as an initial guess for the linear solve.
105 ///
106 /// When using the GmresPoly solver, the polynomial will be applied to \c X at each call
107 /// instead of performing a linear solve.
108
109 void apply( const Tpetra_MultiVector &X, Tpetra_MultiVector &Y,
110 Teuchos::ETransp mode = Teuchos::NO_TRANS,
111 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
112 Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const override
113 {
114 Teuchos::RCP<const Tpetra_MultiVector> vec_X = Teuchos::rcpFromRef( X );
115 Teuchos::RCP<Tpetra_MultiVector> vec_Y = Teuchos::rcpFromRef( Y );
116 if (initSolnVec_)
117 {
118 vec_Y->putScalar( 0.0 );
119 lp_->setInitResVec( vec_X );
120 }
121 lp_->setProblem( vec_Y, vec_X );
122 solver_->solve();
123 };
124 //@}
125
126 //! @name Attribute access functions
127 //@{
128
129 //! Return the name of the requested solver.
130 const std::string getSolverName() const { return solver_name_; };
131
132 //! Return whether the operator supports applying its transpose or conjugate transpose.
133 bool hasTransposeApply() const override { return false; };
134
135 //! Return the domain map for this operator.
136 Teuchos::RCP<const Tpetra_Map> getDomainMap() const override
137 { return lp_->getOperator()->getDomainMap(); };
138
139 //! Return the range map for this operator.
140 Teuchos::RCP<const Tpetra_Map> getRangeMap() const override
141 { return lp_->getOperator()->getRangeMap(); };
142 //@}
143
144 };
145
146 } //end namespace Belos
147
148 // end of file BELOS_TPETRA_OPERATOR_H
149 #endif
150