Skip to content

Commit

Permalink
SoA Names: Ensure Uniqueness
Browse files Browse the repository at this point in the history
Avoid user errors. Happened immediately to me: I added
a runtime component with the same name as a compile-time SoA
component and no error was thrown (now it will).
  • Loading branch information
ax3l committed Jan 17, 2025
1 parent 0233b8e commit 6152178
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Src/Particle/AMReX_ParticleContainer.H
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
#include <memory>
#include <numeric>
#include <random>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>
Expand Down Expand Up @@ -1269,6 +1270,11 @@ public:

void AddRealComp (std::string const & name, int communicate=1)
{
// names must be unique
auto const 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("AddRealComp: name '" + name + "' is already present in the SoA.");
}
m_soa_rdata_names.push_back(name);

m_runtime_comps_defined = true;
Expand Down Expand Up @@ -1297,6 +1303,11 @@ public:

void AddIntComp (std::string const & name, int communicate=1)
{
// names must be unique
auto const 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("AddIntComp: name '" + name + "' is already present in the SoA.");
}
m_soa_idata_names.push_back(name);

m_runtime_comps_defined = true;
Expand Down
7 changes: 7 additions & 0 deletions Src/Particle/AMReX_ParticleContainerI.H
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <AMReX_MakeParticle.H>

#include <set>
#include <string>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -89,6 +90,12 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(rdata_name.size() == NArrayReal, "rdata_name must be equal to NArrayReal");
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(idata_name.size() == NArrayInt, "idata_name must be equal to NArrayInt");

// ensure names for components are unique
std::set<std::string> const unique_r_names(rdata_name.begin(), rdata_name.end());
std::set<std::string> const unique_i_names(idata_name.begin(), idata_name.end());
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(rdata_name.size() == unique_r_names.size(), "SetSoACompileTimeNames: Provided names in rdata_name are not unique!");
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(idata_name.size() == unique_i_names.size(), "SetSoACompileTimeNames: Provided names in idata_name are not unique!");

for (int i=0; i<NArrayReal; ++i)
{
m_soa_rdata_names.at(i) = rdata_name.at(i);
Expand Down

0 comments on commit 6152178

Please sign in to comment.