Coverage for src/meshpy/core/node.py: 94%
67 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-28 04:21 +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
27from meshpy.core.conf import mpy as _mpy
28from meshpy.utils.environment import fourcipp_is_available as _fourcipp_is_available
31class Node(_BaseMeshItem):
32 """This object represents one node in the mesh."""
34 def __init__(self, coordinates, *, is_middle_node=False, **kwargs):
35 super().__init__(data=None, **kwargs)
37 # Coordinates of this node.
38 self.coordinates = _np.array(coordinates)
40 # If this node is at the end of a line or curve (by default only those
41 # nodes are checked for overlapping nodes).
42 self.is_end_node = False
44 # If the node is in the middle of a beam element.
45 self.is_middle_node = is_middle_node
47 # Lists with the objects that this node is linked to.
48 self.element_link = []
49 self.node_sets_link = []
50 self.element_partner_index = None
51 self.mesh = None
53 # If this node is replaced, store a link to the remaining node.
54 self.master_node = None
56 @classmethod
57 def from_legacy_string(cls, input_line):
58 """Create the Node object from a legacy string."""
60 if _fourcipp_is_available():
61 raise ValueError(
62 "Port this functionality to create the node from the dict "
63 "representing the node, not the legacy string."
64 )
66 # Split up the input line.
67 line_split = input_line.split()
69 # Convert the node coordinates into a Node object.
70 return cls([float(line_split[i]) for i in range(3, 6)])
72 def get_master_node(self):
73 """Return the master node of this node.
75 If the node has not been replaced, this object is returned.
76 """
78 if self.master_node is None:
79 return self
80 else:
81 return self.master_node.get_master_node()
83 def replace_with(self, master_node):
84 """Replace this node with another node object."""
86 # Check that the two nodes have the same type.
87 if not isinstance(self, type(master_node)):
88 raise TypeError(
89 "A node can only be replaced by a node with the same type. "
90 + f"Got {type(self)} and {type(master_node)}"
91 )
93 # Replace the links to this node in the referenced objects.
94 self.mesh.replace_node(self, master_node)
95 for element in self.element_link:
96 element.replace_node(self, master_node)
97 for node_set in self.node_sets_link:
98 node_set.replace_node(self, master_node)
100 # Set link to master node.
101 self.master_node = master_node.get_master_node()
103 def unlink(self):
104 """Reset the links to elements, node sets and global indices."""
105 self.element_link = []
106 self.node_sets_link = []
107 self.mesh = None
108 self.i_global = None
110 def rotate(self, *args, **kwargs):
111 """Don't do anything for a standard node, as this node can not be
112 rotated."""
114 def dump_to_list(self):
115 """Return a list with the legacy string representing this node."""
117 if _fourcipp_is_available():
118 raise ValueError(
119 "Port this functionality to create a dict, not the legacy string."
120 )
122 coordinate_string = " ".join([str(item) for item in self.coordinates])
123 return [f"NODE {self.i_global} COORD {coordinate_string}"]
126class NodeCosserat(Node):
127 """This object represents a Cosserat node in the mesh, i.e., it contains
128 three positions and three rotations."""
130 def __init__(self, coordinates, rotation, *, arc_length=None, **kwargs):
131 super().__init__(coordinates, **kwargs)
133 # Rotation of this node.
134 self.rotation = rotation.copy()
136 # Arc length along the filament that this beam is a part of
137 self.arc_length = arc_length
139 def rotate(self, rotation, *, origin=None, only_rotate_triads=False):
140 """Rotate this node.
142 By default the node is rotated around the origin (0,0,0), if the
143 keyword argument origin is given, it is rotated around that
144 point. If only_rotate_triads is True, then only the rotation is
145 affected, the position of the node stays the same.
146 """
148 self.rotation = rotation * self.rotation
150 # Rotate the positions (around origin).
151 if not only_rotate_triads:
152 if origin is not None:
153 self.coordinates = self.coordinates - origin
154 self.coordinates = rotation * self.coordinates
155 if origin is not None:
156 self.coordinates = self.coordinates + origin
159class ControlPoint(Node):
160 """This object represents a control point with a weight in the mesh."""
162 def __init__(self, coordinates, weight, **kwargs):
163 super().__init__(coordinates, **kwargs)
165 # Weight of this node
166 self.weight = weight
168 def dump_to_list(self):
169 """Return a list with the legacy string representing this control
170 point."""
172 if _fourcipp_is_available():
173 raise ValueError(
174 "Port this functionality to create a dict, not the legacy string."
175 )
177 coordinate_string = " ".join([str(item) for item in self.coordinates])
178 return [f"CP {self.i_global} COORD {coordinate_string} {self.weight}"]