Home > fvcom_prepro > write_admesh_mesh.m

write_admesh_mesh

PURPOSE ^

writes admesh mesh files from a Mobj matlab structure variable

SYNOPSIS ^

function write_admesh_mesh(Mobj,varargin)

DESCRIPTION ^

 writes admesh mesh files from a Mobj matlab structure variable

 function write_admesh_mesh(Mobj)

 DESCRIPTION:
    writes admesh 14 file.

 INPUT
   Mobj                   = needs bathymetry, nodes and triangulation
   table. read_sms_mesh provides everything it needs.
   [optional] output_directory       = directory to write mesh.14 file
   [optional] filename       = filename to use instead of default mesh(.14). Don't provide extension 
   [optional] native_coord = cartesian or spherical. Assumes cartesian if
   omitted.

 OUTPUT:
    ADMESH compatible .14 file.

 EXAMPLE USAGE
    write_admesh_mesh(Mobj)

 Author(s):
    Ricardo Torres (Plymouth Marine Laboratory) based on read_gmsh_mesh

 Revision history

   2016-06-22 First version.
   2017-06-06 Removed returned Mobj as it is in no way changed during this
   function. Also set default coordinate type to cartesian unless
   overridden with 'native_coord'.

==============================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_admesh_mesh(Mobj,varargin)
0002 % writes admesh mesh files from a Mobj matlab structure variable
0003 %
0004 % function write_admesh_mesh(Mobj)
0005 %
0006 % DESCRIPTION:
0007 %    writes admesh 14 file.
0008 %
0009 % INPUT
0010 %   Mobj                   = needs bathymetry, nodes and triangulation
0011 %   table. read_sms_mesh provides everything it needs.
0012 %   [optional] output_directory       = directory to write mesh.14 file
0013 %   [optional] filename       = filename to use instead of default mesh(.14). Don't provide extension
0014 %   [optional] native_coord = cartesian or spherical. Assumes cartesian if
0015 %   omitted.
0016 %
0017 % OUTPUT:
0018 %    ADMESH compatible .14 file.
0019 %
0020 % EXAMPLE USAGE
0021 %    write_admesh_mesh(Mobj)
0022 %
0023 % Author(s):
0024 %    Ricardo Torres (Plymouth Marine Laboratory) based on read_gmsh_mesh
0025 %
0026 % Revision history
0027 %
0028 %   2016-06-22 First version.
0029 %   2017-06-06 Removed returned Mobj as it is in no way changed during this
0030 %   function. Also set default coordinate type to cartesian unless
0031 %   overridden with 'native_coord'.
0032 %
0033 %==============================================================================
0034 
0035 [~, subname] = fileparts(mfilename('fullpath'));
0036 global ftbverbose
0037 if ftbverbose
0038     fprintf('\nbegin : %s \n', subname)
0039 end
0040 
0041 %--------------------------------------------------------------------------
0042 % Parse input arguments
0043 %--------------------------------------------------------------------------
0044 
0045 assert(mod(length(varargin), 2) == 0, 'incorrect usage of write_admesh_mesh, use keyword pairs')
0046 
0047 % Assume we have nothing sensible.
0048 out_dir = pwd; % default is to write file to current directory
0049 native_coord = 'cartesian';
0050 have_xy = true;
0051 have_lonlat = false;
0052 filename = 'mesh'
0053 for i = 1:2:length(varargin) - 1
0054     keyword = lower(varargin{i});
0055     
0056     assert(ischar(keyword), 'incorrect usage of write_admesh_mesh')
0057     
0058     switch keyword
0059         case 'output_directory'
0060             out_dir = varargin{i + 1};
0061         case 'filename'
0062             filename = varargin{i + 1};
0063         case 'native_coord'
0064             coord = varargin{i + 1};
0065             if strcmpi(coord, 'spherical')
0066                 native_coord = 'spherical';
0067                 have_lonlat = true;
0068                 have_xy = false;
0069             elseif strcmpi(coord, 'cartesian')
0070                 native_coord = 'cartesian';
0071                 have_xy = true;
0072                 have_lonlat = false;
0073             else
0074                 warning('Unrecognised coordinate system (%s). Valid values are ''spherical'' and ''cartesian''.', native_coord)
0075             end
0076         otherwise
0077             error('Can''t understand property: %s', varargin{i + 1});
0078             
0079     end
0080 end
0081 
0082 %--------------------------------------------------------------------------
0083 % Open the output file
0084 %--------------------------------------------------------------------------
0085 gmsh_msh = fullfile(out_dir,[filename '.14']);
0086 fid = fopen(gmsh_msh, 'wt');
0087 assert(fid >= 0, sprintf('file: %s could not be created\n',  gmsh_msh));
0088 
0089 % Count number of elements and vertices
0090 if ftbverbose
0091     fprintf('Writing to: %s\n', gmsh_msh)
0092 end
0093 
0094 % Read mesh type, written from srcatch:
0095 %
0096 % http://www.geuz.org/pipermail/gmsh/attachments/20071002/642cb6c3/attachment.m
0097 %
0098 title_str = 'Test admesh mesh starting from an sms mesh';
0099 
0100 % first line is title of mesh
0101 fprintf(fid,'%s\n',title_str);
0102 % next line is mesh dimensions
0103 fprintf(fid,'%u %u\n',Mobj.nElems,Mobj.nVerts);
0104 
0105 % Write the node positions and depths.
0106 if have_lonlat
0107     fprintf('Writing Spherical: %s\n', gmsh_msh)
0108     
0109     for nn=1:Mobj.nVerts
0110         fprintf(fid, '%d %f %f %f\n',nn,Mobj.lon(nn),Mobj.lat(nn),Mobj.h(nn));
0111     end
0112 elseif have_xy
0113     fprintf('Writing Cartesian: %s\n', gmsh_msh)
0114     
0115     for nn=1:Mobj.nVerts
0116         fprintf(fid, '%d %f %f %f\n',nn,Mobj.x(nn),Mobj.y(nn),Mobj.h(nn));
0117     end
0118 else
0119     error('Unsupported coordinate type. Give ''spherical'' or ''cartesian''')
0120 end
0121 
0122 % Now we should be at the end of the nodes and about to write the elements.
0123 
0124 % write the element triangulation table. Format is:
0125 %   ID, dim, n1, n2, n3
0126 for nn=1:Mobj.nElems
0127     fprintf(fid, '%5d %5d %7d %7d %7d\n',nn,3,Mobj.tri(nn,1),Mobj.tri(nn,2),Mobj.tri(nn,3));
0128 end
0129 %--------------------------------------------------------------------------
0130 % Write the nodestrings assuming for now that there is at least one
0131 %--------------------------------------------------------------------------
0132 fprintf(fid, '%d  = Number of open boundaries \n',length(Mobj.read_obc_nodes));
0133 
0134 % Mobj.read_obc_nodes would be different if read from admesh or sms mesh
0135 % files
0136 try
0137     TotOBCN = length(cat(1,Mobj.read_obc_nodes{:}));
0138 catch
0139     TotOBCN = length([Mobj.read_obc_nodes{:}]);
0140 end
0141 
0142 fprintf(fid, '%d  = Total number of open boundary nodes \n',TotOBCN);
0143 
0144 % read number of open boundary nodes for each boundary
0145 for nn=1:length(Mobj.read_obc_nodes)
0146     fprintf(fid, '%d  = Number of nodes for open boundary  %d \n',length(Mobj.read_obc_nodes{nn}),nn);
0147     for rr=1:length(Mobj.read_obc_nodes{nn})
0148         
0149         fprintf(fid, '%d \n',Mobj.read_obc_nodes{nn}(rr));
0150     end
0151 end
0152 fclose(fid);
0153 
0154 if ftbverbose
0155     fprintf('end   : %s\n', subname)
0156 end

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005