Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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:
Inheritance graph
[legend]
Collaboration diagram for Nektar::LibUtilities::FieldIO:
Collaboration graph
[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 boost::shared_ptr< FieldIOCreateDefault (const LibUtilities::SessionReaderSharedPtr session)
 Returns an object for the default FieldIO method. More...
 
static boost::shared_ptr< FieldIOCreateForFile (const LibUtilities::SessionReaderSharedPtr session, const std::string &filename)
 Construct a FieldIO object for a given input filename. More...
 

Protected Member Functions

void AddInfoTag (TagWriterSharedPtr root, const FieldMetaDataMap &fieldmetadatamap)
 Add provenance information to the field metadata map. More...
 
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 224 of file FieldIO.h.

Constructor & Destructor Documentation

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

Constructor for FieldIO base class.

Definition at line 313 of file FieldIO.cpp.

314  : m_comm(pComm), m_sharedFilesystem(sharedFilesystem)
315 {
316 }
bool m_sharedFilesystem
Boolean dictating whether we are on a shared filesystem.
Definition: FieldIO.h:267
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:265
virtual Nektar::LibUtilities::FieldIO::~FieldIO ( )
inlinevirtual

Definition at line 230 of file FieldIO.h.

231  {
232  }

Member Function Documentation

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

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 334 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().

336 {
337  FieldMetaDataMap ProvenanceMap;
338 
339  // Nektar++ release version from VERSION file
340  ProvenanceMap["NektarVersion"] = string(NEKTAR_VERSION);
341 
342  // Date/time stamp
343  ptime::time_facet *facet = new ptime::time_facet("%d-%b-%Y %H:%M:%S");
344  std::stringstream wss;
345  wss.imbue(locale(wss.getloc(), facet));
346  wss << ptime::second_clock::local_time();
347  ProvenanceMap["Timestamp"] = wss.str();
348 
349  // Hostname
350  boost::system::error_code ec;
351  ProvenanceMap["Hostname"] = ip::host_name(ec);
352 
353  // Git information
354  // If built from a distributed package, do not include this
355  if (NekConstants::kGitSha1 != "GITDIR-NOTFOUND")
356  {
357  ProvenanceMap["GitSHA1"] = NekConstants::kGitSha1;
358  ProvenanceMap["GitBranch"] = NekConstants::kGitBranch;
359  }
360 
361  TagWriterSharedPtr infoTag = root->AddChild("Metadata");
362 
363  FieldMetaDataMap::const_iterator infoit;
364 
365  TagWriterSharedPtr provTag = infoTag->AddChild("Provenance");
366  for (infoit = ProvenanceMap.begin(); infoit != ProvenanceMap.end();
367  ++infoit)
368  {
369  provTag->SetAttr(infoit->first, infoit->second);
370  }
371 
372  //---------------------------------------------
373  // write field info section
374  if (fieldmetadatamap != NullFieldMetaDataMap)
375  {
376  for (infoit = fieldmetadatamap.begin();
377  infoit != fieldmetadatamap.end();
378  ++infoit)
379  {
380  infoTag->SetAttr(infoit->first, infoit->second);
381  }
382  }
383 }
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:54
const std::string kGitSha1
const std::string kGitBranch
#define NEKTAR_VERSION
Definition: FieldIO.cpp:56
boost::shared_ptr< TagWriter > TagWriterSharedPtr
Definition: FieldIO.h:71
static FieldMetaDataMap NullFieldMetaDataMap
Definition: FieldIO.h:55
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 561 of file FieldIO.cpp.

References ASSERTL0, 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(), and Nektar::LibUtilities::StdPrismData::getNumberOfCoefficients().

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

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

References Nektar::LibUtilities::NekFactory< tKey, tBase, >::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().

183 {
184  std::string iofmt("Xml");
185  if (session->DefinesSolverInfo("IOFormat"))
186  {
187  iofmt = session->GetSolverInfo("IOFormat");
188  }
189 
190  if (session->DefinesCmdLineArgument("io-format"))
191  {
192  iofmt = session->GetCmdLineArgument<std::string>("io-format");
193  }
194 
196  iofmt,
197  session->GetComm(),
198  session->GetSharedFilesystem());
199 }
tBaseSharedPtr CreateInstance(tKey idKey BOOST_PP_COMMA_IF(MAX_PARAM) BOOST_PP_ENUM_BINARY_PARAMS(MAX_PARAM, tParam, x))
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:162
FieldIOFactory & GetFieldIOFactory()
Returns the FieldIO factory.
Definition: FieldIO.cpp:74
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 212 of file FieldIO.cpp.

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

Referenced by Nektar::SolverUtils::UnsteadySystem::CheckForRestartTime(), Nektar::SolverUtils::Forcing::EvaluateFunction(), Nektar::GlobalMapping::Mapping::EvaluateFunction(), Nektar::SolverUtils::EquationSystem::EvaluateFunctionFld(), Nektar::SolverUtils::EquationSystem::ImportFld(), Nektar::LinearisedAdvection::ImportFldBase(), Nektar::SolverUtils::EquationSystem::ImportFldBase(), Nektar::SpatialDomains::MeshGraph::ReadExpansions(), and Nektar::SolverUtils::FilterFieldConvert::v_Initialise().

215 {
216  const std::string iofmt =
217  FieldIO::GetFileType(filename, session->GetComm());
219  iofmt,
220  session->GetComm(),
221  session->GetSharedFilesystem());
222 }
tBaseSharedPtr CreateInstance(tKey idKey BOOST_PP_COMMA_IF(MAX_PARAM) BOOST_PP_ENUM_BINARY_PARAMS(MAX_PARAM, tParam, x))
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:162
FieldIOFactory & GetFieldIOFactory()
Returns the FieldIO factory.
Definition: FieldIO.cpp:74
static const std::string GetFileType(const std::string &filename, CommSharedPtr comm)
Determine file type of given input file.
Definition: FieldIO.cpp:101
virtual const std::string& Nektar::LibUtilities::FieldIO::GetClassName ( ) const
pure virtual
virtual std::string Nektar::LibUtilities::FieldIO::GetFileEnding ( ) const
inlineprotectedvirtual

Helper function that determines default file extension.

Reimplemented in Nektar::LibUtilities::PtsIO.

Definition at line 279 of file FieldIO.h.

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

280  {
281  return "fld";
282  }
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 101 of file FieldIO.cpp.

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

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

103 {
104  FieldIOType ioType = eXML;
105  int size = comm->GetSize();
106  int rank = comm->GetRank();
107 
108  if (size == 1 || rank == 0)
109  {
110  std::string datafilename;
111 
112  // If input is a directory, check for root processor file.
113  if (fs::is_directory(filename))
114  {
115  fs::path p0file("P0000000.fld");
116  fs::path fullpath = filename / p0file;
117  datafilename = PortablePath(fullpath);
118  }
119  else
120  {
121  datafilename = filename;
122  }
123 
124  // Read first 8 bytes. If they correspond with magic bytes below it's an
125  // HDF5 file. XML is potentially a nightmare with all the different
126  // encodings so we'll just assume it's OK if it's not HDF.
127  const unsigned char magic[8] = {
128  0x89, 0x48, 0x44, 0x46, 0x0d, 0x0a, 0x1a, 0x0a};
129 
130  std::ifstream datafile(datafilename.c_str(), ios_base::binary);
131  ASSERTL0(datafile.good(), "Unable to open file: " + filename);
132 
133  ioType = eHDF5;
134  for (unsigned i = 0; i < 8 && datafile.good(); ++i)
135  {
136  unsigned char byte = datafile.get();
137  if (byte != magic[i])
138  {
139  ioType = eXML;
140  break;
141  }
142  }
143  }
144 
145  if (size > 1)
146  {
147  int code = (int)ioType;
148  comm->Bcast(code, 0);
149  ioType = (FieldIOType)code;
150  }
151 
152  std::string iofmt;
153  if (ioType == eXML)
154  {
155  iofmt = "Xml";
156  }
157  else if (ioType == eHDF5)
158  {
159  iofmt = "Hdf5";
160  }
161  else
162  {
163  // Error
164  ASSERTL0(false, "Unknown file format");
165  }
166 
167  return iofmt;
168 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
FieldIOType
Enumerator for auto-detection of FieldIO types.
Definition: FieldIO.cpp:83
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
Definition: FileSystem.cpp:41
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 342 of file FieldIO.h.

References v_Import().

347 {
348  v_Import(infilename, fielddefs, fielddata, fieldinfo, ElementIDs);
349 }
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.
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 358 of file FieldIO.h.

References v_ImportFieldMetaData().

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

360 {
361  return v_ImportFieldMetaData(filename, fieldmetadatamap);
362 }
virtual DataSourceSharedPtr v_ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)=0
Import the metadata from a field file.
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 400 of file FieldIO.cpp.

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

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

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

Referenced by Import().

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::FieldIOHdf5, and Nektar::LibUtilities::FieldIOXml.

Referenced by ImportFieldMetaData().

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.

Referenced by 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 320 of file FieldIO.h.

References v_Write().

325 {
326  v_Write(outFile, fielddefs, fielddata, fieldinfomap, backup);
327 }
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

LibUtilities::CommSharedPtr Nektar::LibUtilities::FieldIO::m_comm
protected
bool Nektar::LibUtilities::FieldIO::m_sharedFilesystem
protected

Boolean dictating whether we are on a shared filesystem.

Definition at line 267 of file FieldIO.h.

Referenced by SetUpOutput().