17#include <dolfinx/common/petsc.h>
18#include <dolfinx/common/types.h>
54 const std::vector<std::span<const PetscScalar>>& x);
72Vec
create_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
73 std::span<const std::int64_t> ghosts,
int bs);
86 std::span<const PetscScalar> x);
120 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
142 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
148 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
152 Vec x,
const std::vector<std::span<const PetscScalar>>& x_b,
154 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
180 std::optional<std::string_view> type = std::nullopt,
181 std::optional<ISLocalToGlobalMapping> rlgmap = std::nullopt,
182 std::optional<ISLocalToGlobalMapping> clgmap = std::nullopt);
222 Vector(Vec x,
bool inc_ref_count);
238 std::int64_t
size()
const;
247 MPI_Comm
comm()
const;
280 static auto set_fn(Mat A, InsertMode mode)
282 return [A, mode, cache = std::vector<PetscInt>()](
285 std::span<const PetscScalar> vals)
mutable ->
int
288#ifdef PETSC_USE_64BIT_INDICES
289 cache.resize(std::ranges::size(rows) + std::ranges::size(cols));
290 std::ranges::copy(rows, cache.begin());
291 std::ranges::copy(cols,
292 std::next(cache.begin(), std::ranges::size(rows)));
293 const PetscInt* _rows = cache.data();
294 const PetscInt* _cols = cache.data() + std::ranges::size(rows);
295 ierr = MatSetValuesLocal(A, std::ranges::size(rows), _rows,
296 std::ranges::size(cols), _cols, vals.data(),
299 ierr = MatSetValuesLocal(A, std::ranges::size(rows),
300 std::ranges::data(rows), std::ranges::size(cols),
301 std::ranges::data(cols), vals.data(), mode);
318 return [A, mode, cache = std::vector<PetscInt>()](
321 std::span<const PetscScalar> vals)
mutable ->
int
324#ifdef PETSC_USE_64BIT_INDICES
325 cache.resize(std::ranges::size(rows) + std::ranges::size(cols));
326 std::ranges::copy(rows, cache.begin());
327 std::ranges::copy(cols,
328 std::next(cache.begin(), std::ranges::size(rows)));
329 const PetscInt* _rows = cache.data();
330 const PetscInt* _cols = cache.data() + std::ranges::size(rows);
331 ierr = MatSetValuesBlockedLocal(A, std::ranges::size(rows), _rows,
332 std::ranges::size(cols), _cols,
335 ierr = MatSetValuesBlockedLocal(
336 A, std::ranges::size(rows), std::ranges::data(rows),
337 std::ranges::size(cols), std::ranges::data(cols), vals.data(), mode);
359 return [A, bs0, bs1, mode, cache0 = std::vector<PetscInt>(),
360 cache1 = std::vector<PetscInt>()](
363 std::span<const PetscScalar> vals)
mutable ->
int
366 const std::int32_t* _rows = std::ranges::data(rows);
367 const std::int32_t* _cols = std::ranges::data(cols);
368 const std::size_t num_rows = std::ranges::size(rows);
369 const std::size_t num_cols = std::ranges::size(cols);
370 cache0.resize(bs0 * num_rows);
371 cache1.resize(bs1 * num_cols);
372 for (std::size_t i = 0; i < num_rows; ++i)
373 for (
int k = 0; k < bs0; ++k)
374 cache0[bs0 * i + k] = bs0 * _rows[i] + k;
376 for (std::size_t i = 0; i < num_cols; ++i)
377 for (
int k = 0; k < bs1; ++k)
378 cache1[bs1 * i + k] = bs1 * _cols[i] + k;
380 ierr = MatSetValuesLocal(A, cache0.size(), cache0.data(), cache1.size(),
381 cache1.data(), vals.data(), mode);
391 std::optional<std::string_view> type = std::nullopt);
400 Matrix(Mat A,
bool inc_ref_count);
419 std::array<std::int64_t, 2>
size()
const;
497 "check the converged reason - positive on convergence, negative on "
498 "divergence")]] KSPConvergedReason
Distribution of a global index range [0, N) across MPI ranks.
Definition IndexMap.h:191
Definition SparsityPattern.h:28
A vector that can be distributed across processes.
Definition Vector.h:53
std::shared_ptr< const common::IndexMap > index_map() const noexcept
Get IndexMap.
Definition Vector.h:465
constexpr int bs() const noexcept
Get block size.
Definition Vector.h:479
container_type & array() noexcept
Get the process-local part of the vector.
Definition Vector.h:484
void set_operator(const Mat A)
Set operator (Mat).
Definition petsc.cpp:672
KSP ksp() const
Return PETSc KSP pointer.
Definition petsc.cpp:746
std::string get_options_prefix() const
Definition petsc.cpp:731
void set_options_prefix(std::string_view options_prefix)
Definition petsc.cpp:722
void set_operators(const Mat A, const Mat P)
Set operator and preconditioner matrix (Mat).
Definition petsc.cpp:674
KrylovSolver(MPI_Comm comm)
Create a Krylov solver.
Definition petsc.cpp:631
void set_from_options() const
Set options from PETSc options database.
Definition petsc.cpp:740
~KrylovSolver()
Destructor.
Definition petsc.cpp:657
KSPConvergedReason solve(Vec x, const Vec b, bool transpose=false)
Solve linear system Ax = b (A^t x = b if transpose is true).
Definition petsc.cpp:681
static auto set_block_fn(Mat A, InsertMode mode)
Return a function with an interface for adding or inserting values into the matrix A using blocked in...
Definition petsc.h:316
~Matrix()
Destructor.
Definition petsc.cpp:562
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition petsc.cpp:624
std::array< std::int64_t, 2 > size() const
Definition petsc.cpp:577
static auto set_fn(Mat A, InsertMode mode)
Return a function with an interface for adding or inserting values into the matrix A (calls MatSetVal...
Definition petsc.h:280
std::string get_options_prefix() const
Definition petsc.cpp:615
Matrix(MPI_Comm comm, const SparsityPattern &sp, std::optional< std::string_view > type=std::nullopt)
Create holder for a PETSc Mat object from a sparsity pattern.
Definition petsc.cpp:535
Vec create_vector(std::size_t dim) const
Initialize vector to be compatible with the matrix-vector product y = Ax. In the parallel case,...
Definition petsc.cpp:585
static auto set_block_expand_fn(Mat A, int bs0, int bs1, InsertMode mode)
Return a function with an interface for adding or inserting blocked values to the matrix A using non-...
Definition petsc.h:357
void set_options_prefix(std::string_view options_prefix)
Definition petsc.cpp:607
Mat mat() const
Return PETSc Mat pointer.
Definition petsc.cpp:605
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition petsc.cpp:526
std::int64_t size() const
Return global size of the vector.
Definition petsc.cpp:475
std::string get_options_prefix() const
Definition petsc.cpp:518
Vector copy() const
Create a copy of the vector.
Definition petsc.cpp:467
void set_options_prefix(std::string_view options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition petsc.cpp:510
~Vector()
Destructor.
Definition petsc.cpp:453
Vector(const common::IndexMap &map, int bs)
Create a vector.
Definition petsc.cpp:430
MPI_Comm comm() const
Return MPI communicator.
Definition petsc.cpp:501
std::array< std::int64_t, 2 > local_range() const
Return ownership range for calling rank.
Definition petsc.cpp:491
std::int32_t local_size() const
Return local size of vector (belonging to the call rank).
Definition petsc.cpp:483
Vec vec() const
Return pointer to PETSc Vec object.
Definition petsc.cpp:532
Concept for a contiguous list of process-local indices, e.g. the row or column indices passed to a ma...
Definition types.h:75
void check(PetscErrorCode ierr, std::string_view petsc_function, std::source_location loc=std::source_location::current())
Throw a std::runtime_error via error() if ierr indicates a PETSc call failed.
Definition petsc.h:41
Miscellaneous classes, functions and types.
Definition dolfinx_common.h:8
PETSc linear algebra functions.
Definition petsc.h:43
Vec create_vector_wrap(const common::IndexMap &map, int bs, std::span< const PetscScalar > x)
Create a PETSc Vec that wraps the data in an array.
Definition petsc.cpp:81
MatNullSpace create_nullspace(MPI_Comm comm, std::span< const Vec > basis)
Create PETSc MatNullSpace. Caller is responsible for destruction returned object.
Definition petsc.cpp:419
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:218
std::vector< IS > create_index_sets(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Index sets addressing a stack of index maps in the calling rank's local numbering.
Definition petsc.cpp:113
std::vector< Vec > create_vectors(MPI_Comm comm, const std::vector< std::span< const PetscScalar > > &x)
Create PETSc vectors from the local data. The data is copied into the PETSc vectors and is not shared...
Definition petsc.cpp:31
Mat create_matrix(MPI_Comm comm, const SparsityPattern &sp, std::optional< std::string_view > type=std::nullopt, std::optional< ISLocalToGlobalMapping > rlgmap=std::nullopt, std::optional< ISLocalToGlobalMapping > clgmap=std::nullopt)
Create a PETSc Mat. Caller is responsible for destroying the returned object.
Definition petsc.cpp:262
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 arrays.
Definition petsc.cpp:172
Vec create_vector(const common::IndexMap &map, int bs)
Create a ghosted PETSc Vec.
Definition petsc.cpp:49
std::vector< IS > create_global_index_sets(const std::vector< std::pair< std::reference_wrapper< const common::IndexMap >, int > > &maps)
Index sets describing each field of a stack of index maps in the global numbering of the stacked prob...
Definition petsc.cpp:133
Linear algebra interface.
Definition dolfinx_la.h:7
dolfinx::la::MatrixCSR< T > transpose(const dolfinx::la::MatrixCSR< T > &A)
Compute the distributed transpose of a MatrixCSR.
Definition mattrans.h:134