Coverage for /dolfinx-env/lib/python3.14/site-packages/io4dolfinx/backends/nastran/helpers.py: 96%

46 statements  

« prev     ^ index     » next       coverage.py v7.18.0, created at 2026-11-30 11:58 +0000

1""" 

2Helpers for the Nastran backend: reading the elements and GRID IDs of a Nastran bulk data 

3file through Gmsh. 

4 

5SPDX License identifier: MIT 

6""" 

7 

8from __future__ import annotations 

9 

10from pathlib import Path 

11from typing import NamedTuple 

12 

13import dolfinx 

14import gmsh 

15import numpy as np 

16import numpy.typing as npt 

17from dolfinx.io.gmsh import _gmsh_to_cells, cell_perm_array, extract_topology_and_markers 

18 

19__all__ = [ 

20 "GRID", 

21 "PROPERTY_TO_DIM", 

22 "ElementBlock", 

23 "read_elements", 

24 "get_single_cell_type", 

25 "get_vertex_nodes", 

26] 

27 

28#: Name of the GRID IDs as point data and vertex tags 

29GRID = "GRID" 

30 

31#: Topological dimension of the elements of each property card 

32PROPERTY_TO_DIM = {"PSOLID": 3, "PSHELL": 2} 

33 

34 

35class ElementBlock(NamedTuple): 

36 """The elements of one type in a Nastran file.""" 

37 

38 cell_type: str #: DOLFINx cell type 

39 degree: int #: Degree of the elements 

40 #: Element nodes as zero-based node indices in DOLFINx ordering, of shape 

41 #: ``(num_elements, num_nodes_per_element)`` 

42 nodes: npt.NDArray[np.int64] 

43 pids: npt.NDArray[np.int32] #: Property ID of each element 

44 

45 

46def read_elements( 

47 filename: Path | str, 

48) -> tuple[dict[int, list[ElementBlock]], npt.NDArray[np.int64]]: 

49 """Read the elements of a Nastran file into the current Gmsh model, by dimension. 

50 

51 Gmsh keeps the GRID IDs as node tags, which :mod:`dolfinx.io.gmsh` requires to be 

52 ``1, ..., num_nodes``, so the nodes are renumbered: node ``i`` is the node with the 

53 ``i``-th smallest GRID ID. 

54 

55 Args: 

56 filename: Path to the Nastran file 

57 

58 Returns: 

59 Tuple containing: 

60 - Map from topological dimension to the element blocks of that dimension, 

61 one per element type, sorted by DOLFINx cell type 

62 - The GRID ID of each node 

63 """ 

64 gmsh.merge(str(filename)) 

65 grid_ids = np.sort(gmsh.model.mesh.getNodes()[0]).astype(np.int64) 

66 gmsh.model.mesh.renumberNodes(grid_ids, np.arange(1, len(grid_ids) + 1)) 

67 # Gmsh puts each PID in an elementary entity; dolfinx.io.gmsh only reads physical groups 

68 for dim, tag in gmsh.model.getEntities(): 

69 gmsh.model.addPhysicalGroup(dim=dim, tags=[tag], tag=tag) 

70 topologies, _ = extract_topology_and_markers(gmsh.model) 

71 if not topologies: 

72 raise ValueError(f"No elements found in {filename}.") 

73 elements: dict[int, list[ElementBlock]] = {} 

74 for gmsh_type, data in topologies.items(): 

75 if gmsh_type not in _gmsh_to_cells: 

76 name = gmsh.model.mesh.getElementProperties(gmsh_type)[0] 

77 raise NotImplementedError(f"Unsupported element type '{name}' in {filename}.") 

78 cell_type, degree = _gmsh_to_cells[gmsh_type] 

79 nodes = data["topology"].astype(np.int64) 

80 perm = cell_perm_array(dolfinx.mesh.to_type(cell_type), nodes.shape[1]) 

81 dim = dolfinx.mesh.cell_dim(dolfinx.mesh.to_type(cell_type)) 

82 pids = data["cell_data"].astype(np.int32) 

83 elements.setdefault(dim, []).append(ElementBlock(cell_type, degree, nodes[:, perm], pids)) 

84 for blocks in elements.values(): 

85 blocks.sort(key=lambda block: dolfinx.mesh.to_type(block.cell_type).value) 

86 return elements, grid_ids 

87 

88 

89def get_single_cell_type( 

90 elements: dict[int, list[ElementBlock]], what: str, filename: Path | str 

91) -> ElementBlock: 

92 """The cells of a mesh, which must be of one type. 

93 

94 Args: 

95 elements: The element blocks of the file, by dimension 

96 what: Description of the data for the error message 

97 filename: Path to the Nastran file 

98 

99 Returns: 

100 The element block of the cells 

101 

102 Raises: 

103 NotImplementedError: If the cells are of several types (a mixed mesh). 

104 """ 

105 blocks = elements[max(elements)] 

106 if len(blocks) > 1: 

107 types = [block.cell_type for block in blocks] 

108 raise NotImplementedError( 

109 f"{what} is not supported on meshes with several cell types {types}, as in {filename}." 

110 ) 

111 return blocks[0] 

112 

113 

114def get_vertex_nodes(cells: ElementBlock) -> npt.NDArray[np.int64]: 

115 """The nodes at the vertices of the cells, sorted. 

116 

117 Higher-order elements also have nodes on edges, faces and in the interior, which are 

118 not vertices. In DOLFINx ordering, the vertex nodes come first. 

119 """ 

120 num_vertices = dolfinx.cpp.mesh.cell_num_vertices(dolfinx.mesh.to_type(cells.cell_type)) 

121 return np.unique(cells.nodes[:, :num_vertices])