Coverage for src/beamme/core/coupling.py: 95%
38 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-29 11:30 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-09-29 11:30 +0000
1# The MIT License (MIT)
2#
3# Copyright (c) 2018-2025 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."""
24from typing import List as _List
25from typing import Union as _Union
27import numpy as _np
29import beamme.core.conf as _conf
30from beamme.core.boundary_condition import (
31 BoundaryConditionBase as _BoundaryConditionBase,
32)
33from beamme.core.conf import bme as _bme
34from beamme.core.geometry_set import GeometrySet as _GeometrySet
35from beamme.core.geometry_set import GeometrySetBase as _GeometrySetBase
36from beamme.core.node import Node as _Node
39class Coupling(_BoundaryConditionBase):
40 """Represents a coupling between geometries in 4C."""
42 def __init__(
43 self,
44 geometry: _Union[_GeometrySetBase, _List[_Node]],
45 coupling_type: _Union[_conf.BoundaryCondition, str],
46 coupling_dof_type: _Union[_conf.CouplingDofType, dict],
47 *,
48 check_overlapping_nodes: bool = True,
49 ):
50 """Initialize this object.
52 Args:
53 geometry: Geometry set or nodes that should be coupled.
54 coupling_type: If this is a string, this will be the section that
55 this coupling will be added to. If it is a bme.bc, the section
56 will be determined automatically.
57 coupling_dof_type: If this is a dictionary it is the dictionary
58 that will be used in the input file, otherwise it has to be
59 of type bme.coupling_dof.
60 check_overlapping_nodes: If all nodes of this coupling condition
61 have to be at the same physical position.
62 """
64 if isinstance(geometry, _GeometrySetBase):
65 pass
66 elif isinstance(geometry, list):
67 geometry = _GeometrySet(geometry)
68 else:
69 raise TypeError(
70 f"Coupling expects a GeometrySetBase item, got {type(geometry)}"
71 )
73 # Couplings only work for point sets
74 if geometry.geometry_type is not _bme.geo.point:
75 raise TypeError("Couplings are only implemented for point sets.")
77 super().__init__(geometry, bc_type=coupling_type, data=coupling_dof_type)
78 self.check_overlapping_nodes = check_overlapping_nodes
80 # Perform sanity checks for this boundary condition
81 self.check()
83 def check(self):
84 """Check that all nodes that are coupled have the same position
85 (depending on the check_overlapping_nodes parameter)."""
87 if not self.check_overlapping_nodes:
88 return
90 nodes = self.geometry_set.get_points()
91 diff = _np.zeros([len(nodes), 3])
92 for i, node in enumerate(nodes):
93 # Get the difference to the first node
94 diff[i, :] = node.coordinates - nodes[0].coordinates
95 if _np.max(_np.linalg.norm(diff, axis=1)) > _bme.eps_pos:
96 raise ValueError(
97 "The nodes given to Coupling do not have the same position."
98 )
101def coupling_factory(
102 geometry: _Union[_GeometrySetBase, _List[_Node]],
103 coupling_type: _conf.BoundaryCondition,
104 coupling_dof_type: _Union[_conf.CouplingDofType, dict],
105 **kwargs,
106) -> list[Coupling]:
107 """Create coupling conditions for the nodes in geometry.
109 Args:
110 geometry: Geometry set or nodes that should be coupled.
111 coupling_type: If this is a string, this will be the section that
112 this coupling will be added to. If it is a bme.bc, the section
113 will be determined automatically.
114 coupling_dof_type: If this is a dictionary it is the dictionary
115 that will be used in the input file, otherwise it has to be
116 of type bme.coupling_dof.
117 kwargs: Will be passed to constructor of `Coupling`.
119 Returns:
120 A list of coupling objects representing the created coupling conditions.
121 - By default, a single coupling object is created that couples all nodes in the given geometry.
122 - If the selected coupling type requires pairwise coupling (e.g., due to solver restrictions),
123 multiple coupling objects are returned, each coupling a pair of nodes accordingly.
124 """
126 if not coupling_type.is_point_coupling_pairwise():
127 return [Coupling(geometry, coupling_type, coupling_dof_type, **kwargs)]
128 else:
129 if isinstance(geometry, _GeometrySetBase):
130 nodes = geometry.get_points()
131 else:
132 nodes = geometry
133 main_node = nodes[0]
134 return [
135 Coupling([main_node, node], coupling_type, coupling_dof_type, **kwargs)
136 for node in nodes[1:]
137 ]