libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
dense_matrix_base_impl.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 #ifndef LIBMESH_DENSE_MATRIX_BASE_IMPL_H
21 #define LIBMESH_DENSE_MATRIX_BASE_IMPL_H
22 
23 // Local Includes
24 #include "libmesh/dense_matrix.h"
25 #include "libmesh/dense_vector_base.h"
26 #include "libmesh/dense_vector.h"
27 #include "libmesh/int_range.h"
28 
29 // C++ includes
30 #include <iomanip> // for std::setw()
31 
32 namespace libMesh
33 {
34 
42 template <typename T>
44 {
45  DenseVector<T> ret(_m);
46  for (decltype(_m) i = 0; i < _m; ++i)
47  ret(i) = el(i, i);
48  return ret;
49 }
50 
60 template<typename T>
62  const DenseMatrixBase<T> & M2,
63  const DenseMatrixBase<T> & M3)
64 {
65  // 断言确保我们传递了正确维度的矩阵
66  libmesh_assert_equal_to(M1.m(), M2.m());
67  libmesh_assert_equal_to(M1.n(), M3.n());
68  libmesh_assert_equal_to(M2.n(), M3.m());
69 
70  const unsigned int m_s = M2.m();
71  const unsigned int p_s = M2.n();
72  const unsigned int n_s = M1.n();
73 
74  // 通过这种方式执行是因为有很大的机会(至少对于约束矩阵来说)
75  // 在右乘时 M3(k,j) = 0.
76  for (unsigned int k = 0; k < p_s; k++)
77  for (unsigned int j = 0; j < n_s; j++)
78  if (M3.el(k, j) != 0.)
79  for (unsigned int i = 0; i < m_s; i++)
80  M1.el(i, j) += M2.el(i, k) * M3.el(k, j);
81 }
82 
93 template<typename T>
94 void DenseMatrixBase<T>::condense(const unsigned int iv,
95  const unsigned int jv,
96  const T val,
97  DenseVectorBase<T> & rhs)
98 {
99  libmesh_assert_equal_to(this->_m, rhs.size());
100  libmesh_assert_equal_to(iv, jv);
101 
102  // 将已知值移入 RHS,并将列置零
103  for (auto i : make_range(this->m()))
104  {
105  rhs.el(i) -= this->el(i, jv) * val;
106  this->el(i, jv) = 0.;
107  }
108 
109  // 将行置零
110  for (auto j : make_range(this->n()))
111  this->el(iv, j) = 0.;
112 
113  this->el(iv, jv) = 1.;
114  rhs.el(iv) = val;
115 }
116 
125 template<typename T>
126 void DenseMatrixBase<T>::print_scientific(std::ostream & os, unsigned precision) const
127 {
128  // 保存初始格式标志
129  std::ios_base::fmtflags os_flags = os.flags();
130 
131  // 打印矩阵元素
132  for (auto i : make_range(this->m()))
133  {
134  for (auto j : make_range(this->n()))
135  os << std::setw(15)
136  << std::scientific
137  << std::setprecision(precision)
138  << this->el(i, j) << " ";
139 
140  os << std::endl;
141  }
142 
143  // 恢复原始格式标志
144  os.flags(os_flags);
145 }
146 
154 template<typename T>
155 void DenseMatrixBase<T>::print(std::ostream & os) const
156 {
157  for (auto i : make_range(this->m()))
158  {
159  for (auto j : make_range(this->n()))
160  os << std::setw(8)
161  << this->el(i, j) << " ";
162 
163  os << std::endl;
164  }
165 
166  return;
167 }
168 
169 
170 
171 }
172 
173 #endif // LIBMESH_DENSE_MATRIX_BASE_IMPL_H
void print_scientific(std::ostream &os, unsigned precision=8) const
以科学计数法格式打印矩阵。
unsigned int n() const
返回矩阵的列维度。
virtual T el(const unsigned int i, const unsigned int j) const =0
返回矩阵的 (i,j) 元素。 由于内部数据表示可能不同,必须重新定义此函数。
unsigned int m() const
返回矩阵的行维度。
virtual unsigned int size() const =0
DenseVector< T > diagonal() const
返回矩阵的对角线。
定义用于有限元计算的抽象稠密向量基类。 可以从这个类派生出特定的稠密向量,例如 DenseSubVectors。
Definition: dof_map.h:63
static void multiply(DenseMatrixBase< T > &M1, const DenseMatrixBase< T > &M2, const DenseMatrixBase< T > &M3)
辅助函数 - 执行计算 M1 = M2 * M3 其中: M1 = (m x n) M2 = (m x p) M3 = (p x n)
定义用于有限元计算的稠密向量类。该类基本上是为了补充 DenseMatrix 类而设计的。 它相对于 std::vector 具有额外的功能,使其在有限元中特别有用,特别是对于方程组。 所有重写的虚拟函...
Definition: dof_map.h:64
为有限元类型的计算定义了一个抽象的稠密矩阵基类。例如 DenseSubMatrices 可以从这个类派生出来。
void print(std::ostream &os=libMesh::out) const
漂亮地打印矩阵,默认为 libMesh::out。
virtual T el(const unsigned int i) const =0
void condense(const unsigned int i, const unsigned int j, const T val, DenseVectorBase< T > &rhs)
将矩阵的 (i,j) 条目压缩出来,强制它取值为 val。这对于在数值模拟中应用边界条件很有用。 保留矩阵的对称性。