Skip to content

Commit d4d9214

Browse files
authored
PC: SoA Name Helpers (#4300)
## Summary Add name to index getters and query (has) functions to SoA names. Ported over from ImpactX. - [x] Rebase after #4299 was merged ## Additional background BLAST-ImpactX/impactx#805 ## Checklist The proposed changes: - [ ] fix a bug or incorrect behavior in AMReX - [x] add new capabilities to AMReX - [ ] changes answers in the test suite to more than roundoff level - [ ] are likely to significantly affect the results of downstream AMReX users - [ ] include documentation in the code and/or rst files, if appropriate
1 parent afa9510 commit d4d9214

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

Src/Particle/AMReX_ParticleContainer.H

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,38 @@ public:
14531453
/** Get the names for the int SoA components **/
14541454
std::vector<std::string> GetIntSoANames () const {return m_soa_idata_names;}
14551455

1456+
/** Check if a container has a ParticleReal component
1457+
*
1458+
* @param name component name to check
1459+
* @return true if found, else false
1460+
*/
1461+
bool HasRealComp (std::string const & name);
1462+
1463+
/** Check if a container has an Integer component
1464+
*
1465+
* @param name component name to check
1466+
* @return true if found, else false
1467+
*/
1468+
bool HasIntComp (std::string const & name);
1469+
1470+
/** Get the ParticleReal SoA index of a component
1471+
*
1472+
* This throws a runtime exception if the component does not exist.
1473+
*
1474+
* @param name component name to query index for
1475+
* @return zero-based index
1476+
*/
1477+
int GetRealCompIndex (std::string const & name);
1478+
1479+
/** Get the Integer SoA index of a component
1480+
*
1481+
* This throws a runtime exception if the component does not exist.
1482+
*
1483+
* @param name component name to query index for
1484+
* @return zero-based index
1485+
*/
1486+
int GetIntCompIndex (std::string const & name);
1487+
14561488
protected:
14571489

14581490
template <class RTYPE>

Src/Particle/AMReX_ParticleContainerI.H

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include <AMReX_MakeParticle.H>
22

3+
#include <algorithm>
4+
#include <iterator>
35
#include <set>
6+
#include <stdexcept>
47
#include <string>
58
#include <type_traits>
69
#include <vector>
@@ -82,10 +85,15 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
8285
}
8386
}
8487

85-
template <typename ParticleType, int NArrayReal, int NArrayInt,
86-
template<class> class Allocator, class CellAssignor>
88+
template<
89+
typename ParticleType,
90+
int NArrayReal,
91+
int NArrayInt,
92+
template<class> class Allocator,
93+
class CellAssignor
94+
>
8795
void
88-
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor> :: SetSoACompileTimeNames (
96+
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::SetSoACompileTimeNames (
8997
std::vector<std::string> const & rdata_name, std::vector<std::string> const & idata_name
9098
)
9199
{
@@ -108,6 +116,71 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
108116
}
109117
}
110118

119+
template<
120+
typename ParticleType,
121+
int NArrayReal,
122+
int NArrayInt,
123+
template<class> class Allocator,
124+
class CellAssignor
125+
>
126+
bool
127+
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::HasRealComp (std::string const & name)
128+
{
129+
return std::find(m_soa_rdata_names.begin(), m_soa_rdata_names.end(), name) != std::end(m_soa_rdata_names);
130+
}
131+
132+
template <typename ParticleType, int NArrayReal, int NArrayInt,
133+
template<class> class Allocator, class CellAssignor>
134+
bool
135+
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::HasIntComp (std::string const & name)
136+
{
137+
return std::find(m_soa_idata_names.begin(), m_soa_idata_names.end(), name) != std::end(m_soa_idata_names);
138+
}
139+
140+
template<
141+
typename ParticleType,
142+
int NArrayReal,
143+
int NArrayInt,
144+
template<class> class Allocator,
145+
class CellAssignor
146+
>
147+
int
148+
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::GetRealCompIndex (std::string const & name)
149+
{
150+
const auto it = std::find(m_soa_rdata_names.begin(), m_soa_rdata_names.end(), name);
151+
152+
if (it == m_soa_rdata_names.end())
153+
{
154+
throw std::runtime_error("GetRealCompIndex: Component " + name + " does not exist!");
155+
}
156+
else
157+
{
158+
return std::distance(m_soa_rdata_names.begin(), it);
159+
}
160+
}
161+
162+
template<
163+
typename ParticleType,
164+
int NArrayReal,
165+
int NArrayInt,
166+
template<class> class Allocator,
167+
class CellAssignor
168+
>
169+
int
170+
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::GetIntCompIndex (std::string const & name)
171+
{
172+
const auto it = std::find(m_soa_idata_names.begin(), m_soa_idata_names.end(), name);
173+
174+
if (it == m_soa_idata_names.end())
175+
{
176+
throw std::runtime_error("GetIntCompIndex: Component " + name + " does not exist!");
177+
}
178+
else
179+
{
180+
return std::distance(m_soa_idata_names.begin(), it);
181+
}
182+
}
183+
111184
template <typename ParticleType, int NArrayReal, int NArrayInt,
112185
template<class> class Allocator, class CellAssignor>
113186
template <typename P, typename Assignor>

0 commit comments

Comments
 (0)