Nektar++
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Nektar::LibUtilities::FieldIO Class Referenceabstract

Class for operating on Nektar++ input/output files. More...

#include <FieldIO.h>

Inheritance diagram for Nektar::LibUtilities::FieldIO:
[legend]

Public Member Functions

 FieldIO (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
 Constructor for FieldIO base class. More...
 
virtual ~FieldIO ()
 
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. More...
 
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. More...
 
DataSourceSharedPtr ImportFieldMetaData (const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
 Import the metadata from a field file. More...
 
virtual const std::string & GetClassName () const =0
 

Static Public Member Functions

static const std::string GetFileType (const std::string &filename, CommSharedPtr comm)
 Determine file type of given input file. More...
 
static std::shared_ptr< FieldIOCreateDefault (const LibUtilities::SessionReaderSharedPtr session)
 Returns an object for the default FieldIO method. More...
 
static std::shared_ptr< FieldIOCreateForFile (const LibUtilities::SessionReaderSharedPtr session, const std::string &filename)
 Construct a FieldIO object for a given input filename. More...
 
static void AddInfoTag (TagWriterSharedPtr root, const FieldMetaDataMap &fieldmetadatamap)
 Add provenance information to the field metadata map. More...
 

Protected Member Functions

int CheckFieldDefinition (const FieldDefinitionsSharedPtr &fielddefs)
 Check field definitions for correctness and return storage size. More...
 
virtual std::string GetFileEnding () const
 Helper function that determines default file extension. More...
 
std::string SetUpOutput (const std::string outname, bool perRank, bool backup=false)
 Set up the filesystem ready for output. More...
 
virtual void v_Write (const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap, const bool backup=false)=0
 Write out the field information to the file outFile. More...
 
virtual void v_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)=0
 Read field information from the file infilename. More...
 
virtual DataSourceSharedPtr v_ImportFieldMetaData (const std::string &filename, FieldMetaDataMap &fieldmetadatamap)=0
 Import the metadata from a field file. More...
 

Protected Attributes

LibUtilities::CommSharedPtr m_comm
 Communicator to use when writing parallel format. More...
 
bool m_sharedFilesystem
 Boolean dictating whether we are on a shared filesystem. More...
 

Detailed Description

Class for operating on Nektar++ input/output files.

Nektar++ input/output of field data can be described as follows:

This base class represents the minimum functionality that subclasses need to implement in order to implement the above functionality. Each subclass is free to determine its own file structure and parallel behaviour.

Definition at line 222 of file FieldIO.h.

Constructor & Destructor Documentation

◆ FieldIO()

Nektar::LibUtilities::FieldIO::FieldIO ( LibUtilities::CommSharedPtr  pComm,
bool  sharedFilesystem 
)

Constructor for FieldIO base class.

Definition at line 327 of file FieldIO.cpp.

328  : m_comm(pComm), m_sharedFilesystem(sharedFilesystem)
329 {
330 }
bool m_sharedFilesystem
Boolean dictating whether we are on a shared filesystem.
Definition: FieldIO.h:268
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:266

◆ ~FieldIO()

virtual Nektar::LibUtilities::FieldIO::~FieldIO ( )
inlinevirtual

Member Function Documentation

◆ AddInfoTag()

void Nektar::LibUtilities::FieldIO::AddInfoTag ( TagWriterSharedPtr  root,
const FieldMetaDataMap fieldmetadatamap 
)
static

Add provenance information to the field metadata map.

This routine adds some basic provenance information to the field metadata to enable better tracking of version information:

  • Nektar++ version
  • Date and time of simulation
  • Hostname of the machine the simulation was performed on
  • git SHA1 and branch name, if Nektar++ was compiled from git and not e.g. a tarball.
Parameters
rootRoot tag, which is encapsulated using the TagWriter structure to enable multi-file format support.
fieldmetadatamapAny existing field metadata.

Definition at line 348 of file FieldIO.cpp.

References Nektar::NekConstants::kGitBranch, Nektar::NekConstants::kGitSha1, NEKTAR_VERSION, and Nektar::LibUtilities::NullFieldMetaDataMap.

Referenced by Nektar::LibUtilities::FieldIOHdf5::v_Write(), Nektar::LibUtilities::FieldIOXml::v_Write(), and Nektar::LibUtilities::FieldIOXml::WriteMultiFldFileIDs().

350 {
351  FieldMetaDataMap ProvenanceMap;
352 
353  // Nektar++ release version from VERSION file
354  ProvenanceMap["NektarVersion"] = std::string(NEKTAR_VERSION);
355 
356  // Date/time stamp
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);
360  char buffer[128];
361  strftime(buffer, sizeof(buffer), "%d-%b-%Y %H:%M:%S", &now_tm);
362  ProvenanceMap["Timestamp"] = buffer;
363 
364  // Hostname
365  boost::system::error_code ec;
366  ProvenanceMap["Hostname"] = ip::host_name(ec);
367 
368  // Git information
369  // If built from a distributed package, do not include this
370  if (NekConstants::kGitSha1 != "GITDIR-NOTFOUND")
371  {
372  ProvenanceMap["GitSHA1"] = NekConstants::kGitSha1;
373  ProvenanceMap["GitBranch"] = NekConstants::kGitBranch;
374  }
375 
376  TagWriterSharedPtr infoTag = root->AddChild("Metadata");
377 
378  TagWriterSharedPtr provTag = infoTag->AddChild("Provenance");
379  for (auto &infoit : ProvenanceMap)
380  {
381  provTag->SetAttr(infoit.first, infoit.second);
382  }
383 
384  //---------------------------------------------
385  // write field info section
386  if (fieldmetadatamap != NullFieldMetaDataMap)
387  {
388  for (auto &infoit : fieldmetadatamap)
389  {
390  infoTag->SetAttr(infoit.first, infoit.second);
391  }
392  }
393 }
array buffer
Definition: GsLib.hpp:61
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:52
std::shared_ptr< TagWriter > TagWriterSharedPtr
Definition: FieldIO.h:69
const std::string kGitSha1
const std::string kGitBranch
#define NEKTAR_VERSION
Definition: FieldIO.cpp:55
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:53

