Belos  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Belos_Details_LinearSolver.hpp
Go to the documentation of this file.
1 //@HEADER
2 // ************************************************************************
3 //
4 // Belos: Block Linear Solvers Package
5 // Copyright 2004 Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 //@HEADER
41 
42 #ifndef BELOS_DETAILS_LINEARSOLVER_HPP
43 #define BELOS_DETAILS_LINEARSOLVER_HPP
44 
47 
48 #include "BelosSolverFactory.hpp"
50 
51 namespace Belos {
52 namespace Details {
53 
75 template<class MV, class OP, class ScalarType, class NormType>
76 class LinearSolver :
77  public Trilinos::Details::LinearSolver<MV, OP, NormType>
78 {
79 private:
84 
85 public:
86 
91  LinearSolver (const std::string& solverName) :
92  solverName_ (solverName)
93  {
94  // In alignment with Belos philosophy, we delay initialization
95  // (which in this case means creation of the solver) until needed.
96  }
97 
98 
100  virtual ~LinearSolver () {}
101 
107  if (problem_.is_null ()) {
108  problem_ = Teuchos::rcp (new problem_type (A, Teuchos::null, Teuchos::null));
109  } else if (A != problem_->getOperator ()) {
110  problem_->setOperator (A);
111  }
112  }
113 
116  if (problem_.is_null ()) {
117  return Teuchos::null;
118  } else {
119  return problem_->getOperator ();
120  }
121  }
122 
124  void solve (MV& X, const MV& B) {
126  (problem_.is_null () || problem_->getOperator ().is_null (),
127  std::runtime_error, "Belos::Details::LinearSolver::solve: "
128  "The matrix A in the linear system to solve has not yet been set. "
129  "Please call setMatrix() with a nonnull input before calling solve().");
130  Teuchos::RCP<MV> X_ptr = Teuchos::rcpFromRef (X);
131  Teuchos::RCP<const MV> B_ptr = Teuchos::rcpFromRef (B);
132 
133  problem_->setLHS (X_ptr);
134  problem_->setRHS (B_ptr);
135  problem_->setProblem ();
136 
137  // We can delay creating the Belos solver until the moment when we
138  // actually need it. This aligns with Belos' preference for lazy
139  // initialization.
140  if (solver_.is_null ()) {
142  solver_ = factory.create (solverName_, params_);
143  solver_->setProblem (problem_);
144  }
145 
146  //Belos::ReturnType ret = solver_->solve ();
147  (void) solver_->solve ();
148  }
149 
152  if (! solver_.is_null () && ! params.is_null ()) {
153  solver_->setParameters (params);
154  }
155  params_ = params;
156  }
157 
159  void symbolic () {
161  (problem_.is_null () || problem_->getOperator ().is_null (),
162  std::runtime_error, "Belos::Details::LinearSolver::symbolic: "
163  "The matrix A in the linear system to solve has not yet been set. "
164  "Please call setMatrix() with a nonnull input before calling this method.");
165 
166  // Belos solvers can't handle changes to the matrix's domain or
167  // range Maps. It's best in this case to destroy the solver and
168  // start over.
169  solver_ = Teuchos::null;
170  }
171 
173  void numeric () {
175  (problem_.is_null () || problem_->getOperator ().is_null (),
176  std::runtime_error, "Belos::Details::LinearSolver::numeric: "
177  "The matrix A in the linear system to solve has not yet been set. "
178  "Please call setMatrix() with a nonnull input before calling this method.");
179  // NOTE (mfh 23 Aug 2015) For the seed or recycling solvers, it
180  // would make sense to do something special here. However, the
181  // line below is always correct. It recomputes the initial
182  // residual vector, which is what Belos expects before a solve if
183  // the matrix or right-hand side may have changed.
184  problem_->setProblem ();
185  }
186 
187 private:
189  std::string solverName_;
196 };
197 
198 } // namespace Details
199 } // namespace Belos
200 
201 #endif /* BELOS_DETAILS_LINEARSOLVER_HPP */
void solve(MV &X, const MV &B)
Solve the linear system AX=B for X.
Belos&#39; implementation of Trilinos::Details::LinearSolver.
virtual ~LinearSolver()
Destructor (virtual for memory safety).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the solver&#39;s matrix.
void symbolic()
Precompute for matrix structure changes.
void numeric()
Precompute for matrix values&#39; changes.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Teuchos::RCP< const OP > getMatrix() const
Get the solver&#39;s matrix.
A linear system to solve, and its associated information.
LinearSolver(const std::string &solverName)
Constructor.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set the solver&#39;s parameters.
typename::Belos::Impl::SolverFactorySelector< SC, MV, OP >::type SolverFactory
bool is_null() const

Generated on Fri Aug 14 2020 10:48:33 for Belos by doxygen 1.8.5