16 #ifndef dealii_petsc_matrix_base_h 17 # define dealii_petsc_matrix_base_h 20 # include <deal.II/base/config.h> 22 # ifdef DEAL_II_WITH_PETSC 24 # include <deal.II/base/subscriptor.h> 26 # include <deal.II/lac/exceptions.h> 27 # include <deal.II/lac/full_matrix.h> 28 # include <deal.II/lac/petsc_compatibility.h> 29 # include <deal.II/lac/petsc_vector_base.h> 30 # include <deal.II/lac/vector_operation.h> 32 # include <petscmat.h> 38 DEAL_II_NAMESPACE_OPEN
40 template <
typename Matrix>
49 namespace MatrixIterators
122 <<
"You tried to access row " << arg1
123 <<
" of a distributed matrix, but only rows " << arg2
124 <<
" through " << arg3
125 <<
" are stored locally and can be accessed.");
238 <<
"Attempt to access element " << arg2 <<
" of row " 239 << arg1 <<
" which doesn't have that many elements.");
376 set(
const std::vector<size_type> & indices,
378 const bool elide_zero_values =
false);
386 set(
const std::vector<size_type> & row_indices,
387 const std::vector<size_type> & col_indices,
389 const bool elide_zero_values =
false);
407 const std::vector<size_type> & col_indices,
408 const std::vector<PetscScalar> &values,
409 const bool elide_zero_values =
false);
429 const PetscScalar *values,
430 const bool elide_zero_values =
false);
464 add(
const std::vector<size_type> & indices,
466 const bool elide_zero_values =
true);
474 add(
const std::vector<size_type> & row_indices,
475 const std::vector<size_type> & col_indices,
477 const bool elide_zero_values =
true);
495 const std::vector<size_type> & col_indices,
496 const std::vector<PetscScalar> &values,
497 const bool elide_zero_values =
true);
517 const PetscScalar *values,
518 const bool elide_zero_values =
true,
519 const bool col_indices_are_sorted =
false);
538 clear_row(
const size_type row,
const PetscScalar new_diag_value = 0);
549 clear_rows(
const std::vector<size_type> &rows,
550 const PetscScalar new_diag_value = 0);
633 std::pair<size_type, size_type>
647 virtual const MPI_Comm &
648 get_mpi_communicator()
const = 0;
656 n_nonzero_elements()
const;
689 frobenius_norm()
const;
712 matrix_norm_square(
const VectorBase &v)
const;
742 operator*=(
const PetscScalar factor);
748 operator/=(
const PetscScalar factor);
756 add(
const PetscScalar factor,
const MatrixBase &other);
766 add(
const MatrixBase &other,
const PetscScalar factor);
882 operator Mat()
const;
903 is_symmetric(
const double tolerance = 1.e-12);
911 is_hermitian(
const double tolerance = 1.e-12);
920 write_ascii(
const PetscViewerFormat format = PETSC_VIEWER_DEFAULT);
930 print(std::ostream &out,
const bool alternative_output =
false)
const;
936 memory_consumption()
const;
942 "You are attempting an operation on two matrices that " 943 "are the same object, but the operation requires that the " 944 "two objects are in fact different.");
952 <<
"You tried to do a " 953 << (arg1 == 1 ?
"'set'" : (arg1 == 2 ?
"'add'" :
"???"))
954 <<
" operation but the matrix is currently in " 955 << (arg2 == 1 ?
"'set'" : (arg2 == 2 ?
"'add'" :
"???"))
956 <<
" mode. You first have to call 'compress()'.");
984 assert_is_compressed();
1075 friend class ::BlockMatrixBase;
1084 namespace MatrixIterators
1089 :
matrix(const_cast<MatrixBase *>(matrix))
1195 return !(*
this == other);
1223 set(i, 1, &j, &
value,
false);
1231 const bool elide_zero_values)
1233 Assert(indices.size() == values.
m(),
1237 for (
size_type i = 0; i < indices.size(); ++i)
1249 const std::vector<size_type> & col_indices,
1251 const bool elide_zero_values)
1253 Assert(row_indices.size() == values.
m(),
1255 Assert(col_indices.size() == values.
n(),
1258 for (
size_type i = 0; i < row_indices.size(); ++i)
1270 const std::vector<size_type> & col_indices,
1271 const std::vector<PetscScalar> &values,
1272 const bool elide_zero_values)
1274 Assert(col_indices.size() == values.size(),
1290 const PetscScalar *values,
1291 const bool elide_zero_values)
1295 const PetscInt petsc_i =
row;
1296 PetscInt
const *col_index_ptr;
1298 PetscScalar
const *col_value_ptr;
1302 if (elide_zero_values ==
false)
1304 col_index_ptr =
reinterpret_cast<const PetscInt *
>(col_indices);
1305 col_value_ptr = values;
1312 if (column_indices.size() < n_cols)
1314 column_indices.resize(n_cols);
1315 column_values.resize(n_cols);
1321 const PetscScalar value = values[j];
1323 if (value != PetscScalar())
1325 column_indices[n_columns] = col_indices[j];
1326 column_values[n_columns] =
value;
1332 col_index_ptr = column_indices.data();
1333 col_value_ptr = column_values.data();
1336 const PetscErrorCode ierr = MatSetValues(
matrix,
1353 if (value == PetscScalar())
1364 add(i, 1, &j, &value,
false);
1372 const bool elide_zero_values)
1374 Assert(indices.size() == values.
m(),
1378 for (
size_type i = 0; i < indices.size(); ++i)
1390 const std::vector<size_type> & col_indices,
1392 const bool elide_zero_values)
1394 Assert(row_indices.size() == values.
m(),
1396 Assert(col_indices.size() == values.
n(),
1399 for (
size_type i = 0; i < row_indices.size(); ++i)
1411 const std::vector<size_type> & col_indices,
1412 const std::vector<PetscScalar> &values,
1413 const bool elide_zero_values)
1415 Assert(col_indices.size() == values.size(),
1431 const PetscScalar *values,
1432 const bool elide_zero_values,
1435 (void)elide_zero_values;
1439 const PetscInt petsc_i =
row;
1440 PetscInt
const *col_index_ptr;
1442 PetscScalar
const *col_value_ptr;
1446 if (elide_zero_values ==
false)
1448 col_index_ptr =
reinterpret_cast<const PetscInt *
>(col_indices);
1449 col_value_ptr = values;
1456 if (column_indices.size() < n_cols)
1458 column_indices.resize(n_cols);
1459 column_values.resize(n_cols);
1465 const PetscScalar value = values[j];
1467 if (value != PetscScalar())
1469 column_indices[n_columns] = col_indices[j];
1470 column_values[n_columns] =
value;
1476 col_index_ptr = column_indices.data();
1477 col_value_ptr = column_values.data();
1480 const PetscErrorCode ierr = MatSetValues(
1481 matrix, 1, &petsc_i, n_columns, col_index_ptr, col_value_ptr, ADD_VALUES);
1512 Assert(in_local_range(r),
1513 ExcIndexRange(r, local_range().first, local_range().second));
1515 if (row_length(r) > 0)
1525 Assert(in_local_range(r),
1526 ExcIndexRange(r, local_range().first, local_range().second));
1539 if (i == local_range().second || (row_length(i) > 0))
1552 PetscInt begin, end;
1554 const PetscErrorCode ierr =
1555 MatGetOwnershipRange(static_cast<const Mat &>(
matrix), &begin, &end);
1558 return ((index >= static_cast<size_type>(begin)) &&
1559 (index < static_cast<size_type>(end)));
1568 last_action = new_action;
1570 Assert(last_action == new_action, ExcWrongMode(last_action, new_action));
1581 ExcMessage(
"Error: missing compress() call."));
1604 DEAL_II_NAMESPACE_CLOSE
1607 # endif // DEAL_II_WITH_PETSC
#define DeclException2(Exception2, type1, type2, outsequence)
void add(const size_type i, const size_type j, const PetscScalar value)
static::ExceptionBase & ExcInvalidIndexWithinRow(int arg1, int arg2)
std::shared_ptr< const std::vector< PetscScalar > > value_cache
Number trace(const SymmetricTensor< 2, dim, Number > &d)
PetscScalar value() const
#define AssertThrow(cond, exc)
types::global_dof_index size_type
bool operator<(const const_iterator &) const
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
unsigned long long int global_dof_index
const_iterator begin() const
std::vector< PetscInt > column_indices
const_iterator(const MatrixBase *matrix, const size_type row, const size_type index)
std::vector< PetscScalar > column_values
static::ExceptionBase & ExcMessage(std::string arg1)
const_iterator end() const
Accessor(const MatrixBase *matrix, const size_type row, const size_type index)
bool operator==(const const_iterator &) const
#define Assert(cond, exc)
static::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
bool in_local_range(const size_type index) const
bool operator!=(const const_iterator &) const
#define DeclExceptionMsg(Exception, defaulttext)
PetscScalar operator()(const size_type i, const size_type j) const
#define DeclException0(Exception0)
size_type row_length(const size_type row) const
SymmetricTensor< rank_, dim, Number > transpose(const SymmetricTensor< rank_, dim, Number > &t)
std::shared_ptr< const std::vector< size_type > > colnum_cache
static::ExceptionBase & ExcIteratorPastEnd()
friend class const_iterator
static::ExceptionBase & ExcNotQuadratic()
VectorOperation::values last_action
void prepare_action(const VectorOperation::values new_action)
const_iterator & operator++()
types::global_dof_index size_type
static::ExceptionBase & ExcAccessToNonlocalRow(int arg1, int arg2, int arg3)
void set(const size_type i, const size_type j, const PetscScalar value)
const Accessor & operator*() const
#define DeclException3(Exception3, type1, type2, type3, outsequence)
static::ExceptionBase & ExcBeyondEndOfMatrix()
types::global_dof_index size_type
#define AssertIsFinite(number)
void assert_is_compressed()
static::ExceptionBase & ExcInternalError()
const Accessor * operator->() const
std::pair< size_type, size_type > local_range() const