1#ifndef AMREX_PARTICLEINIT_H
2#define AMREX_PARTICLEINIT_H
3#include <AMReX_Config.H>
38template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
39 template<
class>
class Allocator,
class CellAssignor>
41ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor>
42::InitFromAsciiFile (
const std::string& file,
int extradata,
const IntVect* Nrep)
44 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitFromAsciiFile()");
52 int NReaders = MaxReaders();
63 else if (NProcs <= 4096)
65 NReaders = std::max(NReaders,128);
68 else if (NProcs <= 8192)
70 NReaders = std::max(NReaders,384);
73 else if (NProcs <= 16384)
75 NReaders = std::max(NReaders,512);
83 if (Nrep !=
nullptr) {
88 Long how_many_read = 0;
92 if (extradata > NStructReal) { nreals.resize(extradata - NStructReal); }
94 if (MyProc < NReaders)
100 ifs.rdbuf()->pubsetbuf(io_buffer.
dataPtr(), io_buffer.
size());
102 ifs.open(file.c_str(), std::ios::in);
111 ifs >> cnt >> std::ws;
117 if (extradata > NStructReal) { r.resize(extradata - NStructReal); }
119 const int Chunk = cnt / NReaders;
121 for (
int i = 0; i < MyProc*Chunk; i++)
123 ifs.ignore(std::numeric_limits<std::streamsize>::max(),
'\n');
129 std::string msg(
"ParticleContainer::InitFromAsciiFile(");
131 msg +=
") failed @ 1";
137 if (MyProc == (NReaders - 1))
139 MyCnt += cnt % NReaders;
144 for (
int i = 0; i < MyCnt; i++)
150 for (
int n = 0; n < extradata; n++)
158 ifs >> r[n - NStructReal];
164 std::string msg(
"ParticleContainer::InitFromAsciiFile(");
165 msg += file; msg +=
") failed @ 2";
176 amrex::AllPrint() <<
"BAD PARTICLE ID WOULD BE " << ParticleType::NextID() <<
'\n'
177 <<
"BAD PARTICLE POS "
183 amrex::Abort(
"ParticleContainer::InitFromAsciiFile(): invalid particle");
188 p.id() = ParticleType::NextID();
192 if(nreals.
size() > extradata - NStructReal) {
193 for (
int n = NStructReal; n < extradata; n++)
195 nreals[n-NStructReal].push_back(r[n-NStructReal]);
202 const Real DomSize[AMREX_SPACEDIM] =
206 int rep[AMREX_SPACEDIM];
208#if AMREX_SPACEDIM > 2
209 for (rep[2] = 1; rep[2] <= lNrep[2]; rep[2]++)
212#if AMREX_SPACEDIM > 1
213 for (rep[1] = 1; rep[1] <= lNrep[1]; rep[1]++)
216 for (rep[0] = 1; rep[0] <= lNrep[0]; rep[0]++)
218 if (!(
AMREX_D_TERM( (rep[0] == 1), && (rep[1] == 1), && (rep[2] == 1) ) ) )
221 for (
int d=0; d<AMREX_SPACEDIM; ++d)
223 p_rep.pos(d) =
static_cast<ParticleReal>(p.pos(d) +
Real(rep[d]-1)*DomSize[d]);
227 for (
int n = 0; n < extradata; n++)
231 p_rep.rdata(n) = p.rdata(n);
235 if (!Where(p_rep, pld))
237 PeriodicShift(p_rep);
238 if (!Where(p_rep, pld))
241 amrex::AllPrint() <<
"BAD REPLICATED PARTICLE ID WOULD BE " << ParticleType::NextID() <<
"\n";
243 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromAsciiFile(): invalid replicated particle");
247 p_rep.id() = ParticleType::NextID();
248 p_rep.cpu() = MyProc;
252 if (nreals.
size() > extradata - NStructReal) {
253 for (
int n = NStructReal; n < extradata; n++)
255 nreals[n-NStructReal].push_back(r[n-NStructReal]);
262#if AMREX_SPACEDIM > 1
265#if AMREX_SPACEDIM > 2
274 int NRedist_chunk = NReaders / NRedist;
278 for (
int nr = 0; nr < NRedist; nr++)
281 host_particles.reserve(15);
282 host_particles.resize(finestLevel()+1);
285 host_real_attribs.reserve(15);
286 host_real_attribs.resize(finestLevel()+1);
290 << nr*NRedist_chunk <<
" to "
291 << (nr+1)*NRedist_chunk-1 <<
'\n';
294 for (
int which = nr*NRedist_chunk; which < (nr+1)*NRedist_chunk; which++)
298 while (!nparticles.
empty())
305 if (nreals.
size() > extradata - NStructReal && NArrayReal > 0)
307 for (
int n = NStructReal; n < extradata; n++)
309 Real rdata = nreals[n-NStructReal].back();
310 host_real_attribs[pld.
m_lev][std::make_pair(pld.
m_grid, pld.
m_tile)][n-NStructReal].push_back(rdata);
316 if (nreals.
size() > extradata - NStructReal)
318 for (
int n = NStructReal; n < extradata; n++)
320 nreals[n-NStructReal].pop_back();
327 for (
int lev = 0; lev < std::ssize(host_particles); ++lev)
329 for (
auto& kv : host_particles[lev])
332 auto grid = kv.first.first;
333 auto tile = kv.first.second;
334 const auto& src_tile = kv.second;
336 auto& dst_tile = GetParticles(lev)[std::make_pair(grid,tile)];
337 auto old_size = dst_tile.GetArrayOfStructs().
size();
338 auto new_size = old_size + src_tile.size();
339 dst_tile.resize(new_size);
342 dst_tile.GetArrayOfStructs().begin() + old_size);
344 if (std::ssize(host_real_attribs[lev][std::make_pair(grid, tile)]) > NArrayReal) {
345 for (
int i = 0; i < NArrayReal; ++i) {
347 host_real_attribs[lev][std::make_pair(grid,tile)][i].
begin(),
348 host_real_attribs[lev][std::make_pair(grid,tile)][i].
end(),
349 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
361 if (NRedist*NRedist_chunk < NReaders) {
363 << NRedist*NRedist_chunk <<
" to "
367 for (
int which = NRedist*NRedist_chunk; which < NReaders; which++)
370 host_particles.reserve(15);
371 host_particles.resize(finestLevel()+1);
374 host_real_attribs.reserve(15);
375 host_real_attribs.resize(finestLevel()+1);
379 while (!nparticles.
empty())
385 if (std::ssize(host_real_attribs[pld.
m_lev][std::make_pair(pld.
m_grid, pld.
m_tile)]) >
386 extradata - NStructReal) {
387 for (
int n = NStructReal; n < extradata; n++)
389 Real rdata = nreals[n-NStructReal].back();
390 host_real_attribs[pld.
m_lev][std::make_pair(pld.
m_grid, pld.
m_tile)][n-NStructReal].push_back(rdata);
396 if (nreals.
size() > extradata - NStructReal) {
397 for (
int n = NStructReal; n < extradata; n++)
399 nreals[n-NStructReal].pop_back();
405 for (
int lev = 0; lev < std::ssize(host_particles); ++lev)
407 for (
auto& kv : host_particles[lev])
409 auto grid = kv.first.first;
410 auto tile = kv.first.second;
411 const auto& src_tile = kv.second;
413 auto& dst_tile = GetParticles(lev)[std::make_pair(grid,tile)];
414 auto old_size = dst_tile.GetArrayOfStructs().
size();
415 auto new_size = old_size + src_tile.size();
416 dst_tile.resize(new_size);
419 dst_tile.GetArrayOfStructs().begin() + old_size);
421 for (
int i = 0; i < NArrayReal; ++i) {
423 host_real_attribs[lev][std::make_pair(grid,tile)][i].
begin(),
424 host_real_attribs[lev][std::make_pair(grid,tile)][i].
end(),
425 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
439 Long num_particles = how_many;
443 if (
AMREX_D_TERM(lNrep[0] == 1, && lNrep[1] == 1, && lNrep[2] == 1))
445 amrex::Print() <<
"Total number of particles: " << num_particles <<
'\n';
449 Long num_particles_read = how_many_read;
454 <<
AMREX_D_TERM(lNrep[0] <<
" ", << lNrep[1] <<
" ", << lNrep[2]) <<
"\n"
455 <<
"Total number of particles read in : " << num_particles_read <<
'\n'
456 <<
"Total number of particles after replication: " << num_particles <<
'\n';
485template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
486 template<
class>
class Allocator,
class CellAssignor>
492 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitFromBinaryFile()");
505 const int NReaders = MaxReaders();
511 const Long NPartPerRedist = MaxParticlesPerRead();
515 amrex::Print() <<
"Reading with " << NReaders <<
" readers\n"
516 <<
"Redistributing after every " << NPartPerRedist <<
" particles for each reader\n";
524 tmp_particles.reserve(15);
525 tmp_particles.resize(finestLevel()+1);
543 std::set<int> readers;
549 if (NReaders == NProcs)
554 for (
int i = 0; i < NProcs; i++) {
581 while (std::ssize(readers) < NReaders);
587 for (
auto it = readers.cbegin(), End = readers.cend();
603 readers.insert(rprocs.begin(), rprocs.end());
610 int RealSizeInFile = 0;
612 if (readers.contains(MyProc))
616 ifs.rdbuf()->pubsetbuf(io_buffer.
dataPtr(), io_buffer.
size());
618 ifs.open(file.c_str(), std::ios::in|std::ios::binary);
624 ifs.read((
char*)&NP,
sizeof(NP));
625 ifs.read((
char*)&DM,
sizeof(DM));
626 ifs.read((
char*)&NX,
sizeof(NX));
631 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): NP <= 0");
636 if (DM != AMREX_SPACEDIM) {
637 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): DM != AMREX_SPACEDIM");
642 if (NX < 0 || NX > NStructReal) {
643 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): NX < 0 || NX > N");
648 if (extradata > NX) {
649 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): extradata > NX");
656 const std::streamoff CURPOS = ifs.tellg();
660 ifs.seekg(0,std::ios::end);
664 const std::streamoff ENDPOS = ifs.tellg();
666 RealSizeInFile =
int((ENDPOS - CURPOS) / (NP*(DM+NX)));
668 AMREX_ASSERT(RealSizeInFile ==
sizeof(
float) || RealSizeInFile ==
sizeof(
double));
672 ifs.seekg(CURPOS, std::ios::beg);
677 for ( ;
id < NReaders;
id++) {
678 if (rprocs[
id] == MyProc) {
685 const std::streamoff NSKIP =
id * (NP/NReaders) * (DM+NX) * RealSizeInFile;
689 ifs.seekg(NSKIP, std::ios::cur);
694 std::string msg(
"ParticleContainer::InitFromBinaryFile(");
696 msg +=
") failed @ 1";
707 Long MyCnt = NP / NReaders;
709 if (MyProc == rprocs[NReaders-1]) {
713 MyCnt += NP % NReaders;
716 Long how_many_redists = NP / (NPartPerRedist*NReaders), how_many_read = 0;
718 if (NP % (NPartPerRedist*NReaders)) { how_many_redists++; }
725 fxtra.resize(extradata);
726 dxtra.resize(extradata);
729 if ((NX-extradata) > 0)
731 fignore.resize(NX-extradata);
732 dignore.resize(NX-extradata);
737 for (
int j = 0; j < how_many_redists; j++)
741 host_particles.reserve(15);
742 host_particles.resize(finestLevel()+1);
744 if (readers.contains(MyProc))
750 const Long NRead = std::min((MyCnt-how_many_read), NPartPerRedist);
752 for (
Long i = 0; i < NRead; i++)
758 if (RealSizeInFile ==
sizeof(
float))
760 float fpos[AMREX_SPACEDIM];
762 ifs.read((
char*)&fpos[0], AMREX_SPACEDIM*
sizeof(
float));
766 p.pos(2) = fpos[2];);
769 else if (RealSizeInFile ==
sizeof(
double))
771 double dpos[AMREX_SPACEDIM];
773 ifs.read((
char*)&dpos[0], AMREX_SPACEDIM*
sizeof(double));
785 if (RealSizeInFile ==
sizeof(
float))
787 ifs.read((
char*)fxtra.data(), std::streamsize(extradata*
sizeof(
float)));
789 for (
int ii = 0; ii < extradata; ii++) {
793 else if (RealSizeInFile ==
sizeof(
double))
795 ifs.read((
char*)dxtra.data(), std::streamsize(extradata*
sizeof(
double)));
797 for (
int ii = 0; ii < extradata; ii++) {
805 if ((NX-extradata) > 0)
807 if (RealSizeInFile ==
sizeof(
float))
809 ifs.read((
char*)fignore.data(), std::streamsize((NX-extradata)*
sizeof(
float)));
811 else if (RealSizeInFile ==
sizeof(
double))
813 ifs.read((
char*)dignore.data(), std::streamsize((NX-extradata)*
sizeof(
double)));
819 std::string msg(
"ParticleContainer::InitFromBinaryFile(");
821 msg +=
") failed @ 2";
832 amrex::AllPrint() <<
"BAD PARTICLE ID WOULD BE " << ParticleType::NextID() <<
'\n'
833 <<
"BAD PARTICLE POS "
839 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitFromBinaryFile(): invalid particle");
843 p.id() = ParticleType::NextID();
849 how_many_read += NRead;
852 for (
int host_lev = 0; host_lev < std::ssize(host_particles); ++host_lev)
854 for (
auto& kv : host_particles[host_lev]) {
855 auto grid = kv.first.first;
856 auto tile = kv.first.second;
857 const auto& src_tile = kv.second;
859 auto& dst_tile = GetParticles(host_lev)[std::make_pair(grid,tile)];
860 auto old_size = dst_tile.GetArrayOfStructs().
size();
861 auto new_size = old_size + src_tile.size();
862 dst_tile.resize(new_size);
865 dst_tile.GetArrayOfStructs().begin() + old_size);
877 for (
int lev = 0; lev < std::ssize(m_particles); lev++)
879 auto& pmap = m_particles[lev];
880 auto& tmp_pmap = tmp_particles[lev];
882 for (
auto& kv : pmap) {
883 auto& aos = kv.second.GetArrayOfStructs()();
884 auto& tmp_aos = tmp_pmap[kv.first].GetArrayOfStructs()();
886 tmp_aos.insert(tmp_aos.end(), aos.begin(), aos.end());
896 tmp_particles.swap(m_particles);
902 Long num_particles_read = how_many_read;
906 amrex::Print() <<
"\nTotal number of particles: " << num_particles_read <<
'\n';
919 amrex::Print() <<
"InitFromBinaryFile() time: " << runtime <<
'\n';
931template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
932 template<
class>
class Allocator,
class CellAssignor>
938 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitFromBinaryMetaFile()");
941 std::ifstream ifs(metafile.c_str(), std::ios::in);
947 std::getline(ifs,file);
949 if (!ifs.good()) {
break; }
952 amrex::Print() <<
"InitFromBinaryMetaFile: processing file: " << file <<
'\n';
955 InitFromBinaryFile(file, extradata);
966 amrex::Print() <<
"InitFromBinaryMetaFile() time: " << runtime <<
'\n';
970template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
971 template<
class>
class Allocator,
class CellAssignor>
980 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitRandom()");
998 if (!containing_bx.
ok()) { containing_bx = geom.
ProbDomain(); }
1007 const Real* xlo = containing_bx.
lo();
1008 const Real* xhi = containing_bx.
hi();
1014 if(icount*AMREX_SPACEDIM >= std::numeric_limits<int>::max())
1017 "InitRandom has serialize=true, but this would cause too much "
1018 "particle data to be sent from IOProc. Set serialize=false, "
1019 "or use fewer than " +
1039 for (
Long j = 0; j < icount; j++)
1041 for (
int i = 0; i < AMREX_SPACEDIM; i++)
1046 x = geom.
ProbLo(i) + (r * len[i]);
1060 host_particles.reserve(15);
1061 host_particles.resize(finestLevel()+1);
1064 host_real_attribs.reserve(15);
1065 host_real_attribs.resize(finestLevel()+1);
1068 host_int_attribs.reserve(15);
1069 host_int_attribs.resize(finestLevel()+1);
1072 host_idcpu.reserve(15);
1073 host_idcpu.resize(finestLevel()+1);
1075 for (
Long j = 0; j < icount; j++)
1079 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1080 ptest.
pos(i) = pos[j*AMREX_SPACEDIM + i];
1083 if (!Where(ptest, pld)) {
1084 amrex::Abort(
"ParticleContainer::InitRandom(): invalid particle");
1090 const int who = ParticleDistributionMap(pld.
m_lev)[pld.
m_grid];
1092 if (who == MyProc) {
1094 if constexpr(!ParticleType::is_soa_particle)
1097 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1098 p.pos(i) = pos[j*AMREX_SPACEDIM + i];
1102 p.id() = ParticleType::NextID();
1105 for (
int i = 0; i < NStructInt; i++) {
1109 for (
int i = 0; i < NStructReal; i++) {
1114 host_particles[pld.
m_lev][ind].push_back(p);
1117 for (
int i = 0; i < NArrayReal; i++) {
1122 for (
int i = 0; i < NArrayInt; i++) {
1126 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1127 host_real_attribs[pld.
m_lev][ind][i].push_back(pos[j*AMREX_SPACEDIM+i]);
1130 host_idcpu[pld.
m_lev][ind].push_back(0);
1134 host_particles[pld.
m_lev][ind];
1137 for (
int i = AMREX_SPACEDIM; i < NArrayReal; i++) {
1142 for (
int i = 2; i < NArrayInt; i++) {
1149 for (
int host_lev = 0; host_lev < std::ssize(host_particles); ++host_lev)
1151 for (
auto& kv : host_particles[host_lev]) {
1152 auto grid = kv.first.first;
1153 auto tile = kv.first.second;
1154 const auto& src_tile = kv.second;
1156 auto& dst_tile = GetParticles(host_lev)[std::make_pair(grid,tile)];
1157 auto old_size = dst_tile.
size();
1158 auto new_size = old_size;
1159 if constexpr(!ParticleType::is_soa_particle)
1161 new_size += src_tile.size();
1163 new_size += host_real_attribs[host_lev][std::make_pair(grid,tile)][0].
size();
1165 dst_tile.resize(new_size);
1167 if constexpr(!ParticleType::is_soa_particle)
1170 dst_tile.GetArrayOfStructs().begin() + old_size);
1173 host_idcpu[host_lev][std::make_pair(grid,tile)].
begin(),
1174 host_idcpu[host_lev][std::make_pair(grid,tile)].
end(),
1175 dst_tile.GetStructOfArrays().GetIdCPUData().begin() + old_size);
1178 for (
int i = 0; i < NArrayReal; ++i) {
1180 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].
begin(),
1181 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].
end(),
1182 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
1185 for (
int i = 0; i < NArrayInt; ++i) {
1187 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].
begin(),
1188 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].
end(),
1189 dst_tile.GetStructOfArrays().GetIntData(i).begin() + old_size);
1200 Long M = icount / NProcs;
1203 M += (icount % NProcs);
1209 host_particles.reserve(15);
1210 host_particles.resize(finestLevel()+1);
1213 host_real_attribs.reserve(15);
1214 host_real_attribs.resize(finestLevel()+1);
1217 host_int_attribs.reserve(15);
1218 host_int_attribs.resize(finestLevel()+1);
1221 host_idcpu.reserve(15);
1222 host_idcpu.resize(finestLevel()+1);
1224 for (
Long icnt = 0; icnt < M; icnt++) {
1226 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1229 x = geom.
ProbLo(i) + (r * len[i]);
1238 ptest.
id() = ParticleType::NextID();
1242 if (!Where(ptest, pld))
1244 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitRandom(): invalid particle");
1249 if constexpr(!ParticleType::is_soa_particle)
1252 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1253 p.pos(i) = ptest.
pos(i);;
1256 p.id() = ptest.
id();
1257 p.cpu() = ptest.
cpu();
1259 for (
int i = 0; i < NStructReal; i++) {
1263 for (
int i = 0; i < NStructInt; i++) {
1268 host_particles[pld.
m_lev][ind].push_back(p);
1271 for (
int i = 0; i < NArrayReal; i++) {
1276 for (
int i = 0; i < NArrayInt; i++) {
1280 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1281 host_real_attribs[pld.
m_lev][ind][i].push_back(ptest.
pos(i));
1284 host_idcpu[pld.
m_lev][ind].push_back(0);
1288 host_particles[pld.
m_lev][ind];
1291 for (
int i = AMREX_SPACEDIM; i < NArrayReal; i++) {
1296 for (
int i = 2; i < NArrayInt; i++) {
1302 for (
int host_lev = 0; host_lev < std::ssize(host_particles); ++host_lev)
1304 for (
auto& kv : host_particles[host_lev]) {
1305 auto grid = kv.first.first;
1306 auto tile = kv.first.second;
1307 const auto& src_tile = kv.second;
1309 auto& dst_tile = GetParticles(host_lev)[std::make_pair(grid,tile)];
1310 auto old_size = dst_tile.
size();
1311 auto new_size = old_size;
1312 if constexpr(!ParticleType::is_soa_particle)
1314 new_size += src_tile.size();
1316 new_size += host_real_attribs[host_lev][std::make_pair(grid,tile)][0].
size();
1318 dst_tile.resize(new_size);
1320 if constexpr(!ParticleType::is_soa_particle)
1323 dst_tile.GetArrayOfStructs().begin() + old_size);
1326 host_idcpu[host_lev][std::make_pair(grid,tile)].
begin(),
1327 host_idcpu[host_lev][std::make_pair(grid,tile)].
end(),
1328 dst_tile.GetStructOfArrays().GetIdCPUData().begin() + old_size);
1331 for (
int i = 0; i < NArrayReal; ++i) {
1333 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].
begin(),
1334 host_real_attribs[host_lev][std::make_pair(grid,tile)][i].
end(),
1335 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
1338 for (
int i = 0; i < NArrayInt; ++i) {
1340 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].
begin(),
1341 host_int_attribs[host_lev][std::make_pair(grid,tile)][i].
end(),
1342 dst_tile.GetStructOfArrays().GetIntData(i).begin() + old_size);
1358 amrex::Print() <<
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitRandom() time: " << stoptime <<
'\n';
1364template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
1365 template<
class>
class Allocator,
class CellAssignor>
1372 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitRandomPerBox()");
1389 std::mt19937 mt(iseed);
1390 std::uniform_real_distribution<double> dist(0.0, 1.0);
1392 m_particles.resize(m_gdb->finestLevel()+1);
1394 for (
int lev = 0; lev < std::ssize(m_particles); lev++)
1400 for (
MFIter mfi(*m_dummy_mf[0],
false); mfi.
isValid(); ++mfi)
1402 Box grid = m_gdb->ParticleBoxArray(0)[mfi.index()];
1405 for (
Long icnt = 0; icnt < icount_per_box; icnt++) {
1406 for (
Long jcnt = 0; jcnt < icount_per_box; jcnt++) {
1407 for (
Long kcnt = 0; kcnt < icount_per_box; kcnt++)
1410 p.pos(0) =
static_cast<ParticleReal>(grid_box.
lo(0) + (dist(mt) +
double(icnt)) /
double(icount_per_box) * grid_box.
length(0));,
1411 p.pos(1) =
static_cast<ParticleReal>(grid_box.
lo(1) + (dist(mt) + double(jcnt)) /
double(icount_per_box) * grid_box.
length(1));,
1412 p.pos(2) =
static_cast<ParticleReal>(grid_box.
lo(2) + (dist(mt) + double(kcnt)) /
double(icount_per_box) * grid_box.
length(2));
1415 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1420 for (
int i = 0; i < NStructReal; i++) {
1425 p.id() = ParticleType::NextID();
1428 for (
int i = 0; i < NStructInt; i++) {
1433 if (!Where(p, pld)) {
1434 amrex::Abort(
"ParticleContainer::InitRandomPerBox(): invalid particle");
1440 m_particles[pld.
m_lev][ind].push_back(p);
1443 for (
int i = 0; i < NArrayReal; i++) {
1448 for (
int i = 0; i < NArrayInt; i++) {
1461 amrex::Print() <<
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitRandomPerBox() time: " << stoptime <<
'\n';
1465template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
1466 template<
class>
class Allocator,
class CellAssignor>
1473 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitOnePerCell()");
1479 AMREX_ASSERT(x_off >= 0. && y_off >= 0. && z_off >= 0.);
1480 AMREX_ASSERT(x_off <= 1. && y_off <= 1. && z_off <= 1.);
1491 for (
MFIter mfi(*m_dummy_mf[0],
false); mfi.
isValid(); ++mfi) {
1492 Box grid = ParticleBoxArray(0)[mfi.index()];
1493 auto ind = std::make_pair(mfi.index(), mfi.LocalTileIndex());
1500 p.pos(1) =
static_cast<ParticleReal>(grid_box.
lo(1) + (y_off + cell[1]-beg[1])*dx[1]);,
1501 p.pos(2) =
static_cast<ParticleReal>(grid_box.
lo(2) + (z_off + cell[2]-beg[2])*dx[2]););
1503 for (
int d = 0; d < AMREX_SPACEDIM; ++d) {
1507 for (
int i = 0; i < NStructReal; i++) {
1512 p.id() = ParticleType::NextID();
1515 for (
int i = 0; i < NStructInt; i++) {
1523 for (
int i = 0; i < NArrayReal; i++) {
1528 for (
int i = 0; i < NArrayInt; i++) {
1540 if (m_verbose > 1) {
1545 amrex::Print() <<
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitOnePerCell() time: " << stoptime <<
'\n';
1549template <
typename ParticleType,
int NArrayReal,
int NArrayInt,
1550 template<
class>
class Allocator,
class CellAssignor>
1555 BL_PROFILE(
"ParticleContainer<NSR, NSI, NAR, NAI>::InitNRandomPerCell()");
1568 for (
int lev = 0; lev < std::ssize(m_particles); lev++) {
1577 for (
MFIter mfi(*m_dummy_mf[0],
false); mfi.
isValid(); ++mfi)
1579 Box grid = ParticleBoxArray(0)[mfi.index()];
1583 host_particles.reserve(15);
1584 host_particles.resize(finestLevel()+1);
1587 host_real_attribs.reserve(15);
1588 host_real_attribs.resize(finestLevel()+1);
1591 host_int_attribs.reserve(15);
1592 host_int_attribs.resize(finestLevel()+1);
1597 for (
int n = 0; n < n_per_cell; n++)
1600 for (
int i = 0; i < AMREX_SPACEDIM; i++) {
1601 constexpr int max_iter = 10;
1603 while (iter < max_iter) {
1605 p.pos(i) =
static_cast<ParticleReal>(grid_box.
lo(i) + (r +
Real(cell[i]-beg[i]))*dx[i]);
1606 if (p.pos(i) < grid_box.
hi(i)) {
break; }
1612 for (
int i = 0; i < NStructReal; i++) {
1617 p.id() = ParticleType::NextID();
1620 for (
int i = 0; i < NStructInt; i++) {
1625 if (!Where(p, pld)) {
1626 amrex::Abort(
"ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt>::InitNRandomPerCell(): invalid particle");
1632 host_particles[pld.
m_lev][ind].push_back(p);
1635 for (
int i = 0; i < NArrayReal; i++) {
1640 for (
int i = 0; i < NArrayInt; i++) {
1646 for (
int host_lev = 0; host_lev < std::ssize(host_particles); ++host_lev)
1648 for (
auto& kv : host_particles[host_lev]) {
1649 auto gid = kv.first.first;
1650 auto tid = kv.first.second;
1651 const auto& src_tid = kv.second;
1653 auto& dst_tile = GetParticles(host_lev)[std::make_pair(gid,tid)];
1654 auto old_size = dst_tile.GetArrayOfStructs().
size();
1655 auto new_size = old_size + src_tid.size();
1656 dst_tile.resize(new_size);
1659 dst_tile.GetArrayOfStructs().begin() + old_size);
1661 for (
int i = 0; i < NArrayReal; ++i)
1664 host_real_attribs[host_lev][std::make_pair(gid,tid)][i].
begin(),
1665 host_real_attribs[host_lev][std::make_pair(gid,tid)][i].
end(),
1666 dst_tile.GetStructOfArrays().GetRealData(i).begin() + old_size);
1669 for (
int i = 0; i < NArrayInt; ++i)
1672 host_int_attribs[host_lev][std::make_pair(gid,tid)][i].
begin(),
1673 host_int_attribs[host_lev][std::make_pair(gid,tid)][i].
end(),
1674 dst_tile.GetStructOfArrays().GetIntData(i).begin() + old_size);
1687 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:172
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
Print on all processors of the default communicator.
Definition AMReX_Print.H:113
__host__ __device__ const IntVectND< dim > & bigEnd() const &noexcept
Return the inclusive upper bound of the box.
Definition AMReX_Box.H:123
__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:1121
__host__ __device__ const IntVectND< dim > & smallEnd() const &noexcept
Return the inclusive lower bound of the box.
Definition AMReX_Box.H:111
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:75
const Real * ProbHi() const noexcept
Returns the hi end of the problem domain in each dimension.
Definition AMReX_Geometry.H:186
const RealBox & ProbDomain() const noexcept
Returns the problem domain.
Definition AMReX_Geometry.H:176
Real ProbLength(int dir) const noexcept
Returns length of problem domain in specified dimension.
Definition AMReX_Geometry.H:214
const Real * ProbLo() const noexcept
Returns the lo end of the problem domain in each dimension.
Definition AMReX_Geometry.H:184
static void streamSynchronize() noexcept
Definition AMReX_GpuDevice.cpp:856
Iterator for looping ever tiles and boxes of amrex::FabArray based containers.
Definition AMReX_MFIter.H:88
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:172
Dynamically allocated vector for trivially copyable data.
Definition AMReX_PODVector.H:308
void pop_back() noexcept
Definition AMReX_PODVector.H:644
T & back() noexcept
Definition AMReX_PODVector.H:662
bool empty() const noexcept
Definition AMReX_PODVector.H:652
void push_back(const T &a_value)
Definition AMReX_PODVector.H:629
A distributed container for Particles sorted onto the levels, grids, and tiles of a block-structured ...
Definition AMReX_ParticleContainer.H:149
std::map< std::pair< int, int >, ParticleTileType > ParticleLevel
Definition AMReX_ParticleContainer.H:196
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:1553
typename AoS::ParticleVector ParticleVector
Definition AMReX_ParticleContainer.H:204
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:1469
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:974
void InitFromBinaryMetaFile(const std::string &file, int extradata)
Definition AMReX_ParticleInit.H:935
void InitFromBinaryFile(const std::string &file, int extradata)
Definition AMReX_ParticleInit.H:489
T_ParticleType ParticleType
Definition AMReX_ParticleContainer.H:151
This class provides the user with a few print options.
Definition AMReX_Print.H:35
A Box with real dimensions.
Definition AMReX_RealBox.H:28
__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:109
void setLo(const Real *a_lo) noexcept
Sets lo side.
Definition AMReX_RealBox.H:71
__host__ __device__ const Real * lo() const &noexcept
Returns lo side.
Definition AMReX_RealBox.H:53
__host__ __device__ const Real * hi() const &noexcept
Returns hide side.
Definition AMReX_RealBox.H:58
__host__ __device__ Real length(int dir) const noexcept
Returns length in specified direction.
Definition AMReX_RealBox.H:69
void setHi(const Real *a_hi) noexcept
Sets hi side.
Definition AMReX_RealBox.H:79
__host__ __device__ bool ok() const noexcept
Is the RealBox OK; i.e. does it have non-negative volume?
Definition AMReX_RealBox.H:88
This class is a thin wrapper around std::vector. Unlike vector, Vector::operator[] provides bound che...
Definition AMReX_Vector.H:28
T * dataPtr() noexcept
get access to the underlying data pointer
Definition AMReX_Vector.H:49
Long size() const noexcept
Definition AMReX_Vector.H:53
static constexpr int IO_Buffer_Size
We try to do I/O with buffers of this size.
Definition AMReX_VisMFBuffer.H:16
amrex_real Real
Floating Point Type for Fields.
Definition AMReX_REAL.H:79
amrex_ulong ULong
Unsigned integer type guaranteed to be wider than unsigned int.
Definition AMReX_INT.H:32
amrex_particle_real ParticleReal
Floating Point Type for Particles.
Definition AMReX_REAL.H:90
amrex_long Long
Definition AMReX_INT.H:30
int MyProc() noexcept
Definition AMReX_ParallelDescriptor.H:128
int NProcs() noexcept
Definition AMReX_ParallelDescriptor.H:255
void ReduceLongSum(Long &)
Definition AMReX_ParallelDescriptor.cpp:1236
int IOProcessorNumber() noexcept
The MPI rank number of the I/O Processor (probably rank 0). This rank is usually used to write to std...
Definition AMReX_ParallelDescriptor.H:279
void ReduceLongMax(Long &)
Definition AMReX_ParallelDescriptor.cpp:1237
bool IOProcessor() noexcept
Is this CPU the I/O Processor? To get the rank number, call IOProcessorNumber()
Definition AMReX_ParallelDescriptor.H:289
void InitRandom(ULong cpu_seed, int nprocs, ULong gpu_seed)
Set the seed of the random number generator.
Definition AMReX_Random.cpp:99
Real Random()
Generate a psuedo-random real from uniform distribution.
Definition AMReX_Random.cpp:133
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:228
static constexpr HostToDevice hostToDevice
Definition AMReX_GpuContainers.H:105
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:310
void Bcast(void *, int, MPI_Datatype, int, MPI_Comm)
Definition AMReX_ParallelDescriptor.cpp:1295
void ReduceRealMax(Vector< std::reference_wrapper< Real > > const &)
Definition AMReX_ParallelDescriptor.cpp:1228
Definition AMReX_Amr.cpp:50
__host__ __device__ void ignore_unused(const Ts &...)
This shuts up the compiler about unused variables.
Definition AMReX.H:139
void FileOpenFailed(const std::string &file)
Output a message and abort when couldn't open the file.
Definition AMReX_Utility.cpp:137
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:222
__host__ __device__ Dim3 begin(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:2006
double second() noexcept
Definition AMReX_Utility.cpp:940
void Error(const std::string &msg)
Print out message to cerr and exit via amrex::Abort().
Definition AMReX.cpp:235
void Abort(const std::string &msg)
Print out message to cerr and exit via abort().
Definition AMReX.cpp:241
const int[]
Definition AMReX_BLProfiler.cpp:1664
__host__ __device__ Dim3 end(BoxND< dim > const &box) noexcept
Definition AMReX_Box.H:2015
Definition AMReX_Particle.H:259
Definition AMReX_Particle.H:154
A struct used to pass initial data into the various Init methods. This struct is used to pass initial...
Definition AMReX_ParticleContainer.H:119
std::array< int, NStructInt > int_struct_data
Definition AMReX_ParticleContainer.H:121
std::array< int, NArrayInt > int_array_data
Definition AMReX_ParticleContainer.H:123
std::array< double, NArrayReal > real_array_data
Definition AMReX_ParticleContainer.H:122
std::array< double, NStructReal > real_struct_data
Definition AMReX_ParticleContainer.H:120
A struct used for storing a particle's position in the AMR hierarchy.
Definition AMReX_ParticleContainer.H:93
int m_grid
Definition AMReX_ParticleContainer.H:95
int m_tile
Definition AMReX_ParticleContainer.H:96
int m_lev
Definition AMReX_ParticleContainer.H:94
Definition AMReX_ParticleTile.H:764
void push_back(const ParticleType &p)
Definition AMReX_ParticleTile.H:1036
int numParticles() const
Returns the number of real particles (excluding neighbors)
Definition AMReX_ParticleTile.H:947
void push_back_int(int comp, int v)
Definition AMReX_ParticleTile.H:1140
void push_back_real(int comp, ParticleReal v)
Definition AMReX_ParticleTile.H:1085
The struct used to store particles.
Definition AMReX_Particle.H:405
__host__ __device__ RealVect pos() const &
Definition AMReX_Particle.H:456
__host__ __device__ ParticleCPUWrapper cpu() &
Definition AMReX_Particle.H:424
__host__ __device__ ParticleIDWrapper id() &
Definition AMReX_Particle.H:427