libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
petsc_shell_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 #ifndef LIBMESH_PETSC_SHELL_MATRIX_H
19 #define LIBMESH_PETSC_SHELL_MATRIX_H
20 
21 
22 #include "libmesh/libmesh_config.h"
23 
24 #ifdef LIBMESH_HAVE_PETSC
25 
26 // Local includes
27 #include "libmesh/libmesh_common.h"
28 #include "libmesh/reference_counted_object.h"
29 #include "libmesh/libmesh.h"
30 #include "libmesh/shell_matrix.h"
31 #include "libmesh/petsc_macro.h"
32 #include "libmesh/petsc_solver_exception.h"
33 #include "libmesh/petsc_vector.h"
34 #include "libmesh/libmesh_common.h"
35 #include "libmesh/wrapped_petsc.h"
36 
37 // Petsc include files.
38 #ifdef I
39 # define LIBMESH_SAW_I
40 #endif
41 #include <petscmat.h>
42 #ifndef LIBMESH_SAW_I
43 # undef I // Avoid complex.h contamination
44 #endif
45 
46 namespace libMesh
47 {
48 
56 template <typename T>
57 class PetscShellMatrix : public ShellMatrix<T>
58 {
59 public:
60 
66  PetscShellMatrix (const Parallel::Communicator & comm_in);
67 
71  virtual ~PetscShellMatrix () = default;
72 
78  virtual numeric_index_type m () const override;
79 
85  virtual numeric_index_type n () const override;
86 
92  virtual numeric_index_type local_m () const;
93 
99  virtual numeric_index_type local_n () const;
100 
107  virtual void vector_mult (NumericVector<T> & dest,
108  const NumericVector<T> & arg) const override;
109 
116  virtual void vector_mult_add (NumericVector<T> & dest,
117  const NumericVector<T> & arg) const override;
118 
124  virtual void get_diagonal (NumericVector<T> & dest) const override;
125 
129  virtual void clear () override;
130 
134  virtual void init () override;
135 
141  virtual bool initialized() const;
142 
148  Mat mat();
149 
150 protected:
151 
156 
161 };
162 
163 
164 //-----------------------------------------------------------------------
165 // PetscShellMatrix inline members
166 template <typename T>
167 inline
168 PetscShellMatrix<T>::PetscShellMatrix (const Parallel::Communicator & comm_in):
169  ShellMatrix<T>(comm_in),
170  _is_initialized(false)
171 {}
172 
173 
174 
175 template <typename T>
176 inline
178 {
179  PetscErrorCode ierr;
180  PetscInt m;
181 
182  ierr = MatGetSize(_mat, &m, nullptr);
183  LIBMESH_CHKERR(ierr);
184 
185  return m;
186 }
187 
188 
189 
190 template <typename T>
191 inline
193 {
194  PetscErrorCode ierr;
195  PetscInt n;
196 
197  ierr = MatGetSize(_mat, nullptr, &n);
198  LIBMESH_CHKERR(ierr);
199 
200  return n;
201 }
202 
203 
204 template <typename T>
205 inline
207 {
208  PetscErrorCode ierr;
209  PetscInt m;
210 
211  ierr = MatGetLocalSize(_mat, &m, nullptr);
212  LIBMESH_CHKERR(ierr);
213 
214  return m;
215 }
216 
217 
218 
219 template <typename T>
220 inline
222 {
223  PetscErrorCode ierr;
224  PetscInt n;
225 
226  ierr = MatGetLocalSize(_mat, nullptr, &n);
227  LIBMESH_CHKERR(ierr);
228 
229  return n;
230 }
231 
232 
233 template <typename T>
234 inline
236 {
237  // 确保传入的NumericVector确实是PetscVector
238  PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
239 
240  PetscErrorCode ierr = MatGetDiagonal(_mat, petsc_dest.vec());
241  LIBMESH_CHKERR(ierr);
242 }
243 
244 
245 
246 } // namespace libMesh
247 
248 #endif // LIBMESH_HAVE_PETSC
249 #endif // LIBMESH_SPARSE_SHELL_MATRIX_H
virtual void vector_mult_add(NumericVector< T > &dest, const NumericVector< T > &arg) const override
执行矩阵与向量的乘法,并将结果添加到目标向量。
该类提供了一个良好的接口,用于访问 PETSc 的 Vec 对象。所有重写的虚拟函数都在 numeric_vector.h 中有文档说明。
Definition: petsc_vector.h:75
WrappedPetsc< Mat > _mat
PETSc Shell 矩阵。
Mat mat()
返回指向底层 PETSc Mat 对象的指针。调用此函数前必须调用 init()。
virtual bool initialized() const
检查矩阵是否已初始化。
这个类允许使用 PETSc shell 矩阵。 所有覆盖的虚拟函数都在 shell_matrix.h 中有文档说明。
virtual void clear() override
清除矩阵的内部数据结构。
提供了不同线性代数库的向量存储方案的统一接口。
Definition: dof_map.h:67
virtual numeric_index_type local_m() const
获取本地处理的矩阵行数。
Vec vec()
获取当前向量的原始 PETSc Vec 指针。
Definition: petsc_vector.h:641
virtual numeric_index_type n() const override
获取矩阵的列数。
virtual numeric_index_type m() const override
获取矩阵的行数。
virtual void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const override
执行矩阵与向量的乘法。
virtual ~PetscShellMatrix()=default
默认虚拟析构函数。
dof_id_type numeric_index_type
Definition: id_types.h:99
bool _is_initialized
Flag that tells if init() has been called.
Definition: libmesh.C:242
bool _is_initialized
表示矩阵是否已初始化的标志。
virtual numeric_index_type local_n() const
获取本地处理的矩阵列数。
virtual void get_diagonal(NumericVector< T > &dest) const override
获取矩阵的对角线。
PetscShellMatrix(const Parallel::Communicator &comm_in)
构造函数。
通用的Shell矩阵,即一个仅定义其对向量的作用的矩阵。此类包含必须在派生类中重写的纯虚拟成员。
virtual void init() override
初始化矩阵。