Loading...
Searching...
No Matches
PETScKrylovSolver.h
// Copyright (C) 2004-2015 Johan Jansson and Garth N. Wells
//
// 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 Anders Logg 2005-2012
// Modified by Johan Hoffman 2005
// Modified by Andy R. Terrel 2005
// Modified by Garth N. Wells 2005-2010
#ifndef __DOLFIN_PETSC_KRYLOV_SOLVER_H
#define __DOLFIN_PETSC_KRYLOV_SOLVER_H
#ifdef HAS_PETSC
#include <map>
#include <memory>
#include <string>
#include <petscksp.h>
#include <dolfin/common/types.h>
#include "GenericLinearSolver.h"
#include "PETScObject.h"
namespace dolfin
{
class GenericMatrix;
class GenericVector;
class PETScBaseMatrix;
class PETScMatrix;
class PETScVector;
class PETScPreconditioner;
class PETScSNESSolver;
class VectorSpaceBasis;
class PETScDMCollection;
class PETScKrylovSolver : public GenericLinearSolver, public PETScObject
{
public:
enum class norm_type {none, default_norm, preconditioned, unpreconditioned, natural};
PETScKrylovSolver(MPI_Comm comm,
std::string method="default",
std::string preconditioner="default");
PETScKrylovSolver(std::string method="default",
std::string preconditioner="default");
PETScKrylovSolver(MPI_Comm comm,
std::string method,
std::shared_ptr<PETScPreconditioner> preconditioner);
PETScKrylovSolver(std::string method,
std::shared_ptr<PETScPreconditioner> preconditioner);
explicit PETScKrylovSolver(KSP ksp);
virtual ~PETScKrylovSolver();
void set_operator(std::shared_ptr<const GenericLinearOperator> A);
void set_operator(const PETScBaseMatrix& A);
void set_operators(std::shared_ptr<const GenericLinearOperator> A,
std::shared_ptr<const GenericLinearOperator> P);
void set_operators(const PETScBaseMatrix& A, const PETScBaseMatrix& P);
std::size_t solve(GenericVector& x, const GenericVector& b);
std::size_t solve(GenericVector& x, const GenericVector& b,
bool transpose);
std::size_t solve(PETScVector& x, const PETScVector& b,
bool transpose=false);
std::size_t solve(const GenericLinearOperator& A, GenericVector& x,
const GenericVector& b);
void set_nonzero_guess(bool nonzero_guess);
void set_reuse_preconditioner(bool reuse_pc);
void set_tolerances(double relative, double absolute, double diverged,
int max_iter);
void set_norm_type(norm_type type);
norm_type get_norm_type() const;
void monitor(bool monitor_convergence);
void set_options_prefix(std::string options_prefix);
std::string get_options_prefix() const;
void set_from_options() const;
std::string str(bool verbose) const;
MPI_Comm mpi_comm() const;
KSP ksp() const;
static std::map<std::string, std::string> methods();
static std::map<std::string, std::string> preconditioners();
static Parameters default_parameters();
std::string parameter_type() const
{ return "krylov_solver"; }
void set_dm(DM dm);
void set_dm_active(bool val);
friend class PETScSNESSolver;
friend class PETScTAOSolver;
private:
// Return norm_type enum for norm string
static PETScKrylovSolver::norm_type get_norm_type(std::string norm);
// Solve linear system Ax = b and return number of iterations
std::size_t _solve(const PETScBaseMatrix& A, PETScVector& x,
const PETScVector& b);
// Report the number of iterations
void write_report(int num_iterations, KSPConvergedReason reason);
void check_dimensions(const PETScBaseMatrix& A, const GenericVector& x,
const GenericVector& b) const;
// Available solvers
static const std::map<std::string, const KSPType> _methods;
// Available solvers descriptions
static const std::map<std::string, std::string> _methods_descr;
// PETSc solver pointer
KSP _ksp;
// Preconditioner
std::shared_ptr<PETScPreconditioner> _preconditioner;
bool preconditioner_set;
};
}
#endif
#endif
dolfin::GenericLinearOperator
Definition GenericLinearOperator.h:43
dolfin::GenericLinearSolver
This class provides a general solver for linear systems Ax = b.
Definition GenericLinearSolver.h:38
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::PETScBaseMatrix
Definition PETScBaseMatrix.h:50
dolfin::PETScDMCollection
Definition PETScDMCollection.h:44
dolfin::PETScKrylovSolver::set_dm
void set_dm(DM dm)
Set the DM.
Definition PETScKrylovSolver.cpp:446
dolfin::PETScKrylovSolver::default_parameters
static Parameters default_parameters()
Default parameter values.
Definition PETScKrylovSolver.cpp:80
dolfin::PETScKrylovSolver::methods
static std::map< std::string, std::string > methods()
Return a list of available solver methods.
Definition PETScKrylovSolver.cpp:70
dolfin::PETScKrylovSolver::set_operators
void set_operators(std::shared_ptr< const GenericLinearOperator > A, std::shared_ptr< const GenericLinearOperator > P)
Set operator (matrix) and preconditioner matrix.
Definition PETScKrylovSolver.cpp:200
dolfin::PETScKrylovSolver::solve
std::size_t solve(GenericVector &x, const GenericVector &b)
Solve linear system Ax = b and return number of iterations.
Definition PETScKrylovSolver.cpp:220
dolfin::PETScKrylovSolver::set_operator
void set_operator(std::shared_ptr< const GenericLinearOperator > A)
Set operator (matrix).
Definition PETScKrylovSolver.cpp:190
dolfin::PETScKrylovSolver::set_options_prefix
void set_options_prefix(std::string options_prefix)
Definition PETScKrylovSolver.cpp:519
dolfin::PETScKrylovSolver::set_norm_type
void set_norm_type(norm_type type)
Definition PETScKrylovSolver.cpp:416
dolfin::PETScKrylovSolver::~PETScKrylovSolver
virtual ~PETScKrylovSolver()
Destructor.
Definition PETScKrylovSolver.cpp:181
dolfin::PETScKrylovSolver::ksp
KSP ksp() const
Return PETSc KSP pointer.
Definition PETScKrylovSolver.cpp:568
dolfin::PETScKrylovSolver::get_norm_type
norm_type get_norm_type() const
Get norm type used in convergence testing.
Definition PETScKrylovSolver.cpp:461
dolfin::PETScKrylovSolver::monitor
void monitor(bool monitor_convergence)
Monitor residual at each iteration.
Definition PETScKrylovSolver.cpp:489
dolfin::PETScKrylovSolver::get_options_prefix
std::string get_options_prefix() const
Definition PETScKrylovSolver.cpp:527
dolfin::PETScKrylovSolver::set_nonzero_guess
void set_nonzero_guess(bool nonzero_guess)
Definition PETScKrylovSolver.cpp:391
dolfin::PETScKrylovSolver::set_reuse_preconditioner
void set_reuse_preconditioner(bool reuse_pc)
Definition PETScKrylovSolver.cpp:399
dolfin::PETScKrylovSolver::PETScKrylovSolver
PETScKrylovSolver(MPI_Comm comm, std::string method="default", std::string preconditioner="default")
Definition PETScKrylovSolver.cpp:92
dolfin::PETScKrylovSolver::preconditioners
static std::map< std::string, std::string > preconditioners()
Return a list of available named preconditioners.
Definition PETScKrylovSolver.cpp:75
dolfin::PETScKrylovSolver::set_tolerances
void set_tolerances(double relative, double absolute, double diverged, int max_iter)
Definition PETScKrylovSolver.cpp:407
dolfin::PETScKrylovSolver::set_from_options
void set_from_options() const
Set options from PETSc options database.
Definition PETScKrylovSolver.cpp:536
dolfin::PETScKrylovSolver::str
std::string str(bool verbose) const
Return informal string representation (pretty-print).
Definition PETScKrylovSolver.cpp:543
dolfin::PETScKrylovSolver::set_dm_active
void set_dm_active(bool val)
Activate/deactivate DM.
Definition PETScKrylovSolver.cpp:452
dolfin::PETScKrylovSolver::norm_type
norm_type
Definition PETScKrylovSolver.h:62
dolfin::PETScKrylovSolver::parameter_type
std::string parameter_type() const
Return parameter type: "krylov_solver" or "lu_solver".
Definition PETScKrylovSolver.h:180
dolfin::PETScKrylovSolver::mpi_comm
MPI_Comm mpi_comm() const
Return MPI communicator.
Definition PETScKrylovSolver.cpp:560
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::PETScSNESSolver
Definition PETScSNESSolver.h:46
dolfin::PETScTAOSolver
Definition PETScTAOSolver.h:52
dolfin::PETScVector
Definition PETScVector.h:61
dolfin::Parameters
Definition Parameters.h:95
dolfin::VectorSpaceBasis
Definition VectorSpaceBasis.h:34
dolfin
Definition adapt.h:30
dolfin::norm
double norm(const GenericVector &x, std::string norm_type="l2")
Definition solve.cpp:173