◆ CheckFieldDefinition()

int Nektar::LibUtilities::FieldIO::CheckFieldDefinition ( const FieldDefinitionsSharedPtr fielddefs)
protected

Check field definitions for correctness and return storage size.

Parameters
fielddefsField definitions to check.

Definition at line 572 of file FieldIO.cpp.

References ASSERTL0, Nektar::ErrorUtil::efatal, Nektar::LibUtilities::eHexahedron, Nektar::LibUtilities::ePrism, Nektar::LibUtilities::ePyramid, Nektar::LibUtilities::eQuadrilateral, Nektar::LibUtilities::eSegment, Nektar::LibUtilities::eTetrahedron, Nektar::LibUtilities::eTriangle, Nektar::LibUtilities::StdTriData::getNumberOfCoefficients(), Nektar::LibUtilities::StdTetData::getNumberOfCoefficients(), Nektar::LibUtilities::StdPyrData::getNumberOfCoefficients(), Nektar::LibUtilities::StdPrismData::getNumberOfCoefficients(), and NEKERROR.

Referenced by Nektar::LibUtilities::FieldIOXml::ImportFieldData(), Nektar::LibUtilities::FieldIOHdf5::ImportFieldData(), Nektar::LibUtilities::FieldIOHdf5::v_Write(), and Nektar::LibUtilities::FieldIOXml::v_Write().

