Coverage for lasso/dyna/test_mapper.py: 92%

102 statements  

« prev     ^ index     » next       coverage.py v7.2.4, created at 2023-04-28 18:45 +0100

1from typing import Dict, Set, Tuple, Union 

2from unittest import TestCase 

3 

4import numpy as np 

5 

6from ..femzip.fz_config import FemzipArrayType, FemzipVariableCategory 

7from .array_type import ArrayType 

8from .femzip_mapper import FemzipMapper 

9 

10part_global_femzip_translations: Dict[Tuple[FemzipArrayType, FemzipVariableCategory], Set[str]] = { 

11 # GLOBAL 

12 (FemzipArrayType.GLOBAL_DATA, FemzipVariableCategory.GLOBAL): { 

13 # ArrayType.global_timesteps, 

14 ArrayType.global_internal_energy, 

15 ArrayType.global_kinetic_energy, 

16 ArrayType.global_total_energy, 

17 ArrayType.global_velocity, 

18 }, 

19 # PART 

20 (FemzipArrayType.PART_RESULTS, FemzipVariableCategory.PART): { 

21 ArrayType.part_hourglass_energy, 

22 ArrayType.part_internal_energy, 

23 ArrayType.part_kinetic_energy, 

24 ArrayType.part_mass, 

25 ArrayType.part_velocity, 

26 }, 

27} 

28 

