14#include <boost/lexical_cast.hpp> 
   18#include <petscoptions.h> 
   37void error(
int error_code, std::string filename, std::string petsc_function);
 
   48               const std::vector<std::span<const PetscScalar>>& x);
 
   65Vec 
create_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
 
   66                  std::span<const std::int64_t> ghosts, 
int bs);
 
   78                       std::span<const PetscScalar> x);
 
  104        std::pair<std::reference_wrapper<const common::IndexMap>, 
int>>& maps);
 
  110        std::pair<std::reference_wrapper<const common::IndexMap>, 
int>>& maps);
 
  114    Vec x, 
const std::vector<std::span<const PetscScalar>>& x_b,
 
  116        std::pair<std::reference_wrapper<const common::IndexMap>, 
int>>& maps);
 
  121                  std::string type = std::string());
 
  138void set(std::string option);
 
  142void set(std::string option, 
const T value)
 
  144  if (option[0] != 
'-')
 
  145    option = 
'-' + option;
 
  148  ierr = PetscOptionsSetValue(
nullptr, option.c_str(),
 
  149                              boost::lexical_cast<std::string>(value).c_str());
 
 
  155void clear(std::string option);
 
 
  192  Vector(Vec x, 
bool inc_ref_count);
 
  208  std::int64_t 
size() 
const;
 
  217  MPI_Comm 
comm() 
const;
 
 
  243  Operator(Mat A, 
bool inc_ref_count);
 
  262  std::array<std::int64_t, 2> 
size() 
const;
 
 
  291  static auto set_fn(Mat A, InsertMode mode)
 
  293    return [A, mode, cache = std::vector<PetscInt>()](
 
  294               std::span<const std::int32_t> rows,
 
  295               std::span<const std::int32_t> cols,
 
  296               std::span<const PetscScalar> vals) 
