Skip to content

Commit 81aff45

Browse files
committed
Split up mesh creation functions
1 parent 4cf5402 commit 81aff45

24 files changed

+881
-738
lines changed

examples/example_2_core_mesh_generation_functions.ipynb

+3-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
"metadata": {},
8181
"outputs": [],
8282
"source": [
83-
"from meshpy.mesh_creation_functions.beam_basic_geometry import create_beam_mesh_line\n",
83+
"from meshpy.mesh_creation_functions.beam_line import create_beam_mesh_line\n",
8484
"\n",
8585
"mesh = Mesh()\n",
8686
"material = beam_mat(radius=0.01)\n",
@@ -129,7 +129,7 @@
129129
],
130130
"metadata": {
131131
"kernelspec": {
132-
"display_name": "meshpy",
132+
"display_name": "Python 3",
133133
"language": "python",
134134
"name": "python3"
135135
},
@@ -143,7 +143,7 @@
143143
"name": "python",
144144
"nbconvert_exporter": "python",
145145
"pygments_lexer": "ipython3",
146-
"version": "3.12.8"
146+
"version": "3.12.9"
147147
}
148148
},
149149
"nbformat": 4,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 contains application-oriented beam mesh generation functions."""

src/meshpy/mesh_creation_functions/beam_fibers_in_rectangle.py renamed to src/meshpy/mesh_creation_functions/applications/beam_fibers_in_rectangle.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
from meshpy.core.geometry_set import GeometryName as _GeometryName
3232
from meshpy.core.geometry_set import GeometrySet as _GeometrySet
33-
from meshpy.mesh_creation_functions.beam_basic_geometry import (
33+
from meshpy.mesh_creation_functions.beam_line import (
3434
create_beam_mesh_line as _create_beam_mesh_line,
3535
)
3636
from meshpy.utils.nodes import check_node_by_coordinate as _check_node_by_coordinate

src/meshpy/mesh_creation_functions/beam_honeycomb.py renamed to src/meshpy/mesh_creation_functions/applications/beam_honeycomb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from meshpy.core.geometry_set import GeometrySet as _GeometrySet
2828
from meshpy.core.mesh import Mesh as _Mesh
2929
from meshpy.core.rotation import Rotation as _Rotation
30-
from meshpy.mesh_creation_functions.beam_basic_geometry import (
30+
from meshpy.mesh_creation_functions.beam_line import (
3131
create_beam_mesh_line as _create_beam_mesh_line,
3232
)
3333
from meshpy.utils.nodes import get_min_max_nodes as _get_min_max_nodes

src/meshpy/mesh_creation_functions/beam_stent.py renamed to src/meshpy/mesh_creation_functions/applications/beam_stent.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
from meshpy.core.geometry_set import GeometrySet as _GeometrySet
2828
from meshpy.core.mesh import Mesh as _Mesh
2929
from meshpy.core.rotation import Rotation as _Rotation
30-
from meshpy.mesh_creation_functions.beam_basic_geometry import (
30+
from meshpy.mesh_creation_functions.beam_arc import (
3131
create_beam_mesh_arc_segment_via_rotation as _create_beam_mesh_arc_segment_via_rotation,
3232
)
33-
from meshpy.mesh_creation_functions.beam_basic_geometry import (
33+
from meshpy.mesh_creation_functions.beam_line import (
3434
create_beam_mesh_line as _create_beam_mesh_line,
3535
)
3636
from meshpy.utils.nodes import get_min_max_nodes as _get_min_max_nodes

src/meshpy/mesh_creation_functions/beam_wire.py renamed to src/meshpy/mesh_creation_functions/applications/beam_wire.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
from meshpy.core.geometry_set import GeometryName as _GeometryName
2727
from meshpy.core.geometry_set import GeometrySet as _GeometrySet
28-
from meshpy.mesh_creation_functions.beam_basic_geometry import (
28+
from meshpy.mesh_creation_functions.beam_line import (
2929
create_beam_mesh_line as _create_beam_mesh_line,
3030
)
3131
from meshpy.utils.nodes import check_node_by_coordinate as _check_node_by_coordinate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
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+
"""Functions to create beam meshes along arcs."""
23+
24+
import warnings as _warnings
25+
26+
import numpy as _np
27+
28+
from meshpy.core.conf import mpy as _mpy
29+
from meshpy.core.mesh import Mesh as _Mesh
30+
from meshpy.core.rotation import Rotation as _Rotation
31+
from meshpy.mesh_creation_functions.beam_generic import (
32+
create_beam_mesh_generic as _create_beam_mesh_generic,
33+
)
34+
from meshpy.utils.nodes import get_single_node as _get_single_node
35+
36+
37+
def create_beam_mesh_arc_segment_via_rotation(
38+
mesh, beam_class, material, center, axis_rotation, radius, angle, **kwargs
39+
):
40+
"""Generate a circular segment of beam elements.
41+
42+
The circular segment is defined via a rotation, specifying the "initial"
43+
triad of the beam at the beginning of the arc.
44+
45+
This function exists for compatibility reasons with older MeshPy implementations.
46+
The user is encouraged to use the newer implementation create_beam_mesh_arc_segment_via_axis
47+
48+
Args
49+
----
50+
mesh: Mesh
51+
Mesh that the arc segment will be added to.
52+
beam_class: Beam
53+
Class of beam that will be used for this line.
54+
material: Material
55+
Material for this segment.
56+
center: _np.array, list
57+
Center of the arc.
58+
axis_rotation: Rotation
59+
This rotation defines the spatial orientation of the arc.
60+
The 3rd base vector of this rotation is the rotation axis of the arc
61+
segment. The segment starts in the direction of the 1st basis vector
62+
and the starting point is along the 2nd basis vector.
63+
radius: float
64+
The radius of the segment.
65+
angle: float
66+
The central angle of this segment in radians.
67+
68+
**kwargs (for all of them look into create_beam_mesh_function)
69+
----
70+
n_el: int
71+
Number of equally spaced beam elements along the line. Defaults to 1.
72+
Mutually exclusive with l_el.
73+
l_el: float
74+
Desired length of beam elements. Mutually exclusive with n_el.
75+
Be aware, that this length might not be achieved, if the elements are
76+
warped after they are created.
77+
78+
Return
79+
----
80+
return_set: GeometryName
81+
Set with the 'start' and 'end' node of the line. Also a 'line' set
82+
with all nodes of the line.
83+
"""
84+
85+
# Convert the input to the one for create_beam_mesh_arc_segment_via_axis
86+
axis = axis_rotation * [0, 0, 1]
87+
start_point = center + radius * (axis_rotation * [0, -1, 0])
88+
return create_beam_mesh_arc_segment_via_axis(
89+
mesh, beam_class, material, axis, center, start_point, angle, **kwargs
90+
)
91+
92+
93+
def create_beam_mesh_arc_segment_via_axis(
94+
mesh,
95+
beam_class,
96+
material,
97+
axis,
98+
axis_point,
99+
start_point,
100+
angle,
101+
**kwargs,
102+
):
103+
"""Generate a circular segment of beam elements.
104+
105+
The arc is defined via a rotation axis, a point on the rotation axis a starting
106+
point, as well as the angle of the arc segment.
107+
108+
Args
109+
----
110+
mesh: Mesh
111+
Mesh that the arc segment will be added to.
112+
beam_class: Beam
113+
Class of beam that will be used for this line.
114+
material: Material
115+
Material for this segment.
116+
axis: _np.array, list
117+
Rotation axis of the arc.
118+
axis_point: _np.array, list
119+
Point lying on the rotation axis. Does not have to be the center of the arc.
120+
start_point: _np.array, list
121+
Start point of the arc.
122+
angle: float
123+
The central angle of this segment in radians.
124+
125+
**kwargs (for all of them look into create_beam_mesh_function)
126+
----
127+
n_el: int
128+
Number of equally spaced beam elements along the line. Defaults to 1.
129+
Mutually exclusive with l_el.
130+
l_el: float
131+
Desired length of beam elements. Mutually exclusive with n_el.
132+
Be aware, that this length might not be achieved, if the elements are
133+
warped after they are created.
134+
135+
Return
136+
----
137+
return_set: GeometryName
138+
Set with the 'start' and 'end' node of the line. Also a 'line' set
139+
with all nodes of the line.
140+
"""
141+
142+
# The angle can not be negative with the current implementation.
143+
if angle <= 0.0:
144+
raise ValueError(
145+
"The angle for a beam arc segment has to be a positive number!"
146+
)
147+
148+
# Shortest distance from the given point to the axis of rotation gives
149+
# the "center" of the arc
150+
axis = _np.asarray(axis)
151+
axis_point = _np.asarray(axis_point)
152+
start_point = _np.asarray(start_point)
153+
154+
axis = axis / _np.linalg.norm(axis)
155+
diff = start_point - axis_point
156+
distance = diff - _np.dot(_np.dot(diff, axis), axis)
157+
radius = _np.linalg.norm(distance)
158+
center = start_point - distance
159+
160+
# Get the rotation at the start
161+
# No need to check the start node here, as eventual rotation offsets in
162+
# tangential direction will be covered by the create beam functionality.
163+
tangent = _np.cross(axis, distance)
164+
tangent /= _np.linalg.norm(tangent)
165+
start_rotation = _Rotation.from_rotation_matrix(
166+
_np.transpose(_np.array([tangent, -distance / radius, axis]))
167+
)
168+
169+
def get_beam_geometry(alpha, beta):
170+
"""Return a function for the position and rotation along the beam
171+
axis."""
172+
173+
def beam_function(xi):
174+
"""Return a point and the triad on the beams axis for a given
175+
parameter coordinate xi."""
176+
phi = 0.5 * (xi + 1) * (beta - alpha) + alpha
177+
arc_rotation = _Rotation(axis, phi)
178+
rot = arc_rotation * start_rotation
179+
pos = center + arc_rotation * distance
180+
return (pos, rot, phi * radius)
181+
182+
return beam_function
183+
184+
# Create the beam in the mesh
185+
return _create_beam_mesh_generic(
186+
mesh,
187+
beam_class=beam_class,
188+
material=material,
189+
function_generator=get_beam_geometry,
190+
interval=[0.0, angle],
191+
interval_length=angle * radius,
192+
**kwargs,
193+
)
194+
195+
196+
def create_beam_mesh_arc_segment_2d(
197+
mesh, beam_class, material, center, radius, phi_start, phi_end, **kwargs
198+
):
199+
"""Generate a circular segment of beam elements in the x-y plane.
200+
201+
Args
202+
----
203+
mesh: Mesh
204+
Mesh that the arc segment will be added to.
205+
beam_class: Beam
206+
Class of beam that will be used for this line.
207+
material: Material
208+
Material for this segment.
209+
center: _np.array, list
210+
Center of the arc. If the z component is not 0, an error will be
211+
thrown.
212+
radius: float
213+
The radius of the segment.
214+
phi_start, phi_end: float
215+
The start and end angles of the arc w.r.t the x-axis. If the start
216+
angle is larger than the end angle the beam faces in counter-clockwise
217+
direction, and if the start angle is smaller than the end angle, the
218+
beam faces in clockwise direction.
219+
220+
**kwargs (for all of them look into create_beam_mesh_function)
221+
----
222+
n_el: int
223+
Number of equally spaced beam elements along the line. Defaults to 1.
224+
Mutually exclusive with l_el.
225+
l_el: float
226+
Desired length of beam elements. Mutually exclusive with n_el.
227+
Be aware, that this length might not be achieved, if the elements are
228+
warped after they are created.
229+
230+
Return
231+
----
232+
return_set: GeometryName
233+
Set with the 'start' and 'end' node of the line. Also a 'line' set
234+
with all nodes of the line.
235+
"""
236+
237+
# The center point has to be on the x-y plane.
238+
if _np.abs(center[2]) > _mpy.eps_pos:
239+
raise ValueError("The z-value of center has to be 0!")
240+
241+
# Check if the beam is in clockwise or counter clockwise direction.
242+
angle = phi_end - phi_start
243+
axis = _np.array([0, 0, 1])
244+
start_point = center + radius * (_Rotation(axis, phi_start) * [1, 0, 0])
245+
246+
counter_clockwise = _np.sign(angle) == 1
247+
if not counter_clockwise:
248+
# If the beam is not in counter clockwise direction, we have to flip
249+
# the rotation axis.
250+
axis = -1.0 * axis
251+
252+
return create_beam_mesh_arc_segment_via_axis(
253+
mesh,
254+
beam_class,
255+
material,
256+
axis,
257+
center,
258+
start_point,
259+
_np.abs(angle),
260+
**kwargs,
261+
)

0 commit comments

Comments
 (0)