Pyvista 3D Field Lines#

Pyvista offers field-line computation and visualization in 3D. In addition to the field computation, Magpylib offers magnet visualization that seamlessly integrates into a Pyvista plotting scene.

import magpylib as magpy
import pyvista as pv

# Create a magnet with Magpylib
magnet = magpy.magnet.Cylinder(polarization=(0, 0, 1), dimension=(0.010, 0.004))

# Create a 3D grid with Pyvista
grid = pv.ImageData(
    dimensions=(41, 41, 41),
    spacing=(0.001, 0.001, 0.001),
    origin=(-0.02, -0.02, -0.02),
)

# Compute B-field and add as data to grid
grid["B"] = magnet.getB(grid.points) * 1000  # T -> mT

# Compute the field lines
seed = pv.Disc(inner=0.001, outer=0.003, r_res=1, c_res=9)
strl = grid.streamlines_from_source(
    seed,
    vectors="B",
    max_step_length=0.1,
    max_time=.02,
    integration_direction="both",
)

# Create a Pyvista plotting scene
pl = pv.Plotter()

# Add magnet to scene - streamlines units in (m)
magpy.show(magnet, canvas=pl, units_length="m", backend="pyvista")

# Prepare legend parameters
legend_args = {
    "title": "B (mT)",
    "title_font_size": 20,
    "color": "black",
    "position_y": 0.25,
    "vertical": True,
}

# Add streamlines and legend to scene
pl.add_mesh(
    strl.tube(radius=0.0002),
    cmap="bwr",
    scalar_bar_args=legend_args,
)

# Prepare and show scene
pl.camera.position = (0.03, 0.03, 0.03)
pl.show()
---------------------------------------------------------------------------
DeprecationError                          Traceback (most recent call last)
Cell In[1], line 19
     15 grid["B"] = magnet.getB(grid.points) * 1000  # T -> mT
     16 
     17 # Compute the field lines
     18 seed = pv.Disc(inner=0.001, outer=0.003, r_res=1, c_res=9)
---> 19 strl = grid.streamlines_from_source(
     20     seed,
     21     vectors="B",
     22     max_step_length=0.1,

File ~/checkouts/readthedocs.org/user_builds/magpylib/checkouts/latest/.venv/lib/python3.13/site-packages/pyvista/_deprecate_positional_args.py:243, in _deprecate_positional_args.<locals>._inner_deprecate_positional_args.<locals>.inner_f(*args, **kwargs)
    239             warn_external(msg, PyVistaDeprecationWarning)
    241         warn_positional_args()
--> 243 return f(*args, **kwargs)

File ~/checkouts/readthedocs.org/user_builds/magpylib/checkouts/latest/.venv/lib/python3.13/site-packages/pyvista/core/filters/data_set.py:3675, in DataSetFilters.streamlines_from_source(self, source, vectors, integrator_type, integration_direction, surface_streamlines, initial_step_length, step_unit, min_step_length, max_step_length, max_steps, terminal_speed, max_error, max_time, compute_vorticity, rotation_scale, interpolator_type, progress_bar, max_length)
   3670 if max_time is not None:
   3671     msg = (
   3672         '``max_time`` parameter is deprecated and no longer supported. '
   3673         'Use ``max_length`` instead.'
   3674     )
-> 3675     raise DeprecationError(msg)
   3677 if max_length is None:
   3678     max_length = 4.0 * self.GetLength()

DeprecationError: ``max_time`` parameter is deprecated and no longer supported. Use ``max_length`` instead.