Skip to content

Commit

Permalink
done. to test
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiangyu-Hu committed Aug 3, 2024
1 parent ddd064d commit 773ab1d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/for_2D_build/meshes/base_mesh_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace SPH
//=================================================================================================//
void ParticleGenerator<BaseParticles, Lattice>::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)
{
Expand All @@ -32,12 +32,12 @@ void ParticleGenerator<SurfaceParticles, Lattice>::prepareGeometricData()
{
// Calculate the total volume and
// count the number of cells inside the body volume, where we might put particles.
std::unique_ptr<BaseMesh> 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))
Expand All @@ -63,7 +63,7 @@ void ParticleGenerator<SurfaceParticles, Lattice>::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))
Expand Down
6 changes: 3 additions & 3 deletions src/for_3D_build/meshes/base_mesh_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace SPH
//=================================================================================================//
void ParticleGenerator<BaseParticles, Lattice>::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)
Expand All @@ -33,13 +33,13 @@ void ParticleGenerator<SurfaceParticles, Lattice>::prepareGeometricData()
{
// Calculate the total volume and
// count the number of cells inside the body volume, where we might put particles.
std::unique_ptr<BaseMesh> 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))
Expand All @@ -66,7 +66,7 @@ void ParticleGenerator<SurfaceParticles, Lattice>::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))
Expand Down
14 changes: 11 additions & 3 deletions src/shared/meshes/base_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>() + 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)
Expand All @@ -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<Real>().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)
Expand Down
38 changes: 18 additions & 20 deletions src/shared/meshes/base_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,52 +47,50 @@ 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.
* Note that there is no mesh-based data defined here.
*/
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);
};

/**
Expand Down
7 changes: 3 additions & 4 deletions src/shared/meshes/mesh_with_data_packages.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,7 @@ class MeshWithGridDataPackages : public Mesh
public:
template <typename... Args>
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();
Expand All @@ -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<int, size_t>; /**< stores the metadata for each cell: (int)singular0/inner1/core2, (size_t)package data index*/
MeshDataMatrix<MetaData> meta_data_mesh_; /**< metadata for all cells. */
Expand Down Expand Up @@ -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. */
Expand Down

0 comments on commit 773ab1d

Please sign in to comment.