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"), InputDat::create,
57  "Reads Tecplot dat file for FE block triangular format.")};
58 
59 /**
60  * @brief Set up InputDat object.
61  *
62  */
63 InputDat::InputDat(FieldSharedPtr f) : InputModule(f)
64 {
65  m_allowedFiles.insert("dat");
66 }
67 
68 /**
69  *
70  */
72 {
73 }
74 
75 /**
76  *
77  */
78 void InputDat::v_Process(po::variables_map &vm)
79 {
80  boost::ignore_unused(vm);
81 
82  string line;
83  std::ifstream datFile;
84 
85  // Open the file stream.
86  string fname = m_f->m_inputfiles["dat"][0];
87 
88  datFile.open(fname.c_str());
89  if (!datFile.good())
90  {
91  cerr << "Error opening file: " << fname << endl;
92  abort();
93  }
94 
95  // read variables
96  // currently assume there are x y and z coordinates
97  int dim = 3;
98  vector<string> fieldNames;
99  while (!datFile.eof())
100  {
101  getline(datFile, line);
102  string linetest = line;
103  boost::to_upper(linetest);
104  if (linetest.find("VARIABLES") != string::npos)
105  {
106  std::size_t pos = line.find('=');
107  pos++;
108 
109  // note this expects a comma separated list but
110  // does not work for white space separated lists!
111  bool valid =
112  ParseUtils::GenerateVector(line.substr(pos), fieldNames);
113  ASSERTL0(valid, "Unable to process list of field variable in "
114  " VARIABLES list: " +
115  line.substr(pos));
116 
117  // remove coordinates from fieldNames
118  fieldNames.erase(fieldNames.begin(), fieldNames.begin() + dim);
119 
120  break;
121  }
122  }
123 
124  // set up basic parameters
125  int nfields = fieldNames.size();
126  int totvars = dim + nfields;
128  vector<Array<OneD, int>> ptsConn;
129 
130  // read zones
131  while (!datFile.eof())
132  {
133  getline(datFile, line);
134  string linetest = line;
135  boost::to_upper(linetest);
136  if ((linetest.find("ZONE") != string::npos))
137  {
138  ReadTecplotFEBlockZone(datFile, line, pts, ptsConn);
139  }
140  }
141 
142  datFile.close();
143 
145  dim, fieldNames, pts);
146  m_f->m_fieldPts->SetPtsType(LibUtilities::ePtsTriBlock);
147  m_f->m_fieldPts->SetConnectivity(ptsConn);
148 
149  // save field names
150  m_f->m_variables = fieldNames;
151 }
152 
153 /**
154  *
155  */
156 void InputDat::ReadTecplotFEBlockZone(std::ifstream &datFile, string &line,
158  vector<Array<OneD, int>> &ptsConn)
159 {
160  ASSERTL0(line.find("FEBlock") != string::npos,
161  "Routine only set up for FEBLock format");
162  ASSERTL0(line.find("ET") != string::npos, "Routine only set up TRIANLES");
163 
164  // read the number of nodes
165 
166  stringstream s;
167  string tag;
168  int start, end;
169 
170  s.clear();
171  s.str(line);
172  tag = s.str();
173 
174  // read the number of vertices
175  start = tag.find("N=");
176  end = tag.find_first_of(',', start);
177  int nvert = atoi(tag.substr(start + 2, end).c_str());
178 
179  // read the number of elements
180  start = tag.find("E=");
181  end = tag.find_first_of(',', start);
182  int nelmt = atoi(tag.substr(start + 2, end).c_str());
183 
184  // set-up or extend m_pts array;
185  int norigpts = pts[0].size();
186  int totfields = pts.size();
187  Array<OneD, Array<OneD, NekDouble>> origpts(totfields);
188  for (int i = 0; i < totfields; ++i)
189  {
190  origpts[i] = pts[i];
191  pts[i] = Array<OneD, NekDouble>(norigpts + nvert);
192  }
193 
194  NekDouble value;
195  for (int n = 0; n < totfields; ++n)
196  {
197 
198  for (int i = 0; i < norigpts; ++i)
199  {
200  pts[n][i] = origpts[n][i];
201  }
202  for (int i = 0; i < nvert; ++i)
203  {
204  datFile >> value;
205  pts[n][norigpts + i] = value;
206  }
207  }
208 
209  // read connectivity and add to list
210  int intvalue;
211  Array<OneD, int> conn(3 * nelmt);
212  for (int i = 0; i < 3 * nelmt; ++i)
213  {
214  datFile >> intvalue;
215  intvalue -= 1; // decrement intvalue by 1 for c array convention
216  conn[i] = norigpts + intvalue;
217  }
218  ptsConn.push_back(conn);
219 
220  getline(datFile, line);
221 }
222 } // namespace FieldUtils
223 } // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:215
virtual void v_Process(po::variables_map &vm) override
Definition: InputDat.cpp:78
void ReadTecplotFEBlockZone(std::ifstream &datFile, std::string &line, Array< OneD, Array< OneD, NekDouble >> &pts, std::vector< Array< OneD, int >> &ptsConn)
Definition: InputDat.cpp:156
Abstract base class for input modules.
Definition: Module.h:278
std::set< std::string > m_allowedFiles
List of allowed file formats.
Definition: Module.h:265
FieldSharedPtr m_f
Field object.
Definition: Module.h:234
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:198
static std::shared_ptr< DataType > AllocateSharedPtr(const Args &...args)
Allocate a shared pointer from the memory pool.
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:131
std::shared_ptr< Field > FieldSharedPtr
Definition: Field.hpp:991
std::pair< ModuleType, std::string > ModuleKey
Definition: Module.h:317
ModuleFactory & GetModuleFactory()
Definition: Module.cpp:49
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
double NekDouble