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.hpp>
52 
53 #include "Xpetra_Map.hpp"
54 #include "Xpetra_EpetraMap.hpp"
55 #include "Xpetra_MultiVector.hpp"
57 #include "Xpetra_Operator.hpp"
58 
59 #include "Xpetra_Utils.hpp"
60 
61 namespace Xpetra {
62 
63  template<class EpetraGlobalOrdinal>
64  class EpetraOperator : public Operator<double, int, EpetraGlobalOrdinal>
65  {
66  typedef double Scalar;
67  typedef int LocalOrdinal;
68  typedef EpetraGlobalOrdinal GlobalOrdinal;
70 
71  public:
73 
75  virtual Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > getDomainMap() const {
76  XPETRA_MONITOR("EpetraOperator::getDomainMap()");
77  return toXpetra(op_->OperatorDomainMap());
78  }
79 
81  virtual Teuchos::RCP<const Map<LocalOrdinal,GlobalOrdinal,Node> > getRangeMap() const {
82  XPETRA_MONITOR("EpetraOperator::getRangeMap()");
83  return toXpetra(op_->OperatorRangeMap());
84  }
85 
87 
92  virtual void
95  Teuchos::ETransp mode = Teuchos::NO_TRANS,
96  Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
97  Scalar beta = Teuchos::ScalarTraits<Scalar>::zero()) const {
98  XPETRA_MONITOR("EpetraOperator::apply");
99 
100  XPETRA_DYNAMIC_CAST(const EpetraMultiVectorT<GlobalOrdinal>, X, eX, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
101  XPETRA_DYNAMIC_CAST( EpetraMultiVectorT<GlobalOrdinal>, Y, eY, "Xpetra::EpetraOperator->apply(): cannot cast input to Xpetra::EpetraMultiVectorT");
102 
103  TEUCHOS_TEST_FOR_EXCEPTION((mode != Teuchos::NO_TRANS) && (mode != Teuchos::TRANS), Exceptions::NotImplemented,
104  "Xpetra::EpetraOperator->apply(): can only accept mode == NO_TRANS or mode == TRANS");
105  TEUCHOS_TEST_FOR_EXCEPTION(mode == Teuchos::TRANS && !hasTransposeApply, Exceptions::RuntimeError,
106  "Xpetra::EpetraOperator->apply(): cannot apply transpose as underlying Epetra operator does not support it");
107 
108  // Helper vector for string A*X
109  RCP<Epetra_MultiVector> epY = eY.getEpetra_MultiVector();
110  RCP<Epetra_MultiVector> tmp = Teuchos::rcp(new Epetra_MultiVector(*epY));
111  tmp->PutScalar(0.0);
112 
113  bool curTranspose = op_->UseTranspose();
114  op_->SetUseTranspose(mode == Teuchos::TRANS);
115  XPETRA_ERR_CHECK(op_->Multiply(*eX.getEpetra_MultiVector(), *tmp));
116  op_->setUseTranspose(curTranspose);
117 
118  // calculate alpha * A * x + beta * y
119  XPETRA_ERR_CHECK(eY.getEpetra_MultiVector()->Update(alpha, *tmp, beta));
120  }
121 
123  virtual bool hasTransposeApply() const {
124  if (op_->UseTransepose()) {
125  // If current setting is to use transpose, then obviously we can use it
126  return true;
127  }
128  // We do not currently use transpose, try setting it
129  int err = op_->SetUseTranspose(true);
130  SetUseTranspose(false);
131  return (err == 0);
132  }
133 
135 
137 
138 
140  std::string description() const { XPETRA_MONITOR("EpetraOperator::description"); return "Epetra_Operator"; }
141 
143  void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const {
144  XPETRA_MONITOR("EpetraOperator::describe");
145  out << "Epetra_Operator" << std::endl;
146  }
147 
149 
151 
152 
154  EpetraOperator(const Teuchos::RCP<Tpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node> > &op) : op_(op) { } //TODO removed const
155 
157 
158  private:
160  RCP< Epetra_Operator> op_;
161 
162  }; // TpetraOperator class
163 
164 
165 } // Xpetra namespace
166 
167 #endif // XPETRA_EPETRAOPERATOR_HPP
virtual bool hasTransposeApply() const
Whether this operator supports applying the transpose or conjugate transpose.
Xpetra namespace
Exception throws to report errors in the internal logical of the program.
Node node_type
The Kokkos Node type.
RCP< Epetra_Operator > op_
The Tpetra::Operator which this class wraps.
#define XPETRA_ERR_CHECK(arg)
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...
std::string description() const
A simple one-line description of this object.
Operator< double, int, GlobalOrdinal >::node_type Node
Exception throws when you call an unimplemented method of Xpetra.
EpetraGlobalOrdinal GlobalOrdinal
#define XPETRA_DYNAMIC_CAST(type, obj, newObj, exceptionMsg)
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...
RCP< const CrsGraph< int, GlobalOrdinal, Node > > toXpetra(const Epetra_CrsGraph &g)
EpetraOperator(const Teuchos::RCP< Tpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &op)
TpetraOperator constructor to wrap a Tpetra::Operator object.
#define XPETRA_MONITOR(funcName)
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 describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Print the object with the given verbosity level to a FancyOStream.