Nektar++
Public Member Functions | Protected Member Functions | List of all members
Nektar::LibUtilities::PtsIO Class Reference

#include <PtsIO.h>

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

Public Member Functions

 PtsIO (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem=false)
 
virtual ~PtsIO ()
 
void Import (const std::string &inFile, PtsFieldSharedPtr &ptsField, FieldMetaDataMap &fieldmetadatamap=NullFieldMetaDataMap)
 Import a pts field from file. More...
 
void Write (const std::string &outFile, const PtsFieldSharedPtr &ptsField, const bool backup=false)
 Save a pts field to a file. More...
 
void ImportFieldData (const std::string inFile, PtsFieldSharedPtr &ptsField)
 
- Public Member Functions inherited from Nektar::LibUtilities::FieldIOXml
 FieldIOXml (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
 Default constructor. More...
 
virtual ~FieldIOXml ()
 
void ImportFieldDefs (DataSourceSharedPtr dataSource, std::vector< FieldDefinitionsSharedPtr > &fielddefs, bool expChild)
 Import field definitions from the target file. More...
 
void ImportFieldData (DataSourceSharedPtr dataSource, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata)
 Import field data from a target file. More...
 
void WriteMultiFldFileIDs (const std::string &outfile, const std::vector< std::string > fileNames, std::vector< std::vector< unsigned int > > &elementList, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap)
 Write out a file containing element ID to partition mapping. More...
 
void SetUpFieldMetaData (const std::string &outname, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, const FieldMetaDataMap &fieldmetadatamap)
 Set up field meta data map. More...
 
void ImportMultiFldFileIDs (const std::string &inFile, std::vector< std::string > &fileNames, std::vector< std::vector< unsigned int > > &elementList, FieldMetaDataMap &fieldmetadatamap)
 Read file containing element ID to partition mapping. More...
 
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)
 Import an XML format file. More...
 
virtual const std::string & GetClassName () const
 Returns the class name. More...
 
- Public Member Functions inherited from Nektar::LibUtilities::FieldIO
 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...
 

Protected Member Functions

virtual void v_ImportFieldData (const std::string inFile, PtsFieldSharedPtr &ptsField)
 
void SetUpFieldMetaData (const std::string outname)
 
virtual std::string GetFileEnding () const
 Helper function that determines default file extension. More...
 
- Protected Member Functions inherited from Nektar::LibUtilities::FieldIO
int CheckFieldDefinition (const FieldDefinitionsSharedPtr &fielddefs)
 Check field definitions for correctness and return storage size. More...
 
std::string SetUpOutput (const std::string outname, bool perRank, bool backup=false)
 Set up the filesystem ready for output. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Nektar::LibUtilities::FieldIOXml
