2 % Reads a mesh in msh format,
version 1 or 2
6 % Copyright (C) 10/2007 R Lorph�re (r.lorphevre@ulg.ac.be)
8 % Based on
load_gmsh supplied with gmsh-2.0 and load_gmsh2 from JP
9 % Moitinho de Almeida (moitinho@civil.ist.utl.pt)
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 ];
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):
19 % [OldRowNumber, NewRowNumber] = sort(OldMatrix(:,SortColumn));
20 % NewMatrix = OldMatrix(NewRowNumber,:);
22 % Change the name of OldMatrix and NewMatrix with the name of yours
23 % SortColumn by the number of the last column
26 mesh.MIN = zeros(3, 1);
27 mesh.MAX = zeros(3, 1);
28 fid = fopen(filename, 'r
');
34 if feof(fid), endoffile=1, break, end
36 if tline(2) == 'N' && tline(3) == 'O
'
40 if tline(2) == 'M' && tline(3) == 'e
'
46 if (tline(1) == '$
' && tline(2) == 'E
'&& tline(3) == 'n
')
50 disp (' This program can only read ASCII mesh file
')
51 disp (' of format 1 or 2 from GMSH,
try again?
')
56 if tline(2) == 'E
' && (tline(3) == 'L
' || tline(3) == 'l
' )
61 if endoffile == 1, break, end
62 if tline(2) == 'N' && ( tline(3) == 'O
' || tline(3) == 'o
' )
64 mesh.nbNod = fscanf(fid, '%d
', 1);
65 mesh.POS = zeros(mesh.nbNod, 3);
67 iNod = fscanf(fid, '%d
', 1);
68 X = fscanf(fid, '%g
', 3);
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
81 mesh.POS(I, 1) = X(1);
82 mesh.POS(I, 2) = X(2);
83 mesh.POS(I, 3) = X(3);
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);
98 mesh.ELE_INFOS = zeros(mesh.nbElm, nbinfo);
101 mesh.nbTriangles = 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);
119 % take the mesh.ELE_INFOS(I, 5) nodes of the element
120 NODES_ELEM = fscanf(fid, '%d
', mesh.ELE_INFOS(I, 5));
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)) );
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);
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);
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);
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);
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);
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);
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);
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);
195 disp('elements have been read
')