OptFEM3DP1 Toolbox: common/load_gmsh.m Source File

| OptFEM3DP1 Toolbox 20130618_070730 Matlab/Octave Optimized P1-Lagrange Finite Element Method in 3D

- Main Page
- Related Pages
- Files

- File List
- File Members

All Files Functions Variables Pages
load_gmsh.m
Go to the documentation of this file.
1 function mesh = load_gmsh(filename)
2 % Reads a mesh in msh format, version 1 or 2
3 % Copyright:
4 % See \ref license
5
6 % Copyright (C) 10/2007 R Lorph�re (r.lorphevre@ulg.ac.be)
7
8 % Based on load_gmsh supplied with gmsh-2.0 and load_gmsh2 from JP
9 % Moitinho de Almeida (moitinho@civil.ist.utl.pt)
10
11 % number of nodes in function of the element type
12 msh.NODES_PER_TYPE_OF_ELEMENT = [
13 2 3 4 4 8 6 5 3 6 9 10 27 18 14 1 8 20 15 13 ];
14
15 % The format 2 don't sort the elements by reg phys but by
16 % reg-elm. If this classification is important for your program,
17 % use this (after calling this function):
18 %
19 % [OldRowNumber, NewRowNumber] = sort(OldMatrix(:,SortColumn));
20 % NewMatrix = OldMatrix(NewRowNumber,:);
21 %
22 % Change the name of OldMatrix and NewMatrix with the name of yours
23 % SortColumn by the number of the last column
24
25 mesh = [];
26 mesh.MIN = zeros(3, 1);
27 mesh.MAX = zeros(3, 1);
28 fid = fopen(filename, 'r');
29 disp (' ')
30 while 1
31 endoffile = 0;
32 while 1
33 tline = fgetl(fid);
34 if feof(fid), endoffile=1, break, end
35 if (tline(1) == '$' )
36 if tline(2) == 'N' && tline(3) == 'O'
37 fileformat = 1 ;
38 break
39 end
40 if tline(2) == 'M' && tline(3) == 'e'
41 fileformat = 2;
42 tline = fgetl(fid);
43 disp('Mesh Type')
44 disp (tline)
45 tline = fgetl(fid);
46 if (tline(1) == '$' && tline(2) == 'E'&& tline(3) == 'n')
47 tline = fgetl(fid);
48 break
49 else
50 disp (' This program can only read ASCII mesh file')
51 disp (' of format 1 or 2 from GMSH, try again?')
52 endoffile=1
53 break
54 end
55 end
56 if tline(2) == 'E' && (tline(3) == 'L' || tline(3) == 'l' )
57 break
58 end
59 end
60 end
61 if endoffile == 1, break, end
62 if tline(2) == 'N' && ( tline(3) == 'O' || tline(3) == 'o' )
63 disp('reading nodes')
64 mesh.nbNod = fscanf(fid, '%d', 1);
65 mesh.POS = zeros(mesh.nbNod, 3);
66 for(I=1:mesh.nbNod)
67 iNod = fscanf(fid, '%d', 1);
68 X = fscanf(fid, '%g', 3);
69 IDS(iNod) = I;
70 if (I == 1)
71 mesh.MIN = X;
72 mesh.MAX = X;
73 else
74 if(mesh.MAX(1) < X(1)) mesh.MAX(1) = X(1); end
75 if(mesh.MAX(2) < X(2)) mesh.MAX(2) = X(2); end
76 if(mesh.MAX(3) < X(3)) mesh.MAX(3) = X(3); end
77 if(mesh.MIN(1) > X(1)) mesh.MIN(1) = X(1); end
78 if(mesh.MIN(2) > X(2)) mesh.MIN(2) = X(2); end
79 if(mesh.MIN(3) > X(3)) mesh.MIN(3) = X(3); end
80 end
81 mesh.POS(I, 1) = X(1);
82 mesh.POS(I, 2) = X(2);
83 mesh.POS(I, 3) = X(3);
84 end
85 tline = fgetl(fid);
86 disp('nodes have been read')
87 elseif tline(2) == 'E' && ( tline(3) == 'L' || tline(3) == 'l')
88 disp('reading elements')
89 mesh.nbElm = fscanf(fid, '%d', 1);
90 if (fileformat == 1)
91 nbinfo = 5;
92 tags = 3;
93 end
94 if (fileformat == 2)
95 nbinfo = 4;
96 tags = 4;
97 end
98 mesh.ELE_INFOS = zeros(mesh.nbElm, nbinfo);
99 mesh.nbPoints = 0;
100 mesh.nbLines = 0;
101 mesh.nbTriangles = 0;
102 mesh.nbQuads = 0;
103 mesh.nbTets = 0;
104 mesh.nbHexas = 0;
105 mesh.nbPrisms = 0;
106 mesh.nbPyramids = 0;
107 % comment next 8 lines to get "tight" arrays (will slow down reading)
108 mesh.POINTS = zeros(mesh.nbElm, 2);
109 mesh.LINES = zeros(mesh.nbElm, 3);
110 mesh.TRIANGLES = zeros(mesh.nbElm, 4);
111 mesh.QUADS = zeros(mesh.nbElm, 5);
112 mesh.TETS = zeros(mesh.nbElm, 5);
113 mesh.HEXAS = zeros(mesh.nbElm, 9);
114 mesh.PRISMS = zeros(mesh.nbElm, 7);
115 mesh.PYRAMIDS = zeros(mesh.nbElm, 6);
116 for(I = 1:mesh.nbElm)
117 mesh.ELE_INFOS(I, :) = fscanf(fid, '%d', nbinfo);
118 if (fileformat == 1)
119 % take the mesh.ELE_INFOS(I, 5) nodes of the element
120 NODES_ELEM = fscanf(fid, '%d', mesh.ELE_INFOS(I, 5));
121 end
122 if (fileformat == 2)
123 mesh.ELE_TAGS(I,:) = fscanf(fid, '%d', (mesh.ELE_INFOS(I,3)-1));
124 % take the msh.NODES_PER_TYPE_OF_ELEMENT (mesh.ELE_INFOS(I, 2)) nodes of the element
125 NODES_ELEM = fscanf(fid, '%d', msh.NODES_PER_TYPE_OF_ELEMENT (mesh.ELE_INFOS(I, 2)) );
126 end
127 if(mesh.ELE_INFOS(I, 2) == 15) %% point
128 mesh.nbPoints = mesh.nbPoints + 1;
129 mesh.POINTS(mesh.nbPoints, 1) = IDS(NODES_ELEM(1));
130 mesh.POINTS(mesh.nbPoints, 2) = mesh.ELE_INFOS(I, tags);
131 end
132 if(mesh.ELE_INFOS(I, 2) == 1) %% line
133 mesh.nbLines = mesh.nbLines + 1;
134 mesh.LINES(mesh.nbLines, 1) = IDS(NODES_ELEM(1));
135 mesh.LINES(mesh.nbLines, 2) = IDS(NODES_ELEM(2));
136 mesh.LINES(mesh.nbLines, 3) = mesh.ELE_INFOS(I, tags);
137 end
138 if(mesh.ELE_INFOS(I, 2) == 2) %% triangle
139 mesh.nbTriangles = mesh.nbTriangles + 1;
140 mesh.TRIANGLES(mesh.nbTriangles, 1) = IDS(NODES_ELEM(1));
141 mesh.TRIANGLES(mesh.nbTriangles, 2) = IDS(NODES_ELEM(2));
142 mesh.TRIANGLES(mesh.nbTriangles, 3) = IDS(NODES_ELEM(3));
143 mesh.TRIANGLES(mesh.nbTriangles, 4) = mesh.ELE_INFOS(I, tags);
144 end
145 if(mesh.ELE_INFOS(I, 2) == 3) %% quadrangle
146 mesh.nbQuads = mesh.nbQuads + 1;
147 mesh.QUADS(mesh.nbQuads, 1) = IDS(NODES_ELEM(1));
148 mesh.QUADS(mesh.nbQuads, 2) = IDS(NODES_ELEM(2));
149 mesh.QUADS(mesh.nbQuads, 3) = IDS(NODES_ELEM(3));
150 mesh.QUADS(mesh.nbQuads, 4) = IDS(NODES_ELEM(4));
151 mesh.QUADS(mesh.nbQuads, 5) = mesh.ELE_INFOS(I, tags);
152 end
153 if(mesh.ELE_INFOS(I, 2) == 4) %% tetrahedron
154 mesh.nbTets = mesh.nbTets + 1;
155 mesh.TETS(mesh.nbTets, 1) = IDS(NODES_ELEM(1));
156 mesh.TETS(mesh.nbTets, 2) = IDS(NODES_ELEM(2));
157 mesh.TETS(mesh.nbTets, 3) = IDS(NODES_ELEM(3));
158 mesh.TETS(mesh.nbTets, 4) = IDS(NODES_ELEM(4));
159 mesh.TETS(mesh.nbTets, 5) = mesh.ELE_INFOS(I, tags);
160 end
161 if(mesh.ELE_INFOS(I, 2) == 5) %% hexahedron
162 mesh.nbHexas = mesh.nbHexas + 1;
163 mesh.HEXAS(mesh.nbHexas, 1) = IDS(NODES_ELEM(1));
164 mesh.HEXAS(mesh.nbHexas, 2) = IDS(NODES_ELEM(2));
165 mesh.HEXAS(mesh.nbHexas, 3) = IDS(NODES_ELEM(3));
166 mesh.HEXAS(mesh.nbHexas, 4) = IDS(NODES_ELEM(4));
167 mesh.HEXAS(mesh.nbHexas, 5) = IDS(NODES_ELEM(5));
168 mesh.HEXAS(mesh.nbHexas, 6) = IDS(NODES_ELEM(6));
169 mesh.HEXAS(mesh.nbHexas, 7) = IDS(NODES_ELEM(7));
170 mesh.HEXAS(mesh.nbHexas, 8) = IDS(NODES_ELEM(8));
171 mesh.HEXAS(mesh.nbHexas, 9) = mesh.ELE_INFOS(I, tags);
172 end
173 if(mesh.ELE_INFOS(I, 2) == 6) %% prism
174 mesh.nbPrisms = mesh.nbPrisms + 1;
175 mesh.PRISMS(mesh.nbPrisms, 1) = IDS(NODES_ELEM(1));
176 mesh.PRISMS(mesh.nbPrisms, 2) = IDS(NODES_ELEM(2));
177 mesh.PRISMS(mesh.nbPrisms, 3) = IDS(NODES_ELEM(3));
178 mesh.PRISMS(mesh.nbPrisms, 4) = IDS(NODES_ELEM(4));
179 mesh.PRISMS(mesh.nbPrisms, 5) = IDS(NODES_ELEM(5));
180 mesh.PRISMS(mesh.nbPrisms, 6) = IDS(NODES_ELEM(6));
181 mesh.PRISMS(mesh.nbPrisms, 7) = mesh.ELE_INFOS(I, tags);
182 end
183 if(mesh.ELE_INFOS(I, 2) == 7) %% pyramid
184 mesh.nbPyramids = mesh.nbPyramids + 1;
185 mesh.PYRAMIDS(mesh.nbPyramids, 1) = IDS(NODES_ELEM(1));
186 mesh.PYRAMIDS(mesh.nbPyramids, 2) = IDS(NODES_ELEM(2));
187 mesh.PYRAMIDS(mesh.nbPyramids, 3) = IDS(NODES_ELEM(3));
188 mesh.PYRAMIDS(mesh.nbPyramids, 4) = IDS(NODES_ELEM(4));
189 mesh.PYRAMIDS(mesh.nbPyramids, 5) = IDS(NODES_ELEM(5));
190 mesh.PYRAMIDS(mesh.nbPyramids, 6) = IDS(NODES_ELEM(6));
191 mesh.PYRAMIDS(mesh.nbPyramids, 7) = mesh.ELE_INFOS(I, tags);
192 end
193 end
194 tline = fgetl(fid);
195 disp('elements have been read')
196
197 end
198 end
199
200 fclose(fid);