Skip to content

Commit 2b7ee95

Browse files
authored
Merge pull request #627 from Xiangyu-Hu/fix/generalize_2_3d
using 1d matrix for cell linked lists
2 parents 85315b8 + 824fccd commit 2b7ee95

File tree

17 files changed

+279
-507
lines changed

17 files changed

+279
-507
lines changed

src/for_2D_build/geometries/level_set_2d.cpp

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,6 @@
99
namespace SPH
1010
{
1111
//=================================================================================================//
12-
LevelSet::LevelSet(BoundingBox tentative_bounds, Real data_spacing,
13-
Shape &shape, SPHAdaptation &sph_adaptation)
14-
: LevelSet(tentative_bounds, data_spacing, 4, shape, sph_adaptation)
15-
{
16-
mesh_parallel_for(MeshRange(Arrayi::Zero(), all_cells_),
17-
[&](size_t i, size_t j)
18-
{
19-
initializeDataInACell(Arrayi(i, j));
20-
});
21-
22-
finishDataPackages();
23-
}
24-
//=================================================================================================//
2512
void LevelSet::initializeDataForSingularPackage(const size_t package_index, Real far_field_level_set)
2613
{
2714
auto &phi = phi_.DataField()[package_index];
@@ -41,54 +28,13 @@ void LevelSet::initializeDataForSingularPackage(const size_t package_index, Real
4128
});
4229
}
4330
//=================================================================================================//
44-
void LevelSet::finishDataPackages()
45-
{
46-
mesh_parallel_for(MeshRange(Arrayi::Zero(), all_cells_),
47-
[&](size_t i, size_t j)
48-
{
49-
tagACellIsInnerPackage(Arrayi(i, j));
50-
});
51-
52-
initializeIndexMesh();
53-
initializeCellNeighborhood();
54-
resizeMeshVariableData();
55-
56-
Real far_field_distance = grid_spacing_ * (Real)buffer_width_;
57-
initializeDataForSingularPackage(0, -far_field_distance);
58-
initializeDataForSingularPackage(1, far_field_distance);
59-
60-
package_parallel_for(
61-
[&](size_t package_index)
62-
{
63-
initializeBasicDataForAPackage(meta_data_cell_[package_index].first, package_index, shape_);
64-
});
65-
66-
updateLevelSetGradient();
67-
updateKernelIntegrals();
68-
}
69-
//=================================================================================================//
70-
void LevelSet::initializeIndexMesh()
71-
{
72-
mesh_for(MeshRange(Arrayi::Zero(), all_cells_),
73-
[&](size_t i, size_t j)
74-
{
75-
Arrayi cell_index = Arrayi(i, j);
76-
if (isInnerDataPackage(cell_index))
77-
{
78-
assignDataPackageIndex(Arrayi(i, j), num_grid_pkgs_);
79-
num_grid_pkgs_++;
80-
}
81-
});
82-
}
83-
//=================================================================================================//
8431
void LevelSet::initializeCellNeighborhood()
8532
{
8633
cell_neighborhood_ = new CellNeighborhood[num_grid_pkgs_];
8734
meta_data_cell_ = new std::pair<Arrayi, int>[num_grid_pkgs_];
8835
mesh_parallel_for(MeshRange(Arrayi::Zero(), all_cells_),
89-
[&](size_t i, size_t j)
36+
[&](const Arrayi &cell_index)
9037
{
91-
Arrayi cell_index = Arrayi(i, j);
9238
if (isInnerDataPackage(cell_index))
9339
{
9440
CellNeighborhood &current = cell_neighborhood_[PackageIndexFromCellIndex(cell_index)];
@@ -103,12 +49,6 @@ void LevelSet::initializeCellNeighborhood()
10349
}
10450
});
10551
}
106-
//=================================================================================================//
107-
bool LevelSet::isWithinCorePackage(Vecd position)
108-
{
109-
Arrayi cell_index = CellIndexFromPosition(position);
110-
return isCoreDataPackage(cell_index);
111-
}
11252
//=============================================================================================//
11353
bool LevelSet::isInnerPackage(const Arrayi &cell_index)
11454
{
@@ -505,18 +445,4 @@ Vecd LevelSet::computeKernelGradientIntegral(const Vecd &position)
505445
return integral * data_spacing_ * data_spacing_;
506446
}
507447
//=============================================================================================//
508-
RefinedLevelSet::RefinedLevelSet(BoundingBox tentative_bounds, LevelSet &coarse_level_set,
509-
Shape &shape, SPHAdaptation &sph_adaptation)
510-
: RefinedMesh(tentative_bounds, coarse_level_set, 4, shape, sph_adaptation)
511-
{
512-
mesh_parallel_for(MeshRange(Arrayi::Zero(), all_cells_),
513-
[&](size_t i, size_t j)
514-
{
515-
initializeDataInACellFromCoarse(Arrayi(i, j));
516-
});
517-
518-
finishDataPackages();
519-
}
520-
//=============================================================================================//
521448
} // namespace SPH
522-
//=============================================================================================//

