From e7a88e16d2d23cbe84233017cc4da3b64437c02b Mon Sep 17 00:00:00 2001 From: Andrew Myers Date: Wed, 2 Oct 2024 15:01:06 -0700 Subject: [PATCH] refactor default names --- Src/Particle/AMReX_ParticleContainer.H | 6 +- Src/Particle/AMReX_ParticleContainerI.H | 17 +----- Src/Particle/AMReX_ParticleIO.H | 68 +++++++-------------- Src/Particle/AMReX_ParticleUtil.H | 20 ++++++ Tests/Particles/NamedSoAComponents/main.cpp | 6 +- 5 files changed, 50 insertions(+), 67 deletions(-) diff --git a/Src/Particle/AMReX_ParticleContainer.H b/Src/Particle/AMReX_ParticleContainer.H index 3280acbdd71..4b347d283b8 100644 --- a/Src/Particle/AMReX_ParticleContainer.H +++ b/Src/Particle/AMReX_ParticleContainer.H @@ -1274,8 +1274,7 @@ public: void AddRealComp (int communicate=1) { - std::string default_name = "runtime_real" + std::to_string(m_num_runtime_real); - AddRealComp(default_name, communicate); + AddRealComp(getDefaultCompNameReal(NArrayReal+m_num_runtime_real), communicate); } void AddIntComp (std::string const & name, int communicate=1) @@ -1303,8 +1302,7 @@ public: void AddIntComp (int communicate=1) { - std::string default_name = "runtime_int" + std::to_string(m_num_runtime_int); - AddIntComp(default_name, communicate); + AddIntComp(getDefaultCompNameInt(NArrayInt+m_num_runtime_int), communicate); } int NumRuntimeRealComps () const { return m_num_runtime_real; } diff --git a/Src/Particle/AMReX_ParticleContainerI.H b/Src/Particle/AMReX_ParticleContainerI.H index 6cd7cb6159c..718405f1e66 100644 --- a/Src/Particle/AMReX_ParticleContainerI.H +++ b/Src/Particle/AMReX_ParticleContainerI.H @@ -65,24 +65,13 @@ ParticleContainer_impl(i)); } for (int i=0; i(i)); } initialized = true; diff --git a/Src/Particle/AMReX_ParticleIO.H b/Src/Particle/AMReX_ParticleIO.H index 01ab0ded869..d10ab21b530 100644 --- a/Src/Particle/AMReX_ParticleIO.H +++ b/Src/Particle/AMReX_ParticleIO.H @@ -51,16 +51,14 @@ ParticleContainer_impl write_real_comp; Vector tmp_real_comp_names; - int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); - for (int i = 0; i < nrc; ++i ) + int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; + for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i ) { write_real_comp.push_back(1); if (real_comp_names.empty()) { - std::stringstream ss; - ss << "real_comp" << i; - tmp_real_comp_names.push_back(ss.str()); + tmp_real_comp_names.push_back(getDefaultCompNameReal(i)); } else { @@ -75,9 +73,7 @@ ParticleContainer_impl(i)); } else { @@ -98,14 +94,12 @@ ParticleContainer_impl write_real_comp; Vector real_comp_names; - int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); - for (int i = 0; i < nrc; ++i ) + int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; + for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i ) { write_real_comp.push_back(1); - std::stringstream ss; - ss << "real_comp" << i; - real_comp_names.push_back(ss.str()); + real_comp_names.push_back(getDefaultCompNameReal(i)); } Vector write_int_comp; @@ -113,9 +107,7 @@ ParticleContainer_impl(i)); } WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp, @@ -182,9 +174,7 @@ ParticleContainer_impl int_comp_names; for (int i = 0; i < NStructInt + NumIntComps(); ++i ) { - std::stringstream ss; - ss << "int_comp" << i; - int_comp_names.push_back(ss.str()); + int_comp_names.push_back(getDefaultCompNameInt(i)); } WriteBinaryParticleData(dir, name, @@ -211,20 +201,16 @@ ParticleContainer_impl real_comp_names; - int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); - for (int i = 0; i < nrc; ++i ) + int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; + for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i ) { - std::stringstream ss; - ss << "real_comp" << i; - real_comp_names.push_back(ss.str()); + real_comp_names.push_back(getDefaultCompNameReal(i)); } Vector int_comp_names; for (int i = 0; i < NStructInt + NumIntComps(); ++i ) { - std::stringstream ss; - ss << "int_comp" << i; - int_comp_names.push_back(ss.str()); + int_comp_names.push_back(getDefaultCompNameInt(i)); } WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp, @@ -259,14 +245,12 @@ ParticleContainer_impl write_real_comp; Vector real_comp_names; - int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); - for (int i = 0; i < nrc; ++i ) + int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; + for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i ) { write_real_comp.push_back(1); - std::stringstream ss; - ss << "real_comp" << i; - real_comp_names.push_back(ss.str()); + real_comp_names.push_back(getDefaultCompNameReal(i)); } Vector write_int_comp; @@ -274,9 +258,7 @@ ParticleContainer_impl(i)); } WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp, @@ -345,9 +327,7 @@ ParticleContainer_impl int_comp_names; for (int i = 0; i < NStructInt + NumIntComps(); ++i ) { - std::stringstream ss; - ss << "int_comp" << i; - int_comp_names.push_back(ss.str()); + int_comp_names.push_back(getDefaultCompNameInt(i)); } WriteBinaryParticleData(dir, name, @@ -374,20 +354,16 @@ ParticleContainer_impl real_comp_names; - int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); - for (int i = 0; i < nrc; ++i ) + int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; + for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i ) { - std::stringstream ss; - ss << "real_comp" << i; - real_comp_names.push_back(ss.str()); + real_comp_names.push_back(getDefaultCompNameReal(i)); } Vector int_comp_names; for (int i = 0; i < NStructInt + NumIntComps(); ++i ) { - std::stringstream ss; - ss << "int_comp" << i; - int_comp_names.push_back(ss.str()); + int_comp_names.push_back(getDefaultCompNameInt(i)); } WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp, diff --git a/Src/Particle/AMReX_ParticleUtil.H b/Src/Particle/AMReX_ParticleUtil.H index b09f1d3583f..f6f2506ec7f 100644 --- a/Src/Particle/AMReX_ParticleUtil.H +++ b/Src/Particle/AMReX_ParticleUtil.H @@ -883,6 +883,26 @@ void PermutationForDeposition (Gpu::DeviceVector& perm, index_type n }); } +template +std::string getDefaultCompNameReal (const int i) { + int first_r_name = 0; + if constexpr (P::is_soa_particle) { + if (i < AMREX_SPACEDIM) { + constexpr int x_in_ascii = 120; + std::string const name{char(x_in_ascii+i)}; + return name; + } + first_r_name = AMREX_SPACEDIM; + } + std::string const name{("real_comp" + std::to_string(i-first_r_name))}; + return name; +} + +template +std::string getDefaultCompNameInt (const int i) { + std::string const name{("int_comp" + std::to_string(i))}; + return name; +} #ifdef AMREX_USE_HDF5_ASYNC void async_vol_es_wait_particle(); diff --git a/Tests/Particles/NamedSoAComponents/main.cpp b/Tests/Particles/NamedSoAComponents/main.cpp index 12655c441be..39b6a70ff52 100644 --- a/Tests/Particles/NamedSoAComponents/main.cpp +++ b/Tests/Particles/NamedSoAComponents/main.cpp @@ -51,9 +51,9 @@ void addParticles () amrex::Print() << "\n"; amrex::Print() << "Adding runtime comps. \n"; - pc.AddRealComp("runtime_rcomp0"); - pc.AddRealComp(); // without name - should be runtime_rcomp1 - pc.AddIntComp(); // without name - should be runtime_icomp0 + pc.AddRealComp("real_comp1"); + pc.AddRealComp(); // without name - should be real_comp2 + pc.AddIntComp(); // without name - should be int_comp0 amrex::Print() << "New Real SoA component names are: "; for (auto& n : pc.GetRealSoANames()) {