Ifpack2 Templated Preconditioning Package Version 1.0
Loading...
Searching...
No Matches
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
28namespace Ifpack2 {
29namespace Details {
30
31template<class SC, class LO, class GO, class NT>
33LinearSolver (const Teuchos::RCP<prec_type>& solver, const std::string& solverName) :
34 solver_ (solver),
35 solverName_ (solverName)
36{
37 using Teuchos::RCP;
38 using Teuchos::rcp_dynamic_cast;
39 const char prefix[] = "Ifpack2::Details::LinearSolver: ";
40 TEUCHOS_TEST_FOR_EXCEPTION(solver.is_null (), std::invalid_argument,
41 prefix << "Input solver is NULL.");
42
43 typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
44 typedef ::Ifpack2::Details::CanChangeMatrix<row_matrix_type> mixin_type;
45 RCP<mixin_type> innerSolver = rcp_dynamic_cast<mixin_type> (solver);
46 TEUCHOS_TEST_FOR_EXCEPTION
47 (innerSolver.is_null (), std::invalid_argument, prefix << "The input "
48 "solver does not implement the setMatrix() feature. Only Ifpack2 solvers "
49 "that inherit from Ifpack2::Details::CanChangeMatrix implement this feature.");
50}
51
52template<class SC, class LO, class GO, class NT>
53void
55setMatrix (const Teuchos::RCP<const OP>& A)
56{
57 using Teuchos::RCP;
58 using Teuchos::rcp_dynamic_cast;
59 typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
60 typedef ::Ifpack2::Details::CanChangeMatrix<row_matrix_type> mixin_type;
61 const char prefix[] = "Ifpack2::Details::LinearSolver::setMatrix: ";
62
63 // It's OK for the input matrix to be null. Ifpack2 solvers may
64 // interpret this as a hint to clear out their state. It's also a
65 // way to keep the preconditioner around, but disassociate it from a
66 // particular matrix. (The code that uses the preconditioner might
67 // not want to or be able to recreate it.)
68 RCP<const row_matrix_type> A_row;
69 if (! A.is_null ()) {
70 A_row = rcp_dynamic_cast<const row_matrix_type> (A);
71 TEUCHOS_TEST_FOR_EXCEPTION
72 (A_row.is_null (), std::invalid_argument, prefix << "The input matrix A, "
73 "if not null, must be a Tpetra::RowMatrix.");
74 }
75 TEUCHOS_TEST_FOR_EXCEPTION
76 (solver_.is_null (), std::logic_error, prefix << "Solver is NULL. "
77 "This should never happen! Please report this bug to the Ifpack2 "
78 "developers.");
79
80 RCP<mixin_type> innerSolver = rcp_dynamic_cast<mixin_type> (solver_);
81 TEUCHOS_TEST_FOR_EXCEPTION
82 (innerSolver.is_null (), std::logic_error, prefix << "The solver does not "
83 "implement the setMatrix() feature. Only input preconditioners that "
84 "inherit from Ifpack2::Details::CanChangeMatrix implement this. We should"
85 " never get here! Please report this bug to the Ifpack2 developers.");
86 innerSolver->setMatrix (A_row);
87
88 A_ = A; // keep a pointer to A, so that getMatrix() works
89}
90
91template<class SC, class LO, class GO, class NT>
92Teuchos::RCP<const typename LinearSolver<SC, LO, GO, NT>::OP>
94getMatrix () const {
95 return A_; // may be null
96}
97
98template<class SC, class LO, class GO, class NT>
99void
101solve (MV& X, const MV& B)
102{
103 const char prefix[] = "Ifpack2::Details::LinearSolver::solve: ";
104 TEUCHOS_TEST_FOR_EXCEPTION
105 (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
106 "This should never happen. Please report this bug to the Ifpack2 "
107 "developers.");
108 TEUCHOS_TEST_FOR_EXCEPTION
109 (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
110 "set yet. You must call setMatrix() with a nonnull matrix before you "
111 "may call this method.");
112 solver_->apply (B, X);
113}
114
115template<class SC, class LO, class GO, class NT>
116void
118setParameters (const Teuchos::RCP<Teuchos::ParameterList>& params)
119{
120 solver_->setParameters (*params);
121}
122
123template<class SC, class LO, class GO, class NT>
124void
126symbolic ()
127{
128 const char prefix[] = "Ifpack2::Details::LinearSolver::symbolic: ";
129 TEUCHOS_TEST_FOR_EXCEPTION
130 (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
131 "This should never happen. Please report this bug to the Ifpack2 "
132 "developers.");
133 TEUCHOS_TEST_FOR_EXCEPTION
134 (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
135 "set yet. You must call setMatrix() with a nonnull matrix before you "
136 "may call this method.");
137 solver_->initialize ();
138}
139
140template<class SC, class LO, class GO, class NT>
141void
143numeric ()
144{
145 const char prefix[] = "Ifpack2::Details::LinearSolver::numeric: ";
146 TEUCHOS_TEST_FOR_EXCEPTION
147 (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
148 "This should never happen. Please report this bug to the Ifpack2 "
149 "developers.");
150 TEUCHOS_TEST_FOR_EXCEPTION
151 (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
152 "set yet. You must call setMatrix() with a nonnull matrix before you "
153 "may call this method.");
154 solver_->compute ();
155}
156
157template<class SC, class LO, class GO, class NT>
158std::string
160description () const
161{
162 const char prefix[] = "Ifpack2::Details::LinearSolver::description: ";
163 TEUCHOS_TEST_FOR_EXCEPTION
164 (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
165 "This should never happen. Please report this bug to the Ifpack2 "
166 "developers.");
167 return solver_->description ();
168}
169
170template<class SC, class LO, class GO, class NT>
171void
173describe (Teuchos::FancyOStream& out,
174 const Teuchos::EVerbosityLevel verbLevel) const
175{
176 const char prefix[] = "Ifpack2::Details::LinearSolver::describe: ";
177 TEUCHOS_TEST_FOR_EXCEPTION
178 (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
179 "This should never happen. Please report this bug to the Ifpack2 "
180 "developers.");
181 solver_->describe (out, verbLevel);
182}
183
184} // namespace Details
185} // namespace Ifpack2
186
187// Explicit template instantiation macro for LinearSolver. This is
188// generally not for users! It is used by automatically generated
189// code, and perhaps by expert Trilinos developers.
190#define IFPACK2_DETAILS_LINEARSOLVER_INSTANT(SC, LO, GO, NT) \
191 template class Ifpack2::Details::LinearSolver<SC, LO, GO, NT>;
192
193#endif // IFPACK2_DETAILS_LINEARSOLVER_DEF_HPP
Declaration of interface for preconditioners that can change their matrix after construction.
void numeric()
Precompute for matrix values' changes.
Definition Ifpack2_Details_LinearSolver_def.hpp:143
void symbolic()
Precompute for matrix structure changes.
Definition Ifpack2_Details_LinearSolver_def.hpp:126
void solve(MV &X, const MV &B)
Solve the linear system AX=B for X.
Definition Ifpack2_Details_LinearSolver_def.hpp:101
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the solver's matrix.
Definition Ifpack2_Details_LinearSolver_def.hpp:55
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set the solver's parameters.
Definition Ifpack2_Details_LinearSolver_def.hpp:118
std::string description() const
Implementation of Teuchos::Describable::description.
Definition Ifpack2_Details_LinearSolver_def.hpp:160
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:173
Teuchos::RCP< const OP > getMatrix() const
Get the solver's matrix.
Definition Ifpack2_Details_LinearSolver_def.hpp:94
LinearSolver(const Teuchos::RCP< prec_type > &solver, const std::string &solverName)
Constructor.
Definition Ifpack2_Details_LinearSolver_def.hpp:33
Preconditioners and smoothers for Tpetra sparse matrices.
Definition Ifpack2_AdditiveSchwarz_decl.hpp:41