sktopt.mesh.utils

sktopt.mesh.utils.build_element_adjacency_matrix(mesh)

Returns sparse adjacency matrix A such that A[i, j] = 1 if element i and j share at least one node.

sktopt.mesh.utils.build_element_adjacency_matrix_fast(mesh)
sktopt.mesh.utils.fix_elements_orientation(mesh)
sktopt.mesh.utils.fix_hexahedron_orientation(t, p)

Ensures that each hexahedral element in the mesh has positive volume (i.e., right-handed orientation). Adjusts the order of nodes if needed.

Parameters:
  • t ((8, n_elem) int) – Hexahedral element connectivity (e.g., mesh.t)

  • p ((3, n_nodes) float) – Node coordinates (e.g., mesh.p)

Returns:

t_fixed – Corrected node ordering for each element.

Return type:

(8, n_elem) int

sktopt.mesh.utils.fix_tetrahedron_orientation(t, p)

Returns a corrected version of t where all tetrahedral elements follow a right-handed (positive volume) orientation.

Parameters:
  • t ((4, n_elem) int) – Tetrahedral element connectivity array (e.g., mesh.t in scikit-fem), where each column contains indices of 4 nodes forming one element.

  • p ((3, n_nodes) float) – Coordinates of the mesh nodes (e.g., mesh.p in scikit-fem), where each column represents a node in 3D space.

Returns:

t_fixed – A corrected connectivity array where the node ordering of each tetrahedron is adjusted (if needed) to ensure positive volume.

Return type:

(4, n_elem) int

sktopt.mesh.utils.fix_tetrahedron_orientation_numba(t, p)

Returns a corrected version of t where all tetrahedral elements follow a right-handed (positive volume) orientation.

Parameters:
  • t ((4, n_elem) int) – Tetrahedral element connectivity array (e.g., mesh.t in scikit-fem), where each column contains indices of 4 nodes forming one element.

  • p ((3, n_nodes) float) – Coordinates of the mesh nodes (e.g., mesh.p in scikit-fem), where each column represents a node in 3D space.

Returns:

t_fixed – A corrected connectivity array where the node ordering of each tetrahedron is adjusted (if needed) to ensure positive volume.

Return type:

(4, n_elem) int

sktopt.mesh.utils.get_adjacent_elements(mesh, element_indices)

Given a list of element indices, return the set of elements that are adjacent (share at least one node) with any of them.

sktopt.mesh.utils.get_adjacent_elements_fast(adjacency, element_indices)
sktopt.mesh.utils.get_boundary_nodes_from_elements(elements: ndarray, mesh: Mesh, dirichlet_nodes: ndarray) ndarray

Given a set of element indices, return the indices of boundary (Dirichlet) nodes contained in those elements.

Parameters:
  • elements (np.ndarray) – Array of element indices (e.g., bc_force_elements).

  • mesh (skfem.Mesh) – The skfem mesh object.

  • dirichlet_nodes (np.ndarray) – Global indices of nodes where Dirichlet boundary conditions are applied.

Returns:

boundary_nodes – Unique indices of boundary nodes contained in the specified elements.

Return type:

np.ndarray

sktopt.mesh.utils.get_dofs_in_range(basis: CellBasis, x_range: tuple, y_range: tuple, z_range: tuple) ndarray
sktopt.mesh.utils.get_elements_in_box(mesh: Mesh, x_range: tuple, y_range: tuple, z_range: tuple) ndarray

Return indices of elements whose centroids lie within a given 3D bounding box.

Parameters:
  • mesh (skfem.Mesh) – The finite element mesh containing node and element connectivity data.

  • x_range (tuple of float) – (min, max) range specifying the bounds in the x-direction.

  • y_range (tuple of float) – (min, max) range specifying the bounds in the y-direction.

  • z_range (tuple of float) – (min, max) range specifying the bounds in the z-direction.

Returns:

Array of element indices (integers) whose centroids are within the specified box.

Return type:

np.ndarray

sktopt.mesh.utils.get_elements_with_nodes(mesh: <module 'skfem.mesh' from '/home/runner/.cache/pypoetry/virtualenvs/scikit-topt-t8TcnFm1-py3.11/lib/python3.11/site-packages/skfem/mesh/__init__.py'>, target_nodes_list: list[~numpy.ndarray]) ndarray
sktopt.mesh.utils.get_elements_with_nodes_fast(mesh: Mesh, target_nodes: ndarray | list[ndarray]) ndarray
Fast retrieval of element indices that

contain any of the given node indices.

Parameters:
  • mesh (skfem.Mesh) – The mesh object from scikit-fem.

  • target_nodes (np.ndarray | list[np.ndarray]) – Array or list of arrays of global node indices.

Returns:

elems – Sorted, unique array of element indices that include any of the target nodes.

Return type:

np.ndarray

sktopt.mesh.utils.get_elements_without_nodes(mesh: <module 'skfem.mesh' from '/home/runner/.cache/pypoetry/virtualenvs/scikit-topt-t8TcnFm1-py3.11/lib/python3.11/site-packages/skfem/mesh/__init__.py'>, excluded_nodes_list: list[~numpy.ndarray])
sktopt.mesh.utils.get_nodes_indices_in_range(basis: CellBasis, x_range: tuple, y_range: tuple, z_range: tuple) ndarray

Get the indices of mesh nodes that lie within a specified 3D bounding box.

Parameters:
  • basis (skfem.Basis) – Finite element basis object containing the mesh and node coordinates.

  • x_range (tuple) – (min, max) range for x-axis.

  • y_range (tuple) – (min, max) range for y-axis.

  • z_range (tuple) – (min, max) range for z-axis.

Returns:

Array of indices corresponding to mesh nodes inside the given box.

Return type:

np.ndarray

sktopt.mesh.utils.in_box(coords: ndarray, x_range: tuple, y_range: tuple, z_range: tuple)

Check whether 3D points lie within a given axis-aligned bounding box.

Parameters:
  • coords (np.ndarray, shape (3, N)) – Array of 3D coordinates where each column is a point (x, y, z).

  • x_range (tuple) – Tuple specifying the (min, max) bounds in the x-direction.

  • y_range (tuple) – Tuple specifying the (min, max) bounds in the y-direction.

  • z_range (tuple) – Tuple specifying the (min, max) bounds in the z-direction.

Returns:

Boolean array of shape (N,) where True indicates the point is inside the box.

Return type:

np.ndarray of bool