3 % Initialization of the Mesh structure from a FreeFEM++ mesh file
7 % cFileName: FreeFEM++ mesh file name (
string)
10 % Mesh: mesh structure
12 % Generated fields of Mesh:
13 % nq: total number of vertices.
14 % q: Array of vertices coordinates, 3-by-nq array.
15 % q(il,j) is the il-th coordinate of the j-th vertex, il in {1,3}
16 % and j in {1,...,nq}.
17 % ql: Array of vertices labels, 1-by-nq array.
18 % nme: total number of elements.
19 % me: Connectivity array, 4-by-nme array.
20 % me(jl,
k) is the storage index of the jl-th vertex
21 % of the k-th triangle in the array q of vertices coordinates,
22 % jl in {1,..,4} and
k in {1,...,nme}.
23 % mel: Array of elements labels, 1-by-nme array.
24 % nbf: total number of boundary faces, also denoted by `\nbf`
25 % bf: Connectivity array for boundary faces, 3-by-nbf array.
26 % bf(il,l) is the storage index of the il-th vertex
27 % of the l-th boundary face in the array q of vertices coordinates,
28 % il in {1,3} and l in {1,...,nbf}.
29 % bfl: Array of boundary faces labels, 1-by-nbf array.
30 % volumes: Array of volumes, 1-by-nme array. volumes(
k) is the volume
31 % of the k-th triangle.
32 % abf: Array of faces areas, 1-by-nbf array. abf(j) is
33 % the area of the j-th boundary face.
36 % Th=GetMesh('cube.mesh')
41 % Copyright (C) 2013 CJS (LAGA)
42 % see README for details
49 p.addParamValue(
'format',
'freefem', @ischar );
54 Format=
p.Results.format;
55 if (strcmp(Format,
'freefem'))
56 Mesh=GetFreefemMesh(cFileName);
59 if (strcmp(Format,
'gmsh'))
60 Mesh=GetGmshMesh(cFileName);
62 if (strcmp(Format,
'medit'))
63 Mesh=GetMeditMesh(cFileName);
67 % Read FreeFEM++ 3D meshes
68 function Mesh=GetFreefemMesh(cFileName)
69 [fid,message]=fopen(cFileName,'r');
71 error([message,
' : ',cFileName]);
77 nq=fscanf(fid,'%d',1);
79 R=fscanf(fid,'%f %f %f %d',[4,nq]);
85 nme=fscanf(fid,'%d',1);
86 R=fscanf(fid,'%d %d %d %d %d',[5,nme]);
93 nbf=fscanf(fid,'%d',1);
94 R=fscanf(fid,'%d %d %d %d',[4,nbf]);
99 nq=fscanf(fid,'%d',1);
101 R=textscan(fid,'%f %f %f %d',nq);
108 nme=fscanf(fid,
'%ld',1);
110 R=textscan(fid,
'%d %d %d %d %d',nme);
111 me=[R{1},R{2},R{3},R{4}]
';
116 nbf=fscanf(fid,
'%d',1);
118 R=textscan(fid,
'%d %d %d %d',nbf);
119 bf=[R{1},R{2},R{3}]
';
123 Mesh=
struct(
'q',q,
'me',double(me),
'ql',ql,
'mel',double(mel),
'bf',double(bf),
'bfl',double(bfl), ...
128 'abf',FaceArea(bf,q));
132 function Th=GetGmshMesh(cFileName)
136 Th.me=msh.TETS(1:msh.nbTets,1:4)';
138 Th.mel=msh.TETS(1:msh.nbTets,5);
143 function Mesh=GetMeditMesh(cFileName)
144 [fid,message]=fopen(cFileName,'r');
146 error([message,' : ',cFileName]);
151 while ~strcmp(strtrim(tline),'Vertices')
154 error('Error : Vertices not found');
157 nq=fscanf(fid,'%d',1);
158 R=fscanf(fid,'%f %f %f %d',[4,nq]);
163 while ~strcmp(strtrim(tline),'Triangles')
166 error('Error : Triangles not found');
169 nbf=fscanf(fid,'%d',1);
170 R=fscanf(fid,'%d %d %d %d',[4,nbf]);
176 while ~strcmp(strtrim(tline),'Tetrahedra')
179 error('Error : Tetrahedra not found');
182 nme=fscanf(fid,'%d',1);
183 R=fscanf(fid,'%d %d %d %d %d',[5,nme]);
188 while ~strcmp(strtrim(tline),'Vertices')
191 nq=fscanf(fid,'%d',1);
193 R=textscan(fid,'%f %f %f %d',nq);
197 while ~strcmp(strtrim(tline),
'Triangles')
200 nbf=fscanf(fid,'%d',1);
202 R=textscan(fid,'%d %d %d %d',nbf);
203 bf=[R{1},R{2},R{3}]
';
206 while ~strcmp(strtrim(tline),
'Tetrahedra')
210 nme=fscanf(fid,'%ld',1);
212 R=textscan(fid,'%d %d %d %d %d',nme);
213 me=[R{1},R{2},R{3},R{4}]
';
217 Mesh=
struct(
'q',q,
'me',double(me),
'ql',ql,
'mel',double(mel),
'bf',double(bf),
'bfl',double(bfl), ...
222 'abf',FaceArea(bf,q));