OptFEM3DP1 Toolbox: Opt/GetMeshOptV2.m Source File

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

- Main Page
- Related Pages
- Files

- File List
- File Members

All Files Functions Variables Pages
GetMeshOptV2.m
Go to the documentation of this file.
1 function Mesh=GetMeshOptV2(cFileName,varargin)
2 % function Mesh=GetMeshOpt(cFileName)
3 % Initialization of the Mesh structure from a FreeFEM++ mesh file
4 % % Optimized version
5 %
6 % Parameters:
7 % cFileName: FreeFEM++ mesh file name (string)
8 %
9 % Return values:
10 % Mesh: mesh structure
11 %
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.
34 %
35 % Example:
36 % Th=GetMesh('cube.mesh')
37 % Copyright:
38 % See \ref license
39
40 %
41 % Copyright (C) 2013 CJS (LAGA)
42 % see README for details
43
44 p = inputParser;
45 if isOctave()
46 p=p.addParamValue('format', 'freefem', @ischar );
47 p=p.parse(varargin{:});
48 else
49 p.addParamValue('format', 'freefem', @ischar );
50 p.parse(varargin{:});
51 end
52
53
54 Format=p.Results.format;
55 if (strcmp(Format,'freefem'))
56 Mesh=GetFreefemMesh(cFileName);
57 end
58
59 if (strcmp(Format,'gmsh'))
60 Mesh=GetGmshMesh(cFileName);
61 end
62 if (strcmp(Format,'medit'))
63 Mesh=GetMeditMesh(cFileName);
64 end
65 end
66
67 % Read FreeFEM++ 3D meshes
68 function Mesh=GetFreefemMesh(cFileName)
69 [fid,message]=fopen(cFileName,'r');
70 if ( fid == -1 )
71 error([message,' : ',cFileName]);
72 end
73 for i=1:5
74 tline = fgetl(fid);
75 end
76 if isOctave()
77 nq=fscanf(fid,'%d',1);
78
79 R=fscanf(fid,'%f %f %f %d',[4,nq]);
80 q=R([1 2 3],:);
81 ql=R(4,:);
82 for i=1:3
83 tline = fgetl(fid);
84 end
85 nme=fscanf(fid,'%d',1);
86 R=fscanf(fid,'%d %d %d %d %d',[5,nme]);
87
88 me=R([1:4],:);
89 mel=R(5,:);
90 for i=1:3
91 tline = fgetl(fid);
92 end
93 nbf=fscanf(fid,'%d',1);
94 R=fscanf(fid,'%d %d %d %d',[4,nbf]);
95
96 bf=R([1 2 3],:);
97 bfl=R(4,:);
98 else % Matlab
99 nq=fscanf(fid,'%d',1);
100
101 R=textscan(fid,'%f %f %f %d',nq);
102 q=[R{1},R{2},R{3}]';
103 ql=R{4}';
104
105 for i=1:3
106 tline = fgetl(fid);
107 end
108 nme=fscanf(fid,'%ld',1);
109
110 R=textscan(fid,'%d %d %d %d %d',nme);
111 me=[R{1},R{2},R{3},R{4}]';
112 mel=R{5}';
113 for i=1:3
114 tline = fgetl(fid);
115 end
116 nbf=fscanf(fid,'%d',1);
117
118 R=textscan(fid,'%d %d %d %d',nbf);
119 bf=[R{1},R{2},R{3}]';
120 bfl=R{4}';
121 end
122 fclose(fid);
123 Mesh=struct('q',q,'me',double(me),'ql',ql,'mel',double(mel),'bf',double(bf),'bfl',double(bfl), ...
124 'nq',nq, ...
125 'nme',nme, ...
126 'nbf',nbf, ...
127 'volumes',ComputeVolumesOpt(me,q),...
128 'abf',FaceArea(bf,q));
129 end
130
131 % Read gmsh meshes
132 function Th=GetGmshMesh(cFileName)
133 msh=load_gmsh(cFileName)
134 Th.nq=msh.nbNod;
135 Th.q=msh.POS';
136 Th.me=msh.TETS(1:msh.nbTets,1:4)';
137 Th.nme=msh.nbTets
138 Th.mel=msh.TETS(1:msh.nbTets,5);
139 Th.volumes=ComputeVolumesOpt(Th.me,Th.q);
140 end
141
142 % Read medit meshes
143 function Mesh=GetMeditMesh(cFileName)
144 [fid,message]=fopen(cFileName,'r');
145 if ( fid == -1 )
146 error([message,' : ',cFileName]);
147 end
148 if isOctave()
149 % Read Vertices
150 tline='';
151 while ~strcmp(strtrim(tline),'Vertices')
152 tline = fgetl(fid);
153 if (tline ==-1)
154 error('Error : Vertices not found');
155 end
156 end
157 nq=fscanf(fid,'%d',1);
158 R=fscanf(fid,'%f %f %f %d',[4,nq]);
159 q=R([1 2 3],:);
160 ql=R(4,:);
161
162 % Read Triangles
163 while ~strcmp(strtrim(tline),'Triangles')
164 tline = fgetl(fid);
165 if (tline ==-1)
166 error('Error : Triangles not found');
167 end
168 end
169 nbf=fscanf(fid,'%d',1);
170 R=fscanf(fid,'%d %d %d %d',[4,nbf]);
171 bf=R([1 2 3],:);
172 bfl=R(4,:);
173
174 % Read Tetrahedra
175 tline='';
176 while ~strcmp(strtrim(tline),'Tetrahedra')
177 tline = fgetl(fid);
178 if (tline ==-1)
179 error('Error : Tetrahedra not found');
180 end
181 end
182 nme=fscanf(fid,'%d',1);
183 R=fscanf(fid,'%d %d %d %d %d',[5,nme]);
184 me=R([1:4],:);
185 mel=R(5,:);
186 else % Matlab
187 tline='';
188 while ~strcmp(strtrim(tline),'Vertices')
189 tline = fgetl(fid);
190 end
191 nq=fscanf(fid,'%d',1);
192
193 R=textscan(fid,'%f %f %f %d',nq);
194 q=[R{1},R{2},R{3}]';
195 ql=R{4}';
196 tline='';
197 while ~strcmp(strtrim(tline),'Triangles')
198 tline = fgetl(fid);
199 end
200 nbf=fscanf(fid,'%d',1);
201
202 R=textscan(fid,'%d %d %d %d',nbf);
203 bf=[R{1},R{2},R{3}]';
204 bfl=R{4}';
205 tline='';
206 while ~strcmp(strtrim(tline),'Tetrahedra')
207 tline = fgetl(fid);
208 end
209 % Apres Tetrahedra
210 nme=fscanf(fid,'%ld',1);
211
212 R=textscan(fid,'%d %d %d %d %d',nme);
213 me=[R{1},R{2},R{3},R{4}]';
214 mel=R{5}';
215 end
216 fclose(fid);
217 Mesh=struct('q',q,'me',double(me),'ql',ql,'mel',double(mel),'bf',double(bf),'bfl',double(bfl), ...
218 'nq',nq, ...
219 'nme',nme, ...
220 'nbf',nbf, ...
221 'volumes',ComputeVolumesOpt(me,q),...
222 'abf',FaceArea(bf,q));
223 end