libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
Public 成员函数 | 静态 Public 成员函数 | Protected 类型 | Protected 成员函数 | Protected 属性 | 静态 Protected 属性 | Private 属性 | 所有成员列表
libMesh::PetscMatrix< T > 模板类 参考final

这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。 更多...

#include <petsc_matrix.h>

类 libMesh::PetscMatrix< T > 继承关系图:
[图例]

Public 成员函数

 PetscMatrix (const Parallel::Communicator &comm_in)
 构造函数:将矩阵初始化为空,没有任何结构。这个构造函数只适用于类的成员矩阵。 所有其他矩阵应该在所有必要信息都可用的数据流中的某一点创建。 更多...
 
 PetscMatrix (Mat m, const Parallel::Communicator &comm_in)
 构造函数:创建一个 PetscMatrix,假设您已经有一个有效的 Mat 对象。 在这种情况下,矩阵 m 不会在 PetscMatrix 析构函数中销毁,这允许 m 的所有权保留给原始创建者,并为 PetscMatrix 提供额外的功能。 更多...
 
 PetscMatrix (PetscMatrix &&)=delete
 删除默认生成的复制和移动函数,以及默认析构函数,因为此类手动管理 C 风格的 Mat 结构。 更多...
 
 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
 初始化 PETSc 矩阵。 更多...
 
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)
 初始化 PETSc 矩阵。 更多...
 
virtual void init (ParallelType=PARALLEL) override
 使用由 dof_map 计算的稀疏性结构初始化此矩阵。 更多...
 
void update_preallocation_and_zero ()
 更新基于 dof_map 的稀疏模式,并将矩阵置零。这在稀疏模式在计算过程中发生变化的情况下很有用。 更多...
 
void reset_preallocation ()
 重置矩阵以使用用户提供的原始非零模式。 更多...
 
virtual void clear () noexceptoverride
 clear() 在析构函数中调用,因此它不应该抛出异常。 更多...
 
virtual void zero () override
 将矩阵所有元素置为零。 更多...
 
virtual std::unique_ptr
< SparseMatrix< T > > 
zero_clone () const override
 创建一个新的空矩阵,并将其所有元素置为零。 更多...
 
virtual std::unique_ptr
< SparseMatrix< T > > 
clone () const 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 local_m () const final
 获取由该进程拥有的矩阵行数。 更多...
 
virtual numeric_index_type n () const override
 获取矩阵的列数。 更多...
 
numeric_index_type local_n () const
 获取由该进程拥有的矩阵列数。 更多...
 
void get_local_size (numeric_index_type &m, numeric_index_type &n) const
 获取由该进程拥有的矩阵行数和列数。 更多...
 
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
 计算 A += a*X,其中 a 是标量,X 是矩阵。 更多...
 
virtual void matrix_matrix_mult (SparseMatrix< T > &X, SparseMatrix< T > &Y, bool reuse=false) override
 计算 Y = A*X,其中 A 是当前矩阵,X 是另一个矩阵。 更多...
 
virtual void add_sparse_matrix (const SparseMatrix< T > &spm, const std::map< numeric_index_type, numeric_index_type > &row_ltog, const std::map< numeric_index_type, numeric_index_type > &col_ltog, const T scalar) override
 将标量乘以稀疏矩阵 spm 添加到此矩阵的指定行和列。 更多...
 
virtual T operator() (const numeric_index_type i, const numeric_index_type j) const override
 获取矩阵中特定位置的值。 更多...
 
virtual Real l1_norm () const override
 计算矩阵的 L1 范数。 更多...
 
virtual Real linfty_norm () const override
 计算矩阵的无穷范数。 更多...
 
virtual bool closed () const override
 检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。 更多...
 
virtual void set_destroy_mat_on_exit (bool destroy=true)
 设置析构时是否删除 Mat 对象。如果设置为 false,则允许 PETSc 管理它。 更多...
 
virtual void print_personal (std::ostream &os=libMesh::out) const override
 使用 PETSc 查看器将矩阵的内容打印到屏幕。 由于我们限制了一个 PETSc 实现,所以此函数仅允许打印到标准输出。 更多...
 
virtual void print_matlab (const std::string &name="") const override
 将矩阵内容以 MATLAB 格式打印出来。 更多...
 
virtual void get_diagonal (NumericVector< T > &dest) const override
 获取矩阵的对角线并将其存储在给定的向量中。 更多...
 
virtual void get_transpose (SparseMatrix< T > &dest) const override
 获取矩阵的转置。 更多...
 
void swap (PetscMatrix< T > &)
 交换两个 PetscMatrix 的内部数据指针,不交换实际值。 更多...
 
Mat mat ()
 返回原始的 PETSc 矩阵指针。 更多...
 
virtual void get_row (numeric_index_type i, std::vector< numeric_index_type > &indices, std::vector< T > &values) const override
 获取矩阵中指定行的索引和值。 更多...
 
