Debian Codegraph @

packages/intrepid/matlab/intrelab/mesh/RectGrid.m

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