libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
laspack_vector.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 
19 
20 // C++ includes
21 #include <algorithm> // for std::min
22 #include <limits>
23 
24 // Local Includes
25 #include "libmesh/dense_subvector.h"
26 #include "libmesh/dense_vector.h"
27 #include "libmesh/laspack_vector.h"
28 #include "libmesh/laspack_matrix.h"
29 #include "libmesh/int_range.h"
30 
31 #ifdef LIBMESH_HAVE_LASPACK
32 
33 namespace libMesh
34 {
35 
36 template <typename T>
38 {
39  libmesh_assert (this->closed());
40 
41  T _sum = 0;
42 
43  const numeric_index_type n = this->size();
44 
45  for (numeric_index_type i=0; i!=n; ++i)
46  _sum += (*this)(i);
47 
48  return _sum;
49 }
50 
51 
52 
53 template <typename T>
55 {
56  libmesh_assert (this->closed());
57 
58  return static_cast<Real>(l1Norm_V(const_cast<QVector*>(&_vec)));
59 }
60 
61 
62 
63 template <typename T>
65 {
66  libmesh_assert (this->closed());
67 
68  return static_cast<Real>(l2Norm_V(const_cast<QVector*>(&_vec)));
69 }
70 
71 
72 
73 template <typename T>
75 {
76  libmesh_assert (this->closed());
77 
78  return static_cast<Real>(MaxNorm_V(const_cast<QVector*>(&_vec)));
79 }
80 
81 
82 
83 template <typename T>
85 {
86  libmesh_assert (this->closed());
87 
88  this->add(1., v);
89 
90  return *this;
91 }
92 
93 
94 
95 
96 template <typename T>
98 {
99  libmesh_assert (this->closed());
100 
101  this->add(-1., v);
102 
103  return *this;
104 }
105 
106 
107 
108 template <typename T>
110 {
111  libmesh_assert_equal_to(size(), v.size());
112 
113 #ifndef NDEBUG
114  const bool was_closed = this->_is_closed;
115 #endif
116 
117  const numeric_index_type n = this->size();
118 
119  for (numeric_index_type i=0; i<n; i++)
120  this->set(i, (*this)(i) * v(i));
121 
122  // This is an embarrassingly parallel method, but set() isn't in
123  // general so set() overzealously marked us as non-closed
124 #ifndef NDEBUG
125  this->_is_closed = was_closed;
126 #endif
127 
128  return *this;
129 }
130 
131 
132 
133 template <typename T>
135 {
136  libmesh_assert_equal_to(size(), v.size());
137 
138 #ifndef NDEBUG
139  const bool was_closed = this->_is_closed;
140 #endif
141 
142  const numeric_index_type n = this->size();
143 
144  for (numeric_index_type i=0; i<n; i++)
145  this->set(i, (*this)(i) / v(i));
146 
147  // This is an embarrassingly parallel method, but set() isn't in
148  // general so set() overzealously marked us as non-closed
149 #ifndef NDEBUG
150  this->_is_closed = was_closed;
151 #endif
152 
153  return *this;
154 }
155 
156 
157 
158 template <typename T>
160 {
161  const numeric_index_type n = this->size();
162 
163 #ifndef NDEBUG
164  const bool was_closed = this->_is_closed;
165 #endif
166 
167  for (numeric_index_type i=0; i<n; i++)
168  {
169  T v = (*this)(i);
170 
171  // Don't divide by zero!
172  libmesh_assert_not_equal_to (v, T(0));
173 
174  this->set(i, 1. / v);
175  }
176 
177  // This is an embarrassingly parallel method, but set() isn't in
178  // general so set() overzealously marked us as non-closed
179 #ifndef NDEBUG
180  this->_is_closed = was_closed;
181 #endif
182 }
183 
184 
185 
186 template <typename T>
188 {
189  const numeric_index_type n = this->size();
190 
191 #ifndef NDEBUG
192  const bool was_closed = this->_is_closed;
193 #endif
194 
195  for (numeric_index_type i=0; i<n; i++)
196  {
197  T v = (*this)(i);
198 
199  this->set(i, libmesh_conj(v) );
200  }
201 
202  // This is an embarrassingly parallel method, but set() isn't in
203  // general so set() overzealously marked us as non-closed
204 #ifndef NDEBUG
205  this->_is_closed = was_closed;
206 #endif
207 }
208 
209 
210 template <typename T>
211 void LaspackVector<T>::add (const T v)
212 {
213 #ifndef NDEBUG
214  const bool was_closed = this->_is_closed;
215 #endif
216 
217  const numeric_index_type n = this->size();
218 
219  for (numeric_index_type i=0; i<n; i++)
220  this->add (i, v);
221 
222  // This is an embarrassingly parallel method, but set() isn't in
223  // general so set() overzealously marked us as non-closed
224 #ifndef NDEBUG
225  this->_is_closed = was_closed;
226 #endif
227 }
228 
229 
230 
231 
232 template <typename T>
234 {
235  this->add (1., v);
236 }
237 
238 
239 
240 template <typename T>
241 void LaspackVector<T>::add (const T a, const NumericVector<T> & v_in)
242 {
243  // Make sure the vector passed in is really a LaspackVector
244  const LaspackVector * v = cast_ptr<const LaspackVector *>(&v_in);
245 
246 #ifndef NDEBUG
247  const bool was_closed = this->_is_closed;
248 #endif
249 
250  libmesh_assert(v);
251  libmesh_assert_equal_to (this->size(), v->size());
252 
253  for (auto i : make_range(v->size()))
254  this->add (i, a*(*v)(i));
255 
256 #ifndef NDEBUG
257  this->_is_closed = was_closed;
258 #endif
259 }
260 
261 
262 
263 template <typename T>
265  const SparseMatrix<T> & mat_in)
266 {
267  // Make sure the data passed in are really in Laspack types
268  const LaspackVector<T> * vec = cast_ptr<const LaspackVector<T> *>(&vec_in);
269  const LaspackMatrix<T> * mat = cast_ptr<const LaspackMatrix<T> *>(&mat_in);
270 
271  libmesh_assert(vec);
272  libmesh_assert(mat);
273 
274  // += mat*vec
275  AddAsgn_VV (&_vec, Mul_QV(const_cast<QMatrix*>(&mat->_QMat),
276  const_cast<QVector*>(&vec->_vec)));
277 }
278 
279 
280 template <typename T>
282  const SparseMatrix<T> &)
283 {
284  libmesh_not_implemented();
285 }
286 
287 
288 
289 template <typename T>
290 void LaspackVector<T>::scale (const T factor)
291 {
292  libmesh_assert (this->initialized());
293 
294  Asgn_VV(&_vec, Mul_SV (factor, &_vec));
295 }
296 
297 template <typename T>
299 {
300  libmesh_assert (this->initialized());
301 
302  const numeric_index_type n = this->size();
303 
304  for (numeric_index_type i=0; i!=n; ++i)
305  this->set(i,std::abs((*this)(i)));
306 }
307 
308 template <typename T>
310 {
311  libmesh_assert (this->initialized());
312 
313  // Make sure the NumericVector passed in is really a LaspackVector
314  const LaspackVector<T> * v = cast_ptr<const LaspackVector<T> *>(&v_in);
315  libmesh_assert(v);
316 
317  return Mul_VV (const_cast<QVector*>(&(this->_vec)),
318  const_cast<QVector*>(&(v->_vec)));
319 }
320 
321 
322 
323 template <typename T>
326 {
327  libmesh_assert (this->initialized());
328  libmesh_assert (this->closed());
329 
330  V_SetAllCmp (&_vec, s);
331 
332  return *this;
333 }
334 
335 
336 
337 template <typename T>
340 {
341  // Make sure the NumericVector passed in is really a LaspackVector
342  const LaspackVector<T> * v =
343  cast_ptr<const LaspackVector<T> *>(&v_in);
344 
345  libmesh_assert(v);
346 
347  *this = *v;
348 
349  return *this;
350 }
351 
352 
353 
354 template <typename T>
357 {
358  libmesh_assert (this->initialized());
359  libmesh_assert (v.closed());
360  libmesh_assert_equal_to (this->size(), v.size());
361 
362  if (v.size() != 0)
363  Asgn_VV (const_cast<QVector*>(&_vec),
364  const_cast<QVector*>(&v._vec)
365  );
366 
367 #ifndef NDEBUG
368  this->_is_closed = true;
369 #endif
370 
371  return *this;
372 }
373 
374 
375 
376 template <typename T>
378 LaspackVector<T>::operator = (const std::vector<T> & v)
379 {
384  if (this->size() == v.size())
385  for (auto i : index_range(v))
386  this->set (i, v[i]);
387 
388  else
389  libmesh_error_msg("this->size() = " << this->size() << " must be equal to v.size() = " << v.size());
390 
391  return *this;
392 }
393 
394 
395 template <typename T>
397 {
398  // Make sure the NumericVector passed in is really a LaspackVector
399  LaspackVector<T> * v_local =
400  cast_ptr<LaspackVector<T> *>(&v_local_in);
401 
402  libmesh_assert(v_local);
403 
404  *v_local = *this;
405 }
406 
407 
408 
409 template <typename T>
411  const std::vector<numeric_index_type> & libmesh_dbg_var(send_list)) const
412 {
413  // Make sure the NumericVector passed in is really a LaspackVector
414  LaspackVector<T> * v_local =
415  cast_ptr<LaspackVector<T> *>(&v_local_in);
416 
417  libmesh_assert(v_local);
418  libmesh_assert_less_equal (send_list.size(), v_local->size());
419 
420  *v_local = *this;
421 }
422 
423 
424 
425 template <typename T>
426 void LaspackVector<T>::localize (std::vector<T> & v_local,
427  const std::vector<numeric_index_type> & indices) const
428 {
429  // LaspackVectors are serial, so we can just copy values
430  v_local.resize(indices.size());
431 
432  for (auto i : index_range(v_local))
433  v_local[i] = (*this)(indices[i]);
434 }
435 
436 
437 
438 template <typename T>
439 void LaspackVector<T>::localize (const numeric_index_type libmesh_dbg_var(first_local_idx),
440  const numeric_index_type libmesh_dbg_var(last_local_idx),
441  const std::vector<numeric_index_type> & libmesh_dbg_var(send_list))
442 {
443  libmesh_assert_equal_to (first_local_idx, 0);
444  libmesh_assert_equal_to (last_local_idx+1, this->size());
445 
446  libmesh_assert_less_equal (send_list.size(), this->size());
447 
448 #ifndef NDEBUG
449  this->_is_closed = true;
450 #endif
451 }
452 
453 
454 
455 template <typename T>
456 void LaspackVector<T>::localize (std::vector<T> & v_local) const
457 
458 {
459  v_local.resize(this->size());
460 
461  for (auto i : index_range(v_local))
462  v_local[i] = (*this)(i);
463 }
464 
465 
466 
467 template <typename T>
468 void LaspackVector<T>::localize_to_one (std::vector<T> & v_local,
469  const processor_id_type libmesh_dbg_var(pid)) const
470 {
471  libmesh_assert_equal_to (pid, 0);
472 
473  this->localize (v_local);
474 }
475 
476 
477 
478 template <typename T>
480  const NumericVector<T> & /*vec2*/)
481 {
482  libmesh_not_implemented();
483 }
484 
485 template <typename T>
487  const NumericVector<T> & /*vec2*/)
488 {
489  libmesh_not_implemented();
490 }
491 
492 template <typename T>
494 {
495  libmesh_assert (this->initialized());
496  if (!this->size())
497  return -std::numeric_limits<Real>::max();
498 
499  Real the_max = libmesh_real((*this)(0));
500 
501  const numeric_index_type n = this->size();
502 
503  for (numeric_index_type i=1; i<n; i++)
504  the_max = std::max (the_max, libmesh_real((*this)(i)));
505 
506  return the_max;
507 }
508 
509 
510 
511 template <typename T>
513 {
514  libmesh_assert (this->initialized());
515  if (!this->size())
516  return std::numeric_limits<Real>::max();
517 
518  Real the_min = libmesh_real((*this)(0));
519 
520  const numeric_index_type n = this->size();
521 
522  for (numeric_index_type i=1; i<n; i++)
523  the_min = std::min (the_min, libmesh_real((*this)(i)));
524 
525  return the_min;
526 }
527 
528 
529 //------------------------------------------------------------------
530 // Explicit instantiations
531 template class LIBMESH_EXPORT LaspackVector<Number>;
532 
533 } // namespace libMesh
534 
535 
536 #endif // #ifdef LIBMESH_HAVE_LASPACK
T libmesh_real(T a)
virtual void add_vector(const NumericVector< T > &v, const SparseMatrix< T > &A) override
将一个向量添加到该向量并将结果存储在其中。
virtual bool closed() const
检查向量是否已经关闭并准备好进行计算。
bool closed()
Checks that the library has been closed.
Definition: libmesh.C:268
T libmesh_conj(T a)
virtual numeric_index_type size() const override
返回向量的大小(全局维度)。
virtual NumericVector< T > & operator-=(const NumericVector< T > &v) override
将另一个向量的元素按元素从该向量中减去。
virtual void abs() override
计算向量的每个元素的绝对值。
virtual void scale(const T factor) override
缩放向量的所有元素,将它们乘以指定的标量因子。
virtual void add_vector_transpose(const NumericVector< T > &v, const SparseMatrix< T > &A) override
将一个向量的转置添加到该向量并将结果存储在其中。
virtual T dot(const NumericVector< T > &v) const override
计算向量与另一个向量的点积。
virtual numeric_index_type size() const =0
获取向量的大小。
LaspackVector< T > & operator=(const LaspackVector< T > &v)
Copy assignment operator.
virtual void pointwise_mult(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
对向量的每个元素执行逐元素乘法。
提供了不同线性代数库的向量存储方案的统一接口。
Definition: dof_map.h:67
virtual NumericVector< T > & operator+=(const NumericVector< T > &v) override
将另一个向量的元素按元素加到该向量。
virtual NumericVector< T > & operator*=(const NumericVector< T > &v) override
将该向量的元素按元素与另一个向量的元素相乘。
uint8_t processor_id_type
Definition: id_types.h:104
ADRealEigenVector< T, D, asd > abs(const ADRealEigenVector< T, D, asd > &)
计算自动微分实数向量的绝对值。
Definition: type_vector.h:112
这是一个通用的稀疏矩阵类。该类包含了必须在派生类中覆盖的纯虚拟成员。 使用一个公共的基类允许从不同的求解器包中以不同的格式统一访问稀疏矩阵。
Definition: dof_map.h:66
virtual Real max() const override
返回向量的最大元素。
virtual Real l2_norm() const override
返回向量的 L2 范数。
dof_id_type numeric_index_type
Definition: id_types.h:99
virtual void add(const numeric_index_type i, const T value) override
将指定值添加到指定索引处的元素。
virtual Real min() const override
返回向量的最小元素。
virtual void localize(std::vector< T > &v_local) const override
将向量的元素本地化,以便在不同处理器之间交换。
virtual void localize_to_one(std::vector< T > &v_local, const processor_id_type proc_id=0) const override
将向量本地化到指定处理器。
这个类为基于laspackc的串行向量数据结构提供了一个很好的接口。 所有被覆盖的虚函数都记录在numeric_vector.h中。
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual void conjugate() override
计算向量中所有元素的复共轭。
virtual Real linfty_norm() const override
返回向量的 Linfinity 范数。
bool initialized()
Checks that library initialization has been done.
Definition: libmesh.C:261
virtual NumericVector< T > & operator/=(const NumericVector< T > &v) override
将该向量的元素按元素除以另一个向量的元素。
virtual T sum() const override
返回向量的所有元素之和。
QVector _vec
用于保存向量条目的实际 Laspack 向量数据类型。
LaspackMatrix类封装了Laspack库中的QMatrix对象。 目前,Laspack仅支持实数数据类型,因此这个类是对 SparseMatrix&lt;T&gt; 的全特化,其中 T = Real。 所...
virtual void reciprocal() override
计算向量中所有元素的倒数(1/x)。
virtual Real l1_norm() const override
返回向量的 L1 范数。
virtual void pointwise_divide(const NumericVector< T > &vec1, const NumericVector< T > &vec2) override
对向量的每个元素执行逐元素除法。