virtual void create_submatrix_nosort (SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const override
 根据给定的 rowscols 向量中的索引创建一个子矩阵,类似于 create_submatrix 函数。 更多...
 
virtual bool initialized () const
 
void attach_dof_map (const DofMap &dof_map)
 设置要使用的 DofMap 的指针。如果不使用单独的稀疏性模式, 则使用来自 DofMap 的模式。 更多...
 
void attach_sparsity_pattern (const SparsityPattern::Build &sp)
 设置要使用的稀疏性模式的指针。在矩阵需要比系统中的大(或更小以提高效率)的模式, 或者在 DofMap 未计算系统稀疏性模式的情况下使用。 更多...
 
virtual bool need_full_sparsity_pattern () const
 
virtual void update_sparsity_pattern (const SparsityPattern::Graph &)
 更新矩阵的稀疏性模式。当您的 SparseMatrix<T> 实现不需要此数据时, 只需不覆盖此方法。 更多...
 
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
 此函数创建一个名为 "submatrix" 的矩阵,其定义由 "rows" 和 "cols" 条目中的行和列索引给定。 目前,此操作仅对 PetscMatrix 类型定义。 注意:rowscols 向量需要排序; 如果 rowscols 向量未排序,则使用下面的 nosort 版本; rowscols 仅包含由此处理器拥有的索引。 更多...
 
virtual void reinit_submatrix (SparseMatrix< T > &submatrix, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) const
 此函数与上述函数类似,但允许您重用 "submatrix" 的现有稀疏性模式,而不是再次分配它。 如果经常提取相同大小的子矩阵,这应该更有效。 更多...
 
void vector_mult (NumericVector< T > &dest, const NumericVector< T > &arg) const
 将矩阵乘以 NumericVector arg 并将结果存储在 NumericVector dest 中。 更多...
 
void vector_mult_add (NumericVector< T > &dest, const NumericVector< T > &arg) const
 将矩阵乘以 NumericVector arg ,并将结果添加到 NumericVector dest 中。 更多...
 

静态 Public 成员函数

static std::unique_ptr
< SparseMatrix< T > > 
build (const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package(), const MatrixBuildType matrix_build_type=MatrixBuildType::AUTOMATIC)
 使用由 solver_package 指定的线性求解器包构建一个 SparseMatrix<T>。 更多...
 
static std::string get_info ()
 Gets a string containing the reference information. 更多...
 
static void print_info (std::ostream &out_stream=libMesh::out)
 Prints the reference information, by default to libMesh::out. 更多...
 
static unsigned int n_objects ()
 Prints the number of outstanding (created, but not yet destroyed) objects. 更多...
 
static void enable_print_counter_info ()
 Methods to enable/disable the reference counter output from print_info() 更多...
 
static void disable_print_counter_info ()
 

Protected 类型

typedef std::map< std::string,
std::pair< unsigned int,
unsigned int > > 
Counts
 Data structure to log the information. 更多...
 

Protected 成员函数

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
 创建或重新初始化一个由 rowscols 向量中给出的索引定义的矩阵 submatrix。 更多...
 
void increment_constructor_count (const std::string &name) noexcept
 Increments the construction counter. 更多...
 
void increment_destructor_count (const std::string &name) noexcept
 Increments the destruction counter. 更多...
 

Protected 属性

DofMap const * _dof_map
 与此对象关联的 DofMap 对象。可以查询其在处理器上的自由度计数。 更多...
 
SparsityPattern::Build const * _sp
 与此对象关联的 sparsity pattern。在需要时, 应查询其入口计数(或使用 need_full_sparsity_pattern,模式)。 更多...
 
bool _is_initialized
 标志,指示矩阵是否已初始化。 更多...
 

静态 Protected 属性

static Counts _counts
 Actually holds the data. 更多...
 
static Threads::atomic
< unsigned int > 
_n_objects
 The number of objects. 更多...
 
static Threads::spin_mutex _mutex
 Mutual exclusion object to enable thread-safe reference counting. 更多...
 
static bool _enable_print_counter = true
 Flag to control whether reference count information is printed when print_info is called. 更多...
 

Private 属性

Mat _mat
 用于存储值的 PETSc 矩阵数据类型。 更多...
 
bool _destroy_mat_on_exit
 此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。 更多...
 
PetscMatrixType _mat_type
 存储 PETSc 矩阵的类型。 更多...
 
std::mutex _petsc_matrix_mutex
 用于线程安全的互斥锁(C++11 线程)。 更多...
 
Threads::spin_mutex _petsc_matrix_mutex
 用于线程安全的旋转锁。 更多...
 

详细描述

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

这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。

作者
Benjamin S. Kirk
日期
2002 SparseMatrix interface to PETSc Mat.

在文件 petsc_matrix.h92 行定义.

成员类型定义说明

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.

在文件 reference_counter.h119 行定义.

构造及析构函数说明

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

构造函数:将矩阵初始化为空,没有任何结构。这个构造函数只适用于类的成员矩阵。 所有其他矩阵应该在所有必要信息都可用的数据流中的某一点创建。

在使用之前,您必须使用 init(...) 初始化矩阵。

参数
comm_in通信器,指定了矩阵的并行性质。

在文件 petsc_matrix.C83 行定义.

83  :
84  SparseMatrix<T>(comm_in),
85  _mat(nullptr),
87  _mat_type(AIJ)
88 {}
bool _destroy_mat_on_exit
此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。
Definition: petsc_matrix.h:508
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
Definition: dof_map.h:66
PetscMatrixType _mat_type
存储 PETSc 矩阵的类型。
Definition: petsc_matrix.h:513
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
libMesh::PetscMatrix< T >::PetscMatrix ( Mat  m,
const Parallel::Communicator &  comm_in 
)
explicit

构造函数:创建一个 PetscMatrix,假设您已经有一个有效的 Mat 对象。 在这种情况下,矩阵 m 不会在 PetscMatrix 析构函数中销毁,这允许 m 的所有权保留给原始创建者,并为 PetscMatrix 提供额外的功能。

参数
m已有的 Mat 对象。
comm_in通信器,指定了矩阵的并行性质。

在文件 petsc_matrix.C95 行定义.

参考 libMesh::SparseMatrix< T >::_is_initialized , 以及 libMesh::PetscMatrix< T >::_mat.

96  :
97  SparseMatrix<T>(comm_in),
98  _destroy_mat_on_exit(false),
99  _mat_type(AIJ)
100 {
101  this->_mat = mat_in;
102  this->_is_initialized = true;
103 }
bool _destroy_mat_on_exit
此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。
Definition: petsc_matrix.h:508
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
Definition: dof_map.h:66
bool _is_initialized
标志,指示矩阵是否已初始化。
PetscMatrixType _mat_type
存储 PETSc 矩阵的类型。
Definition: petsc_matrix.h:513
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T>
libMesh::PetscMatrix< T >::PetscMatrix ( PetscMatrix< T > &&  )
delete

删除默认生成的复制和移动函数,以及默认析构函数,因为此类手动管理 C 风格的 Mat 结构。

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

析构函数,释放矩阵资源。

在文件 petsc_matrix.C109 行定义.

110 {
111  this->clear();
112 }
virtual void clear() noexceptoverride
clear() 在析构函数中调用,因此它不应该抛出异常。
Definition: petsc_matrix.C:503

成员函数说明

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

创建或重新初始化一个由 rowscols 向量中给出的索引定义的矩阵 submatrix。

参数
submatrix要创建或重新初始化的子矩阵。
rows定义子矩阵的行索引。
cols定义子矩阵的列索引。
reuse_submatrix确定 PETSc 是否将 submatrix 视为已使用过(已分配内存)的矩阵,还是新矩阵。
注解
此函数基于 MatGetSubMatrix() 实现。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C812 行定义.

参考 libMesh::SparseMatrix< T >::_is_initialized, libMesh::PetscMatrix< T >::_mat, libMesh::SparseMatrix< T >::clear(), libMesh::PetscMatrix< T >::close(), libMesh::closed(), libMesh::WrappedPetsc< T >::get(), libMesh::SparseMatrix< T >::initialized() , 以及 libMesh::numeric_petsc_cast().

816 {
817  if (!this->closed())
818  {
819  libmesh_deprecated();
820  libmesh_warning("The matrix must be assembled before calling PetscMatrix::create_submatrix().\n"
821  "Please update your code, as this warning will become an error in a future release.");
822  const_cast<PetscMatrix<T> *>(this)->close();
823  }
824 
825  // Make sure the SparseMatrix passed in is really a PetscMatrix
826  PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
827 
828  // If we're not reusing submatrix and submatrix is already initialized
829  // then we need to clear it, otherwise we get a memory leak.
830  if (!reuse_submatrix && submatrix.initialized())
831  submatrix.clear();
832 
833  // Construct row and column index sets.
834  PetscErrorCode ierr=0;
835 
836  WrappedPetsc<IS> isrow;
837  ierr = ISCreateGeneral(this->comm().get(),
838  cast_int<PetscInt>(rows.size()),
839  numeric_petsc_cast(rows.data()),
840  PETSC_USE_POINTER,
841  isrow.get()); LIBMESH_CHKERR(ierr);
842 
843  WrappedPetsc<IS> iscol;
844  ierr = ISCreateGeneral(this->comm().get(),
845  cast_int<PetscInt>(cols.size()),
846  numeric_petsc_cast(cols.data()),
847  PETSC_USE_POINTER,
848  iscol.get()); LIBMESH_CHKERR(ierr);
849 
850  // Extract submatrix
851  ierr = LibMeshCreateSubMatrix(_mat,
852  isrow,
853  iscol,
854  (reuse_submatrix ? MAT_REUSE_MATRIX : MAT_INITIAL_MATRIX),
855  &(petsc_submatrix->_mat)); LIBMESH_CHKERR(ierr);
856 
857  // Specify that the new submatrix is initialized and close it.
858  petsc_submatrix->_is_initialized = true;
859  petsc_submatrix->close();
860 }
virtual void clear()=0
恢复 SparseMatrix&lt;T&gt; 到原始状态。
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
virtual bool initialized() const
bool _is_initialized
标志,指示矩阵是否已初始化。
T * get()
获取托管对象的指针。 这用于模拟以下代码: KSP ksp; KSPCreate(comm, &amp;ksp); 因为在这种上下文中,取包装对象的地址是没有意义的。
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
virtual void close() override
完成任何挂起的插入或添加操作。
Definition: petsc_matrix.C:983
template<typename T >
void libMesh::PetscMatrix< T >::add ( const numeric_index_type  i,
const numeric_index_type  j,
const T  value 
)
overridevirtual

在矩阵的特定位置添加值。

参数
i行索引。
j列索引。
value要添加的值。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1127 行定义.

参考 libMesh::initialized().

1130 {
1131  libmesh_assert (this->initialized());
1132 
1133  PetscErrorCode ierr=0;
1134  PetscInt i_val=i, j_val=j;
1135 
1136  PetscScalar petsc_value = static_cast<PetscScalar>(value);
1137  ierr = MatSetValues(_mat, 1, &i_val, 1, &j_val,
1138  &petsc_value, ADD_VALUES);
1139  LIBMESH_CHKERR(ierr);
1140 }
virtual bool initialized() const
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::add ( const T  a,
const SparseMatrix< T > &  X 
)
overridevirtual

计算 A += a*X,其中 a 是标量,X 是矩阵。

参数
a标量值。
X要添加的矩阵。
注解
矩阵 A 和 X 需要有相同的非零模式,否则会引起 PETSc 崩溃。
建议在添加非零值到 X 时,不仅分配适当的内存,而且也显式地将 this 置零。
X 将在执行任何操作前被关闭,如果它尚未关闭的话。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1158 行定义.

参考 libMesh::PetscMatrix< T >::_mat, libMesh::PetscMatrix< T >::closed(), libMesh::initialized(), libMesh::SparseMatrix< T >::m() , 以及 libMesh::SparseMatrix< T >::n().

1159 {
1160  libmesh_assert (this->initialized());
1161 
1162  // sanity check. but this cannot avoid
1163  // crash due to incompatible sparsity structure...
1164  libmesh_assert_equal_to (this->m(), X_in.m());
1165  libmesh_assert_equal_to (this->n(), X_in.n());
1166 
1167  PetscScalar a = static_cast<PetscScalar> (a_in);
1168  const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1169 
1170  libmesh_assert (X);
1171 
1172  PetscErrorCode ierr=0;
1173 
1174  // the matrix from which we copy the values has to be assembled/closed
1175  libmesh_assert(X->closed());
1176 
1177  semiparallel_only();
1178 
1179  ierr = MatAXPY(_mat, a, X->_mat, DIFFERENT_NONZERO_PATTERN);
1180  LIBMESH_CHKERR(ierr);
1181 }
virtual numeric_index_type n() const override
获取矩阵的列数。
virtual bool initialized() const
virtual numeric_index_type m() const override
获取矩阵的行数。
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
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

将稠密块矩阵添加到此稀疏矩阵的指定块行和块列。

参数
dm要添加的稠密块矩阵。
brows稀疏矩阵中的块行索引。
bcols稀疏矩阵中的块列索引。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C768 行定义.

参考 libMesh::DenseMatrix< T >::get_values(), libMesh::initialized(), libMesh::DenseMatrixBase< T >::m(), libMesh::DenseMatrixBase< T >::n(), libMesh::numeric_petsc_cast() , 以及 libMesh::pPS().

771 {
772  libmesh_assert (this->initialized());
773 
774  const numeric_index_type n_brows =
775  cast_int<numeric_index_type>(brows.size());
776  const numeric_index_type n_bcols =
777  cast_int<numeric_index_type>(bcols.size());
778 
779  PetscErrorCode ierr=0;
780 
781 #ifndef NDEBUG
782  const numeric_index_type n_rows =
783  cast_int<numeric_index_type>(dm.m());
784  const numeric_index_type n_cols =
785  cast_int<numeric_index_type>(dm.n());
786  const numeric_index_type blocksize = n_rows / n_brows;
787 
788  libmesh_assert_equal_to (n_cols / n_bcols, blocksize);
789  libmesh_assert_equal_to (blocksize*n_brows, n_rows);
790  libmesh_assert_equal_to (blocksize*n_bcols, n_cols);
791 
792  PetscInt petsc_blocksize;
793  ierr = MatGetBlockSize(_mat, &petsc_blocksize);
794  LIBMESH_CHKERR(ierr);
795  libmesh_assert_equal_to (blocksize, static_cast<numeric_index_type>(petsc_blocksize));
796 #endif
797 
798  // These casts are required for PETSc <= 2.1.5
799  ierr = MatSetValuesBlocked(_mat,
800  n_brows, numeric_petsc_cast(brows.data()),
801  n_bcols, numeric_petsc_cast(bcols.data()),
802  pPS(const_cast<T*>(dm.get_values().data())),
803  ADD_VALUES);
804  LIBMESH_CHKERR(ierr);
805 }
unsigned int n() const
返回矩阵的列维度。
PetscScalar * pPS(T *ptr)
Definition: petsc_macro.h:172
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
unsigned int m() const
返回矩阵的行维度。
std::vector< T > & get_values()
返回对应于存储向量的引用的底层数据存储矢量。
Definition: dense_matrix.h:488
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T>
virtual void libMesh::PetscMatrix< T >::add_block_matrix ( const DenseMatrix< T > &  dm,
const std::vector< numeric_index_type > &  dof_indices 
)
overridevirtual

将稠密块矩阵添加到此稀疏矩阵的指定位置。

参数
dm要添加的稠密块矩阵。
dof_indices稀疏矩阵中的行和列索引。

重载 libMesh::SparseMatrix< T > .

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

将稠密矩阵添加到此稀疏矩阵的指定行和列。

参数
dm要添加的稠密矩阵。
rows稀疏矩阵中的行索引。
cols稀疏矩阵中的列索引。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C741 行定义.

参考 libMesh::DenseMatrix< T >::get_values(), libMesh::initialized(), libMesh::DenseMatrixBase< T >::m(), libMesh::DenseMatrixBase< T >::n(), libMesh::numeric_petsc_cast() , 以及 libMesh::pPS().

744 {
745  libmesh_assert (this->initialized());
746 
747  const numeric_index_type n_rows = dm.m();
748  const numeric_index_type n_cols = dm.n();
749 
750  libmesh_assert_equal_to (rows.size(), n_rows);
751  libmesh_assert_equal_to (cols.size(), n_cols);
752 
753  PetscErrorCode ierr=0;
754  ierr = MatSetValues(_mat,
755  n_rows, numeric_petsc_cast(rows.data()),
756  n_cols, numeric_petsc_cast(cols.data()),
757  pPS(const_cast<T*>(dm.get_values().data())),
758  ADD_VALUES);
759  LIBMESH_CHKERR(ierr);
760 }
unsigned int n() const
返回矩阵的列维度。
PetscScalar * pPS(T *ptr)
Definition: petsc_macro.h:172
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
unsigned int m() const
返回矩阵的行维度。
std::vector< T > & get_values()
返回对应于存储向量的引用的底层数据存储矢量。
Definition: dense_matrix.h:488
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::add_matrix ( const DenseMatrix< T > &  dm,
const std::vector< numeric_index_type > &  dof_indices 
)
overridevirtual

将稠密矩阵添加到此稀疏矩阵的指定位置。

参数
dm要添加的稠密矩阵。
dof_indices稀疏矩阵中的行和列索引。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1145 行定义.

1147 {
1148  this->add_matrix (dm, dof_indices, dof_indices);
1149 }
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:741
template<typename T >
void libMesh::PetscMatrix< T >::add_sparse_matrix ( const SparseMatrix< T > &  spm,
const std::map< numeric_index_type, numeric_index_type > &  row_ltog,
const std::map< numeric_index_type, numeric_index_type > &  col_ltog,
const T  scalar 
)
overridevirtual

将标量乘以稀疏矩阵 spm 添加到此矩阵的指定行和列。

参数
spm要添加的稀疏矩阵。
row_ltog行的本地到全局索引映射。
col_ltog列的本地到全局索引映射。
scalar乘以 spm 的标量值。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C1221 行定义.

参考 libMesh::closed(), libMesh::SparseMatrix< T >::m(), libMesh::SparseMatrix< T >::n() , 以及 libMesh::PS().

1225 {
1226  // size of spm is usually greater than row_ltog and col_ltog in parallel as the indices are owned by the processor
1227  // also, we should allow adding certain parts of spm to _mat
1228  libmesh_assert_greater_equal(spm.m(), row_ltog.size());
1229  libmesh_assert_greater_equal(spm.n(), col_ltog.size());
1230 
1231  // make sure matrix has larger size than spm
1232  libmesh_assert_greater_equal(this->m(), spm.m());
1233  libmesh_assert_greater_equal(this->n(), spm.n());
1234 
1235  if (!this->closed())
1236  this->close();
1237 
1238  PetscErrorCode ierr = 0;
1239 
1240  auto pscm = cast_ptr<const PetscMatrix<T> *>(&spm);
1241 
1242  PetscInt ncols = 0;
1243 
1244  const PetscInt * lcols;
1245  const PetscScalar * vals;
1246 
1247  std::vector<PetscInt> gcols;
1248  std::vector<PetscScalar> values;
1249 
1250  for (auto ltog : row_ltog)
1251  {
1252  PetscInt grow[] = {static_cast<PetscInt>(ltog.second)}; // global row index
1253 
1254  ierr = MatGetRow(pscm->_mat, static_cast<PetscInt>(ltog.first), &ncols, &lcols, &vals);
1255  LIBMESH_CHKERR(ierr);
1256 
1257  // get global indices (gcols) from lcols, increment values = vals*scalar
1258  gcols.resize(ncols);
1259  values.resize(ncols);
1260  for (auto i : index_range(gcols))
1261  {
1262  gcols[i] = libmesh_map_find(col_ltog, lcols[i]);
1263  values[i] = PS(scalar) * vals[i];
1264  }
1265 
1266  ierr = MatSetValues(_mat, 1, grow, ncols, gcols.data(), values.data(), ADD_VALUES);
1267  LIBMESH_CHKERR(ierr);
1268  ierr = MatRestoreRow(pscm->_mat, static_cast<PetscInt>(ltog.first), &ncols, &lcols, &vals);
1269  LIBMESH_CHKERR(ierr);
1270  }
1271  // Note: We are not closing the matrix because it is expensive to do so when adding multiple sparse matrices.
1272  // Remember to manually close the matrix once all changes to the matrix have been made.
1273 }
virtual numeric_index_type n() const override
获取矩阵的列数。
PetscScalar PS(T val)
Definition: petsc_macro.h:166
virtual numeric_index_type m() const =0
virtual numeric_index_type m() const override
获取矩阵的行数。
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
virtual void close() override
完成任何挂起的插入或添加操作。
Definition: petsc_matrix.C:983
virtual numeric_index_type n() const =0
template<typename T >
void libMesh::SparseMatrix< T >::attach_dof_map ( const DofMap dof_map)
inherited

设置要使用的 DofMap 的指针。如果不使用单独的稀疏性模式, 则使用来自 DofMap 的模式。

dof_map 的生命周期必须超过 this 的生命周期。

在文件 sparse_matrix.C58 行定义.

参考 libMesh::DofMap::get_sparsity_pattern().

参考自 libMesh::DofMap::update_sparsity_pattern().

59 {
60  _dof_map = &dof_map;
61  if (!_sp)
62  _sp = dof_map.get_sparsity_pattern();
63 }
const SparsityPattern::Graph & get_sparsity_pattern() const
Rows of sparse matrix indices, indexed by the offset from the first DoF on this processor.
SparsityPattern::Build const * _sp
与此对象关联的 sparsity pattern。在需要时, 应查询其入口计数(或使用 need_full_sparsity_pattern,模式)。
DofMap const * _dof_map
与此对象关联的 DofMap 对象。可以查询其在处理器上的自由度计数。
template<typename T >
void libMesh::SparseMatrix< T >::attach_sparsity_pattern ( const SparsityPattern::Build sp)
inherited

设置要使用的稀疏性模式的指针。在矩阵需要比系统中的大(或更小以提高效率)的模式, 或者在 DofMap 未计算系统稀疏性模式的情况下使用。

sp 的生命周期必须超过 this 的生命周期。

在文件 sparse_matrix.C68 行定义.

参考自 libMesh::DofMap::update_sparsity_pattern().

69 {
70  _sp = &sp;
71 }
SparsityPattern::Build const * _sp
与此对象关联的 sparsity pattern。在需要时, 应查询其入口计数(或使用 need_full_sparsity_pattern,模式)。
template<typename T >
std::unique_ptr< SparseMatrix< T > > libMesh::SparseMatrix< T >::build ( const Parallel::Communicator &  comm,
const SolverPackage  solver_package = libMesh::default_solver_package(),
const MatrixBuildType  matrix_build_type = MatrixBuildType::AUTOMATIC 
)
staticinherited

使用由 solver_package 指定的线性求解器包构建一个 SparseMatrix<T>。

在文件 sparse_matrix.C151 行定义.

参考 libMesh::EIGEN_SOLVERS, libMesh::LASPACK_SOLVERS, libMesh::libmesh_ignore() , 以及 libMesh::TRILINOS_SOLVERS.

参考自 libMesh::DofMap::process_mesh_constraint_rows().

154 {
155  // Avoid unused parameter warnings when no solver packages are enabled.
156  libmesh_ignore(comm);
157 
158  if (matrix_build_type == MatrixBuildType::DIAGONAL)
159  return std::make_unique<DiagonalMatrix<T>>(comm);
160 
161  // Build the appropriate vector
162  switch (solver_package)
163  {
164 
165 #ifdef LIBMESH_HAVE_LASPACK
166  case LASPACK_SOLVERS:
167  return std::make_unique<LaspackMatrix<T>>(comm);
168 #endif
169 
170 
171 #ifdef LIBMESH_HAVE_PETSC
172  case PETSC_SOLVERS:
173  return std::make_unique<PetscMatrix<T>>(comm);
174 #endif
175 
176 
177 #ifdef LIBMESH_TRILINOS_HAVE_EPETRA
178  case TRILINOS_SOLVERS:
179  return std::make_unique<EpetraMatrix<T>>(comm);
180 #endif
181 
182 
183 #ifdef LIBMESH_HAVE_EIGEN
184  case EIGEN_SOLVERS:
185  return std::make_unique<EigenSparseMatrix<T>>(comm);
186 #endif
187 
188  default:
189  libmesh_error_msg("ERROR: Unrecognized solver package: " << solver_package);
190  }
191 }
EIGEN_SOLVERS
Definition: libmesh.C:249
TRILINOS_SOLVERS
Definition: libmesh.C:247
void libmesh_ignore(const Args &...)
LASPACK_SOLVERS
Definition: libmesh.C:251
template<typename T >
void libMesh::PetscMatrix< T >::clear ( )
overridevirtualnoexcept

clear() 在析构函数中调用,因此它不应该抛出异常。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C503 行定义.

参考 libMesh::libMeshPrivateData::_is_initialized , 以及 libMesh::initialized().

504 {
505  if ((this->initialized()) && (this->_destroy_mat_on_exit))
506  {
507  exceptionless_semiparallel_only();
508 
509  // If we encounter an error here, print a warning but otherwise
510  // keep going since we may be recovering from an exception.
511  PetscErrorCode ierr = MatDestroy (&_mat);
512  if (ierr)
513  libmesh_warning("Warning: MatDestroy returned a non-zero error code which we ignored.");
514 
515  this->_is_initialized = false;
516  }
517 }
bool _destroy_mat_on_exit
此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。
Definition: petsc_matrix.h:508
virtual bool initialized() const
bool _is_initialized
标志,指示矩阵是否已初始化。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
std::unique_ptr< SparseMatrix< T > > libMesh::PetscMatrix< T >::clone ( ) const
overridevirtual

克隆当前矩阵。

返回
返回当前矩阵的唯一指针副本。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C483 行定义.

参考 libMesh::closed().

484 {
485  libmesh_error_msg_if(!this->closed(), "Matrix must be closed before it can be cloned!");
486 
487  // Copy the nonzero pattern and numerical values
488  Mat copy;
489  PetscErrorCode ierr = MatDuplicate(_mat, MAT_COPY_VALUES, &copy);
490  LIBMESH_CHKERR(ierr);
491 
492  // Call wrapping PetscMatrix constructor, have it take over
493  // ownership.
494  auto ret = std::make_unique<PetscMatrix<T>>(copy, this->comm());
495  ret->set_destroy_mat_on_exit(true);
496 
497  // Work around an issue on older compilers. We are able to simply
498  // "return ret;" on newer compilers
499  return std::unique_ptr<SparseMatrix<T>>(ret.release());
500 }
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::close ( )
overridevirtual

完成任何挂起的插入或添加操作。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C983 行定义.

参考自 libMesh::PetscMatrix< T >::_get_submatrix(), libMesh::PetscMatrix< T >::create_submatrix_nosort() , 以及 libMesh::PetscMatrix< T >::get_transpose().

984 {
985  semiparallel_only();
986 
987  // BSK - 1/19/2004
988  // strictly this check should be OK, but it seems to
989  // fail on matrix-free matrices. Do they falsely
990  // state they are assembled? Check with the developers...
991  // if (this->closed())
992  // return;
993 
994  MatAssemblyBeginEnd(this->comm(), _mat, MAT_FINAL_ASSEMBLY);
995 }
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
bool libMesh::PetscMatrix< T >::closed ( ) const
overridevirtual

检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。

返回
如果矩阵已经关闭,则返回 true;否则返回 false。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1390 行定义.

参考 libMesh::initialized().

参考自 libMesh::PetscMatrix< T >::add().

1391 {
1392  libmesh_assert (this->initialized());
1393 
1394  PetscErrorCode ierr=0;
1395  PetscBool assembled;
1396 
1397  ierr = MatAssembled(_mat, &assembled);
1398  LIBMESH_CHKERR(ierr);
1399 
1400  return (assembled == PETSC_TRUE);
1401 }
virtual bool initialized() const
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
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

此函数创建一个名为 "submatrix" 的矩阵,其定义由 "rows" 和 "cols" 条目中的行和列索引给定。 目前,此操作仅对 PetscMatrix 类型定义。 注意:rowscols 向量需要排序; 如果 rowscols 向量未排序,则使用下面的 nosort 版本; rowscols 仅包含由此处理器拥有的索引。

在文件 sparse_matrix.h388 行定义.

参考 libMesh::SparseMatrix< T >::_get_submatrix().

391  {
392  this->_get_submatrix(submatrix,
393  rows,
394  cols,
395  false); // false means DO NOT REUSE submatrix
396  }
virtual void _get_submatrix(SparseMatrix< T > &, const std::vector< numeric_index_type > &, const std::vector< numeric_index_type > &, const bool) const
创建子矩阵和 reinit_submatrix 例程的受保护实现。
template<typename T >
void libMesh::PetscMatrix< T >::create_submatrix_nosort ( SparseMatrix< T > &  submatrix,
const std::vector< numeric_index_type > &  rows,
const std::vector< numeric_index_type > &  cols 
) const
overridevirtual

根据给定的 rowscols 向量中的索引创建一个子矩阵,类似于 create_submatrix 函数。

参数
submatrix存储创建的子矩阵。
rows定义子矩阵的行索引。
cols定义子矩阵的列索引。
注解
rowscols 可以是未排序的; 如果你的索引是排序的,使用上面的函数可以获得更好的效率; rowscols 可以包含由其他处理器拥有的索引。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C863 行定义.

参考 libMesh::SparseMatrix< T >::_is_initialized, libMesh::PetscMatrix< T >::_mat, libMesh::PetscMatrix< T >::close() , 以及 libMesh::closed().

866 {
867  if (!this->closed())
868  {
869  libmesh_deprecated();
870  libmesh_warning("The matrix must be assembled before calling PetscMatrix::create_submatrix_nosort().\n"
871  "Please update your code, as this warning will become an error in a future release.");
872  const_cast<PetscMatrix<T> *>(this)->close();
873  }
874 
875  // Make sure the SparseMatrix passed in is really a PetscMatrix
876  PetscMatrix<T> * petsc_submatrix = cast_ptr<PetscMatrix<T> *>(&submatrix);
877 
878  PetscErrorCode ierr=0;
879 
880  ierr=MatZeroEntries(petsc_submatrix->_mat);
881  LIBMESH_CHKERR(ierr);
882 
883  PetscInt pc_ncols = 0;
884  const PetscInt * pc_cols;
885  const PetscScalar * pc_vals;
886 
887  // // data for creating the submatrix
888  std::vector<PetscInt> sub_cols;
889  std::vector<PetscScalar> sub_vals;
890 
891  for (auto i : index_range(rows))
892  {
893  PetscInt sub_rid[] = {static_cast<PetscInt>(i)};
894  PetscInt rid = static_cast<PetscInt>(rows[i]);
895  // only get value from local rows, and set values to the corresponding columns in the submatrix
896  if (rows[i]>= this->row_start() && rows[i]< this->row_stop())
897  {
898  // get one row of data from the original matrix
899  ierr = MatGetRow(_mat, rid, &pc_ncols, &pc_cols, &pc_vals);
900  LIBMESH_CHKERR(ierr);
901  // extract data from certain cols, save the indices and entries sub_cols and sub_vals
902  for (auto j : index_range(cols))
903  {
904  for (unsigned int idx = 0; idx< static_cast<unsigned int>(pc_ncols); idx++)
905  {
906  if (pc_cols[idx] == static_cast<PetscInt>(cols[j]))
907  {
908  sub_cols.push_back(static_cast<PetscInt>(j));
909  sub_vals.push_back(pc_vals[idx]);
910  }
911  }
912  }
913  // set values
914  ierr = MatSetValues(petsc_submatrix->_mat,
915  1,
916  sub_rid,
917  static_cast<PetscInt>(sub_vals.size()),
918  sub_cols.data(),
919  sub_vals.data(),
920  INSERT_VALUES);
921  LIBMESH_CHKERR(ierr);
922  ierr = MatRestoreRow(_mat, rid, &pc_ncols, &pc_cols, &pc_vals);
923  LIBMESH_CHKERR(ierr);
924  // clear data for this row
925  sub_cols.clear();
926  sub_vals.clear();
927  }
928  }
929  MatAssemblyBeginEnd(petsc_submatrix->comm(), petsc_submatrix->_mat, MAT_FINAL_ASSEMBLY);
930  // Specify that the new submatrix is initialized and close it.
931  petsc_submatrix->_is_initialized = true;
932  petsc_submatrix->close();
933 }
bool _is_initialized
标志,指示矩阵是否已初始化。
virtual numeric_index_type row_stop() const override
获取本地处理的行的结束索引。
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
virtual numeric_index_type row_start() const override
获取本地处理的行的起始索引。
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
virtual void close() override
完成任何挂起的插入或添加操作。
Definition: petsc_matrix.C:983
void libMesh::ReferenceCounter::disable_print_counter_info ( )
staticinherited

在文件 reference_counter.C100 行定义.

参考 libMesh::ReferenceCounter::_enable_print_counter.

101 {
102  _enable_print_counter = false;
103  return;
104 }
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...
void libMesh::ReferenceCounter::enable_print_counter_info ( )
staticinherited

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

在文件 reference_counter.C94 行定义.

参考 libMesh::ReferenceCounter::_enable_print_counter.

95 {
96  _enable_print_counter = true;
97  return;
98 }
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...
template<typename T >
void libMesh::PetscMatrix< T >::flush ( )
overridevirtual

刷新矩阵的内部缓冲区,确保所有操作都已完成。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C998 行定义.

999 {
1000  semiparallel_only();
1001 
1002  MatAssemblyBeginEnd(this->comm(), _mat, MAT_FLUSH_ASSEMBLY);
1003 }
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::get_diagonal ( NumericVector< T > &  dest) const
overridevirtual

获取矩阵的对角线并将其存储在给定的向量中。

参数
dest存储对角线的向量。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C937 行定义.

参考 libMesh::PetscVector< T >::vec().

938 {
939  // Make sure the NumericVector passed in is really a PetscVector
940  PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
941 
942  // Needs a const_cast since PETSc does not work with const.
943  PetscErrorCode ierr =
944  MatGetDiagonal(const_cast<PetscMatrix<T> *>(this)->mat(),petsc_dest.vec()); LIBMESH_CHKERR(ierr);
945 }
该类提供了一个良好的接口,用于访问 PETSc 的 Vec 对象。所有重写的虚拟函数都在 numeric_vector.h 中有文档说明。
Definition: petsc_vector.h:75
Vec vec()
获取当前向量的原始 PETSc Vec 指针。
Definition: petsc_vector.h:641
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat mat()
返回原始的 PETSc 矩阵指针。
Definition: petsc_matrix.h:455
std::string libMesh::ReferenceCounter::get_info ( )
staticinherited

Gets a string containing the reference information.

在文件 reference_counter.C47 行定义.

参考 libMesh::ReferenceCounter::_counts.

参考自 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 & [name, cd] : _counts)
59  oss << "| " << name << " reference count information:\n"
60  << "| Creations: " << cd.first << '\n'
61  << "| Destructions: " << cd.second << '\n';
62 
63  oss << " ---------------------------------------------------------------------------- \n";
64 
65  return oss.str();
66 
67 #else
68 
69  return "";
70 
71 #endif
72 }
static Counts _counts
Actually holds the data.
template<typename T >
void libMesh::PetscMatrix< T >::get_local_size ( numeric_index_type m,
numeric_index_type n 
) const

