Coverage for src/meshpy/core/node.py: 98%
53 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-27 14:07 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-27 14:07 +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 the class that represents one node in the Mesh."""
24import numpy as _np
26from meshpy.core.base_mesh_item import BaseMeshItem as _BaseMeshItem
29class Node(_BaseMeshItem):
30 """This object represents one node in the mesh."""
32 def __init__(self, coordinates, *, is_middle_node=False, **kwargs):
33 super().__init__(**kwargs)
35 # Coordinates of this node.
36 self.coordinates = _np.array(coordinates)
38 # If this node is at the end of a line or curve (by default only those
39 # nodes are checked for overlapping nodes).
40 self.is_end_node = False
42 # If the node is in the middle of a beam element.
43 self.is_middle_node = is_middle_node
45 # Lists with the objects that this node is linked to.
46 self.element_link = []
47 self.node_sets_link = []
48 self.element_partner_index = None
49 self.mesh = None
51 # If this node is replaced, store a link to the remaining node.
52 self.master_node = None
54 def get_master_node(self):
55 """Return the master node of this node.
57 If the node has not been replaced, this object is returned.
58 """
60 if self.master_node is None:
61 return self
62 else:
63 return self.master_node.get_master_node()
65 def replace_with(self, master_node):
66 """Replace this node with another node object."""
68 # Check that the two nodes have the same type.
69 if not isinstance(self, type(master_node)):
70 raise TypeError(
71 "A node can only be replaced by a node with the same type. "
72 + f"Got {type(self)} and {type(master_node)}"
73 )
75 # Replace the links to this node in the referenced objects.
76 self.mesh.replace_node(self, master_node)
77 for element in self.element_link:
78 element.replace_node(self, master_node)
79 for node_set in self.node_sets_link:
80 node_set.replace_node(self, master_node)
82 # Set link to master node.
83 self.master_node = master_node.get_master_node()
85 def unlink(self):
86 """Reset the links to elements, node sets and global indices."""
87 self.element_link = []
88 self.node_sets_link = []
89 self.mesh = None
90 self.i_global = None
92 def rotate(self, *args, **kwargs):
93 """Don't do anything for a standard node, as this node can not be
94 rotated."""
96 def dump_to_list(self):
97 """Return a list with the legacy string representing this node."""
99 return {
100 "id": self.i_global,
101 "COORD": self.coordinates,
102 "data": {"type": "NODE"},
103 }
106class NodeCosserat(Node):
107 """This object represents a Cosserat node in the mesh, i.e., it contains
108 three positions and three rotations."""
110 def __init__(self, coordinates, rotation, *, arc_length=None, **kwargs):
111 super().__init__(coordinates, **kwargs)
113 # Rotation of this node.
114 self.rotation = rotation.copy()
116 # Arc length along the filament that this beam is a part of
117 self.arc_length = arc_length
119 def rotate(self, rotation, *, origin=None, only_rotate_triads=False):
120 """Rotate this node.
122 By default the node is rotated around the origin (0,0,0), if the
123 keyword argument origin is given, it is rotated around that
124 point. If only_rotate_triads is True, then only the rotation is
125 affected, the position of the node stays the same.
126 """
128 self.rotation = rotation * self.rotation
130 # Rotate the positions (around origin).
131 if not only_rotate_triads:
132 if origin is not None:
133 self.coordinates = self.coordinates - origin
134 self.coordinates = rotation * self.coordinates
135 if origin is not None:
136 self.coordinates = self.coordinates + origin
139class ControlPoint(Node):
140 """This object represents a control point with a weight in the mesh."""
142 def __init__(self, coordinates, weight, **kwargs):
143 super().__init__(coordinates, **kwargs)
145 # Weight of this node
146 self.weight = weight
148 def dump_to_list(self):
149 """Return a list with the legacy string representing this control
150 point."""
152 return {
153 "id": self.i_global,
154 "COORD": self.coordinates,
155 "data": {"type": "CP", "weight": self.weight},
156 }