14#include <boost/lexical_cast.hpp> 
   19#include <petscoptions.h> 
   38void error(
int error_code, 
const std::string& filename,
 
   39           const std::string& petsc_function);
 
   49create_vectors(MPI_Comm comm,
 
   50               const std::vector<std::span<const PetscScalar>>& x);
 
   57Vec create_vector(
const common::IndexMap& map, 
int bs);
 
   67Vec create_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
 
   68                  std::span<const std::int64_t> ghosts, 
int bs);
 
   79Vec create_vector_wrap(
const common::IndexMap& map, 
int bs,
 
   80                       std::span<const PetscScalar> x);
 
   86Vec create_vector_wrap(
const la::Vector<V>& x)
 
   88  assert(x.index_map());
 
   89  return create_vector_wrap(*x.index_map(), x.bs(), x.array());
 
  104std::vector<IS> create_index_sets(
 
  106        std::pair<std::reference_wrapper<const common::IndexMap>, 
int>>& maps);
 
  109std::vector<std::vector<PetscScalar>> get_local_vectors(
 
  112        std::pair<std::reference_wrapper<const common::IndexMap>, 
int>>& maps);
 
  115void scatter_local_vectors(
 
  116    Vec x, 
const std::vector<std::span<const PetscScalar>>& x_b,
 
  118        std::pair<std::reference_wrapper<const common::IndexMap>, 
int>>& maps);
 
  122Mat create_matrix(MPI_Comm comm, 
const SparsityPattern& sp,
 
  123                  std::optional<std::string> type = std::nullopt);
 
  130MatNullSpace create_nullspace(MPI_Comm comm, std::span<const Vec> basis);
 
  140void set(std::string option);
 
  144void set(std::string option, 
const T& value)
 
  146  if (option[0] != 
'-')
 
  147    option = 
'-' + option;
 
  150  ierr = PetscOptionsSetValue(
nullptr, option.c_str(),
 
  151                              boost::lexical_cast<std::string>(value).c_str());
 
  153    petsc::error(ierr, __FILE__, 
"PetscOptionsSetValue");
 
  157void clear(std::string option);
 
  175  Vector(
const common::IndexMap& map, 
int bs);
 
  178  Vector(
const Vector& x) = 
delete;
 
  181  Vector(Vector&& x) 
noexcept;
 
  194  Vector(Vec x, 
bool inc_ref_count);
 
  200  Vector& operator=(
const Vector& x) = 
delete;
 
  203  Vector& operator=(Vector&& x) 
noexcept;
 
  210  std::int64_t 
size() 
const;
 
  213  std::int32_t local_size() 
const;
 
  219  MPI_Comm comm() 
const;
 
  222  void set_options_prefix(
const std::string& options_prefix);
 
  226  std::string get_options_prefix() 
const;
 
  229  void set_from_options();
 
  245  Operator(Mat A, 
bool inc_ref_count);
 
  248  Operator(
const Operator& A) = 
delete;
 
  251  Operator(Operator&& A) 
noexcept;
 
  257  Operator& operator=(
const Operator& A) = 
delete;
 
  260  Operator& operator=(Operator&& A) 
noexcept;
 
  264  std::array<std::int64_t, 2> 
size() 
const;
 
  271  Vec create_vector(std::size_t dim) 
const;
 
  286class Matrix : 
public Operator
 
  293  static auto set_fn(Mat A, InsertMode mode)
 
  295    return [A, mode, cache = std::vector<PetscInt>()](
 
  296               std::span<const std::int32_t> rows,
 
  297               std::span<const std::int32_t> cols,
 
  298               std::span<const PetscScalar> vals) 
