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//---------------------------------------------------------------------------//
54template <class Tag>
56
58template <>
60{
61 public:
70 KOKKOS_INLINE_FUNCTION
71 static bool isValid( const std::size_t p, const double, const double,
72 const double, const std::size_t n, const double,
73 const double, const double )
74 {
75 return ( p != n );
76 }
77};
78
80template <>
82{
83 public:
95 KOKKOS_INLINE_FUNCTION static bool
96 isValid( const std::size_t p, const double xp, const double yp,
97 const double zp, const std::size_t n, const double xn,
98 const double yn, const double zn )
99 {
100 return ( ( p != n ) &&
101 ( ( xn > xp ) ||
102 ( ( xn == xp ) &&
103 ( ( yn > yp ) || ( ( yn == yp ) && ( zn > zp ) ) ) ) ) );
104 }
105};
106
107//---------------------------------------------------------------------------//
112template <class NeighborListType>
114{
115 public:
117 using memory_space = typename NeighborListType::memory_space;
118
120 KOKKOS_INLINE_FUNCTION
121 static std::size_t totalNeighbor( const NeighborListType& list );
122
124 KOKKOS_INLINE_FUNCTION
125 static std::size_t maxNeighbor( const NeighborListType& list );
126
128 KOKKOS_INLINE_FUNCTION
129 static std::size_t numNeighbor( const NeighborListType& list,
130 const std::size_t particle_index );
131
133 KOKKOS_INLINE_FUNCTION
134 static std::size_t getNeighbor( const NeighborListType& list,
135 const std::size_t particle_index,
136 const std::size_t neighbor_index );
137
139 KOKKOS_INLINE_FUNCTION
140 std::size_t setNeighbor( NeighborListType& list,
141 const std::size_t particle_index,
142 const std::size_t neighbor_index );
143};
144
145//---------------------------------------------------------------------------//
146
147namespace Impl
148{
150template <class ListType>
151KOKKOS_INLINE_FUNCTION std::size_t
152totalNeighbor( const ListType& list, const std::size_t num_particles )
153{
154 std::size_t total_n = 0;
155 // Sum neighbors across all particles.
156 for ( std::size_t p = 0; p < num_particles; p++ )
157 total_n += NeighborList<ListType>::numNeighbor( list, p );
158 return total_n;
159}
160
162template <class ListType>
163KOKKOS_INLINE_FUNCTION std::size_t
164maxNeighbor( const ListType& list, const std::size_t num_particles )
165{
166 std::size_t max_n = 0;
167 for ( std::size_t p = 0; p < num_particles; p++ )
168 if ( NeighborList<ListType>::numNeighbor( list, p ) > max_n )
169 max_n = NeighborList<ListType>::numNeighbor( list, p );
170 return max_n;
171}
172} // namespace Impl
173
182template <class ExecutionSpace, class ListType>
183Kokkos::View<int* [2], typename ListType::memory_space>
184neighborHistogram( ExecutionSpace exec_space, const std::size_t num_particles,
185 const ListType& list, const int num_bin )
186{
187 // Allocate View of neighbors per particle
188 auto num_neigh = Kokkos::View<int*, typename ListType::memory_space>(
189 "particle_count", num_particles );
190
191 // Extract from neighbor list interface.
192 auto extract_functor = KOKKOS_LAMBDA( const int p )
193 {
194 num_neigh( p ) = NeighborList<ListType>::numNeighbor( list, p );
195 };
196 Kokkos::RangePolicy<ExecutionSpace> particle_policy( exec_space, 0,
197 num_particles );
198 Kokkos::parallel_for( particle_policy, extract_functor );
199 Kokkos::fence();
200
201 auto bin_data = Cabana::binByKey( num_neigh, num_bin );
202
203 auto histogram = Kokkos::View<int* [2], typename ListType::memory_space>(
204 "particle_count", num_bin, 2 );
205 auto histogram_functor = KOKKOS_LAMBDA( const int b )
206 {
207 int max_neigh = NeighborList<ListType>::maxNeighbor( list );
208 double bin_width =
209 static_cast<double>( max_neigh ) / static_cast<double>( num_bin );
210 if ( num_bin > max_neigh )
211 bin_width = 1;
212 // Wait to cast back to int to get the actual bin edge.
213 histogram( b, 0 ) = static_cast<int>( ( b + 1 ) * bin_width );
214 histogram( b, 1 ) = bin_data.binSize( b );
215 };
216 Kokkos::RangePolicy<ExecutionSpace> bin_policy( exec_space, 0, num_bin );
217 Kokkos::parallel_for( bin_policy, histogram_functor );
218 Kokkos::fence();
219
220 return histogram;
221}
222
223} // end namespace Cabana
224
225#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:164
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:152
Sorting and binning built on Kokkos BinSort.
Tag for full neighbor lists.
Definition Cabana_NeighborList.hpp:36
Tag for 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:71
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:96
Neighborhood discriminator.
Definition Cabana_NeighborList.hpp:55
Neighbor list interface. Provides an interface callable at the functor level that gives access to nei...
Definition Cabana_NeighborList.hpp:114
typename NeighborListType::memory_space memory_space
Kokkos memory space.
Definition Cabana_NeighborList.hpp:117
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.
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:418
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:184