29element_nope_femzip_translations: Dict[Tuple[str, FemzipVariableCategory], str] = { 

30 # NODE 

31 ( 

32 FemzipArrayType.NODE_DISPLACEMENT.value, 

33 FemzipVariableCategory.NODE, 

34 ): ArrayType.node_displacement, 

35 ( 

36 FemzipArrayType.NODE_ACCELERATIONS.value, 

37 FemzipVariableCategory.NODE, 

38 ): ArrayType.node_acceleration, 

39 (FemzipArrayType.NODE_VELOCITIES.value, FemzipVariableCategory.NODE): ArrayType.node_velocity, 

40 ( 

41 FemzipArrayType.NODE_TEMPERATURES.value, 

42 FemzipVariableCategory.NODE, 

43 ): ArrayType.node_temperature, 

44 (FemzipArrayType.NODE_HEAT_FLUX.value, FemzipVariableCategory.NODE): ArrayType.node_heat_flux, 

45 ( 

46 FemzipArrayType.NODE_MASS_SCALING.value, 

47 FemzipVariableCategory.NODE, 

48 ): ArrayType.node_mass_scaling, 

49 ( 

50 FemzipArrayType.NODE_TEMPERATURE_GRADIENT.value, 

51 FemzipVariableCategory.NODE, 

52 ): ArrayType.node_temperature_gradient, 

53 # BEAM 

54 ( 

55 FemzipArrayType.BEAM_AXIAL_FORCE.value, 

56 FemzipVariableCategory.BEAM, 

57 ): ArrayType.element_beam_axial_force, 

58 ( 

59 FemzipArrayType.BEAM_S_BENDING_MOMENT.value, 

60 FemzipVariableCategory.BEAM, 

61 ): ArrayType.element_beam_bending_moment, 

62 ( 

63 FemzipArrayType.BEAM_T_BENDING_MOMENT.value, 

64 FemzipVariableCategory.BEAM, 

65 ): ArrayType.element_beam_bending_moment, 

66 ( 

67 FemzipArrayType.BEAM_S_SHEAR_RESULTANT.value, 

68 FemzipVariableCategory.BEAM, 

69 ): ArrayType.element_beam_shear_force, 

70 ( 

71 FemzipArrayType.BEAM_T_SHEAR_RESULTANT.value, 

72 FemzipVariableCategory.BEAM, 

73 ): ArrayType.element_beam_shear_force, 

74 ( 

75 FemzipArrayType.BEAM_TORSIONAL_MOMENT.value, 

76 FemzipVariableCategory.BEAM, 

77 ): ArrayType.element_beam_torsion_moment, 

78 (FemzipArrayType.STRESS_X.value, FemzipVariableCategory.SHELL): ArrayType.element_shell_stress, 

79 (FemzipArrayType.STRESS_Y.value, FemzipVariableCategory.SHELL): ArrayType.element_shell_stress, 

80 (FemzipArrayType.STRESS_Z.value, FemzipVariableCategory.SHELL): ArrayType.element_shell_stress, 

81 (FemzipArrayType.STRESS_XY.value, FemzipVariableCategory.SHELL): ArrayType.element_shell_stress, 

82 (FemzipArrayType.STRESS_YZ.value, FemzipVariableCategory.SHELL): ArrayType.element_shell_stress, 

83 (FemzipArrayType.STRESS_XZ.value, FemzipVariableCategory.SHELL): ArrayType.element_shell_stress, 

84 ( 

85 FemzipArrayType.EFF_PSTRAIN.value, 

86 FemzipVariableCategory.SHELL, 

87 ): ArrayType.element_shell_effective_plastic_strain, 

88 ( 

89 FemzipArrayType.HISTORY_VARS.value, 

90 FemzipVariableCategory.SHELL, 

91 ): ArrayType.element_shell_history_vars, 

92 ( 

93 FemzipArrayType.BENDING_MOMENT_MX.value, 

94 FemzipVariableCategory.SHELL, 

95 ): ArrayType.element_shell_bending_moment, 

96 ( 

97 FemzipArrayType.BENDING_MOMENT_MY.value, 

98 FemzipVariableCategory.SHELL, 

99 ): ArrayType.element_shell_bending_moment, 

100 ( 

101 FemzipArrayType.BENDING_MOMENT_MXY.value, 

102 FemzipVariableCategory.SHELL, 

103 ): ArrayType.element_shell_bending_moment, 

104 ( 

105 FemzipArrayType.SHEAR_FORCE_X.value, 

106 FemzipVariableCategory.SHELL, 

107 ): ArrayType.element_shell_shear_force, 

108 ( 

109 FemzipArrayType.SHEAR_FORCE_Y.value, 

110 FemzipVariableCategory.SHELL, 

111 ): ArrayType.element_shell_shear_force, 

112 ( 

113 FemzipArrayType.NORMAL_FORCE_X.value, 

114 FemzipVariableCategory.SHELL, 

115 ): ArrayType.element_shell_normal_force, 

116 ( 

117 FemzipArrayType.NORMAL_FORCE_Y.value, 

118 FemzipVariableCategory.SHELL, 

119 ): ArrayType.element_shell_normal_force, 

120 ( 

121 FemzipArrayType.NORMAL_FORCE_XY.value, 

122 FemzipVariableCategory.SHELL, 

123 ): ArrayType.element_shell_normal_force, 

124 ( 

125 FemzipArrayType.THICKNESS.value, 

126 FemzipVariableCategory.SHELL, 

127 ): ArrayType.element_shell_thickness, 

128 ( 

129 FemzipArrayType.UNKNOWN_1.value, 

130 FemzipVariableCategory.SHELL, 

131 ): ArrayType.element_shell_unknown_variables, 

132 ( 

133 FemzipArrayType.UNKNOWN_2.value, 

134 FemzipVariableCategory.SHELL, 

135 ): ArrayType.element_shell_unknown_variables, 

136 ( 

137 FemzipArrayType.STRAIN_INNER_X.value, 

138 FemzipVariableCategory.SHELL, 

139 ): ArrayType.element_shell_strain, 

140 ( 

141 FemzipArrayType.STRAIN_INNER_Y.value, 

142 FemzipVariableCategory.SHELL, 

143 ): ArrayType.element_shell_strain, 

144 ( 

145 FemzipArrayType.STRAIN_INNER_Z.value, 

146 FemzipVariableCategory.SHELL, 

147 ): ArrayType.element_shell_strain, 

148 ( 

149 FemzipArrayType.STRAIN_INNER_XY.value, 

150 FemzipVariableCategory.SHELL, 

151 ): ArrayType.element_shell_strain, 

152 ( 

153 FemzipArrayType.STRAIN_INNER_YZ.value, 

154 FemzipVariableCategory.SHELL, 

155 ): ArrayType.element_shell_strain, 

156 ( 

157 FemzipArrayType.STRAIN_INNER_XZ.value, 

158 FemzipVariableCategory.SHELL, 

159 ): ArrayType.element_shell_strain, 

160 ( 

161 FemzipArrayType.STRAIN_OUTER_X.value, 

162 FemzipVariableCategory.SHELL, 

163 ): ArrayType.element_shell_strain, 

164 ( 

165 FemzipArrayType.STRAIN_OUTER_Y.value, 

166 FemzipVariableCategory.SHELL, 

167 ): ArrayType.element_shell_strain, 

168 ( 

169 FemzipArrayType.STRAIN_OUTER_Z.value, 

170 FemzipVariableCategory.SHELL, 

171 ): ArrayType.element_shell_strain, 

172 ( 

173 FemzipArrayType.STRAIN_OUTER_XY.value, 

174 FemzipVariableCategory.SHELL, 

175 ): ArrayType.element_shell_strain, 

176 ( 

177 FemzipArrayType.STRAIN_OUTER_YZ.value, 

178 FemzipVariableCategory.SHELL, 

179 ): ArrayType.element_shell_strain, 

180 ( 

181 FemzipArrayType.STRAIN_OUTER_XZ.value, 

182 FemzipVariableCategory.SHELL, 

183 ): ArrayType.element_shell_strain, 

184 ( 

185 FemzipArrayType.INTERNAL_ENERGY.value, 

186 FemzipVariableCategory.SHELL, 

187 ): ArrayType.element_shell_internal_energy, 

188 # SOLID 

189 (FemzipArrayType.STRESS_X.value, FemzipVariableCategory.SOLID): ArrayType.element_solid_stress, 

190 (FemzipArrayType.STRESS_Y.value, FemzipVariableCategory.SOLID): ArrayType.element_solid_stress, 

191 (FemzipArrayType.STRESS_Z.value, FemzipVariableCategory.SOLID): ArrayType.element_solid_stress, 

192 (FemzipArrayType.STRESS_XY.value, FemzipVariableCategory.SOLID): ArrayType.element_solid_stress, 

193 (FemzipArrayType.STRESS_YZ.value, FemzipVariableCategory.SOLID): ArrayType.element_solid_stress, 

194 (FemzipArrayType.STRESS_XZ.value, FemzipVariableCategory.SOLID): ArrayType.element_solid_stress, 

195 ( 

196 FemzipArrayType.EFF_PSTRAIN.value, 

197 FemzipVariableCategory.SOLID, 

198 ): ArrayType.element_solid_effective_plastic_strain, 

199 ( 

200 FemzipArrayType.STRAIN_INNER_X.value, 

201 FemzipVariableCategory.SOLID, 

202 ): ArrayType.element_solid_strain, 

203 ( 

204 FemzipArrayType.STRAIN_INNER_Y.value, 

205 FemzipVariableCategory.SOLID, 

206 ): ArrayType.element_solid_strain, 

207 ( 

208 FemzipArrayType.STRAIN_INNER_Z.value, 

209 FemzipVariableCategory.SOLID, 

210 ): ArrayType.element_solid_strain, 

211 ( 

212 FemzipArrayType.STRAIN_INNER_XY.value, 

213 FemzipVariableCategory.SOLID, 

214 ): ArrayType.element_solid_strain, 

215 ( 

216 FemzipArrayType.HISTORY_VARS.value, 

217 FemzipVariableCategory.SOLID, 

218 ): ArrayType.element_solid_history_variables, 

219 ( 

220 FemzipArrayType.STRAIN_INNER_YZ.value, 

221 FemzipVariableCategory.SOLID, 

222 ): ArrayType.element_solid_strain, 

223 ( 

224 FemzipArrayType.STRAIN_INNER_XZ.value, 

225 FemzipVariableCategory.SOLID, 

226 ): ArrayType.element_solid_strain, 

227 ( 

228 FemzipArrayType.STRAIN_OUTER_X.value, 

229 FemzipVariableCategory.SOLID, 

230 ): ArrayType.element_solid_strain, 

231 ( 

232 FemzipArrayType.STRAIN_OUTER_Y.value, 

233 FemzipVariableCategory.SOLID, 

234 ): ArrayType.element_solid_strain, 

235 ( 

236 FemzipArrayType.STRAIN_OUTER_Z.value, 

237 FemzipVariableCategory.SOLID, 

238 ): ArrayType.element_solid_strain, 

239 ( 

240 FemzipArrayType.STRAIN_OUTER_XY.value, 

241 FemzipVariableCategory.SOLID, 

242 ): ArrayType.element_solid_strain, 

243 ( 

244 FemzipArrayType.STRAIN_OUTER_YZ.value, 

245 FemzipVariableCategory.SOLID, 

246 ): ArrayType.element_solid_strain, 

247 ( 

248 FemzipArrayType.STRAIN_OUTER_XZ.value, 

249 FemzipVariableCategory.SOLID, 

250 ): ArrayType.element_solid_strain, 

251} 

