Cabana 0.8.0-dev
 
Loading...
Searching...
No Matches
Cabana_NeighborList.hpp
Go to the documentation of this file.
1/****************************************************************************
2 * Copyright (c) 2018-2023 by the Cabana authors *
3 * All rights reserved. *
4 * *
5 * This file is part of the Cabana library. Cabana is distributed under a *
6 * BSD 3-clause license. For the licensing terms see the LICENSE file in *
7 * the top-level directory. *
8 * *
9 * SPDX-License-Identifier: BSD-3-Clause *
10 ****************************************************************************/
11
16#ifndef CABANA_NEIGHBORLIST_HPP
17#define CABANA_NEIGHBORLIST_HPP
18
19#include <Kokkos_Core.hpp>
20
21#include <Cabana_Sort.hpp>
22
23namespace Cabana
24{
25//---------------------------------------------------------------------------//
26// Neighbor List Interface
27//---------------------------------------------------------------------------//
36{
37};
38
39//---------------------------------------------------------------------------//
49{
50};
51
52//---------------------------------------------------------------------------//
57{
58};
59
60//---------------------------------------------------------------------------//
62template <class Tag>
64
68template <>
70{
71 public:
78 KOKKOS_INLINE_FUNCTION
79 static bool isValid( const std::size_t p, const std::size_t n )
80 {
81 return ( p != n );
82 }
83};
84
86template <>
88{
89 public:
98 template <std::size_t NumSpaceDim>
99 KOKKOS_INLINE_FUNCTION static bool
100 isValid( const std::size_t p, const Kokkos::Array<double, NumSpaceDim>,
101 const std::size_t n, const Kokkos::Array<double, NumSpaceDim> )
102 {
103 return ( p != n );
104 }
105
114 KOKKOS_INLINE_FUNCTION
115 static bool isValid( const std::size_t p, const double, const double,
116 const double, const std::size_t n, const double,
117 const double, const double )
118 {
119 return ( p != n );
120 }
121};
122
124template <>
126{
127 public:
139 template <std::size_t NumSpaceDim>
140 KOKKOS_INLINE_FUNCTION static bool
141 isValid( const std::size_t p, const Kokkos::Array<double, NumSpaceDim> xp,
142 const std::size_t n, const Kokkos::Array<double, NumSpaceDim> xn )
143 {
144 return ( ( p != n ) &&
145 ( ( xn[0] > xp[0] ) ||
146 ( ( xn[0] == xp[0] ) &&
147 ( ( xn[1] > xp[1] ) ||
148 ( ( xn[1] == xp[1] ) && ( xn[2] > xp[2] ) ) ) ) ) );
149 }
150
162 KOKKOS_INLINE_FUNCTION static bool
163 isValid( const std::size_t p, const Kokkos::Array<double, 3> xp,
164 const std::size_t n, const Kokkos::Array<double, 3> xn )
165 {
166 return ( ( p != n ) &&
167 ( ( xn[0] > xp[0] ) ||
168 ( ( xn[0] == xp[0] ) &&
169 ( ( xn[1] > xp[1] ) ||
170 ( ( xn[1] == xp[1] ) && ( xn[2] > xp[2] ) ) ) ) ) );
171 }
172
184 KOKKOS_INLINE_FUNCTION static bool
185 isValid( const std::size_t p, const Kokkos::Array<double, 2> xp,
186 const std::size_t n, const Kokkos::Array<double, 2> xn )
187 {
188 return ( ( p != n ) &&
189 ( ( xn[0] > xp[0] ) ||
190 ( ( xn[0] == xp[0] ) && ( ( xn[1] > xp[1] ) ) ) ) );
191 }
192
204 KOKKOS_INLINE_FUNCTION static bool
205 isValid( const std::size_t p, const double xp, const double yp,
206 const double zp, const std::size_t n, const double xn,
207 const double yn, const double zn )
208 {
209 return ( ( p != n ) &&
210 ( ( xn > xp ) ||
211 ( ( xn == xp ) &&
212 ( ( yn > yp ) || ( ( yn == yp ) && ( zn > zp ) ) ) ) ) );
213 }
214};
215
216//---------------------------------------------------------------------------//
221template <class NeighborListType>
223{
224 public:
226 using memory_space = typename NeighborListType::memory_space;
227
229 KOKKOS_INLINE_FUNCTION
230 static std::size_t totalNeighbor( const NeighborListType& list );
231
233 KOKKOS_INLINE_FUNCTION
234 static std::size_t maxNeighbor( const NeighborListType& list );
235
237 KOKKOS_INLINE_FUNCTION
238 static std::size_t numNeighbor( const NeighborListType& list,
239 const std::size_t particle_index );
240
242 KOKKOS_INLINE_FUNCTION
243 static std::size_t getNeighbor( const NeighborListType& list,
244 const std::size_t particle_index,
245 const std::size_t neighbor_index );
246
248 KOKKOS_INLINE_FUNCTION
249 std::size_t setNeighbor( NeighborListType& list,
250 const std::size_t particle_index,
251 const std::size_t neighbor_index );
252};
253
254//---------------------------------------------------------------------------//
255
256namespace Impl
257{
259template <class ListType>
260KOKKOS_INLINE_FUNCTION std::size_t
261totalNeighbor( const ListType& list, const std::size_t num_particles )
262{
263 std::size_t total_n = 0;
264 // Sum neighbors across all particles.
265 for ( std::size_t p = 0; p < num_particles; p++ )
266 total_n += NeighborList<ListType>::numNeighbor( list, p );
267 return total_n;
268}
269
271template <class ListType>
272KOKKOS_INLINE_FUNCTION std::size_t
273maxNeighbor( const ListType& list, const std::size_t num_particles )
274{
275 std::size_t max_n = 0;
276 for ( std::size_t p = 0; p < num_particles; p++ )
277 if ( NeighborList<ListType>::numNeighbor( list, p ) > max_n )
278 max_n = NeighborList<ListType>::numNeighbor( list, p );
279 return max_n;
280}
281} // namespace Impl
282
291template <class ExecutionSpace, class ListType>
292Kokkos::View<int* [2], typename ListType::memory_space>
293neighborHistogram( ExecutionSpace exec_space, const std::size_t num_particles,
294 const ListType& list, const int num_bin )
295{
296 // Allocate View of neighbors per particle
297 auto num_neigh = Kokkos::View<int*, typename ListType::memory_space>(
298 "particle_count", num_particles );
299
300 // Extract from neighbor list interface.
301 auto extract_functor = KOKKOS_LAMBDA( const int p )
302 {
303 num_neigh( p ) = NeighborList<ListType>::numNeighbor( list, p );
304 };
305 Kokkos::RangePolicy<ExecutionSpace> particle_policy( exec_space, 0,
306 num_particles );
307 Kokkos::parallel_for( "Cabana::NeighborList::neighborHistogram::extract",
308 particle_policy, extract_functor );
309 Kokkos::fence();
310
311 auto bin_data = Cabana::binByKey( num_neigh, num_bin );
312
313 auto histogram = Kokkos::View<int* [2], typename ListType::memory_space>(
314 "particle_count", num_bin, 2 );
315 auto histogram_functor = KOKKOS_LAMBDA( const int b )
316 {
317 int max_neigh = NeighborList<ListType>::maxNeighbor( list );
318 double bin_width =
319 static_cast<double>( max_neigh ) / static_cast<double>( num_bin );
320 if ( num_bin > max_neigh )
321 bin_width = 1;
322 // Wait to cast back to int to get the actual bin edge.
323 histogram( b, 0 ) = static_cast<int>( ( b + 1 ) * bin_width );
324 histogram( b, 1 ) = bin_data.binSize( b );
325 };
326 Kokkos::RangePolicy<ExecutionSpace> bin_policy( exec_space, 0, num_bin );
327 Kokkos::parallel_for( "Cabana::NeighborList::neighborHistogram", bin_policy,
328 histogram_functor );
329 Kokkos::fence();
330
331 return histogram;
332}
333
334} // end namespace Cabana
335
336#endif // end CABANA_NEIGHBORLIST_HPP
KOKKOS_INLINE_FUNCTION std::size_t maxNeighbor(const ListType &list, const std::size_t num_particles)
Iterate to find the maximum number of neighbors.
Definition Cabana_NeighborList.hpp:273
KOKKOS_INLINE_FUNCTION std::size_t totalNeighbor(const ListType &list, const std::size_t num_particles)
Iterate to get the total number of neighbors.
Definition Cabana_NeighborList.hpp:261
Sorting and binning built on Kokkos BinSort.
Tag for building full neighbor lists.
Definition Cabana_NeighborList.hpp:36
Tag for building half neighbor lists.
Definition Cabana_NeighborList.hpp:49
static KOKKOS_INLINE_FUNCTION bool isValid(const std::size_t p, const double, const double, const double, const std::size_t n, const double, const double, const double)
Check whether neighbor pair is valid.
Definition Cabana_NeighborList.hpp:115
static KOKKOS_INLINE_FUNCTION bool isValid(const std::size_t p, const Kokkos::Array< double, NumSpaceDim >, const std::size_t n, const Kokkos::Array< double, NumSpaceDim >)
Check whether neighbor pair is valid.
Definition Cabana_NeighborList.hpp:100
static KOKKOS_INLINE_FUNCTION bool isValid(const std::size_t p, const Kokkos::Array< double, NumSpaceDim > xp, const std::size_t n, const Kokkos::Array< double, NumSpaceDim > xn)
Check whether neighbor pair is valid.
Definition Cabana_NeighborList.hpp:141
static KOKKOS_INLINE_FUNCTION bool isValid(const std::size_t p, const Kokkos::Array< double, 3 > xp, const std::size_t n, const Kokkos::Array< double, 3 > xn)
Check whether neighbor pair is valid.
Definition Cabana_NeighborList.hpp:163
static KOKKOS_INLINE_FUNCTION bool isValid(const std::size_t p, const Kokkos::Array< double, 2 > xp, const std::size_t n, const Kokkos::Array< double, 2 > xn)
Check whether neighbor pair is valid.
Definition Cabana_NeighborList.hpp:185
static KOKKOS_INLINE_FUNCTION bool isValid(const std::size_t p, const double xp, const double yp, const double zp, const std::size_t n, const double xn, const double yn, const double zn)
Check whether neighbor pair is valid.
Definition Cabana_NeighborList.hpp:205
static KOKKOS_INLINE_FUNCTION bool isValid(const std::size_t p, const std::size_t n)
Check whether neighbor pair is valid.
Definition Cabana_NeighborList.hpp:79
Neighborhood discriminator.
Definition Cabana_NeighborList.hpp:63
Neighbor list interface. Provides an interface callable at the functor level that gives access to nei...
Definition Cabana_NeighborList.hpp:223
typename NeighborListType::memory_space memory_space
Kokkos memory space.
Definition Cabana_NeighborList.hpp:226
KOKKOS_INLINE_FUNCTION std::size_t setNeighbor(NeighborListType &list, const std::size_t particle_index, const std::size_t neighbor_index)
Set the id for a neighbor for a given particle index and neighbor index.
static KOKKOS_INLINE_FUNCTION std::size_t numNeighbor(const NeighborListType &list, const std::size_t particle_index)
Get the number of neighbors for a given particle index.
static KOKKOS_INLINE_FUNCTION std::size_t getNeighbor(const NeighborListType &list, const std::size_t particle_index, const std::size_t neighbor_index)
Get the id for a neighbor for a given particle index and neighbor index.
static KOKKOS_INLINE_FUNCTION std::size_t totalNeighbor(const NeighborListType &list)
Get the total number of neighbors across all particles.
static KOKKOS_INLINE_FUNCTION std::size_t maxNeighbor(const NeighborListType &list)
Get the maximum number of neighbors across all particles.
Tag for neighbor list iteration, ignoring only self neighbors.
Definition Cabana_NeighborList.hpp:57
Core: particle data structures and algorithms.
Definition Cabana_AoSoA.hpp:36
auto binByKey(KeyViewType keys, const int nbin, const std::size_t begin, const std::size_t end, typename std::enable_if<(Kokkos::is_view< KeyViewType >::value), int >::type *=0)
Bin an AoSoA over a subset of its range based on the associated key values and number of bins....
Definition Cabana_Sort.hpp:413
Kokkos::View< int *[2], typename ListType::memory_space > neighborHistogram(ExecutionSpace exec_space, const std::size_t num_particles, const ListType &list, const int num_bin)
Construct a histogram of neighbors per particle.
Definition Cabana_NeighborList.hpp:293