Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Member Functions | Protected Member Functions | List of all members
Nektar::LibUtilities::PtsIO Class Reference

#include <PtsIO.h>

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

Public Member Functions

 PtsIO (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem=false)
 
void Import (const string &inFile, PtsFieldSharedPtr &ptsField, FieldMetaDataMap &fieldmetadatamap=NullFieldMetaDataMap)
 Import a pts field from file. More...
 
void Write (const string &outFile, const PtsFieldSharedPtr &ptsField)
 Save a pts field to a file. More...
 
void ImportFieldData (TiXmlDocument docInput, PtsFieldSharedPtr &ptsField)
 

Protected Member Functions

void SetUpFieldMetaData (const std::string outname)
 
virtual std::string GetFileEnding () const
 
- Protected Member Functions inherited from Nektar::LibUtilities::FieldIO
void AddInfoTag (TiXmlElement *root, const FieldMetaDataMap &fieldmetadatamap)
 add information about provenance and fieldmetadata More...
 
void GenerateSeqString (const std::vector< unsigned int > &elmtids, std::string &idString)
 
std::string SetUpOutput (const std::string outname)
 
void SetUpFieldMetaData (const std::string outname, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, const FieldMetaDataMap &fieldmetadatamap)
 
void ImportMultiFldFileIDs (const std::string &inFile, std::vector< std::string > &fileNames, std::vector< std::vector< unsigned int > > &elementList, FieldMetaDataMap &fieldmetadatamap)
 
int CheckFieldDefinition (const FieldDefinitionsSharedPtr &fielddefs)
 
 FieldIO (LibUtilities::CommSharedPtr pComm, bool sharedFilesystem=false)
 Constructor. More...
 
void Write (const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap)
 Write data in FLD format. 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)
 Imports an FLD file. More...
 
void ImportFieldMetaData (std::string filename, FieldMetaDataMap &fieldmetadatamap)
 Imports the definition of the meta data. More...
 
void ImportFieldMetaData (TiXmlDocument &doc, FieldMetaDataMap &fieldmetadatamap)
 Imports the definition of the meta data. More...
 
void ImportFieldDefs (TiXmlDocument &doc, std::vector< FieldDefinitionsSharedPtr > &fielddefs, bool expChild)
 Imports the definition of the fields. More...
 
void ImportFieldData (TiXmlDocument &doc, const std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata)
 Imports the data fileds. More...
 
void WriteMultiFldFileIDs (const std::string &outfile, const std::vector< std::string > fileNames, std::vector< std::vector< unsigned int > > &elementList, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap)
 

Additional Inherited Members

- Protected Attributes inherited from Nektar::LibUtilities::FieldIO
LibUtilities::CommSharedPtr m_comm
 Communicator to use when writing parallel format. More...
 
bool m_sharedFilesystem
 True if same filesystem accessible by all processes. More...
 

Detailed Description

Definition at line 70 of file PtsIO.h.

Constructor & Destructor Documentation

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

Definition at line 106 of file PtsIO.cpp.

107  : FieldIO(pComm, sharedFilesystem)
108 {
109 }
FieldIO(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem=false)
Constructor.
Definition: FieldIO.cpp:149

Member Function Documentation

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

Reimplemented from Nektar::LibUtilities::FieldIO.

Definition at line 90 of file PtsIO.h.

Referenced by Import(), and SetUpFieldMetaData().

91  {
92  return "pts";
93  };
void Nektar::LibUtilities::PtsIO::Import ( const 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 117 of file PtsIO.cpp.

References ASSERTL0, GetFileEnding(), ImportFieldData(), Nektar::LibUtilities::FieldIO::ImportFieldMetaData(), Nektar::LibUtilities::FieldIO::ImportMultiFldFileIDs(), Nektar::LibUtilities::FieldIO::m_comm, and Nektar::LibUtilities::PortablePath().

Referenced by Nektar::SolverUtils::EquationSystem::EvaluateFunction(), and Nektar::LibUtilities::Import().

120 {
121  std::string infile = inFile;
122 
123  fs::path pinfilename(infile);
124 
125  // check to see that infile is a directory
126  if (fs::is_directory(pinfilename))
127  {
128  fs::path infofile("Info.xml");
129  fs::path fullpath = pinfilename / infofile;
130  infile = PortablePath(fullpath);
131 
132  std::vector<std::string> filenames;
133  std::vector<std::vector<unsigned int> > elementIDs_OnPartitions;
134 
136  infile, filenames, elementIDs_OnPartitions, fieldmetadatamap);
137 
138  // Load metadata
139  ImportFieldMetaData(infile, fieldmetadatamap);
140 
141  // TODO: This currently only loads the filename matching our rank.
142  filenames.clear();
143  boost::format pad("P%1$07d.%2$s");
144  pad % m_comm->GetRank() % GetFileEnding();
145  filenames.push_back(pad.str());
146 
147  for (int i = 0; i < filenames.size(); ++i)
148  {
149  fs::path pfilename(filenames[i]);
150  fullpath = pinfilename / pfilename;
151  string fname = PortablePath(fullpath);
152 
153  TiXmlDocument doc1(fname);
154  bool loadOkay1 = doc1.LoadFile();
155 
156  std::stringstream errstr;
157  errstr << "Unable to load file: " << fname << std::endl;
158  errstr << "Reason: " << doc1.ErrorDesc() << std::endl;
159  errstr << "Position: Line " << doc1.ErrorRow() << ", Column "
160  << doc1.ErrorCol() << std::endl;
161  ASSERTL0(loadOkay1, errstr.str());
162 
163  ImportFieldData(doc1, ptsField);
164  }
165  }
166  else
167  {
168  TiXmlDocument doc(infile);
169  bool loadOkay = doc.LoadFile();
170 
171  std::stringstream errstr;
172  errstr << "Unable to load file: " << infile << std::endl;
173  errstr << "Reason: " << doc.ErrorDesc() << std::endl;
174  errstr << "Position: Line " << doc.ErrorRow() << ", Column "
175  << doc.ErrorCol() << std::endl;
176  ASSERTL0(loadOkay, errstr.str());
177 
178  ImportFieldData(doc, ptsField);
179  }
180 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
void ImportMultiFldFileIDs(const std::string &inFile, std::vector< std::string > &fileNames, std::vector< std::vector< unsigned int > > &elementList, FieldMetaDataMap &fieldmetadatamap)
Definition: FieldIO.cpp:574
void ImportFieldData(TiXmlDocument docInput, PtsFieldSharedPtr &ptsField)
Definition: PtsIO.cpp:264
virtual std::string GetFileEnding() const
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
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:191
void ImportFieldMetaData(std::string filename, FieldMetaDataMap &fieldmetadatamap)
Imports the definition of the meta data.
Definition: FieldIO.cpp:634
void Nektar::LibUtilities::PtsIO::ImportFieldData ( TiXmlDocument  docInput,
PtsFieldSharedPtr ptsField 
)

Definition at line 264 of file PtsIO.cpp.

References Nektar::MemoryManager< DataType >::AllocateSharedPtr(), ASSERTL0, Nektar::LibUtilities::eIsEquiSpacedData, Nektar::LibUtilities::ePtsPerElmtEdge, Nektar::ParseUtils::GenerateOrderedStringVector(), npts, and Nektar::LibUtilities::NullPtsInfoMap.

Referenced by Import().

265 {
266  TiXmlElement *nektar = docInput.FirstChildElement("NEKTAR");
267  TiXmlElement *points = nektar->FirstChildElement("POINTS");
268  int dim;
269  int err = points->QueryIntAttribute("DIM", &dim);
270 
271  ASSERTL0(err == TIXML_SUCCESS, "Unable to read attribute DIM.");
272 
273  std::string fields = points->Attribute("FIELDS");
274 
275  vector<string> fieldNames;
276  if (!fields.empty())
277  {
278  bool valid =
279  ParseUtils::GenerateOrderedStringVector(fields.c_str(), fieldNames);
280  ASSERTL0(
281  valid,
282  "Unable to process list of field variable in FIELDS attribute: " +
283  fields);
284  }
285 
286  map<PtsInfo,int> ptsInfo = NullPtsInfoMap;
287 
288  const char *ptinfo = points->Attribute("PTSINFO");
289  if(ptinfo&&boost::iequals(ptinfo,"EquiSpaced"))
290  {
291  ptsInfo[eIsEquiSpacedData] = 1;
292  }
293 
294  int np;
295  err = points->QueryIntAttribute("PTSPERELMTEDGE",&np);
296  if(err == TIXML_SUCCESS)
297  {
298  ptsInfo[ePtsPerElmtEdge] = np;
299  }
300 
301  int nfields = fieldNames.size();
302  int totvars = dim + nfields;
303 
304  TiXmlNode *pointsBody = points->FirstChild();
305 
306  std::istringstream pointsDataStrm(pointsBody->ToText()->Value());
307 
308  vector<NekDouble> ptsSerial;
309  Array<OneD, Array<OneD, NekDouble> > pts(totvars);
310 
311  try
312  {
313  NekDouble ptsStream;
314  while (!pointsDataStrm.fail())
315  {
316  pointsDataStrm >> ptsStream;
317 
318  ptsSerial.push_back(ptsStream);
319  }
320  }
321  catch (...)
322  {
323  ASSERTL0(false, "Unable to read Points data.");
324  }
325 
326  int npts = ptsSerial.size() / totvars;
327 
328  for (int i = 0; i < totvars; ++i)
329  {
330  pts[i] = Array<OneD, NekDouble>(npts);
331  }
332 
333  for (int i = 0; i < npts; ++i)
334  {
335  for (int j = 0; j < totvars; ++j)
336  {
337  pts[j][i] = ptsSerial[i * totvars + j];
338  }
339  }
340 
341  ptsField = MemoryManager<PtsField>::AllocateSharedPtr(dim, fieldNames, pts, ptsInfo);
342 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
static bool GenerateOrderedStringVector(const char *const str, std::vector< std::string > &vec)
Definition: ParseUtils.hpp:143
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
static std::string npts
Definition: InputFld.cpp:43
double NekDouble
static map< PtsInfo, int > NullPtsInfoMap
Definition: PtsField.h:72
void Nektar::LibUtilities::PtsIO::SetUpFieldMetaData ( const std::string  outname)
protected

Definition at line 344 of file PtsIO.cpp.

References ASSERTL0, GetFileEnding(), Nektar::LibUtilities::FieldIO::m_comm, Nektar::LibUtilities::PortablePath(), and Nektar::LibUtilities::FieldIO::WriteMultiFldFileIDs().

Referenced by Write().

345 {
346  ASSERTL0(!outname.empty(), "Empty path given to SetUpFieldMetaData()");
347 
348  int nprocs = m_comm->GetSize();
349  int rank = m_comm->GetRank();
350 
351  fs::path specPath(outname);
352 
353  // Collate per-process element lists on root process to generate
354  // the info file.
355  if (rank == 0)
356  {
357  // Set up output names
358  std::vector<std::string> filenames;
359  std::vector<std::vector<unsigned int> > ElementIDs;
360  for (int i = 0; i < nprocs; ++i)
361  {
362  boost::format pad("P%1$07d.%2$s");
363  pad % i % GetFileEnding();
364  filenames.push_back(pad.str());
365 
366  std::vector<unsigned int> tmp;
367  tmp.push_back(0);
368  ElementIDs.push_back(tmp);
369  }
370 
371  // Write the Info.xml file
372  string infofile =
373  LibUtilities::PortablePath(specPath / fs::path("Info.xml"));
374 
375  cout << "Writing: " << specPath << endl;
376 
377  const FieldMetaDataMap fieldmetadatamap;
378  WriteMultiFldFileIDs(infofile, filenames, ElementIDs, fieldmetadatamap);
379  }
380 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:53
virtual std::string GetFileEnding() const
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
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:191
void WriteMultiFldFileIDs(const std::string &outfile, const std::vector< std::string > fileNames, std::vector< std::vector< unsigned int > > &elementList, const FieldMetaDataMap &fieldinfomap=NullFieldMetaDataMap)
Definition: FieldIO.cpp:534
void Nektar::LibUtilities::PtsIO::Write ( const string &  outFile,
const PtsFieldSharedPtr ptsField 
)

Save a pts field to a file.

Parameters
outFilefilename of the file
ptsFieldthe pts field

Definition at line 188 of file PtsIO.cpp.

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

Referenced by Nektar::LibUtilities::Write().

190 {
191  int nTotvars = ptsField->GetNFields() + ptsField->GetDim();
192  int np = ptsField->GetNpoints();
193 
194  std::string filename = SetUpOutput(outFile);
195  SetUpFieldMetaData(outFile);
196 
197  // until tinyxml gains support for line break, write the xml manually
198  std::ofstream ptsFile;
199  ptsFile.open(filename.c_str());
200 
201  ptsFile << "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" << endl;
202  ptsFile << "<NEKTAR>" << endl;
203  ptsFile << " <POINTS ";
204  ptsFile << "DIM=\"" << ptsField->GetDim() << "\" ";
205  string fn = boost::algorithm::join(ptsField->GetFieldNames(), ",");
206  ptsFile << "FIELDS=\"" << fn << "\" ";
207  ptsFile << ">" << endl;
208 
209  Array<OneD, Array<OneD, NekDouble> > pts;
210  ptsField->GetPts(pts);
211  for (int i = 0; i < np; ++i)
212  {
213  ptsFile << " ";
214  ptsFile << pts[0][i];
215  for (int j = 1; j < nTotvars; ++j)
216  {
217  ptsFile << " " << pts[j][i];
218  }
219  ptsFile << endl;
220  }
221  ptsFile << " </POINTS>" << endl;
222  ptsFile << "</NEKTAR>" << endl;
223 
224  ptsFile.close();
225 
226  // this is what the above cpart would read if tinyxml
227  // supported line breaks
228  /*
229  // Create the file (partition)
230  TiXmlDocument doc;
231  TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "utf-8", "");
232  doc.LinkEndChild(decl);
233 
234  TiXmlElement *root = new TiXmlElement("NEKTAR");
235  doc.LinkEndChild(root);
236 
237  TiXmlElement *pointsTag = new TiXmlElement("POINTS");
238  root->LinkEndChild(pointsTag);
239 
240  pointsTag->SetAttribute("DIM", ptsField->GetDim());
241 
242  string fn = boost::algorithm::join(ptsField->GetFieldNames(), ",");
243  pointsTag->SetAttribute("FIELDS", fn);
244 
245  Array <OneD, Array <OneD, NekDouble > > pts;
246  ptsField->GetPts(pts);
247  ostringstream os;
248  for (int i = 0; i < np; ++i)
249  {
250  os << pts[0][i];
251  for (int j = 1; j < nTotvars; ++j)
252  {
253  os << " " << pts[j][i];
254  }
255  os << " ";
256  }
257 
258  pointsTag->LinkEndChild(new TiXmlText(os.str()));
259 
260  doc.SaveFile(filename);
261  */
262 }
std::string SetUpOutput(const std::string outname)
Definition: FieldIO.cpp:1189
void SetUpFieldMetaData(const std::string outname)
Definition: PtsIO.cpp:344