首先在github上下载陈龙包IFEM.
然后把包添加到当前的依赖函数库里:File -> Set Path(IFEM) -> Add with Subfolders
1、三角形一致网格剖分
>> box = [-1, 1, -1, 1];
>> h = 0.1;
>> [node, elem] = squaremesh(box, h);
>> showmesh(node, elem)
2、正方形一致网格剖分
>> [node,elem,T] = squarequadmesh(box,h);
>> showmesh(node,elem);
3、含退化单元的非结构性网格
在原有的一致三角形网格上使用公式:
x
=
x
^
+
0.1
∗
sin
(
2
π
x
^
)
∗
sin
(
2
π
y
^
)
y
=
y
^
+
0.1
∗
sin
(
2
π
x
^
)
∗
sin
(
2
π
y
^
)
x=\hat{x}+0.1*\sin(2\pi\hat{x})*\sin(2\pi\hat{y})\\y=\hat{y}+0.1*\sin(2\pi\hat{x})*\sin(2\pi\hat{y})
x=x^+0.1∗sin(2πx^)∗sin(2πy^)y=y^+0.1∗sin(2πx^)∗sin(2πy^)
在其他的网格上我们也可以使用同样的方法生成非结构网格
>> box = [-1, 1, -1, 1];
>> h = 0.1;
>> [node, elem] = squaremesh(box, h);
x = node(:,1);y=node(:,2);
node(:,1) = x+0.1.*sin(2*pi.*x).*sin(2*pi.*y);
node(:,2) = y+0.1.*sin(2*pi.*x).*sin(2*pi.*y);
>> showmesh(node, elem);
4、Voronoi多边形网格(Polymesher):
参考论文:PolyMesher: a general-purpose mesh generator for polygonal elements written in Matlab
[node, elem, ~, ~, ~] = PolyMesher(@MbbDomain,100, 100);
这里,参数中的第一个100是我们要生成的单元个数,第二个100是迭代次数(一般不会取太低,防止单元间尺寸差距过大)。
我们再使用showmesh(node, elem);
绘制出熟悉的网格。
注意
由于一些版本中的陈龙包IFEM中的showmesh函数只能绘制三角形或四边形网格,而绘制多边形网格需要用函数showmeshpoly函数,但我认为showmeshpoly函数画出来的蓝色太丑了。于是绘制多边形网格时,使用mVEM包(Copyright © Terence Yu)里的showmesh函数,其可以展示2D的三角形和多边形网格,甚至3D的网格,功能更加强大,因此绘制多边形时我们采用该函数。
function showmesh(node,elem,options)
%Showmesh displays a mesh in 2-D and 3-D.
%
% Copyright (C) Terence Yu
if nargin==2, options.FaceAlpha = 0.4; end
dim = size(node,2);
if ~iscell(elem)
h = patch('Faces', elem, 'Vertices', node);
end
if iscell(elem)
if iscell(elem{1}), elem = vertcat(elem{:}); end
max_n_vertices = max(cellfun(@length, elem));
padding_func = @(vertex_ind) [vertex_ind,...
NaN(1,max_n_vertices-length(vertex_ind))]; % function to pad the vacancies
tpad = cellfun(padding_func, elem, 'UniformOutput', false);
tpad = vertcat(tpad{:});
h = patch('Faces', tpad, 'Vertices', node);
end
if dim==3
view(3); set(h,'FaceAlpha',options.FaceAlpha); % transparency
end
if isfield(options,'facecolor')
facecolor = options.facecolor;
else
facecolor = [0.5 0.9 0.45];
end
set(h,'facecolor',facecolor,'edgecolor','k');
axis equal; axis tight;
5、非一致的三角形剖分(Distmesh)
Distmesh可以生成很多种形状的图形,包括在图形中掏一个洞等;
fd=inline('drectangle(p,-1,1,-1,1)','p');
box=[-1,-1;1,1];
fix=[-1,-1;-1,1;1,-1;1,1];
[p,t,~,~]=odtmesh2d(fd,@huniform,0.2,box,fix,1);
其中,变量p,t
即为node,elem
fd=inline('ddiff(drectangle(p,-1,1,-1,1),dcircle(p,0,0,0.4))','p');
box=[-1,-1;1,1];
fix=[-1,-1;-1,1;1,-1;1,1];
[p,t,~,~]=odtmesh2d(fd,@huniform,0.1,box,fix,1);