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
, 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 bypoly_points
.- Parameters:
poly_points (
ndarray
) – Shape(m, 2)
array of polygon vertex coordinates.query_points (
ndarray
) – Shape(n, 2)
array of “query points”.radius (
float
) – Seematplotlib.path.Path.contains_points()
.
- Return type:
- Returns:
A shape
(n, )
boolean array indicating whichquery_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).
- superscreen.fem.adjacency_matrix(triangles, sparse=True)[source]¶
Computes the adjacency matrix for a given set of triangles.
- Parameters:
- Return type:
- 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.
- superscreen.fem.weights_half_cotangent(points, triangles, sparse=True)[source]¶
Weights edges by half of the cotangent of the two angles opposite the edge.
- 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:
- 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:
- Return type:
- 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:
- Returns:
Shape (n, n) Laplacian operator
- superscreen.fem.gradient_triangles(points, triangles, triangle_areas=None)[source]¶
Returns the triangle gradient operators
Gx
andGy
.Given a mesh with
n
vertices andm
triangles and a scalar fieldf
defined at the mesh vertices,Gx
andGy
are shape(m, n)
matrices such thatGx @ f
andGy @ f
compute the gradients off
along x and y, evaluated at the triangle centroids.- Parameters:
- Return type:
- 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
andgy
.Given a mesh with
n
vertices andm
triangles and a scalar fieldf
defined at the mesh vertices,gx
andgy
are shape(n, n)
matrices such thatgx @ f
andgy @ f
compute the gradients off
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:
- Return type:
- Returns:
x and y gradient matrices, both of which have shape
(n, n)