Coverage for src / beamme / four_c / model_importer.py: 93%

75 statements  

« prev     ^ index     » next       coverage.py v7.12.0, created at 2025-11-21 12:57 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2018-2025 BeamMe Authors 

4# 

5# Permission is hereby granted, free of charge, to any person obtaining a copy 

6# of this software and associated documentation files (the "Software"), to deal 

7# in the Software without restriction, including without limitation the rights 

8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 

9# copies of the Software, and to permit persons to whom the Software is 

10# furnished to do so, subject to the following conditions: 

11# 

12# The above copyright notice and this permission notice shall be included in 

13# all copies or substantial portions of the Software. 

14# 

15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 

16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 

18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 

19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 

20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 

21# THE SOFTWARE. 

22"""This module contains functions to load and parse existing 4C input files.""" 

23 

24from collections import defaultdict as _defaultdict 

25from pathlib import Path as _Path 

26from typing import Tuple as _Tuple 

27 

28from beamme.core.boundary_condition import BoundaryCondition as _BoundaryCondition 

29from beamme.core.boundary_condition import ( 

30 BoundaryConditionBase as _BoundaryConditionBase, 

31) 

32from beamme.core.conf import bme as _bme 

33from beamme.core.coupling import Coupling as _Coupling 

34from beamme.core.geometry_set import GeometrySetNodes as _GeometrySetNodes 

35from beamme.core.mesh import Mesh as _Mesh 

36from beamme.core.node import Node as _Node 

37from beamme.four_c.input_file import InputFile as _InputFile 

38from beamme.four_c.input_file_mappings import ( 

39 INPUT_FILE_MAPPINGS as _INPUT_FILE_MAPPINGS, 

40) 

41from beamme.four_c.material import MaterialSolid as _MaterialSolid 

42from beamme.utils.environment import cubitpy_is_available as _cubitpy_is_available 

43 

44if _cubitpy_is_available(): 

45 from cubitpy.cubit_to_fourc_input import ( 

46 get_input_file_with_mesh as _get_input_file_with_mesh, 

47 ) 

48 

49 

50def import_cubitpy_model( 

51 cubit, convert_input_to_mesh: bool = False 

52) -> _Tuple[_InputFile, _Mesh]: 

53 """Convert a CubitPy instance to a BeamMe InputFile. 

54 

55 Args: 

56 cubit (CubitPy): An instance of a cubit model. 

57 convert_input_to_mesh: If this is false, the cubit model will be 

58 converted to plain FourCIPP input data. If this is true, an input 

59 file with all the parameters will be returned and a mesh which 

60 contains the mesh information from cubit converted to BeamMe 

61 objects. 

62 

63 Returns: 

64 A tuple with the input file and the mesh. If convert_input_to_mesh is 

65 False, the mesh will be empty. Note that the input sections which are 

66 converted to a BeamMe mesh are removed from the input file object. 

67 """ 

68 

69 input_file = _InputFile(sections=_get_input_file_with_mesh(cubit).sections) 

70 

71 if convert_input_to_mesh: 

72 return _extract_mesh_sections(input_file) 

73 else: 

74 return input_file, _Mesh() 

75 

76 

77def import_four_c_model( 

78 input_file_path: _Path, convert_input_to_mesh: bool = False 

79) -> _Tuple[_InputFile, _Mesh]: 

80 """Import an existing 4C input file and optionally convert it into a BeamMe 

81 mesh. 

82 

83 Args: 

84 input_file_path: A file path to an existing 4C input file that will be 

85 imported. 

86 convert_input_to_mesh: If True, the input file will be converted to a 

87 BeamMe mesh. 

88 

89 Returns: 

90 A tuple with the input file and the mesh. If convert_input_to_mesh is 

91 False, the mesh will be empty. Note that the input sections which are 

92 converted to a BeamMe mesh are removed from the input file object. 

93 """ 

94 

95 input_file = _InputFile().from_4C_yaml(input_file_path=input_file_path) 

96 

97 if convert_input_to_mesh: 

98 return _extract_mesh_sections(input_file) 

99 else: 

100 return input_file, _Mesh() 

101 

102 

103def _extract_mesh_sections(input_file: _InputFile) -> _Tuple[_InputFile, _Mesh]: 

104 """Convert an InputFile into a native mesh by translating sections like 

105 materials, nodes, elements, geometry sets, and boundary conditions. 

106 

107 Args: 

108 input_file: The input file containing 4C sections. 

109 Returns: 

110 A tuple (input_file, mesh). The input_file is modified in place to remove 

111 sections converted into BeamMe objects. 

112 """ 