获取由该进程拥有的矩阵行数和列数。

参数
m行大小。
n列大小。

在文件 petsc_matrix.C1062 行定义.

参考 libMesh::initialized().

1064 {
1065  libmesh_assert (this->initialized());
1066 
1067  PetscInt petsc_m = 0, petsc_n = 0;
1068 
1069  auto ierr = MatGetLocalSize (_mat, &petsc_m, &petsc_n);
1070  LIBMESH_CHKERR(ierr);
1071 
1072  m = static_cast<numeric_index_type>(petsc_m);
1073  n = static_cast<numeric_index_type>(petsc_n);
1074 }
virtual numeric_index_type n() const override
获取矩阵的列数。
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
virtual numeric_index_type m() const override
获取矩阵的行数。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::get_row ( numeric_index_type  i,
std::vector< numeric_index_type > &  indices,
std::vector< T > &  values 
) const
overridevirtual

获取矩阵中指定行的索引和值。

参数
i行索引。
indices存储该行中非零元素的列索引的向量。
values存储该行中非零元素的值的向量。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C1333 行定义.

参考 libMesh::closed() , 以及 libMesh::initialized().

1336 {
1337  libmesh_assert (this->initialized());
1338 
1339  const PetscScalar * petsc_row;
1340  const PetscInt * petsc_cols;
1341 
1342  PetscErrorCode ierr=0;
1343  PetscInt
1344  ncols=0,
1345  i_val = static_cast<PetscInt>(i_in);
1346 
1347  // the matrix needs to be closed for this to work
1348  // this->close();
1349  // but closing it is a semiparallel operation; we want operator()
1350  // to run on one processor.
1351  libmesh_assert(this->closed());
1352 
1353  // PETSc makes no effort at being thread safe. Helgrind complains about
1354  // possible data races even just in PetscFunctionBegin (due to things
1355  // like stack counter incrementing). Perhaps we could ignore
1356  // this, but there are legitimate data races for Mat data members like
1357  // mat->getrowactive between MatGetRow and MatRestoreRow. Moreover,
1358  // there could be a write into mat->rowvalues during MatGetRow from
1359  // one thread while we are attempting to read from mat->rowvalues
1360  // (through petsc_cols) during data copy in another thread. So
1361  // the safe thing to do is to lock the whole method
1362 
1363 #ifdef LIBMESH_HAVE_CXX11_THREAD
1364  std::lock_guard<std::mutex>
1365 #else
1366  Threads::spin_mutex::scoped_lock
1367 #endif
1368  lock(_petsc_matrix_mutex);
1369 
1370  ierr = MatGetRow(_mat, i_val, &ncols, &petsc_cols, &petsc_row);
1371  LIBMESH_CHKERR(ierr);
1372 
1373  // Copy the data
1374  indices.resize(static_cast<std::size_t>(ncols));
1375  values.resize(static_cast<std::size_t>(ncols));
1376 
1377  for (auto i : index_range(indices))
1378  {
1379  indices[i] = static_cast<numeric_index_type>(petsc_cols[i]);
1380  values[i] = static_cast<T>(petsc_row[i]);
1381  }
1382 
1383  ierr = MatRestoreRow(_mat, i_val,
1384  &ncols, &petsc_cols, &petsc_row);
1385  LIBMESH_CHKERR(ierr);
1386 }
virtual bool initialized() const
std::mutex _petsc_matrix_mutex
用于线程安全的互斥锁(C++11 线程)。
Definition: petsc_matrix.h:519
dof_id_type numeric_index_type
Definition: id_types.h:99
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::get_transpose ( SparseMatrix< T > &  dest) const
overridevirtual

