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>
697 nonconst_this->
_petsc_viewer(filename, PETSCVIEWERBINARY, FILE_MODE_WRITE);
702 template <
typename T>
709 nonconst_this->
_petsc_viewer(filename, PETSCVIEWERHDF5, FILE_MODE_WRITE);
714 template <
typename T>
717 LOG_SCOPE(
"read_petsc_binary()",
"PetscMatrix");
719 this->_petsc_viewer(filename, PETSCVIEWERBINARY, FILE_MODE_READ);
724 template <
typename T>
727 LOG_SCOPE(
"read_petsc_hdf5()",
"PetscMatrix");
729 this->_petsc_viewer(filename, PETSCVIEWERHDF5, FILE_MODE_READ);
734 template <
typename T>
736 const std::vector<numeric_index_type> & rows,
737 const std::vector<numeric_index_type> & cols)
744 libmesh_assert_equal_to (rows.size(), n_rows);
745 libmesh_assert_equal_to (cols.size(), n_cols);
747 std::scoped_lock lock(this->_petsc_matrix_mutex);
748 LibmeshPetscCall(MatSetValues(this->_mat,
760 template <
typename T>
762 const std::vector<numeric_index_type> & brows,
763 const std::vector<numeric_index_type> & bcols)
768 cast_int<numeric_index_type>(brows.size());
770 cast_int<numeric_index_type>(bcols.size());
774 cast_int<numeric_index_type>(dm.
m());
776 cast_int<numeric_index_type>(dm.
n());
779 libmesh_assert_equal_to (n_cols / n_bcols, blocksize);
780 libmesh_assert_equal_to (blocksize*n_brows, n_rows);
781 libmesh_assert_equal_to (blocksize*n_bcols, n_cols);
783 PetscInt petsc_blocksize;
784 LibmeshPetscCall(MatGetBlockSize(this->_mat, &petsc_blocksize));
785 libmesh_assert_equal_to (blocksize, static_cast<numeric_index_type>(petsc_blocksize));
788 std::scoped_lock lock(this->_petsc_matrix_mutex);
790 LibmeshPetscCall(MatSetValuesBlocked(this->_mat,
801 template <
typename T>
803 const std::vector<numeric_index_type> & rows,
804 const std::vector<numeric_index_type> & cols,
805 const bool reuse_submatrix)
const 809 libmesh_deprecated();
810 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::create_submatrix().\n" 811 "Please update your code, as this warning will become an error in a future release.");
818 PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
827 LibmeshPetscCall(ISCreateGeneral(this->comm().
get(),
828 cast_int<PetscInt>(rows.size()),
834 LibmeshPetscCall(ISCreateGeneral(this->comm().
get(),
835 cast_int<PetscInt>(cols.size()),
841 LibmeshPetscCall(LibMeshCreateSubMatrix(this->_mat,
844 (reuse_submatrix ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX),
845 (&petsc_submatrix->
_mat)));
849 petsc_submatrix->
close();
852 template <
typename T>
854 const std::vector<numeric_index_type> & rows,
855 const std::vector<numeric_index_type> & cols)
const 859 libmesh_deprecated();
860 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::create_submatrix_nosort().\n" 861 "Please update your code, as this warning will become an error in a future release.");
866 PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
868 LibmeshPetscCall(MatZeroEntries(petsc_submatrix->
_mat));
870 PetscInt pc_ncols = 0;
871 const PetscInt * pc_cols;
872 const PetscScalar * pc_vals;
875 std::vector<PetscInt> sub_cols;
876 std::vector<PetscScalar> sub_vals;
880 PetscInt sub_rid[] = {
static_cast<PetscInt
>(i)};
881 PetscInt rid =
static_cast<PetscInt
>(rows[i]);
883 if (rows[i]>= this->row_start() && rows[i]< this->row_stop())
886 LibmeshPetscCall(MatGetRow(this->_mat, rid, &pc_ncols, &pc_cols, &pc_vals));
890 for (
unsigned int idx = 0; idx< static_cast<unsigned int>(pc_ncols);
idx++)
892 if (pc_cols[
idx] == static_cast<PetscInt>(cols[j]))
894 sub_cols.push_back(static_cast<PetscInt>(j));
895 sub_vals.push_back(pc_vals[
idx]);
900 LibmeshPetscCall(MatSetValues(petsc_submatrix->
_mat,
903 static_cast<PetscInt>(sub_vals.size()),
907 LibmeshPetscCall(MatRestoreRow(this->_mat, rid, &pc_ncols, &pc_cols, &pc_vals));
913 MatAssemblyBeginEnd(petsc_submatrix->
comm(), petsc_submatrix->
_mat, MAT_FINAL_ASSEMBLY);
916 petsc_submatrix->
close();
920 template <
typename T>
927 LibmeshPetscCall(MatGetDiagonal(
const_cast<PetscMatrix<T> *
>(
this)->mat(),petsc_dest.
vec()));
932 template <
typename T>
940 if (&petsc_dest !=
this)
943 if (&petsc_dest ==
this)
946 #if PETSC_VERSION_LESS_THAN(3,7,0) 947 LibmeshPetscCall(MatTranspose(this->_mat,MAT_REUSE_MATRIX, &petsc_dest.
_mat));
949 LibmeshPetscCall(MatTranspose(this->_mat, MAT_INPLACE_MATRIX, &petsc_dest.
_mat));
952 LibmeshPetscCall(MatTranspose(this->_mat,MAT_INITIAL_MATRIX, &petsc_dest.
_mat));
961 template <
typename T>
966 MatAssemblyBeginEnd(this->comm(), this->_mat, MAT_FLUSH_ASSEMBLY);
971 template <
typename T>
978 PetscInt i_val=i, j_val=j;
980 PetscScalar petsc_value =
static_cast<PetscScalar
>(
value);
981 std::scoped_lock lock(this->_petsc_matrix_mutex);
982 LibmeshPetscCall(MatSetValues(this->_mat, 1, &i_val, 1, &j_val,
983 &petsc_value, INSERT_VALUES));
988 template <
typename T>
995 PetscInt i_val=i, j_val=j;
997 PetscScalar petsc_value =
static_cast<PetscScalar
>(
value);
998 std::scoped_lock lock(this->_petsc_matrix_mutex);
999 LibmeshPetscCall(MatSetValues(this->_mat, 1, &i_val, 1, &j_val,
1000 &petsc_value, ADD_VALUES));
1005 template <
typename T>
1007 const std::vector<numeric_index_type> & dof_indices)
1009 this->add_matrix (dm, dof_indices, dof_indices);
1018 template <
typename T>
1025 libmesh_assert_equal_to (this->m(), X_in.
m());
1026 libmesh_assert_equal_to (this->n(), X_in.
n());
1028 PetscScalar a =
static_cast<PetscScalar
> (a_in);
1029 const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1036 semiparallel_only();
1038 LibmeshPetscCall(MatAXPY(this->_mat, a, X->
_mat, DIFFERENT_NONZERO_PATTERN));
1042 template <
typename T>
1049 libmesh_assert_equal_to (this->n(), X_in.
m());
1051 const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1057 semiparallel_only();
1060 LibmeshPetscCall(MatMatMult(this->_mat, X->_mat, MAT_REUSE_MATRIX, PETSC_DEFAULT, &Y->_mat));
1064 LibmeshPetscCall(MatMatMult(this->_mat, X->_mat, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &Y->_mat));
1068 Y->_is_initialized =
true;
1071 template <
typename T>
1074 const std::map<numeric_index_type, numeric_index_type> & row_ltog,
1075 const std::map<numeric_index_type, numeric_index_type> & col_ltog,
1080 libmesh_assert_greater_equal(spm.
m(), row_ltog.size());
1081 libmesh_assert_greater_equal(spm.
n(), col_ltog.size());
1084 libmesh_assert_greater_equal(this->m(), spm.
m());
1085 libmesh_assert_greater_equal(this->n(), spm.
n());
1090 auto pscm = cast_ptr<const PetscMatrix<T> *>(&spm);
1094 const PetscInt * lcols;
1095 const PetscScalar * vals;
1097 std::vector<PetscInt> gcols;
1098 std::vector<PetscScalar> values;
1100 for (
auto ltog : row_ltog)
1102 PetscInt grow[] = {
static_cast<PetscInt
>(ltog.second)};
1104 LibmeshPetscCall(MatGetRow(pscm->_mat, static_cast<PetscInt>(ltog.first), &ncols, &lcols, &vals));
1107 gcols.resize(ncols);
1108 values.resize(ncols);
1111 gcols[i] = libmesh_map_find(col_ltog, lcols[i]);
1112 values[i] =
PS(scalar) * vals[i];
1115 LibmeshPetscCall(MatSetValues(this->_mat, 1, grow, ncols, gcols.data(), values.data(), ADD_VALUES));
1116 LibmeshPetscCall(MatRestoreRow(pscm->_mat, static_cast<PetscInt>(ltog.first), &ncols, &lcols, &vals));
1122 template <
typename T>
1132 i_val=
static_cast<PetscInt
>(i_in),
1133 j_val=static_cast<PetscInt>(j_in);
1142 LibmeshPetscCall(MatGetValue(this->_mat, i_val, j_val, &
value));
1147 template <
typename T>
1149 std::vector<numeric_index_type> & indices,
1150 std::vector<T> & values)
const 1154 const PetscScalar * petsc_row;
1155 const PetscInt * petsc_cols;
1159 i_val =
static_cast<PetscInt
>(i_in);
1177 std::lock_guard<std::mutex> lock(_petsc_matrix_mutex);
1179 LibmeshPetscCall(MatGetRow(this->_mat, i_val, &ncols, &petsc_cols, &petsc_row));
1182 indices.resize(static_cast<std::size_t>(ncols));
1183 values.resize(static_cast<std::size_t>(ncols));
1188 values[i] =
static_cast<T
>(petsc_row[i]);
1191 LibmeshPetscCall(MatRestoreRow(this->_mat, i_val,
1192 &ncols, &petsc_cols, &petsc_row));
1197 template <
typename T>
1200 semiparallel_only();
1204 PetscBool assembled;
1205 LibmeshPetscCall(MatAssembled(this->_mat, &assembled));
1207 const bool cxx_assembled = (assembled == PETSC_TRUE) ?
true :
false;
1216 LibmeshPetscCall(MatCopy(v.
_mat, this->_mat, DIFFERENT_NONZERO_PATTERN));
1219 LibmeshPetscCall(MatDuplicate(v.
_mat, MAT_COPY_VALUES, &this->_mat));
1226 template <
typename T>
1229 *
this = cast_ref<const PetscMatrix<T> &>(v);
1233 template <
typename T>
1238 LibmeshPetscCall(MatScale(this->_mat,
PS(
scale)));
1241 template <
typename T>
1244 #if PETSC_RELEASE_LESS_THAN(3,19,0) 1251 #if PETSC_RELEASE_GREATER_EQUALS(3,23,0) 1252 template <
typename T>
1253 std::unique_ptr<PetscMatrix<T>>
1259 LibmeshPetscCall(MatDuplicate(this->_mat, MAT_DO_NOT_COPY_VALUES, &xaij));
1260 LibmeshPetscCall(MatCopyHashToXAIJ(this->_mat, xaij));
1261 return std::make_unique<PetscMatrix<T>>(xaij, this->comm(),
true);
1265 template <
typename T>
1269 semiparallel_only();
1271 if (this->_use_hash_table)
1272 #if PETSC_RELEASE_GREATER_EQUALS(3, 23, 0) 1274 LibmeshPetscCall(MatResetHash(this->_mat));
1276 libmesh_error_msg(
"Resetting hash tables not supported until PETSc version 3.23");
1279 this->reset_preallocation();
1289 #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.
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.
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 &...)
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.
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 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 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 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.