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