The mixins module provides a set of tools for interfacing discretize with external libraries such as VTK. These modules are only imported if those external packages are available in the active Python environment and provide extra functionality that different finite volume meshes can inherrit.

VTK Module

This module provides a way for discretize meshes to be converted to VTK data objects (and back when possible) if the VTK Python package is available. The vtkInterface class becomes inherrited by all mesh objects and allows users to directly convert any given mesh by calling that mesh’s toVTK() method (note that this method will not be available if VTK is not available).

This functionality was originally developed so that discretize could be interoperable with PVGeo, providing a direct interface for discretize meshes within ParaView and other VTK powered platforms. This interoperablity allows users to visualize their finite volume meshes and model data from discretize along side all their other datasets in a common rendering environment.

Here’s an example of the types of integrated visualizations that are possible in ParaView leveraging the link between discretize and PVGeo:

PVGeo Example Visualization

Laguna del Maule Bouguer Gravity

This data scene is was produced from the Laguna del Maule Bouguer Gravity example provided by Craig Miller (see Maule volcanic field, Chile. Refer to Miller et al 2016 EPSL for full details.)

The rendering below shows several data sets and a model integrated together:

  • Point Data: the Bouguer gravity anomalies
  • Topography Surface
  • Inverted Model: The model has been both sliced and thresholded for low values
discretize.mixins.vtkModule.assignCellData(vtkDS, models=None)[source]

Assign the model(s) to the VTK dataset as CellData


  • vtkDS (vtk.vtkDataSet) –
    • Any given VTK data object that has cell data
  • models (dict(numpy.ndarray)) – Name(‘s) and array(‘s). Match number of cells
class discretize.mixins.vtkModule.vtkInterface[source]

This class is full of methods that enable discretize meshes to be converted to VTK data objects (and back when possible). This is inherritted by the BaseMesh class so all these methods are available to any mesh object!

CurvilinearMesh, TreeMesh, and TensorMesh are all currently implemented. The CylMesh is not implemeted and will raise and excpetion. The following is an example of how to use the VTK interface to construct VTK data objects or write VTK files.

import discretize
import numpy as np
h1 = np.linspace(.1, .5, 3)
h2 = np.linspace(.1, .5, 5)
h3 = np.linspace(.1, .5, 3)
mesh = discretize.TensorMesh([h1, h2, h3])

# Get a VTK data object

# Save this mesh to a VTK file

Note that if your mesh is defined on a reference frame that is not the traditional <X,Y,Z> system with vectors of \((1,0,0)\), \((0,1,0)\), and \((0,0,1)\), then the mesh will be rotated to be on the traditional reference frame. The previous example snippet provides a vtkRectilinearGrid object because that tensor mesh lies on the traditional reference frame. If we alter the reference frame, then we yield a vtkStructuredGrid that is the same mesh rotated in space.

# Defined a rotated reference frame
mesh.axis_u = (1,-1,0)
mesh.axis_v = (-1,-1,0)
mesh.axis_w = (0,0,1)
# Check that the referenc fram is valid

# Yield the rotated vtkStructuredGrid

# or write it out to a VTK format

The two above code snippets produce a vtkRectilinearGrid and a vtkStructuredGrid respecitvely. To demonstarte the difference, we have plotted the two datasets next to eachother where the first mesh is in green and its data axes are parrallel to the traditional cartesian reference frame. The second, rotated mesh is shown in red and its data axii are rotated from the traditional cartesian refence frame as specified by the axis_u, axis_v, and axis_w properties.

toVTK(mesh, models=None)[source]

Convert this mesh object to it’s proper VTK data object with the given model dictionary as the cell data of that dataset.


Parameters:models (dict(numpy.ndarray)) – Name(‘s) and array(‘s). Match number of cells
writeVTK(mesh, fileName, models=None, directory='')[source]

Makes and saves a VTK object from this mesh and given models


  • fileName (str) – path to the output vtk file or just its name if directory is specified
  • models (dict) – dictionary of numpy.array - Name(‘s) and array(‘s). Match number of cells
  • directory (str) – directory where the UBC GIF file lives
class discretize.mixins.vtkModule.vtkTensorRead[source]

Provides a convienance method for reading VTK Rectilinear Grid files as TensorMesh objects.

classmethod readVTK(TensorMesh, fileName, directory='')[source]

Read VTK Rectilinear (vtr xml file) and return Tensor mesh and model


  • fileName (str) – path to the vtr model file to read or just its name if directory is specified
  • directory (str) – directory where the UBC GIF file lives


Return type:tuple
Returns:(TensorMesh, modelDictionary)