573 {
574  int i;
575 
576  if (fielddefs->m_elementIDs.size() == 0) // empty partition
577  {
578  return 0;
579  }
580 
581  unsigned int numbasis = 0;
582 
583  // Determine nummodes vector lists are correct length
584  switch (fielddefs->m_shapeType)
585  {
586  case eSegment:
587  numbasis = 1;
588  if (fielddefs->m_numHomogeneousDir)
589  {
590  numbasis += fielddefs->m_numHomogeneousDir;
591  }
592 
593  break;
594  case eTriangle:
595  case eQuadrilateral:
596  if (fielddefs->m_numHomogeneousDir)
597  {
598  numbasis = 3;
599  }
600  else
601  {
602  numbasis = 2;
603  }
604  break;
605  case eTetrahedron:
606  case ePyramid:
607  case ePrism:
608  case eHexahedron:
609  numbasis = 3;
610  break;
611  default:
612  NEKERROR(ErrorUtil::efatal, "Unsupported shape type.");
613  break;
614  }
615 
616  size_t datasize = 0;
617 
618  ASSERTL0(fielddefs->m_basis.size() == numbasis,
619  "Length of basis vector is incorrect");
620 
621  if (fielddefs->m_uniOrder == true)
622  {
623  unsigned int cnt = 0;
624  // calculate datasize
625  switch (fielddefs->m_shapeType)
626  {
627  case eSegment:
628  {
629  int l = fielddefs->m_numModes[cnt++];
630  if (fielddefs->m_numHomogeneousDir == 1)
631  {
632  datasize += l * fielddefs->m_homogeneousZIDs.size();
633  cnt++;
634  }
635  else if (fielddefs->m_numHomogeneousDir == 2)
636  {
637  datasize += l * fielddefs->m_homogeneousYIDs.size();
638  cnt += 2;
639  }
640  else
641  {
642  datasize += l;
643  }
644  }
645  break;
646  case eTriangle:
647  {
648  int l = fielddefs->m_numModes[cnt++];
649  int m = fielddefs->m_numModes[cnt++];
650 
651  if (fielddefs->m_numHomogeneousDir == 1)
652  {
653  datasize += StdTriData::getNumberOfCoefficients(l, m) *
654  fielddefs->m_homogeneousZIDs.size();
655  }
656  else
657  {
658  datasize += StdTriData::getNumberOfCoefficients(l, m);
659  }
660  }
661  break;
662  case eQuadrilateral:
663  {
664  int l = fielddefs->m_numModes[cnt++];
665  int m = fielddefs->m_numModes[cnt++];
666  if (fielddefs->m_numHomogeneousDir == 1)
667  {
668  datasize += l * m * fielddefs->m_homogeneousZIDs.size();
669  }
670  else
671  {
672  datasize += l * m;
673  }
674  }
675  break;
676  case eTetrahedron:
677  {
678  int l = fielddefs->m_numModes[cnt++];
679  int m = fielddefs->m_numModes[cnt++];
680  int n = fielddefs->m_numModes[cnt++];
681  datasize += StdTetData::getNumberOfCoefficients(l, m, n);
682  }
683  break;
684  case ePyramid:
685  {
686  int l = fielddefs->m_numModes[cnt++];
687  int m = fielddefs->m_numModes[cnt++];
688  int n = fielddefs->m_numModes[cnt++];
689  datasize += StdPyrData::getNumberOfCoefficients(l, m, n);
690  }
691  break;
692  case ePrism:
693  {
694  int l = fielddefs->m_numModes[cnt++];
695  int m = fielddefs->m_numModes[cnt++];
696  int n = fielddefs->m_numModes[cnt++];
697  datasize += StdPrismData::getNumberOfCoefficients(l, m, n);
698  }
699  break;
700  case eHexahedron:
701  {
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;
706  }
707  break;
708  default:
709  NEKERROR(ErrorUtil::efatal, "Unsupported shape type.");
710  break;
711  }
712 
713  datasize *= fielddefs->m_elementIDs.size();
714  }
715  else
716  {
717  unsigned int cnt = 0;
718  // calculate data length
719  for (i = 0; i < fielddefs->m_elementIDs.size(); ++i)
720  {
721  switch (fielddefs->m_shapeType)
722  {
723  case eSegment:
724  {
725  int l = fielddefs->m_numModes[cnt++];
726  if (fielddefs->m_numHomogeneousDir == 1)
727  {
728  datasize += l * fielddefs->m_homogeneousZIDs.size();
729  cnt++;
730  }
731  else if (fielddefs->m_numHomogeneousDir == 2)
732  {
733  datasize += l * fielddefs->m_homogeneousYIDs.size();
734  cnt += 2;
735  }
736  else
737  {
738  datasize += l;
739  }
740  }
741  break;
742  case eTriangle:
743  {
744  int l = fielddefs->m_numModes[cnt++];
745  int m = fielddefs->m_numModes[cnt++];
746  if (fielddefs->m_numHomogeneousDir == 1)
747  {
748  datasize += StdTriData::getNumberOfCoefficients(l, m) *
749  fielddefs->m_homogeneousZIDs.size();
750  cnt++;
751  }
752  else
753  {
754  datasize += StdTriData::getNumberOfCoefficients(l, m);
755  }
756  }
757  break;
758  case eQuadrilateral:
759  {
760  int l = fielddefs->m_numModes[cnt++];
761  int m = fielddefs->m_numModes[cnt++];
762  if (fielddefs->m_numHomogeneousDir == 1)
763  {
764  datasize += l * m * fielddefs->m_homogeneousZIDs.size();
765  cnt++;
766  }
767  else
768  {
769  datasize += l * m;
770  }
771  }
772  break;
773  case eTetrahedron:
774  {
775  int l = fielddefs->m_numModes[cnt++];
776  int m = fielddefs->m_numModes[cnt++];
777  int n = fielddefs->m_numModes[cnt++];
778  datasize += StdTetData::getNumberOfCoefficients(l, m, n);
779  }
780  break;
781  case ePyramid:
782  {
783  int l = fielddefs->m_numModes[cnt++];
784  int m = fielddefs->m_numModes[cnt++];
785  int n = fielddefs->m_numModes[cnt++];
786  datasize += StdPyrData::getNumberOfCoefficients(l, m, n);
787  }
788  break;
789  case ePrism:
790  {
791  int l = fielddefs->m_numModes[cnt++];
792  int m = fielddefs->m_numModes[cnt++];
793  int n = fielddefs->m_numModes[cnt++];
794  datasize += StdPrismData::getNumberOfCoefficients(l, m, n);
795  }
796  break;
797  case eHexahedron:
798  {
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;
803  }
804  break;
805  default:
806  NEKERROR(ErrorUtil::efatal, "Unsupported shape type.");
807  break;
808  }
809  }
810  }
811 
812  return (int)datasize;
813 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209
int getNumberOfCoefficients(int Na, int Nb, int Nc)
Definition: ShapeType.hpp:287
int getNumberOfCoefficients(int Na, int Nb, int Nc)
Definition: ShapeType.hpp:194
int getNumberOfCoefficients(int Na, int Nb)
Definition: ShapeType.hpp:113
int getNumberOfCoefficients(int Na, int Nb, int Nc)
Definition: ShapeType.hpp:240

◆ CreateDefault()

FieldIOSharedPtr Nektar::LibUtilities::FieldIO::CreateDefault ( const LibUtilities::SessionReaderSharedPtr  session)
static

Returns an object for the default FieldIO method.

This function returns a FieldIO class as determined by the hard-coded default (XML), which can be overridden by changing the session reader SOLVERINFO variable FieldIOFormat.

Parameters
sessionSession reader
Returns
FieldIO object

Definition at line 195 of file FieldIO.cpp.

References Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreateInstance(), and Nektar::LibUtilities::GetFieldIOFactory().

Referenced by Nektar::VortexWaveInteraction::FileRelaxation(), Nektar::FilterBenchmark::FilterBenchmark(), Nektar::SolverUtils::FilterCheckpoint::FilterCheckpoint(), Nektar::FilterCheckpointCellModel::FilterCheckpointCellModel(), Nektar::SolverUtils::FilterModalEnergy::FilterModalEnergy(), Nektar::SolverUtils::FilterThresholdMax::FilterThresholdMax(), Nektar::SolverUtils::FilterThresholdMin::FilterThresholdMin(), Nektar::GlobalMapping::Mapping::Mapping(), and Nektar::SolverUtils::EquationSystem::v_InitObject().

197 {
198  std::string iofmt("Xml");
199  if (session->DefinesSolverInfo("IOFormat"))
200  {
201  iofmt = session->GetSolverInfo("IOFormat");
202  }
203 
204  if (session->DefinesCmdLineArgument("io-format"))
205  {
206  iofmt = session->GetCmdLineArgument<std::string>("io-format");
207  }
208 
210  iofmt,
211  session->GetComm(),
212  session->GetSharedFilesystem());
213 }
FieldIOFactory & GetFieldIOFactory()
Returns the FieldIO factory.
Definition: FieldIO.cpp:72
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:144

