write_SMS_2dm.m 3.07 KB
Newer Older
1
function write_SMS_2dm(file, tri, x, y, z, bnd)
2 3 4
% Output an SMS 2dm ASCII file from the triangulation given by tri, x and
% y.
%
Pierre Cazenave's avatar
Pierre Cazenave committed
5
% write_SMS_2dm('file.2dm', tri, x, y)
6 7 8 9 10 11 12 13 14
% 
% 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.
15
%   z    - array of matching length with x and y of depth values.
16 17 18 19 20 21 22
%   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:
23
%   write_SMS_2dm('/tmp/test.2dm', Mobj.tri, Mobj.x, Mobj.y)
24 25 26 27 28 29
% 
% Author(s):
%   Pierre Cazenave (Plymouth Marine Laboratory)
% 
% Revision history:
%   2013-03-11 First version.
30
%   2017-01-27 Change the arguments to use a z value too.
Pierre Cazenave's avatar
Pierre Cazenave committed
31
%   2017-04-11 Update the help to be more useful.
32 33 34 35
% 
%==========================================================================

global ftbverbose
Pierre Cazenave's avatar
Pierre Cazenave committed
36
[~, subname] = fileparts(mfilename('fullpath'));
37
if ftbverbose
Pierre Cazenave's avatar
Pierre Cazenave committed
38
    fprintf('\nbegin : %s\n', subname)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
end

f = fopen(file, 'w');
if f < 0
    error('Unable to open output file (check permissions?)')
end

if length(x) ~= length(y)
    error('Input coordinate array lengths do not match.')
else
    nn = length(x);
end

nt = size(tri, 1);

% Header
55
fprintf(f, 'MESH2D\nMESHNAME "mesh"\n');
56 57 58 59 60 61 62 63

% Add the connectivity table
for t = 1:nt
    fprintf(f, 'E3T %i %i %i %i 1\n', t, tri(t, :));
end

% Add the list of nodes
for n = 1:nn
64
    fprintf(f, 'ND %i %.8e %.8e %.8e\n', n, x(n), y(n), z(n));
65 66 67 68
end

% Check we've got some open boundaries and create the relevant node string
% output.
69
if nargin == 6
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    for b = 1:length(bnd)
        c = 0; % counter for the weird nodestring format.

        nodestring = bnd{b}; % current open boundary nodestring indices.
        for ns = 1:length(nodestring)
            % If we're at the end of the nodestring, create a negative node
            % ID. 
            if ns == length(nodestring)
                node_id = -nodestring(ns);
            else
                node_id = nodestring(ns);
            end

            % Increment the counter.
            c = c + 1;
            if c == 1
86 87 88
                % Add the nodestring line prefix and the current node ID.
                fprintf(f, 'NS %i ', node_id);
            elseif c > 0 && c < 10
89
                fprintf(f, '%i ', node_id);
90
            elseif c >= 10 || ns == length(nodestring)
91 92 93 94 95 96 97 98 99
                fprintf(f, '%i\n', node_id);
                c = 0;
            end
        end
        % Add a newline at the end of the current nodestring.
        fprintf(f, '\n');
    end
end

100
% Dump all the (apparently ignored) footer information.
101 102 103 104 105 106 107
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');

fclose(f);

if ftbverbose
    fprintf('end   : %s\n', subname)
end