16 #include <deal.II/lac/petsc_matrix_base.h> 18 #ifdef DEAL_II_WITH_PETSC 20 # include <deal.II/lac/exceptions.h> 21 # include <deal.II/lac/petsc_compatibility.h> 22 # include <deal.II/lac/petsc_full_matrix.h> 23 # include <deal.II/lac/petsc_sparse_matrix.h> 24 # include <deal.II/lac/petsc_vector_base.h> 26 DEAL_II_NAMESPACE_OPEN
30 namespace MatrixIterators
38 if (
matrix->in_local_range(this->a_row) ==
false)
48 const PetscInt * colnums;
49 const PetscScalar *values;
52 MatGetRow(*matrix, this->a_row, &ncols, &colnums, &values);
63 std::make_shared<std::vector<size_type>>(colnums, colnums + ncols);
65 std::make_shared<std::vector<PetscScalar>>(values, values + ncols);
68 ierr = MatRestoreRow(*matrix, this->a_row, &ncols, &colnums, &values);
100 const int m = 0,
n = 0, n_nonzero_per_row = 0;
101 const PetscErrorCode ierr = MatCreateSeqAIJ(
102 PETSC_COMM_SELF,
m,
n, n_nonzero_per_row,
nullptr, &
matrix);
116 const PetscErrorCode ierr = MatZeroEntries(
matrix);
127 std::vector<size_type> rows(1, row);
135 const PetscScalar new_diag_value)
141 const std::vector<PetscInt> petsc_rows(rows.begin(), rows.end());
154 const PetscErrorCode ierr =
155 MatZeroRowsIS(
matrix, index_set, new_diag_value,
nullptr,
nullptr);
157 ISDestroy(&index_set);
165 PetscInt petsc_i = i, petsc_j = j;
169 const PetscErrorCode ierr =
170 MatGetValues(
matrix, 1, &petsc_i, 1, &petsc_j, &value);
195 # ifdef DEAL_II_WITH_MPI 199 int all_int_last_action;
201 const int ierr = MPI_Allreduce(&my_int_last_action,
202 &all_int_last_action,
211 ExcMessage(
"Error: not all processors agree on the last " 212 "VectorOperation before this compress() call."));
220 "Missing compress() or calling with wrong VectorOperation argument."));
223 PetscErrorCode ierr = MatAssemblyBegin(
matrix, MAT_FINAL_ASSEMBLY);
226 ierr = MatAssemblyEnd(
matrix, MAT_FINAL_ASSEMBLY);
237 PetscInt n_rows, n_cols;
239 const PetscErrorCode ierr = MatGetSize(
matrix, &n_rows, &n_cols);
250 PetscInt n_rows, n_cols;
252 const PetscErrorCode ierr = MatGetSize(
matrix, &n_rows, &n_cols);
263 PetscInt n_rows, n_cols;
265 const PetscErrorCode ierr = MatGetLocalSize(
matrix, &n_rows, &n_cols);
273 std::pair<MatrixBase::size_type, MatrixBase::size_type>
278 const PetscErrorCode ierr =
279 MatGetOwnershipRange(static_cast<const Mat &>(
matrix), &
begin, &
end);
291 const PetscErrorCode ierr = MatGetInfo(
matrix, MAT_GLOBAL_SUM, &mat_info);
294 return static_cast<size_type>(mat_info.nz_used);
314 const PetscInt * colnums;
315 const PetscScalar *values;
319 PetscErrorCode ierr = MatGetRow(*
this, row, &ncols, &colnums, &values);
327 const PetscInt ncols_saved = ncols;
328 ierr = MatRestoreRow(*
this, row, &ncols, &colnums, &values);
340 const PetscErrorCode ierr = MatNorm(
matrix, NORM_1, &result);
353 const PetscErrorCode ierr = MatNorm(
matrix, NORM_INFINITY, &result);
366 const PetscErrorCode ierr = MatNorm(
matrix, NORM_FROBENIUS, &result);
397 const PetscErrorCode ierr = MatGetTrace(
matrix, &result);
408 const PetscErrorCode ierr = MatScale(
matrix, a);
419 const PetscScalar factor = 1. / a;
420 const PetscErrorCode ierr = MatScale(
matrix, factor);
430 const PetscErrorCode ierr =
431 MatAXPY(
matrix, factor, other, DIFFERENT_NONZERO_PATTERN);
442 return add(factor, other);
451 const PetscErrorCode ierr = MatMult(
matrix, src, dst);
462 const PetscErrorCode ierr = MatMultTranspose(
matrix, src, dst);
473 const PetscErrorCode ierr = MatMultAdd(
matrix, src, dst, dst);
484 const PetscErrorCode ierr = MatMultTransposeAdd(
matrix, src, dst, dst);
496 const bool transpose_left)
498 const bool use_vector = (V.
size() == inputright.
m() ? true :
false);
499 if (transpose_left ==
false)
501 Assert(inputleft.
n() == inputright.
m(),
506 Assert(inputleft.
m() == inputright.
m(),
514 if (use_vector ==
false)
518 ierr = MatTransposeMatMult(inputleft,
527 ierr = MatMatMult(inputleft,
538 ierr = MatDuplicate(inputleft, MAT_COPY_VALUES, &tmp);
542 # if DEAL_II_PETSC_VERSION_LT(3, 8, 0) 543 ierr = MatTranspose(tmp, MAT_REUSE_MATRIX, &tmp);
545 ierr = MatTranspose(tmp, MAT_INPLACE_MATRIX, &tmp);
549 ierr = MatDiagonalScale(tmp,
nullptr, V);
551 ierr = MatMatMult(tmp,
566 internals::perform_mmult(*
this, B, C, V,
false);
574 internals::perform_mmult(*
this, B, C, V,
true);
594 MatrixBase::operator Mat()
const 608 # if DEAL_II_PETSC_VERSION_LT(3, 8, 0) 609 const PetscErrorCode ierr = MatTranspose(
matrix, MAT_REUSE_MATRIX, &
matrix);
611 const PetscErrorCode ierr =
622 const PetscErrorCode ierr = MatIsSymmetric(
matrix, tolerance, &truth);
633 const PetscErrorCode ierr = MatIsHermitian(
matrix, tolerance, &truth);
645 PetscErrorCode ierr =
646 PetscViewerSetFormat(PETSC_VIEWER_STDOUT_WORLD, format);
650 ierr = MatView(
matrix, PETSC_VIEWER_STDOUT_WORLD);
657 std::pair<MatrixBase::size_type, MatrixBase::size_type> loc_range =
661 const PetscInt * colnums;
662 const PetscScalar *values;
665 for (row = loc_range.first; row < loc_range.second; ++row)
667 PetscErrorCode ierr = MatGetRow(*
this, row, &ncols, &colnums, &values);
670 for (PetscInt col = 0; col < ncols; ++col)
672 out <<
"(" << row <<
"," << colnums[col] <<
") " << values[col]
676 ierr = MatRestoreRow(*
this, row, &ncols, &colnums, &values);
689 const PetscErrorCode ierr = MatGetInfo(
matrix, MAT_LOCAL, &info);
692 return sizeof(*this) +
static_cast<size_type>(info.memory);
697 DEAL_II_NAMESPACE_CLOSE
699 #endif // DEAL_II_WITH_PETSC PetscErrorCode destroy_matrix(Mat &matrix)
void Tmmult(MatrixBase &C, const MatrixBase &B, const VectorBase &V) const
Contents is actually a matrix.
PetscScalar matrix_scalar_product(const VectorBase &u, const VectorBase &v) const
void add(const size_type i, const size_type j, const PetscScalar value)
static ::ExceptionBase & ExcIO()
void vmult(VectorBase &dst, const VectorBase &src) const
const_iterator begin() const
real_type l2_norm() const
PetscReal frobenius_norm() const
static ::ExceptionBase & ExcScalarAssignmentOnlyForZeroValue()
size_type row_length(const size_type row) const
#define AssertThrow(cond, exc)
PetscBool is_symmetric(const double tolerance=1.e-12)
PetscBool is_hermitian(const double tolerance=1.e-12)
void clear_rows(const std::vector< size_type > &rows, const PetscScalar new_diag_value=0)
PetscReal linfty_norm() const
void compress(const VectorOperation::values operation)
size_type n_nonzero_elements() const
MatrixBase & operator=(const MatrixBase &)=delete
static ::ExceptionBase & ExcMessage(std::string arg1)
size_type local_size() const
#define Assert(cond, exc)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
void vmult_add(VectorBase &dst, const VectorBase &src) const
PetscScalar residual(VectorBase &dst, const VectorBase &x, const VectorBase &b) const
void mmult(MatrixBase &C, const MatrixBase &B, const VectorBase &V) const
PetscScalar diag_element(const size_type i) const
void Tvmult(VectorBase &dst, const VectorBase &src) const
PetscScalar el(const size_type i, const size_type j) const
void Tvmult_add(VectorBase &dst, const VectorBase &src) const
const_iterator end() const
void print(std::ostream &out, const bool alternative_output=false) const
static ::ExceptionBase & ExcNotQuadratic()
virtual ~MatrixBase() override
void write_ascii(const PetscViewerFormat format=PETSC_VIEWER_DEFAULT)
VectorOperation::values last_action
types::global_dof_index size_type
#define AssertThrowMPI(error_code)
MatrixBase & operator*=(const PetscScalar factor)
std::pair< size_type, size_type > local_range() const
PetscScalar trace() const
PetscScalar matrix_norm_square(const VectorBase &v) const
MatrixBase & operator/=(const PetscScalar factor)
PetscReal l1_norm() const
static ::ExceptionBase & ExcSourceEqualsDestination()
virtual const MPI_Comm & get_mpi_communicator() const =0
void clear_row(const size_type row, const PetscScalar new_diag_value=0)
std::size_t memory_consumption() const
void assert_is_compressed()
static ::ExceptionBase & ExcInternalError()