1#ifndef AMREX_PARTICLEINIT_H
2#define AMREX_PARTICLEINIT_H
3#include <AMReX_Config.H>
35template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
36 template<
class>
class Allocator,
class CellAssignor>
38ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
39::InitFromAsciiFile (
const std::string& file,
int extradata,
const IntVect* Nrep)
41 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitFromAsciiFile()");
49 int NReaders = MaxReaders();
60 else if (NProcs <= 4096)
62 NReaders = std::max(NReaders,128);
65 else if (NProcs <= 8192)
67 NReaders = std::max(NReaders,384);
70 else if (NProcs <= 16384)
72 NReaders = std::max(NReaders,512);
80 if (Nrep !=
nullptr) {
85 Long how_many_read = 0;
89 if (extradata > NStructReal) { nreals.resize(extradata - NStructReal); }
91 if (MyProc < NReaders)
97 ifs.rdbuf()->pubsetbuf(io_buffer.
dataPtr(), io_buffer.
size());
99 ifs.open(file.c_str(), std::ios::in);
108 ifs >> cnt >> std::ws;
114 if (extradata > NStructReal) {
r.resize(extradata - NStructReal); }
116 const int Chunk = cnt / NReaders;
118 for (
int i = 0; i < MyProc*Chunk; i++)
120 ifs.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
126 std::string msg(
"ParticleContainer::InitFromAsciiFile(");
128 msg +=
") failed @ 1";
134 if (MyProc == (NReaders - 1))
136 MyCnt += cnt % NReaders;
141 for (
int i = 0; i < MyCnt; i++)
147 for (
int n = 0; n < extradata; n++)
155 ifs >>
r[n - NStructReal];
161 std::string msg(
"ParticleContainer::InitFromAsciiFile(");
162 msg += file; msg +=
") failed @ 2";
173 amrex::AllPrint() <<
"BAD PARTICLE ID WOULD BE " << ParticleType::NextID() <<
'\n'
174 <<
"BAD PARTICLE POS "
180 amrex::Abort(
"ParticleContainer::InitFromAsciiFile(): invalid particle");
185 p.id() = ParticleType::NextID();
189 if(nreals.
size() > extradata - NStructReal) {
190 for (
int n = NStructReal; n < extradata; n++)
192 nreals[n-NStructReal].push_back(
r[n-NStructReal]);
199 const Real DomSize[AMREX_SPACEDIM] =
203 int rep[AMREX_SPACEDIM];
205#if AMREX_SPACEDIM > 2
206 for (rep[2] = 1; rep[2] <= lNrep[2]; rep[2]++)
209#if AMREX_SPACEDIM > 1
210 for (rep[1] = 1; rep[1] <= lNrep[1]; rep[1]++)
213 for (rep[0] = 1; rep[0] <= lNrep[0]; rep[0]++)
215 if (!(
AMREX_D_TERM( (rep[0] == 1), && (rep[1] == 1), && (rep[2] == 1) ) ) )
218 for (
int d=0; d<AMREX_SPACEDIM; ++d)
220 p_rep.pos(d) =
static_cast<ParticleReal
>(p.pos(d) + Real(rep[d]-1)*DomSize[d]);
224 for (
int n = 0; n < extradata; n++)
228 p_rep.rdata(n) = p.rdata(n);
232 if (!Where(p_rep, pld))
234 PeriodicShift(p_rep);
235 if (!Where(p_rep, pld))
238 amrex::AllPrint() <<
"BAD REPLICATED PARTICLE ID WOULD BE " << ParticleType::NextID() <<
"\n";
240 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromAsciiFile(): invalid replicated particle");
244 p_rep.id() = ParticleType::NextID();
245 p_rep.cpu() = MyProc;
249 if (nreals.
size() > extradata - NStructReal) {
250 for (
int n = NStructReal; n < extradata; n++)
252 nreals[n-NStructReal].push_back(
r[n-NStructReal]);
259#if AMREX_SPACEDIM > 1
262#if AMREX_SPACEDIM > 2
271 int NRedist_chunk = NReaders / NRedist;
275 for (
int nr = 0; nr < NRedist; nr++)
278 host_particles.reserve(15);
279 host_particles.resize(finestLevel()+1);
282 host_real_attribs.reserve(15);
283 host_real_attribs.resize(finestLevel()+1);
287 << nr*NRedist_chunk <<
" to "
288 << (nr+1)*NRedist_chunk-1 <<
'\n';
291 for (
int which = nr*NRedist_chunk; which < (nr+1)*NRedist_chunk; which++)
295 while (!nparticles.
empty())
302 if (nreals.
size() > extradata - NStructReal && NArrayReal > 0)
304 for (
int n = NStructReal; n < extradata; n++)
306 Real rdata = nreals[n-NStructReal].back();
307 host_real_attribs[pld.
m_lev][std::make_pair(pld.
m_grid, pld.
m_tile)][n-NStructReal].push_back(rdata);
313 if (nreals.
size() > extradata - NStructReal)
315 for (
int n = NStructReal; n < extradata; n++)
317 nreals[n-NStructReal].pop_back();
324 for (
int lev = 0; lev < static_cast<int>(host_particles.
size()); ++lev)
326 for (
auto& kv : host_particles[lev])
329 auto grid = kv.first.first;
330 auto tile = kv.first.second;
331 const auto& src_tile = kv.second;
333 auto& dst_tile = GetParticles(lev)[std::make_pair(grid,tile)];
334 auto old_size = dst_tile.GetArrayOfStructs().
size();
335 auto new_size = old_size + src_tile.size();
336 dst_tile.resize(new_size);
339 dst_tile.GetArrayOfStructs().begin() + old_size);
341 if((host_real_attribs[lev][std::make_pair(grid, tile)]).size() > (
long unsigned int) NArrayReal) {
342 for (
int i = 0; i < NArrayReal; ++i) {
344 host_real_attribs[lev][std::make_pair(grid,tile)][i].
begin(),
345 host_real_attribs[lev][std::make_pair(grid,tile)][i].
end(),
346 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
358 if (NRedist*NRedist_chunk < NReaders) {
360 << NRedist*NRedist_chunk <<
" to "
364 for (
int which = NRedist*NRedist_chunk; which < NReaders; which++)
367 host_particles.reserve(15);
368 host_particles.resize(finestLevel()+1);
371 host_real_attribs.reserve(15);
372 host_real_attribs.resize(finestLevel()+1);
376 while (!nparticles.
empty())
382 if((host_real_attribs[pld.
m_lev][std::make_pair(pld.
m_grid, pld.
m_tile)]).size() > (
long unsigned int) (extradata - NStructReal)) {
383 for (
int n = NStructReal; n < extradata; n++)
385 Real rdata = nreals[n-NStructReal].back();
386 host_real_attribs[pld.
m_lev][std::make_pair(pld.
m_grid, pld.
m_tile)][n-NStructReal].push_back(rdata);
392 if (nreals.
size() > extradata - NStructReal) {
393 for (
int n = NStructReal; n < extradata; n++)
395 nreals[n-NStructReal].pop_back();
401 for (
int lev = 0; lev < static_cast<int>(host_particles.
size()); ++lev)
403 for (
auto& kv : host_particles[lev])
405 auto grid = kv.first.first;
406 auto tile = kv.first.second;
407 const auto& src_tile = kv.second;
409 auto& dst_tile = GetParticles(lev)[std::make_pair(grid,tile)];
410 auto old_size = dst_tile.GetArrayOfStructs().
size();
411 auto new_size = old_size + src_tile.size();
412 dst_tile.resize(new_size);
415 dst_tile.GetArrayOfStructs().begin() + old_size);
417 for (
int i = 0; i < NArrayReal; ++i) {
419 host_real_attribs[lev][std::make_pair(grid,tile)][i].
begin(),
420 host_real_attribs[lev][std::make_pair(grid,tile)][i].
end(),
421 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
435 Long num_particles = how_many;
439 if (
AMREX_D_TERM(lNrep[0] == 1, && lNrep[1] == 1, && lNrep[2] == 1))
441 amrex::Print() <<
"Total number of particles: " << num_particles <<
'\n';
445 Long num_particles_read = how_many_read;
450 <<
AMREX_D_TERM(lNrep[0] <<
" ", << lNrep[1] <<
" ", << lNrep[2]) <<
"\n"
451 <<
"Total number of particles read in : " << num_particles_read <<
'\n'
452 <<
"Total number of particles after replication: " << num_particles <<
'\n';
466 amrex::Print() <<
"InitFromAsciiFile() time: " << runtime <<
'\n';
481template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
482 template<
class>
class Allocator,
class CellAssignor>
488 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitFromBinaryFile()");
501 const int NReaders = MaxReaders();
507 const Long NPartPerRedist = MaxParticlesPerRead();
511 amrex::Print() <<
"Reading with " << NReaders <<
" readers\n"
512 <<
"Redistributing after every " << NPartPerRedist <<
" particles for each reader\n";
520 tmp_particles.reserve(15);
521 tmp_particles.resize(finestLevel()+1);
539 std::set<int> readers;
545 if (NReaders == NProcs)
550 for (
int i = 0; i < NProcs; i++) {
577 while (
static_cast<int>(readers.size()) < NReaders);
583 for (
auto it = readers.cbegin(), End = readers.cend();
599 readers.insert(rprocs.begin(), rprocs.end());
606 int RealSizeInFile = 0;
608 if (readers.find(MyProc) != readers.end())
612 ifs.rdbuf()->pubsetbuf(io_buffer.
dataPtr(), io_buffer.
size());
614 ifs.open(file.c_str(), std::ios::in|std::ios::binary);
620 ifs.read((
char*)&NP,
sizeof(NP));
621 ifs.read((
char*)&DM,
sizeof(DM));
622 ifs.read((
char*)&NX,
sizeof(NX));
627 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): NP <= 0");
632 if (DM != AMREX_SPACEDIM) {
633 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): DM != AMREX_SPACEDIM");
638 if (NX < 0 || NX > NStructReal) {
639 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): NX < 0 || NX > N");
644 if (extradata > NX) {
645 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): extradata > NX");
652 const std::streamoff CURPOS = ifs.tellg();
656 ifs.seekg(0,std::ios::end);
660 const std::streamoff ENDPOS = ifs.tellg();
662 RealSizeInFile =
int((ENDPOS - CURPOS) / (NP*(DM+NX)));
664 AMREX_ASSERT(RealSizeInFile ==
sizeof(
float) || RealSizeInFile ==
sizeof(
double));
668 ifs.seekg(CURPOS, std::ios::beg);
673 for ( ;
id < NReaders;
id++) {
674 if (rprocs[
id] == MyProc) {
681 const std::streamoff NSKIP =
id * (NP/NReaders) * (DM+NX) * RealSizeInFile;
685 ifs.seekg(NSKIP, std::ios::cur);
690 std::string msg(
"ParticleContainer::InitFromBinaryFile(");
692 msg +=
") failed @ 1";
703 Long MyCnt = NP / NReaders;
705 if (MyProc == rprocs[0]) {
709 MyCnt += NP % NReaders;
712 Long how_many_redists = NP / (NPartPerRedist*NReaders), how_many_read = 0;
714 if (NP % (NPartPerRedist*NReaders)) { how_many_redists++; }
721 fxtra.resize(extradata);
722 dxtra.resize(extradata);
725 if ((NX-extradata) > 0)
727 fignore.resize(NX-extradata);
728 dignore.resize(NX-extradata);
733 for (
int j = 0; j < how_many_redists; j++)
737 host_particles.reserve(15);
738 host_particles.resize(finestLevel()+1);
740 if (readers.find(MyProc) != readers.end())
746 const Long NRead = std::min((MyCnt-how_many_read), NPartPerRedist);
748 for (Long i = 0; i < NRead; i++)
754 if (RealSizeInFile ==
sizeof(
float))
756 float fpos[AMREX_SPACEDIM];
758 ifs.read((
char*)&fpos[0], AMREX_SPACEDIM*
sizeof(
float));
762 p.pos(2) = fpos[2];);
765 else if (RealSizeInFile ==
sizeof(
double))
767 double dpos[AMREX_SPACEDIM];
769 ifs.read((
char*)&dpos[0], AMREX_SPACEDIM*
sizeof(double));
771 AMREX_D_TERM(p.pos(0) =
static_cast<ParticleReal
>(dpos[0]);,
772 p.pos(1) =
static_cast<ParticleReal
>(dpos[1]);,
773 p.pos(2) =
static_cast<ParticleReal
>(dpos[2]););
781 if (RealSizeInFile ==
sizeof(
float))
783 ifs.read((
char*)fxtra.data(), std::streamsize(extradata*
sizeof(
float)));
785 for (
int ii = 0; ii < extradata; ii++) {
786 p.rdata(ii) =
static_cast<ParticleReal
>(fxtra[ii]);
789 else if (RealSizeInFile ==
sizeof(
double))
791 ifs.read((
char*)dxtra.data(), std::streamsize(extradata*
sizeof(
double)));
793 for (
int ii = 0; ii < extradata; ii++) {
794 p.rdata(ii) =
static_cast<ParticleReal
>(dxtra[ii]);
801 if ((NX-extradata) > 0)
803 if (RealSizeInFile ==
sizeof(
float))
805 ifs.read((
char*)fignore.data(), std::streamsize((NX-extradata)*
sizeof(
float)));
807 else if (RealSizeInFile ==
sizeof(
double))
809 ifs.read((
char*)dignore.data(), std::streamsize((NX-extradata)*
sizeof(
double)));
815 std::string msg(
"ParticleContainer::InitFromBinaryFile(");
817 msg +=
") failed @ 2";
828 amrex::AllPrint() <<
"BAD PARTICLE ID WOULD BE " << ParticleType::NextID() <<
'\n'
829 <<
"BAD PARTICLE POS "
835 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): invalid particle");
839 p.id() = ParticleType::NextID();
845 how_many_read += NRead;
848 for (
int host_lev = 0; host_lev < static_cast<int>(host_particles.
size()); ++host_lev)
850 for (
auto& kv : host_particles[host_lev]) {
851 auto grid = kv.first.first;
852 auto tile = kv.first.second;
853 const auto& src_tile = kv.second;
855 auto& dst_tile = GetParticles(host_lev)[std::make_pair(grid,tile)];
856 auto old_size = dst_tile.GetArrayOfStructs().
size();
857 auto new_size = old_size + src_tile.size();
858 dst_tile.resize(new_size);
861 dst_tile.GetArrayOfStructs().begin() + old_size);
873 for (
int lev = 0; lev < m_particles.size(); lev++)
875 auto& pmap = m_particles[lev];
876 auto& tmp_pmap = tmp_particles[lev];
878 for (
auto& kv : pmap) {
879 auto& aos = kv.second.GetArrayOfStructs()();
880 auto& tmp_aos = tmp_pmap[kv.first].GetArrayOfStructs()();
882 tmp_aos.insert(tmp_aos.end(), aos.begin(), aos.end());
892 tmp_particles.swap(m_particles);
898 Long num_particles_read = how_many_read;
902 amrex::Print() <<
"\nTotal number of particles: " << num_particles_read <<
'\n';
915 amrex::Print() <<
"InitFromBinaryFile() time: " << runtime <<
'\n';
927template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
928 template<
class>
class Allocator,
class CellAssignor>
934 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitFromBinaryMetaFile()");
937 std::ifstream ifs(metafile.c_str(), std::ios::in);
943 std::getline(ifs,file);
945 if (!ifs.good()) {
break; }
948 amrex::Print() <<
"InitFromBinaryMetaFile: processing file: " << file <<
'\n';
951 InitFromBinaryFile(file, extradata);
962 amrex::Print() <<
"InitFromBinaryMetaFile() time: " << runtime <<
'\n';
966template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
967 template<
class>
class Allocator,
class CellAssignor>
976 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitRandom()");
994 if (!containing_bx.
ok()) { containing_bx = geom.
ProbDomain(); }
1003 const Real* xlo = containing_bx.
lo();
1004 const Real* xhi = containing_bx.
hi();
1010 if(icount*AMREX_SPACEDIM >= std::numeric_limits<int>::max())
1013 "InitRandom has serialize=true, but this would cause too much "
1014 "particle data to be sent from IOProc. Set serialize=false, "
1015 "or use fewer than " +
1018 amrex::Real(std::numeric_limits<int>::max()) /
1019 amrex::Real(AMREX_SPACEDIM)
1035 for (Long j = 0; j < icount; j++)
1037 for (
int i = 0; i < AMREX_SPACEDIM; i++)
1044 while (
static_cast<ParticleReal
>(
x) <
static_cast<ParticleReal
>(xlo[i]) ||
static_cast<ParticleReal
>(
x) >=
static_cast<ParticleReal
>(xhi[i]));
1046 pos[j*AMREX_SPACEDIM + i] =
static_cast<ParticleReal
>(
x);
1056 host_particles.reserve(15);
1057 host_particles.resize(finestLevel()+1);
1060 host_real_attribs.reserve(15);
1061 host_real_attribs.resize(finestLevel()+1);
1064 host_int_attribs.reserve(15);
1065 host_int_attribs.resize(finestLevel()+1);
1068 host_idcpu.reserve(15);
1069 host_idcpu.resize(finestLevel()+1);
1071 for (Long j = 0; j < icount; j++)
1075 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1076 ptest.
pos(i) = pos[j*AMREX_SPACEDIM + i];
1079 if (!Where(ptest, pld)) {
1080 amrex::Abort(
"ParticleContainer::InitRandom(): invalid particle");
1086 const int who = ParticleDistributionMap(pld.
m_lev)[pld.
m_grid];
1088 if (who == MyProc) {
1090 if constexpr(!ParticleType::is_soa_particle)
1093 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1094 p.pos(i) = pos[j*AMREX_SPACEDIM + i];
1098 p.id() = ParticleType::NextID();
1101 for (
int i = 0; i < NStructInt; i++) {
1105 for (
int i = 0; i < NStructReal; i++) {
1110 host_particles[pld.
m_lev][ind].push_back(p);
1113 for (
int i = 0; i < NArrayReal; i++) {
1114 host_real_attribs[pld.
m_lev][ind][i].push_back(
static_cast<ParticleReal
>(pdata.
real_array_data[i]));
1118 for (
int i = 0; i < NArrayInt; i++) {
1122 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1123 host_real_attribs[pld.
m_lev][ind][i].push_back(pos[j*AMREX_SPACEDIM+i]);
1126 host_idcpu[pld.
m_lev][ind].push_back(0);
1130 host_particles[pld.
m_lev][ind];
1133 for (
int i = AMREX_SPACEDIM; i < NArrayReal; i++) {
1134 host_real_attribs[pld.
m_lev][ind][i].push_back(
static_cast<ParticleReal
>(pdata.
real_array_data[i]));
1138 for (
int i = 2; i < NArrayInt; i++) {
1145 for (
int host_lev = 0; host_lev < static_cast<int>(host_particles.
size()); ++host_lev)
1147 for (
auto& kv : host_particles[host_lev]) {
1148 auto grid = kv.first.first;
1149 auto tile = kv.first.second;
1150 const auto& src_tile = kv.second;
1152 auto& dst_tile = GetParticles(host_lev)[std::make_pair(grid,tile)];
1153 auto old_size = dst_tile.
size();
1154 auto new_size = old_size;
1155 if constexpr(!ParticleType::is_soa_particle)
1157 new_size += src_tile.size();
1159 new_size += host_real_attribs[host_lev][std::make_pair(grid,tile)][0].
size();
1161 dst_tile.resize(new_size);
1163 if constexpr(!ParticleType::is_soa_particle)
1166 dst_tile.GetArrayOfStructs().begin() + old_size);
1169 host_idcpu[host_lev][std::make_pair(grid,tile)].
begin(),
1170 host_idcpu[host_lev][std::make_pair(grid,tile)].
end(),
1171 dst_tile.GetStructOfArrays().GetIdCPUData().begin() + old_size);
1174 for (
int i = 0; i < NArrayReal; ++i) {
1176 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].
begin(),
1177 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].
end(),
1178 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
1181 for (
int i = 0; i < NArrayInt; ++i) {
1183 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].
begin(),
1184 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].
end(),
1185 dst_tile.GetStructOfArrays().GetIntData(i).begin() + old_size);
1196 Long M = icount / NProcs;
1199 M += (icount % NProcs);
1205 host_particles.reserve(15);
1206 host_particles.resize(finestLevel()+1);
1209 host_real_attribs.reserve(15);
1210 host_real_attribs.resize(finestLevel()+1);
1213 host_int_attribs.reserve(15);
1214 host_int_attribs.resize(finestLevel()+1);
1217 host_idcpu.reserve(15);
1218 host_idcpu.resize(finestLevel()+1);
1220 for (Long icnt = 0; icnt < M; icnt++) {
1222 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1227 while (
static_cast<ParticleReal
>(
x) <
static_cast<ParticleReal
>(xlo[i]) ||
static_cast<ParticleReal
>(
x) >=
static_cast<ParticleReal
>(xhi[i]));
1229 ptest.
pos(i) =
static_cast<ParticleReal
>(
x);
1234 ptest.
id() = ParticleType::NextID();
1238 if (!Where(ptest, pld))
1240 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitRandom(): invalid particle");
1245 if constexpr(!ParticleType::is_soa_particle)
1248 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1249 p.pos(i) = ptest.
pos(i);;
1252 p.id() = ptest.
id();
1253 p.cpu() = ptest.
cpu();
1255 for (
int i = 0; i < NStructReal; i++) {
1259 for (
int i = 0; i < NStructInt; i++) {
1264 host_particles[pld.
m_lev][ind].push_back(p);
1267 for (
int i = 0; i < NArrayReal; i++) {
1268 host_real_attribs[pld.
m_lev][ind][i].push_back(
static_cast<ParticleReal
>(pdata.
real_array_data[i]));
1272 for (
int i = 0; i < NArrayInt; i++) {
1276 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1277 host_real_attribs[pld.
m_lev][ind][i].push_back(ptest.
pos(i));
1280 host_idcpu[pld.
m_lev][ind].push_back(0);
1284 host_particles[pld.
m_lev][ind];
1287 for (
int i = AMREX_SPACEDIM; i < NArrayReal; i++) {
1288 host_real_attribs[pld.
m_lev][ind][i].push_back(
static_cast<ParticleReal
>(pdata.
real_array_data[i]));
1292 for (
int i = 2; i < NArrayInt; i++) {
1298 for (
int host_lev = 0; host_lev < static_cast<int>(host_particles.
size()); ++host_lev)
1300 for (
auto& kv : host_particles[host_lev]) {
1301 auto grid = kv.first.first;
1302 auto tile = kv.first.second;
1303 const auto& src_tile = kv.second;
1305 auto& dst_tile = GetParticles(host_lev)[std::make_pair(grid,tile)];
1306 auto old_size = dst_tile.
size();
1307 auto new_size = old_size;
1308 if constexpr(!ParticleType::is_soa_particle)
1310 new_size += src_tile.size();
1312 new_size += host_real_attribs[host_lev][std::make_pair(grid,tile)][0].
size();
1314 dst_tile.resize(new_size);
1316 if constexpr(!ParticleType::is_soa_particle)
1319 dst_tile.GetArrayOfStructs().begin() + old_size);
1322 host_idcpu[host_lev][std::make_pair(grid,tile)].
begin(),
1323 host_idcpu[host_lev][std::make_pair(grid,tile)].
end(),
1324 dst_tile.GetStructOfArrays().GetIdCPUData().begin() + old_size);
1327 for (
int i = 0; i < NArrayReal; ++i) {
1329 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].
begin(),
1330 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].
end(),
1331 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
1334 for (
int i = 0; i < NArrayInt; ++i) {
1336 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].
begin(),
1337 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].
end(),
1338 dst_tile.GetStructOfArrays().GetIntData(i).begin() + old_size);
1354 amrex::Print() <<
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitRandom() time: " << stoptime <<
'\n';
1360template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
1361 template<
class>
class Allocator,
class CellAssignor>
1368 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitRandomPerBox()");
1385 std::mt19937 mt(iseed);
1386 std::uniform_real_distribution<double> dist(0.0, 1.0);
1388 m_particles.resize(m_gdb->finestLevel()+1);
1390 for (
int lev = 0; lev < m_particles.size(); lev++)
1396 for (
MFIter mfi(*m_dummy_mf[0],
false); mfi.
isValid(); ++mfi)
1398 Box grid = m_gdb->ParticleBoxArray(0)[mfi.index()];
1401 for (Long icnt = 0; icnt < icount_per_box; icnt++) {
1402 for (Long jcnt = 0; jcnt < icount_per_box; jcnt++) {
1403 for (Long kcnt = 0; kcnt < icount_per_box; kcnt++)
1406 p.pos(0) =
static_cast<ParticleReal
>(grid_box.
lo(0) + (dist(mt) +
double(icnt)) /
double(icount_per_box) * grid_box.
length(0));,
1407 p.pos(1) =
static_cast<ParticleReal
>(grid_box.
lo(1) + (dist(mt) + double(jcnt)) /
double(icount_per_box) * grid_box.
length(1));,
1408 p.pos(2) =
static_cast<ParticleReal
>(grid_box.
lo(2) + (dist(mt) + double(kcnt)) /
double(icount_per_box) * grid_box.
length(2));
1411 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1416 for (
int i = 0; i < NStructReal; i++) {
1421 p.id() = ParticleType::NextID();
1424 for (
int i = 0; i < NStructInt; i++) {
1429 if (!Where(p, pld)) {
1430 amrex::Abort(
"ParticleContainer::InitRandomPerBox(): invalid particle");
1436 m_particles[pld.
m_lev][ind].push_back(p);
1439 for (
int i = 0; i < NArrayReal; i++) {
1440 m_particles[pld.
m_lev][ind].push_back_real(i,
static_cast<ParticleReal
>(pdata.
real_array_data[i]));
1444 for (
int i = 0; i < NArrayInt; i++) {
1457 amrex::Print() <<
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitRandomPerBox() time: " << stoptime <<
'\n';
1461template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
1462 template<
class>
class Allocator,
class CellAssignor>
1469 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitOnePerCell()");
1475 AMREX_ASSERT(x_off >= 0. && y_off >= 0. && z_off >= 0.);
1476 AMREX_ASSERT(x_off <= 1. && y_off <= 1. && z_off <= 1.);
1487 for (
MFIter mfi(*m_dummy_mf[0],
false); mfi.
isValid(); ++mfi) {
1488 Box grid = ParticleBoxArray(0)[mfi.index()];
1489 auto ind = std::make_pair(mfi.index(), mfi.LocalTileIndex());
1495 AMREX_D_TERM(p.pos(0) =
static_cast<ParticleReal
>(grid_box.
lo(0) + (x_off + cell[0]-beg[0])*dx[0]);,
1496 p.pos(1) =
static_cast<ParticleReal
>(grid_box.
lo(1) + (y_off + cell[1]-beg[1])*dx[1]);,
1497 p.pos(2) =
static_cast<ParticleReal
>(grid_box.
lo(2) + (z_off + cell[2]-beg[2])*dx[2]););
1499 for (
int d = 0; d < AMREX_SPACEDIM; ++d) {
1503 for (
int i = 0; i < NStructReal; i++) {
1508 p.id() = ParticleType::NextID();
1511 for (
int i = 0; i < NStructInt; i++) {
1519 for (
int i = 0; i < NArrayReal; i++) {
1524 for (
int i = 0; i < NArrayInt; i++) {
1536 if (m_verbose > 1) {
1541 amrex::Print() <<
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitOnePerCell() time: " << stoptime <<
'\n';
1545template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
1546 template<
class>
class Allocator,
class CellAssignor>
1551 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitNRandomPerCell()");
1564 for (
int lev = 0; lev < m_particles.size(); lev++) {
1573 for (
MFIter mfi(*m_dummy_mf[0],
false); mfi.
isValid(); ++mfi)
1575 Box grid = ParticleBoxArray(0)[mfi.index()];
1579 host_particles.reserve(15);
1580 host_particles.resize(finestLevel()+1);
1583 host_real_attribs.reserve(15);
1584 host_real_attribs.resize(finestLevel()+1);
1587 host_int_attribs.reserve(15);
1588 host_int_attribs.resize(finestLevel()+1);
1593 for (
int n = 0; n < n_per_cell; n++)
1596 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1597 constexpr int max_iter = 10;
1599 while (iter < max_iter) {
1601 p.pos(i) =
static_cast<ParticleReal
>(grid_box.
lo(i) + (
r + Real(cell[i]-beg[i]))*dx[i]);
1602 if (p.pos(i) < grid_box.
hi(i)) {
break; }
1608 for (
int i = 0; i < NStructReal; i++) {
1613 p.id() = ParticleType::NextID();
1616 for (
int i = 0; i < NStructInt; i++) {
1621 if (!Where(p, pld)) {
1622 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitNRandomPerCell(): invalid particle");
1628 host_particles[pld.
m_lev][ind].push_back(p);
1631 for (
int i = 0; i < NArrayReal; i++) {
1636 for (
int i = 0; i < NArrayInt; i++) {
1642 for (
int host_lev = 0; host_lev < static_cast<int>(host_particles.
size()); ++host_lev)
1644 for (
auto& kv : host_particles[host_lev]) {
1645 auto gid = kv.first.first;
1646 auto tid = kv.first.second;
1647 const auto& src_tid = kv.second;
1649 auto& dst_tile = GetParticles(host_lev)[std::make_pair(gid,tid)];
1650 auto old_size = dst_tile.GetArrayOfStructs().
size();
1651 auto new_size = old_size + src_tid.size();
1652 dst_tile.resize(new_size);
1655 dst_tile.GetArrayOfStructs().begin() + old_size);
1657 for (
int i = 0; i < NArrayReal; ++i)
1660 host_real_attribs[host_lev][std::make_pair(gid,tid)][i].
begin(),
1661 host_real_attribs[host_lev][std::make_pair(gid,tid)][i].
end(),
1662 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
1665 for (
int i = 0; i < NArrayInt; ++i)
1668 host_int_attribs[host_lev][std::make_pair(gid,tid)][i].
begin(),
1669 host_int_attribs[host_lev][std::make_pair(gid,tid)][i].
end(),
1670 dst_tile.GetStructOfArrays().GetIntData(i).begin() + old_size);
1683 amrex::Print() <<
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitNRandomPerCell() time: " << stoptime <<
'\n';
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:551
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_D_TERM(a, b, c)
Definition AMReX_SPACE.H:129
Print on all processors of the default communicator.
Definition AMReX_Print.H:117
AMREX_GPU_HOST_DEVICE void next(IntVectND< dim > &) const noexcept
Step through the rectangle. It is a runtime error to give a point not inside rectangle....
Definition AMReX_Box.H:1059
AMREX_GPU_HOST_DEVICE const IntVectND< dim > & smallEnd() const &noexcept
Get the smallend of the BoxND.
Definition AMReX_Box.H:105
AMREX_GPU_HOST_DEVICE const IntVectND< dim > & bigEnd() const &noexcept
Get the bigend.
Definition AMReX_Box.H:116
const Real * CellSize() const noexcept
Returns the cellsize for each coordinate direction.
Definition AMReX_CoordSys.H:71
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:73
const Real * ProbHi() const noexcept
Returns the hi end of the problem domain in each dimension.
Definition AMReX_Geometry.H:180
const RealBox & ProbDomain() const noexcept
Returns the problem domain.
Definition AMReX_Geometry.H:170
Real ProbLength(int dir) const noexcept
Returns length of problem domain in specified dimension.
Definition AMReX_Geometry.H:208
const Real * ProbLo() const noexcept
Returns the lo end of the problem domain in each dimension.
Definition AMReX_Geometry.H:178
static void streamSynchronize() noexcept
Definition AMReX_GpuDevice.cpp:681
Definition AMReX_MFIter.H:57
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:141
Definition AMReX_PODVector.H:262
void pop_back() noexcept
Definition AMReX_PODVector.H:587
T & back() noexcept
Definition AMReX_PODVector.H:605
bool empty() const noexcept
Definition AMReX_PODVector.H:595
void push_back(const T &a_value)
Definition AMReX_PODVector.H:572
A distributed container for Particles sorted onto the levels, grids, and tiles of a block-structured ...
Definition AMReX_ParticleContainer.H:146
std::map< std::pair< int, int >, ParticleTileType > ParticleLevel
Definition AMReX_ParticleContainer.H:187
void InitNRandomPerCell(int n_per_cell, const ParticleInitData &pdata)
This initializes the particle container with n_per_cell randomly distributed particles per cell,...
Definition AMReX_ParticleInit.H:1549
typename AoS::ParticleVector ParticleVector
Definition AMReX_ParticleContainer.H:193
void InitOnePerCell(Real x_off, Real y_off, Real z_off, const ParticleInitData &pdata)
This initializes the particle container with one particle per cell, where the other particle data and...
Definition AMReX_ParticleInit.H:1465
void InitRandom(Long icount, ULong iseed, const ParticleInitData &pdata, bool serialize=false, RealBox bx=RealBox())
This initializes the particle container with icount randomly distributed particles....
Definition AMReX_ParticleInit.H:970
void InitFromBinaryMetaFile(const std::string &file, int extradata)
Definition AMReX_ParticleInit.H:931
void InitFromBinaryFile(const std::string &file, int extradata)
Definition AMReX_ParticleInit.H:485
T_ParticleType ParticleType
Definition AMReX_ParticleContainer.H:148
This class provides the user with a few print options.
Definition AMReX_Print.H:35
A Box with real dimensions. A RealBox is OK iff volume >= 0.
Definition AMReX_RealBox.H:21
AMREX_GPU_HOST_DEVICE bool ok() const noexcept
Is the RealBox OK; i.e. does it have non-negative volume?
Definition AMReX_RealBox.H:81
void setLo(const Real *a_lo) noexcept
Sets lo side.
Definition AMReX_RealBox.H:64
AMREX_GPU_HOST_DEVICE const Real * lo() const &noexcept
Returns lo side.
Definition AMReX_RealBox.H:46
AMREX_GPU_HOST_DEVICE bool contains(const Real *point, Real eps=0.0) const noexcept
Is the specified point contained in the RealBox?
Definition AMReX_RealBox.H:102
void setHi(const Real *a_hi) noexcept
Sets hi side.
Definition AMReX_RealBox.H:72
AMREX_GPU_HOST_DEVICE const Real * hi() const &noexcept
Returns hide side.
Definition AMReX_RealBox.H:51
AMREX_GPU_HOST_DEVICE Real length(int dir) const noexcept
Returns length in specified direction.
Definition AMReX_RealBox.H:62
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:27
T * dataPtr() noexcept
get access to the underlying data pointer
Definition AMReX_Vector.H:46
Long size() const noexcept
Definition AMReX_Vector.H:50
@ IO_Buffer_Size
Definition AMReX_VisMFBuffer.H:16
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition AMReX_GpuContainers.H:233
static constexpr HostToDevice hostToDevice
Definition AMReX_GpuContainers.H:98
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:237
int MyProc() noexcept
return the rank number local to the current Parallel Context
Definition AMReX_ParallelDescriptor.H:125
void ReduceLongMax(Long &)
Long max reduction.
Definition AMReX_ParallelDescriptor.cpp:1227
void ReduceLongSum(Long &)
Long sum reduction.
Definition AMReX_ParallelDescriptor.cpp:1226
void Bcast(void *, int, MPI_Datatype, int, MPI_Comm)
Definition AMReX_ParallelDescriptor.cpp:1285
int NProcs() noexcept
return the number of MPI ranks local to the current Parallel Context
Definition AMReX_ParallelDescriptor.H:243
int IOProcessorNumber() noexcept
Definition AMReX_ParallelDescriptor.H:266
bool IOProcessor() noexcept
Is this CPU the I/O Processor? To get the rank number, call IOProcessorNumber()
Definition AMReX_ParallelDescriptor.H:275
void ReduceRealMax(Vector< std::reference_wrapper< Real > > const &)
Definition AMReX_ParallelDescriptor.cpp:1218
Definition AMReX_Amr.cpp:49
void FileOpenFailed(const std::string &file)
Output a message and abort when couldn't open the file.
Definition AMReX_Utility.cpp:131
void InitRandom(ULong cpu_seed, int nprocs, ULong gpu_seed)
Set the seed of the random number generator.
Definition AMReX_Random.cpp:89
void copyParticles(DstTile &dst, const SrcTile &src) noexcept
Copy particles from src to dst. This version copies all the particles, writing them to the beginning ...
Definition AMReX_ParticleTransformation.H:158
Real Random()
Generate a psuedo-random double from uniform distribution.
Definition AMReX_Random.cpp:123
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 end(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:1890
double second() noexcept
Definition AMReX_Utility.cpp:922
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:127
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition AMReX.cpp:224
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE Dim3 begin(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:1881
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:230
const int[]
Definition AMReX_BLProfiler.cpp:1664
Definition AMReX_Particle.H:140
Definition AMReX_Particle.H:37
A struct used to pass initial data into the various Init methods. This struct is used to pass initial...
Definition AMReX_ParticleContainer.H:117
std::array< int, NStructInt > int_struct_data
Definition AMReX_ParticleContainer.H:119
std::array< int, NArrayInt > int_array_data
Definition AMReX_ParticleContainer.H:121
std::array< double, NArrayReal > real_array_data
Definition AMReX_ParticleContainer.H:120
std::array< double, NStructReal > real_struct_data
Definition AMReX_ParticleContainer.H:118
A struct used for storing a particle's position in the AMR hierarchy.
Definition AMReX_ParticleContainer.H:92
int m_grid
Definition AMReX_ParticleContainer.H:94
int m_tile
Definition AMReX_ParticleContainer.H:95
int m_lev
Definition AMReX_ParticleContainer.H:93
Definition AMReX_ParticleTile.H:702
void push_back(const ParticleType &p)
Definition AMReX_ParticleTile.H:923
int numParticles() const
Returns the number of real particles (excluding neighbors)
Definition AMReX_ParticleTile.H:845
void push_back_int(int comp, int v)
Definition AMReX_ParticleTile.H:1019
void push_back_real(int comp, ParticleReal v)
Definition AMReX_ParticleTile.H:967
The struct used to store particles.
Definition AMReX_Particle.H:295
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE ParticleIDWrapper id() &
Definition AMReX_Particle.H:315
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE RealVect pos() const &
Definition AMReX_Particle.H:338
AMREX_GPU_HOST_DEVICE AMREX_FORCE_INLINE ParticleCPUWrapper cpu() &
Definition AMReX_Particle.H:312