Vous êtes sur la page 1sur 2

Elementary 2D Grid Generation Software

For work with domains in two or three dimensions it is very helpful, indeed essential for complex
geometries, to have software that generates the grid of elements automatically after, say, the domain
geometry and maximum element dimensions are input. Although we will consider only elementary
geometries, it is still helpful to generate a grid numerically. As an aid in this task, I will briefly
discuss the m-file gridgen.m which is you may use to generate grids on rectangular domains.
function [grd, connec]=gridgen(low_left, up_right, nx, ny, ind)
%
% FUNCTION [GRD, CONNEC]=GRIDGEN(LOW_LEFT, UP_RIGHT, NX, NY, IND)
%
% Creates a uniform grid on the rectangle with lower left corner
% low_left=[a,b] and upper right corner up_right=[c,d] using nx
% intervals along the x-axis and ny intervals on the y-axis. If ind=1
% the coordinates of nx*ny rectangular elements are returned in grd, and
% the corresponding connectivity matrix is returned in connec. If ind=2
% each rectangle is divided into two triangles and connec gives the
% connectivity matrix.
%
if ~((ind==1)|(ind==2))
error( ind must be 1 (rectangles) or 2 (triangles));
return;
end
ny1=ny+1; nx1=nx+1;
x=linspace(low_left(1),up_right(1),nx1);
y=linspace(low_left(2),up_right(2),ny1);
[xx,yy]=meshgrid(x,y); % arrays xx, yy are ny1 by nx1, xx has rows x, yy columns y
grd=zeros(ny1*nx1,2);
for row=1:ny1 %
for col=1:nx1
node_num=(row-1)*nx1+col;
grd(node_num,:)=[xx(row,col),yy(row,col)];
end
end
conq=zeros(nx*ny,4);
for row=1:ny % nodes for rectangular elements
for col=1:nx
nel=nx*(row-1)+col;
n0=nx1*(row-1)+col; % node at lower left of element nel
conq(nel,1:4)=[n0,n0+1,n0+1+nx1,n0+nx1]; % counter clockwise numbering
end
end
if ind==1
connec=conq;
else
for kk=1:nx*ny % divide each of the nx*ny rectangles into 2 triangles
cont(2*kk-1,1:3)=[conq(kk,4),conq(kk,1),conq(kk,3)]; % local nodes 4,1,3
cont(2*kk,1:3)=[conq(kk,2), conq(kk,3), conq(kk,1)]; % local nodes 2,3,1
end
connec=cont;
end
The grids generated by this program can be plotted if desired using the file

function gridplot(nx,ny,grd,connec,ind)
close all
nx1=nx+1; ny1=ny+1;
x=grd(1:nx1,1); y=grd(1:nx1:nx1*ny1-nx1+1,2);
hx=x(2)-x(1); hy=y(2)-y(1); % grid spacings
hold on
axis([-hx+x(1), x(nx1)+hx, -2*hy+y(1), y(ny1)+hy]);
for kk=1:ny1 plot(x,y(kk)*ones(1,nx1)); end % plot horizontal grid lines
for kk=1:nx1 plot(x(kk)*ones(1,ny1),y); end % plot vertical grid lines
ofx=0.1*hx; ofy=0.2*hy; % offsets
for kk=1:nx1*ny1
str=sprintf(%d,kk);
text(grd(kk,1)+ofx,grd(kk,2)+ofy,str,Color,[1,0,0],FontSize,8);
end
if ind==2 % complete triangles
for kk=1:2:length(connec(:,1))
x1=[grd(connec(kk,2),1),grd(connec(kk,3),1)];
y1=[grd(connec(kk,2),2),grd(connec(kk,3),2)];
plot(x1,y1,b-);
end
end
if ind==1
for kk=1:length(connec(:,1))
xg=grd(connec(kk,1),1)+hx/2; yg=grd(connec(kk,1),2)+hy/2;
str=sprintf(%d,kk);
text(xg,yg,str);
end
else
for kk=1:2:length(connec(:,1))
xg=grd(connec(kk,1),1)+hx/3; yg=grd(connec(kk,1),2)-hy/3;
str=sprintf(%d,kk);
text(xg,yg,str,FontSize,8);
end
for kk=2:2:length(connec(:,1))
xg=grd(connec(kk,3),1)+2*hx/3; yg=grd(connec(kk,3),2)+hy/3;
str=sprintf(%d,kk);
text(xg,yg,str,FontSize,8);
end
end
text(x(1),y(1)-2*hy/3,element numbers,FontSize,8);
text(x(1),y(1)-4*hy/3,node numbers,Color,[1,0,0],FontSize,8);
hold off
Further discussion of grid generation is beyond the scope of our course. It has become a computational field itself with an extensive literature some of which may be found in our library.