Ifpack2 Templated Preconditioning Package  Version 1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Ifpack2_Details_LinearSolver_def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
4 //
5 // Copyright 2009 NTESS and the Ifpack2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
13 
14 #ifndef IFPACK2_DETAILS_LINEARSOLVER_DEF_HPP
15 #define IFPACK2_DETAILS_LINEARSOLVER_DEF_HPP
16 
18 #include "Tpetra_MultiVector.hpp"
19 
20 // Ifpack2: key is for Ifpack2's factory to have subordinate
21 // factories. That way, each package still has one factory, but we
22 // don't have to worry about intrapackage circular dependencies (e.g.,
23 // relating to AdditiveSchwarz). There are two approaches:
24 //
25 // 1. Reuse existing Ifpack2::Details::OneLevelFactory
26 // 2. Have each Ifpack2 solver register itself with Ifpack2's factory
27 
28 namespace Ifpack2 {
29 namespace Details {
30 
31 template <class SC, class LO, class GO, class NT>
33  LinearSolver(const Teuchos::RCP<prec_type>& solver, const std::string& solverName)
34  : solver_(solver)
35  , solverName_(solverName) {
36  using Teuchos::RCP;
37  using Teuchos::rcp_dynamic_cast;
38  const char prefix[] = "Ifpack2::Details::LinearSolver: ";
39  TEUCHOS_TEST_FOR_EXCEPTION(solver.is_null(), std::invalid_argument,
40  prefix << "Input solver is NULL.");
41 
42  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
43  typedef ::Ifpack2::Details::CanChangeMatrix<row_matrix_type> mixin_type;
44  RCP<mixin_type> innerSolver = rcp_dynamic_cast<mixin_type>(solver);
45  TEUCHOS_TEST_FOR_EXCEPTION(innerSolver.is_null(), std::invalid_argument, prefix << "The input "
46  "solver does not implement the setMatrix() feature. Only Ifpack2 solvers "
47  "that inherit from Ifpack2::Details::CanChangeMatrix implement this feature.");
48 }
49 
50 template <class SC, class LO, class GO, class NT>
53  using Teuchos::RCP;
54  using Teuchos::rcp_dynamic_cast;
55  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
56  typedef ::Ifpack2::Details::CanChangeMatrix<row_matrix_type> mixin_type;
57  const char prefix[] = "Ifpack2::Details::LinearSolver::setMatrix: ";
58 
59  // It's OK for the input matrix to be null. Ifpack2 solvers may
60  // interpret this as a hint to clear out their state. It's also a
61  // way to keep the preconditioner around, but disassociate it from a
62  // particular matrix. (The code that uses the preconditioner might
63  // not want to or be able to recreate it.)
64  RCP<const row_matrix_type> A_row;
65  if (!A.is_null()) {
66  A_row = rcp_dynamic_cast<const row_matrix_type>(A);
67  TEUCHOS_TEST_FOR_EXCEPTION(A_row.is_null(), std::invalid_argument, prefix << "The input matrix A, "
68  "if not null, must be a Tpetra::RowMatrix.");
69  }
70  TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::logic_error, prefix << "Solver is NULL. "
71  "This should never happen! Please report this bug to the Ifpack2 "
72  "developers.");
73 
74  RCP<mixin_type> innerSolver = rcp_dynamic_cast<mixin_type>(solver_);
75  TEUCHOS_TEST_FOR_EXCEPTION(innerSolver.is_null(), std::logic_error, prefix << "The solver does not "
76  "implement the setMatrix() feature. Only input preconditioners that "
77  "inherit from Ifpack2::Details::CanChangeMatrix implement this. We should"
78  " never get here! Please report this bug to the Ifpack2 developers.");
79  innerSolver->setMatrix(A_row);
80 
81  A_ = A; // keep a pointer to A, so that getMatrix() works
82 }
83 
84 template <class SC, class LO, class GO, class NT>
87  getMatrix() const {
88  return A_; // may be null
89 }
90 
91 template <class SC, class LO, class GO, class NT>
93  solve(MV& X, const MV& B) {
94  const char prefix[] = "Ifpack2::Details::LinearSolver::solve: ";
95  TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::logic_error, prefix << "The solver is NULL! "
96  "This should never happen. Please report this bug to the Ifpack2 "
97  "developers.");
98  TEUCHOS_TEST_FOR_EXCEPTION(A_.is_null(), std::runtime_error, prefix << "The matrix has not been "
99  "set yet. You must call setMatrix() with a nonnull matrix before you "
100  "may call this method.");
101  solver_->apply(B, X);
102 }
103 
104 template <class SC, class LO, class GO, class NT>
107  solver_->setParameters(*params);
108 }
109 
110 template <class SC, class LO, class GO, class NT>
113  const char prefix[] = "Ifpack2::Details::LinearSolver::symbolic: ";
114  TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::logic_error, prefix << "The solver is NULL! "
115  "This should never happen. Please report this bug to the Ifpack2 "
116  "developers.");
117  TEUCHOS_TEST_FOR_EXCEPTION(A_.is_null(), std::runtime_error, prefix << "The matrix has not been "
118  "set yet. You must call setMatrix() with a nonnull matrix before you "
119  "may call this method.");
120  solver_->initialize();
121 }
122 
123 template <class SC, class LO, class GO, class NT>
126  const char prefix[] = "Ifpack2::Details::LinearSolver::numeric: ";
127  TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::logic_error, prefix << "The solver is NULL! "
128  "This should never happen. Please report this bug to the Ifpack2 "
129  "developers.");
130  TEUCHOS_TEST_FOR_EXCEPTION(A_.is_null(), std::runtime_error, prefix << "The matrix has not been "
131  "set yet. You must call setMatrix() with a nonnull matrix before you "
132  "may call this method.");
133  solver_->compute();
134 }
135 
136 template <class SC, class LO, class GO, class NT>
137 std::string
139  description() const {
140  const char prefix[] = "Ifpack2::Details::LinearSolver::description: ";
141  TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::logic_error, prefix << "The solver is NULL! "
142  "This should never happen. Please report this bug to the Ifpack2 "
143  "developers.");
144  return solver_->description();
145 }
146 
147 template <class SC, class LO, class GO, class NT>
150  const Teuchos::EVerbosityLevel verbLevel) const {
151  const char prefix[] = "Ifpack2::Details::LinearSolver::describe: ";
152  TEUCHOS_TEST_FOR_EXCEPTION(solver_.is_null(), std::logic_error, prefix << "The solver is NULL! "
153  "This should never happen. Please report this bug to the Ifpack2 "
154  "developers.");
155  solver_->describe(out, verbLevel);
156 }
157 
158 } // namespace Details
159 } // namespace Ifpack2
160 
161 // Explicit template instantiation macro for LinearSolver. This is
162 // generally not for users! It is used by automatically generated
163 // code, and perhaps by expert Trilinos developers.
164 #define IFPACK2_DETAILS_LINEARSOLVER_INSTANT(SC, LO, GO, NT) \
165  template class Ifpack2::Details::LinearSolver<SC, LO, GO, NT>;
166 
167 #endif // IFPACK2_DETAILS_LINEARSOLVER_DEF_HPP
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void solve(MV &X, const MV &B)
Solve the linear system AX=B for X.
Definition: Ifpack2_Details_LinearSolver_def.hpp:93
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Implementation of Teuchos::Describable::describe.
Definition: Ifpack2_Details_LinearSolver_def.hpp:149
Teuchos::RCP< const OP > getMatrix() const
Get the solver&#39;s matrix.
Definition: Ifpack2_Details_LinearSolver_def.hpp:87
void symbolic()
Precompute for matrix structure changes.
Definition: Ifpack2_Details_LinearSolver_def.hpp:112
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the solver&#39;s matrix.
Definition: Ifpack2_Details_LinearSolver_def.hpp:52
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set the solver&#39;s parameters.
Definition: Ifpack2_Details_LinearSolver_def.hpp:106
void numeric()
Precompute for matrix values&#39; changes.
Definition: Ifpack2_Details_LinearSolver_def.hpp:125
Declaration of interface for preconditioners that can change their matrix after construction.
std::string description() const
Implementation of Teuchos::Describable::description.
Definition: Ifpack2_Details_LinearSolver_def.hpp:139
LinearSolver(const Teuchos::RCP< prec_type > &solver, const std::string &solverName)
Constructor.
Definition: Ifpack2_Details_LinearSolver_def.hpp:33
bool is_null() const