◆ CreateForFile()

FieldIOSharedPtr Nektar::LibUtilities::FieldIO::CreateForFile ( const LibUtilities::SessionReaderSharedPtr  session,
const std::string &  filename 
)
static

Construct a FieldIO object for a given input filename.

This is a convenience function that takes an input filename and constructs the appropriate FieldIO subclass, using FieldIO::GetFileType.

Parameters
sessionSession reader
filenameInput filename
Returns
FieldIO class reader for filename.

Definition at line 226 of file FieldIO.cpp.

References Nektar::LibUtilities::NekFactory< tKey, tBase, tParam >::CreateInstance(), Nektar::LibUtilities::GetFieldIOFactory(), and GetFileType().

Referenced by Nektar::SolverUtils::UnsteadySystem::CheckForRestartTime(), Nektar::SolverUtils::SessionFunction::EvaluateFld(), Nektar::GlobalMapping::Mapping::EvaluateFunction(), Nektar::SolverUtils::EquationSystem::ImportFld(), Nektar::LinearisedAdvection::ImportFldBase(), and Nektar::SolverUtils::FilterFieldConvert::v_Initialise().

229 {
230  const std::string iofmt =
231  FieldIO::GetFileType(filename, session->GetComm());
233  iofmt,
234  session->GetComm(),
235  session->GetSharedFilesystem());
236 }
FieldIOFactory & GetFieldIOFactory()
Returns the FieldIO factory.
Definition: FieldIO.cpp:72
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:144
static const std::string GetFileType(const std::string &filename, CommSharedPtr comm)
Determine file type of given input file.
Definition: FieldIO.cpp:97

