Nektar++
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 // License for the specific language governing rights and limitations under
14 // Permission is hereby granted, free of charge, to any person obtaining a
15 // copy of this software and associated documentation files (the "Software"),
16 // to deal in the Software without restriction, including without limitation
17 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 // and/or sell copies of the Software, and to permit persons to whom the
19 // Software is furnished to do so, subject to the following conditions:
20 //
21 // The above copyright notice and this permission notice shall be included
22 // in all copies or substantial portions of the Software.
23 //
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30 // DEALINGS IN THE SOFTWARE.
31 //
32 // Description: Swansea session converter.
33 //
34 ////////////////////////////////////////////////////////////////////////////////
35 
37 #include "InputSwan.h"
38 
39 using namespace std;
40 using namespace Nektar::NekMeshUtils;
41 
42 namespace Nektar
43 {
44 namespace Utilities
45 {
46 
47 ModuleKey InputSwan::className = GetModuleFactory().RegisterCreatorFunction(
48  ModuleKey(eInputModule, "plt"),
49  InputSwan::create,
50  "Reads Swansea plt format for third-order tetrahedra.");
51 
52 InputSwan::InputSwan(MeshSharedPtr m) : InputModule(m)
53 {
54 }
55 
57 {
58 }
59 
61 {
62  // Open the file stream.
63  OpenStream();
64 
65  vector<vector<NodeSharedPtr> > elementList;
66  vector<int> tmp, tets;
67  vector<double> pts;
68 
69  if (m_mesh->m_verbose)
70  {
71  cout << "InputSwan: Start reading file..." << endl;
72  }
73 
74  m_mesh->m_expDim = 3;
75  m_mesh->m_spaceDim = 3;
76 
77  // First read in header; 4 integers containing number of tets,
78  // number of points, nas2 (unknown) and order of the grid
79  // (i.e. GridOrder = 3 => cubic mesh).
80  tmp.resize(6);
81  m_mshFile.read(reinterpret_cast<char *>(&tmp[0]),
82  static_cast<int>(6 * sizeof(int)));
83 
84  if (tmp[0] != tmp[5] || tmp[0] != 4 * sizeof(int))
85  {
86  cout << "Header data broken" << endl;
87  m_mshFile.reset();
88  return;
89  }
90 
91  int NB_Tet = tmp[1];
92  int NB_Points = tmp[2];
93  int GridOrder = tmp[4];
94  int ND = (GridOrder + 1) * (GridOrder + 2) * (GridOrder + 3) / 6;
95 
96  cout << "NB_Tet = " << NB_Tet << endl;
97  cout << "NB_Points = " << NB_Points << endl;
98  cout << "GridOrder = " << GridOrder << endl;
99  cout << "ND = " << ND << endl;
100 
101  // Now read in list of tetrahedra. Each tet has ND points, and
102  // data are ordered with memory traversed fastest with point
103  // number.
104  tets.resize(ND * NB_Tet + 2);
105  m_mshFile.read(reinterpret_cast<char *>(&tets[0]),
106  static_cast<int>((ND * NB_Tet + 2) * sizeof(int)));
107 
108  if (tets[0] != tets[ND * NB_Tet + 1] ||
109  tets[0] != ND * NB_Tet * sizeof(int))
110  {
111  cout << "ERROR [InputSwan]: Tetrahedron data broken." << endl;
112  m_mshFile.reset();
113  return;
114  }
115 
116  // Finally, read point data: NB_Points tuples (x,y,z).
117  tmp.resize(2);
118  pts.resize(3 * NB_Points);
119  m_mshFile.read(reinterpret_cast<char *>(&tmp[0]),
120  static_cast<int>(sizeof(int)));
121  m_mshFile.read(reinterpret_cast<char *>(&pts[0]),
122  static_cast<int>(3 * NB_Points * sizeof(double)));
123  m_mshFile.read(reinterpret_cast<char *>(&tmp[1]),
124  static_cast<int>(sizeof(int)));
125 
126  if (tmp[0] != tmp[1] || tmp[0] != 3 * NB_Points * sizeof(double))
127  {
128  cout << "ERROR [InputSwan]: Point data broken." << endl;
129  m_mshFile.reset();
130  return;
131  }
132 
133  int vid = 0, i, j;
135 
136  // Read in list of vertices.
137  for (i = 0; i < NB_Points; ++i)
138  {
139  double x = pts[i];
140  double y = pts[1 * NB_Points + i];
141  double z = pts[2 * NB_Points + i];
142  m_mesh->m_node.push_back(
143  boost::shared_ptr<Node>(new Node(vid, x, y, z)));
144  vid++;
145  }
146 
147  // Iterate over list of tetrahedra: for each, create nodes. At the
148  // moment discard high order data and create linear mesh.
149  for (i = 0; i < NB_Tet; ++i)
150  {
151  vector<NodeSharedPtr> nodeList;
152  for (j = 0; j < 20; ++j)
153  {
154  int vid = tets[j * NB_Tet + i + 1];
155  nodeList.push_back(m_mesh->m_node[vid - 1]);
156  }
157 
158  vector<int> tags;
159  tags.push_back(0); // composite
160  tags.push_back(elType); // element type
161 
162  ElmtConfig conf(elType, 3, true, true);
163  ElementSharedPtr E =
164  GetElementFactory().CreateInstance(elType, conf, nodeList, tags);
165  m_mesh->m_element[3].push_back(E);
166  }
167 
168  // Attempt to read in composites. Need to determine number of
169  // triangles from data.
170  tmp.resize(2);
171  m_mshFile.read(reinterpret_cast<char *>(&tmp[0]),
172  static_cast<int>(sizeof(int)));
173  int n_tri = tmp[0] / sizeof(int) / 5;
174  tets.resize(n_tri * 5);
175  m_mshFile.read(reinterpret_cast<char *>(&tets[0]),
176  static_cast<int>(tmp[0]));
177  m_mshFile.read(reinterpret_cast<char *>(&tmp[1]),
178  static_cast<int>(sizeof(int)));
179 
180  if (tmp[0] != tmp[1])
181  {
182  cout << "ERROR [InputSwan]: Surface data broken." << endl;
183  m_mshFile.reset();
184  return;
185  }
186 
187  elType = LibUtilities::eTriangle;
188 
189  // Process list of triangles forming surfaces.
190  for (i = 0; i < n_tri; ++i)
191  {
192  vector<NodeSharedPtr> nodeList;
193 
194  for (j = 0; j < 3; ++j)
195  {
196  nodeList.push_back(m_mesh->m_node[tets[i + j * n_tri] - 1]);
197  }
198 
199  vector<int> tags;
200  tags.push_back(1); // composite
201  tags.push_back(elType); // element type
202 
203  ElmtConfig conf(elType, 1, false, false);
204  ElementSharedPtr E =
205  GetElementFactory().CreateInstance(elType, conf, nodeList, tags);
206  m_mesh->m_element[2].push_back(E);
207  }
208 
209  m_mshFile.reset();
210 
211  // Process the rest of the mesh.
212  ProcessVertices();
213  ProcessEdges();
214  ProcessFaces();
215  ProcessElements();
217 }
218 }
219 }
virtual void Process()
Populate and validate required data structures.
Definition: InputSwan.cpp:60
Basic information about an element.
Definition: ElementConfig.h:50
io::filtering_istream m_mshFile
Input stream.
tBaseSharedPtr CreateInstance(tKey idKey BOOST_PP_COMMA_IF(MAX_PARAM) BOOST_PP_ENUM_BINARY_PARAMS(MAX_PARAM, tParam, x))
Create an instance of the class referred to by idKey.
Definition: NekFactory.hpp:162
STL namespace.
pair< ModuleType, string > ModuleKey
ElementFactory & GetElementFactory()
Definition: Element.cpp:47
NEKMESHUTILS_EXPORT void OpenStream()
Open a file for input.
virtual NEKMESHUTILS_EXPORT void ProcessFaces(bool ReprocessFaces=true)
Extract element faces.
virtual NEKMESHUTILS_EXPORT void ProcessElements()
Generate element IDs.
Abstract base class for input modules.
boost::shared_ptr< Mesh > MeshSharedPtr
Shared pointer to a mesh.
Definition: Mesh.h:147
virtual NEKMESHUTILS_EXPORT void ProcessVertices()
Extract element vertices.
virtual NEKMESHUTILS_EXPORT void ProcessEdges(bool ReprocessEdges=true)
Extract element edges.
boost::shared_ptr< Element > ElementSharedPtr
Definition: Edge.h:49
std::pair< ModuleType, std::string > ModuleKey
virtual NEKMESHUTILS_EXPORT void ProcessComposites()
Generate composites.
ModuleFactory & GetModuleFactory()
tKey RegisterCreatorFunction(tKey idKey, CreatorFunction classCreator, tDescription pDesc="")
Register a class with the factory.
Definition: NekFactory.hpp:215