Loading...
Searching...
No Matches
PETScTAOSolver.h
// Copyright (C) 2014 Tianyi Li
//
// This file is part of DOLFIN.
//
// DOLFIN is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DOLFIN is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
//
// First added: 2014-06-22
// Last changed: 2014-07-23
#ifndef __PETSC_TAO_SOLVER_H
#define __PETSC_TAO_SOLVER_H
#ifdef HAS_PETSC
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <petsctao.h>
#include <petsctaolinesearch.h>
#include <dolfin/parameter/Parameters.h>
#include <dolfin/la/PETScObject.h>
#include <dolfin/la/PETScVector.h>
namespace dolfin
{
class GenericVector;
class PETScVector;
class PETScMatrix;
class OptimisationProblem;
class PETScTAOSolver : public PETScObject
{
public:
explicit PETScTAOSolver(MPI_Comm comm,
std::string tao_type="default",
std::string ksp_type="default",
std::string pc_type="default");
explicit PETScTAOSolver(std::string tao_type="default",
std::string ksp_type="default",
std::string pc_type="default");
virtual ~PETScTAOSolver();
std::pair<std::size_t, bool> solve(OptimisationProblem& optimisation_problem,
GenericVector& x,
const GenericVector& lb,
const GenericVector& ub);
std::pair<std::size_t, bool> solve(OptimisationProblem& optimisation_problem,
GenericVector& x);
MPI_Comm mpi_comm() const;
static std::vector<std::pair<std::string, std::string>> methods();
static Parameters default_parameters();
Parameters parameters;
Tao tao() const
{ return _tao; }
void init(OptimisationProblem& optimisation_problem, PETScVector& x,
const PETScVector& lb, const PETScVector& ub);
void init(OptimisationProblem& optimisation_problem, PETScVector& x);
private:
std::pair<std::size_t, bool> solve(OptimisationProblem& optimisation_problem,
PETScVector& x, const PETScVector& lb,
const PETScVector& ub);
// TAO context for optimisation problems
struct tao_ctx_t
{
OptimisationProblem* optimisation_problem;
};
struct tao_ctx_t _tao_ctx;
// TAO pointer
Tao _tao;
// Update parameters when tao/ksp/pc_types are explictly given
void update_parameters(std::string tao_type,
std::string ksp_type,
std::string pc_type);
// Set options
void set_tao_options();
void set_ksp_options();
// Set the TAO solver type
void set_tao(std::string tao_type);
// Flag to indicate if the bounds are set
bool _has_bounds;
// Hessian matrix
PETScMatrix _matH;
// Hessian preconditioner matrix
PETScMatrix _matP;
// Available solvers
static const std::map<std::string,
std::pair<std::string, const TaoType>> _methods;
// Compute the nonlinear objective function :math:`f(x)` as well
// as its gradient :math:`F(x) = f'(x)`
static PetscErrorCode FormFunctionGradient(Tao tao, Vec x, PetscReal *fobj,
Vec G, void *ctx);
// Compute the hessian :math:`J(x) = f''(x)`
static PetscErrorCode FormHessian(Tao tao, Vec x, Mat H, Mat Hpre,
void *ctx);
// Tao convergence test
static PetscErrorCode TaoConvergenceTest(Tao tao, void *ctx);
};
}
#endif
#endif
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::OptimisationProblem
Definition OptimisationProblem.h:39
dolfin::PETScMatrix
Definition PETScMatrix.h:59
dolfin::PETScObject::PETScObject
PETScObject()
Constructor. Ensures that PETSc has been initialised.
Definition PETScObject.h:38
dolfin::PETScTAOSolver::mpi_comm
MPI_Comm mpi_comm() const
Return the MPI communicator.
Definition PETScTAOSolver.cpp:172
dolfin::PETScTAOSolver::PETScTAOSolver
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
dolfin::PETScTAOSolver::solve
std::pair< std::size_t, bool > solve(OptimisationProblem &optimisation_problem, GenericVector &x, const GenericVector &lb, const GenericVector &ub)
Definition PETScTAOSolver.cpp:181
dolfin::PETScTAOSolver::methods
static std::vector< std::pair< std::string, std::string > > methods()
Return a list of available solver methods.
Definition PETScTAOSolver.cpp:55
dolfin::PETScTAOSolver::default_parameters
static Parameters default_parameters()
Default parameter values.
Definition PETScTAOSolver.cpp:63
dolfin::PETScTAOSolver::tao
Tao tao() const
Return the TAO pointer.
Definition PETScTAOSolver.h:118
dolfin::PETScTAOSolver::parameters
Parameters parameters
Parameters for the PETSc TAO solver.
Definition PETScTAOSolver.h:115
dolfin::PETScTAOSolver::init
void init(OptimisationProblem &optimisation_problem, PETScVector &x, const PETScVector &lb, const PETScVector &ub)
Definition PETScTAOSolver.cpp:215
dolfin::PETScTAOSolver::~PETScTAOSolver
virtual ~PETScTAOSolver()
Destructor.
Definition PETScTAOSolver.cpp:119
dolfin::PETScVector
Definition PETScVector.h:61
dolfin::Parameters
Definition Parameters.h:95
dolfin
Definition adapt.h:30