27 #include <AMReX_Utility.H>
28 #include <AMReX_IntVect.H>
29 #include <AMReX_ParmParse.H>
30 #include <AMReX_ParallelDescriptor.H>
31 #include <AMReX_VisMF.H>
32 #include <AMReX_VectorIO.H>
33 #include <AMReX_NFiles.H>
71 namespace fs = std::filesystem;
75 std::filesystem::path path = dir.parent_path() / dataDir /
"amr" /
"yt";
76 dir_m = amrex::Concatenate((path /
"plt").
string(), step, 10);
82 fs::create_directories( path );
83 }
catch(
const fs::filesystem_error& ex) {
100 const double& scale) {
108 amrex::VisMF::SetNOutFiles(64);
113 if (!amrex::UtilCreateDirectory(
dir_m, 0755))
114 amrex::CreateDirectoryFailed(
dir_m);
120 std::string HeaderFileName =
dir_m +
"/Header";
122 amrex::VisMF::IO_Buffer io_buffer(amrex::VisMF::IO_Buffer_Size);
124 std::ofstream HeaderFile;
126 HeaderFile.rdbuf()->pubsetbuf(io_buffer.dataPtr(), io_buffer.size());
128 int nData = rho[0]->nComp()
130 + efield[0][0]->nComp()
131 + efield[0][1]->nComp()
132 + efield[0][2]->nComp();
138 HeaderFile.open(HeaderFileName.c_str(), std::ios::out|std::ios::trunc|std::ios::binary);
139 if (!HeaderFile.good())
140 amrex::FileOpenFailed(HeaderFileName);
141 HeaderFile <<
"HyperCLaw-V1.1\n";
143 HeaderFile << nData <<
'\n';
146 for (
int ivar = 1; ivar <= rho[0]->nComp(); ivar++)
147 HeaderFile <<
"rho\n";
149 for (
int ivar = 1; ivar <= phi[0]->nComp(); ivar++)
150 HeaderFile <<
"phi\n";
152 HeaderFile <<
"Ex\nEy\nEz\n";
155 HeaderFile << AMREX_SPACEDIM <<
'\n';
158 HeaderFile << time <<
'\n';
159 HeaderFile << nLevel - 1 <<
'\n';
162 for (
int i = 0; i < AMREX_SPACEDIM; i++)
163 HeaderFile << geom[0].ProbLo(i) / scale <<
' ';
165 for (
int i = 0; i < AMREX_SPACEDIM; i++)
166 HeaderFile << geom[0].ProbHi(i) / scale <<
' ';
170 for (
int i = 0; i < refRatio.size(); ++i)
171 HeaderFile << refRatio[i] <<
' ';
175 for (
int i = 0; i < nLevel; ++i)
176 HeaderFile << geom[i].Domain() <<
' ';
180 for (
int i = 0; i < nLevel; ++i)
181 HeaderFile << 0 <<
' ';
185 for (
int i = 0; i < nLevel; ++i) {
186 for (
int k = 0; k < AMREX_SPACEDIM; k++)
187 HeaderFile << geom[i].CellSize()[k] / scale <<
' ';
192 HeaderFile << geom[0].Coord() <<
'\n';
196 for (
int lev = 0; lev < nLevel; ++lev) {
200 static const std::string BaseName =
"/Cell";
202 snprintf(buf,
sizeof(buf),
"Level_%d", lev);
203 std::string sLevel = buf;
208 std::string FullPath =
dir_m;
209 if (!FullPath.empty() && FullPath[FullPath.length()-1] !=
'/') {
217 if (!amrex::UtilCreateDirectory(FullPath, 0755)) {
218 amrex::CreateDirectoryFailed(FullPath);
227 HeaderFile << lev <<
' ' << rho[lev]->boxArray().size() <<
' ' << 0 <<
'\n';
228 HeaderFile << 0 <<
'\n';
230 for (
int i = 0; i < rho[lev]->boxArray().size(); ++i) {
231 amrex::Real dx[3] = {
232 geom[lev].CellSize(0),
233 geom[lev].CellSize(1),
234 geom[lev].CellSize(2)
239 amrex::Real lo[3] = {
251 for (
int n = 0;
n < AMREX_SPACEDIM;
n++)
252 HeaderFile << loc.lo(
n) <<
' ' << loc.hi(
n) <<
'\n';
255 std::string PathNameInHeader = sLevel;
256 PathNameInHeader += BaseName;
257 HeaderFile << PathNameInHeader <<
'\n';
264 rho[lev]->DistributionMap(),
277 amr::AmrField_t::Copy(data, *rho[lev], 0, 0, 1, 0);
278 amr::AmrField_t::Copy(data, *phi[lev], 0, 1, 1, 0);
279 amr::AmrField_t::Copy(data, *efield[lev][0], 0, 2, 1, 0);
280 amr::AmrField_t::Copy(data, *efield[lev][1], 0, 3, 1, 0);
281 amr::AmrField_t::Copy(data, *efield[lev][2], 0, 4, 1, 0);
286 std::string TheFullPath = FullPath;
287 TheFullPath += BaseName;
289 amrex::VisMF::Write(data, TheFullPath, amrex::VisMF::NFiles,
true);
299 const double& gamma) {
319 const int NProcs = amrex::ParallelDescriptor::NProcs();
320 const int IOProcNumber = amrex::ParallelDescriptor::IOProcessorNumber();
322 bool doUnlink =
true;
327 std::string pdir =
dir_m;
329 if ( ! pdir.empty() && pdir[pdir.size()-1] !=
'/') {
338 if ( ! amrex::UtilCreateDirectory(pdir, 0755)) {
339 amrex::CreateDirectoryFailed(pdir);
351 std::ofstream HdrFile;
358 int nLevel = (&amrpbase_p->
getAmrLayout())->maxLevel() + 1;
360 std::unique_ptr<size_t[]> partPerLevel(
new size_t[nLevel] );
361 std::unique_ptr<size_t[]> globalPartPerLevel(
new size_t[nLevel] );
363 for (
size_t i = 0; i < LocalNumPerLevel.size(); ++i)
364 partPerLevel[i] = LocalNumPerLevel[i];
367 *globalPartPerLevel.get(),
368 nLevel, std::plus<size_t>());
374 std::string HdrFileName = pdir;
375 if ( ! HdrFileName.empty() && HdrFileName[HdrFileName.size()-1] !=
'/') {
379 HdrFileName +=
"Header";
380 HdrFile.open(HdrFileName.c_str(), std::ios::out|std::ios::trunc);
382 if ( ! HdrFile.good()) {
383 amrex::FileOpenFailed(HdrFileName);
392 HdrFile <<
"Version_Two_Dot_Zero_double" <<
'\n';
396 HdrFile << AMREX_SPACEDIM <<
'\n';
400 for (std::size_t j = 0; j <
realData_m.size(); ++j)
405 for (std::size_t j = 0; j <
intData_m.size(); ++j)
409 HdrFile <<
true <<
'\n';
414 HdrFile << nParticles <<
'\n';
419 HdrFile << 0 <<
'\n';
423 HdrFile << finest_level <<
'\n';
427 for (
int lev = 0; lev <= finest_level; ++lev) {
428 HdrFile << layout_p->ParticleBoxArray(lev).size() <<
'\n';
440 for (
int lev = 0; lev <= finest_level; ++lev) {
441 bool gotsome = (globalPartPerLevel[lev] > 0);
445 std::string LevelDir = pdir;
448 if ( ! LevelDir.empty() && LevelDir[LevelDir.size()-1] !=
'/') {
452 LevelDir = amrex::Concatenate(LevelDir +
"Level_", lev, 1);
455 if ( ! amrex::UtilCreateDirectory(LevelDir, 0755)) {
456 amrex::CreateDirectoryFailed(LevelDir);
467 std::string HeaderFileName = LevelDir;
468 HeaderFileName +=
"/Particle_H";
469 std::ofstream ParticleHeader(HeaderFileName);
471 layout_p->ParticleBoxArray(lev).writeOn(ParticleHeader);
472 ParticleHeader <<
'\n';
474 ParticleHeader.flush();
475 ParticleHeader.close();
479 info.SetAlloc(
false);
481 layout_p->ParticleDistributionMap(lev),
487 amrex::Vector<int> which(state.size(),0);
488 amrex::Vector<int > count(state.size(),0);
489 amrex::Vector<long>
where(state.size(),0);
491 std::string filePrefix(LevelDir);
493 filePrefix +=
"DATA_";
496 bool groupSets(
false), setBuf(
true);
497 for(amrex::NFilesIter nfi(nOutFiles, filePrefix, groupSets, setBuf); nfi.ReadyToWrite(); ++nfi) {
498 std::ofstream& myStream = (std::ofstream&) nfi.Stream();
507 amrex::ParallelDescriptor::ReduceIntSum (which.dataPtr(), which.size(), IOProcNumber);
508 amrex::ParallelDescriptor::ReduceIntSum (count.dataPtr(), count.size(), IOProcNumber);
509 amrex::ParallelDescriptor::ReduceLongSum(
where.dataPtr(),
where.size(), IOProcNumber);
513 for (
int j = 0; j < state.size(); j++) {
519 HdrFile << which[j] <<
' ' << count[j] <<
' ' <<
where[j] <<
'\n';
522 if (gotsome && doUnlink) {
526 amrex::Vector<long> cnt(nOutFiles,0);
528 for (
int i = 0, N=count.size(); i < N; i++) {
529 cnt[which[i]] += count[i];
532 for (
int i = 0, N=cnt.size(); i < N; i++) {
534 std::string FullFileName = amrex::NFilesIter::FileName(i, filePrefix);
535 amrex::UnlinkFile(FullFileName.c_str());
545 if ( ! HdrFile.good()) {
547 "Problem writing HdrFile");
556 amrex::Vector<int>& which,
557 amrex::Vector<int>& count,
558 amrex::Vector<long>&
where,
560 const double gamma)
const
585 size_t lBegin = LocalNumPerLevel.
begin(level);
586 size_t lEnd = LocalNumPerLevel.end(level);
588 for (
size_t ip = lBegin; ip < lEnd; ++ip) {
589 const int grid = amrpbase_p->
Grid[ip];
595 info.SetAlloc(
false);
597 layout_p->ParticleDistributionMap(level),
600 for (amrex::MFIter mfi(state); mfi.isValid(); ++mfi) {
601 const int grid = mfi.index();
604 where[grid] = amrex::VisMF::FileOffset(ofs);
606 if (count[grid] == 0) {
611 const int iChunkSize = 2 +
intData_m.size();
612 amrex::Vector<int> istuff(count[grid]*iChunkSize);
613 int* iptr = istuff.dataPtr();
616 for (
size_t ip = lBegin; ip < lEnd; ++ip) {
617 const int pGrid = amrpbase_p->
Grid[ip];
619 if ( grid == pGrid ) {
621 iptr[0] = bunch_p->
ID[ip];
623 iptr[2] = bunch_p->
Bin[ip];
628 amrex::writeIntData(istuff.dataPtr(), istuff.size(), ofs);
631 const int rChunkSize = AMREX_SPACEDIM +
realData_m.size();
632 amrex::Vector<double> rstuff(count[grid]*rChunkSize);
633 double* rptr = rstuff.dataPtr();
637 for (
size_t ip = lBegin; ip < lEnd; ++ip) {
638 const int pGrid = amrpbase_p->
Grid[ip];
640 if ( grid == pGrid ) {
645 for (
int j = 0; j < AMREX_SPACEDIM; ++j)
646 rptr[idx++] = bunch_p->
R[ip](j);
655 for (
int j = 0; j < AMREX_SPACEDIM; ++j)
656 rptr[idx++] = bunch_p->
P[ip](j);
659 rptr[idx++] = bunch_p->
Q[ip];
662 rptr[idx++] = bunch_p->
M[ip];
665 rptr[idx++] = bunch_p->
dt[ip];
671 for (
int j = 0; j < AMREX_SPACEDIM; ++j)
672 rptr[idx++] = bunch_p->
Ef[ip](j);
675 for (
int j = 0; j < AMREX_SPACEDIM; ++j)
676 rptr[idx++] = bunch_p->
Bf[ip](j);
681 amrex::writeDoubleData(rstuff.dataPtr(), rstuff.size(), ofs);
static OpalData * getInstance()
ParticleAttrib< Vector_t > P
std::vector< std::string > intData_m
integer bunch data
ParticleAttrib< Vector_t > Ef
ParticleAttrib< double > M
void writeFields(const amr::AmrScalarFieldContainer_t &rho, const amr::AmrScalarFieldContainer_t &phi, const amr::AmrVectorFieldContainer_t &efield, const amr::AmrIntArray_t &refRatio, const amr::AmrGeomContainer_t &geom, const int &nLevel, const double &time, const double &scale)
pbase_t * getAmrParticleBase()
ParticleLayout< T, Dim > & getLayout()
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
ParticleAttrib< Vector_t > Bf
amrex::Vector< std::unique_ptr< AmrField_t > > AmrScalarFieldContainer_t
amrex::Vector< int > AmrIntArray_t
AmrYtWriter(int step, int bin=0)
size_t getTotalNum() const
static Communicate * Comm
The base class for all OPAL exceptions.
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
void writeParticles_m(int level, std::ofstream &ofs, int fnum, amrex::Vector< int > &which, amrex::Vector< int > &count, amrex::Vector< long > &where, const AmrPartBunch *bunch_p, const double gamma) const
amrex::Vector< AmrGeometry_t > AmrGeomContainer_t
amrex::MultiFab AmrField_t
const ParticleLevelCounter_t & getLocalNumPerLevel() const
void allreduce(const T *input, T *output, int count, Op op)
ParticleAttrib< double > Q
amrex::RealBox AmrDomain_t
ParticleAttrib< double > dt
std::vector< std::string > realData_m
real bunch data
void writeBunch(const AmrPartBunch *bunch_p, const double &time, const double &gamma)
PETE_TTTree< OpWhere, typename Cond_t::PETE_Expr_t, typename True_t::PETE_Expr_t, PETE_Scalar< Vektor< T, Dim > > > where(const PETE_Expr< Cond_t > &c, const PETE_Expr< True_t > &t, const Vektor< T, Dim > &f)
std::string getInputBasename()
get input file name without extension
ParticleAttrib< int > Bin
amrex::Vector< AmrVectorField_t > AmrVectorFieldContainer_t
std::string dir_m
directory where to write files