Skip to content

Commit 66e83ea

Browse files
authored
PC: Ensure Uniqueness of SoA Names (#4299)
## Summary 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). ## Additional background AMReX-Codes/pyamrex#382 ## Checklist The proposed changes: - [x] fix a bug or incorrect behavior in AMReX - [ ] 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 20eb1ab commit 66e83ea

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

Src/Particle/AMReX_ParticleContainer.H

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <memory>
6161
#include <numeric>
6262
#include <random>
63+
#include <stdexcept>
6364
#include <string>
6465
#include <tuple>
6566
#include <type_traits>
@@ -1269,6 +1270,11 @@ public:
12691270

12701271
void AddRealComp (std::string const & name, int communicate=1)
12711272
{
1273+
// names must be unique
1274+
auto const it = std::find(m_soa_rdata_names.begin(), m_soa_rdata_names.end(), name);
1275+
if (it != m_soa_rdata_names.end()) {
1276+
throw std::runtime_error("AddRealComp: name '" + name + "' is already present in the SoA.");
1277+
}
12721278
m_soa_rdata_names.push_back(name);
12731279

12741280
m_runtime_comps_defined = true;
@@ -1297,6 +1303,11 @@ public:
12971303

12981304
void AddIntComp (std::string const & name, int communicate=1)
12991305
{
1306+
// names must be unique
1307+
auto const it = std::find(m_soa_idata_names.begin(), m_soa_idata_names.end(), name);
1308+
if (it != m_soa_idata_names.end()) {
1309+
throw std::runtime_error("AddIntComp: name '" + name + "' is already present in the SoA.");
1310+
}
13001311
m_soa_idata_names.push_back(name);
13011312

13021313
m_runtime_comps_defined = true;

Src/Particle/AMReX_ParticleContainerI.H

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

3+
#include <set>
34
#include <string>
45
#include <type_traits>
56
#include <vector>
@@ -91,6 +92,12 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
9192
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(rdata_name.size() == NArrayReal, "rdata_name must be equal to NArrayReal");
9293
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(idata_name.size() == NArrayInt, "idata_name must be equal to NArrayInt");
9394

95+
// ensure names for components are unique
96+
std::set<std::string> const unique_r_names(rdata_name.begin(), rdata_name.end());
97+
std::set<std::string> const unique_i_names(idata_name.begin(), idata_name.end());
98+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(rdata_name.size() == unique_r_names.size(), "SetSoACompileTimeNames: Provided names in rdata_name are not unique!");
99+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(idata_name.size() == unique_i_names.size(), "SetSoACompileTimeNames: Provided names in idata_name are not unique!");
100+
94101
for (int i=0; i<NArrayReal; ++i)
95102
{
96103
m_soa_rdata_names.at(i) = rdata_name.at(i);

0 commit comments

Comments
 (0)