Skip to content

Commit

Permalink
Add LinkedCell scalar template
Browse files Browse the repository at this point in the history
  • Loading branch information
streeve committed Nov 9, 2023
1 parent c92e183 commit 6afc76e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 45 deletions.
72 changes: 30 additions & 42 deletions core/src/Cabana_LinkedCellList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ namespace Cabana
template <class Scalar>
struct LinkedCellStencil
{
//! Cutoff squared.
Scalar rsqr;
//! Background grid.
Impl::CartesianGrid<double> grid;
Impl::CartesianGrid<Scalar> grid;
//! Maximum cells per dimension.
int max_cells_dir;
//! Maximum total cells.
Expand All @@ -53,10 +51,9 @@ struct LinkedCellStencil
LinkedCellStencil( const Scalar neighborhood_radius,
const Scalar cell_size_ratio, const Scalar grid_min[3],
const Scalar grid_max[3] )
: rsqr( neighborhood_radius * neighborhood_radius )
{
Scalar dx = neighborhood_radius * cell_size_ratio;
grid = Impl::CartesianGrid<double>(
grid = Impl::CartesianGrid<Scalar>(
grid_min[0], grid_min[1], grid_min[2], grid_max[0], grid_max[1],
grid_max[2], dx, dx, dx );
cell_range = std::ceil( 1 / cell_size_ratio );
Expand All @@ -66,8 +63,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 getCells( 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 All @@ -90,8 +87,9 @@ struct LinkedCellStencil
/*!
\brief Data describing the bin sizes and offsets resulting from a binning
operation on a 3d regular Cartesian grid.
\note Defaults to double precision for backwards compatibility.
*/
template <class MemorySpace, class Scalar>
template <class MemorySpace, class Scalar = double>
class LinkedCellList
{
public:
Expand All @@ -115,6 +113,8 @@ class LinkedCellList
using CountView = Kokkos::View<int*, memory_space>;
//! Offset view type.
using OffsetView = Kokkos::View<size_type*, memory_space>;
//! Stencil type.
using stencil_type = Cabana::LinkedCellStencil<Scalar>;

/*!
\brief Default constructor.
Expand All @@ -135,18 +135,16 @@ class LinkedCellList
\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 )
LinkedCellList( SliceType positions, const Scalar grid_delta[3],
const Scalar grid_min[3], const Scalar 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] )
, _cell_stencil( grid_delta[0], 1.0, grid_min, grid_max )
, _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 );
Expand All @@ -170,19 +168,17 @@ class LinkedCellList
\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 )
LinkedCellList( SliceType positions, const std::size_t begin,
const std::size_t end, const Scalar grid_delta[3],
const Scalar grid_min[3], const Scalar 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] )
, _cell_stencil( grid_delta[0], 1.0, grid_min, grid_max )
, _sorted( false )
{
_cell_stencil( grid_delta[0], 1.0, grid_min, grid_max );
allocate( totalBins(), end - begin );
build( positions, begin, end );
}
Expand All @@ -206,12 +202,9 @@ class LinkedCellList
*/
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],
SliceType positions, const Scalar grid_delta[3],
const Scalar grid_min[3], const Scalar 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,
typename std::enable_if<( is_slice<SliceType>::value ), int>::type* =
0 )
: _grid( grid_min[0], grid_min[1], grid_min[2], grid_max[0],
Expand Down Expand Up @@ -250,12 +243,9 @@ class LinkedCellList
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],
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 grid_delta[3], const Scalar grid_min[3],
const Scalar grid_max[3], const Scalar neighborhood_radius,
const Scalar 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],
Expand Down Expand Up @@ -371,10 +361,7 @@ class LinkedCellList
std::size_t rangeEnd() const { return _bin_data.rangeEnd(); }

KOKKOS_INLINE_FUNCTION
Cabana::LinkedCellStencil<Scalar> cell_stencil() const
{
return _cell_stencil;
}
stencil_type cell_stencil() const { return _cell_stencil; }

/*!
\brief Get the 1d bin data.
Expand Down Expand Up @@ -556,19 +543,20 @@ class LinkedCellList
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.getCells( cell, imin, imax, jmin, jmax, kmin, kmax );
}

private:
// Building the linked cell.
BinningData<MemorySpace> _bin_data;
Impl::CartesianGrid<double> _grid;
Impl::CartesianGrid<Scalar> _grid;

CountView _counts;
OffsetView _offsets;
OffsetView _permutes;

Cabana::LinkedCellStencil<Scalar> _cell_stencil;
// Iterating over the linked cell.
stencil_type _cell_stencil;

bool _sorted;
CountView _particle_bins;
Expand Down
6 changes: 3 additions & 3 deletions core/unit_test/tstLinkedCellList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ void testLinkedCellStencil()
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 );
stencil.getCells( cell, imin, imax, jmin, jmax, kmin, kmax );
EXPECT_EQ( imin, 3 );
EXPECT_EQ( imax, 6 );
EXPECT_EQ( jmin, 4 );
Expand All @@ -290,7 +290,7 @@ void testLinkedCellStencil()
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 );
stencil.getCells( cell, imin, imax, jmin, jmax, kmin, kmax );
EXPECT_EQ( imin, 0 );
EXPECT_EQ( imax, 2 );
EXPECT_EQ( jmin, 0 );
Expand All @@ -314,7 +314,7 @@ void testLinkedCellStencil()
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 );
stencil.getCells( cell, imin, imax, jmin, jmax, kmin, kmax );
EXPECT_EQ( imin, 8 );
EXPECT_EQ( imax, 10 );
EXPECT_EQ( jmin, 8 );
Expand Down

0 comments on commit 6afc76e

Please sign in to comment.