Coverage for src/beamme/core/boundary_condition.py: 95%
40 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-07-28 15:20 +0000
« 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 represent boundary conditions."""
24import warnings as _warnings
26import beamme.core.conf as _conf
27from beamme.core.base_mesh_item import BaseMeshItem as _BaseMeshItem
28from beamme.core.conf import bme as _bme
29from beamme.core.container import ContainerBase as _ContainerBase
30from beamme.core.geometry_set import GeometrySet as _GeometrySet
31from beamme.core.geometry_set import GeometrySetBase as _GeometrySetBase
32from beamme.utils.nodes import find_close_nodes as _find_close_nodes
35class BoundaryConditionBase(_BaseMeshItem):
36 """Base class for boundary conditions."""
38 def __init__(
39 self,
40 geometry_set: _GeometrySetBase,
41 bc_type: _conf.BoundaryCondition | str,
42 **kwargs,
43 ):
44 """Initialize the boundary condition.
46 Args:
47 geometry_set: Geometry that this boundary condition acts on.
48 bc_type: Type of the boundary condition.
49 """
50 super().__init__(**kwargs)
51 self.bc_type = bc_type
52 self.geometry_set = geometry_set
55class BoundaryCondition(BoundaryConditionBase):
56 """This object represents one boundary condition, e.g., Dirichlet, Neumann, ..."""
58 def __init__(
59 self,
60 geometry_set: _GeometrySetBase,
61 data: dict,
62 bc_type: _conf.BoundaryCondition | str,
63 *,
64 double_nodes: _conf.DoubleNodes | None = None,
65 **kwargs,
66 ):
67 """Initialize the object.
69 Args:
70 geometry_set: Geometry that this boundary condition acts on.
71 data: Data defining the properties of this boundary condition.
72 bc_type: If this is a string, this will be the section that
73 this BC will be added to. If it is a bme.bc, the section will
74 be determined automatically.
75 double_nodes: Depending on this parameter, it will be checked if point
76 Neumann conditions do contain nodes at the same spatial positions.
77 """
78 super().__init__(geometry_set, bc_type, data=data, **kwargs)
79 self.double_nodes = double_nodes
81 # Perform some sanity checks for this boundary condition.
82 self.check()
84 def check(self):
85 """Check for point Neumann boundaries that there is not a double Node in the
86 set.
88 Duplicate nodes in a point Neumann boundary condition can lead to the same force
89 being applied multiple times at the same spatial position, which results in
90 incorrect load application.
91 """
92 if self.double_nodes is _bme.double_nodes.keep:
93 return
95 if (
96 self.bc_type == _bme.bc.neumann
97 and self.geometry_set.geometry_type == _bme.geo.point
98 ):
99 my_nodes = self.geometry_set.get_points()
100 partners = _find_close_nodes(my_nodes)
101 # Create a list with nodes that will not be kept in the set.
102 double_node_list = []
103 for node_list in partners:
104 for i, node in enumerate(node_list):
105 if i > 0:
106 double_node_list.append(node)
107 if (
108 len(double_node_list) > 0
109 and self.double_nodes is _bme.double_nodes.remove
110 ):
111 # Create the a new geometry set with the unique nodes.
112 self.geometry_set = _GeometrySet(
113 [node for node in my_nodes if (node not in double_node_list)]
114 )
115 elif len(double_node_list) > 0:
116 _warnings.warn(
117 "There are overlapping nodes in this point Neumann boundary, and it is not "
118 "specified on how to handle them!"
119 )
122class BoundaryConditionContainer(_ContainerBase):
123 """A class to group boundary conditions together.
125 The key of the dictionary are (bc_type, geometry_type).
126 """
128 def __init__(self, *args, **kwargs):
129 """Initialize the container and create the default keys in the map."""
130 super().__init__(*args, **kwargs)
132 self.item_types = [BoundaryConditionBase]
134 for bc_key in _bme.bc:
135 for geometry_key in _bme.geo:
136 self[(bc_key, geometry_key)] = []