| 1 |
|
| 2 |
function [mesh] = RectGrid(xmin, xmax, ymin, ymax, nx, ny) |
| 3 |
% |
| 4 |
% [mesh] = RectGrid(xmin, xmax, ymin, ymax, nx, ny) |
| 5 |
% |
| 6 |
%RECTGRID sets up the grid for piecewise linear elements |
| 7 |
% in a rectangular domain. |
| 8 |
% |
| 9 |
% The grid is constructed by subdividing the x-interval into |
| 10 |
% nx subintervals and the y-interval into ny subintervals. |
| 11 |
% This generates a grid with nx*ny rectangles. Each rectangle |
| 12 |
% is then subdivided into two triangles by cutting the rectangle |
| 13 |
% from bottom left to top right. |
| 14 |
% |
| 15 |
% |
| 16 |
% |
| 17 |
% Input |
| 18 |
% xmin, xmax size of the rectangle |
| 19 |
% ymin, ymax |
| 20 |
% nx number of subintervals on x-interval |
| 21 |
% ny number of subintervals on y-interval |
| 22 |
% |
| 23 |
% Output |
| 24 |
% mesh structure array with the following fields |
| 25 |
% |
| 26 |
% mesh.p Real nn x 2 |
| 27 |
% array containing the x- and y- coordinates |
| 28 |
% of the nodes |
| 29 |
% |
| 30 |
% mesh.t Integer nt x 3 |
| 31 |
% t(i,1:4) contains the indices of the vertices of |
| 32 |
% triangle i. |
| 33 |
% |
| 34 |
% mesh.e Integer nf x 3 |
| 35 |
% e(i,1:2) contains the indices of the vertices of |
| 36 |
% edge i. |
| 37 |
% edge(i,3) contains the boundary marker of edge i. |
| 38 |
% Currently set to one. |
| 39 |
% e(i,3) = 1 Dirichlet bdry conds are imposed on edge i |
| 40 |
% e(i,3) = 2 Neumann bdry conds are imposed on edge i |
| 41 |
% e(i,3) = 3 Robin bdry conds are imposed on edge i |
| 42 |
% |
| 43 |
% mesh.sidesPerCell Integer = 3 (triangle) |
| 44 |
% |
| 45 |
% mesh.elem_ss{i} = global element IDs on sideset (boundary) i |
| 46 |
% |
| 47 |
% mesh.side_ss{i} = local side (subcell) IDs on sideset (boundary) i |
| 48 |
% |
| 49 |
% mesh.cellType = string for cell type (= 'Triangle') |
| 50 |
% |
| 51 |
% mesh.sideType = string for side subcell type (= 'Line') |
| 52 |
% |
| 53 |
% |
| 54 |
% Vertical ordering: |
| 55 |
% The triangles are ordered column wise, for instance: |
| 56 |
% |
| 57 |
% 03 -------- 06 -------- 09 -------- 12 |
| 58 |
% | 4 / | 8 / | 12 / | |
| 59 |
% | / | / | / | |
| 60 |
% | / 3 | / 7 | / 11 | |
| 61 |
% 02 -------- 05 -------- 08 -------- 11 |
| 62 |
% | 2 / | 6 / | 10 / | |
| 63 |
% | / | / | / | |
| 64 |
% | / 1 | / 5 | / 9 | |
| 65 |
% 01 -------- 04 -------- 07 -------- 10 |
| 66 |
% |
| 67 |
% The vertices and midpoints in a triangle are numbered |
| 68 |
% counterclockwise, for example |
| 69 |
% triangle 7: (05, 08, 09) |
| 70 |
% triangle 8: (05, 09, 06) |
| 71 |
% |
| 72 |
% (Usually, the local grid.node numbering should not be important.) |
| 73 |
% |
| 74 |
% number of triangles: 2*nx*ny, |
| 75 |
% number of vertices: (nx+1)*(ny+1), |
| 76 |
% |
| 77 |
% AUTHORS: Matthias Heinkenschloss and Denis Ridzal |
| 78 |
% Department of Computational and Applied Mathematics |
| 79 |
% Rice University |
| 80 |
% November 23, 2005 |
| 81 |
% |
| 82 |
% Denis Ridzal |
| 83 |
% Sandia National Laboratories |
| 84 |
|
| 85 |
|
| 86 |
nt = 2*nx*ny; |
| 87 |
np = (nx+1)*(ny+1); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
% Create arrays |
| 93 |
mesh.t = zeros(nt,3); |
| 94 |
mesh.p = zeros(np,2); |
| 95 |
|
| 96 |
|
| 97 |
nxp1 = nx + 1; |
| 98 |
nyp1 = ny + 1; |
| 99 |
|
| 100 |
% Create triangles |
| 101 |
nt = 0; |
| 102 |
for ix = 1:nx |
| 103 |
for iy = 1:ny |
| 104 |
|
| 105 |
iv = (ix-1)*nyp1 + iy; |
| 106 |
iv1 = iv + nyp1; |
| 107 |
|
| 108 |
nt = nt + 1; |
| 109 |
mesh.t(nt,1) = iv; |
| 110 |
mesh.t(nt,2) = iv1; |
| 111 |
mesh.t(nt,3) = iv1+1; |
| 112 |
|
| 113 |
nt = nt+1; |
| 114 |
mesh.t(nt,1) = iv; |
| 115 |
mesh.t(nt,2) = iv1+1; |
| 116 |
mesh.t(nt,3) = iv+1; |
| 117 |
end |
| 118 |
end |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
% Create vertex coodinates |
| 123 |
|
| 124 |
hx = (xmax-xmin)/nx; |
| 125 |
hy = (ymax-ymin)/ny; |
| 126 |
x = xmin; |
| 127 |
|
| 128 |
for ix = 1:nx |
| 129 |
|
| 130 |
% set coordinates for vertices with fixed |
| 131 |
% x-coordinate at x |
| 132 |
i1 = (ix-1)*(ny+1)+1; |
| 133 |
i2 = ix*(ny+1); |
| 134 |
mesh.p(i1:i2,1) = x*ones(nyp1,1); |
| 135 |
mesh.p(i1:i2,2) = (ymin:hy:ymax)'; |
| 136 |
|
| 137 |
x = x + hx; |
| 138 |
end |
| 139 |
|
| 140 |
% set coordinates for vertices with fixed |
| 141 |
% x-coordinate at xmax |
| 142 |
i1 = nx*(ny+1)+1; |
| 143 |
i2 = (nx+1)*(ny+1); |
| 144 |
mesh.p(i1:i2,1) = xmax*ones(nyp1,1); |
| 145 |
mesh.p(i1:i2,2) = (ymin:hy:ymax)'; |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
% Set grid.edge (edges are numbered counter clock wise starting |
| 150 |
% at lower left end). |
| 151 |
|
| 152 |
mesh.e = ones(2*(nx+ny),3); |
| 153 |
|
| 154 |
% edges on left on left boundary |
| 155 |
mesh.e(1:ny,1) = (1:ny)'; |
| 156 |
mesh.e(1:ny,2) = (2:ny+1)'; |
| 157 |
|
| 158 |
% edges on top boundary |
| 159 |
mesh.e(ny+1:nx+ny,1) = (ny+1:ny+1:np-1)'; |
| 160 |
mesh.e(ny+1:nx+ny,2) = (2*(ny+1):ny+1:np)'; |
| 161 |
|
| 162 |
% edges on right boundary |
| 163 |
mesh.e(nx+ny+1:nx+2*ny,1) = (np-ny:np-1)'; |
| 164 |
mesh.e(nx+ny+1:nx+2*ny,2) = (np-ny+1:np)'; |
| 165 |
|
| 166 |
% edges on lower boundary |
| 167 |
mesh.e(nx+2*ny+1:2*(nx+ny),1) = (1:ny+1:np-2*ny-1)'; |
| 168 |
mesh.e(nx+2*ny+1:2*(nx+ny),2) = (ny+2:ny+1:np-ny)'; |
| 169 |
|
| 170 |
% sides per cell |
| 171 |
mesh.sidesPerCell = 3; |
| 172 |
|
| 173 |
% cell type |
| 174 |
mesh.cellType = 'Triangle'; |
| 175 |
|
| 176 |
% side type |
| 177 |
mesh.sideType = 'Line'; |
| 178 |
|
| 179 |
% sidesets |
| 180 |
xvec = [1:nx]'; |
| 181 |
yvec = [1:ny]'; |
| 182 |
mesh.elem_ss{1} = 2*ny*(xvec-1) + 1; % bottom |
| 183 |
mesh.elem_ss{2} = 2*ny*(nx-1) + 2*(yvec-1) + 1; % right |
| 184 |
mesh.elem_ss{3} = 2*ny*xvec; % top |
| 185 |
mesh.elem_ss{4} = 2*yvec; % left |
| 186 |
|
| 187 |
% local side ids for sidesets |
| 188 |
mesh.side_ss{1} = 0*ones(nx,1); % bottom |
| 189 |
mesh.side_ss{2} = 1*ones(ny,1); % right |
| 190 |
mesh.side_ss{3} = 1*ones(nx,1); % top |
| 191 |
mesh.side_ss{4} = 2*ones(ny,1); % left |
| 192 |
|