Belos
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
src
Belos_Details_LinearSolver.hpp
Go to the documentation of this file.
1
// @HEADER
2
// *****************************************************************************
3
// Belos: Block Linear Solvers Package
4
//
5
// Copyright 2004-2016 NTESS and the Belos contributors.
6
// SPDX-License-Identifier: BSD-3-Clause
7
// *****************************************************************************
8
// @HEADER
9
10
#ifndef BELOS_DETAILS_LINEARSOLVER_HPP
11
#define BELOS_DETAILS_LINEARSOLVER_HPP
12
15
16
#include "
BelosSolverFactory.hpp
"
17
#include "Trilinos_Details_LinearSolver.hpp"
18
19
namespace
Belos
{
20
namespace
Details
{
21
43
template
<
class
MV,
class
OP,
class
ScalarType,
class
NormType>
44
class
LinearSolver
:
45
public
Trilinos::Details::LinearSolver<MV, OP, NormType>
46
{
47
private
:
49
typedef
Belos::LinearProblem<ScalarType, MV, OP>
problem_type;
51
typedef
Belos::SolverManager<ScalarType, MV, OP>
solver_type;
52
53
public
:
54
59
LinearSolver
(
const
std::string& solverName) :
60
solverName_ (solverName)
61
{
62
// In alignment with Belos philosophy, we delay initialization
63
// (which in this case means creation of the solver) until needed.
64
}
65
66
68
virtual
~LinearSolver
() {}
69
74
void
setMatrix
(
const
Teuchos::RCP<const OP>& A) {
75
if
(problem_.is_null ()) {
76
problem_ = Teuchos::rcp (
new
problem_type (A, Teuchos::null, Teuchos::null));
77
}
else
if
(A != problem_->getOperator ()) {
78
problem_->setOperator (A);
79
}
80
}
81
83
Teuchos::RCP<const OP>
getMatrix
()
const
{
84
if
(problem_.is_null ()) {
85
return
Teuchos::null;
86
}
else
{
87
return
problem_->getOperator ();
88
}
89
}
90
92
void
solve
(MV& X,
const
MV& B) {
93
TEUCHOS_TEST_FOR_EXCEPTION
94
(problem_.is_null () || problem_->getOperator ().is_null (),
95
std::runtime_error,
"Belos::Details::LinearSolver::solve: "
96
"The matrix A in the linear system to solve has not yet been set. "
97
"Please call setMatrix() with a nonnull input before calling solve()."
);
98
Teuchos::RCP<MV> X_ptr = Teuchos::rcpFromRef (X);
99
Teuchos::RCP<const MV> B_ptr = Teuchos::rcpFromRef (B);
100
101
problem_->setLHS (X_ptr);
102
problem_->setRHS (B_ptr);
103
problem_->setProblem ();
104
105
// We can delay creating the Belos solver until the moment when we
106
// actually need it. This aligns with Belos' preference for lazy
107
// initialization.
108
if
(solver_.is_null ()) {
109
Belos::SolverFactory<ScalarType, MV, OP>
factory;
110
solver_ = factory.create (solverName_, params_);
111
solver_->setProblem (problem_);
112
}
113
114
//Belos::ReturnType ret = solver_->solve ();
115
(void) solver_->solve ();
116
}
117
119
void
setParameters
(
const
Teuchos::RCP<Teuchos::ParameterList>& params) {
120
if
(! solver_.is_null () && ! params.is_null ()) {
121
solver_->setParameters (params);
122
}
123
params_ = params;
124
}
125
127
void
symbolic
() {
128
TEUCHOS_TEST_FOR_EXCEPTION
129
(problem_.is_null () || problem_->getOperator ().is_null (),
130
std::runtime_error,
"Belos::Details::LinearSolver::symbolic: "
131
"The matrix A in the linear system to solve has not yet been set. "
132
"Please call setMatrix() with a nonnull input before calling this method."
);
133
134
// Belos solvers can't handle changes to the matrix's domain or
135
// range Maps. It's best in this case to destroy the solver and
136
// start over.
137
solver_ = Teuchos::null;
138
}
139
141
void
numeric
() {
142
TEUCHOS_TEST_FOR_EXCEPTION
143
(problem_.is_null () || problem_->getOperator ().is_null (),
144
std::runtime_error,
"Belos::Details::LinearSolver::numeric: "
145
"The matrix A in the linear system to solve has not yet been set. "
146
"Please call setMatrix() with a nonnull input before calling this method."
);
147
// NOTE (mfh 23 Aug 2015) For the seed or recycling solvers, it
148
// would make sense to do something special here. However, the
149
// line below is always correct. It recomputes the initial
150
// residual vector, which is what Belos expects before a solve if
151
// the matrix or right-hand side may have changed.
152
problem_->setProblem ();
153
}
154
155
private
:
157
std::string solverName_;
159
Teuchos::RCP<problem_type> problem_;
161
Teuchos::RCP<solver_type> solver_;
163
Teuchos::RCP<Teuchos::ParameterList> params_;
164
};
165
166
}
// namespace Details
167
}
// namespace Belos
168
169
#endif
/* BELOS_DETAILS_LINEARSOLVER_HPP */
BelosSolverFactory.hpp
Belos::Details::LinearSolver::setParameters
void setParameters(const Teuchos::RCP< Teuchos::ParameterList > ¶ms)
Set the solver's parameters.
Definition
Belos_Details_LinearSolver.hpp:119
Belos::Details::LinearSolver::symbolic
void symbolic()
Precompute for matrix structure changes.
Definition
Belos_Details_LinearSolver.hpp:127
Belos::Details::LinearSolver::setMatrix
void setMatrix(const Teuchos::RCP< const OP > &A)
Set the solver's matrix.
Definition
Belos_Details_LinearSolver.hpp:74
Belos::Details::LinearSolver::getMatrix
Teuchos::RCP< const OP > getMatrix() const
Get the solver's matrix.
Definition
Belos_Details_LinearSolver.hpp:83
Belos::Details::LinearSolver::LinearSolver
LinearSolver(const std::string &solverName)
Constructor.
Definition
Belos_Details_LinearSolver.hpp:59
Belos::Details::LinearSolver::~LinearSolver
virtual ~LinearSolver()
Destructor (virtual for memory safety).
Definition
Belos_Details_LinearSolver.hpp:68
Belos::Details::LinearSolver::numeric
void numeric()
Precompute for matrix values' changes.
Definition
Belos_Details_LinearSolver.hpp:141
Belos::Details::LinearSolver::solve
void solve(MV &X, const MV &B)
Solve the linear system AX=B for X.
Definition
Belos_Details_LinearSolver.hpp:92
Belos::LinearProblem
Definition
BelosLinearProblem.hpp:50
Belos::SolverManager
The Belos::SolverManager is a templated virtual base class that defines the basic interface that any ...
Definition
BelosSolverManager.hpp:39
Belos::Details
Definition
Belos_Details_EBelosSolverType.cpp:14
Belos
Definition
Belos_Details_EBelosSolverType.cpp:13
Belos::SolverFactory
typename ::Belos::Impl::SolverFactorySelector< SC, MV, OP >::type SolverFactory
Definition
BelosSolverFactory.hpp:281
Generated for Belos by
1.17.0