libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
diagonal_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/diagonal_matrix.h"
19 #include "libmesh/numeric_vector.h"
20 #include "libmesh/dense_matrix.h"
21 #include "libmesh/dof_map.h"
22 #include "libmesh/libmesh_common.h"
23 
24 
25 // C++ Includes
26 #include <memory>
27 
28 
29 namespace libMesh
30 {
31 
32 template <typename T>
33 DiagonalMatrix<T>::DiagonalMatrix(const Parallel::Communicator & comm_in) : SparseMatrix<T>(comm_in)
34 {
36 }
37 
38 template <typename T>
41 {
42  *_diagonal = vec;
43  return *this;
44 }
45 
46 template <typename T>
49 {
50  // Don't get confused by the &&: vec is an lvalue reference; the && just
51  // indicates that we are receiving an object that is safe to move from. Note
52  // that we are not going to use std::move here because we do not have
53  // (virtual) move assignment operations defined for NumericVector sub-classes
54  _diagonal->swap(vec);
55  return *this;
56 }
57 
58 template <typename T>
59 void
61  const numeric_index_type /*n*/,
62  const numeric_index_type m_l,
63  const numeric_index_type /*n_l*/,
64  const numeric_index_type /*nnz*/,
65  const numeric_index_type /*noz*/,
66  const numeric_index_type /*blocksize*/)
67 {
68  _diagonal->init(m, m_l);
69 }
70 
71 template <typename T>
72 void
73 DiagonalMatrix<T>::init(const ParallelType type)
74 {
75  libmesh_assert(this->_dof_map);
76 
77  _diagonal->init(this->_dof_map->n_dofs(),
78  this->_dof_map->n_dofs_on_processor(this->processor_id()),
79  /*fast=*/false,
80  type);
81 }
82 
83 template <typename T>
84 void
85 DiagonalMatrix<T>::init(const NumericVector<T> & other, const bool fast)
86 {
87  _diagonal->init(other, fast);
88 }
89 
90 template <typename T>
91 void
92 DiagonalMatrix<T>::init(const DiagonalMatrix<T> & other, const bool fast)
93 {
94  init(other.diagonal(), fast);
95 }
96 
97 template <typename T>
98 void
100 {
101  _diagonal->clear();
102 }
103 
104 template <typename T>
105 void
107 {
108  _diagonal->zero();
109 }
110 
111 template <typename T>
112 std::unique_ptr<SparseMatrix<T>> DiagonalMatrix<T>::zero_clone () const
113 {
114  // Make empty copy with matching comm
115  auto mat_copy = std::make_unique<DiagonalMatrix<T>>(this->comm());
116 
117  // Initialize copy with our same nonzero structure, and explicitly
118  // zero values using fast == false.
119  mat_copy->init(*this, /*fast=*/false);
120 
121  // Work around an issue on older compilers. We are able to simply
122  // "return mat_copy;" on newer compilers
123  return std::unique_ptr<SparseMatrix<T>>(mat_copy.release());
124 }
125 
126 
127 
128 template <typename T>
129 std::unique_ptr<SparseMatrix<T>> DiagonalMatrix<T>::clone () const
130 {
131  // Make empty copy with matching comm
132  auto mat_copy = std::make_unique<DiagonalMatrix<T>>(this->comm());
133 
134  // Make copy of our diagonal
135  auto diag_copy = _diagonal->clone();
136 
137  // Swap diag_copy with diagonal in mat_copy
138  *mat_copy = std::move(*diag_copy);
139 
140  // Work around an issue on older compilers. We are able to simply
141  // "return mat_copy;" on newer compilers
142  return std::unique_ptr<SparseMatrix<T>>(mat_copy.release());
143 }
144 
145 template <typename T>
146 void
148 {
149  _diagonal->close();
150 }
151 
152 template <typename T>
155 {
156  return _diagonal->size();
157 }
158 
159 template <typename T>
162 {
163  return _diagonal->size();
164 }
165 
166 template <typename T>
169 {
170  return _diagonal->first_local_index();
171 }
172 
173 template <typename T>
176 {
177  return _diagonal->last_local_index();
178 }
179 
180 template <typename T>
181 void
183 {
184  if (i == j)
185  _diagonal->set(i, value);
186 }
187 
188 template <typename T>
189 void
191 {
192  if (i == j)
193  _diagonal->add(i, value);
194 }
195 
196 template <typename T>
197 void
199  const std::vector<numeric_index_type> & rows,
200  const std::vector<numeric_index_type> & cols)
201 {
202  auto m = dm.m();
203  auto n = dm.n();
204 
205  for (decltype(m) i = 0; i < m; ++i)
206  for (decltype(n) j = 0; j < n; ++j)
207  {
208  auto global_i = rows[i];
209  auto global_j = cols[j];
210  if (global_i == global_j)
211  _diagonal->add(global_i, dm(i, j));
212  }
213 }
214 
215 template <typename T>
216 void
218  const std::vector<numeric_index_type> & dof_indices)
219 {
220  _diagonal->add_vector(dm.diagonal(), dof_indices);
221 }
222 
223 template <typename T>
224 void
226 {
227  auto x_diagonal = _diagonal->zero_clone();
228  X.get_diagonal(*x_diagonal);
229  _diagonal->add(a, *x_diagonal);
230 }
231 
232 template <typename T>
233 T
235 {
236  if (i == j)
237  return (*_diagonal)(i);
238  else
239  return 0;
240 }
241 
242 template <typename T>
243 Real
245 {
246  return _diagonal->l1_norm();
247 }
248 
249 template <typename T>
250 Real
252 {
253  return _diagonal->linfty_norm();
254 }
255 
256 template <typename T>
257 bool
259 {
260  return _diagonal->closed();
261 }
262 
263 template <typename T>
264 void
265 DiagonalMatrix<T>::print_personal(std::ostream & os) const
266 {
267  _diagonal->print(os);
268 }
269 
270 template <typename T>
271 void
273 {
274  dest = *_diagonal;
275 }
276 
277 template <typename T>
278 void
280 {
281  auto diagonal_dest = dynamic_cast<DiagonalMatrix<T> *>(&dest);
282  if (diagonal_dest)
283  *diagonal_dest = *_diagonal;
284  else
285  libmesh_error_msg("DenseMatrix<T>::get_transpose currently only accepts another DenseMatrix<T> "
286  "as its argument");
287 }
288 
289 template <typename T>
290 void
291 DiagonalMatrix<T>::zero_rows(std::vector<numeric_index_type> & rows, T val/*=0*/)
292 {
293  for (auto row : rows)
294  _diagonal->set(row, val);
295 }
296 
297 template <typename T>
298 const NumericVector<T> &
300 {
301  return *_diagonal;
302 }
303 
304 template class LIBMESH_EXPORT DiagonalMatrix<Number>;
305 }
unsigned int n() const
返回矩阵的列维度。
virtual void get_transpose(SparseMatrix< T > &dest) const override
获取当前矩阵的转置,并将其存储在指定的 SparseMatrix 对象中。
提供了不同线性代数库的向量存储方案的统一接口。
Definition: dof_map.h:67
virtual Real l1_norm() const override
计算矩阵的 范数。
virtual void add_matrix(const DenseMatrix< T > &dm, const std::vector< numeric_index_type > &rows, const std::vector< numeric_index_type > &cols) override
将指定的 DenseMatrix dm 添加到矩阵中的指定行和列。
virtual void zero() override
将矩阵的所有元素置零。
virtual bool closed() const override
返回矩阵是否已关闭,即是否已完成初始化并可以用于计算。
virtual numeric_index_type n() const override
返回矩阵的总列数。
const NumericVector< T > & diagonal() const
返回对底层数据存储 NumericVector 的常量引用。
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
Definition: dof_map.h:66
virtual numeric_index_type m() const override
返回矩阵的总行数。
virtual void close() override
完成矩阵的初始化和结构设置,使其可以用于计算。
dof_id_type numeric_index_type
Definition: id_types.h:99
virtual numeric_index_type row_stop() const override
返回矩阵在本地计算中的结束行号。
unsigned int m() const
返回矩阵的行维度。
virtual void clear() override
清除矩阵的所有内容,将其恢复到初始状态。
virtual void get_diagonal(NumericVector< T > &dest) const =0
复制矩阵的对角线部分到 dest。
DiagonalMatrix(const Parallel::Communicator &comm)
构造函数;将矩阵初始化为空,没有任何结构,即矩阵根本无法使用。因此,此构造函数仅对是类成员的矩阵有用。 所有其他矩阵应在数据流中的某一点创建,此时所有必要的信息都是可用的。 ...
virtual T operator()(const numeric_index_type i, const numeric_index_type j) const override
返回矩阵中指定位置的元素值。
对角矩阵类,其底层存储是一个向量
static std::unique_ptr< NumericVector< T > > build(const Parallel::Communicator &comm, const SolverPackage solver_package=libMesh::default_solver_package())
构建一个 NumericVector 对象。
virtual void print_personal(std::ostream &os=libMesh::out) const override
在输出流中打印矩阵的个性化信息。
virtual void zero_rows(std::vector< numeric_index_type > &rows, T val=0) override
将指定行的所有元素置零。
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void get_diagonal(NumericVector< T > &dest) const override
获取对角矩阵的对角线元素,并将它们存储在指定的 NumericVector 对象中。
virtual Real linfty_norm() const override
计算矩阵的 范数。
DenseVector< T > diagonal() const
返回矩阵的对角线。
DiagonalMatrix & operator=(DiagonalMatrix &&)=default
virtual void set(const numeric_index_type i, const numeric_index_type j, const T value) override
设置矩阵中指定位置的元素值。
virtual std::unique_ptr< SparseMatrix< T > > clone() const override
克隆当前矩阵,返回一个新的矩阵,其结构和维度相同。
virtual numeric_index_type row_start() const override
返回矩阵在本地计算中的起始行号。
std::unique_ptr< NumericVector< T > > _diagonal
底层对角矩阵存储
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
初始化矩阵的维度和结构
定义用于有限元类型计算的密集矩阵。 用于在求和成全局矩阵之前存储单元刚度矩阵。所有被覆盖的虚函数都记录在dense_matrix_base.h中。
Definition: dof_map.h:65
virtual void add(const numeric_index_type i, const numeric_index_type j, const T value) override
在矩阵中指定位置的元素上添加一个值。
virtual std::unique_ptr< SparseMatrix< T > > zero_clone() const override
创建当前矩阵的零副本,其结构和维度相同。