mutable -> 
int 
  301#ifdef PETSC_USE_64BIT_INDICES 
  302      cache.resize(rows.size() + cols.size());
 
  303      std::ranges::copy(rows, cache.begin());
 
  304      std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
 
  305      const PetscInt* _rows = cache.data();
 
  306      const PetscInt* _cols = cache.data() + rows.size();
 
  307      ierr = MatSetValuesLocal(A, rows.size(), _rows, cols.size(), _cols,
 
  310      ierr = MatSetValuesLocal(A, rows.size(), rows.data(), cols.size(),
 
  311                               cols.data(), vals.data(), mode);
 
  316        petsc::error(ierr, __FILE__, 
"MatSetValuesLocal");
 
  327  static auto set_block_fn(Mat A, InsertMode mode)
 
  329    return [A, mode, cache = std::vector<PetscInt>()](
 
  330               std::span<const std::int32_t> rows,
 
  331               std::span<const std::int32_t> cols,
 
  332               std::span<const PetscScalar> vals) 
mutable -> 
int 
  335#ifdef PETSC_USE_64BIT_INDICES 
  336      cache.resize(rows.size() + cols.size());
 
  337      std::ranges::copy(rows, cache.begin());
 
  338      std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
 
  339      const PetscInt* _rows = cache.data();
 
  340      const PetscInt* _cols = cache.data() + rows.size();
 
  341      ierr = MatSetValuesBlockedLocal(A, rows.size(), _rows, cols.size(), _cols,
 
  344      ierr = MatSetValuesBlockedLocal(A, rows.size(), rows.data(), cols.size(),
 
  345                                      cols.data(), vals.data(), mode);
 
  350        petsc::error(ierr, __FILE__, 
"MatSetValuesBlockedLocal");
 
  364  static auto set_block_expand_fn(Mat A, 
int bs0, 
int bs1, InsertMode mode)
 
  366    return [A, bs0, bs1, mode, cache0 = std::vector<PetscInt>(),
 
  367            cache1 = std::vector<PetscInt>()](
 
  368               std::span<const std::int32_t> rows,
 
  369               std::span<const std::int32_t> cols,
 
  370               std::span<const PetscScalar> vals) 
mutable -> 
int 
  373      cache0.resize(bs0 * rows.size());
 
  374      cache1.resize(bs1 * cols.size());
 
  375      for (std::size_t i = 0; i < rows.size(); ++i)
 
  376        for (
int k = 0; k < bs0; ++k)
 
  377          cache0[bs0 * i + k] = bs0 * rows[i] + k;
 
  379      for (std::size_t i = 0; i < cols.size(); ++i)
 
  380        for (
int k = 0; k < bs1; ++k)
 
  381          cache1[bs1 * i + k] = bs1 * cols[i] + k;
 
  383      ierr = MatSetValuesLocal(A, cache0.size(), cache0.data(), cache1.size(),
 
  384                               cache1.data(), vals.data(), mode);
 
  387        petsc::error(ierr, __FILE__, 
"MatSetValuesLocal");
 
  394  Matrix(MPI_Comm comm, 
const SparsityPattern& sp,
 
  395         std::optional<std::string> type = std::nullopt);
 
  401  Matrix(Mat A, 
bool inc_ref_count);
 
  404  Matrix(
const Matrix& A) = 
delete;
 
  407  Matrix(Matrix&& A) = 
default;
 
  413  Matrix& operator=(
const Matrix& A) = 
delete;
 
  416  Matrix& operator=(Matrix&& A) = 
default;
 
  421  enum class AssemblyType : std::int8_t
 
  432  void apply(AssemblyType type);
 
  435  double norm(Norm norm_type) 
const;
 
  441  void set_options_prefix(
const std::string& options_prefix);
 
  445  std::string get_options_prefix() 
const;
 
  448  void set_from_options();
 
  458  explicit KrylovSolver(MPI_Comm comm);
 
  463  KrylovSolver(KSP ksp, 
bool inc_ref_count);
 
  466  KrylovSolver(
const KrylovSolver& solver) = 
delete;
 
  469  KrylovSolver(KrylovSolver&& solver) 
noexcept;
 
  475  KrylovSolver& operator=(
const KrylovSolver&) = 
delete;
 
  478  KrylovSolver& operator=(KrylovSolver&& solver) 
noexcept;
 
  481  void set_operator(
const Mat A);
 
  484  void set_operators(
const Mat A, 
const Mat P);
 
  488  int solve(Vec x, 
const Vec b, 
bool transpose = 
false) 
const;
 
  492  void set_options_prefix(
const std::string& options_prefix);
 
  496  std::string get_options_prefix() 
const;
 
  499  void set_from_options() 
const;
 
Definition SparsityPattern.h:26
 
int size(MPI_Comm comm)
Definition MPI.cpp:72
 
constexpr std::array< std::int64_t, 2 > local_range(int rank, std::int64_t N, int size)
Return local range for the calling process, partitioning the global [0, N - 1] range across all ranks...
Definition MPI.h:89
 
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
 
Linear algebra interface.
Definition sparsitybuild.h:15
 
auto norm(const V &x, Norm type=Norm::l2)
Compute the norm of the vector.
Definition Vector.h:477