Skip to content

Commit

Permalink
PC: SoA Name Helpers (#4300)
Browse files Browse the repository at this point in the history
## 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

ECP-WarpX/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
  • Loading branch information
ax3l authored Jan 21, 2025
1 parent afa9510 commit d4d9214
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
32 changes: 32 additions & 0 deletions Src/Particle/AMReX_ParticleContainer.H
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,38 @@ public:
/** Get the names for the int SoA components **/
std::vector<std::string> GetIntSoANames () const {return m_soa_idata_names;}

/** Check if a container has a ParticleReal component
*
* @param name component name to check
* @return true if found, else false
*/
bool HasRealComp (std::string const & name);

/** Check if a container has an Integer component
*
* @param name component name to check
* @return true if found, else false
*/
bool HasIntComp (std::string const & name);

/** Get the ParticleReal SoA index of a component
*
* This throws a runtime exception if the component does not exist.
*
* @param name component name to query index for
* @return zero-based index
*/
int GetRealCompIndex (std::string const & name);

/** Get the Integer SoA index of a component
*
* This throws a runtime exception if the component does not exist.
*
* @param name component name to query index for
* @return zero-based index
*/
int GetIntCompIndex (std::string const & name);

protected:

template <class RTYPE>
Expand Down
79 changes: 76 additions & 3 deletions Src/Particle/AMReX_ParticleContainerI.H
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <AMReX_MakeParticle.H>

#include <algorithm>
#include <iterator>
#include <set>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -82,10 +85,15 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
}
}

template <typename ParticleType, int NArrayReal, int NArrayInt,
template<class> class Allocator, class CellAssignor>
template<
typename ParticleType,
int NArrayReal,
int NArrayInt,
template<class> class Allocator,
class CellAssignor
>
void
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor> :: SetSoACompileTimeNames (
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::SetSoACompileTimeNames (
std::vector<std::string> const & rdata_name, std::vector<std::string> const & idata_name
)
{
Expand All @@ -108,6 +116,71 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
}
}

template<
typename ParticleType,
int NArrayReal,
int NArrayInt,
template<class> class Allocator,
class CellAssignor
>
bool
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::HasRealComp (std::string const & name)
{
return std::find(m_soa_rdata_names.begin(), m_soa_rdata_names.end(), name) != std::end(m_soa_rdata_names);
}

template <typename ParticleType, int NArrayReal, int NArrayInt,
template<class> class Allocator, class CellAssignor>
bool
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::HasIntComp (std::string const & name)
{
return std::find(m_soa_idata_names.begin(), m_soa_idata_names.end(), name) != std::end(m_soa_idata_names);
}

template<
typename ParticleType,
int NArrayReal,
int NArrayInt,
template<class> class Allocator,
class CellAssignor
>
int
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::GetRealCompIndex (std::string const & name)
{
const auto it = std::find(m_soa_rdata_names.begin(), m_soa_rdata_names.end(), name);

if (it == m_soa_rdata_names.end())
{
throw std::runtime_error("GetRealCompIndex: Component " + name + " does not exist!");
}
else
{
return std::distance(m_soa_rdata_names.begin(), it);
}
}

template<
typename ParticleType,
int NArrayReal,
int NArrayInt,
template<class> class Allocator,
class CellAssignor
>
int
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>::GetIntCompIndex (std::string const & name)
{
const auto it = std::find(m_soa_idata_names.begin(), m_soa_idata_names.end(), name);

if (it == m_soa_idata_names.end())
{
throw std::runtime_error("GetIntCompIndex: Component " + name + " does not exist!");
}
else
{
return std::distance(m_soa_idata_names.begin(), it);
}
}

template <typename ParticleType, int NArrayReal, int NArrayInt,
template<class> class Allocator, class CellAssignor>
template <typename P, typename Assignor>
Expand Down

0 comments on commit d4d9214

Please sign in to comment.