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

546 {
547  int i;
548 
549  if (fielddefs->m_elementIDs.size() == 0) // empty partition
550  {
551  return 0;
552  }
553 
554  unsigned int numbasis = 0;
555 
556  // Determine nummodes vector lists are correct length
557  switch (fielddefs->m_shapeType)
558  {
559  case eSegment:
560  numbasis = 1;
561  if (fielddefs->m_numHomogeneousDir)
562  {
563  numbasis += fielddefs->m_numHomogeneousDir;
564  }
565 
566  break;
567  case eTriangle:
568  case eQuadrilateral:
569  if (fielddefs->m_numHomogeneousDir)
570  {
571  numbasis = 3;
572  }
573  else
574  {
575  numbasis = 2;
576  }
577  break;
578  case eTetrahedron:
579  case ePyramid:
580  case ePrism:
581  case eHexahedron:
582  numbasis = 3;
583  break;
584  default:
585  ASSERTL0(false, "Unsupported shape type.");
586  break;
587  }
588 
589  unsigned int datasize = 0;
590 
591  ASSERTL0(fielddefs->m_basis.size() == numbasis,
592  "Length of basis vector is incorrect");
593 
594  if (fielddefs->m_uniOrder == true)
595  {
596  unsigned int cnt = 0;
597  // calculate datasize
598  switch (fielddefs->m_shapeType)
599  {
600  case eSegment:
601  {
602  int l = fielddefs->m_numModes[cnt++];
603  if (fielddefs->m_numHomogeneousDir == 1)
604  {
605  datasize += l * fielddefs->m_homogeneousZIDs.size();
606  cnt++;
607  }
608  else if (fielddefs->m_numHomogeneousDir == 2)
609  {
610  datasize += l * fielddefs->m_homogeneousYIDs.size();
611  cnt += 2;
612  }
613  else
614  {
615  datasize += l;
616  }
617  }
618  break;
619  case eTriangle:
620  {
621  int l = fielddefs->m_numModes[cnt++];
622  int m = fielddefs->m_numModes[cnt++];
623 
624  if (fielddefs->m_numHomogeneousDir == 1)
625  {
626  datasize += StdTriData::getNumberOfCoefficients(l, m) *
627  fielddefs->m_homogeneousZIDs.size();
628  }
629  else
630  {
631  datasize += StdTriData::getNumberOfCoefficients(l, m);
632  }
633  }
634  break;
635  case eQuadrilateral:
636  {
637  int l = fielddefs->m_numModes[cnt++];
638  int m = fielddefs->m_numModes[cnt++];
639  if (fielddefs->m_numHomogeneousDir == 1)
640  {
641  datasize += l * m * fielddefs->m_homogeneousZIDs.size();
642  }
643  else
644  {
645  datasize += l * m;
646  }
647  }
648  break;
649  case eTetrahedron:
650  {
651  int l = fielddefs->m_numModes[cnt++];
652  int m = fielddefs->m_numModes[cnt++];
653  int n = fielddefs->m_numModes[cnt++];
654  datasize += StdTetData::getNumberOfCoefficients(l, m, n);
655  }
656  break;
657  case ePyramid:
658  {
659  int l = fielddefs->m_numModes[cnt++];
660  int m = fielddefs->m_numModes[cnt++];
661  int n = fielddefs->m_numModes[cnt++];
662  datasize += StdPyrData::getNumberOfCoefficients(l, m, n);
663  }
664  break;
665  case ePrism:
666  {
667  int l = fielddefs->m_numModes[cnt++];
668  int m = fielddefs->m_numModes[cnt++];
669  int n = fielddefs->m_numModes[cnt++];
670  datasize += StdPrismData::getNumberOfCoefficients(l, m, n);
671  }
672  break;
673  case eHexahedron:
674  {
675  int l = fielddefs->m_numModes[cnt++];
676  int m = fielddefs->m_numModes[cnt++];
677  int n = fielddefs->m_numModes[cnt++];
678  datasize += l * m * n;
679  }
680  break;
681  default:
682  ASSERTL0(false, "Unsupported shape type.");
683  break;
684  }
685 
686  datasize *= fielddefs->m_elementIDs.size();
687  }
688  else
689  {
690  unsigned int cnt = 0;
691  // calculate data length
692  for (i = 0; i < fielddefs->m_elementIDs.size(); ++i)
693  {
694  switch (fielddefs->m_shapeType)
695  {
696  case eSegment:
697  {
698  int l = fielddefs->m_numModes[cnt++];
699  if (fielddefs->m_numHomogeneousDir == 1)
700  {
701  datasize += l * fielddefs->m_homogeneousZIDs.size();
702  cnt++;
703  }
704  else if (fielddefs->m_numHomogeneousDir == 2)
705  {
706  datasize += l * fielddefs->m_homogeneousYIDs.size();
707  cnt += 2;
708  }
709  else
710  {
711  datasize += l;
712  }
713  }
714  break;
715  case eTriangle:
716  {
717  int l = fielddefs->m_numModes[cnt++];
718  int m = fielddefs->m_numModes[cnt++];
719  if (fielddefs->m_numHomogeneousDir == 1)
720  {
721  datasize += StdTriData::getNumberOfCoefficients(l, m) *
722  fielddefs->m_homogeneousZIDs.size();
723  cnt++;
724  }
725  else
726  {
727  datasize += StdTriData::getNumberOfCoefficients(l, m);
728  }
729  }
730  break;
731  case eQuadrilateral:
732  {
733  int l = fielddefs->m_numModes[cnt++];
734  int m = fielddefs->m_numModes[cnt++];
735  if (fielddefs->m_numHomogeneousDir == 1)
736  {
737  datasize += l * m * fielddefs->m_homogeneousZIDs.size();
738  cnt++;
739  }
740  else
741  {
742  datasize += l * m;
743  }
744  }
745  break;
746  case eTetrahedron:
747  {
748  int l = fielddefs->m_numModes[cnt++];
749  int m = fielddefs->m_numModes[cnt++];
750  int n = fielddefs->m_numModes[cnt++];
751  datasize += StdTetData::getNumberOfCoefficients(l, m, n);
752  }
753  break;
754  case ePyramid:
755  {
756  int l = fielddefs->m_numModes[cnt++];
757  int m = fielddefs->m_numModes[cnt++];
758  int n = fielddefs->m_numModes[cnt++];
759  datasize += StdPyrData::getNumberOfCoefficients(l, m, n);
760  }
761  break;
762  case ePrism:
763  {
764  int l = fielddefs->m_numModes[cnt++];
765  int m = fielddefs->m_numModes[cnt++];
766  int n = fielddefs->m_numModes[cnt++];
767  datasize += StdPrismData::getNumberOfCoefficients(l, m, n);
768  }
769  break;
770  case eHexahedron:
771  {
772  int l = fielddefs->m_numModes[cnt++];
773  int m = fielddefs->m_numModes[cnt++];
774  int n = fielddefs->m_numModes[cnt++];
775  datasize += l * m * n;
776  }
777  break;
778  default:
779  ASSERTL0(false, "Unsupported shape type.");
780  break;
781  }
782  }
783  }
784 
785  return datasize;
786 }
#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(), 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  // wait until rank 0 has backed up the old specPath
437  if (backup)
438  {
439  m_comm->Block();
440  }
441 
442  if (nprocs == 1)
443  {
444  fulloutname = specPath;
445  }
446  else
447  {
448  // Guess at filename that might belong to this process.
449  boost::format pad("P%1$07d.%2$s");
450  pad % m_comm->GetRank() % GetFileEnding();
451 
452  // Generate full path name
453  fs::path poutfile(pad.str());
454  fulloutname = specPath / poutfile;
455  }
456 
457  // Remove any existing file which is in the way
458  if (m_comm->RemoveExistingFiles())
459  {
460  if (m_sharedFilesystem)
461  {
462  // First, each process clears up its .fld file. This might or might
463  // not be there (we might have changed numbers of processors between
464  // runs, for example), but we can try anyway.
465  try
466  {
467  fs::remove_all(fulloutname);
468  }
469  catch (fs::filesystem_error &e)
470  {
471  ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
472  "Filesystem error: " + string(e.what()));
473  }
474  }
475 
476  m_comm->Block();
477 
478  // Now get rank 0 processor to tidy everything else up.
479  if (rank == 0 || !m_sharedFilesystem)
480  {
481  try
482  {
483  fs::remove_all(specPath);
484  }
485  catch (fs::filesystem_error &e)
486  {
487  ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
488  "Filesystem error: " + string(e.what()));
489  }
490  }
491 
492  m_comm->Block();
493  }
494 
495  if (rank == 0)
496  {
497  cout << "Writing: " << specPath;
498  }
499 
500  // serial processing just add ending.
501  if (nprocs == 1)
502  {
503  return LibUtilities::PortablePath(specPath);
504  }
505 
506  // Create the destination directory
507  if (perRank)
508  {
509  try
510  {
511  if (rank == 0 || !m_sharedFilesystem)
512  {
513  fs::create_directory(specPath);
514  }
515  }
516  catch (fs::filesystem_error &e)
517  {
518  ASSERTL0(false, "Filesystem error: " + string(e.what()));
519  }
520 
521  m_comm->Block();
522 
523  // Sit in a loop and make sure target directory has been created
524  int created = 0;
525  while (!created)
526  {
527  created = fs::is_directory(specPath);
528  m_comm->AllReduce(created, ReduceMin);
529  }
530  }
531  else
532  {
533  fulloutname = specPath;
534  }
535 
536  // Return the full path to the partition for this process
537  return LibUtilities::PortablePath(fulloutname);
538 }
#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().