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 
53 // Ifpack2: key is for Ifpack2's factory to have subordinate
54 // factories. That way, each package still has one factory, but we
55 // don't have to worry about intrapackage circular dependencies (e.g.,
56 // relating to AdditiveSchwarz). There are two approaches:
57 //
58 // 1. Reuse existing Ifpack2::Details::OneLevelFactory
59 // 2. Have each Ifpack2 solver register itself with Ifpack2's factory
60 
61 namespace Ifpack2 {
62 namespace Details {
63 
64 template<class SC, class LO, class GO, class NT>
66 LinearSolver (const Teuchos::RCP<prec_type>& solver, const std::string& solverName) :
67  solver_ (solver),
68  solverName_ (solverName)
69 {
70  using Teuchos::RCP;
71  using Teuchos::rcp_dynamic_cast;
72  const char prefix[] = "Ifpack2::Details::LinearSolver: ";
73  TEUCHOS_TEST_FOR_EXCEPTION(solver.is_null (), std::invalid_argument,
74  prefix << "Input solver is NULL.");
75 
76  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
77  typedef ::Ifpack2::Details::CanChangeMatrix<row_matrix_type> mixin_type;
78  RCP<mixin_type> innerSolver = rcp_dynamic_cast<mixin_type> (solver);
80  (innerSolver.is_null (), std::invalid_argument, prefix << "The input "
81  "solver does not implement the setMatrix() feature. Only Ifpack2 solvers "
82  "that inherit from Ifpack2::Details::CanChangeMatrix implement this feature.");
83 }
84 
85 template<class SC, class LO, class GO, class NT>
86 void
89 {
90  using Teuchos::RCP;
91  using Teuchos::rcp_dynamic_cast;
92  typedef Tpetra::RowMatrix<SC, LO, GO, NT> row_matrix_type;
93  typedef ::Ifpack2::Details::CanChangeMatrix<row_matrix_type> mixin_type;
94  const char prefix[] = "Ifpack2::Details::LinearSolver::setMatrix: ";
95 
96  // It's OK for the input matrix to be null. Ifpack2 solvers may
97  // interpret this as a hint to clear out their state. It's also a
98  // way to keep the preconditioner around, but disassociate it from a
99  // particular matrix. (The code that uses the preconditioner might
100  // not want to or be able to recreate it.)
101  RCP<const row_matrix_type> A_row;
102  if (! A.is_null ()) {
103  A_row = rcp_dynamic_cast<const row_matrix_type> (A);
105  (A_row.is_null (), std::invalid_argument, prefix << "The input matrix A, "
106  "if not null, must be a Tpetra::RowMatrix.");
107  }
109  (solver_.is_null (), std::logic_error, prefix << "Solver is NULL. "
110  "This should never happen! Please report this bug to the Ifpack2 "
111  "developers.");
112 
113  RCP<mixin_type> innerSolver = rcp_dynamic_cast<mixin_type> (solver_);
115  (innerSolver.is_null (), std::logic_error, prefix << "The solver does not "
116  "implement the setMatrix() feature. Only input preconditioners that "
117  "inherit from Ifpack2::Details::CanChangeMatrix implement this. We should"
118  " never get here! Please report this bug to the Ifpack2 developers.");
119  innerSolver->setMatrix (A_row);
120 
121  A_ = A; // keep a pointer to A, so that getMatrix() works
122 }
123 
124 template<class SC, class LO, class GO, class NT>
127 getMatrix () const {
128  return A_; // may be null
129 }
130 
131 template<class SC, class LO, class GO, class NT>
132 void
134 solve (MV& X, const MV& B)
135 {
136  const char prefix[] = "Ifpack2::Details::LinearSolver::solve: ";
138  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
139  "This should never happen. Please report this bug to the Ifpack2 "
140  "developers.");
142  (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
143  "set yet. You must call setMatrix() with a nonnull matrix before you "
144  "may call this method.");
145  solver_->apply (B, X);
146 }
147 
148 template<class SC, class LO, class GO, class NT>
149 void
152 {
153  solver_->setParameters (*params);
154 }
155 
156 template<class SC, class LO, class GO, class NT>
157 void
160 {
161  const char prefix[] = "Ifpack2::Details::LinearSolver::symbolic: ";
163  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
164  "This should never happen. Please report this bug to the Ifpack2 "
165  "developers.");
167  (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
168  "set yet. You must call setMatrix() with a nonnull matrix before you "
169  "may call this method.");
170  solver_->initialize ();
171 }
172 
173 template<class SC, class LO, class GO, class NT>
174 void
177 {
178  const char prefix[] = "Ifpack2::Details::LinearSolver::numeric: ";
180  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
181  "This should never happen. Please report this bug to the Ifpack2 "
182  "developers.");
184  (A_.is_null (), std::runtime_error, prefix << "The matrix has not been "
185  "set yet. You must call setMatrix() with a nonnull matrix before you "
186  "may call this method.");
187  solver_->compute ();
188 }
189 
190 template<class SC, class LO, class GO, class NT>
191 std::string
193 description () const
194 {
195  const char prefix[] = "Ifpack2::Details::LinearSolver::description: ";
197  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
198  "This should never happen. Please report this bug to the Ifpack2 "
199  "developers.");
200  return solver_->description ();
201 }
202 
203 template<class SC, class LO, class GO, class NT>
204 void
207  const Teuchos::EVerbosityLevel verbLevel) const
208 {
209  const char prefix[] = "Ifpack2::Details::LinearSolver::describe: ";
211  (solver_.is_null (), std::logic_error, prefix << "The solver is NULL! "
212  "This should never happen. Please report this bug to the Ifpack2 "
213  "developers.");
214  solver_->describe (out, verbLevel);
215 }
216 
217 } // namespace Details
218 } // namespace Ifpack2
219 
220 // Explicit template instantiation macro for LinearSolver. This is
221 // generally not for users! It is used by automatically generated
222 // code, and perhaps by expert Trilinos developers.
223 #define IFPACK2_DETAILS_LINEARSOLVER_INSTANT(SC, LO, GO, NT) \
224  template class Ifpack2::Details::LinearSolver<SC, LO, GO, NT>;
225 
226 #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:134
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:206
Teuchos::RCP< const OP > getMatrix() const
Get the solver&#39;s matrix.
Definition: Ifpack2_Details_LinearSolver_def.hpp:127
void symbolic()
Precompute for matrix structure changes.
Definition: Ifpack2_Details_LinearSolver_def.hpp:159
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the solver&#39;s matrix.
Definition: Ifpack2_Details_LinearSolver_def.hpp:88
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set the solver&#39;s parameters.
Definition: Ifpack2_Details_LinearSolver_def.hpp:151
void numeric()
Precompute for matrix values&#39; changes.
Definition: Ifpack2_Details_LinearSolver_def.hpp:176
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:193
LinearSolver(const Teuchos::RCP< prec_type > &solver, const std::string &solverName)
Constructor.
Definition: Ifpack2_Details_LinearSolver_def.hpp:66
bool is_null() const