Home > eidors > meshing > gmsh > gmsh_read_mesh.m

gmsh_read_mesh

PURPOSE ^

[srf,vtx,fc,bc,simp,edg,mat_ind,phys_names] = gmsh_read_mesh(filename)

SYNOPSIS ^

function[srf,vtx,fc,bc,simp,edg,mat_ind,phys_names] = gmsh_read_mesh(filename)

DESCRIPTION ^

[srf,vtx,fc,bc,simp,edg,mat_ind,phys_names] = gmsh_read_mesh(filename)
 Function to read in a mesh model from Gmsh and saves it in
 five arrays; surface (srf), veritices (vtx), face no. (fc)
 volume (simp) and edges (edg)

 srf        = The surfaces indices into vtx
 simp       = The volume indices into vtx
 vtx        = The vertices matrix
 fc         = A one column matrix containing the face numbers
 edg        = Edge segment information
 filename   = Name of file containing NetGen .vol information
 mat_ind    = Material index
 phys_names = Structure of "Physical" entities in the mesh
              .dim   = dimension
              .name  = name (string)
              .tag   = physical tag
              .nodes = N-x-dim array of indices into vtx

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function[srf,vtx,fc,bc,simp,edg,mat_ind,phys_names] = gmsh_read_mesh(filename)
0002 %[srf,vtx,fc,bc,simp,edg,mat_ind,phys_names] = gmsh_read_mesh(filename)
0003 % Function to read in a mesh model from Gmsh and saves it in
0004 % five arrays; surface (srf), veritices (vtx), face no. (fc)
0005 % volume (simp) and edges (edg)
0006 %
0007 % srf        = The surfaces indices into vtx
0008 % simp       = The volume indices into vtx
0009 % vtx        = The vertices matrix
0010 % fc         = A one column matrix containing the face numbers
0011 % edg        = Edge segment information
0012 % filename   = Name of file containing NetGen .vol information
0013 % mat_ind    = Material index
0014 % phys_names = Structure of "Physical" entities in the mesh
0015 %              .dim   = dimension
0016 %              .name  = name (string)
0017 %              .tag   = physical tag
0018 %              .nodes = N-x-dim array of indices into vtx
0019 
0020 % $Id: gmsh_read_mesh.m 3260 2012-06-30 14:40:10Z bgrychtol $
0021 % (C) 2009 Bartosz Sawicki. Licensed under GPL V2
0022 % Modified by James Snyder <jbsnyder@fanplastic.org>
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 % Select 2d vs 3d model by checking if Z is all the same
0051 if length( unique( nodes(:,4) ) ) > 1 
0052     vtx = nodes(:,2:4);
0053     % Type 2: 3-node triangle
0054     tri = find(arrayfun(@(x)x.type==2,elements));
0055     % Type 4: 4-node tetrahedron
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 % Line Format:
0073 % node-number x-coord y-coord z-coord
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 % Line Format:
0081 % physical-dimension physical-number "physical-name"
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 % Line Format:
0102 % elm-number elm-type number-of-tags < tag > ... node-number-list
0103 tline = fgetl(fid);
0104 n_rows = sscanf(tline,'%d');
0105 % elements = struct('simp',{},'phys_tag',{},'geom_tag',{});
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 %     nsz = size(elements,2)+1;
0115     elements(i).simp = n(n(3) + 4:end);
0116     %
0117     elements(i).type = n(2);
0118     if n(3) > 0 % get tags if they exist
0119         % By default, first tag is number of parent physical entity
0120         % second is parent elementary geometrical entity
0121         % third is number of parent mesh partitions followed by
0122         % partition ids
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

Generated on Fri 01-Jun-2018 15:59:55 by m2html © 2005