◆ GetClassName()

virtual const std::string& Nektar::LibUtilities::FieldIO::GetClassName ( ) const
pure virtual

◆ GetFileEnding()

virtual std::string Nektar::LibUtilities::FieldIO::GetFileEnding ( ) const
inlineprotectedvirtual

Helper function that determines default file extension.

Reimplemented in Nektar::LibUtilities::PtsIO, and Nektar::LibUtilities::CsvIO.

Definition at line 276 of file FieldIO.h.

References LIB_UTILITIES_EXPORT, Nektar::NullInt1DArray, and Nektar::LibUtilities::NullVectorNekDoubleVector.

Referenced by Nektar::LibUtilities::FieldIOXml::SetUpFieldMetaData(), and SetUpOutput().

277  {
278  return "fld";
279  }

◆ GetFileType()

const std::string Nektar::LibUtilities::FieldIO::GetFileType ( const std::string &  filename,
CommSharedPtr  comm 
)
static

Determine file type of given input file.

This method attempts to identify the file type of a given input file filename. It returns a string corresponding to GetFieldIOFactory() or throws an assertion if it cannot be identified.

Parameters
filenameInput filename
commCommunicator for parallel runs
Returns
FieldIO format of filename.

Definition at line 97 of file FieldIO.cpp.

References ASSERTL0, Nektar::ErrorUtil::efatal, Nektar::LibUtilities::eHDF5, Nektar::LibUtilities::eXML, NEKERROR, and Nektar::LibUtilities::PortablePath().

Referenced by CreateForFile(), Nektar::MultiRegions::ExpList::ExtractFileBCs(), Nektar::FieldUtils::Field::FieldIOForFile(), Nektar::LibUtilities::Import(), and Nektar::SpatialDomains::MeshPartition::ReadExpansions().

