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_parallel_sparse_matrix.h> 25 # include <deal.II/lac/petsc_vector_base.h> 27 DEAL_II_NAMESPACE_OPEN
31 namespace MatrixIterators
40 if (
matrix->in_local_range(this->a_row) ==
false)
42 colnum_cache.reset ();
50 const PetscInt *colnums;
51 const PetscScalar *values;
53 PetscErrorCode ierr = MatGetRow(*matrix, this->a_row, &ncols, &colnums,
64 colnum_cache = std::make_shared<std::vector<size_type>> (colnums, colnums+ncols);
65 value_cache = std::make_shared<std::vector<PetscScalar>> (values, values+ncols);
68 ierr = MatRestoreRow(*matrix, this->a_row, &ncols, &colnums, &values);
101 const int m=0,
n=0, n_nonzero_per_row=0;
102 const PetscErrorCode ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,
m,
n,
118 const PetscErrorCode ierr = MatZeroEntries (
matrix);
128 const PetscScalar new_diag_value)
130 std::vector<size_type> rows (1, row);
138 const PetscScalar new_diag_value)
144 const std::vector<PetscInt> petsc_rows (rows.begin(), rows.end());
152 petsc_rows.data(), PETSC_COPY_VALUES, &index_set);
154 const PetscErrorCode ierr = MatZeroRowsIS(
matrix, index_set, new_diag_value,
157 ISDestroy (&index_set);
166 PetscInt petsc_i = i, petsc_j = j;
170 const PetscErrorCode ierr = MatGetValues (
matrix, 1, &petsc_i, 1, &petsc_j,
196 #ifdef DEAL_II_WITH_MPI 200 int all_int_last_action;
202 const int ierr = MPI_Allreduce
203 (&my_int_last_action, &all_int_last_action, 1, MPI_INT, MPI_BOR,
208 ExcMessage(
"Error: not all processors agree on the last " 209 "VectorOperation before this compress() call."));
216 ExcMessage(
"Missing compress() or calling with wrong VectorOperation argument."));
219 PetscErrorCode ierr = MatAssemblyBegin (
matrix,MAT_FINAL_ASSEMBLY);
222 ierr = MatAssemblyEnd (
matrix,MAT_FINAL_ASSEMBLY);
233 PetscInt n_rows, n_cols;
235 const PetscErrorCode ierr = MatGetSize (
matrix, &n_rows, &n_cols);
246 PetscInt n_rows, n_cols;
248 const PetscErrorCode ierr = MatGetSize (
matrix, &n_rows, &n_cols);
259 PetscInt n_rows, n_cols;
261 const PetscErrorCode ierr = MatGetLocalSize (
matrix, &n_rows, &n_cols);
269 std::pair<MatrixBase::size_type, MatrixBase::size_type>
274 const PetscErrorCode ierr = MatGetOwnershipRange (static_cast<const Mat &>(
matrix),
287 const PetscErrorCode ierr = MatGetInfo (
matrix, MAT_GLOBAL_SUM, &mat_info);
290 return static_cast<size_type>(mat_info.nz_used);
310 const PetscInt *colnums;
311 const PetscScalar *values;
315 PetscErrorCode ierr = MatGetRow(*
this, row, &ncols, &colnums, &values);
323 const PetscInt ncols_saved = ncols;
324 ierr = MatRestoreRow(*
this, row, &ncols, &colnums, &values);
336 const PetscErrorCode ierr = MatNorm (
matrix, NORM_1, &result);
349 const PetscErrorCode ierr = MatNorm (
matrix, NORM_INFINITY, &result);
362 const PetscErrorCode ierr = MatNorm (
matrix, NORM_FROBENIUS, &result);
393 const PetscErrorCode ierr = MatGetTrace (
matrix, &result);
404 const PetscErrorCode ierr = MatScale (
matrix, a);
415 const PetscScalar factor = 1./a;
416 const PetscErrorCode ierr = MatScale (
matrix, factor);
427 const PetscErrorCode ierr = MatAXPY (
matrix, factor,
428 other, DIFFERENT_NONZERO_PATTERN);
438 const PetscScalar factor)
440 return add(factor, other);
450 const PetscErrorCode ierr = MatMult (
matrix, src, dst);
462 const PetscErrorCode ierr = MatMultTranspose (
matrix, src, dst);
474 const PetscErrorCode ierr = MatMultAdd (
matrix, src, dst, dst);
486 const PetscErrorCode ierr = MatMultTransposeAdd (
matrix, src, dst, dst);
493 void perform_mmult (
const MatrixBase &inputleft,
497 const bool transpose_left)
499 const bool use_vector = (V.
size() == inputright.
m() ? true :
false);
500 if (transpose_left ==
false)
502 Assert (inputleft.
n() == inputright.
m(),
507 Assert (inputleft.
m() == inputright.
m(),
515 if (use_vector ==
false)
519 ierr = MatTransposeMatMult (inputleft,
528 ierr = MatMatMult (inputleft,
539 ierr = MatDuplicate (inputleft, MAT_COPY_VALUES, &tmp);
543 #if DEAL_II_PETSC_VERSION_LT(3,8,0) 544 ierr = MatTranspose(tmp, MAT_REUSE_MATRIX, &tmp);
546 ierr = MatTranspose(tmp, MAT_INPLACE_MATRIX, &tmp);
550 ierr = MatDiagonalScale (tmp,
nullptr, V);
552 ierr = MatMatMult (tmp,
567 internals::perform_mmult (*
this, B, C, V,
false);
575 internals::perform_mmult (*
this, B, C, V,
true);
595 MatrixBase::operator Mat ()
const 608 const PetscErrorCode ierr = MatTranspose(
matrix, MAT_REUSE_MATRIX, &
matrix);
617 const PetscErrorCode ierr = MatIsSymmetric (
matrix, tolerance, &truth);
628 const PetscErrorCode ierr = MatIsHermitian (
matrix, tolerance, &truth);
640 PetscErrorCode ierr = PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
645 ierr = MatView (
matrix, PETSC_VIEWER_STDOUT_WORLD);
653 std::pair<MatrixBase::size_type, MatrixBase::size_type>
657 const PetscInt *colnums;
658 const PetscScalar *values;
661 for (row = loc_range.first; row < loc_range.second; ++row)
663 PetscErrorCode ierr = MatGetRow(*
this, row, &ncols, &colnums, &values);
666 for (PetscInt col = 0; col < ncols; ++col)
668 out <<
"(" << row <<
"," << colnums[col] <<
") " << values[col] << std::endl;
671 ierr = MatRestoreRow(*
this, row, &ncols, &colnums, &values);
684 const PetscErrorCode ierr = MatGetInfo(
matrix, MAT_LOCAL, &info);
687 return sizeof(*this) +
static_cast<size_type>(info.memory);
692 DEAL_II_NAMESPACE_CLOSE
694 #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
types::global_dof_index size_type
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()
void write_ascii(const PetscViewerFormat format=PETSC_VIEWER_DEFAULT)
VectorOperation::values last_action
#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()