Xpetra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Xpetra_EpetraOperator.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Xpetra: A linear algebra interface package
4 //
5 // Copyright 2012 NTESS and the Xpetra contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef XPETRA_EPETRAOPERATOR_HPP
11 #define XPETRA_EPETRAOPERATOR_HPP
12 
14 
15 #include <Epetra_Operator.h>
16 #include <Epetra_Map.h>
17 
18 #include "Xpetra_Map.hpp"
19 #include "Xpetra_EpetraMap.hpp"
20 #include "Xpetra_MultiVector.hpp"
22 #include "Xpetra_Operator.hpp"
23 
24 #include "Xpetra_Utils.hpp"
25 
26 #if defined(XPETRA_ENABLE_DEPRECATED_CODE)
27 #ifdef __GNUC__
28 #if defined(Xpetra_SHOW_DEPRECATED_WARNINGS)
29 #warning "The header file Trilinos/packages/xpetra/src/Operator/Xpetra_EpetraOperator.hpp is deprecated."
30 #endif
31 #endif
32 #else
33 #error "The header file Trilinos/packages/xpetra/src/Operator/Xpetra_EpetraOperator.hpp is deprecated."
34 #endif
35 
36 namespace Xpetra {
37 
38 template <class EpetraGlobalOrdinal, class Node>
39 class XPETRA_DEPRECATED EpetraOperator : public Operator<double, int, EpetraGlobalOrdinal, Node> {
40  typedef double Scalar;
41  typedef int LocalOrdinal;
42  typedef EpetraGlobalOrdinal GlobalOrdinal;
43 
44  public:
46 
48  virtual const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getDomainMap() const {
49  XPETRA_MONITOR("EpetraOperator::getDomainMap()");
50  return toXpetra<GlobalOrdinal, Node>(op_->OperatorDomainMap());
51  }
52 
54  virtual const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getRangeMap() const {
55  XPETRA_MONITOR("EpetraOperator::getRangeMap()");
56  return toXpetra<GlobalOrdinal, Node>(op_->OperatorRangeMap());
57  }
58 
60 
65  virtual void
68  Teuchos::ETransp mode = Teuchos::NO_TRANS,
69  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
70  Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const {
71  XPETRA_MONITOR("EpetraOperator::apply");
72 
73  XPETRA_DYNAMIC_CAST(const EpetraMultiVectorT<GlobalOrdinal XPETRA_COMMA Node>, X, eX, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
74  XPETRA_DYNAMIC_CAST(EpetraMultiVectorT<GlobalOrdinal XPETRA_COMMA Node>, Y, eY, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
75 
76  TEUCHOS_TEST_FOR_EXCEPTION((mode != Teuchos::NO_TRANS) && (mode != Teuchos::TRANS), Exceptions::NotImplemented,
77  "Xpetra::EpetraOperator->apply(): can only accept mode == NO_TRANS or mode == TRANS");
78  TEUCHOS_TEST_FOR_EXCEPTION(mode == Teuchos::TRANS && !hasTransposeApply(), Exceptions::RuntimeError,
79  "Xpetra::EpetraOperator->apply(): cannot apply transpose as underlying Epetra operator does not support it");
80 
81  // Helper vector for string A*X
82  RCP<Epetra_MultiVector> epY = eY.getEpetra_MultiVector();
83  RCP<Epetra_MultiVector> tmp = Teuchos::rcp(new Epetra_MultiVector(*epY));
84  tmp->PutScalar(0.0);
85 
86  op_->SetUseTranspose(mode == Teuchos::TRANS);
87  XPETRA_ERR_CHECK(op_->Apply(*eX.getEpetra_MultiVector(), *tmp));
88 
89  // calculate alpha * A * x + beta * y
90  XPETRA_ERR_CHECK(eY.getEpetra_MultiVector()->Update(alpha, *tmp, beta));
91  }
92 
94  virtual bool hasTransposeApply() const {
95  // We do not currently use transpose, try setting it
96  int err = op_->SetUseTranspose(true);
97  op_->SetUseTranspose(false);
98  return (err == 0);
99  }
100 
102 
104 
105 
107  std::string description() const {
108  XPETRA_MONITOR("EpetraOperator::description");
109  return "Epetra_Operator";
110  }
111 
113  void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default) const {
114  XPETRA_MONITOR("EpetraOperator::describe");
115  out << "Epetra_Operator" << std::endl;
116  }
117 
119 
121 
122 
124  EpetraOperator(const Teuchos::RCP<Epetra_Operator> &op)
125  : op_(op) {} // TODO removed const
126 
131  using STS = Teuchos::ScalarTraits<Scalar>;
132  R.update(STS::one(), B, STS::zero());
133  this->apply(X, R, Teuchos::NO_TRANS, -STS::one(), STS::one());
134  }
135 
137 
138  private:
140  RCP<Epetra_Operator> op_;
141 
142 }; // EpetraOperator class
143 
144 template <class EpetraGlobalOrdinal, class Node>
145 class EpetraInverseOperator : public Operator<double, int, EpetraGlobalOrdinal, Node> {
146  typedef double Scalar;
147  typedef int LocalOrdinal;
148  typedef EpetraGlobalOrdinal GlobalOrdinal;
149 
150  public:
152 
154  virtual Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> > getDomainMap() const {
155  XPETRA_MONITOR("EpetraOperator::getDomainMap()");
156  return toXpetra<GlobalOrdinal, Node>(op_->OperatorDomainMap());
157  }
158 
160  virtual Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> > getRangeMap() const {
161  XPETRA_MONITOR("EpetraOperator::getRangeMap()");
162  return toXpetra<GlobalOrdinal, Node>(op_->OperatorRangeMap());
163  }
164 
166 
171  virtual void
174  Teuchos::ETransp mode = Teuchos::NO_TRANS,
175  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
176  Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const {
177  XPETRA_MONITOR("EpetraOperator::apply");
178 
179  XPETRA_DYNAMIC_CAST(const EpetraMultiVectorT<GlobalOrdinal XPETRA_COMMA Node>, X, eX, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
180  XPETRA_DYNAMIC_CAST(EpetraMultiVectorT<GlobalOrdinal XPETRA_COMMA Node>, Y, eY, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
181 
182  TEUCHOS_TEST_FOR_EXCEPTION((mode != Teuchos::NO_TRANS) && (mode != Teuchos::TRANS), Exceptions::NotImplemented,
183  "Xpetra::EpetraOperator->apply(): can only accept mode == NO_TRANS or mode == TRANS");
184  TEUCHOS_TEST_FOR_EXCEPTION(mode == Teuchos::TRANS && !hasTransposeApply(), Exceptions::RuntimeError,
185  "Xpetra::EpetraOperator->apply(): cannot apply transpose as underlying Epetra operator does not support it");
186 
187  // Helper vector for string A*X
188  RCP<Epetra_MultiVector> epY = eY.getEpetra_MultiVector();
189  RCP<Epetra_MultiVector> tmp = Teuchos::rcp(new Epetra_MultiVector(*epY));
190  tmp->PutScalar(0.0);
191 
192  op_->SetUseTranspose(mode == Teuchos::TRANS);
193  XPETRA_ERR_CHECK(op_->ApplyInverse(*eX.getEpetra_MultiVector(), *tmp));
194 
195  // calculate alpha * A * x + beta * y
196  XPETRA_ERR_CHECK(eY.getEpetra_MultiVector()->Update(alpha, *tmp, beta));
197  }
198 
200  virtual bool hasTransposeApply() const {
201  // We do not currently use transpose, try setting it
202  int err = op_->SetUseTranspose(true);
203  op_->SetUseTranspose(false);
204  return (err == 0);
205  }
206 
208 
210 
211 
213  std::string description() const {
214  XPETRA_MONITOR("EpetraOperator::description");
215  return "Epetra_Operator";
216  }
217 
219  void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default) const {
220  XPETRA_MONITOR("EpetraOperator::describe");
221  out << "Epetra_Operator" << std::endl;
222  }
223 
225 
227 
228 
230  EpetraInverseOperator(const Teuchos::RCP<Epetra_Operator> &op)
231  : op_(op) {} // TODO removed const
232 
237  using STS = Teuchos::ScalarTraits<Scalar>;
238  R.update(STS::one(), B, STS::zero());
239  this->apply(X, R, Teuchos::NO_TRANS, -STS::one(), STS::one());
240  }
241 
243 
244  private:
246  RCP<Epetra_Operator> op_;
247 };
248 
249 } // namespace Xpetra
250 
251 #endif // XPETRA_EPETRAOPERATOR_HPP
std::string description() const
A simple one-line description of this object.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with the given verbosity level to a FancyOStream.
RCP< Epetra_Operator > op_
The Tpetra::Operator which this class wraps.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
The Map associated with the range of this operator, which must be compatible with Y...
Exception throws to report errors in the internal logical of the program.
EpetraInverseOperator(const Teuchos::RCP< Epetra_Operator > &op)
EpetraOperator constructor to wrap a Epetra_Operator object.
virtual void update(const Scalar &alpha, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &A, const Scalar &beta)=0
Update multi-vector values with scaled values of A, this = beta*this + alpha*A.
RCP< Epetra_Operator > op_
The Tpetra::Operator which this class wraps.
#define XPETRA_ERR_CHECK(arg)
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with the given verbosity level to a FancyOStream.
void residual(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &R) const
Compute a residual R = B - (*this) * X.
EpetraOperator(const Teuchos::RCP< Epetra_Operator > &op)
EpetraOperator constructor to wrap a Epetra_Operator object.
virtual bool hasTransposeApply() const
Whether this operator supports applying the transpose or conjugate transpose.
Exception throws when you call an unimplemented method of Xpetra.
virtual Teuchos::RCP< const Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
The Map associated with the domain of this operator, which must be compatible with X...
#define XPETRA_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
virtual void apply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Computes the operator-multivector application.
virtual bool hasTransposeApply() const
Whether this operator supports applying the transpose or conjugate transpose.
EpetraGlobalOrdinal GlobalOrdinal
virtual const Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getDomainMap() const
The Map associated with the domain of this operator, which must be compatible with X...
virtual const Teuchos::RCP< const Xpetra::Map< LocalOrdinal, GlobalOrdinal, Node > > getRangeMap() const
The Map associated with the range of this operator, which must be compatible with Y...
#define XPETRA_MONITOR(funcName)
std::string description() const
A simple one-line description of this object.
virtual void apply(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Y, Teuchos::ETransp mode=Teuchos::NO_TRANS, Scalar alpha=Teuchos::ScalarTraits< Scalar >::one(), Scalar beta=Teuchos::ScalarTraits< Scalar >::zero()) const
Computes the operator-multivector application.
void residual(const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &X, const MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &B, MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > &R) const
Compute a residual R = B - (*this) * X.