DOLFIN
DOLFIN C++ interface
PETScTAOSolver.h
1 // Copyright (C) 2014 Tianyi Li
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 // First added: 2014-06-22
19 // Last changed: 2014-07-23
20 
21 #ifndef __PETSC_TAO_SOLVER_H
22 #define __PETSC_TAO_SOLVER_H
23 
24 #ifdef HAS_PETSC
25 
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 #include <petsctao.h>
32 #include <petsctaolinesearch.h>
33 
34 #include <dolfin/parameter/Parameters.h>
35 #include <dolfin/la/PETScObject.h>
36 #include <dolfin/la/PETScVector.h>
37 
38 namespace dolfin
39 {
40 
42  class GenericVector;
43  class PETScVector;
44  class PETScMatrix;
45  class OptimisationProblem;
46 
50 
51  class PETScTAOSolver : public PETScObject
52  {
53  public:
54 
56  explicit PETScTAOSolver(MPI_Comm comm,
57  std::string tao_type="default",
58  std::string ksp_type="default",
59  std::string pc_type="default");
60 
62  explicit PETScTAOSolver(std::string tao_type="default",
63  std::string ksp_type="default",
64  std::string pc_type="default");
65 
67  virtual ~PETScTAOSolver();
68 
85  std::pair<std::size_t, bool> solve(OptimisationProblem& optimisation_problem,
86  GenericVector& x,
87  const GenericVector& lb,
88  const GenericVector& ub);
89 
102  std::pair<std::size_t, bool> solve(OptimisationProblem& optimisation_problem,
103  GenericVector& x);
104 
106  MPI_Comm mpi_comm() const;
107 
109  static std::vector<std::pair<std::string, std::string>> methods();
110 
113 
116 
118  Tao tao() const
119  { return _tao; }
120 
124  void init(OptimisationProblem& optimisation_problem, PETScVector& x,
125  const PETScVector& lb, const PETScVector& ub);
126 
130  void init(OptimisationProblem& optimisation_problem, PETScVector& x);
131 
132  private:
133 
150  std::pair<std::size_t, bool> solve(OptimisationProblem& optimisation_problem,
151  PETScVector& x, const PETScVector& lb,
152  const PETScVector& ub);
153 
154  // TAO context for optimisation problems
155  struct tao_ctx_t
156  {
157  OptimisationProblem* optimisation_problem;
158  };
159 
160  struct tao_ctx_t _tao_ctx;
161 
162  // TAO pointer
163  Tao _tao;
164 
165  // Update parameters when tao/ksp/pc_types are explictly given
166  void update_parameters(std::string tao_type,
167  std::string ksp_type,
168  std::string pc_type);
169 
170  // Set options
171  void set_tao_options();
172  void set_ksp_options();
173 
174  // Set the TAO solver type
175  void set_tao(std::string tao_type);
176 
177  // Flag to indicate if the bounds are set
178  bool _has_bounds;
179 
180  // Hessian matrix
181  PETScMatrix _matH;
182 
183  // Hessian preconditioner matrix
184  PETScMatrix _matP;
185 
186  // Available solvers
187  static const std::map<std::string,
188  std::pair<std::string, const TaoType>> _methods;
189 
190  // Compute the nonlinear objective function :math:`f(x)` as well
191  // as its gradient :math:`F(x) = f'(x)`
192  static PetscErrorCode FormFunctionGradient(Tao tao, Vec x, PetscReal *fobj,
193  Vec G, void *ctx);
194 
195  // Compute the hessian :math:`J(x) = f''(x)`
196  static PetscErrorCode FormHessian(Tao tao, Vec x, Mat H, Mat Hpre,
197  void *ctx);
198 
199  // Tao convergence test
200  static PetscErrorCode TaoConvergenceTest(Tao tao, void *ctx);
201  };
202 
203 }
204 
205 #endif
206 #endif
static std::vector< std::pair< std::string, std::string > > methods()
Return a list of available solver methods.
Definition: PETScTAOSolver.cpp:55
Definition: PETScVector.h:60
Definition: adapt.h:29
std::pair< std::size_t, bool > solve(OptimisationProblem &optimisation_problem, GenericVector &x, const GenericVector &lb, const GenericVector &ub)
Definition: PETScTAOSolver.cpp:176
Definition: PETScTAOSolver.h:51
virtual ~PETScTAOSolver()
Destructor.
Definition: PETScTAOSolver.cpp:114
Definition: PETScMatrix.h:58
void init(OptimisationProblem &optimisation_problem, PETScVector &x, const PETScVector &lb, const PETScVector &ub)
Definition: PETScTAOSolver.cpp:210
static Parameters default_parameters()
Default parameter values.
Definition: PETScTAOSolver.cpp:63
Tao tao() const
Return the TAO pointer.
Definition: PETScTAOSolver.h:118
Definition: Parameters.h:94
PETScTAOSolver(MPI_Comm comm, std::string tao_type="default", std::string ksp_type="default", std::string pc_type="default")
Create TAO solver.
Definition: PETScTAOSolver.cpp:88
Definition: PETScObject.h:33
MPI_Comm mpi_comm() const
Return the MPI communicator.
Definition: PETScTAOSolver.cpp:167
This class defines a common interface for vectors.
Definition: GenericVector.h:47
Definition: OptimisationProblem.h:38
Parameters parameters
Parameters for the PETSc TAO solver.
Definition: PETScTAOSolver.h:115