libMesh::PetscMatrix< T > Class Template Reference

SparseMatrix interface to PETSc Mat. More...

#include <petsc_linear_solver.h>

Inheritance diagram for libMesh::PetscMatrix< T >:

Public Member Functions

 PetscMatrix (const Parallel::Communicator &comm_in)
 
 PetscMatrix (Mat m, const Parallel::Communicator &comm_in)
 
 PetscMatrix (PetscMatrix &&)=delete
 
 PetscMatrix (const PetscMatrix &)=delete
 
PetscMatrixoperator= (const PetscMatrix &)=delete
 
PetscMatrixoperator= (PetscMatrix &&)=delete
 
virtual ~PetscMatrix ()
 
void set_matrix_type (PetscMatrixType mat_type)
 
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 nnz=30, const numeric_index_type noz=10, const numeric_index_type blocksize=1) override
 
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 std::vector< numeric_index_type > &n_nz, const std::vector< numeric_index_type > &n_oz, const numeric_index_type blocksize=1)
 
virtual void init () override
 
void update_preallocation_and_zero ()
 
virtual void clear () override
 
virtual void zero () override
 
virtual void zero_rows (std::vector< numeric_index_type > &rows, T diag_value=0.0) override
 
virtual void close () override
 
virtual void flush () override
 
virtual numeric_index_type m () const override
 
virtual numeric_index_type n () const override
 
virtual numeric_index_type row_start () const override
 
virtual numeric_index_type row_stop () const override
 
virtual void set (const numeric_index_type i, const numeric_index_type j, const T value) override
 
virtual void add (const numeric_index_type i, const numeric_index_type j, const T value) override
 
virtual void add_matrix (const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) override
 
virtual void add_matrix (const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &dof_indices) override
 
virtual void add_block_matrix (const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &brows, const std::vector< numeric_index_type > &bcols) override
 
virtual void add_block_matrix (const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &dof_indices) override
 
virtual void add (const T a, const SparseMatrix< T > &X) override
 
virtual T operator() (const numeric_index_type i, const numeric_index_type j) const override
 
virtual Real l1_norm () const override
 
virtual Real linfty_norm () const override
 
virtual bool closed () const override
 
virtual void print_personal (std::ostream &os=libMesh::out) const override
 
virtual void print_matlab (const std::string &name="") const override
 
virtual void get_diagonal (NumericVector< T > &dest) const override
 
virtual void get_transpose (SparseMatrix< T > &dest) const override
 
void swap (PetscMatrix< T > &)
 
Mat mat ()
 
virtual bool initialized () const
 
void attach_dof_map (const DofMap &dof_map)
 
virtual bool need_full_sparsity_pattern () const
 
virtual void update_sparsity_pattern (const SparsityPattern::Graph &)
 
void print (std::ostream &os=libMesh::out, const bool sparse=false) const
 
template<>
void print (std::ostream &os, const bool sparse) const
 
