Plot a Gyroid Surface#

Contour an implicit gyroid field stored on pyvista.ImageData into a periodic surface with pyvista.DataSetFilters.contour().

import numpy as np
import pyvista as pv

Sample the implicit field#

The gyroid is the zero level set of sin(x)cos(y) + sin(y)cos(z) + sin(z)cos(x).

n = 50
x, y, z = np.mgrid[
    -np.pi : np.pi : complex(0, n),
    -np.pi : np.pi : complex(0, n),
    -np.pi : np.pi : complex(0, n),
]
values = np.sin(x) * np.cos(y) + np.sin(y) * np.cos(z) + np.sin(z) * np.cos(x)

grid = pv.ImageData(dimensions=values.shape)
grid.origin = (-np.pi, -np.pi, -np.pi)
grid.spacing = (2 * np.pi / (n - 1),) * 3
grid['gyroid'] = values.ravel(order='F')
grid
ImageData (0x791a9d871060)
  N Cells:      117649
  N Points:     125000
  X Bounds:     -3.142e+00, 3.142e+00
  Y Bounds:     -3.142e+00, 3.142e+00
  Z Bounds:     -3.142e+00, 3.142e+00
  Dimensions:   50, 50, 50
  Spacing:      1.282e-01, 1.282e-01, 1.282e-01
  N Arrays:     1


Extract the zero isosurface#

The contour weaves through the periodic sample volume.

surface = grid.contour([0.0], scalars='gyroid')

pl = pv.Plotter()
pl.add_mesh(surface, color='royalblue', smooth_shading=True)
pl.show()
gyroid

Tags: advanced

Total running time of the script: (0 minutes 0.333 seconds)

Gallery generated by Sphinx-Gallery