Belos Version of the Day
Loading...
Searching...
No Matches
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
49#include "Trilinos_Details_LinearSolver.hpp"
50
51namespace Belos {
52namespace Details {
53
75template<class MV, class OP, class ScalarType, class NormType>
77 public Trilinos::Details::LinearSolver<MV, OP, NormType>
78{
79private:
84
85public:
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
106 void setMatrix (const Teuchos::RCP<const OP>& A) {
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
115 Teuchos::RCP<const OP> getMatrix () const {
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) {
125 TEUCHOS_TEST_FOR_EXCEPTION
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
151 void setParameters (const Teuchos::RCP<Teuchos::ParameterList>& params) {
152 if (! solver_.is_null () && ! params.is_null ()) {
153 solver_->setParameters (params);
154 }
155 params_ = params;
156 }
157
159 void symbolic () {
160 TEUCHOS_TEST_FOR_EXCEPTION
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 () {
174 TEUCHOS_TEST_FOR_EXCEPTION
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
187private:
189 std::string solverName_;
191 Teuchos::RCP<problem_type> problem_;
193 Teuchos::RCP<solver_type> solver_;
195 Teuchos::RCP<Teuchos::ParameterList> params_;
196};
197
198} // namespace Details
199} // namespace Belos
200
201#endif /* BELOS_DETAILS_LINEARSOLVER_HPP */
Belos' implementation of Trilinos::Details::LinearSolver.
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > &params)
Set the solver's parameters.
void symbolic()
Precompute for matrix structure changes.
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the solver's matrix.
Teuchos::RCP< const OP > getMatrix() const
Get the solver's matrix.
LinearSolver(const std::string &solverName)
Constructor.
virtual ~LinearSolver()
Destructor (virtual for memory safety).
void numeric()
Precompute for matrix values' changes.
void solve(MV &X, const MV &B)
Solve the linear system AX=B for X.
A linear system to solve, and its associated information.
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
typename ::Belos::Impl::SolverFactorySelector< SC, MV, OP >::type SolverFactory

Generated for Belos by doxygen 1.10.0