Finite Element Methods

The superscreen.fem module contains functions useful for performing computations on a triangular mesh. Many of the functions take as arguments two arrays, points and triangles, which together define the mesh. points, which has shape (n, 2), defines the coordinates of the triangle vertices. Each row in triangles, which has shape (m, 3) gives the indices of the three vertices in a single triangle, such that points[triangles] is a shape (m, 3, 2) array where each row gives the coordinates of the three vertices of a triangle in the mesh.

The weight matrix, mass matrix, and Laplacian operator are all sparse, meaning that most of their entries are zero. In order to save memory, functions that generate these arrays give the option to return a scipy.sparse matrix. All matrices are converted to dense numpy arrays when simulating a superscreen.device.device.Device, however.

superscreen.fem.in_polygon(poly_points, query_points, radius=0)[source]

Returns a boolean array indicating which points in query_points lie inside the polygon defined by poly_points.

Parameters
Return type

Union[bool, ndarray]

Returns

A shape (n, ) boolean array indicating which query_points lie inside the polygon. If only a single query point is given, then a single boolean value is returned.

superscreen.fem.centroids(points, triangles)[source]

Returns x, y coordinates for triangle centroids (centers of mass).

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices.

  • triangles (ndarray) – Shape (m, 3) array of triangle indices.

Return type

ndarray

Returns

Shape (m, 2) array of triangle centroid (center of mass) coordinates

superscreen.fem.areas(points, triangles)[source]

Calculates the area of each triangle.

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices

  • triangles (ndarray) – Shape (m, 3) array of triangle indices

Return type

ndarray

Returns

Shape (m, ) array of triangle areas

superscreen.fem.adjacency_matrix(triangles, sparse=True)[source]

Computes the adjacency matrix for a given set of triangles.

Parameters
  • triangles (ndarray) – Shape (m, 3) array of triangle indices

  • sparse (bool) – Whether to return a sparse matrix or numpy ndarray.

Return type

Union[ndarray, csr_matrix]

Returns

Shape (n, n) adjacency matrix, where n = triangles.max() + 1

superscreen.fem.weights_inv_euclidean(points, triangles, sparse=True)[source]

Weights edges by the inverse Euclidean distance of the edge lengths.

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices.

  • triangles (ndarray) – Shape (m, 3) array of triangle indices.

  • sparse (bool) – Whether to return a sparse matrix or numpy ndarray.

Return type

Union[ndarray, lil_matrix]

Returns

Shape (n, n) matrix of vertex weights

superscreen.fem.weights_half_cotangent(points, triangles, sparse=True)[source]

Weights edges by half of the cotangent of the two angles opposite the edge.

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices.

  • triangles (ndarray) – Shape (m, 3) array of triangle indices.

  • sparse (bool) – Whether to return a sparse matrix or numpy ndarray.

Return type

Union[ndarray, lil_matrix]

Returns

Shape (n, n) matrix of vertex weights

superscreen.fem.calculate_weights(points, triangles, method, sparse=True)[source]

Returns the weight matrix, calculated using the specified method.

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices.

  • triangles (ndarray) – Shape (m, 3) array of triangle indices.

  • method (str) – Method for calculating the weights. One of: “uniform”, “inv_euclidean”, or “half_cotangent”.

  • sparse (bool) – Whether to return a sparse matrix or numpy ndarray.

Return type

Union[ndarray, csr_matrix]

Returns

Shape (n, n) matrix of vertex weights

superscreen.fem.mass_matrix(points, triangles, sparse=False)[source]

The mass matrix defines an effective area for each vertex.

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices.

  • triangles (ndarray) – Shape (m, 3) array of triangle indices.

  • sparse (bool) – Whether to return a sparse matrix or numpy ndarray.

Return type

Union[ndarray, csc_matrix]

Returns

Shape (n, n) sparse mass matrix or shape (n,) vector of diagonals.

superscreen.fem.laplace_operator(points, triangles, masses=None, weight_method='half_cotangent', sparse=True)[source]

Laplacian operator for the mesh (sometimes called Laplace-Beltrami operator).

The Laplacian operator is defined as inv(M) @ L, where M is the mass matrix and L is the Laplacian matrix.

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices.

  • triangles (ndarray) – Shape (m, 3) array of triangle indices.

  • masses (Union[ndarray, spmatrix, None]) – Pre-computed mass matrix: shape (n, n) sparse diagonal matrix or shape (n,) array of diagonals.

  • weight_method (str) – Method for calculating the weights. One of: “uniform”, “inv_euclidean”, or “half_cotangent”.

  • sparse (bool) – Whether to return a sparse matrix or numpy ndarray.

Return type

Union[ndarray, csr_matrix]

Returns

Shape (n, n) Laplacian operator

superscreen.fem.gradient_triangles(points, triangles, triangle_areas=None)[source]

Returns the triangle gradient operators Gx and Gy.

Given a mesh with n vertices and m triangles and a scalar field f defined at the mesh vertices, Gx and Gy are shape (m, n) matrices such that Gx @ f and Gy @ f compute the gradients of f along x and y, evaluated at the triangle centroids.

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices

  • triangles (ndarray) – Shape (m, 3) array of triangle indices

  • triangle_areas (Optional[ndarray]) – Shape (m, ) array of triangle areas

Return type

Tuple[csr_matrix, csr_matrix]

Returns

x and y gradient matrices, both of which have shape (m, n)

superscreen.fem.gradient_vertices(points, triangles, triangle_areas=None)[source]

Returns the vertex gradient operators gx and gy.

Given a mesh with n vertices and m triangles and a scalar field f defined at the mesh vertices, gx and gy are shape (n, n) matrices such that gx @ f and gy @ f compute the gradients of f along x and y, evaluated at the vertices.

The vertex gradient operators are calculated by averaging the the triangle gradient operators over all triangles adjacent to each vertex.

Parameters
  • points (ndarray) – Shape (n, 2) array of x, y coordinates of vertices

  • triangles (ndarray) – Shape (m, 3) array of triangle indices

  • triangle_areas (Optional[ndarray]) – Shape (m, ) array of triangle areas

Return type

Tuple[csr_matrix, csr_matrix]

Returns

x and y gradient matrices, both of which have shape (n, n)