获取矩阵的转置。

参数
dest存储转置矩阵的对象。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C950 行定义.

参考 libMesh::SparseMatrix< T >::_is_initialized, libMesh::PetscMatrix< T >::_mat, libMesh::SparseMatrix< T >::clear() , 以及 libMesh::PetscMatrix< T >::close().

951 {
952  // Make sure the SparseMatrix passed in is really a PetscMatrix
953  PetscMatrix<T> & petsc_dest = cast_ref<PetscMatrix<T> &>(dest);
954 
955  // If we aren't reusing the matrix then need to clear dest,
956  // otherwise we get a memory leak
957  if (&petsc_dest != this)
958  dest.clear();
959 
960  PetscErrorCode ierr;
961  if (&petsc_dest == this)
962  // The MAT_REUSE_MATRIX flag was replaced by MAT_INPLACE_MATRIX
963  // in PETSc 3.7.0
964 #if PETSC_VERSION_LESS_THAN(3,7,0)
965  ierr = MatTranspose(_mat,MAT_REUSE_MATRIX,&petsc_dest._mat);
966 #else
967  ierr = MatTranspose(_mat, MAT_INPLACE_MATRIX, &petsc_dest._mat);
968 #endif
969  else
970  ierr = MatTranspose(_mat,MAT_INITIAL_MATRIX,&petsc_dest._mat);
971  LIBMESH_CHKERR(ierr);
972 
973  // Specify that the transposed matrix is initialized and close it.
974  petsc_dest._is_initialized = true;
975  petsc_dest.close();
976 }
virtual void clear()=0
恢复 SparseMatrix&lt;T&gt; 到原始状态。
bool _is_initialized
标志,指示矩阵是否已初始化。
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
virtual void close() override
完成任何挂起的插入或添加操作。
Definition: petsc_matrix.C:983
void libMesh::ReferenceCounter::increment_constructor_count ( const std::string &  name)
inlineprotectednoexceptinherited

Increments the construction counter.

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

在文件 reference_counter.h183 行定义.

参考 libMesh::err.

参考自 libMesh::ReferenceCountedObject< SparseMatrix< T > >::ReferenceCountedObject().

184 {
185  libmesh_try
186  {
187  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
188  std::pair<unsigned int, unsigned int> & p = _counts[name];
189  p.first++;
190  }
191  libmesh_catch (...)
192  {
193  auto stream = libMesh::err.get();
194  stream->exceptions(stream->goodbit); // stream must not throw
195  libMesh::err << "Encountered unrecoverable error while calling "
196  << "ReferenceCounter::increment_constructor_count() "
197  << "for a(n) " << name << " object." << std::endl;
198  std::terminate();
199  }
200 }
OStreamProxy err
static Counts _counts
Actually holds the data.
void libMesh::ReferenceCounter::increment_destructor_count ( const std::string &  name)
inlineprotectednoexceptinherited

Increments the destruction counter.

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

在文件 reference_counter.h207 行定义.

参考 libMesh::err.

参考自 libMesh::ReferenceCountedObject< SparseMatrix< T > >::~ReferenceCountedObject().

208 {
209  libmesh_try
210  {
211  Threads::spin_mutex::scoped_lock lock(Threads::spin_mtx);
212  std::pair<unsigned int, unsigned int> & p = _counts[name];
213  p.second++;
214  }
215  libmesh_catch (...)
216  {
217  auto stream = libMesh::err.get();
218  stream->exceptions(stream->goodbit); // stream must not throw
219  libMesh::err << "Encountered unrecoverable error while calling "
220  << "ReferenceCounter::increment_destructor_count() "
221  << "for a(n) " << name << " object." << std::endl;
222  std::terminate();
223  }
224 }
OStreamProxy err
static Counts _counts
Actually holds the data.
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

初始化 PETSc 矩阵。

参数
m全局行数。
n全局列数。
m_l本地行数。
n_l本地列数。
nnz对角线部分本地子矩阵中每行的非零元素数。
noz非对角线部分本地子矩阵中每行的非零元素数。
blocksize多个相同类型变量的系统中,用于指示密集耦合块的可选值。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C121 行定义.

参考 libMesh::libMeshPrivateData::_is_initialized, libMesh::AIJ, libMesh::HYPRE, libMesh::initialized(), libMesh::libmesh_ignore() , 以及 libMesh::zero.

