3 #include <AMReX_Utility.H>
4 #include <AMReX_IntVect.H>
5 #include <AMReX_ParmParse.H>
6 #include <AMReX_ParallelDescriptor.H>
7 #include <AMReX_VisMF.H>
8 #include <AMReX_VectorIO.H>
9 #include <AMReX_NFiles.H>
47 namespace fs = boost::filesystem;
50 boost::filesystem::path path = dir.parent_path() /
"data" /
"amr" /
"yt";
51 dir_m = amrex::Concatenate((path /
"plt").
string(), step, 10);
57 fs::create_directories( path );
58 }
catch(
const fs::filesystem_error& ex) {
84 amrex::VisMF::SetNOutFiles(64);
89 if (!amrex::UtilCreateDirectory(
dir_m, 0755))
90 amrex::CreateDirectoryFailed(
dir_m);
96 std::string HeaderFileName =
dir_m +
"/Header";
98 amrex::VisMF::IO_Buffer io_buffer(amrex::VisMF::IO_Buffer_Size);
100 std::ofstream HeaderFile;
102 HeaderFile.rdbuf()->pubsetbuf(io_buffer.dataPtr(), io_buffer.size());
104 int nData = rho[0]->nComp()
106 + efield[0][0]->nComp()
107 + efield[0][1]->nComp()
108 + efield[0][2]->nComp();
115 HeaderFile.open(HeaderFileName.c_str(), std::ios::out|std::ios::trunc|std::ios::binary);
116 if (!HeaderFile.good())
117 amrex::FileOpenFailed(HeaderFileName);
118 HeaderFile <<
"HyperCLaw-V1.1\n";
120 HeaderFile << nData <<
'\n';
123 for (
int ivar = 1; ivar <= rho[0]->nComp(); ivar++)
124 HeaderFile <<
"rho\n";
126 for (
int ivar = 1; ivar <= phi[0]->nComp(); ivar++)
127 HeaderFile <<
"phi\n";
129 HeaderFile <<
"Ex\nEy\nEz\n";
132 HeaderFile << AMREX_SPACEDIM <<
'\n';
135 HeaderFile << time <<
'\n';
136 HeaderFile << nLevel - 1 <<
'\n';
139 for (
int i = 0; i < AMREX_SPACEDIM; i++)
140 HeaderFile << geom[0].ProbLo(i) / scale <<
' ';
142 for (
int i = 0; i < AMREX_SPACEDIM; i++)
143 HeaderFile << geom[0].ProbHi(i) / scale <<
' ';
147 for (
int i = 0; i < refRatio.size(); ++i)
148 HeaderFile << refRatio[i] <<
' ';
152 for (
int i = 0; i < nLevel; ++i)
153 HeaderFile << geom[i].Domain() <<
' ';
157 for (
int i = 0; i < nLevel; ++i)
158 HeaderFile << 0 <<
' ';
162 for (
int i = 0; i < nLevel; ++i) {
163 for (
int k = 0; k < AMREX_SPACEDIM; k++)
164 HeaderFile << geom[i].CellSize()[k] / scale <<
' ';
169 HeaderFile << geom[0].Coord() <<
'\n';
173 for (
int lev = 0; lev < nLevel; ++lev) {
177 static const std::string BaseName =
"/Cell";
179 sprintf(buf,
"Level_%d", lev);
180 std::string sLevel = buf;
185 std::string FullPath =
dir_m;
186 if (!FullPath.empty() && FullPath[FullPath.length()-1] !=
'/')
193 if (!amrex::UtilCreateDirectory(FullPath, 0755))
194 amrex::CreateDirectoryFailed(FullPath);
202 HeaderFile << lev <<
' ' << rho[lev]->boxArray().size() <<
' ' << 0 <<
'\n';
203 HeaderFile << 0 <<
'\n';
205 for (
int i = 0; i < rho[lev]->boxArray().size(); ++i)
207 amrex::Real dx[3] = {
208 geom[lev].CellSize(0),
209 geom[lev].CellSize(1),
210 geom[lev].CellSize(2)
215 amrex::Real lo[3] = {
227 for (
int n = 0;
n < AMREX_SPACEDIM;
n++)
228 HeaderFile << loc.lo(
n) <<
' ' << loc.hi(
n) <<
'\n';
231 std::string PathNameInHeader = sLevel;
232 PathNameInHeader += BaseName;
233 HeaderFile << PathNameInHeader <<
'\n';
240 rho[lev]->DistributionMap(),
253 amr::AmrField_t::Copy(data, *rho[lev], 0, 0, 1, 0);
254 amr::AmrField_t::Copy(data, *phi[lev], 0, 1, 1, 0);
255 amr::AmrField_t::Copy(data, *efield[lev][0], 0, 2, 1, 0);
256 amr::AmrField_t::Copy(data, *efield[lev][1], 0, 3, 1, 0);
257 amr::AmrField_t::Copy(data, *efield[lev][2], 0, 4, 1, 0);
262 std::string TheFullPath = FullPath;
263 TheFullPath += BaseName;
265 amrex::VisMF::Write(data, TheFullPath, amrex::VisMF::NFiles,
true);
296 const int NProcs = amrex::ParallelDescriptor::NProcs();
297 const int IOProcNumber = amrex::ParallelDescriptor::IOProcessorNumber();
299 bool doUnlink =
true;
304 std::string pdir =
dir_m;
306 if ( ! pdir.empty() && pdir[pdir.size()-1] !=
'/') {
315 if ( ! amrex::UtilCreateDirectory(pdir, 0755)) {
316 amrex::CreateDirectoryFailed(pdir);
328 std::ofstream HdrFile;
335 int nLevel = (&amrpbase_p->
getAmrLayout())->maxLevel() + 1;
337 std::unique_ptr<size_t[]> partPerLevel(
new size_t[nLevel] );
338 std::unique_ptr<size_t[]> globalPartPerLevel(
new size_t[nLevel] );
340 for (
size_t i = 0; i < LocalNumPerLevel.size(); ++i)
341 partPerLevel[i] = LocalNumPerLevel[i];
344 *globalPartPerLevel.get(),
345 nLevel, std::plus<size_t>());
351 std::string HdrFileName = pdir;
353 if ( ! HdrFileName.empty() && HdrFileName[HdrFileName.size()-1] !=
'/') {
358 HdrFileName +=
"Header";
360 HdrFile.open(HdrFileName.c_str(), std::ios::out|std::ios::trunc);
362 if ( ! HdrFile.good()) {
363 amrex::FileOpenFailed(HdrFileName);
372 HdrFile <<
"Version_Two_Dot_Zero_double" <<
'\n';
376 HdrFile << AMREX_SPACEDIM <<
'\n';
380 for (std::size_t j = 0; j <
realData_m.size(); ++j)
385 for (std::size_t j = 0; j <
intData_m.size(); ++j)
389 HdrFile <<
true <<
'\n';
394 HdrFile << nParticles <<
'\n';
399 HdrFile << 0 <<
'\n';
403 HdrFile << finest_level <<
'\n';
407 for (
int lev = 0; lev <= finest_level; ++lev) {
408 HdrFile << layout_p->ParticleBoxArray(lev).size() <<
'\n';
420 for (
int lev = 0; lev <= finest_level; ++lev) {
421 bool gotsome = (globalPartPerLevel[lev] > 0);
425 std::string LevelDir = pdir;
428 if ( ! LevelDir.empty() && LevelDir[LevelDir.size()-1] !=
'/') {
432 LevelDir = amrex::Concatenate(LevelDir +
"Level_", lev, 1);
435 if ( ! amrex::UtilCreateDirectory(LevelDir, 0755)) {
436 amrex::CreateDirectoryFailed(LevelDir);
447 std::string HeaderFileName = LevelDir;
448 HeaderFileName +=
"/Particle_H";
449 std::ofstream ParticleHeader(HeaderFileName);
451 layout_p->ParticleBoxArray(lev).writeOn(ParticleHeader);
452 ParticleHeader <<
'\n';
454 ParticleHeader.flush();
455 ParticleHeader.close();
459 info.SetAlloc(
false);
461 layout_p->ParticleDistributionMap(lev),
467 amrex::Vector<int> which(state.size(),0);
468 amrex::Vector<int > count(state.size(),0);
469 amrex::Vector<long>
where(state.size(),0);
471 std::string filePrefix(LevelDir);
473 filePrefix +=
"DATA_";
474 bool groupSets(
false), setBuf(
true);
477 for(amrex::NFilesIter nfi(nOutFiles, filePrefix, groupSets, setBuf); nfi.ReadyToWrite(); ++nfi) {
478 std::ofstream& myStream = (std::ofstream&) nfi.Stream();
487 amrex::ParallelDescriptor::ReduceIntSum (which.dataPtr(), which.size(), IOProcNumber);
488 amrex::ParallelDescriptor::ReduceIntSum (count.dataPtr(), count.size(), IOProcNumber);
489 amrex::ParallelDescriptor::ReduceLongSum(
where.dataPtr(),
where.size(), IOProcNumber);
493 for (
int j = 0; j < state.size(); j++) {
499 HdrFile << which[j] <<
' ' << count[j] <<
' ' <<
where[j] <<
'\n';
502 if (gotsome && doUnlink) {
506 amrex::Vector<long> cnt(nOutFiles,0);
508 for (
int i = 0, N=count.size(); i < N; i++) {
509 cnt[which[i]] += count[i];
512 for (
int i = 0, N=cnt.size(); i < N; i++) {
514 std::string FullFileName = amrex::NFilesIter::FileName(i, filePrefix);
515 amrex::UnlinkFile(FullFileName.c_str());
525 if ( ! HdrFile.good()) {
527 "Problem writing HdrFile");
536 amrex::Vector<int>& which,
537 amrex::Vector<int>& count,
538 amrex::Vector<long>&
where,
540 const double gamma)
const
565 size_t lBegin = LocalNumPerLevel.
begin(level);
566 size_t lEnd = LocalNumPerLevel.end(level);
568 for (
size_t ip = lBegin; ip < lEnd; ++ip) {
569 const int grid = amrpbase_p->
Grid[ip];
575 info.SetAlloc(
false);
577 layout_p->ParticleDistributionMap(level),
580 for (amrex::MFIter mfi(state); mfi.isValid(); ++mfi) {
581 const int grid = mfi.index();
584 where[grid] = amrex::VisMF::FileOffset(ofs);
586 if (count[grid] == 0) {
592 const int iChunkSize = 2 +
intData_m.size();
593 amrex::Vector<int> istuff(count[grid]*iChunkSize);
594 int* iptr = istuff.dataPtr();
597 for (
size_t ip = lBegin; ip < lEnd; ++ip) {
598 const int pGrid = amrpbase_p->
Grid[ip];
600 if ( grid == pGrid ) {
602 iptr[0] = bunch_p->
ID[ip];
604 iptr[2] = bunch_p->
Bin[ip];
609 amrex::writeIntData(istuff.dataPtr(), istuff.size(), ofs);
612 const int rChunkSize = AMREX_SPACEDIM +
realData_m.size();
613 amrex::Vector<double> rstuff(count[grid]*rChunkSize);
614 double* rptr = rstuff.dataPtr();
618 for (
size_t ip = lBegin; ip < lEnd; ++ip) {
619 const int pGrid = amrpbase_p->
Grid[ip];
621 if ( grid == pGrid ) {
626 for (
int j = 0; j < AMREX_SPACEDIM; ++j)
627 rptr[idx++] = bunch_p->
R[ip](j);
636 for (
int j = 0; j < AMREX_SPACEDIM; ++j)
637 rptr[idx++] = bunch_p->
P[ip](j);
640 rptr[idx++] = bunch_p->
Q[ip];
643 rptr[idx++] = bunch_p->
M[ip];
646 rptr[idx++] = bunch_p->
dt[ip];
652 for (
int j = 0; j < AMREX_SPACEDIM; ++j)
653 rptr[idx++] = bunch_p->
Ef[ip](j);
656 for (
int j = 0; j < AMREX_SPACEDIM; ++j)
657 rptr[idx++] = bunch_p->
Bf[ip](j);
662 amrex::writeDoubleData(rstuff.dataPtr(), rstuff.size(), ofs);
ParticleAttrib< Vector_t > P
ParticleAttrib< Vector_t > Ef
AmrYtWriter(int step, int bin=0)
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
The base class for all OPAL exceptions.
ParticleAttrib< double > Q
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
std::string dir_m
directory where to write files
const ParticleLevelCounter_t & getLocalNumPerLevel() const
amrex::Vector< AmrGeometry_t > AmrGeomContainer_t
size_t getTotalNum() const
void writeBunch(const AmrPartBunch *bunch_p, const double &time, const double &gamma)
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()
amrex::MultiFab AmrField_t
static OpalData * getInstance()
ParticleLayout< T, Dim > & getLayout()
amrex::Vector< int > AmrIntArray_t
amrex::RealBox AmrDomain_t
std::vector< std::string > intData_m
integer bunch data
void allreduce(const T *input, T *output, int count, Op op)
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)
ParticleAttrib< double > dt
ParticleAttrib< int > Bin
ParticleAttrib< Vector_t > Bf
ParticleAttrib< double > M
amrex::Vector< AmrVectorField_t > AmrVectorFieldContainer_t
std::string getInputBasename()
get input file name without extension
static Communicate * Comm
T::PETE_Expr_t::PETE_Return_t min(const PETE_Expr< T > &expr, NDIndex< D > &loc)
amrex::Vector< std::unique_ptr< AmrField_t > > AmrScalarFieldContainer_t
std::vector< std::string > realData_m
real bunch data