Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
GmshReader.cpp
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_GmshReader_cpp_
9#define model_GmshReader_cpp_
10
11#include <iostream>
12#include <string>
13#include <fstream>
14#include <vector>
15#include <map>
16#include <assert.h>
17#include <chrono>
18#include <Eigen/Dense>
19
20#include <TerminalColors.h>
21#include <GmshReader.h>
22
23namespace model
24{
25
26
27
28 std::map<int,std::string> GmshReader::getElementTypes()
29 {
30 std::map<int,std::string> temp;
31 temp.emplace(1,"2-node line");
32 temp.emplace(2,"3-node triangle");
33 temp.emplace(3,"4-node quadrangle");
34 temp.emplace(4,"4-node tetrahedron");
35 temp.emplace(5,"8-node hexahedron");
36 temp.emplace(6,"6-node prism");
37 temp.emplace(7,"5-node pyramid");
38 temp.emplace(8,"3-node second-order line");
39 temp.emplace(9,"6-node second-order triangle");
40 temp.emplace(10,"9-node second-order quadrangle");
41 temp.emplace(11,"10-node tetrahedron");
42 temp.emplace(12,"27-node second order hexahedron");
43 temp.emplace(13,"18-node second order prism");
44 temp.emplace(14,"14-node second order pyramid");
45 temp.emplace(15,"1-node point");
46 return temp;
47 }
48
49 /**********************************************************************/
50 int GmshReader::numberOfNodes(const int& type)
51 {
52
53 switch (type)
54 {
55 case 1: // 2-node line
56 return 2;
57 break;
58
59 case 2: // 3-node triangle
60 return 3;
61 break;
62
63 case 3: // 4-node quadrangle
64 return 4;
65 break;
66
67 case 4: // 4-node tetrahedron
68 return 4;
69 break;
70
71 case 8: // 3-node second order line (2 nodes associated with the vertices and 1 with the edge).
72 return 3;
73 break;
74
75 case 9: // 6-node second order triangle (3 nodes associated with the vertices and 3 with the edges).
76 return 6;
77 break;
78
79 case 11: // 10-node tetrahedron
80 return 10;
81 break;
82
83 case 12: // 27-node second order hexahedron (8 nodes associated with the vertices, 12 with the edges, 6 with the faces and 1 with the volume).
84 return 27;
85 break;
86
87 case 13: // 18-node second order prism (6 nodes associated with the vertices, 9 with the edges and 3 with the quadrangular faces).
88 return 18;
89 break;
90
91 case 14: // 14-node second order pyramid (5 nodes associated with the vertices, 8 with the edges and 1 with the quadrangular face).
92 return 14;
93 break;
94
95 case 15: // 1-node point.
96 return 1;
97 break;
98
99 default:
100 {
101 std::cout<<"Element type="<<type<<std::endl;
102 assert(0 && "FINISH HERE");
103 return 0;
104 break;
105 }
106 }
107 }
108
109 /**********************************************************************/
110 GmshReader::GmshReader(const std::string& meshFileName) :
111 /* init */ elementTypes(getElementTypes())
112 {
113 read(meshFileName,Eigen::Matrix<double,3,3>::Identity(),Eigen::Matrix<double,3,1>::Zero());
114 }
115
116 GmshReader::GmshReader(const std::string& meshFileName,const Eigen::Matrix<double,3,3>& A,const Eigen::Matrix<double,3,1>& x0) :
117 /* init */ elementTypes(getElementTypes())
118 {
119 read(meshFileName,A,x0);
120 }
121
122 /**********************************************************************/
123 const std::map<size_t,Eigen::Vector3d>& GmshReader::nodes() const
124 {
125 return *this;
126 }
127
128 /**********************************************************************/
129 std::map<size_t,Eigen::Vector3d>& GmshReader::nodes()
130 {
131 return *this;
132 }
133
134 /**********************************************************************/
135 const std::map<size_t,GmshElement>& GmshReader::elements() const
136 {
137 return *this;
138 }
139
140 /**********************************************************************/
141 std::map<size_t,GmshElement>& GmshReader::elements()
142 {
143 return *this;
144 }
145
146 std::map<int,GmshPhysicalName>& GmshReader::physicalNames()
147 {
148 return *this;
149 }
150
151 const std::map<int,GmshPhysicalName>& GmshReader::physicalNames() const
152 {
153 return *this;
154 }
155
156 /**********************************************************************/
157 void GmshReader::readVersion2(std::ifstream& meshFile,const Eigen::Matrix<double,3,3>& A,const Eigen::Matrix<double,3,1>& x0)
158 {
159 std::string line;
160 while (std::getline(meshFile, line))
161 {
162
163 if(line=="$PhysicalNames")
164 {
165 std::getline(meshFile, line);
166 numberPhysicalNames=std::atoi(line.c_str());
167 for(int k=0;k<numberPhysicalNames;++k)
168 {
169 std::getline(meshFile, line);
170 // FINISH HERE
171 }
172 }
173
174 if(line=="$Nodes")
175 {
176
177 size_t nodeID(0);
178 Eigen::Vector3d nodePos;
179
180 std::getline(meshFile, line);
181 numberNodes=std::atoi(line.c_str());
182 for(int k=0;k<numberNodes;++k)
183 {// Note Gmsh is only 3-dimensional
184 std::getline(meshFile, line);
185 std::stringstream ss(line);
186 ss>>nodeID;
187 ss>>nodePos(0);
188 ss>>nodePos(1);
189 ss>>nodePos(2);
190 const bool success=nodes().emplace(nodeID,A*(nodePos-x0)).second;
191 if(!success)
192 {
193 std::cout<<"Could not insert node "<<nodeID<<" "<<nodePos.transpose()<<". Exiting."<<std::endl;
194 exit(EXIT_FAILURE);
195 }
196 }
197 }
198
199 if(line=="$Elements")
200 {
201
202 std::getline(meshFile, line);
203 numberElements=std::atoi(line.c_str());
204 for(int k=0;k<numberElements;++k)
205 {
206 GmshElement element;
207
208
209 std::getline(meshFile, line);
210 std::stringstream ss(line);
211 ss>>element.ID;
212 ss>>element.type;
213 int tags(0);
214 ss>>tags;
215 for(int n=0;n<tags;++n)
216 {
217 element.tags.push_back(0);
218 ss>>element.tags.back();
219 }
220 for(int n=0;n<numberOfNodes(element.type);++n)
221 {
222 element.nodeIDs.push_back(0);
223 ss>>element.nodeIDs.back();
224 }
225
226 const bool success=elements().emplace(element.ID,element).second;
227 if(!success)
228 {
229 std::cout<<"Could not insert element "<<element.ID<<". Exiting."<<std::endl;
230 exit(EXIT_FAILURE);
231 }
232
233 numberElementsByType[element.type]++;
234
235 }
236 }
237 }
238 }
239
240 /**********************************************************************/
241 void GmshReader::read(const std::string& meshFileName,const Eigen::Matrix<double,3,3>& A,const Eigen::Matrix<double,3,1>& x0)
242 {
243 nodes().clear();
244 elements().clear();
245
246 std::ifstream meshFile(meshFileName);
247
248 if(meshFile.is_open())
249 {
250 const auto t0= std::chrono::system_clock::now();
251 std::cout<<greenBoldColor<<"Reading msh file "<<meshFileName<<defaultColor<<std::flush;
252 std::string line;
253 while (std::getline(meshFile, line))
254 {
255
256 if(line=="$MeshFormat")
257 {
258 meshFile >> version >> format >> size;
259
260
261 if(format!=0)
262 {
263 std::cout<<"Unsupported format. Only format=0 (ASCII) is supported. "<<std::endl;
264 exit (EXIT_FAILURE);
265 }
266
267 if(version>=2.0 && version<3.0)
268 {
269 readVersion2(meshFile,A,x0);
270 }
271 else
272 {
273 std::cout<<"Unsupported msh version"<<std::endl;
274 exit (EXIT_FAILURE);
275 }
276 break;
277 }
278 }
279 std::cout<<magentaColor<<" ["<<(std::chrono::duration<double>(std::chrono::system_clock::now()-t0)).count()<<" sec]"<<defaultColor<<std::endl;
280 std::cout<<"version="<<version<<std::endl;
281 std::cout<<"format="<<format<<std::endl;
282 std::cout<<"size="<<size<<std::endl;
283 std::cout<<"numberPhysicalNames="<<numberPhysicalNames<<std::endl;
284 std::cout<<"number of Nodes="<<numberNodes<<std::endl;
285 std::cout<<"number of Elements="<<numberElements<<std::endl;
286 for(const auto& pair : numberElementsByType)
287 {
288 std::cout<<" "<<pair.second<<" of type "<<pair.first<<": "<<elementTypes.at(pair.first)<<std::endl;
289 }
290 }
291 else
292 {
293 std::cout<<"File "<<meshFileName<<" cannot be opened. Exiting."<<std::endl;
294 exit(EXIT_FAILURE);
295 }
296
297
298 }
299
300
301}
302#endif
303
const std::map< size_t, Eigen::Vector3d > & nodes() const
std::map< int, GmshPhysicalName > & physicalNames()
const std::map< size_t, GmshElement > & elements() const
GmshReader(const std::string &meshFileName)
void readVersion2(std::ifstream &meshFile, const Eigen::Matrix< double, 3, 3 > &A, const Eigen::Matrix< double, 3, 1 > &x0)
const std::map< int, std::string > elementTypes
Definition GmshReader.h:55
void read(const std::string &meshFileName, const Eigen::Matrix< double, 3, 3 > &A, const Eigen::Matrix< double, 3, 1 > &x0)
std::map< int, size_t > numberElementsByType
Definition GmshReader.h:62
function infiniteDisplacement syms Ax Ay Az Bx By Bz qx qy qz u L x y z real A
size_t InstanceCount< Derived >::count
static std::string magentaColor
static std::string greenBoldColor
static std::string defaultColor
std::vector< int > tags
Definition GmshReader.h:39
std::vector< size_t > nodeIDs
Definition GmshReader.h:40
REAL t0
newsubseg ss[0]
int k
enum insertvertexresult success
vertex temp