Nektar++
InputSwan.cpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // File: InputSwan.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: Swansea session converter.
32 //
33 ////////////////////////////////////////////////////////////////////////////////
34 
36 #include "InputSwan.h"
37 
38 using namespace std;
39 using namespace Nektar::NekMeshUtils;
40 
41 namespace Nektar
42 {
43 namespace Utilities
44 {
45 
46 ModuleKey InputSwan::className = GetModuleFactory().RegisterCreatorFunction(
47  ModuleKey(eInputModule, "plt"),
48  InputSwan::create,
49  "Reads Swansea plt format for third-order tetrahedra.");
50 
51 InputSwan::InputSwan(MeshSharedPtr m) : InputModule(m)
52 {
53 }
54 
56 {
57 }
58 
60 {
61  // Open the file stream.
62  OpenStream();
63 
64  vector<vector<NodeSharedPtr> > elementList;
65  vector<int> tmp, tets;
66  vector<double> pts;
67 
68  if (m_mesh->m_verbose)
69  {
70  cout << "InputSwan: Start reading file..." << endl;
71  }
72 
73  m_mesh->m_expDim = 3;
74  m_mesh->m_spaceDim = 3;
75 
76  // First read in header; 4 integers containing number of tets,
77  // number of points, nas2 (unknown) and order of the grid
78  // (i.e. GridOrder = 3 => cubic mesh).
79  tmp.resize(6);
80  m_mshFile.read(reinterpret_cast<char *>(&tmp[0]),
81  static_cast<int>(6 * sizeof(int)));
82 
83  if (tmp[0] != tmp[5] || tmp[0] != 4 * sizeof(int))
84  {
85  cout << "Header data broken" << endl;
86  m_mshFile.reset();
87  return;
88  }
89 
90  int NB_Tet = tmp[1];
91  int NB_Points = tmp[2];
92  int GridOrder = tmp[4];
93  int ND = (GridOrder + 1) * (GridOrder + 2) * (GridOrder + 3) / 6;
94 
95  cout << "NB_Tet = " << NB_Tet << endl;
96  cout << "NB_Points = " << NB_Points << endl;
97  cout << "GridOrder = " << GridOrder << endl;
98  cout << "ND = " << ND << endl;
99 
100  // Now read in list of tetrahedra. Each tet has ND points, and
101  // data are ordered with memory traversed fastest with point
102  // number.
103  tets.resize(ND * NB_Tet + 2);
104  m_mshFile.read(reinterpret_cast<char *>(&tets[0]),
105  static_cast<int>((ND * NB_Tet + 2) * sizeof(int)));
106 
107  if (tets[0] != tets[ND * NB_Tet + 1] ||
108  tets[0] != ND * NB_Tet * sizeof(int))
109  {
110  cout << "ERROR [InputSwan]: Tetrahedron data broken." << endl;
111  m_mshFile.reset();
112  return;
113  }
114 
115  // Finally, read point data: NB_Points tuples (x,y,z).
116  tmp.resize(2);
117  pts.resize(3 * NB_Points);
118  m_mshFile.read(reinterpret_cast<char *>(&tmp[0]),
119  static_cast<int>(sizeof(int)));
120  m_mshFile.read(reinterpret_cast<char *>(&pts[0]),
121  static_cast<int>(3 * NB_Points * sizeof(double)));
122  m_mshFile.read(reinterpret_cast<char *>(&tmp[1]),
123  static_cast<int>(sizeof(int)));
124 
125  if (tmp[0] != tmp[1] || tmp[0] != 3 * NB_Points * sizeof(double))
126  {
127  cout << "ERROR [InputSwan]: Point data broken." << endl;
128  m_mshFile.reset();
129  return;
130  }
131 
132  int vid = 0, i, j;
134 
135  // Read in list of vertices.
136  for (i = 0; i < NB_Points; ++i)
137  {
138  double x = pts[i];
139  double y = pts[1 * NB_Points + i];
140  double z = pts[2 * NB_Points + i];
141  m_mesh->m_node.push_back(
142  std::shared_ptr<Node>(new Node(vid, x, y, z)));
143  vid++;
144  }
145 
146  // Iterate over list of tetrahedra: for each, create nodes. At the
147  // moment discard high order data and create linear mesh.
148  for (i = 0; i < NB_Tet; ++i)
149  {
150  vector<NodeSharedPtr> nodeList;
151  for (j = 0; j < 20; ++j)
152  {
153  int vid = tets[j * NB_Tet + i + 1];
154  nodeList.push_back(m_mesh->m_node[vid - 1]);
155  }
156 
157  vector<int> tags;
158  tags.push_back(0); // composite
159  tags.push_back(elType); // element type
160 
161  ElmtConfig conf(elType, 3, true, true);
162  ElementSharedPtr E =
163  GetElementFactory().CreateInstance(elType, conf, nodeList, tags);
164  m_mesh->m_element[3].push_back(E);
165  }
166 
167  // Attempt to read in composites. Need to determine number of
168  // triangles from data.
169  tmp.resize(2);
170  m_mshFile.read(reinterpret_cast<char *>(&tmp[0]),
171  static_cast<int>(sizeof(int)));
172  int n_tri = tmp[0] / sizeof(int) / 5;
173  tets.resize(n_tri * 5);
174  m_mshFile.read(reinterpret_cast<char *>(&tets[0]),
175  static_cast<int>(tmp[0]));
176  m_mshFile.read(reinterpret_cast<char *>(&tmp[1]),
177  static_cast<int>(sizeof(int)));
178 
179  if (tmp[0] != tmp[1])
180  {
181  cout << "ERROR [InputSwan]: Surface data broken." << endl;
182  m_mshFile.reset();
183  return;
184  }
185 
186  elType = LibUtilities::eTriangle;
187 
188  // Process list of triangles forming surfaces.
189  for (i = 0; i < n_tri; ++i)
190  {
191  vector<NodeSharedPtr> nodeList;
192 
193  for (j = 0; j < 3; ++j)
194  {
195  nodeList.push_back(m_mesh->m_node[tets[i + j * n_tri] - 1]);
196  }
197 
198  vector<int> tags;
199  tags.push_back(1); // composite
200  tags.push_back(elType); // element type
201 
202  ElmtConfig conf(elType, 1, false, false);
203  ElementSharedPtr E =
204  GetElementFactory().CreateInstance(elType, conf, nodeList, tags);
205  m_mesh->m_element[2].push_back(E);
206  }
207 
208  m_mshFile.reset();
209 
210  // Process the rest of the mesh.
211  ProcessVertices();
212  ProcessEdges();
213  ProcessFaces();
214  ProcessElements();
216 }
217 }
218 }
virtual void Process()
Populate and validate required data structures.
Definition: InputSwan.cpp:59
Basic information about an element.
Definition: ElementConfig.h:49
io::filtering_istream m_mshFile
Input stream.
STL namespace.
std::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:156
ElementFactory & GetElementFactory()
Definition: Element.cpp:44
NEKMESHUTILS_EXPORT void OpenStream()
Open a file for input.
tBaseSharedPtr CreateInstance(tKey idKey, tParam... args)
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:144
std::pair< ModuleType, std::string > ModuleKey
virtual NEKMESHUTILS_EXPORT void ProcessFaces(bool ReprocessFaces=true)
Extract element faces.
std::shared_ptr< Element > ElementSharedPtr
Definition: Edge.h:49
virtual NEKMESHUTILS_EXPORT void ProcessElements()
Generate element IDs.
Abstract base class for input modules.
virtual NEKMESHUTILS_EXPORT void ProcessVertices()
Extract element vertices.
virtual NEKMESHUTILS_EXPORT void ProcessEdges(bool ReprocessEdges=true)
Extract element edges.
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, std::string pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:199
std::pair< ModuleType, std::string > ModuleKey
virtual NEKMESHUTILS_EXPORT void ProcessComposites()
Generate composites.
ModuleFactory & GetModuleFactory()