SPHinXsys  alpha version
base_mesh.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  * -----------------------------------------------------------------------------*/
34 #ifndef BASE_MESH_H
35 #define BASE_MESH_H
36 
37 #include "base_data_package.h"
38 #include "sph_data_containers.h"
39 #include "my_memory_pool.h"
40 
41 #include <fstream>
42 #include <algorithm>
43 #include <mutex>
44 #include <functional>
45 using namespace std::placeholders;
46 
47 namespace SPH
48 {
50  typedef std::function<void(const Vecu &, Real)> MeshFunctor;
52  void MeshIterator(const Vecu &index_begin, const Vecu &index_end, MeshFunctor &mesh_functor, Real dt = 0.0);
54  void MeshIterator_parallel(const Vecu &index_begin, const Vecu &index_end, MeshFunctor &mesh_functor, Real dt = 0.0);
55 
63  class BaseMesh
64  {
65  protected:
69  public:
70  BaseMesh();
71  explicit BaseMesh(Vecu number_of_grid_points);
72  BaseMesh(Vecd mesh_lower_bound, Real grid_spacing, Vecu number_of_grid_points);
73  BaseMesh(BoundingBox tentative_bounds, Real grid_spacing, size_t buffer_width);
74  virtual ~BaseMesh(){};
75 
76  Vecd MeshLowerBound() { return mesh_lower_bound_; };
77  Real GridSpacing() { return grid_spacing_; };
78  Vecu NumberOfGridPoints() { return number_of_grid_points_; };
79  Vecu NumberOfGridPointsFromNumberOfCells(const Vecu &number_of_cells) { return number_of_cells + Vecu(1); };
80  Vecu NumberOfCellsFromNumberOfGridPoints(const Vecu &number_of_grid_points) { return number_of_grid_points - Vecu(1); };
81  Vecd GridPositionFromCellPosition(const Vecd &cell_position) { return cell_position - Vecd(0.5 * grid_spacing_); };
82 
83  Vecu CellIndexFromPosition(const Vecd &position);
84  Vecd CellPositionFromIndex(const Vecu &cell_index);
86  Vecd GridPositionFromIndex(const Vecu &grid_index);
87  Vecu transfer1DtoMeshIndex(const Vecu &number_of_mesh_indexes, size_t i);
88  size_t transferMeshIndexTo1D(const Vecu &number_of_mesh_indexes, const Vecu &mesh_index);
94  size_t MortonCode(const size_t &i);
96  size_t transferMeshIndexToMortonOrder(const Vecu &mesh_index);
97  };
98 
106  class Mesh : public BaseMesh
107  {
108  protected:
109  size_t buffer_width_;
112  void copyMeshProperties(Mesh *another_mesh);
113 
114  public:
115  Mesh(BoundingBox tentative_bounds, Real grid_spacing, size_t buffer_width);
116  Mesh(Vecd mesh_lower_bound, Vecu number_of_cells, Real grid_spacing);
117  virtual ~Mesh(){};
118 
119  Vecu NumberOfCells() { return number_of_cells_; };
120  size_t MeshBufferSize() { return buffer_width_; };
121  virtual Real DataSpacing() { return grid_spacing_; };
122  };
123 
129  {
130  protected:
131  std::string name_;
132 
133  public:
134  explicit BaseMeshField(const std::string &name) : name_(name){};
135  virtual ~BaseMeshField(){};
136 
137  std::string Name() { return name_; };
139  virtual void writeMeshFieldToPlt(std::ofstream &output_file) = 0;
140  };
141 
147  template <class CoarseMeshType>
148  class RefinedMesh : public CoarseMeshType
149  {
150  public:
151  template <typename... Args>
152  RefinedMesh(BoundingBox tentative_bounds, CoarseMeshType &coarse_mesh, Args &&...args)
153  : CoarseMeshType(tentative_bounds, 0.5 * coarse_mesh.DataSpacing(), std::forward<Args>(args)...),
154  coarse_mesh_(coarse_mesh){};
155  virtual ~RefinedMesh(){};
156 
157  protected:
158  CoarseMeshType &coarse_mesh_;
159  };
160 
165  template <class MeshFieldType, class CoarsestMeshType, class RefinedMeshType>
167  {
168  private:
169  UniquePtrKeepers<CoarsestMeshType> mesh_level_ptr_vector_keeper_;
170 
171  protected:
172  size_t total_levels_;
173  StdVec<CoarsestMeshType *> mesh_levels_;
174 
175  public:
178  template <typename... Args>
179  MultilevelMesh(BoundingBox tentative_bounds, Real reference_spacing, size_t total_levels, Args &&...args)
180  : MeshFieldType(std::forward<Args>(args)...), total_levels_(total_levels)
181  {
182  Real coarsest_spacing = reference_spacing;
183  mesh_levels_.push_back(
184  mesh_level_ptr_vector_keeper_
185  .template createPtr<CoarsestMeshType>(tentative_bounds, coarsest_spacing, std::forward<Args>(args)...));
186 
187  for (size_t level = 1; level != total_levels_; ++level)
188  {
190  mesh_levels_.push_back(
191  mesh_level_ptr_vector_keeper_
192  .template createPtr<RefinedMeshType>(tentative_bounds, *mesh_levels_.back(), std::forward<Args>(args)...));
193  }
194  };
195 
196  virtual ~MultilevelMesh(){};
197 
198  StdVec<CoarsestMeshType *> getMeshLevels() { return mesh_levels_; };
199 
200  void writeMeshFieldToPlt(std::ofstream &output_file) override
201  {
202  for (size_t l = 0; l != total_levels_; ++l)
203  {
204  mesh_levels_[l]->writeMeshFieldToPlt(output_file);
205  }
206  }
207  };
208 }
209 #endif //BASE_MESH_H
std::function< void(const Vecu &, Real)> MeshFunctor
Definition: base_mesh.h:50
Vecu number_of_cells_
Definition: base_mesh.h:110
void MeshIterator_parallel(const Vecu &index_begin, const Vecu &index_end, MeshFunctor &mesh_functor, Real dt)
Definition: base_mesh_supplementary.cpp:19
Definition: base_data_type.h:43
size_t total_levels_
Definition: base_mesh.h:172
Abstract base class for cell-based mesh by introducing number of cells, buffer width and mesh-based d...
Definition: base_mesh.h:106
Vecu number_of_grid_points_
Definition: base_mesh.h:68
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 the field data saved on a mesh.
Definition: base_mesh.h:128
size_t buffer_width_
Definition: base_mesh.h:109
Multi-level Meshes with successively double the resolution.
Definition: base_mesh.h:166
Vecd mesh_lower_bound_
Definition: base_mesh.h:66
Abstract base class derived from the coarse mesh but has double resolution. Currently, the design is simple but can be extending for more inter-mesh operations.
Definition: base_mesh.h:148
void MeshIterator(const Vecu &index_begin, const Vecu &index_end, MeshFunctor &mesh_functor, Real dt)
Definition: base_mesh_supplementary.cpp:11
Set up of basic data structure.
Real grid_spacing_
Definition: base_mesh.h:67
MultilevelMesh(BoundingBox tentative_bounds, Real reference_spacing, size_t total_levels, Args &&...args)
Definition: base_mesh.h:179
Definition: solid_body_supplementary.cpp:9