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 52 void 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)
114 template <
typename T>
125 this->
init(m_in, n_in, m_l, n_l, n_nz, n_oz, blocksize_in);
130 template <
typename T>
133 template <
typename T>
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,
""));
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 ();
213 template <
typename T>
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");
259 template <
typename T>
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");
328 template <
typename T>
333 LibmeshPetscCall(MatSetOption(this->_mat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
337 template <
typename T>
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);
353 template <
typename T>
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);
378 template <
typename T>
381 libmesh_not_implemented();
384 template <
typename T>
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");
400 template <
typename T>
409 LibmeshPetscCall(MatGetLocalSize(this->_mat,&m_l,&n_l));
412 LibmeshPetscCall(MatZeroEntries(this->_mat));
415 template <
typename T>
427 LibmeshPetscCall(MatZeroRows(this->_mat, cast_int<PetscInt>(rows.size()),
431 LibmeshPetscCall(MatZeroRows(this->_mat, 0, NULL,
PS(diag_value), NULL, NULL));
434 template <
typename T>
437 libmesh_error_msg_if(!this->
closed(),
"Matrix must be closed before it can be cloned!");
441 LibmeshPetscCall(MatDuplicate(this->_mat, MAT_DO_NOT_COPY_VALUES, ©));
445 auto ret = std::make_unique<PetscMatrix<T>>(copy, this->comm());
446 ret->set_destroy_mat_on_exit(
true);
453 template <
typename T>
456 libmesh_error_msg_if(!this->
closed(),
"Matrix must be closed before it can be cloned!");
460 LibmeshPetscCall(MatDuplicate(this->_mat, MAT_COPY_VALUES, ©));
464 auto ret = std::make_unique<PetscMatrix<T>>(copy, this->comm());
465 ret->set_destroy_mat_on_exit(
true);
472 template <
typename T>
473 template <NormType N>
480 PetscReal petsc_value;
485 LibmeshPetscCall(MatNorm(this->_mat, N, &petsc_value));
491 template <
typename T>
496 template <
typename T>
501 template <
typename T>
509 template <
typename T>
518 libmesh_deprecated();
519 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::print_matlab().\n" 520 "Please update your code, as this warning will become an error in a future release.");
530 LibmeshPetscCall(PetscViewerASCIIOpen( this->comm().
get(),
532 petsc_viewer.
get()));
534 #if PETSC_VERSION_LESS_THAN(3,7,0) 535 LibmeshPetscCall(PetscViewerSetFormat (petsc_viewer,
536 PETSC_VIEWER_ASCII_MATLAB));
538 LibmeshPetscCall(PetscViewerPushFormat (petsc_viewer,
539 PETSC_VIEWER_ASCII_MATLAB));
542 LibmeshPetscCall(MatView (this->_mat, petsc_viewer));
548 #if PETSC_VERSION_LESS_THAN(3,7,0) 549 LibmeshPetscCall(PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
550 PETSC_VIEWER_ASCII_MATLAB));
552 LibmeshPetscCall(PetscViewerPushFormat (PETSC_VIEWER_STDOUT_WORLD,
553 PETSC_VIEWER_ASCII_MATLAB));
556 LibmeshPetscCall(MatView (this->_mat, PETSC_VIEWER_STDOUT_WORLD));
564 template <
typename T>
581 libmesh_deprecated();
582 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::print_personal().\n" 583 "Please update your code, as this warning will become an error in a future release.");
588 if (os.rdbuf() == std::cout.rdbuf())
589 LibmeshPetscCall(MatView(this->_mat, NULL));
596 std::string temp_filename;
600 char c[] =
"temp_petsc_matrix.XXXXXX";
605 if (this->processor_id() == 0)
610 libmesh_error_msg_if(fd == -1,
"mkstemp failed in PetscMatrix::print_personal()");
622 this->comm().broadcast(temp_filename);
625 PetscViewer petsc_viewer;
631 LibmeshPetscCall(PetscViewerASCIIOpen( this->comm().
get(),
632 temp_filename.c_str(),
641 LibmeshPetscCall(MatView (this->_mat, petsc_viewer));
643 if (this->processor_id() == 0)
647 std::ifstream input_stream(temp_filename.c_str());
648 os << input_stream.rdbuf();
652 input_stream.close();
653 std::remove(temp_filename.c_str());
660 template <
typename T>
662 PetscViewerType viewertype,
663 PetscFileMode filemode)
665 parallel_object_only();
671 LibmeshPetscCall(MatCreate(this->comm().
get(), &this->_mat));
676 LibmeshPetscCall(PetscViewerCreate(this->comm().
get(), &viewer));
677 LibmeshPetscCall(PetscViewerSetType(viewer, viewertype));
678 LibmeshPetscCall(PetscViewerSetFromOptions(viewer));
679 LibmeshPetscCall(PetscViewerFileSetMode(viewer, filemode));
680 LibmeshPetscCall(PetscViewerFileSetName(viewer, filename.c_str()));
681 if (filemode == FILE_MODE_READ)
682 LibmeshPetscCall(MatLoad(this->_mat, viewer));
684 LibmeshPetscCall(MatView(this->_mat, viewer));
685 LibmeshPetscCall(PetscViewerDestroy(&viewer));
690 template <
typename T>
695 this->_petsc_viewer(filename, PETSCVIEWERBINARY, FILE_MODE_WRITE);
700 template <
typename T>
705 this->_petsc_viewer(filename, PETSCVIEWERHDF5, FILE_MODE_WRITE);
710 template <
typename T>
713 LOG_SCOPE(
"read_petsc_binary()",
"PetscMatrix");
715 this->_petsc_viewer(filename, PETSCVIEWERBINARY, FILE_MODE_READ);
720 template <
typename T>
723 LOG_SCOPE(
"read_petsc_hdf5()",
"PetscMatrix");
725 this->_petsc_viewer(filename, PETSCVIEWERHDF5, FILE_MODE_READ);
730 template <
typename T>
732 const std::vector<numeric_index_type> & rows,
733 const std::vector<numeric_index_type> & cols)
740 libmesh_assert_equal_to (rows.size(), n_rows);
741 libmesh_assert_equal_to (cols.size(), n_cols);
743 std::scoped_lock lock(this->_petsc_matrix_mutex);
744 LibmeshPetscCall(MatSetValues(this->_mat,
756 template <
typename T>
758 const std::vector<numeric_index_type> & brows,
759 const std::vector<numeric_index_type> & bcols)
764 cast_int<numeric_index_type>(brows.size());
766 cast_int<numeric_index_type>(bcols.size());
770 cast_int<numeric_index_type>(dm.
m());
772 cast_int<numeric_index_type>(dm.
n());
775 libmesh_assert_equal_to (n_cols / n_bcols, blocksize);
776 libmesh_assert_equal_to (blocksize*n_brows, n_rows);
777 libmesh_assert_equal_to (blocksize*n_bcols, n_cols);
779 PetscInt petsc_blocksize;
780 LibmeshPetscCall(MatGetBlockSize(this->_mat, &petsc_blocksize));
781 libmesh_assert_equal_to (blocksize, static_cast<numeric_index_type>(petsc_blocksize));
784 std::scoped_lock lock(this->_petsc_matrix_mutex);
786 LibmeshPetscCall(MatSetValuesBlocked(this->_mat,
797 template <
typename T>
799 const std::vector<numeric_index_type> & rows,
800 const std::vector<numeric_index_type> & cols,
801 const bool reuse_submatrix)
const 805 libmesh_deprecated();
806 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::create_submatrix().\n" 807 "Please update your code, as this warning will become an error in a future release.");
814 PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
823 LibmeshPetscCall(ISCreateGeneral(this->comm().
get(),
824 cast_int<PetscInt>(rows.size()),
830 LibmeshPetscCall(ISCreateGeneral(this->comm().
get(),
831 cast_int<PetscInt>(cols.size()),
837 LibmeshPetscCall(LibMeshCreateSubMatrix(this->_mat,
840 (reuse_submatrix ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX),
841 (&petsc_submatrix->
_mat)));
845 petsc_submatrix->
close();
848 template <
typename T>
850 const std::vector<numeric_index_type> & rows,
851 const std::vector<numeric_index_type> & cols)
const 855 libmesh_deprecated();
856 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::create_submatrix_nosort().\n" 857 "Please update your code, as this warning will become an error in a future release.");
862 PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
864 LibmeshPetscCall(MatZeroEntries(petsc_submatrix->
_mat));
866 PetscInt pc_ncols = 0;
867 const PetscInt * pc_cols;
868 const PetscScalar * pc_vals;
871 std::vector<PetscInt> sub_cols;
872 std::vector<PetscScalar> sub_vals;
876 PetscInt sub_rid[] = {
static_cast<PetscInt
>(i)};
877 PetscInt rid =
static_cast<PetscInt
>(rows[i]);
879 if (rows[i]>= this->row_start() && rows[i]< this->row_stop())
882 LibmeshPetscCall(MatGetRow(this->_mat, rid, &pc_ncols, &pc_cols, &pc_vals));
886 for (
unsigned int idx = 0; idx< static_cast<unsigned int>(pc_ncols);
idx++)
888 if (pc_cols[
idx] == static_cast<PetscInt>(cols[j]))
890 sub_cols.push_back(static_cast<PetscInt>(j));
891 sub_vals.push_back(pc_vals[
idx]);
896 LibmeshPetscCall(MatSetValues(petsc_submatrix->
_mat,
899 static_cast<PetscInt>(sub_vals.size()),
903 LibmeshPetscCall(MatRestoreRow(this->_mat, rid, &pc_ncols, &pc_cols, &pc_vals));
909 MatAssemblyBeginEnd(petsc_submatrix->
comm(), petsc_submatrix->
_mat, MAT_FINAL_ASSEMBLY);
912 petsc_submatrix->
close();
916 template <
typename T>
923 LibmeshPetscCall(MatGetDiagonal(
const_cast<PetscMatrix<T> *
>(
this)->mat(),petsc_dest.
vec()));
928 template <
typename T>
936 if (&petsc_dest !=
this)
939 if (&petsc_dest ==
this)
942 #if PETSC_VERSION_LESS_THAN(3,7,0) 943 LibmeshPetscCall(MatTranspose(this->_mat,MAT_REUSE_MATRIX, &petsc_dest.
_mat));
945 LibmeshPetscCall(MatTranspose(this->_mat, MAT_INPLACE_MATRIX, &petsc_dest.
_mat));
948 LibmeshPetscCall(MatTranspose(this->_mat,MAT_INITIAL_MATRIX, &petsc_dest.
_mat));
957 template <
typename T>
962 MatAssemblyBeginEnd(this->comm(), this->_mat, MAT_FLUSH_ASSEMBLY);
967 template <
typename T>
974 PetscInt i_val=i, j_val=j;
976 PetscScalar petsc_value =
static_cast<PetscScalar
>(
value);
977 std::scoped_lock lock(this->_petsc_matrix_mutex);
978 LibmeshPetscCall(MatSetValues(this->_mat, 1, &i_val, 1, &j_val,
979 &petsc_value, INSERT_VALUES));
984 template <
typename T>
991 PetscInt i_val=i, j_val=j;
993 PetscScalar petsc_value =
static_cast<PetscScalar
>(
value);
994 std::scoped_lock lock(this->_petsc_matrix_mutex);
995 LibmeshPetscCall(MatSetValues(this->_mat, 1, &i_val, 1, &j_val,
996 &petsc_value, ADD_VALUES));
1001 template <
typename T>
1003 const std::vector<numeric_index_type> & dof_indices)
1005 this->add_matrix (dm, dof_indices, dof_indices);
1014 template <
typename T>
1021 libmesh_assert_equal_to (this->m(), X_in.
m());
1022 libmesh_assert_equal_to (this->n(), X_in.
n());
1024 PetscScalar a =
static_cast<PetscScalar
> (a_in);
1025 const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1032 semiparallel_only();
1034 LibmeshPetscCall(MatAXPY(this->_mat, a, X->
_mat, DIFFERENT_NONZERO_PATTERN));
1038 template <
typename T>
1045 libmesh_assert_equal_to (this->n(), X_in.
m());
1047 const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1053 semiparallel_only();
1056 LibmeshPetscCall(MatMatMult(this->_mat, X->_mat, MAT_REUSE_MATRIX, PETSC_DEFAULT, &Y->_mat));
1060 LibmeshPetscCall(MatMatMult(this->_mat, X->_mat, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &Y->_mat));
1064 Y->_is_initialized =
true;
1067 template <
typename T>
1070 const std::map<numeric_index_type, numeric_index_type> & row_ltog,
1071 const std::map<numeric_index_type, numeric_index_type> & col_ltog,
1076 libmesh_assert_greater_equal(spm.
m(), row_ltog.size());
1077 libmesh_assert_greater_equal(spm.
n(), col_ltog.size());
1080 libmesh_assert_greater_equal(this->m(), spm.
m());
1081 libmesh_assert_greater_equal(this->n(), spm.
n());
1086 auto pscm = cast_ptr<const PetscMatrix<T> *>(&spm);
1090 const PetscInt * lcols;
1091 const PetscScalar * vals;
1093 std::vector<PetscInt> gcols;
1094 std::vector<PetscScalar> values;
1096 for (
auto ltog : row_ltog)
1098 PetscInt grow[] = {
static_cast<PetscInt
>(ltog.second)};
1100 LibmeshPetscCall(MatGetRow(pscm->_mat, static_cast<PetscInt>(ltog.first), &ncols, &lcols, &vals));
1103 gcols.resize(ncols);
1104 values.resize(ncols);
1107 gcols[i] = libmesh_map_find(col_ltog, lcols[i]);
1108 values[i] =
PS(scalar) * vals[i];
1111 LibmeshPetscCall(MatSetValues(this->_mat, 1, grow, ncols, gcols.data(), values.data(), ADD_VALUES));
1112 LibmeshPetscCall(MatRestoreRow(pscm->_mat, static_cast<PetscInt>(ltog.first), &ncols, &lcols, &vals));
1118 template <
typename T>
1128 i_val=
static_cast<PetscInt
>(i_in),
1129 j_val=static_cast<PetscInt>(j_in);
1138 LibmeshPetscCall(MatGetValue(this->_mat, i_val, j_val, &
value));
1143 template <
typename T>
1145 std::vector<numeric_index_type> & indices,
1146 std::vector<T> & values)
const 1150 const PetscScalar * petsc_row;
1151 const PetscInt * petsc_cols;
1155 i_val =
static_cast<PetscInt
>(i_in);
1173 std::lock_guard<std::mutex> lock(_petsc_matrix_mutex);
1175 LibmeshPetscCall(MatGetRow(this->_mat, i_val, &ncols, &petsc_cols, &petsc_row));
1178 indices.resize(static_cast<std::size_t>(ncols));
1179 values.resize(static_cast<std::size_t>(ncols));
1184 values[i] =
static_cast<T
>(petsc_row[i]);
1187 LibmeshPetscCall(MatRestoreRow(this->_mat, i_val,
1188 &ncols, &petsc_cols, &petsc_row));
1193 template <
typename T>
1196 semiparallel_only();
1200 PetscBool assembled;
1201 LibmeshPetscCall(MatAssembled(this->_mat, &assembled));
1203 const bool cxx_assembled = (assembled == PETSC_TRUE) ?
true :
false;
1212 LibmeshPetscCall(MatCopy(v.
_mat, this->_mat, DIFFERENT_NONZERO_PATTERN));
1215 LibmeshPetscCall(MatDuplicate(v.
_mat, MAT_COPY_VALUES, &this->_mat));
1222 template <
typename T>
1225 *
this = cast_ref<const PetscMatrix<T> &>(v);
1229 template <
typename T>
1234 LibmeshPetscCall(MatScale(this->_mat,
PS(
scale)));
1237 template <
typename T>
1240 #if PETSC_RELEASE_LESS_THAN(3,19,0) 1247 #if PETSC_RELEASE_GREATER_EQUALS(3,23,0) 1248 template <
typename T>
1249 std::unique_ptr<PetscMatrix<T>>
1255 LibmeshPetscCall(MatDuplicate(this->_mat, MAT_DO_NOT_COPY_VALUES, &xaij));
1256 LibmeshPetscCall(MatCopyHashToXAIJ(this->_mat, xaij));
1257 return std::make_unique<PetscMatrix<T>>(xaij, this->comm(),
true);
1261 template <
typename T>
1265 semiparallel_only();
1267 if (this->_use_hash_table)
1268 #if PETSC_RELEASE_GREATER_EQUALS(3, 23, 0) 1270 LibmeshPetscCall(MatResetHash(this->_mat));
1272 libmesh_error_msg(
"Resetting hash tables not supported until PETSc version 3.23");
1275 this->reset_preallocation();
1285 #endif // #ifdef LIBMESH_HAVE_PETSC std::string name(const ElemQuality q)
This function returns a string containing some name for q.
virtual bool initialized() const
bool closed()
Checks that the library has been closed.
void reset_preallocation()
Reset matrix to use the original nonzero pattern provided by users.
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
This class provides a nice interface to PETSc's Vec object.
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 print_matlab(const std::string &name="") const override
Print the contents of the matrix in Matlab's sparse matrix format.
This class provides a nice interface to the PETSc C-based data structures for parallel, sparse matrices.
void finish_initialization()
virtual void zero() override
Set all entries to 0.
virtual Real frobenius_norm() const
PetscMatrix(const Parallel::Communicator &comm_in)
Constructor; initializes the matrix to be empty, without any structure, i.e.
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_petsc_binary(const std::string &filename) override
Write the contents of the matrix to a file in PETSc's binary sparse matrix format.
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)
PetscMatrix & operator=(PetscMatrix &&)=delete
Provides a uniform interface to vector storage schemes for different linear algebra libraries...
const Parallel::Communicator & comm() const
void update_preallocation_and_zero()
Update the sparsity pattern based on dof_map, and set the matrix to zero.
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.
PetscScalar * pPS(T *ptr)
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 read_petsc_binary(const std::string &filename) override
Read the contents of the matrix from a file in PETSc's binary sparse matrix format.
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const override
The libMesh namespace provides an interface to certain functionality in the library.
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 std::unique_ptr< SparseMatrix< T > > clone() const override
virtual void clear()=0
Restores the SparseMatrix<T> to a pristine state.
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
void libmesh_ignore(const Args &...)
dof_id_type numeric_index_type
bool _is_initialized
Flag that tells if init() has been called.
bool _is_initialized
Flag indicating whether or not the matrix has been initialized.
virtual numeric_index_type m() const =0
std::unique_ptr< PetscMatrix< T > > copy_from_hash()
Creates a copy of the current hash table matrix and then performs assembly.
virtual void close() override
Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across p...
virtual bool supports_hash_table() const override
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 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...
std::vector< T > & get_values()
Mat _mat
PETSc matrix datatype to store values.
void _petsc_viewer(const std::string &filename, PetscViewerType viewertype, PetscFileMode filemode)
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(const numeric_index_type i, const numeric_index_type j, const T value) override
Add value to the element (i,j).
virtual void get_diagonal(NumericVector< T > &dest) const override
Copies the diagonal part of the matrix into dest.
PetscMatrixType _mat_type
virtual bool closed() const override
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void print_petsc_hdf5(const std::string &filename) override
Write the contents of the matrix to a file in PETSc's HDF5 sparse matrix format.
virtual void get_transpose(SparseMatrix< T > &dest) const override
Copies the transpose of the matrix into dest, which may be *this.
virtual Real l1_norm() const override
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 set(const numeric_index_type i, const numeric_index_type j, const T value) override
Set the element (i,j) to value.
This class provides a nice interface to the PETSc C-based AIJ data structures for parallel...
virtual void scale(const T scale) override
Scales all elements of this matrix by scale.
bool initialized()
Checks that library initialization has been done.
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 restore_original_nonzero_pattern() override
Reset the memory storage of 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.
Defines a dense matrix for use in Finite Element-type computations.
void finish_initialization()
Finish up the initialization process.
virtual void flush() override
For PETSc matrix , this function is similar to close but without shrinking memory.
auto index_range(const T &sizable)
Helper function that returns an IntRange<std::size_t> representing all the indices of the passed-in v...
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.
virtual Real linfty_norm() const override
virtual numeric_index_type n() const =0
ParallelType
Defines an enum for parallel data structure types.