me471s03_lec06

Upload: hasen-bebba

Post on 01-Jun-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 me471s03_lec06

    1/2

    Elementary 2D Grid Generation Software

    For work with domains in two or three dimensions it is very helpful, indeed essential for complexgeometries, to have software that generates the grid of elements automatically after, say, the domaingeometry and maximum element dimensions are input. Although we will consider only elementarygeometries, it is still helpful to generate a grid numerically. As an aid in this task, I will brieydiscuss the m-le 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;

    endny1=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 ygrd=zeros(ny1*nx1,2);for row=1:ny1 %

    for col=1:nx1node_num=(row-1)*nx1+col;grd(node_num,:)=[xx(row,col),yy(row,col)];

    endendconq=zeros(nx*ny,4);for row=1:ny % nodes for rectangular elements

    for col=1:nxnel=nx*(row-1)+col;n0=nx1*(row-1)+col; % node at lower left of element nelconq(nel,1:4)=[n0,n0+1,n0+1+nx1,n0+nx1]; % counter clockwise numbering

    endendif ind==1

    connec=conq;else

    for kk=1:nx*ny % divide each of the nx*ny rectangles into 2 trianglescont(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,1endconnec=cont;

    end

    The grids generated by this program can be plotted if desired using the le

  • 8/9/2019 me471s03_lec06

    2/2

    function gridplot(nx,ny,grd,connec,ind)close allnx1=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 spacingshold onaxis([-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 linesfor kk=1:nx1 plot(x(kk)*ones(1,ny1),y); end % plot vertical grid linesofx=0.1*hx; ofy=0.2*hy; % offsetsfor kk=1:nx1*ny1

    str=sprintf(’%d’,kk);text(grd(kk,1)+ofx,grd(kk,2)+ofy,str,’Color’,[1,0,0],’FontSize’,8);

    endif 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

    endif ind==1for 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);

    endelse

    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);

    endendtext(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 computa-tional eld itself with an extensive literature – some of which may be found in our library.