TriangularMesh utility class

The TriangularMesh class allows for fast interpolation on triangular meshes.

class tokamesh.TriangularMesh(R: ndarray, z: ndarray, triangles: ndarray)

Class for performing operations with a triangular mesh, such as interpolation and plotting.

Parameters
  • R – The major radius of each mesh vertex as a 1D numpy array.

  • z – The z-height of each mesh vertex as a 1D numpy array.

  • triangles – A 2D numpy array of integers specifying the indices of the vertices which form each of the triangles in the mesh. The array must have shape (N, 3) where N is the total number of triangles.

build_interpolator_matrix(R: ndarray, z: ndarray) ndarray

For a given set of points, construct an ‘interpolator’ matrix, such that its product with a vector of field values at each mesh vertex yields the interpolated values of the field at the given set of points.

Parameters
  • R – The major-radius of each interpolation point as 1D numpy.ndarray.

  • z – The z-height of each interpolation point as a 1D numpy.ndarray.

Returns

The interpolator matrix as a 2D numpy.ndarray with a shape of the number of interpolation points by the number of mesh vertices.

draw(ax, **kwargs)

Draw the mesh using a given matplotlib.pyplot axis object.

Parameters
  • ax – A matplotlib.pyplot axis object on which the mesh will be drawn by calling the ‘plot’ method of the object.

  • kwargs – Any valid keyword argument of matplotlib.pyplot.plot may be given in order to change the properties of the plot.

find_triangle(R: ndarray, z: ndarray) ndarray

Find the indices of the triangles which contain a given set of points.

Parameters
  • R – The major-radius of each point as a numpy array.

  • z – The z-height of each point as a numpy array.

Returns

The indices of the triangles which contain each point as numpy array. Any points which are not inside a triangle are given an index of -1.

get_field_image(vertex_values: ndarray, shape=(150, 150), pad_fraction=0.01)

Given the value of a field at each mesh vertex, use interpolation to generate an image of the field across the whole mesh.

Parameters
  • vertex_values – The value of the field being plotted at each vertex of the mesh as a 1D numpy array.

  • shape – A tuple of two integers specifying the dimensions of the image.

  • pad_fraction – The fraction of the mesh width/height used as padding to create a gap between the edge of the mesh and the edge of the plot.

Return R_axis, z_axis, field_image

R_axis is a 1D array of the major-radius value of each column of the image array. z_axis is a 1D array of the z-height value of each column of the image array. field_image is a 2D array of the interpolated field values. Any points outside the mesh are assigned a value of zero.

interpolate(R: ndarray, z: ndarray, vertex_values: ndarray) ndarray

Given the values of a function at each vertex of the mesh, use barycentric interpolation to approximate the function at a chosen set of points. Any points which lie outside the mesh will be assigned a value of zero.

Parameters
  • R – The major-radius of each interpolation point as a numpy array.

  • z – The z-height of each interpolation point as a numpy array.

  • vertex_values – The function value at each mesh vertex as a 1D numpy array.

Returns

The interpolated function values as a numpy array.

classmethod load(filepath: str)

Load and return a previously saved instance of TriangularMesh.

Parameters

filepath (str) – File path of the saved mesh.

Returns

The loaded mesh as an instance of TriangularMesh.

plot_field(ax, vertex_values: ndarray, color_levels=64, colormap='viridis', mesh_color=None, mesh_thickness=0.5)

Generates a filled color contour plot of a given field specified by its value at each mesh vertex.

Parameters
  • ax – A matplotlib.pyplot axis object on which the field will be plotted by calling the tricontourf method of the object.

  • vertex_values – The value of the field being plotted at each vertex of the mesh as a 1D numpy array.

  • color_levels – The number of color levels used to plot the field contours.

  • colormap – The name (or an instance) of a matplotlib colormap which will be used to color the field contours.

  • mesh_color – The name of a matplotlib.pyplot color which will be used to draw the mesh over the field. If unspecified, the mesh will not be drawn.

  • mesh_thickness – The line-width used to draw the mesh.

save(filepath: str)

Save the mesh using the numpy ‘npz’ format.

Parameters

filepath (str) – File path to which the mesh will be saved.