mutable -> 
int 
  299#ifdef PETSC_USE_64BIT_INDICES 
  300      cache.resize(rows.size() + cols.size());
 
  301      std::ranges::copy(rows, cache.begin());
 
  302      std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
 
  303      const PetscInt* _rows = cache.data();
 
  304      const PetscInt* _cols = cache.data() + rows.size();
 
  305      ierr = MatSetValuesLocal(A, rows.size(), _rows, cols.size(), _cols,
 
  308      ierr = MatSetValuesLocal(A, rows.size(), rows.data(), cols.size(),
 
  309                               cols.data(), vals.data(), mode);
 
 
  327    return [A, mode, cache = std::vector<PetscInt>()](
 
  328               std::span<const std::int32_t> rows,
 
  329               std::span<const std::int32_t> cols,
 
  330               std::span<const PetscScalar> vals) 
mutable -> 
int 
  333#ifdef PETSC_USE_64BIT_INDICES 
  334      cache.resize(rows.size() + cols.size());
 
  335      std::ranges::copy(rows, cache.begin());
 
  336      std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
 
  337      const PetscInt* _rows = cache.data();
 
  338      const PetscInt* _cols = cache.data() + rows.size();
 
  339      ierr = MatSetValuesBlockedLocal(A, rows.size(), _rows, cols.size(), _cols,
 
  342      ierr = MatSetValuesBlockedLocal(A, rows.size(), rows.data(), cols.size(),
 
  343                                      cols.data(), vals.data(), mode);
 
  348        petsc::error(ierr, __FILE__, 
"MatSetValuesBlockedLocal");
 
 
  364    return [A, bs0, bs1, mode, cache0 = std::vector<PetscInt>(),
 
  365            cache1 = std::vector<PetscInt>()](
 
  366               std::span<const std::int32_t> rows,
 
  367               std::span<const std::int32_t> cols,
 
  368               std::span<const PetscScalar> vals) 
mutable -> 
int 
  371      cache0.resize(bs0 * rows.size());
 
  372      cache1.resize(bs1 * cols.size());
 
  373      for (std::size_t i = 0; i < rows.size(); ++i)
 
  374        for (
int k = 0; k < bs0; ++k)
 
  375          cache0[bs0 * i + k] = bs0 * rows[i] + k;
 
  377      for (std::size_t i = 0; i < cols.size(); ++i)
 
  378        for (
int k = 0; k < bs1; ++k)
 
  379          cache1[bs1 * i + k] = bs1 * cols[i] + k;
 
  381      ierr = MatSetValuesLocal(A, cache0.size(), cache0.data(), cache1.size(),
 
  382                               cache1.data(), vals.data(), mode);
 
 
  393         std::string type = std::string());
 
  399  Matrix(Mat A, 
bool inc_ref_count);
 
 
  486  int solve(Vec x, 
const Vec b, 
bool transpose = 
false) 
const;
 
 
 
Definition SparsityPattern.h:26
constexpr int bs() const
Get block size.
Definition Vector.h:187
std::span< const value_type > array() const
Get local part of the vector (const version)
Definition Vector.h:190
std::shared_ptr< const common::IndexMap > index_map() const
Get IndexMap.
Definition Vector.h:184
void set_operator(const Mat A)
Set operator (Mat)
Definition petsc.cpp:677
KSP ksp() const
Return PETSc KSP pointer.
Definition petsc.cpp:793
std::string get_options_prefix() const
Definition petsc.cpp:775
void set_operators(const Mat A, const Mat P)
Set operator and preconditioner matrix (Mat)
Definition petsc.cpp:679
KrylovSolver(MPI_Comm comm)
Definition petsc.cpp:640
void set_from_options() const
Set options from PETSc options database.
Definition petsc.cpp:785
~KrylovSolver()
Destructor.
Definition petsc.cpp:665
int solve(Vec x, const Vec b, bool transpose=false) const
Definition petsc.cpp:688
void set_options_prefix(std::string options_prefix)
Definition petsc.cpp:766
static auto set_block_fn(Mat A, InsertMode mode)
Definition petsc.h:325
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition petsc.cpp:633
double norm(Norm norm_type) const
Return norm of matrix.
Definition petsc.cpp:577
static auto set_fn(Mat A, InsertMode mode)
Definition petsc.h:291
Matrix(MPI_Comm comm, const SparsityPattern &sp, std::string type=std::string())
Create holder for a PETSc Mat object from a sparsity pattern.
Definition petsc.cpp:565
Matrix(Matrix &&A)=default
Move constructor (falls through to base class move constructor)
std::string get_options_prefix() const
Definition petsc.cpp:625
Matrix & operator=(Matrix &&A)=default
Move assignment operator.
static auto set_block_expand_fn(Mat A, int bs0, int bs1, InsertMode mode)
Definition petsc.h:362
~Matrix()=default
Destructor.
Matrix & operator=(const Matrix &A)=delete
Assignment operator (deleted)
AssemblyType
Definition petsc.h:420
void apply(AssemblyType type)
Definition petsc.cpp:602
void set_options_prefix(std::string options_prefix)
Definition petsc.cpp:619
std::array< std::int64_t, 2 > size() const
Definition petsc.cpp:523
Operator & operator=(const Operator &A)=delete
Assignment operator (deleted)
Operator(Mat A, bool inc_ref_count)
Constructor.
Definition petsc.cpp:498
Vec create_vector(std::size_t dim) const
Definition petsc.cpp:533
Mat mat() const
Return PETSc Mat pointer.
Definition petsc.cpp:562
virtual ~Operator()
Destructor.
Definition petsc.cpp:509
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition petsc.cpp:488
std::int64_t size() const
Return global size of the vector.
Definition petsc.cpp:435
std::string get_options_prefix() const
Definition petsc.cpp:479
Vector copy() const
Definition petsc.cpp:425
virtual ~Vector()
Destructor.
Definition petsc.cpp:413
Vector(const common::IndexMap &map, int bs)
Definition petsc.cpp:398
MPI_Comm comm() const
Return MPI communicator.
Definition petsc.cpp:463
std::array< std::int64_t, 2 > local_range() const
Return ownership range for calling rank.
Definition petsc.cpp:453
std::int32_t local_size() const
Return local size of vector (belonging to the call rank)
Definition petsc.cpp:444
Vec vec() const
Return pointer to PETSc Vec object.
Definition petsc.cpp:495
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition petsc.cpp:472
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
void clear()
Clear PETSc global options database.
Definition petsc.cpp:390
void set(std::string option)
Set PETSc option that takes no value.
Definition petsc.cpp:374
Vec create_vector_wrap(const common::IndexMap &map, int bs, std::span< const PetscScalar > x)
Definition petsc.cpp:102
MatNullSpace create_nullspace(MPI_Comm comm, std::span< const Vec > basis)
Definition petsc.cpp:362
void scatter_local_vectors(Vec x, const std::vector< std::span< const PetscScalar > > &x_b, const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Scatter local vectors to Vec.
Definition petsc.cpp:192
void error(int error_code, std::string filename, std::string petsc_function)
Print error message for PETSc calls that return an error.
Definition petsc.cpp:34
std::vector< IS > create_index_sets(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Compute PETSc IndexSets (IS) for a stack of index maps.
Definition petsc.cpp:129
std::vector< Vec > create_vectors(MPI_Comm comm, const std::vector< std::span< const PetscScalar > > &x)
Definition petsc.cpp:52
Mat create_matrix(MPI_Comm comm, const SparsityPattern &sp, std::string type=std::string())
Definition petsc.cpp:235
std::vector< std::vector< PetscScalar > > get_local_vectors(const Vec x, const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Copy blocks from Vec into local vectors.
Definition petsc.cpp:149
Vec create_vector(const common::IndexMap &map, int bs)
Definition petsc.cpp:68
Linear algebra interface.
Definition sparsitybuild.h:15
Norm
Norm types.
Definition utils.h:17