20#include "libmesh/libmesh_config.h"
22#ifdef LIBMESH_HAVE_PETSC
25#include "libmesh/petsc_matrix.h"
28#include "libmesh/dof_map.h"
29#include "libmesh/dense_matrix.h"
30#include "libmesh/libmesh_logging.h"
31#include "libmesh/petsc_vector.h"
32#include "libmesh/parallel.h"
33#include "libmesh/utility.h"
34#include "libmesh/wrapped_petsc.h"
37#ifdef LIBMESH_HAVE_UNISTD_H
42#ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
52void transform_preallocation_arrays (
const PetscInt blocksize,
53 const std::vector<numeric_index_type> & n_nz,
54 const std::vector<numeric_index_type> & n_oz,
55 std::vector<numeric_index_type> & b_n_nz,
56 std::vector<numeric_index_type> & b_n_oz)
58 libmesh_assert_equal_to (n_nz.size(), n_oz.size());
59 libmesh_assert_equal_to (n_nz.size()%blocksize, 0);
61 b_n_nz.clear(); b_n_nz.reserve(n_nz.size()/blocksize);
62 b_n_oz.clear(); b_n_oz.reserve(n_oz.size()/blocksize);
64 for (std::size_t nn=0, nnzs=n_nz.size(); nn<nnzs; nn += blocksize)
66 b_n_nz.push_back (n_nz[nn]/blocksize);
67 b_n_oz.push_back (n_oz[nn]/blocksize);
98 const bool destroy_on_exit) :
102 LibmeshPetscCall(MatGetType(mat_in, &mat_type));
104 LibmeshPetscCall(PetscStrcmp(mat_type, MATHYPRE, &is_hypre));
105 if (is_hypre == PETSC_TRUE)
125 this->
init(m_in, n_in, m_l, n_l, n_nz, n_oz, blocksize_in);
148 PetscInt m_global =
static_cast<PetscInt
>(m_in);
149 PetscInt n_global =
static_cast<PetscInt
>(n_in);
150 PetscInt m_local =
static_cast<PetscInt
>(m_l);
151 PetscInt n_local =
static_cast<PetscInt
>(n_l);
153 LibmeshPetscCall(MatCreate(this->comm().get(), &this->_mat));
154 LibmeshPetscCall(MatSetSizes(this->_mat, m_local, n_local, m_global, n_global));
155 PetscInt blocksize =
static_cast<PetscInt
>(blocksize_in);
156 LibmeshPetscCall(MatSetBlockSize(this->_mat,blocksize));
157 LibmeshPetscCall(MatSetOptionsPrefix(this->_mat, this->options_prefix().c_str()));
159#ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
164 libmesh_assert_equal_to (m_local % blocksize, 0);
165 libmesh_assert_equal_to (n_local % blocksize, 0);
166 libmesh_assert_equal_to (m_global % blocksize, 0);
167 libmesh_assert_equal_to (n_global % blocksize, 0);
168 libmesh_assert_equal_to (n_nz % blocksize, 0);
169 libmesh_assert_equal_to (n_oz % blocksize, 0);
171 LibmeshPetscCall(MatSetType(this->_mat, MATBAIJ));
176 LibmeshPetscCall(MatSetFromOptions(this->_mat));
181 switch (this->_mat_type) {
183 LibmeshPetscCall(MatSetType(this->_mat, MATAIJ));
188 LibmeshPetscCall(MatSetFromOptions(this->_mat));
192#if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
193 LibmeshPetscCall(MatSetType(this->_mat, MATHYPRE));
198 LibmeshPetscCall(MatSetFromOptions(this->_mat));
200 libmesh_error_msg(
"PETSc 3.9.4 or higher with hypre is required for MatHypre");
204 default: libmesh_error_msg(
"Unsupported petsc matrix type");
208 this->set_context ();
222 this->init_without_preallocation(m_in, n_in, m_l, n_l, blocksize_in);
224 PetscInt n_nz =
static_cast<PetscInt
>(nnz);
225 PetscInt n_oz =
static_cast<PetscInt
>(noz);
227#ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
230 LibmeshPetscCall(MatSeqBAIJSetPreallocation(this->_mat, blocksize, n_nz/blocksize, NULL));
231 LibmeshPetscCall(MatMPIBAIJSetPreallocation(this->_mat, blocksize,
232 n_nz/blocksize, NULL,
233 n_oz/blocksize, NULL));
238 switch (this->_mat_type) {
240 LibmeshPetscCall(MatSeqAIJSetPreallocation(this->_mat, n_nz, NULL));
241 LibmeshPetscCall(MatMPIAIJSetPreallocation(this->_mat, n_nz, NULL, n_oz, NULL));
245#if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
246 LibmeshPetscCall(MatHYPRESetPreallocation(this->_mat, n_nz, NULL, n_oz, NULL));
248 libmesh_error_msg(
"PETSc 3.9.4 or higher with hypre is required for MatHypre");
252 default: libmesh_error_msg(
"Unsupported petsc matrix type");
261 const std::vector<numeric_index_type> & n_nz,
262 const std::vector<numeric_index_type> & n_oz,
266 libmesh_assert_equal_to (n_nz.size(), m_l);
267 libmesh_assert_equal_to (n_oz.size(), m_l);
271#ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
272 PetscInt blocksize =
static_cast<PetscInt
>(blocksize_in);
277 std::vector<numeric_index_type> b_n_nz, b_n_oz;
279 transform_preallocation_arrays (blocksize,
283 LibmeshPetscCall(MatSeqBAIJSetPreallocation (this->_mat,
288 LibmeshPetscCall(MatMPIBAIJSetPreallocation (this->_mat,
298 switch (this->_mat_type) {
300 LibmeshPetscCall(MatSeqAIJSetPreallocation (this->_mat,
303 LibmeshPetscCall(MatMPIAIJSetPreallocation (this->_mat,
311#if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
312 LibmeshPetscCall(MatHYPRESetPreallocation (this->_mat,
318 libmesh_error_msg(
"PETSc 3.9.4 or higher with hypre is required for MatHypre");
322 default: libmesh_error_msg(
"Unsupported petsc matrix type");
333 LibmeshPetscCall(MatSetOption(this->_mat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
334 this->_is_initialized =
true;
342 const std::vector<numeric_index_type> & n_nz,
343 const std::vector<numeric_index_type> & n_oz,
346 this->init_without_preallocation(m_in, n_in, m_l, n_l, blocksize_in);
347 this->preallocate(m_l, n_nz, n_oz, blocksize_in);
363 const auto blocksize = this->_dof_map->block_size();
365 this->init_without_preallocation(m_in, m_in, m_l, m_l, blocksize);
366 if (!this->_use_hash_table)
368 const std::vector<numeric_index_type> & n_nz = this->_sp->get_n_nz();
369 const std::vector<numeric_index_type> & n_oz = this->_sp->get_n_oz();
371 this->preallocate(m_l, n_nz, n_oz, blocksize);
381 libmesh_not_implemented();
389#if !PETSC_VERSION_LESS_THAN(3,9,0)
392 LibmeshPetscCall(MatResetPreallocation(this->_mat));
394 libmesh_warning(
"Your version of PETSc doesn't support resetting of "
395 "preallocation, so we will use your most recent sparsity "
396 "pattern. This may result in a degradation of performance\n");
410 LibmeshPetscCall(MatZeroEntries(this->_mat));
425 LibmeshPetscCall(MatZeroRows(this->_mat, cast_int<PetscInt>(rows.size()),
429 LibmeshPetscCall(MatZeroRows(this->_mat, 0, NULL,
PS(diag_value), NULL, NULL));
435 libmesh_error_msg_if(!this->
closed(),
"Matrix must be closed before it can be cloned!");
439 LibmeshPetscCall(MatDuplicate(this->_mat, MAT_DO_NOT_COPY_VALUES, ©));
443 auto ret = std::make_unique<PetscMatrix<T>>(copy, this->comm());
444 ret->set_destroy_mat_on_exit(
true);
454 libmesh_error_msg_if(!this->
closed(),
"Matrix must be closed before it can be cloned!");
458 LibmeshPetscCall(MatDuplicate(this->_mat, MAT_COPY_VALUES, ©));
462 auto ret = std::make_unique<PetscMatrix<T>>(copy, this->comm());
463 ret->set_destroy_mat_on_exit(
true);
478 PetscReal petsc_value;
483 LibmeshPetscCall(MatNorm(this->_mat, N, &petsc_value));
516 libmesh_deprecated();
517 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::print_matlab().\n"
518 "Please update your code, as this warning will become an error in a future release.");
528 LibmeshPetscCall(PetscViewerASCIIOpen( this->comm().get(),
530 petsc_viewer.
get()));
532#if PETSC_VERSION_LESS_THAN(3,7,0)
533 LibmeshPetscCall(PetscViewerSetFormat (petsc_viewer,
534 PETSC_VIEWER_ASCII_MATLAB));
536 LibmeshPetscCall(PetscViewerPushFormat (petsc_viewer,
537 PETSC_VIEWER_ASCII_MATLAB));
540 LibmeshPetscCall(MatView (this->_mat, petsc_viewer));
546#if PETSC_VERSION_LESS_THAN(3,7,0)
547 LibmeshPetscCall(PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
548 PETSC_VIEWER_ASCII_MATLAB));
550 LibmeshPetscCall(PetscViewerPushFormat (PETSC_VIEWER_STDOUT_WORLD,
551 PETSC_VIEWER_ASCII_MATLAB));
554 LibmeshPetscCall(MatView (this->_mat, PETSC_VIEWER_STDOUT_WORLD));
579 libmesh_deprecated();
580 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::print_personal().\n"
581 "Please update your code, as this warning will become an error in a future release.");
586 if (os.rdbuf() == std::cout.rdbuf())
587 LibmeshPetscCall(MatView(this->_mat, NULL));
594 std::string temp_filename;
598 char c[] =
"temp_petsc_matrix.XXXXXX";
603 if (this->processor_id() == 0)
608 libmesh_error_msg_if(fd == -1,
"mkstemp failed in PetscMatrix::print_personal()");
620 this->comm().broadcast(temp_filename);
623 PetscViewer petsc_viewer;
629 LibmeshPetscCall(PetscViewerASCIIOpen( this->comm().get(),
630 temp_filename.c_str(),
639 LibmeshPetscCall(MatView (this->_mat, petsc_viewer));
641 if (this->processor_id() == 0)
645 std::ifstream input_stream(temp_filename.c_str());
646 os << input_stream.rdbuf();
650 input_stream.close();
651 std::remove(temp_filename.c_str());
660 PetscViewerType viewertype,
661 PetscFileMode filemode)
663 parallel_object_only();
669 LibmeshPetscCall(MatCreate(this->comm().get(), &this->_mat));
670 this->_is_initialized =
true;
674 LibmeshPetscCall(PetscViewerCreate(this->comm().get(), &viewer));
675 LibmeshPetscCall(PetscViewerSetType(viewer, viewertype));
676 LibmeshPetscCall(PetscViewerSetFromOptions(viewer));
677 LibmeshPetscCall(PetscViewerFileSetMode(viewer, filemode));
678 LibmeshPetscCall(PetscViewerFileSetName(viewer, filename.c_str()));
679 if (filemode == FILE_MODE_READ)
680 LibmeshPetscCall(MatLoad(this->_mat, viewer));
682 LibmeshPetscCall(MatView(this->_mat, viewer));
683 LibmeshPetscCall(PetscViewerDestroy(&viewer));
695 nonconst_this->
_petsc_viewer(filename, PETSCVIEWERBINARY, FILE_MODE_WRITE);
707 nonconst_this->
_petsc_viewer(filename, PETSCVIEWERHDF5, FILE_MODE_WRITE);
715 LOG_SCOPE(
"read_petsc_binary()",
"PetscMatrix");
717 this->_petsc_viewer(filename, PETSCVIEWERBINARY, FILE_MODE_READ);
725 LOG_SCOPE(
"read_petsc_hdf5()",
"PetscMatrix");
727 this->_petsc_viewer(filename, PETSCVIEWERHDF5, FILE_MODE_READ);
734 const std::vector<numeric_index_type> & rows,
735 const std::vector<numeric_index_type> & cols)
742 libmesh_assert_equal_to (rows.size(), n_rows);
743 libmesh_assert_equal_to (cols.size(), n_cols);
745 std::scoped_lock lock(this->_petsc_matrix_mutex);
746 LibmeshPetscCall(MatSetValues(this->_mat,
760 const std::vector<numeric_index_type> & brows,
761 const std::vector<numeric_index_type> & bcols)
766 cast_int<numeric_index_type>(brows.size());
768 cast_int<numeric_index_type>(bcols.size());
772 cast_int<numeric_index_type>(dm.
m());
774 cast_int<numeric_index_type>(dm.
n());
777 libmesh_assert_equal_to (n_cols / n_bcols, blocksize);
778 libmesh_assert_equal_to (blocksize*n_brows, n_rows);
779 libmesh_assert_equal_to (blocksize*n_bcols, n_cols);
781 PetscInt petsc_blocksize;
782 LibmeshPetscCall(MatGetBlockSize(this->_mat, &petsc_blocksize));
783 libmesh_assert_equal_to (blocksize,
static_cast<numeric_index_type>(petsc_blocksize));
786 std::scoped_lock lock(this->_petsc_matrix_mutex);
788 LibmeshPetscCall(MatSetValuesBlocked(this->_mat,
801 const std::vector<numeric_index_type> & rows,
802 const std::vector<numeric_index_type> & cols,
803 const bool reuse_submatrix)
const
807 libmesh_deprecated();
808 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::create_submatrix().\n"
809 "Please update your code, as this warning will become an error in a future release.");
816 PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
825 LibmeshPetscCall(ISCreateGeneral(this->comm().get(),
826 cast_int<PetscInt>(rows.size()),
832 LibmeshPetscCall(ISCreateGeneral(this->comm().get(),
833 cast_int<PetscInt>(cols.size()),
839 LibmeshPetscCall(LibMeshCreateSubMatrix(this->_mat,
842 (reuse_submatrix ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX),
843 (&petsc_submatrix->
_mat)));
847 petsc_submatrix->
close();
852 const std::vector<numeric_index_type> & rows,
853 const std::vector<numeric_index_type> & cols)
const
857 libmesh_deprecated();
858 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::create_submatrix_nosort().\n"
859 "Please update your code, as this warning will become an error in a future release.");
864 PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
866 LibmeshPetscCall(MatZeroEntries(petsc_submatrix->
_mat));
868 PetscInt pc_ncols = 0;
869 const PetscInt * pc_cols;
870 const PetscScalar * pc_vals;
873 std::vector<PetscInt> sub_cols;
874 std::vector<PetscScalar> sub_vals;
878 PetscInt sub_rid[] = {
static_cast<PetscInt
>(i)};
879 PetscInt rid =
static_cast<PetscInt
>(rows[i]);
881 if (rows[i]>= this->row_start() && rows[i]< this->row_stop())
884 LibmeshPetscCall(MatGetRow(this->_mat, rid, &pc_ncols, &pc_cols, &pc_vals));
888 for (
unsigned int idx = 0; idx< static_cast<unsigned int>(pc_ncols); idx++)
890 if (pc_cols[idx] ==
static_cast<PetscInt
>(cols[j]))
892 sub_cols.push_back(
static_cast<PetscInt
>(j));
893 sub_vals.push_back(pc_vals[idx]);
898 LibmeshPetscCall(MatSetValues(petsc_submatrix->
_mat,
901 static_cast<PetscInt
>(sub_vals.size()),
905 LibmeshPetscCall(MatRestoreRow(this->_mat, rid, &pc_ncols, &pc_cols, &pc_vals));
911 MatAssemblyBeginEnd(petsc_submatrix->
comm(), petsc_submatrix->
_mat, MAT_FINAL_ASSEMBLY);
914 petsc_submatrix->
close();
925 LibmeshPetscCall(MatGetDiagonal(
const_cast<PetscMatrix<T> *
>(
this)->mat(),petsc_dest.
vec()));
938 if (&petsc_dest !=
this)
941 if (&petsc_dest ==
this)
944#if PETSC_VERSION_LESS_THAN(3,7,0)
945 LibmeshPetscCall(MatTranspose(this->_mat,MAT_REUSE_MATRIX, &petsc_dest.
_mat));
947 LibmeshPetscCall(MatTranspose(this->_mat, MAT_INPLACE_MATRIX, &petsc_dest.
_mat));
950 LibmeshPetscCall(MatTranspose(this->_mat,MAT_INITIAL_MATRIX, &petsc_dest.
_mat));
964 MatAssemblyBeginEnd(this->comm(), this->_mat, MAT_FLUSH_ASSEMBLY);
976 PetscInt i_val=i, j_val=j;
978 PetscScalar petsc_value =
static_cast<PetscScalar
>(
value);
979 std::scoped_lock lock(this->_petsc_matrix_mutex);
980 LibmeshPetscCall(MatSetValues(this->_mat, 1, &i_val, 1, &j_val,
981 &petsc_value, INSERT_VALUES));
993 PetscInt i_val=i, j_val=j;
995 PetscScalar petsc_value =
static_cast<PetscScalar
>(
value);
996 std::scoped_lock lock(this->_petsc_matrix_mutex);
997 LibmeshPetscCall(MatSetValues(this->_mat, 1, &i_val, 1, &j_val,
998 &petsc_value, ADD_VALUES));
1003template <
typename T>
1005 const std::vector<numeric_index_type> & dof_indices)
1007 this->add_matrix (dm, dof_indices, dof_indices);
1016template <
typename T>
1023 libmesh_assert_equal_to (this->m(), X_in.
m());
1024 libmesh_assert_equal_to (this->n(), X_in.
n());
1026 PetscScalar a =
static_cast<PetscScalar
> (a_in);
1027 const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1034 semiparallel_only();
1036 LibmeshPetscCall(MatAXPY(this->_mat, a, X->
_mat, DIFFERENT_NONZERO_PATTERN));
1040template <
typename T>
1047 libmesh_assert_equal_to (this->n(), X_in.
m());
1049 const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1055 semiparallel_only();
1058 LibmeshPetscCall(MatMatMult(this->_mat, X->
_mat, MAT_REUSE_MATRIX, PETSC_DEFAULT, &Y->_mat));
1062 LibmeshPetscCall(MatMatMult(this->_mat, X->
_mat, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &Y->_mat));
1066 Y->_is_initialized =
true;
1069template <
typename T>
1072 const std::map<numeric_index_type, numeric_index_type> & row_ltog,
1073 const std::map<numeric_index_type, numeric_index_type> & col_ltog,
1078 libmesh_assert_greater_equal(spm.
m(), row_ltog.size());
1079 libmesh_assert_greater_equal(spm.
n(), col_ltog.size());
1082 libmesh_assert_greater_equal(this->m(), spm.
m());
1083 libmesh_assert_greater_equal(this->n(), spm.
n());
1088 auto pscm = cast_ptr<const PetscMatrix<T> *>(&spm);
1092 const PetscInt * lcols;
1093 const PetscScalar * vals;
1095 std::vector<PetscInt> gcols;
1096 std::vector<PetscScalar> values;
1098 for (
auto ltog : row_ltog)
1100 PetscInt grow[] = {
static_cast<PetscInt
>(ltog.second)};
1102 LibmeshPetscCall(MatGetRow(pscm->_mat,
static_cast<PetscInt
>(ltog.first), &ncols, &lcols, &vals));
1105 gcols.resize(ncols);
1106 values.resize(ncols);
1109 gcols[i] = libmesh_map_find(col_ltog, lcols[i]);
1110 values[i] =
PS(scalar) * vals[i];
1113 LibmeshPetscCall(MatSetValues(this->_mat, 1, grow, ncols, gcols.data(), values.data(), ADD_VALUES));
1114 LibmeshPetscCall(MatRestoreRow(pscm->_mat,
static_cast<PetscInt
>(ltog.first), &ncols, &lcols, &vals));
1120template <
typename T>
1130 i_val=
static_cast<PetscInt
>(i_in),
1131 j_val=
static_cast<PetscInt
>(j_in);
1140 LibmeshPetscCall(MatGetValue(this->_mat, i_val, j_val, &
value));
1145template <
typename T>
1147 std::vector<numeric_index_type> & indices,
1148 std::vector<T> & values)
const
1152 const PetscScalar * petsc_row;
1153 const PetscInt * petsc_cols;
1157 i_val =
static_cast<PetscInt
>(i_in);
1175 std::lock_guard<std::mutex> lock(_petsc_matrix_mutex);
1177 LibmeshPetscCall(MatGetRow(this->_mat, i_val, &ncols, &petsc_cols, &petsc_row));
1180 indices.resize(
static_cast<std::size_t
>(ncols));
1181 values.resize(
static_cast<std::size_t
>(ncols));
1186 values[i] =
static_cast<T
>(petsc_row[i]);
1189 LibmeshPetscCall(MatRestoreRow(this->_mat, i_val,
1190 &ncols, &petsc_cols, &petsc_row));
1195template <
typename T>
1198 semiparallel_only();
1202 PetscBool assembled;
1203 LibmeshPetscCall(MatAssembled(this->_mat, &assembled));
1205 const bool cxx_assembled = (assembled == PETSC_TRUE) ?
true :
false;
1214 LibmeshPetscCall(MatCopy(v.
_mat, this->_mat, DIFFERENT_NONZERO_PATTERN));
1217 LibmeshPetscCall(MatDuplicate(v.
_mat, MAT_COPY_VALUES, &this->_mat));
1219 this->_is_initialized =
true;
1224template <
typename T>
1227 *
this = cast_ref<const PetscMatrix<T> &>(v);
1231template <
typename T>
1236 LibmeshPetscCall(MatScale(this->_mat,
PS(scale)));
1239template <
typename T>
1242#if PETSC_RELEASE_LESS_THAN(3,19,0)
1249#if PETSC_RELEASE_GREATER_EQUALS(3,23,0)
1250template <
typename T>
1251std::unique_ptr<PetscMatrix<T>>
1257 LibmeshPetscCall(MatDuplicate(this->_mat, MAT_DO_NOT_COPY_VALUES, &xaij));
1258 LibmeshPetscCall(MatCopyHashToXAIJ(this->_mat, xaij));
1259 return std::make_unique<PetscMatrix<T>>(xaij, this->comm(),
true);
1263template <
typename T>
1267 semiparallel_only();
1269 if (this->_use_hash_table)
1270#if PETSC_RELEASE_GREATER_EQUALS(3, 23, 0)
1272 LibmeshPetscCall(MatResetHash(this->_mat));
1274 libmesh_error_msg(
"Resetting hash tables not supported until PETSc version 3.23");
1277 this->reset_preallocation();
void finish_initialization()
Defines a dense matrix for use in Finite Element-type computations.
std::vector< T > & get_values()
Provides a uniform interface to vector storage schemes for different linear algebra libraries.
const Parallel::Communicator & comm() const
This class provides a nice interface to the PETSc C-based data structures for parallel,...
virtual void close() override
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
virtual bool closed() const override
Mat _mat
PETSc matrix datatype to store values.
This class provides a nice interface to the PETSc C-based AIJ data structures for parallel,...
void update_preallocation_and_zero()
Update the sparsity pattern based on dof_map, and set the matrix to zero.
virtual Real l1_norm() const override
virtual void _get_submatrix(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols, const bool reuse_submatrix) const override
This function either creates or re-initializes a matrix called submatrix which is defined by the indi...
virtual void flush() override
For PETSc matrix , this function is similar to close but without shrinking memory.
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) override
Add the full matrix dm to the SparseMatrix.
virtual void get_row(numeric_index_type i, std::vector< numeric_index_type > &indices, std::vector< T > &values) const override
Get a row from the matrix.
virtual void zero_rows(std::vector< numeric_index_type > &rows, T diag_value=0.0) override
Sets all row entries to 0 then puts diag_value in the diagonal entry.
virtual bool supports_hash_table() const override
virtual void print_petsc_hdf5(const std::string &filename) const override
Write the contents of the matrix to a file in PETSc's HDF5 sparse matrix format.
virtual void get_diagonal(NumericVector< T > &dest) const override
Copies the diagonal part of the matrix into dest.
std::unique_ptr< PetscMatrix< T > > copy_from_hash()
Creates a copy of the current hash table matrix and then performs assembly.
virtual void matrix_matrix_mult(SparseMatrix< T > &X, SparseMatrix< T > &Y, bool reuse=false) override
Compute Y = A*X for matrix X.
virtual void add_sparse_matrix(const SparseMatrix< T > &spm, const std::map< numeric_index_type, numeric_index_type > &row_ltog, const std::map< numeric_index_type, numeric_index_type > &col_ltog, const T scalar) override
Add scalar* spm to the rows and cols of this matrix (A): A(rows[i], cols[j]) += scalar * spm(i,...
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override
Add value to the element (i,j).
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const override
virtual void create_submatrix_nosort(SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const override
Similar to the create_submatrix function, this function creates a submatrix which is defined by the i...
virtual void restore_original_nonzero_pattern() override
Reset the memory storage of the matrix.
PetscMatrix(const Parallel::Communicator &comm_in)
Constructor; initializes the matrix to be empty, without any structure, i.e.
virtual void read_petsc_binary(const std::string &filename) override
Read the contents of the matrix from a file in PETSc's binary sparse matrix format.
void reset_preallocation()
Reset matrix to use the original nonzero pattern provided by users.
void preallocate(numeric_index_type m_l, const std::vector< numeric_index_type > &n_nz, const std::vector< numeric_index_type > &n_oz, numeric_index_type blocksize)
virtual void print_personal(std::ostream &os=libMesh::out) const override
Print the contents of the matrix to the screen with the PETSc viewer.
virtual void get_transpose(SparseMatrix< T > &dest) const override
Copies the transpose of the matrix into dest, which may be *this.
PetscMatrixType _mat_type
virtual std::unique_ptr< SparseMatrix< T > > clone() const override
virtual void scale(const T scale) override
Scales all elements of this matrix by scale.
void finish_initialization()
Finish up the initialization process.
virtual void read_petsc_hdf5(const std::string &filename) override
Read the contents of the matrix from a file in PETSc's HDF5 sparse matrix format.
virtual void init(const numeric_index_type m, const numeric_index_type n, const numeric_index_type m_l, const numeric_index_type n_l, const numeric_index_type n_nz=30, const numeric_index_type n_oz=10, const numeric_index_type blocksize=1) override
Initialize a PETSc matrix.
PetscMatrix & operator=(PetscMatrix &&)=delete
virtual Real linfty_norm() const override
void init_without_preallocation(numeric_index_type m, numeric_index_type n, numeric_index_type m_l, numeric_index_type n_l, numeric_index_type blocksize)
Perform matrix initialization steps sans preallocation.
virtual void print_matlab(const std::string &name="") const override
Print the contents of the matrix in Matlab's sparse matrix format.
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &brows, const std::vector< numeric_index_type > &bcols) override
Add the full matrix dm to the SparseMatrix.
void _petsc_viewer(const std::string &filename, PetscViewerType viewertype, PetscFileMode filemode)
virtual void zero() override
Set all entries to 0.
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
Set the element (i,j) to value.
Real frobenius_norm() const
virtual void print_petsc_binary(const std::string &filename) const override
Write the contents of the matrix to a file in PETSc's binary sparse matrix format.
This class provides a nice interface to PETSc's Vec object.
virtual bool initialized() const
virtual numeric_index_type n() const =0
virtual void clear()=0
Restores the SparseMatrix<T> to a pristine state.
virtual numeric_index_type m() const =0
bool _is_initialized
Flag indicating whether or not the matrix has been initialized.
The libMesh namespace provides an interface to certain functionality in the library.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
ParallelType
Defines an enum for parallel data structure types.
bool closed()
Checks that the library has been closed.
void libmesh_ignore(const Args &...)
PetscScalar * pPS(T *ptr)
dof_id_type numeric_index_type
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
bool initialized()
Checks that library initialization has been done.
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real