coconut_tools.pyvista_slice

Coconut Visualization with PyVista

This module provides tools to read, convert, and visualize MHD simulation data from COCONUT using PyVista. The main objective is to demonstrate how to load a VTU file, convert the data to physical units and spherical coordinates, and create multiple visualizations (slice, clipping, streamlines, isosurfaces).

Typical visualizations include:
  • Slice of radial velocity (vr)

  • Clipped spherical region colored by radial magnetic field (br)

  • Streamlines of the magnetic field

  • Isosurface of density (optional)

Author: Luis, Quentin

Functions

convert_to_spherical(mesh[, verbose])

Convert Cartesian coordinates to spherical and compute radial components.

convert_units(mesh[, verbose])

Convert mesh quantities into physical units and add them to the mesh.

make_streamlines(mesh[, vectors, max_steps, ...])

Wrapper that normalizes types for VTK/PyVista streamline settings.

read_mesh(filename[, verbose])

Load a VTU mesh file.

visualize(mesh[, slice_normal, ...])

Create a PyVista visualization of MHD quantities.

coconut_tools.pyvista_slice.convert_to_spherical(mesh, verbose=True)[source]

Convert Cartesian coordinates to spherical and compute radial components.

Parameters:

mesh (pv.DataSet) – Mesh with Cartesian and dimensional quantities.

Returns:

Updated mesh with spherical quantities.

Return type:

pv.DataSet

coconut_tools.pyvista_slice.convert_units(mesh, verbose=True)[source]

Convert mesh quantities into physical units and add them to the mesh.

Parameters:

mesh (pv.DataSet) – PyVista mesh.

Returns:

Updated mesh with new fields.

Return type:

pv.DataSet

coconut_tools.pyvista_slice.make_streamlines(mesh, vectors='B', max_steps=1000, n_points=1000, source_radius=2.0, source_center=(0, 0, 0), initial_step_length=None, step_unit_val=None, max_error=None, max_step_length=None)[source]

Wrapper that normalizes types for VTK/PyVista streamline settings. - Casts step-like params to int where VTK expects integers. - Uses max_steps (replacement for deprecated max_time).

coconut_tools.pyvista_slice.read_mesh(filename, verbose=True)[source]

Load a VTU mesh file.

Parameters:

filename (str) – Path to the VTU file.

Returns:

PyVista mesh object.

Return type:

pv.DataSet

coconut_tools.pyvista_slice.visualize(mesh, slice_normal='y', slice_plane_scalar='vr', save_path=None, show=True, vr_clim=None, br_clim=None, stream_clim=None, volumic_vr=None, rho_iso=1e-13, AlfvSurf=False, camera_radius=18.0, camera_phi_deg=60.0, camera_z=4.0, verbose=True)[source]

Create a PyVista visualization of MHD quantities.

The visualization includes slice planes, streamlines, optional iso surfaces, and a configurable camera position.

Parameters:
  • mesh – PyVista dataset containing the physical fields.

  • slice_normal (str, optional) – Normal of the slice plane. Must be one of {“x”, “y”, “z”}. Default is “y”.

  • slice_plane_scalar (str, optional) – Scalar field used for coloring the slice plane. Default is “vr”.

  • save_path (str | None, optional) – Path where the rendered image is saved. If None, the image is not saved. Default is None.

  • show (bool, optional) – If True, show the interactive viewer. If False, run in off screen mode only. Default is True.

  • vr_clim (tuple[float, float] | None, optional) – Color limits for the radial velocity field. Default is None.

  • br_clim (tuple[float, float] | None, optional) – Color limits for the radial magnetic field. Default is None.

  • stream_clim (tuple[float, float] | None, optional) – Color limits for the streamline magnitude. Default is None.

  • volumic_vr (float | None, optional) – If not None, draw an iso surface of the radial velocity at this value. Default is None.

  • rho_iso (float, optional) – Density iso surface value in g cm^(-3). Default is 1e-13.

  • AlfvSurf (bool, optional) – If True, plot the Alfvén surface. Default is False.

  • camera_radius (float, optional) – Radial distance of the camera from the origin. Default is 18.0.

  • camera_phi_deg (float, optional) – Azimuthal angle of the camera in degrees. Default is 60.0.

  • camera_z (float, optional) – Height of the camera above the equatorial plane. Default is 4.0.

  • verbose (bool)

Returns:

None