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