0

当我添加像Plotting Glyphs (Vectors)这样的矢量箭头时,我已经绘制了 ABC 字段轮廓。我不知道如何为 3D 数据制作网格。

import pyvista as pv
import numpy as np
from numpy import mgrid

xmin = -800.
xmax = 800.
Lx = xmax-xmin

B0 = 1
k = 1
alpha = 2.0*np.pi*k/Lx

x, y, z = Lx*mgrid[0:1:51j, 0:1:51j, 0:1:51j]

Bx = B0*(np.sin(alpha*z) + np.cos(alpha*y))
By = B0*(np.sin(alpha*x) + np.cos(alpha*z))
Bz = B0*(np.sin(alpha*y) + np.cos(alpha*x))

B = Bx+By+Bz

grid = pv.StructuredGrid(x, y, z)
grid["ABC field 3D isocontour"] = B.flatten()
contours = grid.contour()

pv.set_plot_theme('document')
p = pv.Plotter()
p.add_mesh(contours)

#contours.plot(show_edges=True, show_grid=True, cpos="xy")
contours.plot(show_grid=True,screenshot='abc3d.png')
#p.show(screenshot='abc3d.png')

结果:

ABC 场等高线图

4

1 回答 1

0

感谢开发人员的回复,我现在更好地理解了如何mesh定义。以上数据集需要np.column_stack()

import pyvista as pv
import numpy as np
from numpy import mgrid
import matplotlib.pyplot as plt

print('initializing domain')
xmin = -800.
xmax = 800.
Lx = xmax-xmin
B0 = 1
k = 1
alpha = 2.0*np.pi*k/Lx
x, y, z = Lx*mgrid[0:1:51j, 0:1:51j, 0:1:51j]


print('initializing 3D B field')
Bx = B0*(np.sin(alpha*z) + np.cos(alpha*y))
By = B0*(np.sin(alpha*x) + np.cos(alpha*z))
Bz = B0*(np.sin(alpha*y) + np.cos(alpha*x))
B = np.column_stack((Bx.ravel(), By.ravel(), Bz.ravel()))
grid = pv.StructuredGrid(x, y, z)
grid["ABC field magnitude"] = np.linalg.norm(B, axis=1)
grid["ABC field vectors"] = B
grid.set_active_vectors("ABC field vectors")
contours = grid.contour(8, scalars="ABC field magnitude")
arrows = contours.glyph(orient="ABC field vectors", factor=200.0)
arrows_grid = grid.glyph(orient="ABC field vectors", factor=50.0)


print('plotting')
pv.set_plot_theme('document')
p = pv.Plotter(notebook=0, shape=(2,2))
cmap = plt.cm.get_cmap("viridis", 4)
#p.background_color='white'
#p.window_size
p.add_mesh(grid, cmap=cmap)
p.add_mesh(arrows_grid)
p.subplot(0,1)
slices = grid.slice_orthogonal(x=20, y=20, z=30)
#slices = grid.slice_orthogonal()
p.add_mesh(slices, cmap=cmap)
p.subplot(1,0)
p.add_mesh(contours, opacity=1)
p.show_grid()
p.subplot(1,1)
p.add_mesh(arrows)
p.show_grid()

p.link_views()
p.view_isometric()
p.show(screenshot='abc3d.png')

结果

Arnold-Beltrami-Childress 磁场

于 2020-06-15T13:01:24.050 回答