Coverage for src/meshpy/core/boundary_condition.py: 95%

43 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-04-28 04:21 +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"""This module implements a class to represent boundary conditions in 

23MeshPy.""" 

24 

25import warnings as _warnings 

26from typing import Dict as _Dict 

27from typing import Optional as _Optional 

28from typing import Union as _Union 

29 

30import meshpy.core.conf as _conf 

31from meshpy.core.base_mesh_item import BaseMeshItem as _BaseMeshItem 

32from meshpy.core.conf import mpy as _mpy 

33from meshpy.core.container import ContainerBase as _ContainerBase 

34from meshpy.core.geometry_set import GeometrySet as _GeometrySet 

35from meshpy.core.geometry_set import GeometrySetBase as _GeometrySetBase 

36from meshpy.utils.nodes import find_close_nodes as _find_close_nodes 

37 

38 

39class BoundaryConditionBase(_BaseMeshItem): 

40 """Base class for boundary conditions.""" 

41 

42 def __init__( 

43 self, 

44 geometry_set: _GeometrySetBase, 

45 bc_type: _Union[_conf.BoundaryCondition, str], 

46 **kwargs, 

47 ): 

48 """Initialize the boundary condition. 

49 

50 Args: 

51 geometry_set: Geometry that this boundary condition acts on. 

52 bc_type: Type of the boundary condition. 

53 """ 

54 

55 super().__init__(**kwargs) 

56 self.bc_type = bc_type 

57 self.geometry_set = geometry_set 

58 

59 

60class BoundaryCondition(BoundaryConditionBase): 

61 """This object represents one boundary condition, e.g., Dirichlet, Neumann, 

62 ...""" 

63 

64 def __init__( 

65 self, 

66 geometry_set: _GeometrySetBase, 

67 data: _Dict, 

68 bc_type: _Union[_conf.BoundaryCondition, str], 

69 *, 

70 double_nodes: _Optional[_conf.DoubleNodes] = None, 

71 **kwargs, 

72 ): 

73 """Initialize the object. 

74 

75 Args: 

76 geometry_set: Geometry that this boundary condition acts on. 

77 data: Data defining the properties of this boundary condition. 

78 bc_type: Type of the boundary condition. 

79 double_nodes: Depending on this parameter, it will be checked if point 

80 Neumann conditions do contain nodes at the same spatial positions. 

81 """ 

82 

83 super().__init__(geometry_set, bc_type, data=data, **kwargs) 

84 self.double_nodes = double_nodes 

85 

86 # Perform some sanity checks for this boundary condition. 

87 self.check() 

88 

89 def check(self): 

90 """Check for point Neumann boundaries that there is not a double Node 

91 in the set. 

92 

93 Duplicate nodes in a point Neumann boundary condition can lead 

94 to the same force being applied multiple times at the same 

95 spatial position, which results in incorrect load application. 

96 """ 

97 

98 if self.double_nodes is _mpy.double_nodes.keep: 

99 return 

100 

101 if ( 

102 self.bc_type == _mpy.bc.neumann 

103 and self.geometry_set.geometry_type == _mpy.geo.point 

104 ): 

105 my_nodes = self.geometry_set.get_points() 

106 partners = _find_close_nodes(my_nodes) 

107 # Create a list with nodes that will not be kept in the set. 

108 double_node_list = [] 

109 for node_list in partners: 

110 for i, node in enumerate(node_list): 

111 if i > 0: 

112 double_node_list.append(node) 

113 if ( 

114 len(double_node_list) > 0 

115 and self.double_nodes is _mpy.double_nodes.remove 

116 ): 

117 # Create the a new geometry set with the unique nodes. 

118 self.geometry_set = _GeometrySet( 

119 [node for node in my_nodes if (node not in double_node_list)] 

120 ) 

121 elif len(double_node_list) > 0: 

122 _warnings.warn( 

123 "There are overlapping nodes in this point Neumann boundary, and it is not " 

124 "specified on how to handle them!" 

125 ) 

126 

127 

128class BoundaryConditionContainer(_ContainerBase): 

129 """A class to group boundary conditions together. 

130 

131 The key of the dictionary are (bc_type, geometry_type). 

132 """ 

133 

134 def __init__(self, *args, **kwargs): 

135 """Initialize the container and create the default keys in the map.""" 

136 super().__init__(*args, **kwargs) 

137 

138 self.item_types = [BoundaryConditionBase] 

139 

140 for bc_key in _mpy.bc: 

141 for geometry_key in _mpy.geo: 

142 self[(bc_key, geometry_key)] = []