20 #ifndef LIBMESH_PETSC_MATRIX_H
21 #define LIBMESH_PETSC_MATRIX_H
23 #include "libmesh/libmesh_common.h"
25 #ifdef LIBMESH_HAVE_PETSC
28 #include "libmesh/sparse_matrix.h"
29 #include "libmesh/petsc_macro.h"
33 #ifdef LIBMESH_HAVE_CXX11_THREAD
42 #undef semiparallel_only
43 #undef exceptionless_semiparallel_only
47 #define semiparallel_only() do { if (this->initialized()) { const char * mytype; \
48 MatGetType(_mat,&mytype); \
49 if (!strcmp(mytype, MATSEQAIJ)) \
50 parallel_object_only(); } } while (0)
51 #define exceptionless_semiparallel_only() do { if (this->initialized()) { const char * mytype; \
52 MatGetType(_mat,&mytype); \
53 if (!strcmp(mytype, MATSEQAIJ)) \
54 exceptionless_parallel_object_only(); } } while (0)
56 #define semiparallel_only()
57 #define exceptionless_semiparallel_only()
63 # define LIBMESH_SAW_I
67 # undef I // Avoid complex.h contamination
76 template <
typename T>
class DenseMatrix;
103 explicit PetscMatrix (
const Parallel::Communicator & comm_in);
113 const Parallel::Communicator & comm_in);
169 const std::vector<numeric_index_type> & n_nz,
170 const std::vector<numeric_index_type> & n_oz,
173 virtual void init (ParallelType = PARALLEL)
override;
188 virtual void clear () noexcept
override;
193 virtual void zero ()
override;
200 virtual std::unique_ptr<SparseMatrix<T>>
zero_clone ()
const override;
207 virtual std::unique_ptr<SparseMatrix<T>>
clone ()
const override;
215 virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0)
override;
220 virtual void close ()
override;
225 virtual void flush ()
override;
286 const T value)
override;
297 const T value)
override;
307 const std::vector<numeric_index_type> & rows,
308 const std::vector<numeric_index_type> & cols)
override;
317 const std::vector<numeric_index_type> & dof_indices)
override;
327 const std::vector<numeric_index_type> & brows,
328 const std::vector<numeric_index_type> & bcols)
override;
337 const std::vector<numeric_index_type> & dof_indices)
override;
369 const std::map<numeric_index_type,numeric_index_type> & row_ltog,
370 const std::map<numeric_index_type,numeric_index_type> & col_ltog,
371 const T scalar)
override;
402 virtual bool closed()
const override;
424 virtual void print_matlab(
const std::string & name =
"")
const override;
465 std::vector<numeric_index_type> & indices,
466 std::vector<T> & values)
const override;
480 const std::vector<numeric_index_type> & rows,
481 const std::vector<numeric_index_type> & cols)
const override;
495 const std::vector<numeric_index_type> & rows,
496 const std::vector<numeric_index_type> & cols,
497 const bool reuse_submatrix)
const override;
515 #ifdef LIBMESH_HAVE_CXX11_THREAD
530 #endif // #ifdef LIBMESH_HAVE_PETSC
531 #endif // LIBMESH_PETSC_MATRIX_H
void reset_preallocation()
重置矩阵以使用用户提供的原始非零模式。
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
获取矩阵中特定位置的值。
void set_matrix_type(PetscMatrixType mat_type)
设置矩阵的类型(如稀疏矩阵类型)。
virtual void print_matlab(const std::string &name="") const override
将矩阵内容以 MATLAB 格式打印出来。
bool _destroy_mat_on_exit
此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。
virtual numeric_index_type local_m() const final
获取由该进程拥有的矩阵行数。
virtual void zero() override
将矩阵所有元素置为零。
PetscMatrix(const Parallel::Communicator &comm_in)
构造函数:将矩阵初始化为空,没有任何结构。这个构造函数只适用于类的成员矩阵。 所有其他矩阵应该在所有必要信息都可用的数据流中的某一点创建。
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 矩阵。
virtual void clear() noexceptoverride
clear() 在析构函数中调用,因此它不应该抛出异常。
void get_local_size(numeric_index_type &m, numeric_index_type &n) const
获取由该进程拥有的矩阵行数和列数。
virtual numeric_index_type n() const override
获取矩阵的列数。
Threads::spin_mutex _petsc_matrix_mutex
用于线程安全的旋转锁。
void update_preallocation_and_zero()
更新基于 dof_map 的稀疏模式,并将矩阵置零。这在稀疏模式在计算过程中发生变化的情况下很有用。
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 print_personal(std::ostream &os=libMesh::out) const override
使用 PETSc 查看器将矩阵的内容打印到屏幕。 由于我们限制了一个 PETSc 实现,所以此函数仅允许打印到标准输出。
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const override
创建一个新的空矩阵,并将其所有元素置为零。
void swap(PetscMatrix< T > &)
交换两个 PetscMatrix 的内部数据指针,不交换实际值。
numeric_index_type local_n() const
获取由该进程拥有的矩阵列数。
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 std::unique_ptr< SparseMatrix< T > > clone() const override
克隆当前矩阵。
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
virtual ~PetscMatrix()
析构函数,释放矩阵资源。
std::mutex _petsc_matrix_mutex
用于线程安全的互斥锁(C++11 线程)。
dof_id_type numeric_index_type
PetscMatrix & operator=(const PetscMatrix &)=delete
virtual numeric_index_type row_stop() const override
获取本地处理的行的结束索引。
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
根据给定的 rows 和 cols 向量中的索引创建一个子矩阵,类似于 create_submatrix 函数。
virtual void set_destroy_mat_on_exit(bool destroy=true)
设置析构时是否删除 Mat 对象。如果设置为 false,则允许 PETSc 管理它。
virtual numeric_index_type m() const override
获取矩阵的行数。
virtual void matrix_matrix_mult(SparseMatrix< T > &X, SparseMatrix< T > &Y, bool reuse=false) override
计算 Y = A*X,其中 A 是当前矩阵,X 是另一个矩阵。
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override
在矩阵的特定位置添加值。
PetscMatrixType _mat_type
存储 PETSc 矩阵的类型。
virtual void get_diagonal(NumericVector< T > &dest) const override
获取矩阵的对角线并将其存储在给定的向量中。
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual bool closed() const override
检查矩阵是否已经关闭,即所有的插入和添加操作是否已经完成。
virtual numeric_index_type row_start() const override
获取本地处理的行的起始索引。
virtual void get_transpose(SparseMatrix< T > &dest) const override
获取矩阵的转置。
virtual Real l1_norm() const override
计算矩阵的 L1 范数。
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 set(const numeric_index_type i, const numeric_index_type j, const T value) override
设置矩阵中特定位置的值。
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
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
创建或重新初始化一个由 rows 和 cols 向量中给出的索引定义的矩阵 submatrix。
virtual void zero_rows(std::vector< numeric_index_type > &rows, T diag_value=0.0) override
将指定行的所有元素置为零,并在对角线位置放置一个指定值。
Mat mat()
返回原始的 PETSc 矩阵指针。
定义用于有限元类型计算的密集矩阵。 用于在求和成全局矩阵之前存储单元刚度矩阵。所有被覆盖的虚函数都记录在dense_matrix_base.h中。
virtual void flush() override
刷新矩阵的内部缓冲区,确保所有操作都已完成。
virtual void close() override
完成任何挂起的插入或添加操作。
virtual Real linfty_norm() const override
计算矩阵的无穷范数。