Loading...
Searching...
No Matches
PETScSNESSolver.h
// Copyright (C) 2012 Patrick E. Farrell
//
// 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/>.
//
// Modified by Corrado Maurini, 2013.
#ifndef __PETSC_SNES_SOLVER_H
#define __PETSC_SNES_SOLVER_H
#ifdef HAS_PETSC
#include <map>
#include <memory>
#include <petscsnes.h>
#include <dolfin/common/MPI.h>
#include <dolfin/la/PETScObject.h>
#include <dolfin/la/PETScMatrix.h>
#include <dolfin/nls/NewtonSolver.h>
#include <dolfin/parameter/Parameters.h>
namespace dolfin
{
class PETScVector;
class PETScSNESSolver : public PETScObject
{
public:
explicit PETScSNESSolver(MPI_Comm comm, std::string nls_type="default");
explicit PETScSNESSolver(std::string nls_type="default");
virtual ~PETScSNESSolver();
std::pair<std::size_t, bool> solve(NonlinearProblem& nonlinear_problem,
GenericVector& x,
const GenericVector& lb,
const GenericVector& ub);
std::pair<std::size_t, bool> solve(NonlinearProblem& nonlinear_function,
GenericVector& x);
void init(NonlinearProblem& nonlinear_problem, GenericVector& x);
void set_from_options() const;
void set_options_prefix(std::string options_prefix);
std::string get_options_prefix() const;
MPI_Comm mpi_comm() const;
static std::vector<std::pair<std::string, std::string>> methods();
static Parameters default_parameters();
Parameters parameters;
SNES snes() const
{ return _snes; }
private:
struct snes_ctx_t
{
// Constructor
snes_ctx_t() : nonlinear_problem(NULL), x(NULL), f_tmp(NULL), xl(NULL),
xu(NULL) {}
// Destructor
~snes_ctx_t()
{
if (f_tmp)
VecDestroy(&f_tmp);
}
NonlinearProblem* nonlinear_problem;
PETScVector* x;
Vec f_tmp;
const PETScVector* xl;
const PETScVector* xu;
};
// PETSc solver pointer
SNES _snes;
// Update the linear solver parameters
void set_linear_solver_parameters();
// Available solvers
static const std::map<std::string,
std::pair<std::string, const SNESType>> _methods;
// The callback for PETSc to compute F, the nonlinear residual
static PetscErrorCode FormFunction(SNES snes, Vec x, Vec f, void* ctx);
// The callback for PETSc to compute A, the Jacobian
static PetscErrorCode FormJacobian(SNES snes, Vec x, Mat A, Mat B,
void* ctx);
static PetscErrorCode FormObjective(SNES snes, Vec x, PetscReal* out,
void* ctx);
// Set the bounds on the problem from the parameters, if desired
// Here, x is passed in as a model vector from which we make our
// Vecs that tell PETSc the bounds if the "sign" parameter is
// used.
void set_bounds(GenericVector& x);
// Check if the problem is a variational inequality
bool is_vi() const;
// Jacobian matrix
PETScMatrix _matJ;
// Jacobian preconditioner matrix
PETScMatrix _matP;
// Upper and lower bounds for bound-constrained solvers
std::shared_ptr<const PETScVector> lb;
std::shared_ptr<const PETScVector> ub;
// Flag to indicate if explicit bounds are set
bool _has_explicit_bounds;
// SNES context
struct snes_ctx_t _snes_ctx;
};
}
#endif
#endif
dolfin::GenericVector
This class defines a common interface for vectors.
Definition GenericVector.h:48
dolfin::NonlinearProblem
Definition NonlinearProblem.h:37
dolfin::PETScObject::PETScObject
PETScObject()
Constructor. Ensures that PETSc has been initialised.
Definition PETScObject.h:38
dolfin::PETScSNESSolver::set_from_options
void set_from_options() const
Set options from the PETSc options database.
Definition PETScSNESSolver.cpp:308
dolfin::PETScSNESSolver::solve
std::pair< std::size_t, bool > solve(NonlinearProblem &nonlinear_problem, GenericVector &x, const GenericVector &lb, const GenericVector &ub)
Definition PETScSNESSolver.cpp:142
dolfin::PETScSNESSolver::parameters
Parameters parameters
Parameters.
Definition PETScSNESSolver.h:120
dolfin::PETScSNESSolver::methods
static std::vector< std::pair< std::string, std::string > > methods()
Return a list of available solver methods.
Definition PETScSNESSolver.cpp:75
dolfin::PETScSNESSolver::snes
SNES snes() const
Return PETSc SNES pointer.
Definition PETScSNESSolver.h:123
dolfin::PETScSNESSolver::get_options_prefix
std::string get_options_prefix() const
Definition PETScSNESSolver.cpp:299
dolfin::PETScSNESSolver::init
void init(NonlinearProblem &nonlinear_problem, GenericVector &x)
Definition PETScSNESSolver.cpp:173
dolfin::PETScSNESSolver::mpi_comm
MPI_Comm mpi_comm() const
Return the MPI communicator.
Definition PETScSNESSolver.cpp:315
dolfin::PETScSNESSolver::~PETScSNESSolver
virtual ~PETScSNESSolver()
Destructor.
Definition PETScSNESSolver.cpp:135
dolfin::PETScSNESSolver::set_options_prefix
void set_options_prefix(std::string options_prefix)
Definition PETScSNESSolver.cpp:291
dolfin::PETScSNESSolver::default_parameters
static Parameters default_parameters()
Default parameter values.
Definition PETScSNESSolver.cpp:83
dolfin::PETScSNESSolver::PETScSNESSolver
PETScSNESSolver(MPI_Comm comm, std::string nls_type="default")
Create SNES solver.
Definition PETScSNESSolver.cpp:98
dolfin::PETScVector
Definition PETScVector.h:61
dolfin::Parameters
Definition Parameters.h:95
dolfin
Definition adapt.h:30