From 6152178fd6f1f75ca7eab088583c0871f3f91cb0 Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Fri, 17 Jan 2025 15:31:09 -0800 Subject: [PATCH] SoA Names: Ensure Uniqueness 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). --- Src/Particle/AMReX_ParticleContainer.H | 11 +++++++++++ Src/Particle/AMReX_ParticleContainerI.H | 7 +++++++ 2 files changed, 18 insertions(+) diff --git a/Src/Particle/AMReX_ParticleContainer.H b/Src/Particle/AMReX_ParticleContainer.H index 95cc3a996d8..2782e743359 100644 --- a/Src/Particle/AMReX_ParticleContainer.H +++ b/Src/Particle/AMReX_ParticleContainer.H @@ -60,6 +60,7 @@ #include #include #include +#include #include #include #include @@ -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; @@ -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; diff --git a/Src/Particle/AMReX_ParticleContainerI.H b/Src/Particle/AMReX_ParticleContainerI.H index 9d749bb3b8f..769ab997f3f 100644 --- a/Src/Particle/AMReX_ParticleContainerI.H +++ b/Src/Particle/AMReX_ParticleContainerI.H @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -89,6 +90,12 @@ ParticleContainer_impl const unique_r_names(rdata_name.begin(), rdata_name.end()); + std::set 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