128 {
129  // So compilers don't warn when !LIBMESH_ENABLE_BLOCKED_STORAGE
130  libmesh_ignore(blocksize_in);
131 
132  // Clear initialized matrices
133  if (this->initialized())
134  this->clear();
135 
136  this->_is_initialized = true;
137 
138 
139  PetscErrorCode ierr = 0;
140  PetscInt m_global = static_cast<PetscInt>(m_in);
141  PetscInt n_global = static_cast<PetscInt>(n_in);
142  PetscInt m_local = static_cast<PetscInt>(m_l);
143  PetscInt n_local = static_cast<PetscInt>(n_l);
144  PetscInt n_nz = static_cast<PetscInt>(nnz);
145  PetscInt n_oz = static_cast<PetscInt>(noz);
146 
147  ierr = MatCreate(this->comm().get(), &_mat);
148  LIBMESH_CHKERR(ierr);
149  ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
150  LIBMESH_CHKERR(ierr);
151  PetscInt blocksize = static_cast<PetscInt>(blocksize_in);
152  ierr = MatSetBlockSize(_mat,blocksize);
153  LIBMESH_CHKERR(ierr);
154 
155 #ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
156  if (blocksize > 1)
157  {
158  // specified blocksize, bs>1.
159  // double check sizes.
160  libmesh_assert_equal_to (m_local % blocksize, 0);
161  libmesh_assert_equal_to (n_local % blocksize, 0);
162  libmesh_assert_equal_to (m_global % blocksize, 0);
163  libmesh_assert_equal_to (n_global % blocksize, 0);
164  libmesh_assert_equal_to (n_nz % blocksize, 0);
165  libmesh_assert_equal_to (n_oz % blocksize, 0);
166 
167  ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
168  LIBMESH_CHKERR(ierr);
169 
170  // MatSetFromOptions needs to happen before Preallocation routines
171  // since MatSetFromOptions can change matrix type and remove incompatible
172  // preallocation
173  ierr = MatSetOptionsPrefix(_mat, "");
174  LIBMESH_CHKERR(ierr);
175  ierr = MatSetFromOptions(_mat);
176  LIBMESH_CHKERR(ierr);
177  ierr = MatSeqBAIJSetPreallocation(_mat, blocksize, n_nz/blocksize, NULL);
178  LIBMESH_CHKERR(ierr);
179  ierr = MatMPIBAIJSetPreallocation(_mat, blocksize,
180  n_nz/blocksize, NULL,
181  n_oz/blocksize, NULL);
182  LIBMESH_CHKERR(ierr);
183  }
184  else
185 #endif
186  {
187  switch (_mat_type) {
188  case AIJ:
189  ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
190  LIBMESH_CHKERR(ierr);
191 
192  // MatSetFromOptions needs to happen before Preallocation routines
193  // since MatSetFromOptions can change matrix type and remove incompatible
194  // preallocation
195  ierr = MatSetOptionsPrefix(_mat, "");
196  LIBMESH_CHKERR(ierr);
197  ierr = MatSetFromOptions(_mat);
198  LIBMESH_CHKERR(ierr);
199  ierr = MatSeqAIJSetPreallocation(_mat, n_nz, NULL);
200  LIBMESH_CHKERR(ierr);
201  ierr = MatMPIAIJSetPreallocation(_mat, n_nz, NULL, n_oz, NULL);
202  LIBMESH_CHKERR(ierr);
203  break;
204 
205  case HYPRE:
206 #if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
207  ierr = MatSetType(_mat, MATHYPRE);
208 
209  // MatSetFromOptions needs to happen before Preallocation routines
210  // since MatSetFromOptions can change matrix type and remove incompatible
211  // preallocation
212  LIBMESH_CHKERR(ierr);
213  ierr = MatSetOptionsPrefix(_mat, "");
214  LIBMESH_CHKERR(ierr);
215  ierr = MatSetFromOptions(_mat);
216  LIBMESH_CHKERR(ierr);
217  ierr = MatHYPRESetPreallocation(_mat, n_nz, NULL, n_oz, NULL);
218  LIBMESH_CHKERR(ierr);
219 #else
220  libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
221 #endif
222  break;
223 
224  default: libmesh_error_msg("Unsupported petsc matrix type");
225  }
226  }
227 
228  // Make it an error for PETSc to allocate new nonzero entries during assembly
229  ierr = MatSetOption(_mat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
230  LIBMESH_CHKERR(ierr);
231 
232  this->zero ();
233 }
virtual void zero() override
将矩阵所有元素置为零。
Definition: petsc_matrix.C:417
virtual void clear() noexceptoverride
clear() 在析构函数中调用,因此它不应该抛出异常。
Definition: petsc_matrix.C:503
virtual bool initialized() const
void libmesh_ignore(const Args &...)
bool _is_initialized
标志,指示矩阵是否已初始化。
PetscMatrixType _mat_type
存储 PETSc 矩阵的类型。
Definition: petsc_matrix.h:513
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
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 
)

初始化 PETSc 矩阵。

参数
m全局行数。
n全局列数。
m_l本地行数。
n_l本地列数。
n_nz包含本地子矩阵对角线部分每行的非零元素数的数组。
n_oz包含本地子矩阵非对角线部分每行的非零元素数的数组。
blocksize多个相同类型变量的系统中,用于指示密集耦合块的可选值。

在文件 petsc_matrix.C237 行定义.

参考 libMesh::libMeshPrivateData::_is_initialized, libMesh::AIJ, libMesh::HYPRE, libMesh::initialized(), libMesh::libmesh_ignore(), libMesh::numeric_petsc_cast() , 以及 libMesh::zero.

244 {
245  // So compilers don't warn when !LIBMESH_ENABLE_BLOCKED_STORAGE
246  libmesh_ignore(blocksize_in);
247 
248  PetscInt blocksize = static_cast<PetscInt>(blocksize_in);
249 
250  // Clear initialized matrices
251  if (this->initialized())
252  this->clear();
253 
254  this->_is_initialized = true;
255 
256  // Make sure the sparsity pattern isn't empty unless the matrix is 0x0
257  libmesh_assert_equal_to (n_nz.size(), m_l);
258  libmesh_assert_equal_to (n_oz.size(), m_l);
259 
260  PetscErrorCode ierr = 0;
261  PetscInt m_global = static_cast<PetscInt>(m_in);
262  PetscInt n_global = static_cast<PetscInt>(n_in);
263  PetscInt m_local = static_cast<PetscInt>(m_l);
264  PetscInt n_local = static_cast<PetscInt>(n_l);
265 
266  ierr = MatCreate(this->comm().get(), &_mat);
267  LIBMESH_CHKERR(ierr);
268  ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
269  LIBMESH_CHKERR(ierr);
270  ierr = MatSetBlockSize(_mat,blocksize);
271  LIBMESH_CHKERR(ierr);
272 
273 #ifdef LIBMESH_ENABLE_BLOCKED_STORAGE
274  if (blocksize > 1)
275  {
276  // specified blocksize, bs>1.
277  // double check sizes.
278  libmesh_assert_equal_to (m_local % blocksize, 0);
279  libmesh_assert_equal_to (n_local % blocksize, 0);
280  libmesh_assert_equal_to (m_global % blocksize, 0);
281  libmesh_assert_equal_to (n_global % blocksize, 0);
282 
283  ierr = MatSetType(_mat, MATBAIJ); // Automatically chooses seqbaij or mpibaij
284  LIBMESH_CHKERR(ierr);
285 
286  // MatSetFromOptions needs to happen before Preallocation routines
287  // since MatSetFromOptions can change matrix type and remove incompatible
288  // preallocation
289  LIBMESH_CHKERR(ierr);
290  ierr = MatSetOptionsPrefix(_mat, "");
291  LIBMESH_CHKERR(ierr);
292  ierr = MatSetFromOptions(_mat);
293  LIBMESH_CHKERR(ierr);
294  // transform the per-entry n_nz and n_oz arrays into their block counterparts.
295  std::vector<numeric_index_type> b_n_nz, b_n_oz;
296 
297  transform_preallocation_arrays (blocksize,
298  n_nz, n_oz,
299  b_n_nz, b_n_oz);
300 
301  ierr = MatSeqBAIJSetPreallocation (_mat,
302  blocksize,
303  0,
304  numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()));
305  LIBMESH_CHKERR(ierr);
306 
307  ierr = MatMPIBAIJSetPreallocation (_mat,
308  blocksize,
309  0,
310  numeric_petsc_cast(b_n_nz.empty() ? nullptr : b_n_nz.data()),
311  0,
312  numeric_petsc_cast(b_n_oz.empty() ? nullptr : b_n_oz.data()));
313  LIBMESH_CHKERR(ierr);
314  }
315  else
316 #endif
317  {
318  switch (_mat_type) {
319  case AIJ:
320  ierr = MatSetType(_mat, MATAIJ); // Automatically chooses seqaij or mpiaij
321  LIBMESH_CHKERR(ierr);
322 
323  // MatSetFromOptions needs to happen before Preallocation routines
324  // since MatSetFromOptions can change matrix type and remove incompatible
325  // preallocation
326  LIBMESH_CHKERR(ierr);
327  ierr = MatSetOptionsPrefix(_mat, "");
328  LIBMESH_CHKERR(ierr);
329  ierr = MatSetFromOptions(_mat);
330  LIBMESH_CHKERR(ierr);
331  ierr = MatSeqAIJSetPreallocation (_mat,
332  0,
333  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()));
334  LIBMESH_CHKERR(ierr);
335  ierr = MatMPIAIJSetPreallocation (_mat,
336  0,
337  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
338  0,
339  numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
340  break;
341 
342  case HYPRE:
343 #if !PETSC_VERSION_LESS_THAN(3,9,4) && LIBMESH_HAVE_PETSC_HYPRE
344  ierr = MatSetType(_mat, MATHYPRE);
345  LIBMESH_CHKERR(ierr);
346 
347  // MatSetFromOptions needs to happen before Preallocation routines
348  // since MatSetFromOptions can change matrix type and remove incompatible
349  // preallocation
350  LIBMESH_CHKERR(ierr);
351  ierr = MatSetOptionsPrefix(_mat, "");
352  LIBMESH_CHKERR(ierr);
353  ierr = MatSetFromOptions(_mat);
354  LIBMESH_CHKERR(ierr);
355  ierr = MatHYPRESetPreallocation (_mat,
356  0,
357  numeric_petsc_cast(n_nz.empty() ? nullptr : n_nz.data()),
358  0,
359  numeric_petsc_cast(n_oz.empty() ? nullptr : n_oz.data()));
360  LIBMESH_CHKERR(ierr);
361 #else
362  libmesh_error_msg("PETSc 3.9.4 or higher with hypre is required for MatHypre");
363 #endif
364  break;
365 
366  default: libmesh_error_msg("Unsupported petsc matrix type");
367  }
368 
369  }
370 
371  // Make it an error for PETSc to allocate new nonzero entries during assembly
372  ierr = MatSetOption(_mat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
373  LIBMESH_CHKERR(ierr);
374  this->zero();
375 }
virtual void zero() override
将矩阵所有元素置为零。
Definition: petsc_matrix.C:417
virtual void clear() noexceptoverride
clear() 在析构函数中调用,因此它不应该抛出异常。
Definition: petsc_matrix.C:503
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
virtual bool initialized() const
void libmesh_ignore(const Args &...)
bool _is_initialized
标志,指示矩阵是否已初始化。
PetscMatrixType _mat_type
存储 PETSc 矩阵的类型。
Definition: petsc_matrix.h:513
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::init ( ParallelType  type = PARALLEL)
overridevirtual

使用由 dof_map 计算的稀疏性结构初始化此矩阵。

参数
type矩阵的串行/并行/幽灵类型
注解
此函数使用由 dof_map 计算的稀疏性结构初始化矩阵。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C379 行定义.

380 {
381  libmesh_assert(this->_dof_map);
382 
383  const numeric_index_type m_in = this->_dof_map->n_dofs();
384  const numeric_index_type m_l = this->_dof_map->n_dofs_on_processor(this->processor_id());
385 
386  const std::vector<numeric_index_type> & n_nz = this->_sp->get_n_nz();
387  const std::vector<numeric_index_type> & n_oz = this->_sp->get_n_oz();
388 
389  PetscInt blocksize = static_cast<PetscInt>(this->_dof_map->block_size());
390 
391  this->init(m_in, m_in, m_l, m_l, n_nz, n_oz, blocksize);
392 }
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
初始化 PETSc 矩阵。
Definition: petsc_matrix.C:121
SparsityPattern::Build const * _sp
与此对象关联的 sparsity pattern。在需要时, 应查询其入口计数(或使用 need_full_sparsity_pattern,模式)。
unsigned int block_size() const
Definition: dof_map.h:651
dof_id_type n_dofs() const
Definition: dof_map.h:659
dof_id_type n_dofs_on_processor(const processor_id_type proc) const
Definition: dof_map.h:675
const std::vector< dof_id_type > & get_n_oz() const
The number of off-processor nonzeros in my portion of the global matrix.
dof_id_type numeric_index_type
Definition: id_types.h:99
const std::vector< dof_id_type > & get_n_nz() const
The number of on-processor nonzeros in my portion of the global matrix.
DofMap const * _dof_map
与此对象关联的 DofMap 对象。可以查询其在处理器上的自由度计数。
template<typename T>
virtual bool libMesh::SparseMatrix< T >::initialized ( ) const
inlinevirtualinherited
返回
如果矩阵已初始化,则为 true,否则为 false。

