14#include <boost/lexical_cast.hpp>
18#include <petscoptions.h>
37void error(
int error_code, std::string filename, std::string petsc_function);
47create_vectors(MPI_Comm comm,
48 const std::vector<std::span<const PetscScalar>>& x);
55Vec create_vector(
const common::IndexMap& map,
int bs);
65Vec create_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
66 std::span<const std::int64_t> ghosts,
int bs);
77Vec create_vector_wrap(
const common::IndexMap& map,
int bs,
78 std::span<const PetscScalar> x);
84Vec create_vector_wrap(
const la::Vector<V>& x)
86 assert(x.index_map());
87 return create_vector_wrap(*x.index_map(), x.bs(), x.array());
102std::vector<IS> create_index_sets(
104 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
107std::vector<std::vector<PetscScalar>> get_local_vectors(
110 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
113void scatter_local_vectors(
114 Vec x,
const std::vector<std::span<const PetscScalar>>& x_b,
116 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
120Mat create_matrix(MPI_Comm comm,
const SparsityPattern& sp,
121 std::string type = std::string());
128MatNullSpace create_nullspace(MPI_Comm comm, std::span<const Vec> basis);
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());
151 petsc::error(ierr, __FILE__,
"PetscOptionsSetValue");
155void clear(std::string option);
173 Vector(
const common::IndexMap& map,
int bs);
176 Vector(
const Vector& x) =
delete;
192 Vector(Vec x,
bool inc_ref_count);
198 Vector& operator=(
const Vector& x) =
delete;
201 Vector& operator=(Vector&& x);
208 std::int64_t
size()
const;
211 std::int32_t local_size()
const;
217 MPI_Comm comm()
const;
220 void set_options_prefix(std::string options_prefix);
224 std::string get_options_prefix()
const;
227 void set_from_options();
243 Operator(Mat A,
bool inc_ref_count);
246 Operator(
const Operator& A) =
delete;
249 Operator(Operator&& A);
255 Operator& operator=(
const Operator& A) =
delete;
258 Operator& operator=(Operator&& A);
262 std::array<std::int64_t, 2>
size()
const;
269 Vec create_vector(std::size_t dim)
const;
284class Matrix :
public Operator
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);
314 petsc::error(ierr, __FILE__,
"MatSetValuesLocal");
325 static auto set_block_fn(Mat A, InsertMode 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");
362 static auto set_block_expand_fn(Mat A,
int bs0,
int bs1, InsertMode mode)
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);
385 petsc::error(ierr, __FILE__,
"MatSetValuesLocal");
392 Matrix(MPI_Comm comm,
const SparsityPattern& sp,
393 std::string type = std::string());
399 Matrix(Mat A,
bool inc_ref_count);
402 Matrix(
const Matrix& A) =
delete;
405 Matrix(Matrix&& A) =
default;
411 Matrix& operator=(
const Matrix& A) =
delete;
414 Matrix& operator=(Matrix&& A) =
default;
419 enum class AssemblyType : std::int32_t
430 void apply(AssemblyType type);
433 double norm(Norm norm_type)
const;
439 void set_options_prefix(std::string options_prefix);
443 std::string get_options_prefix()
const;
446 void set_from_options();
456 explicit KrylovSolver(MPI_Comm comm);
461 KrylovSolver(KSP ksp,
bool inc_ref_count);
464 KrylovSolver(
const KrylovSolver& solver) =
delete;
467 KrylovSolver(KrylovSolver&& solver);
473 KrylovSolver& operator=(
const KrylovSolver&) =
delete;
476 KrylovSolver& operator=(KrylovSolver&& solver);
479 void set_operator(
const Mat A);
482 void set_operators(
const Mat A,
const Mat P);
486 int solve(Vec x,
const Vec b,
bool transpose =
false)
const;
490 void set_options_prefix(std::string options_prefix);
494 std::string get_options_prefix()
const;
497 void set_from_options()
const;
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:91
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)
Definition Vector.h:268