Cabana 0.8.0-dev
 
Loading...
Searching...
No Matches
Cabana_ParticleList.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_PARTICLELIST_HPP
17#define CABANA_PARTICLELIST_HPP
18
19#include <Cabana_AoSoA.hpp>
20#include <Cabana_Fields.hpp>
22#include <Cabana_SoA.hpp>
23#include <Cabana_Tuple.hpp>
24
25#include <memory>
26#include <string>
27#include <type_traits>
28
29namespace Cabana
30{
31//---------------------------------------------------------------------------//
33template <class... FieldTags>
35{
37 using member_types = Cabana::MemberTypes<typename FieldTags::data_type...>;
38};
39
40//---------------------------------------------------------------------------//
42template <class... FieldTags>
44{
46 using traits = ParticleTraits<FieldTags...>;
50 static constexpr int vector_length = 1;
51
53 Particle() = default;
54
56 KOKKOS_FORCEINLINE_FUNCTION
58 : _tuple( tuple )
59 {
60 }
61
63 KOKKOS_FORCEINLINE_FUNCTION
64 tuple_type& tuple() { return _tuple; }
65
67 KOKKOS_FORCEINLINE_FUNCTION
68 const tuple_type& tuple() const { return _tuple; }
69
72};
73
74//---------------------------------------------------------------------------//
76template <int VectorLength, class... FieldTags>
78{
80 using traits = ParticleTraits<FieldTags...>;
84 static constexpr int vector_length = VectorLength;
85
87 ParticleView() = default;
88
90 KOKKOS_FORCEINLINE_FUNCTION
91 ParticleView( soa_type& soa, const int vector_index )
92 : _soa( soa )
93 , _vector_index( vector_index )
94 {
95 }
96
98 KOKKOS_FORCEINLINE_FUNCTION
99 soa_type& soa() { return _soa; }
100
102 KOKKOS_FORCEINLINE_FUNCTION
103 const soa_type& soa() const { return _soa; }
104
106 KOKKOS_FORCEINLINE_FUNCTION
107 int vectorIndex() const { return _vector_index; }
108
111
114};
115
116//---------------------------------------------------------------------------//
117// Particle accessors.
118//---------------------------------------------------------------------------//
120template <class FieldTag, class... FieldTags, class... IndexTypes>
121KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
122 sizeof...( IndexTypes ) == FieldTag::rank,
124 template member_const_reference_type<
125 TypeIndexer<FieldTag, FieldTags...>::index>>::type
126get( const Particle<FieldTags...>& particle, FieldTag, IndexTypes... indices )
127{
128 return Cabana::get<TypeIndexer<FieldTag, FieldTags...>::index>(
129 particle.tuple(), indices... );
130}
131
133template <class FieldTag, class... FieldTags, class... IndexTypes>
134KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
135 sizeof...( IndexTypes ) == FieldTag::rank,
136 typename Particle<FieldTags...>::tuple_type::template member_reference_type<
137 TypeIndexer<FieldTag, FieldTags...>::index>>::type
138get( Particle<FieldTags...>& particle, FieldTag, IndexTypes... indices )
139{
140 return Cabana::get<TypeIndexer<FieldTag, FieldTags...>::index>(
141 particle.tuple(), indices... );
142}
143
144//---------------------------------------------------------------------------//
146template <class FieldTag, class... FieldTags, class... IndexTypes,
147 int VectorLength>
148KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
149 sizeof...( IndexTypes ) == FieldTag::rank,
151 template member_const_reference_type<
152 TypeIndexer<FieldTag, FieldTags...>::index>>::type
153get( const ParticleView<VectorLength, FieldTags...>& particle, FieldTag,
154 IndexTypes... indices )
155{
156 return Cabana::get<TypeIndexer<FieldTag, FieldTags...>::index>(
157 particle.soa(), particle.vectorIndex(), indices... );
158}
159
161template <class FieldTag, class... FieldTags, class... IndexTypes,
162 int VectorLength>
163KOKKOS_FORCEINLINE_FUNCTION typename std::enable_if<
164 sizeof...( IndexTypes ) == FieldTag::rank,
165 typename ParticleView<VectorLength, FieldTags...>::soa_type::
166 template member_reference_type<
167 TypeIndexer<FieldTag, FieldTags...>::index>>::type
169 IndexTypes... indices )
170{
171 return Cabana::get<TypeIndexer<FieldTag, FieldTags...>::index>(
172 particle.soa(), particle.vectorIndex(), indices... );
173}
174
175//---------------------------------------------------------------------------//
177template <class MemorySpace, int VectorLength, class... FieldTags>
179{
180 public:
182 using memory_space = MemorySpace;
184 using traits = ParticleTraits<FieldTags...>;
195 template <std::size_t M>
196 using slice_type = typename aosoa_type::template member_slice_type<M>;
198 using particle_type = Particle<FieldTags...>;
202
204 ParticleList( const std::string& label )
205 : _aosoa( label )
206 {
207 }
208
211 : _aosoa( aosoa )
212 {
213 }
214
216 KOKKOS_INLINE_FUNCTION std::size_t size() const { return _aosoa.size(); }
218 void resize( const std::size_t n ) { _aosoa.resize( n ); }
219
221 aosoa_type& aosoa() { return _aosoa; }
223 KOKKOS_INLINE_FUNCTION const aosoa_type& aosoa() const { return _aosoa; }
224
226 template <class IndexType>
227 KOKKOS_INLINE_FUNCTION auto getParticle( IndexType p ) const
228 {
229 return particle_type( _aosoa.getTuple( p ) );
230 }
231
233 template <class ParticleType, class IndexType>
234 KOKKOS_INLINE_FUNCTION void setParticle( ParticleType particle,
235 IndexType p ) const
236 {
237 _aosoa.setTuple( p, particle.tuple() );
238 }
239
241 template <class IndexType>
242 KOKKOS_INLINE_FUNCTION auto getParticleView( IndexType p ) const
243 {
246 return particle_view_type( _aosoa.access( s ), a );
247 }
248
250 const std::string& label() const { return _aosoa.label(); }
251
253 template <class FieldTag>
254 slice_type<TypeIndexer<FieldTag, FieldTags...>::index>
255 slice( FieldTag ) const
256 {
257 return Cabana::slice<TypeIndexer<FieldTag, FieldTags...>::index>(
258 _aosoa, FieldTag::label() );
259 }
260
261 protected:
264};
265
266template <class>
267struct is_particle_list_impl : public std::false_type
268{
269};
270
271template <class MemorySpace, int VectorLength, class... FieldTags>
273 ParticleList<MemorySpace, VectorLength, FieldTags...>>
274 : public std::true_type
275{
276};
277
279template <class T>
281 : public is_particle_list_impl<typename std::remove_cv<T>::type>::type
282{
283};
284
285//---------------------------------------------------------------------------//
290template <class MemorySpace, int VectorLength, class... FieldTags>
291auto createParticleList( const std::string& label,
293{
294 return ParticleList<MemorySpace, VectorLength, FieldTags...>( label );
295}
296
301template <class MemorySpace, class... FieldTags>
302auto createParticleList( const std::string& label,
304{
305 return ParticleList<
306 MemorySpace,
308 typename MemorySpace::execution_space>::vector_length,
309 FieldTags...>( label );
310}
311
312//---------------------------------------------------------------------------//
313
314} // end namespace Cabana
315
316#endif // end CABANA_PARTICLELIST_HPP
Array-of-Struct-of-Arrays particle data structure.
Particle field types and common field examples.
AoSoA tuple member types.
Struct-of-Arrays for building AoSoA.
Tuple of single particle information to build AoSoA.
Array-of-Struct-of-Arrays.
Definition Cabana_AoSoA.hpp:121
Tuple< member_types > tuple_type
Definition Cabana_AoSoA.hpp:176
static constexpr int vector_length
Definition Cabana_AoSoA.hpp:152
static KOKKOS_FORCEINLINE_FUNCTION constexpr std::size_t a(const std::size_t i)
Given a tuple index get the AoSoA array index.
Definition Cabana_Index.hpp:77
static KOKKOS_FORCEINLINE_FUNCTION constexpr std::size_t s(const std::size_t i)
Given a tuple index get the AoSoA struct index.
Definition Cabana_Index.hpp:62
Default settings for execution spaces.
Definition Cabana_PerformanceTraits.hpp:32
List of particle fields stored in AoSoA.
Definition Cabana_ParticleList.hpp:179
ParticleList(const std::string &label)
Default constructor.
Definition Cabana_ParticleList.hpp:204
Particle< FieldTags... > particle_type
Single particle type.
Definition Cabana_ParticleList.hpp:198
typename aosoa_type::template member_slice_type< M > slice_type
Single field slice type.
Definition Cabana_ParticleList.hpp:196
aosoa_type _aosoa
Definition Cabana_ParticleList.hpp:263
ParticleList(const aosoa_type &aosoa)
Constructor from existing AoSoA.
Definition Cabana_ParticleList.hpp:210
KOKKOS_INLINE_FUNCTION const aosoa_type & aosoa() const
Get the AoSoA (const).
Definition Cabana_ParticleList.hpp:223
const std::string & label() const
Definition Cabana_ParticleList.hpp:250
KOKKOS_INLINE_FUNCTION auto getParticle(IndexType p) const
Get a single particle.
Definition Cabana_ParticleList.hpp:227
aosoa_type & aosoa()
Definition Cabana_ParticleList.hpp:221
KOKKOS_INLINE_FUNCTION auto getParticleView(IndexType p) const
Set a single particle with the struct+array indexing.
Definition Cabana_ParticleList.hpp:242
void resize(const std::size_t n)
Update the number of particles in the list.
Definition Cabana_ParticleList.hpp:218
ParticleTraits< FieldTags... > traits
AoSoA member field types.
Definition Cabana_ParticleList.hpp:184
typename aosoa_type::tuple_type tuple_type
Particle tuple type.
Definition Cabana_ParticleList.hpp:190
ParticleView< aosoa_type::vector_length, FieldTags... > particle_view_type
Single SoA type.
Definition Cabana_ParticleList.hpp:200
KOKKOS_INLINE_FUNCTION void setParticle(ParticleType particle, IndexType p) const
Set a single particle.
Definition Cabana_ParticleList.hpp:234
MemorySpace memory_space
Kokkos memory space.
Definition Cabana_ParticleList.hpp:182
typename traits::member_types member_types
AoSoA member types.
Definition Cabana_ParticleList.hpp:186
slice_type< TypeIndexer< FieldTag, FieldTags... >::index > slice(FieldTag) const
Get a slice of a given field.
Definition Cabana_ParticleList.hpp:255
Cabana::AoSoA< member_types, memory_space, VectorLength > aosoa_type
AoSoA type.
Definition Cabana_ParticleList.hpp:188
KOKKOS_INLINE_FUNCTION std::size_t size() const
Get the number of particles in the list.
Definition Cabana_ParticleList.hpp:216
Core: particle data structures and algorithms.
Definition Cabana_AoSoA.hpp:36
auto createParticleList(const std::string &label, ParticleTraits< FieldTags... >)
ParticleList creation function.
Definition Cabana_ParticleList.hpp:291
AoSoA_t::template member_slice_type< M > slice(const AoSoA_t &aosoa, const std::string &slice_label="")
Create a slice from an AoSoA.
Definition Cabana_AoSoA.hpp:77
KOKKOS_FORCEINLINE_FUNCTION std::enable_if< is_parameter_pack< ParameterPack_t >::value, typenameParameterPack_t::templatevalue_type< N > & >::type get(ParameterPack_t &pp)
Get an element from a parameter pack.
Definition Cabana_ParameterPack.hpp:129
General sequence of types for SoA and AoSoA member data.
Definition Cabana_MemberTypes.hpp:28
Extract AoSoA particle fields for ParticleList.
Definition Cabana_ParticleList.hpp:35
Cabana::MemberTypes< typename FieldTags::data_type... > member_types
AoSoA particle fields.
Definition Cabana_ParticleList.hpp:37
Single SoA particle view. Wraps a view of the SoA the particle resides in.
Definition Cabana_ParticleList.hpp:78
KOKKOS_FORCEINLINE_FUNCTION const soa_type & soa() const
Get the underlying SoA (const).
Definition Cabana_ParticleList.hpp:103
int _vector_index
Definition Cabana_ParticleList.hpp:113
ParticleTraits< FieldTags... > traits
Particle AoSoA member types.
Definition Cabana_ParticleList.hpp:80
KOKKOS_FORCEINLINE_FUNCTION int vectorIndex() const
Get the vector index of the particle in the SoA.
Definition Cabana_ParticleList.hpp:107
KOKKOS_FORCEINLINE_FUNCTION ParticleView(soa_type &soa, const int vector_index)
Tuple wrapper constructor.
Definition Cabana_ParticleList.hpp:91
static constexpr int vector_length
Definition Cabana_ParticleList.hpp:84
Cabana::SoA< typename traits::member_types, VectorLength > soa_type
Particle SoA type.
Definition Cabana_ParticleList.hpp:82
KOKKOS_FORCEINLINE_FUNCTION soa_type & soa()
Definition Cabana_ParticleList.hpp:99
ParticleView()=default
Default constructor.
soa_type & _soa
Definition Cabana_ParticleList.hpp:110
Single particle copy. Wraps a tuple copy of a particle.
Definition Cabana_ParticleList.hpp:44
KOKKOS_FORCEINLINE_FUNCTION const tuple_type & tuple() const
Get the underlying tuple (const).
Definition Cabana_ParticleList.hpp:68
KOKKOS_FORCEINLINE_FUNCTION Particle(const tuple_type &tuple)
Tuple wrapper constructor.
Definition Cabana_ParticleList.hpp:57
static constexpr int vector_length
Definition Cabana_ParticleList.hpp:50
Particle()=default
Default constructor.
Cabana::Tuple< typename traits::member_types > tuple_type
Particle tuple type.
Definition Cabana_ParticleList.hpp:48
tuple_type _tuple
Definition Cabana_ParticleList.hpp:71
ParticleTraits< FieldTags... > traits
Particle AoSoA member types.
Definition Cabana_ParticleList.hpp:46
KOKKOS_FORCEINLINE_FUNCTION tuple_type & tuple()
Definition Cabana_ParticleList.hpp:64
Definition Cabana_SoA.hpp:32
Definition Cabana_Tuple.hpp:32
Get the index of a field type within a particle type list.
Definition Cabana_Fields.hpp:122
Definition Cabana_ParticleList.hpp:268
ParticleList static type checker.
Definition Cabana_ParticleList.hpp:282