Skip to content

Commit

Permalink
rename getBins -> getParticleBins plus formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lebuller committed Nov 8, 2023
1 parent 74712a9 commit c92e183
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 31 deletions.
107 changes: 87 additions & 20 deletions core/src/Cabana_LinkedCellList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ struct LinkedCellStencil

//! Given a cell, get the index bounds of the cell stencil.
KOKKOS_INLINE_FUNCTION
void getStencilCells( const int cell, int& imin, int& imax, int& jmin, int& jmax,
int& kmin, int& kmax ) const
void getStencilCells( const int cell, int& imin, int& imax, int& jmin,
int& jmax, int& kmin, int& kmax ) const
{
int i, j, k;
grid.ijkBinIndex( cell, i, j, k );
Expand Down Expand Up @@ -121,6 +121,72 @@ class LinkedCellList
*/
LinkedCellList() {}

/*!
\brief Slice constructor
\tparam SliceType Slice type for positions.
\param positions Slice of positions.
\param grid_delta Grid sizes in each cardinal direction.
\param grid_min Grid minimum value in each direction.
\param grid_max Grid maximum value in each direction.
*/
template <class SliceType>
LinkedCellList(
SliceType positions, const typename SliceType::value_type grid_delta[3],
const typename SliceType::value_type grid_min[3],
const typename SliceType::value_type grid_max[3],
typename std::enable_if<( is_slice<SliceType>::value ), int>::type* =
0 )
: _grid( grid_min[0], grid_min[1], grid_min[2], grid_max[0],
grid_max[1], grid_max[2], grid_delta[0], grid_delta[1],
grid_delta[2] )
, _sorted( false )
{
_cell_stencil( grid_delta[0], 1.0, grid_min, grid_max );
std::size_t np = positions.size();
allocate( totalBins(), np );
build( positions, 0, np );
}

/*!
\brief Slice constructor
\tparam SliceType Slice type for positions.
\param positions Slice of positions.
\param begin The beginning index of the AoSoA range to sort.
\param end The end index of the AoSoA range to sort.
\param grid_delta Grid sizes in each cardinal direction.
\param grid_min Grid minimum value in each direction.
\param grid_max Grid maximum value in each direction.
*/
template <class SliceType>
LinkedCellList(
SliceType positions, const std::size_t begin, const std::size_t end,
const typename SliceType::value_type grid_delta[3],
const typename SliceType::value_type grid_min[3],
const typename SliceType::value_type grid_max[3],
typename std::enable_if<( is_slice<SliceType>::value ), int>::type* =
0 )
: _grid( grid_min[0], grid_min[1], grid_min[2], grid_max[0],
grid_max[1], grid_max[2], grid_delta[0], grid_delta[1],
grid_delta[2] )
, _sorted( false )
{
_cell_stencil( grid_delta[0], 1.0, grid_min, grid_max );
allocate( totalBins(), end - begin );
build( positions, begin, end );
}

/*!
\brief Slice constructor
Expand All @@ -143,16 +209,16 @@ class LinkedCellList
SliceType positions, const typename SliceType::value_type grid_delta[3],
const typename SliceType::value_type grid_min[3],
const typename SliceType::value_type grid_max[3],
const Scalar neighborhood_radius,
const Scalar cell_size_ratio = 1,
// const typename SliceType::value_type neighborhood_radius,
// const typename SliceType::value cell_size_ratio = 1,
const Scalar neighborhood_radius, const Scalar cell_size_ratio = 1,
// const typename SliceType::value_type neighborhood_radius,
// const typename SliceType::value cell_size_ratio = 1,
typename std::enable_if<( is_slice<SliceType>::value ), int>::type* =
0 )
: _grid( grid_min[0], grid_min[1], grid_min[2], grid_max[0],
grid_max[1], grid_max[2], grid_delta[0], grid_delta[1],
grid_delta[2] )
, _cell_stencil( neighborhood_radius, cell_size_ratio, grid_min, grid_max )
, _cell_stencil( neighborhood_radius, cell_size_ratio, grid_min,
grid_max )
, _sorted( false )
{
std::size_t np = positions.size();
Expand Down Expand Up @@ -187,16 +253,16 @@ class LinkedCellList
const typename SliceType::value_type grid_delta[3],
const typename SliceType::value_type grid_min[3],
const typename SliceType::value_type grid_max[3],
const Scalar neighborhood_radius,
const Scalar cell_size_ratio = 1,
// const typename SliceType::value_type neighborhood_radius,
// const typename SliceType::value cell_size_ratio = 1,
const Scalar neighborhood_radius, const Scalar cell_size_ratio = 1,
// const typename SliceType::value_type neighborhood_radius,
// const typename SliceType::value cell_size_ratio = 1,
typename std::enable_if<( is_slice<SliceType>::value ), int>::type* =
0 )
: _grid( grid_min[0], grid_min[1], grid_min[2], grid_max[0],
grid_max[1], grid_max[2], grid_delta[0], grid_delta[1],
grid_delta[2] )
, _cell_stencil( neighborhood_radius, cell_size_ratio, grid_min, grid_max )
, _cell_stencil( neighborhood_radius, cell_size_ratio, grid_min,
grid_max )
, _sorted( false )
{
allocate( totalBins(), end - begin );
Expand Down Expand Up @@ -443,7 +509,7 @@ class LinkedCellList
/*!
\brief Get the bin cell index for each binned particle.
*/
auto getBins() const
auto getParticleBins() const
{
Kokkos::parallel_for(
"Cabana::LinkedCellList::storeBinIndices",
Expand Down Expand Up @@ -487,10 +553,11 @@ class LinkedCellList
auto sorted() const { return _sorted; }

KOKKOS_INLINE_FUNCTION
void getStencilCells( const int cell, int& imin, int& imax, int& jmin, int& jmax,
int& kmin, int& kmax ) const
void getStencilCells( const int cell, int& imin, int& imax, int& jmin,
int& jmax, int& kmin, int& kmax ) const
{
_cell_stencil.getStencilCells( cell, imin, imax, jmin, jmax, kmin, kmax );
_cell_stencil.getStencilCells( cell, imin, imax, jmin, jmax, kmin,
kmax );
}

private:
Expand Down Expand Up @@ -533,7 +600,7 @@ struct is_linked_cell_list_impl : public std::false_type
};

template <typename MemorySpace, typename Scalar>
struct is_linked_cell_list_impl<LinkedCellList<MemorySpace,Scalar>>
struct is_linked_cell_list_impl<LinkedCellList<MemorySpace, Scalar>>
: public std::true_type
{
};
Expand Down Expand Up @@ -599,7 +666,7 @@ void permute(
//---------------------------------------------------------------------------//
//! LinkedCellList NeighborList interface.
template <class DeviceType, typename Scalar>
class NeighborList<LinkedCellList<DeviceType,Scalar>>
class NeighborList<LinkedCellList<DeviceType, Scalar>>
{
public:
//! Kokkos memory space.
Expand All @@ -626,8 +693,8 @@ class NeighborList<LinkedCellList<DeviceType,Scalar>>
{
int total_count = 0;
int imin, imax, jmin, jmax, kmin, kmax;
list.getStencilCells( cell( particle_index ), imin, imax, jmin, jmax, kmin,
kmax );
list.getStencilCells( cell( particle_index ), imin, imax, jmin, jmax,
kmin, kmax );

// Loop over the cell stencil.
for ( int i = imin; i < imax; ++i )
Expand Down
10 changes: 6 additions & 4 deletions core/src/Cabana_Parallel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,8 @@ struct LinkedCellParallelFor
KOKKOS_FUNCTION void operator()( SerialOpTag, const index_type i ) const
{
int imin, imax, jmin, jmax, kmin, kmax;
_list.getStencilCells( _cells( i ), imin, imax, jmin, jmax, kmin, kmax );
_list.getStencilCells( _cells( i ), imin, imax, jmin, jmax, kmin,
kmax );

// Loop over the cell stencil.
for ( int gi = imin; gi < imax; ++gi )
Expand Down Expand Up @@ -1211,7 +1212,8 @@ struct LinkedCellParallelFor
{
index_type i = team.league_rank() + _begin;
int imin, imax, jmin, jmax, kmin, kmax;
_list.getStencilCells( _cells( i ), imin, imax, jmin, jmax, kmin, kmax );
_list.getStencilCells( _cells( i ), imin, imax, jmin, jmax, kmin,
kmax );

// Loop over the cell stencil.
for ( int gi = imin; gi < imax; ++gi )
Expand Down Expand Up @@ -1304,7 +1306,7 @@ inline void neighbor_parallel_for(

static_assert( is_accessible_from<memory_space, execution_space>{}, "" );

auto particle_bins = list.getBins();
auto particle_bins = list.getParticleBins();

LinkedCellParallelFor<work_tag, FunctorType, linear_policy_type,
LinkedCellType, typename LinkedCellType::CountView>
Expand Down Expand Up @@ -1372,7 +1374,7 @@ inline void neighbor_parallel_for(

static_assert( is_accessible_from<memory_space, execution_space>{}, "" );

auto particle_bins = list.getBins();
auto particle_bins = list.getParticleBins();

LinkedCellParallelFor<work_tag, FunctorType, team_policy_type,
LinkedCellType, typename LinkedCellType::CountView>
Expand Down
16 changes: 10 additions & 6 deletions core/src/Cabana_VerletList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ struct VerletListBuilder

// Get the stencil for this cell.
int imin, imax, jmin, jmax, kmin, kmax;
linked_cell_list.getStencilCells( cell, imin, imax, jmin, jmax, kmin, kmax );
linked_cell_list.getStencilCells( cell, imin, imax, jmin, jmax, kmin,
kmax );

// Operate on the particles in the bin.
std::size_t b_offset = bin_data_1d.binOffset( cell );
Expand All @@ -236,8 +237,9 @@ struct VerletListBuilder
{
// See if we should actually check this box for
// neighbors.
if ( linked_cell_list.cell_stencil().grid.minDistanceToPoint(
x_p, y_p, z_p, i, j, k ) <= rsqr )
if ( linked_cell_list.cell_stencil()
.grid.minDistanceToPoint(
x_p, y_p, z_p, i, j, k ) <= rsqr )
{
std::size_t n_offset =
linked_cell_list.binOffset( i, j, k );
Expand Down Expand Up @@ -425,7 +427,8 @@ struct VerletListBuilder

// Get the stencil for this cell.
int imin, imax, jmin, jmax, kmin, kmax;
linked_cell_list.getStencilCells( cell, imin, imax, jmin, jmax, kmin, kmax );
linked_cell_list.getStencilCells( cell, imin, imax, jmin, jmax, kmin,
kmax );

// Operate on the particles in the bin.
std::size_t b_offset = bin_data_1d.binOffset( cell );
Expand All @@ -451,8 +454,9 @@ struct VerletListBuilder
{
// See if we should actually check this box for
// neighbors.
if ( linked_cell_list.cell_stencil().grid.minDistanceToPoint(
x_p, y_p, z_p, i, j, k ) <= rsqr )
if ( linked_cell_list.cell_stencil()
.grid.minDistanceToPoint(
x_p, y_p, z_p, i, j, k ) <= rsqr )
{
// Check the particles in this bin to see if
// they are neighbors.
Expand Down
82 changes: 81 additions & 1 deletion core/unit_test/tstLinkedCellList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,84 @@ void checkLinkedCell( const LCLTestData test_data,
}
}

//---------------------------------------------------------------------------//
// Linked cell list cell stencil test.

void testLinkedCellStencil()
{
// Point in the middle
{
double min[3] = { 0.0, 0.0, 0.0 };
double max[3] = { 10.0, 10.0, 10.0 };
double radius = 1.0;
double ratio = 1.0;
Cabana::LinkedCellStencil<double> stencil( radius, ratio, min, max );

double xp = 4.5;
double yp = 5.5;
double zp = 3.5;
int ic, jc, kc;
stencil.grid.locatePoint( xp, yp, zp, ic, jc, kc );
int cell = stencil.grid.cardinalCellIndex( ic, jc, kc );
int imin, imax, jmin, jmax, kmin, kmax;
stencil.getStencilCells( cell, imin, imax, jmin, jmax, kmin, kmax );
EXPECT_EQ( imin, 3 );
EXPECT_EQ( imax, 6 );
EXPECT_EQ( jmin, 4 );
EXPECT_EQ( jmax, 7 );
EXPECT_EQ( kmin, 2 );
EXPECT_EQ( kmax, 5 );
}

// Point in the lower right corner
{
double min[3] = { 0.0, 0.0, 0.0 };
double max[3] = { 10.0, 10.0, 10.0 };
double radius = 1.0;
double ratio = 1.0;
Cabana::LinkedCellStencil<double> stencil( radius, ratio, min, max );

double xp = 0.5;
double yp = 0.5;
double zp = 0.5;
int ic, jc, kc;
stencil.grid.locatePoint( xp, yp, zp, ic, jc, kc );
int cell = stencil.grid.cardinalCellIndex( ic, jc, kc );
int imin, imax, jmin, jmax, kmin, kmax;
stencil.getStencilCells( cell, imin, imax, jmin, jmax, kmin, kmax );
EXPECT_EQ( imin, 0 );
EXPECT_EQ( imax, 2 );
EXPECT_EQ( jmin, 0 );
EXPECT_EQ( jmax, 2 );
EXPECT_EQ( kmin, 0 );
EXPECT_EQ( kmax, 2 );
}

// Point in the upper left corner
{
double min[3] = { 0.0, 0.0, 0.0 };
double max[3] = { 10.0, 10.0, 10.0 };
double radius = 1.0;
double ratio = 1.0;
Cabana::LinkedCellStencil<double> stencil( radius, ratio, min, max );

double xp = 9.5;
double yp = 9.5;
double zp = 9.5;
int ic, jc, kc;
stencil.grid.locatePoint( xp, yp, zp, ic, jc, kc );
int cell = stencil.grid.cardinalCellIndex( ic, jc, kc );
int imin, imax, jmin, jmax, kmin, kmax;
stencil.getStencilCells( cell, imin, imax, jmin, jmax, kmin, kmax );
EXPECT_EQ( imin, 8 );
EXPECT_EQ( imax, 10 );
EXPECT_EQ( jmin, 8 );
EXPECT_EQ( jmax, 10 );
EXPECT_EQ( kmin, 8 );
EXPECT_EQ( kmax, 10 );
}
}

//---------------------------------------------------------------------------//
void testLinkedList()
{
Expand All @@ -262,7 +340,7 @@ void testLinkedList()
auto end = test_data.end;
Cabana::LinkedCellList<TEST_MEMSPACE, double> cell_list(
pos, begin, end, grid_delta, grid_min, grid_max,
test_data.grid_delta[0] );
test_data.grid_delta[0], 1.0 );
Cabana::permute( cell_list, test_data.aosoa );

copyListToHost( test_data, cell_list );
Expand Down Expand Up @@ -438,6 +516,8 @@ void testLinkedCellParallel()
//---------------------------------------------------------------------------//
// RUN TESTS
//---------------------------------------------------------------------------//
TEST( TEST_CATEGORY, linked_cell_stencil_test ) { testLinkedCellStencil(); }

TEST( TEST_CATEGORY, linked_list_test ) { testLinkedList(); }

TEST( TEST_CATEGORY, linked_list_slice_test ) { testLinkedListSlice(); }
Expand Down

0 comments on commit c92e183

Please sign in to comment.