SPHinXsys  alpha version
mesh_with_data_packages.h
Go to the documentation of this file.
1 /* -----------------------------------------------------------------------------*
2  * SPHinXsys *
3  * -----------------------------------------------------------------------------*
4  * SPHinXsys (pronunciation: s'finksis) is an acronym from Smoothed Particle *
5  * Hydrodynamics for industrial compleX systems. It provides C++ APIs for *
6  * physical accurate simulation and aims to model coupled industrial dynamic *
7  * systems including fluid, solid, multi-body dynamics and beyond with SPH *
8  * (smoothed particle hydrodynamics), a meshless computational method using *
9  * particle discretization. *
10  * *
11  * SPHinXsys is partially funded by German Research Foundation *
12  * (Deutsche Forschungsgemeinschaft) DFG HU1527/6-1, HU1527/10-1, *
13  * HU1527/12-1 and HU1527/12-4. *
14  * *
15  * Portions copyright (c) 2017-2022 Technical University of Munich and *
16  * the authors' affiliations. *
17  * *
18  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
19  * not use this file except in compliance with the License. You may obtain a *
20  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
21  * *
22  * -----------------------------------------------------------------------------*/
30 #ifndef MESH_WITH_DATA_PACKAGES_H
31 #define MESH_WITH_DATA_PACKAGES_H
32 
33 #include "base_mesh.h"
34 #include "my_memory_pool.h"
35 
36 #include <fstream>
37 #include <algorithm>
38 #include <mutex>
39 #include <functional>
40 using namespace std::placeholders;
41 
42 namespace SPH
43 {
44  class Kernel;
45 
47  template <class ReturnType, class DataPackageType>
48  using PackageFunctor = std::function<ReturnType(DataPackageType *, Real)>;
50  template <class DataPackageType>
52  PackageFunctor<void, DataPackageType> &pkg_functor, Real dt = 0.0)
53  {
54  for (size_t i = 0; i != data_pkgs.size(); ++i)
55  pkg_functor(data_pkgs[i], dt);
56  };
58  template <class DataPackageType>
60  PackageFunctor<void, DataPackageType> &pkg_functor, Real dt = 0.0)
61  {
62  parallel_for(
63  blocked_range<size_t>(0, data_pkgs.size()),
64  [&](const blocked_range<size_t> &r)
65  {
66  for (size_t i = r.begin(); i != r.end(); ++i)
67  {
68  pkg_functor(data_pkgs[i], dt);
69  }
70  },
71  ap);
72  };
74  template <class ReturnType, typename ReduceOperation, class DataPackageType>
75  ReturnType ReducePackageIterator(ConcurrentVector<DataPackageType *> &data_pkgs, ReturnType temp,
77  ReduceOperation &reduce_operation, Real dt = 0.0)
78  {
79  for (size_t i = 0; i < data_pkgs.size(); ++i)
80  {
81  temp = reduce_operation(temp, reduce_pkg_functor(data_pkgs[i], dt));
82  }
83  return temp;
84  };
86  template <class ReturnType, typename ReduceOperation, class DataPackageType>
89  ReduceOperation &reduce_operation, Real dt = 0.0)
90  {
91  return parallel_reduce(
92  blocked_range<size_t>(0, data_pkgs.size()),
93  temp, [&](const blocked_range<size_t> &r, ReturnType temp0) -> ReturnType
94  {
95  for (size_t i = r.begin(); i != r.end(); ++i)
96  {
97  temp0 = reduce_operation(temp, reduce_pkg_functor(data_pkgs[i], dt));
98  }
99  return temp0; },
100  [&](ReturnType x, ReturnType y) -> ReturnType
101  {
102  return reduce_operation(x, y);
103  });
104  };
105 
115  {
116  public:
120  BaseDataPackage() : pkg_index_(0), is_inner_pkg_(false){};
121  virtual ~BaseDataPackage(){};
122  };
123 
132  template <int PKG_SIZE, int ADDRS_SIZE>
133  class GridDataPackage : public BaseDataPackage, public BaseMesh
134  {
135  public:
136  template <typename DataType>
137  using PackageData = PackageDataMatrix<DataType, PKG_SIZE>;
138  template <typename DataType>
139  using PackageDataAddress = PackageDataMatrix<DataType *, ADDRS_SIZE>;
140  GeneralDataAssemble<PackageData> all_pkg_data_;
141  GeneralDataAssemble<PackageDataAddress> all_pkg_data_addrs_;
142 
144  template <typename DataType>
145  using PackageTemporaryData = PackageDataMatrix<DataType, ADDRS_SIZE>;
146 
147  GridDataPackage() : BaseDataPackage(), BaseMesh(Vecu(ADDRS_SIZE)){};
148  virtual ~GridDataPackage(){};
149 
150  constexpr int PackageSize() { return PKG_SIZE; };
151  constexpr int AddressSize() { return ADDRS_SIZE; };
152  constexpr int AddressBufferWidth() { return (ADDRS_SIZE - PKG_SIZE) / 2; };
153  constexpr int OperationUpperBound() { return PKG_SIZE + AddressBufferWidth(); };
155  Vecd DataLowerBound() { return mesh_lower_bound_ + Vecd(grid_spacing_) * (Real)AddressBufferWidth(); };
157  void initializePackageGeometry(const Vecd &pkg_lower_bound, Real data_spacing)
158  {
159  mesh_lower_bound_ = pkg_lower_bound - Vecd(data_spacing) * ((Real)AddressBufferWidth() - 0.5);
160  grid_spacing_ = data_spacing;
161  };
163  template <typename DataType>
164  DataType probeDataPackage(PackageDataAddress<DataType> &pkg_data_addrs, const Vecd &position);
166  template <typename InDataType, typename OutDataType>
167  void computeGradient(PackageDataAddress<InDataType> &in_pkg_data_addrs,
168  PackageDataAddress<OutDataType> out_pkg_data_addrs, Real dt = 0.0);
170  template <typename InDataType, typename OutDataType>
171  void computeNormalizedGradient(PackageDataAddress<InDataType> &in_pkg_data_addrs,
172  PackageDataAddress<OutDataType> out_pkg_data_addrs, Real dt = 0.0);
173 
174  protected:
176  template <typename DataType>
177  void registerPackageData(PackageData<DataType> &pkg_data,
178  PackageDataAddress<DataType> &pkg_data_addrs)
179  {
180  constexpr int type_index = DataTypeIndex<DataType>::value;
181  std::get<type_index>(all_pkg_data_).push_back(&pkg_data);
182  std::get<type_index>(all_pkg_data_addrs_).push_back(&pkg_data_addrs);
183  };
185  template <typename DataType>
187  {
188  void operator()(GeneralDataAssemble<PackageData> &all_pkg_data,
189  GeneralDataAssemble<PackageDataAddress> &all_pkg_data_addrs);
190  };
191  DataAssembleOperation<initializePackageDataAddress> initialize_pkg_data_addrs_;
193  template <typename DataType>
195  {
196  void operator()(GeneralDataAssemble<PackageDataAddress> &all_pkg_data_addrs,
197  const Vecu &addrs_index,
198  GeneralDataAssemble<PackageData> &all_pkg_data,
199  const Vecu &data_index);
200  };
202 
204  template <typename DataType>
205  DataType CornerAverage(PackageDataAddress<DataType> &pkg_data_addrs, Veci addrs_index, Veci corner_direction);
206 
207  public:
208  void initializeSingularDataAddress()
209  {
210  initialize_pkg_data_addrs_(all_pkg_data_, all_pkg_data_addrs_);
211  };
212 
213  void assignAllPackageDataAddress(const Vecu &addrs_index, GridDataPackage *src_pkg, const Vecu &data_index)
214  {
215  assign_pkg_data_addrs_(all_pkg_data_addrs_, addrs_index, src_pkg->all_pkg_data_, data_index);
216  };
217  };
218 
235  template <class MeshFieldType, class GridDataPackageType>
237  {
238  public:
240  MeshDataMatrix<GridDataPackageType *> data_pkg_addrs_;
243  template <typename... Args>
244  explicit MeshWithGridDataPackages(BoundingBox tentative_bounds, Real data_spacing, size_t buffer_size, Args &&...args)
245  : MeshFieldType(std::forward<Args>(args)...),
246  Mesh(tentative_bounds, GridDataPackageType().PackageSize() * data_spacing, buffer_size),
247  data_spacing_(data_spacing),
248  global_mesh_(this->mesh_lower_bound_ + Vecd(data_spacing) * 0.5, data_spacing, this->number_of_cells_ * pkg_size_)
249  {
250  allocateMeshDataMatrix();
251  };
252  virtual ~MeshWithGridDataPackages() { deleteMeshDataMatrix(); };
253 
254  void allocateMeshDataMatrix();
255  void deleteMeshDataMatrix();
258  template <class DataType, typename PackageDataAddressType, PackageDataAddressType GridDataPackageType::*MemPtr>
259  DataType probeMesh(const Vecd &position);
260  virtual Real DataSpacing() override { return data_spacing_; };
261 
262  protected:
263  Real data_spacing_;
264  const int pkg_size_ = GridDataPackageType().PackageSize();
265  const int pkg_addrs_buffer_ = GridDataPackageType().AddressBufferWidth();
266  const int pkg_operations_ = pkg_size_ + pkg_addrs_buffer_;
267  const int pkg_addrs_size_ = pkg_size_ + 2 * pkg_addrs_buffer_;
268  std::mutex mutex_my_pool;
272  StdVec<GridDataPackageType *> singular_data_pkgs_addrs_;
273 
274  template <typename... ConstructorArgs>
275  void initializeASingularDataPackage(ConstructorArgs &&...args)
276  {
277  GridDataPackageType *new_data_pkg = data_pkg_pool_.malloc();
278  new_data_pkg->registerAllVariables();
279  new_data_pkg->initializeSingularData(std::forward<ConstructorArgs>(args)...);
280  new_data_pkg->initializeSingularDataAddress();
281  singular_data_pkgs_addrs_.push_back(new_data_pkg);
282  };
283 
284  void assignDataPackageAddress(const Vecu &cell_index, GridDataPackageType *data_pkg);
285  GridDataPackageType *DataPackageFromCellIndex(const Vecu &cell_index);
287  template <typename DataType, typename PackageDataType, PackageDataType GridDataPackageType::*MemPtr>
288  DataType DataValueFromGlobalIndex(const Vecu &global_grid_index);
289  void initializePackageAddressesInACell(const Vecu &cell_index);
291  std::pair<int, int> CellShiftAndDataIndex(int data_addrs_index_component)
292  {
293  std::pair<int, int> shift_and_index;
294  int signed_date_index = data_addrs_index_component - pkg_addrs_buffer_;
295  shift_and_index.first = (signed_date_index + pkg_size_) / pkg_size_ - 1;
296  shift_and_index.second = signed_date_index - shift_and_index.first * pkg_size_;
297  return shift_and_index;
298  }
299  };
300 }
301 #endif // MESH_WITH_DATA_PACKAGES_H
void initializePackageGeometry(const Vecd &pkg_lower_bound, Real data_spacing)
Definition: mesh_with_data_packages.h:157
This is the base classes of mesh, which is used to describe ordered and indexed data sets...
std::function< ReturnType(DataPackageType *, Real)> PackageFunctor
Definition: mesh_with_data_packages.h:48
Definition: base_data_type.h:43
std::pair< int, int > CellShiftAndDataIndex(int data_addrs_index_component)
Definition: mesh_with_data_packages.h:291
void PackageIterator_parallel(ConcurrentVector< DataPackageType *> &data_pkgs, PackageFunctor< void, DataPackageType > &pkg_functor, Real dt=0.0)
Definition: mesh_with_data_packages.h:59
Definition: base_data_package.h:46
Abstract base class for cell-based mesh by introducing number of cells, buffer width and mesh-based d...
Definition: base_mesh.h:106
Vecd DataLowerBound()
Definition: mesh_with_data_packages.h:155
Base class for all structured meshes which may be grid or cell based. The basic properties of the mes...
Definition: base_mesh.h:63
Abstract base class for a data package, by which the data in a derived class can be on- or off-grid...
Definition: mesh_with_data_packages.h:114
StdVec< GridDataPackageType * > singular_data_pkgs_addrs_
Definition: mesh_with_data_packages.h:272
PackageDataMatrix< DataType, ADDRS_SIZE > PackageTemporaryData
Definition: mesh_with_data_packages.h:145
ReturnType ReducePackageIterator_parallel(ConcurrentVector< DataPackageType *> &data_pkgs, ReturnType temp, PackageFunctor< ReturnType, DataPackageType > &reduce_pkg_functor, ReduceOperation &reduce_operation, Real dt=0.0)
Definition: mesh_with_data_packages.h:87
ConcurrentVector< GridDataPackageType * > inner_data_pkgs_
Definition: mesh_with_data_packages.h:241
LargeVec< DataType > ConcurrentVector
Definition: sph_data_containers.h:50
void PackageIterator(ConcurrentVector< DataPackageType *> &data_pkgs, PackageFunctor< void, DataPackageType > &pkg_functor, Real dt=0.0)
Definition: mesh_with_data_packages.h:51
BaseMesh global_mesh_
Definition: mesh_with_data_packages.h:269
Abstract class for mesh with data packages.
Definition: mesh_with_data_packages.h:236
Definition: mesh_with_data_packages.h:186
MeshDataMatrix< GridDataPackageType * > data_pkg_addrs_
Definition: mesh_with_data_packages.h:240
void registerPackageData(PackageData< DataType > &pkg_data, PackageDataAddress< DataType > &pkg_data_addrs)
Definition: mesh_with_data_packages.h:177
std::mutex mutex_my_pool
Definition: mesh_with_data_packages.h:268
Definition: mesh_with_data_packages.h:194
Vecu pkg_index_
Definition: mesh_with_data_packages.h:117
ReturnType ReducePackageIterator(ConcurrentVector< DataPackageType *> &data_pkgs, ReturnType temp, PackageFunctor< ReturnType, DataPackageType > &reduce_pkg_functor, ReduceOperation &reduce_operation, Real dt=0.0)
Definition: mesh_with_data_packages.h:75
MyMemoryPool< GridDataPackageType > data_pkg_pool_
Definition: mesh_with_data_packages.h:239
Definition: base_data_type.h:320
Abstract base class for a data package whose data are defined on the grids of a small mesh patch...
Definition: mesh_with_data_packages.h:133
bool is_inner_pkg_
Definition: mesh_with_data_packages.h:118
Definition: solid_body_supplementary.cpp:9