From 773ab1d672083f465ff6d778a999c11cb72dd025 Mon Sep 17 00:00:00 2001 From: Xiangyu Hu Date: Sat, 3 Aug 2024 16:46:17 +0000 Subject: [PATCH] done. to test --- src/for_2D_build/meshes/base_mesh_2d.cpp | 6 +-- .../particle_generator_lattice_2d.cpp | 12 +++--- src/for_3D_build/meshes/base_mesh_3d.cpp | 6 +-- .../particle_generator_lattice_3d.cpp | 12 +++--- src/shared/meshes/base_mesh.cpp | 14 +++++-- src/shared/meshes/base_mesh.h | 38 +++++++++---------- src/shared/meshes/mesh_with_data_packages.h | 7 ++-- 7 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/for_2D_build/meshes/base_mesh_2d.cpp b/src/for_2D_build/meshes/base_mesh_2d.cpp index ebd9e38828..8f4c231c5e 100644 --- a/src/for_2D_build/meshes/base_mesh_2d.cpp +++ b/src/for_2D_build/meshes/base_mesh_2d.cpp @@ -3,19 +3,19 @@ namespace SPH { //=============================================================================================// -Arrayi BaseMesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i) +Arrayi Mesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i) { size_t row_size = mesh_size[1]; size_t column = i / row_size; return Arrayi(column, i - column * row_size); } //=============================================================================================// -size_t BaseMesh::transferMeshIndexTo1D(const Arrayi &mesh_size, const Arrayi &mesh_index) +size_t Mesh::transferMeshIndexTo1D(const Arrayi &mesh_size, const Arrayi &mesh_index) { return mesh_index[0] * mesh_size[1] + mesh_index[1]; } //=============================================================================================// -size_t BaseMesh::transferMeshIndexToMortonOrder(const Arrayi &mesh_index) +size_t Mesh::transferMeshIndexToMortonOrder(const Arrayi &mesh_index) { return MortonCode(mesh_index[0]) | (MortonCode(mesh_index[1]) << 1); } diff --git a/src/for_2D_build/particle_generator/particle_generator_lattice_2d.cpp b/src/for_2D_build/particle_generator/particle_generator_lattice_2d.cpp index 3f682a7f54..062296cf2e 100644 --- a/src/for_2D_build/particle_generator/particle_generator_lattice_2d.cpp +++ b/src/for_2D_build/particle_generator/particle_generator_lattice_2d.cpp @@ -11,9 +11,9 @@ namespace SPH //=================================================================================================// void ParticleGenerator::prepareGeometricData() { - BaseMesh mesh(domain_bounds_, lattice_spacing_, 0); + Mesh mesh(domain_bounds_, lattice_spacing_, 0); Real particle_volume = lattice_spacing_ * lattice_spacing_; - Arrayi number_of_lattices = mesh.AllCellsFromAllGridPoints(mesh.AllGridPoints()); + Arrayi number_of_lattices = mesh.AllCells(); for (int i = 0; i < number_of_lattices[0]; ++i) for (int j = 0; j < number_of_lattices[1]; ++j) { @@ -32,12 +32,12 @@ void ParticleGenerator::prepareGeometricData() { // Calculate the total volume and // count the number of cells inside the body volume, where we might put particles. - std::unique_ptr mesh(new BaseMesh(domain_bounds_, lattice_spacing_, 0)); - Arrayi number_of_lattices = mesh->AllCellsFromAllGridPoints(mesh->AllGridPoints()); + Mesh mesh(domain_bounds_, lattice_spacing_, 0); + Arrayi number_of_lattices = mesh.AllCells(); for (int i = 0; i < number_of_lattices[0]; ++i) for (int j = 0; j < number_of_lattices[1]; ++j) { - Vecd particle_position = mesh->CellPositionFromIndex(Arrayi(i, j)); + Vecd particle_position = mesh.CellPositionFromIndex(Arrayi(i, j)); if (initial_shape_.checkNotFar(particle_position, lattice_spacing_)) { if (initial_shape_.checkContain(particle_position)) @@ -63,7 +63,7 @@ void ParticleGenerator::prepareGeometricData() for (int i = 0; i < number_of_lattices[0]; ++i) for (int j = 0; j < number_of_lattices[1]; ++j) { - Vecd particle_position = mesh->CellPositionFromIndex(Arrayi(i, j)); + Vecd particle_position = mesh.CellPositionFromIndex(Arrayi(i, j)); if (initial_shape_.checkNotFar(particle_position, lattice_spacing_)) { if (initial_shape_.checkContain(particle_position)) diff --git a/src/for_3D_build/meshes/base_mesh_3d.cpp b/src/for_3D_build/meshes/base_mesh_3d.cpp index 752943086e..2bcb4e6d66 100644 --- a/src/for_3D_build/meshes/base_mesh_3d.cpp +++ b/src/for_3D_build/meshes/base_mesh_3d.cpp @@ -3,7 +3,7 @@ namespace SPH { //=================================================================================================// -Arrayi BaseMesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i) +Arrayi Mesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i) { size_t row_times_column_size = mesh_size[1] * mesh_size[2]; size_t page = i / row_times_column_size; @@ -13,14 +13,14 @@ Arrayi BaseMesh::transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i) return Arrayi(page, column, left_over - column * row_size); } //=================================================================================================// -size_t BaseMesh::transferMeshIndexTo1D(const Arrayi &mesh_size, const Arrayi &mesh_index) +size_t Mesh::transferMeshIndexTo1D(const Arrayi &mesh_size, const Arrayi &mesh_index) { return mesh_index[0] * mesh_size[1] * mesh_size[2] + mesh_index[1] * mesh_size[2] + mesh_index[2]; } //=================================================================================================// -size_t BaseMesh::transferMeshIndexToMortonOrder(const Arrayi &mesh_index) +size_t Mesh::transferMeshIndexToMortonOrder(const Arrayi &mesh_index) { return MortonCode(mesh_index[0]) | (MortonCode(mesh_index[1]) << 1) | (MortonCode(mesh_index[2]) << 2); } diff --git a/src/for_3D_build/particle_generator/particle_generator_lattice_3d.cpp b/src/for_3D_build/particle_generator/particle_generator_lattice_3d.cpp index f7ede7482c..61e8e4ee92 100644 --- a/src/for_3D_build/particle_generator/particle_generator_lattice_3d.cpp +++ b/src/for_3D_build/particle_generator/particle_generator_lattice_3d.cpp @@ -11,9 +11,9 @@ namespace SPH //=================================================================================================// void ParticleGenerator::prepareGeometricData() { - BaseMesh mesh(domain_bounds_, lattice_spacing_, 0); + Mesh mesh(domain_bounds_, lattice_spacing_, 0); Real particle_volume = lattice_spacing_ * lattice_spacing_ * lattice_spacing_; - Arrayi number_of_lattices = mesh.AllCellsFromAllGridPoints(mesh.AllGridPoints()); + Arrayi number_of_lattices = mesh.AllCells(); for (int i = 0; i < number_of_lattices[0]; ++i) for (int j = 0; j < number_of_lattices[1]; ++j) for (int k = 0; k < number_of_lattices[2]; ++k) @@ -33,13 +33,13 @@ void ParticleGenerator::prepareGeometricData() { // Calculate the total volume and // count the number of cells inside the body volume, where we might put particles. - std::unique_ptr mesh(new BaseMesh(domain_bounds_, lattice_spacing_, 0)); - Arrayi number_of_lattices = mesh->AllCellsFromAllGridPoints(mesh->AllGridPoints()); + Mesh mesh(domain_bounds_, lattice_spacing_, 0); + Arrayi number_of_lattices = mesh.AllCells(); for (int i = 0; i < number_of_lattices[0]; ++i) for (int j = 0; j < number_of_lattices[1]; ++j) for (int k = 0; k < number_of_lattices[2]; ++k) { - Vecd particle_position = mesh->CellPositionFromIndex(Arrayi(i, j, k)); + Vecd particle_position = mesh.CellPositionFromIndex(Arrayi(i, j, k)); if (initial_shape_.checkNotFar(particle_position, lattice_spacing_)) { if (initial_shape_.checkContain(particle_position)) @@ -66,7 +66,7 @@ void ParticleGenerator::prepareGeometricData() for (int j = 0; j < number_of_lattices[1]; ++j) for (int k = 0; k < number_of_lattices[2]; ++k) { - Vecd particle_position = mesh->CellPositionFromIndex(Arrayi(i, j, k)); + Vecd particle_position = mesh.CellPositionFromIndex(Arrayi(i, j, k)); if (initial_shape_.checkNotFar(particle_position, lattice_spacing_)) { if (initial_shape_.checkContain(particle_position)) diff --git a/src/shared/meshes/base_mesh.cpp b/src/shared/meshes/base_mesh.cpp index f81fb92954..5cc15455cf 100644 --- a/src/shared/meshes/base_mesh.cpp +++ b/src/shared/meshes/base_mesh.cpp @@ -4,12 +4,20 @@ namespace SPH { //=================================================================================================// Mesh::Mesh(BoundingBox tentative_bounds, Real grid_spacing, size_t buffer_width) - : grid_spacing_(grid_spacing) + : grid_spacing_(grid_spacing), buffer_width_(buffer_width) { Vecd mesh_buffer = Real(buffer_width) * grid_spacing * Vecd::Ones(); mesh_lower_bound_ = tentative_bounds.first_ - mesh_buffer; Vecd tentative_dimension = tentative_bounds.second_ + mesh_buffer - mesh_lower_bound_; all_grid_points_ = ceil(tentative_dimension.array() / grid_spacing).cast() + Arrayi::Ones(); + all_cells_ = all_grid_points_ - Arrayi::Ones(); +} +//=================================================================================================// +Mesh::Mesh(Vecd mesh_lower_bound, Real grid_spacing, Arrayi all_grid_points) + : mesh_lower_bound_{mesh_lower_bound}, grid_spacing_{grid_spacing}, + buffer_width_(0), all_grid_points_{all_grid_points} +{ + all_cells_ = all_grid_points_ - Arrayi::Ones(); } //=================================================================================================// Arrayi Mesh::CellIndexFromPosition(const Vecd &position) @@ -20,14 +28,14 @@ Arrayi Mesh::CellIndexFromPosition(const Vecd &position) .min(all_grid_points_ - 2 * Arrayi::Ones()); } //=================================================================================================// -Vecd Mesh::CellLowerCorner(const Arrayi &cell_index) +Vecd Mesh::CellLowerCornerPosition(const Arrayi &cell_index) { return mesh_lower_bound_ + cell_index.cast().matrix() * grid_spacing_; } //=================================================================================================// Vecd Mesh::CellPositionFromIndex(const Arrayi &cell_index) { - return CellLowerCorner(cell_index) + 0.5 * Vecd::Ones() * grid_spacing_; + return CellLowerCornerPosition(cell_index) + 0.5 * Vecd::Ones() * grid_spacing_; } //=================================================================================================// Vecd Mesh::GridPositionFromIndex(const Arrayi &grid_index) diff --git a/src/shared/meshes/base_mesh.h b/src/shared/meshes/base_mesh.h index d11f47d306..41f4385b62 100644 --- a/src/shared/meshes/base_mesh.h +++ b/src/shared/meshes/base_mesh.h @@ -47,7 +47,7 @@ using namespace std::placeholders; namespace SPH { /** - * @class BaseMesh + * @class Mesh * @brief Base class for all structured meshes which may be grid or cell based. * The basic properties of the mesh, such as lower bound, grid spacing * and number of grid points may be determined by the derived class. @@ -55,44 +55,42 @@ namespace SPH */ class Mesh { - protected: - Vecd mesh_lower_bound_; /**< mesh lower bound as reference coordinate */ - Real grid_spacing_; /**< grid_spacing */ - Arrayi all_grid_points_; /**< number of grid points by dimension */ public: Mesh(BoundingBox tentative_bounds, Real grid_spacing, size_t buffer_width); + Mesh(Vecd mesh_lower_bound, Real grid_spacing, Arrayi all_grid_points); ~Mesh(){}; Vecd MeshLowerBound() { return mesh_lower_bound_; }; Real GridSpacing() { return grid_spacing_; }; Arrayi AllGridPoints() { return all_grid_points_; }; - Arrayi AllCells() { return all_grid_points_ - Arrayi::Ones(); }; + Arrayi AllCells() { return all_cells_; }; size_t NumberOfGridPoints() { return transferMeshIndexTo1D(all_grid_points_, all_grid_points_); }; - size_t NumberOfCells() { return transferMeshIndexTo1D(AllCells(), AllCells()); }; - /** Given the grid point number, return the cell number. */ - Arrayi AllCellsFromAllGridPoints(const Arrayi &all_grid_points) { return all_grid_points - Arrayi::Ones(); }; - /** Given the cell position, return the grid position. */ - Vecd GridPositionFromCellPosition(const Vecd &cell_position) { return cell_position - 0.5 * grid_spacing_ * Vecd::Ones(); }; - /** Given the position, return the cell index. */ + size_t NumberOfCells() { return transferMeshIndexTo1D(all_cells_, all_cells_); }; Arrayi CellIndexFromPosition(const Vecd &position); - /** Given the cell index, return the cell position. */ Vecd CellPositionFromIndex(const Arrayi &cell_index); - /** Given the cell index, return the position of its lower corner. */ - Vecd CellLowerCorner(const Arrayi &cell_index); - /** Given the index, return the grid position. */ Vecd GridPositionFromIndex(const Arrayi &grid_index); - /** Transfer 1D int to mesh index. */ + Vecd CellLowerCornerPosition(const Arrayi &cell_index); + //---------------------------------------------------------------------- + // Transferring between 1D mesh indexes. + // Here, mesh size can be either AllGridPoints or AllCells. + //---------------------------------------------------------------------- Arrayi transfer1DtoMeshIndex(const Arrayi &mesh_size, size_t i); - /** Transfer mesh index to 1D int. */ size_t transferMeshIndexTo1D(const Arrayi &mesh_size, const Arrayi &mesh_index); /** converts mesh index into a Morton order. * Interleave a 10 bit number in 32 bits, fill one bit and leave the other 2 as zeros * https://stackoverflow.com/questions/18529057/ * produce-interleaving-bit-patterns-morton-keys-for-32-bit-64-bit-and-128bit */ - size_t MortonCode(const size_t &i); - /** Converts mesh index into a Morton order. */ size_t transferMeshIndexToMortonOrder(const Arrayi &mesh_index); + + protected: + Vecd mesh_lower_bound_; /**< mesh lower bound as reference coordinate */ + Real grid_spacing_; /**< grid_spacing */ + size_t buffer_width_; /**< buffer width to avoid bound check.*/ + Arrayi all_grid_points_; /**< number of grid points by dimension */ + Arrayi all_cells_; /**< number of cells by dimension */ + + size_t MortonCode(const size_t &i); }; /** diff --git a/src/shared/meshes/mesh_with_data_packages.h b/src/shared/meshes/mesh_with_data_packages.h index d61e550dcc..9647df10f9 100644 --- a/src/shared/meshes/mesh_with_data_packages.h +++ b/src/shared/meshes/mesh_with_data_packages.h @@ -118,8 +118,7 @@ class MeshWithGridDataPackages : public Mesh public: template explicit MeshWithGridDataPackages(BoundingBox tentative_bounds, Real data_spacing, size_t buffer_size) - : Mesh(tentative_bounds, pkg_size * data_spacing, buffer_size), - data_spacing_(data_spacing), + : Mesh(tentative_bounds, pkg_size * data_spacing, buffer_size), data_spacing_(data_spacing), global_mesh_(mesh_lower_bound_ + 0.5 * data_spacing * Vecd::Ones(), data_spacing, all_cells_ * pkg_size) { allocateMetaDataMatrix(); @@ -137,7 +136,7 @@ class MeshWithGridDataPackages : public Mesh MeshVariableAssemble all_mesh_variables_; /**< all mesh variables on this mesh. */ static constexpr int pkg_size = PKG_SIZE; /**< the size of the data package matrix*/ const Real data_spacing_; /**< spacing of data in the data packages*/ - BaseMesh global_mesh_; /**< the mesh for the locations of all possible data points. */ + Mesh global_mesh_; /**< the mesh for the locations of all possible data points. */ size_t num_grid_pkgs_ = 2; /**< the number of all distinct packages, initially only 2 singular packages. */ using MetaData = std::pair; /**< stores the metadata for each cell: (int)singular0/inner1/core2, (size_t)package data index*/ MeshDataMatrix meta_data_mesh_; /**< metadata for all cells. */ @@ -235,7 +234,7 @@ class MeshWithGridDataPackages : public Mesh /** return the position of the lower bound data in a cell. */ Vecd DataLowerBoundInCell(const Arrayi &cell_index) { - return CellLowerCorner(cell_index) + 0.5 * data_spacing_ * Vecd::Ones(); + return CellLowerCornerPosition(cell_index) + 0.5 * data_spacing_ * Vecd::Ones(); } /** return the grid index from its position and the index of the cell it belongs to. */