99 {
100  FieldIOType ioType = eXML;
101  int size = comm->GetSize();
102  bool root = comm->TreatAsRankZero();
103 
104  if (size == 1 || root)
105  {
106  std::string datafilename;
107 
108  // If input is a directory, check for root processor file.
109  if (fs::is_directory(filename))
110  {
111  fs::path fullpath = filename;
112 
113  fs::path d = fullpath;
114  boost::regex expr("P\\d{7}.fld");
115  boost::smatch what;
116 
117  bool found = false;
118  for (auto &f : fs::directory_iterator(d))
119  {
120  if (boost::regex_match(f.path().filename().string(), what, expr))
121  {
122  found = true;
123  fullpath = f.path();
124  break;
125  }
126  }
127 
128  ASSERTL0(found,std::string("Failed to open a PXXXXXXX.fld file "
129  "in directory: " + filename).c_str());
130 
131  datafilename = PortablePath(fullpath);
132  }
133  else
134  {
135  datafilename = filename;
136  }
137 
138  // Read first 8 bytes. If they correspond with magic bytes below it's an
139  // HDF5 file. XML is potentially a nightmare with all the different
140  // encodings so we'll just assume it's OK if it's not HDF.
141  const unsigned char magic[8] = {
142  0x89, 0x48, 0x44, 0x46, 0x0d, 0x0a, 0x1a, 0x0a};
143 
144  std::ifstream datafile(datafilename.c_str(), std::ios_base::binary);
145  ASSERTL0(datafile.good(), "Unable to open file: " + filename);
146 
147  ioType = eHDF5;
148  for (unsigned i = 0; i < 8 && datafile.good(); ++i)
149  {
150  int byte = datafile.get();
151  if (byte != magic[i])
152  {
153  ioType = eXML;
154  break;
155  }
156  }
157  }
158 
159  if (size > 1)
160  {
161  int code = (int)ioType;
162  comm->Bcast(code, 0);
163  ioType = (FieldIOType)code;
164  }
165 
166  std::string iofmt;
167  if (ioType == eXML)
168  {
169  iofmt = "Xml";
170  }
171  else if (ioType == eHDF5)
172  {
173  iofmt = "Hdf5";
174  }
175  else
176  {
177  // Error
178  NEKERROR(ErrorUtil::efatal, "Unknown file format");
179  }
180 
181  return iofmt;
182 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209
FieldIOType
Enumerator for auto-detection of FieldIO types.
Definition: FieldIO.cpp:79
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
Definition: FileSystem.cpp:41

◆ Import()

void Nektar::LibUtilities::FieldIO::Import ( const std::string &  infilename,
std::vector< FieldDefinitionsSharedPtr > &  fielddefs,
std::vector< std::vector< NekDouble > > &  fielddata = NullVectorNekDoubleVector,
FieldMetaDataMap fieldinfo = NullFieldMetaDataMap,
const Array< OneD, int > &  ElementIDs = NullInt1DArray 
)
inline

Read field information from the file infilename.

Parameters
infilenameInput filename (or directory if parallel format)
fielddefsOn return contains field definitions as read from the input.
fielddataOn return, contains binary field data that stores the input corresponding to fielddefs.
fieldinfoOn returnm, contains the associated field metadata map.
ElementIDsElement IDs that lie on this processor, which can be optionally supplied to avoid reading the entire file on each processor.

Definition at line 339 of file FieldIO.h.

344 {
345  v_Import(infilename, fielddefs, fielddata, fieldinfo, ElementIDs);
346 }
virtual void v_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)=0
Read field information from the file infilename.

◆ ImportFieldMetaData()

DataSourceSharedPtr Nektar::LibUtilities::FieldIO::ImportFieldMetaData ( const std::string &  filename,
FieldMetaDataMap fieldmetadatamap 
)
inline

Import the metadata from a field file.

Parameters
filenameInput filename.
fieldmetadatamapOn return contains the field metadata map from filename.

Definition at line 355 of file FieldIO.h.

Referenced by Nektar::LibUtilities::PtsIO::Import(), and Nektar::LibUtilities::FieldIOXml::v_Import().

357 {
358  return v_ImportFieldMetaData(filename, fieldmetadatamap);
359 }
virtual DataSourceSharedPtr v_ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)=0
Import the metadata from a field file.

◆ SetUpOutput()

std::string Nektar::LibUtilities::FieldIO::SetUpOutput ( const std::string  outname,
bool  perRank,
bool  backup = false 
)
protected

Set up the filesystem ready for output.

This function sets up the output given an output filename outname. This will therefore remove any file or directory with the desired output filename and return the absolute path to the output.

If perRank is set, a new directory will be created to contain one file per process rank.

Parameters
outnameDesired output filename.
perRankTrue if one file-per-rank output is required.
Returns
Absolute path to resulting file.

Definition at line 410 of file FieldIO.cpp.

