Coverage for src/beamme/core/coupling.py: 94%
36 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-08 11:03 +0000
« 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 couple geometry together."""
24import numpy as _np
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
36class Coupling(_BoundaryConditionBase):
37 """Represents a coupling between geometries in 4C."""
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.
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 """
61 if isinstance(geometry, _GeometrySetBase):
62 pass
63 elif isinstance(geometry, list):
64 geometry = _GeometrySet(geometry)
65 else:
66 raise TypeError(
67 f"Coupling expects a GeometrySetBase item, got {type(geometry)}"
68 )
70 # Couplings only work for point sets
71 if geometry.geometry_type is not _bme.geo.point:
72 raise TypeError("Couplings are only implemented for point sets.")
74 super().__init__(geometry, bc_type=coupling_type, data=coupling_dof_type)
75 self.check_overlapping_nodes = check_overlapping_nodes
77 # Perform sanity checks for this boundary condition
78 self.check()
80 def check(self):
81 """Check that all nodes that are coupled have the same position
82 (depending on the check_overlapping_nodes parameter)."""
84 if not self.check_overlapping_nodes:
85 return
87 nodes = self.geometry_set.get_points()
88 diff = _np.zeros([len(nodes), 3])
89 for i, node in enumerate(nodes):
90 # Get the difference to the first node
91 diff[i, :] = node.coordinates - nodes[0].coordinates
92 if _np.max(_np.linalg.norm(diff, axis=1)) > _bme.eps_pos:
93 raise ValueError(
94 "The nodes given to Coupling do not have the same position."
95 )
98def coupling_factory(
99 geometry: _GeometrySetBase | list[_Node],
100 coupling_type: _conf.BoundaryCondition,
101 coupling_dof_type: _conf.CouplingDofType | dict,
102 **kwargs,
103) -> list[Coupling]:
104 """Create coupling conditions for the nodes in geometry.
106 Args:
107 geometry: Geometry set or nodes that should be coupled.
108 coupling_type: If this is a string, this will be the section that
109 this coupling will be added to. If it is a bme.bc, the section
110 will be determined automatically.
111 coupling_dof_type: If this is a dictionary it is the dictionary
112 that will be used in the input file, otherwise it has to be
113 of type bme.coupling_dof.
114 kwargs: Will be passed to constructor of `Coupling`.
116 Returns:
117 A list of coupling objects representing the created coupling conditions.
118 - By default, a single coupling object is created that couples all nodes in the given geometry.
119 - If the selected coupling type requires pairwise coupling (e.g., due to solver restrictions),
120 multiple coupling objects are returned, each coupling a pair of nodes accordingly.
121 """
123 if not coupling_type.is_point_coupling_pairwise():
124 return [Coupling(geometry, coupling_type, coupling_dof_type, **kwargs)]
125 else:
126 if isinstance(geometry, _GeometrySetBase):
127 nodes = geometry.get_points()
128 else:
129 nodes = geometry
130 main_node = nodes[0]
131 return [
132 Coupling([main_node, node], coupling_type, coupling_dof_type, **kwargs)
133 for node in nodes[1:]
134 ]