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