252 

253 

254class MapperTest(TestCase): 

255 def validate( 

256 self, 

257 fz: Dict[Tuple[str, FemzipVariableCategory], np.ndarray], 

258 d3plot_shape: tuple, 

259 data_index_positions: Union[tuple, slice] = slice(None), 

260 ): 

261 """Validate that the arrays have the same shape and that the 

262 raw data has been allocated to the correct positions in the 

263 d3plot arrays. 

264 

265 Parameters 

266 ---------- 

267 fz: 

268 femzip data following the same schema as in the api 

269 d3plot_shape: 

270 shape of the d3plot array 

271 data_index_positions: 

272 positions of the raw data in the d3plot array 

273 """ 

274 d3plot_name = [] 

275 

276 m = FemzipMapper() 

277 

278 # filter out parts and globals if they exist 

279 for key in fz.keys(): 

280 if key in element_nope_femzip_translations: 

281 d3plot_name.append(element_nope_femzip_translations[key]) 

282 

283 d3plot_name = set(d3plot_name).pop() 

284 

285 m.map(fz) 

286 

287 _ = m.d3plot_arrays[d3plot_name] 

288 

289 def test_nodal_disp(self): 

290 m = FemzipMapper() 

291 

292 # NODE DISPLACEMENT 

293 nd = np.random.randn(2, 10, 3) 