References ASSERTL0, Nektar::ErrorUtil::efatal, CellMLToNektar.pycml::format, GetFileEnding(), m_comm, m_sharedFilesystem, NEKERROR, Nektar::LibUtilities::PortablePath(), Nektar::LibUtilities::ReduceMax, and Nektar::LibUtilities::ReduceMin.

Referenced by Nektar::LibUtilities::FieldIOHdf5::v_Write(), Nektar::LibUtilities::FieldIOXml::v_Write(), Nektar::LibUtilities::CsvIO::Write(), and Nektar::LibUtilities::PtsIO::Write().

411 {
412  ASSERTL0(!outname.empty(), "Empty path given to SetUpOutput()");
413 
414  int nprocs = m_comm->GetSize();
415  bool root = m_comm->TreatAsRankZero();
416 
417  // Path to output: will be directory if parallel, normal file if
418  // serial.
419  fs::path specPath(outname), fulloutname;
420 
421  // in case we are rank 0 or not on a shared filesystem, check if the specPath already exists
422  if (backup && (root || !m_sharedFilesystem) && fs::exists(specPath))
423  {
424  // rename. foo/bar_123.chk -> foo/bar_123.bak0.chk and in case
425  // foo/bar_123.bak0.chk already exists, foo/bar_123.chk -> foo/bar_123.bak1.chk
426  fs::path bakPath = specPath;
427  int cnt = 0;
428  while (fs::exists(bakPath))
429  {
430  bakPath = specPath.parent_path();
431  bakPath += specPath.stem();
432  bakPath += fs::path(".bak" + std::to_string(cnt++));
433  bakPath += specPath.extension();
434  }
435  std::cout << "renaming " << specPath << " -> " << bakPath << std::endl;
436  try
437  {
438  fs::rename(specPath, bakPath);
439  }
440  catch (fs::filesystem_error &e)
441  {
442  ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
443  "Filesystem error: " + std::string(e.what()));
444  }
445  }
446 
447  // wait until rank 0 has moved the old specPath and the changes
448  // have propagated through the filesystem
449  if (backup)
450  {
451  m_comm->Block();
452  int exists = 1;
453  while (exists && perRank)
454  {
455  exists = fs::exists(specPath);
456  m_comm->AllReduce(exists, ReduceMax);
457  }
458  }
459 
460  if (nprocs == 1)
461  {
462  fulloutname = specPath;
463  }
464  else
465  {
466  // Guess at filename that might belong to this process.
467  boost::format pad("P%1$07d.%2$s");
468  pad % m_comm->GetRank() % GetFileEnding();
469 
470  // Generate full path name
471  fs::path poutfile(pad.str());
472  fulloutname = specPath / poutfile;
473  }
474 
475  // Remove any existing file which is in the way
476  if (m_comm->RemoveExistingFiles() && !backup)
477  {
478  if (m_sharedFilesystem)
479  {
480  // First, each process clears up its .fld file. This might or might
481  // not be there (we might have changed numbers of processors between
482  // runs, for example), but we can try anyway.
483  try
484  {
485  fs::remove_all(fulloutname);
486  }
487  catch (fs::filesystem_error &e)
488  {
489  ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
490  "Filesystem error: " + std::string(e.what()));
491  }
492  }
493 
494  m_comm->Block();
495 
496  // Now get rank 0 processor to tidy everything else up.
497  if (root || !m_sharedFilesystem)
498  {
499  try
500  {
501  fs::remove_all(specPath);
502  }
503  catch (fs::filesystem_error &e)
504  {
505  ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
506  "Filesystem error: " + std::string(e.what()));
507  }
508  }
509 
510  // wait until rank 0 has removed specPath and the changes
511  // have propagated through the filesystem
512  m_comm->Block();
513  int exists = 1;
514  while (exists && perRank)
515  {
516  exists = fs::exists(specPath);
517  m_comm->AllReduce(exists, ReduceMax);
518  }
519  }
520 
521  if (root)
522  {
523  std::cout << "Writing: " << specPath;
524  }
525 
526  // serial processing just add ending.
527  if (nprocs == 1)
528  {
529  return LibUtilities::PortablePath(specPath);
530  }
531 
532  // Create the destination directory
533  if (perRank)
534  {
535  try
536  {
537  if (root || !m_sharedFilesystem)
538  {
539  fs::create_directory(specPath);
540  }
541  }
542  catch (fs::filesystem_error &e)
543  {
545  "Filesystem error: " + std::string(e.what()));
546  }
547 
548  m_comm->Block();
549 
550  // Sit in a loop and make sure target directory has been created
551  int created = 0;
552  while (!created)
553  {
554  created = fs::is_directory(specPath);
555  m_comm->AllReduce(created, ReduceMin);
556  }
557  }
558  else
559  {
560  fulloutname = specPath;
561  }
562 
563  // Return the full path to the partition for this process
564  return LibUtilities::PortablePath(fulloutname);
565 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
#define NEKERROR(type, msg)
Assert Level 0 – Fundamental assert which is used whether in FULLDEBUG, DEBUG or OPT compilation mod...
Definition: ErrorUtil.hpp:209
bool m_sharedFilesystem
Boolean dictating whether we are on a shared filesystem.
Definition: FieldIO.h:268
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
Definition: FileSystem.cpp:41
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:266
virtual std::string GetFileEnding() const
Helper function that determines default file extension.
Definition: FieldIO.h:276

