Skip to content

Commit

Permalink
refactor default names
Browse files Browse the repository at this point in the history
  • Loading branch information
atmyers committed Oct 2, 2024
1 parent 7676c4d commit e7a88e1
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 67 deletions.
6 changes: 2 additions & 4 deletions Src/Particle/AMReX_ParticleContainer.H
Original file line number Diff line number Diff line change
Expand Up @@ -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<ParticleType>(NArrayReal+m_num_runtime_real), communicate);
}

void AddIntComp (std::string const & name, int communicate=1)
Expand Down Expand Up @@ -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<ParticleType>(NArrayInt+m_num_runtime_int), communicate);
}

int NumRuntimeRealComps () const { return m_num_runtime_real; }
Expand Down
17 changes: 3 additions & 14 deletions Src/Particle/AMReX_ParticleContainerI.H
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,13 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
pp.queryAdd("do_mem_efficient_sort", memEfficientSort);

// add default names for SoA Real and Int compile-time arguments
int first_r_name = 0;
// push back x,y,z
if constexpr (ParticleType::is_soa_particle) {
constexpr int x_in_ascii = 120;
for (int i=0; i<AMREX_SPACEDIM; ++i)
{
std::string const name{char(x_in_ascii+i)};
m_soa_rdata_names.push_back(name);
}
first_r_name = AMREX_SPACEDIM;
}
for (int i=first_r_name; i<NArrayReal; ++i)
for (int i=0; i<NArrayReal; ++i)
{
m_soa_rdata_names.push_back("real_comp" + std::to_string(i-first_r_name));
m_soa_rdata_names.push_back(getDefaultCompNameReal<ParticleType>(i));
}
for (int i=0; i<NArrayInt; ++i)
{
m_soa_idata_names.push_back("int_comp" + std::to_string(i));
m_soa_idata_names.push_back(getDefaultCompNameInt<ParticleType>(i));
}

initialized = true;
Expand Down
68 changes: 22 additions & 46 deletions Src/Particle/AMReX_ParticleIO.H
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
{
Vector<int> write_real_comp;
Vector<std::string> 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<ParticleType>(i));
}
else
{
Expand All @@ -75,9 +73,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
write_int_comp.push_back(1);
if (int_comp_names.empty())
{
std::stringstream ss;
ss << "int_comp" << i;
tmp_int_comp_names.push_back(ss.str());
tmp_int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
}
else
{
Expand All @@ -98,24 +94,20 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
{
Vector<int> write_real_comp;
Vector<std::string> 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<ParticleType>(i));
}

Vector<int> write_int_comp;
Vector<std::string> int_comp_names;
for (int i = 0; i < NStructInt + NumIntComps(); ++i )
{
write_int_comp.push_back(1);
std::stringstream ss;
ss << "int_comp" << i;
int_comp_names.push_back(ss.str());
int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
}

WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp,
Expand Down Expand Up @@ -182,9 +174,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
Vector<std::string> 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<ParticleType>(i));
}

WriteBinaryParticleData(dir, name,
Expand All @@ -211,20 +201,16 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
AMREX_ASSERT(write_int_comp.size() == NStructInt + NArrayInt );

Vector<std::string> 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<ParticleType>(i));
}

Vector<std::string> 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<ParticleType>(i));
}

WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp,
Expand Down Expand Up @@ -259,24 +245,20 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
{
Vector<int> write_real_comp;
Vector<std::string> 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<ParticleType>(i));
}

Vector<int> write_int_comp;
Vector<std::string> int_comp_names;
for (int i = 0; i < NStructInt + NumIntComps(); ++i )
{
write_int_comp.push_back(1);
std::stringstream ss;
ss << "int_comp" << i;
int_comp_names.push_back(ss.str());
int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
}

WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp,
Expand Down Expand Up @@ -345,9 +327,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
Vector<std::string> 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<ParticleType>(i));
}

WriteBinaryParticleData(dir, name,
Expand All @@ -374,20 +354,16 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
AMREX_ASSERT(write_int_comp.size() == NStructInt + NumIntComps() );

Vector<std::string> 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<ParticleType>(i));
}

Vector<std::string> 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<ParticleType>(i));
}

WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp,
Expand Down
20 changes: 20 additions & 0 deletions Src/Particle/AMReX_ParticleUtil.H
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,26 @@ void PermutationForDeposition (Gpu::DeviceVector<index_type>& perm, index_type n
});
}

template <typename P>
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 <typename P>
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();
Expand Down
6 changes: 3 additions & 3 deletions Tests/Particles/NamedSoAComponents/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down

0 comments on commit e7a88e1

Please sign in to comment.