294 

295 fz = {(1, FemzipArrayType.NODE_DISPLACEMENT.value, FemzipVariableCategory.NODE): nd} 

296 

297 m.map(fz) 

298 

299 result = m.d3plot_arrays 

300 self.assertTrue(np.allclose(nd, result["node_displacement"])) 

301 

302 def test_tshells(self): 

303 d = np.random.randn(2, 2) 

304 

305 fz = { 

306 (1, "Sigma-z (IP 2)", FemzipVariableCategory.THICK_SHELL): d, 

307 (2, "Epsilon-xy (inner)", FemzipVariableCategory.THICK_SHELL): d, 

308 } 

309 

310 m = FemzipMapper() 

311 

312 m.map(fz) 

313 

314 r = m.d3plot_arrays 

315 

316 self.assertEqual(r[ArrayType.element_tshell_stress].shape, (2, 2, 2, 3)) 

317 self.assertTrue(np.allclose(r[ArrayType.element_tshell_stress][:, :, 1, 2], d)) 

318 

319 self.assertEqual(r[ArrayType.element_tshell_strain].shape, (2, 2, 1, 4)) 

320 self.assertTrue(np.allclose(r[ArrayType.element_tshell_strain][:, :, 0, 3], d)) 

321 

322 def test_internal_shell_energy(self): 

