diff --git a/Src/Particle/AMReX_ParticleIO.H b/Src/Particle/AMReX_ParticleIO.H index a10f9973a01..c243a61d81d 100644 --- a/Src/Particle/AMReX_ParticleIO.H +++ b/Src/Particle/AMReX_ParticleIO.H @@ -51,7 +51,9 @@ ParticleContainer_impl write_real_comp; Vector tmp_real_comp_names; - for (int i = 0; i < NStructReal + NumRealComps(); ++i ) + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + + for (int i = 0; i < nrc; ++i ) { write_real_comp.push_back(1); if (real_comp_names.empty()) @@ -96,7 +98,9 @@ ParticleContainer_impl write_real_comp; Vector real_comp_names; - for (int i = 0; i < NStructReal + NumRealComps(); ++i ) + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + + for (int i = 0; i < nrc; ++i ) { write_real_comp.push_back(1); std::stringstream ss; @@ -127,11 +131,16 @@ ParticleContainer_impl& real_comp_names, const Vector& int_comp_names) const { - AMREX_ASSERT(real_comp_names.size() == NStructReal + NumRealComps()); + if constexpr(ParticleType::is_soa_particle) { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == NumRealComps() + NStructReal - AMREX_SPACEDIM); // pure SoA: skip positions + } else { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == NumRealComps() + NStructReal); + } AMREX_ASSERT( int_comp_names.size() == NStructInt + NumIntComps() ); Vector write_real_comp; - for (int i = 0; i < NStructReal + NumRealComps(); ++i) { + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + for (int i = 0; i < nrc; ++i) { write_real_comp.push_back(1); } @@ -153,10 +162,15 @@ ParticleContainer_impl& real_comp_names) const { - AMREX_ASSERT(real_comp_names.size() == NStructReal + NumRealComps()); + if constexpr(ParticleType::is_soa_particle) { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == NumRealComps() + NStructReal - AMREX_SPACEDIM); // pure SoA: skip positions + } else { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == NumRealComps() + NStructReal); + } Vector write_real_comp; - for (int i = 0; i < NStructReal + NumRealComps(); ++i) { + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + for (int i = 0; i < nrc; ++i) { write_real_comp.push_back(1); } @@ -188,11 +202,17 @@ ParticleContainer_impl& write_real_comp, const Vector& write_int_comp) const { - AMREX_ASSERT(write_real_comp.size() == NStructReal + NumRealComps()); + + if constexpr(ParticleType::is_soa_particle) { + AMREX_ALWAYS_ASSERT(write_real_comp.size() == NumRealComps() + NStructReal - AMREX_SPACEDIM); // pure SoA: skip positions + } else { + AMREX_ALWAYS_ASSERT(write_real_comp.size() == NumRealComps() + NStructReal); + } AMREX_ASSERT(write_int_comp.size() == NStructInt + NArrayInt ); Vector real_comp_names; - for (int i = 0; i < NStructReal + NumRealComps(); ++i ) + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + for (int i = 0; i < nrc; ++i ) { std::stringstream ss; ss << "real_comp" << i; @@ -239,7 +259,9 @@ ParticleContainer_impl write_real_comp; Vector real_comp_names; - for (int i = 0; i < NStructReal + NumRealComps(); ++i ) + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + + for (int i = 0; i < nrc; ++i ) { write_real_comp.push_back(1); std::stringstream ss; @@ -271,11 +293,16 @@ ParticleContainer_impl& real_comp_names, const Vector& int_comp_names, F&& f) const { - AMREX_ASSERT(real_comp_names.size() == NStructReal + NumRealComps()); + if constexpr(ParticleType::is_soa_particle) { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == NumRealComps() + NStructReal - AMREX_SPACEDIM); // pure SoA: skip positions + } else { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == NumRealComps() + NStructReal); + } AMREX_ASSERT( int_comp_names.size() == NStructInt + NArrayInt ); Vector write_real_comp; - for (int i = 0; i < NStructReal + NumRealComps(); ++i) { + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + for (int i = 0; i < nrc; ++i) { write_real_comp.push_back(1); } @@ -298,10 +325,15 @@ ParticleContainer_impl& real_comp_names, F&& f) const { - AMREX_ASSERT(real_comp_names.size() == NStructReal + NumRealComps()); + if constexpr(ParticleType::is_soa_particle) { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == NumRealComps() + NStructReal - AMREX_SPACEDIM); // pure SoA: skip positions + } else { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == NumRealComps() + NStructReal); + } Vector write_real_comp; - for (int i = 0; i < NStructReal + NumRealComps(); ++i) { + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + for (int i = 0; i < nrc; ++i) { write_real_comp.push_back(1); } @@ -334,11 +366,16 @@ ParticleContainer_impl& write_real_comp, const Vector& write_int_comp, F&& f) const { - AMREX_ASSERT(write_real_comp.size() == NStructReal + NumRealComps()); + if constexpr(ParticleType::is_soa_particle) { + AMREX_ALWAYS_ASSERT(write_real_comp.size() == NumRealComps() + NStructReal - AMREX_SPACEDIM); // pure SoA: skip positions + } else { + AMREX_ALWAYS_ASSERT(write_real_comp.size() == NumRealComps() + NStructReal); + } AMREX_ASSERT(write_int_comp.size() == NStructInt + NumIntComps() ); Vector real_comp_names; - for (int i = 0; i < NStructReal + NumRealComps(); ++i ) + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + for (int i = 0; i < nrc; ++i ) { std::stringstream ss; ss << "real_comp" << i; @@ -680,8 +717,9 @@ ParticleContainer_impl> nr; - if (nr != NStructReal + NumRealComps()) { - amrex::Abort("ParticleContainer::Restart(): nr != NStructReal + NumRealComps()"); + int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps(); + if (nr != nrc) { + amrex::Abort("ParticleContainer::Restart(): nr not the expected value"); } std::string comp_name; @@ -929,7 +967,7 @@ ParticleContainer_impl rstuff(std::size_t(cnt)*rChunkSize); ReadParticleRealData(rstuff.dataPtr(), rstuff.size(), ifs); diff --git a/Src/Particle/AMReX_WriteBinaryParticleData.H b/Src/Particle/AMReX_WriteBinaryParticleData.H index 105a254a348..449c26c6ef2 100644 --- a/Src/Particle/AMReX_WriteBinaryParticleData.H +++ b/Src/Particle/AMReX_WriteBinaryParticleData.H @@ -289,7 +289,7 @@ packIOData (Vector& idata, Vector& rdata, const PC& pc, int l idata.resize(np*iChunkSize); int num_output_real = 0; - for (int i = 0; i < pc.NumRealComps() + PC::NStructReal; ++i) { + for (int i = 0; i < (int) write_real_comp.size(); ++i) { if (write_real_comp[i]) { ++num_output_real; } } @@ -361,7 +361,9 @@ packIOData (Vector& idata, Vector& rdata, const PC& pc, int l // extra SoA Real components const int real_start_offset = PC::ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; // pure SoA: skip positions for (int j = real_start_offset; j < pc.NumRealComps(); j++) { - if (write_real_comp[PC::NStructReal+j]) { + const int write_comp_offset = PC::ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; // pure SoA: skip positions + const int write_comp_index = PC::NStructReal+j-write_comp_offset; + if (write_real_comp[write_comp_index]) { *rptr = (ParticleReal) soa.GetRealData(j)[pindex]; ++rptr; } @@ -393,7 +395,11 @@ void WriteBinaryParticleDataSync (PC const& pc, const int NProcs = ParallelDescriptor::NProcs(); const int IOProcNumber = ParallelDescriptor::IOProcessorNumber(); - AMREX_ALWAYS_ASSERT(real_comp_names.size() == pc.NumRealComps() + NStructReal); + if constexpr(PC::ParticleType::is_soa_particle) { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == pc.NumRealComps() + NStructReal - AMREX_SPACEDIM); // pure SoA: skip positions + } else { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == pc.NumRealComps() + NStructReal); + } AMREX_ALWAYS_ASSERT( int_comp_names.size() == pc.NumIntComps() + NStructInt); std::string pdir = dir; @@ -476,7 +482,7 @@ void WriteBinaryParticleDataSync (PC const& pc, } int num_output_real = 0; - for (int i = 0; i < pc.NumRealComps() + NStructReal; ++i) { + for (int i = 0; i < (int) write_real_comp.size(); ++i) { if (write_real_comp[i]) { ++num_output_real; } } @@ -492,7 +498,7 @@ void WriteBinaryParticleDataSync (PC const& pc, HdrFile << num_output_real << '\n'; // Real component names - for (int i = 0; i < NStructReal + pc.NumRealComps(); ++i ) { + for (int i = 0; i < (int) real_comp_names.size(); ++i ) { if (write_real_comp[i]) { HdrFile << real_comp_names[i] << '\n'; } } @@ -683,7 +689,11 @@ void WriteBinaryParticleDataAsync (PC const& pc, const int NProcs = ParallelDescriptor::NProcs(); const int IOProcNumber = NProcs - 1; - AMREX_ALWAYS_ASSERT(real_comp_names.size() == pc.NumRealComps() + NStructReal); + if constexpr(PC::ParticleType::is_soa_particle) { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == pc.NumRealComps() + NStructReal - AMREX_SPACEDIM); // pure SoA: skip positions + } else { + AMREX_ALWAYS_ASSERT(real_comp_names.size() == pc.NumRealComps() + NStructReal); + } AMREX_ALWAYS_ASSERT( int_comp_names.size() == pc.NumIntComps() + NStructInt); Vector > np_per_grid_local(pc.finestLevel()+1); @@ -847,6 +857,7 @@ void WriteBinaryParticleDataAsync (PC const& pc, int nrc = pc.NumRealComps(); int nic = pc.NumIntComps(); + int rnames_size = (int) real_comp_names.size(); auto RD = pc.ParticleRealDescriptor; @@ -881,7 +892,7 @@ void WriteBinaryParticleDataAsync (PC const& pc, } int num_output_real = 0; - for (int i = 0; i < nrc + NStructReal; ++i) { + for (int i = 0; i < rnames_size; ++i) { if (write_real_comp[i]) { ++num_output_real; } } @@ -897,7 +908,7 @@ void WriteBinaryParticleDataAsync (PC const& pc, HdrFile << num_output_real << '\n'; // Real component names - for (int i = 0; i < NStructReal + nrc; ++i ) { + for (int i = 0; i < rnames_size; ++i ) { if (write_real_comp[i]) { HdrFile << real_comp_names[i] << '\n'; } } @@ -1047,7 +1058,7 @@ void WriteBinaryParticleDataAsync (PC const& pc, // Write the Real data in binary. int num_output_real = 0; - for (int i = 0; i < nrc + NStructReal; ++i) { + for (int i = 0; i < rnames_size; ++i) { if (write_real_comp[i]) { ++num_output_real; } } @@ -1093,7 +1104,9 @@ void WriteBinaryParticleDataAsync (PC const& pc, const int real_start_offset = PC::ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; // pure SoA: positions for (int j = real_start_offset; j < nrc; j++) { - if (write_real_comp[NStructReal+j]) + const int write_comp_offset = PC::ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0; // pure SoA: skip positions + const int write_comp_index = PC::NStructReal+j-write_comp_offset; + if (write_real_comp[write_comp_index]) { *rptr = (typename PC::ParticleType::RealType) soa.GetRealData(j)[pindex]; ++rptr; diff --git a/Tests/Particles/CheckpointRestartSOA/main.cpp b/Tests/Particles/CheckpointRestartSOA/main.cpp index e8629560196..0458239fdcd 100644 --- a/Tests/Particles/CheckpointRestartSOA/main.cpp +++ b/Tests/Particles/CheckpointRestartSOA/main.cpp @@ -120,7 +120,7 @@ void test () amrex::Print() << " done \n"; Vector particle_realnames; - for (int i = 0; i < NReal; ++i) { + for (int i = 0; i < NReal-AMREX_SPACEDIM; ++i) { particle_realnames.push_back("particle_real_component_" + std::to_string(i)); }