Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using 1d matrix for cell linked lists #627

Merged
merged 7 commits into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 1 addition & 75 deletions src/for_2D_build/geometries/level_set_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@
namespace SPH
{
//=================================================================================================//
LevelSet::LevelSet(BoundingBox tentative_bounds, Real data_spacing,
Shape &shape, SPHAdaptation &sph_adaptation)
: LevelSet(tentative_bounds, data_spacing, 4, shape, sph_adaptation)
{
mesh_parallel_for(MeshRange(Arrayi::Zero(), all_cells_),
[&](size_t i, size_t j)
{
initializeDataInACell(Arrayi(i, j));
});

finishDataPackages();
}
//=================================================================================================//
void LevelSet::initializeDataForSingularPackage(const size_t package_index, Real far_field_level_set)
{
auto &phi = phi_.DataField()[package_index];
Expand All @@ -41,54 +28,13 @@ void LevelSet::initializeDataForSingularPackage(const size_t package_index, Real
});
}
//=================================================================================================//
void LevelSet::finishDataPackages()
{
mesh_parallel_for(MeshRange(Arrayi::Zero(), all_cells_),
[&](size_t i, size_t j)
{
tagACellIsInnerPackage(Arrayi(i, j));
});

initializeIndexMesh();
initializeCellNeighborhood();
resizeMeshVariableData();

Real far_field_distance = grid_spacing_ * (Real)buffer_width_;
initializeDataForSingularPackage(0, -far_field_distance);
initializeDataForSingularPackage(1, far_field_distance);

package_parallel_for(
[&](size_t package_index)
{
initializeBasicDataForAPackage(meta_data_cell_[package_index].first, package_index, shape_);
});

updateLevelSetGradient();
updateKernelIntegrals();
}
//=================================================================================================//
void LevelSet::initializeIndexMesh()
{
mesh_for(MeshRange(Arrayi::Zero(), all_cells_),
[&](size_t i, size_t j)
{
Arrayi cell_index = Arrayi(i, j);
if (isInnerDataPackage(cell_index))
{
assignDataPackageIndex(Arrayi(i, j), num_grid_pkgs_);
num_grid_pkgs_++;
}
});
}
//=================================================================================================//
void LevelSet::initializeCellNeighborhood()
{
cell_neighborhood_ = new CellNeighborhood[num_grid_pkgs_];
meta_data_cell_ = new std::pair<Arrayi, int>[num_grid_pkgs_];
mesh_parallel_for(MeshRange(Arrayi::Zero(), all_cells_),
[&](size_t i, size_t j)
[&](const Arrayi &cell_index)
{
Arrayi cell_index = Arrayi(i, j);
if (isInnerDataPackage(cell_index))
{
CellNeighborhood &current = cell_neighborhood_[PackageIndexFromCellIndex(cell_index)];
Expand All @@ -103,12 +49,6 @@ void LevelSet::initializeCellNeighborhood()
}
});
}
//=================================================================================================//
bool LevelSet::isWithinCorePackage(Vecd position)
{
Arrayi cell_index = CellIndexFromPosition(position);
return isCoreDataPackage(cell_index);
}
//=============================================================================================//
bool LevelSet::isInnerPackage(const Arrayi &cell_index)
{
Expand Down Expand Up @@ -505,18 +445,4 @@ Vecd LevelSet::computeKernelGradientIntegral(const Vecd &position)
return integral * data_spacing_ * data_spacing_;
}
//=============================================================================================//
RefinedLevelSet::RefinedLevelSet(BoundingBox tentative_bounds, LevelSet &coarse_level_set,
Shape &shape, SPHAdaptation &sph_adaptation)
: RefinedMesh(tentative_bounds, coarse_level_set, 4, shape, sph_adaptation)
{
mesh_parallel_for(MeshRange(Arrayi::Zero(), all_cells_),
[&](size_t i, size_t j)
{
initializeDataInACellFromCoarse(Arrayi(i, j));
});

finishDataPackages();
}
//=============================================================================================//
} // namespace SPH
//=============================================================================================//
134 changes: 5 additions & 129 deletions src/for_2D_build/meshes/cell_linked_list_2d.cpp
Original file line number Diff line number Diff line change
@@ -1,134 +1,10 @@
#include "cell_linked_list.h"

