HArD::Core2D
Hybrid Arbitrary Degree::Core 2D - Library to implement 2D schemes with edge and cell polynomials as unknowns
MeshReaderTyp2.hpp
Go to the documentation of this file.
1 // Class to read a typ2 mesh file
2 //
3 // Author: Liam Yemm (liam.yemm@monash.edu)
4 //
5 
6 #ifndef MESHREADERTYP2a_HPP
7 #define MESHREADERTYP2a_HPP
8 
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12 #include <vector>
13 #include <array>
14 namespace Mesh2D
15 {
16 
17  // ----------------------------------------------------------------------------
18  // Class definition
19  // ----------------------------------------------------------------------------
20 
28  {
29  public:
35  // MeshReaderTyp2a(std::string file_name); ///< class to read the cells and vertices in a file
36 
37  MeshReaderTyp2(std::string file_name) : _file_name(file_name) {}
38 
39  ~MeshReaderTyp2() {} // default destructor
40 
47  // void read_file(std::vector<std::array<double, 2>> &vertices, std::vector<std::vector<std::size_t>> &cells);
48 
49  void read_mesh(std::vector<std::array<double, 2>> &vertices, std::vector<std::vector<std::size_t>> &cells)
50  {
51  std::ifstream inFile;
52 
53  std::cout << "[MeshReaderTyp2] Reading mesh file " + _file_name;
54 
55  inFile.open(_file_name);
56  if (!inFile)
57  {
58  std::cerr << " Unable to open mesh file\n";
59  exit(1);
60  }
61 
62  // std::cout << " Reading vertices...\n";
63 
64  // Read mesh file
65  std::string ignore_line;
66  std::getline(inFile, ignore_line); // ignore the first line (should simply say "Vertices")
67 
68  std::size_t n_verts;
69  inFile >> n_verts;
70 
71  std::size_t vert_count = 0;
72  double xcoord, ycoord;
73  std::array<double, 2> coord;
74 
75  while (vert_count < n_verts && inFile >> xcoord >> ycoord)
76  {
77  coord[0] = xcoord;
78  coord[1] = ycoord;
79  vertices.push_back(coord);
80  vert_count++;
81  }
82 
83  std::cout << " (" + std::to_string(vertices.size()) + "/" + std::to_string(n_verts) + " vertices, ";
84 
85  std::getline(inFile, ignore_line); // ignore the remainder of previous line
86  std::getline(inFile, ignore_line); // ignore the following line (should simply say "cells")
87 
88  std::size_t n_cells;
89  inFile >> n_cells;
90 
91  std::size_t cell_count = 0;
92  std::size_t n_local_cell_nodes;
93 
94  while (cell_count < n_cells && inFile >> n_local_cell_nodes)
95  {
96  std::size_t node_count = 0;
97  std::vector<std::size_t> cell_nodes(n_local_cell_nodes);
98  while (node_count < n_local_cell_nodes && inFile >> cell_nodes[node_count])
99  {
100  node_count++;
101  }
102  cells.push_back(cell_nodes);
103  cell_count++;
104  }
105 
106  std::cout << std::to_string(cells.size()) + "/" + std::to_string(n_cells) + " cells)\n";
107 
108  inFile.close();
109  }
110 
111  private:
112  std::string _file_name;
113  };
114 
115  // MeshReaderTyp2a::MeshReaderTyp2a(std::string file_name) : _file_name(file_name) {}
116 
117  // void MeshReaderTyp2a::read_file(std::vector<std::array<double, 2>> &vertices, std::vector<std::vector<std::size_t>> &cells)
118  // {
119  // std::ifstream inFile;
120  // inFile.open(_file_name + ".typ2");
121  // if (!inFile)
122  // {
123  // std::cerr << " Unable to open mesh file\n";
124  // exit(1);
125  // }
126 
127  // // std::cout << " Reading vertices...\n";
128 
129  // // Read mesh file
130  // std::string ignore_line;
131  // std::getline(inFile, ignore_line); // ignore the first line (should simply say "Vertices")
132 
133  // std::size_t n_verts;
134  // inFile >> n_verts;
135 
136  // std::size_t vert_count = 0;
137  // double xcoord, ycoord;
138  // std::array<double, 2> coord;
139 
140  // while (vert_count < n_verts && inFile >> xcoord >> ycoord)
141  // {
142  // coord[0] = xcoord;
143  // coord[1] = ycoord;
144  // vertices.push_back(coord);
145  // vert_count++;
146  // }
147 
148  // std::cout << "Read " + std::to_string(vertices.size()) + "/" + std::to_string(n_verts) + " vertices\n";
149 
150  // std::getline(inFile, ignore_line); // ignore the next line (should simply say "cells")
151 
152  // std::size_t n_cells;
153  // inFile >> n_cells;
154 
155  // std::size_t cell_count = 0;
156  // std::size_t n_local_cell_nodes;
157 
158  // while (cell_count < n_cells && inFile >> n_local_cell_nodes)
159  // {
160  // std::size_t node_count = 0;
161  // std::vector<std::size_t> cell_nodes(n_local_cell_nodes);
162  // while (node_count < n_local_cell_nodes && inFile >> cell_nodes[node_count])
163  // {
164  // node_count++;
165  // }
166  // cells.push_back(cell_nodes);
167  // cell_count++;
168  // }
169 
170  // std::cout << "Read " + std::to_string(cells.size()) + "/" + std::to_string(n_cells) + " cells\n";
171 
172  // inFile.close();
173  // }
174 
175 } // end namespace Mesh2D
176 #endif
The MeshReaderTyp2 class provides functions to read a typ2 mesh file.
Definition: MeshReaderTyp2.hpp:28
~MeshReaderTyp2()
Definition: MeshReaderTyp2.hpp:39
void read_mesh(std::vector< std::array< double, 2 >> &vertices, std::vector< std::vector< std::size_t >> &cells)
Definition: MeshReaderTyp2.hpp:49
MeshReaderTyp2(std::string file_name)
Definition: MeshReaderTyp2.hpp:37
Definition: Mesh2D.hpp:7