FEALPy 创建各种各样的网格

本文最后更新于:1 个月前

FEALPy 创建各种各样的网格

利用 Mesh Factory 生成网格

三角形网格

1
2
3
4
5
6
7
8
9
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

domain = [0, 1, 0, 1]
mesh = MF.boxmesh2d(domain, nx=10, ny=10, meshtype='tri')
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x121f92970>
png

矩形网格

1
2
3
4
5
6
7
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1]
mesh = MF.boxmesh2d(domain, nx=10, ny=10, meshtype='quad')
# mesh.uniform_refine(n=2) # 网格加密次数
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x12208cf40>
png

多项式网格

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1]
mesh = MF.boxmesh2d(domain, nx=10, ny=10, meshtype='poly')
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PatchCollection at 0x1220f9b20>
png

鱼骨网格

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1]
mesh = MF.special_boxmesh2d(domain, n=10, meshtype='fishbone')
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x12215d850>
png

米字型网格

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1]
mesh = MF.special_boxmesh2d(domain, n=10, meshtype='rice')
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x1221c47c0>
png

交叉型网格

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1]
mesh = MF.special_boxmesh2d(domain, n=10, meshtype='cross')
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x12222c2b0>
png

非一致网格

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1]
mesh = MF.special_boxmesh2d(domain, n=10, meshtype='nonuniform')
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x12229a940>
png

圆上的网格

1
2
3
4
5
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
mesh = MF.unitcirclemesh(h=0.1, meshtype='tri')
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x12232b610>
png

L-shape 网格

1
2
3
4
5
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
mesh = MF.lshape_mesh(n=2)
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x1223bb880>
png

Polygon 网格

1
2
3
4
5
6
7
8
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
mesh = MF.polygon_mesh()
mesh.uniform_refine(n=3) # 网格加密次数
NC = mesh.number_of_cells()
print('Number of cells:', NC)
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
Number of cells: 288





<matplotlib.collections.PatchCollection at 0x122428b50>
png

四面体网格

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1, 0, 1]
mesh = MF.boxmesh3d(domain, nx=4, ny=4, nz=4, meshtype='tet')
fig = plt.figure()
axes = fig.gca(projection='3d')
mesh.add_plot(axes)
memory size of node array (GB):  2.7939677238464355e-06
memory size of cell array (GB):  1.1444091796875e-05
memory size of face array (GB):  1.9311904907226562e-05
memory size of edge array (GB):  9.000301361083984e-06
memory size of face2cell array (GB):  2.574920654296875e-05
memory size of cell2edge array (GB):  1.71661376953125e-05
Total memory size (GB):  8.546561002731323e-05


/var/folders/z_/_4v3r8650yv8dpcq7bz94d3m0000gn/T/ipykernel_9683/2032695478.py:5: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  axes = fig.gca(projection='3d')





<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x1222f55e0>
png

六面体网格

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1, 0, 1]
mesh = MF.boxmesh3d(domain, nx=4, ny=4, nz=4, meshtype='hex')
fig = plt.figure()
axes = fig.gca(projection='3d')
mesh.add_plot(axes)
/var/folders/z_/_4v3r8650yv8dpcq7bz94d3m0000gn/T/ipykernel_9683/691261962.py:5: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().
  axes = fig.gca(projection='3d')





<mpl_toolkits.mplot3d.art3d.Poly3DCollection at 0x122485970>
png

利用Threshold构造你想要的网格

我们之前给出过 L-shape 的网格,但是有些时候想要变换 L-shape 的方向,这时候便要借助 Threshold 函数来完成。

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1]
mesh = MF.boxmesh2d(domain, nx=10, ny=10, meshtype='tri', threshold=lambda p: (p[..., 0]<0.5) & (p[..., 1]<0.5))
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x1225d2bb0>
png

挖掉一部分的网络

1
2
3
4
5
6
from fealpy.mesh import MeshFactory as MF # 导入网格工厂模块
domain = [0, 1, 0, 1]
mesh = MF.boxmesh2d(domain, nx=50, ny=50, meshtype='tri', threshold=lambda p: (p[..., 0]-0.5) ** 2 + (p[..., 1]-0.5) ** 2 < 0.2)
fig = plt.figure()
axes = fig.gca()
mesh.add_plot(axes)
<matplotlib.collections.PolyCollection at 0x122626f40>
png

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!