libmesh解析
本工作只是尝试解析原libmesh的代码,供学习使用
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 
composite_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_FUNCTION_H
19 #define LIBMESH_COMPOSITE_FUNCTION_H
20 
21 // libMesh includes
22 #include "libmesh/dense_vector.h"
23 #include "libmesh/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 CompositeFunction : public FunctionBase<Output>
45 {
46 public:
50  explicit
51  CompositeFunction() = default;
52 
58 
62  CompositeFunction(const CompositeFunction &) = delete;
64 
68  virtual ~CompositeFunction() = default;
69 
80  std::vector<unsigned int> index_map)
81  {
82  const unsigned int subfunction_index =
83  cast_int<unsigned int>(subfunctions.size());
84  libmesh_assert_equal_to(subfunctions.size(), index_maps.size());
85 
86  subfunctions.push_back(f.clone());
87 
88  unsigned int max_index =
89  *std::max_element(index_map.begin(), index_map.end());
90 
91  if (max_index >= reverse_index_map.size())
92  reverse_index_map.resize
93  (max_index+1, std::make_pair(libMesh::invalid_uint,
95 
96  for (auto j : index_range(index_map))
97  {
98  libmesh_assert_less(index_map[j], reverse_index_map.size());
99  libmesh_assert_equal_to(reverse_index_map[index_map[j]].first,
101  libmesh_assert_equal_to(reverse_index_map[index_map[j]].second,
103  reverse_index_map[index_map[j]] =
104  std::make_pair(subfunction_index, j);
105  }
106 
107  // 检查是否有时间依赖性
108  // 我们只检查刚刚添加的函数,而不是重新研究所有子函数
109  // 如果这是第一个子函数,则确定时间依赖性。
110  if (subfunctions.size() == 1)
112 
113  // 否则,我们已经有了多于1个函数。
114  // 如果 _is_time_dependent 为 true,则之前的某个子函数是时间依赖的,因此此 CompositeFunction 是时间依赖的。
115  // 如果 _is_time_dependent 为 false,则刚刚添加的子函数确定时间依赖性。
116  else if (!this->_is_time_dependent)
118 
119  index_maps.push_back(std::move(index_map));
120  }
121 
128  virtual Output operator()(const Point & p,
129  const Real time = 0) override
130  {
131  return this->component(0,p,time);
132  }
133 
140  virtual void operator()(const Point & p,
141  const Real time,
142  DenseVector<Output> & output) override
143  {
144  libmesh_assert_greater_equal (output.size(),
145  reverse_index_map.size());
146 
147  // 必要时,清零未被任何子函数覆盖的输出分量
148  output.zero();
149 
150  DenseVector<Output> temp;
151  for (auto i : index_range(subfunctions))
152  {
153  temp.resize(cast_int<unsigned int>(index_maps[i].size()));
154  (*subfunctions[i])(p, time, temp);
155  for (auto j : index_range(temp))
156  output(index_maps[i][j]) = temp(j);
157  }
158  }
159 
167  virtual Output component(unsigned int i,
168  const Point & p,
169  Real time) override
170  {
171  if (i >= reverse_index_map.size() ||
173  return 0;
174 
175  libmesh_assert_less(reverse_index_map[i].first,
176  subfunctions.size());
177  libmesh_assert_not_equal_to(reverse_index_map[i].second,
179  return subfunctions[reverse_index_map[i].first]->
180  component(reverse_index_map[i].second,p,time);
181  }
182 
187  virtual std::unique_ptr<FunctionBase<Output>> clone() const override
188  {
189  CompositeFunction * returnval = new CompositeFunction();
190  for (auto i : index_range(subfunctions))
191  returnval->attach_subfunction(*subfunctions[i], index_maps[i]);
192  return std::unique_ptr<FunctionBase<Output>> (returnval);
193  }
194 
199  unsigned int n_subfunctions() const
200  {
201  return subfunctions.size();
202  }
203 
208  unsigned int n_components() const
209  {
210  return reverse_index_map.size();
211  }
212 
213 private:
214  // 填充值的函数列表
215  std::vector<std::unique_ptr<FunctionBase<Output>>> subfunctions;
216 
222  std::vector<std::vector<unsigned int>> index_maps;
223 
224 
229  std::vector<std::pair<unsigned int, unsigned int>> reverse_index_map;
230 };
231 
232 
233 } // namespace libMesh
234 
235 #endif // LIBMESH_COMPOSITE_FUNCTION_H
std::vector< std::unique_ptr< FunctionBase< Output > > > subfunctions
unsigned int n_subfunctions() const
获取子函数的数量。
CompositeFunction & operator=(CompositeFunction &&)=default
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
virtual void operator()(const Point &p, const Real time, DenseVector< Output > &output) override
重载操作符,用于计算矢量值。
void resize(const unsigned int n)
调整向量的大小。将所有元素设置为0。
Definition: dense_vector.h:404
unsigned int n_components() const
获取组件的数量。
bool is_time_dependent() const
virtual std::unique_ptr< FunctionBase< Output > > clone() const override
克隆函数。创建并返回一个与当前实例相同类型的新实例。
virtual Output operator()(const Point &p, const Real time=0) override
重载操作符,用于计算标量值。
将不同函数的结果组合成单个向量的函数。所有重写的虚拟函数在function_base.h中都有文档。
bool _is_time_dependent
成员变量用于缓存函数是否真正依赖于时间。
virtual void zero() overridefinal
将向量中的每个元素设置为0。由于派生类中的存储方法可能不同,需要将其声明为纯虚函数。
Definition: dense_vector.h:428
CompositeFunction()=default
默认构造函数。
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
virtual ~CompositeFunction()=default
子函数向量会自动清理。
std::vector< std::pair< unsigned int, unsigned int > > reverse_index_map
对于每个全局指数,存储该指数是哪个子函数的本地指数。 每个元素是一个std::pair&lt;unsigned int, unsigned int&gt;,表示子函数的索引和子函数中的本地指数。 ...
std::vector< std::vector< unsigned int > > index_maps
对于每个子函数,存储从子函数的索引到全局函数索引的映射的列表。
virtual std::unique_ptr< FunctionBase< Output > > clone() const =0
virtual Output component(unsigned int i, const Point &p, Real time) override
重载操作符,用于计算特定分量的值。
FunctionBase是一个函数对象的基类,可以在某一点(可选地包括时间)进行评估。
virtual unsigned int size() const overridefinal
Definition: dense_vector.h:111
void attach_subfunction(const FunctionBase< Output > &f, std::vector< unsigned int > index_map)
附加新的子函数,以及从附加的子函数的索引到组合函数的索引的映射。