Skip to content

Commit 4db0fbb

Browse files
committed
Create cubegradmesh.m
1 parent bc58c59 commit 4db0fbb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

mesh/cubegradmesh.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function [node,elem,HB] = cubegradmesh(xh,yh,zh)
2+
%% CUBEGRADMESH a uniform mesh of a cube
3+
%
4+
%
5+
% Example
6+
% xh = 0:0.125:1;
7+
% yh = 0:0.125:1;
8+
% zh = 0:0.125:0.5;
9+
% [node,elem] = cubegradmesh(xh,yh,zh);
10+
% showmesh3(node,elem);
11+
%
12+
%
13+
% Copyright (C) Long Chen. See COPYRIGHT.txt for details.
14+
15+
16+
% 8 --- 7
17+
% /| /|
18+
% 5 --- 6 | z
19+
% | 4 --| 3 | y
20+
% |/ |/ | /
21+
% 1 -- 2 o --- x
22+
%
23+
% The order of vertices is important for the uniform refinement.
24+
25+
[x,y,z] = ndgrid(xh,yh,zh);
26+
node = [x(:),y(:),z(:)];
27+
[nx,ny,nz] = size(x);
28+
elem = zeros(6*(nx-1)*(ny-1)*(nz-1),4);
29+
indexMap = reshape(1:nx*ny*nz,nx,ny,nz);
30+
localIndex = zeros(8,1);
31+
idx = 1;
32+
for k = 1:nz-1
33+
for j = 1:ny-1
34+
for i = 1:nx-1
35+
localIndex(1) = indexMap(i,j,k);
36+
localIndex(2) = indexMap(i+1,j,k);
37+
localIndex(3) = indexMap(i+1,j+1,k);
38+
localIndex(4) = indexMap(i,j+1,k);
39+
localIndex(5) = indexMap(i,j,k+1);
40+
localIndex(6) = indexMap(i+1,j,k+1);
41+
localIndex(7) = indexMap(i+1,j+1,k+1);
42+
localIndex(8) = indexMap(i,j+1,k+1);
43+
elem(idx:idx+5,:) = localIndex([1 2 3 7; 1 4 3 7; 1 5 6 7;...
44+
1 5 8 7; 1 2 6 7; 1 4 8 7]);
45+
idx = idx + 6;
46+
end
47+
end
48+
end
49+
% Set this as an initial grid
50+
N0 = size(node,1);
51+
HB = zeros(N0,4);
52+
HB(1:N0,1:3) = repmat((1:N0)',1,3);

0 commit comments

Comments
 (0)