static FieldIOSharedPtr create (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
 Creates an instance of this class. More...
 
- Static Public Member Functions inherited from Nektar::LibUtilities::FieldIO
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...
 
- Static Public Attributes inherited from Nektar::LibUtilities::FieldIOXml
static std::string className
 Name of class. More...
 
- Protected Attributes inherited from Nektar::LibUtilities::FieldIO
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

Definition at line 60 of file PtsIO.h.

Constructor & Destructor Documentation

◆ PtsIO()

Nektar::LibUtilities::PtsIO::PtsIO ( LibUtilities::CommSharedPtr  pComm,
bool  sharedFilesystem = false 
)

Definition at line 58 of file PtsIO.cpp.

59  : FieldIOXml(pComm, sharedFilesystem)
60 {
61 }
FieldIOXml(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
Default constructor.
Definition: FieldIOXml.cpp:62

◆ ~PtsIO()

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

Member Function Documentation

◆ GetFileEnding()

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

Helper function that determines default file extension.

Reimplemented from Nektar::LibUtilities::FieldIO.

Reimplemented in Nektar::LibUtilities::CsvIO.

Definition at line 90 of file PtsIO.h.

Referenced by Import(), and SetUpFieldMetaData().

91  {
92  return "pts";
93  };

◆ Import()

void Nektar::LibUtilities::PtsIO::Import ( const std::string &  inFile,
PtsFieldSharedPtr ptsField,
FieldMetaDataMap fieldmetadatamap = NullFieldMetaDataMap 
)

Import a pts field from file.

Parameters
inFilefilename of the file to read
ptsFieldthe resulting pts field.

Definition at line 69 of file PtsIO.cpp.

References CellMLToNektar.pycml::format, GetFileEnding(), ImportFieldData(), Nektar::LibUtilities::FieldIO::ImportFieldMetaData(), Nektar::LibUtilities::FieldIOXml::ImportMultiFldFileIDs(), Nektar::LibUtilities::FieldIO::m_comm, and Nektar::LibUtilities::PortablePath().

Referenced by Nektar::SolverUtils::SessionFunction::EvaluatePts(), and ~PtsIO().

72 {
73  std::string infile = inFile;
74 
75  fs::path pinfilename(infile);
76 
77  // check to see that infile is a directory
78  if (fs::is_directory(pinfilename))
79  {
80  fs::path infofile("Info.xml");
81  fs::path fullpath = pinfilename / infofile;
82  infile = PortablePath(fullpath);
83 
84  std::vector<std::string> filenames;
85  std::vector<std::vector<unsigned int> > elementIDs_OnPartitions;
86 
88  infile, filenames, elementIDs_OnPartitions, fieldmetadatamap);
89 
90  // Load metadata
91  ImportFieldMetaData(infile, fieldmetadatamap);
92 
93  if (filenames.size() == m_comm->GetSize())
94  {
95  // only load the file that matches this rank
96  filenames.clear();
97  boost::format pad("P%1$07d.%2$s");
98  pad % m_comm->GetRank() % GetFileEnding();
99  filenames.push_back(pad.str());
100  }
101 
102  for (int i = 0; i < filenames.size(); ++i)
103  {
104  fs::path pfilename(filenames[i]);
105  fullpath = pinfilename / pfilename;
106  string fname = PortablePath(fullpath);
107 
108  if (i == 0)
109  {
110  ImportFieldData(fname, ptsField);
111  }
112  else
113  {
115  ImportFieldData(fname, newPtsField);
116  Array<OneD, Array<OneD, NekDouble> > pts;
117  newPtsField->GetPts(pts);
118  ptsField->AddPoints(pts);
119  }
120 
121  }
122  }
123  else
124  {
125  ImportFieldData(infile, ptsField);
126  }
127 }
void ImportMultiFldFileIDs(const std::string &inFile, std::vector< std::string > &fileNames, std::vector< std::vector< unsigned int > > &elementList, FieldMetaDataMap &fieldmetadatamap)
Read file containing element ID to partition mapping.
Definition: FieldIOXml.cpp:428
void ImportFieldData(const std::string inFile, PtsFieldSharedPtr &ptsField)
Definition: PtsIO.cpp:211
virtual std::string GetFileEnding() const
Helper function that determines default file extension.
Definition: PtsIO.h:90
std::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:179
DataSourceSharedPtr ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import the metadata from a field file.
Definition: FieldIO.h:355
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

◆ ImportFieldData()

void Nektar::LibUtilities::PtsIO::ImportFieldData ( const std::string  inFile,
PtsFieldSharedPtr ptsField 
)

Definition at line 211 of file PtsIO.cpp.

References v_ImportFieldData().

Referenced by Import(), and ~PtsIO().

212 {
213  v_ImportFieldData(inFile, ptsField);
214 }
virtual void v_ImportFieldData(const std::string inFile, PtsFieldSharedPtr &ptsField)
Definition: PtsIO.cpp:217

◆ SetUpFieldMetaData()

void Nektar::LibUtilities::PtsIO::SetUpFieldMetaData ( const std::string  outname)
protected

Definition at line 307 of file PtsIO.cpp.

References ASSERTL0, CellMLToNektar.pycml::format, GetFileEnding(), Nektar::LibUtilities::FieldIO::m_comm, Nektar::LibUtilities::PortablePath(), and Nektar::LibUtilities::FieldIOXml::WriteMultiFldFileIDs().

Referenced by Nektar::LibUtilities::CsvIO::Write(), Write(), and ~PtsIO().

308 {
309  ASSERTL0(!outname.empty(), "Empty path given to SetUpFieldMetaData()");
310 
311  int nprocs = m_comm->GetSize();
312  int rank = m_comm->GetRank();
313 
314  fs::path specPath(outname);
315 
316  // Collate per-process element lists on root process to generate
317  // the info file.
318  if (rank == 0)
319  {
320  // Set up output names
321  std::vector<std::string> filenames;
322  std::vector<std::vector<unsigned int> > ElementIDs;
323  for (int i = 0; i < nprocs; ++i)
324  {
325  boost::format pad("P%1$07d.%2$s");
326  pad % i % GetFileEnding();
327  filenames.push_back(pad.str());
328 
329  std::vector<unsigned int> tmp;
330  tmp.push_back(0);
331  ElementIDs.push_back(tmp);
332  }
333 
334  // Write the Info.xml file
335  string infofile =
336  LibUtilities::PortablePath(specPath / fs::path("Info.xml"));
337 
338  cout << "Writing: " << specPath << endl;
339 
340  const FieldMetaDataMap fieldmetadatamap;
341  WriteMultiFldFileIDs(infofile, filenames, ElementIDs, fieldmetadatamap);
342  }
343 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:52
virtual std::string GetFileEnding() const
Helper function that determines default file extension.
Definition: PtsIO.h:90
std::string PortablePath(const boost::filesystem::path &path)
create portable path on different platforms for boost::filesystem path
Definition: FileSystem.cpp:41
void WriteMultiFldFileIDs(const std::string &outfile, const std::vector< std::string > fileNames, std::vector< std::vector< unsigned int > > &elementList, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap)
Write out a file containing element ID to partition mapping.
Definition: FieldIOXml.cpp:380
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:266

◆ v_ImportFieldData()

void Nektar::LibUtilities::PtsIO::v_ImportFieldData ( const std::string  inFile,
PtsFieldSharedPtr ptsField 
)
protectedvirtual

Reimplemented in Nektar::LibUtilities::CsvIO.

Definition at line 217 of file PtsIO.cpp.

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), ASSERTL0, Nektar::ErrorUtil::efatal, Nektar::LibUtilities::eIsEquiSpacedData, Nektar::LibUtilities::ePtsPerElmtEdge, Nektar::ParseUtils::GenerateVector(), NEKERROR, and Nektar::LibUtilities::NullPtsInfoMap.

Referenced by ImportFieldData(), and ~PtsIO().

218 {
219  TiXmlDocument docInput(inFile);
220  bool loadOkay1 = docInput.LoadFile();
221 
222  std::stringstream errstr;
223  errstr << "Unable to load file: " << inFile << std::endl;
224  errstr << "Reason: " << docInput.ErrorDesc() << std::endl;
225  errstr << "Position: Line " << docInput.ErrorRow() << ", Column "
226  << docInput.ErrorCol() << std::endl;
227  ASSERTL0(loadOkay1, errstr.str());
228 
229  TiXmlElement *nektar = docInput.FirstChildElement("NEKTAR");
230  TiXmlElement *points = nektar->FirstChildElement("POINTS");
231  int dim;
232  int err = points->QueryIntAttribute("DIM", &dim);
233 
234  ASSERTL0(err == TIXML_SUCCESS, "Unable to read attribute DIM.");
235 
236  std::string fields = points->Attribute("FIELDS");
237 
238  vector<string> fieldNames;
239  if (!fields.empty())
240  {
241  bool valid =
242  ParseUtils::GenerateVector(fields, fieldNames);
243  ASSERTL0(
244  valid,
245  "Unable to process list of field variable in FIELDS attribute: " +
246  fields);
247  }
248 
249  map<PtsInfo,int> ptsInfo = NullPtsInfoMap;
250 
251  const char *ptinfo = points->Attribute("PTSINFO");
252  if(ptinfo&&boost::iequals(ptinfo,"EquiSpaced"))
253  {
254  ptsInfo[eIsEquiSpacedData] = 1;
255  }
256 
257  int np;
258  err = points->QueryIntAttribute("PTSPERELMTEDGE",&np);
259  if(err == TIXML_SUCCESS)
260  {
261  ptsInfo[ePtsPerElmtEdge] = np;
262  }
263 
264  size_t nfields = fieldNames.size();
265  size_t totvars = dim + nfields;
266 
267  TiXmlNode *pointsBody = points->FirstChild();
268 
269  std::istringstream pointsDataStrm(pointsBody->ToText()->Value());
270 
271  vector<NekDouble> ptsSerial;
272  Array<OneD, Array<OneD, NekDouble> > pts(totvars);
273 
274  try
275  {
276  NekDouble ptsStream;
277  while (!pointsDataStrm.fail())
278  {
279  pointsDataStrm >> ptsStream;
280 
281  ptsSerial.push_back(ptsStream);
282  }
283  }
284  catch (...)
285  {
286  NEKERROR(ErrorUtil::efatal, "Unable to read Points data.");
287  }
288 
289  size_t npts = ptsSerial.size() / totvars;
290 
291  for (size_t i = 0; i < totvars; ++i)
292  {
293  pts[i] = Array<OneD, NekDouble>(npts);
294  }
295 
296  for (size_t i = 0; i < npts; ++i)
297  {
298  for (size_t j = 0; j < totvars; ++j)
299  {
300  pts[j][i] = ptsSerial[i * totvars + j];
301  }
302  }
303 
304  ptsField = MemoryManager<PtsField>::AllocateSharedPtr(dim, fieldNames, pts, ptsInfo);
305 }
static std::map< PtsInfo, int > NullPtsInfoMap
Definition: PtsField.h:70
#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
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
double NekDouble
static bool GenerateVector(const std::string &str, std::vector< T > &out)
Takes a comma-separated string and converts it to entries in a vector.
Definition: ParseUtils.cpp:135

◆ Write()

void Nektar::LibUtilities::PtsIO::Write ( const std::string &  outFile,
const PtsFieldSharedPtr ptsField,
const bool  backup = false 
)

Save a pts field to a file.

Parameters
outFilefilename of the file
ptsFieldthe pts field

Definition at line 135 of file PtsIO.cpp.

References SetUpFieldMetaData(), and Nektar::LibUtilities::FieldIO::SetUpOutput().

Referenced by Nektar::FieldUtils::OutputPts::OutputFromPts(), and ~PtsIO().

138 {
139  size_t nTotvars = ptsField->GetNFields() + ptsField->GetDim();
140  size_t np = ptsField->GetNpoints();
141 
142  std::string filename = SetUpOutput(outFile, true, backup);
143  SetUpFieldMetaData(outFile);
144 
145  // until tinyxml gains support for line break, write the xml manually
146  std::ofstream ptsFile;
147  ptsFile.open(filename.c_str());
148 
149  ptsFile << "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" << endl;
150  ptsFile << "<NEKTAR>" << endl;
151  ptsFile << " <POINTS ";
152  ptsFile << "DIM=\"" << ptsField->GetDim() << "\" ";
153  string fn = boost::algorithm::join(ptsField->GetFieldNames(), ",");
154  ptsFile << "FIELDS=\"" << fn << "\" ";
155  ptsFile << ">" << endl;
156 
157  Array<OneD, Array<OneD, NekDouble> > pts;
158  ptsField->GetPts(pts);
159  for (size_t i = 0; i < np; ++i)
160  {
161  ptsFile << " ";
162  ptsFile << pts[0][i];
163  for (size_t j = 1; j < nTotvars; ++j)
164  {
165  ptsFile << " " << pts[j][i];
166  }
167  ptsFile << endl;
168  }
169  ptsFile << " </POINTS>" << endl;
170  ptsFile << "</NEKTAR>" << endl;
171 
172  ptsFile.close();
173 
174  // this is what the above cpart would read if tinyxml
175  // supported line breaks
176  /*
177  // Create the file (partition)
178  TiXmlDocument doc;
179  TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "utf-8", "");
180  doc.LinkEndChild(decl);
181 
182  TiXmlElement *root = new TiXmlElement("NEKTAR");
183  doc.LinkEndChild(root);
184 
185  TiXmlElement *pointsTag = new TiXmlElement("POINTS");
186  root->LinkEndChild(pointsTag);
187 
188  pointsTag->SetAttribute("DIM", ptsField->GetDim());
189 
190  string fn = boost::algorithm::join(ptsField->GetFieldNames(), ",");
191  pointsTag->SetAttribute("FIELDS", fn);
192 
193  Array <OneD, Array <OneD, NekDouble > > pts;
194  ptsField->GetPts(pts);
195  ostringstream os;
196  for (int i = 0; i < np; ++i)
197  {
198  os << pts[0][i];
199  for (int j = 1; j < nTotvars; ++j)
200  {
201  os << " " << pts[j][i];
202  }
203  os << " ";
204  }
205 
206  pointsTag->LinkEndChild(new TiXmlText(os.str()));
207 
208  doc.SaveFile(filename);
209  */
210 }
std::string SetUpOutput(const std::string outname, bool perRank, bool backup=false)
Set up the filesystem ready for output.
Definition: FieldIO.cpp:410
void SetUpFieldMetaData(const std::string outname)
Definition: PtsIO.cpp:307