Loading...
Searching...
No Matches
TAOLinearBoundSolver.h
// Copyright (C) 2012 Corrado Maurini
//
// 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/>.
#ifndef _TAOLinearBoundSolver_H
#define _TAOLinearBoundSolver_H
#ifdef HAS_PETSC
#include <map>
#include <memory>
#include <petscksp.h>
#include <petscpc.h>
#include <petsctao.h>
#include <dolfin/common/NoDeleter.h>
#include <dolfin/common/types.h>
#include <dolfin/la/PETScObject.h>
#include <dolfin/la/KrylovSolver.h>
namespace dolfin
{
// Forward declarations
class GenericMatrix;
class GenericVector;
class PETScMatrix;
class PETScVector;
class PETScPreconditioner;
class PETScKrylovSolver;
class PETScKSPDeleter;
class TAOLinearBoundSolver : public Variable, public PETScObject
{
public:
explicit TAOLinearBoundSolver(MPI_Comm comm);
TAOLinearBoundSolver(const std::string method = "default",
const std::string ksp_type = "default",
const std::string pc_type = "default");
~TAOLinearBoundSolver();
std::size_t solve(const GenericMatrix& A, GenericVector& x,
const GenericVector& b, const GenericVector& xl,
const GenericVector& xu);
std::size_t solve(const PETScMatrix& A, PETScVector& x,
const PETScVector& b,
const PETScVector& xl, const PETScVector& xu);
void set_solver(const std::string&);
void set_ksp(const std::string ksp_type = "default");
Tao tao() const;
static std::map<std::string, std::string> methods();
static std::map<std::string, std::string> krylov_solvers();
static std::map<std::string, std::string> preconditioners();
static Parameters default_parameters()
{
Parameters p("tao_solver");
p.add("monitor_convergence" , false);
p.add("report" , false);
p.add("gradient_absolute_tol" , 1.0e-8);
p.add("gradient_relative_tol" , 1.0e-8);
p.add("gradient_t_tol" , 0.0);
p.add("error_on_nonconvergence", true);
p.add("maximum_iterations" , 100);
p.add("options_prefix" , "default");
Parameters ksp("krylov_solver");
ksp = KrylovSolver::default_parameters();
p.add(ksp);
return p;
}
std::shared_ptr<const PETScMatrix> get_matrix() const;
std::shared_ptr<const PETScVector> get_vector() const;
private:
// Set operators with GenericMatrix and GenericVector
void set_operators(std::shared_ptr<const GenericMatrix> A,
std::shared_ptr<const GenericVector> b);
// Set operators with shared pointer to PETSc objects
void set_operators(std::shared_ptr<const PETScMatrix> A,
std::shared_ptr<const PETScVector> b);
// Callback for changes in parameter values
void read_parameters();
// Available ksp solvers
static const std::map<std::string, const KSPType> _ksp_methods;
// Available tao solvers descriptions
static const std::map<std::string, std::string> _methods_descr;
// Set options
void set_ksp_options();
// Initialize TAO solver
void init(const std::string& method);
// Tao solver pointer
Tao _tao;
// Petsc preconditioner
std::shared_ptr<PETScPreconditioner> _preconditioner;
// Operator (the matrix) and the vector
std::shared_ptr<const PETScMatrix> _matA;
std::shared_ptr<const PETScVector> _b;
bool _preconditioner_set;
// Computes the value of the objective function and its gradient.
static PetscErrorCode
__TAOFormFunctionGradientQuadraticProblem(Tao tao, Vec X,
PetscReal *ener, Vec G,
void *ptr);
// Computes the hessian of the quadratic objective function
static PetscErrorCode
__TAOFormHessianQuadraticProblem(Tao tao,Vec X, Mat H, Mat Hpre,
void *ptr);
//-------------------------------------------------------------------------
// Monitor the state of the solution at each iteration. The
// output printed to the screen is:
//
// iterate - the current iterate number (>=0)
// f - the current function value
// gnorm - the square of the gradient norm, duality gap, or other
// measure
// indicating distance from optimality.
// cnorm - the infeasibility of the current solution with regard
// to the constraints.
// xdiff - the step length or trust region radius of the most
// recent iterate.
//-------------------------------------------------------------------------
static PetscErrorCode __TAOMonitor(Tao tao, void *ctx);
};
}
#endif
#endif
dolfin::GenericMatrix
This class defines a common interface for matrices.
Definition GenericMatrix.h:47
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::KrylovSolver::default_parameters
static Parameters default_parameters()
Default parameter values.
Definition KrylovSolver.cpp:32
dolfin::PETScKrylovSolver
Definition PETScKrylovSolver.h:55
dolfin::PETScMatrix
Definition PETScMatrix.h:59
dolfin::PETScObject::PETScObject
PETScObject()
Constructor. Ensures that PETSc has been initialised.
Definition PETScObject.h:38
dolfin::PETScPreconditioner
Definition PETScPreconditioner.h:49
dolfin::PETScVector
Definition PETScVector.h:61
dolfin::Parameters
Definition Parameters.h:95
dolfin::Parameters::add
void add(std::string key)
Definition Parameters.h:128
dolfin::TAOLinearBoundSolver::set_ksp
void set_ksp(const std::string ksp_type="default")
Set PETSC Krylov Solver (ksp) used by TAO.
Definition TAOLinearBoundSolver.cpp:317
dolfin::TAOLinearBoundSolver::krylov_solvers
static std::map< std::string, std::string > krylov_solvers()
Return a list of available krylov solvers.
Definition TAOLinearBoundSolver.cpp:69
dolfin::TAOLinearBoundSolver::get_vector
std::shared_ptr< const PETScVector > get_vector() const
Return load vector shared pointer.
Definition TAOLinearBoundSolver.cpp:352
dolfin::TAOLinearBoundSolver::default_parameters
static Parameters default_parameters()
Default parameter values.
Definition TAOLinearBoundSolver.h:122
dolfin::TAOLinearBoundSolver::preconditioners
static std::map< std::string, std::string > preconditioners()
Return a list of available preconditioners.
Definition TAOLinearBoundSolver.cpp:74
dolfin::TAOLinearBoundSolver::tao
Tao tao() const
Return TAO solver pointer.
Definition TAOLinearBoundSolver.cpp:342
dolfin::TAOLinearBoundSolver::solve
std::size_t solve(const GenericMatrix &A, GenericVector &x, const GenericVector &b, const GenericVector &xl, const GenericVector &xu)
Definition TAOLinearBoundSolver.cpp:142
dolfin::TAOLinearBoundSolver::TAOLinearBoundSolver
TAOLinearBoundSolver(MPI_Comm comm)
Create TAO bound constrained solver.
Definition TAOLinearBoundSolver.cpp:79
dolfin::TAOLinearBoundSolver::get_matrix
std::shared_ptr< const PETScMatrix > get_matrix() const
Return Matrix shared pointer.
Definition TAOLinearBoundSolver.cpp:347
dolfin::TAOLinearBoundSolver::methods
static std::map< std::string, std::string > methods()
Return a list of available Tao solver methods.
Definition TAOLinearBoundSolver.cpp:64
dolfin::TAOLinearBoundSolver::set_solver
void set_solver(const std::string &)
Set the TAO solver type.
Definition TAOLinearBoundSolver.cpp:287
dolfin::TAOLinearBoundSolver::~TAOLinearBoundSolver
~TAOLinearBoundSolver()
Destructor.
Definition TAOLinearBoundSolver.cpp:119
dolfin::Variable::Variable
Variable()
Create unnamed variable.
Definition Variable.cpp:31
dolfin
Definition adapt.h:30
dolfin::init
void init(int argc, char *argv[])
Definition init.cpp:27