libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
eigen_sparse_vector.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 
21 #ifndef LIBMESH_EIGEN_SPARSE_VECTOR_H
22 #define LIBMESH_EIGEN_SPARSE_VECTOR_H
23 
24 
25 
26 #include "libmesh/libmesh_common.h"
27 
28 #ifdef LIBMESH_HAVE_EIGEN
29 
30 // Local includes
31 #include "libmesh/eigen_core_support.h"
32 #include "libmesh/numeric_vector.h"
33 
34 // C++ includes
35 #include <limits>
36 #include <mutex>
37 
38 namespace libMesh
39 {
40 
41 // Forward declarations
42 template <typename T> class EigenSparseMatrix;
43 template <typename T> class EigenSparseLinearSolver;
44 template <typename T> class SparseMatrix;
45 
54 template <typename T>
55 class EigenSparseVector final : public NumericVector<T>
56 {
57 public:
58 
62  explicit
63  EigenSparseVector (const Parallel::Communicator & comm_in,
64  const ParallelType = AUTOMATIC);
65 
69  explicit
70  EigenSparseVector (const Parallel::Communicator & comm_in,
71  const numeric_index_type n,
72  const ParallelType = AUTOMATIC);
73 
78  EigenSparseVector (const Parallel::Communicator & comm_in,
79  const numeric_index_type n,
80  const numeric_index_type n_local,
81  const ParallelType = AUTOMATIC);
82 
88  EigenSparseVector (const Parallel::Communicator & comm_in,
89  const numeric_index_type N,
90  const numeric_index_type n_local,
91  const std::vector<numeric_index_type> & ghost,
92  const ParallelType = AUTOMATIC);
93 
99  EigenSparseVector<T> & operator= (const EigenSparseVector<T> & v);
100 
105  EigenSparseVector (EigenSparseVector &&) = default;
106  EigenSparseVector (const EigenSparseVector &) = default;
108  virtual ~EigenSparseVector () = default;
109 
113  typedef EigenSV DataType;
114 
115  virtual void close () override;
116 
117  virtual void clear () override;
118 
119  virtual void zero () override;
120 
121  virtual std::unique_ptr<NumericVector<T>> zero_clone () const override;
122 
123  virtual std::unique_ptr<NumericVector<T>> clone () const override;
124 
125  virtual void init (const numeric_index_type N,
126  const numeric_index_type n_local,
127  const bool fast=false,
128  const ParallelType ptype=AUTOMATIC) override;
129 
130  virtual void init (const numeric_index_type N,
131  const bool fast=false,
132  const ParallelType ptype=AUTOMATIC) override;
133 
134  virtual void init (const numeric_index_type N,
135  const numeric_index_type n_local,
136  const std::vector<numeric_index_type> & ghost,
137  const bool fast = false,
138  const ParallelType = AUTOMATIC) override;
139 
140  virtual void init (const NumericVector<T> & other,
141  const bool fast = false) override;
142 
143  virtual NumericVector<T> & operator= (const T s) override;
144 
145  virtual NumericVector<T> & operator= (const NumericVector<T> & v) override;
146 
147  virtual NumericVector<T> & operator= (const std::vector<T> & v) override;
148 
149  virtual Real min () const override;
150 
151  virtual Real max () const override;
152 
153  virtual T sum () const override;
154 
155  virtual Real l1_norm () const override;
156 
157  virtual Real l2_norm () const override;
158 
159  virtual Real linfty_norm () const override;
160 
161  virtual numeric_index_type size () const override;
162 
163  virtual numeric_index_type local_size() const override;
164 
165  virtual numeric_index_type first_local_index() const override;
166 
167  virtual numeric_index_type last_local_index() const override;
168 
169  virtual T operator() (const numeric_index_type i) const override;
170 
171  virtual NumericVector<T> & operator += (const NumericVector<T> & v) override;
172 
173  virtual NumericVector<T> & operator -= (const NumericVector<T> & v) override;
174 
175  virtual NumericVector<T> & operator *= (const NumericVector<T> & v_in) override;
176 
177  virtual NumericVector<T> & operator /= (const NumericVector<T> & v_in) override;
178 
179  virtual void reciprocal() override;
180 
181  virtual void conjugate() override;
182 
183  virtual void set (const numeric_index_type i, const T value) override;
184 
185  virtual void add (const numeric_index_type i, const T value) override;
186 
187  virtual void add (const T s) override;
188 
189  virtual void add (const NumericVector<T> & v) override;
190 
191  virtual void add (const T a, const NumericVector<T> & v) override;
192 
198 
199  virtual void add_vector (const NumericVector<T> & v,
200  const SparseMatrix<T> & A) override;
201 
202  virtual void add_vector_transpose (const NumericVector<T> & v,
203  const SparseMatrix<T> & A) override;
204 
205  virtual void scale (const T factor) override;
206 
207  virtual void abs() override;
208 
209  virtual T dot(const NumericVector<T> & v) const override;
210 
211  virtual void localize (std::vector<T> & v_local) const override;
212 
213  virtual void localize (NumericVector<T> & v_local) const override;
214 
215  virtual void localize (NumericVector<T> & v_local,
216  const std::vector<numeric_index_type> & send_list) const override;
217 
218  virtual void localize (std::vector<T> & v_local,
219  const std::vector<numeric_index_type> & indices) const override;
220 
221  virtual void localize (const numeric_index_type first_local_idx,
222  const numeric_index_type last_local_idx,
223  const std::vector<numeric_index_type> & send_list) override;
224 
225  virtual void localize_to_one (std::vector<T> & v_local,
226  const processor_id_type proc_id=0) const override;
227 
228  virtual void pointwise_mult (const NumericVector<T> & vec1,
229  const NumericVector<T> & vec2) override;
230 
231  virtual void pointwise_divide (const NumericVector<T> & vec1,
232  const NumericVector<T> & vec2) override;
233 
234  virtual void swap (NumericVector<T> & v) override;
235 
236  virtual std::size_t max_allowed_id() const override;
237 
243  DataType & vec () { return _vec; }
244  const DataType & vec () const { return _vec; }
245 
246 private:
247 
252 
256  friend class EigenSparseMatrix<T>;
257  friend class EigenSparseLinearSolver<T>;
258 };
259 
260 
261 
262 // ---------------------------------------------------------
263 // EigenSparseVector inline methods
264 template <typename T>
265 inline
266 EigenSparseVector<T>::EigenSparseVector (const Parallel::Communicator & comm_in,
267  const ParallelType ptype)
268  : NumericVector<T>(comm_in, ptype)
269 {
270  this->_type = ptype;
271 }
272 
273 
274 
275 template <typename T>
276 inline
277 EigenSparseVector<T>::EigenSparseVector (const Parallel::Communicator & comm_in,
278  const numeric_index_type n,
279  const ParallelType ptype)
280  : NumericVector<T>(comm_in, ptype)
281 {
282  this->init(n, n, false, ptype);
283 }
284 
285 
286 
287 template <typename T>
288 inline
289 EigenSparseVector<T>::EigenSparseVector (const Parallel::Communicator & comm_in,
290  const numeric_index_type n,
291  const numeric_index_type n_local,
292  const ParallelType ptype)
293  : NumericVector<T>(comm_in, ptype)
294 {
295  this->init(n, n_local, false, ptype);
296 }
297 
298 
299 
300 template <typename T>
301 inline
302 EigenSparseVector<T>::EigenSparseVector (const Parallel::Communicator & comm_in,
303  const numeric_index_type N,
304  const numeric_index_type n_local,
305  const std::vector<numeric_index_type> & ghost,
306  const ParallelType ptype)
307  : NumericVector<T>(comm_in, ptype)
308 {
309  this->init(N, n_local, ghost, false, ptype);
310 }
311 
312 
313 
314 template <typename T>
315 inline
317  const numeric_index_type n_local,
318  const bool fast,
319  const ParallelType)
320 {
321  // Eigen vectors only for serial cases,
322  // but can provide a "parallel" vector on one processor.
323  libmesh_error_msg_if(n != n_local, "Error: EigenSparseVectors can only be used in serial!");
324 
325  this->_type = SERIAL;
326 
327  // Clear initialized vectors
328  if (this->initialized())
329  this->clear();
330 
331  _vec.resize(n);
332 
333  this->_is_initialized = true;
334  this->_is_closed = true;
335 
336  // Optionally zero out all components
337  if (fast == false)
338  this->zero ();
339 
340  return;
341 }
342 
343 
344 
345 template <typename T>
346 inline
348  const bool fast,
349  const ParallelType ptype)
350 {
351  this->init(n,n,fast,ptype);
352 }
353 
354 
355 template <typename T>
356 inline
358  const numeric_index_type n_local,
359  const std::vector<numeric_index_type> & libmesh_dbg_var(ghost),
360  const bool fast,
361  const ParallelType ptype)
362 {
363  libmesh_assert(ghost.empty());
364  this->init(n,n_local,fast,ptype);
365 }
366 
367 
368 
369 /* Default implementation for solver packages for which ghosted
370  vectors are not yet implemented. */
371 template <class T>
373  const bool fast)
374 {
375  this->init(other.size(),other.local_size(),fast,other.type());
376 }
377 
378 
379 
380 template <typename T>
381 inline
383 {
384  libmesh_assert (this->initialized());
385 
386  this->_is_closed = true;
387 }
388 
389 
390 
391 template <typename T>
392 inline
394 {
395  _vec.resize(0);
396 
397  this->_is_initialized = false;
398  this->_is_closed = false;
399 }
400 
401 
402 
403 template <typename T> inline
405 {
406  libmesh_assert (this->initialized());
407  libmesh_assert (this->closed());
408 
409  _vec.setZero();
410 }
411 
412 
413 
414 template <typename T>
415 inline
416 std::unique_ptr<NumericVector<T>> EigenSparseVector<T>::zero_clone () const
417 {
418  NumericVector<T> * cloned_vector = new EigenSparseVector<T>(this->comm());
419  cloned_vector->init(*this);
420  return std::unique_ptr<NumericVector<T>>(cloned_vector);
421 }
422 
423 
424 
425 template <typename T>
426 inline
427 std::unique_ptr<NumericVector<T>> EigenSparseVector<T>::clone () const
428 {
429  NumericVector<T> * cloned_vector = new EigenSparseVector<T>(this->comm());
430  cloned_vector->init(*this, true);
431  *cloned_vector = *this;
432  return std::unique_ptr<NumericVector<T>>(cloned_vector);
433 }
434 
435 
436 
437 template <typename T>
438 inline
440 {
441  libmesh_assert (this->initialized());
442 
443  return static_cast<numeric_index_type>(_vec.size());
444 }
445 
446 
447 
448 template <typename T>
449 inline
451 {
452  libmesh_assert (this->initialized());
453 
454  return this->size();
455 }
456 
457 
458 
459 template <typename T>
460 inline
462 {
463  libmesh_assert (this->initialized());
464 
465  return 0;
466 }
467 
468 
469 
470 template <typename T>
471 inline
473 {
474  libmesh_assert (this->initialized());
475 
476  return this->size();
477 }
478 
479 
480 
481 template <typename T>
482 inline
483 void EigenSparseVector<T>::set (const numeric_index_type i, const T value)
484 {
485  libmesh_assert (this->initialized());
486  libmesh_assert_less (i, this->size());
487 
488  std::scoped_lock lock(this->_numeric_vector_mutex);
489  _vec[static_cast<eigen_idx_type>(i)] = value;
490 
491  this->_is_closed = false;
492 }
493 
494 
495 
496 template <typename T>
497 inline
498 void EigenSparseVector<T>::add (const numeric_index_type i, const T value)
499 {
500  libmesh_assert (this->initialized());
501  libmesh_assert_less (i, this->size());
502 
503  std::scoped_lock lock(this->_numeric_vector_mutex);
504  _vec[static_cast<eigen_idx_type>(i)] += value;
505 
506  this->_is_closed = false;
507 }
508 
509 
510 
511 template <typename T>
512 inline
514 {
515  libmesh_assert (this->initialized());
516  libmesh_assert ( ((i >= this->first_local_index()) &&
517  (i < this->last_local_index())) );
518 
519  return _vec[static_cast<eigen_idx_type>(i)];
520 }
521 
522 
523 
524 template <typename T>
525 inline
527 {
528  EigenSparseVector<T> & v = cast_ref<EigenSparseVector<T> &>(other);
529 
530  _vec.swap(v._vec);
531 
532  std::swap (this->_is_closed, v._is_closed);
533  std::swap (this->_is_initialized, v._is_initialized);
534  std::swap (this->_type, v._type);
535 }
536 
537 
538 
539 template <typename T>
540 inline
542 {
543  // We use the Eigen::Matrix type which appears to be templated on
544  // int for its sizes, see e.g. https://eigen.tuxfamily.org/dox/classEigen_1_1Matrix.html
545  return std::numeric_limits<int>::max();
546 }
547 
548 } // namespace libMesh
549 
550 
551 #endif // #ifdef LIBMESH_HAVE_EIGEN
552 #endif // LIBMESH_EIGEN_SPARSE_VECTOR_H
virtual std::size_t max_allowed_id() const override
返回 NumericVector 可以包含的最大条目数(在所有处理器上)。
bool closed()
Checks that the library has been closed.
Definition: libmesh.C:268
EigenSparseMatrix 类包装了来自 Eigen 库的稀疏矩阵对象。
virtual Real linfty_norm() const override
获取向量的 -范数,即向量的最大绝对值。
virtual void localize(std::vector< T > &v_local) const override
创建全局向量的副本并存储在本地向量 v_local 中。
virtual void conjugate() override
反转向量中每个条目的虚部。
virtual void pointwise_divide(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
计算该向量与另一个向量的逐点除法。
virtual void pointwise_mult(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
比较该向量与另一个向量的全局相对差异。
virtual T sum() const override
获取向量中所有值的总和。
virtual void add_vector(const NumericVector< T > &v, const SparseMatrix< T > &A) override
计算 , 即将 SparseMatrix A 和 NumericVector v 的乘积添加到 this。
DataType _vec
Actual Eigen::SparseVector&lt;&gt; we are wrapping.
virtual void reciprocal() override
计算每个向量条目的分量倒数, 。
virtual numeric_index_type size() const =0
获取向量的大小。
virtual NumericVector< T > & operator-=(const NumericVector< T > &v) override
将 v 从 *this 减去, 。等价于 u.add(-1, v)。
virtual Real min() const override
获取向量中的最小值,或者在复数情况下获取最小的实部。
virtual std::unique_ptr< NumericVector< T > > clone() const override
返回一个包装了此向量副本的智能指针。
提供了不同线性代数库的向量存储方案的统一接口。
Definition: dof_map.h:67
virtual numeric_index_type last_local_index() const override
获取实际存储在该处理器上的最后一个向量元素的索引+1。
int32_t eigen_idx_type
const Number zero
.
Definition: libmesh.h:248
const DataType & vec() const
virtual void close() override
调用 NumericVector 的内部组装函数,确保值在处理器之间一致。
virtual void init(const numeric_index_type n, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC)=0
更改向量的维度为 n 。如果可能的话 ,该向量的保留内存保持不变。 如果 n==0 ,所有内存都将被释放。因此,如果要调整向量的大小并释放不需要的内存, 必须首先调用 init(0) ,然后调用 ini...
virtual void swap(NumericVector< T > &v) override
交换该向量的内容与向量 v 的内容。子类应提供足够的间接性以使此操作成为 O(1) 的头部交换操作。
virtual std::unique_ptr< NumericVector< T > > zero_clone() const override
返回一个智能指针,指向具有相同类型、大小和分区的此向量的副本,但所有条目都为零。
uint8_t processor_id_type
Definition: id_types.h:104
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
Definition: dof_map.h:66
EigenSV DataType
Convenient typedefs.
virtual Real l2_norm() 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
virtual void clear() override
将 NumericVector&lt;T&gt; 恢复到原始状态。
virtual Real max() const override
获取向量中的最大值,或者在复数情况下获取最大的实部。
virtual NumericVector< T > & operator/=(const NumericVector< T > &v_in) override
计算此向量条目与另一个向量的分量除法, 。
Eigen::Matrix< Number, Eigen::Dynamic, 1 > EigenSV
virtual numeric_index_type size() const override
获取向量的大小。
virtual numeric_index_type local_size() const override
获取向量的本地大小,即 index_stop - index_start。
EigenSparseVector< T > & operator=(const EigenSparseVector< T > &v)
Copy assignment operator.
virtual void init(const numeric_index_type N, const numeric_index_type n_local, const bool fast=false, const ParallelType ptype=AUTOMATIC) override
更改向量的维度为 n 。如果可能的话 ,该向量的保留内存保持不变。 如果 n==0 ,所有内存都将被释放。因此,如果要调整向量的大小并释放不需要的内存, 必须首先调用 init(0) ,然后调用 ini...
virtual NumericVector< T > & operator*=(const NumericVector< T > &v_in) override
计算此向量条目与另一个向量的条目之间的分量乘法, 。
virtual void add_vector_transpose(const NumericVector< T > &v, const SparseMatrix< T > &A) override
计算 , 即将矩阵 A 的转置与 NumericVector v 的乘积添加到 this。
virtual void abs() override
设置 ,对向量中的每个条目进行绝对值操作。
DataType & vec()
References to the underlying Eigen data types.
virtual void localize_to_one(std::vector< T > &v_local, const processor_id_type proc_id=0) const override
在处理器 proc_id 上创建全局向量的本地副本。 默认情况下,数据发送到处理器 0。此方法对于从一个处理器输出数据非常有用。
ParallelType _type
向量的类型。
virtual void set(const numeric_index_type i, const T value) override
设置 v(i) = value 。 请注意,此方法的库实现是线程安全的, 例如,将在写入向量之前锁定 _numeric_vector_mutex 。
virtual void scale(const T factor) override
缩放向量的每个元素。
virtual numeric_index_type first_local_index() const override
获取实际存储在该处理器上的第一个向量元素的索引。
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual Real l1_norm() const override
获取向量的 -范数,即条目的绝对值之和。
virtual void add(const numeric_index_type i, const T value) override
将 value 添加到由 i 指定的向量条目。 请注意,此方法的库实现是线程安全的, 例如,将在写入向量之前锁定 _numeric_vector_mutex 。
virtual numeric_index_type local_size() const =0
获取向量的本地大小,即 index_stop - index_start。
virtual void zero() override
将所有条目设置为零。等同于 v = 0,但更明显且更快。
ParallelType type() const
获取向量的类型。
bool initialized()
Checks that library initialization has been done.
Definition: libmesh.C:261
EigenSparseVector(const Parallel::Communicator &comm_in, const ParallelType=AUTOMATIC)
Dummy-Constructor.
virtual T operator()(const numeric_index_type i) const override
获取向量的第 i 个条目的副本。
virtual ~EigenSparseVector()=default
virtual T dot(const NumericVector< T > &v) const override
计算 ,即 (*this) 与向量 v 的点积。
virtual NumericVector< T > & operator+=(const NumericVector< T > &v) override
将向量加上 v , 。等价于 u.add(1, v)。
This class provides a nice interface to the Eigen C++-based data structures for serial vectors...