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 // ***********************************************************************
4 //
5 // Xpetra: A linear algebra interface package
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact
39 // Jonathan Hu (jhu@sandia.gov)
40 // Andrey Prokopenko (aprokop@sandia.gov)
41 // Ray Tuminaro (rstumin@sandia.gov)
42 //
43 // ***********************************************************************
44 //
45 // @HEADER
46 #ifndef XPETRA_EPETRAOPERATOR_HPP
47 #define XPETRA_EPETRAOPERATOR_HPP
48 
50 
51 #include <Epetra_Operator.h>
52 #include <Epetra_Map.h>
53 
54 #include "Xpetra_Map.hpp"
55 #include "Xpetra_EpetraMap.hpp"
56 #include "Xpetra_MultiVector.hpp"
58 #include "Xpetra_Operator.hpp"
59 
60 #include "Xpetra_Utils.hpp"
61 
62 #if defined(XPETRA_ENABLE_DEPRECATED_CODE)
63 #ifdef __GNUC__
64 #warning "The header file Trilinos/packages/xpetra/src/Operator/Xpetra_EpetraOperator.hpp is deprecated."
65 #endif
66 #else
67 #error "The header file Trilinos/packages/xpetra/src/Operator/Xpetra_EpetraOperator.hpp is deprecated."
68 #endif
69 
70 namespace Xpetra {
71 
72 template <class EpetraGlobalOrdinal, class Node>
73 class XPETRA_DEPRECATED EpetraOperator : public Operator<double, int, EpetraGlobalOrdinal, Node> {
74  typedef double Scalar;
75  typedef int LocalOrdinal;
76  typedef EpetraGlobalOrdinal GlobalOrdinal;
77 
78  public:
80 
82  virtual const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getDomainMap() const {
83  XPETRA_MONITOR("EpetraOperator::getDomainMap()");
84  return toXpetra<GlobalOrdinal, Node>(op_->OperatorDomainMap());
85  }
86 
88  virtual const Teuchos::RCP<const Xpetra::Map<LocalOrdinal, GlobalOrdinal, Node> > getRangeMap() const {
89  XPETRA_MONITOR("EpetraOperator::getRangeMap()");
90  return toXpetra<GlobalOrdinal, Node>(op_->OperatorRangeMap());
91  }
92 
94 
99  virtual void
102  Teuchos::ETransp mode = Teuchos::NO_TRANS,
103  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
104  Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const {
105  XPETRA_MONITOR("EpetraOperator::apply");
106 
107  XPETRA_DYNAMIC_CAST(const EpetraMultiVectorT<GlobalOrdinal XPETRA_COMMA Node>, X, eX, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
108  XPETRA_DYNAMIC_CAST(EpetraMultiVectorT<GlobalOrdinal XPETRA_COMMA Node>, Y, eY, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
109 
110  TEUCHOS_TEST_FOR_EXCEPTION((mode != Teuchos::NO_TRANS) && (mode != Teuchos::TRANS), Exceptions::NotImplemented,
111  "Xpetra::EpetraOperator->apply(): can only accept mode == NO_TRANS or mode == TRANS");
112  TEUCHOS_TEST_FOR_EXCEPTION(mode == Teuchos::TRANS && !hasTransposeApply(), Exceptions::RuntimeError,
113  "Xpetra::EpetraOperator->apply(): cannot apply transpose as underlying Epetra operator does not support it");
114 
115  // Helper vector for string A*X
116  RCP<Epetra_MultiVector> epY = eY.getEpetra_MultiVector();
117  RCP<Epetra_MultiVector> tmp = Teuchos::rcp(new Epetra_MultiVector(*epY));
118  tmp->PutScalar(0.0);
119 
120  op_->SetUseTranspose(mode == Teuchos::TRANS);
121  XPETRA_ERR_CHECK(op_->Apply(*eX.getEpetra_MultiVector(), *tmp));
122 
123  // calculate alpha * A * x + beta * y
124  XPETRA_ERR_CHECK(eY.getEpetra_MultiVector()->Update(alpha, *tmp, beta));
125  }
126 
128  virtual bool hasTransposeApply() const {
129  // We do not currently use transpose, try setting it
130  int err = op_->SetUseTranspose(true);
131  op_->SetUseTranspose(false);
132  return (err == 0);
133  }
134 
136 
138 
139 
141  std::string description() const {
142  XPETRA_MONITOR("EpetraOperator::description");
143  return "Epetra_Operator";
144  }
145 
147  void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default) const {
148  XPETRA_MONITOR("EpetraOperator::describe");
149  out << "Epetra_Operator" << std::endl;
150  }
151 
153 
155 
156 
158  EpetraOperator(const Teuchos::RCP<Epetra_Operator> &op)
159  : op_(op) {} // TODO removed const
160 
165  using STS = Teuchos::ScalarTraits<Scalar>;
166  R.update(STS::one(), B, STS::zero());
167  this->apply(X, R, Teuchos::NO_TRANS, -STS::one(), STS::one());
168  }
169 
171 
172  private:
174  RCP<Epetra_Operator> op_;
175 
176 }; // EpetraOperator class
177 
178 template <class EpetraGlobalOrdinal, class Node>
179 class EpetraInverseOperator : public Operator<double, int, EpetraGlobalOrdinal, Node> {
180  typedef double Scalar;
181  typedef int LocalOrdinal;
182  typedef EpetraGlobalOrdinal GlobalOrdinal;
183 
184  public:
186 
188  virtual Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> > getDomainMap() const {
189  XPETRA_MONITOR("EpetraOperator::getDomainMap()");
190  return toXpetra<GlobalOrdinal, Node>(op_->OperatorDomainMap());
191  }
192 
194  virtual Teuchos::RCP<const Map<LocalOrdinal, GlobalOrdinal, Node> > getRangeMap() const {
195  XPETRA_MONITOR("EpetraOperator::getRangeMap()");
196  return toXpetra<GlobalOrdinal, Node>(op_->OperatorRangeMap());
197  }
198 
200 
205  virtual void
208  Teuchos::ETransp mode = Teuchos::NO_TRANS,
209  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
210  Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const {
211  XPETRA_MONITOR("EpetraOperator::apply");
212 
213  XPETRA_DYNAMIC_CAST(const EpetraMultiVectorT<GlobalOrdinal XPETRA_COMMA Node>, X, eX, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
214  XPETRA_DYNAMIC_CAST(EpetraMultiVectorT<GlobalOrdinal XPETRA_COMMA Node>, Y, eY, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
215 
216  TEUCHOS_TEST_FOR_EXCEPTION((mode != Teuchos::NO_TRANS) && (mode != Teuchos::TRANS), Exceptions::NotImplemented,
217  "Xpetra::EpetraOperator->apply(): can only accept mode == NO_TRANS or mode == TRANS");
218  TEUCHOS_TEST_FOR_EXCEPTION(mode == Teuchos::TRANS && !hasTransposeApply(), Exceptions::RuntimeError,
219  "Xpetra::EpetraOperator->apply(): cannot apply transpose as underlying Epetra operator does not support it");
220 
221  // Helper vector for string A*X
222  RCP<Epetra_MultiVector> epY = eY.getEpetra_MultiVector();
223  RCP<Epetra_MultiVector> tmp = Teuchos::rcp(new Epetra_MultiVector(*epY));
224  tmp->PutScalar(0.0);
225 
226  op_->SetUseTranspose(mode == Teuchos::TRANS);
227  XPETRA_ERR_CHECK(op_->ApplyInverse(*eX.getEpetra_MultiVector(), *tmp));
228 
229  // calculate alpha * A * x + beta * y
230  XPETRA_ERR_CHECK(eY.getEpetra_MultiVector()->Update(alpha, *tmp, beta));
231  }
232 
234  virtual bool hasTransposeApply() const {
235  // We do not currently use transpose, try setting it
236  int err = op_->SetUseTranspose(true);
237  op_->SetUseTranspose(false);
238  return (err == 0);
239  }
240 
242 
244 
245 
247  std::string description() const {
248  XPETRA_MONITOR("EpetraOperator::description");
249  return "Epetra_Operator";
250  }
251 
253  void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel = Teuchos::Describable::verbLevel_default) const {
254  XPETRA_MONITOR("EpetraOperator::describe");
255  out << "Epetra_Operator" << std::endl;
256  }
257 
259 
261 
262 
264  EpetraInverseOperator(const Teuchos::RCP<Epetra_Operator> &op)
265  : op_(op) {} // TODO removed const
266 
271  using STS = Teuchos::ScalarTraits<Scalar>;
272  R.update(STS::one(), B, STS::zero());
273  this->apply(X, R, Teuchos::NO_TRANS, -STS::one(), STS::one());
274  }
275 
277 
278  private:
280  RCP<Epetra_Operator> op_;
281 };
282 
283 } // namespace Xpetra
284 
285 #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.