15#include <dolfinx/common/petsc.h>
49 const std::vector<std::span<const PetscScalar>>& x);
67Vec
create_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
68 std::span<const std::int64_t> ghosts,
int bs);
81 std::span<const PetscScalar> x);
107 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
113 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
117 Vec x,
const std::vector<std::span<const PetscScalar>>& x_b,
119 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
129 std::optional<std::string_view> type = std::nullopt);
169 Vector(Vec x,
bool inc_ref_count);
185 std::int64_t
size()
const;
194 MPI_Comm
comm()
const;
227 static auto set_fn(Mat A, InsertMode mode)
229 return [A, mode, cache = std::vector<PetscInt>()](
230 std::span<const std::int32_t> rows,
231 std::span<const std::int32_t> cols,
232 std::span<const PetscScalar> vals)
mutable ->
int
235#ifdef PETSC_USE_64BIT_INDICES
236 cache.resize(rows.size() + cols.size());
237 std::ranges::copy(rows, cache.begin());
238 std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
239 const PetscInt* _rows = cache.data();
240 const PetscInt* _cols = cache.data() + rows.size();
241 ierr = MatSetValuesLocal(A, rows.size(), _rows, cols.size(), _cols,
244 ierr = MatSetValuesLocal(A, rows.size(), rows.data(), cols.size(),
245 cols.data(), vals.data(), mode);
262 return [A, mode, cache = std::vector<PetscInt>()](
263 std::span<const std::int32_t> rows,
264 std::span<const std::int32_t> cols,
265 std::span<const PetscScalar> vals)
mutable ->
int
268#ifdef PETSC_USE_64BIT_INDICES
269 cache.resize(rows.size() + cols.size());
270 std::ranges::copy(rows, cache.begin());
271 std::ranges::copy(cols, std::next(cache.begin(), rows.size()));
272 const PetscInt* _rows = cache.data();
273 const PetscInt* _cols = cache.data() + rows.size();
274 ierr = MatSetValuesBlockedLocal(A, rows.size(), _rows, cols.size(), _cols,
277 ierr = MatSetValuesBlockedLocal(A, rows.size(), rows.data(), cols.size(),
278 cols.data(), vals.data(), mode);
300 return [A, bs0, bs1, mode, cache0 = std::vector<PetscInt>(),
301 cache1 = std::vector<PetscInt>()](
302 std::span<const std::int32_t> rows,
303 std::span<const std::int32_t> cols,
304 std::span<const PetscScalar> vals)
mutable ->
int
307 cache0.resize(bs0 * rows.size());
308 cache1.resize(bs1 * cols.size());
309 for (std::size_t i = 0; i < rows.size(); ++i)
310 for (
int k = 0; k < bs0; ++k)
311 cache0[bs0 * i + k] = bs0 * rows[i] + k;
313 for (std::size_t i = 0; i < cols.size(); ++i)
314 for (
int k = 0; k < bs1; ++k)
315 cache1[bs1 * i + k] = bs1 * cols[i] + k;
317 ierr = MatSetValuesLocal(A, cache0.size(), cache0.data(), cache1.size(),
318 cache1.data(), vals.data(), mode);
328 std::optional<std::string_view> type = std::nullopt);
337 Matrix(Mat A,
bool inc_ref_count);
356 std::array<std::int64_t, 2>
size()
const;
434 "check the converged reason - positive on convergence, negative on "
435 "divergence")]] KSPConvergedReason
Definition SparsityPattern.h:26
A vector that can be distributed across processes.
Definition Vector.h:50
container_type & array()
Get the process-local part of the vector.
Definition Vector.h:390
constexpr int bs() const
Get block size.
Definition Vector.h:385
std::shared_ptr< const common::IndexMap > index_map() const
Get IndexMap.
Definition Vector.h:382
void set_operator(const Mat A)
Set operator (Mat).
Definition petsc.cpp:599
KSP ksp() const
Return PETSc KSP pointer.
Definition petsc.cpp:673
std::string get_options_prefix() const
Definition petsc.cpp:658
void set_options_prefix(std::string_view options_prefix)
Definition petsc.cpp:649
void set_operators(const Mat A, const Mat P)
Set operator and preconditioner matrix (Mat).
Definition petsc.cpp:601
KrylovSolver(MPI_Comm comm)
Create a Krylov solver.
Definition petsc.cpp:560
void set_from_options() const
Set options from PETSc options database.
Definition petsc.cpp:667
~KrylovSolver()
Destructor.
Definition petsc.cpp:584
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:608
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:260
~Matrix()
Destructor.
Definition petsc.cpp:491
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition petsc.cpp:553
std::array< std::int64_t, 2 > size() const
Definition petsc.cpp:506
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:227
std::string get_options_prefix() const
Definition petsc.cpp:544
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:467
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:514
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:298
void set_options_prefix(std::string_view options_prefix)
Definition petsc.cpp:536
Mat mat() const
Return PETSc Mat pointer.
Definition petsc.cpp:534
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition petsc.cpp:458
std::int64_t size() const
Return global size of the vector.
Definition petsc.cpp:407
std::string get_options_prefix() const
Definition petsc.cpp:450
Vector copy() const
Create a copy of the vector.
Definition petsc.cpp:399
void set_options_prefix(std::string_view options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition petsc.cpp:442
virtual ~Vector()
Destructor.
Definition petsc.cpp:385
Vector(const common::IndexMap &map, int bs)
Create a vector.
Definition petsc.cpp:365
MPI_Comm comm() const
Return MPI communicator.
Definition petsc.cpp:433
std::array< std::int64_t, 2 > local_range() const
Return ownership range for calling rank.
Definition petsc.cpp:423
std::int32_t local_size() const
Return local size of vector (belonging to the call rank).
Definition petsc.cpp:415
Vec vec() const
Return pointer to PETSc Vec object.
Definition petsc.cpp:464
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:38
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:80
MatNullSpace create_nullspace(MPI_Comm comm, std::span< const Vec > basis)
Create PETSc MatNullSpace. Caller is responsible for destruction returned object.
Definition petsc.cpp:354
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:178
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:112
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:29
Mat create_matrix(MPI_Comm comm, const SparsityPattern &sp, std::optional< std::string_view > type=std::nullopt)
Create a PETSc Mat. Caller is responsible for destroying the returned object.
Definition petsc.cpp:222
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:132
Vec create_vector(const common::IndexMap &map, int bs)
Create a ghosted PETSc Vec.
Definition petsc.cpp:47
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:128