◆ v_Import()

virtual void Nektar::LibUtilities::FieldIO::v_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 
)
protectedpure virtual

Read field information from the file infilename.

Parameters
infilenameInput filename (or directory if parallel format)
fielddefsOn return contains field definitions as read from the input.
fielddataOn return, contains binary field data that stores the input corresponding to fielddefs.
fieldinfoOn returnm, contains the associated field metadata map.
ElementIDsElement IDs that lie on this processor, which can be optionally supplied to avoid reading the entire file on each processor.

Implemented in Nektar::LibUtilities::FieldIOXml, and Nektar::LibUtilities::FieldIOHdf5.

◆ v_ImportFieldMetaData()

virtual DataSourceSharedPtr Nektar::LibUtilities::FieldIO::v_ImportFieldMetaData ( const std::string &  filename,
FieldMetaDataMap fieldmetadatamap 
)
protectedpure virtual

Import the metadata from a field file.

Parameters
filenameInput filename.
fieldmetadatamapOn return contains the field metadata map from filename.

Implemented in Nektar::LibUtilities::FieldIOXml, and Nektar::LibUtilities::FieldIOHdf5.

◆ v_Write()

virtual void Nektar::LibUtilities::FieldIO::v_Write ( const std::string &  outFile,
std::vector< FieldDefinitionsSharedPtr > &  fielddefs,
std::vector< std::vector< NekDouble > > &  fielddata,
const FieldMetaDataMap fieldinfomap,
const bool  backup = false 
)
protectedpure virtual

Write out the field information to the file outFile.

Parameters
outFileOutput filename
fielddefsField definitions that define the output
fielddataBinary field data that stores the output corresponding to fielddefs.
fieldinfomapAssociated field metadata map.

Implemented in Nektar::LibUtilities::FieldIOXml, and Nektar::LibUtilities::FieldIOHdf5.

◆ Write()

void Nektar::LibUtilities::FieldIO::Write ( const std::string &  outFile,
std::vector< FieldDefinitionsSharedPtr > &  fielddefs,
std::vector< std::vector< NekDouble > > &  fielddata,
const FieldMetaDataMap fieldinfomap = NullFieldMetaDataMap,
const bool  backup = false 
)
inline

Write out the field information to the file outFile.

Parameters
outFileOutput filename
fielddefsField definitions that define the output
fielddataBinary field data that stores the output corresponding to fielddefs.
fieldinfomapAssociated field metadata map.

Definition at line 317 of file FieldIO.h.

322 {
323  v_Write(outFile, fielddefs, fielddata, fieldinfomap, backup);
324 }
virtual void v_Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap, const bool backup=false)=0
Write out the field information to the file outFile.

Member Data Documentation

◆ m_comm

LibUtilities::CommSharedPtr Nektar::LibUtilities::FieldIO::m_comm
protected

◆ m_sharedFilesystem

bool Nektar::LibUtilities::FieldIO::m_sharedFilesystem
protected

Boolean dictating whether we are on a shared filesystem.

Definition at line 268 of file FieldIO.h.

Referenced by SetUpOutput().