src/for_2D_build/meshes/cell_linked_list_2d.cpp

Lines changed: 5 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,10 @@
11
#include "cell_linked_list.h"
22

33
#include "base_particles.hpp"
4-
#include "mesh_iterators.hpp"
54

65
namespace SPH
76
{
87
//=================================================================================================//
9-
void CellLinkedList ::allocateMeshDataMatrix()
10-
{
11-
Allocate2dArray(cell_index_lists_, all_cells_);
12-
Allocate2dArray(cell_data_lists_, all_cells_);
13-
14-
mesh_parallel_for(MeshRange(Array2i::Zero(), all_cells_),
15-
[&](int i, int j)
16-
{
17-
cell_index_lists_[i][j].reserve(12);
18-
cell_data_lists_[i][j].reserve(12);
19-
});
20-
}
21-
//=================================================================================================//
22-
void CellLinkedList ::deleteMeshDataMatrix()
23-
{
24-
Delete2dArray(cell_index_lists_, all_cells_);
25-
Delete2dArray(cell_data_lists_, all_cells_);
26-
}
27-
//=================================================================================================//
28-
void CellLinkedList::clearCellLists()
29-
{
30-
mesh_parallel_for(MeshRange(Array2i::Zero(), all_cells_),
31-
[&](int i, int j)
32-
{
33-
cell_index_lists_[i][j].clear();
34-
});
35-
}
36-
//=================================================================================================//
37-
void CellLinkedList::UpdateCellListData(BaseParticles &base_particles)
38-
{
39-
StdLargeVec<Vecd> &pos = base_particles.ParticlePositions();
40-
mesh_parallel_for(
41-
MeshRange(Array2i::Zero(), all_cells_),
42-
[&](int i, int j)
43-
{
44-
cell_data_lists_[i][j].clear();
45-
ConcurrentIndexVector &cell_list = cell_index_lists_[i][j];
46-
for (size_t s = 0; s != cell_list.size(); ++s)
47-
{
48-
size_t index = cell_list[s];
49-
cell_data_lists_[i][j].emplace_back(std::make_pair(index, pos[index]));
50-
}
51-
});
52-
}
53-
//=================================================================================================//
54-
void CellLinkedList::updateSplitCellLists(SplitCellLists &split_cell_lists)
55-
{
56-
// clear the data
57-
clearSplitCellLists(split_cell_lists);
58-
mesh_parallel_for(
59-
MeshRange(Array2i::Zero(), all_cells_),
60-
[&](int i, int j)
61-
{
62-
size_t real_particles_in_cell = cell_index_lists_[i][j].size();
63-
if (real_particles_in_cell != 0)
64-
{
65-
split_cell_lists[transferMeshIndexTo1D(Array2i(3, 3), Array2i(i % 3, j % 3))]
66-
.push_back(&cell_index_lists_[i][j]);
67-
}
68-
});
69-
}
70-
//=================================================================================================//
71-
void CellLinkedList ::insertParticleIndex(size_t particle_index, const Vecd &particle_position)
72-
{
73-
Array2i cellpos = CellIndexFromPosition(particle_position);
74-
cell_index_lists_[cellpos[0]][cellpos[1]].emplace_back(particle_index);
75-
}
76-
//=================================================================================================//
77-
void CellLinkedList ::InsertListDataEntry(size_t particle_index, const Vecd &particle_position)
78-
{
79-
Array2i cellpos = CellIndexFromPosition(particle_position);
80-
cell_data_lists_[cellpos[0]][cellpos[1]]
81-
.emplace_back(std::make_pair(particle_index, particle_position));
82-
}
83-
//=================================================================================================//
84-
ListData CellLinkedList::findNearestListDataEntry(const Vecd &position)
85-
{
86-
Real min_distance_sqr = MaxReal;
87-
ListData nearest_entry(MaxSize_t, MaxReal * Vecd::Ones());
88-
89-
Array2i cell = CellIndexFromPosition(position);
90-
mesh_for_each(
91-
Array2i::Zero().max(cell - Array2i::Ones()),
92-
all_cells_.min(cell + 2 * Array2i::Ones()),
93-
[&](int l, int m)
94-
{
95-
ListDataVector &target_particles = cell_data_lists_[l][m];
96-
for (const ListData &list_data : target_particles)
97-
{
98-
Real distance_sqr = (position - std::get<1>(list_data)).squaredNorm();
99-
if (distance_sqr < min_distance_sqr)
100-
{
101-
min_distance_sqr = distance_sqr;
102-
nearest_entry = list_data;
103-
}
104-
}
105-
});
106-
return nearest_entry;
107-
}
108-
//=================================================================================================//
109-
void CellLinkedList::
110-
tagBodyPartByCell(ConcurrentCellLists &cell_lists, std::function<bool(Vecd, Real)> &check_included)
111-
{
112-
mesh_parallel_for(
113-
MeshRange(Array2i::Zero(), all_cells_),
114-
[&](int i, int j)
115-
{
116-
bool is_included = false;
117-
mesh_for_each(
118-
Array2i::Zero().max(Array2i(i, j) - Array2i::Ones()),
119-
all_cells_.min(Array2i(i, j) + 2 * Array2i::Ones()),
120-
[&](int l, int m)
121-
{
122-
if (check_included(CellPositionFromIndex(Array2i(l, m)), grid_spacing_))
123-
{
124-
is_included = true;
125-
}
126-
});
127-
if (is_included == true)
128-
cell_lists.push_back(&cell_index_lists_[i][j]);
129-
});
130-
}
131-
//=================================================================================================//
1328
void CellLinkedList::
1339
tagBoundingCells(StdVec<CellLists> &cell_data_lists, const BoundingBox &bounding_bounds, int axis)
13410
{
@@ -145,8 +21,8 @@ void CellLinkedList::
14521
Array2i cell = Array2i::Zero();
14622
cell[axis] = i;
14723
cell[second_axis] = j;
148-
cell_data_lists[0].first.push_back(&cell_index_lists_[cell[0]][cell[1]]);
149-
cell_data_lists[0].second.push_back(&cell_data_lists_[cell[0]][cell[1]]);
24+
cell_data_lists[0].first.push_back(&getCellDataList(cell_index_lists_, cell));
25+
cell_data_lists[0].second.push_back(&getCellDataList(cell_data_lists_, cell));
15026
}
15127

15228
// upper bound cells
@@ -158,8 +34,8 @@ void CellLinkedList::
15834
Array2i cell = Array2i::Zero();
15935
cell[axis] = i;
16036
cell[second_axis] = j;
161-
cell_data_lists[1].first.push_back(&cell_index_lists_[cell[0]][cell[1]]);
162-
cell_data_lists[1].second.push_back(&cell_data_lists_[cell[0]][cell[1]]);
37+
cell_data_lists[1].first.push_back(&getCellDataList(cell_index_lists_, cell));
38+
cell_data_lists[1].second.push_back(&getCellDataList(cell_data_lists_, cell));
16339
}
16440
}
16541
//=============================================================================================//
@@ -202,7 +78,7 @@ void CellLinkedList::writeMeshFieldToPlt(std::ofstream &output_file)
20278
{
20379
for (int i = 0; i != number_of_operation[0]; ++i)
20480
{
205-
output_file << cell_index_lists_[i][j].size() << " ";
81+
output_file << getCellDataList(cell_index_lists_, Array2i(i, j)).size() << " ";
20682
}
20783
output_file << " \n";
20884
}

