libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
petsc_shell_matrix.C
浏览该文件的文档.
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 #include "libmesh/libmesh_config.h"
19 #ifdef LIBMESH_HAVE_PETSC
20 
21 // Local includes
22 #include "libmesh/petsc_shell_matrix.h"
23 
24 namespace libMesh
25 {
26 
27 template <typename T>
29  const NumericVector<T> & arg) const
30 {
31  PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
32  const PetscVector<T> & petsc_arg = cast_ref<const PetscVector<T> &>(arg);
33 
34  PetscErrorCode ierr = MatMult(_mat, petsc_arg.vec(), petsc_dest.vec());
35  LIBMESH_CHKERR(ierr);
36 }
37 
38 
39 
40 template <typename T>
42  const NumericVector<T> & arg) const
43 {
44  PetscVector<T> & petsc_dest = cast_ref<PetscVector<T> &>(dest);
45  const PetscVector<T> & petsc_arg = cast_ref<const PetscVector<T> &>(arg);
46 
47  PetscErrorCode ierr = MatMultAdd(_mat, petsc_arg.vec(), petsc_dest.vec(), petsc_dest.vec());
48  LIBMESH_CHKERR(ierr);
49 }
50 
51 
52 template <typename T>
54 {
55  if (this->initialized())
56  {
57  _mat.destroy();
58  this->_is_initialized = false;
59  }
60 }
61 
62 
63 template <typename T>
65 {
66  libmesh_assert(this->_dof_map);
67 
68  // Clear initialized matrices
69  if (this->initialized())
70  this->clear();
71 
72  this->_is_initialized = true;
73 
74 
75  const numeric_index_type my_m = this->_dof_map->n_dofs();
76  const numeric_index_type my_n = my_m;
77  const numeric_index_type n_l = this->_dof_map->n_dofs_on_processor(this->processor_id());
78  const numeric_index_type m_l = n_l;
79 
80 
81  PetscErrorCode ierr = 0;
82  PetscInt m_global = static_cast<PetscInt>(my_m);
83  PetscInt n_global = static_cast<PetscInt>(my_n);
84  PetscInt m_local = static_cast<PetscInt>(m_l);
85  PetscInt n_local = static_cast<PetscInt>(n_l);
86 
87  ierr = MatCreate(this->comm().get(), _mat.get());
88  LIBMESH_CHKERR(ierr);
89  ierr = MatSetSizes(_mat, m_local, n_local, m_global, n_global);
90  LIBMESH_CHKERR(ierr);
91  PetscInt blocksize = static_cast<PetscInt>(this->_dof_map->block_size());
92  ierr = MatSetBlockSize(_mat, blocksize);
93  LIBMESH_CHKERR(ierr);
94 
95  ierr = MatSetType(_mat, MATSHELL);
96  LIBMESH_CHKERR(ierr);
97 
98  // Is prefix information available somewhere? Perhaps pass in the system name?
99  ierr = MatSetOptionsPrefix(_mat, "");
100  LIBMESH_CHKERR(ierr);
101  ierr = MatSetFromOptions(_mat);
102  LIBMESH_CHKERR(ierr);
103  ierr = MatSetUp(_mat);
104  LIBMESH_CHKERR(ierr);
105 }
106 
107 template <typename T>
109 {
110  return _is_initialized;
111 }
112 
113 template <typename T>
115 {
116  libmesh_error_msg_if(!_mat, "A petsc shell matrix is not created yet. Please call init() first.");
117  return _mat;
118 }
119 
120 //------------------------------------------------------------------
121 // Explicit instantiations
122 template class LIBMESH_EXPORT PetscShellMatrix<Number>;
123 
124 } // namespace libMesh
125 
126 #endif
virtual void vector_mult_add(NumericVector< T > &dest, const NumericVector< T > &arg) const override
执行矩阵与向量的乘法,并将结果添加到目标向量。
该类提供了一个良好的接口,用于访问 PETSc 的 Vec 对象。所有重写的虚拟函数都在 numeric_vector.h 中有文档说明。
Definition: petsc_vector.h:75
Mat mat()
返回指向底层 PETSc Mat 对象的指针。调用此函数前必须调用 init()。
virtual bool initialized() const
检查矩阵是否已初始化。
这个类允许使用 PETSc shell 矩阵。 所有覆盖的虚拟函数都在 shell_matrix.h 中有文档说明。
virtual void clear() override
清除矩阵的内部数据结构。
提供了不同线性代数库的向量存储方案的统一接口。
Definition: dof_map.h:67
Vec vec()
获取当前向量的原始 PETSc Vec 指针。
Definition: petsc_vector.h:641
virtual void vector_mult(NumericVector< T > &dest, const NumericVector< T > &arg) const override
执行矩阵与向量的乘法。
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 initialized()
Checks that library initialization has been done.
Definition: libmesh.C:261
virtual void init() override
初始化矩阵。