libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
composite_fem_function.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 #ifndef LIBMESH_COMPOSITE_FEM_FUNCTION_H
19 #define LIBMESH_COMPOSITE_FEM_FUNCTION_H
20 
21 // libMesh includes
22 #include "libmesh/dense_vector.h"
23 #include "libmesh/fem_function_base.h"
24 #include "libmesh/int_range.h"
25 #include "libmesh/libmesh.h"
26 #include "libmesh/point.h"
27 
28 // C++ includes
29 #include <algorithm>
30 #include <utility>
31 #include <vector>
32 
33 namespace libMesh
34 {
35 
43 template <typename Output=Number>
44 class CompositeFEMFunction : public FEMFunctionBase<Output>
45 {
46 public:
50  explicit
51  CompositeFEMFunction() = default;
52 
58 
64 
68  virtual ~CompositeFEMFunction() = default;
69 
78  std::vector<unsigned int> index_map)
79  {
80  const unsigned int subfunction_index =
81  cast_int<unsigned int>(subfunctions.size());
82  libmesh_assert_equal_to(subfunctions.size(), index_maps.size());
83 
84  subfunctions.push_back(f.clone());
85 
86  unsigned int max_index =
87  *std::max_element(index_map.begin(), index_map.end());
88 
89  if (max_index >= reverse_index_map.size())
90  reverse_index_map.resize
91  (max_index+1, std::make_pair(libMesh::invalid_uint,
93 
94  for (auto j : index_range(index_map))
95  {
96  libmesh_assert_less(index_map[j], reverse_index_map.size());
97  libmesh_assert_equal_to(reverse_index_map[index_map[j]].first,
99  libmesh_assert_equal_to(reverse_index_map[index_map[j]].second,
101  reverse_index_map[index_map[j]] =
102  std::make_pair(subfunction_index, j);
103  }
104 
105  index_maps.push_back(std::move(index_map));
106  }
107 
115  virtual Output operator()(const FEMContext & c,
116  const Point & p,
117  const Real time = 0) override
118  {
119  return this->component(c, 0, p, time);
120  }
121 
129  virtual void operator()(const FEMContext & c,
130  const Point & p,
131  const Real time,
132  DenseVector<Output> & output) override
133  {
134  libmesh_assert_greater_equal (output.size(),
135  reverse_index_map.size());
136 
137  // 必要时,清零未被任何子函数覆盖的输出分量
138  output.zero();
139 
140  DenseVector<Output> temp;
141  for (auto i : index_range(subfunctions))
142  {
143  temp.resize(cast_int<unsigned int>(index_maps[i].size()));
144  (*subfunctions[i])(c, p, time, temp);
145  for (auto j : index_range(temp))
146  output(index_maps[i][j]) = temp(j);
147  }
148  }
149 
158  virtual Output component(const FEMContext & c,
159  unsigned int i,
160  const Point & p,
161  Real time) override
162  {
163  if (i >= reverse_index_map.size() ||
165  return 0;
166 
167  libmesh_assert_less(reverse_index_map[i].first,
168  subfunctions.size());
169  libmesh_assert_not_equal_to(reverse_index_map[i].second,
171  return subfunctions[reverse_index_map[i].first]->
172  component(c, reverse_index_map[i].second, p, time);
173  }
174 
179  virtual std::unique_ptr<FEMFunctionBase<Output>> clone() const override
180  {
181  CompositeFEMFunction * returnval = new CompositeFEMFunction();
182  for (auto i : index_range(subfunctions))
183  returnval->attach_subfunction(*subfunctions[i], index_maps[i]);
184  return std::unique_ptr<FEMFunctionBase<Output>> (returnval);
185  }
186 
191  unsigned int n_subfunctions() const
192  {
193  return subfunctions.size();
194  }
195 
200  unsigned int n_components() const
201  {
202  return reverse_index_map.size();
203  }
204 
205 private:
206  // 填充值的函数列表
207  std::vector<std::unique_ptr<FEMFunctionBase<Output>>> subfunctions;
208 
214  std::vector<std::vector<unsigned int>> index_maps;
215 
220  std::vector<std::pair<unsigned int, unsigned int>> reverse_index_map;
221 };
222 
223 } // namespace libMesh
224 
225 #endif // LIBMESH_COMPOSITE_FEM_FUNCTION_H
unsigned int n_subfunctions() const
获取子函数的数量。
void attach_subfunction(const FEMFunctionBase< Output > &f, std::vector< unsigned int > index_map)
附加新的子函数和索引映射到复合函数。 子函数被克隆并添加到subfunctions中,同时更新index_maps和reverse_index_map。 (*this)(index_map[i]) 将返...
const unsigned int invalid_uint
A number which is used quite often to represent an invalid or uninitialized value for an unsigned int...
Definition: libmesh.h:254
std::vector< std::pair< unsigned int, unsigned int > > reverse_index_map
对于每个全局指数,存储该指数是哪个子函数的本地指数。 每个元素是一个std::pair&lt;unsigned int, unsigned int&gt;,表示子函数的索引和子函数中的本地指数。 ...
void resize(const unsigned int n)
调整向量的大小。将所有元素设置为0。
Definition: dense_vector.h:404
virtual std::unique_ptr< FEMFunctionBase< Output > > clone() const override
克隆函数。创建并返回一个与当前实例相同类型的新实例。
virtual void operator()(const FEMContext &c, const Point &p, const Real time, DenseVector< Output > &output) override
重载操作符,用于计算矢量值。
virtual Output component(const FEMContext &c, unsigned int i, const Point &p, Real time) override
重载操作符,用于计算特定分量的值。
CompositeFEMFunction & operator=(CompositeFEMFunction &&)=default
std::vector< std::vector< unsigned int > > index_maps
对于每个子函数,存储从子函数的索引到全局函数索引的映射的列表。
FEMFunction函数的另一个函数。
std::vector< std::unique_ptr< FEMFunctionBase< Output > > > subfunctions
virtual Output operator()(const FEMContext &c, const Point &p, const Real time=0) override
重载操作符,用于计算标量值。
virtual void zero() overridefinal
将向量中的每个元素设置为0。由于派生类中的存储方法可能不同,需要将其声明为纯虚函数。
Definition: dense_vector.h:428
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
CompositeFEMFunction()=default
默认构造函数。
unsigned int n_components() const
获取组件的数量。
virtual ~CompositeFEMFunction()=default
子函数向量会自动清理。
virtual std::unique_ptr< FEMFunctionBase< Output > > clone() const =0
创建并返回函数的新副本。
FEMFunctionBase是一个基类,用户可以从中派生出“函数样式”的对象,以在FEMSystem中使用。
virtual unsigned int size() const overridefinal
Definition: dense_vector.h:111