PyVista是一个基于VTK的Python高级API,用于3D可视化和网格处理。它提供简洁易用的接口,无需深入了解VTK细节,即可实现复杂3D图形绘制。
该库的详细介绍可进一步阅览:VTK库学习 | 百万级自由度有限元模型云图绘制只需0.02秒!
默认情况下,Pyvista在进行有限元后处理时,只能显示线性单元,当用户想要进行高阶单元可视化时,需要进一步处理。本次推文将结合PyGmsh
分享我的处理思路:
line
具体如何实现呢?
import pygmsh
import pyvista as pv
import meshio
def meshGenerator():
with pygmsh.geo.Geometry() as geom:
geom.add_circle(
[0.0, 0.0, 0.0],
1.0,
mesh_size=0.3,
)
mesh = geom.generate_mesh(order=2)
return mesh
mesh = meshGenerator()
meshio_mesh = meshio.Mesh(points=mesh.points, cells=[("triangle6", mesh.cells_dict['triangle6'])])
plotter = pv.Plotter()
darg = dict(
show_edges=False,
)
plotter.add_mesh(meshio_mesh, **darg)
plotter.add_points(meshio_mesh.points, color='red', point_size=15, render_points_as_spheres=True)
for cell in meshio_mesh.cells_dict['triangle6']:
# 每个单元数组包含形成三角形的点索引
# 高阶三角形单元顺序:0-3-1-4-2-5-0(连接三角形的角点和边中点)
triangle_points = meshio_mesh.points[cell]
boundary_order = [0, 3, 1, 4, 2, 5, 0]
points_for_lines = triangle_points[boundary_order]
# 绘制边界线
plotter.add_lines(points_for_lines, color='blue', width=3, connected=True)
plotter.show()
import pygmsh
import pyvista as pv
import meshio
def meshGenerator():
with pygmsh.geo.Geometry() as geom:
rectangle = geom.add_rectangle(0, 5, 0, 5, 0.0, 0.8)
geom.set_recombined_surfaces([rectangle.surface])
mesh = geom.generate_mesh(order=2)
return mesh
mesh = meshGenerator()
meshio_mesh = meshio.Mesh(points=mesh.points, cells=[("quad9", mesh.cells_dict['quad9'])])
plotter = pv.Plotter()
darg = dict(
show_edges=False,
)
plotter.add_mesh(meshio_mesh, **darg)
plotter.add_points(meshio_mesh.points, color='red', point_size=15, render_points_as_spheres=True)
for cell in meshio_mesh.cells_dict['quad9']:
# 每个单元数组包含形成三角形的点索引
# 高阶四边形单元顺序 0-4-1-5-2-6-3-7-0
triangle_points = meshio_mesh.points[cell]
boundary_order = [0, 4, 1, 5, 2, 6, 3, 7, 0]
points_for_lines = triangle_points[boundary_order]
# 绘制边界线
plotter.add_lines(points_for_lines, color='blue', width=3, connected=True)
plotter.view_xy()
plotter.show()
从上面的可视化结果来看,PyGmsh的高阶四边形单元指的是Quad9,即九节点单元,有关单元节点顺序可参考Gmsh的节点连接规则: