Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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)
 
virtual ~PtsIO ()
 
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, const bool backup=false)
 Save a pts field to a file. More...
 
void ImportFieldData (TiXmlDocument docInput, 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

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
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...
 
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 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...
 
- 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 64 of file PtsIO.h.

Constructor & Destructor Documentation

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
virtual Nektar::LibUtilities::PtsIO::~PtsIO ( )
inlinevirtual

Definition at line 70 of file PtsIO.h.

71  {
72  }

Member Function Documentation

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

Helper function that determines default file extension.

Reimplemented from Nektar::LibUtilities::FieldIO.

Definition at line 89 of file PtsIO.h.

Referenced by Import(), and SetUpFieldMetaData().

90  {
91  return "pts";
92  };
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 69 of file PtsIO.cpp.

References ASSERTL0, 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::EquationSystem::LoadPts().

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  TiXmlDocument doc1(fname);
109  bool loadOkay1 = doc1.LoadFile();
110 
111  std::stringstream errstr;
112  errstr << "Unable to load file: " << fname << std::endl;
113  errstr << "Reason: " << doc1.ErrorDesc() << std::endl;
114  errstr << "Position: Line " << doc1.ErrorRow() << ", Column "
115  << doc1.ErrorCol() << std::endl;
116  ASSERTL0(loadOkay1, errstr.str());
117 
118  if (i == 0)
119  {
120  ImportFieldData(doc1, ptsField);
121  }
122  else
123  {
125  ImportFieldData(doc1, newPtsField);
126  Array<OneD, Array<OneD, NekDouble> > pts;
127  newPtsField->GetPts(pts);
128  ptsField->AddPoints(pts);
129  }
130 
131  }
132  }
133  else
134  {
135  TiXmlDocument doc(infile);
136  bool loadOkay = doc.LoadFile();
137 
138  std::stringstream errstr;
139  errstr << "Unable to load file: " << infile << std::endl;
140  errstr << "Reason: " << doc.ErrorDesc() << std::endl;
141  errstr << "Position: Line " << doc.ErrorRow() << ", Column "
142  << doc.ErrorCol() << std::endl;
143  ASSERTL0(loadOkay, errstr.str());
144 
145  ImportFieldData(doc, ptsField);
146  }
147 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
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
boost::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:178
void ImportFieldData(TiXmlDocument docInput, PtsFieldSharedPtr &ptsField)
Definition: PtsIO.cpp:232
virtual std::string GetFileEnding() const
Helper function that determines default file extension.
Definition: PtsIO.h:89
DataSourceSharedPtr ImportFieldMetaData(const std::string &filename, FieldMetaDataMap &fieldmetadatamap)
Import the metadata from a field file.
Definition: FieldIO.h:358
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
void Nektar::LibUtilities::PtsIO::ImportFieldData ( TiXmlDocument  docInput,
PtsFieldSharedPtr ptsField 
)

Definition at line 232 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().

233 {
234  TiXmlElement *nektar = docInput.FirstChildElement("NEKTAR");
235  TiXmlElement *points = nektar->FirstChildElement("POINTS");
236  int dim;
237  int err = points->QueryIntAttribute("DIM", &dim);
238 
239  ASSERTL0(err == TIXML_SUCCESS, "Unable to read attribute DIM.");
240 
241  std::string fields = points->Attribute("FIELDS");
242 
243  vector<string> fieldNames;
244  if (!fields.empty())
245  {
246  bool valid =
247  ParseUtils::GenerateOrderedStringVector(fields.c_str(), fieldNames);
248  ASSERTL0(
249  valid,
250  "Unable to process list of field variable in FIELDS attribute: " +
251  fields);
252  }
253 
254  map<PtsInfo,int> ptsInfo = NullPtsInfoMap;
255 
256  const char *ptinfo = points->Attribute("PTSINFO");
257  if(ptinfo&&boost::iequals(ptinfo,"EquiSpaced"))
258  {
259  ptsInfo[eIsEquiSpacedData] = 1;
260  }
261 
262  int np;
263  err = points->QueryIntAttribute("PTSPERELMTEDGE",&np);
264  if(err == TIXML_SUCCESS)
265  {
266  ptsInfo[ePtsPerElmtEdge] = np;
267  }
268 
269  int nfields = fieldNames.size();
270  int totvars = dim + nfields;
271 
272  TiXmlNode *pointsBody = points->FirstChild();
273 
274  std::istringstream pointsDataStrm(pointsBody->ToText()->Value());
275 
276  vector<NekDouble> ptsSerial;
277  Array<OneD, Array<OneD, NekDouble> > pts(totvars);
278 
279  try
280  {
281  NekDouble ptsStream;
282  while (!pointsDataStrm.fail())
283  {
284  pointsDataStrm >> ptsStream;
285 
286  ptsSerial.push_back(ptsStream);
287  }
288  }
289  catch (...)
290  {
291  ASSERTL0(false, "Unable to read Points data.");
292  }
293 
294  int npts = ptsSerial.size() / totvars;
295 
296  for (int i = 0; i < totvars; ++i)
297  {
298  pts[i] = Array<OneD, NekDouble>(npts);
299  }
300 
301  for (int i = 0; i < npts; ++i)
302  {
303  for (int j = 0; j < totvars; ++j)
304  {
305  pts[j][i] = ptsSerial[i * totvars + j];
306  }
307  }
308 
309  ptsField = MemoryManager<PtsField>::AllocateSharedPtr(dim, fieldNames, pts, ptsInfo);
310 }
static std::map< PtsInfo, int > NullPtsInfoMap
Definition: PtsField.h:71
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
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
void Nektar::LibUtilities::PtsIO::SetUpFieldMetaData ( const std::string  outname)
protected

