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

41 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-08 11:03 +0000

1# The MIT License (MIT) 

2# 

3# Copyright (c) 2018-2026 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 implements a class to represent boundary conditions.""" 

23 

24import warnings as _warnings 

25from typing import Dict as _Dict 

26 

27import beamme.core.conf as _conf 

28from beamme.core.base_mesh_item import BaseMeshItem as _BaseMeshItem 

29from beamme.core.conf import bme as _bme 

30from beamme.core.container import ContainerBase as _ContainerBase 

31from beamme.core.geometry_set import GeometrySet as _GeometrySet 

32from beamme.core.geometry_set import GeometrySetBase as _GeometrySetBase 

33from beamme.utils.nodes import find_close_nodes as _find_close_nodes 

34 

35 

36class BoundaryConditionBase(_BaseMeshItem): 

37 """Base class for boundary conditions.""" 

38 

39 def __init__( 

40 self, 

41 geometry_set: _GeometrySetBase, 

42 bc_type: _conf.BoundaryCondition | str, 

43 **kwargs, 

44 ): 

45 """Initialize the boundary condition. 

46 

47 Args: 

48 geometry_set: Geometry that this boundary condition acts on. 

49 bc_type: Type of the boundary condition. 

50 """ 

51 

52 super().__init__(**kwargs) 

53 self.bc_type = bc_type 

54 self.geometry_set = geometry_set 

55 

56 

57class BoundaryCondition(BoundaryConditionBase): 

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

59 ...""" 

60 

61 def __init__( 

62 self, 

63 geometry_set: _GeometrySetBase, 

64 data: _Dict, 

65 bc_type: _conf.BoundaryCondition | str, 

66 *, 

67 double_nodes: _conf.DoubleNodes | None = None, 

68 **kwargs, 

69 ): 

70 """Initialize the object. 

71 

72 Args: 

73 geometry_set: Geometry that this boundary condition acts on. 

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

75 bc_type: If this is a string, this will be the section that 

76 this BC will be added to. If it is a bme.bc, the section will 

77 be determined automatically. 

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

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

80 """ 

81 

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

83 self.double_nodes = double_nodes 

84 

85 # Perform some sanity checks for this boundary condition. 

86 self.check() 

87 

88 def check(self): 

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

90 in the set. 

91 

92 Duplicate nodes in a point Neumann boundary condition can lead 

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

94 spatial position, which results in incorrect load application. 

95 """ 

96 

97 if self.double_nodes is _bme.double_nodes.keep: 

98 return 

99 

100 if ( 

101 self.bc_type == _bme.bc.neumann 

102 and self.geometry_set.geometry_type == _bme.geo.point 

103 ): 

104 my_nodes = self.geometry_set.get_points() 

105 partners = _find_close_nodes(my_nodes) 

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

107 double_node_list = [] 

108 for node_list in partners: 

109 for i, node in enumerate(node_list): 

110 if i > 0: 

111 double_node_list.append(node) 

112 if ( 

113 len(double_node_list) > 0 

114 and self.double_nodes is _bme.double_nodes.remove 

115 ): 

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

117 self.geometry_set = _GeometrySet( 

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

119 ) 

120 elif len(double_node_list) > 0: 

121 _warnings.warn( 

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

123 "specified on how to handle them!" 

124 ) 

125 

126 

127class BoundaryConditionContainer(_ContainerBase): 

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

129 

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

131 """ 

132 

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

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

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

136 

137 self.item_types = [BoundaryConditionBase] 

138 

139 for bc_key in _bme.bc: 

140 for geometry_key in _bme.geo: 

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