MueLu Version of the Day
Loading...
Searching...
No Matches
Xpetra_ThyraLinearOp.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//
43// ***********************************************************************
44//
45// @HEADER
46#ifndef MUELU_XPETRA_THYRALINEAROP_HPP
47#define MUELU_XPETRA_THYRALINEAROP_HPP
48
49#if defined(HAVE_MUELU_STRATIMIKOS) && defined(HAVE_MUELU_THYRA)
50
51#include "MueLu_ConfigDefs.hpp"
52
53#include <Xpetra_Operator.hpp>
54#include <Xpetra_MultiVector.hpp>
55
56// Stratimikos
57#include <Thyra_VectorBase.hpp>
58#include <Thyra_SolveSupportTypes.hpp>
59#include <Thyra_LinearOpWithSolveBase.hpp>
60#include <Teuchos_AbstractFactoryStd.hpp>
61#include <Stratimikos_LinearSolverBuilder.hpp>
63
64
65namespace MueLu {
66
69 template <class Scalar = DefaultScalar,
72 class Node = DefaultNode>
73 class XpetraThyraLinearOp : public Xpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> {
74 protected:
75 XpetraThyraLinearOp() = default;
76 public:
77
79
80
82 XpetraThyraLinearOp(RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > A,
83 RCP<ParameterList> params) : A_(A) {
84 throw Exceptions::RuntimeError("Interface not supported");
85 };
86
88 ~XpetraThyraLinearOp() = default;
89
91
93 Teuchos::RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > getDomainMap() const {
94 throw Exceptions::RuntimeError("Interface not supported");
95 }
96
97 // //! Returns the Tpetra::Map object associated with the range of this operator.
98 Teuchos::RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > getRangeMap() const {
99 throw Exceptions::RuntimeError("Interface not supported");
100 }
101
103
107 void apply(const Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X,
108 Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y,
109 Teuchos::ETransp mode = Teuchos::NO_TRANS,
110 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
111 Scalar beta = Teuchos::ScalarTraits<Scalar>::one()) const {
112 throw Exceptions::RuntimeError("Interface not supported");
113 }
114
116 bool hasTransposeApply() const { return false; }
117
119 void residual(const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > & X,
120 const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > & B,
121 Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > & R) const {
122 throw Exceptions::RuntimeError("Interface not supported");
123 }
124
125
126 private:
127 RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > A_;
128 };
129
130
131 // Partial specialization for Scalar == double.
132 // Allows to avoid issues with Stokhos instantiating Thyra objects.
133 template <class LocalOrdinal,
134 class GlobalOrdinal,
135 class Node>
136 class XpetraThyraLinearOp<double,LocalOrdinal,GlobalOrdinal,Node> : public Xpetra::Operator<double,LocalOrdinal,GlobalOrdinal,Node> {
137
138 using Scalar = double;
139
140 protected:
141 XpetraThyraLinearOp() = default;
142 public:
143
145
146
148 XpetraThyraLinearOp(RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > A,
149 RCP<ParameterList> params) : A_(A) {
150 // Build Thyra linear algebra objects
151 RCP<const Thyra::LinearOpBase<Scalar> > thyraA = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyra(Teuchos::rcp_dynamic_cast<Xpetra::CrsMatrixWrap<Scalar,LocalOrdinal,GlobalOrdinal,Node>>(A)->getCrsMatrix());
152
153 Stratimikos::LinearSolverBuilder<Scalar> linearSolverBuilder;
154 typedef Thyra::PreconditionerFactoryBase<Scalar> Base;
155 typedef Thyra::MueLuPreconditionerFactory<Scalar, LocalOrdinal, GlobalOrdinal, Node> ImplMueLu;
156 linearSolverBuilder.setPreconditioningStrategyFactory(Teuchos::abstractFactoryStd<Base, ImplMueLu>(), "MueLu");
157
158 linearSolverBuilder.setParameterList(params);
159
160 // Build a new "solver factory" according to the previously specified parameter list.
161 // RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> > solverFactory = Thyra::createLinearSolveStrategy(linearSolverBuilder);
162
163 auto precFactory = Thyra::createPreconditioningStrategy(linearSolverBuilder);
164 auto prec = precFactory->createPrec();
165
166 precFactory->initializePrec(Thyra::defaultLinearOpSource(thyraA), prec.get(), Thyra::SUPPORT_SOLVE_UNSPECIFIED);
167 prec_ = prec;
168 };
169
171 ~XpetraThyraLinearOp() = default;
172
174
176 Teuchos::RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > getDomainMap() const {
177 return A_->getDomainMap();
178 }
179
180 // //! Returns the Tpetra::Map object associated with the range of this operator.
181 Teuchos::RCP<const Xpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > getRangeMap() const {
182 return A_->getRangeMap();
183 }
184
186
190 void apply(const Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& X,
191 Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>& Y,
192 Teuchos::ETransp mode = Teuchos::NO_TRANS,
193 Scalar alpha = Teuchos::ScalarTraits<Scalar>::one(),
194 Scalar beta = Teuchos::ScalarTraits<Scalar>::one()) const {
195
196 RCP<const Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > rcpX = Teuchos::rcpFromRef(X);
197 RCP<const Thyra::MultiVectorBase<Scalar> > thyraX = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyraMultiVector(rcpX);
198
199 RCP<Xpetra::MultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node> > rcpY = Teuchos::rcpFromRef(Y);
200 RCP<Thyra::MultiVectorBase<Scalar> > thyraY = Teuchos::rcp_const_cast<Thyra::MultiVectorBase<Scalar> >(Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyraMultiVector(rcpY));
201
202 prec_->getUnspecifiedPrecOp()->apply(Thyra::NOTRANS, *thyraX, thyraY.ptr(), alpha, beta);
203 Y = *Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toXpetra(thyraY, Y.getMap()->getComm());
204 }
205
207 bool hasTransposeApply() const { return false; }
208
210 void residual(const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > & X,
211 const Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > & B,
212 Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > & R) const {
213 using STS = Teuchos::ScalarTraits<Scalar>;
214 R.update(STS::one(),B,STS::zero());
215 this->apply (X, R, Teuchos::NO_TRANS, -STS::one(), STS::one());
216 }
217
218
219 private:
220 RCP<Xpetra::Matrix<Scalar,LocalOrdinal,GlobalOrdinal,Node> > A_;
221 RCP<Thyra::PreconditionerBase<Scalar> > prec_;
222 };
223
224} // namespace
225
226#endif // defined(HAVE_MUELU_STRATIMIKOS) && defined(HAVE_MUELU_THYRA)
227
228#endif // MUELU_XPETRA_THYRALINEAROP_HPP
MueLu::DefaultLocalOrdinal LocalOrdinal
MueLu::DefaultScalar Scalar
MueLu::DefaultGlobalOrdinal GlobalOrdinal
MueLu::DefaultNode Node
Namespace for MueLu classes and methods.
Tpetra::KokkosClassic::DefaultNode::DefaultNodeType DefaultNode
Tpetra::Details::DefaultTypes::scalar_type DefaultScalar