Coverage for src/meshpy/mesh_creation_functions/beam_line.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.9.0, created at 2025-06-13 04:26 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2018-2025 MeshPy 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"""Functions to create beam meshes along straight lines.""" 

23 

24import numpy as _np 

25 

26from meshpy.core.conf import mpy as _mpy 

27from meshpy.core.rotation import Rotation as _Rotation 

28from meshpy.mesh_creation_functions.beam_generic import ( 

29 create_beam_mesh_generic as _create_beam_mesh_generic, 

30) 

31 

32 

33def create_beam_mesh_line(mesh, beam_class, material, start_point, end_point, **kwargs): 

34 """Generate a straight line of beam elements. 

35 

36 Args 

37 ---- 

38 mesh: Mesh 

39 Mesh that the line will be added to. 

40 beam_class: Beam 

41 Class of beam that will be used for this line. 

42 material: Material 

43 Material for this line. 

44 start_point, end_point: _np.array, list 

45 3D-coordinates for the start and end point of the line. 

46 

47 **kwargs (for all of them look into create_beam_mesh_function) 

48 ---- 

49 n_el: int 

50 Number of equally spaced beam elements along the line. Defaults to 1. 

51 Mutually exclusive with l_el. 

52 l_el: float 

53 Desired length of beam elements. Mutually exclusive with n_el. 

54 Be aware, that this length might not be achieved, if the elements are 

55 warped after they are created. 

56 start_node: Node, GeometrySet 

57 Node to use as the first node for this line. Use this if the line 

58 is connected to other lines (angles have to be the same, otherwise 

59 connections should be used). If a geometry set is given, it can 

60 contain one, and one node only. 

61 add_sets: bool 

62 If this is true the sets are added to the mesh and then displayed 

63 in eventual VTK output, even if they are not used for a boundary 

64 condition or coupling. 

65 

66 Return 

67 ---- 

68 return_set: GeometryName 

69 Set with the 'start' and 'end' node of the line. Also a 'line' set 

70 with all nodes of the line. 

71 """ 

72 

73 # Get geometrical values for this line. 

74 start_point = _np.asarray(start_point) 

75 end_point = _np.asarray(end_point) 

76 direction = end_point - start_point 

77 line_length = _np.linalg.norm(direction) 

78 t1 = direction / line_length 

79 

80 # Check if the z or y axis are larger projected onto the direction. 

81 # The tolerance is used here to ensure that round-off changes in the last digits of 

82 # the floating point values don't switch the case. This increases the robustness in 

83 # testing. 

84 if abs(_np.dot(t1, [0, 0, 1])) < abs(_np.dot(t1, [0, 1, 0])) - _mpy.eps_quaternion: 

85 t2 = [0, 0, 1] 

86 else: 

87 t2 = [0, 1, 0] 

88 rotation = _Rotation.from_basis(t1, t2) 

89 

90 def get_beam_geometry(parameter_a, parameter_b): 

91 """Return a function for the position along the beams axis.""" 

92 

93 def beam_function(xi): 

94 """Return a point on the beams axis for a given parameter 

95 coordinate xi.""" 

96 point_a = start_point + parameter_a * direction 

97 point_b = start_point + parameter_b * direction 

98 pos = 0.5 * (1 - xi) * point_a + 0.5 * (1 + xi) * point_b 

99 arc_length = ( 

100 0.5 * (1 - xi) * parameter_a + 0.5 * (1 + xi) * parameter_b 

101 ) * line_length 

102 return (pos, rotation, arc_length) 

103 

104 return beam_function 

105 

106 # Create the beam in the mesh 

107 return _create_beam_mesh_generic( 

108 mesh, 

109 beam_class=beam_class, 

110 material=material, 

111 function_generator=get_beam_geometry, 

112 interval=[0.0, 1.0], 

113 interval_length=line_length, 

114 **kwargs, 

115 )