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 // License for the specific language governing rights and limitations under
15 // Permission is hereby granted, free of charge, to any person obtaining a
16 // copy of this software and associated documentation files (the "Software"),
17 // to deal in the Software without restriction, including without limitation
18 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 // and/or sell copies of the Software, and to permit persons to whom the
20 // Software is furnished to do so, subject to the following conditions:
21 //
22 // The above copyright notice and this permission notice shall be included
23 // in all copies or substantial portions of the Software.
24 //
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
26 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 // DEALINGS IN THE SOFTWARE.
32 //
33 // Description: I/O routines relating to Pts data
34 //
35 ////////////////////////////////////////////////////////////////////////////////
36 
38 
39 namespace Nektar
40 {
41 namespace LibUtilities
42 {
43 
44 void Import(const string &inFile, PtsFieldSharedPtr &ptsField)
45 {
46  PtsIO p;
47  p.Import(inFile, ptsField);
48 }
49 
50 
51 void Write(const string &outFile, const PtsFieldSharedPtr &ptsField)
52 {
53  PtsIO p;
54  p.Write(outFile, ptsField);
55 }
56 
57 
58 /**
59  * @brief Import a pts field from file
60  *
61  * @param inFile filename of the file to read
62  * @param ptsField the resulting pts field.
63  */
64 void PtsIO::Import(const string &inFile, PtsFieldSharedPtr &ptsField)
65 {
66  TiXmlDocument docInput;
67  ASSERTL0(docInput.LoadFile(inFile), "Unable to open file '" + inFile + "'.");
68 
69  TiXmlElement *nektar = docInput.FirstChildElement("NEKTAR");
70  TiXmlElement *points = nektar->FirstChildElement("POINTS");
71  int dim;
72  int err = points->QueryIntAttribute("DIM", &dim);
73 
74  ASSERTL0(err == TIXML_SUCCESS, "Unable to read attribute DIM.");
75 
76  std::string fields = points->Attribute("FIELDS");
77 
78  vector<string> fieldNames;
79  if (!fields.empty())
80  {
82  fields.c_str(), fieldNames);
83  ASSERTL0(valid,
84  "Unable to process list of field variable in FIELDS attribute: " + fields);
85  }
86 
87  int nfields = fieldNames.size();
88  int totvars = dim + nfields;
89 
90  TiXmlNode *pointsBody = points->FirstChild();
91 
92  std::istringstream pointsDataStrm(pointsBody->ToText()->Value());
93 
94  vector<NekDouble> ptsSerial;
96 
97  try
98  {
99  NekDouble ptsStream;
100  while (!pointsDataStrm.fail())
101  {
102  pointsDataStrm >> ptsStream;
103 
104  ptsSerial.push_back(ptsStream);
105  }
106  }
107  catch (...)
108  {
109  ASSERTL0(false, "Unable to read Points data.");
110  }
111 
112  int npts = ptsSerial.size() / totvars;
113 
114  for (int i = 0; i < totvars; ++i)
115  {
116  pts[i] = Array<OneD, NekDouble>(npts);
117  }
118 
119  for (int i = 0; i < npts; ++i)
120  {
121  for (int j = 0; j < totvars; ++j)
122  {
123  pts[j][i] = ptsSerial[i * totvars + j];
124  }
125  }
126 
127  ptsField = MemoryManager<PtsField>::AllocateSharedPtr(dim, fieldNames, pts);
128 }
129 
130 
131 /**
132  * @brief Save a pts field to a file
133  *
134  * @param inFile filename of the file
135  * @param ptsField the pts field
136  */
137 void PtsIO::Write(const string &outFile, const PtsFieldSharedPtr &ptsField)
138 {
139 
140  ASSERTL0(false, "Not implemented yet");
141 }
142 
143 
144 }
145 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:161
static bool GenerateOrderedStringVector(const char *const str, std::vector< std::string > &vec)
Definition: ParseUtils.hpp:142
void Write(const string &outFile, const LibUtilities::PtsFieldSharedPtr &ptsField)
Save a pts field to a file.
Definition: PtsIO.cpp:137
static boost::shared_ptr< DataType > AllocateSharedPtr()
Allocate a shared pointer from the memory pool.
boost::shared_ptr< PtsField > PtsFieldSharedPtr
Definition: PtsField.h:248
void Import(const std::string &infilename, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, FieldMetaDataMap &fieldinfomap, const Array< OneD, int > ElementiDs)
Imports an FLD file.
Definition: FieldIO.cpp:106
void Import(const string &inFile, LibUtilities::PtsFieldSharedPtr &ptsField)
Import a pts field from file.
Definition: PtsIO.cpp:64
static std::string npts
Definition: InputFld.cpp:43
double NekDouble
void Write(const std::string &outFile, std::vector< FieldDefinitionsSharedPtr > &fielddefs, std::vector< std::vector< NekDouble > > &fielddata, const FieldMetaDataMap &fieldinfomap)
Write a field file in serial only.
Definition: FieldIO.cpp:72