Generating a Random 3D Vector Field

In this example we are going to generate a random 3D vector field with a Gaussian covariance model. The mesh on which we generate the field will be externally defined and it will be generated by PyVista.

import pyvista as pv

import gstools as gs

# mainly for setting a white background

create a uniform grid with PyVista

dims, spacing, origin = (40, 30, 10), (1, 1, 1), (-10, 0, 0)
mesh = pv.ImageData(dimensions=dims, spacing=spacing, origin=origin)

create an incompressible random 3d velocity field on the given mesh with added mean velocity in x-direction

model = gs.Gaussian(dim=3, var=3, len_scale=1.5)
srf = gs.SRF(model, mean=(0.5, 0, 0), generator="VectorField", seed=198412031)
srf.mesh(mesh, points="points", name="Velocity")

Now, we can do the plotting

streamlines = mesh.streamlines(

# set a fancy camera position
cpos = [(25, 23, 17), (0, 10, 0), (0, 0, 1)]

p = pv.Plotter()
# adding an outline might help navigating in 3D space
# p.add_mesh(mesh.outline(), color="k")
/home/docs/checkouts/ UserWarning:
This system does not appear to be running an xserver.
PyVista will likely segfault when rendering.

Try starting a virtual frame buffer with xvfb, or using



PyVista is not working on readthedocs, but you can try it out yourself by uncommenting the following line of code.


The result should look like this:

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

Gallery generated by Sphinx-Gallery