在文件 sparse_matrix.h102 行定义.

参考 libMesh::SparseMatrix< T >::_is_initialized.

参考自 libMesh::PetscMatrix< T >::_get_submatrix().

102 { return _is_initialized; }
bool _is_initialized
标志,指示矩阵是否已初始化。
template<typename T >
Real libMesh::PetscMatrix< T >::l1_norm ( ) const
overridevirtual

计算矩阵的 L1 范数。

返回
返回矩阵的 L1 范数。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C522 行定义.

参考 libMesh::closed(), libMesh::initialized() , 以及 libMesh::Real.

523 {
524  libmesh_assert (this->initialized());
525 
526  semiparallel_only();
527 
528  PetscErrorCode ierr=0;
529  PetscReal petsc_value;
530  Real value;
531 
532  libmesh_assert (this->closed());
533 
534  ierr = MatNorm(_mat, NORM_1, &petsc_value);
535  LIBMESH_CHKERR(ierr);
536 
537  value = static_cast<Real>(petsc_value);
538 
539  return value;
540 }
virtual bool initialized() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
Real libMesh::PetscMatrix< T >::linfty_norm ( ) const
overridevirtual

计算矩阵的无穷范数。

返回
返回矩阵的无穷范数。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C545 行定义.

参考 libMesh::closed(), libMesh::initialized() , 以及 libMesh::Real.

546 {
547  libmesh_assert (this->initialized());
548 
549  semiparallel_only();
550 
551  PetscErrorCode ierr=0;
552  PetscReal petsc_value;
553  Real value;
554 
555  libmesh_assert (this->closed());
556 
557  ierr = MatNorm(_mat, NORM_INFINITY, &petsc_value);
558  LIBMESH_CHKERR(ierr);
559 
560  value = static_cast<Real>(petsc_value);
561 
562  return value;
563 }
virtual bool initialized() const
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::local_m ( ) const
finalvirtual

获取由该进程拥有的矩阵行数。

返回
返回由该进程拥有的矩阵行数。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C1022 行定义.

参考 libMesh::initialized().

1023 {
1024  libmesh_assert (this->initialized());
1025 
1026  PetscInt m = 0;
1027 
1028  auto ierr = MatGetLocalSize (_mat, &m, NULL);
1029  LIBMESH_CHKERR(ierr);
1030 
1031  return static_cast<numeric_index_type>(m);
1032 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
virtual numeric_index_type m() const override
获取矩阵的行数。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::local_n ( ) const

获取由该进程拥有的矩阵列数。

返回
返回由该进程拥有的矩阵列数。

在文件 petsc_matrix.C1049 行定义.

参考 libMesh::initialized().

1050 {
1051  libmesh_assert (this->initialized());
1052 
1053  PetscInt n = 0;
1054 
1055  auto ierr = MatGetLocalSize (_mat, NULL, &n);
1056  LIBMESH_CHKERR(ierr);
1057 
1058  return static_cast<numeric_index_type>(n);
1059 }
virtual numeric_index_type n() const override
获取矩阵的列数。
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::m ( ) const
overridevirtual

获取矩阵的行数。

返回
返回矩阵的行数。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1008 行定义.

参考 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_m);
1019 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T>
Mat libMesh::PetscMatrix< T >::mat ( )
inline

返回原始的 PETSc 矩阵指针。

返回
返回 PETSc 矩阵的原始指针。
注解
这通常在用户级别的代码中不是必需的。
不要做任何疯狂的事情,比如在它上面调用 MatDestroy(),否则可能会发生非常糟糕的事情!

在文件 petsc_matrix.h455 行定义.

参考 libMesh::PetscMatrix< T >::_mat.

455 { libmesh_assert (_mat); return _mat; }
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::matrix_matrix_mult ( SparseMatrix< T > &  X,
SparseMatrix< T > &  Y,
bool  reuse = false 
)
overridevirtual

计算 Y = A*X,其中 A 是当前矩阵,X 是另一个矩阵。

参数
X输入矩阵。
Y结果矩阵。
reuse如果 this->_mat 和 X 有相同的非零模式,并且 Y 是从之前对此函数的调用中获得的,则设置为 true。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C1185 行定义.

参考 libMesh::initialized() , 以及 libMesh::SparseMatrix< T >::m().

1186 {
1187  libmesh_assert (this->initialized());
1188 
1189  // sanity check
1190  // we do not check the Y_out size here as we will initialize & close it at the end
1191  libmesh_assert_equal_to (this->n(), X_in.m());
1192 
1193  const PetscMatrix<T> * X = cast_ptr<const PetscMatrix<T> *> (&X_in);
1194  PetscMatrix<T> * Y = cast_ptr<PetscMatrix<T> *> (&Y_out);
1195 
1196  PetscErrorCode ierr = 0;
1197 
1198  // the matrix from which we copy the values has to be assembled/closed
1199  libmesh_assert(X->closed());
1200 
1201  semiparallel_only();
1202 
1203  if (reuse)
1204  {
1205  ierr = MatMatMult(_mat, X->_mat, MAT_REUSE_MATRIX, PETSC_DEFAULT, &Y->_mat);
1206  LIBMESH_CHKERR(ierr);
1207  }
1208  else
1209  {
1210  Y->clear();
1211  ierr = MatMatMult(_mat, X->_mat, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &Y->_mat);
1212  LIBMESH_CHKERR(ierr);
1213  }
1214  // Specify that the new matrix is initialized
1215  // We do not close it here as `MatMatMult` ensures Y being closed
1216  Y->_is_initialized = true;
1217 }
virtual numeric_index_type n() const override
获取矩阵的列数。
virtual bool initialized() const
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::n ( ) const
overridevirtual

获取矩阵的列数。

返回
返回矩阵的列数。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1035 行定义.

参考 libMesh::initialized().

