0001 function[srf,vtx,fc,bc,simp,edg,mat_ind,phys_names] = gmsh_read_mesh(filename)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024 fid = fopen(filename,'r');
0025 phys_names = [];
0026 while 1
0027 tline = fgetl(fid);
0028 if ~ischar(tline); fclose(fid); break; end
0029
0030 if strcmp(tline,'$Elements')
0031 elements= parse_elements( fid );
0032 elseif strcmp(tline,'$Nodes')
0033 nodes= get_lines_with_nodes( fid );
0034 elseif strcmp(tline,'$PhysicalNames')
0035 phys_names= parse_names( fid );
0036 end
0037 end
0038
0039 if ~isempty(phys_names)
0040 for i = 1:length(phys_names)
0041 tmpelements = find(arrayfun(@(x)x.phys_tag==phys_names(i).tag,elements));
0042 phys_names(i).nodes = cat(1,elements(tmpelements).simp);
0043 end
0044 end
0045
0046 edg = [];
0047 bc = [];
0048 mat_ind = [];
0049
0050
0051 if length( unique( nodes(:,4) ) ) > 1
0052 vtx = nodes(:,2:4);
0053
0054 tri = find(arrayfun(@(x)x.type==2,elements));
0055
0056 tet = find(arrayfun(@(x)x.type==4,elements));
0057 simp = cat(1,elements(tet).simp);
0058 srf = cat(1,elements(tri).simp);
0059 else
0060 vtx = nodes(:,2:3);
0061 tri = find(arrayfun(@(x)x.type==2,elements));
0062 simp = cat(1,elements(tri).simp);
0063 srf = [];
0064 end
0065
0066 elemtags = cat(1,elements.phys_tag);
0067 fc = elemtags(tri,1);
0068 end
0069
0070
0071 function mat = get_lines_with_nodes( fid )
0072
0073
0074 tline = fgetl(fid);
0075 n_rows = sscanf(tline,'%d');
0076 mat= fscanf(fid,'%f',[4,n_rows])';
0077 end
0078
0079 function names = parse_names( fid )
0080
0081
0082 tline = fgetl(fid);
0083 n_rows = sscanf(tline,'%d');
0084 names = struct('tag',{},'dim',{},'name',{});
0085 for i = 1:n_rows
0086 tline = fgetl(fid);
0087 if exist('OCTAVE_VERSION')
0088 parts = strsplit(tline,' ');
0089 else
0090 parts = regexp(tline,' ','split');
0091 end
0092 nsz = size(names,2)+1;
0093 names(nsz).dim = str2double( parts(1) );
0094 names(nsz).tag = str2double( parts(2) );
0095 tname = parts(3);
0096 names(nsz).name = strrep(tname{1},'"','');
0097 end
0098 end
0099
0100 function elements = parse_elements( fid )
0101
0102
0103 tline = fgetl(fid);
0104 n_rows = sscanf(tline,'%d');
0105
0106 elements(n_rows).simp = [];
0107 elements(n_rows).phys_tag = [];
0108 elements(n_rows).geom_tag = [];
0109 elements(n_rows).type = [];
0110
0111 for i = 1:n_rows
0112 tline = fgetl(fid);
0113 n = sscanf(tline, '%d')';
0114
0115 elements(i).simp = n(n(3) + 4:end);
0116
0117 elements(i).type = n(2);
0118 if n(3) > 0
0119
0120
0121
0122
0123 tags = n(4:3+n(3));
0124 if length(tags) >= 1
0125 elements(i).phys_tag = tags(1);
0126 if length(tags) >= 2
0127 elements(i).geom_tag = tags(2);
0128 end
0129 end
0130 end
0131 end
0132 end