libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
petsc_matrix.h
浏览该文件的文档.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2023 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_PETSC_MATRIX_H
21 #define LIBMESH_PETSC_MATRIX_H
22 
23 #include "libmesh/libmesh_common.h"
24 
25 #ifdef LIBMESH_HAVE_PETSC
26 
27 // Local includes
28 #include "libmesh/sparse_matrix.h"
29 #include "libmesh/petsc_macro.h"
30 
31 // C++ includes
32 #include <algorithm>
33 #ifdef LIBMESH_HAVE_CXX11_THREAD
34 #include <atomic>
35 #include <mutex>
36 #endif
37 
38 // Macro to identify and debug functions which should be called in
39 // parallel on parallel matrices but which may be called in serial on
40 // serial matrices. This macro will only be valid inside non-static
41 // PetscMatrix methods
42 #undef semiparallel_only
43 #undef exceptionless_semiparallel_only
44 #ifndef NDEBUG
45 #include <cstring>
46 
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)
55 #else
56 #define semiparallel_only()
57 #define exceptionless_semiparallel_only()
58 #endif
59 
60 
61 // Petsc include files.
62 #ifdef I
63 # define LIBMESH_SAW_I
64 #endif
65 #include <petscmat.h>
66 #ifndef LIBMESH_SAW_I
67 # undef I // Avoid complex.h contamination
68 #endif
69 
70 
71 
72 namespace libMesh
73 {
74 
75 // Forward Declarations
76 template <typename T> class DenseMatrix;
77 
78 enum PetscMatrixType : int {
79  AIJ=0,
81 
82 
91 template <typename T>
92 class PetscMatrix final : public SparseMatrix<T>
93 {
94 public:
103  explicit PetscMatrix (const Parallel::Communicator & comm_in);
104 
112  explicit PetscMatrix (Mat m,
113  const Parallel::Communicator & comm_in);
114 
118  PetscMatrix (PetscMatrix &&) = delete;
119  PetscMatrix (const PetscMatrix &) = delete;
120  PetscMatrix & operator= (const PetscMatrix &) = delete;
121  PetscMatrix & operator= (PetscMatrix &&) = delete;
122 
126  virtual ~PetscMatrix ();
127 
133  void set_matrix_type(PetscMatrixType mat_type);
134 
146  virtual void init (const numeric_index_type m,
147  const numeric_index_type n,
148  const numeric_index_type m_l,
149  const numeric_index_type n_l,
150  const numeric_index_type nnz=30,
151  const numeric_index_type noz=10,
152  const numeric_index_type blocksize=1) override;
153 
165  void init (const numeric_index_type m,
166  const numeric_index_type n,
167  const numeric_index_type m_l,
168  const numeric_index_type n_l,
169  const std::vector<numeric_index_type> & n_nz,
170  const std::vector<numeric_index_type> & n_oz,
171  const numeric_index_type blocksize=1);
172 
173  virtual void init (ParallelType = PARALLEL) override;
174 
179 
183  void reset_preallocation();
184 
188  virtual void clear () noexcept override;
189 
193  virtual void zero () override;
194 
200  virtual std::unique_ptr<SparseMatrix<T>> zero_clone () const override;
201 
207  virtual std::unique_ptr<SparseMatrix<T>> clone () const override;
208 
215  virtual void zero_rows (std::vector<numeric_index_type> & rows, T diag_value = 0.0) override;
216 
220  virtual void close () override;
221 
225  virtual void flush () override;
226 
232  virtual numeric_index_type m () const override;
233 
239  virtual numeric_index_type local_m () const final;
240 
246  virtual numeric_index_type n () const override;
247 
253  numeric_index_type local_n () const;
254 
262 
268  virtual numeric_index_type row_start () const override;
269 
275  virtual numeric_index_type row_stop () const override;
276 
284  virtual void set (const numeric_index_type i,
285  const numeric_index_type j,
286  const T value) override;
287 
295  virtual void add (const numeric_index_type i,
296  const numeric_index_type j,
297  const T value) override;
298 
306  virtual void add_matrix (const DenseMatrix<T> & dm,
307  const std::vector<numeric_index_type> & rows,
308  const std::vector<numeric_index_type> & cols) override;
309 
316  virtual void add_matrix (const DenseMatrix<T> & dm,
317  const std::vector<numeric_index_type> & dof_indices) override;
318 
326  virtual void add_block_matrix (const DenseMatrix<T> & dm,
327  const std::vector<numeric_index_type> & brows,
328  const std::vector<numeric_index_type> & bcols) override;
329 
336  virtual void add_block_matrix (const DenseMatrix<T> & dm,
337  const std::vector<numeric_index_type> & dof_indices) override;
338 
349  virtual void add (const T a, const SparseMatrix<T> & X) override;
350 
358  virtual void matrix_matrix_mult (SparseMatrix<T> & X, SparseMatrix<T> & Y, bool reuse = false) override;
359 
368  virtual void add_sparse_matrix (const SparseMatrix<T> & spm,
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;
372 
380  virtual T operator () (const numeric_index_type i,
381  const numeric_index_type j) const override;
382 
388  virtual Real l1_norm () const override;
389 
395  virtual Real linfty_norm () const override;
396 
402  virtual bool closed() const override;
403 
409  virtual void set_destroy_mat_on_exit(bool destroy = true);
410 
417  virtual void print_personal(std::ostream & os=libMesh::out) const override;
418 
424  virtual void print_matlab(const std::string & name = "") const override;
425 
431  virtual void get_diagonal (NumericVector<T> & dest) const override;
432 
438  virtual void get_transpose (SparseMatrix<T> & dest) const override;
439 
445  void swap (PetscMatrix<T> &);
446 
455 Mat mat () { libmesh_assert (_mat); return _mat; }
456 
464 virtual void get_row(numeric_index_type i,
465  std::vector<numeric_index_type> & indices,
466  std::vector<T> & values) const override;
467 
479 virtual void create_submatrix_nosort(SparseMatrix<T> & submatrix,
480  const std::vector<numeric_index_type> & rows,
481  const std::vector<numeric_index_type> & cols) const override;
482 protected:
483 
494 virtual void _get_submatrix(SparseMatrix<T> & submatrix,
495  const std::vector<numeric_index_type> & rows,
496  const std::vector<numeric_index_type> & cols,
497  const bool reuse_submatrix) const override;
498 private:
499 
503 Mat _mat;
504 
509 
514 
515 #ifdef LIBMESH_HAVE_CXX11_THREAD
516 
519 mutable std::mutex _petsc_matrix_mutex;
520 #else
521 
524 mutable Threads::spin_mutex _petsc_matrix_mutex;
525 #endif
526 };
527 
528 } // namespace libMesh
529 
530 #endif // #ifdef LIBMESH_HAVE_PETSC
531 #endif // LIBMESH_PETSC_MATRIX_H
void reset_preallocation()
重置矩阵以使用用户提供的原始非零模式。
Definition: petsc_matrix.C:402
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
获取矩阵中特定位置的值。
void set_matrix_type(PetscMatrixType mat_type)
设置矩阵的类型(如稀疏矩阵类型)。
Definition: petsc_matrix.C:115
virtual void print_matlab(const std::string &name="") const override
将矩阵内容以 MATLAB 格式打印出来。
Definition: petsc_matrix.C:568
bool _destroy_mat_on_exit
此布尔值仅应在接受 PETSc Mat 对象的构造函数中设置为 false。
Definition: petsc_matrix.h:508
virtual numeric_index_type local_m() const final
获取由该进程拥有的矩阵行数。
virtual void zero() override
将矩阵所有元素置为零。
Definition: petsc_matrix.C:417
PetscMatrix(const Parallel::Communicator &comm_in)
构造函数:将矩阵初始化为空,没有任何结构。这个构造函数只适用于类的成员矩阵。 所有其他矩阵应该在所有必要信息都可用的数据流中的某一点创建。
Definition: petsc_matrix.C:83
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
virtual void clear() noexceptoverride
clear() 在析构函数中调用,因此它不应该抛出异常。
Definition: petsc_matrix.C:503
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
用于线程安全的旋转锁。
Definition: petsc_matrix.h:524
提供了不同线性代数库的向量存储方案的统一接口。
Definition: dof_map.h:67
void update_preallocation_and_zero()
更新基于 dof_map 的稀疏模式,并将矩阵置零。这在稀疏模式在计算过程中发生变化的情况下很有用。
Definition: petsc_matrix.C:396
virtual void add_block_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &brows, const std::vector< numeric_index_type > &bcols) override
将稠密块矩阵添加到此稀疏矩阵的指定块行和块列。
Definition: petsc_matrix.C:768
virtual void print_personal(std::ostream &os=libMesh::out) const override
使用 PETSc 查看器将矩阵的内容打印到屏幕。 由于我们限制了一个 PETSc 实现,所以此函数仅允许打印到标准输出。
Definition: petsc_matrix.C:635
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const override
创建一个新的空矩阵,并将其所有元素置为零。
Definition: petsc_matrix.C:461
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
克隆当前矩阵。
Definition: petsc_matrix.C:483
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
Definition: dof_map.h:66
virtual ~PetscMatrix()
析构函数,释放矩阵资源。
Definition: petsc_matrix.C:109
std::mutex _petsc_matrix_mutex
用于线程安全的互斥锁(C++11 线程)。
Definition: petsc_matrix.h:519
dof_id_type numeric_index_type
Definition: id_types.h:99
PetscMatrix & operator=(const PetscMatrix &)=delete
virtual numeric_index_type row_stop() const override
获取本地处理的行的结束索引。
OStreamProxy out
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 函数。
Definition: petsc_matrix.C:863
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 矩阵的类型。
Definition: petsc_matrix.h:513
virtual void get_diagonal(NumericVector< T > &dest) const override
获取矩阵的对角线并将其存储在给定的向量中。
Definition: petsc_matrix.C:937
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
获取矩阵的转置。
Definition: petsc_matrix.C:950
virtual Real l1_norm() const override
计算矩阵的 L1 范数。
Definition: petsc_matrix.C:522
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
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
设置矩阵中特定位置的值。
这个类提供了一个方便的接口,用于操作并行稀疏矩阵的 PETSc C 库数据结构。 所有覆盖的虚拟函数都在 sparse_matrix.h 中有详细的文档说明。
Definition: petsc_matrix.h:92
Mat _mat
用于存储值的 PETSc 矩阵数据类型。
Definition: petsc_matrix.h:503
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。
Definition: petsc_matrix.C:812
virtual void zero_rows(std::vector< numeric_index_type > &rows, T diag_value=0.0) override
将指定行的所有元素置为零,并在对角线位置放置一个指定值。
Definition: petsc_matrix.C:438
Mat mat()
返回原始的 PETSc 矩阵指针。
Definition: petsc_matrix.h:455
定义用于有限元类型计算的密集矩阵。 用于在求和成全局矩阵之前存储单元刚度矩阵。所有被覆盖的虚函数都记录在dense_matrix_base.h中。
Definition: dof_map.h:65
virtual void flush() override
刷新矩阵的内部缓冲区,确保所有操作都已完成。
Definition: petsc_matrix.C:998
virtual void close() override
完成任何挂起的插入或添加操作。
Definition: petsc_matrix.C:983
virtual Real linfty_norm() const override
计算矩阵的无穷范数。
Definition: petsc_matrix.C:545