14#ifndef MUELU_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
15#define MUELU_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
17#include "MueLu_config.hpp"
18#include "Trilinos_Details_LinearSolver.hpp"
19#include "Trilinos_Details_LinearSolverFactory.hpp"
22#ifdef HAVE_MUELU_EPETRA
23#include "Epetra_CrsMatrix.h"
27#include "Tpetra_Operator.hpp"
33template <
class MV,
class OP,
class NormType>
34class LinearSolver :
public Trilinos::Details::LinearSolver<MV, OP, NormType>,
35 virtual public Teuchos::Describable {
74 const Teuchos::EVerbosityLevel verbLevel =
75 Teuchos::Describable::verbLevel_default)
const;
78 Teuchos::RCP<const OP>
A_;
79 Teuchos::RCP<Teuchos::ParameterList>
params_;
83#if defined(HAVE_MUELU_SERIAL) and defined(HAVE_MUELU_EPETRA)
86 virtual public Teuchos::Describable {
91 , changedParams_(
false) {}
100 void setMatrix(
const Teuchos::RCP<const Epetra_Operator>& A) {
101 const char prefix[] =
"MueLu::Details::LinearSolver::setMatrix: ";
104 if (solver_ != Teuchos::null)
107 A_ = rcp_dynamic_cast<const Epetra_CrsMatrix>(A);
108 TEUCHOS_TEST_FOR_EXCEPTION(
A_.is_null(), std::runtime_error, prefix <<
"MueLu requires "
109 "an Epetra_CrsMatrix, but the matrix you provided is of a "
110 "different type. Please provide an Epetra_CrsMatrix instead.");
115 Teuchos::RCP<const Epetra_Operator>
getMatrix()
const {
122 const char prefix[] =
"MueLu::Details::LinearSolver::solve: ";
123 TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::runtime_error, prefix <<
"The solver does not "
124 "exist yet. You must call numeric() before you may call this method.");
125 TEUCHOS_TEST_FOR_EXCEPTION(changedA_, std::runtime_error, prefix <<
"The matrix A has been reset "
126 "since the last call to numeric(). Please call numeric() again.");
127 TEUCHOS_TEST_FOR_EXCEPTION(changedParams_, std::runtime_error, prefix <<
"The parameters have been reset "
128 "since the last call to numeric(). Please call numeric() again.");
130 int err = solver_->ApplyInverse(B, X);
132 TEUCHOS_TEST_FOR_EXCEPTION(err != 0, std::runtime_error, prefix <<
"EpetraOperator::ApplyInverse returned "
133 "nonzero error code "
138 void setParameters(
const Teuchos::RCP<Teuchos::ParameterList>& params) {
139 if (solver_ != Teuchos::null && params !=
params_)
140 changedParams_ =
true;
152 const char prefix[] =
"MueLu::Details::LinearSolver::numeric: ";
155 if (solver_ == Teuchos::null || changedA_ || changedParams_) {
157 changedParams_ =
false;
159 TEUCHOS_TEST_FOR_EXCEPTION(
A_ == Teuchos::null, std::runtime_error, prefix <<
"The matrix has not been "
160 "set yet. You must call setMatrix() with a nonnull matrix before you may "
161 "call this method.");
174 if (solver_.is_null()) {
175 return "\"MueLu::Details::LinearSolver\": {MV: Epetra_MultiVector, OP: Epetra_Operator, NormType: double}";
177 return solver_->GetHierarchy()->description();
183 describe(Teuchos::FancyOStream& out,
184 const Teuchos::EVerbosityLevel verbLevel =
185 Teuchos::Describable::verbLevel_default)
const {
187 if (solver_.is_null()) {
188 if (verbLevel > Teuchos::VERB_NONE) {
189 Teuchos::OSTab tab0(out);
190 out <<
"\"MueLu::Details::LinearSolver\":" << endl;
191 Teuchos::OSTab tab1(out);
192 out <<
"MV: Epetra_MultiVector" << endl
193 <<
"OP: Epetra_Operator" << endl
194 <<
"NormType: double" << endl;
197 solver_->GetHierarchy()->describe(out, verbLevel);
202 Teuchos::RCP<const Epetra_CrsMatrix>
A_;
203 Teuchos::RCP<Teuchos::ParameterList>
params_;
204 Teuchos::RCP<EpetraOperator> solver_;
210template <
class Scalar,
class LO,
class GO,
class Node>
212 Tpetra::Operator<Scalar, LO, GO, Node>,
213 typename Teuchos::ScalarTraits<Scalar>::magnitudeType> :
public Trilinos::Details::LinearSolver<Tpetra::MultiVector<Scalar, LO, GO, Node>,
214 Tpetra::Operator<Scalar, LO, GO, Node>,
215 typename Teuchos::ScalarTraits<Scalar>::magnitudeType>,
216 virtual public Teuchos::Describable {
221 , changedParams_(false) {}
230 void setMatrix(
const Teuchos::RCP<
const Tpetra::Operator<Scalar, LO, GO, Node> >& A) {
232 if (solver_ != Teuchos::null)
240 Teuchos::RCP<const Tpetra::Operator<Scalar, LO, GO, Node> >
getMatrix()
const {
245 void solve(Tpetra::MultiVector<Scalar, LO, GO, Node>& X,
const Tpetra::MultiVector<Scalar, LO, GO, Node>& B) {
247 const char prefix[] =
"MueLu::Details::LinearSolver::solve: ";
248 TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::runtime_error, prefix <<
"The solver does not "
249 "exist yet. You must call numeric() before you may call this method.");
250 TEUCHOS_TEST_FOR_EXCEPTION(changedA_, std::runtime_error, prefix <<
"The matrix A has been reset "
251 "since the last call to numeric(). Please call numeric() again.");
252 TEUCHOS_TEST_FOR_EXCEPTION(changedParams_, std::runtime_error, prefix <<
"The parameters have been reset "
253 "since the last call to numeric(). Please call numeric() again.");
255 solver_->apply(B, X);
260 if (solver_ != Teuchos::null && params !=
params_)
261 changedParams_ =
true;
273 const char prefix[] =
"MueLu::Details::LinearSolver::numeric: ";
276 if (solver_ == Teuchos::null || changedParams_) {
277 TEUCHOS_TEST_FOR_EXCEPTION(
A_ == Teuchos::null, std::runtime_error, prefix <<
"The matrix has not been "
278 "set yet. You must call setMatrix() with a nonnull matrix before you may "
279 "call this method.");
287 }
else if (changedA_) {
288 TEUCHOS_TEST_FOR_EXCEPTION(
A_ == Teuchos::null, std::runtime_error, prefix <<
"The matrix has not been "
289 "set yet. You must call setMatrix() with a nonnull matrix before you may "
290 "call this method.");
294 RCP<const Tpetra::CrsMatrix<Scalar, LO, GO, Node> > helperMat;
295 helperMat = rcp_dynamic_cast<const Tpetra::CrsMatrix<Scalar, LO, GO, Node> >(
A_);
296 TEUCHOS_TEST_FOR_EXCEPTION(helperMat.is_null(), std::runtime_error, prefix <<
"MueLu requires "
297 "a Tpetra::CrsMatrix, but the matrix you provided is of a "
298 "different type. Please provide a Tpetra::CrsMatrix instead.");
303 changedParams_ =
false;
308 using Teuchos::TypeNameTraits;
309 if (solver_.is_null()) {
310 std::ostringstream os;
311 os <<
"\"MueLu::Details::LinearSolver\": {"
312 <<
"MV: " << TypeNameTraits<Tpetra::MultiVector<Scalar, LO, GO, Node> >::name()
313 <<
"OP: " << TypeNameTraits<Tpetra::Operator<Scalar, LO, GO, Node> >::name()
314 <<
"NormType: " << TypeNameTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::name()
318 return solver_->GetHierarchy()->description();
325 const Teuchos::EVerbosityLevel verbLevel =
326 Teuchos::Describable::verbLevel_default)
const {
328 using Teuchos::TypeNameTraits;
329 if (solver_.is_null()) {
330 if (verbLevel > Teuchos::VERB_NONE) {
331 Teuchos::OSTab tab0(out);
332 out <<
"\"MueLu::Details::LinearSolver\":" << endl;
333 Teuchos::OSTab tab1(out);
334 out <<
"MV: " << TypeNameTraits<Tpetra::MultiVector<Scalar, LO, GO, Node> >::name() << endl
335 <<
"OP: " << TypeNameTraits<Tpetra::Operator<Scalar, LO, GO, Node> >::name() << endl
336 <<
"NormType: " << TypeNameTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::name() << endl;
339 solver_->GetHierarchy()->describe(out, verbLevel);
344 Teuchos::RCP<const Tpetra::Operator<Scalar, LO, GO, Node> >
A_;
346 Teuchos::RCP<TpetraOperator<Scalar, LO, GO, Node> >
solver_;
351template <
class MV,
class OP,
class NormType>
352Teuchos::RCP<Trilinos::Details::LinearSolver<MV, OP, NormType> >
359template <
class MV,
class OP,
class NormType>
362#ifdef HAVE_TEUCHOSCORE_CXX11
363 typedef std::shared_ptr<MueLu::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
366 typedef Teuchos::RCP<MueLu::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
371 Trilinos::Details::registerLinearSolverFactory<MV, OP, NormType>(
"MueLu", factory);
384#define MUELU_DETAILS_LINEARSOLVERFACTORY_INSTANT(SC, LO, GO, NT) \
385 template class MueLu::Details::LinearSolverFactory<Tpetra::MultiVector<SC, LO, GO, NT>, \
386 Tpetra::Operator<SC, LO, GO, NT>, \
387 typename Tpetra::MultiVector<SC, LO, GO, NT>::mag_type>;
Various adapters that will create a MueLu preconditioner that is an Epetra_Operator.
Various adapters that will create a MueLu preconditioner that is a Tpetra::Operator.
Interface for a "factory" that creates MueLu solvers.
static void registerLinearSolverFactory()
Register this LinearSolverFactory with the central registry.
virtual Teuchos::RCP< Trilinos::Details::LinearSolver< MV, OP, NormType > > getLinearSolver(const std::string &solverName)
Get an instance of a MueLu solver.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Implementation of Teuchos::Describable::describe.
void setMatrix(const Teuchos::RCP< const Tpetra::Operator< Scalar, LO, GO, Node > > &A)
Set the Solver's matrix.
Teuchos::RCP< TpetraOperator< Scalar, LO, GO, Node > > solver_
void symbolic()
Set up any part of the solve that depends on the structure of the input matrix, but not its numerical...
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set this solver's parameters.
Teuchos::RCP< const Tpetra::Operator< Scalar, LO, GO, Node > > getMatrix() const
Get a pointer to this Solver's matrix.
void solve(Tpetra::MultiVector< Scalar, LO, GO, Node > &X, const Tpetra::MultiVector< Scalar, LO, GO, Node > &B)
Solve the linear system(s) AX=B.
virtual ~LinearSolver()
Destructor (virtual for memory safety).
Teuchos::RCP< Teuchos::ParameterList > params_
void numeric()
Set up any part of the solve that depends on both the structure and the numerical values of the input...
std::string description() const
Implementation of Teuchos::Describable::description.
LinearSolver()
Constructor.
Teuchos::RCP< const Tpetra::Operator< Scalar, LO, GO, Node > > A_
Teuchos::RCP< Teuchos::ParameterList > params_
std::string description() const
Implementation of Teuchos::Describable::description.
void symbolic()
Set up any part of the solve that depends on the structure of the input matrix, but not its numerical...
virtual ~LinearSolver()
Destructor (virtual for memory safety).
Teuchos::RCP< const OP > A_
Teuchos::RCP< const OP > getMatrix() const
Get a pointer to this Solver's matrix.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set this solver's parameters.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Implementation of Teuchos::Describable::describe.
void solve(MV &X, const MV &B)
Solve the linear system(s) AX=B.
void numeric()
Set up any part of the solve that depends on both the structure and the numerical values of the input...
LinearSolver()
Constructor.
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the Solver's matrix.
Namespace for MueLu classes and methods.
Teuchos::RCP< MueLu::TpetraOperator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > CreateTpetraPreconditioner(const Teuchos::RCP< Tpetra::Operator< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &inA, Teuchos::ParameterList &inParamList)
Helper function to create a MueLu or AMGX preconditioner that can be used by Tpetra....
void ReuseTpetraPreconditioner(const Teuchos::RCP< Tpetra::CrsMatrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > &inA, MueLu::TpetraOperator< Scalar, LocalOrdinal, GlobalOrdinal, Node > &Op)
Helper function to reuse an existing MueLu preconditioner.
Teuchos::RCP< MueLu::EpetraOperator > CreateEpetraPreconditioner(const Teuchos::RCP< Epetra_CrsMatrix > &inA, Teuchos::ParameterList ¶mListIn)
Helper function to create a MueLu preconditioner that can be used by Epetra.Given a EpetraCrs_Matrix,...