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 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // Ifpack2: Templated Object-Oriented Algebraic Preconditioner Package
6 // Copyright (2009) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
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 Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
47 
48 #ifndef IFPACK2_DETAILS_LINEARSOLVER_DEF_HPP
49 #define IFPACK2_DETAILS_LINEARSOLVER_DEF_HPP
50 
52 #include "Tpetra_MultiVector.hpp"
53 
54 // Ifpack2: key is for Ifpack2's factory to have subordinate
55 // factories. That way, each package still has one factory, but we
56 // don't have to worry about intrapackage circular dependencies (e.g.,
57 // relating to AdditiveSchwarz). There are two approaches:
58 //
59 // 1. Reuse existing Ifpack2::Details::OneLevelFactory
60 // 2. Have each Ifpack2 solver register itself with Ifpack2's factory
61 
62 namespace Ifpack2 {
63 namespace Details {
64 
65 template<class SC, class LO, class GO, class NT>
67 LinearSolver (const Teuchos::RCP<prec_type>& solver, const std::string& solverName) :
68  solver_ (solver),
69  solverName_ (solverName)
70 {
71  using Teuchos::RCP;
72  using Teuchos::rcp_dynamic_cast;
73  const char prefix[] = "Ifpack2::Details::LinearSolver: ";
74  TEUCHOS_TEST_FOR_EXCEPTION(solver.is_null (), std::invalid_argument,
75  prefix << "Input solver is NULL.");
76 
77  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
78  typedef ::Ifpack2::Details::CanChangeMatrix<row_matrix_type> mixin_type;
79  RCP<mixin_type> innerSolver = rcp_dynamic_cast<mixin_type> (solver);
81  (innerSolver.is_null (), std::invalid_argument, prefix << "The input "
82  "solver does not implement the setMatrix() feature. Only Ifpack2 solvers "
83  "that inherit from Ifpack2::Details::CanChangeMatrix implement this feature.");
84 }
85 
86 template<class SC, class LO, class GO, class NT>
87 void
90 {
91  using Teuchos::RCP;
92  using Teuchos::rcp_dynamic_cast;
93  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
94  typedef ::Ifpack2::Details::CanChangeMatrix<row_matrix_type> mixin_type;
95  const char prefix[] = "Ifpack2::Details::LinearSolver::setMatrix: ";
96 
97  // It's OK for the input matrix to be null. Ifpack2 solvers may
98  // interpret this as a hint to clear out their state. It's also a
99  // way to keep the preconditioner around, but disassociate it from a
100  // particular matrix. (The code that uses the preconditioner might
101  // not want to or be able to recreate it.)
102  RCP<const row_matrix_type> A_row;
103  if (! A.is_null ()) {
104  A_row = rcp_dynamic_cast<const row_matrix_type> (A);
106  (A_row.is_null (), std::invalid_argument, prefix << "The input matrix A, "
107  "if not null, must be a Tpetra::RowMatrix.");
108  }
110  (solver_.is_null (), std::logic_error, prefix << "Solver is NULL. "
111  "This should never happen! Please report this bug to the Ifpack2 "
112  "developers.");
113 
114  RCP<mixin_type> innerSolver = rcp_dynamic_cast<mixin_type> (solver_);
116  (innerSolver.is_null (), std::logic_error, prefix << "The solver does not "
117  "implement the setMatrix() feature. Only input preconditioners that "
118  "inherit from Ifpack2::Details::CanChangeMatrix implement this. We should"
119  " never get here! Please report this bug to the Ifpack2 developers.");
120  innerSolver->setMatrix (A_row);
121 
122  A_ = A; // keep a pointer to A, so that getMatrix() works
123 }
124 
125 template<class SC, class LO, class GO, class NT>
128 getMatrix () const {
129  return A_; // may be null
130 }
131 
132 template<class SC, class LO, class GO, class NT>
133 void
135 solve (MV& X, const MV& B)
136 {
137  const char prefix[] = "Ifpack2::Details::LinearSolver::solve: ";
139  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
140  "This should never happen. Please report this bug to the Ifpack2 "
141  "developers.");
143  (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
144  "set yet. You must call setMatrix() with a nonnull matrix before you "
145  "may call this method.");
146  solver_->apply (B, X);
147 }
148 
149 template<class SC, class LO, class GO, class NT>
150 void
153 {
154  solver_->setParameters (*params);
155 }
156 
157 template<class SC, class LO, class GO, class NT>
158 void
161 {
162  const char prefix[] = "Ifpack2::Details::LinearSolver::symbolic: ";
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.");
168  (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
169  "set yet. You must call setMatrix() with a nonnull matrix before you "
170  "may call this method.");
171  solver_->initialize ();
172 }
173 
174 template<class SC, class LO, class GO, class NT>
175 void
178 {
179  const char prefix[] = "Ifpack2::Details::LinearSolver::numeric: ";
181  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
182  "This should never happen. Please report this bug to the Ifpack2 "
183  "developers.");
185  (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
186  "set yet. You must call setMatrix() with a nonnull matrix before you "
187  "may call this method.");
188  solver_->compute ();
189 }
190 
191 template<class SC, class LO, class GO, class NT>
192 std::string
194 description () const
195 {
196  const char prefix[] = "Ifpack2::Details::LinearSolver::description: ";
198  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
199  "This should never happen. Please report this bug to the Ifpack2 "
200  "developers.");
201  return solver_->description ();
202 }
203 
204 template<class SC, class LO, class GO, class NT>
205 void
208  const Teuchos::EVerbosityLevel verbLevel) const
209 {
210  const char prefix[] = "Ifpack2::Details::LinearSolver::describe: ";
212  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
213  "This should never happen. Please report this bug to the Ifpack2 "
214  "developers.");
215  solver_->describe (out, verbLevel);
216 }
217 
218 } // namespace Details
219 } // namespace Ifpack2
220 
221 // Explicit template instantiation macro for LinearSolver. This is
222 // generally not for users! It is used by automatically generated
223 // code, and perhaps by expert Trilinos developers.
224 #define IFPACK2_DETAILS_LINEARSOLVER_INSTANT(SC, LO, GO, NT) \
225  template class Ifpack2::Details::LinearSolver<SC, LO, GO, NT>;
226 
227 #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:135
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:207
Teuchos::RCP< const OP > getMatrix() const
Get the solver&#39;s matrix.
Definition: Ifpack2_Details_LinearSolver_def.hpp:128
void symbolic()
Precompute for matrix structure changes.
Definition: Ifpack2_Details_LinearSolver_def.hpp:160
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the solver&#39;s matrix.
Definition: Ifpack2_Details_LinearSolver_def.hpp:89
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set the solver&#39;s parameters.
Definition: Ifpack2_Details_LinearSolver_def.hpp:152
void numeric()
Precompute for matrix values&#39; changes.
Definition: Ifpack2_Details_LinearSolver_def.hpp:177
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:194
LinearSolver(const Teuchos::RCP< prec_type > &solver, const std::string &solverName)
Constructor.
Definition: Ifpack2_Details_LinearSolver_def.hpp:67
bool is_null() const