Nektar++
PtsIO.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: PtsIO.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2014 Kilian Lackhove
10 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
11 // Department of Aeronautics, Imperial College London (UK), and Scientific
12 // Computing and Imaging Institute, University of Utah (USA).
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: I/O routines relating to Pts data
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
36 #include <boost/algorithm/string.hpp>
38 
39 #include <fstream>
40 
41 #include <boost/format.hpp>
42 
43 #ifdef NEKTAR_USE_MPI
44 #include <mpi.h>
45 #endif
46 
49 
50 using namespace std;
51 
52 namespace Nektar
53 {
54 namespace LibUtilities
55 {
56 
57 
58 PtsIO::PtsIO(CommSharedPtr pComm, bool sharedFilesystem)
59  : FieldIOXml(pComm, sharedFilesystem)
60 {
61 }
62 
63 /**
64  * @brief Import a pts field from file
65  *
66  * @param inFile filename of the file to read
67  * @param ptsField the resulting pts field.
68  */
69 void PtsIO::Import(const string &inFile,
70  PtsFieldSharedPtr &ptsField,
71  FieldMetaDataMap &fieldmetadatamap)
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);
117  newPtsField->GetPts(pts);
118  ptsField->AddPoints(pts);
119  }
120 
121  }
122  }
123  else
124  {
125  ImportFieldData(infile, ptsField);
126  }
127 }
128 
129 /**
130  * @brief Save a pts field to a file
131  *
132  * @param outFile filename of the file
133  * @param ptsField the pts field
134  */
135 void PtsIO::Write(const string &outFile,
137  const bool backup)
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 
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 }
211 void PtsIO::ImportFieldData(const string inFile, PtsFieldSharedPtr &ptsField)
212 {
213  v_ImportFieldData(inFile, ptsField);
214 }
215 
216 
217 void PtsIO::v_ImportFieldData(const string inFile, PtsFieldSharedPtr &ptsField)
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 }
306 
307 void PtsIO::SetUpFieldMetaData(const string outname)
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 }
344 }
345 }
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
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
std::shared_ptr< Comm > CommSharedPtr
Pointer to a Communicator object.
Definition: Comm.h:53
void ImportFieldData(const std::string inFile, PtsFieldSharedPtr &ptsField)
Definition: PtsIO.cpp:211
STL namespace.
std::map< std::string, std::string > FieldMetaDataMap
Definition: FieldIO.h:52
void Import(const std::string &inFile, PtsFieldSharedPtr &ptsField, FieldMetaDataMap &fieldmetadatamap=NullFieldMetaDataMap)
Import a pts field from file.
Definition: PtsIO.cpp:69
void Write(const std::string &outFile, const PtsFieldSharedPtr &ptsField, const bool backup=false)
Save a pts field to a file.
Definition: PtsIO.cpp:135
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
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
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
double NekDouble
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
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
std::string SetUpOutput(const std::string outname, bool perRank, bool backup=false)
Set up the filesystem ready for output.
Definition: FieldIO.cpp:410
LibUtilities::CommSharedPtr m_comm
Communicator to use when writing parallel format.
Definition: FieldIO.h:266
virtual void v_ImportFieldData(const std::string inFile, PtsFieldSharedPtr &ptsField)
Definition: PtsIO.cpp:217
void SetUpFieldMetaData(const std::string outname)
Definition: PtsIO.cpp:307