Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ElmerCSC
GitHub Repository: ElmerCSC/elmerfem
Path: blob/devel/ElmerGUI/netgen/libsrc/interface/writegmsh.cpp
3206 views
1
/*************************************
2
* Write Gmsh file
3
* First issue the 04/26/2004 by Paul CARRICO ([email protected])
4
* At the moment, the GMSH format is available for
5
* linear tetrahedron elements i.e. in 3D
6
* (based on Neutral Format)
7
*
8
* Second issue the 05/05/2004 by Paul CARRICO
9
* Thanks to Joachim Schoeberl for the correction of a minor bug
10
* the 2 initial Gmsh Format (i.e. volume format and surface format) are group together)
11
* in only one file
12
**************************************/
13
14
#include <mystdlib.h>
15
16
#include <myadt.hpp>
17
#include <linalg.hpp>
18
#include <csg.hpp>
19
#include <meshing.hpp>
20
21
namespace netgen
22
{
23
#include "writeuser.hpp"
24
25
26
27
/*
28
* GMSH mesh format
29
* points, elements, surface elements and physical entities
30
*/
31
32
void WriteGmshFormat (const Mesh & mesh,
33
const CSGeometry & geom,
34
const string & filename)
35
{
36
ofstream outfile (filename.c_str());
37
outfile.precision(6);
38
outfile.setf (ios::fixed, ios::floatfield);
39
outfile.setf (ios::showpoint);
40
41
int np = mesh.GetNP(); /// number of point
42
int ne = mesh.GetNE(); /// number of element
43
int nse = mesh.GetNSE(); /// number of surface element (BC)
44
int i, j, k, l;
45
46
47
/*
48
* 3D section : Linear volume elements (only tetrahedra)
49
*/
50
51
if (ne > 0 && mesh.VolumeElement(1).GetNP() == 4)
52
{
53
cout << "Write GMSH Format \n";
54
cout << "The GMSH format is available for linear tetrahedron elements only in 3D\n" << endl;
55
56
int inverttets = mparam.inverttets;
57
int invertsurf = mparam.inverttrigs;
58
59
60
/// Write nodes
61
outfile << "$NOD\n";
62
outfile << np << "\n";
63
64
for (i = 1; i <= np; i++)
65
{
66
const Point3d & p = mesh.Point(i);
67
outfile << i << " "; /// node number
68
outfile << p.X() << " ";
69
outfile << p.Y() << " ";
70
outfile << p.Z() << "\n";
71
}
72
outfile << "$ENDNOD\n";
73
74
/// write elements
75
outfile << "$ELM\n";
76
outfile << ne + nse << "\n"; //// number of elements + number of surfaces BC
77
78
for (i = 1; i <= nse; i++)
79
{
80
Element2d el = mesh.SurfaceElement(i);
81
if (invertsurf) el.Invert();
82
outfile << i;
83
outfile << " ";
84
outfile << "2";
85
outfile << " ";
86
outfile << mesh.GetFaceDescriptor (el.GetIndex()).BCProperty() << " ";
87
/// that means that physical entity = elementary entity (arbitrary approach)
88
outfile << mesh.GetFaceDescriptor (el.GetIndex()).BCProperty() << " ";
89
outfile << "3";
90
outfile << " ";
91
for (j = 1; j <= el.GetNP(); j++)
92
{
93
outfile << " ";
94
outfile << el.PNum(j);
95
}
96
outfile << "\n";
97
}
98
99
100
for (i = 1; i <= ne; i++)
101
{
102
Element el = mesh.VolumeElement(i);
103
if (inverttets) el.Invert();
104
outfile << nse + i; /// element number
105
outfile << " ";
106
outfile << "4"; /// element type i.e. Tetraedron == 4
107
outfile << " ";
108
outfile << 100000 + el.GetIndex();
109
/// that means that physical entity = elementary entity (arbitrary approach)
110
outfile << " ";
111
outfile << 100000 + el.GetIndex(); /// volume number
112
outfile << " ";
113
outfile << "4"; /// number of nodes i.e. 4 for a tetrahedron
114
115
for (j = 1; j <= el.GetNP(); j++)
116
{
117
outfile << " ";
118
outfile << el.PNum(j);
119
}
120
outfile << "\n";
121
}
122
123
124
outfile << "$ENDELM\n";
125
}
126
127
/*
128
* End of 3D section
129
*/
130
131
132
133
134
135
/*
136
* 2D section : available for triangles and quadrangles
137
*/
138
else if (ne == 0) /// means that there's no 3D element
139
{
140
cout << "\n Write Gmsh Surface Mesh (triangle and/or quadrangles)" << endl;
141
142
/// Write nodes
143
outfile << "$NOD\n";
144
outfile << np << "\n";
145
146
for (i = 1; i <= np; i++)
147
{
148
const Point3d & p = mesh.Point(i);
149
outfile << i << " "; /// node number
150
outfile << p.X() << " ";
151
outfile << p.Y() << " ";
152
outfile << p.Z() << "\n";
153
}
154
outfile << "$ENDNOD\n";
155
156
157
/// write triangles & quadrangles
158
outfile << "$ELM\n";
159
outfile << nse << "\n";
160
161
for (k = 1; k <= nse; k++)
162
{
163
const Element2d & el = mesh.SurfaceElement(k);
164
165
166
outfile << k;
167
outfile << " ";
168
outfile << (el.GetNP()-1); // 2 for a triangle and 3 for a quadrangle
169
outfile << " ";
170
outfile << mesh.GetFaceDescriptor (el.GetIndex()).BCProperty() << " ";
171
/// that means that physical entity = elementary entity (arbitrary approach)
172
outfile << mesh.GetFaceDescriptor (el.GetIndex()).BCProperty() << " ";
173
outfile << (el.GetNP()); // number of node per surfacic element
174
outfile << " ";
175
176
for (l = 1; l <= el.GetNP(); l++)
177
{
178
outfile << " ";
179
outfile << el.PNum(l);
180
}
181
outfile << "\n";
182
183
}
184
outfile << "$ENDELM$ \n";
185
}
186
187
/*
188
* End of 2D section
189
*/
190
191
else
192
{
193
cout << " Invalide element type for Gmsh volume Format !\n";
194
}
195
196
197
}
198
}
199
200
201
202