DOLFIN
DOLFIN C++ interface
PETScSNESSolver.h
1 // Copyright (C) 2012 Patrick E. Farrell
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 Corrado Maurini, 2013.
19 
20 #ifndef __PETSC_SNES_SOLVER_H
21 #define __PETSC_SNES_SOLVER_H
22 
23 #ifdef HAS_PETSC
24 
25 #include <map>
26 #include <memory>
27 #include <petscsnes.h>
28 
29 #include <dolfin/common/MPI.h>
30 #include <dolfin/la/PETScObject.h>
31 #include <dolfin/la/PETScMatrix.h>
32 #include <dolfin/nls/NewtonSolver.h>
33 #include <dolfin/parameter/Parameters.h>
34 
35 namespace dolfin
36 {
37 
39  class PETScVector;
40 
46  {
47  public:
48 
50  explicit PETScSNESSolver(MPI_Comm comm, std::string nls_type="default");
51 
53  explicit PETScSNESSolver(std::string nls_type="default");
54 
56  virtual ~PETScSNESSolver();
57 
74  std::pair<std::size_t, bool> solve(NonlinearProblem& nonlinear_problem,
75  GenericVector& x,
76  const GenericVector& lb,
77  const GenericVector& ub);
78 
92  std::pair<std::size_t, bool> solve(NonlinearProblem& nonlinear_function,
93  GenericVector& x);
94 
97  void init(NonlinearProblem& nonlinear_problem, GenericVector& x);
98 
100  void set_from_options() const;
101 
104  void set_options_prefix(std::string options_prefix);
105 
108  std::string get_options_prefix() const;
109 
111  MPI_Comm mpi_comm() const;
112 
114  static std::vector<std::pair<std::string, std::string>> methods();
115 
118 
121 
123  SNES snes() const
124  { return _snes; }
125 
126  private:
127 
128  struct snes_ctx_t
129  {
130  // Constructor
131  snes_ctx_t() : nonlinear_problem(NULL), x(NULL), f_tmp(NULL), xl(NULL),
132  xu(NULL) {}
133 
134  // Destructor
135  ~snes_ctx_t()
136  {
137  if (f_tmp)
138  VecDestroy(&f_tmp);
139  }
140 
141  NonlinearProblem* nonlinear_problem;
142  PETScVector* x;
143  Vec f_tmp;
144  const PETScVector* xl;
145  const PETScVector* xu;
146  };
147 
148  // PETSc solver pointer
149  SNES _snes;
150 
151  // Update the linear solver parameters
152  void set_linear_solver_parameters();
153 
154  // Available solvers
155  static const std::map<std::string,
156  std::pair<std::string, const SNESType>> _methods;
157 
158  // The callback for PETSc to compute F, the nonlinear residual
159  static PetscErrorCode FormFunction(SNES snes, Vec x, Vec f, void* ctx);
160 
161  // The callback for PETSc to compute A, the Jacobian
162  static PetscErrorCode FormJacobian(SNES snes, Vec x, Mat A, Mat B,
163  void* ctx);
164 
165  static PetscErrorCode FormObjective(SNES snes, Vec x, PetscReal* out,
166  void* ctx);
167 
168  // Set the bounds on the problem from the parameters, if desired
169  // Here, x is passed in as a model vector from which we make our
170  // Vecs that tell PETSc the bounds if the "sign" parameter is
171  // used.
172  void set_bounds(GenericVector& x);
173 
174  // Check if the problem is a variational inequality
175  bool is_vi() const;
176 
177  // Jacobian matrix
178  PETScMatrix _matJ;
179 
180  // Jacobian preconditioner matrix
181  PETScMatrix _matP;
182 
183  // Upper and lower bounds for bound-constrained solvers
184  std::shared_ptr<const PETScVector> lb;
185  std::shared_ptr<const PETScVector> ub;
186 
187  // Flag to indicate if explicit bounds are set
188  bool _has_explicit_bounds;
189 
190  // SNES context
191  struct snes_ctx_t _snes_ctx;
192  };
193 
194 }
195 
196 #endif
197 
198 #endif
Definition: PETScVector.h:60
void set_options_prefix(std::string options_prefix)
Definition: PETScSNESSolver.cpp:291
Definition: adapt.h:29
void set_from_options() const
Set options from the PETSc options database.
Definition: PETScSNESSolver.cpp:308
MPI_Comm mpi_comm() const
Return the MPI communicator.
Definition: PETScSNESSolver.cpp:315
std::pair< std::size_t, bool > solve(NonlinearProblem &nonlinear_problem, GenericVector &x, const GenericVector &lb, const GenericVector &ub)
Definition: PETScSNESSolver.cpp:142
Definition: PETScMatrix.h:58
Definition: NonlinearProblem.h:36
virtual ~PETScSNESSolver()
Destructor.
Definition: PETScSNESSolver.cpp:135
std::string get_options_prefix() const
Definition: PETScSNESSolver.cpp:299
PETScSNESSolver(MPI_Comm comm, std::string nls_type="default")
Create SNES solver.
Definition: PETScSNESSolver.cpp:98
Definition: Parameters.h:94
Definition: PETScSNESSolver.h:45
Parameters parameters
Parameters.
Definition: PETScSNESSolver.h:120
static std::vector< std::pair< std::string, std::string > > methods()
Return a list of available solver methods.
Definition: PETScSNESSolver.cpp:75
SNES snes() const
Return PETSc SNES pointer.
Definition: PETScSNESSolver.h:123
void init(NonlinearProblem &nonlinear_problem, GenericVector &x)
Definition: PETScSNESSolver.cpp:173
Definition: PETScObject.h:33
static Parameters default_parameters()
Default parameter values.
Definition: PETScSNESSolver.cpp:83
This class defines a common interface for vectors.
Definition: GenericVector.h:47