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.");
525 LibmeshPetscCall(PetscViewerCreate (this->comm().
get(),
526 petsc_viewer.
get()));
532 LibmeshPetscCall(PetscViewerASCIIOpen( this->comm().
get(),
534 petsc_viewer.
get()));
536 #if PETSC_VERSION_LESS_THAN(3,7,0) 537 LibmeshPetscCall(PetscViewerSetFormat (petsc_viewer,
538 PETSC_VIEWER_ASCII_MATLAB));
540 LibmeshPetscCall(PetscViewerPushFormat (petsc_viewer,
541 PETSC_VIEWER_ASCII_MATLAB));
544 LibmeshPetscCall(MatView (this->_mat, petsc_viewer));
550 #if PETSC_VERSION_LESS_THAN(3,7,0) 551 LibmeshPetscCall(PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
552 PETSC_VIEWER_ASCII_MATLAB));
554 LibmeshPetscCall(PetscViewerPushFormat (PETSC_VIEWER_STDOUT_WORLD,
555 PETSC_VIEWER_ASCII_MATLAB));
558 LibmeshPetscCall(MatView (this->_mat, PETSC_VIEWER_STDOUT_WORLD));
566 template <
typename T>
583 libmesh_deprecated();
584 libmesh_warning(
"The matrix must be assembled before calling PetscMatrix::print_personal().\n" 585 "Please update your code, as this warning will become an error in a future release.");
590 if (os.rdbuf() == std::cout.rdbuf())
591 LibmeshPetscCall(MatView(this->_mat, NULL));
598 std::string temp_filename;
602 char c[] =
"temp_petsc_matrix.XXXXXX";
607 if (this->processor_id() == 0)
612 libmesh_error_msg_if(fd == -1,
"mkstemp failed in PetscMatrix::print_personal()");
624 this->comm().broadcast(temp_filename);
627 PetscViewer petsc_viewer;
633 LibmeshPetscCall(PetscViewerASCIIOpen( this->comm().
get(),
634 temp_filename.c_str(),
643 LibmeshPetscCall(MatView (this->_mat, petsc_viewer));
645 if (this->processor_id() == 0)
649 std::ifstream input_stream(temp_filename.c_str());
650 os << input_stream.rdbuf();
654 input_stream.close();
655 std::remove(temp_filename.c_str());
662 template <
typename T>
664 PetscViewerType viewertype,
665 PetscFileMode filemode)
667 parallel_object_only();
673 LibmeshPetscCall(MatCreate(this->comm().
get(), &this->_mat));
678 LibmeshPetscCall(PetscViewerCreate(this->comm().
get(), &viewer));
679 LibmeshPetscCall(PetscViewerSetType(viewer, viewertype));
680 LibmeshPetscCall(PetscViewerSetFromOptions(viewer));
681 LibmeshPetscCall(PetscViewerFileSetMode(viewer, filemode));
682 LibmeshPetscCall(PetscViewerFileSetName(viewer, filename.c_str()));
683 if (filemode == FILE_MODE_READ)
684 LibmeshPetscCall(MatLoad(this->_mat, viewer));
686 LibmeshPetscCall(MatView(this->_mat, viewer));
687 LibmeshPetscCall(PetscViewerDestroy(&viewer));
692 template <
typename T>
697 this->_petsc_viewer(filename, PETSCVIEWERBINARY, FILE_MODE_WRITE);
702 template <
typename T>
707 this->_petsc_viewer(filename, PETSCVIEWERHDF5, FILE_MODE_WRITE);
712 template <
typename T>
715 LOG_SCOPE(
"read_petsc_binary()",
"PetscMatrix");
717 this->_petsc_viewer(filename, PETSCVIEWERBINARY, FILE_MODE_READ);
722 template <
typename T>
725 LOG_SCOPE(
"read_petsc_hdf5()",
"PetscMatrix");
727 this->_petsc_viewer(filename, PETSCVIEWERHDF5, FILE_MODE_READ);
732 template <
typename T>
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,
758 template <
typename T>
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,
799 template <
typename T>
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();
850 template <
typename T>
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();
918 template <
typename T>
925 LibmeshPetscCall(MatGetDiagonal(
const_cast<PetscMatrix<T> *
>(
this)->mat(),petsc_dest.
vec()));
930 template <
typename T>
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));
959 template <
typename T>
964 MatAssemblyBeginEnd(this->comm(), this->_mat, MAT_FLUSH_ASSEMBLY);
969 template <
typename T>
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));
986 template <
typename T>
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));
1003 template <
typename T>
1005 const std::vector<numeric_index_type> & dof_indices)
1007 this->add_matrix (dm, dof_indices, dof_indices);
1016 template <
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));
1040 template <
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;
1069 template <
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));
1120 template <
typename T>
1127 const PetscScalar * petsc_row;
1128 const PetscInt * petsc_cols;
1135 i_val=
static_cast<PetscInt
>(i_in),
1136 j_val=static_cast<PetscInt>(j_in);
1145 LibmeshPetscCall(MatGetRow(this->_mat, i_val, &ncols, &petsc_cols, &petsc_row));
1149 std::pair<const PetscInt *, const PetscInt *> p =
1150 std::equal_range (petsc_cols, petsc_cols + ncols, j_val);
1153 if (p.first != p.second)
1157 const std::size_t j =
1159 const_cast<PetscInt *>(p.first));
1161 libmesh_assert_less (j, ncols);
1162 libmesh_assert_equal_to (petsc_cols[j], j_val);
1164 value =
static_cast<T
> (petsc_row[j]);
1167 LibmeshPetscCall(MatRestoreRow(this->_mat, i_val,
1168 &ncols, &petsc_cols, &petsc_row));
1173 template <
typename T>
1175 std::vector<numeric_index_type> & indices,
1176 std::vector<T> & values)
const 1180 const PetscScalar * petsc_row;
1181 const PetscInt * petsc_cols;
1185 i_val =
static_cast<PetscInt
>(i_in);
1203 std::lock_guard<std::mutex> lock(_petsc_matrix_mutex);
1205 LibmeshPetscCall(MatGetRow(this->_mat, i_val, &ncols, &petsc_cols, &petsc_row));
1208 indices.resize(static_cast<std::size_t>(ncols));
1209 values.resize(static_cast<std::size_t>(ncols));
1214 values[i] =
static_cast<T
>(petsc_row[i]);
1217 LibmeshPetscCall(MatRestoreRow(this->_mat, i_val,
1218 &ncols, &petsc_cols, &petsc_row));
1223 template <
typename T>
1226 semiparallel_only();
1230 PetscBool assembled;
1231 LibmeshPetscCall(MatAssembled(this->_mat, &assembled));
1233 const bool cxx_assembled = (assembled == PETSC_TRUE) ?
true :
false;
1242 LibmeshPetscCall(MatCopy(v.
_mat, this->_mat, DIFFERENT_NONZERO_PATTERN));
1245 LibmeshPetscCall(MatDuplicate(v.
_mat, MAT_COPY_VALUES, &this->_mat));
1252 template <
typename T>
1255 *
this = cast_ref<const PetscMatrix<T> &>(v);
1259 template <
typename T>
1264 LibmeshPetscCall(MatScale(this->_mat,
PS(
scale)));
1267 template <
typename T>
1270 #if PETSC_RELEASE_LESS_THAN(3,19,0) 1277 #if PETSC_RELEASE_GREATER_EQUALS(3,23,0) 1278 template <
typename T>
1279 std::unique_ptr<PetscMatrix<T>>
1285 LibmeshPetscCall(MatDuplicate(this->_mat, MAT_DO_NOT_COPY_VALUES, &xaij));
1286 LibmeshPetscCall(MatCopyHashToXAIJ(this->_mat, xaij));
1287 return std::make_unique<PetscMatrix<T>>(xaij, this->comm(),
true);
1291 template <
typename T>
1295 semiparallel_only();
1297 if (this->_use_hash_table)
1298 #if PETSC_RELEASE_GREATER_EQUALS(3, 23, 0) 1300 LibmeshPetscCall(MatResetHash(this->_mat));
1302 libmesh_error_msg(
"Resetting hash tables not supported until PETSc version 3.23");
1305 this->reset_preallocation();
1315 #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...
Real distance(const Point &p)
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.