DOLFIN
DOLFIN C++ interface
Loading...
Searching...
No Matches
NewtonSolver.h
1// Copyright (C) 2005-2008 Garth N. Wells
2//
3// This file is part of DOLFIN.
4//
5// DOLFIN is free software: you can redistribute it and/or modify
6// it under the terms of the GNU Lesser General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// DOLFIN is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public License
16// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17//
18// Modified by Anders Logg 2006-2011
19// Modified by Anders E. Johansen 2011
20//
21// First added: 2005-10-23
22// Last changed: 2013-11-20
23
24#ifndef __NEWTON_SOLVER_H
25#define __NEWTON_SOLVER_H
26
27#include <utility>
28#include <memory>
29#include <dolfin/common/MPI.h>
30#include <dolfin/common/Variable.h>
31
32namespace dolfin
33{
34
35 // Forward declarations
38 class GenericMatrix;
39 class GenericVector;
40 class NonlinearProblem;
41
44
45 class NewtonSolver : public Variable
46 {
47 public:
48
50 explicit NewtonSolver(MPI_Comm comm=MPI_COMM_WORLD);
51
61 NewtonSolver(MPI_Comm comm, std::shared_ptr<GenericLinearSolver> solver,
63
65 virtual ~NewtonSolver();
66
80 std::pair<std::size_t, bool> solve(NonlinearProblem& nonlinear_function,
81 GenericVector& x);
82
88 std::size_t iteration() const;
89
96 std::size_t krylov_iterations() const;
97
103 double residual() const;
104
110 double residual0() const;
111
117 double relative_residual() const;
118
125
132
140 void set_relaxation_parameter(double relaxation_parameter)
141 { _relaxation_parameter = relaxation_parameter; }
142
149 { return _relaxation_parameter; }
150
151 protected:
152
167 virtual bool converged(const GenericVector& r,
168 const NonlinearProblem& nonlinear_problem,
169 std::size_t iteration);
170
186 virtual void solver_setup(std::shared_ptr<const GenericMatrix> A,
187 std::shared_ptr<const GenericMatrix> P,
188 const NonlinearProblem& nonlinear_problem,
189 std::size_t iteration);
190
207 virtual void update_solution(GenericVector& x,
208 const GenericVector& dx,
209 double relaxation_parameter,
210 const NonlinearProblem& nonlinear_problem,
211 std::size_t iteration);
212
213 private:
214
215 // Current number of Newton iterations
216 std::size_t _newton_iteration;
217
218 // Accumulated number of Krylov iterations since solve began
219 std::size_t _krylov_iterations;
220
221 // Relaxation parameter
222 double _relaxation_parameter;
223
224 // Most recent residual and initial residual
225 double _residual, _residual0;
226
227 // Solver
228 std::shared_ptr<GenericLinearSolver> _solver;
229
230 // Jacobian matrix
231 std::shared_ptr<GenericMatrix> _matA;
232
233 // Preconditioner matrix
234 std::shared_ptr<GenericMatrix> _matP;
235
236 // Solution vector
237 std::shared_ptr<GenericVector> _dx;
238
239 // Residual vector
240 std::shared_ptr<GenericVector> _b;
241
242 // MPI communicator
243 dolfin::MPI::Comm _mpi_comm;
244
245 };
246
247}
248
249#endif
Base class for LinearAlgebra factories.
Definition GenericLinearAlgebraFactory.h:47
This class provides a general solver for linear systems Ax = b.
Definition GenericLinearSolver.h:38
This class defines a common interface for matrices.
Definition GenericMatrix.h:47
This class defines a common interface for vectors.
Definition GenericVector.h:48
Definition MPI.h:77
double residual0() const
Definition NewtonSolver.cpp:247
double residual() const
Definition NewtonSolver.cpp:242
std::pair< std::size_t, bool > solve(NonlinearProblem &nonlinear_function, GenericVector &x)
Definition NewtonSolver.cpp:99
virtual bool converged(const GenericVector &r, const NonlinearProblem &nonlinear_problem, std::size_t iteration)
Definition NewtonSolver.cpp:271
double relative_residual() const
Definition NewtonSolver.cpp:252
virtual ~NewtonSolver()
Destructor.
Definition NewtonSolver.cpp:93
NewtonSolver(MPI_Comm comm=MPI_COMM_WORLD)
Create nonlinear solver.
Definition NewtonSolver.cpp:87
void set_relaxation_parameter(double relaxation_parameter)
Definition NewtonSolver.h:140
std::size_t krylov_iterations() const
Definition NewtonSolver.cpp:237
virtual void solver_setup(std::shared_ptr< const GenericMatrix > A, std::shared_ptr< const GenericMatrix > P, const NonlinearProblem &nonlinear_problem, std::size_t iteration)
Definition NewtonSolver.cpp:302
std::size_t iteration() const
Definition NewtonSolver.cpp:232
GenericLinearSolver & linear_solver() const
Definition NewtonSolver.cpp:257
virtual void update_solution(GenericVector &x, const GenericVector &dx, double relaxation_parameter, const NonlinearProblem &nonlinear_problem, std::size_t iteration)
Definition NewtonSolver.cpp:319
double get_relaxation_parameter()
Definition NewtonSolver.h:148
static Parameters default_parameters()
Definition NewtonSolver.cpp:45
Definition NonlinearProblem.h:37
Definition Parameters.h:95
Variable()
Create unnamed variable.
Definition Variable.cpp:31
Definition adapt.h:30