Matlab Code - 2D Structured and Unstructred Mesh
Matlab Code - 2D Structured and Unstructred Mesh
discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/278786548
CITATIONS
READS
88
1 author:
Mayur Pal
Maersk Oil
68 PUBLICATIONS 251 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
Finite Element Based Matlab Solver for Poission's Equation View project
GIAN - Course on Integrated Subsurface flow and solute transport in porous medium - Opportunities
and Challenges View project
All content following this page was uploaded by Mayur Pal on 21 June 2015.
The user has requested enhancement of the downloaded file.
for i=1:nrefine
%refine mesh
[nodes,bnod,xnod,ynod] = refine(nodes,bnod,xnod,ynod);
end
nele = size(nodes,1);nno = length(xnod);
figure;
plotmesh(xnod,ynod,nodes)
...')
...')
...')
...')
for i = 1:nele
ind = nodes(i,[1,2,3]);
if det([xnod(ind)';ynod(ind)';ones(1,3)]) < 0
warning('Introducing positive orientation ....')
nodes(i,:) = nodes(i,[1,3,2,4]);
end
end
% Testing of planarity. The above tests are assumed to be successful!
nno = max(max(nodes));
M = 10*nele+1;
htable = zeros(M,1);
nodesn = [nodes,nodes(:,1)];
for k = 1:nele
for i = 1:4
key = nodesn(k,i)+(nno-1)*nodesn(k,i+1);
pos = key - M*floor(key/M)+1;
while 1
switch htable(pos)
case 0
htable(pos) = key;
break
case key
error('Double edge ...')
otherwise
pos = pos+1;
if pos > M
pos = 1;
end
end
end
end
end
nodesn = nodes;
function plotmesh(xnod,ynod,nodes,fkt)
%
% plotmesh(xnod,ynod,nodes,fkt)
%
%
plotmesh plots an unstrucured grid in 2D
%
%
nn = nodes(:); % nodes in a long list
xx = xnod(nn); yy = ynod(nn); %coordinates corresponding to nodes
zz = fkt(nn);
xplot = reshape(xx,size(nodes));
yplot = reshape(yy,size(nodes));
zplot = reshape(zz,size(nodes));
clf;
axis equal;
%
fill(xplot',yplot','w');
fill3(xplot',yplot',zplot',zplot')
function plotmesh(xnod,ynod,nodes)
%
% plotmesh(xnod,ynod,nodes)
%
%
plotmesh plots an unstrucured grid in 2D
%
%
nn = nodes(:); % nodes in a long list
xx = xnod(nn); yy = ynod(nn); %coordinates corresponding to nodes
xplot = reshape(xx,size(nodes));
yplot = reshape(yy,size(nodes));
%
zplot = 0.001*sin(2*pi*xplot).*cos(2*pi*yplot);
clf;
axis equal;
fill(xplot',yplot','w');
%
fill3(xplot',yplot',zplot',zplot')
function [nodes,xnod,ynod,bnod,nele,nnot] = readq(ext)
% function [nodes,xnod,ynod,bnod,nele,nnot] = readq(ext)
% Input Argument: nrefine ===> Read the quadrilateral mesh from the files
'xcoor', 'ycoor', 'bnode',
% 'nodes'. The file 'nodes' contains the nodes rowwise, i.e. first the
four
% nodes of the first element, then the four nodes of the second element
% and so on. The argument ext contains the file extension if present.
% If ext='foo', the files to be read are 'xcoor.foo' and so on.
%
%
%
%
%
%
%
Output Arguments
nodes : Mesh Connectivity
xnod : coloumn vector containing X - coordinates of the mesh
ynod : coloumn vector containing Y - coordinates of the mesh
bnod : Boundary node
nele : total no of mesh elements in case of quad mesh
nnot : total no of nodes
end
nnot = sum(tv);
% Eliminate unused nodes
if nnot ~= nno
iv = find(tv);
xnod = xnod(iv);
ynod = ynod(iv);
bnod = bnod(iv);
p = zeros(1,nno);
p(iv) = 1:nnot;
nodes = p(nodes);
end
function [nodesn,bnodn,xnodn,ynodn] = refine(nodes,bnode,xnod,ynod)
%
% [nodesn,bnodn,xnodn,ynodn] = refine(nodes,bnod,xnod,ynod)
%
% refines a quadrilateral mesh once.
%
nele = size(nodes,1);
nno = length(xnod);
nbn = sum(bnode);
nnon = nno+3*nele+nbn/2;
nelen = 4*nele;
nodesn = zeros(nelen,4);
bnodn = zeros(nnon,1);
xnodn = zeros(nnon,1);
ynodn = zeros(nnon,1);
bnodn(1:nno) = bnode;
xnodn(1:nno) = xnod;
ynodn(1:nno) = ynod;
sum_1 = ones(1,4)/4.;
check = sparse(1,1,0,nno,nno,10*nno);
idxn = 1;
nno = nno+1;
for iel = 1:nele
%
new nodes
iv = nodes(iel,:);
xnodn(nno) = sum_1*xnod(iv);
ynodn(nno) = sum_1*ynod(iv);
nodmid = nno;
nno = nno+1;
if check(iv(1),iv(2)) == 0
xnodn(nno) = (xnod(iv(1))+xnod(iv(2)))/2.;
ynodn(nno) = (ynod(iv(1))+ynod(iv(2)))/2.;
check(iv(1),iv(2)) = nno; check(iv(2),iv(1)) = nno;
if bnodn(iv(1)) == 1 & bnodn(iv(2)) == 1
bnodn(nno) = 1;
end
nno = nno+1;
end
if check(iv(3),iv(2)) == 0
xnodn(nno) = (xnod(iv(3))+xnod(iv(2)))/2.;
ynodn(nno) = (ynod(iv(3))+ynod(iv(2)))/2.;
check(iv(3),iv(2)) = nno; check(iv(2),iv(3)) = nno;
meshtest(nodesn,xnodn,ynodn);
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
function [tri,xnod,ynod] = triangulate_delaunay(xnod,ynod,nodes,bnod);
tri = delaunay(xnod,ynod);
% calling matlab delaunay function
v1=tri(:,1);
v2=tri(:,2);
v3=tri(:,3);
d = (xnod(v2)-xnod(v1)).*(ynod(v3)-ynod(v1)) - (xnod(v3)ynod(v1)).*(ynod(v2)-ynod(v1));
% rearranging delaunay tessellations
rvrs = d>0;
tri(rvrs,:)=[tri(rvrs,1) tri(rvrs,3) tri(rvrs,2)];
% Testting critera to eleminate triangles generated on empty region by
% delaunay
% Removing unnecessary
% storing boundary nodes
% adding perturbation
% keeping boundary nodes
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% INPUT FILES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%bnode%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% copy lines below and save in bnode.txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
%nodes%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% save as nodes.txt lines below
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1
2
7
6
2
3
8
7
3
4
9
8
4
5
10
9
6
7
12
11
9
10
14
13
11
12
16
15
13
14
19
18
15
16
21
20
16
17
22
21
17
18
23
22
18
19
24
23
%xcoor%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% save as xcoor.txt lines below
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0.0
0.25
0.5
0.75
1.0
0.0
0.25
0.5
0.75
1.0
0.0
0.25
0.75
1.0
0.0
0.25
0.5
0.75
1.0
0.0
0.25
0.5
0.75
1.0
%ycoor%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% save as ycoor.txt lines below
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
0.0
0.0
0.0
0.0
0.0
0.25
0.25
0.25
0.25
0.25
0.5
0.5
0.5
0.5
0.75
0.75
0.75
0.75
0.75
1.0
1.0
1.0
1.0
1.0