Skip to content

Commit

Permalink
PC: SoA Name Helpers
Browse files Browse the repository at this point in the history
Add name to index getters and query (has) functions to SoA
names. Ported over from ImpactX.
  • Loading branch information
ax3l committed Jan 18, 2025
1 parent 6152178 commit 5486f1d
Show file tree
Hide file tree
Showing 2 changed files with 100 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
71 changes: 68 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 @@ -80,10 +83,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 @@ -106,6 +114,63 @@ 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 5486f1d

Please sign in to comment.