Home > utilities > write_SMS_2dm.m

write_SMS_2dm

PURPOSE ^

Output an SMS 2dm ASCII file from the triangulation given by tri, x and

SYNOPSIS ^

function write_SMS_2dm(file, tri, x, y, z, bnd)

DESCRIPTION ^

 Output an SMS 2dm ASCII file from the triangulation given by tri, x and
 y.

 write_SMS_2dm('file.2dm', tri, x, y)
 
 DESCRIPTION:
   Create an ASCII file in the SMS 2dm format of the triangulation in tri,
   x and y.
 
 INPUT:
   file - file name to save to.
   tri  - triangulation matrix of the nodes in x and y.
   x, y - coordinate pairs for the unstructured grid.
   z    - array of matching length with x and y of depth values.
   bnd  - [optional] cell array of open boundary node ids to create node
          strings in SMS.
 
 OUTPUT:
   file - ASCII file in SMS 2dm format.
 
 EXAMPLE USAGE:
   write_SMS_2dm('/tmp/test.2dm', Mobj.tri, Mobj.x, Mobj.y)
 
 Author(s):
   Pierre Cazenave (Plymouth Marine Laboratory)
 
 Revision history:
   2013-03-11 First version.
   2017-01-27 Change the arguments to use a z value too.
   2017-04-11 Update the help to be more useful.
 
==========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function write_SMS_2dm(file, tri, x, y, z, bnd)
0002 % Output an SMS 2dm ASCII file from the triangulation given by tri, x and
0003 % y.
0004 %
0005 % write_SMS_2dm('file.2dm', tri, x, y)
0006 %
0007 % DESCRIPTION:
0008 %   Create an ASCII file in the SMS 2dm format of the triangulation in tri,
0009 %   x and y.
0010 %
0011 % INPUT:
0012 %   file - file name to save to.
0013 %   tri  - triangulation matrix of the nodes in x and y.
0014 %   x, y - coordinate pairs for the unstructured grid.
0015 %   z    - array of matching length with x and y of depth values.
0016 %   bnd  - [optional] cell array of open boundary node ids to create node
0017 %          strings in SMS.
0018 %
0019 % OUTPUT:
0020 %   file - ASCII file in SMS 2dm format.
0021 %
0022 % EXAMPLE USAGE:
0023 %   write_SMS_2dm('/tmp/test.2dm', Mobj.tri, Mobj.x, Mobj.y)
0024 %
0025 % Author(s):
0026 %   Pierre Cazenave (Plymouth Marine Laboratory)
0027 %
0028 % Revision history:
0029 %   2013-03-11 First version.
0030 %   2017-01-27 Change the arguments to use a z value too.
0031 %   2017-04-11 Update the help to be more useful.
0032 %
0033 %==========================================================================
0034 
0035 global ftbverbose
0036 [~, subname] = fileparts(mfilename('fullpath'));
0037 if ftbverbose
0038     fprintf('\nbegin : %s\n', subname)
0039 end
0040 
0041 f = fopen(file, 'w');
0042 if f < 0
0043     error('Unable to open output file (check permissions?)')
0044 end
0045 
0046 if length(x) ~= length(y)
0047     error('Input coordinate array lengths do not match.')
0048 else
0049     nn = length(x);
0050 end
0051 
0052 nt = size(tri, 1);
0053 
0054 % Header
0055 fprintf(f, 'MESH2D\nMESHNAME "mesh"\n');
0056 
0057 % Add the connectivity table
0058 for t = 1:nt
0059     fprintf(f, 'E3T %i %i %i %i 1\n', t, tri(t, :));
0060 end
0061 
0062 % Add the list of nodes
0063 for n = 1:nn
0064     fprintf(f, 'ND %i %.8e %.8e %.8e\n', n, x(n), y(n), z(n));
0065 end
0066 
0067 % Check we've got some open boundaries and create the relevant node string
0068 % output.
0069 if nargin == 6
0070     for b = 1:length(bnd)
0071         c = 0; % counter for the weird nodestring format.
0072 
0073         nodestring = bnd{b}; % current open boundary nodestring indices.
0074         for ns = 1:length(nodestring)
0075             % If we're at the end of the nodestring, create a negative node
0076             % ID.
0077             if ns == length(nodestring)
0078                 node_id = -nodestring(ns);
0079             else
0080                 node_id = nodestring(ns);
0081             end
0082 
0083             % Increment the counter.
0084             c = c + 1;
0085             if c == 1
0086                 % Add the nodestring line prefix and the current node ID.
0087                 fprintf(f, 'NS %i ', node_id);
0088             elseif c > 0 && c < 10
0089                 fprintf(f, '%i ', node_id);
0090             elseif c >= 10 || ns == length(nodestring)
0091                 fprintf(f, '%i\n', node_id);
0092                 c = 0;
0093             end
0094         end
0095         % Add a newline at the end of the current nodestring.
0096         fprintf(f, '\n');
0097     end
0098 end
0099 
0100 % Dump all the (apparently ignored) footer information.
0101 fprintf(f, 'BEGPARAMDEF\nGM  "Mesh"\nSI  0\nDY  0\nTU  ""\nTD  0  0\nNUME  3\nBCPGC  0\nBEDISP  0 0 0 0 1 0 1 0 0 0 0 1\nBEFONT  0 2\nBEDISP  1 0 0 0 1 0 1 0 0 0 0 1\nBEFONT  1 2\nBEDISP  2 0 0 0 1 0 1 0 0 0 0 1\nBEFONT  2 2\nENDPARAMDEF\nBEG2DMBC\nMAT  1 "material 01"\nEND2DMBC\n');
0102 
0103 fclose(f);
0104 
0105 if ftbverbose
0106     fprintf('end   : %s\n', subname)
0107 end

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