#include "base_particles.hpp"
#include "mesh_iterators.hpp"

namespace SPH
{
//=================================================================================================//
void CellLinkedList ::allocateMeshDataMatrix()
{
Allocate2dArray(cell_index_lists_, all_cells_);
Allocate2dArray(cell_data_lists_, all_cells_);

mesh_parallel_for(MeshRange(Array2i::Zero(), all_cells_),
[&](int i, int j)
{
cell_index_lists_[i][j].reserve(12);
cell_data_lists_[i][j].reserve(12);
});
}
//=================================================================================================//
void CellLinkedList ::deleteMeshDataMatrix()
{
Delete2dArray(cell_index_lists_, all_cells_);
Delete2dArray(cell_data_lists_, all_cells_);
}
//=================================================================================================//
void CellLinkedList::clearCellLists()
{
mesh_parallel_for(MeshRange(Array2i::Zero(), all_cells_),
[&](int i, int j)
{
cell_index_lists_[i][j].clear();
});
}
//=================================================================================================//
void CellLinkedList::UpdateCellListData(BaseParticles &base_particles)
{
StdLargeVec<Vecd> &pos = base_particles.ParticlePositions();
mesh_parallel_for(
MeshRange(Array2i::Zero(), all_cells_),
[&](int i, int j)
{
cell_data_lists_[i][j].clear();
ConcurrentIndexVector &cell_list = cell_index_lists_[i][j];
for (size_t s = 0; s != cell_list.size(); ++s)
{
size_t index = cell_list[s];
cell_data_lists_[i][j].emplace_back(std::make_pair(index, pos[index]));
}
});
}
//=================================================================================================//
void CellLinkedList::updateSplitCellLists(SplitCellLists &split_cell_lists)
{
// clear the data
clearSplitCellLists(split_cell_lists);
mesh_parallel_for(
MeshRange(Array2i::Zero(), all_cells_),
[&](int i, int j)
{
size_t real_particles_in_cell = cell_index_lists_[i][j].size();
if (real_particles_in_cell != 0)
{
split_cell_lists[transferMeshIndexTo1D(Array2i(3, 3), Array2i(i % 3, j % 3))]
.push_back(&cell_index_lists_[i][j]);
}
});
}
//=================================================================================================//
void CellLinkedList ::insertParticleIndex(size_t particle_index, const Vecd &particle_position)
{
Array2i cellpos = CellIndexFromPosition(particle_position);
cell_index_lists_[cellpos[0]][cellpos[1]].emplace_back(particle_index);
}
//=================================================================================================//
void CellLinkedList ::InsertListDataEntry(size_t particle_index, const Vecd &particle_position)
{
Array2i cellpos = CellIndexFromPosition(particle_position);
cell_data_lists_[cellpos[0]][cellpos[1]]
.emplace_back(std::make_pair(particle_index, particle_position));
}
//=================================================================================================//
ListData CellLinkedList::findNearestListDataEntry(const Vecd &position)
{
Real min_distance_sqr = MaxReal;
ListData nearest_entry(MaxSize_t, MaxReal * Vecd::Ones());

Array2i cell = CellIndexFromPosition(position);
mesh_for_each(
Array2i::Zero().max(cell - Array2i::Ones()),
all_cells_.min(cell + 2 * Array2i::Ones()),
[&](int l, int m)
{
ListDataVector &target_particles = cell_data_lists_[l][m];
for (const ListData &list_data : target_particles)
{
Real distance_sqr = (position - std::get<1>(list_data)).squaredNorm();
if (distance_sqr < min_distance_sqr)
{
min_distance_sqr = distance_sqr;
nearest_entry = list_data;
}
}
});
return nearest_entry;
}
//=================================================================================================//
void CellLinkedList::
tagBodyPartByCell(ConcurrentCellLists &cell_lists, std::function<bool(Vecd, Real)> &check_included)
{
mesh_parallel_for(
MeshRange(Array2i::Zero(), all_cells_),
[&](int i, int j)
{
bool is_included = false;
mesh_for_each(
Array2i::Zero().max(Array2i(i, j) - Array2i::Ones()),
all_cells_.min(Array2i(i, j) + 2 * Array2i::Ones()),
[&](int l, int m)
{
if (check_included(CellPositionFromIndex(Array2i(l, m)), grid_spacing_))
{
is_included = true;
}
});
if (is_included == true)
cell_lists.push_back(&cell_index_lists_[i][j]);
});
}
//=================================================================================================//
void CellLinkedList::
tagBoundingCells(StdVec<CellLists> &cell_data_lists, const BoundingBox &bounding_bounds, int axis)
{
Expand All @@ -145,8 +21,8 @@ void CellLinkedList::
Array2i cell = Array2i::Zero();
cell[axis] = i;
cell[second_axis] = j;
cell_data_lists[0].first.push_back(&cell_index_lists_[cell[0]][cell[1]]);
cell_data_lists[0].second.push_back(&cell_data_lists_[cell[0]][cell[1]]);
cell_data_lists[0].first.push_back(&getCellDataList(cell_index_lists_, cell));
cell_data_lists[0].second.push_back(&getCellDataList(cell_data_lists_, cell));
}

// upper bound cells
Expand All @@ -158,8 +34,8 @@ void CellLinkedList::
Array2i cell = Array2i::Zero();
cell[axis] = i;
cell[second_axis] = j;
cell_data_lists[1].first.push_back(&cell_index_lists_[cell[0]][cell[1]]);
cell_data_lists[1].second.push_back(&cell_data_lists_[cell[0]][cell[1]]);
cell_data_lists[1].first.push_back(&getCellDataList(cell_index_lists_, cell));
cell_data_lists[1].second.push_back(&getCellDataList(cell_data_lists_, cell));
}
}
//=============================================================================================//
Expand Down Expand Up @@ -202,7 +78,7 @@ void CellLinkedList::writeMeshFieldToPlt(std::ofstream &output_file)
{
for (int i = 0; i != number_of_operation[0]; ++i)
{
output_file << cell_index_lists_[i][j].size() << " ";
output_file << getCellDataList(cell_index_lists_, Array2i(i, j)).size() << " ";
}
output_file << " \n";
}
Expand Down
6 changes: 3 additions & 3 deletions src/for_2D_build/meshes/mesh_iterators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void mesh_for_each(const Array2i &lower, const Array2i &upper, const FunctionOnE
for (int l = lower[0]; l != upper[0]; ++l)
for (int m = lower[1]; m != upper[1]; ++m)
{
function(l, m);
function(Array2i(l, m));
}
}
//=================================================================================================//
Expand All @@ -63,7 +63,7 @@ void mesh_for(const MeshRange &mesh_range, const LocalFunction &local_function,
for (int i = (mesh_range.first)[0]; i != (mesh_range.second)[0]; ++i)
for (int j = (mesh_range.first)[1]; j != (mesh_range.second)[1]; ++j)
{
local_function(i, j);
local_function(Array2i(i, j));
}
}
//=================================================================================================//
Expand All @@ -78,7 +78,7 @@ void mesh_parallel_for(const MeshRange &mesh_range, const LocalFunction &local_f
for (size_t i = r.rows().begin(); i != r.rows().end(); ++i)
for (size_t j = r.cols().begin(); j != r.cols().end(); ++j)
{
local_function(i, j);
local_function(Array2i(i, j));
}
},
ap);
Expand Down
Loading
Loading