virtual void create_submatrix (SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
 
virtual void reinit_submatrix (SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
 
void vector_mult (NumericVector< T > &dest, const NumericVector< T > &arg) const
 
void vector_mult_add (NumericVector< T > &dest, const NumericVector< T > &arg) const
 
const Parallel::Communicatorcomm () const
 
processor_id_type n_processors () const
 
processor_id_type processor_id () const
 

Static Public Member Functions

static std::unique_ptr< SparseMatrix< T > > build (const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
 
static std::string get_info ()
 
static void print_info (std::ostream &out=libMesh::out)
 
static unsigned int n_objects ()
 
static void enable_print_counter_info ()
 
static void disable_print_counter_info ()
 

Protected Types

typedef std::map< std::string, std::pair< unsigned int, unsigned int > > Counts
 

Protected Member Functions

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
 
void increment_constructor_count (const std::string &name)
 
void increment_destructor_count (const std::string &name)
 

Protected Attributes

DofMap const * _dof_map
 
bool _is_initialized
 
const Parallel::Communicator_communicator
 

Static Protected Attributes

static Counts _counts
 
static Threads::atomic< unsigned int > _n_objects
 
static Threads::spin_mutex _mutex
 
static bool _enable_print_counter = true
 

Private Attributes

Mat _mat
 
bool _destroy_mat_on_exit
 
PetscMatrixType _mat_type
 

Detailed Description

template<typename T>
class libMesh::PetscMatrix< T >

SparseMatrix interface to PETSc Mat.

This class provides a nice interface to the PETSc C-based data structures for parallel, sparse matrices. All overridden virtual functions are documented in sparse_matrix.h.

Author
Benjamin S. Kirk
Date
2002

Definition at line 90 of file petsc_linear_solver.h.

Member Typedef Documentation

◆ Counts

typedef std::map<std::string, std::pair<unsigned int, unsigned int> > libMesh::ReferenceCounter::Counts
protectedinherited

Data structure to log the information. The log is identified by the class name.

Definition at line 117 of file reference_counter.h.

Constructor & Destructor Documentation

◆ PetscMatrix() [1/4]

template<typename T >
libMesh::PetscMatrix< T >::PetscMatrix ( const Parallel::Communicator comm_in)
explicit

Constructor; initializes the matrix to be empty, without any structure, i.e. the matrix is not usable at all. This constructor is therefore only useful for matrices which are members of a class. All other matrices should be created at a point in the data flow where all necessary information is available.

You have to initialize the matrix before usage with init(...).

Definition at line 90 of file petsc_matrix.C.

90  :
91  SparseMatrix<T>(comm_in),
93  _mat_type(AIJ)
94 {}
PetscMatrixType _mat_type
Definition: petsc_matrix.h:279

◆ PetscMatrix() [2/4]

template<typename T >
libMesh::PetscMatrix< T >::PetscMatrix ( Mat  m,
const Parallel::Communicator comm_in 
)
explicit

Constructor. Creates a PetscMatrix assuming you already have a valid Mat object. In this case, m is NOT destroyed by the PetscMatrix destructor when this object goes out of scope. This allows ownership of m to remain with the original creator, and to simply provide additional functionality with the PetscMatrix.

Definition at line 101 of file petsc_matrix.C.

References libMesh::SparseMatrix< T >::_is_initialized, and libMesh::PetscMatrix< T >::_mat.

102  :
103  SparseMatrix<T>(comm_in),
104  _destroy_mat_on_exit(false),
105  _mat_type(AIJ)
106 {
107  this->_mat = mat_in;
108  this->_is_initialized = true;
109 }
PetscMatrixType _mat_type
Definition: petsc_matrix.h:279

◆ PetscMatrix() [3/4]

template<typename T>
libMesh::PetscMatrix< T >::PetscMatrix ( PetscMatrix< T > &&  )
delete

This class manages a C-style struct (Mat) manually, so we don't want to allow any automatic copy/move functions to be generated, and we can't default the destructor.

◆ PetscMatrix() [4/4]

template<typename T>
libMesh::PetscMatrix< T >::PetscMatrix ( const PetscMatrix< T > &  )
delete

◆ ~PetscMatrix()

template<typename T >
libMesh::PetscMatrix< T >::~PetscMatrix ( )
virtual

Definition at line 115 of file petsc_matrix.C.

116 {
117  this->clear();
118 }
virtual void clear() override
Definition: petsc_matrix.C:528

Member Function Documentation

◆ _get_submatrix()

template<typename T >
void libMesh::PetscMatrix< T >::_get_submatrix ( SparseMatrix< T > &  submatrix,
const std::vector< numeric_index_type > &  rows,
const std::vector< numeric_index_type > &  cols,
const bool  reuse_submatrix 
) const
overrideprotectedvirtual

This function either creates or re-initializes a matrix called submatrix which is defined by the indices given in the rows and cols vectors.

This function is implemented in terms of MatGetSubMatrix(). The reuse_submatrix parameter determines whether or not PETSc will treat submatrix as one which has already been used (had memory allocated) or as a new matrix.

Reimplemented from libMesh::SparseMatrix< T >.

Definition at line 849 of file petsc_matrix.C.

References libMesh::SparseMatrix< T >::_is_initialized, libMesh::PetscMatrix< T >::_mat, libMesh::SparseMatrix< T >::clear(), libMesh::PetscMatrix< T >::close(), libMesh::closed(), ierr, libMesh::SparseMatrix< T >::initialized(), libMesh::numeric_petsc_cast(), and PETSC_USE_POINTER.

853 {
854  if (!this->closed())
855  {
856  libmesh_deprecated();
857  libmesh_warning("The matrix must be assembled before calling PetscMatrix::create_submatrix().\n"
858  "Please update your code, as this warning will become an error in a future release.");
859  const_cast<PetscMatrix<T> *>(this)->close();
860  }
861 
862  // Make sure the SparseMatrix passed in is really a PetscMatrix
863  PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
864 
865  // If we're not reusing submatrix and submatrix is already initialized
866  // then we need to clear it, otherwise we get a memory leak.
867  if (!reuse_submatrix && submatrix.initialized())
868  submatrix.clear();
869 
870  // Construct row and column index sets.
871  PetscErrorCode ierr=0;
872  IS isrow, iscol;
873 
874  ierr = ISCreateLibMesh(this->comm().get(),
875  cast_int<PetscInt>(rows.size()),
876  numeric_petsc_cast(rows.data()),
878  &isrow); LIBMESH_CHKERR(ierr);
879 
880  ierr = ISCreateLibMesh(this->comm().get(),
881  cast_int<PetscInt>(cols.size()),
882  numeric_petsc_cast(cols.data()),
884  &iscol); LIBMESH_CHKERR(ierr);
885 
886  // Extract submatrix
887  ierr = LibMeshCreateSubMatrix(_mat,
888  isrow,
889  iscol,
890 #if PETSC_RELEASE_LESS_THAN(3,0,1)
891  PETSC_DECIDE,
892 #endif
893  (reuse_submatrix ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX),
894  &(petsc_submatrix->_mat)); LIBMESH_CHKERR(ierr);
895 
896  // Specify that the new submatrix is initialized and close it.
897  petsc_submatrix->_is_initialized = true;
898  petsc_submatrix->close();
899 
900  // Clean up PETSc data structures
901  ierr = LibMeshISDestroy(&isrow); LIBMESH_CHKERR(ierr);
902  ierr = LibMeshISDestroy(&iscol); LIBMESH_CHKERR(ierr);
903 }
virtual bool initialized() const
const Parallel::Communicator & comm() const
virtual void clear()=0
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
virtual bool closed() const override
PetscErrorCode ierr
SparseMatrix interface to PETSc Mat.
virtual void close() override
Definition: petsc_matrix.C:957

◆ add() [1/2]

template<typename T >
void libMesh::PetscMatrix< T >::add ( const numeric_index_type  i,
const numeric_index_type  j,
const T  value 
)
overridevirtual

Add value to the element (i,j). Throws an error if the entry does not exist. Zero values can be "added" to non-existent entries.

Implements libMesh::SparseMatrix< T >.

Definition at line 1074 of file petsc_matrix.C.

References ierr, libMesh::initialized(), and value.

1077 {
1078  libmesh_assert (this->initialized());
1079 
1080  PetscErrorCode ierr=0;
1081  PetscInt i_val=i, j_val=j;
1082 
1083  PetscScalar petsc_value = static_cast<PetscScalar>(value);
1084  ierr = MatSetValues(_mat, 1, &i_val, 1, &j_val,
1085  &petsc_value, ADD_VALUES);
1086  LIBMESH_CHKERR(ierr);
1087 }
virtual bool initialized() const
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ add() [2/2]

template<typename T >
void libMesh::PetscMatrix< T >::add ( const T  a,
const SparseMatrix< T > &  X 
)
overridevirtual

Compute A += a*X for scalar a, matrix X.

Note
The matrices A and X need to have the same nonzero pattern, otherwise PETSc will crash!
It is advisable to not only allocate appropriate memory with init(), but also explicitly zero the terms of this whenever you add a non-zero value to X.
X will be closed, if not already done, before performing any work.

Implements libMesh::SparseMatrix< T >.

Definition at line 1105 of file petsc_matrix.C.

References libMesh::PetscMatrix< T >::_mat, libMesh::PetscMatrix< T >::closed(), ierr, libMesh::initialized(), libMesh::SparseMatrix< T >::m(), and libMesh::SparseMatrix< T >::n().

1106 {
1107  libmesh_assert (this->initialized());
1108 
1109  // sanity check. but this cannot avoid
1110  // crash due to incompatible sparsity structure...
1111  libmesh_assert_equal_to (this->m(), X_in.m());
1112  libmesh_assert_equal_to (this->n(), X_in.n());
1113 
1114  PetscScalar a = static_cast<PetscScalar> (a_in);
1115  const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1116 
1117  libmesh_assert (X);
1118 
1119  PetscErrorCode ierr=0;
1120 
1121  // the matrix from which we copy the values has to be assembled/closed
1122  libmesh_assert(X->closed());
1123 
1124  semiparallel_only();
1125 
1126  ierr = MatAXPY(_mat, a, X->_mat, DIFFERENT_NONZERO_PATTERN);
1127  LIBMESH_CHKERR(ierr);
1128 }
virtual bool initialized() const
virtual numeric_index_type n() const override
virtual numeric_index_type m() const override
Definition: petsc_matrix.C:992
virtual bool closed() const override
PetscErrorCode ierr
SparseMatrix interface to PETSc Mat.

◆ add_block_matrix() [1/2]

template<typename T >
void libMesh::PetscMatrix< T >::add_block_matrix ( const DenseMatrix< T > &  dm,
const std::vector< numeric_index_type > &  brows,
const std::vector< numeric_index_type > &  bcols 
)
overridevirtual

Add the full matrix dm to the SparseMatrix. This is useful for adding an element matrix at assembly time. The matrix is assumed blocked, and brow, bcol correspond to the block row and column indices.

Reimplemented from libMesh::SparseMatrix< T >.

Definition at line 805 of file petsc_matrix.C.

References libMesh::DenseMatrix< T >::get_values(), ierr, libMesh::initialized(), libMesh::DenseMatrixBase< T >::m(), libMesh::DenseMatrixBase< T >::n(), and libMesh::numeric_petsc_cast().

Referenced by libMesh::PetscMatrix< T >::add_block_matrix().

808 {
809  libmesh_assert (this->initialized());
810 
811  const numeric_index_type n_brows =
812  cast_int<numeric_index_type>(brows.size());
813  const numeric_index_type n_bcols =
814  cast_int<numeric_index_type>(bcols.size());
815 
816  PetscErrorCode ierr=0;
817 
818 #ifndef NDEBUG
819  const numeric_index_type n_rows =
820  cast_int<numeric_index_type>(dm.m());
821  const numeric_index_type n_cols =
822  cast_int<numeric_index_type>(dm.n());
823  const numeric_index_type blocksize = n_rows / n_brows;
824 
825  libmesh_assert_equal_to (n_cols / n_bcols, blocksize);
826  libmesh_assert_equal_to (blocksize*n_brows, n_rows);
827  libmesh_assert_equal_to (blocksize*n_bcols, n_cols);
828 
829  PetscInt petsc_blocksize;
830  ierr = MatGetBlockSize(_mat, &petsc_blocksize);
831  LIBMESH_CHKERR(ierr);
832  libmesh_assert_equal_to (blocksize, static_cast<numeric_index_type>(petsc_blocksize));
833 #endif
834 
835  // These casts are required for PETSc <= 2.1.5
836  ierr = MatSetValuesBlocked(_mat,
837  n_brows, numeric_petsc_cast(brows.data()),
838  n_bcols, numeric_petsc_cast(bcols.data()),
839  const_cast<PetscScalar *>(dm.get_values().data()),
840  ADD_VALUES);
841  LIBMESH_CHKERR(ierr);
842 }
virtual bool initialized() const
unsigned int m() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
dof_id_type numeric_index_type
Definition: id_types.h:92
std::vector< T > & get_values()
Definition: dense_matrix.h:341
PetscErrorCode ierr
unsigned int n() const

◆ add_block_matrix() [2/2]

template<typename T>
virtual void libMesh::PetscMatrix< T >::add_block_matrix ( const DenseMatrix< T > &  dm,
const std::vector< numeric_index_type > &  dof_indices 
)
inlineoverridevirtual

Same as add_block_matrix(), but assumes the row and column maps are the same. Thus the matrix dm must be square.

Reimplemented from libMesh::SparseMatrix< T >.

Definition at line 191 of file petsc_matrix.h.

References libMesh::PetscMatrix< T >::add_block_matrix().

193  { this->add_block_matrix (dm, dof_indices, dof_indices); }
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &brows, const std::vector< numeric_index_type > &bcols) override
Definition: petsc_matrix.C:805

◆ add_matrix() [1/2]

template<typename T >
void libMesh::PetscMatrix< T >::add_matrix ( const DenseMatrix< T > &  dm,
const std::vector< numeric_index_type > &  rows,
const std::vector< numeric_index_type > &  cols 
)
overridevirtual

Add the full matrix dm to the SparseMatrix. This is useful for adding an element matrix at assembly time.

Implements libMesh::SparseMatrix< T >.

Definition at line 778 of file petsc_matrix.C.

References libMesh::DenseMatrix< T >::get_values(), ierr, libMesh::initialized(), libMesh::DenseMatrixBase< T >::m(), libMesh::DenseMatrixBase< T >::n(), and libMesh::numeric_petsc_cast().

781 {
782  libmesh_assert (this->initialized());
783 
784  const numeric_index_type n_rows = dm.m();
785  const numeric_index_type n_cols = dm.n();
786 
787  libmesh_assert_equal_to (rows.size(), n_rows);
788  libmesh_assert_equal_to (cols.size(), n_cols);
789 
790  PetscErrorCode ierr=0;
791  ierr = MatSetValues(_mat,
792  n_rows, numeric_petsc_cast(rows.data()),
793  n_cols, numeric_petsc_cast(cols.data()),
794  const_cast<PetscScalar *>(dm.get_values().data()),
795  ADD_VALUES);
796  LIBMESH_CHKERR(ierr);
797 }
virtual bool initialized() const
unsigned int m() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
dof_id_type numeric_index_type
Definition: id_types.h:92
std::vector< T > & get_values()
Definition: dense_matrix.h:341
PetscErrorCode ierr
unsigned int n() const

◆ add_matrix() [2/2]

template<typename T >
void libMesh::PetscMatrix< T >::add_matrix ( const DenseMatrix< T > &  dm,
const std::vector< numeric_index_type > &  dof_indices 
)
overridevirtual

Same as add_matrix, but assumes the row and column maps are the same. Thus the matrix dm must be square.

Implements libMesh::SparseMatrix< T >.

Definition at line 1092 of file petsc_matrix.C.

1094 {
1095  this->add_matrix (dm, dof_indices, dof_indices);
1096 }
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) override
Definition: petsc_matrix.C:778

◆ attach_dof_map()

template<typename T>
void libMesh::SparseMatrix< T >::attach_dof_map ( const DofMap dof_map)
inlineinherited

Get a pointer to the DofMap to use.

Definition at line 109 of file sparse_matrix.h.

Referenced by libMesh::__libmesh_tao_hessian(), and libMesh::DofMap::attach_matrix().

110  { _dof_map = &dof_map; }
DofMap const * _dof_map

◆ build()

template<typename T >
std::unique_ptr< SparseMatrix< T > > libMesh::SparseMatrix< T >::build ( const Parallel::Communicator comm,
const SolverPackage  solver_package = libMesh::default_solver_package() 
)
staticinherited

Builds a SparseMatrix<T> using the linear solver package specified by solver_package

Definition at line 130 of file sparse_matrix.C.

Referenced by libMesh::ImplicitSystem::add_matrix(), libMesh::EigenSystem::init_data(), and libMesh::EigenSystem::init_matrices().

132 {
133  // Avoid unused parameter warnings when no solver packages are enabled.
135 
136  // Build the appropriate vector
137  switch (solver_package)
138  {
139 
140 #ifdef LIBMESH_HAVE_LASPACK
141  case LASPACK_SOLVERS:
142  return libmesh_make_unique<LaspackMatrix<T>>(comm);
143 #endif
144 
145 
146 #ifdef LIBMESH_HAVE_PETSC
147  case PETSC_SOLVERS:
148  return libmesh_make_unique<PetscMatrix<T>>(comm);
149 #endif
150 
151 
152 #ifdef LIBMESH_TRILINOS_HAVE_EPETRA
153  case TRILINOS_SOLVERS:
154  return libmesh_make_unique<EpetraMatrix<T>>(comm);
155 #endif
156 
157 
158 #ifdef LIBMESH_HAVE_EIGEN
159  case EIGEN_SOLVERS:
160  return libmesh_make_unique<EigenSparseMatrix<T>>(comm);
161 #endif
162 
163  default:
164  libmesh_error_msg("ERROR: Unrecognized solver package: " << solver_package);
165  }
166 }
EIGEN_SOLVERS
Definition: libmesh.C:246
TRILINOS_SOLVERS
Definition: libmesh.C:244
const Parallel::Communicator & comm() const
void libmesh_ignore(const Args &...)
LASPACK_SOLVERS
Definition: libmesh.C:248

◆ clear()

template<typename T >
void libMesh::PetscMatrix< T >::clear ( )
overridevirtual

Restores the SparseMatrix<T> to a pristine state.

Implements libMesh::SparseMatrix< T >.

Definition at line 528 of file petsc_matrix.C.

References libMesh::libMeshPrivateData::_is_initialized, ierr, and libMesh::initialized().

529 {
530  PetscErrorCode ierr=0;
531 
532  if ((this->initialized()) && (this->_destroy_mat_on_exit))
533  {
534  semiparallel_only();
535 
536  ierr = LibMeshMatDestroy (&_mat);
537  LIBMESH_CHKERR(ierr);
538 
539  this->_is_initialized = false;
540  }
541 }
virtual bool initialized() const
PetscErrorCode ierr

◆ close()

template<typename T >
void libMesh::PetscMatrix< T >::close ( )
overridevirtual

Calls the SparseMatrix's internal assembly routines, ensuring that the values are consistent across processors.

Implements libMesh::SparseMatrix< T >.

Definition at line 957 of file petsc_matrix.C.

References ierr.

Referenced by libMesh::PetscMatrix< T >::_get_submatrix(), libMesh::PetscLinearSolver< T >::adjoint_solve(), libMesh::PetscMatrix< T >::get_transpose(), libMesh::PetscLinearSolver< T >::solve(), libMesh::SlepcEigenSolver< T >::solve_generalized(), and libMesh::SlepcEigenSolver< T >::solve_standard().

958 {
959  semiparallel_only();
960 
961  // BSK - 1/19/2004
962  // strictly this check should be OK, but it seems to
963  // fail on matrix-free matrices. Do they falsely
964  // state they are assembled? Check with the developers...
965  // if (this->closed())
966  // return;
967 
968  PetscErrorCode ierr=0;
969 
970  ierr = MatAssemblyBegin (_mat, MAT_FINAL_ASSEMBLY);
971  LIBMESH_CHKERR(ierr);
972  ierr = MatAssemblyEnd (_mat, MAT_FINAL_ASSEMBLY);
973  LIBMESH_CHKERR(ierr);
974 }
PetscErrorCode ierr

◆ closed()

template<typename T >
bool libMesh::PetscMatrix< T >::closed ( ) const
overridevirtual
Returns
true if the matrix has been assembled.

Implements libMesh::SparseMatrix< T >.

Definition at line 1194 of file petsc_matrix.C.

References ierr, and libMesh::initialized().

Referenced by libMesh::PetscMatrix< T >::add().

1195 {
1196  libmesh_assert (this->initialized());
1197 
1198  PetscErrorCode ierr=0;
1199  PetscBool assembled;
1200 
1201  ierr = MatAssembled(_mat, &assembled);
1202  LIBMESH_CHKERR(ierr);
1203 
1204  return (assembled == PETSC_TRUE);
1205 }
virtual bool initialized() const
PetscTruth PetscBool
Definition: petsc_macro.h:67
PetscErrorCode ierr

◆ comm()

const Parallel::Communicator& libMesh::ParallelObject::comm ( ) const
inlineinherited
Returns
A reference to the Parallel::Communicator object used by this mesh.

Definition at line 89 of file parallel_object.h.

References libMesh::ParallelObject::_communicator.

Referenced by libMesh::__libmesh_petsc_diff_solver_jacobian(), libMesh::__libmesh_petsc_diff_solver_monitor(), libMesh::__libmesh_petsc_diff_solver_residual(), libMesh::__libmesh_tao_equality_constraints(), libMesh::__libmesh_tao_equality_constraints_jacobian(), libMesh::__libmesh_tao_gradient(), libMesh::__libmesh_tao_hessian(), libMesh::__libmesh_tao_inequality_constraints(), libMesh::__libmesh_tao_inequality_constraints_jacobian(), libMesh::__libmesh_tao_objective(), libMesh::MeshRefinement::_coarsen_elements(), libMesh::ExactSolution::_compute_error(), libMesh::UniformRefinementEstimator::_estimate_error(), libMesh::BoundaryInfo::_find_id_maps(), libMesh::SlepcEigenSolver< T >::_petsc_shell_matrix_get_diagonal(), libMesh::PetscLinearSolver< T >::_petsc_shell_matrix_get_diagonal(), libMesh::SlepcEigenSolver< T >::_petsc_shell_matrix_mult(), libMesh::PetscLinearSolver< T >::_petsc_shell_matrix_mult(), libMesh::PetscLinearSolver< T >::_petsc_shell_matrix_mult_add(), libMesh::EquationSystems::_read_impl(), libMesh::MeshRefinement::_refine_elements(), libMesh::MeshRefinement::_smooth_flags(), libMesh::PetscDMWrapper::add_dofs_helper(), libMesh::PetscDMWrapper::add_dofs_to_section(), libMesh::ImplicitSystem::add_matrix(), libMesh::System::add_vector(), libMesh::UnstructuredMesh::all_second_order(), libMesh::MeshTools::Modification::all_tri(), libMesh::LaplaceMeshSmoother::allgather_graph(), libMesh::FEMSystem::assemble_qoi(), libMesh::MeshCommunication::assign_global_indices(), libMesh::DofMap::attach_matrix(), libMesh::MeshTools::Generation::build_extrusion(), libMesh::BoundaryInfo::build_node_list_from_side_list(), libMesh::EquationSystems::build_parallel_elemental_solution_vector(), libMesh::EquationSystems::build_parallel_solution_vector(), libMesh::PetscDMWrapper::build_section(), libMesh::PetscDMWrapper::build_sf(), libMesh::MeshBase::cache_elem_dims(), libMesh::System::calculate_norm(), libMesh::DofMap::check_dirichlet_bcid_consistency(), libMesh::PetscDMWrapper::check_section_n_dofs(), libMesh::Nemesis_IO_Helper::compute_num_global_elem_blocks(), libMesh::Nemesis_IO_Helper::compute_num_global_nodesets(), libMesh::Nemesis_IO_Helper::compute_num_global_sidesets(), libMesh::Problem_Interface::computeF(), libMesh::Problem_Interface::computeJacobian(), libMesh::Problem_Interface::computePreconditioner(), libMesh::ExodusII_IO::copy_elemental_solution(), libMesh::MeshTools::correct_node_proc_ids(), libMesh::MeshTools::create_bounding_box(), libMesh::MeshTools::create_nodal_bounding_box(), libMesh::MeshRefinement::create_parent_error_vector(), libMesh::MeshTools::create_processor_bounding_box(), libMesh::MeshTools::create_subdomain_bounding_box(), libMesh::MeshCommunication::delete_remote_elements(), libMesh::DofMap::distribute_dofs(), DMlibMeshFunction(), DMlibMeshJacobian(), DMlibMeshSetSystem_libMesh(), DMVariableBounds_libMesh(), libMesh::MeshRefinement::eliminate_unrefined_patches(), libMesh::EpetraVector< T >::EpetraVector(), libMesh::WeightedPatchRecoveryErrorEstimator::estimate_error(), libMesh::PatchRecoveryErrorEstimator::estimate_error(), libMesh::JumpErrorEstimator::estimate_error(), libMesh::AdjointRefinementEstimator::estimate_error(), libMesh::ExactErrorEstimator::estimate_error(), libMesh::MeshRefinement::flag_elements_by_elem_fraction(), libMesh::MeshRefinement::flag_elements_by_error_fraction(), libMesh::MeshRefinement::flag_elements_by_nelem_target(), libMesh::CondensedEigenSystem::get_eigenpair(), libMesh::DofMap::get_info(), libMesh::ImplicitSystem::get_linear_solver(), libMesh::LocationMap< T >::init(), libMesh::TimeSolver::init(), libMesh::SystemSubsetBySubdomain::init(), libMesh::PetscDMWrapper::init_and_attach_petscdm(), libMesh::EigenSystem::init_data(), libMesh::EigenSystem::init_matrices(), libMesh::OptimizationSystem::initialize_equality_constraints_storage(), libMesh::OptimizationSystem::initialize_inequality_constraints_storage(), libMesh::MeshTools::libmesh_assert_consistent_distributed(), libMesh::MeshTools::libmesh_assert_consistent_distributed_nodes(), libMesh::MeshTools::libmesh_assert_contiguous_dof_ids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_new_node_procids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Elem >(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Node >(), libMesh::MeshTools::libmesh_assert_topology_consistent_procids< Node >(), libMesh::MeshTools::libmesh_assert_valid_boundary_ids(), libMesh::MeshTools::libmesh_assert_valid_dof_ids(), libMesh::MeshTools::libmesh_assert_valid_neighbors(), libMesh::DistributedMesh::libmesh_assert_valid_parallel_flags(), libMesh::DistributedMesh::libmesh_assert_valid_parallel_object_ids(), libMesh::DistributedMesh::libmesh_assert_valid_parallel_p_levels(), libMesh::MeshTools::libmesh_assert_valid_refinement_flags(), libMesh::MeshTools::libmesh_assert_valid_unique_ids(), libMesh::libmesh_petsc_snes_fd_residual(), libMesh::libmesh_petsc_snes_jacobian(), libMesh::libmesh_petsc_snes_mffd_residual(), libMesh::libmesh_petsc_snes_postcheck(), libMesh::libmesh_petsc_snes_residual(), libMesh::libmesh_petsc_snes_residual_helper(), libMesh::MeshRefinement::limit_level_mismatch_at_edge(), libMesh::MeshRefinement::limit_level_mismatch_at_node(), libMesh::MeshRefinement::limit_overrefined_boundary(), libMesh::MeshRefinement::limit_underrefined_boundary(), libMesh::MeshRefinement::make_coarsening_compatible(), libMesh::MeshCommunication::make_elems_parallel_consistent(), libMesh::MeshRefinement::make_flags_parallel_consistent(), libMesh::MeshCommunication::make_new_node_proc_ids_parallel_consistent(), libMesh::MeshCommunication::make_new_nodes_parallel_consistent(), libMesh::MeshCommunication::make_node_ids_parallel_consistent(), libMesh::MeshCommunication::make_node_proc_ids_parallel_consistent(), libMesh::MeshCommunication::make_node_unique_ids_parallel_consistent(), libMesh::MeshCommunication::make_nodes_parallel_consistent(), libMesh::MeshCommunication::make_p_levels_parallel_consistent(), libMesh::MeshRefinement::make_refinement_compatible(), libMesh::FEMSystem::mesh_position_set(), libMesh::DistributedMesh::n_active_elem(), libMesh::MeshTools::n_active_levels(), libMesh::BoundaryInfo::n_boundary_conds(), libMesh::BoundaryInfo::n_edge_conds(), libMesh::CondensedEigenSystem::n_global_non_condensed_dofs(), libMesh::MeshTools::n_levels(), libMesh::BoundaryInfo::n_nodeset_conds(), libMesh::MeshTools::n_p_levels(), libMesh::BoundaryInfo::n_shellface_conds(), libMesh::DistributedMesh::parallel_max_elem_id(), libMesh::DistributedMesh::parallel_max_node_id(), libMesh::ReplicatedMesh::parallel_max_unique_id(), libMesh::DistributedMesh::parallel_max_unique_id(), libMesh::DistributedMesh::parallel_n_elem(), libMesh::DistributedMesh::parallel_n_nodes(), libMesh::SparsityPattern::Build::parallel_sync(), libMesh::MeshTools::paranoid_n_levels(), libMesh::petsc_auto_fieldsplit(), libMesh::System::point_gradient(), libMesh::System::point_hessian(), libMesh::System::point_value(), libMesh::MeshBase::prepare_for_use(), libMesh::Nemesis_IO::read(), libMesh::XdrIO::read(), libMesh::CheckpointIO::read_header(), libMesh::XdrIO::read_header(), libMesh::System::read_header(), libMesh::System::read_legacy_data(), libMesh::System::read_SCALAR_dofs(), libMesh::XdrIO::read_serialized_bc_names(), libMesh::XdrIO::read_serialized_bcs_helper(), libMesh::System::read_serialized_blocked_dof_objects(), libMesh::XdrIO::read_serialized_connectivity(), libMesh::XdrIO::read_serialized_nodes(), libMesh::XdrIO::read_serialized_nodesets(), libMesh::XdrIO::read_serialized_subdomain_names(), libMesh::System::read_serialized_vector(), libMesh::MeshBase::recalculate_n_partitions(), libMesh::MeshRefinement::refine_and_coarsen_elements(), libMesh::DistributedMesh::renumber_dof_objects(), libMesh::CheckpointIO::select_split_config(), libMesh::DofMap::set_nonlocal_dof_objects(), libMesh::PetscDMWrapper::set_point_range_in_section(), libMesh::PetscDiffSolver::setup_petsc_data(), libMesh::LaplaceMeshSmoother::smooth(), libMesh::split_mesh(), libMesh::MeshBase::subdomain_ids(), libMesh::BoundaryInfo::sync(), libMesh::MeshRefinement::test_level_one(), libMesh::MeshRefinement::test_unflagged(), libMesh::MeshTools::total_weight(), libMesh::MeshRefinement::uniformly_coarsen(), libMesh::NameBasedIO::write(), libMesh::XdrIO::write(), libMesh::System::write_SCALAR_dofs(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::System::write_serialized_blocked_dof_objects(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodes(), and libMesh::XdrIO::write_serialized_nodesets().

90  { return _communicator; }
const Parallel::Communicator & _communicator

◆ create_submatrix()

template<typename T>
virtual void libMesh::SparseMatrix< T >::create_submatrix ( SparseMatrix< T > &  submatrix,
const std::vector< numeric_index_type > &  rows,
const std::vector< numeric_index_type > &  cols 
) const
inlinevirtualinherited

This function creates a matrix called "submatrix" which is defined by the row and column indices given in the "rows" and "cols" entries. Currently this operation is only defined for the PetscMatrix type.

Definition at line 354 of file sparse_matrix.h.

357  {
358  this->_get_submatrix(submatrix,
359  rows,
360  cols,
361  false); // false means DO NOT REUSE submatrix
362  }
virtual void _get_submatrix(SparseMatrix< T > &, const std::vector< numeric_index_type > &, const std::vector< numeric_index_type > &, const bool) const

◆ disable_print_counter_info()

void libMesh::ReferenceCounter::disable_print_counter_info ( )
staticinherited

Definition at line 106 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter.

Referenced by libMesh::LibMeshInit::LibMeshInit().

107 {
108  _enable_print_counter = false;
109  return;
110 }

◆ enable_print_counter_info()

void libMesh::ReferenceCounter::enable_print_counter_info ( )
staticinherited

Methods to enable/disable the reference counter output from print_info()

Definition at line 100 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter.

101 {
102  _enable_print_counter = true;
103  return;
104 }

◆ flush()

template<typename T >
void libMesh::PetscMatrix< T >::flush ( )
overridevirtual

For PETSc matrix , this function is similar to close but without shrinking memory. This is useful when we want to switch between ADD_VALUES and INSERT_VALUES. close should be called before using the matrix.

Reimplemented from libMesh::SparseMatrix< T >.

Definition at line 977 of file petsc_matrix.C.

References ierr.

978 {
979  semiparallel_only();
980 
981  PetscErrorCode ierr=0;
982 
983  ierr = MatAssemblyBegin (_mat, MAT_FLUSH_ASSEMBLY);
984  LIBMESH_CHKERR(ierr);
985  ierr = MatAssemblyEnd (_mat, MAT_FLUSH_ASSEMBLY);
986  LIBMESH_CHKERR(ierr);
987 }
PetscErrorCode ierr

◆ get_diagonal()

template<typename T >
void libMesh::PetscMatrix< T >::get_diagonal ( NumericVector< T > &  dest) const
overridevirtual

Copies the diagonal part of the matrix into dest.

Implements libMesh::SparseMatrix< T >.

Definition at line 908 of file petsc_matrix.C.

References ierr, and libMesh::PetscVector< T >::vec().

909 {
910  // Make sure the NumericVector passed in is really a PetscVector
911  PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
912 
913  // Needs a const_cast since PETSc does not work with const.
914  PetscErrorCode ierr =
915  MatGetDiagonal(const_cast<PetscMatrix<T> *>(this)->mat(),petsc_dest.vec()); LIBMESH_CHKERR(ierr);
916 }
NumericVector interface to PETSc Vec.
Definition: petsc_vector.h:64
PetscErrorCode ierr
SparseMatrix interface to PETSc Mat.

◆ get_info()

std::string libMesh::ReferenceCounter::get_info ( )
staticinherited

Gets a string containing the reference information.

Definition at line 47 of file reference_counter.C.

References libMesh::ReferenceCounter::_counts, and libMesh::Quality::name().

Referenced by libMesh::ReferenceCounter::print_info().

48 {
49 #if defined(LIBMESH_ENABLE_REFERENCE_COUNTING) && defined(DEBUG)
50 
51  std::ostringstream oss;
52 
53  oss << '\n'
54  << " ---------------------------------------------------------------------------- \n"
55  << "| Reference count information |\n"
56  << " ---------------------------------------------------------------------------- \n";
57 
58  for (const auto & pr : _counts)
59  {
60  const std::string name(pr.first);
61  const unsigned int creations = pr.second.first;
62  const unsigned int destructions = pr.second.second;
63 
64  oss << "| " << name << " reference count information:\n"
65  << "| Creations: " << creations << '\n'
66  << "| Destructions: " << destructions << '\n';
67  }
68 
69  oss << " ---------------------------------------------------------------------------- \n";
70 
71  return oss.str();
72 
73 #else
74 
75  return "";
76 
77 #endif
78 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:42

◆ get_transpose()

template<typename T >
void libMesh::PetscMatrix< T >::get_transpose ( SparseMatrix< T > &  dest) const
overridevirtual

Copies the transpose of the matrix into dest, which may be *this.

Implements libMesh::SparseMatrix< T >.

Definition at line 921 of file petsc_matrix.C.

References libMesh::SparseMatrix< T >::_is_initialized, libMesh::PetscMatrix< T >::_mat, libMesh::SparseMatrix< T >::clear(), libMesh::PetscMatrix< T >::close(), and ierr.

922 {
923  // Make sure the SparseMatrix passed in is really a PetscMatrix
924  PetscMatrix<T> & petsc_dest = cast_ref<PetscMatrix<T> &>(dest);
925 
926  // If we aren't reusing the matrix then need to clear dest,
927  // otherwise we get a memory leak
928  if (&petsc_dest != this)
929  dest.clear();
930 
931  PetscErrorCode ierr;
932 #if PETSC_VERSION_LESS_THAN(3,0,0)
933  if (&petsc_dest == this)
934  ierr = MatTranspose(_mat,PETSC_NULL);
935  else
936  ierr = MatTranspose(_mat,&petsc_dest._mat);
937  LIBMESH_CHKERR(ierr);
938 #else
939  // FIXME - we can probably use MAT_REUSE_MATRIX in more situations
940  if (&petsc_dest == this)
941  ierr = MatTranspose(_mat,MAT_REUSE_MATRIX,&petsc_dest._mat);
942  else
943  ierr = MatTranspose(_mat,MAT_INITIAL_MATRIX,&petsc_dest._mat);
944  LIBMESH_CHKERR(ierr);
945 #endif
946 
947  // Specify that the transposed matrix is initialized and close it.
948  petsc_dest._is_initialized = true;
949  petsc_dest.close();
950 }
virtual void clear()=0
PetscErrorCode ierr
SparseMatrix interface to PETSc Mat.
virtual void close() override
Definition: petsc_matrix.C:957

◆ increment_constructor_count()

void libMesh::ReferenceCounter::increment_constructor_count ( const std::string &  name)
inlineprotectedinherited

Increments the construction counter. Should be called in the constructor of any derived class that will be reference counted.

Definition at line 181 of file reference_counter.h.

References libMesh::ReferenceCounter::_counts, libMesh::Quality::name(), and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::ReferenceCountedObject().

182 {
183  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
184  std::pair<unsigned int, unsigned int> & p = _counts[name];
185 
186  p.first++;
187 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
spin_mutex spin_mtx
Definition: threads.C:29

◆ increment_destructor_count()

void libMesh::ReferenceCounter::increment_destructor_count ( const std::string &  name)
inlineprotectedinherited

Increments the destruction counter. Should be called in the destructor of any derived class that will be reference counted.

Definition at line 194 of file reference_counter.h.

References libMesh::ReferenceCounter::_counts, libMesh::Quality::name(), and libMesh::Threads::spin_mtx.

Referenced by libMesh::ReferenceCountedObject< RBParametrized >::~ReferenceCountedObject().

195 {
196  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
197  std::pair<unsigned int, unsigned int> & p = _counts[name];
198 
199  p.second++;
200 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
spin_mutex spin_mtx
Definition: threads.C:29

◆ init() [1/3]

template<typename T >
void libMesh::PetscMatrix< T >::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  nnz = 30,
const numeric_index_type  noz = 10,
const numeric_index_type  blocksize = 1 
)
overridevirtual

Initialize SparseMatrix with the specified sizes.

Parameters
mThe global number of rows.
nThe global number of columns.
m_lThe local number of rows.
n_lThe local number of columns.
nnzThe number of on-diagonal nonzeros per row (defaults to 30).
nozThe number of off-diagonal nonzeros per row (defaults to 10).
blocksizeOptional value indicating dense coupled blocks for systems with multiple variables all of the same type.

Implements libMesh::SparseMatrix< T >.

Definition at line 127 of file petsc_matrix.C.

References libMesh::libMeshPrivateData::_is_initialized, libMesh::AIJ, libMesh::HYPRE, ierr, libMesh::initialized(), libMesh::libmesh_ignore(), and libMesh::zero.

134 {
135  // So compilers don't warn when !LIBMESH_ENABLE_BLOCKED_STORAGE
136  libmesh_ignore(blocksize_in);
137 
138  // Clear initialized matrices
139  if (this->initialized())
140  this->clear();
141 
142  this->_is_initialized = true;
143 
144 
145  PetscErrorCode ierr = 0;
146  PetscInt m_global = static_cast<PetscInt>(m_in);
147  PetscInt n_global = static_cast<PetscInt>(n_in);
148  PetscInt m_local = static_cast<PetscInt>(m_l);
149  PetscInt n_local = static_cast<PetscInt>(n_l);
150  PetscInt n_nz = static_cast<PetscInt>(nnz);
151  PetscInt n_oz = static_cast<PetscInt>(noz);
152 
153  ierr = MatCreate(this->comm().get(), &_mat);
154  LIBMESH_CHKERR(ierr);
155  ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
156  LIBMESH_CHKERR(ierr);
157 
158 #ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
159  PetscInt blocksize = static_cast<PetscInt>(blocksize_in);
160  if (blocksize > 1)
161  {
162  // specified blocksize, bs>1.
163  // double check sizes.
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);
170 
171  ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
172  LIBMESH_CHKERR(ierr);
173  ierr = MatSetBlockSize(_mat, blocksize);
174  LIBMESH_CHKERR(ierr);
175  ierr = MatSeqBAIJSetPreallocation(_mat, blocksize, n_nz/blocksize, PETSC_NULL);
176  LIBMESH_CHKERR(ierr);
177  ierr = MatMPIBAIJSetPreallocation(_mat, blocksize,
178  n_nz/blocksize, PETSC_NULL,
179  n_oz/blocksize, PETSC_NULL);
180  LIBMESH_CHKERR(ierr);
181  }
182  else
183 #endif
184  {
185  switch (_mat_type) {
186  case AIJ:
187  ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
188  LIBMESH_CHKERR(ierr);
189  ierr = MatSeqAIJSetPreallocation(_mat, n_nz, PETSC_NULL);
190  LIBMESH_CHKERR(ierr);
191  ierr = MatMPIAIJSetPreallocation(_mat, n_nz, PETSC_NULL, n_oz, PETSC_NULL);
192  LIBMESH_CHKERR(ierr);
193  break;
194 
195  case HYPRE:
196 #if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
197  ierr = MatSetType(_mat, MATHYPRE);
198  LIBMESH_CHKERR(ierr);
199  ierr = MatHYPRESetPreallocation(_mat, n_nz, PETSC_NULL, n_oz, PETSC_NULL);
200  LIBMESH_CHKERR(ierr);
201 #else
202  libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
203 #endif
204  break;
205 
206  default: libmesh_error_msg("Unsupported petsc matrix type");
207  }
208  }
209 
210  // Make it an error for PETSc to allocate new nonzero entries during assembly
211 #if PETSC_VERSION_LESS_THAN(3,0,0)
212  ierr = MatSetOption(_mat, MAT_NEW_NONZERO_ALLOCATION_ERR);
213 #else
214  ierr = MatSetOption(_mat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
215 #endif
216  LIBMESH_CHKERR(ierr);
217 
218  // Is prefix information available somewhere? Perhaps pass in the system name?
219  ierr = MatSetOptionsPrefix(_mat, "");
220  LIBMESH_CHKERR(ierr);
221  ierr = MatSetFromOptions(_mat);
222  LIBMESH_CHKERR(ierr);
223 
224  this->zero ();
225 }
virtual bool initialized() const
virtual void zero() override
Definition: petsc_matrix.C:475
const Parallel::Communicator & comm() const
void libmesh_ignore(const Args &...)
PetscMatrixType _mat_type
Definition: petsc_matrix.h:279
virtual void clear() override
Definition: petsc_matrix.C:528
PetscErrorCode ierr

◆ init() [2/3]

template<typename T >
void libMesh::PetscMatrix< T >::init ( const numeric_index_type  m,
const numeric_index_type  n,
const numeric_index_type  m_l,
const numeric_index_type  n_l,
const std::vector< numeric_index_type > &  n_nz,
const std::vector< numeric_index_type > &  n_oz,
const numeric_index_type  blocksize = 1 
)

Initialize a PETSc matrix.

Parameters
mThe global number of rows.
nThe global number of columns.
m_lThe local number of rows.
n_lThe local number of columns.
n_nzarray containing the number of nonzeros in each row of the DIAGONAL portion of the local submatrix.
n_ozArray containing the number of nonzeros in each row of the OFF-DIAGONAL portion of the local submatrix.
blocksizeOptional value indicating dense coupled blocks for systems with multiple variables all of the same type.

Definition at line 229 of file petsc_matrix.C.

References libMesh::libMeshPrivateData::_is_initialized, libMesh::AIJ, libMesh::HYPRE, ierr, libMesh::initialized(), libMesh::libmesh_ignore(), libMesh::numeric_petsc_cast(), and libMesh::zero.

236 {
237  // So compilers don't warn when !LIBMESH_ENABLE_BLOCKED_STORAGE
238  libmesh_ignore(blocksize_in);
239 
240  // Clear initialized matrices
241  if (this->initialized())
242  this->clear();
243 
244  this->_is_initialized = true;
245 
246  // Make sure the sparsity pattern isn't empty unless the matrix is 0x0
247  libmesh_assert_equal_to (n_nz.size(), m_l);
248  libmesh_assert_equal_to (n_oz.size(), m_l);
249 
250  PetscErrorCode ierr = 0;
251  PetscInt m_global = static_cast<PetscInt>(m_in);
252  PetscInt n_global = static_cast<PetscInt>(n_in);
253  PetscInt m_local = static_cast<PetscInt>(m_l);
254  PetscInt n_local = static_cast<PetscInt>(n_l);
255 
256  ierr = MatCreate(this->comm().get(), &_mat);
257  LIBMESH_CHKERR(ierr);
258  ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
259  LIBMESH_CHKERR(ierr);
260 
261 #ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
262  PetscInt blocksize = static_cast<PetscInt>(blocksize_in);
263  if (blocksize > 1)
264  {
265  // specified blocksize, bs>1.
266  // double check sizes.
267  libmesh_assert_equal_to (m_local % blocksize, 0);
268  libmesh_assert_equal_to (n_local % blocksize, 0);
269  libmesh_assert_equal_to (m_global % blocksize, 0);
270  libmesh_assert_equal_to (n_global % blocksize, 0);
271 
272  ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
273  LIBMESH_CHKERR(ierr);
274  ierr = MatSetBlockSize(_mat, blocksize);
275  LIBMESH_CHKERR(ierr);
276 
277  // transform the per-entry n_nz and n_oz arrays into their block counterparts.
278  std::vector<numeric_index_type> b_n_nz, b_n_oz;
279 
280  transform_preallocation_arrays (blocksize,
281  n_nz, n_oz,
282  b_n_nz, b_n_oz);
283 
284  ierr = MatSeqBAIJSetPreallocation (_mat,
285  blocksize,
286  0,
287  numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()));
288  LIBMESH_CHKERR(ierr);
289 
290  ierr = MatMPIBAIJSetPreallocation (_mat,
291  blocksize,
292  0,
293  numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()),
294  0,
295  numeric_petsc_cast(b_n_oz.empty() ? nullptr : b_n_oz.data()));
296  LIBMESH_CHKERR(ierr);
297  }
298  else
299 #endif
300  {
301  switch (_mat_type) {
302  case AIJ:
303  ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
304  LIBMESH_CHKERR(ierr);
305  ierr = MatSeqAIJSetPreallocation (_mat,
306  0,
307  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()));
308  LIBMESH_CHKERR(ierr);
309  ierr = MatMPIAIJSetPreallocation (_mat,
310  0,
311  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
312  0,
313  numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
314  break;
315 
316  case HYPRE:
317 #if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
318  ierr = MatSetType(_mat, MATHYPRE);
319  LIBMESH_CHKERR(ierr);
320  ierr = MatHYPRESetPreallocation (_mat,
321  0,
322  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
323  0,
324  numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
325  LIBMESH_CHKERR(ierr);
326 #else
327  libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
328 #endif
329  break;
330 
331  default: libmesh_error_msg("Unsupported petsc matrix type");
332  }
333 
334  }
335 
336  // Is prefix information available somewhere? Perhaps pass in the system name?
337  ierr = MatSetOptionsPrefix(_mat, "");
338  LIBMESH_CHKERR(ierr);
339  ierr = MatSetFromOptions(_mat);
340  LIBMESH_CHKERR(ierr);
341 
342 
343  this->zero();
344 }
virtual bool initialized() const
virtual void zero() override
Definition: petsc_matrix.C:475
const Parallel::Communicator & comm() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
void libmesh_ignore(const Args &...)
PetscMatrixType _mat_type
Definition: petsc_matrix.h:279
virtual void clear() override
Definition: petsc_matrix.C:528
PetscErrorCode ierr

◆ init() [3/3]

template<typename T >
void libMesh::PetscMatrix< T >::init ( )
overridevirtual

Initialize this matrix using the sparsity structure computed by dof_map.

Implements libMesh::SparseMatrix< T >.

Definition at line 348 of file petsc_matrix.C.

References libMesh::libMeshPrivateData::_is_initialized, libMesh::AIJ, libMesh::HYPRE, ierr, libMesh::initialized(), libMesh::numeric_petsc_cast(), and libMesh::zero.

349 {
350  libmesh_assert(this->_dof_map);
351 
352  // Clear initialized matrices
353  if (this->initialized())
354  this->clear();
355 
356  this->_is_initialized = true;
357 
358 
359  const numeric_index_type my_m = this->_dof_map->n_dofs();
360  const numeric_index_type my_n = my_m;
361  const numeric_index_type n_l = this->_dof_map->n_dofs_on_processor(this->processor_id());
362  const numeric_index_type m_l = n_l;
363 
364 
365  const std::vector<numeric_index_type> & n_nz = this->_dof_map->get_n_nz();
366  const std::vector<numeric_index_type> & n_oz = this->_dof_map->get_n_oz();
367 
368  // Make sure the sparsity pattern isn't empty unless the matrix is 0x0
369  libmesh_assert_equal_to (n_nz.size(), m_l);
370  libmesh_assert_equal_to (n_oz.size(), m_l);
371 
372  PetscErrorCode ierr = 0;
373  PetscInt m_global = static_cast<PetscInt>(my_m);
374  PetscInt n_global = static_cast<PetscInt>(my_n);
375  PetscInt m_local = static_cast<PetscInt>(m_l);
376  PetscInt n_local = static_cast<PetscInt>(n_l);
377 
378  ierr = MatCreate(this->comm().get(), &_mat);
379  LIBMESH_CHKERR(ierr);
380  ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
381  LIBMESH_CHKERR(ierr);
382 
383 #ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
384  PetscInt blocksize = static_cast<PetscInt>(this->_dof_map->block_size());
385  if (blocksize > 1)
386  {
387  // specified blocksize, bs>1.
388  // double check sizes.
389  libmesh_assert_equal_to (m_local % blocksize, 0);
390  libmesh_assert_equal_to (n_local % blocksize, 0);
391  libmesh_assert_equal_to (m_global % blocksize, 0);
392  libmesh_assert_equal_to (n_global % blocksize, 0);
393 
394  ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
395  LIBMESH_CHKERR(ierr);
396  ierr = MatSetBlockSize(_mat, blocksize);
397  LIBMESH_CHKERR(ierr);
398 
399  // transform the per-entry n_nz and n_oz arrays into their block counterparts.
400  std::vector<numeric_index_type> b_n_nz, b_n_oz;
401 
402  transform_preallocation_arrays (blocksize,
403  n_nz, n_oz,
404  b_n_nz, b_n_oz);
405 
406  ierr = MatSeqBAIJSetPreallocation (_mat,
407  blocksize,
408  0,
409  numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()));
410  LIBMESH_CHKERR(ierr);
411 
412  ierr = MatMPIBAIJSetPreallocation (_mat,
413  blocksize,
414  0,
415  numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()),
416  0,
417  numeric_petsc_cast(b_n_oz.empty() ? nullptr : b_n_oz.data()));
418  LIBMESH_CHKERR(ierr);
419  }
420  else
421 #endif
422  {
423  switch (_mat_type) {
424  case AIJ:
425  ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
426  LIBMESH_CHKERR(ierr);
427  ierr = MatSeqAIJSetPreallocation (_mat,
428  0,
429  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()));
430  LIBMESH_CHKERR(ierr);
431  ierr = MatMPIAIJSetPreallocation (_mat,
432  0,
433  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
434  0,
435  numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
436  break;
437 
438  case HYPRE:
439 #if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
440  ierr = MatSetType(_mat, MATHYPRE);
441  LIBMESH_CHKERR(ierr);
442  ierr = MatHYPRESetPreallocation (_mat,
443  0,
444  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
445  0,
446  numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
447  LIBMESH_CHKERR(ierr);
448 #else
449  libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
450 #endif
451  break;
452 
453  default: libmesh_error_msg("Unsupported petsc matrix type");
454  }
455  }
456 
457  // Is prefix information available somewhere? Perhaps pass in the system name?
458  ierr = MatSetOptionsPrefix(_mat, "");
459  LIBMESH_CHKERR(ierr);
460  ierr = MatSetFromOptions(_mat);
461  LIBMESH_CHKERR(ierr);
462 
463  this->zero();
464 }
virtual bool initialized() const
const std::vector< dof_id_type > & get_n_oz() const
Definition: dof_map.h:472
dof_id_type n_dofs_on_processor(const processor_id_type proc) const
Definition: dof_map.h:590
virtual void zero() override
Definition: petsc_matrix.C:475
unsigned int block_size() const
Definition: dof_map.h:562
const Parallel::Communicator & comm() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
dof_id_type n_dofs() const
Definition: dof_map.h:574
dof_id_type numeric_index_type
Definition: id_types.h:92
DofMap const * _dof_map
PetscMatrixType _mat_type
Definition: petsc_matrix.h:279
virtual void clear() override
Definition: petsc_matrix.C:528
PetscErrorCode ierr
const std::vector< dof_id_type > & get_n_nz() const
Definition: dof_map.h:459
processor_id_type processor_id() const

◆ initialized()

template<typename T>
virtual bool libMesh::SparseMatrix< T >::initialized ( ) const
inlinevirtualinherited
Returns
true if the matrix has been initialized, false otherwise.

Definition at line 104 of file sparse_matrix.h.

Referenced by libMesh::PetscMatrix< T >::_get_submatrix(), libMesh::ImplicitSystem::assemble(), and libMesh::ImplicitSystem::init_matrices().

104 { return _is_initialized; }

◆ l1_norm()

template<typename T >
Real libMesh::PetscMatrix< T >::l1_norm ( ) const
overridevirtual
Returns
The $ \ell_1 $-norm of the matrix, that is the max column sum: $ |M|_1 = \max_{j} \sum_{i} |M_{ij}| $

This is the natural matrix norm that is compatible with the $ \ell_1 $-norm for vectors, i.e. $ |Mv|_1 \leq |M|_1 |v|_1 $. (cf. Haemmerlin-Hoffmann : Numerische Mathematik)

Implements libMesh::SparseMatrix< T >.

Definition at line 546 of file petsc_matrix.C.

References libMesh::closed(), ierr, libMesh::initialized(), libMesh::Real, and value.

547 {
548  libmesh_assert (this->initialized());
549 
550  semiparallel_only();
551 
552  PetscErrorCode ierr=0;
553  PetscReal petsc_value;
554  Real value;
555 
556  libmesh_assert (this->closed());
557 
558  ierr = MatNorm(_mat, NORM_1, &petsc_value);
559  LIBMESH_CHKERR(ierr);
560 
561  value = static_cast<Real>(petsc_value);
562 
563  return value;
564 }
virtual bool initialized() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual bool closed() const override
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ linfty_norm()

template<typename T >
Real libMesh::PetscMatrix< T >::linfty_norm ( ) const
overridevirtual
Returns
The $ \ell_{\infty} $-norm of the matrix, that is the max row sum:

$ |M|_{\infty} = \max_{i} \sum_{j} |M_{ij}| $

This is the natural matrix norm that is compatible to the $ \ell_{\infty} $-norm of vectors, i.e. $ |Mv|_{\infty} \leq |M|_{\infty} |v|_{\infty} $. (cf. Haemmerlin-Hoffmann : Numerische Mathematik)

Implements libMesh::SparseMatrix< T >.

Definition at line 569 of file petsc_matrix.C.

References libMesh::closed(), ierr, libMesh::initialized(), libMesh::Real, and value.

570 {
571  libmesh_assert (this->initialized());
572 
573  semiparallel_only();
574 
575  PetscErrorCode ierr=0;
576  PetscReal petsc_value;
577  Real value;
578 
579  libmesh_assert (this->closed());
580 
581  ierr = MatNorm(_mat, NORM_INFINITY, &petsc_value);
582  LIBMESH_CHKERR(ierr);
583 
584  value = static_cast<Real>(petsc_value);
585 
586  return value;
587 }
virtual bool initialized() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual bool closed() const override
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ m()

template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::m ( ) const
overridevirtual
Returns
The row-dimension of the matrix.

Implements libMesh::SparseMatrix< T >.

Definition at line 992 of file petsc_matrix.C.

References ierr, and libMesh::initialized().

993 {
994  libmesh_assert (this->initialized());
995 
996  PetscInt petsc_m=0, petsc_n=0;
997  PetscErrorCode ierr=0;
998 
999  ierr = MatGetSize (_mat, &petsc_m, &petsc_n);
1000  LIBMESH_CHKERR(ierr);
1001 
1002  return static_cast<numeric_index_type>(petsc_m);
1003 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr

◆ mat()

template<typename T>
Mat libMesh::PetscMatrix< T >::mat ( )
inline
Returns
The raw PETSc matrix pointer.
Note
This is generally not required in user-level code.
Don't do anything crazy like calling LibMeshMatDestroy() on it, or very bad things will likely happen!

Definition at line 247 of file petsc_matrix.h.

References libMesh::PetscMatrix< T >::_mat.

Referenced by libMesh::PetscLinearSolver< T >::adjoint_solve(), libMesh::PetscPreconditioner< T >::init(), libMesh::PetscLinearSolver< T >::init(), libMesh::PetscDiffSolver::solve(), libMesh::TaoOptimizationSolver< T >::solve(), libMesh::PetscNonlinearSolver< Number >::solve(), libMesh::PetscLinearSolver< T >::solve(), libMesh::SlepcEigenSolver< T >::solve_generalized(), and libMesh::SlepcEigenSolver< T >::solve_standard().

247 { libmesh_assert (_mat); return _mat; }

◆ n()

template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::n ( ) const
overridevirtual
Returns
The column-dimension of the matrix.

Implements libMesh::SparseMatrix< T >.

Definition at line 1008 of file petsc_matrix.C.

References ierr, and libMesh::initialized().

1009 {
1010  libmesh_assert (this->initialized());
1011 
1012  PetscInt petsc_m=0, petsc_n=0;
1013  PetscErrorCode ierr=0;
1014 
1015  ierr = MatGetSize (_mat, &petsc_m, &petsc_n);
1016  LIBMESH_CHKERR(ierr);
1017 
1018  return static_cast<numeric_index_type>(petsc_n);
1019 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:92
PetscErrorCode ierr

◆ n_objects()

static unsigned int libMesh::ReferenceCounter::n_objects ( )
inlinestaticinherited

Prints the number of outstanding (created, but not yet destroyed) objects.

Definition at line 83 of file reference_counter.h.

References libMesh::ReferenceCounter::_n_objects.

84  { return _n_objects; }
static Threads::atomic< unsigned int > _n_objects

◆ n_processors()

processor_id_type libMesh::ParallelObject::n_processors ( ) const
inlineinherited
Returns
The number of processors in the group.

Definition at line 95 of file parallel_object.h.

References libMesh::ParallelObject::_communicator, and libMesh::Parallel::Communicator::size().

Referenced by libMesh::BoundaryInfo::_find_id_maps(), libMesh::PetscDMWrapper::add_dofs_to_section(), libMesh::DistributedMesh::add_elem(), libMesh::DistributedMesh::add_node(), libMesh::LaplaceMeshSmoother::allgather_graph(), libMesh::FEMSystem::assembly(), libMesh::AztecLinearSolver< T >::AztecLinearSolver(), libMesh::BoundaryInfo::build_node_list_from_side_list(), libMesh::EquationSystems::build_parallel_elemental_solution_vector(), libMesh::DistributedMesh::clear(), libMesh::Nemesis_IO_Helper::compute_border_node_ids(), libMesh::Nemesis_IO_Helper::construct_nemesis_filename(), libMesh::UnstructuredMesh::create_pid_mesh(), libMesh::MeshTools::create_processor_bounding_box(), libMesh::DofMap::distribute_dofs(), libMesh::DofMap::distribute_local_dofs_node_major(), libMesh::DofMap::distribute_local_dofs_var_major(), libMesh::EnsightIO::EnsightIO(), libMesh::MeshBase::get_info(), libMesh::SystemSubsetBySubdomain::init(), libMesh::PetscDMWrapper::init_and_attach_petscdm(), libMesh::Nemesis_IO_Helper::initialize(), libMesh::DistributedMesh::insert_elem(), libMesh::MeshTools::libmesh_assert_contiguous_dof_ids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_new_node_procids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Elem >(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Node >(), libMesh::MeshTools::libmesh_assert_topology_consistent_procids< Node >(), libMesh::MeshTools::libmesh_assert_valid_boundary_ids(), libMesh::MeshTools::libmesh_assert_valid_dof_ids(), libMesh::MeshTools::libmesh_assert_valid_neighbors(), libMesh::MeshTools::libmesh_assert_valid_refinement_flags(), libMesh::DofMap::local_variable_indices(), libMesh::MeshRefinement::make_coarsening_compatible(), libMesh::MeshBase::n_active_elem_on_proc(), libMesh::MeshBase::n_elem_on_proc(), libMesh::MeshBase::n_nodes_on_proc(), libMesh::MeshBase::partition(), libMesh::PetscLinearSolver< T >::PetscLinearSolver(), libMesh::System::point_gradient(), libMesh::System::point_hessian(), libMesh::System::point_value(), libMesh::NameBasedIO::read(), libMesh::Nemesis_IO::read(), libMesh::CheckpointIO::read(), libMesh::CheckpointIO::read_connectivity(), libMesh::XdrIO::read_header(), libMesh::CheckpointIO::read_nodes(), libMesh::System::read_parallel_data(), libMesh::System::read_SCALAR_dofs(), libMesh::System::read_serialized_blocked_dof_objects(), libMesh::System::read_serialized_vector(), libMesh::DistributedMesh::renumber_dof_objects(), libMesh::DofMap::set_nonlocal_dof_objects(), libMesh::PetscDMWrapper::set_point_range_in_section(), libMesh::MeshRefinement::uniformly_coarsen(), libMesh::DistributedMesh::update_parallel_id_counts(), libMesh::GMVIO::write_binary(), libMesh::GMVIO::write_discontinuous_gmv(), libMesh::System::write_parallel_data(), libMesh::System::write_SCALAR_dofs(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::System::write_serialized_blocked_dof_objects(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::XdrIO::write_serialized_nodes(), and libMesh::XdrIO::write_serialized_nodesets().

96  { return cast_int<processor_id_type>(_communicator.size()); }
processor_id_type size() const
Definition: communicator.h:175
const Parallel::Communicator & _communicator

◆ need_full_sparsity_pattern()

template<typename T>
virtual bool libMesh::SparseMatrix< T >::need_full_sparsity_pattern ( ) const
inlinevirtualinherited
Returns
true if this sparse matrix format needs to be fed the graph of the sparse matrix.

This is true for LaspackMatrix, but not PetscMatrix. In the case where the full graph is not required, we can efficiently approximate it to provide a good estimate of the required size of the sparse matrix.

Reimplemented in libMesh::EpetraMatrix< T >, and libMesh::LaspackMatrix< T >.

Definition at line 121 of file sparse_matrix.h.

Referenced by libMesh::DofMap::attach_matrix().

122  { return false; }

◆ operator()()

template<typename T >
T libMesh::PetscMatrix< T >::operator() ( const numeric_index_type  i,
const numeric_index_type  j 
) const
overridevirtual
Returns
A copy of matrix entry (i,j).
Note
This may be an expensive operation, and you should always be careful where you call this function.

Implements libMesh::SparseMatrix< T >.

Definition at line 1134 of file petsc_matrix.C.

References libMesh::closed(), ierr, libMesh::initialized(), and value.

1136 {
1137  libmesh_assert (this->initialized());
1138 
1139  // PETSc 2.2.1 & newer
1140  const PetscScalar * petsc_row;
1141  const PetscInt * petsc_cols;
1142 
1143  // If the entry is not in the sparse matrix, it is 0.
1144  T value=0.;
1145 
1146  PetscErrorCode
1147  ierr=0;
1148  PetscInt
1149  ncols=0,
1150  i_val=static_cast<PetscInt>(i_in),
1151  j_val=static_cast<PetscInt>(j_in);
1152 
1153 
1154  // the matrix needs to be closed for this to work
1155  // this->close();
1156  // but closing it is a semiparallel operation; we want operator()
1157  // to run on one processor.
1158  libmesh_assert(this->closed());
1159 
1160  ierr = MatGetRow(_mat, i_val, &ncols, &petsc_cols, &petsc_row);
1161  LIBMESH_CHKERR(ierr);
1162 
1163  // Perform a binary search to find the contiguous index in
1164  // petsc_cols (resp. petsc_row) corresponding to global index j_val
1165  std::pair<const PetscInt *, const PetscInt *> p =
1166  std::equal_range (petsc_cols, petsc_cols + ncols, j_val);
1167 
1168  // Found an entry for j_val
1169  if (p.first != p.second)
1170  {
1171  // The entry in the contiguous row corresponding
1172  // to the j_val column of interest
1173  const std::size_t j =
1174  std::distance (const_cast<PetscInt *>(petsc_cols),
1175  const_cast<PetscInt *>(p.first));
1176 
1177  libmesh_assert_less (static_cast<PetscInt>(j), ncols);
1178  libmesh_assert_equal_to (petsc_cols[j], j_val);
1179 
1180  value = static_cast<T> (petsc_row[j]);
1181  }
1182 
1183  ierr = MatRestoreRow(_mat, i_val,
1184  &ncols, &petsc_cols, &petsc_row);
1185  LIBMESH_CHKERR(ierr);
1186 
1187  return value;
1188 }
virtual bool initialized() const
virtual bool closed() const override
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ operator=() [1/2]

template<typename T>
PetscMatrix& libMesh::PetscMatrix< T >::operator= ( const PetscMatrix< T > &  )
delete

◆ operator=() [2/2]

template<typename T>
PetscMatrix& libMesh::PetscMatrix< T >::operator= ( PetscMatrix< T > &&  )
delete

◆ print() [1/2]

template<>
void libMesh::SparseMatrix< Complex >::print ( std::ostream &  os,
const bool  sparse 
) const
inherited

Definition at line 96 of file sparse_matrix.C.

97 {
98  // std::complex<>::operator<<() is defined, but use this form
99 
100  if (sparse)
101  {
102  libmesh_not_implemented();
103  }
104 
105  os << "Real part:" << std::endl;
106  for (numeric_index_type i=0; i<this->m(); i++)
107  {
108  for (numeric_index_type j=0; j<this->n(); j++)
109  os << std::setw(8) << (*this)(i,j).real() << " ";
110  os << std::endl;
111  }
112 
113  os << std::endl << "Imaginary part:" << std::endl;
114  for (numeric_index_type i=0; i<this->m(); i++)
115  {
116  for (numeric_index_type j=0; j<this->n(); j++)
117  os << std::setw(8) << (*this)(i,j).imag() << " ";
118  os << std::endl;
119  }
120 }
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type m() const =0
virtual numeric_index_type n() const =0

◆ print() [2/2]

template<typename T >
void libMesh::SparseMatrix< T >::print ( std::ostream &  os = libMesh::out,
const bool  sparse = false 
) const
inherited

Print the contents of the matrix to the screen in a uniform style, regardless of matrix/solver package being used.

Definition at line 200 of file sparse_matrix.C.

Referenced by libMesh::EigenSparseMatrix< T >::print_personal(), and libMesh::LaspackMatrix< T >::print_personal().

201 {
202  parallel_object_only();
203 
204  libmesh_assert (this->initialized());
205 
206  if (!this->_dof_map)
207  libmesh_error_msg("Error! Trying to print a matrix with no dof_map set!");
208 
209  // We'll print the matrix from processor 0 to make sure
210  // it's serialized properly
211  if (this->processor_id() == 0)
212  {
213  libmesh_assert_equal_to (this->_dof_map->first_dof(), 0);
214  for (numeric_index_type i=this->_dof_map->first_dof();
215  i!=this->_dof_map->end_dof(); ++i)
216  {
217  if (sparse)
218  {
219  for (numeric_index_type j=0; j<this->n(); j++)
220  {
221  T c = (*this)(i,j);
222  if (c != static_cast<T>(0.0))
223  {
224  os << i << " " << j << " " << c << std::endl;
225  }
226  }
227  }
228  else
229  {
230  for (numeric_index_type j=0; j<this->n(); j++)
231  os << (*this)(i,j) << " ";
232  os << std::endl;
233  }
234  }
235 
236  std::vector<numeric_index_type> ibuf, jbuf;
237  std::vector<T> cbuf;
238  numeric_index_type currenti = this->_dof_map->end_dof();
239  for (processor_id_type p=1; p < this->n_processors(); ++p)
240  {
241  this->comm().receive(p, ibuf);
242  this->comm().receive(p, jbuf);
243  this->comm().receive(p, cbuf);
244  libmesh_assert_equal_to (ibuf.size(), jbuf.size());
245  libmesh_assert_equal_to (ibuf.size(), cbuf.size());
246 
247  if (ibuf.empty())
248  continue;
249  libmesh_assert_greater_equal (ibuf.front(), currenti);
250  libmesh_assert_greater_equal (ibuf.back(), ibuf.front());
251 
252  std::size_t currentb = 0;
253  for (;currenti <= ibuf.back(); ++currenti)
254  {
255  if (sparse)
256  {
257  for (numeric_index_type j=0; j<this->n(); j++)
258  {
259  if (currentb < ibuf.size() &&
260  ibuf[currentb] == currenti &&
261  jbuf[currentb] == j)
262  {
263  os << currenti << " " << j << " " << cbuf[currentb] << std::endl;
264  currentb++;
265  }
266  }
267  }
268  else
269  {
270  for (numeric_index_type j=0; j<this->n(); j++)
271  {
272  if (currentb < ibuf.size() &&
273  ibuf[currentb] == currenti &&
274  jbuf[currentb] == j)
275  {
276  os << cbuf[currentb] << " ";
277  currentb++;
278  }
279  else
280  os << static_cast<T>(0.0) << " ";
281  }
282  os << std::endl;
283  }
284  }
285  }
286  if (!sparse)
287  {
288  for (; currenti != this->m(); ++currenti)
289  {
290  for (numeric_index_type j=0; j<this->n(); j++)
291  os << static_cast<T>(0.0) << " ";
292  os << std::endl;
293  }
294  }
295  }
296  else
297  {
298  std::vector<numeric_index_type> ibuf, jbuf;
299  std::vector<T> cbuf;
300 
301  // We'll assume each processor has access to entire
302  // matrix rows, so (*this)(i,j) is valid if i is a local index.
303  for (numeric_index_type i=this->_dof_map->first_dof();
304  i!=this->_dof_map->end_dof(); ++i)
305  {
306  for (numeric_index_type j=0; j<this->n(); j++)
307  {
308  T c = (*this)(i,j);
309  if (c != static_cast<T>(0.0))
310  {
311  ibuf.push_back(i);
312  jbuf.push_back(j);
313  cbuf.push_back(c);
314  }
315  }
316  }
317  this->comm().send(0,ibuf);
318  this->comm().send(0,jbuf);
319  this->comm().send(0,cbuf);
320  }
321 }
virtual bool initialized() const
void send(const unsigned int dest_processor_id, const T &buf, const MessageTag &tag=no_tag) const
uint8_t processor_id_type
Definition: id_types.h:99
const Parallel::Communicator & comm() const
processor_id_type n_processors() const
dof_id_type numeric_index_type
Definition: id_types.h:92
virtual numeric_index_type m() const =0
DofMap const * _dof_map
Status receive(const unsigned int dest_processor_id, T &buf, const MessageTag &tag=any_tag) const
dof_id_type first_dof(const processor_id_type proc) const
Definition: dof_map.h:599
dof_id_type end_dof(const processor_id_type proc) const
Definition: dof_map.h:641
processor_id_type processor_id() const
virtual numeric_index_type n() const =0

◆ print_info()

void libMesh::ReferenceCounter::print_info ( std::ostream &  out = libMesh::out)
staticinherited

Prints the reference information, by default to libMesh::out.

Definition at line 87 of file reference_counter.C.

References libMesh::ReferenceCounter::_enable_print_counter, and libMesh::ReferenceCounter::get_info().

88 {
90  out_stream << ReferenceCounter::get_info();
91 }
static std::string get_info()

◆ print_matlab()

template<typename T >
void libMesh::PetscMatrix< T >::print_matlab ( const std::string &  = "") const
overridevirtual

Print the contents of the matrix in Matlab's sparse matrix format. Optionally prints the matrix to the file named name. If name is not specified it is dumped to the screen.

Create an ASCII file containing the matrix if a filename was provided.

Otherwise the matrix will be dumped to the screen.

Destroy the viewer.

Reimplemented from libMesh::SparseMatrix< T >.

Definition at line 592 of file petsc_matrix.C.

References libMesh::closed(), ierr, libMesh::initialized(), and libMesh::Quality::name().

593 {
594  libmesh_assert (this->initialized());
595 
596  semiparallel_only();
597 
598  if (!this->closed())
599  {
600  libmesh_deprecated();
601  libmesh_warning("The matrix must be assembled before calling PetscMatrix::print_matlab().\n"
602  "Please update your code, as this warning will become an error in a future release.");
603  const_cast<PetscMatrix<T> *>(this)->close();
604  }
605 
606  PetscErrorCode ierr=0;
607  PetscViewer petsc_viewer;
608 
609 
610  ierr = PetscViewerCreate (this->comm().get(),
611  &petsc_viewer);
612  LIBMESH_CHKERR(ierr);
613 
618  if (name != "")
619  {
620  ierr = PetscViewerASCIIOpen( this->comm().get(),
621  name.c_str(),
622  &petsc_viewer);
623  LIBMESH_CHKERR(ierr);
624 
625 #if PETSC_VERSION_LESS_THAN(3,7,0)
626  ierr = PetscViewerSetFormat (petsc_viewer,
627  PETSC_VIEWER_ASCII_MATLAB);
628 #else
629  ierr = PetscViewerPushFormat (petsc_viewer,
630  PETSC_VIEWER_ASCII_MATLAB);
631 #endif
632 
633  LIBMESH_CHKERR(ierr);
634 
635  ierr = MatView (_mat, petsc_viewer);
636  LIBMESH_CHKERR(ierr);
637  }
638 
642  else
643  {
644 #if PETSC_VERSION_LESS_THAN(3,7,0)
645  ierr = PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
646  PETSC_VIEWER_ASCII_MATLAB);
647 #else
648  ierr = PetscViewerPushFormat (PETSC_VIEWER_STDOUT_WORLD,
649  PETSC_VIEWER_ASCII_MATLAB);
650 #endif
651 
652  LIBMESH_CHKERR(ierr);
653 
654  ierr = MatView (_mat, PETSC_VIEWER_STDOUT_WORLD);
655  LIBMESH_CHKERR(ierr);
656  }
657 
658 
662  ierr = LibMeshPetscViewerDestroy (&petsc_viewer);
663  LIBMESH_CHKERR(ierr);
664 }
std::string name(const ElemQuality q)
Definition: elem_quality.C:42
virtual bool initialized() const
const Parallel::Communicator & comm() const
virtual bool closed() const override
PetscErrorCode ierr
SparseMatrix interface to PETSc Mat.
virtual void close() override
Definition: petsc_matrix.C:957

◆ print_personal()

template<typename T >
void libMesh::PetscMatrix< T >::print_personal ( std::ostream &  os = libMesh::out) const
overridevirtual

Print the contents of the matrix to the screen with the PETSc viewer. This function only allows printing to standard out since we have limited ourselves to one PETSc implementation for writing.

Implements libMesh::SparseMatrix< T >.

Definition at line 671 of file petsc_matrix.C.

References libMesh::closed(), ierr, and libMesh::initialized().

672 {
673  libmesh_assert (this->initialized());
674 
675  // Routine must be called in parallel on parallel matrices
676  // and serial on serial matrices.
677  semiparallel_only();
678 
679  // #ifndef NDEBUG
680  // if (os != std::cout)
681  // libMesh::err << "Warning! PETSc can only print to std::cout!" << std::endl;
682  // #endif
683 
684  // Matrix must be in an assembled state to be printed
685  if (!this->closed())
686  {
687  libmesh_deprecated();
688  libmesh_warning("The matrix must be assembled before calling PetscMatrix::print_personal().\n"
689  "Please update your code, as this warning will become an error in a future release.");
690  const_cast<PetscMatrix<T> *>(this)->close();
691  }
692 
693  PetscErrorCode ierr=0;
694 
695  // Print to screen if ostream is stdout
696  if (os.rdbuf() == std::cout.rdbuf())
697  {
698  ierr = MatView(_mat, PETSC_VIEWER_STDOUT_SELF);
699  LIBMESH_CHKERR(ierr);
700  }
701 
702  // Otherwise, print to the requested file, in a roundabout way...
703  else
704  {
705  // We will create a temporary filename, and file, for PETSc to
706  // write to.
707  std::string temp_filename;
708 
709  {
710  // Template for temporary filename
711  char c[] = "temp_petsc_matrix.XXXXXX";
712 
713  // Generate temporary, unique filename only on processor 0. We will
714  // use this filename for PetscViewerASCIIOpen, before copying it into
715  // the user's stream
716  if (this->processor_id() == 0)
717  {
718  int fd = mkstemp(c);
719 
720  // Check to see that mkstemp did not fail.
721  if (fd == -1)
722  libmesh_error_msg("mkstemp failed in PetscMatrix::print_personal()");
723 
724  // mkstemp returns a file descriptor for an open file,
725  // so let's close it before we hand it to PETSc!
726  ::close (fd);
727  }
728 
729  // Store temporary filename as string, makes it easier to broadcast
730  temp_filename = c;
731  }
732 
733  // Now broadcast the filename from processor 0 to all processors.
734  this->comm().broadcast(temp_filename);
735 
736  // PetscViewer object for passing to MatView
737  PetscViewer petsc_viewer;
738 
739  // This PETSc function only takes a string and handles the opening/closing
740  // of the file internally. Since print_personal() takes a reference to
741  // an ostream, we have to do an extra step... print_personal() should probably
742  // have a version that takes a string to get rid of this problem.
743  ierr = PetscViewerASCIIOpen( this->comm().get(),
744  temp_filename.c_str(),
745  &petsc_viewer);
746  LIBMESH_CHKERR(ierr);
747 
748  // Probably don't need to set the format if it's default...
749  // ierr = PetscViewerSetFormat (petsc_viewer,
750  // PETSC_VIEWER_DEFAULT);
751  // LIBMESH_CHKERR(ierr);
752 
753  // Finally print the matrix using the viewer
754  ierr = MatView (_mat, petsc_viewer);
755  LIBMESH_CHKERR(ierr);
756 
757  if (this->processor_id() == 0)
758  {
759  // Now the inefficient bit: open temp_filename as an ostream and copy the contents
760  // into the user's desired ostream. We can't just do a direct file copy, we don't even have the filename!
761  std::ifstream input_stream(temp_filename.c_str());
762  os << input_stream.rdbuf(); // The "most elegant" way to copy one stream into another.
763  // os.close(); // close not defined in ostream
764 
765  // Now remove the temporary file
766  input_stream.close();
767  std::remove(temp_filename.c_str());
768  }
769  }
770 }
virtual bool initialized() const
const Parallel::Communicator & comm() const
virtual bool closed() const override
PetscErrorCode ierr
SparseMatrix interface to PETSc Mat.
processor_id_type processor_id() const
virtual void close() override
Definition: petsc_matrix.C:957
void broadcast(T &data, const unsigned int root_id=0) const

◆ processor_id()

processor_id_type libMesh::ParallelObject::processor_id ( ) const
inlineinherited
Returns
The rank of this processor in the group.

Definition at line 101 of file parallel_object.h.

References libMesh::ParallelObject::_communicator, and libMesh::Parallel::Communicator::rank().

Referenced by libMesh::BoundaryInfo::_find_id_maps(), libMesh::EquationSystems::_read_impl(), libMesh::PetscDMWrapper::add_dofs_to_section(), libMesh::DistributedMesh::add_elem(), libMesh::BoundaryInfo::add_elements(), libMesh::DofMap::add_neighbors_to_send_list(), libMesh::DistributedMesh::add_node(), libMesh::UnstructuredMesh::all_second_order(), libMesh::MeshTools::Modification::all_tri(), libMesh::FEMSystem::assembly(), libMesh::EquationSystems::build_discontinuous_solution_vector(), libMesh::Nemesis_IO_Helper::build_element_and_node_maps(), libMesh::InfElemBuilder::build_inf_elem(), libMesh::BoundaryInfo::build_node_list_from_side_list(), libMesh::EquationSystems::build_parallel_elemental_solution_vector(), libMesh::DistributedMesh::clear(), libMesh::ExodusII_IO_Helper::close(), libMesh::Nemesis_IO_Helper::compute_border_node_ids(), libMesh::Nemesis_IO_Helper::compute_communication_map_parameters(), libMesh::Nemesis_IO_Helper::compute_internal_and_border_elems_and_internal_nodes(), libMesh::Nemesis_IO_Helper::compute_node_communication_maps(), libMesh::Nemesis_IO_Helper::compute_num_global_elem_blocks(), libMesh::Nemesis_IO_Helper::compute_num_global_nodesets(), libMesh::Nemesis_IO_Helper::compute_num_global_sidesets(), libMesh::Nemesis_IO_Helper::construct_nemesis_filename(), libMesh::MeshTools::correct_node_proc_ids(), libMesh::ExodusII_IO_Helper::create(), libMesh::DistributedMesh::delete_elem(), libMesh::DistributedMesh::delete_node(), libMesh::MeshCommunication::delete_remote_elements(), libMesh::DofMap::distribute_dofs(), libMesh::DofMap::distribute_local_dofs_node_major(), libMesh::DofMap::distribute_local_dofs_var_major(), libMesh::DistributedMesh::DistributedMesh(), libMesh::DofMap::end_dof(), libMesh::DofMap::end_old_dof(), libMesh::EnsightIO::EnsightIO(), libMesh::MeshFunction::find_element(), libMesh::MeshFunction::find_elements(), libMesh::UnstructuredMesh::find_neighbors(), libMesh::DofMap::first_dof(), libMesh::DofMap::first_old_dof(), libMesh::Nemesis_IO_Helper::get_cmap_params(), libMesh::Nemesis_IO_Helper::get_eb_info_global(), libMesh::Nemesis_IO_Helper::get_elem_cmap(), libMesh::Nemesis_IO_Helper::get_elem_map(), libMesh::MeshBase::get_info(), libMesh::DofMap::get_info(), libMesh::Nemesis_IO_Helper::get_init_global(), libMesh::Nemesis_IO_Helper::get_init_info(), libMesh::Nemesis_IO_Helper::get_loadbal_param(), libMesh::Nemesis_IO_Helper::get_node_cmap(), libMesh::Nemesis_IO_Helper::get_node_map(), libMesh::Nemesis_IO_Helper::get_ns_param_global(), libMesh::Nemesis_IO_Helper::get_ss_param_global(), libMesh::SparsityPattern::Build::handle_vi_vj(), libMesh::SystemSubsetBySubdomain::init(), libMesh::ExodusII_IO_Helper::initialize(), libMesh::ExodusII_IO_Helper::initialize_element_variables(), libMesh::ExodusII_IO_Helper::initialize_global_variables(), libMesh::ExodusII_IO_Helper::initialize_nodal_variables(), libMesh::DistributedMesh::insert_elem(), libMesh::DofMap::is_evaluable(), libMesh::SparsityPattern::Build::join(), libMesh::DofMap::last_dof(), libMesh::MeshTools::libmesh_assert_consistent_distributed(), libMesh::MeshTools::libmesh_assert_consistent_distributed_nodes(), libMesh::MeshTools::libmesh_assert_contiguous_dof_ids(), libMesh::MeshTools::libmesh_assert_parallel_consistent_procids< Elem >(), libMesh::MeshTools::libmesh_assert_valid_neighbors(), libMesh::DistributedMesh::libmesh_assert_valid_parallel_object_ids(), libMesh::DofMap::local_variable_indices(), libMesh::MeshRefinement::make_coarsening_compatible(), libMesh::MeshBase::n_active_local_elem(), libMesh::BoundaryInfo::n_boundary_conds(), libMesh::BoundaryInfo::n_edge_conds(), libMesh::DofMap::n_local_dofs(), libMesh::System::n_local_dofs(), libMesh::MeshBase::n_local_elem(), libMesh::MeshBase::n_local_nodes(), libMesh::BoundaryInfo::n_nodeset_conds(), libMesh::BoundaryInfo::n_shellface_conds(), libMesh::SparsityPattern::Build::operator()(), libMesh::DistributedMesh::own_node(), libMesh::System::point_gradient(), libMesh::System::point_hessian(), libMesh::System::point_value(), libMesh::Nemesis_IO_Helper::put_cmap_params(), libMesh::Nemesis_IO_Helper::put_elem_cmap(), libMesh::Nemesis_IO_Helper::put_elem_map(), libMesh::Nemesis_IO_Helper::put_loadbal_param(), libMesh::Nemesis_IO_Helper::put_node_cmap(), libMesh::Nemesis_IO_Helper::put_node_map(), libMesh::NameBasedIO::read(), libMesh::Nemesis_IO::read(), libMesh::XdrIO::read(), libMesh::CheckpointIO::read(), libMesh::ExodusII_IO_Helper::read_elem_num_map(), libMesh::ExodusII_IO_Helper::read_global_values(), libMesh::CheckpointIO::read_header(), libMesh::XdrIO::read_header(), libMesh::System::read_header(), libMesh::System::read_legacy_data(), libMesh::ExodusII_IO_Helper::read_node_num_map(), libMesh::System::read_parallel_data(), libMesh::System::read_SCALAR_dofs(), libMesh::XdrIO::read_serialized_bc_names(), libMesh::XdrIO::read_serialized_bcs_helper(), libMesh::System::read_serialized_blocked_dof_objects(), libMesh::XdrIO::read_serialized_connectivity(), libMesh::System::read_serialized_data(), libMesh::XdrIO::read_serialized_nodes(), libMesh::XdrIO::read_serialized_nodesets(), libMesh::XdrIO::read_serialized_subdomain_names(), libMesh::System::read_serialized_vector(), libMesh::System::read_serialized_vectors(), libMesh::DistributedMesh::renumber_dof_objects(), libMesh::CheckpointIO::select_split_config(), libMesh::DofMap::set_nonlocal_dof_objects(), libMesh::PetscDMWrapper::set_point_range_in_section(), libMesh::LaplaceMeshSmoother::smooth(), libMesh::MeshTools::total_weight(), libMesh::MeshRefinement::uniformly_coarsen(), libMesh::Parallel::Packing< T >::unpack(), libMesh::DistributedMesh::update_parallel_id_counts(), libMesh::NameBasedIO::write(), libMesh::XdrIO::write(), libMesh::CheckpointIO::write(), libMesh::EquationSystems::write(), libMesh::GMVIO::write_discontinuous_gmv(), libMesh::ExodusII_IO::write_element_data(), libMesh::ExodusII_IO_Helper::write_element_values(), libMesh::ExodusII_IO_Helper::write_elements(), libMesh::ExodusII_IO::write_global_data(), libMesh::ExodusII_IO_Helper::write_global_values(), libMesh::System::write_header(), libMesh::ExodusII_IO::write_information_records(), libMesh::ExodusII_IO_Helper::write_information_records(), libMesh::ExodusII_IO_Helper::write_nodal_coordinates(), libMesh::UCDIO::write_nodal_data(), libMesh::ExodusII_IO::write_nodal_data(), libMesh::ExodusII_IO::write_nodal_data_discontinuous(), libMesh::ExodusII_IO_Helper::write_nodal_values(), libMesh::Nemesis_IO_Helper::write_nodesets(), libMesh::ExodusII_IO_Helper::write_nodesets(), libMesh::System::write_parallel_data(), libMesh::System::write_SCALAR_dofs(), libMesh::XdrIO::write_serialized_bc_names(), libMesh::XdrIO::write_serialized_bcs_helper(), libMesh::System::write_serialized_blocked_dof_objects(), libMesh::XdrIO::write_serialized_connectivity(), libMesh::System::write_serialized_data(), libMesh::XdrIO::write_serialized_nodes(), libMesh::XdrIO::write_serialized_nodesets(), libMesh::XdrIO::write_serialized_subdomain_names(), libMesh::System::write_serialized_vector(), libMesh::System::write_serialized_vectors(), libMesh::Nemesis_IO_Helper::write_sidesets(), libMesh::ExodusII_IO_Helper::write_sidesets(), libMesh::ExodusII_IO::write_timestep(), libMesh::ExodusII_IO_Helper::write_timestep(), and libMesh::ExodusII_IO::write_timestep_discontinuous().

102  { return cast_int<processor_id_type>(_communicator.rank()); }
const Parallel::Communicator & _communicator
processor_id_type rank() const
Definition: communicator.h:173

◆ reinit_submatrix()

template<typename T>
virtual void libMesh::SparseMatrix< T >::reinit_submatrix ( SparseMatrix< T > &  submatrix,
const std::vector< numeric_index_type > &  rows,
const std::vector< numeric_index_type > &  cols 
) const
inlinevirtualinherited

This function is similar to the one above, but it allows you to reuse the existing sparsity pattern of "submatrix" instead of reallocating it again. This should hopefully be more efficient if you are frequently extracting submatrices of the same size.

Definition at line 370 of file sparse_matrix.h.

373  {
374  this->_get_submatrix(submatrix,
375  rows,
376  cols,
377  true); // true means REUSE submatrix
378  }
virtual void _get_submatrix(SparseMatrix< T > &, const std::vector< numeric_index_type > &, const std::vector< numeric_index_type > &, const bool) const

◆ row_start()

template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::row_start ( ) const
overridevirtual
Returns
The index of the first matrix row stored on this processor.

Implements libMesh::SparseMatrix< T >.

Definition at line 1024 of file petsc_matrix.C.

References ierr, libMesh::initialized(), and libMesh::MacroFunctions::stop().

1025 {
1026  libmesh_assert (this->initialized());
1027 
1028  PetscInt start=0, stop=0;
1029  PetscErrorCode ierr=0;
1030 
1031  ierr = MatGetOwnershipRange(_mat, &start, &stop);
1032  LIBMESH_CHKERR(ierr);
1033 
1034  return static_cast<numeric_index_type>(start);
1035 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:92
void stop(const char *file, int line, const char *date, const char *time)
PetscErrorCode ierr

◆ row_stop()

template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::row_stop ( ) const
overridevirtual
Returns
The index of the last matrix row (+1) stored on this processor.

Implements libMesh::SparseMatrix< T >.

Definition at line 1040 of file petsc_matrix.C.

References ierr, libMesh::initialized(), and libMesh::MacroFunctions::stop().

1041 {
1042  libmesh_assert (this->initialized());
1043 
1044  PetscInt start=0, stop=0;
1045  PetscErrorCode ierr=0;
1046 
1047  ierr = MatGetOwnershipRange(_mat, &start, &stop);
1048  LIBMESH_CHKERR(ierr);
1049 
1050  return static_cast<numeric_index_type>(stop);
1051 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:92
void stop(const char *file, int line, const char *date, const char *time)
PetscErrorCode ierr

◆ set()

template<typename T >
void libMesh::PetscMatrix< T >::set ( const numeric_index_type  i,
const numeric_index_type  j,
const T  value 
)
overridevirtual

Set the element (i,j) to value. Throws an error if the entry does not exist. Zero values can be "stored" in non-existent fields.

Implements libMesh::SparseMatrix< T >.

Definition at line 1056 of file petsc_matrix.C.

References ierr, libMesh::initialized(), and value.

1059 {
1060  libmesh_assert (this->initialized());
1061 
1062  PetscErrorCode ierr=0;
1063  PetscInt i_val=i, j_val=j;
1064 
1065  PetscScalar petsc_value = static_cast<PetscScalar>(value);
1066  ierr = MatSetValues(_mat, 1, &i_val, 1, &j_val,
1067  &petsc_value, INSERT_VALUES);
1068  LIBMESH_CHKERR(ierr);
1069 }
virtual bool initialized() const
PetscErrorCode ierr
static const bool value
Definition: xdr_io.C:109

◆ set_matrix_type()

template<typename T >
void libMesh::PetscMatrix< T >::set_matrix_type ( PetscMatrixType  mat_type)

Definition at line 121 of file petsc_matrix.C.

122 {
123  _mat_type = mat_type;
124 }
PetscMatrixType _mat_type
Definition: petsc_matrix.h:279

◆ swap()

template<typename T >
void libMesh::PetscMatrix< T >::swap ( PetscMatrix< T > &  m_in)

Swaps the internal data pointers of two PetscMatrices, no actual values are swapped.

Definition at line 1210 of file petsc_matrix.C.

References libMesh::PetscMatrix< T >::_destroy_mat_on_exit, libMesh::PetscMatrix< T >::_mat, and swap().

Referenced by libMesh::__libmesh_petsc_diff_solver_jacobian().

1211 {
1212  std::swap(_mat, m_in._mat);
1214 }
void swap(Iterator &lhs, Iterator &rhs)

◆ update_preallocation_and_zero()

template<typename T >
void libMesh::PetscMatrix< T >::update_preallocation_and_zero ( )

Update the sparsity pattern based on dof_map, and set the matrix to zero. This is useful in cases where the sparsity pattern changes during a computation.

Definition at line 468 of file petsc_matrix.C.

469 {
470  libmesh_not_implemented();
471 }

◆ update_sparsity_pattern()

template<typename T>
virtual void libMesh::SparseMatrix< T >::update_sparsity_pattern ( const SparsityPattern::Graph )
inlinevirtualinherited

Updates the matrix sparsity pattern. When your SparseMatrix<T> implementation does not need this data, simply do not override this method.

Reimplemented in libMesh::EpetraMatrix< T >, and libMesh::LaspackMatrix< T >.

Definition at line 129 of file sparse_matrix.h.

Referenced by libMesh::DofMap::attach_matrix().

129 {}

◆ vector_mult()

template<typename T>
void libMesh::SparseMatrix< T >::vector_mult ( NumericVector< T > &  dest,
const NumericVector< T > &  arg 
) const
inherited

Multiplies the matrix by the NumericVector arg and stores the result in NumericVector dest.

Definition at line 170 of file sparse_matrix.C.

Referenced by libMesh::ImplicitSystem::qoi_parameter_hessian(), and libMesh::ImplicitSystem::qoi_parameter_hessian_vector_product().

172 {
173  dest.zero();
174  this->vector_mult_add(dest,arg);
175 }
void vector_mult_add(NumericVector< T > &dest, const NumericVector< T > &arg) const

◆ vector_mult_add()

template<typename T>
void libMesh::SparseMatrix< T >::vector_mult_add ( NumericVector< T > &  dest,
const NumericVector< T > &  arg 
) const
inherited

Multiplies the matrix by the NumericVector arg and adds the result to the NumericVector dest.

Definition at line 180 of file sparse_matrix.C.

Referenced by libMesh::ImplicitSystem::weighted_sensitivity_adjoint_solve().

182 {
183  /* This functionality is actually implemented in the \p
184  NumericVector class. */
185  dest.add_vector(arg,*this);
186 }

◆ zero()

template<typename T >
void libMesh::PetscMatrix< T >::zero ( )
overridevirtual

Set all entries to 0.

Implements libMesh::SparseMatrix< T >.

Definition at line 475 of file petsc_matrix.C.

References ierr, and libMesh::initialized().

476 {
477  libmesh_assert (this->initialized());
478 
479  semiparallel_only();
480 
481  PetscErrorCode ierr=0;
482 
483  PetscInt m_l, n_l;
484 
485  ierr = MatGetLocalSize(_mat,&m_l,&n_l);
486  LIBMESH_CHKERR(ierr);
487 
488  if (n_l)
489  {
490  ierr = MatZeroEntries(_mat);
491  LIBMESH_CHKERR(ierr);
492  }
493 }
virtual bool initialized() const
PetscErrorCode ierr

◆ zero_rows()

template<typename T >
void libMesh::PetscMatrix< T >::zero_rows ( std::vector< numeric_index_type > &  rows,
diag_value = 0.0 
)
overridevirtual

Sets all row entries to 0 then puts diag_value in the diagonal entry.

Reimplemented from libMesh::SparseMatrix< T >.

Definition at line 496 of file petsc_matrix.C.

References ierr, libMesh::initialized(), and libMesh::numeric_petsc_cast().

497 {
498  libmesh_assert (this->initialized());
499 
500  semiparallel_only();
501 
502  PetscErrorCode ierr=0;
503 
504 #if PETSC_RELEASE_LESS_THAN(3,1,1)
505  if (!rows.empty())
506  ierr = MatZeroRows(_mat, rows.size(),
507  numeric_petsc_cast(rows.data()), diag_value);
508  else
509  ierr = MatZeroRows(_mat, 0, PETSC_NULL, diag_value);
510 #else
511  // As of petsc-dev at the time of 3.1.0, MatZeroRows now takes two additional
512  // optional arguments. The optional arguments (x,b) can be used to specify the
513  // solutions for the zeroed rows (x) and right hand side (b) to update.
514  // Could be useful for setting boundary conditions...
515  if (!rows.empty())
516  ierr = MatZeroRows(_mat, cast_int<PetscInt>(rows.size()),
517  numeric_petsc_cast(rows.data()), diag_value,
518  PETSC_NULL, PETSC_NULL);
519  else
520  ierr = MatZeroRows(_mat, 0, PETSC_NULL, diag_value, PETSC_NULL,
521  PETSC_NULL);
522 #endif
523 
524  LIBMESH_CHKERR(ierr);
525 }
virtual bool initialized() const
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
PetscErrorCode ierr

Member Data Documentation

◆ _communicator

◆ _counts

ReferenceCounter::Counts libMesh::ReferenceCounter::_counts
staticprotectedinherited

◆ _destroy_mat_on_exit

template<typename T>
bool libMesh::PetscMatrix< T >::_destroy_mat_on_exit
private

This boolean value should only be set to false for the constructor which takes a PETSc Mat object.

Definition at line 277 of file petsc_matrix.h.

Referenced by libMesh::PetscMatrix< T >::swap().

◆ _dof_map

template<typename T>
DofMap const* libMesh::SparseMatrix< T >::_dof_map
protectedinherited

The DofMap object associated with this object.

Definition at line 425 of file sparse_matrix.h.

Referenced by libMesh::SparseMatrix< ValOut >::attach_dof_map().

◆ _enable_print_counter

bool libMesh::ReferenceCounter::_enable_print_counter = true
staticprotectedinherited

Flag to control whether reference count information is printed when print_info is called.

Definition at line 141 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::disable_print_counter_info(), libMesh::ReferenceCounter::enable_print_counter_info(), and libMesh::ReferenceCounter::print_info().

◆ _is_initialized

template<typename T>
bool libMesh::SparseMatrix< T >::_is_initialized
protectedinherited

◆ _mat

◆ _mat_type

template<typename T>
PetscMatrixType libMesh::PetscMatrix< T >::_mat_type
private

Definition at line 279 of file petsc_matrix.h.

◆ _mutex

Threads::spin_mutex libMesh::ReferenceCounter::_mutex
staticprotectedinherited

Mutual exclusion object to enable thread-safe reference counting.

Definition at line 135 of file reference_counter.h.

◆ _n_objects

Threads::atomic< unsigned int > libMesh::ReferenceCounter::_n_objects
staticprotectedinherited

The number of objects. Print the reference count information when the number returns to 0.

Definition at line 130 of file reference_counter.h.

Referenced by libMesh::ReferenceCounter::n_objects(), libMesh::ReferenceCounter::ReferenceCounter(), and libMesh::ReferenceCounter::~ReferenceCounter().


The documentation for this class was generated from the following files: