Coverage for src/beamme/core/coupling.py: 94%

36 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-07-28 15:20 +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 couple geometry together.""" 

23 

24import numpy as _np 

25 

26import beamme.core.conf as _conf 

27from beamme.core.boundary_condition import ( 

28 BoundaryConditionBase as _BoundaryConditionBase, 

29) 

30from beamme.core.conf import bme as _bme 

31from beamme.core.geometry_set import GeometrySet as _GeometrySet 

32from beamme.core.geometry_set import GeometrySetBase as _GeometrySetBase 

33from beamme.core.node import Node as _Node 

34 

35 

36class Coupling(_BoundaryConditionBase): 

37 """Represents a coupling between geometries in 4C.""" 

38 

39 def __init__( 

40 self, 

41 geometry: _GeometrySetBase | list[_Node], 

42 coupling_type: _conf.BoundaryCondition | str, 

43 coupling_dof_type: _conf.CouplingDofType | dict, 

44 *, 

45 check_overlapping_nodes: bool = True, 

46 ): 

47 """Initialize this object. 

48 

49 Args: 

50 geometry: Geometry set or nodes that should be coupled. 

51 coupling_type: If this is a string, this will be the section that 

52 this coupling will be added to. If it is a bme.bc, the section 

53 will be determined automatically. 

54 coupling_dof_type: If this is a dictionary it is the dictionary 

55 that will be used in the input file, otherwise it has to be 

56 of type bme.coupling_dof. 

57 check_overlapping_nodes: If all nodes of this coupling condition 

58 have to be at the same physical position. 

59 """ 

60 if isinstance(geometry, _GeometrySetBase): 

61 pass 

62 elif isinstance(geometry, list): 

63 geometry = _GeometrySet(geometry) 

64 else: 

65 raise TypeError( 

66 f"Coupling expects a GeometrySetBase item, got {type(geometry)}" 

67 ) 

68 

69 # Couplings only work for point sets 

70 if geometry.geometry_type is not _bme.geo.point: 

71 raise TypeError("Couplings are only implemented for point sets.") 

72 

73 super().__init__(geometry, bc_type=coupling_type, data=coupling_dof_type) 

74 self.check_overlapping_nodes = check_overlapping_nodes 

75 

76 # Perform sanity checks for this boundary condition 

77 self.check() 

78 

79 def check(self): 

80 """Check that all nodes that are coupled have the same position (depending on 

81 the check_overlapping_nodes parameter).""" 

82 if not self.check_overlapping_nodes: 

83 return 

84 

85 nodes = self.geometry_set.get_points() 

86 diff = _np.zeros([len(nodes), 3]) 

87 for i, node in enumerate(nodes): 

88 # Get the difference to the first node 

89 diff[i, :] = node.coordinates - nodes[0].coordinates 

90 if _np.max(_np.linalg.norm(diff, axis=1)) > _bme.eps_pos: 

91 raise ValueError( 

92 "The nodes given to Coupling do not have the same position." 

93 ) 

94 

95 

96def coupling_factory( 

97 geometry: _GeometrySetBase | list[_Node], 

98 coupling_type: _conf.BoundaryCondition, 

99 coupling_dof_type: _conf.CouplingDofType | dict, 

100 **kwargs, 

101) -> list[Coupling]: 

102 """Create coupling conditions for the nodes in geometry. 

103 

104 Args: 

105 geometry: Geometry set or nodes that should be coupled. 

106 coupling_type: If this is a string, this will be the section that 

107 this coupling will be added to. If it is a bme.bc, the section 

108 will be determined automatically. 

109 coupling_dof_type: If this is a dictionary it is the dictionary 

110 that will be used in the input file, otherwise it has to be 

111 of type bme.coupling_dof. 

112 kwargs: Will be passed to constructor of `Coupling`. 

113 

114 Returns: 

115 A list of coupling objects representing the created coupling conditions. 

116 - By default, a single coupling object is created that couples all nodes in the given geometry. 

117 - If the selected coupling type requires pairwise coupling (e.g., due to solver restrictions), 

118 multiple coupling objects are returned, each coupling a pair of nodes accordingly. 

119 """ 

120 if not coupling_type.is_point_coupling_pairwise(): 

121 return [Coupling(geometry, coupling_type, coupling_dof_type, **kwargs)] 

122 else: 

123 if isinstance(geometry, _GeometrySetBase): 

124 nodes = geometry.get_points() 

125 else: 

126 nodes = geometry 

127 main_node = nodes[0] 

128 return [ 

129 Coupling([main_node, node], coupling_type, coupling_dof_type, **kwargs) 

130 for node in nodes[1:] 

131 ]