Definition at line 312 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 Write().

313 {
314  ASSERTL0(!outname.empty(), "Empty path given to SetUpFieldMetaData()");
315 
316  int nprocs = m_comm->GetSize();
317  int rank = m_comm->GetRank();
318 
319  fs::path specPath(outname);
320 
321  // Collate per-process element lists on root process to generate
322  // the info file.
323  if (rank == 0)
324  {
325  // Set up output names
326  std::vector<std::string> filenames;
327  std::vector<std::vector<unsigned int> > ElementIDs;
328  for (int i = 0; i < nprocs; ++i)
329  {
330  boost::format pad("P%1$07d.%2$s");
331  pad % i % GetFileEnding();
332  filenames.push_back(pad.str());
333 
334  std::vector<unsigned int> tmp;
335  tmp.push_back(0);
336  ElementIDs.push_back(tmp);
337  }
338 
339  // Write the Info.xml file
340  string infofile =
341  LibUtilities::PortablePath(specPath / fs::path("Info.xml"));
342 
343  cout << "Writing: " << specPath << endl;
344 
345  const FieldMetaDataMap fieldmetadatamap;
346  WriteMultiFldFileIDs(infofile, filenames, ElementIDs, fieldmetadatamap);
347  }
348 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:198
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:54
virtual std::string GetFileEnding() const
Helper function that determines default file extension.
Definition: PtsIO.h:89
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:265
void Nektar::LibUtilities::PtsIO::Write ( const 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 155 of file PtsIO.cpp.

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

Referenced by Nektar::FieldUtils::OutputPts::Process().

158 {
159  int nTotvars = ptsField->GetNFields() + ptsField->GetDim();
160  int np = ptsField->GetNpoints();
161 
162  std::string filename = SetUpOutput(outFile, true, backup);
163  SetUpFieldMetaData(outFile);
164 
165  // until tinyxml gains support for line break, write the xml manually
166  std::ofstream ptsFile;
167  ptsFile.open(filename.c_str());
168 
169  ptsFile << "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" << endl;
170  ptsFile << "<NEKTAR>" << endl;
171  ptsFile << " <POINTS ";
172  ptsFile << "DIM=\"" << ptsField->GetDim() << "\" ";
173  string fn = boost::algorithm::join(ptsField->GetFieldNames(), ",");
174  ptsFile << "FIELDS=\"" << fn << "\" ";
175  ptsFile << ">" << endl;
176 
177  Array<OneD, Array<OneD, NekDouble> > pts;
178  ptsField->GetPts(pts);
179  for (int i = 0; i < np; ++i)
180  {
181  ptsFile << " ";
182  ptsFile << pts[0][i];
183  for (int j = 1; j < nTotvars; ++j)
184  {
185  ptsFile << " " << pts[j][i];
186  }
187  ptsFile << endl;
188  }
189  ptsFile << " </POINTS>" << endl;
190  ptsFile << "</NEKTAR>" << endl;
191 
192  ptsFile.close();
193 
194  // this is what the above cpart would read if tinyxml
195  // supported line breaks
196  /*
197  // Create the file (partition)
198  TiXmlDocument doc;
199  TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "utf-8", "");
200  doc.LinkEndChild(decl);
201 
202  TiXmlElement *root = new TiXmlElement("NEKTAR");
203  doc.LinkEndChild(root);
204 
205  TiXmlElement *pointsTag = new TiXmlElement("POINTS");
206  root->LinkEndChild(pointsTag);
207 
208  pointsTag->SetAttribute("DIM", ptsField->GetDim());
209 
210  string fn = boost::algorithm::join(ptsField->GetFieldNames(), ",");
211  pointsTag->SetAttribute("FIELDS", fn);
212 
213  Array <OneD, Array <OneD, NekDouble > > pts;
214  ptsField->GetPts(pts);
215  ostringstream os;
216  for (int i = 0; i < np; ++i)
217  {
218  os << pts[0][i];
219  for (int j = 1; j < nTotvars; ++j)
220  {
221  os << " " << pts[j][i];
222  }
223  os << " ";
224  }
225 
226  pointsTag->LinkEndChild(new TiXmlText(os.str()));
227 
228  doc.SaveFile(filename);
229  */
230 }
std::string SetUpOutput(const std::string outname, bool perRank, bool backup=false)
Set up the filesystem ready for output.
Definition: FieldIO.cpp:400
void SetUpFieldMetaData(const std::string outname)
Definition: PtsIO.cpp:312