48#ifndef MUELU_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
49#define MUELU_DETAILS_LINEARSOLVERFACTORY_DEF_HPP
51#include "MueLu_config.hpp"
52#include "Trilinos_Details_LinearSolver.hpp"
53#include "Trilinos_Details_LinearSolverFactory.hpp"
56#ifdef HAVE_MUELU_EPETRA
57# include "Epetra_CrsMatrix.h"
61# include "Tpetra_Operator.hpp"
67template<
class MV,
class OP,
class NormType>
69 public Trilinos::Details::LinearSolver<MV, OP, NormType>,
70 virtual public Teuchos::Describable
93 void solve (MV& X,
const MV& B);
96 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params);
112 const Teuchos::EVerbosityLevel verbLevel =
113 Teuchos::Describable::verbLevel_default)
const;
116 Teuchos::RCP<const OP>
A_;
121#if defined(HAVE_MUELU_SERIAL) and defined(HAVE_MUELU_EPETRA)
124 public Trilinos::Details::LinearSolver<Epetra_MultiVector, Epetra_Operator, double>,
125 virtual public Teuchos::Describable
133 changedParams_(
false)
143 void setMatrix (
const Teuchos::RCP<const Epetra_Operator>& A)
145 const char prefix[] =
"MueLu::Details::LinearSolver::setMatrix: ";
149 if(solver_ != Teuchos::null)
152 A_ = rcp_dynamic_cast<const Epetra_CrsMatrix>(A);
153 TEUCHOS_TEST_FOR_EXCEPTION
154 (
A_.is_null(), std::runtime_error, prefix <<
"MueLu requires "
155 "an Epetra_CrsMatrix, but the matrix you provided is of a "
156 "different type. Please provide an Epetra_CrsMatrix instead.");
161 Teuchos::RCP<const Epetra_Operator>
getMatrix ()
const {
166 void solve (Epetra_MultiVector& X,
const Epetra_MultiVector& B)
169 const char prefix[] =
"MueLu::Details::LinearSolver::solve: ";
170 TEUCHOS_TEST_FOR_EXCEPTION
171 (solver_.is_null (), std::runtime_error, prefix <<
"The solver does not "
172 "exist yet. You must call numeric() before you may call this method.");
173 TEUCHOS_TEST_FOR_EXCEPTION
174 (changedA_, std::runtime_error, prefix <<
"The matrix A has been reset "
175 "since the last call to numeric(). Please call numeric() again.");
176 TEUCHOS_TEST_FOR_EXCEPTION
177 (changedParams_, std::runtime_error, prefix <<
"The parameters have been reset "
178 "since the last call to numeric(). Please call numeric() again.");
180 int err = solver_->ApplyInverse(B, X);
182 TEUCHOS_TEST_FOR_EXCEPTION
183 (err != 0, std::runtime_error, prefix <<
"EpetraOperator::ApplyInverse returned "
184 "nonzero error code " << err);
188 void setParameters (
const Teuchos::RCP<Teuchos::ParameterList>& params)
190 if(solver_ != Teuchos::null && params !=
params_)
191 changedParams_ =
true;
204 const char prefix[] =
"MueLu::Details::LinearSolver::numeric: ";
207 if(solver_ == Teuchos::null || changedA_ || changedParams_)
210 changedParams_ =
false;
212 TEUCHOS_TEST_FOR_EXCEPTION
213 (
A_ == Teuchos::null, std::runtime_error, prefix <<
"The matrix has not been "
214 "set yet. You must call setMatrix() with a nonnull matrix before you may "
215 "call this method.");
229 if (solver_.is_null()) {
230 return "\"MueLu::Details::LinearSolver\": {MV: Epetra_MultiVector, OP: Epetra_Operator, NormType: double}";
233 return solver_->GetHierarchy()->description ();
239 describe (Teuchos::FancyOStream& out,
240 const Teuchos::EVerbosityLevel verbLevel =
241 Teuchos::Describable::verbLevel_default)
const
244 if (solver_.is_null()) {
245 if(verbLevel > Teuchos::VERB_NONE) {
246 Teuchos::OSTab tab0 (out);
247 out <<
"\"MueLu::Details::LinearSolver\":" << endl;
248 Teuchos::OSTab tab1 (out);
249 out <<
"MV: Epetra_MultiVector" << endl
250 <<
"OP: Epetra_Operator" << endl
251 <<
"NormType: double" << endl;
255 solver_->GetHierarchy()->describe (out, verbLevel);
260 Teuchos::RCP<const Epetra_CrsMatrix>
A_;
261 Teuchos::RCP<Teuchos::ParameterList>
params_;
262 Teuchos::RCP<EpetraOperator> solver_;
268template<
class Scalar,
class LO,
class GO,
class Node>
270 Tpetra::Operator<Scalar,LO,GO,Node>,
271 typename Teuchos::ScalarTraits<Scalar>::magnitudeType> :
272 public Trilinos::Details::LinearSolver<Tpetra::MultiVector<Scalar,LO,GO,Node>,
273 Tpetra::Operator<Scalar,LO,GO,Node>,
274 typename Teuchos::ScalarTraits<Scalar>::magnitudeType>,
275 virtual public Teuchos::Describable
293 void setMatrix (
const Teuchos::RCP<
const Tpetra::Operator<Scalar,LO,GO,Node> >& A)
305 Teuchos::RCP<const Tpetra::Operator<Scalar,LO,GO,Node> >
getMatrix ()
const {
310 void solve (Tpetra::MultiVector<Scalar,LO,GO,Node>& X,
const Tpetra::MultiVector<Scalar,LO,GO,Node>& B)
313 const char prefix[] =
"MueLu::Details::LinearSolver::solve: ";
314 TEUCHOS_TEST_FOR_EXCEPTION
315 (
solver_.is_null (), std::runtime_error, prefix <<
"The solver does not "
316 "exist yet. You must call numeric() before you may call this method.");
317 TEUCHOS_TEST_FOR_EXCEPTION
318 (
changedA_, std::runtime_error, prefix <<
"The matrix A has been reset "
319 "since the last call to numeric(). Please call numeric() again.");
320 TEUCHOS_TEST_FOR_EXCEPTION
321 (
changedParams_, std::runtime_error, prefix <<
"The parameters have been reset "
322 "since the last call to numeric(). Please call numeric() again.");
344 const char prefix[] =
"MueLu::Details::LinearSolver::numeric: ";
349 TEUCHOS_TEST_FOR_EXCEPTION
350 (
A_ == Teuchos::null, std::runtime_error, prefix <<
"The matrix has not been "
351 "set yet. You must call setMatrix() with a nonnull matrix before you may "
352 "call this method.");
363 TEUCHOS_TEST_FOR_EXCEPTION
364 (
A_ == Teuchos::null, std::runtime_error, prefix <<
"The matrix has not been "
365 "set yet. You must call setMatrix() with a nonnull matrix before you may "
366 "call this method.");
370 RCP<const Tpetra::CrsMatrix<Scalar,LO,GO,Node> > helperMat;
371 helperMat = rcp_dynamic_cast<const Tpetra::CrsMatrix<Scalar,LO,GO,Node> >(
A_);
372 TEUCHOS_TEST_FOR_EXCEPTION
373 (helperMat.is_null(), std::runtime_error, prefix <<
"MueLu requires "
374 "a Tpetra::CrsMatrix, but the matrix you provided is of a "
375 "different type. Please provide a Tpetra::CrsMatrix instead.");
386 using Teuchos::TypeNameTraits;
388 std::ostringstream os;
389 os <<
"\"MueLu::Details::LinearSolver\": {"
390 <<
"MV: " << TypeNameTraits<Tpetra::MultiVector<Scalar,LO,GO,Node> >::name()
391 <<
"OP: " << TypeNameTraits<Tpetra::Operator<Scalar,LO,GO,Node> >::name()
392 <<
"NormType: " << TypeNameTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::name()
397 return solver_->GetHierarchy()->description ();
404 const Teuchos::EVerbosityLevel verbLevel =
405 Teuchos::Describable::verbLevel_default)
const
407 using Teuchos::TypeNameTraits;
410 if(verbLevel > Teuchos::VERB_NONE) {
411 Teuchos::OSTab tab0 (out);
412 out <<
"\"MueLu::Details::LinearSolver\":" << endl;
413 Teuchos::OSTab tab1 (out);
414 out <<
"MV: " << TypeNameTraits<Tpetra::MultiVector<Scalar,LO,GO,Node> >::name() << endl
415 <<
"OP: " << TypeNameTraits<Tpetra::Operator<Scalar,LO,GO,Node> >::name() << endl
416 <<
"NormType: " << TypeNameTraits<typename Teuchos::ScalarTraits<Scalar>::magnitudeType>::name() << endl;
420 solver_->GetHierarchy()->describe (out, verbLevel);
425 Teuchos::RCP<const Tpetra::Operator<Scalar,LO,GO,Node> >
A_;
427 Teuchos::RCP<TpetraOperator<Scalar,LO,GO,Node> >
solver_;
432template<
class MV,
class OP,
class NormType>
433Teuchos::RCP<Trilinos::Details::LinearSolver<MV, OP, NormType> >
441template<
class MV,
class OP,
class NormType>
446#ifdef HAVE_TEUCHOSCORE_CXX11
447 typedef std::shared_ptr<MueLu::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
450 typedef Teuchos::RCP<MueLu::Details::LinearSolverFactory<MV, OP, NormType> > ptr_type;
455 Trilinos::Details::registerLinearSolverFactory<MV, OP, NormType> (
"MueLu", factory);
468#define MUELU_DETAILS_LINEARSOLVERFACTORY_INSTANT(SC, LO, GO, NT) \
469 template class MueLu::Details::LinearSolverFactory<Tpetra::MultiVector<SC, LO, GO, NT>, \
470 Tpetra::Operator<SC, LO, GO, NT>, \
471 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.
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.
Teuchos::RCP< const Tpetra::Operator< Scalar, LO, GO, Node > > A_
Teuchos::RCP< TpetraOperator< Scalar, LO, GO, Node > > solver_
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< 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,...