/
inertia.py
195 lines (152 loc) · 5.96 KB
/
inertia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from sympy.core.backend import sympify
from sympy.physics.vector import Point, Dyadic, ReferenceFrame
from collections import namedtuple
__all__ = ['inertia', 'inertia_of_point_mass', 'Inertia']
def inertia(frame, ixx, iyy, izz, ixy=0, iyz=0, izx=0):
"""Simple way to create inertia Dyadic object.
Explanation
===========
Creates an inertia Dyadic based on the given tensor values and a body-fixed
reference frame.
Parameters
==========
frame : ReferenceFrame
The frame the inertia is defined in.
ixx : Sympifyable
The xx element in the inertia dyadic.
iyy : Sympifyable
The yy element in the inertia dyadic.
izz : Sympifyable
The zz element in the inertia dyadic.
ixy : Sympifyable
The xy element in the inertia dyadic.
iyz : Sympifyable
The yz element in the inertia dyadic.
izx : Sympifyable
The zx element in the inertia dyadic.
Examples
========
>>> from sympy.physics.mechanics import ReferenceFrame, inertia
>>> N = ReferenceFrame('N')
>>> inertia(N, 1, 2, 3)
(N.x|N.x) + 2*(N.y|N.y) + 3*(N.z|N.z)
"""
if not isinstance(frame, ReferenceFrame):
raise TypeError('Need to define the inertia in a frame')
ixx, iyy, izz = sympify(ixx), sympify(iyy), sympify(izz)
ixy, iyz, izx = sympify(ixy), sympify(iyz), sympify(izx)
return (ixx * (frame.x | frame.x) + ixy * (frame.x | frame.y) +
izx * (frame.x | frame.z) + ixy * (frame.y | frame.x) +
iyy * (frame.y | frame.y) + iyz * (frame.y | frame.z) +
izx * (frame.z | frame.x) + iyz * (frame.z | frame.y) +
izz * (frame.z | frame.z))
def inertia_of_point_mass(mass, pos_vec, frame):
"""Inertia dyadic of a point mass relative to point O.
Parameters
==========
mass : Sympifyable
Mass of the point mass
pos_vec : Vector
Position from point O to point mass
frame : ReferenceFrame
Reference frame to express the dyadic in
Examples
========
>>> from sympy import symbols
>>> from sympy.physics.mechanics import ReferenceFrame, inertia_of_point_mass
>>> N = ReferenceFrame('N')
>>> r, m = symbols('r m')
>>> px = r * N.x
>>> inertia_of_point_mass(m, px, N)
m*r**2*(N.y|N.y) + m*r**2*(N.z|N.z)
"""
return mass * (
((frame.x | frame.x) + (frame.y | frame.y) + (frame.z | frame.z)) *
(pos_vec & pos_vec) - (pos_vec | pos_vec))
class Inertia(namedtuple('Inertia', ['dyadic', 'point'])):
"""Inertia object consisting of a Dyadic and a Point of reference.
Explanation
===========
This is a simple class to store the Point and Dyadic, belonging to an
inertia.
Attributes
==========
dyadic : Dyadic
The dyadic of the inertia.
point : Point
The reference point of the inertia.
Examples
========
>>> from sympy.physics.mechanics import ReferenceFrame, Point, Inertia
>>> N = ReferenceFrame('N')
>>> Po = Point('Po')
>>> Inertia(N.x.outer(N.x) + N.y.outer(N.y) + N.z.outer(N.z), Po)
((N.x|N.x) + (N.y|N.y) + (N.z|N.z), Po)
In the example above the Dyadic was created manually, one can however also
use the ``inertia`` function for this or the class method ``from_tensor`` as
shown below.
>>> Inertia.from_inertia_scalars(Po, N, 1, 1, 1)
((N.x|N.x) + (N.y|N.y) + (N.z|N.z), Po)
"""
def __new__(cls, dyadic, point):
# Switch order if given in the wrong order
if isinstance(dyadic, Point) and isinstance(point, Dyadic):
point, dyadic = dyadic, point
if not isinstance(point, Point):
raise TypeError('Reference point should be of type Point')
if not isinstance(dyadic, Dyadic):
raise TypeError('Inertia value should be expressed as a Dyadic')
return super().__new__(cls, dyadic, point)
@classmethod
def from_inertia_scalars(cls, point, frame, ixx, iyy, izz, ixy=0, iyz=0,
izx=0):
"""Simple way to create an Inertia object based on the tensor values.
Explanation
===========
This class method uses the :func`~.inertia` to create the Dyadic based
on the tensor values.
Parameters
==========
point : Point
The reference point of the inertia.
frame : ReferenceFrame
The frame the inertia is defined in.
ixx : Sympifyable
The xx element in the inertia dyadic.
iyy : Sympifyable
The yy element in the inertia dyadic.
izz : Sympifyable
The zz element in the inertia dyadic.
ixy : Sympifyable
The xy element in the inertia dyadic.
iyz : Sympifyable
The yz element in the inertia dyadic.
izx : Sympifyable
The zx element in the inertia dyadic.
Examples
========
>>> from sympy import symbols
>>> from sympy.physics.mechanics import ReferenceFrame, Point, Inertia
>>> ixx, iyy, izz, ixy, iyz, izx = symbols('ixx iyy izz ixy iyz izx')
>>> N = ReferenceFrame('N')
>>> P = Point('P')
>>> I = Inertia.from_inertia_scalars(P, N, ixx, iyy, izz, ixy, iyz, izx)
The tensor values can easily be seen when converting the dyadic to a
matrix.
>>> I.dyadic.to_matrix(N)
Matrix([
[ixx, ixy, izx],
[ixy, iyy, iyz],
[izx, iyz, izz]])
"""
return cls(inertia(frame, ixx, iyy, izz, ixy, iyz, izx), point)
def __add__(self, other):
raise TypeError(f"unsupported operand type(s) for +: "
f"'{self.__class__.__name__}' and "
f"'{other.__class__.__name__}'")
def __mul__(self, other):
raise TypeError(f"unsupported operand type(s) for *: "
f"'{self.__class__.__name__}' and "
f"'{other.__class__.__name__}'")
__radd__ = __add__
__rmul__ = __mul__