Finite Volume Methods
The tdgl.finite_volume module contains the implementation of the finite volume methods described
in the Theoretical Background.
Finite Volume Meshes
- class tdgl.finite_volume.Mesh(sites, elements, boundary_indices, areas=None, dual_sites=None, edge_mesh=None, voronoi_polygons=None)[source]
A triangular mesh of a simply- or multiply-connected polygon.
Tip
Use
Mesh.from_triangulation()to create a new mesh from a triangulation.- Parameters:
sites (
Sequence[Tuple[float,float]]) – The (x, y) coordinates of the mesh vertices.elements (
Sequence[Tuple[int,int,int]]) – A list of triplets that correspond to the indices of he vertices that form a triangle. [[0, 1, 2], [0, 1, 3]] corresponds to a triangle connecting vertices 0, 1, and 2 and another triangle connecting vertices 0, 1, and 3.boundary_indices (
Sequence[int]) – Indices corresponding to the boundary.areas (
Optional[Sequence[float]]) – The areas corresponding to the sites.dual_sites (
Optional[Sequence[Tuple[float,float]]]) – The (x, y) coordinates of the dual (Voronoi) mesh verticesvoronoi_polygons (
Optional[List[Sequence[Tuple[float,float]]]]) – A list of Voronoi polygon vertices. There is one set of Voronoi polygon vertices for each mesh site.
- property center_of_mass: Tuple[float, float]
The
(x, y)coordinates of the center of mass of the mesh.
- static from_triangulation(sites, elements, create_submesh=True)[source]
Create a triangular mesh from the coordinates of the triangle vertices and a list of indices corresponding to the vertices that connect to triangles.
- Parameters:
sites (
Sequence[Tuple[float,float]]) – The (x, y) coordinates of the mesh sites.elements (
Sequence[Tuple[int,int,int]]) – A list of triplets that correspond to the indices of the vertices that form a triangle. E.g. [[0, 1, 2], [0, 1, 3]] corresponds to a triangle connecting vertices 0, 1, and 2 and another triangle connecting vertices 0, 1, and 3.create_submesh (
bool) – Whether to generate the correspondingtdgl.finit_volume.EdgeMeshand Voronoi dual mesh.
- Return type:
- Returns:
A new
tdgl.finite_volume.Meshinstance
- static compute_voronoi_areas_polygons(sites, elements, dual_sites, edge_mesh, boundary_indices)[source]
Compute the area and indices of the Voronoi region for each vertex.
- Parameters:
sites (
ndarray) – The (x, y) coordinates of the mesh sites.elements (
ndarray) – The mesh triangle indices.dual_sites (
ndarray) – The (x, y) coordinates of the dual mesh vertices.edge_mesh (
EdgeMesh) – Atdgl.finite_volume.EdgeMeshinstance for the triangulation defined bysitesandelements.boundary_indices (
ndarray) – The site indices corresponding to the boundary.
- Return type:
- Returns:
The Voronoi cell areas and the counterclockwise-oriented vertices of the Voronoi cells.
- get_quantity_on_site(quantity_on_edge, vector=True, use_cupy=False)[source]
Compute the quantity on site by averaging over all edges connecting to each site.
- smooth(iterations, create_submesh=True)[source]
Perform Laplacian smoothing of the mesh, i.e., moving each interior vertex to the arithmetic average of its neighboring points.
- Parameters:
- Return type:
- Returns:
A new
tdgl.finite_volume.Meshwith relaxed vertex positions.
- plot(ax=None, show_sites=True, show_edges=False, show_dual_edges=True, show_voronoi_centroids=False, site_color=None, edge_color='k', centroid_color=None, dual_edge_color='k', linewidth=0.75, linestyle='-', marker='.')[source]
Plot the mesh.
- Parameters:
ax (
Optional[Axes]) – Aplt.Axesinstance on which to plot the mesh.show_sites (
bool) – Whether to show the mesh sites.show_edges (
bool) – Whether to show the mesh edges.show_dual_edges (
bool) – Whether to show the dual mesh edges.show_voronoi_centroids (
bool) – Whether to show the centroid of each Voronoi cell.site_color (
Union[str,Sequence[float],None]) – The color for the sites.edge_color (
Union[str,Sequence[float],None]) – The color for the edges.dual_edge_color (
Union[str,Sequence[float],None]) – The color for the dual edges.centroid_color (
Union[str,Sequence[float],None]) – The color for the Voronoi centroids.linewidth (
float) – The line width for all edges.linestyle (
str) – The line style for all edges.marker (
str) – The marker to use for the mesh sites and Voronoi centroids.
- Return type:
- Returns:
The resulting
plt.Axes
- static from_hdf5(h5group)[source]
Load a mesh from an HDF5 file.
- Parameters:
h5group (
Group) – The HDF5 group to load the mesh from.- Return type:
- Returns:
The loaded mesh.
- static is_restorable(h5group)[source]
Returns
Trueif theh5py.Groupcontains all of the data necessary to create atdgl.finite_volume.Meshwithout re-computing any values.- Parameters:
h5group (
Group) – Theh5py.Groupto check.- Return type:
- Returns:
Whether the mesh can be restored from the given group.
- class tdgl.finite_volume.EdgeMesh(centers, edges, boundary_edge_indices, directions, edge_lengths, dual_edge_lengths)[source]
A mesh composed of the edges in a triangular mesh.
Tip
Use
EdgeMesh.from_mesh()to create from an existing mesh.- Parameters:
centers (
Sequence[Tuple[float,float]]) – The (x, y) coordinates for the edge_centers.edges (
Sequence[Tuple[int,int]]) – The edges as a sequence of indices.boundary_edge_indices (
Sequence[int]) – Edges on the boundary.directions (
Sequence[Tuple[float,float]]) – Directions of the edges.dual_edge_lengths (
Sequence[float]) – Length of the dual edges.
Matrices
- class tdgl.finite_volume.MeshOperators(mesh, sparse_solver, use_cupy=False, fixed_sites=None, fix_psi=True)[source]
A container for the finite volume operators for a given mesh.
- Parameters:
mesh (
Mesh) – Thetdgl.finite_volume.Meshinstance for which to construct operators.sparse_solver (
SparseSolver) – The sparse solver for which to build mesh operators.use_cupy (
bool) – Use CuPy for linear algebra.fixed_sites (
Optional[ndarray]) – The indices of any sites for which the value of \(\psi\) and \(\mu\) are fixed as boundary conditions.fix_psi (bool) –
- tdgl.finite_volume.operators.build_divergence(mesh)[source]
Build the divergence matrix that takes the divergence of a function living on the edges onto the sites.
- tdgl.finite_volume.operators.build_gradient(mesh, link_exponents=None, weights=None)[source]
Build the gradient for a function living on the sites onto the edges.
- tdgl.finite_volume.operators.build_laplacian(mesh, link_exponents=None, fixed_sites=None, free_rows=None, fixed_sites_eigenvalues=1, weights=None)[source]
Build a Laplacian matrix on a given mesh.
The default boundary condition is homogenous Neumann conditions. To get Dirichlet conditions, add fixed sites. To get non-homogenous Neumann condition, the flux needs to be specified using a Neumann boundary Laplacian matrix.