src/for_2D_build/meshes/mesh_iterators.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void mesh_for_each(const Array2i &lower, const Array2i &upper, const FunctionOnE
4141
for (int l = lower[0]; l != upper[0]; ++l)
4242
for (int m = lower[1]; m != upper[1]; ++m)
4343
{
44-
function(l, m);
44+
function(Array2i(l, m));
4545
}
4646
}
4747
//=================================================================================================//
@@ -63,7 +63,7 @@ void mesh_for(const MeshRange &mesh_range, const LocalFunction &local_function,
6363
for (int i = (mesh_range.first)[0]; i != (mesh_range.second)[0]; ++i)
6464
for (int j = (mesh_range.first)[1]; j != (mesh_range.second)[1]; ++j)
6565
{
66-
local_function(i, j);
66+
local_function(Array2i(i, j));
6767
}
6868
}
6969
//=================================================================================================//
@@ -78,7 +78,7 @@ void mesh_parallel_for(const MeshRange &mesh_range, const LocalFunction &local_f
7878
for (size_t i = r.rows().begin(); i != r.rows().end(); ++i)
7979
for (size_t j = r.cols().begin(); j != r.cols().end(); ++j)
8080
{
81-
local_function(i, j);
81+
local_function(Array2i(i, j));
8282
}
8383
},
8484
ap);

0 commit comments

Comments
 (0)