323 interal_energy = np.array([[1, 1, 0.12, 2.121202, 2.1123, 7.213]]).reshape(2, 3) 

324 

325 fz = { 

326 (1, "internal_energy", FemzipVariableCategory.SHELL): interal_energy, 

327 } 

328 m = FemzipMapper() 

329 

330 m.map(fz) 

331 

332 r = m.d3plot_arrays[ArrayType.element_shell_internal_energy] 

333 self.assertTrue(np.allclose(r, interal_energy)) 

334 

335 def test_dependent_variable(self): 

336 d1 = np.random.randn(2, 1200) 

337 d2 = np.random.randn(2, 1200) 

338 

339 fz: Dict[Tuple[int, str, FemzipVariableCategory], np.ndarray] = { 

340 (1, "element_dependent_variable_2", FemzipVariableCategory.SHELL): d2, 

341 (2, "element_dependent_variable_1", FemzipVariableCategory.SHELL): d1, 

342 } 

343 m = FemzipMapper() 

344 

345 m.map(fz) 

346 

347 r = m.d3plot_arrays 

348 

349 self.assertEqual(r[ArrayType.element_shell_unknown_variables].shape, (2, 1200, 2)) 

350 

351 self.assertTrue(np.allclose(d1, r[ArrayType.element_shell_unknown_variables][:, :, 0])) 

352 self.assertTrue(np.allclose(d2, r[ArrayType.element_shell_unknown_variables][:, :, 1])) 

353 

354 def test_effective_p_strain(self): 

355 m = FemzipMapper() 

356 d1 = np.random.randn(2, 20000) 

357 d2 = np.random.randn(2, 20000) 

358 d3 = np.random.randn(2, 20000) 

359 

360 fz: Dict[Tuple[int, str, FemzipVariableCategory], np.ndarray] = { 

361 (1, "Effective plastic strain ( 1)", FemzipVariableCategory.SHELL): d1, 

362 (2, "Effective plastic strain ( 2)", FemzipVariableCategory.SHELL): d2, 

363 (3, "Effective plastic strain ( 3)", FemzipVariableCategory.SHELL): d3, 

364 } 

365 

366 m.map(fz) 

367 

368 r = m.d3plot_arrays 

369 

370 self.assertEqual(r[ArrayType.element_shell_effective_plastic_strain].shape, (2, 20000, 3)) 

371 

372 self.assertTrue( 

373 np.allclose(d1, r[ArrayType.element_shell_effective_plastic_strain][:, :, 0]) 

374 ) 

375 self.assertTrue( 

376 np.allclose(d2, r[ArrayType.element_shell_effective_plastic_strain][:, :, 1]) 

377 ) 

378 self.assertTrue( 

379 np.allclose(d3, r[ArrayType.element_shell_effective_plastic_strain][:, :, 2]) 

380 ) 

381 

382 def test_others(self): 

383 stress_1 = np.random.randn(2, 2) 

384 stress_2 = np.random.randn(2, 2) 

385 stress_3 = np.random.randn(2, 2) 

386 

387 strain1 = np.random.randn(1, 2) 

388 strain2 = np.random.randn(1, 2) 

389 

390 history_vars = np.array([[1, 2], [0, 3], [12, 2]], dtype=float) 

391 

392 history_vars1 = np.random.randn(3, 2) 

393 history_vars2 = np.random.randn(3, 2) 

394 

