35 #include <boost/asio/ip/host_name.hpp> 36 #include <boost/format.hpp> 37 #include <boost/regex.hpp> 54 #ifndef NEKTAR_VERSION 55 #define NEKTAR_VERSION "Unknown" 58 namespace berrc = boost::system::errc;
59 namespace ip = boost::asio::ip;
63 namespace LibUtilities
67 "io-format",
"i",
"Default input/output format (e.g. Xml, Hdf5)");
101 int size = comm->GetSize();
102 bool root = comm->TreatAsRankZero();
104 if (size == 1 || root)
106 std::string datafilename;
109 if (fs::is_directory(filename))
111 fs::path fullpath = filename;
113 fs::path d = fullpath;
114 boost::regex expr(
"P\\d{7}.fld");
118 for (
auto &f : fs::directory_iterator(d))
120 if (boost::regex_match(f.path().filename().string(), what, expr))
128 ASSERTL0(found,std::string(
"Failed to open a PXXXXXXX.fld file " 129 "in directory: " + filename).c_str());
135 datafilename = filename;
141 const unsigned char magic[8] = {
142 0x89, 0x48, 0x44, 0x46, 0x0d, 0x0a, 0x1a, 0x0a};
144 std::ifstream datafile(datafilename.c_str(), std::ios_base::binary);
145 ASSERTL0(datafile.good(),
"Unable to open file: " + filename);
148 for (
unsigned i = 0; i < 8 && datafile.good(); ++i)
150 int byte = datafile.get();
151 if (byte != magic[i])
161 int code = (int)ioType;
162 comm->Bcast(code, 0);
171 else if (ioType ==
eHDF5)
198 std::string iofmt(
"Xml");
199 if (session->DefinesSolverInfo(
"IOFormat"))
201 iofmt = session->GetSolverInfo(
"IOFormat");
204 if (session->DefinesCmdLineArgument(
"io-format"))
206 iofmt = session->GetCmdLineArgument<std::string>(
"io-format");
212 session->GetSharedFilesystem());
228 const std::string &filename)
230 const std::string iofmt =
235 session->GetSharedFilesystem());
249 void Write(
const std::string &outFile,
250 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
251 std::vector<std::vector<NekDouble> > &fielddata,
255 #ifdef NEKTAR_USE_MPI 258 MPI_Initialized(&init);
267 MPI_Comm_size(MPI_COMM_WORLD, &size);
269 "This static function is not available in parallel. Please" 270 "instantiate a FieldIO object for parallel use.");
275 f->Write(outFile, fielddefs, fielddata, fieldinfomap, backup);
294 const std::string &infilename,
295 std::vector<FieldDefinitionsSharedPtr> &fielddefs,
296 std::vector<std::vector<NekDouble> > &fielddata,
300 #ifdef NEKTAR_USE_MPI 303 MPI_Initialized(&init);
312 MPI_Comm_size(MPI_COMM_WORLD, &size);
314 "This static function is not available in parallel. Please" 315 "instantiate a FieldIO object for parallel use.");
321 f->Import(infilename, fielddefs, fielddata, fieldinfomap, ElementIDs);
357 auto now = std::chrono::system_clock::now();
358 auto now_t = std::chrono::system_clock::to_time_t(now);
359 auto now_tm = *std::localtime(&now_t);
361 strftime(buffer,
sizeof(buffer),
"%d-%b-%Y %H:%M:%S", &now_tm);
362 ProvenanceMap[
"Timestamp"] =
buffer;
365 boost::system::error_code ec;
366 ProvenanceMap[
"Hostname"] = ip::host_name(ec);
379 for (
auto &infoit : ProvenanceMap)
381 provTag->SetAttr(infoit.first, infoit.second);
388 for (
auto &infoit : fieldmetadatamap)
390 infoTag->SetAttr(infoit.first, infoit.second);
412 ASSERTL0(!outname.empty(),
"Empty path given to SetUpOutput()");
414 int nprocs =
m_comm->GetSize();
415 bool root =
m_comm->TreatAsRankZero();
419 fs::path specPath(outname), fulloutname;
426 fs::path bakPath = specPath;
428 while (fs::exists(bakPath))
430 bakPath = specPath.parent_path();
431 bakPath += specPath.stem();
432 bakPath += fs::path(
".bak" + std::to_string(cnt++));
433 bakPath += specPath.extension();
435 std::cout <<
"renaming " << specPath <<
" -> " << bakPath << std::endl;
438 fs::rename(specPath, bakPath);
440 catch (fs::filesystem_error &e)
442 ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
443 "Filesystem error: " + std::string(e.what()));
453 while (exists && perRank)
455 exists = fs::exists(specPath);
462 fulloutname = specPath;
471 fs::path poutfile(pad.str());
472 fulloutname = specPath / poutfile;
476 if (
m_comm->RemoveExistingFiles() && !backup)
485 fs::remove_all(fulloutname);
487 catch (fs::filesystem_error &e)
489 ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
490 "Filesystem error: " + std::string(e.what()));
501 fs::remove_all(specPath);
503 catch (fs::filesystem_error &e)
505 ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
506 "Filesystem error: " + std::string(e.what()));
514 while (exists && perRank)
516 exists = fs::exists(specPath);
523 std::cout <<
"Writing: " << specPath;
539 fs::create_directory(specPath);
542 catch (fs::filesystem_error &e)
545 "Filesystem error: " + std::string(e.what()));
554 created = fs::is_directory(specPath);
560 fulloutname = specPath;
576 if (fielddefs->m_elementIDs.size() == 0)
581 unsigned int numbasis = 0;
584 switch (fielddefs->m_shapeType)
588 if (fielddefs->m_numHomogeneousDir)
590 numbasis += fielddefs->m_numHomogeneousDir;
596 if (fielddefs->m_numHomogeneousDir)
618 ASSERTL0(fielddefs->m_basis.size() == numbasis,
619 "Length of basis vector is incorrect");
621 if (fielddefs->m_uniOrder ==
true)
623 unsigned int cnt = 0;
625 switch (fielddefs->m_shapeType)
629 int l = fielddefs->m_numModes[cnt++];
630 if (fielddefs->m_numHomogeneousDir == 1)
632 datasize += l * fielddefs->m_homogeneousZIDs.size();
635 else if (fielddefs->m_numHomogeneousDir == 2)
637 datasize += l * fielddefs->m_homogeneousYIDs.size();
648 int l = fielddefs->m_numModes[cnt++];
649 int m = fielddefs->m_numModes[cnt++];
651 if (fielddefs->m_numHomogeneousDir == 1)
654 fielddefs->m_homogeneousZIDs.size();
664 int l = fielddefs->m_numModes[cnt++];
665 int m = fielddefs->m_numModes[cnt++];
666 if (fielddefs->m_numHomogeneousDir == 1)
668 datasize += l * m * fielddefs->m_homogeneousZIDs.size();
678 int l = fielddefs->m_numModes[cnt++];
679 int m = fielddefs->m_numModes[cnt++];
680 int n = fielddefs->m_numModes[cnt++];
686 int l = fielddefs->m_numModes[cnt++];
687 int m = fielddefs->m_numModes[cnt++];
688 int n = fielddefs->m_numModes[cnt++];
694 int l = fielddefs->m_numModes[cnt++];
695 int m = fielddefs->m_numModes[cnt++];
696 int n = fielddefs->m_numModes[cnt++];
702 int l = fielddefs->m_numModes[cnt++];
703 int m = fielddefs->m_numModes[cnt++];
704 int n = fielddefs->m_numModes[cnt++];
705 datasize += l * m * n;
713 datasize *= fielddefs->m_elementIDs.size();
717 unsigned int cnt = 0;
719 for (i = 0; i < fielddefs->m_elementIDs.size(); ++i)
721 switch (fielddefs->m_shapeType)
725 int l = fielddefs->m_numModes[cnt++];
726 if (fielddefs->m_numHomogeneousDir == 1)
728 datasize += l * fielddefs->m_homogeneousZIDs.size();
731 else if (fielddefs->m_numHomogeneousDir == 2)
733 datasize += l * fielddefs->m_homogeneousYIDs.size();
744 int l = fielddefs->m_numModes[cnt++];
745 int m = fielddefs->m_numModes[cnt++];
746 if (fielddefs->m_numHomogeneousDir == 1)
749 fielddefs->m_homogeneousZIDs.size();
760 int l = fielddefs->m_numModes[cnt++];
761 int m = fielddefs->m_numModes[cnt++];
762 if (fielddefs->m_numHomogeneousDir == 1)
764 datasize += l * m * fielddefs->m_homogeneousZIDs.size();
775 int l = fielddefs->m_numModes[cnt++];
776 int m = fielddefs->m_numModes[cnt++];
777 int n = fielddefs->m_numModes[cnt++];
783 int l = fielddefs->m_numModes[cnt++];
784 int m = fielddefs->m_numModes[cnt++];
785 int n = fielddefs->m_numModes[cnt++];
791 int l = fielddefs->m_numModes[cnt++];
792 int m = fielddefs->m_numModes[cnt++];
793 int n = fielddefs->m_numModes[cnt++];
799 int l = fielddefs->m_numModes[cnt++];
800 int m = fielddefs->m_numModes[cnt++];
801 int n = fielddefs->m_numModes[cnt++];
802 datasize += l * m * n;
812 return (
int)datasize;
#define ASSERTL0(condition, msg)
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
int getNumberOfCoefficients(int Na, int Nb, int Nc)
static std::string RegisterCmdLineArgument(const std::string &pName, const std::string &pShortName, const std::string &pDescription)
Registers a command-line argument with the session reader.
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
int getNumberOfCoefficients(int Na, int Nb, int Nc)
FieldIOFactory & GetFieldIOFactory()
Returns the FieldIO factory.
void Import(const std::string &infilename, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata=NullVectorNekDoubleVector, FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const Array< OneD, int > &ElementIDs=NullInt1DArray)
Read field information from the file infilename.
std::map< std::string, std::string > FieldMetaDataMap
CommFactory & GetCommFactory()
int CheckFieldDefinition(const FieldDefinitionsSharedPtr &fielddefs)
Check field definitions for correctness and return storage size.
std::shared_ptr< TagWriter > TagWriterSharedPtr
bool m_sharedFilesystem
Boolean dictating whether we are on a shared filesystem.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
const std::string kGitSha1
FieldIOType
Enumerator for auto-detection of FieldIO types.
static const std::string GetFileType(const std::string &filename, CommSharedPtr comm)
Determine file type of given input file.
#define LIB_UTILITIES_EXPORT
int getNumberOfCoefficients(int Na, int Nb)
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
const std::string kGitBranch
std::string SetUpOutput(const std::string outname, bool perRank, bool backup=false)
Set up the filesystem ready for output.
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
int getNumberOfCoefficients(int Na, int Nb, int Nc)
static std::shared_ptr< FieldIO > CreateDefault(const LibUtilities::SessionReaderSharedPtr session)
Returns an object for the default FieldIO method.
virtual std::string GetFileEnding() const
Helper function that determines default file extension.
static std::shared_ptr< FieldIO > CreateForFile(const LibUtilities::SessionReaderSharedPtr session, const std::string &filename)
Construct a FieldIO object for a given input filename.
std::shared_ptr< FieldDefinitions > FieldDefinitionsSharedPtr
std::shared_ptr< FieldIO > FieldIOSharedPtr
static FieldMetaDataMap NullFieldMetaDataMap
static void AddInfoTag(TagWriterSharedPtr root, const FieldMetaDataMap &fieldmetadatamap)
Add provenance information to the field metadata map.
std::shared_ptr< SessionReader > SessionReaderSharedPtr
void Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap, const bool backup=false)
Write out the field information to the file outFile.
Provides a generic Factory class.
FieldIO(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Constructor for FieldIO base class.