MueLu  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Thyra_XpetraLinearOp_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // MueLu: A package for multigrid based preconditioning
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 // Tobias Wiesner (tawiesn@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef THYRA_XPETRA_LINEAR_OP_HPP
48 #define THYRA_XPETRA_LINEAR_OP_HPP
49 
51 #include "Teuchos_ScalarTraits.hpp"
53 
55 #include "Xpetra_MapExtractor.hpp"
56 
57 namespace Thyra {
58 
59 // Constructors/initializers
60 
61 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
63 
64 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
66  const RCP<const VectorSpaceBase<Scalar> > &rangeSpace,
67  const RCP<const VectorSpaceBase<Scalar> > &domainSpace,
69  initializeImpl(rangeSpace, domainSpace, xpetraOperator);
70 }
71 
72 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
74  const RCP<const VectorSpaceBase<Scalar> > &rangeSpace,
75  const RCP<const VectorSpaceBase<Scalar> > &domainSpace,
77  initializeImpl(rangeSpace, domainSpace, xpetraOperator);
78 }
79 
80 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
83  return xpetraOperator_.getNonconstObj();
84 }
85 
86 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
89  return xpetraOperator_;
90 }
91 
92 // Public Overridden functions from LinearOpBase
93 
94 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
97  return rangeSpace_;
98 }
99 
100 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
103  return domainSpace_;
104 }
105 
106 // Protected Overridden functions from LinearOpBase
107 
108 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
110  Thyra::EOpTransp M_trans) const {
111  if (is_null(xpetraOperator_))
112  return false;
113 
114  if (M_trans == NOTRANS)
115  return true;
116 
117  if (M_trans == CONJ) {
118  // For non-complex scalars, CONJ is always supported since it is equivalent to NO_TRANS.
119  // For complex scalars, Xpetra does not support conjugation without transposition.
121  }
122 
123  return xpetraOperator_->hasTransposeApply();
124 }
125 
126 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
128  const Thyra::EOpTransp M_trans,
129  const Thyra::MultiVectorBase<Scalar> &X_in,
130  const Teuchos::Ptr<Thyra::MultiVectorBase<Scalar> > &Y_inout,
131  const Scalar alpha,
132  const Scalar beta) const {
133  using Teuchos::rcpFromPtr;
134  using Teuchos::rcpFromRef;
135 
136  TEUCHOS_TEST_FOR_EXCEPTION(getConstXpetraOperator() == Teuchos::null, MueLu::Exceptions::RuntimeError, "XpetraLinearOp::applyImpl: internal Xpetra::Operator is null.");
137  RCP<const Teuchos::Comm<int> > comm = getConstXpetraOperator()->getRangeMap()->getComm();
138 
140  Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(rcpFromRef(X_in), comm);
142  Xpetra::ThyraUtils<Scalar, LocalOrdinal, GlobalOrdinal, Node>::toXpetra(rcpFromPtr(Y_inout), comm);
143  Teuchos::ETransp transp;
144  switch (M_trans) {
145  case NOTRANS: transp = Teuchos::NO_TRANS; break;
146  case TRANS: transp = Teuchos::TRANS; break;
147  case CONJTRANS: transp = Teuchos::CONJ_TRANS; break;
148  default: TEUCHOS_TEST_FOR_EXCEPTION(true, MueLu::Exceptions::NotImplemented, "Thyra::XpetraLinearOp::apply. Unknown value for M_trans. Only NOTRANS, TRANS and CONJTRANS are supported.");
149  }
150 
151  xpetraOperator_->apply(*tX_in, *tY_inout, transp, alpha, beta);
152 
153  // check whether Y is a product vector
156  Teuchos::ptr_dynamic_cast<Thyra::ProductMultiVectorBase<Scalar> >(Y_inout);
157  if (prodY_inout != Teuchos::null) {
158  // If Y is a product vector we split up the data from tY and merge them
159  // into the product vector. The necessary Xpetra::MapExtractor is extracted
160  // from the fine level operator (not this!)
161 
162  // get underlying fine level operator (BlockedCrsMatrix)
163  // to extract the range MapExtractor
165  Teuchos::rcp_dynamic_cast<MueLu::XpetraOperator<Scalar, LocalOrdinal, GlobalOrdinal, Node> >(xpetraOperator_.getNonconstObj());
166 
168  mueXop->GetHierarchy()->GetLevel(0)->template Get<RCP<Xpetra::Matrix<Scalar, LocalOrdinal, GlobalOrdinal, Node> > >("A");
170 
173  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(bA));
174 
175  rgMapExtractor = bA->getRangeMapExtractor();
176  TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(rgMapExtractor));
177  }
178 }
179 
180 // private
181 
182 template <class Scalar, class LocalOrdinal, class GlobalOrdinal, class Node>
183 template <class XpetraOperator_t>
185  const RCP<const VectorSpaceBase<Scalar> > &rangeSpace,
186  const RCP<const VectorSpaceBase<Scalar> > &domainSpace,
187  const RCP<XpetraOperator_t> &xpetraOperator) {
188 #ifdef THYRA_DEBUG
189  TEUCHOS_ASSERT(nonnull(rangeSpace));
190  TEUCHOS_ASSERT(nonnull(domainSpace));
191  TEUCHOS_ASSERT(nonnull(xpetraOperator));
192 #endif
193  rangeSpace_ = rangeSpace;
194  domainSpace_ = domainSpace;
195  xpetraOperator_ = xpetraOperator;
196 }
197 
198 } // namespace Thyra
199 
200 #endif // THYRA_XPETRA_LINEAR_OP_HPP
bool is_null(const boost::shared_ptr< T > &p)
RCP< const Thyra::VectorSpaceBase< Scalar > > range() const
RCP< const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getConstXpetraOperator() const
Get embedded const Xpetra::Operator.
bool is_null(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
bool opSupportedImpl(Thyra::EOpTransp M_trans) const
RCP< const Thyra::VectorSpaceBase< Scalar > > domain() const
MueLu::DefaultScalar Scalar
void initializeImpl(const RCP< const VectorSpaceBase< Scalar > > &rangeSpace, const RCP< const VectorSpaceBase< Scalar > > &domainSpace, const RCP< XpetraOperator_t > &xpetraOperator)
XpetraLinearOp()
Construct to uninitialized.
Exception throws when you call an unimplemented method of MueLu.
void applyImpl(const Thyra::EOpTransp M_trans, const Thyra::MultiVectorBase< Scalar > &X_in, const Teuchos::Ptr< Thyra::MultiVectorBase< Scalar > > &Y_inout, const Scalar alpha, const Scalar beta) const
RCP< Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > getXpetraOperator()
Get embedded non-const Xpetra::Operator.
bool nonnull(const boost::shared_ptr< T > &p)
void initialize(const RCP< const VectorSpaceBase< Scalar > > &rangeSpace, const RCP< const VectorSpaceBase< Scalar > > &domainSpace, const RCP< Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &xpetraOperator)
Initialize.
Exception throws to report errors in the internal logical of the program.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
void constInitialize(const RCP< const VectorSpaceBase< Scalar > > &rangeSpace, const RCP< const VectorSpaceBase< Scalar > > &domainSpace, const RCP< const Xpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &xpetraOperator)
Initialize.
Wraps an existing MueLu::Hierarchy as a Xpetra::Operator.