395 fz: Dict[Tuple[int, str, FemzipVariableCategory], np.ndarray] = { 

396 # stress 

397 (1, "Sigma-x (IP 6)", FemzipVariableCategory.SOLID): stress_1, 

398 (2, "Sigma-y (IP 3)", FemzipVariableCategory.SOLID): stress_2, 

399 (3, "Sigma-x (IP 3)", FemzipVariableCategory.SOLID): stress_3, 

400 # history 

401 (4, "extra_value_per_element 2 (IP 2)", FemzipVariableCategory.SOLID): history_vars, 

402 (5, "extra_value_per_element 21 (IP 15)", FemzipVariableCategory.SOLID): history_vars1, 

403 (6, "extra_value_per_element 4 (IP 3)", FemzipVariableCategory.SOLID): history_vars2, 

404 # strain 

405 (7, "Epsilon-xy (outer)", FemzipVariableCategory.SHELL): strain1, 

406 (8, "Epsilon-z (outer)", FemzipVariableCategory.SHELL): strain2, 

407 } 

408 

409 m = FemzipMapper() 

410 

411 m.map(fz) 

412 

413 r = m.d3plot_arrays 

414 

415 self.assertEqual(r[ArrayType.element_solid_stress].shape, (2, 2, 6, 2)) 

416 

417 self.assertTrue(np.allclose(stress_1, r[ArrayType.element_solid_stress][:, :, 5, 0])) 

418 self.assertTrue(np.allclose(stress_2, r[ArrayType.element_solid_stress][:, :, 2, 1])) 

419 self.assertTrue(np.allclose(stress_3, r[ArrayType.element_solid_stress][:, :, 2, 0])) 

420 

421 self.assertEqual(r[ArrayType.element_solid_history_variables].shape, (3, 2, 15, 21)) 

422 

423 self.assertTrue( 

424 np.allclose(history_vars, r[ArrayType.element_solid_history_variables][:, :, 1, 1]) 

425 ) 

426 

427 self.assertTrue( 

428 np.allclose(history_vars1, r[ArrayType.element_solid_history_variables][:, :, 14, 20]) 

429 ) 

430 

431 self.assertTrue( 

432 np.allclose(history_vars2, r[ArrayType.element_solid_history_variables][:, :, 2, 3]) 

433 ) 

434 

435 self.assertEqual(r[ArrayType.element_shell_strain].shape, (1, 2, 2, 4)) 

436 

437 def test_beam(self): 

438 axial_force = np.random.randn(5, 12) 

439 shear = np.random.randn(2, 4) 

440 

441 bending = np.random.randn(3, 123) 

442 torsion = np.random.rand(2, 5) 

443 

444 fz: Dict[Tuple[int, str, FemzipVariableCategory], np.ndarray] = { 

445 (1, "axial_force", FemzipVariableCategory.BEAM): axial_force, 

446 (2, "s_shear_resultant", FemzipVariableCategory.BEAM): shear, 

447 (3, "t_bending_moment", FemzipVariableCategory.BEAM): bending, 

448 (4, "torsional_resultant", FemzipVariableCategory.BEAM): torsion, 

449 } 

450 

451 m = FemzipMapper() 

452 

453 m.map(fz) 

454 

455 r = m.d3plot_arrays 

456 

457 # axial force 

458 self.assertTrue(np.allclose(r["element_beam_axial_force"], axial_force)) 

459 

460 self.assertEqual(r[ArrayType.element_beam_shear_force].shape, (2, 4, 1)) 

461 self.assertTrue(np.allclose(r[ArrayType.element_beam_shear_force][:, :, 0], shear)) 

462 

463 # bending moment 

464 self.assertEqual(r[ArrayType.element_beam_bending_moment].shape, (3, 123, 2)) 

465 self.assertTrue(np.allclose(r[ArrayType.element_beam_bending_moment][:, :, 1], bending)) 

466 

467 # torsion 

468 self.assertEqual(r[ArrayType.element_beam_torsion_moment].shape, (2, 5)) 

469 self.assertTrue(np.allclose(r[ArrayType.element_beam_torsion_moment], torsion)) 

470 

471 # # TODO 

472 # # unknown1 and unknown2 

473 

474 # # shp 

475 

476 # # airbags 

477 

478 # # rigids