113 

114 # function to pop sections from the input file 

115 _pop_section = lambda name: input_file.pop(name, []) 

116 

117 # convert all sections to native objects and add to a new mesh 

118 mesh = _Mesh() 

119 

120 # extract materials 

121 material_id_map = {} 

122 

123 for mat in _pop_section("MATERIALS"): 

124 mat_id = mat.pop("MAT") 

125 if len(mat) != 1: 

126 raise ValueError( 

127 f"Could not convert the material data `{mat}` to a BeamMe material!" 

128 ) 

129 mat_name, mat_data = list(mat.items())[0] 

130 material = _MaterialSolid(material_string=mat_name, data=mat_data) 

131 mesh.add(material) 

132 material_id_map[mat_id] = material 

133 

134 # extract nodes 

135 mesh.nodes = [_Node(node["COORD"]) for node in _pop_section("NODE COORDS")] 

136 

137 # extract elements 

138 for input_element in _pop_section("STRUCTURE ELEMENTS"): 

139 if ( 

140 input_element["cell"]["type"] 

141 not in _INPUT_FILE_MAPPINGS["element_four_c_string_to_type"] 

142 ): 

143 raise TypeError( 

144 f"Could not create a BeamMe element for `{input_element['data']['type']}` `{input_element['cell']['type']}`!" 

145 ) 

146 nodes = [mesh.nodes[i - 1] for i in input_element["cell"]["connectivity"]] 

147 element_class = _INPUT_FILE_MAPPINGS["element_four_c_string_to_type"][ 

148 input_element["cell"]["type"] 

149 ] 

150 element = element_class(nodes=nodes, data=input_element["data"]) 

151 if "MAT" in element.data: 

152 element.data["MAT"] = material_id_map[element.data["MAT"]] 

153 mesh.elements.append(element) 

154 

155 # extract geometry sets 

156 geometry_sets_in_sections: dict[str, dict[int, _GeometrySetNodes]] = _defaultdict( 

157 dict 

158 ) 

159 

160 for section_name in input_file.sections: 

161 if not section_name.endswith("TOPOLOGY"): 

162 continue 

163 

164 items = _pop_section(section_name) 

165 if not items: 

166 continue 

167 

168 # Find geometry type for this section 

169 try: 

170 geometry_type = _INPUT_FILE_MAPPINGS[ 

171 "geometry_sets_condition_to_geometry_name" 

172 ][section_name] 

173 except ValueError as e: 

174 raise ValueError(f"Unknown geometry section: {section_name}") from e 

175 

176 # Extract geometry set indices 

177 geom_dict: dict[int, list[int]] = _defaultdict(list) 

178 for entry in items: 

179 geom_dict[entry["d_id"]].append(entry["node_id"] - 1) 

180 

181 geometry_sets_in_sections[geometry_type] = { 

182 gid: _GeometrySetNodes(geometry_type, nodes=[mesh.nodes[i] for i in ids]) 

183 for gid, ids in geom_dict.items() 

184 } 

185 

186 mesh.geometry_sets[geometry_type] = list( 

187 geometry_sets_in_sections[geometry_type].values() 

188 ) 

189 

190 # extract boundary conditions 

191 _standard_bc_types = ( 

192 _bme.bc.dirichlet, 

193 _bme.bc.neumann, 

194 _bme.bc.locsys, 

195 _bme.bc.beam_to_solid_surface_meshtying, 

196 _bme.bc.beam_to_solid_surface_contact, 

197 _bme.bc.beam_to_solid_volume_meshtying, 

198 ) 

199 

200 for (bc_key, geometry_type), section_name in _INPUT_FILE_MAPPINGS[ 

201 "boundary_conditions" 

202 ].items(): 

203 for bc_data in _pop_section(section_name): 

204 geometry_set = geometry_sets_in_sections[geometry_type][bc_data.pop("E")] 

205 

206 bc_obj: _BoundaryConditionBase 

207 

208 if bc_key in _standard_bc_types or isinstance(bc_key, str): 

209 bc_obj = _BoundaryCondition(geometry_set, bc_data, bc_type=bc_key) 

210 elif bc_key is _bme.bc.point_coupling: 

211 bc_obj = _Coupling( 

212 geometry_set, bc_key, bc_data, check_overlapping_nodes=False 

213 ) 

214 else: 

215 raise ValueError(f"Unexpected boundary condition: {bc_key}") 

216 

217 mesh.boundary_conditions.append((bc_key, geometry_type), bc_obj) 

218 

219 return input_file, mesh