1036 {
1037  libmesh_assert (this->initialized());
1038 
1039  PetscInt petsc_m=0, petsc_n=0;
1040  PetscErrorCode ierr=0;
1041 
1042  ierr = MatGetSize (_mat, &petsc_m, &petsc_n);
1043  LIBMESH_CHKERR(ierr);
1044 
1045  return static_cast<numeric_index_type>(petsc_n);
1046 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
static unsigned int libMesh::ReferenceCounter::n_objects ( )
inlinestaticinherited

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

在文件 reference_counter.h85 行定义.

参考 libMesh::ReferenceCounter::_n_objects.

参考自 libMesh::LibMeshInit::~LibMeshInit().

86  { return _n_objects; }
static Threads::atomic< unsigned int > _n_objects
The number of objects.
template<typename T>
virtual bool libMesh::SparseMatrix< T >::need_full_sparsity_pattern ( ) const
inlinevirtualinherited
返回
如果此稀疏矩阵格式需要提供稀疏矩阵的图形,则为 true。

这对于 LaspackMatrix 是 true,但对于 PetscMatrix 是 false。 在不需要完整图形的情况下,我们可以有效地近似它以提供对稀疏矩阵所需大小的良好估计。

libMesh::EpetraMatrix< T > , 以及 libMesh::LaspackMatrix< T > 重载.

在文件 sparse_matrix.h126 行定义.

参考自 libMesh::DofMap::attach_matrix() , 以及 libMesh::DofMap::update_sparsity_pattern().

127  { return false; }
template<typename T >
T libMesh::PetscMatrix< T >::operator() ( const numeric_index_type  i,
const numeric_index_type  j 
) const
overridevirtual

获取矩阵中特定位置的值。

参数
i行索引。
j列索引。
返回
返回矩阵中 (i, j) 位置的值。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1276 行定义.

参考 libMesh::closed() , 以及 libMesh::initialized().

1278 {
1279  libmesh_assert (this->initialized());
1280 
1281  // PETSc 2.2.1 & newer
1282  const PetscScalar * petsc_row;
1283  const PetscInt * petsc_cols;
1284 
1285  // If the entry is not in the sparse matrix, it is 0.
1286  T value=0.;
1287 
1288  PetscErrorCode
1289  ierr=0;
1290  PetscInt
1291  ncols=0,
1292  i_val=static_cast<PetscInt>(i_in),
1293  j_val=static_cast<PetscInt>(j_in);
1294 
1295 
1296  // the matrix needs to be closed for this to work
1297  // this->close();
1298  // but closing it is a semiparallel operation; we want operator()
1299  // to run on one processor.
1300  libmesh_assert(this->closed());
1301 
1302  ierr = MatGetRow(_mat, i_val, &ncols, &petsc_cols, &petsc_row);
1303  LIBMESH_CHKERR(ierr);
1304 
1305  // Perform a binary search to find the contiguous index in
1306  // petsc_cols (resp. petsc_row) corresponding to global index j_val
1307  std::pair<const PetscInt *, const PetscInt *> p =
1308  std::equal_range (petsc_cols, petsc_cols + ncols, j_val);
1309 
1310  // Found an entry for j_val
1311  if (p.first != p.second)
1312  {
1313  // The entry in the contiguous row corresponding
1314  // to the j_val column of interest
1315  const std::size_t j =
1316  std::distance (const_cast<PetscInt *>(petsc_cols),
1317  const_cast<PetscInt *>(p.first));
1318 
1319  libmesh_assert_less (j, ncols);
1320  libmesh_assert_equal_to (petsc_cols[j], j_val);
1321 
1322  value = static_cast<T> (petsc_row[j]);
1323  }
1324 
1325  ierr = MatRestoreRow(_mat, i_val,
1326  &ncols, &petsc_cols, &petsc_row);
1327  LIBMESH_CHKERR(ierr);
1328 
1329  return value;
1330 }
virtual bool initialized() const
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T>
PetscMatrix& libMesh::PetscMatrix< T >::operator= ( const PetscMatrix< T > &  )
delete
template<typename T>
PetscMatrix& libMesh::PetscMatrix< T >::operator= ( PetscMatrix< T > &&  )
delete
template<>
void libMesh::SparseMatrix< Complex >::print ( std::ostream &  os,
const bool  sparse 
) const
inherited

在文件 sparse_matrix.C117 行定义.

参考 std::imag() , 以及 std::real().

118 {
119  // std::complex<>::operator<<() is defined, but use this form
120 
121  if (sparse)
122  {
123  libmesh_not_implemented();
124  }
125 
126  os << "Real part:" << std::endl;
127  for (auto i : make_range(this->m()))
128  {
129  for (auto j : make_range(this->n()))
130  os << std::setw(8) << (*this)(i,j).real() << " ";
131  os << std::endl;
132  }
133 
134  os << std::endl << "Imaginary part:" << std::endl;
135  for (auto i : make_range(this->m()))
136  {
137  for (auto j : make_range(this->n()))
138  os << std::setw(8) << (*this)(i,j).imag() << " ";
139  os << std::endl;
140  }
141 }
boost::multiprecision::float128 real(const boost::multiprecision::float128 in)
virtual numeric_index_type m() const =0
boost::multiprecision::float128 imag(const boost::multiprecision::float128)
virtual numeric_index_type n() const =0
template<typename T >
void libMesh::SparseMatrix< T >::print ( std::ostream &  os = libMesh::out,
const bool  sparse = false 
) const
inherited

将矩阵的内容以统一的样式打印到屏幕上,而不考虑正在使用的矩阵/求解器包。

在文件 sparse_matrix.C225 行定义.

参考 libMesh::initialized().

参考自 libMesh::EigenSparseMatrix< T >::print_personal() , 以及 libMesh::LaspackMatrix< T >::print_personal().

226 {
227  parallel_object_only();
228 
229  libmesh_assert (this->initialized());
230 
231  libmesh_error_msg_if(!this->_dof_map, "Error! Trying to print a matrix with no dof_map set!");
232 
233  // We'll print the matrix from processor 0 to make sure
234  // it's serialized properly
235  if (this->processor_id() == 0)
236  {
237  libmesh_assert_equal_to (this->_dof_map->first_dof(), 0);
238  for (numeric_index_type i=this->_dof_map->first_dof();
239  i!=this->_dof_map->end_dof(); ++i)
240  {
241  if (sparse)
242  {
243  for (auto j : make_range(this->n()))
244  {
245  T c = (*this)(i,j);
246  if (c != static_cast<T>(0.0))
247  {
248  os << i << " " << j << " " << c << std::endl;
249  }
250  }
251  }
252  else
253  {
254  for (auto j : make_range(this->n()))
255  os << (*this)(i,j) << " ";
256  os << std::endl;
257  }
258  }
259 
260  std::vector<numeric_index_type> ibuf, jbuf;
261  std::vector<T> cbuf;
262  numeric_index_type currenti = this->_dof_map->end_dof();
263  for (auto p : IntRange<processor_id_type>(1, this->n_processors()))
264  {
265  this->comm().receive(p, ibuf);
266  this->comm().receive(p, jbuf);
267  this->comm().receive(p, cbuf);
268  libmesh_assert_equal_to (ibuf.size(), jbuf.size());
269  libmesh_assert_equal_to (ibuf.size(), cbuf.size());
270 
271  if (ibuf.empty())
272  continue;
273  libmesh_assert_greater_equal (ibuf.front(), currenti);
274  libmesh_assert_greater_equal (ibuf.back(), ibuf.front());
275 
276  std::size_t currentb = 0;
277  for (;currenti <= ibuf.back(); ++currenti)
278  {
279  if (sparse)
280  {
281  for (numeric_index_type j=0; j<this->n(); j++)
282  {
283  if (currentb < ibuf.size() &&
284  ibuf[currentb] == currenti &&
285  jbuf[currentb] == j)
286  {
287  os << currenti << " " << j << " " << cbuf[currentb] << std::endl;
288  currentb++;
289  }
290  }
291  }
292  else
293  {
294  for (auto j : make_range(this->n()))
295  {
296  if (currentb < ibuf.size() &&
297  ibuf[currentb] == currenti &&
298  jbuf[currentb] == j)
299  {
300  os << cbuf[currentb] << " ";
301  currentb++;
302  }
303  else
304  os << static_cast<T>(0.0) << " ";
305  }
306  os << std::endl;
307  }
308  }
309  }
310  if (!sparse)
311  {
312  for (; currenti != this->m(); ++currenti)
313  {
314  for (numeric_index_type j=0; j<this->n(); j++)
315  os << static_cast<T>(0.0) << " ";
316  os << std::endl;
317  }
318  }
319  }
320  else
321  {
322  std::vector<numeric_index_type> ibuf, jbuf;
323  std::vector<T> cbuf;
324 
325  // We'll assume each processor has access to entire
326  // matrix rows, so (*this)(i,j) is valid if i is a local index.
327  for (numeric_index_type i=this->_dof_map->first_dof();
328  i!=this->_dof_map->end_dof(); ++i)
329  {
330  for (auto j : make_range(this->n()))
331  {
332  T c = (*this)(i,j);
333  if (c != static_cast<T>(0.0))
334  {
335  ibuf.push_back(i);
336  jbuf.push_back(j);
337  cbuf.push_back(c);
338  }
339  }
340  }
341  this->comm().send(0,ibuf);
342  this->comm().send(0,jbuf);
343  this->comm().send(0,cbuf);
344  }
345 }
virtual bool initialized() const
dof_id_type first_dof(const processor_id_type proc) const
Definition: dof_map.h:684
dof_id_type end_dof(const processor_id_type proc) const
Definition: dof_map.h:708
dof_id_type numeric_index_type
Definition: id_types.h:99
virtual numeric_index_type m() const =0
DofMap const * _dof_map
与此对象关联的 DofMap 对象。可以查询其在处理器上的自由度计数。
virtual numeric_index_type n() const =0
void libMesh::ReferenceCounter::print_info ( std::ostream &  out_stream = libMesh::out)
staticinherited

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

在文件 reference_counter.C81 行定义.

参考 libMesh::ReferenceCounter::_enable_print_counter , 以及 libMesh::ReferenceCounter::get_info().

参考自 libMesh::LibMeshInit::~LibMeshInit().

82 {
84  out_stream << ReferenceCounter::get_info();
85 }
static std::string get_info()
Gets a string containing the reference information.
static bool _enable_print_counter
Flag to control whether reference count information is printed when print_info is called...
template<typename T >
void libMesh::PetscMatrix< T >::print_matlab ( const std::string &  name = "") const
overridevirtual

将矩阵内容以 MATLAB 格式打印出来。

参数
name输出的矩阵名称,默认为空字符串。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C568 行定义.

参考 libMesh::closed(), libMesh::WrappedPetsc< T >::get() , 以及 libMesh::initialized().

569 {
570  libmesh_assert (this->initialized());
571 
572  semiparallel_only();
573 
574  if (!this->closed())
575  {
576  libmesh_deprecated();
577  libmesh_warning("The matrix must be assembled before calling PetscMatrix::print_matlab().\n"
578  "Please update your code, as this warning will become an error in a future release.");
579  const_cast<PetscMatrix<T> *>(this)->close();
580  }
581 
582  PetscErrorCode ierr = 0;
583 
584  WrappedPetsc<PetscViewer> petsc_viewer;
585  ierr = PetscViewerCreate (this->comm().get(),
586  petsc_viewer.get());
587  LIBMESH_CHKERR(ierr);
588 
589  // Create an ASCII file containing the matrix
590  // if a filename was provided.
591  if (name != "")
592  {
593  ierr = PetscViewerASCIIOpen( this->comm().get(),
594  name.c_str(),
595  petsc_viewer.get());
596  LIBMESH_CHKERR(ierr);
597 
598 #if PETSC_VERSION_LESS_THAN(3,7,0)
599  ierr = PetscViewerSetFormat (petsc_viewer,
600  PETSC_VIEWER_ASCII_MATLAB);
601 #else
602  ierr = PetscViewerPushFormat (petsc_viewer,
603  PETSC_VIEWER_ASCII_MATLAB);
604 #endif
605 
606  LIBMESH_CHKERR(ierr);
607 
608  ierr = MatView (_mat, petsc_viewer);
609  LIBMESH_CHKERR(ierr);
610  }
611 
612  // Otherwise the matrix will be dumped to the screen.
613  else
614  {
615 #if PETSC_VERSION_LESS_THAN(3,7,0)
616  ierr = PetscViewerSetFormat (PETSC_VIEWER_STDOUT_WORLD,
617  PETSC_VIEWER_ASCII_MATLAB);
618 #else
619  ierr = PetscViewerPushFormat (PETSC_VIEWER_STDOUT_WORLD,
620  PETSC_VIEWER_ASCII_MATLAB);
621 #endif
622 
623  LIBMESH_CHKERR(ierr);
624 
625  ierr = MatView (_mat, PETSC_VIEWER_STDOUT_WORLD);
626  LIBMESH_CHKERR(ierr);
627  }
628 }
virtual bool initialized() const
T * get()
获取托管对象的指针。 这用于模拟以下代码: KSP ksp; KSPCreate(comm, &amp;ksp); 因为在这种上下文中,取包装对象的地址是没有意义的。
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
virtual void close() override
完成任何挂起的插入或添加操作。
Definition: petsc_matrix.C:983
template<typename T >
void libMesh::PetscMatrix< T >::print_personal ( std::ostream &  os = libMesh::out) const
overridevirtual

使用 PETSc 查看器将矩阵的内容打印到屏幕。 由于我们限制了一个 PETSc 实现,所以此函数仅允许打印到标准输出。

参数
os要写入的输出流,默认为 libMesh::out。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C635 行定义.

参考 libMesh::closed(), libMesh::initialized() , 以及 mkstemp().

636 {
637  libmesh_assert (this->initialized());
638 
639  // Routine must be called in parallel on parallel matrices
640  // and serial on serial matrices.
641  semiparallel_only();
642 
643  // #ifndef NDEBUG
644  // if (os != std::cout)
645  // libMesh::err << "Warning! PETSc can only print to std::cout!" << std::endl;
646  // #endif
647 
648  // Matrix must be in an assembled state to be printed
649  if (!this->closed())
650  {
651  libmesh_deprecated();
652  libmesh_warning("The matrix must be assembled before calling PetscMatrix::print_personal().\n"
653  "Please update your code, as this warning will become an error in a future release.");
654  const_cast<PetscMatrix<T> *>(this)->close();
655  }
656 
657  PetscErrorCode ierr=0;
658 
659  // Print to screen if ostream is stdout
660  if (os.rdbuf() == std::cout.rdbuf())
661  {
662  ierr = MatView(_mat, NULL);
663  LIBMESH_CHKERR(ierr);
664  }
665 
666  // Otherwise, print to the requested file, in a roundabout way...
667  else
668  {
669  // We will create a temporary filename, and file, for PETSc to
670  // write to.
671  std::string temp_filename;
672 
673  {
674  // Template for temporary filename
675  char c[] = "temp_petsc_matrix.XXXXXX";
676 
677  // Generate temporary, unique filename only on processor 0. We will
678  // use this filename for PetscViewerASCIIOpen, before copying it into
679  // the user's stream
680  if (this->processor_id() == 0)
681  {
682  int fd = mkstemp(c);
683 
684  // Check to see that mkstemp did not fail.
685  libmesh_error_msg_if(fd == -1, "mkstemp failed in PetscMatrix::print_personal()");
686 
687  // mkstemp returns a file descriptor for an open file,
688  // so let's close it before we hand it to PETSc!
689  ::close (fd);
690  }
691 
692  // Store temporary filename as string, makes it easier to broadcast
693  temp_filename = c;
694  }
695 
696  // Now broadcast the filename from processor 0 to all processors.
697  this->comm().broadcast(temp_filename);
698 
699  // PetscViewer object for passing to MatView
700  PetscViewer petsc_viewer;
701 
702  // This PETSc function only takes a string and handles the opening/closing
703  // of the file internally. Since print_personal() takes a reference to
704  // an ostream, we have to do an extra step... print_personal() should probably
705  // have a version that takes a string to get rid of this problem.
706  ierr = PetscViewerASCIIOpen( this->comm().get(),
707  temp_filename.c_str(),
708  &petsc_viewer);
709  LIBMESH_CHKERR(ierr);
710 
711  // Probably don't need to set the format if it's default...
712  // ierr = PetscViewerSetFormat (petsc_viewer,
713  // PETSC_VIEWER_DEFAULT);
714  // LIBMESH_CHKERR(ierr);
715 
716  // Finally print the matrix using the viewer
717  ierr = MatView (_mat, petsc_viewer);
718  LIBMESH_CHKERR(ierr);
719 
720  if (this->processor_id() == 0)
721  {
722  // Now the inefficient bit: open temp_filename as an ostream and copy the contents
723  // into the user's desired ostream. We can't just do a direct file copy, we don't even have the filename!
724  std::ifstream input_stream(temp_filename.c_str());
725  os << input_stream.rdbuf(); // The "most elegant" way to copy one stream into another.
726  // os.close(); // close not defined in ostream
727 
728  // Now remove the temporary file
729  input_stream.close();
730  std::remove(temp_filename.c_str());
731  }
732  }
733 }
virtual bool initialized() const
int mkstemp(char *tmpl)
Definition: win_mkstemp.h:13
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
virtual void close() override
完成任何挂起的插入或添加操作。
Definition: petsc_matrix.C:983
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

此函数与上述函数类似,但允许您重用 "submatrix" 的现有稀疏性模式,而不是再次分配它。 如果经常提取相同大小的子矩阵,这应该更有效。

在文件 sparse_matrix.h415 行定义.

参考 libMesh::SparseMatrix< T >::_get_submatrix().

418  {
419  this->_get_submatrix(submatrix,
420  rows,
421  cols,
422  true); // true 表示重用 submatrix
423  }
virtual void _get_submatrix(SparseMatrix< T > &, const std::vector< numeric_index_type > &, const std::vector< numeric_index_type > &, const bool) const
创建子矩阵和 reinit_submatrix 例程的受保护实现。
template<typename T >
void libMesh::PetscMatrix< T >::reset_preallocation ( )

重置矩阵以使用用户提供的原始非零模式。

在文件 petsc_matrix.C402 行定义.

参考 libMesh::initialized().

403 {
404 #if !PETSC_VERSION_LESS_THAN(3,9,0)
405  libmesh_assert (this->initialized());
406 
407  auto ierr = MatResetPreallocation(_mat);
408  LIBMESH_CHKERR(ierr);
409 #else
410  libmesh_warning("Your version of PETSc doesn't support resetting of "
411  "preallocation, so we will use your most recent sparsity "
412  "pattern. This may result in a degradation of performance\n");
413 #endif
414 }
virtual bool initialized() const
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::row_start ( ) const
overridevirtual

获取本地处理的行的起始索引。

返回
返回本地处理的行的起始索引。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1077 行定义.

参考 libMesh::initialized() , 以及 libMesh::MacroFunctions::stop().

1078 {
1079  libmesh_assert (this->initialized());
1080 
1081  PetscInt start=0, stop=0;
1082  PetscErrorCode ierr=0;
1083 
1084  ierr = MatGetOwnershipRange(_mat, &start, &stop);
1085  LIBMESH_CHKERR(ierr);
1086 
1087  return static_cast<numeric_index_type>(start);
1088 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
void stop(const char *file, int line, const char *date, const char *time)
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
numeric_index_type libMesh::PetscMatrix< T >::row_stop ( ) const
overridevirtual

获取本地处理的行的结束索引。

返回
返回本地处理的行的结束索引。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1093 行定义.

参考 libMesh::initialized() , 以及 libMesh::MacroFunctions::stop().

1094 {
1095  libmesh_assert (this->initialized());
1096 
1097  PetscInt start=0, stop=0;
1098  PetscErrorCode ierr=0;
1099 
1100  ierr = MatGetOwnershipRange(_mat, &start, &stop);
1101  LIBMESH_CHKERR(ierr);
1102 
1103  return static_cast<numeric_index_type>(stop);
1104 }
virtual bool initialized() const
dof_id_type numeric_index_type
Definition: id_types.h:99
void stop(const char *file, int line, const char *date, const char *time)
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::set ( const numeric_index_type  i,
const numeric_index_type  j,
const T  value 
)
overridevirtual

设置矩阵中特定位置的值。

参数
i行索引。
j列索引。
value要设置的值。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C1109 行定义.

参考 libMesh::initialized().

1112 {
1113  libmesh_assert (this->initialized());
1114 
1115  PetscErrorCode ierr=0;
1116  PetscInt i_val=i, j_val=j;
1117 
1118  PetscScalar petsc_value = static_cast<PetscScalar>(value);
1119  ierr = MatSetValues(_mat, 1, &i_val, 1, &j_val,
1120  &petsc_value, INSERT_VALUES);
1121  LIBMESH_CHKERR(ierr);
1122 }
virtual bool initialized() const
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::set_destroy_mat_on_exit ( bool  destroy = true)
virtual

设置析构时是否删除 Mat 对象。如果设置为 false,则允许 PETSc 管理它。

参数
destroy如果为 true,则析构时删除 Mat 对象;如果为 false,则不删除。

在文件 petsc_matrix.C1404 行定义.

1405 {
1406  this->_destroy_mat_on_exit = destroy;
1407 }
bool _destroy_mat_on_exit
此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。
Definition: petsc_matrix.h:508
template<typename T >
void libMesh::PetscMatrix< T >::set_matrix_type ( PetscMatrixType  mat_type)

设置矩阵的类型(如稀疏矩阵类型)。

参数
mat_type矩阵的类型。

在文件 petsc_matrix.C115 行定义.

116 {
117  _mat_type = mat_type;
118 }
PetscMatrixType _mat_type
存储 PETSc 矩阵的类型。
Definition: petsc_matrix.h:513
template<typename T >
void libMesh::PetscMatrix< T >::swap ( PetscMatrix< T > &  m_in)

交换两个 PetscMatrix 的内部数据指针,不交换实际值。

参数
matrix要交换的另一个 PetscMatrix 对象。

在文件 petsc_matrix.C1411 行定义.

参考 libMesh::PetscMatrix< T >::_destroy_mat_on_exit , 以及 libMesh::PetscMatrix< T >::_mat.

1412 {
1413  std::swap(_mat, m_in._mat);
1414  std::swap(_destroy_mat_on_exit, m_in._destroy_mat_on_exit);
1415 }
bool _destroy_mat_on_exit
此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。
Definition: petsc_matrix.h:508
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::update_preallocation_and_zero ( )

更新基于 dof_map 的稀疏模式,并将矩阵置零。这在稀疏模式在计算过程中发生变化的情况下很有用。

在文件 petsc_matrix.C396 行定义.

397 {
398  libmesh_not_implemented();
399 }
template<typename T>
virtual void libMesh::SparseMatrix< T >::update_sparsity_pattern ( const SparsityPattern::Graph )
inlinevirtualinherited

更新矩阵的稀疏性模式。当您的 SparseMatrix<T> 实现不需要此数据时, 只需不覆盖此方法。

libMesh::EpetraMatrix< T > , 以及 libMesh::LaspackMatrix< T > 重载.

在文件 sparse_matrix.h133 行定义.

参考自 libMesh::DofMap::update_sparsity_pattern().

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

将矩阵乘以 NumericVector arg 并将结果存储在 NumericVector dest 中。

参数
dest存储结果的目标 NumericVector。
arg乘法的源 NumericVector。
注解
此函数将矩阵乘以 NumericVector arg 并将结果存储在 dest 中。

在文件 sparse_matrix.C195 行定义.

参考 libMesh::NumericVector< T >::zero().

197 {
198  dest.zero();
199  this->vector_mult_add(dest,arg);
200 }
void vector_mult_add(NumericVector< T > &dest, const NumericVector< T > &arg) const
将矩阵乘以 NumericVector arg ,并将结果添加到 NumericVector dest 中。
template<typename T >
void libMesh::SparseMatrix< T >::vector_mult_add ( NumericVector< T > &  dest,
const NumericVector< T > &  arg 
) const
inherited

将矩阵乘以 NumericVector arg ,并将结果添加到 NumericVector dest 中。

在文件 sparse_matrix.C205 行定义.

参考 libMesh::NumericVector< T >::add_vector().

207 {
208  /* This functionality is actually implemented in the \p
209  NumericVector class. */
210  dest.add_vector(arg,*this);
211 }
template<typename T >
void libMesh::PetscMatrix< T >::zero ( )
overridevirtual

将矩阵所有元素置为零。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C417 行定义.

参考 libMesh::initialized().

418 {
419  libmesh_assert (this->initialized());
420 
421  semiparallel_only();
422 
423  PetscErrorCode ierr=0;
424 
425  PetscInt m_l, n_l;
426 
427  ierr = MatGetLocalSize(_mat,&m_l,&n_l);
428  LIBMESH_CHKERR(ierr);
429 
430  if (n_l)
431  {
432  ierr = MatZeroEntries(_mat);
433  LIBMESH_CHKERR(ierr);
434  }
435 }
virtual bool initialized() const
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
std::unique_ptr< SparseMatrix< T > > libMesh::PetscMatrix< T >::zero_clone ( ) const
overridevirtual

创建一个新的空矩阵,并将其所有元素置为零。

返回
返回新创建的空矩阵的唯一指针。

实现了 libMesh::SparseMatrix< T >.

在文件 petsc_matrix.C461 行定义.

参考 libMesh::closed().

462 {
463  libmesh_error_msg_if(!this->closed(), "Matrix must be closed before it can be cloned!");
464 
465  // Copy the nonzero pattern only
466  Mat copy;
467  PetscErrorCode ierr = MatDuplicate(_mat, MAT_DO_NOT_COPY_VALUES, &copy);
468  LIBMESH_CHKERR(ierr);
469 
470  // Call wrapping PetscMatrix constructor, have it take over
471  // ownership.
472  auto ret = std::make_unique<PetscMatrix<T>>(copy, this->comm());
473  ret->set_destroy_mat_on_exit(true);
474 
475  // Work around an issue on older compilers. We are able to simply
476  // "return ret;" on newer compilers
477  return std::unique_ptr<SparseMatrix<T>>(ret.release());
478 }
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
template<typename T >
void libMesh::PetscMatrix< T >::zero_rows ( std::vector< numeric_index_type > &  rows,
diag_value = 0.0 
)
overridevirtual

将指定行的所有元素置为零,并在对角线位置放置一个指定值。

参数
rows要置零的行的索引。
diag_value对角线上要放置的值,默认为 0.0。

重载 libMesh::SparseMatrix< T > .

在文件 petsc_matrix.C438 行定义.

参考 libMesh::initialized(), libMesh::numeric_petsc_cast() , 以及 libMesh::PS().

439 {
440  libmesh_assert (this->initialized());
441 
442  semiparallel_only();
443 
444  PetscErrorCode ierr=0;
445 
446  // As of petsc-dev at the time of 3.1.0, MatZeroRows now takes two additional
447  // optional arguments. The optional arguments (x,b) can be used to specify the
448  // solutions for the zeroed rows (x) and right hand side (b) to update.
449  // Could be useful for setting boundary conditions...
450  if (!rows.empty())
451  ierr = MatZeroRows(_mat, cast_int<PetscInt>(rows.size()),
452  numeric_petsc_cast(rows.data()), PS(diag_value),
453  NULL, NULL);
454  else
455  ierr = MatZeroRows(_mat, 0, NULL, PS(diag_value), NULL, NULL);
456 
457  LIBMESH_CHKERR(ierr);
458 }
PetscInt * numeric_petsc_cast(const numeric_index_type *p)
virtual bool initialized() const
PetscScalar PS(T val)
Definition: petsc_macro.h:166
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503

类成员变量说明

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

Actually holds the data.

在文件 reference_counter.h124 行定义.

参考自 libMesh::ReferenceCounter::get_info().

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

此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。

在文件 petsc_matrix.h508 行定义.

参考自 libMesh::PetscMatrix< T >::swap().

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

与此对象关联的 DofMap 对象。可以查询其在处理器上的自由度计数。

在文件 sparse_matrix.h494 行定义.

bool libMesh::ReferenceCounter::_enable_print_counter = true
staticprotectedinherited

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

在文件 reference_counter.h143 行定义.

参考自 libMesh::ReferenceCounter::disable_print_counter_info(), libMesh::ReferenceCounter::enable_print_counter_info() , 以及 libMesh::ReferenceCounter::print_info().

template<typename T>
bool libMesh::SparseMatrix< T >::_is_initialized
protectedinherited
template<typename T>
Mat libMesh::PetscMatrix< T >::_mat
private
template<typename T>
PetscMatrixType libMesh::PetscMatrix< T >::_mat_type
private

存储 PETSc 矩阵的类型。

在文件 petsc_matrix.h513 行定义.

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

Mutual exclusion object to enable thread-safe reference counting.

在文件 reference_counter.h137 行定义.

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

The number of objects.

Print the reference count information when the number returns to 0.

在文件 reference_counter.h132 行定义.

参考自 libMesh::ReferenceCounter::n_objects(), libMesh::ReferenceCounter::ReferenceCounter() , 以及 libMesh::ReferenceCounter::~ReferenceCounter().

template<typename T>
std::mutex libMesh::PetscMatrix< T >::_petsc_matrix_mutex
mutableprivate

用于线程安全的互斥锁(C++11 线程)。

在文件 petsc_matrix.h519 行定义.

template<typename T>
Threads::spin_mutex libMesh::PetscMatrix< T >::_petsc_matrix_mutex
mutableprivate

用于线程安全的旋转锁。

在文件 petsc_matrix.h524 行定义.

template<typename T>
SparsityPattern::Build const* libMesh::SparseMatrix< T >::_sp
protectedinherited

与此对象关联的 sparsity pattern。在需要时, 应查询其入口计数(或使用 need_full_sparsity_pattern,模式)。

在文件 sparse_matrix.h500 行定义.


该类的文档由以下文件生成: