只是添加一些进一步的想法,这可能会帮助其他有不规则域类型问题的人。对于用户具有三个向量/列表的情况,x,y,z 表示 2D 解决方案,其中 z 将绘制在矩形网格上作为表面,ArtifixR 的“plot_trisurf()”cmets 是适用的。一个类似但非矩形域的例子是:
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D
# problem parameters
nu = 50; nv = 50
u = np.linspace(0, 2*np.pi, nu,)
v = np.linspace(0, np.pi, nv,)
xx = np.zeros((nu,nv),dtype='d')
yy = np.zeros((nu,nv),dtype='d')
zz = np.zeros((nu,nv),dtype='d')
# populate x,y,z arrays
for i in range(nu):
for j in range(nv):
xx[i,j] = np.sin(v[j])*np.cos(u[i])
yy[i,j] = np.sin(v[j])*np.sin(u[i])
zz[i,j] = np.exp(-4*(xx[i,j]**2 + yy[i,j]**2)) # bell curve
# convert arrays to vectors
x = xx.flatten()
y = yy.flatten()
z = zz.flatten()
# Plot solution surface
fig = plt.figure(figsize=(6,6))
ax = Axes3D(fig)
ax.plot_trisurf(x, y, z, cmap=cm.jet, linewidth=0,
antialiased=False)
ax.set_title(r'trisurf example',fontsize=16, color='k')
ax.view_init(60, 35)
fig.tight_layout()
plt.show()
以上代码产生:
但是,这可能无法解决所有问题,尤其是在问题定义在不规则域上的情况下。此外,在域具有一个或多个凹面区域的情况下,delaunay 三角剖分可能会导致在域外部生成虚假三角形。在这种情况下,必须从三角剖分中移除这些流氓三角形,以实现正确的表面表示。对于这些情况,用户可能必须明确包括 delaunay 三角剖分计算,以便可以通过编程方式删除这些三角形。在这种情况下,下面的代码可以代替之前的情节代码:
import matplotlib.tri as mtri
import scipy.spatial
# plot final solution
pts = np.vstack([x, y]).T
tess = scipy.spatial.Delaunay(pts) # tessilation
# Create the matplotlib Triangulation object
xx = tess.points[:, 0]
yy = tess.points[:, 1]
tri = tess.vertices # or tess.simplices depending on scipy version
#############################################################
# NOTE: If 2D domain has concave properties one has to
# remove delaunay triangles that are exterior to the domain.
# This operation is problem specific!
# For simple situations create a polygon of the
# domain from boundary nodes and identify triangles
# in 'tri' outside the polygon. Then delete them from
# 'tri'.
# <ADD THE CODE HERE>
#############################################################
triDat = mtri.Triangulation(x=pts[:, 0], y=pts[:, 1], triangles=tri)
# Plot solution surface
fig = plt.figure(figsize=(6,6))
ax = fig.gca(projection='3d')
ax.plot_trisurf(triDat, z, linewidth=0, edgecolor='none',
antialiased=False, cmap=cm.jet)
ax.set_title(r'trisurf with delaunay triangulation',
fontsize=16, color='k')
plt.show()
下面给出的示例图说明了解决方案 1) 带有虚假三角形的解决方案,以及 2) 它们已被移除的位置:
希望以上对解数据有凹凸情况的人有所帮助。