Google
Grid Generation: Unstructured 2D Triangular Mesh Matlab

Tuesday, November 07, 2006

Unstructured 2D Triangular Mesh Matlab

Hello all,


For most of the Numerical simulations Unstructured Mesh are most favourable ones. Especially for Finite Element methods triangular delaunay meshes are used quite a lot. Today I am going to descibe about unstructured Mesh generation in MATLAB.

A typical Unstructured mesh in 2D looks like figure 1.

Figure 1 Unstructured Mesh


Triangular meshs can also be structured triangular with triangulation in some particular direction as shown in figure 2 and figure 3.





The Unstructured mesh shown in Figure 1 is basically made by randomly perturbing the nodes of a unifrom triangular mesh and it look very much like a delaunay mesh. Figure 4 shows a unstructured triangular delaunay mesh.



Now comming to How to generate shuch kind of meshes. I will here give a simple example of generating regular structured triangular mesh.

The code below is used to create the above triangular structured mesh

x=[0:1/(numx):1];

y=[0:1/(numy):1]; %Matlab's meshgrid is used to create 2D grid from specified divisons above
[X,Y] = meshgrid(x,y);


X1=reshape(X',length(x)*length(y),1);

Y1=reshape(Y',length(x)*length(y),1); %Coordinates of the node

node=[X1 Y1]; % Node
tri = triangulate(X1,Y1,element); % element connectivity

nele = size(tri,1);

Z= zeros(length(y)-2,length(x)-2);

nn = tri; % nodes in a long list

xx = X1(nn); yy = Y1(nn); % coordinates corresponding to nodes

xplot = reshape(xx,size(tri));

yplot = reshape(yy,size(tri));figure(1);

clf;

fill(xplot',yplot','w');

title(['Triangular Mesh ', num2str(length(nn)),' elements'])

%%%%%%Subfunction % triangulate



function tri = triangulate(xnod,ynod,nodes)
%
% tri = triangulate(xnod,ynod,nodes)
%
% triangulate the quadrilateral mesh
%

nele = size(nodes,1);
tri = zeros(3,2*nele)';
iv = [];
ii1 = [2 3 1];
jj1 = [4 1 3];
ii2 = [1 2 4];
jj2 = [2 3 4];
nrtri = 0;
for iel = 1:nele
iv = nodes(iel,:);
d1 = norm([xnod(iv(1))-xnod(iv(3));ynod(iv(1))-ynod(iv(3))]);
d2 = norm([xnod(iv(2))-xnod(iv(4));ynod(iv(2))-ynod(iv(4))]);
if d1 <= d2
nrtri = nrtri+1;
tri(nrtri,:) = iv(ii1);
nrtri = nrtri+1;
tri(nrtri,:) = iv(jj1);
else
nrtri = nrtri+1;
tri(nrtri,:) = iv(ii2);
nrtri = nrtri+1;
tri(nrtri,:) = iv(jj2);
end
end


This code will enable any one to generate a regular triangular mesh and different changes can be made to this code to get other kind of Triangular meshes as shown in figure 1 -4

16 comments:

Siamak said...

Very nice grid generation blog

-Siamak
http://www.pitchup.com

G_G said...

Are you folks still active? I'm looking for a simple mesh generation software that will convert a drawing into a mesh. Please advise if it needs to be developed. Thanks.

kiran said...

do u guys have any codes for grid generation in fortran..can u upload them if u have..it wud be very helpful

mohaj said...

Hi...
please upload a source code with Fortran or visual compaq. 2D unstructured grid gen.
very pleased to see this blog...
ali.mhj@gmail.com

Shlong said...

Hi, fine blog!

But i have a problem with the line
tri = triangulate(X1,Y1,element); % element connectivity
...

Where do i get the element connectivity and what is it ?

Maybe could someone help me ?

LEDS said...

HI!
I liked your site!
Can you help me to get what I want?
I would like to know how to create a 3D pyramid with the mesh command...

Can you help me?

thanks

My mail box
o_criativo@yahoo.com.br

Dulip said...

I wonder if anybody got this code working. Appreciate if somebody can explain me the way to run it also.

chady said...
This comment has been removed by the author.
rtfgvb768 said...

IS VERY GOOD..............................

Margarida said...

Hi!

Could someone help me? I'm not understanding this:
tri = triangulate(X1,Y1,element); % element connectivity

Thanks:)

chady said...

Hi Margarida, this is very easy :

X1 is the vector which contain the x coordinates of your nodes in the mesh you create. The position (line number) in this vector of each coordinate will be the number of the node.

Y1 is the same but for the y coordinates

element is the finite element connectivity table, which is a matrix of 3 vectors, so each line contains 3 numbers, which are the number of the nodes of the triangle.

Hope I was clear,
Regards

Sholihun said...
This comment has been removed by the author.
Sholihun said...

Dear Chady,

I have not understood yet how mus I do to fill "element"
Could you please give me an example to fill "element"...
thanks before..

Mia Hart said...

Great post. I have been wondering about the mesh generation. Do you happen to have any recommendations? Please let me know, thanks.

chady said...

Check GMSH, it is a free mesh generation software and the best I have seen, however the interface is not very user friendly. If you want to read something search for Delaunay mesh.
Welcome

Harshal Wagh said...

please tell me how to recall that function in above given code and if u have it please post that function file
harshbwagh@gmail.com