


function gen_unstructured_mesh(lx,ly,res,cornermod)
DESCRIPTION:
Generate an unstructured mesh in a rectangular domain of size dist_x*dist_y
having a resolution of res.
INPUT
lx : domain length in x-direction
ly : domain length in y-direction
res : target triangle edge length in same units as lx,ly
cornermod : ensure no cell shares a boundary on two of the rectangles edges
OUTPUT:
Preprocessing toolbox mesh object
EXAMPLE USAGE
gen_unstructured_mesh(1000,100,10,false);
Author(s):
Geoff Cowles (University of Massachusetts Dartmouth)
TODO
Implement the edgeswap to satisfy cornermod=true
Dependency - Matlab mesh2d - at least v24
http://www.mathworks.com/matlabcentral/fileexchange/25555-mesh2d-automatic-mesh-generation
==============================================================================

0001 function [Mobj] = gen_unstructured_mesh(lx,ly,res,cornermod) 0002 0003 % function gen_unstructured_mesh(lx,ly,res,cornermod) 0004 % 0005 % DESCRIPTION: 0006 % Generate an unstructured mesh in a rectangular domain of size dist_x*dist_y 0007 % having a resolution of res. 0008 % 0009 % INPUT 0010 % lx : domain length in x-direction 0011 % ly : domain length in y-direction 0012 % res : target triangle edge length in same units as lx,ly 0013 % cornermod : ensure no cell shares a boundary on two of the rectangles edges 0014 % 0015 % 0016 % OUTPUT: 0017 % Preprocessing toolbox mesh object 0018 % 0019 % EXAMPLE USAGE 0020 % gen_unstructured_mesh(1000,100,10,false); 0021 % 0022 % Author(s): 0023 % Geoff Cowles (University of Massachusetts Dartmouth) 0024 % 0025 % TODO 0026 % Implement the edgeswap to satisfy cornermod=true 0027 % 0028 % 0029 % Dependency - Matlab mesh2d - at least v24 0030 % http://www.mathworks.com/matlabcentral/fileexchange/25555-mesh2d-automatic-mesh-generation 0031 % 0032 %============================================================================== 0033 0034 subname = 'gen_unstructured_mesh'; 0035 fprintf('\n') 0036 fprintf(['begin : ' subname '\n']) 0037 0038 userproject = false; 0039 0040 % test vals 0041 % lx = 1000; 0042 % ly = 500; 0043 % cornermod = true; 0044 % res = 50; 0045 % close all; warning off; 0046 0047 % generate mesh 0048 node = [0 0; lx 0; lx ly ; 0 ly]; 0049 edge = [1 2 ; 2 3 ; 3 4; 4 1]; 0050 hdata.hmax = res; 0051 options.output = true; 0052 [p,t] = mesh2d(node,edge,hdata,options); 0053 0054 % generate connectivity 0055 [e,te,et2,bnd] = connectivity(p,t); 0056 0057 0058 % option to eliminate corner elements TODO 0059 % if(cornermod) 0060 % 0061 % for pt=1:1 0062 % xpt = node(pt,1); ypt = node(pt,2); 0063 % dist = sqrt( (xpt-p(:,1)).^2 + (ypt-p(:,2)).^2); 0064 % [imin,ipt] = min(dist); 0065 % end; 0066 % 0067 % 0068 % end; 0069 0070 %------------------------------------------------------------------------------ 0071 % Create a blank mesh object 0072 %------------------------------------------------------------------------------ 0073 Mobj = make_blank_mesh(); 0074 coordinate = 'cartesian'; 0075 0076 0077 0078 0079 % ensure all elements are CCW 0080 area = triarea(p,t); 0081 fprintf('min area %f max area %f\n',min(area),max(area)); 0082 0083 %------------------------------------------------------------------------------ 0084 % Transfer to Mesh structure 0085 %------------------------------------------------------------------------------ 0086 0087 % set dimensions 0088 nElems = numel(t(:,1)); 0089 nVerts = numel(p(:,1)); 0090 fprintf('# of elements in the mesh %d\n',nElems); 0091 fprintf('# of verts in the mesh %d\n',nVerts); 0092 0093 % allocate memory to hold mesh and auxiliary structures 0094 tri = zeros(nElems,3); 0095 x = zeros(nVerts,1); 0096 y = zeros(nVerts,1); 0097 h = zeros(nVerts,1); 0098 lon = zeros(nVerts,1); 0099 lat = zeros(nVerts,1); 0100 ts = zeros(nVerts,1); 0101 0102 0103 Mobj.nVerts = nVerts; 0104 Mobj.nElems = nElems; 0105 Mobj.nativeCoords = coordinate; 0106 0107 Mobj.have_lonlat = false; 0108 Mobj.have_xy = true; 0109 Mobj.have_bath = false; 0110 0111 Mobj.x = p(:,1); 0112 Mobj.y = p(:,2); 0113 Mobj.ts = ts; 0114 Mobj.lon = lon; 0115 Mobj.lat = lat; 0116 Mobj.h = h; 0117 Mobj.tri = t; 0118 0119 0120 fprintf(['end : ' subname '\n']) 0121 %write_dtank('junk.dtascii',Mobj); 0122 0123