Nektar++
InputDat.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: InputDat.cpp
4 //
5 // For more information, please see: http://www.nektar.info/
6 //
7 // The MIT License
8 //
9 // Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10 // Department of Aeronautics, Imperial College London (UK), and Scientific
11 // Computing and Imaging Institute, University of Utah (USA).
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining a
14 // copy of this software and associated documentation files (the "Software"),
15 // to deal in the Software without restriction, including without limitation
16 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 // and/or sell copies of the Software, and to permit persons to whom the
18 // Software is furnished to do so, subject to the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be included
21 // in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 // DEALINGS IN THE SOFTWARE.
30 //
31 // Description: Read tecplot dat for isontours in 2D triangular FE block format
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
35 #include <iostream>
36 #include <string>
37 using namespace std;
38 
39 #include <boost/core/ignore_unused.hpp>
40 
44 
45 #include <tinyxml.h>
46 
47 #include "InputDat.h"
48 
49 namespace Nektar
50 {
51 namespace FieldUtils
52 {
53 
54 ModuleKey InputDat::m_className[1] = {
56  ModuleKey(eInputModule, "dat"),
57  InputDat::create,
58  "Reads Tecplot dat file for FE block triangular format.")
59 };
60 
61 /**
62  * @brief Set up InputDat object.
63  *
64  */
65 InputDat::InputDat(FieldSharedPtr f) : InputModule(f)
66 {
67  m_allowedFiles.insert("dat");
68 }
69 
70 /**
71  *
72  */
74 {
75 }
76 
77 /**
78  *
79  */
80 void InputDat::Process(po::variables_map &vm)
81 {
82  boost::ignore_unused(vm);
83 
84  string line;
85  std::ifstream datFile;
86 
87  // Open the file stream.
88  string fname = m_f->m_inputfiles["dat"][0];
89 
90  datFile.open(fname.c_str());
91  if (!datFile.good())
92  {
93  cerr << "Error opening file: " << fname << endl;
94  abort();
95  }
96 
97  // read variables
98  // currently assume there are x y and z coordinates
99  int dim = 3;
100  vector<string> fieldNames;
101  while (!datFile.eof())
102  {
103  getline(datFile, line);
104  string linetest = line;
105  boost::to_upper(linetest);
106  if (linetest.find("VARIABLES") != string::npos)
107  {
108  std::size_t pos = line.find('=');
109  pos++;
110 
111  // note this expects a comma separated list but
112  // does not work for white space separated lists!
113  bool valid = ParseUtils::GenerateVector(
114  line.substr(pos), fieldNames);
115  ASSERTL0(valid, "Unable to process list of field variable in "
116  " VARIABLES list: " +
117  line.substr(pos));
118 
119  // remove coordinates from fieldNames
120  fieldNames.erase(fieldNames.begin(), fieldNames.begin() + dim);
121 
122  break;
123  }
124  }
125 
126  // set up basic parameters
127  int nfields = fieldNames.size();
128  int totvars = dim + nfields;
129  Array<OneD, Array<OneD, NekDouble> > pts(totvars);
130  vector<Array<OneD, int> > ptsConn;
131 
132  // read zones
133  while (!datFile.eof())
134  {
135  getline(datFile, line);
136  string linetest = line;
137  boost::to_upper(linetest);
138  if ((linetest.find("ZONE") != string::npos))
139  {
140  ReadTecplotFEBlockZone(datFile, line, pts, ptsConn);
141  }
142  }
143 
144  datFile.close();
145 
147  dim, fieldNames, pts);
148  m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsTriBlock);
149  m_f->m_fieldPts->SetConnectivity(ptsConn);
150 
151  // save field names
152  m_f->m_variables = fieldNames;
153 }
154 
155 /**
156  *
157  */
158 void InputDat::ReadTecplotFEBlockZone(std::ifstream &datFile,
159  string &line,
161  vector<Array<OneD, int> > &ptsConn)
162 {
163  ASSERTL0(line.find("FEBlock") != string::npos,
164  "Routine only set up for FEBLock format");
165  ASSERTL0(line.find("ET") != string::npos, "Routine only set up TRIANLES");
166 
167  // read the number of nodes
168 
169  stringstream s;
170  string tag;
171  int start, end;
172 
173  s.clear();
174  s.str(line);
175  tag = s.str();
176 
177  // read the number of vertices
178  start = tag.find("N=");
179  end = tag.find_first_of(',', start);
180  int nvert = atoi(tag.substr(start + 2, end).c_str());
181 
182  // read the number of elements
183  start = tag.find("E=");
184  end = tag.find_first_of(',', start);
185  int nelmt = atoi(tag.substr(start + 2, end).c_str());
186 
187  // set-up or extend m_pts array;
188  int norigpts = pts[0].num_elements();
189  int totfields = pts.num_elements();
190  Array<OneD, Array<OneD, NekDouble> > origpts(totfields);
191  for (int i = 0; i < totfields; ++i)
192  {
193  origpts[i] = pts[i];
194  pts[i] = Array<OneD, NekDouble>(norigpts + nvert);
195  }
196 
197  NekDouble value;
198  for (int n = 0; n < totfields; ++n)
199  {
200 
201  for (int i = 0; i < norigpts; ++i)
202  {
203  pts[n][i] = origpts[n][i];
204  }
205  for (int i = 0; i < nvert; ++i)
206  {
207  datFile >> value;
208  pts[n][norigpts + i] = value;
209  }
210  }
211 
212  // read connectivity and add to list
213  int intvalue;
214  Array<OneD, int> conn(3 * nelmt);
215  for (int i = 0; i < 3 * nelmt; ++i)
216  {
217  datFile >> intvalue;
218  intvalue -= 1; // decrement intvalue by 1 for c array convention
219  conn[i] = norigpts + intvalue;
220  }
221  ptsConn.push_back(conn);
222 
223  getline(datFile, line);
224 }
225 }
226 }
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:216
virtual void Process(po::variables_map &vm)
Definition: InputDat.cpp:80
STL namespace.
void ReadTecplotFEBlockZone(std::ifstream &datFile, string &line, Array< OneD, Array< OneD, NekDouble > > &pts, vector< Array< OneD, int > > &ptsConn)
Definition: InputDat.cpp:158
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:762
Abstract base class for input modules.
std::pair< ModuleType, std::string > ModuleKey
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
std::set< std::string > m_allowedFiles
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
ModuleFactory & GetModuleFactory()
FieldSharedPtr m_f
Field object.