12 #include <boost/lexical_cast.hpp>
16 #include <petscoptions.h>
29 class SparsityPattern;
34 void error(
int error_code, std::string filename, std::string petsc_function);
44 create_vectors(MPI_Comm comm,
45 const std::vector<std::span<const PetscScalar>>& x);
52 Vec create_vector(
const common::IndexMap& map,
int bs);
62 Vec create_vector(MPI_Comm comm, std::array<std::int64_t, 2> range,
63 const std::span<const std::int64_t>& ghosts,
int bs);
74 Vec create_vector_wrap(
const common::IndexMap& map,
int bs,
75 const std::span<const PetscScalar>& x);
80 template <
typename Allocator>
81 Vec create_vector_wrap(
const la::Vector<PetscScalar, Allocator>& x)
84 return create_vector_wrap(*x.map(), x.bs(), x.array());
98 std::vector<IS> create_index_sets(
100 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
103 std::vector<std::vector<PetscScalar>> get_local_vectors(
106 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
109 void scatter_local_vectors(
110 Vec x,
const std::vector<std::span<const PetscScalar>>& x_b,
112 std::pair<std::reference_wrapper<const common::IndexMap>,
int>>& maps);
117 const std::string& type = std::string());
124 MatNullSpace create_nullspace(MPI_Comm comm,
const std::span<const Vec>& basis);
134 void set(std::string option);
137 template <
typename T>
138 void set(std::string option,
const T value)
140 if (option[0] !=
'-')
141 option =
'-' + option;
144 ierr = PetscOptionsSetValue(
nullptr, option.c_str(),
145 boost::lexical_cast<std::string>(value).c_str());
147 petsc::error(ierr, __FILE__,
"PetscOptionsSetValue");
151 void clear(std::string option);
188 Vector(Vec x,
bool inc_ref_count);
204 std::int64_t
size()
const;
213 MPI_Comm
comm()
const;
239 Operator(Mat A,
bool inc_ref_count);
258 std::array<std::int64_t, 2>
size()
const;
287 static auto set_fn(Mat A, InsertMode mode)
289 return [A, mode, cache = std::vector<PetscInt>()](
290 const std::span<const std::int32_t>& rows,
291 const std::span<const std::int32_t>& cols,
292 const std::span<const PetscScalar>& vals)
mutable ->
int
295 #ifdef PETSC_USE_64BIT_INDICES
296 cache.resize(rows.size() + cols.size());
297 std::copy(rows.begin(), rows.end(), cache.begin());
298 std::copy(cols.begin(), cols.end(),
299 std::next(cache.begin(), rows.size()));
300 const PetscInt* _rows = cache.data();
301 const PetscInt* _cols = cache.data() + rows.size();
302 ierr = MatSetValuesLocal(A, rows.size(), _rows, cols.size(), _cols,
305 ierr = MatSetValuesLocal(A, rows.size(), rows.data(), cols.size(),
306 cols.data(), vals.data(), mode);
311 petsc::error(ierr, __FILE__,
"MatSetValuesLocal");
324 return [A, mode, cache = std::vector<PetscInt>()](
325 const std::span<const std::int32_t>& rows,
326 const std::span<const std::int32_t>& cols,
327 const std::span<const PetscScalar>& vals)
mutable ->
int
330 #ifdef PETSC_USE_64BIT_INDICES
331 cache.resize(rows.size() + cols.size());
332 std::copy(rows.begin(), rows.end(), cache.begin());
333 std::copy(cols.begin(), cols.end(),
334 std::next(cache.begin(), rows.size()));
335 const PetscInt* _rows = cache.data();
336 const PetscInt* _cols = cache.data() + rows.size();
337 ierr = MatSetValuesBlockedLocal(A, rows.size(), _rows, cols.size(), _cols,
340 ierr = MatSetValuesBlockedLocal(A, rows.size(), rows.data(), cols.size(),
341 cols.data(), vals.data(), mode);
346 petsc::error(ierr, __FILE__,
"MatSetValuesBlockedLocal");
362 return [A, bs0, bs1, mode, cache0 = std::vector<PetscInt>(),
363 cache1 = std::vector<PetscInt>()](
364 const std::span<const std::int32_t>& rows,
365 const std::span<const std::int32_t>& cols,
366 const std::span<const PetscScalar>& vals)
mutable ->
int
369 cache0.resize(bs0 * rows.size());
370 cache1.resize(bs1 * cols.size());
371 for (std::size_t i = 0; i < rows.size(); ++i)
372 for (
int k = 0; k < bs0; ++k)
373 cache0[bs0 * i + k] = bs0 * rows[i] + k;
375 for (std::size_t i = 0; i < cols.size(); ++i)
376 for (
int k = 0; k < bs1; ++k)
377 cache1[bs1 * i + k] = bs1 * cols[i] + k;
379 ierr = MatSetValuesLocal(A, cache0.size(), cache0.data(), cache1.size(),
380 cache1.data(), vals.data(), mode);
383 petsc::error(ierr, __FILE__,
"MatSetValuesLocal");
391 const std::string& type = std::string());
397 Matrix(Mat A,
bool inc_ref_count);
484 int solve(Vec x,
const Vec b,
bool transpose =
false)
const;
This class represents the distribution index arrays across processes. An index array is a contiguous ...
Definition: IndexMap.h:64
This class provides a sparsity pattern data structure that can be used to initialize sparse matrices....
Definition: SparsityPattern.h:34
This class implements Krylov methods for linear systems of the form Ax = b. It is a wrapper for the K...
Definition: petsc.h:450
void set_operator(const Mat A)
Set operator (Mat)
Definition: petsc.cpp:652
KSP ksp() const
Return PETSc KSP pointer.
Definition: petsc.cpp:780
std::string get_options_prefix() const
Returns the prefix used by PETSc when searching the PETSc options database.
Definition: petsc.cpp:762
void set_operators(const Mat A, const Mat P)
Set operator and preconditioner matrix (Mat)
Definition: petsc.cpp:654
KrylovSolver(MPI_Comm comm)
Create Krylov solver for a particular method and named preconditioner.
Definition: petsc.cpp:615
void set_from_options() const
Set options from PETSc options database.
Definition: petsc.cpp:772
~KrylovSolver()
Destructor.
Definition: petsc.cpp:640
int solve(Vec x, const Vec b, bool transpose=false) const
Solve linear system Ax = b and return number of iterations (A^t x = b if transpose is true)
Definition: petsc.cpp:663
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the PETSc options database.
Definition: petsc.cpp:753
It is a simple wrapper for a PETSc matrix pointer (Mat). Its main purpose is to assist memory managem...
Definition: petsc.h:281
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:322
void set_from_options()
Call PETSc function MatSetFromOptions on the PETSc Mat object.
Definition: petsc.cpp:608
double norm(Norm norm_type) const
Return norm of matrix.
Definition: petsc.cpp:552
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:287
Matrix(MPI_Comm comm, const SparsityPattern &sp, const std::string &type=std::string())
Create holder for a PETSc Mat object from a sparsity pattern.
Definition: petsc.cpp:540
Matrix(Matrix &&A)=default
Move constructor (falls through to base class move constructor)
std::string get_options_prefix() const
Returns the prefix used by PETSc when searching the options database.
Definition: petsc.cpp:600
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:360
~Matrix()=default
Destructor.
Matrix & operator=(Matrix &&A)=default
Move assignment operator.
AssemblyType
Assembly type FINAL - corresponds to PETSc MAT_FINAL_ASSEMBLY FLUSH - corresponds to PETSc MAT_FLUSH_...
Definition: petsc.h:418
void apply(AssemblyType type)
Finalize assembly of tensor. The following values are recognized for the mode parameter:
Definition: petsc.cpp:577
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition: petsc.cpp:594
Matrix & operator=(const Matrix &A)=delete
Assignment operator (deleted)
This class is a base class for matrices that can be used in petsc::KrylovSolver.
Definition: petsc.h:236
std::array< std::int64_t, 2 > size() const
Return number of rows and columns (num_rows, num_cols). PETSc returns -1 if size has not been set.
Definition: petsc.cpp:499
Operator(Mat A, bool inc_ref_count)
Constructor.
Definition: petsc.cpp:474
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:509
Mat mat() const
Return PETSc Mat pointer.
Definition: petsc.cpp:537
Operator & operator=(const Operator &A)=delete
Assignment operator (deleted)
virtual ~Operator()
Destructor.
Definition: petsc.cpp:485
A simple wrapper for a PETSc vector pointer (Vec). Its main purpose is to assist with memory/lifetime...
Definition: petsc.h:163
void set_from_options()
Call PETSc function VecSetFromOptions on the underlying Vec object.
Definition: petsc.cpp:464
std::int64_t size() const
Return global size of the vector.
Definition: petsc.cpp:411
std::string get_options_prefix() const
Returns the prefix used by PETSc when searching the options database.
Definition: petsc.cpp:455
Vector copy() const
Create a copy of the vector.
Definition: petsc.cpp:401
virtual ~Vector()
Destructor.
Definition: petsc.cpp:389
Vector(const common::IndexMap &map, int bs)
Create a vector.
Definition: petsc.cpp:374
MPI_Comm comm() const
Return MPI communicator.
Definition: petsc.cpp:439
std::array< std::int64_t, 2 > local_range() const
Return ownership range for calling rank.
Definition: petsc.cpp:429
std::int32_t local_size() const
Return local size of vector (belonging to the call rank)
Definition: petsc.cpp:420
Vec vec() const
Return pointer to PETSc Vec object.
Definition: petsc.cpp:471
void set_options_prefix(std::string options_prefix)
Sets the prefix used by PETSc when searching the options database.
Definition: petsc.cpp:448
Miscellaneous classes, functions and types.
Mat create_matrix(const Form< PetscScalar > &a, const std::string &type=std::string())
Create a matrix.
Definition: petsc.cpp:21
void clear(std::string option)
Clear a PETSc option.
Definition: petsc.cpp:354
void set(std::string option)
Set PETSc option that takes no value.
Definition: petsc.cpp:349
Linear algebra interface.
Definition: sparsitybuild.h:15
Norm
Norm types.
Definition: utils.h:13