Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/actuator.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/body.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/functions.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/lagrange.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/linearize.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/particle.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/pathway.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/wrapping_geometry.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/actuator.py +992 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/body.py +710 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/body_base.py +94 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/functions.py +735 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/joint.py +2188 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/jointsmethod.py +318 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/lagrange.py +512 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/loads.py +177 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/method.py +39 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/models.py +230 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/particle.py +209 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/pathway.py +688 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/system.py +1553 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__init__.py +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_actuator.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_body.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_functions.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_inertia.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_joint.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_jointsmethod.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane2.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane3.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane4.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane5.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_lagrange.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_lagrange2.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_linearize.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_loads.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_method.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_models.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_particle.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_pathway.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_rigidbody.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_system.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_system_class.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_wrapping_geometry.cpython-310.pyc +0 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/test_actuator.py +851 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/test_body.py +340 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/test_functions.py +262 -0
- mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/test_inertia.py +71 -0
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/actuator.cpython-310.pyc
ADDED
|
Binary file (35.6 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/body.cpython-310.pyc
ADDED
|
Binary file (23.5 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/functions.cpython-310.pyc
ADDED
|
Binary file (23.1 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/lagrange.cpython-310.pyc
ADDED
|
Binary file (17.7 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/linearize.cpython-310.pyc
ADDED
|
Binary file (12 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/particle.cpython-310.pyc
ADDED
|
Binary file (6.61 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/pathway.cpython-310.pyc
ADDED
|
Binary file (26.4 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/__pycache__/wrapping_geometry.cpython-310.pyc
ADDED
|
Binary file (19.8 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/actuator.py
ADDED
|
@@ -0,0 +1,992 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Implementations of actuators for linked force and torque application."""
|
| 2 |
+
|
| 3 |
+
from abc import ABC, abstractmethod
|
| 4 |
+
|
| 5 |
+
from sympy import S, sympify
|
| 6 |
+
from sympy.physics.mechanics.joint import PinJoint
|
| 7 |
+
from sympy.physics.mechanics.loads import Torque
|
| 8 |
+
from sympy.physics.mechanics.pathway import PathwayBase
|
| 9 |
+
from sympy.physics.mechanics.rigidbody import RigidBody
|
| 10 |
+
from sympy.physics.vector import ReferenceFrame, Vector
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
__all__ = [
|
| 14 |
+
'ActuatorBase',
|
| 15 |
+
'ForceActuator',
|
| 16 |
+
'LinearDamper',
|
| 17 |
+
'LinearSpring',
|
| 18 |
+
'TorqueActuator',
|
| 19 |
+
'DuffingSpring'
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class ActuatorBase(ABC):
|
| 24 |
+
"""Abstract base class for all actuator classes to inherit from.
|
| 25 |
+
|
| 26 |
+
Notes
|
| 27 |
+
=====
|
| 28 |
+
|
| 29 |
+
Instances of this class cannot be directly instantiated by users. However,
|
| 30 |
+
it can be used to created custom actuator types through subclassing.
|
| 31 |
+
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
def __init__(self):
|
| 35 |
+
"""Initializer for ``ActuatorBase``."""
|
| 36 |
+
pass
|
| 37 |
+
|
| 38 |
+
@abstractmethod
|
| 39 |
+
def to_loads(self):
|
| 40 |
+
"""Loads required by the equations of motion method classes.
|
| 41 |
+
|
| 42 |
+
Explanation
|
| 43 |
+
===========
|
| 44 |
+
|
| 45 |
+
``KanesMethod`` requires a list of ``Point``-``Vector`` tuples to be
|
| 46 |
+
passed to the ``loads`` parameters of its ``kanes_equations`` method
|
| 47 |
+
when constructing the equations of motion. This method acts as a
|
| 48 |
+
utility to produce the correctly-structred pairs of points and vectors
|
| 49 |
+
required so that these can be easily concatenated with other items in
|
| 50 |
+
the list of loads and passed to ``KanesMethod.kanes_equations``. These
|
| 51 |
+
loads are also in the correct form to also be passed to the other
|
| 52 |
+
equations of motion method classes, e.g. ``LagrangesMethod``.
|
| 53 |
+
|
| 54 |
+
"""
|
| 55 |
+
pass
|
| 56 |
+
|
| 57 |
+
def __repr__(self):
|
| 58 |
+
"""Default representation of an actuator."""
|
| 59 |
+
return f'{self.__class__.__name__}()'
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
class ForceActuator(ActuatorBase):
|
| 63 |
+
"""Force-producing actuator.
|
| 64 |
+
|
| 65 |
+
Explanation
|
| 66 |
+
===========
|
| 67 |
+
|
| 68 |
+
A ``ForceActuator`` is an actuator that produces a (expansile) force along
|
| 69 |
+
its length.
|
| 70 |
+
|
| 71 |
+
A force actuator uses a pathway instance to determine the direction and
|
| 72 |
+
number of forces that it applies to a system. Consider the simplest case
|
| 73 |
+
where a ``LinearPathway`` instance is used. This pathway is made up of two
|
| 74 |
+
points that can move relative to each other, and results in a pair of equal
|
| 75 |
+
and opposite forces acting on the endpoints. If the positive time-varying
|
| 76 |
+
Euclidean distance between the two points is defined, then the "extension
|
| 77 |
+
velocity" is the time derivative of this distance. The extension velocity
|
| 78 |
+
is positive when the two points are moving away from each other and
|
| 79 |
+
negative when moving closer to each other. The direction for the force
|
| 80 |
+
acting on either point is determined by constructing a unit vector directed
|
| 81 |
+
from the other point to this point. This establishes a sign convention such
|
| 82 |
+
that a positive force magnitude tends to push the points apart, this is the
|
| 83 |
+
meaning of "expansile" in this context. The following diagram shows the
|
| 84 |
+
positive force sense and the distance between the points::
|
| 85 |
+
|
| 86 |
+
P Q
|
| 87 |
+
o<--- F --->o
|
| 88 |
+
| |
|
| 89 |
+
|<--l(t)--->|
|
| 90 |
+
|
| 91 |
+
Examples
|
| 92 |
+
========
|
| 93 |
+
|
| 94 |
+
To construct an actuator, an expression (or symbol) must be supplied to
|
| 95 |
+
represent the force it can produce, alongside a pathway specifying its line
|
| 96 |
+
of action. Let's also create a global reference frame and spatially fix one
|
| 97 |
+
of the points in it while setting the other to be positioned such that it
|
| 98 |
+
can freely move in the frame's x direction specified by the coordinate
|
| 99 |
+
``q``.
|
| 100 |
+
|
| 101 |
+
>>> from sympy import symbols
|
| 102 |
+
>>> from sympy.physics.mechanics import (ForceActuator, LinearPathway,
|
| 103 |
+
... Point, ReferenceFrame)
|
| 104 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 105 |
+
>>> N = ReferenceFrame('N')
|
| 106 |
+
>>> q = dynamicsymbols('q')
|
| 107 |
+
>>> force = symbols('F')
|
| 108 |
+
>>> pA, pB = Point('pA'), Point('pB')
|
| 109 |
+
>>> pA.set_vel(N, 0)
|
| 110 |
+
>>> pB.set_pos(pA, q*N.x)
|
| 111 |
+
>>> pB.pos_from(pA)
|
| 112 |
+
q(t)*N.x
|
| 113 |
+
>>> linear_pathway = LinearPathway(pA, pB)
|
| 114 |
+
>>> actuator = ForceActuator(force, linear_pathway)
|
| 115 |
+
>>> actuator
|
| 116 |
+
ForceActuator(F, LinearPathway(pA, pB))
|
| 117 |
+
|
| 118 |
+
Parameters
|
| 119 |
+
==========
|
| 120 |
+
|
| 121 |
+
force : Expr
|
| 122 |
+
The scalar expression defining the (expansile) force that the actuator
|
| 123 |
+
produces.
|
| 124 |
+
pathway : PathwayBase
|
| 125 |
+
The pathway that the actuator follows. This must be an instance of a
|
| 126 |
+
concrete subclass of ``PathwayBase``, e.g. ``LinearPathway``.
|
| 127 |
+
|
| 128 |
+
"""
|
| 129 |
+
|
| 130 |
+
def __init__(self, force, pathway):
|
| 131 |
+
"""Initializer for ``ForceActuator``.
|
| 132 |
+
|
| 133 |
+
Parameters
|
| 134 |
+
==========
|
| 135 |
+
|
| 136 |
+
force : Expr
|
| 137 |
+
The scalar expression defining the (expansile) force that the
|
| 138 |
+
actuator produces.
|
| 139 |
+
pathway : PathwayBase
|
| 140 |
+
The pathway that the actuator follows. This must be an instance of
|
| 141 |
+
a concrete subclass of ``PathwayBase``, e.g. ``LinearPathway``.
|
| 142 |
+
|
| 143 |
+
"""
|
| 144 |
+
self.force = force
|
| 145 |
+
self.pathway = pathway
|
| 146 |
+
|
| 147 |
+
@property
|
| 148 |
+
def force(self):
|
| 149 |
+
"""The magnitude of the force produced by the actuator."""
|
| 150 |
+
return self._force
|
| 151 |
+
|
| 152 |
+
@force.setter
|
| 153 |
+
def force(self, force):
|
| 154 |
+
if hasattr(self, '_force'):
|
| 155 |
+
msg = (
|
| 156 |
+
f'Can\'t set attribute `force` to {repr(force)} as it is '
|
| 157 |
+
f'immutable.'
|
| 158 |
+
)
|
| 159 |
+
raise AttributeError(msg)
|
| 160 |
+
self._force = sympify(force, strict=True)
|
| 161 |
+
|
| 162 |
+
@property
|
| 163 |
+
def pathway(self):
|
| 164 |
+
"""The ``Pathway`` defining the actuator's line of action."""
|
| 165 |
+
return self._pathway
|
| 166 |
+
|
| 167 |
+
@pathway.setter
|
| 168 |
+
def pathway(self, pathway):
|
| 169 |
+
if hasattr(self, '_pathway'):
|
| 170 |
+
msg = (
|
| 171 |
+
f'Can\'t set attribute `pathway` to {repr(pathway)} as it is '
|
| 172 |
+
f'immutable.'
|
| 173 |
+
)
|
| 174 |
+
raise AttributeError(msg)
|
| 175 |
+
if not isinstance(pathway, PathwayBase):
|
| 176 |
+
msg = (
|
| 177 |
+
f'Value {repr(pathway)} passed to `pathway` was of type '
|
| 178 |
+
f'{type(pathway)}, must be {PathwayBase}.'
|
| 179 |
+
)
|
| 180 |
+
raise TypeError(msg)
|
| 181 |
+
self._pathway = pathway
|
| 182 |
+
|
| 183 |
+
def to_loads(self):
|
| 184 |
+
"""Loads required by the equations of motion method classes.
|
| 185 |
+
|
| 186 |
+
Explanation
|
| 187 |
+
===========
|
| 188 |
+
|
| 189 |
+
``KanesMethod`` requires a list of ``Point``-``Vector`` tuples to be
|
| 190 |
+
passed to the ``loads`` parameters of its ``kanes_equations`` method
|
| 191 |
+
when constructing the equations of motion. This method acts as a
|
| 192 |
+
utility to produce the correctly-structred pairs of points and vectors
|
| 193 |
+
required so that these can be easily concatenated with other items in
|
| 194 |
+
the list of loads and passed to ``KanesMethod.kanes_equations``. These
|
| 195 |
+
loads are also in the correct form to also be passed to the other
|
| 196 |
+
equations of motion method classes, e.g. ``LagrangesMethod``.
|
| 197 |
+
|
| 198 |
+
Examples
|
| 199 |
+
========
|
| 200 |
+
|
| 201 |
+
The below example shows how to generate the loads produced by a force
|
| 202 |
+
actuator that follows a linear pathway. In this example we'll assume
|
| 203 |
+
that the force actuator is being used to model a simple linear spring.
|
| 204 |
+
First, create a linear pathway between two points separated by the
|
| 205 |
+
coordinate ``q`` in the ``x`` direction of the global frame ``N``.
|
| 206 |
+
|
| 207 |
+
>>> from sympy.physics.mechanics import (LinearPathway, Point,
|
| 208 |
+
... ReferenceFrame)
|
| 209 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 210 |
+
>>> q = dynamicsymbols('q')
|
| 211 |
+
>>> N = ReferenceFrame('N')
|
| 212 |
+
>>> pA, pB = Point('pA'), Point('pB')
|
| 213 |
+
>>> pB.set_pos(pA, q*N.x)
|
| 214 |
+
>>> pathway = LinearPathway(pA, pB)
|
| 215 |
+
|
| 216 |
+
Now create a symbol ``k`` to describe the spring's stiffness and
|
| 217 |
+
instantiate a force actuator that produces a (contractile) force
|
| 218 |
+
proportional to both the spring's stiffness and the pathway's length.
|
| 219 |
+
Note that actuator classes use the sign convention that expansile
|
| 220 |
+
forces are positive, so for a spring to produce a contractile force the
|
| 221 |
+
spring force needs to be calculated as the negative for the stiffness
|
| 222 |
+
multiplied by the length.
|
| 223 |
+
|
| 224 |
+
>>> from sympy import symbols
|
| 225 |
+
>>> from sympy.physics.mechanics import ForceActuator
|
| 226 |
+
>>> stiffness = symbols('k')
|
| 227 |
+
>>> spring_force = -stiffness*pathway.length
|
| 228 |
+
>>> spring = ForceActuator(spring_force, pathway)
|
| 229 |
+
|
| 230 |
+
The forces produced by the spring can be generated in the list of loads
|
| 231 |
+
form that ``KanesMethod`` (and other equations of motion methods)
|
| 232 |
+
requires by calling the ``to_loads`` method.
|
| 233 |
+
|
| 234 |
+
>>> spring.to_loads()
|
| 235 |
+
[(pA, k*q(t)*N.x), (pB, - k*q(t)*N.x)]
|
| 236 |
+
|
| 237 |
+
A simple linear damper can be modeled in a similar way. Create another
|
| 238 |
+
symbol ``c`` to describe the dampers damping coefficient. This time
|
| 239 |
+
instantiate a force actuator that produces a force proportional to both
|
| 240 |
+
the damper's damping coefficient and the pathway's extension velocity.
|
| 241 |
+
Note that the damping force is negative as it acts in the opposite
|
| 242 |
+
direction to which the damper is changing in length.
|
| 243 |
+
|
| 244 |
+
>>> damping_coefficient = symbols('c')
|
| 245 |
+
>>> damping_force = -damping_coefficient*pathway.extension_velocity
|
| 246 |
+
>>> damper = ForceActuator(damping_force, pathway)
|
| 247 |
+
|
| 248 |
+
Again, the forces produces by the damper can be generated by calling
|
| 249 |
+
the ``to_loads`` method.
|
| 250 |
+
|
| 251 |
+
>>> damper.to_loads()
|
| 252 |
+
[(pA, c*Derivative(q(t), t)*N.x), (pB, - c*Derivative(q(t), t)*N.x)]
|
| 253 |
+
|
| 254 |
+
"""
|
| 255 |
+
return self.pathway.to_loads(self.force)
|
| 256 |
+
|
| 257 |
+
def __repr__(self):
|
| 258 |
+
"""Representation of a ``ForceActuator``."""
|
| 259 |
+
return f'{self.__class__.__name__}({self.force}, {self.pathway})'
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
class LinearSpring(ForceActuator):
|
| 263 |
+
"""A spring with its spring force as a linear function of its length.
|
| 264 |
+
|
| 265 |
+
Explanation
|
| 266 |
+
===========
|
| 267 |
+
|
| 268 |
+
Note that the "linear" in the name ``LinearSpring`` refers to the fact that
|
| 269 |
+
the spring force is a linear function of the springs length. I.e. for a
|
| 270 |
+
linear spring with stiffness ``k``, distance between its ends of ``x``, and
|
| 271 |
+
an equilibrium length of ``0``, the spring force will be ``-k*x``, which is
|
| 272 |
+
a linear function in ``x``. To create a spring that follows a linear, or
|
| 273 |
+
straight, pathway between its two ends, a ``LinearPathway`` instance needs
|
| 274 |
+
to be passed to the ``pathway`` parameter.
|
| 275 |
+
|
| 276 |
+
A ``LinearSpring`` is a subclass of ``ForceActuator`` and so follows the
|
| 277 |
+
same sign conventions for length, extension velocity, and the direction of
|
| 278 |
+
the forces it applies to its points of attachment on bodies. The sign
|
| 279 |
+
convention for the direction of forces is such that, for the case where a
|
| 280 |
+
linear spring is instantiated with a ``LinearPathway`` instance as its
|
| 281 |
+
pathway, they act to push the two ends of the spring away from one another.
|
| 282 |
+
Because springs produces a contractile force and acts to pull the two ends
|
| 283 |
+
together towards the equilibrium length when stretched, the scalar portion
|
| 284 |
+
of the forces on the endpoint are negative in order to flip the sign of the
|
| 285 |
+
forces on the endpoints when converted into vector quantities. The
|
| 286 |
+
following diagram shows the positive force sense and the distance between
|
| 287 |
+
the points::
|
| 288 |
+
|
| 289 |
+
P Q
|
| 290 |
+
o<--- F --->o
|
| 291 |
+
| |
|
| 292 |
+
|<--l(t)--->|
|
| 293 |
+
|
| 294 |
+
Examples
|
| 295 |
+
========
|
| 296 |
+
|
| 297 |
+
To construct a linear spring, an expression (or symbol) must be supplied to
|
| 298 |
+
represent the stiffness (spring constant) of the spring, alongside a
|
| 299 |
+
pathway specifying its line of action. Let's also create a global reference
|
| 300 |
+
frame and spatially fix one of the points in it while setting the other to
|
| 301 |
+
be positioned such that it can freely move in the frame's x direction
|
| 302 |
+
specified by the coordinate ``q``.
|
| 303 |
+
|
| 304 |
+
>>> from sympy import symbols
|
| 305 |
+
>>> from sympy.physics.mechanics import (LinearPathway, LinearSpring,
|
| 306 |
+
... Point, ReferenceFrame)
|
| 307 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 308 |
+
>>> N = ReferenceFrame('N')
|
| 309 |
+
>>> q = dynamicsymbols('q')
|
| 310 |
+
>>> stiffness = symbols('k')
|
| 311 |
+
>>> pA, pB = Point('pA'), Point('pB')
|
| 312 |
+
>>> pA.set_vel(N, 0)
|
| 313 |
+
>>> pB.set_pos(pA, q*N.x)
|
| 314 |
+
>>> pB.pos_from(pA)
|
| 315 |
+
q(t)*N.x
|
| 316 |
+
>>> linear_pathway = LinearPathway(pA, pB)
|
| 317 |
+
>>> spring = LinearSpring(stiffness, linear_pathway)
|
| 318 |
+
>>> spring
|
| 319 |
+
LinearSpring(k, LinearPathway(pA, pB))
|
| 320 |
+
|
| 321 |
+
This spring will produce a force that is proportional to both its stiffness
|
| 322 |
+
and the pathway's length. Note that this force is negative as SymPy's sign
|
| 323 |
+
convention for actuators is that negative forces are contractile.
|
| 324 |
+
|
| 325 |
+
>>> spring.force
|
| 326 |
+
-k*sqrt(q(t)**2)
|
| 327 |
+
|
| 328 |
+
To create a linear spring with a non-zero equilibrium length, an expression
|
| 329 |
+
(or symbol) can be passed to the ``equilibrium_length`` parameter on
|
| 330 |
+
construction on a ``LinearSpring`` instance. Let's create a symbol ``l``
|
| 331 |
+
to denote a non-zero equilibrium length and create another linear spring.
|
| 332 |
+
|
| 333 |
+
>>> l = symbols('l')
|
| 334 |
+
>>> spring = LinearSpring(stiffness, linear_pathway, equilibrium_length=l)
|
| 335 |
+
>>> spring
|
| 336 |
+
LinearSpring(k, LinearPathway(pA, pB), equilibrium_length=l)
|
| 337 |
+
|
| 338 |
+
The spring force of this new spring is again proportional to both its
|
| 339 |
+
stiffness and the pathway's length. However, the spring will not produce
|
| 340 |
+
any force when ``q(t)`` equals ``l``. Note that the force will become
|
| 341 |
+
expansile when ``q(t)`` is less than ``l``, as expected.
|
| 342 |
+
|
| 343 |
+
>>> spring.force
|
| 344 |
+
-k*(-l + sqrt(q(t)**2))
|
| 345 |
+
|
| 346 |
+
Parameters
|
| 347 |
+
==========
|
| 348 |
+
|
| 349 |
+
stiffness : Expr
|
| 350 |
+
The spring constant.
|
| 351 |
+
pathway : PathwayBase
|
| 352 |
+
The pathway that the actuator follows. This must be an instance of a
|
| 353 |
+
concrete subclass of ``PathwayBase``, e.g. ``LinearPathway``.
|
| 354 |
+
equilibrium_length : Expr, optional
|
| 355 |
+
The length at which the spring is in equilibrium, i.e. it produces no
|
| 356 |
+
force. The default value is 0, i.e. the spring force is a linear
|
| 357 |
+
function of the pathway's length with no constant offset.
|
| 358 |
+
|
| 359 |
+
See Also
|
| 360 |
+
========
|
| 361 |
+
|
| 362 |
+
ForceActuator: force-producing actuator (superclass of ``LinearSpring``).
|
| 363 |
+
LinearPathway: straight-line pathway between a pair of points.
|
| 364 |
+
|
| 365 |
+
"""
|
| 366 |
+
|
| 367 |
+
def __init__(self, stiffness, pathway, equilibrium_length=S.Zero):
|
| 368 |
+
"""Initializer for ``LinearSpring``.
|
| 369 |
+
|
| 370 |
+
Parameters
|
| 371 |
+
==========
|
| 372 |
+
|
| 373 |
+
stiffness : Expr
|
| 374 |
+
The spring constant.
|
| 375 |
+
pathway : PathwayBase
|
| 376 |
+
The pathway that the actuator follows. This must be an instance of
|
| 377 |
+
a concrete subclass of ``PathwayBase``, e.g. ``LinearPathway``.
|
| 378 |
+
equilibrium_length : Expr, optional
|
| 379 |
+
The length at which the spring is in equilibrium, i.e. it produces
|
| 380 |
+
no force. The default value is 0, i.e. the spring force is a linear
|
| 381 |
+
function of the pathway's length with no constant offset.
|
| 382 |
+
|
| 383 |
+
"""
|
| 384 |
+
self.stiffness = stiffness
|
| 385 |
+
self.pathway = pathway
|
| 386 |
+
self.equilibrium_length = equilibrium_length
|
| 387 |
+
|
| 388 |
+
@property
|
| 389 |
+
def force(self):
|
| 390 |
+
"""The spring force produced by the linear spring."""
|
| 391 |
+
return -self.stiffness*(self.pathway.length - self.equilibrium_length)
|
| 392 |
+
|
| 393 |
+
@force.setter
|
| 394 |
+
def force(self, force):
|
| 395 |
+
raise AttributeError('Can\'t set computed attribute `force`.')
|
| 396 |
+
|
| 397 |
+
@property
|
| 398 |
+
def stiffness(self):
|
| 399 |
+
"""The spring constant for the linear spring."""
|
| 400 |
+
return self._stiffness
|
| 401 |
+
|
| 402 |
+
@stiffness.setter
|
| 403 |
+
def stiffness(self, stiffness):
|
| 404 |
+
if hasattr(self, '_stiffness'):
|
| 405 |
+
msg = (
|
| 406 |
+
f'Can\'t set attribute `stiffness` to {repr(stiffness)} as it '
|
| 407 |
+
f'is immutable.'
|
| 408 |
+
)
|
| 409 |
+
raise AttributeError(msg)
|
| 410 |
+
self._stiffness = sympify(stiffness, strict=True)
|
| 411 |
+
|
| 412 |
+
@property
|
| 413 |
+
def equilibrium_length(self):
|
| 414 |
+
"""The length of the spring at which it produces no force."""
|
| 415 |
+
return self._equilibrium_length
|
| 416 |
+
|
| 417 |
+
@equilibrium_length.setter
|
| 418 |
+
def equilibrium_length(self, equilibrium_length):
|
| 419 |
+
if hasattr(self, '_equilibrium_length'):
|
| 420 |
+
msg = (
|
| 421 |
+
f'Can\'t set attribute `equilibrium_length` to '
|
| 422 |
+
f'{repr(equilibrium_length)} as it is immutable.'
|
| 423 |
+
)
|
| 424 |
+
raise AttributeError(msg)
|
| 425 |
+
self._equilibrium_length = sympify(equilibrium_length, strict=True)
|
| 426 |
+
|
| 427 |
+
def __repr__(self):
|
| 428 |
+
"""Representation of a ``LinearSpring``."""
|
| 429 |
+
string = f'{self.__class__.__name__}({self.stiffness}, {self.pathway}'
|
| 430 |
+
if self.equilibrium_length == S.Zero:
|
| 431 |
+
string += ')'
|
| 432 |
+
else:
|
| 433 |
+
string += f', equilibrium_length={self.equilibrium_length})'
|
| 434 |
+
return string
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
class LinearDamper(ForceActuator):
|
| 438 |
+
"""A damper whose force is a linear function of its extension velocity.
|
| 439 |
+
|
| 440 |
+
Explanation
|
| 441 |
+
===========
|
| 442 |
+
|
| 443 |
+
Note that the "linear" in the name ``LinearDamper`` refers to the fact that
|
| 444 |
+
the damping force is a linear function of the damper's rate of change in
|
| 445 |
+
its length. I.e. for a linear damper with damping ``c`` and extension
|
| 446 |
+
velocity ``v``, the damping force will be ``-c*v``, which is a linear
|
| 447 |
+
function in ``v``. To create a damper that follows a linear, or straight,
|
| 448 |
+
pathway between its two ends, a ``LinearPathway`` instance needs to be
|
| 449 |
+
passed to the ``pathway`` parameter.
|
| 450 |
+
|
| 451 |
+
A ``LinearDamper`` is a subclass of ``ForceActuator`` and so follows the
|
| 452 |
+
same sign conventions for length, extension velocity, and the direction of
|
| 453 |
+
the forces it applies to its points of attachment on bodies. The sign
|
| 454 |
+
convention for the direction of forces is such that, for the case where a
|
| 455 |
+
linear damper is instantiated with a ``LinearPathway`` instance as its
|
| 456 |
+
pathway, they act to push the two ends of the damper away from one another.
|
| 457 |
+
Because dampers produce a force that opposes the direction of change in
|
| 458 |
+
length, when extension velocity is positive the scalar portions of the
|
| 459 |
+
forces applied at the two endpoints are negative in order to flip the sign
|
| 460 |
+
of the forces on the endpoints wen converted into vector quantities. When
|
| 461 |
+
extension velocity is negative (i.e. when the damper is shortening), the
|
| 462 |
+
scalar portions of the fofces applied are also negative so that the signs
|
| 463 |
+
cancel producing forces on the endpoints that are in the same direction as
|
| 464 |
+
the positive sign convention for the forces at the endpoints of the pathway
|
| 465 |
+
(i.e. they act to push the endpoints away from one another). The following
|
| 466 |
+
diagram shows the positive force sense and the distance between the
|
| 467 |
+
points::
|
| 468 |
+
|
| 469 |
+
P Q
|
| 470 |
+
o<--- F --->o
|
| 471 |
+
| |
|
| 472 |
+
|<--l(t)--->|
|
| 473 |
+
|
| 474 |
+
Examples
|
| 475 |
+
========
|
| 476 |
+
|
| 477 |
+
To construct a linear damper, an expression (or symbol) must be supplied to
|
| 478 |
+
represent the damping coefficient of the damper (we'll use the symbol
|
| 479 |
+
``c``), alongside a pathway specifying its line of action. Let's also
|
| 480 |
+
create a global reference frame and spatially fix one of the points in it
|
| 481 |
+
while setting the other to be positioned such that it can freely move in
|
| 482 |
+
the frame's x direction specified by the coordinate ``q``. The velocity
|
| 483 |
+
that the two points move away from one another can be specified by the
|
| 484 |
+
coordinate ``u`` where ``u`` is the first time derivative of ``q``
|
| 485 |
+
(i.e., ``u = Derivative(q(t), t)``).
|
| 486 |
+
|
| 487 |
+
>>> from sympy import symbols
|
| 488 |
+
>>> from sympy.physics.mechanics import (LinearDamper, LinearPathway,
|
| 489 |
+
... Point, ReferenceFrame)
|
| 490 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 491 |
+
>>> N = ReferenceFrame('N')
|
| 492 |
+
>>> q = dynamicsymbols('q')
|
| 493 |
+
>>> damping = symbols('c')
|
| 494 |
+
>>> pA, pB = Point('pA'), Point('pB')
|
| 495 |
+
>>> pA.set_vel(N, 0)
|
| 496 |
+
>>> pB.set_pos(pA, q*N.x)
|
| 497 |
+
>>> pB.pos_from(pA)
|
| 498 |
+
q(t)*N.x
|
| 499 |
+
>>> pB.vel(N)
|
| 500 |
+
Derivative(q(t), t)*N.x
|
| 501 |
+
>>> linear_pathway = LinearPathway(pA, pB)
|
| 502 |
+
>>> damper = LinearDamper(damping, linear_pathway)
|
| 503 |
+
>>> damper
|
| 504 |
+
LinearDamper(c, LinearPathway(pA, pB))
|
| 505 |
+
|
| 506 |
+
This damper will produce a force that is proportional to both its damping
|
| 507 |
+
coefficient and the pathway's extension length. Note that this force is
|
| 508 |
+
negative as SymPy's sign convention for actuators is that negative forces
|
| 509 |
+
are contractile and the damping force of the damper will oppose the
|
| 510 |
+
direction of length change.
|
| 511 |
+
|
| 512 |
+
>>> damper.force
|
| 513 |
+
-c*sqrt(q(t)**2)*Derivative(q(t), t)/q(t)
|
| 514 |
+
|
| 515 |
+
Parameters
|
| 516 |
+
==========
|
| 517 |
+
|
| 518 |
+
damping : Expr
|
| 519 |
+
The damping constant.
|
| 520 |
+
pathway : PathwayBase
|
| 521 |
+
The pathway that the actuator follows. This must be an instance of a
|
| 522 |
+
concrete subclass of ``PathwayBase``, e.g. ``LinearPathway``.
|
| 523 |
+
|
| 524 |
+
See Also
|
| 525 |
+
========
|
| 526 |
+
|
| 527 |
+
ForceActuator: force-producing actuator (superclass of ``LinearDamper``).
|
| 528 |
+
LinearPathway: straight-line pathway between a pair of points.
|
| 529 |
+
|
| 530 |
+
"""
|
| 531 |
+
|
| 532 |
+
def __init__(self, damping, pathway):
|
| 533 |
+
"""Initializer for ``LinearDamper``.
|
| 534 |
+
|
| 535 |
+
Parameters
|
| 536 |
+
==========
|
| 537 |
+
|
| 538 |
+
damping : Expr
|
| 539 |
+
The damping constant.
|
| 540 |
+
pathway : PathwayBase
|
| 541 |
+
The pathway that the actuator follows. This must be an instance of
|
| 542 |
+
a concrete subclass of ``PathwayBase``, e.g. ``LinearPathway``.
|
| 543 |
+
|
| 544 |
+
"""
|
| 545 |
+
self.damping = damping
|
| 546 |
+
self.pathway = pathway
|
| 547 |
+
|
| 548 |
+
@property
|
| 549 |
+
def force(self):
|
| 550 |
+
"""The damping force produced by the linear damper."""
|
| 551 |
+
return -self.damping*self.pathway.extension_velocity
|
| 552 |
+
|
| 553 |
+
@force.setter
|
| 554 |
+
def force(self, force):
|
| 555 |
+
raise AttributeError('Can\'t set computed attribute `force`.')
|
| 556 |
+
|
| 557 |
+
@property
|
| 558 |
+
def damping(self):
|
| 559 |
+
"""The damping constant for the linear damper."""
|
| 560 |
+
return self._damping
|
| 561 |
+
|
| 562 |
+
@damping.setter
|
| 563 |
+
def damping(self, damping):
|
| 564 |
+
if hasattr(self, '_damping'):
|
| 565 |
+
msg = (
|
| 566 |
+
f'Can\'t set attribute `damping` to {repr(damping)} as it is '
|
| 567 |
+
f'immutable.'
|
| 568 |
+
)
|
| 569 |
+
raise AttributeError(msg)
|
| 570 |
+
self._damping = sympify(damping, strict=True)
|
| 571 |
+
|
| 572 |
+
def __repr__(self):
|
| 573 |
+
"""Representation of a ``LinearDamper``."""
|
| 574 |
+
return f'{self.__class__.__name__}({self.damping}, {self.pathway})'
|
| 575 |
+
|
| 576 |
+
|
| 577 |
+
class TorqueActuator(ActuatorBase):
|
| 578 |
+
"""Torque-producing actuator.
|
| 579 |
+
|
| 580 |
+
Explanation
|
| 581 |
+
===========
|
| 582 |
+
|
| 583 |
+
A ``TorqueActuator`` is an actuator that produces a pair of equal and
|
| 584 |
+
opposite torques on a pair of bodies.
|
| 585 |
+
|
| 586 |
+
Examples
|
| 587 |
+
========
|
| 588 |
+
|
| 589 |
+
To construct a torque actuator, an expression (or symbol) must be supplied
|
| 590 |
+
to represent the torque it can produce, alongside a vector specifying the
|
| 591 |
+
axis about which the torque will act, and a pair of frames on which the
|
| 592 |
+
torque will act.
|
| 593 |
+
|
| 594 |
+
>>> from sympy import symbols
|
| 595 |
+
>>> from sympy.physics.mechanics import (ReferenceFrame, RigidBody,
|
| 596 |
+
... TorqueActuator)
|
| 597 |
+
>>> N = ReferenceFrame('N')
|
| 598 |
+
>>> A = ReferenceFrame('A')
|
| 599 |
+
>>> torque = symbols('T')
|
| 600 |
+
>>> axis = N.z
|
| 601 |
+
>>> parent = RigidBody('parent', frame=N)
|
| 602 |
+
>>> child = RigidBody('child', frame=A)
|
| 603 |
+
>>> bodies = (child, parent)
|
| 604 |
+
>>> actuator = TorqueActuator(torque, axis, *bodies)
|
| 605 |
+
>>> actuator
|
| 606 |
+
TorqueActuator(T, axis=N.z, target_frame=A, reaction_frame=N)
|
| 607 |
+
|
| 608 |
+
Note that because torques actually act on frames, not bodies,
|
| 609 |
+
``TorqueActuator`` will extract the frame associated with a ``RigidBody``
|
| 610 |
+
when one is passed instead of a ``ReferenceFrame``.
|
| 611 |
+
|
| 612 |
+
Parameters
|
| 613 |
+
==========
|
| 614 |
+
|
| 615 |
+
torque : Expr
|
| 616 |
+
The scalar expression defining the torque that the actuator produces.
|
| 617 |
+
axis : Vector
|
| 618 |
+
The axis about which the actuator applies torques.
|
| 619 |
+
target_frame : ReferenceFrame | RigidBody
|
| 620 |
+
The primary frame on which the actuator will apply the torque.
|
| 621 |
+
reaction_frame : ReferenceFrame | RigidBody | None
|
| 622 |
+
The secondary frame on which the actuator will apply the torque. Note
|
| 623 |
+
that the (equal and opposite) reaction torque is applied to this frame.
|
| 624 |
+
|
| 625 |
+
"""
|
| 626 |
+
|
| 627 |
+
def __init__(self, torque, axis, target_frame, reaction_frame=None):
|
| 628 |
+
"""Initializer for ``TorqueActuator``.
|
| 629 |
+
|
| 630 |
+
Parameters
|
| 631 |
+
==========
|
| 632 |
+
|
| 633 |
+
torque : Expr
|
| 634 |
+
The scalar expression defining the torque that the actuator
|
| 635 |
+
produces.
|
| 636 |
+
axis : Vector
|
| 637 |
+
The axis about which the actuator applies torques.
|
| 638 |
+
target_frame : ReferenceFrame | RigidBody
|
| 639 |
+
The primary frame on which the actuator will apply the torque.
|
| 640 |
+
reaction_frame : ReferenceFrame | RigidBody | None
|
| 641 |
+
The secondary frame on which the actuator will apply the torque.
|
| 642 |
+
Note that the (equal and opposite) reaction torque is applied to
|
| 643 |
+
this frame.
|
| 644 |
+
|
| 645 |
+
"""
|
| 646 |
+
self.torque = torque
|
| 647 |
+
self.axis = axis
|
| 648 |
+
self.target_frame = target_frame
|
| 649 |
+
self.reaction_frame = reaction_frame
|
| 650 |
+
|
| 651 |
+
@classmethod
|
| 652 |
+
def at_pin_joint(cls, torque, pin_joint):
|
| 653 |
+
"""Alternate construtor to instantiate from a ``PinJoint`` instance.
|
| 654 |
+
|
| 655 |
+
Examples
|
| 656 |
+
========
|
| 657 |
+
|
| 658 |
+
To create a pin joint the ``PinJoint`` class requires a name, parent
|
| 659 |
+
body, and child body to be passed to its constructor. It is also
|
| 660 |
+
possible to control the joint axis using the ``joint_axis`` keyword
|
| 661 |
+
argument. In this example let's use the parent body's reference frame's
|
| 662 |
+
z-axis as the joint axis.
|
| 663 |
+
|
| 664 |
+
>>> from sympy.physics.mechanics import (PinJoint, ReferenceFrame,
|
| 665 |
+
... RigidBody, TorqueActuator)
|
| 666 |
+
>>> N = ReferenceFrame('N')
|
| 667 |
+
>>> A = ReferenceFrame('A')
|
| 668 |
+
>>> parent = RigidBody('parent', frame=N)
|
| 669 |
+
>>> child = RigidBody('child', frame=A)
|
| 670 |
+
>>> pin_joint = PinJoint(
|
| 671 |
+
... 'pin',
|
| 672 |
+
... parent,
|
| 673 |
+
... child,
|
| 674 |
+
... joint_axis=N.z,
|
| 675 |
+
... )
|
| 676 |
+
|
| 677 |
+
Let's also create a symbol ``T`` that will represent the torque applied
|
| 678 |
+
by the torque actuator.
|
| 679 |
+
|
| 680 |
+
>>> from sympy import symbols
|
| 681 |
+
>>> torque = symbols('T')
|
| 682 |
+
|
| 683 |
+
To create the torque actuator from the ``torque`` and ``pin_joint``
|
| 684 |
+
variables previously instantiated, these can be passed to the alternate
|
| 685 |
+
constructor class method ``at_pin_joint`` of the ``TorqueActuator``
|
| 686 |
+
class. It should be noted that a positive torque will cause a positive
|
| 687 |
+
displacement of the joint coordinate or that the torque is applied on
|
| 688 |
+
the child body with a reaction torque on the parent.
|
| 689 |
+
|
| 690 |
+
>>> actuator = TorqueActuator.at_pin_joint(torque, pin_joint)
|
| 691 |
+
>>> actuator
|
| 692 |
+
TorqueActuator(T, axis=N.z, target_frame=A, reaction_frame=N)
|
| 693 |
+
|
| 694 |
+
Parameters
|
| 695 |
+
==========
|
| 696 |
+
|
| 697 |
+
torque : Expr
|
| 698 |
+
The scalar expression defining the torque that the actuator
|
| 699 |
+
produces.
|
| 700 |
+
pin_joint : PinJoint
|
| 701 |
+
The pin joint, and by association the parent and child bodies, on
|
| 702 |
+
which the torque actuator will act. The pair of bodies acted upon
|
| 703 |
+
by the torque actuator are the parent and child bodies of the pin
|
| 704 |
+
joint, with the child acting as the reaction body. The pin joint's
|
| 705 |
+
axis is used as the axis about which the torque actuator will apply
|
| 706 |
+
its torque.
|
| 707 |
+
|
| 708 |
+
"""
|
| 709 |
+
if not isinstance(pin_joint, PinJoint):
|
| 710 |
+
msg = (
|
| 711 |
+
f'Value {repr(pin_joint)} passed to `pin_joint` was of type '
|
| 712 |
+
f'{type(pin_joint)}, must be {PinJoint}.'
|
| 713 |
+
)
|
| 714 |
+
raise TypeError(msg)
|
| 715 |
+
return cls(
|
| 716 |
+
torque,
|
| 717 |
+
pin_joint.joint_axis,
|
| 718 |
+
pin_joint.child_interframe,
|
| 719 |
+
pin_joint.parent_interframe,
|
| 720 |
+
)
|
| 721 |
+
|
| 722 |
+
@property
|
| 723 |
+
def torque(self):
|
| 724 |
+
"""The magnitude of the torque produced by the actuator."""
|
| 725 |
+
return self._torque
|
| 726 |
+
|
| 727 |
+
@torque.setter
|
| 728 |
+
def torque(self, torque):
|
| 729 |
+
if hasattr(self, '_torque'):
|
| 730 |
+
msg = (
|
| 731 |
+
f'Can\'t set attribute `torque` to {repr(torque)} as it is '
|
| 732 |
+
f'immutable.'
|
| 733 |
+
)
|
| 734 |
+
raise AttributeError(msg)
|
| 735 |
+
self._torque = sympify(torque, strict=True)
|
| 736 |
+
|
| 737 |
+
@property
|
| 738 |
+
def axis(self):
|
| 739 |
+
"""The axis about which the torque acts."""
|
| 740 |
+
return self._axis
|
| 741 |
+
|
| 742 |
+
@axis.setter
|
| 743 |
+
def axis(self, axis):
|
| 744 |
+
if hasattr(self, '_axis'):
|
| 745 |
+
msg = (
|
| 746 |
+
f'Can\'t set attribute `axis` to {repr(axis)} as it is '
|
| 747 |
+
f'immutable.'
|
| 748 |
+
)
|
| 749 |
+
raise AttributeError(msg)
|
| 750 |
+
if not isinstance(axis, Vector):
|
| 751 |
+
msg = (
|
| 752 |
+
f'Value {repr(axis)} passed to `axis` was of type '
|
| 753 |
+
f'{type(axis)}, must be {Vector}.'
|
| 754 |
+
)
|
| 755 |
+
raise TypeError(msg)
|
| 756 |
+
self._axis = axis
|
| 757 |
+
|
| 758 |
+
@property
|
| 759 |
+
def target_frame(self):
|
| 760 |
+
"""The primary reference frames on which the torque will act."""
|
| 761 |
+
return self._target_frame
|
| 762 |
+
|
| 763 |
+
@target_frame.setter
|
| 764 |
+
def target_frame(self, target_frame):
|
| 765 |
+
if hasattr(self, '_target_frame'):
|
| 766 |
+
msg = (
|
| 767 |
+
f'Can\'t set attribute `target_frame` to {repr(target_frame)} '
|
| 768 |
+
f'as it is immutable.'
|
| 769 |
+
)
|
| 770 |
+
raise AttributeError(msg)
|
| 771 |
+
if isinstance(target_frame, RigidBody):
|
| 772 |
+
target_frame = target_frame.frame
|
| 773 |
+
elif not isinstance(target_frame, ReferenceFrame):
|
| 774 |
+
msg = (
|
| 775 |
+
f'Value {repr(target_frame)} passed to `target_frame` was of '
|
| 776 |
+
f'type {type(target_frame)}, must be {ReferenceFrame}.'
|
| 777 |
+
)
|
| 778 |
+
raise TypeError(msg)
|
| 779 |
+
self._target_frame = target_frame
|
| 780 |
+
|
| 781 |
+
@property
|
| 782 |
+
def reaction_frame(self):
|
| 783 |
+
"""The primary reference frames on which the torque will act."""
|
| 784 |
+
return self._reaction_frame
|
| 785 |
+
|
| 786 |
+
@reaction_frame.setter
|
| 787 |
+
def reaction_frame(self, reaction_frame):
|
| 788 |
+
if hasattr(self, '_reaction_frame'):
|
| 789 |
+
msg = (
|
| 790 |
+
f'Can\'t set attribute `reaction_frame` to '
|
| 791 |
+
f'{repr(reaction_frame)} as it is immutable.'
|
| 792 |
+
)
|
| 793 |
+
raise AttributeError(msg)
|
| 794 |
+
if isinstance(reaction_frame, RigidBody):
|
| 795 |
+
reaction_frame = reaction_frame.frame
|
| 796 |
+
elif (
|
| 797 |
+
not isinstance(reaction_frame, ReferenceFrame)
|
| 798 |
+
and reaction_frame is not None
|
| 799 |
+
):
|
| 800 |
+
msg = (
|
| 801 |
+
f'Value {repr(reaction_frame)} passed to `reaction_frame` was '
|
| 802 |
+
f'of type {type(reaction_frame)}, must be {ReferenceFrame}.'
|
| 803 |
+
)
|
| 804 |
+
raise TypeError(msg)
|
| 805 |
+
self._reaction_frame = reaction_frame
|
| 806 |
+
|
| 807 |
+
def to_loads(self):
|
| 808 |
+
"""Loads required by the equations of motion method classes.
|
| 809 |
+
|
| 810 |
+
Explanation
|
| 811 |
+
===========
|
| 812 |
+
|
| 813 |
+
``KanesMethod`` requires a list of ``Point``-``Vector`` tuples to be
|
| 814 |
+
passed to the ``loads`` parameters of its ``kanes_equations`` method
|
| 815 |
+
when constructing the equations of motion. This method acts as a
|
| 816 |
+
utility to produce the correctly-structred pairs of points and vectors
|
| 817 |
+
required so that these can be easily concatenated with other items in
|
| 818 |
+
the list of loads and passed to ``KanesMethod.kanes_equations``. These
|
| 819 |
+
loads are also in the correct form to also be passed to the other
|
| 820 |
+
equations of motion method classes, e.g. ``LagrangesMethod``.
|
| 821 |
+
|
| 822 |
+
Examples
|
| 823 |
+
========
|
| 824 |
+
|
| 825 |
+
The below example shows how to generate the loads produced by a torque
|
| 826 |
+
actuator that acts on a pair of bodies attached by a pin joint.
|
| 827 |
+
|
| 828 |
+
>>> from sympy import symbols
|
| 829 |
+
>>> from sympy.physics.mechanics import (PinJoint, ReferenceFrame,
|
| 830 |
+
... RigidBody, TorqueActuator)
|
| 831 |
+
>>> torque = symbols('T')
|
| 832 |
+
>>> N = ReferenceFrame('N')
|
| 833 |
+
>>> A = ReferenceFrame('A')
|
| 834 |
+
>>> parent = RigidBody('parent', frame=N)
|
| 835 |
+
>>> child = RigidBody('child', frame=A)
|
| 836 |
+
>>> pin_joint = PinJoint(
|
| 837 |
+
... 'pin',
|
| 838 |
+
... parent,
|
| 839 |
+
... child,
|
| 840 |
+
... joint_axis=N.z,
|
| 841 |
+
... )
|
| 842 |
+
>>> actuator = TorqueActuator.at_pin_joint(torque, pin_joint)
|
| 843 |
+
|
| 844 |
+
The forces produces by the damper can be generated by calling the
|
| 845 |
+
``to_loads`` method.
|
| 846 |
+
|
| 847 |
+
>>> actuator.to_loads()
|
| 848 |
+
[(A, T*N.z), (N, - T*N.z)]
|
| 849 |
+
|
| 850 |
+
Alternatively, if a torque actuator is created without a reaction frame
|
| 851 |
+
then the loads returned by the ``to_loads`` method will contain just
|
| 852 |
+
the single load acting on the target frame.
|
| 853 |
+
|
| 854 |
+
>>> actuator = TorqueActuator(torque, N.z, N)
|
| 855 |
+
>>> actuator.to_loads()
|
| 856 |
+
[(N, T*N.z)]
|
| 857 |
+
|
| 858 |
+
"""
|
| 859 |
+
loads = [
|
| 860 |
+
Torque(self.target_frame, self.torque*self.axis),
|
| 861 |
+
]
|
| 862 |
+
if self.reaction_frame is not None:
|
| 863 |
+
loads.append(Torque(self.reaction_frame, -self.torque*self.axis))
|
| 864 |
+
return loads
|
| 865 |
+
|
| 866 |
+
def __repr__(self):
|
| 867 |
+
"""Representation of a ``TorqueActuator``."""
|
| 868 |
+
string = (
|
| 869 |
+
f'{self.__class__.__name__}({self.torque}, axis={self.axis}, '
|
| 870 |
+
f'target_frame={self.target_frame}'
|
| 871 |
+
)
|
| 872 |
+
if self.reaction_frame is not None:
|
| 873 |
+
string += f', reaction_frame={self.reaction_frame})'
|
| 874 |
+
else:
|
| 875 |
+
string += ')'
|
| 876 |
+
return string
|
| 877 |
+
|
| 878 |
+
|
| 879 |
+
class DuffingSpring(ForceActuator):
|
| 880 |
+
"""A nonlinear spring based on the Duffing equation.
|
| 881 |
+
|
| 882 |
+
Explanation
|
| 883 |
+
===========
|
| 884 |
+
|
| 885 |
+
Here, ``DuffingSpring`` represents the force exerted by a nonlinear spring based on the Duffing equation:
|
| 886 |
+
F = -beta*x-alpha*x**3, where x is the displacement from the equilibrium position, beta is the linear spring constant,
|
| 887 |
+
and alpha is the coefficient for the nonlinear cubic term.
|
| 888 |
+
|
| 889 |
+
Parameters
|
| 890 |
+
==========
|
| 891 |
+
|
| 892 |
+
linear_stiffness : Expr
|
| 893 |
+
The linear stiffness coefficient (beta).
|
| 894 |
+
nonlinear_stiffness : Expr
|
| 895 |
+
The nonlinear stiffness coefficient (alpha).
|
| 896 |
+
pathway : PathwayBase
|
| 897 |
+
The pathway that the actuator follows.
|
| 898 |
+
equilibrium_length : Expr, optional
|
| 899 |
+
The length at which the spring is in equilibrium (x).
|
| 900 |
+
"""
|
| 901 |
+
|
| 902 |
+
def __init__(self, linear_stiffness, nonlinear_stiffness, pathway, equilibrium_length=S.Zero):
|
| 903 |
+
self.linear_stiffness = sympify(linear_stiffness, strict=True)
|
| 904 |
+
self.nonlinear_stiffness = sympify(nonlinear_stiffness, strict=True)
|
| 905 |
+
self.equilibrium_length = sympify(equilibrium_length, strict=True)
|
| 906 |
+
|
| 907 |
+
if not isinstance(pathway, PathwayBase):
|
| 908 |
+
raise TypeError("pathway must be an instance of PathwayBase.")
|
| 909 |
+
self._pathway = pathway
|
| 910 |
+
|
| 911 |
+
@property
|
| 912 |
+
def linear_stiffness(self):
|
| 913 |
+
return self._linear_stiffness
|
| 914 |
+
|
| 915 |
+
@linear_stiffness.setter
|
| 916 |
+
def linear_stiffness(self, linear_stiffness):
|
| 917 |
+
if hasattr(self, '_linear_stiffness'):
|
| 918 |
+
msg = (
|
| 919 |
+
f'Can\'t set attribute `linear_stiffness` to '
|
| 920 |
+
f'{repr(linear_stiffness)} as it is immutable.'
|
| 921 |
+
)
|
| 922 |
+
raise AttributeError(msg)
|
| 923 |
+
self._linear_stiffness = sympify(linear_stiffness, strict=True)
|
| 924 |
+
|
| 925 |
+
@property
|
| 926 |
+
def nonlinear_stiffness(self):
|
| 927 |
+
return self._nonlinear_stiffness
|
| 928 |
+
|
| 929 |
+
@nonlinear_stiffness.setter
|
| 930 |
+
def nonlinear_stiffness(self, nonlinear_stiffness):
|
| 931 |
+
if hasattr(self, '_nonlinear_stiffness'):
|
| 932 |
+
msg = (
|
| 933 |
+
f'Can\'t set attribute `nonlinear_stiffness` to '
|
| 934 |
+
f'{repr(nonlinear_stiffness)} as it is immutable.'
|
| 935 |
+
)
|
| 936 |
+
raise AttributeError(msg)
|
| 937 |
+
self._nonlinear_stiffness = sympify(nonlinear_stiffness, strict=True)
|
| 938 |
+
|
| 939 |
+
@property
|
| 940 |
+
def pathway(self):
|
| 941 |
+
return self._pathway
|
| 942 |
+
|
| 943 |
+
@pathway.setter
|
| 944 |
+
def pathway(self, pathway):
|
| 945 |
+
if hasattr(self, '_pathway'):
|
| 946 |
+
msg = (
|
| 947 |
+
f'Can\'t set attribute `pathway` to {repr(pathway)} as it is '
|
| 948 |
+
f'immutable.'
|
| 949 |
+
)
|
| 950 |
+
raise AttributeError(msg)
|
| 951 |
+
if not isinstance(pathway, PathwayBase):
|
| 952 |
+
msg = (
|
| 953 |
+
f'Value {repr(pathway)} passed to `pathway` was of type '
|
| 954 |
+
f'{type(pathway)}, must be {PathwayBase}.'
|
| 955 |
+
)
|
| 956 |
+
raise TypeError(msg)
|
| 957 |
+
self._pathway = pathway
|
| 958 |
+
|
| 959 |
+
@property
|
| 960 |
+
def equilibrium_length(self):
|
| 961 |
+
return self._equilibrium_length
|
| 962 |
+
|
| 963 |
+
@equilibrium_length.setter
|
| 964 |
+
def equilibrium_length(self, equilibrium_length):
|
| 965 |
+
if hasattr(self, '_equilibrium_length'):
|
| 966 |
+
msg = (
|
| 967 |
+
f'Can\'t set attribute `equilibrium_length` to '
|
| 968 |
+
f'{repr(equilibrium_length)} as it is immutable.'
|
| 969 |
+
)
|
| 970 |
+
raise AttributeError(msg)
|
| 971 |
+
self._equilibrium_length = sympify(equilibrium_length, strict=True)
|
| 972 |
+
|
| 973 |
+
@property
|
| 974 |
+
def force(self):
|
| 975 |
+
"""The force produced by the Duffing spring."""
|
| 976 |
+
displacement = self.pathway.length - self.equilibrium_length
|
| 977 |
+
return -self.linear_stiffness * displacement - self.nonlinear_stiffness * displacement**3
|
| 978 |
+
|
| 979 |
+
@force.setter
|
| 980 |
+
def force(self, force):
|
| 981 |
+
if hasattr(self, '_force'):
|
| 982 |
+
msg = (
|
| 983 |
+
f'Can\'t set attribute `force` to {repr(force)} as it is '
|
| 984 |
+
f'immutable.'
|
| 985 |
+
)
|
| 986 |
+
raise AttributeError(msg)
|
| 987 |
+
self._force = sympify(force, strict=True)
|
| 988 |
+
|
| 989 |
+
def __repr__(self):
|
| 990 |
+
return (f"{self.__class__.__name__}("
|
| 991 |
+
f"{self.linear_stiffness}, {self.nonlinear_stiffness}, {self.pathway}, "
|
| 992 |
+
f"equilibrium_length={self.equilibrium_length})")
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/body.py
ADDED
|
@@ -0,0 +1,710 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy import Symbol
|
| 2 |
+
from sympy.physics.vector import Point, Vector, ReferenceFrame, Dyadic
|
| 3 |
+
from sympy.physics.mechanics import RigidBody, Particle, Inertia
|
| 4 |
+
from sympy.physics.mechanics.body_base import BodyBase
|
| 5 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 6 |
+
|
| 7 |
+
__all__ = ['Body']
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# XXX: We use type:ignore because the classes RigidBody and Particle have
|
| 11 |
+
# inconsistent parallel axis methods that take different numbers of arguments.
|
| 12 |
+
class Body(RigidBody, Particle): # type: ignore
|
| 13 |
+
"""
|
| 14 |
+
Body is a common representation of either a RigidBody or a Particle SymPy
|
| 15 |
+
object depending on what is passed in during initialization. If a mass is
|
| 16 |
+
passed in and central_inertia is left as None, the Particle object is
|
| 17 |
+
created. Otherwise a RigidBody object will be created.
|
| 18 |
+
|
| 19 |
+
.. deprecated:: 1.13
|
| 20 |
+
The Body class is deprecated. Its functionality is captured by
|
| 21 |
+
:class:`~.RigidBody` and :class:`~.Particle`.
|
| 22 |
+
|
| 23 |
+
Explanation
|
| 24 |
+
===========
|
| 25 |
+
|
| 26 |
+
The attributes that Body possesses will be the same as a Particle instance
|
| 27 |
+
or a Rigid Body instance depending on which was created. Additional
|
| 28 |
+
attributes are listed below.
|
| 29 |
+
|
| 30 |
+
Attributes
|
| 31 |
+
==========
|
| 32 |
+
|
| 33 |
+
name : string
|
| 34 |
+
The body's name
|
| 35 |
+
masscenter : Point
|
| 36 |
+
The point which represents the center of mass of the rigid body
|
| 37 |
+
frame : ReferenceFrame
|
| 38 |
+
The reference frame which the body is fixed in
|
| 39 |
+
mass : Sympifyable
|
| 40 |
+
The body's mass
|
| 41 |
+
inertia : (Dyadic, Point)
|
| 42 |
+
The body's inertia around its center of mass. This attribute is specific
|
| 43 |
+
to the rigid body form of Body and is left undefined for the Particle
|
| 44 |
+
form
|
| 45 |
+
loads : iterable
|
| 46 |
+
This list contains information on the different loads acting on the
|
| 47 |
+
Body. Forces are listed as a (point, vector) tuple and torques are
|
| 48 |
+
listed as (reference frame, vector) tuples.
|
| 49 |
+
|
| 50 |
+
Parameters
|
| 51 |
+
==========
|
| 52 |
+
|
| 53 |
+
name : String
|
| 54 |
+
Defines the name of the body. It is used as the base for defining
|
| 55 |
+
body specific properties.
|
| 56 |
+
masscenter : Point, optional
|
| 57 |
+
A point that represents the center of mass of the body or particle.
|
| 58 |
+
If no point is given, a point is generated.
|
| 59 |
+
mass : Sympifyable, optional
|
| 60 |
+
A Sympifyable object which represents the mass of the body. If no
|
| 61 |
+
mass is passed, one is generated.
|
| 62 |
+
frame : ReferenceFrame, optional
|
| 63 |
+
The ReferenceFrame that represents the reference frame of the body.
|
| 64 |
+
If no frame is given, a frame is generated.
|
| 65 |
+
central_inertia : Dyadic, optional
|
| 66 |
+
Central inertia dyadic of the body. If none is passed while creating
|
| 67 |
+
RigidBody, a default inertia is generated.
|
| 68 |
+
|
| 69 |
+
Examples
|
| 70 |
+
========
|
| 71 |
+
|
| 72 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 73 |
+
purposes only. The functionality of Body is fully captured by
|
| 74 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 75 |
+
warning we can use the ignore_warnings context manager.
|
| 76 |
+
|
| 77 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 78 |
+
|
| 79 |
+
Default behaviour. This results in the creation of a RigidBody object for
|
| 80 |
+
which the mass, mass center, frame and inertia attributes are given default
|
| 81 |
+
values. ::
|
| 82 |
+
|
| 83 |
+
>>> from sympy.physics.mechanics import Body
|
| 84 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 85 |
+
... body = Body('name_of_body')
|
| 86 |
+
|
| 87 |
+
This next example demonstrates the code required to specify all of the
|
| 88 |
+
values of the Body object. Note this will also create a RigidBody version of
|
| 89 |
+
the Body object. ::
|
| 90 |
+
|
| 91 |
+
>>> from sympy import Symbol
|
| 92 |
+
>>> from sympy.physics.mechanics import ReferenceFrame, Point, inertia
|
| 93 |
+
>>> from sympy.physics.mechanics import Body
|
| 94 |
+
>>> mass = Symbol('mass')
|
| 95 |
+
>>> masscenter = Point('masscenter')
|
| 96 |
+
>>> frame = ReferenceFrame('frame')
|
| 97 |
+
>>> ixx = Symbol('ixx')
|
| 98 |
+
>>> body_inertia = inertia(frame, ixx, 0, 0)
|
| 99 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 100 |
+
... body = Body('name_of_body', masscenter, mass, frame, body_inertia)
|
| 101 |
+
|
| 102 |
+
The minimal code required to create a Particle version of the Body object
|
| 103 |
+
involves simply passing in a name and a mass. ::
|
| 104 |
+
|
| 105 |
+
>>> from sympy import Symbol
|
| 106 |
+
>>> from sympy.physics.mechanics import Body
|
| 107 |
+
>>> mass = Symbol('mass')
|
| 108 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 109 |
+
... body = Body('name_of_body', mass=mass)
|
| 110 |
+
|
| 111 |
+
The Particle version of the Body object can also receive a masscenter point
|
| 112 |
+
and a reference frame, just not an inertia.
|
| 113 |
+
"""
|
| 114 |
+
|
| 115 |
+
def __init__(self, name, masscenter=None, mass=None, frame=None,
|
| 116 |
+
central_inertia=None):
|
| 117 |
+
sympy_deprecation_warning(
|
| 118 |
+
"""
|
| 119 |
+
Support for the Body class has been removed, as its functionality is
|
| 120 |
+
fully captured by RigidBody and Particle.
|
| 121 |
+
""",
|
| 122 |
+
deprecated_since_version="1.13",
|
| 123 |
+
active_deprecations_target="deprecated-mechanics-body-class"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
self._loads = []
|
| 127 |
+
|
| 128 |
+
if frame is None:
|
| 129 |
+
frame = ReferenceFrame(name + '_frame')
|
| 130 |
+
|
| 131 |
+
if masscenter is None:
|
| 132 |
+
masscenter = Point(name + '_masscenter')
|
| 133 |
+
|
| 134 |
+
if central_inertia is None and mass is None:
|
| 135 |
+
ixx = Symbol(name + '_ixx')
|
| 136 |
+
iyy = Symbol(name + '_iyy')
|
| 137 |
+
izz = Symbol(name + '_izz')
|
| 138 |
+
izx = Symbol(name + '_izx')
|
| 139 |
+
ixy = Symbol(name + '_ixy')
|
| 140 |
+
iyz = Symbol(name + '_iyz')
|
| 141 |
+
_inertia = Inertia.from_inertia_scalars(masscenter, frame, ixx, iyy,
|
| 142 |
+
izz, ixy, iyz, izx)
|
| 143 |
+
else:
|
| 144 |
+
_inertia = (central_inertia, masscenter)
|
| 145 |
+
|
| 146 |
+
if mass is None:
|
| 147 |
+
_mass = Symbol(name + '_mass')
|
| 148 |
+
else:
|
| 149 |
+
_mass = mass
|
| 150 |
+
|
| 151 |
+
masscenter.set_vel(frame, 0)
|
| 152 |
+
|
| 153 |
+
# If user passes masscenter and mass then a particle is created
|
| 154 |
+
# otherwise a rigidbody. As a result a body may or may not have inertia.
|
| 155 |
+
# Note: BodyBase.__init__ is used to prevent problems with super() calls in
|
| 156 |
+
# Particle and RigidBody arising due to multiple inheritance.
|
| 157 |
+
if central_inertia is None and mass is not None:
|
| 158 |
+
BodyBase.__init__(self, name, masscenter, _mass)
|
| 159 |
+
self.frame = frame
|
| 160 |
+
self._central_inertia = Dyadic(0)
|
| 161 |
+
else:
|
| 162 |
+
BodyBase.__init__(self, name, masscenter, _mass)
|
| 163 |
+
self.frame = frame
|
| 164 |
+
self.inertia = _inertia
|
| 165 |
+
|
| 166 |
+
def __repr__(self):
|
| 167 |
+
if self.is_rigidbody:
|
| 168 |
+
return RigidBody.__repr__(self)
|
| 169 |
+
return Particle.__repr__(self)
|
| 170 |
+
|
| 171 |
+
@property
|
| 172 |
+
def loads(self):
|
| 173 |
+
return self._loads
|
| 174 |
+
|
| 175 |
+
@property
|
| 176 |
+
def x(self):
|
| 177 |
+
"""The basis Vector for the Body, in the x direction."""
|
| 178 |
+
return self.frame.x
|
| 179 |
+
|
| 180 |
+
@property
|
| 181 |
+
def y(self):
|
| 182 |
+
"""The basis Vector for the Body, in the y direction."""
|
| 183 |
+
return self.frame.y
|
| 184 |
+
|
| 185 |
+
@property
|
| 186 |
+
def z(self):
|
| 187 |
+
"""The basis Vector for the Body, in the z direction."""
|
| 188 |
+
return self.frame.z
|
| 189 |
+
|
| 190 |
+
@property
|
| 191 |
+
def inertia(self):
|
| 192 |
+
"""The body's inertia about a point; stored as (Dyadic, Point)."""
|
| 193 |
+
if self.is_rigidbody:
|
| 194 |
+
return RigidBody.inertia.fget(self)
|
| 195 |
+
return (self.central_inertia, self.masscenter)
|
| 196 |
+
|
| 197 |
+
@inertia.setter
|
| 198 |
+
def inertia(self, I):
|
| 199 |
+
RigidBody.inertia.fset(self, I)
|
| 200 |
+
|
| 201 |
+
@property
|
| 202 |
+
def is_rigidbody(self):
|
| 203 |
+
if hasattr(self, '_inertia'):
|
| 204 |
+
return True
|
| 205 |
+
return False
|
| 206 |
+
|
| 207 |
+
def kinetic_energy(self, frame):
|
| 208 |
+
"""Kinetic energy of the body.
|
| 209 |
+
|
| 210 |
+
Parameters
|
| 211 |
+
==========
|
| 212 |
+
|
| 213 |
+
frame : ReferenceFrame or Body
|
| 214 |
+
The Body's angular velocity and the velocity of it's mass
|
| 215 |
+
center are typically defined with respect to an inertial frame but
|
| 216 |
+
any relevant frame in which the velocities are known can be supplied.
|
| 217 |
+
|
| 218 |
+
Examples
|
| 219 |
+
========
|
| 220 |
+
|
| 221 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 222 |
+
purposes only. The functionality of Body is fully captured by
|
| 223 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 224 |
+
warning we can use the ignore_warnings context manager.
|
| 225 |
+
|
| 226 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 227 |
+
>>> from sympy.physics.mechanics import Body, ReferenceFrame, Point
|
| 228 |
+
>>> from sympy import symbols
|
| 229 |
+
>>> m, v, r, omega = symbols('m v r omega')
|
| 230 |
+
>>> N = ReferenceFrame('N')
|
| 231 |
+
>>> O = Point('O')
|
| 232 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 233 |
+
... P = Body('P', masscenter=O, mass=m)
|
| 234 |
+
>>> P.masscenter.set_vel(N, v * N.y)
|
| 235 |
+
>>> P.kinetic_energy(N)
|
| 236 |
+
m*v**2/2
|
| 237 |
+
|
| 238 |
+
>>> N = ReferenceFrame('N')
|
| 239 |
+
>>> b = ReferenceFrame('b')
|
| 240 |
+
>>> b.set_ang_vel(N, omega * b.x)
|
| 241 |
+
>>> P = Point('P')
|
| 242 |
+
>>> P.set_vel(N, v * N.x)
|
| 243 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 244 |
+
... B = Body('B', masscenter=P, frame=b)
|
| 245 |
+
>>> B.kinetic_energy(N)
|
| 246 |
+
B_ixx*omega**2/2 + B_mass*v**2/2
|
| 247 |
+
|
| 248 |
+
See Also
|
| 249 |
+
========
|
| 250 |
+
|
| 251 |
+
sympy.physics.mechanics : Particle, RigidBody
|
| 252 |
+
|
| 253 |
+
"""
|
| 254 |
+
if isinstance(frame, Body):
|
| 255 |
+
frame = Body.frame
|
| 256 |
+
if self.is_rigidbody:
|
| 257 |
+
return RigidBody(self.name, self.masscenter, self.frame, self.mass,
|
| 258 |
+
(self.central_inertia, self.masscenter)).kinetic_energy(frame)
|
| 259 |
+
return Particle(self.name, self.masscenter, self.mass).kinetic_energy(frame)
|
| 260 |
+
|
| 261 |
+
def apply_force(self, force, point=None, reaction_body=None, reaction_point=None):
|
| 262 |
+
"""Add force to the body(s).
|
| 263 |
+
|
| 264 |
+
Explanation
|
| 265 |
+
===========
|
| 266 |
+
|
| 267 |
+
Applies the force on self or equal and opposite forces on
|
| 268 |
+
self and other body if both are given on the desired point on the bodies.
|
| 269 |
+
The force applied on other body is taken opposite of self, i.e, -force.
|
| 270 |
+
|
| 271 |
+
Parameters
|
| 272 |
+
==========
|
| 273 |
+
|
| 274 |
+
force: Vector
|
| 275 |
+
The force to be applied.
|
| 276 |
+
point: Point, optional
|
| 277 |
+
The point on self on which force is applied.
|
| 278 |
+
By default self's masscenter.
|
| 279 |
+
reaction_body: Body, optional
|
| 280 |
+
Second body on which equal and opposite force
|
| 281 |
+
is to be applied.
|
| 282 |
+
reaction_point : Point, optional
|
| 283 |
+
The point on other body on which equal and opposite
|
| 284 |
+
force is applied. By default masscenter of other body.
|
| 285 |
+
|
| 286 |
+
Example
|
| 287 |
+
=======
|
| 288 |
+
|
| 289 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 290 |
+
purposes only. The functionality of Body is fully captured by
|
| 291 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 292 |
+
warning we can use the ignore_warnings context manager.
|
| 293 |
+
|
| 294 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 295 |
+
>>> from sympy import symbols
|
| 296 |
+
>>> from sympy.physics.mechanics import Body, Point, dynamicsymbols
|
| 297 |
+
>>> m, g = symbols('m g')
|
| 298 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 299 |
+
... B = Body('B')
|
| 300 |
+
>>> force1 = m*g*B.z
|
| 301 |
+
>>> B.apply_force(force1) #Applying force on B's masscenter
|
| 302 |
+
>>> B.loads
|
| 303 |
+
[(B_masscenter, g*m*B_frame.z)]
|
| 304 |
+
|
| 305 |
+
We can also remove some part of force from any point on the body by
|
| 306 |
+
adding the opposite force to the body on that point.
|
| 307 |
+
|
| 308 |
+
>>> f1, f2 = dynamicsymbols('f1 f2')
|
| 309 |
+
>>> P = Point('P') #Considering point P on body B
|
| 310 |
+
>>> B.apply_force(f1*B.x + f2*B.y, P)
|
| 311 |
+
>>> B.loads
|
| 312 |
+
[(B_masscenter, g*m*B_frame.z), (P, f1(t)*B_frame.x + f2(t)*B_frame.y)]
|
| 313 |
+
|
| 314 |
+
Let's remove f1 from point P on body B.
|
| 315 |
+
|
| 316 |
+
>>> B.apply_force(-f1*B.x, P)
|
| 317 |
+
>>> B.loads
|
| 318 |
+
[(B_masscenter, g*m*B_frame.z), (P, f2(t)*B_frame.y)]
|
| 319 |
+
|
| 320 |
+
To further demonstrate the use of ``apply_force`` attribute,
|
| 321 |
+
consider two bodies connected through a spring.
|
| 322 |
+
|
| 323 |
+
>>> from sympy.physics.mechanics import Body, dynamicsymbols
|
| 324 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 325 |
+
... N = Body('N') #Newtonion Frame
|
| 326 |
+
>>> x = dynamicsymbols('x')
|
| 327 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 328 |
+
... B1 = Body('B1')
|
| 329 |
+
... B2 = Body('B2')
|
| 330 |
+
>>> spring_force = x*N.x
|
| 331 |
+
|
| 332 |
+
Now let's apply equal and opposite spring force to the bodies.
|
| 333 |
+
|
| 334 |
+
>>> P1 = Point('P1')
|
| 335 |
+
>>> P2 = Point('P2')
|
| 336 |
+
>>> B1.apply_force(spring_force, point=P1, reaction_body=B2, reaction_point=P2)
|
| 337 |
+
|
| 338 |
+
We can check the loads(forces) applied to bodies now.
|
| 339 |
+
|
| 340 |
+
>>> B1.loads
|
| 341 |
+
[(P1, x(t)*N_frame.x)]
|
| 342 |
+
>>> B2.loads
|
| 343 |
+
[(P2, - x(t)*N_frame.x)]
|
| 344 |
+
|
| 345 |
+
Notes
|
| 346 |
+
=====
|
| 347 |
+
|
| 348 |
+
If a new force is applied to a body on a point which already has some
|
| 349 |
+
force applied on it, then the new force is added to the already applied
|
| 350 |
+
force on that point.
|
| 351 |
+
|
| 352 |
+
"""
|
| 353 |
+
|
| 354 |
+
if not isinstance(point, Point):
|
| 355 |
+
if point is None:
|
| 356 |
+
point = self.masscenter # masscenter
|
| 357 |
+
else:
|
| 358 |
+
raise TypeError("Force must be applied to a point on the body.")
|
| 359 |
+
if not isinstance(force, Vector):
|
| 360 |
+
raise TypeError("Force must be a vector.")
|
| 361 |
+
|
| 362 |
+
if reaction_body is not None:
|
| 363 |
+
reaction_body.apply_force(-force, point=reaction_point)
|
| 364 |
+
|
| 365 |
+
for load in self._loads:
|
| 366 |
+
if point in load:
|
| 367 |
+
force += load[1]
|
| 368 |
+
self._loads.remove(load)
|
| 369 |
+
break
|
| 370 |
+
|
| 371 |
+
self._loads.append((point, force))
|
| 372 |
+
|
| 373 |
+
def apply_torque(self, torque, reaction_body=None):
|
| 374 |
+
"""Add torque to the body(s).
|
| 375 |
+
|
| 376 |
+
Explanation
|
| 377 |
+
===========
|
| 378 |
+
|
| 379 |
+
Applies the torque on self or equal and opposite torques on
|
| 380 |
+
self and other body if both are given.
|
| 381 |
+
The torque applied on other body is taken opposite of self,
|
| 382 |
+
i.e, -torque.
|
| 383 |
+
|
| 384 |
+
Parameters
|
| 385 |
+
==========
|
| 386 |
+
|
| 387 |
+
torque: Vector
|
| 388 |
+
The torque to be applied.
|
| 389 |
+
reaction_body: Body, optional
|
| 390 |
+
Second body on which equal and opposite torque
|
| 391 |
+
is to be applied.
|
| 392 |
+
|
| 393 |
+
Example
|
| 394 |
+
=======
|
| 395 |
+
|
| 396 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 397 |
+
purposes only. The functionality of Body is fully captured by
|
| 398 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 399 |
+
warning we can use the ignore_warnings context manager.
|
| 400 |
+
|
| 401 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 402 |
+
>>> from sympy import symbols
|
| 403 |
+
>>> from sympy.physics.mechanics import Body, dynamicsymbols
|
| 404 |
+
>>> t = symbols('t')
|
| 405 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 406 |
+
... B = Body('B')
|
| 407 |
+
>>> torque1 = t*B.z
|
| 408 |
+
>>> B.apply_torque(torque1)
|
| 409 |
+
>>> B.loads
|
| 410 |
+
[(B_frame, t*B_frame.z)]
|
| 411 |
+
|
| 412 |
+
We can also remove some part of torque from the body by
|
| 413 |
+
adding the opposite torque to the body.
|
| 414 |
+
|
| 415 |
+
>>> t1, t2 = dynamicsymbols('t1 t2')
|
| 416 |
+
>>> B.apply_torque(t1*B.x + t2*B.y)
|
| 417 |
+
>>> B.loads
|
| 418 |
+
[(B_frame, t1(t)*B_frame.x + t2(t)*B_frame.y + t*B_frame.z)]
|
| 419 |
+
|
| 420 |
+
Let's remove t1 from Body B.
|
| 421 |
+
|
| 422 |
+
>>> B.apply_torque(-t1*B.x)
|
| 423 |
+
>>> B.loads
|
| 424 |
+
[(B_frame, t2(t)*B_frame.y + t*B_frame.z)]
|
| 425 |
+
|
| 426 |
+
To further demonstrate the use, let us consider two bodies such that
|
| 427 |
+
a torque `T` is acting on one body, and `-T` on the other.
|
| 428 |
+
|
| 429 |
+
>>> from sympy.physics.mechanics import Body, dynamicsymbols
|
| 430 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 431 |
+
... N = Body('N') #Newtonion frame
|
| 432 |
+
... B1 = Body('B1')
|
| 433 |
+
... B2 = Body('B2')
|
| 434 |
+
>>> v = dynamicsymbols('v')
|
| 435 |
+
>>> T = v*N.y #Torque
|
| 436 |
+
|
| 437 |
+
Now let's apply equal and opposite torque to the bodies.
|
| 438 |
+
|
| 439 |
+
>>> B1.apply_torque(T, B2)
|
| 440 |
+
|
| 441 |
+
We can check the loads (torques) applied to bodies now.
|
| 442 |
+
|
| 443 |
+
>>> B1.loads
|
| 444 |
+
[(B1_frame, v(t)*N_frame.y)]
|
| 445 |
+
>>> B2.loads
|
| 446 |
+
[(B2_frame, - v(t)*N_frame.y)]
|
| 447 |
+
|
| 448 |
+
Notes
|
| 449 |
+
=====
|
| 450 |
+
|
| 451 |
+
If a new torque is applied on body which already has some torque applied on it,
|
| 452 |
+
then the new torque is added to the previous torque about the body's frame.
|
| 453 |
+
|
| 454 |
+
"""
|
| 455 |
+
|
| 456 |
+
if not isinstance(torque, Vector):
|
| 457 |
+
raise TypeError("A Vector must be supplied to add torque.")
|
| 458 |
+
|
| 459 |
+
if reaction_body is not None:
|
| 460 |
+
reaction_body.apply_torque(-torque)
|
| 461 |
+
|
| 462 |
+
for load in self._loads:
|
| 463 |
+
if self.frame in load:
|
| 464 |
+
torque += load[1]
|
| 465 |
+
self._loads.remove(load)
|
| 466 |
+
break
|
| 467 |
+
self._loads.append((self.frame, torque))
|
| 468 |
+
|
| 469 |
+
def clear_loads(self):
|
| 470 |
+
"""
|
| 471 |
+
Clears the Body's loads list.
|
| 472 |
+
|
| 473 |
+
Example
|
| 474 |
+
=======
|
| 475 |
+
|
| 476 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 477 |
+
purposes only. The functionality of Body is fully captured by
|
| 478 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 479 |
+
warning we can use the ignore_warnings context manager.
|
| 480 |
+
|
| 481 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 482 |
+
>>> from sympy.physics.mechanics import Body
|
| 483 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 484 |
+
... B = Body('B')
|
| 485 |
+
>>> force = B.x + B.y
|
| 486 |
+
>>> B.apply_force(force)
|
| 487 |
+
>>> B.loads
|
| 488 |
+
[(B_masscenter, B_frame.x + B_frame.y)]
|
| 489 |
+
>>> B.clear_loads()
|
| 490 |
+
>>> B.loads
|
| 491 |
+
[]
|
| 492 |
+
|
| 493 |
+
"""
|
| 494 |
+
|
| 495 |
+
self._loads = []
|
| 496 |
+
|
| 497 |
+
def remove_load(self, about=None):
|
| 498 |
+
"""
|
| 499 |
+
Remove load about a point or frame.
|
| 500 |
+
|
| 501 |
+
Parameters
|
| 502 |
+
==========
|
| 503 |
+
|
| 504 |
+
about : Point or ReferenceFrame, optional
|
| 505 |
+
The point about which force is applied,
|
| 506 |
+
and is to be removed.
|
| 507 |
+
If about is None, then the torque about
|
| 508 |
+
self's frame is removed.
|
| 509 |
+
|
| 510 |
+
Example
|
| 511 |
+
=======
|
| 512 |
+
|
| 513 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 514 |
+
purposes only. The functionality of Body is fully captured by
|
| 515 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 516 |
+
warning we can use the ignore_warnings context manager.
|
| 517 |
+
|
| 518 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 519 |
+
>>> from sympy.physics.mechanics import Body, Point
|
| 520 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 521 |
+
... B = Body('B')
|
| 522 |
+
>>> P = Point('P')
|
| 523 |
+
>>> f1 = B.x
|
| 524 |
+
>>> f2 = B.y
|
| 525 |
+
>>> B.apply_force(f1)
|
| 526 |
+
>>> B.apply_force(f2, P)
|
| 527 |
+
>>> B.loads
|
| 528 |
+
[(B_masscenter, B_frame.x), (P, B_frame.y)]
|
| 529 |
+
|
| 530 |
+
>>> B.remove_load(P)
|
| 531 |
+
>>> B.loads
|
| 532 |
+
[(B_masscenter, B_frame.x)]
|
| 533 |
+
|
| 534 |
+
"""
|
| 535 |
+
|
| 536 |
+
if about is not None:
|
| 537 |
+
if not isinstance(about, Point):
|
| 538 |
+
raise TypeError('Load is applied about Point or ReferenceFrame.')
|
| 539 |
+
else:
|
| 540 |
+
about = self.frame
|
| 541 |
+
|
| 542 |
+
for load in self._loads:
|
| 543 |
+
if about in load:
|
| 544 |
+
self._loads.remove(load)
|
| 545 |
+
break
|
| 546 |
+
|
| 547 |
+
def masscenter_vel(self, body):
|
| 548 |
+
"""
|
| 549 |
+
Returns the velocity of the mass center with respect to the provided
|
| 550 |
+
rigid body or reference frame.
|
| 551 |
+
|
| 552 |
+
Parameters
|
| 553 |
+
==========
|
| 554 |
+
|
| 555 |
+
body: Body or ReferenceFrame
|
| 556 |
+
The rigid body or reference frame to calculate the velocity in.
|
| 557 |
+
|
| 558 |
+
Example
|
| 559 |
+
=======
|
| 560 |
+
|
| 561 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 562 |
+
purposes only. The functionality of Body is fully captured by
|
| 563 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 564 |
+
warning we can use the ignore_warnings context manager.
|
| 565 |
+
|
| 566 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 567 |
+
>>> from sympy.physics.mechanics import Body
|
| 568 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 569 |
+
... A = Body('A')
|
| 570 |
+
... B = Body('B')
|
| 571 |
+
>>> A.masscenter.set_vel(B.frame, 5*B.frame.x)
|
| 572 |
+
>>> A.masscenter_vel(B)
|
| 573 |
+
5*B_frame.x
|
| 574 |
+
>>> A.masscenter_vel(B.frame)
|
| 575 |
+
5*B_frame.x
|
| 576 |
+
|
| 577 |
+
"""
|
| 578 |
+
|
| 579 |
+
if isinstance(body, ReferenceFrame):
|
| 580 |
+
frame=body
|
| 581 |
+
elif isinstance(body, Body):
|
| 582 |
+
frame = body.frame
|
| 583 |
+
return self.masscenter.vel(frame)
|
| 584 |
+
|
| 585 |
+
def ang_vel_in(self, body):
|
| 586 |
+
"""
|
| 587 |
+
Returns this body's angular velocity with respect to the provided
|
| 588 |
+
rigid body or reference frame.
|
| 589 |
+
|
| 590 |
+
Parameters
|
| 591 |
+
==========
|
| 592 |
+
|
| 593 |
+
body: Body or ReferenceFrame
|
| 594 |
+
The rigid body or reference frame to calculate the angular velocity in.
|
| 595 |
+
|
| 596 |
+
Example
|
| 597 |
+
=======
|
| 598 |
+
|
| 599 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 600 |
+
purposes only. The functionality of Body is fully captured by
|
| 601 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 602 |
+
warning we can use the ignore_warnings context manager.
|
| 603 |
+
|
| 604 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 605 |
+
>>> from sympy.physics.mechanics import Body, ReferenceFrame
|
| 606 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 607 |
+
... A = Body('A')
|
| 608 |
+
>>> N = ReferenceFrame('N')
|
| 609 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 610 |
+
... B = Body('B', frame=N)
|
| 611 |
+
>>> A.frame.set_ang_vel(N, 5*N.x)
|
| 612 |
+
>>> A.ang_vel_in(B)
|
| 613 |
+
5*N.x
|
| 614 |
+
>>> A.ang_vel_in(N)
|
| 615 |
+
5*N.x
|
| 616 |
+
|
| 617 |
+
"""
|
| 618 |
+
|
| 619 |
+
if isinstance(body, ReferenceFrame):
|
| 620 |
+
frame=body
|
| 621 |
+
elif isinstance(body, Body):
|
| 622 |
+
frame = body.frame
|
| 623 |
+
return self.frame.ang_vel_in(frame)
|
| 624 |
+
|
| 625 |
+
def dcm(self, body):
|
| 626 |
+
"""
|
| 627 |
+
Returns the direction cosine matrix of this body relative to the
|
| 628 |
+
provided rigid body or reference frame.
|
| 629 |
+
|
| 630 |
+
Parameters
|
| 631 |
+
==========
|
| 632 |
+
|
| 633 |
+
body: Body or ReferenceFrame
|
| 634 |
+
The rigid body or reference frame to calculate the dcm.
|
| 635 |
+
|
| 636 |
+
Example
|
| 637 |
+
=======
|
| 638 |
+
|
| 639 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 640 |
+
purposes only. The functionality of Body is fully captured by
|
| 641 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 642 |
+
warning we can use the ignore_warnings context manager.
|
| 643 |
+
|
| 644 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 645 |
+
>>> from sympy.physics.mechanics import Body
|
| 646 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 647 |
+
... A = Body('A')
|
| 648 |
+
... B = Body('B')
|
| 649 |
+
>>> A.frame.orient_axis(B.frame, B.frame.x, 5)
|
| 650 |
+
>>> A.dcm(B)
|
| 651 |
+
Matrix([
|
| 652 |
+
[1, 0, 0],
|
| 653 |
+
[0, cos(5), sin(5)],
|
| 654 |
+
[0, -sin(5), cos(5)]])
|
| 655 |
+
>>> A.dcm(B.frame)
|
| 656 |
+
Matrix([
|
| 657 |
+
[1, 0, 0],
|
| 658 |
+
[0, cos(5), sin(5)],
|
| 659 |
+
[0, -sin(5), cos(5)]])
|
| 660 |
+
|
| 661 |
+
"""
|
| 662 |
+
|
| 663 |
+
if isinstance(body, ReferenceFrame):
|
| 664 |
+
frame=body
|
| 665 |
+
elif isinstance(body, Body):
|
| 666 |
+
frame = body.frame
|
| 667 |
+
return self.frame.dcm(frame)
|
| 668 |
+
|
| 669 |
+
def parallel_axis(self, point, frame=None):
|
| 670 |
+
"""Returns the inertia dyadic of the body with respect to another
|
| 671 |
+
point.
|
| 672 |
+
|
| 673 |
+
Parameters
|
| 674 |
+
==========
|
| 675 |
+
|
| 676 |
+
point : sympy.physics.vector.Point
|
| 677 |
+
The point to express the inertia dyadic about.
|
| 678 |
+
frame : sympy.physics.vector.ReferenceFrame
|
| 679 |
+
The reference frame used to construct the dyadic.
|
| 680 |
+
|
| 681 |
+
Returns
|
| 682 |
+
=======
|
| 683 |
+
|
| 684 |
+
inertia : sympy.physics.vector.Dyadic
|
| 685 |
+
The inertia dyadic of the rigid body expressed about the provided
|
| 686 |
+
point.
|
| 687 |
+
|
| 688 |
+
Example
|
| 689 |
+
=======
|
| 690 |
+
|
| 691 |
+
As Body has been deprecated, the following examples are for illustrative
|
| 692 |
+
purposes only. The functionality of Body is fully captured by
|
| 693 |
+
:class:`~.RigidBody` and :class:`~.Particle`. To ignore the deprecation
|
| 694 |
+
warning we can use the ignore_warnings context manager.
|
| 695 |
+
|
| 696 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 697 |
+
>>> from sympy.physics.mechanics import Body
|
| 698 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 699 |
+
... A = Body('A')
|
| 700 |
+
>>> P = A.masscenter.locatenew('point', 3 * A.x + 5 * A.y)
|
| 701 |
+
>>> A.parallel_axis(P).to_matrix(A.frame)
|
| 702 |
+
Matrix([
|
| 703 |
+
[A_ixx + 25*A_mass, A_ixy - 15*A_mass, A_izx],
|
| 704 |
+
[A_ixy - 15*A_mass, A_iyy + 9*A_mass, A_iyz],
|
| 705 |
+
[ A_izx, A_iyz, A_izz + 34*A_mass]])
|
| 706 |
+
|
| 707 |
+
"""
|
| 708 |
+
if self.is_rigidbody:
|
| 709 |
+
return RigidBody.parallel_axis(self, point, frame)
|
| 710 |
+
return Particle.parallel_axis(self, point, frame)
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/body_base.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import ABC, abstractmethod
|
| 2 |
+
from sympy import Symbol, sympify
|
| 3 |
+
from sympy.physics.vector import Point
|
| 4 |
+
|
| 5 |
+
__all__ = ['BodyBase']
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class BodyBase(ABC):
|
| 9 |
+
"""Abstract class for body type objects."""
|
| 10 |
+
def __init__(self, name, masscenter=None, mass=None):
|
| 11 |
+
# Note: If frame=None, no auto-generated frame is created, because a
|
| 12 |
+
# Particle does not need to have a frame by default.
|
| 13 |
+
if not isinstance(name, str):
|
| 14 |
+
raise TypeError('Supply a valid name.')
|
| 15 |
+
self._name = name
|
| 16 |
+
if mass is None:
|
| 17 |
+
mass = Symbol(f'{name}_mass')
|
| 18 |
+
if masscenter is None:
|
| 19 |
+
masscenter = Point(f'{name}_masscenter')
|
| 20 |
+
self.mass = mass
|
| 21 |
+
self.masscenter = masscenter
|
| 22 |
+
self.potential_energy = 0
|
| 23 |
+
self.points = []
|
| 24 |
+
|
| 25 |
+
def __str__(self):
|
| 26 |
+
return self.name
|
| 27 |
+
|
| 28 |
+
def __repr__(self):
|
| 29 |
+
return (f'{self.__class__.__name__}({repr(self.name)}, masscenter='
|
| 30 |
+
f'{repr(self.masscenter)}, mass={repr(self.mass)})')
|
| 31 |
+
|
| 32 |
+
@property
|
| 33 |
+
def name(self):
|
| 34 |
+
"""The name of the body."""
|
| 35 |
+
return self._name
|
| 36 |
+
|
| 37 |
+
@property
|
| 38 |
+
def masscenter(self):
|
| 39 |
+
"""The body's center of mass."""
|
| 40 |
+
return self._masscenter
|
| 41 |
+
|
| 42 |
+
@masscenter.setter
|
| 43 |
+
def masscenter(self, point):
|
| 44 |
+
if not isinstance(point, Point):
|
| 45 |
+
raise TypeError("The body's center of mass must be a Point object.")
|
| 46 |
+
self._masscenter = point
|
| 47 |
+
|
| 48 |
+
@property
|
| 49 |
+
def mass(self):
|
| 50 |
+
"""The body's mass."""
|
| 51 |
+
return self._mass
|
| 52 |
+
|
| 53 |
+
@mass.setter
|
| 54 |
+
def mass(self, mass):
|
| 55 |
+
self._mass = sympify(mass)
|
| 56 |
+
|
| 57 |
+
@property
|
| 58 |
+
def potential_energy(self):
|
| 59 |
+
"""The potential energy of the body.
|
| 60 |
+
|
| 61 |
+
Examples
|
| 62 |
+
========
|
| 63 |
+
|
| 64 |
+
>>> from sympy.physics.mechanics import Particle, Point
|
| 65 |
+
>>> from sympy import symbols
|
| 66 |
+
>>> m, g, h = symbols('m g h')
|
| 67 |
+
>>> O = Point('O')
|
| 68 |
+
>>> P = Particle('P', O, m)
|
| 69 |
+
>>> P.potential_energy = m * g * h
|
| 70 |
+
>>> P.potential_energy
|
| 71 |
+
g*h*m
|
| 72 |
+
|
| 73 |
+
"""
|
| 74 |
+
return self._potential_energy
|
| 75 |
+
|
| 76 |
+
@potential_energy.setter
|
| 77 |
+
def potential_energy(self, scalar):
|
| 78 |
+
self._potential_energy = sympify(scalar)
|
| 79 |
+
|
| 80 |
+
@abstractmethod
|
| 81 |
+
def kinetic_energy(self, frame):
|
| 82 |
+
pass
|
| 83 |
+
|
| 84 |
+
@abstractmethod
|
| 85 |
+
def linear_momentum(self, frame):
|
| 86 |
+
pass
|
| 87 |
+
|
| 88 |
+
@abstractmethod
|
| 89 |
+
def angular_momentum(self, point, frame):
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
@abstractmethod
|
| 93 |
+
def parallel_axis(self, point, frame):
|
| 94 |
+
pass
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/functions.py
ADDED
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.utilities import dict_merge
|
| 2 |
+
from sympy.utilities.iterables import iterable
|
| 3 |
+
from sympy.physics.vector import (Dyadic, Vector, ReferenceFrame,
|
| 4 |
+
Point, dynamicsymbols)
|
| 5 |
+
from sympy.physics.vector.printing import (vprint, vsprint, vpprint, vlatex,
|
| 6 |
+
init_vprinting)
|
| 7 |
+
from sympy.physics.mechanics.particle import Particle
|
| 8 |
+
from sympy.physics.mechanics.rigidbody import RigidBody
|
| 9 |
+
from sympy.simplify.simplify import simplify
|
| 10 |
+
from sympy import Matrix, Mul, Derivative, sin, cos, tan, S
|
| 11 |
+
from sympy.core.function import AppliedUndef
|
| 12 |
+
from sympy.physics.mechanics.inertia import (inertia as _inertia,
|
| 13 |
+
inertia_of_point_mass as _inertia_of_point_mass)
|
| 14 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 15 |
+
|
| 16 |
+
__all__ = ['linear_momentum',
|
| 17 |
+
'angular_momentum',
|
| 18 |
+
'kinetic_energy',
|
| 19 |
+
'potential_energy',
|
| 20 |
+
'Lagrangian',
|
| 21 |
+
'mechanics_printing',
|
| 22 |
+
'mprint',
|
| 23 |
+
'msprint',
|
| 24 |
+
'mpprint',
|
| 25 |
+
'mlatex',
|
| 26 |
+
'msubs',
|
| 27 |
+
'find_dynamicsymbols']
|
| 28 |
+
|
| 29 |
+
# These are functions that we've moved and renamed during extracting the
|
| 30 |
+
# basic vector calculus code from the mechanics packages.
|
| 31 |
+
|
| 32 |
+
mprint = vprint
|
| 33 |
+
msprint = vsprint
|
| 34 |
+
mpprint = vpprint
|
| 35 |
+
mlatex = vlatex
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def mechanics_printing(**kwargs):
|
| 39 |
+
"""
|
| 40 |
+
Initializes time derivative printing for all SymPy objects in
|
| 41 |
+
mechanics module.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
init_vprinting(**kwargs)
|
| 45 |
+
|
| 46 |
+
mechanics_printing.__doc__ = init_vprinting.__doc__
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def inertia(frame, ixx, iyy, izz, ixy=0, iyz=0, izx=0):
|
| 50 |
+
sympy_deprecation_warning(
|
| 51 |
+
"""
|
| 52 |
+
The inertia function has been moved.
|
| 53 |
+
Import it from "sympy.physics.mechanics".
|
| 54 |
+
""",
|
| 55 |
+
deprecated_since_version="1.13",
|
| 56 |
+
active_deprecations_target="moved-mechanics-functions"
|
| 57 |
+
)
|
| 58 |
+
return _inertia(frame, ixx, iyy, izz, ixy, iyz, izx)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def inertia_of_point_mass(mass, pos_vec, frame):
|
| 62 |
+
sympy_deprecation_warning(
|
| 63 |
+
"""
|
| 64 |
+
The inertia_of_point_mass function has been moved.
|
| 65 |
+
Import it from "sympy.physics.mechanics".
|
| 66 |
+
""",
|
| 67 |
+
deprecated_since_version="1.13",
|
| 68 |
+
active_deprecations_target="moved-mechanics-functions"
|
| 69 |
+
)
|
| 70 |
+
return _inertia_of_point_mass(mass, pos_vec, frame)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def linear_momentum(frame, *body):
|
| 74 |
+
"""Linear momentum of the system.
|
| 75 |
+
|
| 76 |
+
Explanation
|
| 77 |
+
===========
|
| 78 |
+
|
| 79 |
+
This function returns the linear momentum of a system of Particle's and/or
|
| 80 |
+
RigidBody's. The linear momentum of a system is equal to the vector sum of
|
| 81 |
+
the linear momentum of its constituents. Consider a system, S, comprised of
|
| 82 |
+
a rigid body, A, and a particle, P. The linear momentum of the system, L,
|
| 83 |
+
is equal to the vector sum of the linear momentum of the particle, L1, and
|
| 84 |
+
the linear momentum of the rigid body, L2, i.e.
|
| 85 |
+
|
| 86 |
+
L = L1 + L2
|
| 87 |
+
|
| 88 |
+
Parameters
|
| 89 |
+
==========
|
| 90 |
+
|
| 91 |
+
frame : ReferenceFrame
|
| 92 |
+
The frame in which linear momentum is desired.
|
| 93 |
+
body1, body2, body3... : Particle and/or RigidBody
|
| 94 |
+
The body (or bodies) whose linear momentum is required.
|
| 95 |
+
|
| 96 |
+
Examples
|
| 97 |
+
========
|
| 98 |
+
|
| 99 |
+
>>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
|
| 100 |
+
>>> from sympy.physics.mechanics import RigidBody, outer, linear_momentum
|
| 101 |
+
>>> N = ReferenceFrame('N')
|
| 102 |
+
>>> P = Point('P')
|
| 103 |
+
>>> P.set_vel(N, 10 * N.x)
|
| 104 |
+
>>> Pa = Particle('Pa', P, 1)
|
| 105 |
+
>>> Ac = Point('Ac')
|
| 106 |
+
>>> Ac.set_vel(N, 25 * N.y)
|
| 107 |
+
>>> I = outer(N.x, N.x)
|
| 108 |
+
>>> A = RigidBody('A', Ac, N, 20, (I, Ac))
|
| 109 |
+
>>> linear_momentum(N, A, Pa)
|
| 110 |
+
10*N.x + 500*N.y
|
| 111 |
+
|
| 112 |
+
"""
|
| 113 |
+
|
| 114 |
+
if not isinstance(frame, ReferenceFrame):
|
| 115 |
+
raise TypeError('Please specify a valid ReferenceFrame')
|
| 116 |
+
else:
|
| 117 |
+
linear_momentum_sys = Vector(0)
|
| 118 |
+
for e in body:
|
| 119 |
+
if isinstance(e, (RigidBody, Particle)):
|
| 120 |
+
linear_momentum_sys += e.linear_momentum(frame)
|
| 121 |
+
else:
|
| 122 |
+
raise TypeError('*body must have only Particle or RigidBody')
|
| 123 |
+
return linear_momentum_sys
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def angular_momentum(point, frame, *body):
|
| 127 |
+
"""Angular momentum of a system.
|
| 128 |
+
|
| 129 |
+
Explanation
|
| 130 |
+
===========
|
| 131 |
+
|
| 132 |
+
This function returns the angular momentum of a system of Particle's and/or
|
| 133 |
+
RigidBody's. The angular momentum of such a system is equal to the vector
|
| 134 |
+
sum of the angular momentum of its constituents. Consider a system, S,
|
| 135 |
+
comprised of a rigid body, A, and a particle, P. The angular momentum of
|
| 136 |
+
the system, H, is equal to the vector sum of the angular momentum of the
|
| 137 |
+
particle, H1, and the angular momentum of the rigid body, H2, i.e.
|
| 138 |
+
|
| 139 |
+
H = H1 + H2
|
| 140 |
+
|
| 141 |
+
Parameters
|
| 142 |
+
==========
|
| 143 |
+
|
| 144 |
+
point : Point
|
| 145 |
+
The point about which angular momentum of the system is desired.
|
| 146 |
+
frame : ReferenceFrame
|
| 147 |
+
The frame in which angular momentum is desired.
|
| 148 |
+
body1, body2, body3... : Particle and/or RigidBody
|
| 149 |
+
The body (or bodies) whose angular momentum is required.
|
| 150 |
+
|
| 151 |
+
Examples
|
| 152 |
+
========
|
| 153 |
+
|
| 154 |
+
>>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
|
| 155 |
+
>>> from sympy.physics.mechanics import RigidBody, outer, angular_momentum
|
| 156 |
+
>>> N = ReferenceFrame('N')
|
| 157 |
+
>>> O = Point('O')
|
| 158 |
+
>>> O.set_vel(N, 0 * N.x)
|
| 159 |
+
>>> P = O.locatenew('P', 1 * N.x)
|
| 160 |
+
>>> P.set_vel(N, 10 * N.x)
|
| 161 |
+
>>> Pa = Particle('Pa', P, 1)
|
| 162 |
+
>>> Ac = O.locatenew('Ac', 2 * N.y)
|
| 163 |
+
>>> Ac.set_vel(N, 5 * N.y)
|
| 164 |
+
>>> a = ReferenceFrame('a')
|
| 165 |
+
>>> a.set_ang_vel(N, 10 * N.z)
|
| 166 |
+
>>> I = outer(N.z, N.z)
|
| 167 |
+
>>> A = RigidBody('A', Ac, a, 20, (I, Ac))
|
| 168 |
+
>>> angular_momentum(O, N, Pa, A)
|
| 169 |
+
10*N.z
|
| 170 |
+
|
| 171 |
+
"""
|
| 172 |
+
|
| 173 |
+
if not isinstance(frame, ReferenceFrame):
|
| 174 |
+
raise TypeError('Please enter a valid ReferenceFrame')
|
| 175 |
+
if not isinstance(point, Point):
|
| 176 |
+
raise TypeError('Please specify a valid Point')
|
| 177 |
+
else:
|
| 178 |
+
angular_momentum_sys = Vector(0)
|
| 179 |
+
for e in body:
|
| 180 |
+
if isinstance(e, (RigidBody, Particle)):
|
| 181 |
+
angular_momentum_sys += e.angular_momentum(point, frame)
|
| 182 |
+
else:
|
| 183 |
+
raise TypeError('*body must have only Particle or RigidBody')
|
| 184 |
+
return angular_momentum_sys
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def kinetic_energy(frame, *body):
|
| 188 |
+
"""Kinetic energy of a multibody system.
|
| 189 |
+
|
| 190 |
+
Explanation
|
| 191 |
+
===========
|
| 192 |
+
|
| 193 |
+
This function returns the kinetic energy of a system of Particle's and/or
|
| 194 |
+
RigidBody's. The kinetic energy of such a system is equal to the sum of
|
| 195 |
+
the kinetic energies of its constituents. Consider a system, S, comprising
|
| 196 |
+
a rigid body, A, and a particle, P. The kinetic energy of the system, T,
|
| 197 |
+
is equal to the vector sum of the kinetic energy of the particle, T1, and
|
| 198 |
+
the kinetic energy of the rigid body, T2, i.e.
|
| 199 |
+
|
| 200 |
+
T = T1 + T2
|
| 201 |
+
|
| 202 |
+
Kinetic energy is a scalar.
|
| 203 |
+
|
| 204 |
+
Parameters
|
| 205 |
+
==========
|
| 206 |
+
|
| 207 |
+
frame : ReferenceFrame
|
| 208 |
+
The frame in which the velocity or angular velocity of the body is
|
| 209 |
+
defined.
|
| 210 |
+
body1, body2, body3... : Particle and/or RigidBody
|
| 211 |
+
The body (or bodies) whose kinetic energy is required.
|
| 212 |
+
|
| 213 |
+
Examples
|
| 214 |
+
========
|
| 215 |
+
|
| 216 |
+
>>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
|
| 217 |
+
>>> from sympy.physics.mechanics import RigidBody, outer, kinetic_energy
|
| 218 |
+
>>> N = ReferenceFrame('N')
|
| 219 |
+
>>> O = Point('O')
|
| 220 |
+
>>> O.set_vel(N, 0 * N.x)
|
| 221 |
+
>>> P = O.locatenew('P', 1 * N.x)
|
| 222 |
+
>>> P.set_vel(N, 10 * N.x)
|
| 223 |
+
>>> Pa = Particle('Pa', P, 1)
|
| 224 |
+
>>> Ac = O.locatenew('Ac', 2 * N.y)
|
| 225 |
+
>>> Ac.set_vel(N, 5 * N.y)
|
| 226 |
+
>>> a = ReferenceFrame('a')
|
| 227 |
+
>>> a.set_ang_vel(N, 10 * N.z)
|
| 228 |
+
>>> I = outer(N.z, N.z)
|
| 229 |
+
>>> A = RigidBody('A', Ac, a, 20, (I, Ac))
|
| 230 |
+
>>> kinetic_energy(N, Pa, A)
|
| 231 |
+
350
|
| 232 |
+
|
| 233 |
+
"""
|
| 234 |
+
|
| 235 |
+
if not isinstance(frame, ReferenceFrame):
|
| 236 |
+
raise TypeError('Please enter a valid ReferenceFrame')
|
| 237 |
+
ke_sys = S.Zero
|
| 238 |
+
for e in body:
|
| 239 |
+
if isinstance(e, (RigidBody, Particle)):
|
| 240 |
+
ke_sys += e.kinetic_energy(frame)
|
| 241 |
+
else:
|
| 242 |
+
raise TypeError('*body must have only Particle or RigidBody')
|
| 243 |
+
return ke_sys
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
def potential_energy(*body):
|
| 247 |
+
"""Potential energy of a multibody system.
|
| 248 |
+
|
| 249 |
+
Explanation
|
| 250 |
+
===========
|
| 251 |
+
|
| 252 |
+
This function returns the potential energy of a system of Particle's and/or
|
| 253 |
+
RigidBody's. The potential energy of such a system is equal to the sum of
|
| 254 |
+
the potential energy of its constituents. Consider a system, S, comprising
|
| 255 |
+
a rigid body, A, and a particle, P. The potential energy of the system, V,
|
| 256 |
+
is equal to the vector sum of the potential energy of the particle, V1, and
|
| 257 |
+
the potential energy of the rigid body, V2, i.e.
|
| 258 |
+
|
| 259 |
+
V = V1 + V2
|
| 260 |
+
|
| 261 |
+
Potential energy is a scalar.
|
| 262 |
+
|
| 263 |
+
Parameters
|
| 264 |
+
==========
|
| 265 |
+
|
| 266 |
+
body1, body2, body3... : Particle and/or RigidBody
|
| 267 |
+
The body (or bodies) whose potential energy is required.
|
| 268 |
+
|
| 269 |
+
Examples
|
| 270 |
+
========
|
| 271 |
+
|
| 272 |
+
>>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
|
| 273 |
+
>>> from sympy.physics.mechanics import RigidBody, outer, potential_energy
|
| 274 |
+
>>> from sympy import symbols
|
| 275 |
+
>>> M, m, g, h = symbols('M m g h')
|
| 276 |
+
>>> N = ReferenceFrame('N')
|
| 277 |
+
>>> O = Point('O')
|
| 278 |
+
>>> O.set_vel(N, 0 * N.x)
|
| 279 |
+
>>> P = O.locatenew('P', 1 * N.x)
|
| 280 |
+
>>> Pa = Particle('Pa', P, m)
|
| 281 |
+
>>> Ac = O.locatenew('Ac', 2 * N.y)
|
| 282 |
+
>>> a = ReferenceFrame('a')
|
| 283 |
+
>>> I = outer(N.z, N.z)
|
| 284 |
+
>>> A = RigidBody('A', Ac, a, M, (I, Ac))
|
| 285 |
+
>>> Pa.potential_energy = m * g * h
|
| 286 |
+
>>> A.potential_energy = M * g * h
|
| 287 |
+
>>> potential_energy(Pa, A)
|
| 288 |
+
M*g*h + g*h*m
|
| 289 |
+
|
| 290 |
+
"""
|
| 291 |
+
|
| 292 |
+
pe_sys = S.Zero
|
| 293 |
+
for e in body:
|
| 294 |
+
if isinstance(e, (RigidBody, Particle)):
|
| 295 |
+
pe_sys += e.potential_energy
|
| 296 |
+
else:
|
| 297 |
+
raise TypeError('*body must have only Particle or RigidBody')
|
| 298 |
+
return pe_sys
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def gravity(acceleration, *bodies):
|
| 302 |
+
from sympy.physics.mechanics.loads import gravity as _gravity
|
| 303 |
+
sympy_deprecation_warning(
|
| 304 |
+
"""
|
| 305 |
+
The gravity function has been moved.
|
| 306 |
+
Import it from "sympy.physics.mechanics.loads".
|
| 307 |
+
""",
|
| 308 |
+
deprecated_since_version="1.13",
|
| 309 |
+
active_deprecations_target="moved-mechanics-functions"
|
| 310 |
+
)
|
| 311 |
+
return _gravity(acceleration, *bodies)
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
def center_of_mass(point, *bodies):
|
| 315 |
+
"""
|
| 316 |
+
Returns the position vector from the given point to the center of mass
|
| 317 |
+
of the given bodies(particles or rigidbodies).
|
| 318 |
+
|
| 319 |
+
Example
|
| 320 |
+
=======
|
| 321 |
+
|
| 322 |
+
>>> from sympy import symbols, S
|
| 323 |
+
>>> from sympy.physics.vector import Point
|
| 324 |
+
>>> from sympy.physics.mechanics import Particle, ReferenceFrame, RigidBody, outer
|
| 325 |
+
>>> from sympy.physics.mechanics.functions import center_of_mass
|
| 326 |
+
>>> a = ReferenceFrame('a')
|
| 327 |
+
>>> m = symbols('m', real=True)
|
| 328 |
+
>>> p1 = Particle('p1', Point('p1_pt'), S(1))
|
| 329 |
+
>>> p2 = Particle('p2', Point('p2_pt'), S(2))
|
| 330 |
+
>>> p3 = Particle('p3', Point('p3_pt'), S(3))
|
| 331 |
+
>>> p4 = Particle('p4', Point('p4_pt'), m)
|
| 332 |
+
>>> b_f = ReferenceFrame('b_f')
|
| 333 |
+
>>> b_cm = Point('b_cm')
|
| 334 |
+
>>> mb = symbols('mb')
|
| 335 |
+
>>> b = RigidBody('b', b_cm, b_f, mb, (outer(b_f.x, b_f.x), b_cm))
|
| 336 |
+
>>> p2.point.set_pos(p1.point, a.x)
|
| 337 |
+
>>> p3.point.set_pos(p1.point, a.x + a.y)
|
| 338 |
+
>>> p4.point.set_pos(p1.point, a.y)
|
| 339 |
+
>>> b.masscenter.set_pos(p1.point, a.y + a.z)
|
| 340 |
+
>>> point_o=Point('o')
|
| 341 |
+
>>> point_o.set_pos(p1.point, center_of_mass(p1.point, p1, p2, p3, p4, b))
|
| 342 |
+
>>> expr = 5/(m + mb + 6)*a.x + (m + mb + 3)/(m + mb + 6)*a.y + mb/(m + mb + 6)*a.z
|
| 343 |
+
>>> point_o.pos_from(p1.point)
|
| 344 |
+
5/(m + mb + 6)*a.x + (m + mb + 3)/(m + mb + 6)*a.y + mb/(m + mb + 6)*a.z
|
| 345 |
+
|
| 346 |
+
"""
|
| 347 |
+
if not bodies:
|
| 348 |
+
raise TypeError("No bodies(instances of Particle or Rigidbody) were passed.")
|
| 349 |
+
|
| 350 |
+
total_mass = 0
|
| 351 |
+
vec = Vector(0)
|
| 352 |
+
for i in bodies:
|
| 353 |
+
total_mass += i.mass
|
| 354 |
+
|
| 355 |
+
masscenter = getattr(i, 'masscenter', None)
|
| 356 |
+
if masscenter is None:
|
| 357 |
+
masscenter = i.point
|
| 358 |
+
vec += i.mass*masscenter.pos_from(point)
|
| 359 |
+
|
| 360 |
+
return vec/total_mass
|
| 361 |
+
|
| 362 |
+
|
| 363 |
+
def Lagrangian(frame, *body):
|
| 364 |
+
"""Lagrangian of a multibody system.
|
| 365 |
+
|
| 366 |
+
Explanation
|
| 367 |
+
===========
|
| 368 |
+
|
| 369 |
+
This function returns the Lagrangian of a system of Particle's and/or
|
| 370 |
+
RigidBody's. The Lagrangian of such a system is equal to the difference
|
| 371 |
+
between the kinetic energies and potential energies of its constituents. If
|
| 372 |
+
T and V are the kinetic and potential energies of a system then it's
|
| 373 |
+
Lagrangian, L, is defined as
|
| 374 |
+
|
| 375 |
+
L = T - V
|
| 376 |
+
|
| 377 |
+
The Lagrangian is a scalar.
|
| 378 |
+
|
| 379 |
+
Parameters
|
| 380 |
+
==========
|
| 381 |
+
|
| 382 |
+
frame : ReferenceFrame
|
| 383 |
+
The frame in which the velocity or angular velocity of the body is
|
| 384 |
+
defined to determine the kinetic energy.
|
| 385 |
+
|
| 386 |
+
body1, body2, body3... : Particle and/or RigidBody
|
| 387 |
+
The body (or bodies) whose Lagrangian is required.
|
| 388 |
+
|
| 389 |
+
Examples
|
| 390 |
+
========
|
| 391 |
+
|
| 392 |
+
>>> from sympy.physics.mechanics import Point, Particle, ReferenceFrame
|
| 393 |
+
>>> from sympy.physics.mechanics import RigidBody, outer, Lagrangian
|
| 394 |
+
>>> from sympy import symbols
|
| 395 |
+
>>> M, m, g, h = symbols('M m g h')
|
| 396 |
+
>>> N = ReferenceFrame('N')
|
| 397 |
+
>>> O = Point('O')
|
| 398 |
+
>>> O.set_vel(N, 0 * N.x)
|
| 399 |
+
>>> P = O.locatenew('P', 1 * N.x)
|
| 400 |
+
>>> P.set_vel(N, 10 * N.x)
|
| 401 |
+
>>> Pa = Particle('Pa', P, 1)
|
| 402 |
+
>>> Ac = O.locatenew('Ac', 2 * N.y)
|
| 403 |
+
>>> Ac.set_vel(N, 5 * N.y)
|
| 404 |
+
>>> a = ReferenceFrame('a')
|
| 405 |
+
>>> a.set_ang_vel(N, 10 * N.z)
|
| 406 |
+
>>> I = outer(N.z, N.z)
|
| 407 |
+
>>> A = RigidBody('A', Ac, a, 20, (I, Ac))
|
| 408 |
+
>>> Pa.potential_energy = m * g * h
|
| 409 |
+
>>> A.potential_energy = M * g * h
|
| 410 |
+
>>> Lagrangian(N, Pa, A)
|
| 411 |
+
-M*g*h - g*h*m + 350
|
| 412 |
+
|
| 413 |
+
"""
|
| 414 |
+
|
| 415 |
+
if not isinstance(frame, ReferenceFrame):
|
| 416 |
+
raise TypeError('Please supply a valid ReferenceFrame')
|
| 417 |
+
for e in body:
|
| 418 |
+
if not isinstance(e, (RigidBody, Particle)):
|
| 419 |
+
raise TypeError('*body must have only Particle or RigidBody')
|
| 420 |
+
return kinetic_energy(frame, *body) - potential_energy(*body)
|
| 421 |
+
|
| 422 |
+
|
| 423 |
+
def find_dynamicsymbols(expression, exclude=None, reference_frame=None):
|
| 424 |
+
"""Find all dynamicsymbols in expression.
|
| 425 |
+
|
| 426 |
+
Explanation
|
| 427 |
+
===========
|
| 428 |
+
|
| 429 |
+
If the optional ``exclude`` kwarg is used, only dynamicsymbols
|
| 430 |
+
not in the iterable ``exclude`` are returned.
|
| 431 |
+
If we intend to apply this function on a vector, the optional
|
| 432 |
+
``reference_frame`` is also used to inform about the corresponding frame
|
| 433 |
+
with respect to which the dynamic symbols of the given vector is to be
|
| 434 |
+
determined.
|
| 435 |
+
|
| 436 |
+
Parameters
|
| 437 |
+
==========
|
| 438 |
+
|
| 439 |
+
expression : SymPy expression
|
| 440 |
+
|
| 441 |
+
exclude : iterable of dynamicsymbols, optional
|
| 442 |
+
|
| 443 |
+
reference_frame : ReferenceFrame, optional
|
| 444 |
+
The frame with respect to which the dynamic symbols of the
|
| 445 |
+
given vector is to be determined.
|
| 446 |
+
|
| 447 |
+
Examples
|
| 448 |
+
========
|
| 449 |
+
|
| 450 |
+
>>> from sympy.physics.mechanics import dynamicsymbols, find_dynamicsymbols
|
| 451 |
+
>>> from sympy.physics.mechanics import ReferenceFrame
|
| 452 |
+
>>> x, y = dynamicsymbols('x, y')
|
| 453 |
+
>>> expr = x + x.diff()*y
|
| 454 |
+
>>> find_dynamicsymbols(expr)
|
| 455 |
+
{x(t), y(t), Derivative(x(t), t)}
|
| 456 |
+
>>> find_dynamicsymbols(expr, exclude=[x, y])
|
| 457 |
+
{Derivative(x(t), t)}
|
| 458 |
+
>>> a, b, c = dynamicsymbols('a, b, c')
|
| 459 |
+
>>> A = ReferenceFrame('A')
|
| 460 |
+
>>> v = a * A.x + b * A.y + c * A.z
|
| 461 |
+
>>> find_dynamicsymbols(v, reference_frame=A)
|
| 462 |
+
{a(t), b(t), c(t)}
|
| 463 |
+
|
| 464 |
+
"""
|
| 465 |
+
t_set = {dynamicsymbols._t}
|
| 466 |
+
if exclude:
|
| 467 |
+
if iterable(exclude):
|
| 468 |
+
exclude_set = set(exclude)
|
| 469 |
+
else:
|
| 470 |
+
raise TypeError("exclude kwarg must be iterable")
|
| 471 |
+
else:
|
| 472 |
+
exclude_set = set()
|
| 473 |
+
if isinstance(expression, Vector):
|
| 474 |
+
if reference_frame is None:
|
| 475 |
+
raise ValueError("You must provide reference_frame when passing a "
|
| 476 |
+
"vector expression, got %s." % reference_frame)
|
| 477 |
+
else:
|
| 478 |
+
expression = expression.to_matrix(reference_frame)
|
| 479 |
+
return {i for i in expression.atoms(AppliedUndef, Derivative) if
|
| 480 |
+
i.free_symbols == t_set} - exclude_set
|
| 481 |
+
|
| 482 |
+
|
| 483 |
+
def msubs(expr, *sub_dicts, smart=False, **kwargs):
|
| 484 |
+
"""A custom subs for use on expressions derived in physics.mechanics.
|
| 485 |
+
|
| 486 |
+
Traverses the expression tree once, performing the subs found in sub_dicts.
|
| 487 |
+
Terms inside ``Derivative`` expressions are ignored:
|
| 488 |
+
|
| 489 |
+
Examples
|
| 490 |
+
========
|
| 491 |
+
|
| 492 |
+
>>> from sympy.physics.mechanics import dynamicsymbols, msubs
|
| 493 |
+
>>> x = dynamicsymbols('x')
|
| 494 |
+
>>> msubs(x.diff() + x, {x: 1})
|
| 495 |
+
Derivative(x(t), t) + 1
|
| 496 |
+
|
| 497 |
+
Note that sub_dicts can be a single dictionary, or several dictionaries:
|
| 498 |
+
|
| 499 |
+
>>> x, y, z = dynamicsymbols('x, y, z')
|
| 500 |
+
>>> sub1 = {x: 1, y: 2}
|
| 501 |
+
>>> sub2 = {z: 3, x.diff(): 4}
|
| 502 |
+
>>> msubs(x.diff() + x + y + z, sub1, sub2)
|
| 503 |
+
10
|
| 504 |
+
|
| 505 |
+
If smart=True (default False), also checks for conditions that may result
|
| 506 |
+
in ``nan``, but if simplified would yield a valid expression. For example:
|
| 507 |
+
|
| 508 |
+
>>> from sympy import sin, tan
|
| 509 |
+
>>> (sin(x)/tan(x)).subs(x, 0)
|
| 510 |
+
nan
|
| 511 |
+
>>> msubs(sin(x)/tan(x), {x: 0}, smart=True)
|
| 512 |
+
1
|
| 513 |
+
|
| 514 |
+
It does this by first replacing all ``tan`` with ``sin/cos``. Then each
|
| 515 |
+
node is traversed. If the node is a fraction, subs is first evaluated on
|
| 516 |
+
the denominator. If this results in 0, simplification of the entire
|
| 517 |
+
fraction is attempted. Using this selective simplification, only
|
| 518 |
+
subexpressions that result in 1/0 are targeted, resulting in faster
|
| 519 |
+
performance.
|
| 520 |
+
|
| 521 |
+
"""
|
| 522 |
+
|
| 523 |
+
sub_dict = dict_merge(*sub_dicts)
|
| 524 |
+
if smart:
|
| 525 |
+
func = _smart_subs
|
| 526 |
+
elif hasattr(expr, 'msubs'):
|
| 527 |
+
return expr.msubs(sub_dict)
|
| 528 |
+
else:
|
| 529 |
+
func = lambda expr, sub_dict: _crawl(expr, _sub_func, sub_dict)
|
| 530 |
+
if isinstance(expr, (Matrix, Vector, Dyadic)):
|
| 531 |
+
return expr.applyfunc(lambda x: func(x, sub_dict))
|
| 532 |
+
else:
|
| 533 |
+
return func(expr, sub_dict)
|
| 534 |
+
|
| 535 |
+
|
| 536 |
+
def _crawl(expr, func, *args, **kwargs):
|
| 537 |
+
"""Crawl the expression tree, and apply func to every node."""
|
| 538 |
+
val = func(expr, *args, **kwargs)
|
| 539 |
+
if val is not None:
|
| 540 |
+
return val
|
| 541 |
+
new_args = (_crawl(arg, func, *args, **kwargs) for arg in expr.args)
|
| 542 |
+
return expr.func(*new_args)
|
| 543 |
+
|
| 544 |
+
|
| 545 |
+
def _sub_func(expr, sub_dict):
|
| 546 |
+
"""Perform direct matching substitution, ignoring derivatives."""
|
| 547 |
+
if expr in sub_dict:
|
| 548 |
+
return sub_dict[expr]
|
| 549 |
+
elif not expr.args or expr.is_Derivative:
|
| 550 |
+
return expr
|
| 551 |
+
|
| 552 |
+
|
| 553 |
+
def _tan_repl_func(expr):
|
| 554 |
+
"""Replace tan with sin/cos."""
|
| 555 |
+
if isinstance(expr, tan):
|
| 556 |
+
return sin(*expr.args) / cos(*expr.args)
|
| 557 |
+
elif not expr.args or expr.is_Derivative:
|
| 558 |
+
return expr
|
| 559 |
+
|
| 560 |
+
|
| 561 |
+
def _smart_subs(expr, sub_dict):
|
| 562 |
+
"""Performs subs, checking for conditions that may result in `nan` or
|
| 563 |
+
`oo`, and attempts to simplify them out.
|
| 564 |
+
|
| 565 |
+
The expression tree is traversed twice, and the following steps are
|
| 566 |
+
performed on each expression node:
|
| 567 |
+
- First traverse:
|
| 568 |
+
Replace all `tan` with `sin/cos`.
|
| 569 |
+
- Second traverse:
|
| 570 |
+
If node is a fraction, check if the denominator evaluates to 0.
|
| 571 |
+
If so, attempt to simplify it out. Then if node is in sub_dict,
|
| 572 |
+
sub in the corresponding value.
|
| 573 |
+
|
| 574 |
+
"""
|
| 575 |
+
expr = _crawl(expr, _tan_repl_func)
|
| 576 |
+
|
| 577 |
+
def _recurser(expr, sub_dict):
|
| 578 |
+
# Decompose the expression into num, den
|
| 579 |
+
num, den = _fraction_decomp(expr)
|
| 580 |
+
if den != 1:
|
| 581 |
+
# If there is a non trivial denominator, we need to handle it
|
| 582 |
+
denom_subbed = _recurser(den, sub_dict)
|
| 583 |
+
if denom_subbed.evalf() == 0:
|
| 584 |
+
# If denom is 0 after this, attempt to simplify the bad expr
|
| 585 |
+
expr = simplify(expr)
|
| 586 |
+
else:
|
| 587 |
+
# Expression won't result in nan, find numerator
|
| 588 |
+
num_subbed = _recurser(num, sub_dict)
|
| 589 |
+
return num_subbed / denom_subbed
|
| 590 |
+
# We have to crawl the tree manually, because `expr` may have been
|
| 591 |
+
# modified in the simplify step. First, perform subs as normal:
|
| 592 |
+
val = _sub_func(expr, sub_dict)
|
| 593 |
+
if val is not None:
|
| 594 |
+
return val
|
| 595 |
+
new_args = (_recurser(arg, sub_dict) for arg in expr.args)
|
| 596 |
+
return expr.func(*new_args)
|
| 597 |
+
return _recurser(expr, sub_dict)
|
| 598 |
+
|
| 599 |
+
|
| 600 |
+
def _fraction_decomp(expr):
|
| 601 |
+
"""Return num, den such that expr = num/den."""
|
| 602 |
+
if not isinstance(expr, Mul):
|
| 603 |
+
return expr, 1
|
| 604 |
+
num = []
|
| 605 |
+
den = []
|
| 606 |
+
for a in expr.args:
|
| 607 |
+
if a.is_Pow and a.args[1] < 0:
|
| 608 |
+
den.append(1 / a)
|
| 609 |
+
else:
|
| 610 |
+
num.append(a)
|
| 611 |
+
if not den:
|
| 612 |
+
return expr, 1
|
| 613 |
+
num = Mul(*num)
|
| 614 |
+
den = Mul(*den)
|
| 615 |
+
return num, den
|
| 616 |
+
|
| 617 |
+
|
| 618 |
+
def _f_list_parser(fl, ref_frame):
|
| 619 |
+
"""Parses the provided forcelist composed of items
|
| 620 |
+
of the form (obj, force).
|
| 621 |
+
Returns a tuple containing:
|
| 622 |
+
vel_list: The velocity (ang_vel for Frames, vel for Points) in
|
| 623 |
+
the provided reference frame.
|
| 624 |
+
f_list: The forces.
|
| 625 |
+
|
| 626 |
+
Used internally in the KanesMethod and LagrangesMethod classes.
|
| 627 |
+
|
| 628 |
+
"""
|
| 629 |
+
def flist_iter():
|
| 630 |
+
for pair in fl:
|
| 631 |
+
obj, force = pair
|
| 632 |
+
if isinstance(obj, ReferenceFrame):
|
| 633 |
+
yield obj.ang_vel_in(ref_frame), force
|
| 634 |
+
elif isinstance(obj, Point):
|
| 635 |
+
yield obj.vel(ref_frame), force
|
| 636 |
+
else:
|
| 637 |
+
raise TypeError('First entry in each forcelist pair must '
|
| 638 |
+
'be a point or frame.')
|
| 639 |
+
|
| 640 |
+
if not fl:
|
| 641 |
+
vel_list, f_list = (), ()
|
| 642 |
+
else:
|
| 643 |
+
unzip = lambda l: list(zip(*l)) if l[0] else [(), ()]
|
| 644 |
+
vel_list, f_list = unzip(list(flist_iter()))
|
| 645 |
+
return vel_list, f_list
|
| 646 |
+
|
| 647 |
+
|
| 648 |
+
def _validate_coordinates(coordinates=None, speeds=None, check_duplicates=True,
|
| 649 |
+
is_dynamicsymbols=True, u_auxiliary=None):
|
| 650 |
+
"""Validate the generalized coordinates and generalized speeds.
|
| 651 |
+
|
| 652 |
+
Parameters
|
| 653 |
+
==========
|
| 654 |
+
coordinates : iterable, optional
|
| 655 |
+
Generalized coordinates to be validated.
|
| 656 |
+
speeds : iterable, optional
|
| 657 |
+
Generalized speeds to be validated.
|
| 658 |
+
check_duplicates : bool, optional
|
| 659 |
+
Checks if there are duplicates in the generalized coordinates and
|
| 660 |
+
generalized speeds. If so it will raise a ValueError. The default is
|
| 661 |
+
True.
|
| 662 |
+
is_dynamicsymbols : iterable, optional
|
| 663 |
+
Checks if all the generalized coordinates and generalized speeds are
|
| 664 |
+
dynamicsymbols. If any is not a dynamicsymbol, a ValueError will be
|
| 665 |
+
raised. The default is True.
|
| 666 |
+
u_auxiliary : iterable, optional
|
| 667 |
+
Auxiliary generalized speeds to be validated.
|
| 668 |
+
|
| 669 |
+
"""
|
| 670 |
+
t_set = {dynamicsymbols._t}
|
| 671 |
+
# Convert input to iterables
|
| 672 |
+
if coordinates is None:
|
| 673 |
+
coordinates = []
|
| 674 |
+
elif not iterable(coordinates):
|
| 675 |
+
coordinates = [coordinates]
|
| 676 |
+
if speeds is None:
|
| 677 |
+
speeds = []
|
| 678 |
+
elif not iterable(speeds):
|
| 679 |
+
speeds = [speeds]
|
| 680 |
+
if u_auxiliary is None:
|
| 681 |
+
u_auxiliary = []
|
| 682 |
+
elif not iterable(u_auxiliary):
|
| 683 |
+
u_auxiliary = [u_auxiliary]
|
| 684 |
+
|
| 685 |
+
msgs = []
|
| 686 |
+
if check_duplicates: # Check for duplicates
|
| 687 |
+
seen = set()
|
| 688 |
+
coord_duplicates = {x for x in coordinates if x in seen or seen.add(x)}
|
| 689 |
+
seen = set()
|
| 690 |
+
speed_duplicates = {x for x in speeds if x in seen or seen.add(x)}
|
| 691 |
+
seen = set()
|
| 692 |
+
aux_duplicates = {x for x in u_auxiliary if x in seen or seen.add(x)}
|
| 693 |
+
overlap_coords = set(coordinates).intersection(speeds)
|
| 694 |
+
overlap_aux = set(coordinates).union(speeds).intersection(u_auxiliary)
|
| 695 |
+
if coord_duplicates:
|
| 696 |
+
msgs.append(f'The generalized coordinates {coord_duplicates} are '
|
| 697 |
+
f'duplicated, all generalized coordinates should be '
|
| 698 |
+
f'unique.')
|
| 699 |
+
if speed_duplicates:
|
| 700 |
+
msgs.append(f'The generalized speeds {speed_duplicates} are '
|
| 701 |
+
f'duplicated, all generalized speeds should be unique.')
|
| 702 |
+
if aux_duplicates:
|
| 703 |
+
msgs.append(f'The auxiliary speeds {aux_duplicates} are duplicated,'
|
| 704 |
+
f' all auxiliary speeds should be unique.')
|
| 705 |
+
if overlap_coords:
|
| 706 |
+
msgs.append(f'{overlap_coords} are defined as both generalized '
|
| 707 |
+
f'coordinates and generalized speeds.')
|
| 708 |
+
if overlap_aux:
|
| 709 |
+
msgs.append(f'The auxiliary speeds {overlap_aux} are also defined '
|
| 710 |
+
f'as generalized coordinates or generalized speeds.')
|
| 711 |
+
if is_dynamicsymbols: # Check whether all coordinates are dynamicsymbols
|
| 712 |
+
for coordinate in coordinates:
|
| 713 |
+
if not (isinstance(coordinate, (AppliedUndef, Derivative)) and
|
| 714 |
+
coordinate.free_symbols == t_set):
|
| 715 |
+
msgs.append(f'Generalized coordinate "{coordinate}" is not a '
|
| 716 |
+
f'dynamicsymbol.')
|
| 717 |
+
for speed in speeds:
|
| 718 |
+
if not (isinstance(speed, (AppliedUndef, Derivative)) and
|
| 719 |
+
speed.free_symbols == t_set):
|
| 720 |
+
msgs.append(
|
| 721 |
+
f'Generalized speed "{speed}" is not a dynamicsymbol.')
|
| 722 |
+
for aux in u_auxiliary:
|
| 723 |
+
if not (isinstance(aux, (AppliedUndef, Derivative)) and
|
| 724 |
+
aux.free_symbols == t_set):
|
| 725 |
+
msgs.append(
|
| 726 |
+
f'Auxiliary speed "{aux}" is not a dynamicsymbol.')
|
| 727 |
+
if msgs:
|
| 728 |
+
raise ValueError('\n'.join(msgs))
|
| 729 |
+
|
| 730 |
+
|
| 731 |
+
def _parse_linear_solver(linear_solver):
|
| 732 |
+
"""Helper function to retrieve a specified linear solver."""
|
| 733 |
+
if callable(linear_solver):
|
| 734 |
+
return linear_solver
|
| 735 |
+
return lambda A, b: Matrix.solve(A, b, method=linear_solver)
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/joint.py
ADDED
|
@@ -0,0 +1,2188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
|
| 3 |
+
from abc import ABC, abstractmethod
|
| 4 |
+
|
| 5 |
+
from sympy import pi, Derivative, Matrix
|
| 6 |
+
from sympy.core.function import AppliedUndef
|
| 7 |
+
from sympy.physics.mechanics.body_base import BodyBase
|
| 8 |
+
from sympy.physics.mechanics.functions import _validate_coordinates
|
| 9 |
+
from sympy.physics.vector import (Vector, dynamicsymbols, cross, Point,
|
| 10 |
+
ReferenceFrame)
|
| 11 |
+
from sympy.utilities.iterables import iterable
|
| 12 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 13 |
+
|
| 14 |
+
__all__ = ['Joint', 'PinJoint', 'PrismaticJoint', 'CylindricalJoint',
|
| 15 |
+
'PlanarJoint', 'SphericalJoint', 'WeldJoint']
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class Joint(ABC):
|
| 19 |
+
"""Abstract base class for all specific joints.
|
| 20 |
+
|
| 21 |
+
Explanation
|
| 22 |
+
===========
|
| 23 |
+
|
| 24 |
+
A joint subtracts degrees of freedom from a body. This is the base class
|
| 25 |
+
for all specific joints and holds all common methods acting as an interface
|
| 26 |
+
for all joints. Custom joint can be created by inheriting Joint class and
|
| 27 |
+
defining all abstract functions.
|
| 28 |
+
|
| 29 |
+
The abstract methods are:
|
| 30 |
+
|
| 31 |
+
- ``_generate_coordinates``
|
| 32 |
+
- ``_generate_speeds``
|
| 33 |
+
- ``_orient_frames``
|
| 34 |
+
- ``_set_angular_velocity``
|
| 35 |
+
- ``_set_linear_velocity``
|
| 36 |
+
|
| 37 |
+
Parameters
|
| 38 |
+
==========
|
| 39 |
+
|
| 40 |
+
name : string
|
| 41 |
+
A unique name for the joint.
|
| 42 |
+
parent : Particle or RigidBody or Body
|
| 43 |
+
The parent body of joint.
|
| 44 |
+
child : Particle or RigidBody or Body
|
| 45 |
+
The child body of joint.
|
| 46 |
+
coordinates : iterable of dynamicsymbols, optional
|
| 47 |
+
Generalized coordinates of the joint.
|
| 48 |
+
speeds : iterable of dynamicsymbols, optional
|
| 49 |
+
Generalized speeds of joint.
|
| 50 |
+
parent_point : Point or Vector, optional
|
| 51 |
+
Attachment point where the joint is fixed to the parent body. If a
|
| 52 |
+
vector is provided, then the attachment point is computed by adding the
|
| 53 |
+
vector to the body's mass center. The default value is the parent's mass
|
| 54 |
+
center.
|
| 55 |
+
child_point : Point or Vector, optional
|
| 56 |
+
Attachment point where the joint is fixed to the child body. If a
|
| 57 |
+
vector is provided, then the attachment point is computed by adding the
|
| 58 |
+
vector to the body's mass center. The default value is the child's mass
|
| 59 |
+
center.
|
| 60 |
+
parent_axis : Vector, optional
|
| 61 |
+
.. deprecated:: 1.12
|
| 62 |
+
Axis fixed in the parent body which aligns with an axis fixed in the
|
| 63 |
+
child body. The default is the x axis of parent's reference frame.
|
| 64 |
+
For more information on this deprecation, see
|
| 65 |
+
:ref:`deprecated-mechanics-joint-axis`.
|
| 66 |
+
child_axis : Vector, optional
|
| 67 |
+
.. deprecated:: 1.12
|
| 68 |
+
Axis fixed in the child body which aligns with an axis fixed in the
|
| 69 |
+
parent body. The default is the x axis of child's reference frame.
|
| 70 |
+
For more information on this deprecation, see
|
| 71 |
+
:ref:`deprecated-mechanics-joint-axis`.
|
| 72 |
+
parent_interframe : ReferenceFrame, optional
|
| 73 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 74 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 75 |
+
is created which aligns its X axis with the given vector. The default
|
| 76 |
+
value is the parent's own frame.
|
| 77 |
+
child_interframe : ReferenceFrame, optional
|
| 78 |
+
Intermediate frame of the child body with respect to which the joint
|
| 79 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 80 |
+
is created which aligns its X axis with the given vector. The default
|
| 81 |
+
value is the child's own frame.
|
| 82 |
+
parent_joint_pos : Point or Vector, optional
|
| 83 |
+
.. deprecated:: 1.12
|
| 84 |
+
This argument is replaced by parent_point and will be removed in a
|
| 85 |
+
future version.
|
| 86 |
+
See :ref:`deprecated-mechanics-joint-pos` for more information.
|
| 87 |
+
child_joint_pos : Point or Vector, optional
|
| 88 |
+
.. deprecated:: 1.12
|
| 89 |
+
This argument is replaced by child_point and will be removed in a
|
| 90 |
+
future version.
|
| 91 |
+
See :ref:`deprecated-mechanics-joint-pos` for more information.
|
| 92 |
+
|
| 93 |
+
Attributes
|
| 94 |
+
==========
|
| 95 |
+
|
| 96 |
+
name : string
|
| 97 |
+
The joint's name.
|
| 98 |
+
parent : Particle or RigidBody or Body
|
| 99 |
+
The joint's parent body.
|
| 100 |
+
child : Particle or RigidBody or Body
|
| 101 |
+
The joint's child body.
|
| 102 |
+
coordinates : Matrix
|
| 103 |
+
Matrix of the joint's generalized coordinates.
|
| 104 |
+
speeds : Matrix
|
| 105 |
+
Matrix of the joint's generalized speeds.
|
| 106 |
+
parent_point : Point
|
| 107 |
+
Attachment point where the joint is fixed to the parent body.
|
| 108 |
+
child_point : Point
|
| 109 |
+
Attachment point where the joint is fixed to the child body.
|
| 110 |
+
parent_axis : Vector
|
| 111 |
+
The axis fixed in the parent frame that represents the joint.
|
| 112 |
+
child_axis : Vector
|
| 113 |
+
The axis fixed in the child frame that represents the joint.
|
| 114 |
+
parent_interframe : ReferenceFrame
|
| 115 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 116 |
+
transformation is formulated.
|
| 117 |
+
child_interframe : ReferenceFrame
|
| 118 |
+
Intermediate frame of the child body with respect to which the joint
|
| 119 |
+
transformation is formulated.
|
| 120 |
+
kdes : Matrix
|
| 121 |
+
Kinematical differential equations of the joint.
|
| 122 |
+
|
| 123 |
+
Notes
|
| 124 |
+
=====
|
| 125 |
+
|
| 126 |
+
When providing a vector as the intermediate frame, a new intermediate frame
|
| 127 |
+
is created which aligns its X axis with the provided vector. This is done
|
| 128 |
+
with a single fixed rotation about a rotation axis. This rotation axis is
|
| 129 |
+
determined by taking the cross product of the ``body.x`` axis with the
|
| 130 |
+
provided vector. In the case where the provided vector is in the ``-body.x``
|
| 131 |
+
direction, the rotation is done about the ``body.y`` axis.
|
| 132 |
+
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
def __init__(self, name, parent, child, coordinates=None, speeds=None,
|
| 136 |
+
parent_point=None, child_point=None, parent_interframe=None,
|
| 137 |
+
child_interframe=None, parent_axis=None, child_axis=None,
|
| 138 |
+
parent_joint_pos=None, child_joint_pos=None):
|
| 139 |
+
|
| 140 |
+
if not isinstance(name, str):
|
| 141 |
+
raise TypeError('Supply a valid name.')
|
| 142 |
+
self._name = name
|
| 143 |
+
|
| 144 |
+
if not isinstance(parent, BodyBase):
|
| 145 |
+
raise TypeError('Parent must be a body.')
|
| 146 |
+
self._parent = parent
|
| 147 |
+
|
| 148 |
+
if not isinstance(child, BodyBase):
|
| 149 |
+
raise TypeError('Child must be a body.')
|
| 150 |
+
self._child = child
|
| 151 |
+
|
| 152 |
+
if parent_axis is not None or child_axis is not None:
|
| 153 |
+
sympy_deprecation_warning(
|
| 154 |
+
"""
|
| 155 |
+
The parent_axis and child_axis arguments for the Joint classes
|
| 156 |
+
are deprecated. Instead use parent_interframe, child_interframe.
|
| 157 |
+
""",
|
| 158 |
+
deprecated_since_version="1.12",
|
| 159 |
+
active_deprecations_target="deprecated-mechanics-joint-axis",
|
| 160 |
+
stacklevel=4
|
| 161 |
+
)
|
| 162 |
+
if parent_interframe is None:
|
| 163 |
+
parent_interframe = parent_axis
|
| 164 |
+
if child_interframe is None:
|
| 165 |
+
child_interframe = child_axis
|
| 166 |
+
|
| 167 |
+
# Set parent and child frame attributes
|
| 168 |
+
if hasattr(self._parent, 'frame'):
|
| 169 |
+
self._parent_frame = self._parent.frame
|
| 170 |
+
else:
|
| 171 |
+
if isinstance(parent_interframe, ReferenceFrame):
|
| 172 |
+
self._parent_frame = parent_interframe
|
| 173 |
+
else:
|
| 174 |
+
self._parent_frame = ReferenceFrame(
|
| 175 |
+
f'{self.name}_{self._parent.name}_frame')
|
| 176 |
+
if hasattr(self._child, 'frame'):
|
| 177 |
+
self._child_frame = self._child.frame
|
| 178 |
+
else:
|
| 179 |
+
if isinstance(child_interframe, ReferenceFrame):
|
| 180 |
+
self._child_frame = child_interframe
|
| 181 |
+
else:
|
| 182 |
+
self._child_frame = ReferenceFrame(
|
| 183 |
+
f'{self.name}_{self._child.name}_frame')
|
| 184 |
+
|
| 185 |
+
self._parent_interframe = self._locate_joint_frame(
|
| 186 |
+
self._parent, parent_interframe, self._parent_frame)
|
| 187 |
+
self._child_interframe = self._locate_joint_frame(
|
| 188 |
+
self._child, child_interframe, self._child_frame)
|
| 189 |
+
self._parent_axis = self._axis(parent_axis, self._parent_frame)
|
| 190 |
+
self._child_axis = self._axis(child_axis, self._child_frame)
|
| 191 |
+
|
| 192 |
+
if parent_joint_pos is not None or child_joint_pos is not None:
|
| 193 |
+
sympy_deprecation_warning(
|
| 194 |
+
"""
|
| 195 |
+
The parent_joint_pos and child_joint_pos arguments for the Joint
|
| 196 |
+
classes are deprecated. Instead use parent_point and child_point.
|
| 197 |
+
""",
|
| 198 |
+
deprecated_since_version="1.12",
|
| 199 |
+
active_deprecations_target="deprecated-mechanics-joint-pos",
|
| 200 |
+
stacklevel=4
|
| 201 |
+
)
|
| 202 |
+
if parent_point is None:
|
| 203 |
+
parent_point = parent_joint_pos
|
| 204 |
+
if child_point is None:
|
| 205 |
+
child_point = child_joint_pos
|
| 206 |
+
self._parent_point = self._locate_joint_pos(
|
| 207 |
+
self._parent, parent_point, self._parent_frame)
|
| 208 |
+
self._child_point = self._locate_joint_pos(
|
| 209 |
+
self._child, child_point, self._child_frame)
|
| 210 |
+
|
| 211 |
+
self._coordinates = self._generate_coordinates(coordinates)
|
| 212 |
+
self._speeds = self._generate_speeds(speeds)
|
| 213 |
+
_validate_coordinates(self.coordinates, self.speeds)
|
| 214 |
+
self._kdes = self._generate_kdes()
|
| 215 |
+
|
| 216 |
+
self._orient_frames()
|
| 217 |
+
self._set_angular_velocity()
|
| 218 |
+
self._set_linear_velocity()
|
| 219 |
+
|
| 220 |
+
def __str__(self):
|
| 221 |
+
return self.name
|
| 222 |
+
|
| 223 |
+
def __repr__(self):
|
| 224 |
+
return self.__str__()
|
| 225 |
+
|
| 226 |
+
@property
|
| 227 |
+
def name(self):
|
| 228 |
+
"""Name of the joint."""
|
| 229 |
+
return self._name
|
| 230 |
+
|
| 231 |
+
@property
|
| 232 |
+
def parent(self):
|
| 233 |
+
"""Parent body of Joint."""
|
| 234 |
+
return self._parent
|
| 235 |
+
|
| 236 |
+
@property
|
| 237 |
+
def child(self):
|
| 238 |
+
"""Child body of Joint."""
|
| 239 |
+
return self._child
|
| 240 |
+
|
| 241 |
+
@property
|
| 242 |
+
def coordinates(self):
|
| 243 |
+
"""Matrix of the joint's generalized coordinates."""
|
| 244 |
+
return self._coordinates
|
| 245 |
+
|
| 246 |
+
@property
|
| 247 |
+
def speeds(self):
|
| 248 |
+
"""Matrix of the joint's generalized speeds."""
|
| 249 |
+
return self._speeds
|
| 250 |
+
|
| 251 |
+
@property
|
| 252 |
+
def kdes(self):
|
| 253 |
+
"""Kinematical differential equations of the joint."""
|
| 254 |
+
return self._kdes
|
| 255 |
+
|
| 256 |
+
@property
|
| 257 |
+
def parent_axis(self):
|
| 258 |
+
"""The axis of parent frame."""
|
| 259 |
+
# Will be removed with `deprecated-mechanics-joint-axis`
|
| 260 |
+
return self._parent_axis
|
| 261 |
+
|
| 262 |
+
@property
|
| 263 |
+
def child_axis(self):
|
| 264 |
+
"""The axis of child frame."""
|
| 265 |
+
# Will be removed with `deprecated-mechanics-joint-axis`
|
| 266 |
+
return self._child_axis
|
| 267 |
+
|
| 268 |
+
@property
|
| 269 |
+
def parent_point(self):
|
| 270 |
+
"""Attachment point where the joint is fixed to the parent body."""
|
| 271 |
+
return self._parent_point
|
| 272 |
+
|
| 273 |
+
@property
|
| 274 |
+
def child_point(self):
|
| 275 |
+
"""Attachment point where the joint is fixed to the child body."""
|
| 276 |
+
return self._child_point
|
| 277 |
+
|
| 278 |
+
@property
|
| 279 |
+
def parent_interframe(self):
|
| 280 |
+
return self._parent_interframe
|
| 281 |
+
|
| 282 |
+
@property
|
| 283 |
+
def child_interframe(self):
|
| 284 |
+
return self._child_interframe
|
| 285 |
+
|
| 286 |
+
@abstractmethod
|
| 287 |
+
def _generate_coordinates(self, coordinates):
|
| 288 |
+
"""Generate Matrix of the joint's generalized coordinates."""
|
| 289 |
+
pass
|
| 290 |
+
|
| 291 |
+
@abstractmethod
|
| 292 |
+
def _generate_speeds(self, speeds):
|
| 293 |
+
"""Generate Matrix of the joint's generalized speeds."""
|
| 294 |
+
pass
|
| 295 |
+
|
| 296 |
+
@abstractmethod
|
| 297 |
+
def _orient_frames(self):
|
| 298 |
+
"""Orient frames as per the joint."""
|
| 299 |
+
pass
|
| 300 |
+
|
| 301 |
+
@abstractmethod
|
| 302 |
+
def _set_angular_velocity(self):
|
| 303 |
+
"""Set angular velocity of the joint related frames."""
|
| 304 |
+
pass
|
| 305 |
+
|
| 306 |
+
@abstractmethod
|
| 307 |
+
def _set_linear_velocity(self):
|
| 308 |
+
"""Set velocity of related points to the joint."""
|
| 309 |
+
pass
|
| 310 |
+
|
| 311 |
+
@staticmethod
|
| 312 |
+
def _to_vector(matrix, frame):
|
| 313 |
+
"""Converts a matrix to a vector in the given frame."""
|
| 314 |
+
return Vector([(matrix, frame)])
|
| 315 |
+
|
| 316 |
+
@staticmethod
|
| 317 |
+
def _axis(ax, *frames):
|
| 318 |
+
"""Check whether an axis is fixed in one of the frames."""
|
| 319 |
+
if ax is None:
|
| 320 |
+
ax = frames[0].x
|
| 321 |
+
return ax
|
| 322 |
+
if not isinstance(ax, Vector):
|
| 323 |
+
raise TypeError("Axis must be a Vector.")
|
| 324 |
+
ref_frame = None # Find a body in which the axis can be expressed
|
| 325 |
+
for frame in frames:
|
| 326 |
+
try:
|
| 327 |
+
ax.to_matrix(frame)
|
| 328 |
+
except ValueError:
|
| 329 |
+
pass
|
| 330 |
+
else:
|
| 331 |
+
ref_frame = frame
|
| 332 |
+
break
|
| 333 |
+
if ref_frame is None:
|
| 334 |
+
raise ValueError("Axis cannot be expressed in one of the body's "
|
| 335 |
+
"frames.")
|
| 336 |
+
if not ax.dt(ref_frame) == 0:
|
| 337 |
+
raise ValueError('Axis cannot be time-varying when viewed from the '
|
| 338 |
+
'associated body.')
|
| 339 |
+
return ax
|
| 340 |
+
|
| 341 |
+
@staticmethod
|
| 342 |
+
def _choose_rotation_axis(frame, axis):
|
| 343 |
+
components = axis.to_matrix(frame)
|
| 344 |
+
x, y, z = components[0], components[1], components[2]
|
| 345 |
+
|
| 346 |
+
if x != 0:
|
| 347 |
+
if y != 0:
|
| 348 |
+
if z != 0:
|
| 349 |
+
return cross(axis, frame.x)
|
| 350 |
+
if z != 0:
|
| 351 |
+
return frame.y
|
| 352 |
+
return frame.z
|
| 353 |
+
else:
|
| 354 |
+
if y != 0:
|
| 355 |
+
return frame.x
|
| 356 |
+
return frame.y
|
| 357 |
+
|
| 358 |
+
@staticmethod
|
| 359 |
+
def _create_aligned_interframe(frame, align_axis, frame_axis=None,
|
| 360 |
+
frame_name=None):
|
| 361 |
+
"""
|
| 362 |
+
Returns an intermediate frame, where the ``frame_axis`` defined in
|
| 363 |
+
``frame`` is aligned with ``axis``. By default this means that the X
|
| 364 |
+
axis will be aligned with ``axis``.
|
| 365 |
+
|
| 366 |
+
Parameters
|
| 367 |
+
==========
|
| 368 |
+
|
| 369 |
+
frame : BodyBase or ReferenceFrame
|
| 370 |
+
The body or reference frame with respect to which the intermediate
|
| 371 |
+
frame is oriented.
|
| 372 |
+
align_axis : Vector
|
| 373 |
+
The vector with respect to which the intermediate frame will be
|
| 374 |
+
aligned.
|
| 375 |
+
frame_axis : Vector
|
| 376 |
+
The vector of the frame which should get aligned with ``axis``. The
|
| 377 |
+
default is the X axis of the frame.
|
| 378 |
+
frame_name : string
|
| 379 |
+
Name of the to be created intermediate frame. The default adds
|
| 380 |
+
"_int_frame" to the name of ``frame``.
|
| 381 |
+
|
| 382 |
+
Example
|
| 383 |
+
=======
|
| 384 |
+
|
| 385 |
+
An intermediate frame, where the X axis of the parent becomes aligned
|
| 386 |
+
with ``parent.y + parent.z`` can be created as follows:
|
| 387 |
+
|
| 388 |
+
>>> from sympy.physics.mechanics.joint import Joint
|
| 389 |
+
>>> from sympy.physics.mechanics import RigidBody
|
| 390 |
+
>>> parent = RigidBody('parent')
|
| 391 |
+
>>> parent_interframe = Joint._create_aligned_interframe(
|
| 392 |
+
... parent, parent.y + parent.z)
|
| 393 |
+
>>> parent_interframe
|
| 394 |
+
parent_int_frame
|
| 395 |
+
>>> parent.frame.dcm(parent_interframe)
|
| 396 |
+
Matrix([
|
| 397 |
+
[ 0, -sqrt(2)/2, -sqrt(2)/2],
|
| 398 |
+
[sqrt(2)/2, 1/2, -1/2],
|
| 399 |
+
[sqrt(2)/2, -1/2, 1/2]])
|
| 400 |
+
>>> (parent.y + parent.z).express(parent_interframe)
|
| 401 |
+
sqrt(2)*parent_int_frame.x
|
| 402 |
+
|
| 403 |
+
Notes
|
| 404 |
+
=====
|
| 405 |
+
|
| 406 |
+
The direction cosine matrix between the given frame and intermediate
|
| 407 |
+
frame is formed using a simple rotation about an axis that is normal to
|
| 408 |
+
both ``align_axis`` and ``frame_axis``. In general, the normal axis is
|
| 409 |
+
formed by crossing the ``frame_axis`` with the ``align_axis``. The
|
| 410 |
+
exception is if the axes are parallel with opposite directions, in which
|
| 411 |
+
case the rotation vector is chosen using the rules in the following
|
| 412 |
+
table with the vectors expressed in the given frame:
|
| 413 |
+
|
| 414 |
+
.. list-table::
|
| 415 |
+
:header-rows: 1
|
| 416 |
+
|
| 417 |
+
* - ``align_axis``
|
| 418 |
+
- ``frame_axis``
|
| 419 |
+
- ``rotation_axis``
|
| 420 |
+
* - ``-x``
|
| 421 |
+
- ``x``
|
| 422 |
+
- ``z``
|
| 423 |
+
* - ``-y``
|
| 424 |
+
- ``y``
|
| 425 |
+
- ``x``
|
| 426 |
+
* - ``-z``
|
| 427 |
+
- ``z``
|
| 428 |
+
- ``y``
|
| 429 |
+
* - ``-x-y``
|
| 430 |
+
- ``x+y``
|
| 431 |
+
- ``z``
|
| 432 |
+
* - ``-y-z``
|
| 433 |
+
- ``y+z``
|
| 434 |
+
- ``x``
|
| 435 |
+
* - ``-x-z``
|
| 436 |
+
- ``x+z``
|
| 437 |
+
- ``y``
|
| 438 |
+
* - ``-x-y-z``
|
| 439 |
+
- ``x+y+z``
|
| 440 |
+
- ``(x+y+z) × x``
|
| 441 |
+
|
| 442 |
+
"""
|
| 443 |
+
if isinstance(frame, BodyBase):
|
| 444 |
+
frame = frame.frame
|
| 445 |
+
if frame_axis is None:
|
| 446 |
+
frame_axis = frame.x
|
| 447 |
+
if frame_name is None:
|
| 448 |
+
if frame.name[-6:] == '_frame':
|
| 449 |
+
frame_name = f'{frame.name[:-6]}_int_frame'
|
| 450 |
+
else:
|
| 451 |
+
frame_name = f'{frame.name}_int_frame'
|
| 452 |
+
angle = frame_axis.angle_between(align_axis)
|
| 453 |
+
rotation_axis = cross(frame_axis, align_axis)
|
| 454 |
+
if rotation_axis == Vector(0) and angle == 0:
|
| 455 |
+
return frame
|
| 456 |
+
if angle == pi:
|
| 457 |
+
rotation_axis = Joint._choose_rotation_axis(frame, align_axis)
|
| 458 |
+
|
| 459 |
+
int_frame = ReferenceFrame(frame_name)
|
| 460 |
+
int_frame.orient_axis(frame, rotation_axis, angle)
|
| 461 |
+
int_frame.set_ang_vel(frame, 0 * rotation_axis)
|
| 462 |
+
return int_frame
|
| 463 |
+
|
| 464 |
+
def _generate_kdes(self):
|
| 465 |
+
"""Generate kinematical differential equations."""
|
| 466 |
+
kdes = []
|
| 467 |
+
t = dynamicsymbols._t
|
| 468 |
+
for i in range(len(self.coordinates)):
|
| 469 |
+
kdes.append(-self.coordinates[i].diff(t) + self.speeds[i])
|
| 470 |
+
return Matrix(kdes)
|
| 471 |
+
|
| 472 |
+
def _locate_joint_pos(self, body, joint_pos, body_frame=None):
|
| 473 |
+
"""Returns the attachment point of a body."""
|
| 474 |
+
if body_frame is None:
|
| 475 |
+
body_frame = body.frame
|
| 476 |
+
if joint_pos is None:
|
| 477 |
+
return body.masscenter
|
| 478 |
+
if not isinstance(joint_pos, (Point, Vector)):
|
| 479 |
+
raise TypeError('Attachment point must be a Point or Vector.')
|
| 480 |
+
if isinstance(joint_pos, Vector):
|
| 481 |
+
point_name = f'{self.name}_{body.name}_joint'
|
| 482 |
+
joint_pos = body.masscenter.locatenew(point_name, joint_pos)
|
| 483 |
+
if not joint_pos.pos_from(body.masscenter).dt(body_frame) == 0:
|
| 484 |
+
raise ValueError('Attachment point must be fixed to the associated '
|
| 485 |
+
'body.')
|
| 486 |
+
return joint_pos
|
| 487 |
+
|
| 488 |
+
def _locate_joint_frame(self, body, interframe, body_frame=None):
|
| 489 |
+
"""Returns the attachment frame of a body."""
|
| 490 |
+
if body_frame is None:
|
| 491 |
+
body_frame = body.frame
|
| 492 |
+
if interframe is None:
|
| 493 |
+
return body_frame
|
| 494 |
+
if isinstance(interframe, Vector):
|
| 495 |
+
interframe = Joint._create_aligned_interframe(
|
| 496 |
+
body_frame, interframe,
|
| 497 |
+
frame_name=f'{self.name}_{body.name}_int_frame')
|
| 498 |
+
elif not isinstance(interframe, ReferenceFrame):
|
| 499 |
+
raise TypeError('Interframe must be a ReferenceFrame.')
|
| 500 |
+
if not interframe.ang_vel_in(body_frame) == 0:
|
| 501 |
+
raise ValueError(f'Interframe {interframe} is not fixed to body '
|
| 502 |
+
f'{body}.')
|
| 503 |
+
body.masscenter.set_vel(interframe, 0) # Fixate interframe to body
|
| 504 |
+
return interframe
|
| 505 |
+
|
| 506 |
+
def _fill_coordinate_list(self, coordinates, n_coords, label='q', offset=0,
|
| 507 |
+
number_single=False):
|
| 508 |
+
"""Helper method for _generate_coordinates and _generate_speeds.
|
| 509 |
+
|
| 510 |
+
Parameters
|
| 511 |
+
==========
|
| 512 |
+
|
| 513 |
+
coordinates : iterable
|
| 514 |
+
Iterable of coordinates or speeds that have been provided.
|
| 515 |
+
n_coords : Integer
|
| 516 |
+
Number of coordinates that should be returned.
|
| 517 |
+
label : String, optional
|
| 518 |
+
Coordinate type either 'q' (coordinates) or 'u' (speeds). The
|
| 519 |
+
Default is 'q'.
|
| 520 |
+
offset : Integer
|
| 521 |
+
Count offset when creating new dynamicsymbols. The default is 0.
|
| 522 |
+
number_single : Boolean
|
| 523 |
+
Boolean whether if n_coords == 1, number should still be used. The
|
| 524 |
+
default is False.
|
| 525 |
+
|
| 526 |
+
"""
|
| 527 |
+
|
| 528 |
+
def create_symbol(number):
|
| 529 |
+
if n_coords == 1 and not number_single:
|
| 530 |
+
return dynamicsymbols(f'{label}_{self.name}')
|
| 531 |
+
return dynamicsymbols(f'{label}{number}_{self.name}')
|
| 532 |
+
|
| 533 |
+
name = 'generalized coordinate' if label == 'q' else 'generalized speed'
|
| 534 |
+
generated_coordinates = []
|
| 535 |
+
if coordinates is None:
|
| 536 |
+
coordinates = []
|
| 537 |
+
elif not iterable(coordinates):
|
| 538 |
+
coordinates = [coordinates]
|
| 539 |
+
if not (len(coordinates) == 0 or len(coordinates) == n_coords):
|
| 540 |
+
raise ValueError(f'Expected {n_coords} {name}s, instead got '
|
| 541 |
+
f'{len(coordinates)} {name}s.')
|
| 542 |
+
# Supports more iterables, also Matrix
|
| 543 |
+
for i, coord in enumerate(coordinates):
|
| 544 |
+
if coord is None:
|
| 545 |
+
generated_coordinates.append(create_symbol(i + offset))
|
| 546 |
+
elif isinstance(coord, (AppliedUndef, Derivative)):
|
| 547 |
+
generated_coordinates.append(coord)
|
| 548 |
+
else:
|
| 549 |
+
raise TypeError(f'The {name} {coord} should have been a '
|
| 550 |
+
f'dynamicsymbol.')
|
| 551 |
+
for i in range(len(coordinates) + offset, n_coords + offset):
|
| 552 |
+
generated_coordinates.append(create_symbol(i))
|
| 553 |
+
return Matrix(generated_coordinates)
|
| 554 |
+
|
| 555 |
+
|
| 556 |
+
class PinJoint(Joint):
|
| 557 |
+
"""Pin (Revolute) Joint.
|
| 558 |
+
|
| 559 |
+
.. raw:: html
|
| 560 |
+
:file: ../../../doc/src/modules/physics/mechanics/api/PinJoint.svg
|
| 561 |
+
|
| 562 |
+
Explanation
|
| 563 |
+
===========
|
| 564 |
+
|
| 565 |
+
A pin joint is defined such that the joint rotation axis is fixed in both
|
| 566 |
+
the child and parent and the location of the joint is relative to the mass
|
| 567 |
+
center of each body. The child rotates an angle, θ, from the parent about
|
| 568 |
+
the rotation axis and has a simple angular speed, ω, relative to the
|
| 569 |
+
parent. The direction cosine matrix between the child interframe and
|
| 570 |
+
parent interframe is formed using a simple rotation about the joint axis.
|
| 571 |
+
The page on the joints framework gives a more detailed explanation of the
|
| 572 |
+
intermediate frames.
|
| 573 |
+
|
| 574 |
+
Parameters
|
| 575 |
+
==========
|
| 576 |
+
|
| 577 |
+
name : string
|
| 578 |
+
A unique name for the joint.
|
| 579 |
+
parent : Particle or RigidBody or Body
|
| 580 |
+
The parent body of joint.
|
| 581 |
+
child : Particle or RigidBody or Body
|
| 582 |
+
The child body of joint.
|
| 583 |
+
coordinates : dynamicsymbol, optional
|
| 584 |
+
Generalized coordinates of the joint.
|
| 585 |
+
speeds : dynamicsymbol, optional
|
| 586 |
+
Generalized speeds of joint.
|
| 587 |
+
parent_point : Point or Vector, optional
|
| 588 |
+
Attachment point where the joint is fixed to the parent body. If a
|
| 589 |
+
vector is provided, then the attachment point is computed by adding the
|
| 590 |
+
vector to the body's mass center. The default value is the parent's mass
|
| 591 |
+
center.
|
| 592 |
+
child_point : Point or Vector, optional
|
| 593 |
+
Attachment point where the joint is fixed to the child body. If a
|
| 594 |
+
vector is provided, then the attachment point is computed by adding the
|
| 595 |
+
vector to the body's mass center. The default value is the child's mass
|
| 596 |
+
center.
|
| 597 |
+
parent_axis : Vector, optional
|
| 598 |
+
.. deprecated:: 1.12
|
| 599 |
+
Axis fixed in the parent body which aligns with an axis fixed in the
|
| 600 |
+
child body. The default is the x axis of parent's reference frame.
|
| 601 |
+
For more information on this deprecation, see
|
| 602 |
+
:ref:`deprecated-mechanics-joint-axis`.
|
| 603 |
+
child_axis : Vector, optional
|
| 604 |
+
.. deprecated:: 1.12
|
| 605 |
+
Axis fixed in the child body which aligns with an axis fixed in the
|
| 606 |
+
parent body. The default is the x axis of child's reference frame.
|
| 607 |
+
For more information on this deprecation, see
|
| 608 |
+
:ref:`deprecated-mechanics-joint-axis`.
|
| 609 |
+
parent_interframe : ReferenceFrame, optional
|
| 610 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 611 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 612 |
+
is created which aligns its X axis with the given vector. The default
|
| 613 |
+
value is the parent's own frame.
|
| 614 |
+
child_interframe : ReferenceFrame, optional
|
| 615 |
+
Intermediate frame of the child body with respect to which the joint
|
| 616 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 617 |
+
is created which aligns its X axis with the given vector. The default
|
| 618 |
+
value is the child's own frame.
|
| 619 |
+
joint_axis : Vector
|
| 620 |
+
The axis about which the rotation occurs. Note that the components
|
| 621 |
+
of this axis are the same in the parent_interframe and child_interframe.
|
| 622 |
+
parent_joint_pos : Point or Vector, optional
|
| 623 |
+
.. deprecated:: 1.12
|
| 624 |
+
This argument is replaced by parent_point and will be removed in a
|
| 625 |
+
future version.
|
| 626 |
+
See :ref:`deprecated-mechanics-joint-pos` for more information.
|
| 627 |
+
child_joint_pos : Point or Vector, optional
|
| 628 |
+
.. deprecated:: 1.12
|
| 629 |
+
This argument is replaced by child_point and will be removed in a
|
| 630 |
+
future version.
|
| 631 |
+
See :ref:`deprecated-mechanics-joint-pos` for more information.
|
| 632 |
+
|
| 633 |
+
Attributes
|
| 634 |
+
==========
|
| 635 |
+
|
| 636 |
+
name : string
|
| 637 |
+
The joint's name.
|
| 638 |
+
parent : Particle or RigidBody or Body
|
| 639 |
+
The joint's parent body.
|
| 640 |
+
child : Particle or RigidBody or Body
|
| 641 |
+
The joint's child body.
|
| 642 |
+
coordinates : Matrix
|
| 643 |
+
Matrix of the joint's generalized coordinates. The default value is
|
| 644 |
+
``dynamicsymbols(f'q_{joint.name}')``.
|
| 645 |
+
speeds : Matrix
|
| 646 |
+
Matrix of the joint's generalized speeds. The default value is
|
| 647 |
+
``dynamicsymbols(f'u_{joint.name}')``.
|
| 648 |
+
parent_point : Point
|
| 649 |
+
Attachment point where the joint is fixed to the parent body.
|
| 650 |
+
child_point : Point
|
| 651 |
+
Attachment point where the joint is fixed to the child body.
|
| 652 |
+
parent_axis : Vector
|
| 653 |
+
The axis fixed in the parent frame that represents the joint.
|
| 654 |
+
child_axis : Vector
|
| 655 |
+
The axis fixed in the child frame that represents the joint.
|
| 656 |
+
parent_interframe : ReferenceFrame
|
| 657 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 658 |
+
transformation is formulated.
|
| 659 |
+
child_interframe : ReferenceFrame
|
| 660 |
+
Intermediate frame of the child body with respect to which the joint
|
| 661 |
+
transformation is formulated.
|
| 662 |
+
joint_axis : Vector
|
| 663 |
+
The axis about which the rotation occurs. Note that the components of
|
| 664 |
+
this axis are the same in the parent_interframe and child_interframe.
|
| 665 |
+
kdes : Matrix
|
| 666 |
+
Kinematical differential equations of the joint.
|
| 667 |
+
|
| 668 |
+
Examples
|
| 669 |
+
=========
|
| 670 |
+
|
| 671 |
+
A single pin joint is created from two bodies and has the following basic
|
| 672 |
+
attributes:
|
| 673 |
+
|
| 674 |
+
>>> from sympy.physics.mechanics import RigidBody, PinJoint
|
| 675 |
+
>>> parent = RigidBody('P')
|
| 676 |
+
>>> parent
|
| 677 |
+
P
|
| 678 |
+
>>> child = RigidBody('C')
|
| 679 |
+
>>> child
|
| 680 |
+
C
|
| 681 |
+
>>> joint = PinJoint('PC', parent, child)
|
| 682 |
+
>>> joint
|
| 683 |
+
PinJoint: PC parent: P child: C
|
| 684 |
+
>>> joint.name
|
| 685 |
+
'PC'
|
| 686 |
+
>>> joint.parent
|
| 687 |
+
P
|
| 688 |
+
>>> joint.child
|
| 689 |
+
C
|
| 690 |
+
>>> joint.parent_point
|
| 691 |
+
P_masscenter
|
| 692 |
+
>>> joint.child_point
|
| 693 |
+
C_masscenter
|
| 694 |
+
>>> joint.parent_axis
|
| 695 |
+
P_frame.x
|
| 696 |
+
>>> joint.child_axis
|
| 697 |
+
C_frame.x
|
| 698 |
+
>>> joint.coordinates
|
| 699 |
+
Matrix([[q_PC(t)]])
|
| 700 |
+
>>> joint.speeds
|
| 701 |
+
Matrix([[u_PC(t)]])
|
| 702 |
+
>>> child.frame.ang_vel_in(parent.frame)
|
| 703 |
+
u_PC(t)*P_frame.x
|
| 704 |
+
>>> child.frame.dcm(parent.frame)
|
| 705 |
+
Matrix([
|
| 706 |
+
[1, 0, 0],
|
| 707 |
+
[0, cos(q_PC(t)), sin(q_PC(t))],
|
| 708 |
+
[0, -sin(q_PC(t)), cos(q_PC(t))]])
|
| 709 |
+
>>> joint.child_point.pos_from(joint.parent_point)
|
| 710 |
+
0
|
| 711 |
+
|
| 712 |
+
To further demonstrate the use of the pin joint, the kinematics of simple
|
| 713 |
+
double pendulum that rotates about the Z axis of each connected body can be
|
| 714 |
+
created as follows.
|
| 715 |
+
|
| 716 |
+
>>> from sympy import symbols, trigsimp
|
| 717 |
+
>>> from sympy.physics.mechanics import RigidBody, PinJoint
|
| 718 |
+
>>> l1, l2 = symbols('l1 l2')
|
| 719 |
+
|
| 720 |
+
First create bodies to represent the fixed ceiling and one to represent
|
| 721 |
+
each pendulum bob.
|
| 722 |
+
|
| 723 |
+
>>> ceiling = RigidBody('C')
|
| 724 |
+
>>> upper_bob = RigidBody('U')
|
| 725 |
+
>>> lower_bob = RigidBody('L')
|
| 726 |
+
|
| 727 |
+
The first joint will connect the upper bob to the ceiling by a distance of
|
| 728 |
+
``l1`` and the joint axis will be about the Z axis for each body.
|
| 729 |
+
|
| 730 |
+
>>> ceiling_joint = PinJoint('P1', ceiling, upper_bob,
|
| 731 |
+
... child_point=-l1*upper_bob.frame.x,
|
| 732 |
+
... joint_axis=ceiling.frame.z)
|
| 733 |
+
|
| 734 |
+
The second joint will connect the lower bob to the upper bob by a distance
|
| 735 |
+
of ``l2`` and the joint axis will also be about the Z axis for each body.
|
| 736 |
+
|
| 737 |
+
>>> pendulum_joint = PinJoint('P2', upper_bob, lower_bob,
|
| 738 |
+
... child_point=-l2*lower_bob.frame.x,
|
| 739 |
+
... joint_axis=upper_bob.frame.z)
|
| 740 |
+
|
| 741 |
+
Once the joints are established the kinematics of the connected bodies can
|
| 742 |
+
be accessed. First the direction cosine matrices of pendulum link relative
|
| 743 |
+
to the ceiling are found:
|
| 744 |
+
|
| 745 |
+
>>> upper_bob.frame.dcm(ceiling.frame)
|
| 746 |
+
Matrix([
|
| 747 |
+
[ cos(q_P1(t)), sin(q_P1(t)), 0],
|
| 748 |
+
[-sin(q_P1(t)), cos(q_P1(t)), 0],
|
| 749 |
+
[ 0, 0, 1]])
|
| 750 |
+
>>> trigsimp(lower_bob.frame.dcm(ceiling.frame))
|
| 751 |
+
Matrix([
|
| 752 |
+
[ cos(q_P1(t) + q_P2(t)), sin(q_P1(t) + q_P2(t)), 0],
|
| 753 |
+
[-sin(q_P1(t) + q_P2(t)), cos(q_P1(t) + q_P2(t)), 0],
|
| 754 |
+
[ 0, 0, 1]])
|
| 755 |
+
|
| 756 |
+
The position of the lower bob's masscenter is found with:
|
| 757 |
+
|
| 758 |
+
>>> lower_bob.masscenter.pos_from(ceiling.masscenter)
|
| 759 |
+
l1*U_frame.x + l2*L_frame.x
|
| 760 |
+
|
| 761 |
+
The angular velocities of the two pendulum links can be computed with
|
| 762 |
+
respect to the ceiling.
|
| 763 |
+
|
| 764 |
+
>>> upper_bob.frame.ang_vel_in(ceiling.frame)
|
| 765 |
+
u_P1(t)*C_frame.z
|
| 766 |
+
>>> lower_bob.frame.ang_vel_in(ceiling.frame)
|
| 767 |
+
u_P1(t)*C_frame.z + u_P2(t)*U_frame.z
|
| 768 |
+
|
| 769 |
+
And finally, the linear velocities of the two pendulum bobs can be computed
|
| 770 |
+
with respect to the ceiling.
|
| 771 |
+
|
| 772 |
+
>>> upper_bob.masscenter.vel(ceiling.frame)
|
| 773 |
+
l1*u_P1(t)*U_frame.y
|
| 774 |
+
>>> lower_bob.masscenter.vel(ceiling.frame)
|
| 775 |
+
l1*u_P1(t)*U_frame.y + l2*(u_P1(t) + u_P2(t))*L_frame.y
|
| 776 |
+
|
| 777 |
+
"""
|
| 778 |
+
|
| 779 |
+
def __init__(self, name, parent, child, coordinates=None, speeds=None,
|
| 780 |
+
parent_point=None, child_point=None, parent_interframe=None,
|
| 781 |
+
child_interframe=None, parent_axis=None, child_axis=None,
|
| 782 |
+
joint_axis=None, parent_joint_pos=None, child_joint_pos=None):
|
| 783 |
+
|
| 784 |
+
self._joint_axis = joint_axis
|
| 785 |
+
super().__init__(name, parent, child, coordinates, speeds, parent_point,
|
| 786 |
+
child_point, parent_interframe, child_interframe,
|
| 787 |
+
parent_axis, child_axis, parent_joint_pos,
|
| 788 |
+
child_joint_pos)
|
| 789 |
+
|
| 790 |
+
def __str__(self):
|
| 791 |
+
return (f'PinJoint: {self.name} parent: {self.parent} '
|
| 792 |
+
f'child: {self.child}')
|
| 793 |
+
|
| 794 |
+
@property
|
| 795 |
+
def joint_axis(self):
|
| 796 |
+
"""Axis about which the child rotates with respect to the parent."""
|
| 797 |
+
return self._joint_axis
|
| 798 |
+
|
| 799 |
+
def _generate_coordinates(self, coordinate):
|
| 800 |
+
return self._fill_coordinate_list(coordinate, 1, 'q')
|
| 801 |
+
|
| 802 |
+
def _generate_speeds(self, speed):
|
| 803 |
+
return self._fill_coordinate_list(speed, 1, 'u')
|
| 804 |
+
|
| 805 |
+
def _orient_frames(self):
|
| 806 |
+
self._joint_axis = self._axis(self.joint_axis, self.parent_interframe)
|
| 807 |
+
self.child_interframe.orient_axis(
|
| 808 |
+
self.parent_interframe, self.joint_axis, self.coordinates[0])
|
| 809 |
+
|
| 810 |
+
def _set_angular_velocity(self):
|
| 811 |
+
self.child_interframe.set_ang_vel(self.parent_interframe, self.speeds[
|
| 812 |
+
0] * self.joint_axis.normalize())
|
| 813 |
+
|
| 814 |
+
def _set_linear_velocity(self):
|
| 815 |
+
self.child_point.set_pos(self.parent_point, 0)
|
| 816 |
+
self.parent_point.set_vel(self._parent_frame, 0)
|
| 817 |
+
self.child_point.set_vel(self._child_frame, 0)
|
| 818 |
+
self.child.masscenter.v2pt_theory(self.parent_point,
|
| 819 |
+
self._parent_frame, self._child_frame)
|
| 820 |
+
|
| 821 |
+
|
| 822 |
+
class PrismaticJoint(Joint):
|
| 823 |
+
"""Prismatic (Sliding) Joint.
|
| 824 |
+
|
| 825 |
+
.. image:: PrismaticJoint.svg
|
| 826 |
+
|
| 827 |
+
Explanation
|
| 828 |
+
===========
|
| 829 |
+
|
| 830 |
+
It is defined such that the child body translates with respect to the parent
|
| 831 |
+
body along the body-fixed joint axis. The location of the joint is defined
|
| 832 |
+
by two points, one in each body, which coincide when the generalized
|
| 833 |
+
coordinate is zero. The direction cosine matrix between the
|
| 834 |
+
parent_interframe and child_interframe is the identity matrix. Therefore,
|
| 835 |
+
the direction cosine matrix between the parent and child frames is fully
|
| 836 |
+
defined by the definition of the intermediate frames. The page on the joints
|
| 837 |
+
framework gives a more detailed explanation of the intermediate frames.
|
| 838 |
+
|
| 839 |
+
Parameters
|
| 840 |
+
==========
|
| 841 |
+
|
| 842 |
+
name : string
|
| 843 |
+
A unique name for the joint.
|
| 844 |
+
parent : Particle or RigidBody or Body
|
| 845 |
+
The parent body of joint.
|
| 846 |
+
child : Particle or RigidBody or Body
|
| 847 |
+
The child body of joint.
|
| 848 |
+
coordinates : dynamicsymbol, optional
|
| 849 |
+
Generalized coordinates of the joint. The default value is
|
| 850 |
+
``dynamicsymbols(f'q_{joint.name}')``.
|
| 851 |
+
speeds : dynamicsymbol, optional
|
| 852 |
+
Generalized speeds of joint. The default value is
|
| 853 |
+
``dynamicsymbols(f'u_{joint.name}')``.
|
| 854 |
+
parent_point : Point or Vector, optional
|
| 855 |
+
Attachment point where the joint is fixed to the parent body. If a
|
| 856 |
+
vector is provided, then the attachment point is computed by adding the
|
| 857 |
+
vector to the body's mass center. The default value is the parent's mass
|
| 858 |
+
center.
|
| 859 |
+
child_point : Point or Vector, optional
|
| 860 |
+
Attachment point where the joint is fixed to the child body. If a
|
| 861 |
+
vector is provided, then the attachment point is computed by adding the
|
| 862 |
+
vector to the body's mass center. The default value is the child's mass
|
| 863 |
+
center.
|
| 864 |
+
parent_axis : Vector, optional
|
| 865 |
+
.. deprecated:: 1.12
|
| 866 |
+
Axis fixed in the parent body which aligns with an axis fixed in the
|
| 867 |
+
child body. The default is the x axis of parent's reference frame.
|
| 868 |
+
For more information on this deprecation, see
|
| 869 |
+
:ref:`deprecated-mechanics-joint-axis`.
|
| 870 |
+
child_axis : Vector, optional
|
| 871 |
+
.. deprecated:: 1.12
|
| 872 |
+
Axis fixed in the child body which aligns with an axis fixed in the
|
| 873 |
+
parent body. The default is the x axis of child's reference frame.
|
| 874 |
+
For more information on this deprecation, see
|
| 875 |
+
:ref:`deprecated-mechanics-joint-axis`.
|
| 876 |
+
parent_interframe : ReferenceFrame, optional
|
| 877 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 878 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 879 |
+
is created which aligns its X axis with the given vector. The default
|
| 880 |
+
value is the parent's own frame.
|
| 881 |
+
child_interframe : ReferenceFrame, optional
|
| 882 |
+
Intermediate frame of the child body with respect to which the joint
|
| 883 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 884 |
+
is created which aligns its X axis with the given vector. The default
|
| 885 |
+
value is the child's own frame.
|
| 886 |
+
joint_axis : Vector
|
| 887 |
+
The axis along which the translation occurs. Note that the components
|
| 888 |
+
of this axis are the same in the parent_interframe and child_interframe.
|
| 889 |
+
parent_joint_pos : Point or Vector, optional
|
| 890 |
+
.. deprecated:: 1.12
|
| 891 |
+
This argument is replaced by parent_point and will be removed in a
|
| 892 |
+
future version.
|
| 893 |
+
See :ref:`deprecated-mechanics-joint-pos` for more information.
|
| 894 |
+
child_joint_pos : Point or Vector, optional
|
| 895 |
+
.. deprecated:: 1.12
|
| 896 |
+
This argument is replaced by child_point and will be removed in a
|
| 897 |
+
future version.
|
| 898 |
+
See :ref:`deprecated-mechanics-joint-pos` for more information.
|
| 899 |
+
|
| 900 |
+
Attributes
|
| 901 |
+
==========
|
| 902 |
+
|
| 903 |
+
name : string
|
| 904 |
+
The joint's name.
|
| 905 |
+
parent : Particle or RigidBody or Body
|
| 906 |
+
The joint's parent body.
|
| 907 |
+
child : Particle or RigidBody or Body
|
| 908 |
+
The joint's child body.
|
| 909 |
+
coordinates : Matrix
|
| 910 |
+
Matrix of the joint's generalized coordinates.
|
| 911 |
+
speeds : Matrix
|
| 912 |
+
Matrix of the joint's generalized speeds.
|
| 913 |
+
parent_point : Point
|
| 914 |
+
Attachment point where the joint is fixed to the parent body.
|
| 915 |
+
child_point : Point
|
| 916 |
+
Attachment point where the joint is fixed to the child body.
|
| 917 |
+
parent_axis : Vector
|
| 918 |
+
The axis fixed in the parent frame that represents the joint.
|
| 919 |
+
child_axis : Vector
|
| 920 |
+
The axis fixed in the child frame that represents the joint.
|
| 921 |
+
parent_interframe : ReferenceFrame
|
| 922 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 923 |
+
transformation is formulated.
|
| 924 |
+
child_interframe : ReferenceFrame
|
| 925 |
+
Intermediate frame of the child body with respect to which the joint
|
| 926 |
+
transformation is formulated.
|
| 927 |
+
kdes : Matrix
|
| 928 |
+
Kinematical differential equations of the joint.
|
| 929 |
+
|
| 930 |
+
Examples
|
| 931 |
+
=========
|
| 932 |
+
|
| 933 |
+
A single prismatic joint is created from two bodies and has the following
|
| 934 |
+
basic attributes:
|
| 935 |
+
|
| 936 |
+
>>> from sympy.physics.mechanics import RigidBody, PrismaticJoint
|
| 937 |
+
>>> parent = RigidBody('P')
|
| 938 |
+
>>> parent
|
| 939 |
+
P
|
| 940 |
+
>>> child = RigidBody('C')
|
| 941 |
+
>>> child
|
| 942 |
+
C
|
| 943 |
+
>>> joint = PrismaticJoint('PC', parent, child)
|
| 944 |
+
>>> joint
|
| 945 |
+
PrismaticJoint: PC parent: P child: C
|
| 946 |
+
>>> joint.name
|
| 947 |
+
'PC'
|
| 948 |
+
>>> joint.parent
|
| 949 |
+
P
|
| 950 |
+
>>> joint.child
|
| 951 |
+
C
|
| 952 |
+
>>> joint.parent_point
|
| 953 |
+
P_masscenter
|
| 954 |
+
>>> joint.child_point
|
| 955 |
+
C_masscenter
|
| 956 |
+
>>> joint.parent_axis
|
| 957 |
+
P_frame.x
|
| 958 |
+
>>> joint.child_axis
|
| 959 |
+
C_frame.x
|
| 960 |
+
>>> joint.coordinates
|
| 961 |
+
Matrix([[q_PC(t)]])
|
| 962 |
+
>>> joint.speeds
|
| 963 |
+
Matrix([[u_PC(t)]])
|
| 964 |
+
>>> child.frame.ang_vel_in(parent.frame)
|
| 965 |
+
0
|
| 966 |
+
>>> child.frame.dcm(parent.frame)
|
| 967 |
+
Matrix([
|
| 968 |
+
[1, 0, 0],
|
| 969 |
+
[0, 1, 0],
|
| 970 |
+
[0, 0, 1]])
|
| 971 |
+
>>> joint.child_point.pos_from(joint.parent_point)
|
| 972 |
+
q_PC(t)*P_frame.x
|
| 973 |
+
|
| 974 |
+
To further demonstrate the use of the prismatic joint, the kinematics of two
|
| 975 |
+
masses sliding, one moving relative to a fixed body and the other relative
|
| 976 |
+
to the moving body. about the X axis of each connected body can be created
|
| 977 |
+
as follows.
|
| 978 |
+
|
| 979 |
+
>>> from sympy.physics.mechanics import PrismaticJoint, RigidBody
|
| 980 |
+
|
| 981 |
+
First create bodies to represent the fixed ceiling and one to represent
|
| 982 |
+
a particle.
|
| 983 |
+
|
| 984 |
+
>>> wall = RigidBody('W')
|
| 985 |
+
>>> Part1 = RigidBody('P1')
|
| 986 |
+
>>> Part2 = RigidBody('P2')
|
| 987 |
+
|
| 988 |
+
The first joint will connect the particle to the ceiling and the
|
| 989 |
+
joint axis will be about the X axis for each body.
|
| 990 |
+
|
| 991 |
+
>>> J1 = PrismaticJoint('J1', wall, Part1)
|
| 992 |
+
|
| 993 |
+
The second joint will connect the second particle to the first particle
|
| 994 |
+
and the joint axis will also be about the X axis for each body.
|
| 995 |
+
|
| 996 |
+
>>> J2 = PrismaticJoint('J2', Part1, Part2)
|
| 997 |
+
|
| 998 |
+
Once the joint is established the kinematics of the connected bodies can
|
| 999 |
+
be accessed. First the direction cosine matrices of Part relative
|
| 1000 |
+
to the ceiling are found:
|
| 1001 |
+
|
| 1002 |
+
>>> Part1.frame.dcm(wall.frame)
|
| 1003 |
+
Matrix([
|
| 1004 |
+
[1, 0, 0],
|
| 1005 |
+
[0, 1, 0],
|
| 1006 |
+
[0, 0, 1]])
|
| 1007 |
+
|
| 1008 |
+
>>> Part2.frame.dcm(wall.frame)
|
| 1009 |
+
Matrix([
|
| 1010 |
+
[1, 0, 0],
|
| 1011 |
+
[0, 1, 0],
|
| 1012 |
+
[0, 0, 1]])
|
| 1013 |
+
|
| 1014 |
+
The position of the particles' masscenter is found with:
|
| 1015 |
+
|
| 1016 |
+
>>> Part1.masscenter.pos_from(wall.masscenter)
|
| 1017 |
+
q_J1(t)*W_frame.x
|
| 1018 |
+
|
| 1019 |
+
>>> Part2.masscenter.pos_from(wall.masscenter)
|
| 1020 |
+
q_J1(t)*W_frame.x + q_J2(t)*P1_frame.x
|
| 1021 |
+
|
| 1022 |
+
The angular velocities of the two particle links can be computed with
|
| 1023 |
+
respect to the ceiling.
|
| 1024 |
+
|
| 1025 |
+
>>> Part1.frame.ang_vel_in(wall.frame)
|
| 1026 |
+
0
|
| 1027 |
+
|
| 1028 |
+
>>> Part2.frame.ang_vel_in(wall.frame)
|
| 1029 |
+
0
|
| 1030 |
+
|
| 1031 |
+
And finally, the linear velocities of the two particles can be computed
|
| 1032 |
+
with respect to the ceiling.
|
| 1033 |
+
|
| 1034 |
+
>>> Part1.masscenter.vel(wall.frame)
|
| 1035 |
+
u_J1(t)*W_frame.x
|
| 1036 |
+
|
| 1037 |
+
>>> Part2.masscenter.vel(wall.frame)
|
| 1038 |
+
u_J1(t)*W_frame.x + Derivative(q_J2(t), t)*P1_frame.x
|
| 1039 |
+
|
| 1040 |
+
"""
|
| 1041 |
+
|
| 1042 |
+
def __init__(self, name, parent, child, coordinates=None, speeds=None,
|
| 1043 |
+
parent_point=None, child_point=None, parent_interframe=None,
|
| 1044 |
+
child_interframe=None, parent_axis=None, child_axis=None,
|
| 1045 |
+
joint_axis=None, parent_joint_pos=None, child_joint_pos=None):
|
| 1046 |
+
|
| 1047 |
+
self._joint_axis = joint_axis
|
| 1048 |
+
super().__init__(name, parent, child, coordinates, speeds, parent_point,
|
| 1049 |
+
child_point, parent_interframe, child_interframe,
|
| 1050 |
+
parent_axis, child_axis, parent_joint_pos,
|
| 1051 |
+
child_joint_pos)
|
| 1052 |
+
|
| 1053 |
+
def __str__(self):
|
| 1054 |
+
return (f'PrismaticJoint: {self.name} parent: {self.parent} '
|
| 1055 |
+
f'child: {self.child}')
|
| 1056 |
+
|
| 1057 |
+
@property
|
| 1058 |
+
def joint_axis(self):
|
| 1059 |
+
"""Axis along which the child translates with respect to the parent."""
|
| 1060 |
+
return self._joint_axis
|
| 1061 |
+
|
| 1062 |
+
def _generate_coordinates(self, coordinate):
|
| 1063 |
+
return self._fill_coordinate_list(coordinate, 1, 'q')
|
| 1064 |
+
|
| 1065 |
+
def _generate_speeds(self, speed):
|
| 1066 |
+
return self._fill_coordinate_list(speed, 1, 'u')
|
| 1067 |
+
|
| 1068 |
+
def _orient_frames(self):
|
| 1069 |
+
self._joint_axis = self._axis(self.joint_axis, self.parent_interframe)
|
| 1070 |
+
self.child_interframe.orient_axis(
|
| 1071 |
+
self.parent_interframe, self.joint_axis, 0)
|
| 1072 |
+
|
| 1073 |
+
def _set_angular_velocity(self):
|
| 1074 |
+
self.child_interframe.set_ang_vel(self.parent_interframe, 0)
|
| 1075 |
+
|
| 1076 |
+
def _set_linear_velocity(self):
|
| 1077 |
+
axis = self.joint_axis.normalize()
|
| 1078 |
+
self.child_point.set_pos(self.parent_point, self.coordinates[0] * axis)
|
| 1079 |
+
self.parent_point.set_vel(self._parent_frame, 0)
|
| 1080 |
+
self.child_point.set_vel(self._child_frame, 0)
|
| 1081 |
+
self.child_point.set_vel(self._parent_frame, self.speeds[0] * axis)
|
| 1082 |
+
self.child.masscenter.set_vel(self._parent_frame, self.speeds[0] * axis)
|
| 1083 |
+
|
| 1084 |
+
|
| 1085 |
+
class CylindricalJoint(Joint):
|
| 1086 |
+
"""Cylindrical Joint.
|
| 1087 |
+
|
| 1088 |
+
.. image:: CylindricalJoint.svg
|
| 1089 |
+
:align: center
|
| 1090 |
+
:width: 600
|
| 1091 |
+
|
| 1092 |
+
Explanation
|
| 1093 |
+
===========
|
| 1094 |
+
|
| 1095 |
+
A cylindrical joint is defined such that the child body both rotates about
|
| 1096 |
+
and translates along the body-fixed joint axis with respect to the parent
|
| 1097 |
+
body. The joint axis is both the rotation axis and translation axis. The
|
| 1098 |
+
location of the joint is defined by two points, one in each body, which
|
| 1099 |
+
coincide when the generalized coordinate corresponding to the translation is
|
| 1100 |
+
zero. The direction cosine matrix between the child interframe and parent
|
| 1101 |
+
interframe is formed using a simple rotation about the joint axis. The page
|
| 1102 |
+
on the joints framework gives a more detailed explanation of the
|
| 1103 |
+
intermediate frames.
|
| 1104 |
+
|
| 1105 |
+
Parameters
|
| 1106 |
+
==========
|
| 1107 |
+
|
| 1108 |
+
name : string
|
| 1109 |
+
A unique name for the joint.
|
| 1110 |
+
parent : Particle or RigidBody or Body
|
| 1111 |
+
The parent body of joint.
|
| 1112 |
+
child : Particle or RigidBody or Body
|
| 1113 |
+
The child body of joint.
|
| 1114 |
+
rotation_coordinate : dynamicsymbol, optional
|
| 1115 |
+
Generalized coordinate corresponding to the rotation angle. The default
|
| 1116 |
+
value is ``dynamicsymbols(f'q0_{joint.name}')``.
|
| 1117 |
+
translation_coordinate : dynamicsymbol, optional
|
| 1118 |
+
Generalized coordinate corresponding to the translation distance. The
|
| 1119 |
+
default value is ``dynamicsymbols(f'q1_{joint.name}')``.
|
| 1120 |
+
rotation_speed : dynamicsymbol, optional
|
| 1121 |
+
Generalized speed corresponding to the angular velocity. The default
|
| 1122 |
+
value is ``dynamicsymbols(f'u0_{joint.name}')``.
|
| 1123 |
+
translation_speed : dynamicsymbol, optional
|
| 1124 |
+
Generalized speed corresponding to the translation velocity. The default
|
| 1125 |
+
value is ``dynamicsymbols(f'u1_{joint.name}')``.
|
| 1126 |
+
parent_point : Point or Vector, optional
|
| 1127 |
+
Attachment point where the joint is fixed to the parent body. If a
|
| 1128 |
+
vector is provided, then the attachment point is computed by adding the
|
| 1129 |
+
vector to the body's mass center. The default value is the parent's mass
|
| 1130 |
+
center.
|
| 1131 |
+
child_point : Point or Vector, optional
|
| 1132 |
+
Attachment point where the joint is fixed to the child body. If a
|
| 1133 |
+
vector is provided, then the attachment point is computed by adding the
|
| 1134 |
+
vector to the body's mass center. The default value is the child's mass
|
| 1135 |
+
center.
|
| 1136 |
+
parent_interframe : ReferenceFrame, optional
|
| 1137 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 1138 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 1139 |
+
is created which aligns its X axis with the given vector. The default
|
| 1140 |
+
value is the parent's own frame.
|
| 1141 |
+
child_interframe : ReferenceFrame, optional
|
| 1142 |
+
Intermediate frame of the child body with respect to which the joint
|
| 1143 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 1144 |
+
is created which aligns its X axis with the given vector. The default
|
| 1145 |
+
value is the child's own frame.
|
| 1146 |
+
joint_axis : Vector, optional
|
| 1147 |
+
The rotation as well as translation axis. Note that the components of
|
| 1148 |
+
this axis are the same in the parent_interframe and child_interframe.
|
| 1149 |
+
|
| 1150 |
+
Attributes
|
| 1151 |
+
==========
|
| 1152 |
+
|
| 1153 |
+
name : string
|
| 1154 |
+
The joint's name.
|
| 1155 |
+
parent : Particle or RigidBody or Body
|
| 1156 |
+
The joint's parent body.
|
| 1157 |
+
child : Particle or RigidBody or Body
|
| 1158 |
+
The joint's child body.
|
| 1159 |
+
rotation_coordinate : dynamicsymbol
|
| 1160 |
+
Generalized coordinate corresponding to the rotation angle.
|
| 1161 |
+
translation_coordinate : dynamicsymbol
|
| 1162 |
+
Generalized coordinate corresponding to the translation distance.
|
| 1163 |
+
rotation_speed : dynamicsymbol
|
| 1164 |
+
Generalized speed corresponding to the angular velocity.
|
| 1165 |
+
translation_speed : dynamicsymbol
|
| 1166 |
+
Generalized speed corresponding to the translation velocity.
|
| 1167 |
+
coordinates : Matrix
|
| 1168 |
+
Matrix of the joint's generalized coordinates.
|
| 1169 |
+
speeds : Matrix
|
| 1170 |
+
Matrix of the joint's generalized speeds.
|
| 1171 |
+
parent_point : Point
|
| 1172 |
+
Attachment point where the joint is fixed to the parent body.
|
| 1173 |
+
child_point : Point
|
| 1174 |
+
Attachment point where the joint is fixed to the child body.
|
| 1175 |
+
parent_interframe : ReferenceFrame
|
| 1176 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 1177 |
+
transformation is formulated.
|
| 1178 |
+
child_interframe : ReferenceFrame
|
| 1179 |
+
Intermediate frame of the child body with respect to which the joint
|
| 1180 |
+
transformation is formulated.
|
| 1181 |
+
kdes : Matrix
|
| 1182 |
+
Kinematical differential equations of the joint.
|
| 1183 |
+
joint_axis : Vector
|
| 1184 |
+
The axis of rotation and translation.
|
| 1185 |
+
|
| 1186 |
+
Examples
|
| 1187 |
+
=========
|
| 1188 |
+
|
| 1189 |
+
A single cylindrical joint is created between two bodies and has the
|
| 1190 |
+
following basic attributes:
|
| 1191 |
+
|
| 1192 |
+
>>> from sympy.physics.mechanics import RigidBody, CylindricalJoint
|
| 1193 |
+
>>> parent = RigidBody('P')
|
| 1194 |
+
>>> parent
|
| 1195 |
+
P
|
| 1196 |
+
>>> child = RigidBody('C')
|
| 1197 |
+
>>> child
|
| 1198 |
+
C
|
| 1199 |
+
>>> joint = CylindricalJoint('PC', parent, child)
|
| 1200 |
+
>>> joint
|
| 1201 |
+
CylindricalJoint: PC parent: P child: C
|
| 1202 |
+
>>> joint.name
|
| 1203 |
+
'PC'
|
| 1204 |
+
>>> joint.parent
|
| 1205 |
+
P
|
| 1206 |
+
>>> joint.child
|
| 1207 |
+
C
|
| 1208 |
+
>>> joint.parent_point
|
| 1209 |
+
P_masscenter
|
| 1210 |
+
>>> joint.child_point
|
| 1211 |
+
C_masscenter
|
| 1212 |
+
>>> joint.parent_axis
|
| 1213 |
+
P_frame.x
|
| 1214 |
+
>>> joint.child_axis
|
| 1215 |
+
C_frame.x
|
| 1216 |
+
>>> joint.coordinates
|
| 1217 |
+
Matrix([
|
| 1218 |
+
[q0_PC(t)],
|
| 1219 |
+
[q1_PC(t)]])
|
| 1220 |
+
>>> joint.speeds
|
| 1221 |
+
Matrix([
|
| 1222 |
+
[u0_PC(t)],
|
| 1223 |
+
[u1_PC(t)]])
|
| 1224 |
+
>>> child.frame.ang_vel_in(parent.frame)
|
| 1225 |
+
u0_PC(t)*P_frame.x
|
| 1226 |
+
>>> child.frame.dcm(parent.frame)
|
| 1227 |
+
Matrix([
|
| 1228 |
+
[1, 0, 0],
|
| 1229 |
+
[0, cos(q0_PC(t)), sin(q0_PC(t))],
|
| 1230 |
+
[0, -sin(q0_PC(t)), cos(q0_PC(t))]])
|
| 1231 |
+
>>> joint.child_point.pos_from(joint.parent_point)
|
| 1232 |
+
q1_PC(t)*P_frame.x
|
| 1233 |
+
>>> child.masscenter.vel(parent.frame)
|
| 1234 |
+
u1_PC(t)*P_frame.x
|
| 1235 |
+
|
| 1236 |
+
To further demonstrate the use of the cylindrical joint, the kinematics of
|
| 1237 |
+
two cylindrical joints perpendicular to each other can be created as follows.
|
| 1238 |
+
|
| 1239 |
+
>>> from sympy import symbols
|
| 1240 |
+
>>> from sympy.physics.mechanics import RigidBody, CylindricalJoint
|
| 1241 |
+
>>> r, l, w = symbols('r l w')
|
| 1242 |
+
|
| 1243 |
+
First create bodies to represent the fixed floor with a fixed pole on it.
|
| 1244 |
+
The second body represents a freely moving tube around that pole. The third
|
| 1245 |
+
body represents a solid flag freely translating along and rotating around
|
| 1246 |
+
the Y axis of the tube.
|
| 1247 |
+
|
| 1248 |
+
>>> floor = RigidBody('floor')
|
| 1249 |
+
>>> tube = RigidBody('tube')
|
| 1250 |
+
>>> flag = RigidBody('flag')
|
| 1251 |
+
|
| 1252 |
+
The first joint will connect the first tube to the floor with it translating
|
| 1253 |
+
along and rotating around the Z axis of both bodies.
|
| 1254 |
+
|
| 1255 |
+
>>> floor_joint = CylindricalJoint('C1', floor, tube, joint_axis=floor.z)
|
| 1256 |
+
|
| 1257 |
+
The second joint will connect the tube perpendicular to the flag along the Y
|
| 1258 |
+
axis of both the tube and the flag, with the joint located at a distance
|
| 1259 |
+
``r`` from the tube's center of mass and a combination of the distances
|
| 1260 |
+
``l`` and ``w`` from the flag's center of mass.
|
| 1261 |
+
|
| 1262 |
+
>>> flag_joint = CylindricalJoint('C2', tube, flag,
|
| 1263 |
+
... parent_point=r * tube.y,
|
| 1264 |
+
... child_point=-w * flag.y + l * flag.z,
|
| 1265 |
+
... joint_axis=tube.y)
|
| 1266 |
+
|
| 1267 |
+
Once the joints are established the kinematics of the connected bodies can
|
| 1268 |
+
be accessed. First the direction cosine matrices of both the body and the
|
| 1269 |
+
flag relative to the floor are found:
|
| 1270 |
+
|
| 1271 |
+
>>> tube.frame.dcm(floor.frame)
|
| 1272 |
+
Matrix([
|
| 1273 |
+
[ cos(q0_C1(t)), sin(q0_C1(t)), 0],
|
| 1274 |
+
[-sin(q0_C1(t)), cos(q0_C1(t)), 0],
|
| 1275 |
+
[ 0, 0, 1]])
|
| 1276 |
+
>>> flag.frame.dcm(floor.frame)
|
| 1277 |
+
Matrix([
|
| 1278 |
+
[cos(q0_C1(t))*cos(q0_C2(t)), sin(q0_C1(t))*cos(q0_C2(t)), -sin(q0_C2(t))],
|
| 1279 |
+
[ -sin(q0_C1(t)), cos(q0_C1(t)), 0],
|
| 1280 |
+
[sin(q0_C2(t))*cos(q0_C1(t)), sin(q0_C1(t))*sin(q0_C2(t)), cos(q0_C2(t))]])
|
| 1281 |
+
|
| 1282 |
+
The position of the flag's center of mass is found with:
|
| 1283 |
+
|
| 1284 |
+
>>> flag.masscenter.pos_from(floor.masscenter)
|
| 1285 |
+
q1_C1(t)*floor_frame.z + (r + q1_C2(t))*tube_frame.y + w*flag_frame.y - l*flag_frame.z
|
| 1286 |
+
|
| 1287 |
+
The angular velocities of the two tubes can be computed with respect to the
|
| 1288 |
+
floor.
|
| 1289 |
+
|
| 1290 |
+
>>> tube.frame.ang_vel_in(floor.frame)
|
| 1291 |
+
u0_C1(t)*floor_frame.z
|
| 1292 |
+
>>> flag.frame.ang_vel_in(floor.frame)
|
| 1293 |
+
u0_C1(t)*floor_frame.z + u0_C2(t)*tube_frame.y
|
| 1294 |
+
|
| 1295 |
+
Finally, the linear velocities of the two tube centers of mass can be
|
| 1296 |
+
computed with respect to the floor, while expressed in the tube's frame.
|
| 1297 |
+
|
| 1298 |
+
>>> tube.masscenter.vel(floor.frame).to_matrix(tube.frame)
|
| 1299 |
+
Matrix([
|
| 1300 |
+
[ 0],
|
| 1301 |
+
[ 0],
|
| 1302 |
+
[u1_C1(t)]])
|
| 1303 |
+
>>> flag.masscenter.vel(floor.frame).to_matrix(tube.frame).simplify()
|
| 1304 |
+
Matrix([
|
| 1305 |
+
[-l*u0_C2(t)*cos(q0_C2(t)) - r*u0_C1(t) - w*u0_C1(t) - q1_C2(t)*u0_C1(t)],
|
| 1306 |
+
[ -l*u0_C1(t)*sin(q0_C2(t)) + Derivative(q1_C2(t), t)],
|
| 1307 |
+
[ l*u0_C2(t)*sin(q0_C2(t)) + u1_C1(t)]])
|
| 1308 |
+
|
| 1309 |
+
"""
|
| 1310 |
+
|
| 1311 |
+
def __init__(self, name, parent, child, rotation_coordinate=None,
|
| 1312 |
+
translation_coordinate=None, rotation_speed=None,
|
| 1313 |
+
translation_speed=None, parent_point=None, child_point=None,
|
| 1314 |
+
parent_interframe=None, child_interframe=None,
|
| 1315 |
+
joint_axis=None):
|
| 1316 |
+
self._joint_axis = joint_axis
|
| 1317 |
+
coordinates = (rotation_coordinate, translation_coordinate)
|
| 1318 |
+
speeds = (rotation_speed, translation_speed)
|
| 1319 |
+
super().__init__(name, parent, child, coordinates, speeds,
|
| 1320 |
+
parent_point, child_point,
|
| 1321 |
+
parent_interframe=parent_interframe,
|
| 1322 |
+
child_interframe=child_interframe)
|
| 1323 |
+
|
| 1324 |
+
def __str__(self):
|
| 1325 |
+
return (f'CylindricalJoint: {self.name} parent: {self.parent} '
|
| 1326 |
+
f'child: {self.child}')
|
| 1327 |
+
|
| 1328 |
+
@property
|
| 1329 |
+
def joint_axis(self):
|
| 1330 |
+
"""Axis about and along which the rotation and translation occurs."""
|
| 1331 |
+
return self._joint_axis
|
| 1332 |
+
|
| 1333 |
+
@property
|
| 1334 |
+
def rotation_coordinate(self):
|
| 1335 |
+
"""Generalized coordinate corresponding to the rotation angle."""
|
| 1336 |
+
return self.coordinates[0]
|
| 1337 |
+
|
| 1338 |
+
@property
|
| 1339 |
+
def translation_coordinate(self):
|
| 1340 |
+
"""Generalized coordinate corresponding to the translation distance."""
|
| 1341 |
+
return self.coordinates[1]
|
| 1342 |
+
|
| 1343 |
+
@property
|
| 1344 |
+
def rotation_speed(self):
|
| 1345 |
+
"""Generalized speed corresponding to the angular velocity."""
|
| 1346 |
+
return self.speeds[0]
|
| 1347 |
+
|
| 1348 |
+
@property
|
| 1349 |
+
def translation_speed(self):
|
| 1350 |
+
"""Generalized speed corresponding to the translation velocity."""
|
| 1351 |
+
return self.speeds[1]
|
| 1352 |
+
|
| 1353 |
+
def _generate_coordinates(self, coordinates):
|
| 1354 |
+
return self._fill_coordinate_list(coordinates, 2, 'q')
|
| 1355 |
+
|
| 1356 |
+
def _generate_speeds(self, speeds):
|
| 1357 |
+
return self._fill_coordinate_list(speeds, 2, 'u')
|
| 1358 |
+
|
| 1359 |
+
def _orient_frames(self):
|
| 1360 |
+
self._joint_axis = self._axis(self.joint_axis, self.parent_interframe)
|
| 1361 |
+
self.child_interframe.orient_axis(
|
| 1362 |
+
self.parent_interframe, self.joint_axis, self.rotation_coordinate)
|
| 1363 |
+
|
| 1364 |
+
def _set_angular_velocity(self):
|
| 1365 |
+
self.child_interframe.set_ang_vel(
|
| 1366 |
+
self.parent_interframe,
|
| 1367 |
+
self.rotation_speed * self.joint_axis.normalize())
|
| 1368 |
+
|
| 1369 |
+
def _set_linear_velocity(self):
|
| 1370 |
+
self.child_point.set_pos(
|
| 1371 |
+
self.parent_point,
|
| 1372 |
+
self.translation_coordinate * self.joint_axis.normalize())
|
| 1373 |
+
self.parent_point.set_vel(self._parent_frame, 0)
|
| 1374 |
+
self.child_point.set_vel(self._child_frame, 0)
|
| 1375 |
+
self.child_point.set_vel(
|
| 1376 |
+
self._parent_frame,
|
| 1377 |
+
self.translation_speed * self.joint_axis.normalize())
|
| 1378 |
+
self.child.masscenter.v2pt_theory(self.child_point, self._parent_frame,
|
| 1379 |
+
self.child_interframe)
|
| 1380 |
+
|
| 1381 |
+
|
| 1382 |
+
class PlanarJoint(Joint):
|
| 1383 |
+
"""Planar Joint.
|
| 1384 |
+
|
| 1385 |
+
.. raw:: html
|
| 1386 |
+
:file: ../../../doc/src/modules/physics/mechanics/api/PlanarJoint.svg
|
| 1387 |
+
|
| 1388 |
+
Explanation
|
| 1389 |
+
===========
|
| 1390 |
+
|
| 1391 |
+
A planar joint is defined such that the child body translates over a fixed
|
| 1392 |
+
plane of the parent body as well as rotate about the rotation axis, which
|
| 1393 |
+
is perpendicular to that plane. The origin of this plane is the
|
| 1394 |
+
``parent_point`` and the plane is spanned by two nonparallel planar vectors.
|
| 1395 |
+
The location of the ``child_point`` is based on the planar vectors
|
| 1396 |
+
($\\vec{v}_1$, $\\vec{v}_2$) and generalized coordinates ($q_1$, $q_2$),
|
| 1397 |
+
i.e. $\\vec{r} = q_1 \\hat{v}_1 + q_2 \\hat{v}_2$. The direction cosine
|
| 1398 |
+
matrix between the ``child_interframe`` and ``parent_interframe`` is formed
|
| 1399 |
+
using a simple rotation ($q_0$) about the rotation axis.
|
| 1400 |
+
|
| 1401 |
+
In order to simplify the definition of the ``PlanarJoint``, the
|
| 1402 |
+
``rotation_axis`` and ``planar_vectors`` are set to be the unit vectors of
|
| 1403 |
+
the ``parent_interframe`` according to the table below. This ensures that
|
| 1404 |
+
you can only define these vectors by creating a separate frame and supplying
|
| 1405 |
+
that as the interframe. If you however would only like to supply the normals
|
| 1406 |
+
of the plane with respect to the parent and child bodies, then you can also
|
| 1407 |
+
supply those to the ``parent_interframe`` and ``child_interframe``
|
| 1408 |
+
arguments. An example of both of these cases is in the examples section
|
| 1409 |
+
below and the page on the joints framework provides a more detailed
|
| 1410 |
+
explanation of the intermediate frames.
|
| 1411 |
+
|
| 1412 |
+
.. list-table::
|
| 1413 |
+
|
| 1414 |
+
* - ``rotation_axis``
|
| 1415 |
+
- ``parent_interframe.x``
|
| 1416 |
+
* - ``planar_vectors[0]``
|
| 1417 |
+
- ``parent_interframe.y``
|
| 1418 |
+
* - ``planar_vectors[1]``
|
| 1419 |
+
- ``parent_interframe.z``
|
| 1420 |
+
|
| 1421 |
+
Parameters
|
| 1422 |
+
==========
|
| 1423 |
+
|
| 1424 |
+
name : string
|
| 1425 |
+
A unique name for the joint.
|
| 1426 |
+
parent : Particle or RigidBody or Body
|
| 1427 |
+
The parent body of joint.
|
| 1428 |
+
child : Particle or RigidBody or Body
|
| 1429 |
+
The child body of joint.
|
| 1430 |
+
rotation_coordinate : dynamicsymbol, optional
|
| 1431 |
+
Generalized coordinate corresponding to the rotation angle. The default
|
| 1432 |
+
value is ``dynamicsymbols(f'q0_{joint.name}')``.
|
| 1433 |
+
planar_coordinates : iterable of dynamicsymbols, optional
|
| 1434 |
+
Two generalized coordinates used for the planar translation. The default
|
| 1435 |
+
value is ``dynamicsymbols(f'q1_{joint.name} q2_{joint.name}')``.
|
| 1436 |
+
rotation_speed : dynamicsymbol, optional
|
| 1437 |
+
Generalized speed corresponding to the angular velocity. The default
|
| 1438 |
+
value is ``dynamicsymbols(f'u0_{joint.name}')``.
|
| 1439 |
+
planar_speeds : dynamicsymbols, optional
|
| 1440 |
+
Two generalized speeds used for the planar translation velocity. The
|
| 1441 |
+
default value is ``dynamicsymbols(f'u1_{joint.name} u2_{joint.name}')``.
|
| 1442 |
+
parent_point : Point or Vector, optional
|
| 1443 |
+
Attachment point where the joint is fixed to the parent body. If a
|
| 1444 |
+
vector is provided, then the attachment point is computed by adding the
|
| 1445 |
+
vector to the body's mass center. The default value is the parent's mass
|
| 1446 |
+
center.
|
| 1447 |
+
child_point : Point or Vector, optional
|
| 1448 |
+
Attachment point where the joint is fixed to the child body. If a
|
| 1449 |
+
vector is provided, then the attachment point is computed by adding the
|
| 1450 |
+
vector to the body's mass center. The default value is the child's mass
|
| 1451 |
+
center.
|
| 1452 |
+
parent_interframe : ReferenceFrame, optional
|
| 1453 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 1454 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 1455 |
+
is created which aligns its X axis with the given vector. The default
|
| 1456 |
+
value is the parent's own frame.
|
| 1457 |
+
child_interframe : ReferenceFrame, optional
|
| 1458 |
+
Intermediate frame of the child body with respect to which the joint
|
| 1459 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 1460 |
+
is created which aligns its X axis with the given vector. The default
|
| 1461 |
+
value is the child's own frame.
|
| 1462 |
+
|
| 1463 |
+
Attributes
|
| 1464 |
+
==========
|
| 1465 |
+
|
| 1466 |
+
name : string
|
| 1467 |
+
The joint's name.
|
| 1468 |
+
parent : Particle or RigidBody or Body
|
| 1469 |
+
The joint's parent body.
|
| 1470 |
+
child : Particle or RigidBody or Body
|
| 1471 |
+
The joint's child body.
|
| 1472 |
+
rotation_coordinate : dynamicsymbol
|
| 1473 |
+
Generalized coordinate corresponding to the rotation angle.
|
| 1474 |
+
planar_coordinates : Matrix
|
| 1475 |
+
Two generalized coordinates used for the planar translation.
|
| 1476 |
+
rotation_speed : dynamicsymbol
|
| 1477 |
+
Generalized speed corresponding to the angular velocity.
|
| 1478 |
+
planar_speeds : Matrix
|
| 1479 |
+
Two generalized speeds used for the planar translation velocity.
|
| 1480 |
+
coordinates : Matrix
|
| 1481 |
+
Matrix of the joint's generalized coordinates.
|
| 1482 |
+
speeds : Matrix
|
| 1483 |
+
Matrix of the joint's generalized speeds.
|
| 1484 |
+
parent_point : Point
|
| 1485 |
+
Attachment point where the joint is fixed to the parent body.
|
| 1486 |
+
child_point : Point
|
| 1487 |
+
Attachment point where the joint is fixed to the child body.
|
| 1488 |
+
parent_interframe : ReferenceFrame
|
| 1489 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 1490 |
+
transformation is formulated.
|
| 1491 |
+
child_interframe : ReferenceFrame
|
| 1492 |
+
Intermediate frame of the child body with respect to which the joint
|
| 1493 |
+
transformation is formulated.
|
| 1494 |
+
kdes : Matrix
|
| 1495 |
+
Kinematical differential equations of the joint.
|
| 1496 |
+
rotation_axis : Vector
|
| 1497 |
+
The axis about which the rotation occurs.
|
| 1498 |
+
planar_vectors : list
|
| 1499 |
+
The vectors that describe the planar translation directions.
|
| 1500 |
+
|
| 1501 |
+
Examples
|
| 1502 |
+
=========
|
| 1503 |
+
|
| 1504 |
+
A single planar joint is created between two bodies and has the following
|
| 1505 |
+
basic attributes:
|
| 1506 |
+
|
| 1507 |
+
>>> from sympy.physics.mechanics import RigidBody, PlanarJoint
|
| 1508 |
+
>>> parent = RigidBody('P')
|
| 1509 |
+
>>> parent
|
| 1510 |
+
P
|
| 1511 |
+
>>> child = RigidBody('C')
|
| 1512 |
+
>>> child
|
| 1513 |
+
C
|
| 1514 |
+
>>> joint = PlanarJoint('PC', parent, child)
|
| 1515 |
+
>>> joint
|
| 1516 |
+
PlanarJoint: PC parent: P child: C
|
| 1517 |
+
>>> joint.name
|
| 1518 |
+
'PC'
|
| 1519 |
+
>>> joint.parent
|
| 1520 |
+
P
|
| 1521 |
+
>>> joint.child
|
| 1522 |
+
C
|
| 1523 |
+
>>> joint.parent_point
|
| 1524 |
+
P_masscenter
|
| 1525 |
+
>>> joint.child_point
|
| 1526 |
+
C_masscenter
|
| 1527 |
+
>>> joint.rotation_axis
|
| 1528 |
+
P_frame.x
|
| 1529 |
+
>>> joint.planar_vectors
|
| 1530 |
+
[P_frame.y, P_frame.z]
|
| 1531 |
+
>>> joint.rotation_coordinate
|
| 1532 |
+
q0_PC(t)
|
| 1533 |
+
>>> joint.planar_coordinates
|
| 1534 |
+
Matrix([
|
| 1535 |
+
[q1_PC(t)],
|
| 1536 |
+
[q2_PC(t)]])
|
| 1537 |
+
>>> joint.coordinates
|
| 1538 |
+
Matrix([
|
| 1539 |
+
[q0_PC(t)],
|
| 1540 |
+
[q1_PC(t)],
|
| 1541 |
+
[q2_PC(t)]])
|
| 1542 |
+
>>> joint.rotation_speed
|
| 1543 |
+
u0_PC(t)
|
| 1544 |
+
>>> joint.planar_speeds
|
| 1545 |
+
Matrix([
|
| 1546 |
+
[u1_PC(t)],
|
| 1547 |
+
[u2_PC(t)]])
|
| 1548 |
+
>>> joint.speeds
|
| 1549 |
+
Matrix([
|
| 1550 |
+
[u0_PC(t)],
|
| 1551 |
+
[u1_PC(t)],
|
| 1552 |
+
[u2_PC(t)]])
|
| 1553 |
+
>>> child.frame.ang_vel_in(parent.frame)
|
| 1554 |
+
u0_PC(t)*P_frame.x
|
| 1555 |
+
>>> child.frame.dcm(parent.frame)
|
| 1556 |
+
Matrix([
|
| 1557 |
+
[1, 0, 0],
|
| 1558 |
+
[0, cos(q0_PC(t)), sin(q0_PC(t))],
|
| 1559 |
+
[0, -sin(q0_PC(t)), cos(q0_PC(t))]])
|
| 1560 |
+
>>> joint.child_point.pos_from(joint.parent_point)
|
| 1561 |
+
q1_PC(t)*P_frame.y + q2_PC(t)*P_frame.z
|
| 1562 |
+
>>> child.masscenter.vel(parent.frame)
|
| 1563 |
+
u1_PC(t)*P_frame.y + u2_PC(t)*P_frame.z
|
| 1564 |
+
|
| 1565 |
+
To further demonstrate the use of the planar joint, the kinematics of a
|
| 1566 |
+
block sliding on a slope, can be created as follows.
|
| 1567 |
+
|
| 1568 |
+
>>> from sympy import symbols
|
| 1569 |
+
>>> from sympy.physics.mechanics import PlanarJoint, RigidBody, ReferenceFrame
|
| 1570 |
+
>>> a, d, h = symbols('a d h')
|
| 1571 |
+
|
| 1572 |
+
First create bodies to represent the slope and the block.
|
| 1573 |
+
|
| 1574 |
+
>>> ground = RigidBody('G')
|
| 1575 |
+
>>> block = RigidBody('B')
|
| 1576 |
+
|
| 1577 |
+
To define the slope you can either define the plane by specifying the
|
| 1578 |
+
``planar_vectors`` or/and the ``rotation_axis``. However it is advisable to
|
| 1579 |
+
create a rotated intermediate frame, so that the ``parent_vectors`` and
|
| 1580 |
+
``rotation_axis`` will be the unit vectors of this intermediate frame.
|
| 1581 |
+
|
| 1582 |
+
>>> slope = ReferenceFrame('A')
|
| 1583 |
+
>>> slope.orient_axis(ground.frame, ground.y, a)
|
| 1584 |
+
|
| 1585 |
+
The planar joint can be created using these bodies and intermediate frame.
|
| 1586 |
+
We can specify the origin of the slope to be ``d`` above the slope's center
|
| 1587 |
+
of mass and the block's center of mass to be a distance ``h`` above the
|
| 1588 |
+
slope's surface. Note that we can specify the normal of the plane using the
|
| 1589 |
+
rotation axis argument.
|
| 1590 |
+
|
| 1591 |
+
>>> joint = PlanarJoint('PC', ground, block, parent_point=d * ground.x,
|
| 1592 |
+
... child_point=-h * block.x, parent_interframe=slope)
|
| 1593 |
+
|
| 1594 |
+
Once the joint is established the kinematics of the bodies can be accessed.
|
| 1595 |
+
First the ``rotation_axis``, which is normal to the plane and the
|
| 1596 |
+
``plane_vectors``, can be found.
|
| 1597 |
+
|
| 1598 |
+
>>> joint.rotation_axis
|
| 1599 |
+
A.x
|
| 1600 |
+
>>> joint.planar_vectors
|
| 1601 |
+
[A.y, A.z]
|
| 1602 |
+
|
| 1603 |
+
The direction cosine matrix of the block with respect to the ground can be
|
| 1604 |
+
found with:
|
| 1605 |
+
|
| 1606 |
+
>>> block.frame.dcm(ground.frame)
|
| 1607 |
+
Matrix([
|
| 1608 |
+
[ cos(a), 0, -sin(a)],
|
| 1609 |
+
[sin(a)*sin(q0_PC(t)), cos(q0_PC(t)), sin(q0_PC(t))*cos(a)],
|
| 1610 |
+
[sin(a)*cos(q0_PC(t)), -sin(q0_PC(t)), cos(a)*cos(q0_PC(t))]])
|
| 1611 |
+
|
| 1612 |
+
The angular velocity of the block can be computed with respect to the
|
| 1613 |
+
ground.
|
| 1614 |
+
|
| 1615 |
+
>>> block.frame.ang_vel_in(ground.frame)
|
| 1616 |
+
u0_PC(t)*A.x
|
| 1617 |
+
|
| 1618 |
+
The position of the block's center of mass can be found with:
|
| 1619 |
+
|
| 1620 |
+
>>> block.masscenter.pos_from(ground.masscenter)
|
| 1621 |
+
d*G_frame.x + h*B_frame.x + q1_PC(t)*A.y + q2_PC(t)*A.z
|
| 1622 |
+
|
| 1623 |
+
Finally, the linear velocity of the block's center of mass can be
|
| 1624 |
+
computed with respect to the ground.
|
| 1625 |
+
|
| 1626 |
+
>>> block.masscenter.vel(ground.frame)
|
| 1627 |
+
u1_PC(t)*A.y + u2_PC(t)*A.z
|
| 1628 |
+
|
| 1629 |
+
In some cases it could be your preference to only define the normals of the
|
| 1630 |
+
plane with respect to both bodies. This can most easily be done by supplying
|
| 1631 |
+
vectors to the ``interframe`` arguments. What will happen in this case is
|
| 1632 |
+
that an interframe will be created with its ``x`` axis aligned with the
|
| 1633 |
+
provided vector. For a further explanation of how this is done see the notes
|
| 1634 |
+
of the ``Joint`` class. In the code below, the above example (with the block
|
| 1635 |
+
on the slope) is recreated by supplying vectors to the interframe arguments.
|
| 1636 |
+
Note that the previously described option is however more computationally
|
| 1637 |
+
efficient, because the algorithm now has to compute the rotation angle
|
| 1638 |
+
between the provided vector and the 'x' axis.
|
| 1639 |
+
|
| 1640 |
+
>>> from sympy import symbols, cos, sin
|
| 1641 |
+
>>> from sympy.physics.mechanics import PlanarJoint, RigidBody
|
| 1642 |
+
>>> a, d, h = symbols('a d h')
|
| 1643 |
+
>>> ground = RigidBody('G')
|
| 1644 |
+
>>> block = RigidBody('B')
|
| 1645 |
+
>>> joint = PlanarJoint(
|
| 1646 |
+
... 'PC', ground, block, parent_point=d * ground.x,
|
| 1647 |
+
... child_point=-h * block.x, child_interframe=block.x,
|
| 1648 |
+
... parent_interframe=cos(a) * ground.x + sin(a) * ground.z)
|
| 1649 |
+
>>> block.frame.dcm(ground.frame).simplify()
|
| 1650 |
+
Matrix([
|
| 1651 |
+
[ cos(a), 0, sin(a)],
|
| 1652 |
+
[-sin(a)*sin(q0_PC(t)), cos(q0_PC(t)), sin(q0_PC(t))*cos(a)],
|
| 1653 |
+
[-sin(a)*cos(q0_PC(t)), -sin(q0_PC(t)), cos(a)*cos(q0_PC(t))]])
|
| 1654 |
+
|
| 1655 |
+
"""
|
| 1656 |
+
|
| 1657 |
+
def __init__(self, name, parent, child, rotation_coordinate=None,
|
| 1658 |
+
planar_coordinates=None, rotation_speed=None,
|
| 1659 |
+
planar_speeds=None, parent_point=None, child_point=None,
|
| 1660 |
+
parent_interframe=None, child_interframe=None):
|
| 1661 |
+
# A ready to merge implementation of setting the planar_vectors and
|
| 1662 |
+
# rotation_axis was added and removed in PR #24046
|
| 1663 |
+
coordinates = (rotation_coordinate, planar_coordinates)
|
| 1664 |
+
speeds = (rotation_speed, planar_speeds)
|
| 1665 |
+
super().__init__(name, parent, child, coordinates, speeds,
|
| 1666 |
+
parent_point, child_point,
|
| 1667 |
+
parent_interframe=parent_interframe,
|
| 1668 |
+
child_interframe=child_interframe)
|
| 1669 |
+
|
| 1670 |
+
def __str__(self):
|
| 1671 |
+
return (f'PlanarJoint: {self.name} parent: {self.parent} '
|
| 1672 |
+
f'child: {self.child}')
|
| 1673 |
+
|
| 1674 |
+
@property
|
| 1675 |
+
def rotation_coordinate(self):
|
| 1676 |
+
"""Generalized coordinate corresponding to the rotation angle."""
|
| 1677 |
+
return self.coordinates[0]
|
| 1678 |
+
|
| 1679 |
+
@property
|
| 1680 |
+
def planar_coordinates(self):
|
| 1681 |
+
"""Two generalized coordinates used for the planar translation."""
|
| 1682 |
+
return self.coordinates[1:, 0]
|
| 1683 |
+
|
| 1684 |
+
@property
|
| 1685 |
+
def rotation_speed(self):
|
| 1686 |
+
"""Generalized speed corresponding to the angular velocity."""
|
| 1687 |
+
return self.speeds[0]
|
| 1688 |
+
|
| 1689 |
+
@property
|
| 1690 |
+
def planar_speeds(self):
|
| 1691 |
+
"""Two generalized speeds used for the planar translation velocity."""
|
| 1692 |
+
return self.speeds[1:, 0]
|
| 1693 |
+
|
| 1694 |
+
@property
|
| 1695 |
+
def rotation_axis(self):
|
| 1696 |
+
"""The axis about which the rotation occurs."""
|
| 1697 |
+
return self.parent_interframe.x
|
| 1698 |
+
|
| 1699 |
+
@property
|
| 1700 |
+
def planar_vectors(self):
|
| 1701 |
+
"""The vectors that describe the planar translation directions."""
|
| 1702 |
+
return [self.parent_interframe.y, self.parent_interframe.z]
|
| 1703 |
+
|
| 1704 |
+
def _generate_coordinates(self, coordinates):
|
| 1705 |
+
rotation_speed = self._fill_coordinate_list(coordinates[0], 1, 'q',
|
| 1706 |
+
number_single=True)
|
| 1707 |
+
planar_speeds = self._fill_coordinate_list(coordinates[1], 2, 'q', 1)
|
| 1708 |
+
return rotation_speed.col_join(planar_speeds)
|
| 1709 |
+
|
| 1710 |
+
def _generate_speeds(self, speeds):
|
| 1711 |
+
rotation_speed = self._fill_coordinate_list(speeds[0], 1, 'u',
|
| 1712 |
+
number_single=True)
|
| 1713 |
+
planar_speeds = self._fill_coordinate_list(speeds[1], 2, 'u', 1)
|
| 1714 |
+
return rotation_speed.col_join(planar_speeds)
|
| 1715 |
+
|
| 1716 |
+
def _orient_frames(self):
|
| 1717 |
+
self.child_interframe.orient_axis(
|
| 1718 |
+
self.parent_interframe, self.rotation_axis,
|
| 1719 |
+
self.rotation_coordinate)
|
| 1720 |
+
|
| 1721 |
+
def _set_angular_velocity(self):
|
| 1722 |
+
self.child_interframe.set_ang_vel(
|
| 1723 |
+
self.parent_interframe,
|
| 1724 |
+
self.rotation_speed * self.rotation_axis)
|
| 1725 |
+
|
| 1726 |
+
def _set_linear_velocity(self):
|
| 1727 |
+
self.child_point.set_pos(
|
| 1728 |
+
self.parent_point,
|
| 1729 |
+
self.planar_coordinates[0] * self.planar_vectors[0] +
|
| 1730 |
+
self.planar_coordinates[1] * self.planar_vectors[1])
|
| 1731 |
+
self.parent_point.set_vel(self.parent_interframe, 0)
|
| 1732 |
+
self.child_point.set_vel(self.child_interframe, 0)
|
| 1733 |
+
self.child_point.set_vel(
|
| 1734 |
+
self._parent_frame, self.planar_speeds[0] * self.planar_vectors[0] +
|
| 1735 |
+
self.planar_speeds[1] * self.planar_vectors[1])
|
| 1736 |
+
self.child.masscenter.v2pt_theory(self.child_point, self._parent_frame,
|
| 1737 |
+
self._child_frame)
|
| 1738 |
+
|
| 1739 |
+
|
| 1740 |
+
class SphericalJoint(Joint):
|
| 1741 |
+
"""Spherical (Ball-and-Socket) Joint.
|
| 1742 |
+
|
| 1743 |
+
.. image:: SphericalJoint.svg
|
| 1744 |
+
:align: center
|
| 1745 |
+
:width: 600
|
| 1746 |
+
|
| 1747 |
+
Explanation
|
| 1748 |
+
===========
|
| 1749 |
+
|
| 1750 |
+
A spherical joint is defined such that the child body is free to rotate in
|
| 1751 |
+
any direction, without allowing a translation of the ``child_point``. As can
|
| 1752 |
+
also be seen in the image, the ``parent_point`` and ``child_point`` are
|
| 1753 |
+
fixed on top of each other, i.e. the ``joint_point``. This rotation is
|
| 1754 |
+
defined using the :func:`parent_interframe.orient(child_interframe,
|
| 1755 |
+
rot_type, amounts, rot_order)
|
| 1756 |
+
<sympy.physics.vector.frame.ReferenceFrame.orient>` method. The default
|
| 1757 |
+
rotation consists of three relative rotations, i.e. body-fixed rotations.
|
| 1758 |
+
Based on the direction cosine matrix following from these rotations, the
|
| 1759 |
+
angular velocity is computed based on the generalized coordinates and
|
| 1760 |
+
generalized speeds.
|
| 1761 |
+
|
| 1762 |
+
Parameters
|
| 1763 |
+
==========
|
| 1764 |
+
|
| 1765 |
+
name : string
|
| 1766 |
+
A unique name for the joint.
|
| 1767 |
+
parent : Particle or RigidBody or Body
|
| 1768 |
+
The parent body of joint.
|
| 1769 |
+
child : Particle or RigidBody or Body
|
| 1770 |
+
The child body of joint.
|
| 1771 |
+
coordinates: iterable of dynamicsymbols, optional
|
| 1772 |
+
Generalized coordinates of the joint.
|
| 1773 |
+
speeds : iterable of dynamicsymbols, optional
|
| 1774 |
+
Generalized speeds of joint.
|
| 1775 |
+
parent_point : Point or Vector, optional
|
| 1776 |
+
Attachment point where the joint is fixed to the parent body. If a
|
| 1777 |
+
vector is provided, then the attachment point is computed by adding the
|
| 1778 |
+
vector to the body's mass center. The default value is the parent's mass
|
| 1779 |
+
center.
|
| 1780 |
+
child_point : Point or Vector, optional
|
| 1781 |
+
Attachment point where the joint is fixed to the child body. If a
|
| 1782 |
+
vector is provided, then the attachment point is computed by adding the
|
| 1783 |
+
vector to the body's mass center. The default value is the child's mass
|
| 1784 |
+
center.
|
| 1785 |
+
parent_interframe : ReferenceFrame, optional
|
| 1786 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 1787 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 1788 |
+
is created which aligns its X axis with the given vector. The default
|
| 1789 |
+
value is the parent's own frame.
|
| 1790 |
+
child_interframe : ReferenceFrame, optional
|
| 1791 |
+
Intermediate frame of the child body with respect to which the joint
|
| 1792 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 1793 |
+
is created which aligns its X axis with the given vector. The default
|
| 1794 |
+
value is the child's own frame.
|
| 1795 |
+
rot_type : str, optional
|
| 1796 |
+
The method used to generate the direction cosine matrix. Supported
|
| 1797 |
+
methods are:
|
| 1798 |
+
|
| 1799 |
+
- ``'Body'``: three successive rotations about new intermediate axes,
|
| 1800 |
+
also called "Euler and Tait-Bryan angles"
|
| 1801 |
+
- ``'Space'``: three successive rotations about the parent frames' unit
|
| 1802 |
+
vectors
|
| 1803 |
+
|
| 1804 |
+
The default method is ``'Body'``.
|
| 1805 |
+
amounts :
|
| 1806 |
+
Expressions defining the rotation angles or direction cosine matrix.
|
| 1807 |
+
These must match the ``rot_type``. See examples below for details. The
|
| 1808 |
+
input types are:
|
| 1809 |
+
|
| 1810 |
+
- ``'Body'``: 3-tuple of expressions, symbols, or functions
|
| 1811 |
+
- ``'Space'``: 3-tuple of expressions, symbols, or functions
|
| 1812 |
+
|
| 1813 |
+
The default amounts are the given ``coordinates``.
|
| 1814 |
+
rot_order : str or int, optional
|
| 1815 |
+
If applicable, the order of the successive of rotations. The string
|
| 1816 |
+
``'123'`` and integer ``123`` are equivalent, for example. Required for
|
| 1817 |
+
``'Body'`` and ``'Space'``. The default value is ``123``.
|
| 1818 |
+
|
| 1819 |
+
Attributes
|
| 1820 |
+
==========
|
| 1821 |
+
|
| 1822 |
+
name : string
|
| 1823 |
+
The joint's name.
|
| 1824 |
+
parent : Particle or RigidBody or Body
|
| 1825 |
+
The joint's parent body.
|
| 1826 |
+
child : Particle or RigidBody or Body
|
| 1827 |
+
The joint's child body.
|
| 1828 |
+
coordinates : Matrix
|
| 1829 |
+
Matrix of the joint's generalized coordinates.
|
| 1830 |
+
speeds : Matrix
|
| 1831 |
+
Matrix of the joint's generalized speeds.
|
| 1832 |
+
parent_point : Point
|
| 1833 |
+
Attachment point where the joint is fixed to the parent body.
|
| 1834 |
+
child_point : Point
|
| 1835 |
+
Attachment point where the joint is fixed to the child body.
|
| 1836 |
+
parent_interframe : ReferenceFrame
|
| 1837 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 1838 |
+
transformation is formulated.
|
| 1839 |
+
child_interframe : ReferenceFrame
|
| 1840 |
+
Intermediate frame of the child body with respect to which the joint
|
| 1841 |
+
transformation is formulated.
|
| 1842 |
+
kdes : Matrix
|
| 1843 |
+
Kinematical differential equations of the joint.
|
| 1844 |
+
|
| 1845 |
+
Examples
|
| 1846 |
+
=========
|
| 1847 |
+
|
| 1848 |
+
A single spherical joint is created from two bodies and has the following
|
| 1849 |
+
basic attributes:
|
| 1850 |
+
|
| 1851 |
+
>>> from sympy.physics.mechanics import RigidBody, SphericalJoint
|
| 1852 |
+
>>> parent = RigidBody('P')
|
| 1853 |
+
>>> parent
|
| 1854 |
+
P
|
| 1855 |
+
>>> child = RigidBody('C')
|
| 1856 |
+
>>> child
|
| 1857 |
+
C
|
| 1858 |
+
>>> joint = SphericalJoint('PC', parent, child)
|
| 1859 |
+
>>> joint
|
| 1860 |
+
SphericalJoint: PC parent: P child: C
|
| 1861 |
+
>>> joint.name
|
| 1862 |
+
'PC'
|
| 1863 |
+
>>> joint.parent
|
| 1864 |
+
P
|
| 1865 |
+
>>> joint.child
|
| 1866 |
+
C
|
| 1867 |
+
>>> joint.parent_point
|
| 1868 |
+
P_masscenter
|
| 1869 |
+
>>> joint.child_point
|
| 1870 |
+
C_masscenter
|
| 1871 |
+
>>> joint.parent_interframe
|
| 1872 |
+
P_frame
|
| 1873 |
+
>>> joint.child_interframe
|
| 1874 |
+
C_frame
|
| 1875 |
+
>>> joint.coordinates
|
| 1876 |
+
Matrix([
|
| 1877 |
+
[q0_PC(t)],
|
| 1878 |
+
[q1_PC(t)],
|
| 1879 |
+
[q2_PC(t)]])
|
| 1880 |
+
>>> joint.speeds
|
| 1881 |
+
Matrix([
|
| 1882 |
+
[u0_PC(t)],
|
| 1883 |
+
[u1_PC(t)],
|
| 1884 |
+
[u2_PC(t)]])
|
| 1885 |
+
>>> child.frame.ang_vel_in(parent.frame).to_matrix(child.frame)
|
| 1886 |
+
Matrix([
|
| 1887 |
+
[ u0_PC(t)*cos(q1_PC(t))*cos(q2_PC(t)) + u1_PC(t)*sin(q2_PC(t))],
|
| 1888 |
+
[-u0_PC(t)*sin(q2_PC(t))*cos(q1_PC(t)) + u1_PC(t)*cos(q2_PC(t))],
|
| 1889 |
+
[ u0_PC(t)*sin(q1_PC(t)) + u2_PC(t)]])
|
| 1890 |
+
>>> child.frame.x.to_matrix(parent.frame)
|
| 1891 |
+
Matrix([
|
| 1892 |
+
[ cos(q1_PC(t))*cos(q2_PC(t))],
|
| 1893 |
+
[sin(q0_PC(t))*sin(q1_PC(t))*cos(q2_PC(t)) + sin(q2_PC(t))*cos(q0_PC(t))],
|
| 1894 |
+
[sin(q0_PC(t))*sin(q2_PC(t)) - sin(q1_PC(t))*cos(q0_PC(t))*cos(q2_PC(t))]])
|
| 1895 |
+
>>> joint.child_point.pos_from(joint.parent_point)
|
| 1896 |
+
0
|
| 1897 |
+
|
| 1898 |
+
To further demonstrate the use of the spherical joint, the kinematics of a
|
| 1899 |
+
spherical joint with a ZXZ rotation can be created as follows.
|
| 1900 |
+
|
| 1901 |
+
>>> from sympy import symbols
|
| 1902 |
+
>>> from sympy.physics.mechanics import RigidBody, SphericalJoint
|
| 1903 |
+
>>> l1 = symbols('l1')
|
| 1904 |
+
|
| 1905 |
+
First create bodies to represent the fixed floor and a pendulum bob.
|
| 1906 |
+
|
| 1907 |
+
>>> floor = RigidBody('F')
|
| 1908 |
+
>>> bob = RigidBody('B')
|
| 1909 |
+
|
| 1910 |
+
The joint will connect the bob to the floor, with the joint located at a
|
| 1911 |
+
distance of ``l1`` from the child's center of mass and the rotation set to a
|
| 1912 |
+
body-fixed ZXZ rotation.
|
| 1913 |
+
|
| 1914 |
+
>>> joint = SphericalJoint('S', floor, bob, child_point=l1 * bob.y,
|
| 1915 |
+
... rot_type='body', rot_order='ZXZ')
|
| 1916 |
+
|
| 1917 |
+
Now that the joint is established, the kinematics of the connected body can
|
| 1918 |
+
be accessed.
|
| 1919 |
+
|
| 1920 |
+
The position of the bob's masscenter is found with:
|
| 1921 |
+
|
| 1922 |
+
>>> bob.masscenter.pos_from(floor.masscenter)
|
| 1923 |
+
- l1*B_frame.y
|
| 1924 |
+
|
| 1925 |
+
The angular velocities of the pendulum link can be computed with respect to
|
| 1926 |
+
the floor.
|
| 1927 |
+
|
| 1928 |
+
>>> bob.frame.ang_vel_in(floor.frame).to_matrix(
|
| 1929 |
+
... floor.frame).simplify()
|
| 1930 |
+
Matrix([
|
| 1931 |
+
[u1_S(t)*cos(q0_S(t)) + u2_S(t)*sin(q0_S(t))*sin(q1_S(t))],
|
| 1932 |
+
[u1_S(t)*sin(q0_S(t)) - u2_S(t)*sin(q1_S(t))*cos(q0_S(t))],
|
| 1933 |
+
[ u0_S(t) + u2_S(t)*cos(q1_S(t))]])
|
| 1934 |
+
|
| 1935 |
+
Finally, the linear velocity of the bob's center of mass can be computed.
|
| 1936 |
+
|
| 1937 |
+
>>> bob.masscenter.vel(floor.frame).to_matrix(bob.frame)
|
| 1938 |
+
Matrix([
|
| 1939 |
+
[ l1*(u0_S(t)*cos(q1_S(t)) + u2_S(t))],
|
| 1940 |
+
[ 0],
|
| 1941 |
+
[-l1*(u0_S(t)*sin(q1_S(t))*sin(q2_S(t)) + u1_S(t)*cos(q2_S(t)))]])
|
| 1942 |
+
|
| 1943 |
+
"""
|
| 1944 |
+
def __init__(self, name, parent, child, coordinates=None, speeds=None,
|
| 1945 |
+
parent_point=None, child_point=None, parent_interframe=None,
|
| 1946 |
+
child_interframe=None, rot_type='BODY', amounts=None,
|
| 1947 |
+
rot_order=123):
|
| 1948 |
+
self._rot_type = rot_type
|
| 1949 |
+
self._amounts = amounts
|
| 1950 |
+
self._rot_order = rot_order
|
| 1951 |
+
super().__init__(name, parent, child, coordinates, speeds,
|
| 1952 |
+
parent_point, child_point,
|
| 1953 |
+
parent_interframe=parent_interframe,
|
| 1954 |
+
child_interframe=child_interframe)
|
| 1955 |
+
|
| 1956 |
+
def __str__(self):
|
| 1957 |
+
return (f'SphericalJoint: {self.name} parent: {self.parent} '
|
| 1958 |
+
f'child: {self.child}')
|
| 1959 |
+
|
| 1960 |
+
def _generate_coordinates(self, coordinates):
|
| 1961 |
+
return self._fill_coordinate_list(coordinates, 3, 'q')
|
| 1962 |
+
|
| 1963 |
+
def _generate_speeds(self, speeds):
|
| 1964 |
+
return self._fill_coordinate_list(speeds, len(self.coordinates), 'u')
|
| 1965 |
+
|
| 1966 |
+
def _orient_frames(self):
|
| 1967 |
+
supported_rot_types = ('BODY', 'SPACE')
|
| 1968 |
+
if self._rot_type.upper() not in supported_rot_types:
|
| 1969 |
+
raise NotImplementedError(
|
| 1970 |
+
f'Rotation type "{self._rot_type}" is not implemented. '
|
| 1971 |
+
f'Implemented rotation types are: {supported_rot_types}')
|
| 1972 |
+
amounts = self.coordinates if self._amounts is None else self._amounts
|
| 1973 |
+
self.child_interframe.orient(self.parent_interframe, self._rot_type,
|
| 1974 |
+
amounts, self._rot_order)
|
| 1975 |
+
|
| 1976 |
+
def _set_angular_velocity(self):
|
| 1977 |
+
t = dynamicsymbols._t
|
| 1978 |
+
vel = self.child_interframe.ang_vel_in(self.parent_interframe).xreplace(
|
| 1979 |
+
{q.diff(t): u for q, u in zip(self.coordinates, self.speeds)}
|
| 1980 |
+
)
|
| 1981 |
+
self.child_interframe.set_ang_vel(self.parent_interframe, vel)
|
| 1982 |
+
|
| 1983 |
+
def _set_linear_velocity(self):
|
| 1984 |
+
self.child_point.set_pos(self.parent_point, 0)
|
| 1985 |
+
self.parent_point.set_vel(self._parent_frame, 0)
|
| 1986 |
+
self.child_point.set_vel(self._child_frame, 0)
|
| 1987 |
+
self.child.masscenter.v2pt_theory(self.parent_point, self._parent_frame,
|
| 1988 |
+
self._child_frame)
|
| 1989 |
+
|
| 1990 |
+
|
| 1991 |
+
class WeldJoint(Joint):
|
| 1992 |
+
"""Weld Joint.
|
| 1993 |
+
|
| 1994 |
+
.. raw:: html
|
| 1995 |
+
:file: ../../../doc/src/modules/physics/mechanics/api/WeldJoint.svg
|
| 1996 |
+
|
| 1997 |
+
Explanation
|
| 1998 |
+
===========
|
| 1999 |
+
|
| 2000 |
+
A weld joint is defined such that there is no relative motion between the
|
| 2001 |
+
child and parent bodies. The direction cosine matrix between the attachment
|
| 2002 |
+
frame (``parent_interframe`` and ``child_interframe``) is the identity
|
| 2003 |
+
matrix and the attachment points (``parent_point`` and ``child_point``) are
|
| 2004 |
+
coincident. The page on the joints framework gives a more detailed
|
| 2005 |
+
explanation of the intermediate frames.
|
| 2006 |
+
|
| 2007 |
+
Parameters
|
| 2008 |
+
==========
|
| 2009 |
+
|
| 2010 |
+
name : string
|
| 2011 |
+
A unique name for the joint.
|
| 2012 |
+
parent : Particle or RigidBody or Body
|
| 2013 |
+
The parent body of joint.
|
| 2014 |
+
child : Particle or RigidBody or Body
|
| 2015 |
+
The child body of joint.
|
| 2016 |
+
parent_point : Point or Vector, optional
|
| 2017 |
+
Attachment point where the joint is fixed to the parent body. If a
|
| 2018 |
+
vector is provided, then the attachment point is computed by adding the
|
| 2019 |
+
vector to the body's mass center. The default value is the parent's mass
|
| 2020 |
+
center.
|
| 2021 |
+
child_point : Point or Vector, optional
|
| 2022 |
+
Attachment point where the joint is fixed to the child body. If a
|
| 2023 |
+
vector is provided, then the attachment point is computed by adding the
|
| 2024 |
+
vector to the body's mass center. The default value is the child's mass
|
| 2025 |
+
center.
|
| 2026 |
+
parent_interframe : ReferenceFrame, optional
|
| 2027 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 2028 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 2029 |
+
is created which aligns its X axis with the given vector. The default
|
| 2030 |
+
value is the parent's own frame.
|
| 2031 |
+
child_interframe : ReferenceFrame, optional
|
| 2032 |
+
Intermediate frame of the child body with respect to which the joint
|
| 2033 |
+
transformation is formulated. If a Vector is provided then an interframe
|
| 2034 |
+
is created which aligns its X axis with the given vector. The default
|
| 2035 |
+
value is the child's own frame.
|
| 2036 |
+
|
| 2037 |
+
Attributes
|
| 2038 |
+
==========
|
| 2039 |
+
|
| 2040 |
+
name : string
|
| 2041 |
+
The joint's name.
|
| 2042 |
+
parent : Particle or RigidBody or Body
|
| 2043 |
+
The joint's parent body.
|
| 2044 |
+
child : Particle or RigidBody or Body
|
| 2045 |
+
The joint's child body.
|
| 2046 |
+
coordinates : Matrix
|
| 2047 |
+
Matrix of the joint's generalized coordinates. The default value is
|
| 2048 |
+
``dynamicsymbols(f'q_{joint.name}')``.
|
| 2049 |
+
speeds : Matrix
|
| 2050 |
+
Matrix of the joint's generalized speeds. The default value is
|
| 2051 |
+
``dynamicsymbols(f'u_{joint.name}')``.
|
| 2052 |
+
parent_point : Point
|
| 2053 |
+
Attachment point where the joint is fixed to the parent body.
|
| 2054 |
+
child_point : Point
|
| 2055 |
+
Attachment point where the joint is fixed to the child body.
|
| 2056 |
+
parent_interframe : ReferenceFrame
|
| 2057 |
+
Intermediate frame of the parent body with respect to which the joint
|
| 2058 |
+
transformation is formulated.
|
| 2059 |
+
child_interframe : ReferenceFrame
|
| 2060 |
+
Intermediate frame of the child body with respect to which the joint
|
| 2061 |
+
transformation is formulated.
|
| 2062 |
+
kdes : Matrix
|
| 2063 |
+
Kinematical differential equations of the joint.
|
| 2064 |
+
|
| 2065 |
+
Examples
|
| 2066 |
+
=========
|
| 2067 |
+
|
| 2068 |
+
A single weld joint is created from two bodies and has the following basic
|
| 2069 |
+
attributes:
|
| 2070 |
+
|
| 2071 |
+
>>> from sympy.physics.mechanics import RigidBody, WeldJoint
|
| 2072 |
+
>>> parent = RigidBody('P')
|
| 2073 |
+
>>> parent
|
| 2074 |
+
P
|
| 2075 |
+
>>> child = RigidBody('C')
|
| 2076 |
+
>>> child
|
| 2077 |
+
C
|
| 2078 |
+
>>> joint = WeldJoint('PC', parent, child)
|
| 2079 |
+
>>> joint
|
| 2080 |
+
WeldJoint: PC parent: P child: C
|
| 2081 |
+
>>> joint.name
|
| 2082 |
+
'PC'
|
| 2083 |
+
>>> joint.parent
|
| 2084 |
+
P
|
| 2085 |
+
>>> joint.child
|
| 2086 |
+
C
|
| 2087 |
+
>>> joint.parent_point
|
| 2088 |
+
P_masscenter
|
| 2089 |
+
>>> joint.child_point
|
| 2090 |
+
C_masscenter
|
| 2091 |
+
>>> joint.coordinates
|
| 2092 |
+
Matrix(0, 0, [])
|
| 2093 |
+
>>> joint.speeds
|
| 2094 |
+
Matrix(0, 0, [])
|
| 2095 |
+
>>> child.frame.ang_vel_in(parent.frame)
|
| 2096 |
+
0
|
| 2097 |
+
>>> child.frame.dcm(parent.frame)
|
| 2098 |
+
Matrix([
|
| 2099 |
+
[1, 0, 0],
|
| 2100 |
+
[0, 1, 0],
|
| 2101 |
+
[0, 0, 1]])
|
| 2102 |
+
>>> joint.child_point.pos_from(joint.parent_point)
|
| 2103 |
+
0
|
| 2104 |
+
|
| 2105 |
+
To further demonstrate the use of the weld joint, two relatively-fixed
|
| 2106 |
+
bodies rotated by a quarter turn about the Y axis can be created as follows:
|
| 2107 |
+
|
| 2108 |
+
>>> from sympy import symbols, pi
|
| 2109 |
+
>>> from sympy.physics.mechanics import ReferenceFrame, RigidBody, WeldJoint
|
| 2110 |
+
>>> l1, l2 = symbols('l1 l2')
|
| 2111 |
+
|
| 2112 |
+
First create the bodies to represent the parent and rotated child body.
|
| 2113 |
+
|
| 2114 |
+
>>> parent = RigidBody('P')
|
| 2115 |
+
>>> child = RigidBody('C')
|
| 2116 |
+
|
| 2117 |
+
Next the intermediate frame specifying the fixed rotation with respect to
|
| 2118 |
+
the parent can be created.
|
| 2119 |
+
|
| 2120 |
+
>>> rotated_frame = ReferenceFrame('Pr')
|
| 2121 |
+
>>> rotated_frame.orient_axis(parent.frame, parent.y, pi / 2)
|
| 2122 |
+
|
| 2123 |
+
The weld between the parent body and child body is located at a distance
|
| 2124 |
+
``l1`` from the parent's center of mass in the X direction and ``l2`` from
|
| 2125 |
+
the child's center of mass in the child's negative X direction.
|
| 2126 |
+
|
| 2127 |
+
>>> weld = WeldJoint('weld', parent, child, parent_point=l1 * parent.x,
|
| 2128 |
+
... child_point=-l2 * child.x,
|
| 2129 |
+
... parent_interframe=rotated_frame)
|
| 2130 |
+
|
| 2131 |
+
Now that the joint has been established, the kinematics of the bodies can be
|
| 2132 |
+
accessed. The direction cosine matrix of the child body with respect to the
|
| 2133 |
+
parent can be found:
|
| 2134 |
+
|
| 2135 |
+
>>> child.frame.dcm(parent.frame)
|
| 2136 |
+
Matrix([
|
| 2137 |
+
[0, 0, -1],
|
| 2138 |
+
[0, 1, 0],
|
| 2139 |
+
[1, 0, 0]])
|
| 2140 |
+
|
| 2141 |
+
As can also been seen from the direction cosine matrix, the parent X axis is
|
| 2142 |
+
aligned with the child's Z axis:
|
| 2143 |
+
>>> parent.x == child.z
|
| 2144 |
+
True
|
| 2145 |
+
|
| 2146 |
+
The position of the child's center of mass with respect to the parent's
|
| 2147 |
+
center of mass can be found with:
|
| 2148 |
+
|
| 2149 |
+
>>> child.masscenter.pos_from(parent.masscenter)
|
| 2150 |
+
l1*P_frame.x + l2*C_frame.x
|
| 2151 |
+
|
| 2152 |
+
The angular velocity of the child with respect to the parent is 0 as one
|
| 2153 |
+
would expect.
|
| 2154 |
+
|
| 2155 |
+
>>> child.frame.ang_vel_in(parent.frame)
|
| 2156 |
+
0
|
| 2157 |
+
|
| 2158 |
+
"""
|
| 2159 |
+
|
| 2160 |
+
def __init__(self, name, parent, child, parent_point=None, child_point=None,
|
| 2161 |
+
parent_interframe=None, child_interframe=None):
|
| 2162 |
+
super().__init__(name, parent, child, [], [], parent_point,
|
| 2163 |
+
child_point, parent_interframe=parent_interframe,
|
| 2164 |
+
child_interframe=child_interframe)
|
| 2165 |
+
self._kdes = Matrix(1, 0, []).T # Removes stackability problems #10770
|
| 2166 |
+
|
| 2167 |
+
def __str__(self):
|
| 2168 |
+
return (f'WeldJoint: {self.name} parent: {self.parent} '
|
| 2169 |
+
f'child: {self.child}')
|
| 2170 |
+
|
| 2171 |
+
def _generate_coordinates(self, coordinate):
|
| 2172 |
+
return Matrix()
|
| 2173 |
+
|
| 2174 |
+
def _generate_speeds(self, speed):
|
| 2175 |
+
return Matrix()
|
| 2176 |
+
|
| 2177 |
+
def _orient_frames(self):
|
| 2178 |
+
self.child_interframe.orient_axis(self.parent_interframe,
|
| 2179 |
+
self.parent_interframe.x, 0)
|
| 2180 |
+
|
| 2181 |
+
def _set_angular_velocity(self):
|
| 2182 |
+
self.child_interframe.set_ang_vel(self.parent_interframe, 0)
|
| 2183 |
+
|
| 2184 |
+
def _set_linear_velocity(self):
|
| 2185 |
+
self.child_point.set_pos(self.parent_point, 0)
|
| 2186 |
+
self.parent_point.set_vel(self._parent_frame, 0)
|
| 2187 |
+
self.child_point.set_vel(self._child_frame, 0)
|
| 2188 |
+
self.child.masscenter.set_vel(self._parent_frame, 0)
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/jointsmethod.py
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.physics.mechanics import (Body, Lagrangian, KanesMethod, LagrangesMethod,
|
| 2 |
+
RigidBody, Particle)
|
| 3 |
+
from sympy.physics.mechanics.body_base import BodyBase
|
| 4 |
+
from sympy.physics.mechanics.method import _Methods
|
| 5 |
+
from sympy import Matrix
|
| 6 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 7 |
+
|
| 8 |
+
__all__ = ['JointsMethod']
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class JointsMethod(_Methods):
|
| 12 |
+
"""Method for formulating the equations of motion using a set of interconnected bodies with joints.
|
| 13 |
+
|
| 14 |
+
.. deprecated:: 1.13
|
| 15 |
+
The JointsMethod class is deprecated. Its functionality has been
|
| 16 |
+
replaced by the new :class:`~.System` class.
|
| 17 |
+
|
| 18 |
+
Parameters
|
| 19 |
+
==========
|
| 20 |
+
|
| 21 |
+
newtonion : Body or ReferenceFrame
|
| 22 |
+
The newtonion(inertial) frame.
|
| 23 |
+
*joints : Joint
|
| 24 |
+
The joints in the system
|
| 25 |
+
|
| 26 |
+
Attributes
|
| 27 |
+
==========
|
| 28 |
+
|
| 29 |
+
q, u : iterable
|
| 30 |
+
Iterable of the generalized coordinates and speeds
|
| 31 |
+
bodies : iterable
|
| 32 |
+
Iterable of Body objects in the system.
|
| 33 |
+
loads : iterable
|
| 34 |
+
Iterable of (Point, vector) or (ReferenceFrame, vector) tuples
|
| 35 |
+
describing the forces on the system.
|
| 36 |
+
mass_matrix : Matrix, shape(n, n)
|
| 37 |
+
The system's mass matrix
|
| 38 |
+
forcing : Matrix, shape(n, 1)
|
| 39 |
+
The system's forcing vector
|
| 40 |
+
mass_matrix_full : Matrix, shape(2*n, 2*n)
|
| 41 |
+
The "mass matrix" for the u's and q's
|
| 42 |
+
forcing_full : Matrix, shape(2*n, 1)
|
| 43 |
+
The "forcing vector" for the u's and q's
|
| 44 |
+
method : KanesMethod or Lagrange's method
|
| 45 |
+
Method's object.
|
| 46 |
+
kdes : iterable
|
| 47 |
+
Iterable of kde in they system.
|
| 48 |
+
|
| 49 |
+
Examples
|
| 50 |
+
========
|
| 51 |
+
|
| 52 |
+
As Body and JointsMethod have been deprecated, the following examples are
|
| 53 |
+
for illustrative purposes only. The functionality of Body is fully captured
|
| 54 |
+
by :class:`~.RigidBody` and :class:`~.Particle` and the functionality of
|
| 55 |
+
JointsMethod is fully captured by :class:`~.System`. To ignore the
|
| 56 |
+
deprecation warning we can use the ignore_warnings context manager.
|
| 57 |
+
|
| 58 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 59 |
+
|
| 60 |
+
This is a simple example for a one degree of freedom translational
|
| 61 |
+
spring-mass-damper.
|
| 62 |
+
|
| 63 |
+
>>> from sympy import symbols
|
| 64 |
+
>>> from sympy.physics.mechanics import Body, JointsMethod, PrismaticJoint
|
| 65 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 66 |
+
>>> c, k = symbols('c k')
|
| 67 |
+
>>> x, v = dynamicsymbols('x v')
|
| 68 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 69 |
+
... wall = Body('W')
|
| 70 |
+
... body = Body('B')
|
| 71 |
+
>>> J = PrismaticJoint('J', wall, body, coordinates=x, speeds=v)
|
| 72 |
+
>>> wall.apply_force(c*v*wall.x, reaction_body=body)
|
| 73 |
+
>>> wall.apply_force(k*x*wall.x, reaction_body=body)
|
| 74 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 75 |
+
... method = JointsMethod(wall, J)
|
| 76 |
+
>>> method.form_eoms()
|
| 77 |
+
Matrix([[-B_mass*Derivative(v(t), t) - c*v(t) - k*x(t)]])
|
| 78 |
+
>>> M = method.mass_matrix_full
|
| 79 |
+
>>> F = method.forcing_full
|
| 80 |
+
>>> rhs = M.LUsolve(F)
|
| 81 |
+
>>> rhs
|
| 82 |
+
Matrix([
|
| 83 |
+
[ v(t)],
|
| 84 |
+
[(-c*v(t) - k*x(t))/B_mass]])
|
| 85 |
+
|
| 86 |
+
Notes
|
| 87 |
+
=====
|
| 88 |
+
|
| 89 |
+
``JointsMethod`` currently only works with systems that do not have any
|
| 90 |
+
configuration or motion constraints.
|
| 91 |
+
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
def __init__(self, newtonion, *joints):
|
| 95 |
+
sympy_deprecation_warning(
|
| 96 |
+
"""
|
| 97 |
+
The JointsMethod class is deprecated.
|
| 98 |
+
Its functionality has been replaced by the new System class.
|
| 99 |
+
""",
|
| 100 |
+
deprecated_since_version="1.13",
|
| 101 |
+
active_deprecations_target="deprecated-mechanics-jointsmethod"
|
| 102 |
+
)
|
| 103 |
+
if isinstance(newtonion, BodyBase):
|
| 104 |
+
self.frame = newtonion.frame
|
| 105 |
+
else:
|
| 106 |
+
self.frame = newtonion
|
| 107 |
+
|
| 108 |
+
self._joints = joints
|
| 109 |
+
self._bodies = self._generate_bodylist()
|
| 110 |
+
self._loads = self._generate_loadlist()
|
| 111 |
+
self._q = self._generate_q()
|
| 112 |
+
self._u = self._generate_u()
|
| 113 |
+
self._kdes = self._generate_kdes()
|
| 114 |
+
|
| 115 |
+
self._method = None
|
| 116 |
+
|
| 117 |
+
@property
|
| 118 |
+
def bodies(self):
|
| 119 |
+
"""List of bodies in they system."""
|
| 120 |
+
return self._bodies
|
| 121 |
+
|
| 122 |
+
@property
|
| 123 |
+
def loads(self):
|
| 124 |
+
"""List of loads on the system."""
|
| 125 |
+
return self._loads
|
| 126 |
+
|
| 127 |
+
@property
|
| 128 |
+
def q(self):
|
| 129 |
+
"""List of the generalized coordinates."""
|
| 130 |
+
return self._q
|
| 131 |
+
|
| 132 |
+
@property
|
| 133 |
+
def u(self):
|
| 134 |
+
"""List of the generalized speeds."""
|
| 135 |
+
return self._u
|
| 136 |
+
|
| 137 |
+
@property
|
| 138 |
+
def kdes(self):
|
| 139 |
+
"""List of the generalized coordinates."""
|
| 140 |
+
return self._kdes
|
| 141 |
+
|
| 142 |
+
@property
|
| 143 |
+
def forcing_full(self):
|
| 144 |
+
"""The "forcing vector" for the u's and q's."""
|
| 145 |
+
return self.method.forcing_full
|
| 146 |
+
|
| 147 |
+
@property
|
| 148 |
+
def mass_matrix_full(self):
|
| 149 |
+
"""The "mass matrix" for the u's and q's."""
|
| 150 |
+
return self.method.mass_matrix_full
|
| 151 |
+
|
| 152 |
+
@property
|
| 153 |
+
def mass_matrix(self):
|
| 154 |
+
"""The system's mass matrix."""
|
| 155 |
+
return self.method.mass_matrix
|
| 156 |
+
|
| 157 |
+
@property
|
| 158 |
+
def forcing(self):
|
| 159 |
+
"""The system's forcing vector."""
|
| 160 |
+
return self.method.forcing
|
| 161 |
+
|
| 162 |
+
@property
|
| 163 |
+
def method(self):
|
| 164 |
+
"""Object of method used to form equations of systems."""
|
| 165 |
+
return self._method
|
| 166 |
+
|
| 167 |
+
def _generate_bodylist(self):
|
| 168 |
+
bodies = []
|
| 169 |
+
for joint in self._joints:
|
| 170 |
+
if joint.child not in bodies:
|
| 171 |
+
bodies.append(joint.child)
|
| 172 |
+
if joint.parent not in bodies:
|
| 173 |
+
bodies.append(joint.parent)
|
| 174 |
+
return bodies
|
| 175 |
+
|
| 176 |
+
def _generate_loadlist(self):
|
| 177 |
+
load_list = []
|
| 178 |
+
for body in self.bodies:
|
| 179 |
+
if isinstance(body, Body):
|
| 180 |
+
load_list.extend(body.loads)
|
| 181 |
+
return load_list
|
| 182 |
+
|
| 183 |
+
def _generate_q(self):
|
| 184 |
+
q_ind = []
|
| 185 |
+
for joint in self._joints:
|
| 186 |
+
for coordinate in joint.coordinates:
|
| 187 |
+
if coordinate in q_ind:
|
| 188 |
+
raise ValueError('Coordinates of joints should be unique.')
|
| 189 |
+
q_ind.append(coordinate)
|
| 190 |
+
return Matrix(q_ind)
|
| 191 |
+
|
| 192 |
+
def _generate_u(self):
|
| 193 |
+
u_ind = []
|
| 194 |
+
for joint in self._joints:
|
| 195 |
+
for speed in joint.speeds:
|
| 196 |
+
if speed in u_ind:
|
| 197 |
+
raise ValueError('Speeds of joints should be unique.')
|
| 198 |
+
u_ind.append(speed)
|
| 199 |
+
return Matrix(u_ind)
|
| 200 |
+
|
| 201 |
+
def _generate_kdes(self):
|
| 202 |
+
kd_ind = Matrix(1, 0, []).T
|
| 203 |
+
for joint in self._joints:
|
| 204 |
+
kd_ind = kd_ind.col_join(joint.kdes)
|
| 205 |
+
return kd_ind
|
| 206 |
+
|
| 207 |
+
def _convert_bodies(self):
|
| 208 |
+
# Convert `Body` to `Particle` and `RigidBody`
|
| 209 |
+
bodylist = []
|
| 210 |
+
for body in self.bodies:
|
| 211 |
+
if not isinstance(body, Body):
|
| 212 |
+
bodylist.append(body)
|
| 213 |
+
continue
|
| 214 |
+
if body.is_rigidbody:
|
| 215 |
+
rb = RigidBody(body.name, body.masscenter, body.frame, body.mass,
|
| 216 |
+
(body.central_inertia, body.masscenter))
|
| 217 |
+
rb.potential_energy = body.potential_energy
|
| 218 |
+
bodylist.append(rb)
|
| 219 |
+
else:
|
| 220 |
+
part = Particle(body.name, body.masscenter, body.mass)
|
| 221 |
+
part.potential_energy = body.potential_energy
|
| 222 |
+
bodylist.append(part)
|
| 223 |
+
return bodylist
|
| 224 |
+
|
| 225 |
+
def form_eoms(self, method=KanesMethod):
|
| 226 |
+
"""Method to form system's equation of motions.
|
| 227 |
+
|
| 228 |
+
Parameters
|
| 229 |
+
==========
|
| 230 |
+
|
| 231 |
+
method : Class
|
| 232 |
+
Class name of method.
|
| 233 |
+
|
| 234 |
+
Returns
|
| 235 |
+
========
|
| 236 |
+
|
| 237 |
+
Matrix
|
| 238 |
+
Vector of equations of motions.
|
| 239 |
+
|
| 240 |
+
Examples
|
| 241 |
+
========
|
| 242 |
+
|
| 243 |
+
As Body and JointsMethod have been deprecated, the following examples
|
| 244 |
+
are for illustrative purposes only. The functionality of Body is fully
|
| 245 |
+
captured by :class:`~.RigidBody` and :class:`~.Particle` and the
|
| 246 |
+
functionality of JointsMethod is fully captured by :class:`~.System`. To
|
| 247 |
+
ignore the deprecation warning we can use the ignore_warnings context
|
| 248 |
+
manager.
|
| 249 |
+
|
| 250 |
+
>>> from sympy.utilities.exceptions import ignore_warnings
|
| 251 |
+
|
| 252 |
+
This is a simple example for a one degree of freedom translational
|
| 253 |
+
spring-mass-damper.
|
| 254 |
+
|
| 255 |
+
>>> from sympy import S, symbols
|
| 256 |
+
>>> from sympy.physics.mechanics import LagrangesMethod, dynamicsymbols, Body
|
| 257 |
+
>>> from sympy.physics.mechanics import PrismaticJoint, JointsMethod
|
| 258 |
+
>>> q = dynamicsymbols('q')
|
| 259 |
+
>>> qd = dynamicsymbols('q', 1)
|
| 260 |
+
>>> m, k, b = symbols('m k b')
|
| 261 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 262 |
+
... wall = Body('W')
|
| 263 |
+
... part = Body('P', mass=m)
|
| 264 |
+
>>> part.potential_energy = k * q**2 / S(2)
|
| 265 |
+
>>> J = PrismaticJoint('J', wall, part, coordinates=q, speeds=qd)
|
| 266 |
+
>>> wall.apply_force(b * qd * wall.x, reaction_body=part)
|
| 267 |
+
>>> with ignore_warnings(DeprecationWarning):
|
| 268 |
+
... method = JointsMethod(wall, J)
|
| 269 |
+
>>> method.form_eoms(LagrangesMethod)
|
| 270 |
+
Matrix([[b*Derivative(q(t), t) + k*q(t) + m*Derivative(q(t), (t, 2))]])
|
| 271 |
+
|
| 272 |
+
We can also solve for the states using the 'rhs' method.
|
| 273 |
+
|
| 274 |
+
>>> method.rhs()
|
| 275 |
+
Matrix([
|
| 276 |
+
[ Derivative(q(t), t)],
|
| 277 |
+
[(-b*Derivative(q(t), t) - k*q(t))/m]])
|
| 278 |
+
|
| 279 |
+
"""
|
| 280 |
+
|
| 281 |
+
bodylist = self._convert_bodies()
|
| 282 |
+
if issubclass(method, LagrangesMethod): #LagrangesMethod or similar
|
| 283 |
+
L = Lagrangian(self.frame, *bodylist)
|
| 284 |
+
self._method = method(L, self.q, self.loads, bodylist, self.frame)
|
| 285 |
+
else: #KanesMethod or similar
|
| 286 |
+
self._method = method(self.frame, q_ind=self.q, u_ind=self.u, kd_eqs=self.kdes,
|
| 287 |
+
forcelist=self.loads, bodies=bodylist)
|
| 288 |
+
soln = self.method._form_eoms()
|
| 289 |
+
return soln
|
| 290 |
+
|
| 291 |
+
def rhs(self, inv_method=None):
|
| 292 |
+
"""Returns equations that can be solved numerically.
|
| 293 |
+
|
| 294 |
+
Parameters
|
| 295 |
+
==========
|
| 296 |
+
|
| 297 |
+
inv_method : str
|
| 298 |
+
The specific sympy inverse matrix calculation method to use. For a
|
| 299 |
+
list of valid methods, see
|
| 300 |
+
:meth:`~sympy.matrices.matrixbase.MatrixBase.inv`
|
| 301 |
+
|
| 302 |
+
Returns
|
| 303 |
+
========
|
| 304 |
+
|
| 305 |
+
Matrix
|
| 306 |
+
Numerically solvable equations.
|
| 307 |
+
|
| 308 |
+
See Also
|
| 309 |
+
========
|
| 310 |
+
|
| 311 |
+
sympy.physics.mechanics.kane.KanesMethod.rhs:
|
| 312 |
+
KanesMethod's rhs function.
|
| 313 |
+
sympy.physics.mechanics.lagrange.LagrangesMethod.rhs:
|
| 314 |
+
LagrangesMethod's rhs function.
|
| 315 |
+
|
| 316 |
+
"""
|
| 317 |
+
|
| 318 |
+
return self.method.rhs(inv_method=inv_method)
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/lagrange.py
ADDED
|
@@ -0,0 +1,512 @@
|
|
|
|
|
|
|
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+
from sympy import diff, zeros, Matrix, eye, sympify
|
| 2 |
+
from sympy.core.sorting import default_sort_key
|
| 3 |
+
from sympy.physics.vector import dynamicsymbols, ReferenceFrame
|
| 4 |
+
from sympy.physics.mechanics.method import _Methods
|
| 5 |
+
from sympy.physics.mechanics.functions import (
|
| 6 |
+
find_dynamicsymbols, msubs, _f_list_parser, _validate_coordinates)
|
| 7 |
+
from sympy.physics.mechanics.linearize import Linearizer
|
| 8 |
+
from sympy.utilities.iterables import iterable
|
| 9 |
+
|
| 10 |
+
__all__ = ['LagrangesMethod']
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class LagrangesMethod(_Methods):
|
| 14 |
+
"""Lagrange's method object.
|
| 15 |
+
|
| 16 |
+
Explanation
|
| 17 |
+
===========
|
| 18 |
+
|
| 19 |
+
This object generates the equations of motion in a two step procedure. The
|
| 20 |
+
first step involves the initialization of LagrangesMethod by supplying the
|
| 21 |
+
Lagrangian and the generalized coordinates, at the bare minimum. If there
|
| 22 |
+
are any constraint equations, they can be supplied as keyword arguments.
|
| 23 |
+
The Lagrange multipliers are automatically generated and are equal in
|
| 24 |
+
number to the constraint equations. Similarly any non-conservative forces
|
| 25 |
+
can be supplied in an iterable (as described below and also shown in the
|
| 26 |
+
example) along with a ReferenceFrame. This is also discussed further in the
|
| 27 |
+
__init__ method.
|
| 28 |
+
|
| 29 |
+
Attributes
|
| 30 |
+
==========
|
| 31 |
+
|
| 32 |
+
q, u : Matrix
|
| 33 |
+
Matrices of the generalized coordinates and speeds
|
| 34 |
+
loads : iterable
|
| 35 |
+
Iterable of (Point, vector) or (ReferenceFrame, vector) tuples
|
| 36 |
+
describing the forces on the system.
|
| 37 |
+
bodies : iterable
|
| 38 |
+
Iterable containing the rigid bodies and particles of the system.
|
| 39 |
+
mass_matrix : Matrix
|
| 40 |
+
The system's mass matrix
|
| 41 |
+
forcing : Matrix
|
| 42 |
+
The system's forcing vector
|
| 43 |
+
mass_matrix_full : Matrix
|
| 44 |
+
The "mass matrix" for the qdot's, qdoubledot's, and the
|
| 45 |
+
lagrange multipliers (lam)
|
| 46 |
+
forcing_full : Matrix
|
| 47 |
+
The forcing vector for the qdot's, qdoubledot's and
|
| 48 |
+
lagrange multipliers (lam)
|
| 49 |
+
|
| 50 |
+
Examples
|
| 51 |
+
========
|
| 52 |
+
|
| 53 |
+
This is a simple example for a one degree of freedom translational
|
| 54 |
+
spring-mass-damper.
|
| 55 |
+
|
| 56 |
+
In this example, we first need to do the kinematics.
|
| 57 |
+
This involves creating generalized coordinates and their derivatives.
|
| 58 |
+
Then we create a point and set its velocity in a frame.
|
| 59 |
+
|
| 60 |
+
>>> from sympy.physics.mechanics import LagrangesMethod, Lagrangian
|
| 61 |
+
>>> from sympy.physics.mechanics import ReferenceFrame, Particle, Point
|
| 62 |
+
>>> from sympy.physics.mechanics import dynamicsymbols
|
| 63 |
+
>>> from sympy import symbols
|
| 64 |
+
>>> q = dynamicsymbols('q')
|
| 65 |
+
>>> qd = dynamicsymbols('q', 1)
|
| 66 |
+
>>> m, k, b = symbols('m k b')
|
| 67 |
+
>>> N = ReferenceFrame('N')
|
| 68 |
+
>>> P = Point('P')
|
| 69 |
+
>>> P.set_vel(N, qd * N.x)
|
| 70 |
+
|
| 71 |
+
We need to then prepare the information as required by LagrangesMethod to
|
| 72 |
+
generate equations of motion.
|
| 73 |
+
First we create the Particle, which has a point attached to it.
|
| 74 |
+
Following this the lagrangian is created from the kinetic and potential
|
| 75 |
+
energies.
|
| 76 |
+
Then, an iterable of nonconservative forces/torques must be constructed,
|
| 77 |
+
where each item is a (Point, Vector) or (ReferenceFrame, Vector) tuple,
|
| 78 |
+
with the Vectors representing the nonconservative forces or torques.
|
| 79 |
+
|
| 80 |
+
>>> Pa = Particle('Pa', P, m)
|
| 81 |
+
>>> Pa.potential_energy = k * q**2 / 2.0
|
| 82 |
+
>>> L = Lagrangian(N, Pa)
|
| 83 |
+
>>> fl = [(P, -b * qd * N.x)]
|
| 84 |
+
|
| 85 |
+
Finally we can generate the equations of motion.
|
| 86 |
+
First we create the LagrangesMethod object. To do this one must supply
|
| 87 |
+
the Lagrangian, and the generalized coordinates. The constraint equations,
|
| 88 |
+
the forcelist, and the inertial frame may also be provided, if relevant.
|
| 89 |
+
Next we generate Lagrange's equations of motion, such that:
|
| 90 |
+
Lagrange's equations of motion = 0.
|
| 91 |
+
We have the equations of motion at this point.
|
| 92 |
+
|
| 93 |
+
>>> l = LagrangesMethod(L, [q], forcelist = fl, frame = N)
|
| 94 |
+
>>> print(l.form_lagranges_equations())
|
| 95 |
+
Matrix([[b*Derivative(q(t), t) + 1.0*k*q(t) + m*Derivative(q(t), (t, 2))]])
|
| 96 |
+
|
| 97 |
+
We can also solve for the states using the 'rhs' method.
|
| 98 |
+
|
| 99 |
+
>>> print(l.rhs())
|
| 100 |
+
Matrix([[Derivative(q(t), t)], [(-b*Derivative(q(t), t) - 1.0*k*q(t))/m]])
|
| 101 |
+
|
| 102 |
+
Please refer to the docstrings on each method for more details.
|
| 103 |
+
"""
|
| 104 |
+
|
| 105 |
+
def __init__(self, Lagrangian, qs, forcelist=None, bodies=None, frame=None,
|
| 106 |
+
hol_coneqs=None, nonhol_coneqs=None):
|
| 107 |
+
"""Supply the following for the initialization of LagrangesMethod.
|
| 108 |
+
|
| 109 |
+
Lagrangian : Sympifyable
|
| 110 |
+
|
| 111 |
+
qs : array_like
|
| 112 |
+
The generalized coordinates
|
| 113 |
+
|
| 114 |
+
hol_coneqs : array_like, optional
|
| 115 |
+
The holonomic constraint equations
|
| 116 |
+
|
| 117 |
+
nonhol_coneqs : array_like, optional
|
| 118 |
+
The nonholonomic constraint equations
|
| 119 |
+
|
| 120 |
+
forcelist : iterable, optional
|
| 121 |
+
Takes an iterable of (Point, Vector) or (ReferenceFrame, Vector)
|
| 122 |
+
tuples which represent the force at a point or torque on a frame.
|
| 123 |
+
This feature is primarily to account for the nonconservative forces
|
| 124 |
+
and/or moments.
|
| 125 |
+
|
| 126 |
+
bodies : iterable, optional
|
| 127 |
+
Takes an iterable containing the rigid bodies and particles of the
|
| 128 |
+
system.
|
| 129 |
+
|
| 130 |
+
frame : ReferenceFrame, optional
|
| 131 |
+
Supply the inertial frame. This is used to determine the
|
| 132 |
+
generalized forces due to non-conservative forces.
|
| 133 |
+
"""
|
| 134 |
+
|
| 135 |
+
self._L = Matrix([sympify(Lagrangian)])
|
| 136 |
+
self.eom = None
|
| 137 |
+
self._m_cd = Matrix() # Mass Matrix of differentiated coneqs
|
| 138 |
+
self._m_d = Matrix() # Mass Matrix of dynamic equations
|
| 139 |
+
self._f_cd = Matrix() # Forcing part of the diff coneqs
|
| 140 |
+
self._f_d = Matrix() # Forcing part of the dynamic equations
|
| 141 |
+
self.lam_coeffs = Matrix() # The coeffecients of the multipliers
|
| 142 |
+
|
| 143 |
+
forcelist = forcelist if forcelist else []
|
| 144 |
+
if not iterable(forcelist):
|
| 145 |
+
raise TypeError('Force pairs must be supplied in an iterable.')
|
| 146 |
+
self._forcelist = forcelist
|
| 147 |
+
if frame and not isinstance(frame, ReferenceFrame):
|
| 148 |
+
raise TypeError('frame must be a valid ReferenceFrame')
|
| 149 |
+
self._bodies = bodies
|
| 150 |
+
self.inertial = frame
|
| 151 |
+
|
| 152 |
+
self.lam_vec = Matrix()
|
| 153 |
+
|
| 154 |
+
self._term1 = Matrix()
|
| 155 |
+
self._term2 = Matrix()
|
| 156 |
+
self._term3 = Matrix()
|
| 157 |
+
self._term4 = Matrix()
|
| 158 |
+
|
| 159 |
+
# Creating the qs, qdots and qdoubledots
|
| 160 |
+
if not iterable(qs):
|
| 161 |
+
raise TypeError('Generalized coordinates must be an iterable')
|
| 162 |
+
self._q = Matrix(qs)
|
| 163 |
+
self._qdots = self.q.diff(dynamicsymbols._t)
|
| 164 |
+
self._qdoubledots = self._qdots.diff(dynamicsymbols._t)
|
| 165 |
+
_validate_coordinates(self.q)
|
| 166 |
+
|
| 167 |
+
mat_build = lambda x: Matrix(x) if x else Matrix()
|
| 168 |
+
hol_coneqs = mat_build(hol_coneqs)
|
| 169 |
+
nonhol_coneqs = mat_build(nonhol_coneqs)
|
| 170 |
+
self.coneqs = Matrix([hol_coneqs.diff(dynamicsymbols._t),
|
| 171 |
+
nonhol_coneqs])
|
| 172 |
+
self._hol_coneqs = hol_coneqs
|
| 173 |
+
|
| 174 |
+
def form_lagranges_equations(self):
|
| 175 |
+
"""Method to form Lagrange's equations of motion.
|
| 176 |
+
|
| 177 |
+
Returns a vector of equations of motion using Lagrange's equations of
|
| 178 |
+
the second kind.
|
| 179 |
+
"""
|
| 180 |
+
|
| 181 |
+
qds = self._qdots
|
| 182 |
+
qdd_zero = dict.fromkeys(self._qdoubledots, 0)
|
| 183 |
+
n = len(self.q)
|
| 184 |
+
|
| 185 |
+
# Internally we represent the EOM as four terms:
|
| 186 |
+
# EOM = term1 - term2 - term3 - term4 = 0
|
| 187 |
+
|
| 188 |
+
# First term
|
| 189 |
+
self._term1 = self._L.jacobian(qds)
|
| 190 |
+
self._term1 = self._term1.diff(dynamicsymbols._t).T
|
| 191 |
+
|
| 192 |
+
# Second term
|
| 193 |
+
self._term2 = self._L.jacobian(self.q).T
|
| 194 |
+
|
| 195 |
+
# Third term
|
| 196 |
+
if self.coneqs:
|
| 197 |
+
coneqs = self.coneqs
|
| 198 |
+
m = len(coneqs)
|
| 199 |
+
# Creating the multipliers
|
| 200 |
+
self.lam_vec = Matrix(dynamicsymbols('lam1:' + str(m + 1)))
|
| 201 |
+
self.lam_coeffs = -coneqs.jacobian(qds)
|
| 202 |
+
self._term3 = self.lam_coeffs.T * self.lam_vec
|
| 203 |
+
# Extracting the coeffecients of the qdds from the diff coneqs
|
| 204 |
+
diffconeqs = coneqs.diff(dynamicsymbols._t)
|
| 205 |
+
self._m_cd = diffconeqs.jacobian(self._qdoubledots)
|
| 206 |
+
# The remaining terms i.e. the 'forcing' terms in diff coneqs
|
| 207 |
+
self._f_cd = -diffconeqs.subs(qdd_zero)
|
| 208 |
+
else:
|
| 209 |
+
self._term3 = zeros(n, 1)
|
| 210 |
+
|
| 211 |
+
# Fourth term
|
| 212 |
+
if self.forcelist:
|
| 213 |
+
N = self.inertial
|
| 214 |
+
self._term4 = zeros(n, 1)
|
| 215 |
+
for i, qd in enumerate(qds):
|
| 216 |
+
flist = zip(*_f_list_parser(self.forcelist, N))
|
| 217 |
+
self._term4[i] = sum(v.diff(qd, N).dot(f) for (v, f) in flist)
|
| 218 |
+
else:
|
| 219 |
+
self._term4 = zeros(n, 1)
|
| 220 |
+
|
| 221 |
+
# Form the dynamic mass and forcing matrices
|
| 222 |
+
without_lam = self._term1 - self._term2 - self._term4
|
| 223 |
+
self._m_d = without_lam.jacobian(self._qdoubledots)
|
| 224 |
+
self._f_d = -without_lam.subs(qdd_zero)
|
| 225 |
+
|
| 226 |
+
# Form the EOM
|
| 227 |
+
self.eom = without_lam - self._term3
|
| 228 |
+
return self.eom
|
| 229 |
+
|
| 230 |
+
def _form_eoms(self):
|
| 231 |
+
return self.form_lagranges_equations()
|
| 232 |
+
|
| 233 |
+
@property
|
| 234 |
+
def mass_matrix(self):
|
| 235 |
+
"""Returns the mass matrix, which is augmented by the Lagrange
|
| 236 |
+
multipliers, if necessary.
|
| 237 |
+
|
| 238 |
+
Explanation
|
| 239 |
+
===========
|
| 240 |
+
|
| 241 |
+
If the system is described by 'n' generalized coordinates and there are
|
| 242 |
+
no constraint equations then an n X n matrix is returned.
|
| 243 |
+
|
| 244 |
+
If there are 'n' generalized coordinates and 'm' constraint equations
|
| 245 |
+
have been supplied during initialization then an n X (n+m) matrix is
|
| 246 |
+
returned. The (n + m - 1)th and (n + m)th columns contain the
|
| 247 |
+
coefficients of the Lagrange multipliers.
|
| 248 |
+
"""
|
| 249 |
+
|
| 250 |
+
if self.eom is None:
|
| 251 |
+
raise ValueError('Need to compute the equations of motion first')
|
| 252 |
+
if self.coneqs:
|
| 253 |
+
return (self._m_d).row_join(self.lam_coeffs.T)
|
| 254 |
+
else:
|
| 255 |
+
return self._m_d
|
| 256 |
+
|
| 257 |
+
@property
|
| 258 |
+
def mass_matrix_full(self):
|
| 259 |
+
"""Augments the coefficients of qdots to the mass_matrix."""
|
| 260 |
+
|
| 261 |
+
if self.eom is None:
|
| 262 |
+
raise ValueError('Need to compute the equations of motion first')
|
| 263 |
+
n = len(self.q)
|
| 264 |
+
m = len(self.coneqs)
|
| 265 |
+
row1 = eye(n).row_join(zeros(n, n + m))
|
| 266 |
+
row2 = zeros(n, n).row_join(self.mass_matrix)
|
| 267 |
+
if self.coneqs:
|
| 268 |
+
row3 = zeros(m, n).row_join(self._m_cd).row_join(zeros(m, m))
|
| 269 |
+
return row1.col_join(row2).col_join(row3)
|
| 270 |
+
else:
|
| 271 |
+
return row1.col_join(row2)
|
| 272 |
+
|
| 273 |
+
@property
|
| 274 |
+
def forcing(self):
|
| 275 |
+
"""Returns the forcing vector from 'lagranges_equations' method."""
|
| 276 |
+
|
| 277 |
+
if self.eom is None:
|
| 278 |
+
raise ValueError('Need to compute the equations of motion first')
|
| 279 |
+
return self._f_d
|
| 280 |
+
|
| 281 |
+
@property
|
| 282 |
+
def forcing_full(self):
|
| 283 |
+
"""Augments qdots to the forcing vector above."""
|
| 284 |
+
|
| 285 |
+
if self.eom is None:
|
| 286 |
+
raise ValueError('Need to compute the equations of motion first')
|
| 287 |
+
if self.coneqs:
|
| 288 |
+
return self._qdots.col_join(self.forcing).col_join(self._f_cd)
|
| 289 |
+
else:
|
| 290 |
+
return self._qdots.col_join(self.forcing)
|
| 291 |
+
|
| 292 |
+
def to_linearizer(self, q_ind=None, qd_ind=None, q_dep=None, qd_dep=None,
|
| 293 |
+
linear_solver='LU'):
|
| 294 |
+
"""Returns an instance of the Linearizer class, initiated from the data
|
| 295 |
+
in the LagrangesMethod class. This may be more desirable than using the
|
| 296 |
+
linearize class method, as the Linearizer object will allow more
|
| 297 |
+
efficient recalculation (i.e. about varying operating points).
|
| 298 |
+
|
| 299 |
+
Parameters
|
| 300 |
+
==========
|
| 301 |
+
|
| 302 |
+
q_ind, qd_ind : array_like, optional
|
| 303 |
+
The independent generalized coordinates and speeds.
|
| 304 |
+
q_dep, qd_dep : array_like, optional
|
| 305 |
+
The dependent generalized coordinates and speeds.
|
| 306 |
+
linear_solver : str, callable
|
| 307 |
+
Method used to solve the several symbolic linear systems of the
|
| 308 |
+
form ``A*x=b`` in the linearization process. If a string is
|
| 309 |
+
supplied, it should be a valid method that can be used with the
|
| 310 |
+
:meth:`sympy.matrices.matrixbase.MatrixBase.solve`. If a callable is
|
| 311 |
+
supplied, it should have the format ``x = f(A, b)``, where it
|
| 312 |
+
solves the equations and returns the solution. The default is
|
| 313 |
+
``'LU'`` which corresponds to SymPy's ``A.LUsolve(b)``.
|
| 314 |
+
``LUsolve()`` is fast to compute but will often result in
|
| 315 |
+
divide-by-zero and thus ``nan`` results.
|
| 316 |
+
|
| 317 |
+
Returns
|
| 318 |
+
=======
|
| 319 |
+
Linearizer
|
| 320 |
+
An instantiated
|
| 321 |
+
:class:`sympy.physics.mechanics.linearize.Linearizer`.
|
| 322 |
+
|
| 323 |
+
"""
|
| 324 |
+
|
| 325 |
+
# Compose vectors
|
| 326 |
+
t = dynamicsymbols._t
|
| 327 |
+
q = self.q
|
| 328 |
+
u = self._qdots
|
| 329 |
+
ud = u.diff(t)
|
| 330 |
+
# Get vector of lagrange multipliers
|
| 331 |
+
lams = self.lam_vec
|
| 332 |
+
|
| 333 |
+
mat_build = lambda x: Matrix(x) if x else Matrix()
|
| 334 |
+
q_i = mat_build(q_ind)
|
| 335 |
+
q_d = mat_build(q_dep)
|
| 336 |
+
u_i = mat_build(qd_ind)
|
| 337 |
+
u_d = mat_build(qd_dep)
|
| 338 |
+
|
| 339 |
+
# Compose general form equations
|
| 340 |
+
f_c = self._hol_coneqs
|
| 341 |
+
f_v = self.coneqs
|
| 342 |
+
f_a = f_v.diff(t)
|
| 343 |
+
f_0 = u
|
| 344 |
+
f_1 = -u
|
| 345 |
+
f_2 = self._term1
|
| 346 |
+
f_3 = -(self._term2 + self._term4)
|
| 347 |
+
f_4 = -self._term3
|
| 348 |
+
|
| 349 |
+
# Check that there are an appropriate number of independent and
|
| 350 |
+
# dependent coordinates
|
| 351 |
+
if len(q_d) != len(f_c) or len(u_d) != len(f_v):
|
| 352 |
+
raise ValueError(("Must supply {:} dependent coordinates, and " +
|
| 353 |
+
"{:} dependent speeds").format(len(f_c), len(f_v)))
|
| 354 |
+
if set(Matrix([q_i, q_d])) != set(q):
|
| 355 |
+
raise ValueError("Must partition q into q_ind and q_dep, with " +
|
| 356 |
+
"no extra or missing symbols.")
|
| 357 |
+
if set(Matrix([u_i, u_d])) != set(u):
|
| 358 |
+
raise ValueError("Must partition qd into qd_ind and qd_dep, " +
|
| 359 |
+
"with no extra or missing symbols.")
|
| 360 |
+
|
| 361 |
+
# Find all other dynamic symbols, forming the forcing vector r.
|
| 362 |
+
# Sort r to make it canonical.
|
| 363 |
+
insyms = set(Matrix([q, u, ud, lams]))
|
| 364 |
+
r = list(find_dynamicsymbols(f_3, insyms))
|
| 365 |
+
r.sort(key=default_sort_key)
|
| 366 |
+
# Check for any derivatives of variables in r that are also found in r.
|
| 367 |
+
for i in r:
|
| 368 |
+
if diff(i, dynamicsymbols._t) in r:
|
| 369 |
+
raise ValueError('Cannot have derivatives of specified \
|
| 370 |
+
quantities when linearizing forcing terms.')
|
| 371 |
+
|
| 372 |
+
return Linearizer(f_0, f_1, f_2, f_3, f_4, f_c, f_v, f_a, q, u, q_i,
|
| 373 |
+
q_d, u_i, u_d, r, lams, linear_solver=linear_solver)
|
| 374 |
+
|
| 375 |
+
def linearize(self, q_ind=None, qd_ind=None, q_dep=None, qd_dep=None,
|
| 376 |
+
linear_solver='LU', **kwargs):
|
| 377 |
+
"""Linearize the equations of motion about a symbolic operating point.
|
| 378 |
+
|
| 379 |
+
Parameters
|
| 380 |
+
==========
|
| 381 |
+
linear_solver : str, callable
|
| 382 |
+
Method used to solve the several symbolic linear systems of the
|
| 383 |
+
form ``A*x=b`` in the linearization process. If a string is
|
| 384 |
+
supplied, it should be a valid method that can be used with the
|
| 385 |
+
:meth:`sympy.matrices.matrixbase.MatrixBase.solve`. If a callable is
|
| 386 |
+
supplied, it should have the format ``x = f(A, b)``, where it
|
| 387 |
+
solves the equations and returns the solution. The default is
|
| 388 |
+
``'LU'`` which corresponds to SymPy's ``A.LUsolve(b)``.
|
| 389 |
+
``LUsolve()`` is fast to compute but will often result in
|
| 390 |
+
divide-by-zero and thus ``nan`` results.
|
| 391 |
+
**kwargs
|
| 392 |
+
Extra keyword arguments are passed to
|
| 393 |
+
:meth:`sympy.physics.mechanics.linearize.Linearizer.linearize`.
|
| 394 |
+
|
| 395 |
+
Explanation
|
| 396 |
+
===========
|
| 397 |
+
|
| 398 |
+
If kwarg A_and_B is False (default), returns M, A, B, r for the
|
| 399 |
+
linearized form, M*[q', u']^T = A*[q_ind, u_ind]^T + B*r.
|
| 400 |
+
|
| 401 |
+
If kwarg A_and_B is True, returns A, B, r for the linearized form
|
| 402 |
+
dx = A*x + B*r, where x = [q_ind, u_ind]^T. Note that this is
|
| 403 |
+
computationally intensive if there are many symbolic parameters. For
|
| 404 |
+
this reason, it may be more desirable to use the default A_and_B=False,
|
| 405 |
+
returning M, A, and B. Values may then be substituted in to these
|
| 406 |
+
matrices, and the state space form found as
|
| 407 |
+
A = P.T*M.inv()*A, B = P.T*M.inv()*B, where P = Linearizer.perm_mat.
|
| 408 |
+
|
| 409 |
+
In both cases, r is found as all dynamicsymbols in the equations of
|
| 410 |
+
motion that are not part of q, u, q', or u'. They are sorted in
|
| 411 |
+
canonical form.
|
| 412 |
+
|
| 413 |
+
The operating points may be also entered using the ``op_point`` kwarg.
|
| 414 |
+
This takes a dictionary of {symbol: value}, or a an iterable of such
|
| 415 |
+
dictionaries. The values may be numeric or symbolic. The more values
|
| 416 |
+
you can specify beforehand, the faster this computation will run.
|
| 417 |
+
|
| 418 |
+
For more documentation, please see the ``Linearizer`` class."""
|
| 419 |
+
|
| 420 |
+
linearizer = self.to_linearizer(q_ind, qd_ind, q_dep, qd_dep,
|
| 421 |
+
linear_solver=linear_solver)
|
| 422 |
+
result = linearizer.linearize(**kwargs)
|
| 423 |
+
return result + (linearizer.r,)
|
| 424 |
+
|
| 425 |
+
def solve_multipliers(self, op_point=None, sol_type='dict'):
|
| 426 |
+
"""Solves for the values of the lagrange multipliers symbolically at
|
| 427 |
+
the specified operating point.
|
| 428 |
+
|
| 429 |
+
Parameters
|
| 430 |
+
==========
|
| 431 |
+
|
| 432 |
+
op_point : dict or iterable of dicts, optional
|
| 433 |
+
Point at which to solve at. The operating point is specified as
|
| 434 |
+
a dictionary or iterable of dictionaries of {symbol: value}. The
|
| 435 |
+
value may be numeric or symbolic itself.
|
| 436 |
+
|
| 437 |
+
sol_type : str, optional
|
| 438 |
+
Solution return type. Valid options are:
|
| 439 |
+
- 'dict': A dict of {symbol : value} (default)
|
| 440 |
+
- 'Matrix': An ordered column matrix of the solution
|
| 441 |
+
"""
|
| 442 |
+
|
| 443 |
+
# Determine number of multipliers
|
| 444 |
+
k = len(self.lam_vec)
|
| 445 |
+
if k == 0:
|
| 446 |
+
raise ValueError("System has no lagrange multipliers to solve for.")
|
| 447 |
+
# Compose dict of operating conditions
|
| 448 |
+
if isinstance(op_point, dict):
|
| 449 |
+
op_point_dict = op_point
|
| 450 |
+
elif iterable(op_point):
|
| 451 |
+
op_point_dict = {}
|
| 452 |
+
for op in op_point:
|
| 453 |
+
op_point_dict.update(op)
|
| 454 |
+
elif op_point is None:
|
| 455 |
+
op_point_dict = {}
|
| 456 |
+
else:
|
| 457 |
+
raise TypeError("op_point must be either a dictionary or an "
|
| 458 |
+
"iterable of dictionaries.")
|
| 459 |
+
# Compose the system to be solved
|
| 460 |
+
mass_matrix = self.mass_matrix.col_join(-self.lam_coeffs.row_join(
|
| 461 |
+
zeros(k, k)))
|
| 462 |
+
force_matrix = self.forcing.col_join(self._f_cd)
|
| 463 |
+
# Sub in the operating point
|
| 464 |
+
mass_matrix = msubs(mass_matrix, op_point_dict)
|
| 465 |
+
force_matrix = msubs(force_matrix, op_point_dict)
|
| 466 |
+
# Solve for the multipliers
|
| 467 |
+
sol_list = mass_matrix.LUsolve(-force_matrix)[-k:]
|
| 468 |
+
if sol_type == 'dict':
|
| 469 |
+
return dict(zip(self.lam_vec, sol_list))
|
| 470 |
+
elif sol_type == 'Matrix':
|
| 471 |
+
return Matrix(sol_list)
|
| 472 |
+
else:
|
| 473 |
+
raise ValueError("Unknown sol_type {:}.".format(sol_type))
|
| 474 |
+
|
| 475 |
+
def rhs(self, inv_method=None, **kwargs):
|
| 476 |
+
"""Returns equations that can be solved numerically.
|
| 477 |
+
|
| 478 |
+
Parameters
|
| 479 |
+
==========
|
| 480 |
+
|
| 481 |
+
inv_method : str
|
| 482 |
+
The specific sympy inverse matrix calculation method to use. For a
|
| 483 |
+
list of valid methods, see
|
| 484 |
+
:meth:`~sympy.matrices.matrixbase.MatrixBase.inv`
|
| 485 |
+
"""
|
| 486 |
+
|
| 487 |
+
if inv_method is None:
|
| 488 |
+
self._rhs = self.mass_matrix_full.LUsolve(self.forcing_full)
|
| 489 |
+
else:
|
| 490 |
+
self._rhs = (self.mass_matrix_full.inv(inv_method,
|
| 491 |
+
try_block_diag=True) * self.forcing_full)
|
| 492 |
+
return self._rhs
|
| 493 |
+
|
| 494 |
+
@property
|
| 495 |
+
def q(self):
|
| 496 |
+
return self._q
|
| 497 |
+
|
| 498 |
+
@property
|
| 499 |
+
def u(self):
|
| 500 |
+
return self._qdots
|
| 501 |
+
|
| 502 |
+
@property
|
| 503 |
+
def bodies(self):
|
| 504 |
+
return self._bodies
|
| 505 |
+
|
| 506 |
+
@property
|
| 507 |
+
def forcelist(self):
|
| 508 |
+
return self._forcelist
|
| 509 |
+
|
| 510 |
+
@property
|
| 511 |
+
def loads(self):
|
| 512 |
+
return self._forcelist
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/loads.py
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import ABC
|
| 2 |
+
from collections import namedtuple
|
| 3 |
+
from sympy.physics.mechanics.body_base import BodyBase
|
| 4 |
+
from sympy.physics.vector import Vector, ReferenceFrame, Point
|
| 5 |
+
|
| 6 |
+
__all__ = ['LoadBase', 'Force', 'Torque']
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class LoadBase(ABC, namedtuple('LoadBase', ['location', 'vector'])):
|
| 10 |
+
"""Abstract base class for the various loading types."""
|
| 11 |
+
|
| 12 |
+
def __add__(self, other):
|
| 13 |
+
raise TypeError(f"unsupported operand type(s) for +: "
|
| 14 |
+
f"'{self.__class__.__name__}' and "
|
| 15 |
+
f"'{other.__class__.__name__}'")
|
| 16 |
+
|
| 17 |
+
def __mul__(self, other):
|
| 18 |
+
raise TypeError(f"unsupported operand type(s) for *: "
|
| 19 |
+
f"'{self.__class__.__name__}' and "
|
| 20 |
+
f"'{other.__class__.__name__}'")
|
| 21 |
+
|
| 22 |
+
__radd__ = __add__
|
| 23 |
+
__rmul__ = __mul__
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class Force(LoadBase):
|
| 27 |
+
"""Force acting upon a point.
|
| 28 |
+
|
| 29 |
+
Explanation
|
| 30 |
+
===========
|
| 31 |
+
|
| 32 |
+
A force is a vector that is bound to a line of action. This class stores
|
| 33 |
+
both a point, which lies on the line of action, and the vector. A tuple can
|
| 34 |
+
also be used, with the location as the first entry and the vector as second
|
| 35 |
+
entry.
|
| 36 |
+
|
| 37 |
+
Examples
|
| 38 |
+
========
|
| 39 |
+
|
| 40 |
+
A force of magnitude 2 along N.x acting on a point Po can be created as
|
| 41 |
+
follows:
|
| 42 |
+
|
| 43 |
+
>>> from sympy.physics.mechanics import Point, ReferenceFrame, Force
|
| 44 |
+
>>> N = ReferenceFrame('N')
|
| 45 |
+
>>> Po = Point('Po')
|
| 46 |
+
>>> Force(Po, 2 * N.x)
|
| 47 |
+
(Po, 2*N.x)
|
| 48 |
+
|
| 49 |
+
If a body is supplied, then the center of mass of that body is used.
|
| 50 |
+
|
| 51 |
+
>>> from sympy.physics.mechanics import Particle
|
| 52 |
+
>>> P = Particle('P', point=Po)
|
| 53 |
+
>>> Force(P, 2 * N.x)
|
| 54 |
+
(Po, 2*N.x)
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
def __new__(cls, point, force):
|
| 59 |
+
if isinstance(point, BodyBase):
|
| 60 |
+
point = point.masscenter
|
| 61 |
+
if not isinstance(point, Point):
|
| 62 |
+
raise TypeError('Force location should be a Point.')
|
| 63 |
+
if not isinstance(force, Vector):
|
| 64 |
+
raise TypeError('Force vector should be a Vector.')
|
| 65 |
+
return super().__new__(cls, point, force)
|
| 66 |
+
|
| 67 |
+
def __repr__(self):
|
| 68 |
+
return (f'{self.__class__.__name__}(point={self.point}, '
|
| 69 |
+
f'force={self.force})')
|
| 70 |
+
|
| 71 |
+
@property
|
| 72 |
+
def point(self):
|
| 73 |
+
return self.location
|
| 74 |
+
|
| 75 |
+
@property
|
| 76 |
+
def force(self):
|
| 77 |
+
return self.vector
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
class Torque(LoadBase):
|
| 81 |
+
"""Torque acting upon a frame.
|
| 82 |
+
|
| 83 |
+
Explanation
|
| 84 |
+
===========
|
| 85 |
+
|
| 86 |
+
A torque is a free vector that is acting on a reference frame, which is
|
| 87 |
+
associated with a rigid body. This class stores both the frame and the
|
| 88 |
+
vector. A tuple can also be used, with the location as the first item and
|
| 89 |
+
the vector as second item.
|
| 90 |
+
|
| 91 |
+
Examples
|
| 92 |
+
========
|
| 93 |
+
|
| 94 |
+
A torque of magnitude 2 about N.x acting on a frame N can be created as
|
| 95 |
+
follows:
|
| 96 |
+
|
| 97 |
+
>>> from sympy.physics.mechanics import ReferenceFrame, Torque
|
| 98 |
+
>>> N = ReferenceFrame('N')
|
| 99 |
+
>>> Torque(N, 2 * N.x)
|
| 100 |
+
(N, 2*N.x)
|
| 101 |
+
|
| 102 |
+
If a body is supplied, then the frame fixed to that body is used.
|
| 103 |
+
|
| 104 |
+
>>> from sympy.physics.mechanics import RigidBody
|
| 105 |
+
>>> rb = RigidBody('rb', frame=N)
|
| 106 |
+
>>> Torque(rb, 2 * N.x)
|
| 107 |
+
(N, 2*N.x)
|
| 108 |
+
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
def __new__(cls, frame, torque):
|
| 112 |
+
if isinstance(frame, BodyBase):
|
| 113 |
+
frame = frame.frame
|
| 114 |
+
if not isinstance(frame, ReferenceFrame):
|
| 115 |
+
raise TypeError('Torque location should be a ReferenceFrame.')
|
| 116 |
+
if not isinstance(torque, Vector):
|
| 117 |
+
raise TypeError('Torque vector should be a Vector.')
|
| 118 |
+
return super().__new__(cls, frame, torque)
|
| 119 |
+
|
| 120 |
+
def __repr__(self):
|
| 121 |
+
return (f'{self.__class__.__name__}(frame={self.frame}, '
|
| 122 |
+
f'torque={self.torque})')
|
| 123 |
+
|
| 124 |
+
@property
|
| 125 |
+
def frame(self):
|
| 126 |
+
return self.location
|
| 127 |
+
|
| 128 |
+
@property
|
| 129 |
+
def torque(self):
|
| 130 |
+
return self.vector
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
def gravity(acceleration, *bodies):
|
| 134 |
+
"""
|
| 135 |
+
Returns a list of gravity forces given the acceleration
|
| 136 |
+
due to gravity and any number of particles or rigidbodies.
|
| 137 |
+
|
| 138 |
+
Example
|
| 139 |
+
=======
|
| 140 |
+
|
| 141 |
+
>>> from sympy.physics.mechanics import ReferenceFrame, Particle, RigidBody
|
| 142 |
+
>>> from sympy.physics.mechanics.loads import gravity
|
| 143 |
+
>>> from sympy import symbols
|
| 144 |
+
>>> N = ReferenceFrame('N')
|
| 145 |
+
>>> g = symbols('g')
|
| 146 |
+
>>> P = Particle('P')
|
| 147 |
+
>>> B = RigidBody('B')
|
| 148 |
+
>>> gravity(g*N.y, P, B)
|
| 149 |
+
[(P_masscenter, P_mass*g*N.y),
|
| 150 |
+
(B_masscenter, B_mass*g*N.y)]
|
| 151 |
+
|
| 152 |
+
"""
|
| 153 |
+
|
| 154 |
+
gravity_force = []
|
| 155 |
+
for body in bodies:
|
| 156 |
+
if not isinstance(body, BodyBase):
|
| 157 |
+
raise TypeError(f'{type(body)} is not a body type')
|
| 158 |
+
gravity_force.append(Force(body.masscenter, body.mass * acceleration))
|
| 159 |
+
return gravity_force
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def _parse_load(load):
|
| 163 |
+
"""Helper function to parse loads and convert tuples to load objects."""
|
| 164 |
+
if isinstance(load, LoadBase):
|
| 165 |
+
return load
|
| 166 |
+
elif isinstance(load, tuple):
|
| 167 |
+
if len(load) != 2:
|
| 168 |
+
raise ValueError(f'Load {load} should have a length of 2.')
|
| 169 |
+
if isinstance(load[0], Point):
|
| 170 |
+
return Force(load[0], load[1])
|
| 171 |
+
elif isinstance(load[0], ReferenceFrame):
|
| 172 |
+
return Torque(load[0], load[1])
|
| 173 |
+
else:
|
| 174 |
+
raise ValueError(f'Load not recognized. The load location {load[0]}'
|
| 175 |
+
f' should either be a Point or a ReferenceFrame.')
|
| 176 |
+
raise TypeError(f'Load type {type(load)} not recognized as a load. It '
|
| 177 |
+
f'should be a Force, Torque or tuple.')
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/method.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from abc import ABC, abstractmethod
|
| 2 |
+
|
| 3 |
+
class _Methods(ABC):
|
| 4 |
+
"""Abstract Base Class for all methods."""
|
| 5 |
+
|
| 6 |
+
@abstractmethod
|
| 7 |
+
def q(self):
|
| 8 |
+
pass
|
| 9 |
+
|
| 10 |
+
@abstractmethod
|
| 11 |
+
def u(self):
|
| 12 |
+
pass
|
| 13 |
+
|
| 14 |
+
@abstractmethod
|
| 15 |
+
def bodies(self):
|
| 16 |
+
pass
|
| 17 |
+
|
| 18 |
+
@abstractmethod
|
| 19 |
+
def loads(self):
|
| 20 |
+
pass
|
| 21 |
+
|
| 22 |
+
@abstractmethod
|
| 23 |
+
def mass_matrix(self):
|
| 24 |
+
pass
|
| 25 |
+
|
| 26 |
+
@abstractmethod
|
| 27 |
+
def forcing(self):
|
| 28 |
+
pass
|
| 29 |
+
|
| 30 |
+
@abstractmethod
|
| 31 |
+
def mass_matrix_full(self):
|
| 32 |
+
pass
|
| 33 |
+
|
| 34 |
+
@abstractmethod
|
| 35 |
+
def forcing_full(self):
|
| 36 |
+
pass
|
| 37 |
+
|
| 38 |
+
def _form_eoms(self):
|
| 39 |
+
raise NotImplementedError("Subclasses must implement this.")
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/models.py
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"""This module contains some sample symbolic models used for testing and
|
| 3 |
+
examples."""
|
| 4 |
+
|
| 5 |
+
# Internal imports
|
| 6 |
+
from sympy.core import backend as sm
|
| 7 |
+
import sympy.physics.mechanics as me
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def multi_mass_spring_damper(n=1, apply_gravity=False,
|
| 11 |
+
apply_external_forces=False):
|
| 12 |
+
r"""Returns a system containing the symbolic equations of motion and
|
| 13 |
+
associated variables for a simple multi-degree of freedom point mass,
|
| 14 |
+
spring, damper system with optional gravitational and external
|
| 15 |
+
specified forces. For example, a two mass system under the influence of
|
| 16 |
+
gravity and external forces looks like:
|
| 17 |
+
|
| 18 |
+
::
|
| 19 |
+
|
| 20 |
+
----------------
|
| 21 |
+
| | | | g
|
| 22 |
+
\ | | | V
|
| 23 |
+
k0 / --- c0 |
|
| 24 |
+
| | | x0, v0
|
| 25 |
+
--------- V
|
| 26 |
+
| m0 | -----
|
| 27 |
+
--------- |
|
| 28 |
+
| | | |
|
| 29 |
+
\ v | | |
|
| 30 |
+
k1 / f0 --- c1 |
|
| 31 |
+
| | | x1, v1
|
| 32 |
+
--------- V
|
| 33 |
+
| m1 | -----
|
| 34 |
+
---------
|
| 35 |
+
| f1
|
| 36 |
+
V
|
| 37 |
+
|
| 38 |
+
Parameters
|
| 39 |
+
==========
|
| 40 |
+
|
| 41 |
+
n : integer
|
| 42 |
+
The number of masses in the serial chain.
|
| 43 |
+
apply_gravity : boolean
|
| 44 |
+
If true, gravity will be applied to each mass.
|
| 45 |
+
apply_external_forces : boolean
|
| 46 |
+
If true, a time varying external force will be applied to each mass.
|
| 47 |
+
|
| 48 |
+
Returns
|
| 49 |
+
=======
|
| 50 |
+
|
| 51 |
+
kane : sympy.physics.mechanics.kane.KanesMethod
|
| 52 |
+
A KanesMethod object.
|
| 53 |
+
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
mass = sm.symbols('m:{}'.format(n))
|
| 57 |
+
stiffness = sm.symbols('k:{}'.format(n))
|
| 58 |
+
damping = sm.symbols('c:{}'.format(n))
|
| 59 |
+
|
| 60 |
+
acceleration_due_to_gravity = sm.symbols('g')
|
| 61 |
+
|
| 62 |
+
coordinates = me.dynamicsymbols('x:{}'.format(n))
|
| 63 |
+
speeds = me.dynamicsymbols('v:{}'.format(n))
|
| 64 |
+
specifieds = me.dynamicsymbols('f:{}'.format(n))
|
| 65 |
+
|
| 66 |
+
ceiling = me.ReferenceFrame('N')
|
| 67 |
+
origin = me.Point('origin')
|
| 68 |
+
origin.set_vel(ceiling, 0)
|
| 69 |
+
|
| 70 |
+
points = [origin]
|
| 71 |
+
kinematic_equations = []
|
| 72 |
+
particles = []
|
| 73 |
+
forces = []
|
| 74 |
+
|
| 75 |
+
for i in range(n):
|
| 76 |
+
|
| 77 |
+
center = points[-1].locatenew('center{}'.format(i),
|
| 78 |
+
coordinates[i] * ceiling.x)
|
| 79 |
+
center.set_vel(ceiling, points[-1].vel(ceiling) +
|
| 80 |
+
speeds[i] * ceiling.x)
|
| 81 |
+
points.append(center)
|
| 82 |
+
|
| 83 |
+
block = me.Particle('block{}'.format(i), center, mass[i])
|
| 84 |
+
|
| 85 |
+
kinematic_equations.append(speeds[i] - coordinates[i].diff())
|
| 86 |
+
|
| 87 |
+
total_force = (-stiffness[i] * coordinates[i] -
|
| 88 |
+
damping[i] * speeds[i])
|
| 89 |
+
try:
|
| 90 |
+
total_force += (stiffness[i + 1] * coordinates[i + 1] +
|
| 91 |
+
damping[i + 1] * speeds[i + 1])
|
| 92 |
+
except IndexError: # no force from below on last mass
|
| 93 |
+
pass
|
| 94 |
+
|
| 95 |
+
if apply_gravity:
|
| 96 |
+
total_force += mass[i] * acceleration_due_to_gravity
|
| 97 |
+
|
| 98 |
+
if apply_external_forces:
|
| 99 |
+
total_force += specifieds[i]
|
| 100 |
+
|
| 101 |
+
forces.append((center, total_force * ceiling.x))
|
| 102 |
+
|
| 103 |
+
particles.append(block)
|
| 104 |
+
|
| 105 |
+
kane = me.KanesMethod(ceiling, q_ind=coordinates, u_ind=speeds,
|
| 106 |
+
kd_eqs=kinematic_equations)
|
| 107 |
+
kane.kanes_equations(particles, forces)
|
| 108 |
+
|
| 109 |
+
return kane
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
def n_link_pendulum_on_cart(n=1, cart_force=True, joint_torques=False):
|
| 113 |
+
r"""Returns the system containing the symbolic first order equations of
|
| 114 |
+
motion for a 2D n-link pendulum on a sliding cart under the influence of
|
| 115 |
+
gravity.
|
| 116 |
+
|
| 117 |
+
::
|
| 118 |
+
|
| 119 |
+
|
|
| 120 |
+
o y v
|
| 121 |
+
\ 0 ^ g
|
| 122 |
+
\ |
|
| 123 |
+
--\-|----
|
| 124 |
+
| \| |
|
| 125 |
+
F-> | o --|---> x
|
| 126 |
+
| |
|
| 127 |
+
---------
|
| 128 |
+
o o
|
| 129 |
+
|
| 130 |
+
Parameters
|
| 131 |
+
==========
|
| 132 |
+
|
| 133 |
+
n : integer
|
| 134 |
+
The number of links in the pendulum.
|
| 135 |
+
cart_force : boolean, default=True
|
| 136 |
+
If true an external specified lateral force is applied to the cart.
|
| 137 |
+
joint_torques : boolean, default=False
|
| 138 |
+
If true joint torques will be added as specified inputs at each
|
| 139 |
+
joint.
|
| 140 |
+
|
| 141 |
+
Returns
|
| 142 |
+
=======
|
| 143 |
+
|
| 144 |
+
kane : sympy.physics.mechanics.kane.KanesMethod
|
| 145 |
+
A KanesMethod object.
|
| 146 |
+
|
| 147 |
+
Notes
|
| 148 |
+
=====
|
| 149 |
+
|
| 150 |
+
The degrees of freedom of the system are n + 1, i.e. one for each
|
| 151 |
+
pendulum link and one for the lateral motion of the cart.
|
| 152 |
+
|
| 153 |
+
M x' = F, where x = [u0, ..., un+1, q0, ..., qn+1]
|
| 154 |
+
|
| 155 |
+
The joint angles are all defined relative to the ground where the x axis
|
| 156 |
+
defines the ground line and the y axis points up. The joint torques are
|
| 157 |
+
applied between each adjacent link and the between the cart and the
|
| 158 |
+
lower link where a positive torque corresponds to positive angle.
|
| 159 |
+
|
| 160 |
+
"""
|
| 161 |
+
if n <= 0:
|
| 162 |
+
raise ValueError('The number of links must be a positive integer.')
|
| 163 |
+
|
| 164 |
+
q = me.dynamicsymbols('q:{}'.format(n + 1))
|
| 165 |
+
u = me.dynamicsymbols('u:{}'.format(n + 1))
|
| 166 |
+
|
| 167 |
+
if joint_torques is True:
|
| 168 |
+
T = me.dynamicsymbols('T1:{}'.format(n + 1))
|
| 169 |
+
|
| 170 |
+
m = sm.symbols('m:{}'.format(n + 1))
|
| 171 |
+
l = sm.symbols('l:{}'.format(n))
|
| 172 |
+
g, t = sm.symbols('g t')
|
| 173 |
+
|
| 174 |
+
I = me.ReferenceFrame('I')
|
| 175 |
+
O = me.Point('O')
|
| 176 |
+
O.set_vel(I, 0)
|
| 177 |
+
|
| 178 |
+
P0 = me.Point('P0')
|
| 179 |
+
P0.set_pos(O, q[0] * I.x)
|
| 180 |
+
P0.set_vel(I, u[0] * I.x)
|
| 181 |
+
Pa0 = me.Particle('Pa0', P0, m[0])
|
| 182 |
+
|
| 183 |
+
frames = [I]
|
| 184 |
+
points = [P0]
|
| 185 |
+
particles = [Pa0]
|
| 186 |
+
forces = [(P0, -m[0] * g * I.y)]
|
| 187 |
+
kindiffs = [q[0].diff(t) - u[0]]
|
| 188 |
+
|
| 189 |
+
if cart_force is True or joint_torques is True:
|
| 190 |
+
specified = []
|
| 191 |
+
else:
|
| 192 |
+
specified = None
|
| 193 |
+
|
| 194 |
+
for i in range(n):
|
| 195 |
+
Bi = I.orientnew('B{}'.format(i), 'Axis', [q[i + 1], I.z])
|
| 196 |
+
Bi.set_ang_vel(I, u[i + 1] * I.z)
|
| 197 |
+
frames.append(Bi)
|
| 198 |
+
|
| 199 |
+
Pi = points[-1].locatenew('P{}'.format(i + 1), l[i] * Bi.y)
|
| 200 |
+
Pi.v2pt_theory(points[-1], I, Bi)
|
| 201 |
+
points.append(Pi)
|
| 202 |
+
|
| 203 |
+
Pai = me.Particle('Pa' + str(i + 1), Pi, m[i + 1])
|
| 204 |
+
particles.append(Pai)
|
| 205 |
+
|
| 206 |
+
forces.append((Pi, -m[i + 1] * g * I.y))
|
| 207 |
+
|
| 208 |
+
if joint_torques is True:
|
| 209 |
+
|
| 210 |
+
specified.append(T[i])
|
| 211 |
+
|
| 212 |
+
if i == 0:
|
| 213 |
+
forces.append((I, -T[i] * I.z))
|
| 214 |
+
|
| 215 |
+
if i == n - 1:
|
| 216 |
+
forces.append((Bi, T[i] * I.z))
|
| 217 |
+
else:
|
| 218 |
+
forces.append((Bi, T[i] * I.z - T[i + 1] * I.z))
|
| 219 |
+
|
| 220 |
+
kindiffs.append(q[i + 1].diff(t) - u[i + 1])
|
| 221 |
+
|
| 222 |
+
if cart_force is True:
|
| 223 |
+
F = me.dynamicsymbols('F')
|
| 224 |
+
forces.append((P0, F * I.x))
|
| 225 |
+
specified.append(F)
|
| 226 |
+
|
| 227 |
+
kane = me.KanesMethod(I, q_ind=q, u_ind=u, kd_eqs=kindiffs)
|
| 228 |
+
kane.kanes_equations(particles, forces)
|
| 229 |
+
|
| 230 |
+
return kane
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/particle.py
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy import S
|
| 2 |
+
from sympy.physics.vector import cross, dot
|
| 3 |
+
from sympy.physics.mechanics.body_base import BodyBase
|
| 4 |
+
from sympy.physics.mechanics.inertia import inertia_of_point_mass
|
| 5 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 6 |
+
|
| 7 |
+
__all__ = ['Particle']
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class Particle(BodyBase):
|
| 11 |
+
"""A particle.
|
| 12 |
+
|
| 13 |
+
Explanation
|
| 14 |
+
===========
|
| 15 |
+
|
| 16 |
+
Particles have a non-zero mass and lack spatial extension; they take up no
|
| 17 |
+
space.
|
| 18 |
+
|
| 19 |
+
Values need to be supplied on initialization, but can be changed later.
|
| 20 |
+
|
| 21 |
+
Parameters
|
| 22 |
+
==========
|
| 23 |
+
|
| 24 |
+
name : str
|
| 25 |
+
Name of particle
|
| 26 |
+
point : Point
|
| 27 |
+
A physics/mechanics Point which represents the position, velocity, and
|
| 28 |
+
acceleration of this Particle
|
| 29 |
+
mass : Sympifyable
|
| 30 |
+
A SymPy expression representing the Particle's mass
|
| 31 |
+
potential_energy : Sympifyable
|
| 32 |
+
The potential energy of the Particle.
|
| 33 |
+
|
| 34 |
+
Examples
|
| 35 |
+
========
|
| 36 |
+
|
| 37 |
+
>>> from sympy.physics.mechanics import Particle, Point
|
| 38 |
+
>>> from sympy import Symbol
|
| 39 |
+
>>> po = Point('po')
|
| 40 |
+
>>> m = Symbol('m')
|
| 41 |
+
>>> pa = Particle('pa', po, m)
|
| 42 |
+
>>> # Or you could change these later
|
| 43 |
+
>>> pa.mass = m
|
| 44 |
+
>>> pa.point = po
|
| 45 |
+
|
| 46 |
+
"""
|
| 47 |
+
point = BodyBase.masscenter
|
| 48 |
+
|
| 49 |
+
def __init__(self, name, point=None, mass=None):
|
| 50 |
+
super().__init__(name, point, mass)
|
| 51 |
+
|
| 52 |
+
def linear_momentum(self, frame):
|
| 53 |
+
"""Linear momentum of the particle.
|
| 54 |
+
|
| 55 |
+
Explanation
|
| 56 |
+
===========
|
| 57 |
+
|
| 58 |
+
The linear momentum L, of a particle P, with respect to frame N is
|
| 59 |
+
given by:
|
| 60 |
+
|
| 61 |
+
L = m * v
|
| 62 |
+
|
| 63 |
+
where m is the mass of the particle, and v is the velocity of the
|
| 64 |
+
particle in the frame N.
|
| 65 |
+
|
| 66 |
+
Parameters
|
| 67 |
+
==========
|
| 68 |
+
|
| 69 |
+
frame : ReferenceFrame
|
| 70 |
+
The frame in which linear momentum is desired.
|
| 71 |
+
|
| 72 |
+
Examples
|
| 73 |
+
========
|
| 74 |
+
|
| 75 |
+
>>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
|
| 76 |
+
>>> from sympy.physics.mechanics import dynamicsymbols
|
| 77 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 78 |
+
>>> init_vprinting(pretty_print=False)
|
| 79 |
+
>>> m, v = dynamicsymbols('m v')
|
| 80 |
+
>>> N = ReferenceFrame('N')
|
| 81 |
+
>>> P = Point('P')
|
| 82 |
+
>>> A = Particle('A', P, m)
|
| 83 |
+
>>> P.set_vel(N, v * N.x)
|
| 84 |
+
>>> A.linear_momentum(N)
|
| 85 |
+
m*v*N.x
|
| 86 |
+
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
return self.mass * self.point.vel(frame)
|
| 90 |
+
|
| 91 |
+
def angular_momentum(self, point, frame):
|
| 92 |
+
"""Angular momentum of the particle about the point.
|
| 93 |
+
|
| 94 |
+
Explanation
|
| 95 |
+
===========
|
| 96 |
+
|
| 97 |
+
The angular momentum H, about some point O of a particle, P, is given
|
| 98 |
+
by:
|
| 99 |
+
|
| 100 |
+
``H = cross(r, m * v)``
|
| 101 |
+
|
| 102 |
+
where r is the position vector from point O to the particle P, m is
|
| 103 |
+
the mass of the particle, and v is the velocity of the particle in
|
| 104 |
+
the inertial frame, N.
|
| 105 |
+
|
| 106 |
+
Parameters
|
| 107 |
+
==========
|
| 108 |
+
|
| 109 |
+
point : Point
|
| 110 |
+
The point about which angular momentum of the particle is desired.
|
| 111 |
+
|
| 112 |
+
frame : ReferenceFrame
|
| 113 |
+
The frame in which angular momentum is desired.
|
| 114 |
+
|
| 115 |
+
Examples
|
| 116 |
+
========
|
| 117 |
+
|
| 118 |
+
>>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
|
| 119 |
+
>>> from sympy.physics.mechanics import dynamicsymbols
|
| 120 |
+
>>> from sympy.physics.vector import init_vprinting
|
| 121 |
+
>>> init_vprinting(pretty_print=False)
|
| 122 |
+
>>> m, v, r = dynamicsymbols('m v r')
|
| 123 |
+
>>> N = ReferenceFrame('N')
|
| 124 |
+
>>> O = Point('O')
|
| 125 |
+
>>> A = O.locatenew('A', r * N.x)
|
| 126 |
+
>>> P = Particle('P', A, m)
|
| 127 |
+
>>> P.point.set_vel(N, v * N.y)
|
| 128 |
+
>>> P.angular_momentum(O, N)
|
| 129 |
+
m*r*v*N.z
|
| 130 |
+
|
| 131 |
+
"""
|
| 132 |
+
|
| 133 |
+
return cross(self.point.pos_from(point),
|
| 134 |
+
self.mass * self.point.vel(frame))
|
| 135 |
+
|
| 136 |
+
def kinetic_energy(self, frame):
|
| 137 |
+
"""Kinetic energy of the particle.
|
| 138 |
+
|
| 139 |
+
Explanation
|
| 140 |
+
===========
|
| 141 |
+
|
| 142 |
+
The kinetic energy, T, of a particle, P, is given by:
|
| 143 |
+
|
| 144 |
+
``T = 1/2 (dot(m * v, v))``
|
| 145 |
+
|
| 146 |
+
where m is the mass of particle P, and v is the velocity of the
|
| 147 |
+
particle in the supplied ReferenceFrame.
|
| 148 |
+
|
| 149 |
+
Parameters
|
| 150 |
+
==========
|
| 151 |
+
|
| 152 |
+
frame : ReferenceFrame
|
| 153 |
+
The Particle's velocity is typically defined with respect to
|
| 154 |
+
an inertial frame but any relevant frame in which the velocity is
|
| 155 |
+
known can be supplied.
|
| 156 |
+
|
| 157 |
+
Examples
|
| 158 |
+
========
|
| 159 |
+
|
| 160 |
+
>>> from sympy.physics.mechanics import Particle, Point, ReferenceFrame
|
| 161 |
+
>>> from sympy import symbols
|
| 162 |
+
>>> m, v, r = symbols('m v r')
|
| 163 |
+
>>> N = ReferenceFrame('N')
|
| 164 |
+
>>> O = Point('O')
|
| 165 |
+
>>> P = Particle('P', O, m)
|
| 166 |
+
>>> P.point.set_vel(N, v * N.y)
|
| 167 |
+
>>> P.kinetic_energy(N)
|
| 168 |
+
m*v**2/2
|
| 169 |
+
|
| 170 |
+
"""
|
| 171 |
+
|
| 172 |
+
return S.Half * self.mass * dot(self.point.vel(frame),
|
| 173 |
+
self.point.vel(frame))
|
| 174 |
+
|
| 175 |
+
def set_potential_energy(self, scalar):
|
| 176 |
+
sympy_deprecation_warning(
|
| 177 |
+
"""
|
| 178 |
+
The sympy.physics.mechanics.Particle.set_potential_energy()
|
| 179 |
+
method is deprecated. Instead use
|
| 180 |
+
|
| 181 |
+
P.potential_energy = scalar
|
| 182 |
+
""",
|
| 183 |
+
deprecated_since_version="1.5",
|
| 184 |
+
active_deprecations_target="deprecated-set-potential-energy",
|
| 185 |
+
)
|
| 186 |
+
self.potential_energy = scalar
|
| 187 |
+
|
| 188 |
+
def parallel_axis(self, point, frame):
|
| 189 |
+
"""Returns an inertia dyadic of the particle with respect to another
|
| 190 |
+
point and frame.
|
| 191 |
+
|
| 192 |
+
Parameters
|
| 193 |
+
==========
|
| 194 |
+
|
| 195 |
+
point : sympy.physics.vector.Point
|
| 196 |
+
The point to express the inertia dyadic about.
|
| 197 |
+
frame : sympy.physics.vector.ReferenceFrame
|
| 198 |
+
The reference frame used to construct the dyadic.
|
| 199 |
+
|
| 200 |
+
Returns
|
| 201 |
+
=======
|
| 202 |
+
|
| 203 |
+
inertia : sympy.physics.vector.Dyadic
|
| 204 |
+
The inertia dyadic of the particle expressed about the provided
|
| 205 |
+
point and frame.
|
| 206 |
+
|
| 207 |
+
"""
|
| 208 |
+
return inertia_of_point_mass(self.mass, self.point.pos_from(point),
|
| 209 |
+
frame)
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/pathway.py
ADDED
|
@@ -0,0 +1,688 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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|
| 3 |
+
from abc import ABC, abstractmethod
|
| 4 |
+
|
| 5 |
+
from sympy.core.singleton import S
|
| 6 |
+
from sympy.physics.mechanics.loads import Force
|
| 7 |
+
from sympy.physics.mechanics.wrapping_geometry import WrappingGeometryBase
|
| 8 |
+
from sympy.physics.vector import Point, dynamicsymbols
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
__all__ = ['PathwayBase', 'LinearPathway', 'ObstacleSetPathway',
|
| 12 |
+
'WrappingPathway']
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class PathwayBase(ABC):
|
| 16 |
+
"""Abstract base class for all pathway classes to inherit from.
|
| 17 |
+
|
| 18 |
+
Notes
|
| 19 |
+
=====
|
| 20 |
+
|
| 21 |
+
Instances of this class cannot be directly instantiated by users. However,
|
| 22 |
+
it can be used to created custom pathway types through subclassing.
|
| 23 |
+
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
def __init__(self, *attachments):
|
| 27 |
+
"""Initializer for ``PathwayBase``."""
|
| 28 |
+
self.attachments = attachments
|
| 29 |
+
|
| 30 |
+
@property
|
| 31 |
+
def attachments(self):
|
| 32 |
+
"""The pair of points defining a pathway's ends."""
|
| 33 |
+
return self._attachments
|
| 34 |
+
|
| 35 |
+
@attachments.setter
|
| 36 |
+
def attachments(self, attachments):
|
| 37 |
+
if hasattr(self, '_attachments'):
|
| 38 |
+
msg = (
|
| 39 |
+
f'Can\'t set attribute `attachments` to {repr(attachments)} '
|
| 40 |
+
f'as it is immutable.'
|
| 41 |
+
)
|
| 42 |
+
raise AttributeError(msg)
|
| 43 |
+
if len(attachments) != 2:
|
| 44 |
+
msg = (
|
| 45 |
+
f'Value {repr(attachments)} passed to `attachments` was an '
|
| 46 |
+
f'iterable of length {len(attachments)}, must be an iterable '
|
| 47 |
+
f'of length 2.'
|
| 48 |
+
)
|
| 49 |
+
raise ValueError(msg)
|
| 50 |
+
for i, point in enumerate(attachments):
|
| 51 |
+
if not isinstance(point, Point):
|
| 52 |
+
msg = (
|
| 53 |
+
f'Value {repr(point)} passed to `attachments` at index '
|
| 54 |
+
f'{i} was of type {type(point)}, must be {Point}.'
|
| 55 |
+
)
|
| 56 |
+
raise TypeError(msg)
|
| 57 |
+
self._attachments = tuple(attachments)
|
| 58 |
+
|
| 59 |
+
@property
|
| 60 |
+
@abstractmethod
|
| 61 |
+
def length(self):
|
| 62 |
+
"""An expression representing the pathway's length."""
|
| 63 |
+
pass
|
| 64 |
+
|
| 65 |
+
@property
|
| 66 |
+
@abstractmethod
|
| 67 |
+
def extension_velocity(self):
|
| 68 |
+
"""An expression representing the pathway's extension velocity."""
|
| 69 |
+
pass
|
| 70 |
+
|
| 71 |
+
@abstractmethod
|
| 72 |
+
def to_loads(self, force):
|
| 73 |
+
"""Loads required by the equations of motion method classes.
|
| 74 |
+
|
| 75 |
+
Explanation
|
| 76 |
+
===========
|
| 77 |
+
|
| 78 |
+
``KanesMethod`` requires a list of ``Point``-``Vector`` tuples to be
|
| 79 |
+
passed to the ``loads`` parameters of its ``kanes_equations`` method
|
| 80 |
+
when constructing the equations of motion. This method acts as a
|
| 81 |
+
utility to produce the correctly-structred pairs of points and vectors
|
| 82 |
+
required so that these can be easily concatenated with other items in
|
| 83 |
+
the list of loads and passed to ``KanesMethod.kanes_equations``. These
|
| 84 |
+
loads are also in the correct form to also be passed to the other
|
| 85 |
+
equations of motion method classes, e.g. ``LagrangesMethod``.
|
| 86 |
+
|
| 87 |
+
"""
|
| 88 |
+
pass
|
| 89 |
+
|
| 90 |
+
def __repr__(self):
|
| 91 |
+
"""Default representation of a pathway."""
|
| 92 |
+
attachments = ', '.join(str(a) for a in self.attachments)
|
| 93 |
+
return f'{self.__class__.__name__}({attachments})'
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class LinearPathway(PathwayBase):
|
| 97 |
+
"""Linear pathway between a pair of attachment points.
|
| 98 |
+
|
| 99 |
+
Explanation
|
| 100 |
+
===========
|
| 101 |
+
|
| 102 |
+
A linear pathway forms a straight-line segment between two points and is
|
| 103 |
+
the simplest pathway that can be formed. It will not interact with any
|
| 104 |
+
other objects in the system, i.e. a ``LinearPathway`` will intersect other
|
| 105 |
+
objects to ensure that the path between its two ends (its attachments) is
|
| 106 |
+
the shortest possible.
|
| 107 |
+
|
| 108 |
+
A linear pathway is made up of two points that can move relative to each
|
| 109 |
+
other, and a pair of equal and opposite forces acting on the points. If the
|
| 110 |
+
positive time-varying Euclidean distance between the two points is defined,
|
| 111 |
+
then the "extension velocity" is the time derivative of this distance. The
|
| 112 |
+
extension velocity is positive when the two points are moving away from
|
| 113 |
+
each other and negative when moving closer to each other. The direction for
|
| 114 |
+
the force acting on either point is determined by constructing a unit
|
| 115 |
+
vector directed from the other point to this point. This establishes a sign
|
| 116 |
+
convention such that a positive force magnitude tends to push the points
|
| 117 |
+
apart. The following diagram shows the positive force sense and the
|
| 118 |
+
distance between the points::
|
| 119 |
+
|
| 120 |
+
P Q
|
| 121 |
+
o<--- F --->o
|
| 122 |
+
| |
|
| 123 |
+
|<--l(t)--->|
|
| 124 |
+
|
| 125 |
+
Examples
|
| 126 |
+
========
|
| 127 |
+
|
| 128 |
+
>>> from sympy.physics.mechanics import LinearPathway
|
| 129 |
+
|
| 130 |
+
To construct a pathway, two points are required to be passed to the
|
| 131 |
+
``attachments`` parameter as a ``tuple``.
|
| 132 |
+
|
| 133 |
+
>>> from sympy.physics.mechanics import Point
|
| 134 |
+
>>> pA, pB = Point('pA'), Point('pB')
|
| 135 |
+
>>> linear_pathway = LinearPathway(pA, pB)
|
| 136 |
+
>>> linear_pathway
|
| 137 |
+
LinearPathway(pA, pB)
|
| 138 |
+
|
| 139 |
+
The pathway created above isn't very interesting without the positions and
|
| 140 |
+
velocities of its attachment points being described. Without this its not
|
| 141 |
+
possible to describe how the pathway moves, i.e. its length or its
|
| 142 |
+
extension velocity.
|
| 143 |
+
|
| 144 |
+
>>> from sympy.physics.mechanics import ReferenceFrame
|
| 145 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 146 |
+
>>> N = ReferenceFrame('N')
|
| 147 |
+
>>> q = dynamicsymbols('q')
|
| 148 |
+
>>> pB.set_pos(pA, q*N.x)
|
| 149 |
+
>>> pB.pos_from(pA)
|
| 150 |
+
q(t)*N.x
|
| 151 |
+
|
| 152 |
+
A pathway's length can be accessed via its ``length`` attribute.
|
| 153 |
+
|
| 154 |
+
>>> linear_pathway.length
|
| 155 |
+
sqrt(q(t)**2)
|
| 156 |
+
|
| 157 |
+
Note how what appears to be an overly-complex expression is returned. This
|
| 158 |
+
is actually required as it ensures that a pathway's length is always
|
| 159 |
+
positive.
|
| 160 |
+
|
| 161 |
+
A pathway's extension velocity can be accessed similarly via its
|
| 162 |
+
``extension_velocity`` attribute.
|
| 163 |
+
|
| 164 |
+
>>> linear_pathway.extension_velocity
|
| 165 |
+
sqrt(q(t)**2)*Derivative(q(t), t)/q(t)
|
| 166 |
+
|
| 167 |
+
Parameters
|
| 168 |
+
==========
|
| 169 |
+
|
| 170 |
+
attachments : tuple[Point, Point]
|
| 171 |
+
Pair of ``Point`` objects between which the linear pathway spans.
|
| 172 |
+
Constructor expects two points to be passed, e.g.
|
| 173 |
+
``LinearPathway(Point('pA'), Point('pB'))``. More or fewer points will
|
| 174 |
+
cause an error to be thrown.
|
| 175 |
+
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
def __init__(self, *attachments):
|
| 179 |
+
"""Initializer for ``LinearPathway``.
|
| 180 |
+
|
| 181 |
+
Parameters
|
| 182 |
+
==========
|
| 183 |
+
|
| 184 |
+
attachments : Point
|
| 185 |
+
Pair of ``Point`` objects between which the linear pathway spans.
|
| 186 |
+
Constructor expects two points to be passed, e.g.
|
| 187 |
+
``LinearPathway(Point('pA'), Point('pB'))``. More or fewer points
|
| 188 |
+
will cause an error to be thrown.
|
| 189 |
+
|
| 190 |
+
"""
|
| 191 |
+
super().__init__(*attachments)
|
| 192 |
+
|
| 193 |
+
@property
|
| 194 |
+
def length(self):
|
| 195 |
+
"""Exact analytical expression for the pathway's length."""
|
| 196 |
+
return _point_pair_length(*self.attachments)
|
| 197 |
+
|
| 198 |
+
@property
|
| 199 |
+
def extension_velocity(self):
|
| 200 |
+
"""Exact analytical expression for the pathway's extension velocity."""
|
| 201 |
+
return _point_pair_extension_velocity(*self.attachments)
|
| 202 |
+
|
| 203 |
+
def to_loads(self, force):
|
| 204 |
+
"""Loads required by the equations of motion method classes.
|
| 205 |
+
|
| 206 |
+
Explanation
|
| 207 |
+
===========
|
| 208 |
+
|
| 209 |
+
``KanesMethod`` requires a list of ``Point``-``Vector`` tuples to be
|
| 210 |
+
passed to the ``loads`` parameters of its ``kanes_equations`` method
|
| 211 |
+
when constructing the equations of motion. This method acts as a
|
| 212 |
+
utility to produce the correctly-structred pairs of points and vectors
|
| 213 |
+
required so that these can be easily concatenated with other items in
|
| 214 |
+
the list of loads and passed to ``KanesMethod.kanes_equations``. These
|
| 215 |
+
loads are also in the correct form to also be passed to the other
|
| 216 |
+
equations of motion method classes, e.g. ``LagrangesMethod``.
|
| 217 |
+
|
| 218 |
+
Examples
|
| 219 |
+
========
|
| 220 |
+
|
| 221 |
+
The below example shows how to generate the loads produced in a linear
|
| 222 |
+
actuator that produces an expansile force ``F``. First, create a linear
|
| 223 |
+
actuator between two points separated by the coordinate ``q`` in the
|
| 224 |
+
``x`` direction of the global frame ``N``.
|
| 225 |
+
|
| 226 |
+
>>> from sympy.physics.mechanics import (LinearPathway, Point,
|
| 227 |
+
... ReferenceFrame)
|
| 228 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 229 |
+
>>> q = dynamicsymbols('q')
|
| 230 |
+
>>> N = ReferenceFrame('N')
|
| 231 |
+
>>> pA, pB = Point('pA'), Point('pB')
|
| 232 |
+
>>> pB.set_pos(pA, q*N.x)
|
| 233 |
+
>>> linear_pathway = LinearPathway(pA, pB)
|
| 234 |
+
|
| 235 |
+
Now create a symbol ``F`` to describe the magnitude of the (expansile)
|
| 236 |
+
force that will be produced along the pathway. The list of loads that
|
| 237 |
+
``KanesMethod`` requires can be produced by calling the pathway's
|
| 238 |
+
``to_loads`` method with ``F`` passed as the only argument.
|
| 239 |
+
|
| 240 |
+
>>> from sympy import symbols
|
| 241 |
+
>>> F = symbols('F')
|
| 242 |
+
>>> linear_pathway.to_loads(F)
|
| 243 |
+
[(pA, - F*q(t)/sqrt(q(t)**2)*N.x), (pB, F*q(t)/sqrt(q(t)**2)*N.x)]
|
| 244 |
+
|
| 245 |
+
Parameters
|
| 246 |
+
==========
|
| 247 |
+
|
| 248 |
+
force : Expr
|
| 249 |
+
Magnitude of the force acting along the length of the pathway. As
|
| 250 |
+
per the sign conventions for the pathway length, pathway extension
|
| 251 |
+
velocity, and pair of point forces, if this ``Expr`` is positive
|
| 252 |
+
then the force will act to push the pair of points away from one
|
| 253 |
+
another (it is expansile).
|
| 254 |
+
|
| 255 |
+
"""
|
| 256 |
+
relative_position = _point_pair_relative_position(*self.attachments)
|
| 257 |
+
loads = [
|
| 258 |
+
Force(self.attachments[0], -force*relative_position/self.length),
|
| 259 |
+
Force(self.attachments[-1], force*relative_position/self.length),
|
| 260 |
+
]
|
| 261 |
+
return loads
|
| 262 |
+
|
| 263 |
+
|
| 264 |
+
class ObstacleSetPathway(PathwayBase):
|
| 265 |
+
"""Obstacle-set pathway between a set of attachment points.
|
| 266 |
+
|
| 267 |
+
Explanation
|
| 268 |
+
===========
|
| 269 |
+
|
| 270 |
+
An obstacle-set pathway forms a series of straight-line segment between
|
| 271 |
+
pairs of consecutive points in a set of points. It is similiar to multiple
|
| 272 |
+
linear pathways joined end-to-end. It will not interact with any other
|
| 273 |
+
objects in the system, i.e. an ``ObstacleSetPathway`` will intersect other
|
| 274 |
+
objects to ensure that the path between its pairs of points (its
|
| 275 |
+
attachments) is the shortest possible.
|
| 276 |
+
|
| 277 |
+
Examples
|
| 278 |
+
========
|
| 279 |
+
|
| 280 |
+
To construct an obstacle-set pathway, three or more points are required to
|
| 281 |
+
be passed to the ``attachments`` parameter as a ``tuple``.
|
| 282 |
+
|
| 283 |
+
>>> from sympy.physics.mechanics import ObstacleSetPathway, Point
|
| 284 |
+
>>> pA, pB, pC, pD = Point('pA'), Point('pB'), Point('pC'), Point('pD')
|
| 285 |
+
>>> obstacle_set_pathway = ObstacleSetPathway(pA, pB, pC, pD)
|
| 286 |
+
>>> obstacle_set_pathway
|
| 287 |
+
ObstacleSetPathway(pA, pB, pC, pD)
|
| 288 |
+
|
| 289 |
+
The pathway created above isn't very interesting without the positions and
|
| 290 |
+
velocities of its attachment points being described. Without this its not
|
| 291 |
+
possible to describe how the pathway moves, i.e. its length or its
|
| 292 |
+
extension velocity.
|
| 293 |
+
|
| 294 |
+
>>> from sympy import cos, sin
|
| 295 |
+
>>> from sympy.physics.mechanics import ReferenceFrame
|
| 296 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 297 |
+
>>> N = ReferenceFrame('N')
|
| 298 |
+
>>> q = dynamicsymbols('q')
|
| 299 |
+
>>> pO = Point('pO')
|
| 300 |
+
>>> pA.set_pos(pO, N.y)
|
| 301 |
+
>>> pB.set_pos(pO, -N.x)
|
| 302 |
+
>>> pC.set_pos(pA, cos(q) * N.x - (sin(q) + 1) * N.y)
|
| 303 |
+
>>> pD.set_pos(pA, sin(q) * N.x + (cos(q) - 1) * N.y)
|
| 304 |
+
>>> pB.pos_from(pA)
|
| 305 |
+
- N.x - N.y
|
| 306 |
+
>>> pC.pos_from(pA)
|
| 307 |
+
cos(q(t))*N.x + (-sin(q(t)) - 1)*N.y
|
| 308 |
+
>>> pD.pos_from(pA)
|
| 309 |
+
sin(q(t))*N.x + (cos(q(t)) - 1)*N.y
|
| 310 |
+
|
| 311 |
+
A pathway's length can be accessed via its ``length`` attribute.
|
| 312 |
+
|
| 313 |
+
>>> obstacle_set_pathway.length.simplify()
|
| 314 |
+
sqrt(2)*(sqrt(cos(q(t)) + 1) + 2)
|
| 315 |
+
|
| 316 |
+
A pathway's extension velocity can be accessed similarly via its
|
| 317 |
+
``extension_velocity`` attribute.
|
| 318 |
+
|
| 319 |
+
>>> obstacle_set_pathway.extension_velocity.simplify()
|
| 320 |
+
-sqrt(2)*sin(q(t))*Derivative(q(t), t)/(2*sqrt(cos(q(t)) + 1))
|
| 321 |
+
|
| 322 |
+
Parameters
|
| 323 |
+
==========
|
| 324 |
+
|
| 325 |
+
attachments : tuple[Point, Point]
|
| 326 |
+
The set of ``Point`` objects that define the segmented obstacle-set
|
| 327 |
+
pathway.
|
| 328 |
+
|
| 329 |
+
"""
|
| 330 |
+
|
| 331 |
+
def __init__(self, *attachments):
|
| 332 |
+
"""Initializer for ``ObstacleSetPathway``.
|
| 333 |
+
|
| 334 |
+
Parameters
|
| 335 |
+
==========
|
| 336 |
+
|
| 337 |
+
attachments : tuple[Point, ...]
|
| 338 |
+
The set of ``Point`` objects that define the segmented obstacle-set
|
| 339 |
+
pathway.
|
| 340 |
+
|
| 341 |
+
"""
|
| 342 |
+
super().__init__(*attachments)
|
| 343 |
+
|
| 344 |
+
@property
|
| 345 |
+
def attachments(self):
|
| 346 |
+
"""The set of points defining a pathway's segmented path."""
|
| 347 |
+
return self._attachments
|
| 348 |
+
|
| 349 |
+
@attachments.setter
|
| 350 |
+
def attachments(self, attachments):
|
| 351 |
+
if hasattr(self, '_attachments'):
|
| 352 |
+
msg = (
|
| 353 |
+
f'Can\'t set attribute `attachments` to {repr(attachments)} '
|
| 354 |
+
f'as it is immutable.'
|
| 355 |
+
)
|
| 356 |
+
raise AttributeError(msg)
|
| 357 |
+
if len(attachments) <= 2:
|
| 358 |
+
msg = (
|
| 359 |
+
f'Value {repr(attachments)} passed to `attachments` was an '
|
| 360 |
+
f'iterable of length {len(attachments)}, must be an iterable '
|
| 361 |
+
f'of length 3 or greater.'
|
| 362 |
+
)
|
| 363 |
+
raise ValueError(msg)
|
| 364 |
+
for i, point in enumerate(attachments):
|
| 365 |
+
if not isinstance(point, Point):
|
| 366 |
+
msg = (
|
| 367 |
+
f'Value {repr(point)} passed to `attachments` at index '
|
| 368 |
+
f'{i} was of type {type(point)}, must be {Point}.'
|
| 369 |
+
)
|
| 370 |
+
raise TypeError(msg)
|
| 371 |
+
self._attachments = tuple(attachments)
|
| 372 |
+
|
| 373 |
+
@property
|
| 374 |
+
def length(self):
|
| 375 |
+
"""Exact analytical expression for the pathway's length."""
|
| 376 |
+
length = S.Zero
|
| 377 |
+
attachment_pairs = zip(self.attachments[:-1], self.attachments[1:])
|
| 378 |
+
for attachment_pair in attachment_pairs:
|
| 379 |
+
length += _point_pair_length(*attachment_pair)
|
| 380 |
+
return length
|
| 381 |
+
|
| 382 |
+
@property
|
| 383 |
+
def extension_velocity(self):
|
| 384 |
+
"""Exact analytical expression for the pathway's extension velocity."""
|
| 385 |
+
extension_velocity = S.Zero
|
| 386 |
+
attachment_pairs = zip(self.attachments[:-1], self.attachments[1:])
|
| 387 |
+
for attachment_pair in attachment_pairs:
|
| 388 |
+
extension_velocity += _point_pair_extension_velocity(*attachment_pair)
|
| 389 |
+
return extension_velocity
|
| 390 |
+
|
| 391 |
+
def to_loads(self, force):
|
| 392 |
+
"""Loads required by the equations of motion method classes.
|
| 393 |
+
|
| 394 |
+
Explanation
|
| 395 |
+
===========
|
| 396 |
+
|
| 397 |
+
``KanesMethod`` requires a list of ``Point``-``Vector`` tuples to be
|
| 398 |
+
passed to the ``loads`` parameters of its ``kanes_equations`` method
|
| 399 |
+
when constructing the equations of motion. This method acts as a
|
| 400 |
+
utility to produce the correctly-structred pairs of points and vectors
|
| 401 |
+
required so that these can be easily concatenated with other items in
|
| 402 |
+
the list of loads and passed to ``KanesMethod.kanes_equations``. These
|
| 403 |
+
loads are also in the correct form to also be passed to the other
|
| 404 |
+
equations of motion method classes, e.g. ``LagrangesMethod``.
|
| 405 |
+
|
| 406 |
+
Examples
|
| 407 |
+
========
|
| 408 |
+
|
| 409 |
+
The below example shows how to generate the loads produced in an
|
| 410 |
+
actuator that follows an obstacle-set pathway between four points and
|
| 411 |
+
produces an expansile force ``F``. First, create a pair of reference
|
| 412 |
+
frames, ``A`` and ``B``, in which the four points ``pA``, ``pB``,
|
| 413 |
+
``pC``, and ``pD`` will be located. The first two points in frame ``A``
|
| 414 |
+
and the second two in frame ``B``. Frame ``B`` will also be oriented
|
| 415 |
+
such that it relates to ``A`` via a rotation of ``q`` about an axis
|
| 416 |
+
``N.z`` in a global frame (``N.z``, ``A.z``, and ``B.z`` are parallel).
|
| 417 |
+
|
| 418 |
+
>>> from sympy.physics.mechanics import (ObstacleSetPathway, Point,
|
| 419 |
+
... ReferenceFrame)
|
| 420 |
+
>>> from sympy.physics.vector import dynamicsymbols
|
| 421 |
+
>>> q = dynamicsymbols('q')
|
| 422 |
+
>>> N = ReferenceFrame('N')
|
| 423 |
+
>>> N = ReferenceFrame('N')
|
| 424 |
+
>>> A = N.orientnew('A', 'axis', (0, N.x))
|
| 425 |
+
>>> B = A.orientnew('B', 'axis', (q, N.z))
|
| 426 |
+
>>> pO = Point('pO')
|
| 427 |
+
>>> pA, pB, pC, pD = Point('pA'), Point('pB'), Point('pC'), Point('pD')
|
| 428 |
+
>>> pA.set_pos(pO, A.x)
|
| 429 |
+
>>> pB.set_pos(pO, -A.y)
|
| 430 |
+
>>> pC.set_pos(pO, B.y)
|
| 431 |
+
>>> pD.set_pos(pO, B.x)
|
| 432 |
+
>>> obstacle_set_pathway = ObstacleSetPathway(pA, pB, pC, pD)
|
| 433 |
+
|
| 434 |
+
Now create a symbol ``F`` to describe the magnitude of the (expansile)
|
| 435 |
+
force that will be produced along the pathway. The list of loads that
|
| 436 |
+
``KanesMethod`` requires can be produced by calling the pathway's
|
| 437 |
+
``to_loads`` method with ``F`` passed as the only argument.
|
| 438 |
+
|
| 439 |
+
>>> from sympy import Symbol
|
| 440 |
+
>>> F = Symbol('F')
|
| 441 |
+
>>> obstacle_set_pathway.to_loads(F)
|
| 442 |
+
[(pA, sqrt(2)*F/2*A.x + sqrt(2)*F/2*A.y),
|
| 443 |
+
(pB, - sqrt(2)*F/2*A.x - sqrt(2)*F/2*A.y),
|
| 444 |
+
(pB, - F/sqrt(2*cos(q(t)) + 2)*A.y - F/sqrt(2*cos(q(t)) + 2)*B.y),
|
| 445 |
+
(pC, F/sqrt(2*cos(q(t)) + 2)*A.y + F/sqrt(2*cos(q(t)) + 2)*B.y),
|
| 446 |
+
(pC, - sqrt(2)*F/2*B.x + sqrt(2)*F/2*B.y),
|
| 447 |
+
(pD, sqrt(2)*F/2*B.x - sqrt(2)*F/2*B.y)]
|
| 448 |
+
|
| 449 |
+
Parameters
|
| 450 |
+
==========
|
| 451 |
+
|
| 452 |
+
force : Expr
|
| 453 |
+
The force acting along the length of the pathway. It is assumed
|
| 454 |
+
that this ``Expr`` represents an expansile force.
|
| 455 |
+
|
| 456 |
+
"""
|
| 457 |
+
loads = []
|
| 458 |
+
attachment_pairs = zip(self.attachments[:-1], self.attachments[1:])
|
| 459 |
+
for attachment_pair in attachment_pairs:
|
| 460 |
+
relative_position = _point_pair_relative_position(*attachment_pair)
|
| 461 |
+
length = _point_pair_length(*attachment_pair)
|
| 462 |
+
loads.extend([
|
| 463 |
+
Force(attachment_pair[0], -force*relative_position/length),
|
| 464 |
+
Force(attachment_pair[1], force*relative_position/length),
|
| 465 |
+
])
|
| 466 |
+
return loads
|
| 467 |
+
|
| 468 |
+
|
| 469 |
+
class WrappingPathway(PathwayBase):
|
| 470 |
+
"""Pathway that wraps a geometry object.
|
| 471 |
+
|
| 472 |
+
Explanation
|
| 473 |
+
===========
|
| 474 |
+
|
| 475 |
+
A wrapping pathway interacts with a geometry object and forms a path that
|
| 476 |
+
wraps smoothly along its surface. The wrapping pathway along the geometry
|
| 477 |
+
object will be the geodesic that the geometry object defines based on the
|
| 478 |
+
two points. It will not interact with any other objects in the system, i.e.
|
| 479 |
+
a ``WrappingPathway`` will intersect other objects to ensure that the path
|
| 480 |
+
between its two ends (its attachments) is the shortest possible.
|
| 481 |
+
|
| 482 |
+
To explain the sign conventions used for pathway length, extension
|
| 483 |
+
velocity, and direction of applied forces, we can ignore the geometry with
|
| 484 |
+
which the wrapping pathway interacts. A wrapping pathway is made up of two
|
| 485 |
+
points that can move relative to each other, and a pair of equal and
|
| 486 |
+
opposite forces acting on the points. If the positive time-varying
|
| 487 |
+
Euclidean distance between the two points is defined, then the "extension
|
| 488 |
+
velocity" is the time derivative of this distance. The extension velocity
|
| 489 |
+
is positive when the two points are moving away from each other and
|
| 490 |
+
negative when moving closer to each other. The direction for the force
|
| 491 |
+
acting on either point is determined by constructing a unit vector directed
|
| 492 |
+
from the other point to this point. This establishes a sign convention such
|
| 493 |
+
that a positive force magnitude tends to push the points apart. The
|
| 494 |
+
following diagram shows the positive force sense and the distance between
|
| 495 |
+
the points::
|
| 496 |
+
|
| 497 |
+
P Q
|
| 498 |
+
o<--- F --->o
|
| 499 |
+
| |
|
| 500 |
+
|<--l(t)--->|
|
| 501 |
+
|
| 502 |
+
Examples
|
| 503 |
+
========
|
| 504 |
+
|
| 505 |
+
>>> from sympy.physics.mechanics import WrappingPathway
|
| 506 |
+
|
| 507 |
+
To construct a wrapping pathway, like other pathways, a pair of points must
|
| 508 |
+
be passed, followed by an instance of a wrapping geometry class as a
|
| 509 |
+
keyword argument. We'll use a cylinder with radius ``r`` and its axis
|
| 510 |
+
parallel to ``N.x`` passing through a point ``pO``.
|
| 511 |
+
|
| 512 |
+
>>> from sympy import symbols
|
| 513 |
+
>>> from sympy.physics.mechanics import Point, ReferenceFrame, WrappingCylinder
|
| 514 |
+
>>> r = symbols('r')
|
| 515 |
+
>>> N = ReferenceFrame('N')
|
| 516 |
+
>>> pA, pB, pO = Point('pA'), Point('pB'), Point('pO')
|
| 517 |
+
>>> cylinder = WrappingCylinder(r, pO, N.x)
|
| 518 |
+
>>> wrapping_pathway = WrappingPathway(pA, pB, cylinder)
|
| 519 |
+
>>> wrapping_pathway
|
| 520 |
+
WrappingPathway(pA, pB, geometry=WrappingCylinder(radius=r, point=pO,
|
| 521 |
+
axis=N.x))
|
| 522 |
+
|
| 523 |
+
Parameters
|
| 524 |
+
==========
|
| 525 |
+
|
| 526 |
+
attachment_1 : Point
|
| 527 |
+
First of the pair of ``Point`` objects between which the wrapping
|
| 528 |
+
pathway spans.
|
| 529 |
+
attachment_2 : Point
|
| 530 |
+
Second of the pair of ``Point`` objects between which the wrapping
|
| 531 |
+
pathway spans.
|
| 532 |
+
geometry : WrappingGeometryBase
|
| 533 |
+
Geometry about which the pathway wraps.
|
| 534 |
+
|
| 535 |
+
"""
|
| 536 |
+
|
| 537 |
+
def __init__(self, attachment_1, attachment_2, geometry):
|
| 538 |
+
"""Initializer for ``WrappingPathway``.
|
| 539 |
+
|
| 540 |
+
Parameters
|
| 541 |
+
==========
|
| 542 |
+
|
| 543 |
+
attachment_1 : Point
|
| 544 |
+
First of the pair of ``Point`` objects between which the wrapping
|
| 545 |
+
pathway spans.
|
| 546 |
+
attachment_2 : Point
|
| 547 |
+
Second of the pair of ``Point`` objects between which the wrapping
|
| 548 |
+
pathway spans.
|
| 549 |
+
geometry : WrappingGeometryBase
|
| 550 |
+
Geometry about which the pathway wraps.
|
| 551 |
+
The geometry about which the pathway wraps.
|
| 552 |
+
|
| 553 |
+
"""
|
| 554 |
+
super().__init__(attachment_1, attachment_2)
|
| 555 |
+
self.geometry = geometry
|
| 556 |
+
|
| 557 |
+
@property
|
| 558 |
+
def geometry(self):
|
| 559 |
+
"""Geometry around which the pathway wraps."""
|
| 560 |
+
return self._geometry
|
| 561 |
+
|
| 562 |
+
@geometry.setter
|
| 563 |
+
def geometry(self, geometry):
|
| 564 |
+
if hasattr(self, '_geometry'):
|
| 565 |
+
msg = (
|
| 566 |
+
f'Can\'t set attribute `geometry` to {repr(geometry)} as it '
|
| 567 |
+
f'is immutable.'
|
| 568 |
+
)
|
| 569 |
+
raise AttributeError(msg)
|
| 570 |
+
if not isinstance(geometry, WrappingGeometryBase):
|
| 571 |
+
msg = (
|
| 572 |
+
f'Value {repr(geometry)} passed to `geometry` was of type '
|
| 573 |
+
f'{type(geometry)}, must be {WrappingGeometryBase}.'
|
| 574 |
+
)
|
| 575 |
+
raise TypeError(msg)
|
| 576 |
+
self._geometry = geometry
|
| 577 |
+
|
| 578 |
+
@property
|
| 579 |
+
def length(self):
|
| 580 |
+
"""Exact analytical expression for the pathway's length."""
|
| 581 |
+
return self.geometry.geodesic_length(*self.attachments)
|
| 582 |
+
|
| 583 |
+
@property
|
| 584 |
+
def extension_velocity(self):
|
| 585 |
+
"""Exact analytical expression for the pathway's extension velocity."""
|
| 586 |
+
return self.length.diff(dynamicsymbols._t)
|
| 587 |
+
|
| 588 |
+
def to_loads(self, force):
|
| 589 |
+
"""Loads required by the equations of motion method classes.
|
| 590 |
+
|
| 591 |
+
Explanation
|
| 592 |
+
===========
|
| 593 |
+
|
| 594 |
+
``KanesMethod`` requires a list of ``Point``-``Vector`` tuples to be
|
| 595 |
+
passed to the ``loads`` parameters of its ``kanes_equations`` method
|
| 596 |
+
when constructing the equations of motion. This method acts as a
|
| 597 |
+
utility to produce the correctly-structred pairs of points and vectors
|
| 598 |
+
required so that these can be easily concatenated with other items in
|
| 599 |
+
the list of loads and passed to ``KanesMethod.kanes_equations``. These
|
| 600 |
+
loads are also in the correct form to also be passed to the other
|
| 601 |
+
equations of motion method classes, e.g. ``LagrangesMethod``.
|
| 602 |
+
|
| 603 |
+
Examples
|
| 604 |
+
========
|
| 605 |
+
|
| 606 |
+
The below example shows how to generate the loads produced in an
|
| 607 |
+
actuator that produces an expansile force ``F`` while wrapping around a
|
| 608 |
+
cylinder. First, create a cylinder with radius ``r`` and an axis
|
| 609 |
+
parallel to the ``N.z`` direction of the global frame ``N`` that also
|
| 610 |
+
passes through a point ``pO``.
|
| 611 |
+
|
| 612 |
+
>>> from sympy import symbols
|
| 613 |
+
>>> from sympy.physics.mechanics import (Point, ReferenceFrame,
|
| 614 |
+
... WrappingCylinder)
|
| 615 |
+
>>> N = ReferenceFrame('N')
|
| 616 |
+
>>> r = symbols('r', positive=True)
|
| 617 |
+
>>> pO = Point('pO')
|
| 618 |
+
>>> cylinder = WrappingCylinder(r, pO, N.z)
|
| 619 |
+
|
| 620 |
+
Create the pathway of the actuator using the ``WrappingPathway`` class,
|
| 621 |
+
defined to span between two points ``pA`` and ``pB``. Both points lie
|
| 622 |
+
on the surface of the cylinder and the location of ``pB`` is defined
|
| 623 |
+
relative to ``pA`` by the dynamics symbol ``q``.
|
| 624 |
+
|
| 625 |
+
>>> from sympy import cos, sin
|
| 626 |
+
>>> from sympy.physics.mechanics import WrappingPathway, dynamicsymbols
|
| 627 |
+
>>> q = dynamicsymbols('q')
|
| 628 |
+
>>> pA = Point('pA')
|
| 629 |
+
>>> pB = Point('pB')
|
| 630 |
+
>>> pA.set_pos(pO, r*N.x)
|
| 631 |
+
>>> pB.set_pos(pO, r*(cos(q)*N.x + sin(q)*N.y))
|
| 632 |
+
>>> pB.pos_from(pA)
|
| 633 |
+
(r*cos(q(t)) - r)*N.x + r*sin(q(t))*N.y
|
| 634 |
+
>>> pathway = WrappingPathway(pA, pB, cylinder)
|
| 635 |
+
|
| 636 |
+
Now create a symbol ``F`` to describe the magnitude of the (expansile)
|
| 637 |
+
force that will be produced along the pathway. The list of loads that
|
| 638 |
+
``KanesMethod`` requires can be produced by calling the pathway's
|
| 639 |
+
``to_loads`` method with ``F`` passed as the only argument.
|
| 640 |
+
|
| 641 |
+
>>> F = symbols('F')
|
| 642 |
+
>>> loads = pathway.to_loads(F)
|
| 643 |
+
>>> [load.__class__(load.location, load.vector.simplify()) for load in loads]
|
| 644 |
+
[(pA, F*N.y), (pB, F*sin(q(t))*N.x - F*cos(q(t))*N.y),
|
| 645 |
+
(pO, - F*sin(q(t))*N.x + F*(cos(q(t)) - 1)*N.y)]
|
| 646 |
+
|
| 647 |
+
Parameters
|
| 648 |
+
==========
|
| 649 |
+
|
| 650 |
+
force : Expr
|
| 651 |
+
Magnitude of the force acting along the length of the pathway. It
|
| 652 |
+
is assumed that this ``Expr`` represents an expansile force.
|
| 653 |
+
|
| 654 |
+
"""
|
| 655 |
+
pA, pB = self.attachments
|
| 656 |
+
pO = self.geometry.point
|
| 657 |
+
pA_force, pB_force = self.geometry.geodesic_end_vectors(pA, pB)
|
| 658 |
+
pO_force = -(pA_force + pB_force)
|
| 659 |
+
|
| 660 |
+
loads = [
|
| 661 |
+
Force(pA, force * pA_force),
|
| 662 |
+
Force(pB, force * pB_force),
|
| 663 |
+
Force(pO, force * pO_force),
|
| 664 |
+
]
|
| 665 |
+
return loads
|
| 666 |
+
|
| 667 |
+
def __repr__(self):
|
| 668 |
+
"""Representation of a ``WrappingPathway``."""
|
| 669 |
+
attachments = ', '.join(str(a) for a in self.attachments)
|
| 670 |
+
return (
|
| 671 |
+
f'{self.__class__.__name__}({attachments}, '
|
| 672 |
+
f'geometry={self.geometry})'
|
| 673 |
+
)
|
| 674 |
+
|
| 675 |
+
|
| 676 |
+
def _point_pair_relative_position(point_1, point_2):
|
| 677 |
+
"""The relative position between a pair of points."""
|
| 678 |
+
return point_2.pos_from(point_1)
|
| 679 |
+
|
| 680 |
+
|
| 681 |
+
def _point_pair_length(point_1, point_2):
|
| 682 |
+
"""The length of the direct linear path between two points."""
|
| 683 |
+
return _point_pair_relative_position(point_1, point_2).magnitude()
|
| 684 |
+
|
| 685 |
+
|
| 686 |
+
def _point_pair_extension_velocity(point_1, point_2):
|
| 687 |
+
"""The extension velocity of the direct linear path between two points."""
|
| 688 |
+
return _point_pair_length(point_1, point_2).diff(dynamicsymbols._t)
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/system.py
ADDED
|
@@ -0,0 +1,1553 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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ye, zeros
|
| 6 |
+
from sympy.core.containers import OrderedSet
|
| 7 |
+
from sympy.physics.mechanics.actuator import ActuatorBase
|
| 8 |
+
from sympy.physics.mechanics.body_base import BodyBase
|
| 9 |
+
from sympy.physics.mechanics.functions import (
|
| 10 |
+
Lagrangian, _validate_coordinates, find_dynamicsymbols)
|
| 11 |
+
from sympy.physics.mechanics.joint import Joint
|
| 12 |
+
from sympy.physics.mechanics.kane import KanesMethod
|
| 13 |
+
from sympy.physics.mechanics.lagrange import LagrangesMethod
|
| 14 |
+
from sympy.physics.mechanics.loads import _parse_load, gravity
|
| 15 |
+
from sympy.physics.mechanics.method import _Methods
|
| 16 |
+
from sympy.physics.mechanics.particle import Particle
|
| 17 |
+
from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols
|
| 18 |
+
from sympy.utilities.iterables import iterable
|
| 19 |
+
from sympy.utilities.misc import filldedent
|
| 20 |
+
|
| 21 |
+
__all__ = ['SymbolicSystem', 'System']
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _reset_eom_method(method):
|
| 25 |
+
"""Decorator to reset the eom_method if a property is changed."""
|
| 26 |
+
|
| 27 |
+
@wraps(method)
|
| 28 |
+
def wrapper(self, *args, **kwargs):
|
| 29 |
+
self._eom_method = None
|
| 30 |
+
return method(self, *args, **kwargs)
|
| 31 |
+
|
| 32 |
+
return wrapper
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
class System(_Methods):
|
| 36 |
+
"""Class to define a multibody system and form its equations of motion.
|
| 37 |
+
|
| 38 |
+
Explanation
|
| 39 |
+
===========
|
| 40 |
+
|
| 41 |
+
A ``System`` instance stores the different objects associated with a model,
|
| 42 |
+
including bodies, joints, constraints, and other relevant information. With
|
| 43 |
+
all the relationships between components defined, the ``System`` can be used
|
| 44 |
+
to form the equations of motion using a backend, such as ``KanesMethod``.
|
| 45 |
+
The ``System`` has been designed to be compatible with third-party
|
| 46 |
+
libraries for greater flexibility and integration with other tools.
|
| 47 |
+
|
| 48 |
+
Attributes
|
| 49 |
+
==========
|
| 50 |
+
|
| 51 |
+
frame : ReferenceFrame
|
| 52 |
+
Inertial reference frame of the system.
|
| 53 |
+
fixed_point : Point
|
| 54 |
+
A fixed point in the inertial reference frame.
|
| 55 |
+
x : Vector
|
| 56 |
+
Unit vector fixed in the inertial reference frame.
|
| 57 |
+
y : Vector
|
| 58 |
+
Unit vector fixed in the inertial reference frame.
|
| 59 |
+
z : Vector
|
| 60 |
+
Unit vector fixed in the inertial reference frame.
|
| 61 |
+
q : ImmutableMatrix
|
| 62 |
+
Matrix of all the generalized coordinates, i.e. the independent
|
| 63 |
+
generalized coordinates stacked upon the dependent.
|
| 64 |
+
u : ImmutableMatrix
|
| 65 |
+
Matrix of all the generalized speeds, i.e. the independent generealized
|
| 66 |
+
speeds stacked upon the dependent.
|
| 67 |
+
q_ind : ImmutableMatrix
|
| 68 |
+
Matrix of the independent generalized coordinates.
|
| 69 |
+
q_dep : ImmutableMatrix
|
| 70 |
+
Matrix of the dependent generalized coordinates.
|
| 71 |
+
u_ind : ImmutableMatrix
|
| 72 |
+
Matrix of the independent generalized speeds.
|
| 73 |
+
u_dep : ImmutableMatrix
|
| 74 |
+
Matrix of the dependent generalized speeds.
|
| 75 |
+
u_aux : ImmutableMatrix
|
| 76 |
+
Matrix of auxiliary generalized speeds.
|
| 77 |
+
kdes : ImmutableMatrix
|
| 78 |
+
Matrix of the kinematical differential equations as expressions equated
|
| 79 |
+
to the zero matrix.
|
| 80 |
+
bodies : tuple of BodyBase subclasses
|
| 81 |
+
Tuple of all bodies that make up the system.
|
| 82 |
+
joints : tuple of Joint
|
| 83 |
+
Tuple of all joints that connect bodies in the system.
|
| 84 |
+
loads : tuple of LoadBase subclasses
|
| 85 |
+
Tuple of all loads that have been applied to the system.
|
| 86 |
+
actuators : tuple of ActuatorBase subclasses
|
| 87 |
+
Tuple of all actuators present in the system.
|
| 88 |
+
holonomic_constraints : ImmutableMatrix
|
| 89 |
+
Matrix with the holonomic constraints as expressions equated to the zero
|
| 90 |
+
matrix.
|
| 91 |
+
nonholonomic_constraints : ImmutableMatrix
|
| 92 |
+
Matrix with the nonholonomic constraints as expressions equated to the
|
| 93 |
+
zero matrix.
|
| 94 |
+
velocity_constraints : ImmutableMatrix
|
| 95 |
+
Matrix with the velocity constraints as expressions equated to the zero
|
| 96 |
+
matrix. These are by default derived as the time derivatives of the
|
| 97 |
+
holonomic constraints extended with the nonholonomic constraints.
|
| 98 |
+
eom_method : subclass of KanesMethod or LagrangesMethod
|
| 99 |
+
Backend for forming the equations of motion.
|
| 100 |
+
|
| 101 |
+
Examples
|
| 102 |
+
========
|
| 103 |
+
|
| 104 |
+
In the example below a cart with a pendulum is created. The cart moves along
|
| 105 |
+
the x axis of the rail and the pendulum rotates about the z axis. The length
|
| 106 |
+
of the pendulum is ``l`` with the pendulum represented as a particle. To
|
| 107 |
+
move the cart a time dependent force ``F`` is applied to the cart.
|
| 108 |
+
|
| 109 |
+
We first need to import some functions and create some of our variables.
|
| 110 |
+
|
| 111 |
+
>>> from sympy import symbols, simplify
|
| 112 |
+
>>> from sympy.physics.mechanics import (
|
| 113 |
+
... mechanics_printing, dynamicsymbols, RigidBody, Particle,
|
| 114 |
+
... ReferenceFrame, PrismaticJoint, PinJoint, System)
|
| 115 |
+
>>> mechanics_printing(pretty_print=False)
|
| 116 |
+
>>> g, l = symbols('g l')
|
| 117 |
+
>>> F = dynamicsymbols('F')
|
| 118 |
+
|
| 119 |
+
The next step is to create bodies. It is also useful to create a frame for
|
| 120 |
+
locating the particle with respect to the pin joint later on, as a particle
|
| 121 |
+
does not have a body-fixed frame.
|
| 122 |
+
|
| 123 |
+
>>> rail = RigidBody('rail')
|
| 124 |
+
>>> cart = RigidBody('cart')
|
| 125 |
+
>>> bob = Particle('bob')
|
| 126 |
+
>>> bob_frame = ReferenceFrame('bob_frame')
|
| 127 |
+
|
| 128 |
+
Initialize the system, with the rail as the Newtonian reference. The body is
|
| 129 |
+
also automatically added to the system.
|
| 130 |
+
|
| 131 |
+
>>> system = System.from_newtonian(rail)
|
| 132 |
+
>>> print(system.bodies[0])
|
| 133 |
+
rail
|
| 134 |
+
|
| 135 |
+
Create the joints, while immediately also adding them to the system.
|
| 136 |
+
|
| 137 |
+
>>> system.add_joints(
|
| 138 |
+
... PrismaticJoint('slider', rail, cart, joint_axis=rail.x),
|
| 139 |
+
... PinJoint('pin', cart, bob, joint_axis=cart.z,
|
| 140 |
+
... child_interframe=bob_frame,
|
| 141 |
+
... child_point=l * bob_frame.y)
|
| 142 |
+
... )
|
| 143 |
+
>>> system.joints
|
| 144 |
+
(PrismaticJoint: slider parent: rail child: cart,
|
| 145 |
+
PinJoint: pin parent: cart child: bob)
|
| 146 |
+
|
| 147 |
+
While adding the joints, the associated generalized coordinates, generalized
|
| 148 |
+
speeds, kinematic differential equations and bodies are also added to the
|
| 149 |
+
system.
|
| 150 |
+
|
| 151 |
+
>>> system.q
|
| 152 |
+
Matrix([
|
| 153 |
+
[q_slider],
|
| 154 |
+
[ q_pin]])
|
| 155 |
+
>>> system.u
|
| 156 |
+
Matrix([
|
| 157 |
+
[u_slider],
|
| 158 |
+
[ u_pin]])
|
| 159 |
+
>>> system.kdes
|
| 160 |
+
Matrix([
|
| 161 |
+
[u_slider - q_slider'],
|
| 162 |
+
[ u_pin - q_pin']])
|
| 163 |
+
>>> [body.name for body in system.bodies]
|
| 164 |
+
['rail', 'cart', 'bob']
|
| 165 |
+
|
| 166 |
+
With the kinematics established, we can now apply gravity and the cart force
|
| 167 |
+
``F``.
|
| 168 |
+
|
| 169 |
+
>>> system.apply_uniform_gravity(-g * system.y)
|
| 170 |
+
>>> system.add_loads((cart.masscenter, F * rail.x))
|
| 171 |
+
>>> system.loads
|
| 172 |
+
((rail_masscenter, - g*rail_mass*rail_frame.y),
|
| 173 |
+
(cart_masscenter, - cart_mass*g*rail_frame.y),
|
| 174 |
+
(bob_masscenter, - bob_mass*g*rail_frame.y),
|
| 175 |
+
(cart_masscenter, F*rail_frame.x))
|
| 176 |
+
|
| 177 |
+
With the entire system defined, we can now form the equations of motion.
|
| 178 |
+
Before forming the equations of motion, one can also run some checks that
|
| 179 |
+
will try to identify some common errors.
|
| 180 |
+
|
| 181 |
+
>>> system.validate_system()
|
| 182 |
+
>>> system.form_eoms()
|
| 183 |
+
Matrix([
|
| 184 |
+
[bob_mass*l*u_pin**2*sin(q_pin) - bob_mass*l*cos(q_pin)*u_pin'
|
| 185 |
+
- (bob_mass + cart_mass)*u_slider' + F],
|
| 186 |
+
[ -bob_mass*g*l*sin(q_pin) - bob_mass*l**2*u_pin'
|
| 187 |
+
- bob_mass*l*cos(q_pin)*u_slider']])
|
| 188 |
+
>>> simplify(system.mass_matrix)
|
| 189 |
+
Matrix([
|
| 190 |
+
[ bob_mass + cart_mass, bob_mass*l*cos(q_pin)],
|
| 191 |
+
[bob_mass*l*cos(q_pin), bob_mass*l**2]])
|
| 192 |
+
>>> system.forcing
|
| 193 |
+
Matrix([
|
| 194 |
+
[bob_mass*l*u_pin**2*sin(q_pin) + F],
|
| 195 |
+
[ -bob_mass*g*l*sin(q_pin)]])
|
| 196 |
+
|
| 197 |
+
The complexity of the above example can be increased if we add a constraint
|
| 198 |
+
to prevent the particle from moving in the horizontal (x) direction. This
|
| 199 |
+
can be done by adding a holonomic constraint. After which we should also
|
| 200 |
+
redefine what our (in)dependent generalized coordinates and speeds are.
|
| 201 |
+
|
| 202 |
+
>>> system.add_holonomic_constraints(
|
| 203 |
+
... bob.masscenter.pos_from(rail.masscenter).dot(system.x)
|
| 204 |
+
... )
|
| 205 |
+
>>> system.q_ind = system.get_joint('pin').coordinates
|
| 206 |
+
>>> system.q_dep = system.get_joint('slider').coordinates
|
| 207 |
+
>>> system.u_ind = system.get_joint('pin').speeds
|
| 208 |
+
>>> system.u_dep = system.get_joint('slider').speeds
|
| 209 |
+
|
| 210 |
+
With the updated system the equations of motion can be formed again.
|
| 211 |
+
|
| 212 |
+
>>> system.validate_system()
|
| 213 |
+
>>> system.form_eoms()
|
| 214 |
+
Matrix([[-bob_mass*g*l*sin(q_pin)
|
| 215 |
+
- bob_mass*l**2*u_pin'
|
| 216 |
+
- bob_mass*l*cos(q_pin)*u_slider'
|
| 217 |
+
- l*(bob_mass*l*u_pin**2*sin(q_pin)
|
| 218 |
+
- bob_mass*l*cos(q_pin)*u_pin'
|
| 219 |
+
- (bob_mass + cart_mass)*u_slider')*cos(q_pin)
|
| 220 |
+
- l*F*cos(q_pin)]])
|
| 221 |
+
>>> simplify(system.mass_matrix)
|
| 222 |
+
Matrix([
|
| 223 |
+
[bob_mass*l**2*sin(q_pin)**2, -cart_mass*l*cos(q_pin)],
|
| 224 |
+
[ l*cos(q_pin), 1]])
|
| 225 |
+
>>> simplify(system.forcing)
|
| 226 |
+
Matrix([
|
| 227 |
+
[-l*(bob_mass*g*sin(q_pin) + bob_mass*l*u_pin**2*sin(2*q_pin)/2
|
| 228 |
+
+ F*cos(q_pin))],
|
| 229 |
+
[
|
| 230 |
+
l*u_pin**2*sin(q_pin)]])
|
| 231 |
+
|
| 232 |
+
"""
|
| 233 |
+
|
| 234 |
+
def __init__(self, frame=None, fixed_point=None):
|
| 235 |
+
"""Initialize the system.
|
| 236 |
+
|
| 237 |
+
Parameters
|
| 238 |
+
==========
|
| 239 |
+
|
| 240 |
+
frame : ReferenceFrame, optional
|
| 241 |
+
The inertial frame of the system. If none is supplied, a new frame
|
| 242 |
+
will be created.
|
| 243 |
+
fixed_point : Point, optional
|
| 244 |
+
A fixed point in the inertial reference frame. If none is supplied,
|
| 245 |
+
a new fixed_point will be created.
|
| 246 |
+
|
| 247 |
+
"""
|
| 248 |
+
if frame is None:
|
| 249 |
+
frame = ReferenceFrame('inertial_frame')
|
| 250 |
+
elif not isinstance(frame, ReferenceFrame):
|
| 251 |
+
raise TypeError('Frame must be an instance of ReferenceFrame.')
|
| 252 |
+
self._frame = frame
|
| 253 |
+
if fixed_point is None:
|
| 254 |
+
fixed_point = Point('inertial_point')
|
| 255 |
+
elif not isinstance(fixed_point, Point):
|
| 256 |
+
raise TypeError('Fixed point must be an instance of Point.')
|
| 257 |
+
self._fixed_point = fixed_point
|
| 258 |
+
self._fixed_point.set_vel(self._frame, 0)
|
| 259 |
+
self._q_ind = ImmutableMatrix(1, 0, []).T
|
| 260 |
+
self._q_dep = ImmutableMatrix(1, 0, []).T
|
| 261 |
+
self._u_ind = ImmutableMatrix(1, 0, []).T
|
| 262 |
+
self._u_dep = ImmutableMatrix(1, 0, []).T
|
| 263 |
+
self._u_aux = ImmutableMatrix(1, 0, []).T
|
| 264 |
+
self._kdes = ImmutableMatrix(1, 0, []).T
|
| 265 |
+
self._hol_coneqs = ImmutableMatrix(1, 0, []).T
|
| 266 |
+
self._nonhol_coneqs = ImmutableMatrix(1, 0, []).T
|
| 267 |
+
self._vel_constrs = None
|
| 268 |
+
self._bodies = []
|
| 269 |
+
self._joints = []
|
| 270 |
+
self._loads = []
|
| 271 |
+
self._actuators = []
|
| 272 |
+
self._eom_method = None
|
| 273 |
+
|
| 274 |
+
@classmethod
|
| 275 |
+
def from_newtonian(cls, newtonian):
|
| 276 |
+
"""Constructs the system with respect to a Newtonian body."""
|
| 277 |
+
if isinstance(newtonian, Particle):
|
| 278 |
+
raise TypeError('A Particle has no frame so cannot act as '
|
| 279 |
+
'the Newtonian.')
|
| 280 |
+
system = cls(frame=newtonian.frame, fixed_point=newtonian.masscenter)
|
| 281 |
+
system.add_bodies(newtonian)
|
| 282 |
+
return system
|
| 283 |
+
|
| 284 |
+
@property
|
| 285 |
+
def fixed_point(self):
|
| 286 |
+
"""Fixed point in the inertial reference frame."""
|
| 287 |
+
return self._fixed_point
|
| 288 |
+
|
| 289 |
+
@property
|
| 290 |
+
def frame(self):
|
| 291 |
+
"""Inertial reference frame of the system."""
|
| 292 |
+
return self._frame
|
| 293 |
+
|
| 294 |
+
@property
|
| 295 |
+
def x(self):
|
| 296 |
+
"""Unit vector fixed in the inertial reference frame."""
|
| 297 |
+
return self._frame.x
|
| 298 |
+
|
| 299 |
+
@property
|
| 300 |
+
def y(self):
|
| 301 |
+
"""Unit vector fixed in the inertial reference frame."""
|
| 302 |
+
return self._frame.y
|
| 303 |
+
|
| 304 |
+
@property
|
| 305 |
+
def z(self):
|
| 306 |
+
"""Unit vector fixed in the inertial reference frame."""
|
| 307 |
+
return self._frame.z
|
| 308 |
+
|
| 309 |
+
@property
|
| 310 |
+
def bodies(self):
|
| 311 |
+
"""Tuple of all bodies that have been added to the system."""
|
| 312 |
+
return tuple(self._bodies)
|
| 313 |
+
|
| 314 |
+
@bodies.setter
|
| 315 |
+
@_reset_eom_method
|
| 316 |
+
def bodies(self, bodies):
|
| 317 |
+
bodies = self._objects_to_list(bodies)
|
| 318 |
+
self._check_objects(bodies, [], BodyBase, 'Bodies', 'bodies')
|
| 319 |
+
self._bodies = bodies
|
| 320 |
+
|
| 321 |
+
@property
|
| 322 |
+
def joints(self):
|
| 323 |
+
"""Tuple of all joints that have been added to the system."""
|
| 324 |
+
return tuple(self._joints)
|
| 325 |
+
|
| 326 |
+
@joints.setter
|
| 327 |
+
@_reset_eom_method
|
| 328 |
+
def joints(self, joints):
|
| 329 |
+
joints = self._objects_to_list(joints)
|
| 330 |
+
self._check_objects(joints, [], Joint, 'Joints', 'joints')
|
| 331 |
+
self._joints = []
|
| 332 |
+
self.add_joints(*joints)
|
| 333 |
+
|
| 334 |
+
@property
|
| 335 |
+
def loads(self):
|
| 336 |
+
"""Tuple of loads that have been applied on the system."""
|
| 337 |
+
return tuple(self._loads)
|
| 338 |
+
|
| 339 |
+
@loads.setter
|
| 340 |
+
@_reset_eom_method
|
| 341 |
+
def loads(self, loads):
|
| 342 |
+
loads = self._objects_to_list(loads)
|
| 343 |
+
self._loads = [_parse_load(load) for load in loads]
|
| 344 |
+
|
| 345 |
+
@property
|
| 346 |
+
def actuators(self):
|
| 347 |
+
"""Tuple of actuators present in the system."""
|
| 348 |
+
return tuple(self._actuators)
|
| 349 |
+
|
| 350 |
+
@actuators.setter
|
| 351 |
+
@_reset_eom_method
|
| 352 |
+
def actuators(self, actuators):
|
| 353 |
+
actuators = self._objects_to_list(actuators)
|
| 354 |
+
self._check_objects(actuators, [], ActuatorBase, 'Actuators',
|
| 355 |
+
'actuators')
|
| 356 |
+
self._actuators = actuators
|
| 357 |
+
|
| 358 |
+
@property
|
| 359 |
+
def q(self):
|
| 360 |
+
"""Matrix of all the generalized coordinates with the independent
|
| 361 |
+
stacked upon the dependent."""
|
| 362 |
+
return self._q_ind.col_join(self._q_dep)
|
| 363 |
+
|
| 364 |
+
@property
|
| 365 |
+
def u(self):
|
| 366 |
+
"""Matrix of all the generalized speeds with the independent stacked
|
| 367 |
+
upon the dependent."""
|
| 368 |
+
return self._u_ind.col_join(self._u_dep)
|
| 369 |
+
|
| 370 |
+
@property
|
| 371 |
+
def q_ind(self):
|
| 372 |
+
"""Matrix of the independent generalized coordinates."""
|
| 373 |
+
return self._q_ind
|
| 374 |
+
|
| 375 |
+
@q_ind.setter
|
| 376 |
+
@_reset_eom_method
|
| 377 |
+
def q_ind(self, q_ind):
|
| 378 |
+
self._q_ind, self._q_dep = self._parse_coordinates(
|
| 379 |
+
self._objects_to_list(q_ind), True, [], self.q_dep, 'coordinates')
|
| 380 |
+
|
| 381 |
+
@property
|
| 382 |
+
def q_dep(self):
|
| 383 |
+
"""Matrix of the dependent generalized coordinates."""
|
| 384 |
+
return self._q_dep
|
| 385 |
+
|
| 386 |
+
@q_dep.setter
|
| 387 |
+
@_reset_eom_method
|
| 388 |
+
def q_dep(self, q_dep):
|
| 389 |
+
self._q_ind, self._q_dep = self._parse_coordinates(
|
| 390 |
+
self._objects_to_list(q_dep), False, self.q_ind, [], 'coordinates')
|
| 391 |
+
|
| 392 |
+
@property
|
| 393 |
+
def u_ind(self):
|
| 394 |
+
"""Matrix of the independent generalized speeds."""
|
| 395 |
+
return self._u_ind
|
| 396 |
+
|
| 397 |
+
@u_ind.setter
|
| 398 |
+
@_reset_eom_method
|
| 399 |
+
def u_ind(self, u_ind):
|
| 400 |
+
self._u_ind, self._u_dep = self._parse_coordinates(
|
| 401 |
+
self._objects_to_list(u_ind), True, [], self.u_dep, 'speeds')
|
| 402 |
+
|
| 403 |
+
@property
|
| 404 |
+
def u_dep(self):
|
| 405 |
+
"""Matrix of the dependent generalized speeds."""
|
| 406 |
+
return self._u_dep
|
| 407 |
+
|
| 408 |
+
@u_dep.setter
|
| 409 |
+
@_reset_eom_method
|
| 410 |
+
def u_dep(self, u_dep):
|
| 411 |
+
self._u_ind, self._u_dep = self._parse_coordinates(
|
| 412 |
+
self._objects_to_list(u_dep), False, self.u_ind, [], 'speeds')
|
| 413 |
+
|
| 414 |
+
@property
|
| 415 |
+
def u_aux(self):
|
| 416 |
+
"""Matrix of auxiliary generalized speeds."""
|
| 417 |
+
return self._u_aux
|
| 418 |
+
|
| 419 |
+
@u_aux.setter
|
| 420 |
+
@_reset_eom_method
|
| 421 |
+
def u_aux(self, u_aux):
|
| 422 |
+
self._u_aux = self._parse_coordinates(
|
| 423 |
+
self._objects_to_list(u_aux), True, [], [], 'u_auxiliary')[0]
|
| 424 |
+
|
| 425 |
+
@property
|
| 426 |
+
def kdes(self):
|
| 427 |
+
"""Kinematical differential equations as expressions equated to the zero
|
| 428 |
+
matrix. These equations describe the coupling between the generalized
|
| 429 |
+
coordinates and the generalized speeds."""
|
| 430 |
+
return self._kdes
|
| 431 |
+
|
| 432 |
+
@kdes.setter
|
| 433 |
+
@_reset_eom_method
|
| 434 |
+
def kdes(self, kdes):
|
| 435 |
+
kdes = self._objects_to_list(kdes)
|
| 436 |
+
self._kdes = self._parse_expressions(
|
| 437 |
+
kdes, [], 'kinematic differential equations')
|
| 438 |
+
|
| 439 |
+
@property
|
| 440 |
+
def holonomic_constraints(self):
|
| 441 |
+
"""Matrix with the holonomic constraints as expressions equated to the
|
| 442 |
+
zero matrix."""
|
| 443 |
+
return self._hol_coneqs
|
| 444 |
+
|
| 445 |
+
@holonomic_constraints.setter
|
| 446 |
+
@_reset_eom_method
|
| 447 |
+
def holonomic_constraints(self, constraints):
|
| 448 |
+
constraints = self._objects_to_list(constraints)
|
| 449 |
+
self._hol_coneqs = self._parse_expressions(
|
| 450 |
+
constraints, [], 'holonomic constraints')
|
| 451 |
+
|
| 452 |
+
@property
|
| 453 |
+
def nonholonomic_constraints(self):
|
| 454 |
+
"""Matrix with the nonholonomic constraints as expressions equated to
|
| 455 |
+
the zero matrix."""
|
| 456 |
+
return self._nonhol_coneqs
|
| 457 |
+
|
| 458 |
+
@nonholonomic_constraints.setter
|
| 459 |
+
@_reset_eom_method
|
| 460 |
+
def nonholonomic_constraints(self, constraints):
|
| 461 |
+
constraints = self._objects_to_list(constraints)
|
| 462 |
+
self._nonhol_coneqs = self._parse_expressions(
|
| 463 |
+
constraints, [], 'nonholonomic constraints')
|
| 464 |
+
|
| 465 |
+
@property
|
| 466 |
+
def velocity_constraints(self):
|
| 467 |
+
"""Matrix with the velocity constraints as expressions equated to the
|
| 468 |
+
zero matrix. The velocity constraints are by default derived from the
|
| 469 |
+
holonomic and nonholonomic constraints unless they are explicitly set.
|
| 470 |
+
"""
|
| 471 |
+
if self._vel_constrs is None:
|
| 472 |
+
return self.holonomic_constraints.diff(dynamicsymbols._t).col_join(
|
| 473 |
+
self.nonholonomic_constraints)
|
| 474 |
+
return self._vel_constrs
|
| 475 |
+
|
| 476 |
+
@velocity_constraints.setter
|
| 477 |
+
@_reset_eom_method
|
| 478 |
+
def velocity_constraints(self, constraints):
|
| 479 |
+
if constraints is None:
|
| 480 |
+
self._vel_constrs = None
|
| 481 |
+
return
|
| 482 |
+
constraints = self._objects_to_list(constraints)
|
| 483 |
+
self._vel_constrs = self._parse_expressions(
|
| 484 |
+
constraints, [], 'velocity constraints')
|
| 485 |
+
|
| 486 |
+
@property
|
| 487 |
+
def eom_method(self):
|
| 488 |
+
"""Backend for forming the equations of motion."""
|
| 489 |
+
return self._eom_method
|
| 490 |
+
|
| 491 |
+
@staticmethod
|
| 492 |
+
def _objects_to_list(lst):
|
| 493 |
+
"""Helper to convert passed objects to a list."""
|
| 494 |
+
if not iterable(lst): # Only one object
|
| 495 |
+
return [lst]
|
| 496 |
+
return list(lst[:]) # converts Matrix and tuple to flattened list
|
| 497 |
+
|
| 498 |
+
@staticmethod
|
| 499 |
+
def _check_objects(objects, obj_lst, expected_type, obj_name, type_name):
|
| 500 |
+
"""Helper to check the objects that are being added to the system.
|
| 501 |
+
|
| 502 |
+
Explanation
|
| 503 |
+
===========
|
| 504 |
+
This method checks that the objects that are being added to the system
|
| 505 |
+
are of the correct type and have not already been added. If any of the
|
| 506 |
+
objects are not of the correct type or have already been added, then
|
| 507 |
+
an error is raised.
|
| 508 |
+
|
| 509 |
+
Parameters
|
| 510 |
+
==========
|
| 511 |
+
objects : iterable
|
| 512 |
+
The objects that would be added to the system.
|
| 513 |
+
obj_lst : list
|
| 514 |
+
The list of objects that are already in the system.
|
| 515 |
+
expected_type : type
|
| 516 |
+
The type that the objects should be.
|
| 517 |
+
obj_name : str
|
| 518 |
+
The name of the category of objects. This string is used to
|
| 519 |
+
formulate the error message for the user.
|
| 520 |
+
type_name : str
|
| 521 |
+
The name of the type that the objects should be. This string is used
|
| 522 |
+
to formulate the error message for the user.
|
| 523 |
+
|
| 524 |
+
"""
|
| 525 |
+
seen = set(obj_lst)
|
| 526 |
+
duplicates = set()
|
| 527 |
+
wrong_types = set()
|
| 528 |
+
for obj in objects:
|
| 529 |
+
if not isinstance(obj, expected_type):
|
| 530 |
+
wrong_types.add(obj)
|
| 531 |
+
if obj in seen:
|
| 532 |
+
duplicates.add(obj)
|
| 533 |
+
else:
|
| 534 |
+
seen.add(obj)
|
| 535 |
+
if wrong_types:
|
| 536 |
+
raise TypeError(f'{obj_name} {wrong_types} are not {type_name}.')
|
| 537 |
+
if duplicates:
|
| 538 |
+
raise ValueError(f'{obj_name} {duplicates} have already been added '
|
| 539 |
+
f'to the system.')
|
| 540 |
+
|
| 541 |
+
def _parse_coordinates(self, new_coords, independent, old_coords_ind,
|
| 542 |
+
old_coords_dep, coord_type='coordinates'):
|
| 543 |
+
"""Helper to parse coordinates and speeds."""
|
| 544 |
+
# Construct lists of the independent and dependent coordinates
|
| 545 |
+
coords_ind, coords_dep = old_coords_ind[:], old_coords_dep[:]
|
| 546 |
+
if not iterable(independent):
|
| 547 |
+
independent = [independent] * len(new_coords)
|
| 548 |
+
for coord, indep in zip(new_coords, independent):
|
| 549 |
+
if indep:
|
| 550 |
+
coords_ind.append(coord)
|
| 551 |
+
else:
|
| 552 |
+
coords_dep.append(coord)
|
| 553 |
+
# Check types and duplicates
|
| 554 |
+
current = {'coordinates': self.q_ind[:] + self.q_dep[:],
|
| 555 |
+
'speeds': self.u_ind[:] + self.u_dep[:],
|
| 556 |
+
'u_auxiliary': self._u_aux[:],
|
| 557 |
+
coord_type: coords_ind + coords_dep}
|
| 558 |
+
_validate_coordinates(**current)
|
| 559 |
+
return (ImmutableMatrix(1, len(coords_ind), coords_ind).T,
|
| 560 |
+
ImmutableMatrix(1, len(coords_dep), coords_dep).T)
|
| 561 |
+
|
| 562 |
+
@staticmethod
|
| 563 |
+
def _parse_expressions(new_expressions, old_expressions, name,
|
| 564 |
+
check_negatives=False):
|
| 565 |
+
"""Helper to parse expressions like constraints."""
|
| 566 |
+
old_expressions = old_expressions[:]
|
| 567 |
+
new_expressions = list(new_expressions) # Converts a possible tuple
|
| 568 |
+
if check_negatives:
|
| 569 |
+
check_exprs = old_expressions + [-expr for expr in old_expressions]
|
| 570 |
+
else:
|
| 571 |
+
check_exprs = old_expressions
|
| 572 |
+
System._check_objects(new_expressions, check_exprs, Basic, name,
|
| 573 |
+
'expressions')
|
| 574 |
+
for expr in new_expressions:
|
| 575 |
+
if expr == 0:
|
| 576 |
+
raise ValueError(f'Parsed {name} are zero.')
|
| 577 |
+
return ImmutableMatrix(1, len(old_expressions) + len(new_expressions),
|
| 578 |
+
old_expressions + new_expressions).T
|
| 579 |
+
|
| 580 |
+
@_reset_eom_method
|
| 581 |
+
def add_coordinates(self, *coordinates, independent=True):
|
| 582 |
+
"""Add generalized coordinate(s) to the system.
|
| 583 |
+
|
| 584 |
+
Parameters
|
| 585 |
+
==========
|
| 586 |
+
|
| 587 |
+
*coordinates : dynamicsymbols
|
| 588 |
+
One or more generalized coordinates to be added to the system.
|
| 589 |
+
independent : bool or list of bool, optional
|
| 590 |
+
Boolean whether a coordinate is dependent or independent. The
|
| 591 |
+
default is True, so the coordinates are added as independent by
|
| 592 |
+
default.
|
| 593 |
+
|
| 594 |
+
"""
|
| 595 |
+
self._q_ind, self._q_dep = self._parse_coordinates(
|
| 596 |
+
coordinates, independent, self.q_ind, self.q_dep, 'coordinates')
|
| 597 |
+
|
| 598 |
+
@_reset_eom_method
|
| 599 |
+
def add_speeds(self, *speeds, independent=True):
|
| 600 |
+
"""Add generalized speed(s) to the system.
|
| 601 |
+
|
| 602 |
+
Parameters
|
| 603 |
+
==========
|
| 604 |
+
|
| 605 |
+
*speeds : dynamicsymbols
|
| 606 |
+
One or more generalized speeds to be added to the system.
|
| 607 |
+
independent : bool or list of bool, optional
|
| 608 |
+
Boolean whether a speed is dependent or independent. The default is
|
| 609 |
+
True, so the speeds are added as independent by default.
|
| 610 |
+
|
| 611 |
+
"""
|
| 612 |
+
self._u_ind, self._u_dep = self._parse_coordinates(
|
| 613 |
+
speeds, independent, self.u_ind, self.u_dep, 'speeds')
|
| 614 |
+
|
| 615 |
+
@_reset_eom_method
|
| 616 |
+
def add_auxiliary_speeds(self, *speeds):
|
| 617 |
+
"""Add auxiliary speed(s) to the system.
|
| 618 |
+
|
| 619 |
+
Parameters
|
| 620 |
+
==========
|
| 621 |
+
|
| 622 |
+
*speeds : dynamicsymbols
|
| 623 |
+
One or more auxiliary speeds to be added to the system.
|
| 624 |
+
|
| 625 |
+
"""
|
| 626 |
+
self._u_aux = self._parse_coordinates(
|
| 627 |
+
speeds, True, self._u_aux, [], 'u_auxiliary')[0]
|
| 628 |
+
|
| 629 |
+
@_reset_eom_method
|
| 630 |
+
def add_kdes(self, *kdes):
|
| 631 |
+
"""Add kinematic differential equation(s) to the system.
|
| 632 |
+
|
| 633 |
+
Parameters
|
| 634 |
+
==========
|
| 635 |
+
|
| 636 |
+
*kdes : Expr
|
| 637 |
+
One or more kinematic differential equations.
|
| 638 |
+
|
| 639 |
+
"""
|
| 640 |
+
self._kdes = self._parse_expressions(
|
| 641 |
+
kdes, self.kdes, 'kinematic differential equations',
|
| 642 |
+
check_negatives=True)
|
| 643 |
+
|
| 644 |
+
@_reset_eom_method
|
| 645 |
+
def add_holonomic_constraints(self, *constraints):
|
| 646 |
+
"""Add holonomic constraint(s) to the system.
|
| 647 |
+
|
| 648 |
+
Parameters
|
| 649 |
+
==========
|
| 650 |
+
|
| 651 |
+
*constraints : Expr
|
| 652 |
+
One or more holonomic constraints, which are expressions that should
|
| 653 |
+
be zero.
|
| 654 |
+
|
| 655 |
+
"""
|
| 656 |
+
self._hol_coneqs = self._parse_expressions(
|
| 657 |
+
constraints, self._hol_coneqs, 'holonomic constraints',
|
| 658 |
+
check_negatives=True)
|
| 659 |
+
|
| 660 |
+
@_reset_eom_method
|
| 661 |
+
def add_nonholonomic_constraints(self, *constraints):
|
| 662 |
+
"""Add nonholonomic constraint(s) to the system.
|
| 663 |
+
|
| 664 |
+
Parameters
|
| 665 |
+
==========
|
| 666 |
+
|
| 667 |
+
*constraints : Expr
|
| 668 |
+
One or more nonholonomic constraints, which are expressions that
|
| 669 |
+
should be zero.
|
| 670 |
+
|
| 671 |
+
"""
|
| 672 |
+
self._nonhol_coneqs = self._parse_expressions(
|
| 673 |
+
constraints, self._nonhol_coneqs, 'nonholonomic constraints',
|
| 674 |
+
check_negatives=True)
|
| 675 |
+
|
| 676 |
+
@_reset_eom_method
|
| 677 |
+
def add_bodies(self, *bodies):
|
| 678 |
+
"""Add body(ies) to the system.
|
| 679 |
+
|
| 680 |
+
Parameters
|
| 681 |
+
==========
|
| 682 |
+
|
| 683 |
+
bodies : Particle or RigidBody
|
| 684 |
+
One or more bodies.
|
| 685 |
+
|
| 686 |
+
"""
|
| 687 |
+
self._check_objects(bodies, self.bodies, BodyBase, 'Bodies', 'bodies')
|
| 688 |
+
self._bodies.extend(bodies)
|
| 689 |
+
|
| 690 |
+
@_reset_eom_method
|
| 691 |
+
def add_loads(self, *loads):
|
| 692 |
+
"""Add load(s) to the system.
|
| 693 |
+
|
| 694 |
+
Parameters
|
| 695 |
+
==========
|
| 696 |
+
|
| 697 |
+
*loads : Force or Torque
|
| 698 |
+
One or more loads.
|
| 699 |
+
|
| 700 |
+
"""
|
| 701 |
+
loads = [_parse_load(load) for load in loads] # Checks the loads
|
| 702 |
+
self._loads.extend(loads)
|
| 703 |
+
|
| 704 |
+
@_reset_eom_method
|
| 705 |
+
def apply_uniform_gravity(self, acceleration):
|
| 706 |
+
"""Apply uniform gravity to all bodies in the system by adding loads.
|
| 707 |
+
|
| 708 |
+
Parameters
|
| 709 |
+
==========
|
| 710 |
+
|
| 711 |
+
acceleration : Vector
|
| 712 |
+
The acceleration due to gravity.
|
| 713 |
+
|
| 714 |
+
"""
|
| 715 |
+
self.add_loads(*gravity(acceleration, *self.bodies))
|
| 716 |
+
|
| 717 |
+
@_reset_eom_method
|
| 718 |
+
def add_actuators(self, *actuators):
|
| 719 |
+
"""Add actuator(s) to the system.
|
| 720 |
+
|
| 721 |
+
Parameters
|
| 722 |
+
==========
|
| 723 |
+
|
| 724 |
+
*actuators : subclass of ActuatorBase
|
| 725 |
+
One or more actuators.
|
| 726 |
+
|
| 727 |
+
"""
|
| 728 |
+
self._check_objects(actuators, self.actuators, ActuatorBase,
|
| 729 |
+
'Actuators', 'actuators')
|
| 730 |
+
self._actuators.extend(actuators)
|
| 731 |
+
|
| 732 |
+
@_reset_eom_method
|
| 733 |
+
def add_joints(self, *joints):
|
| 734 |
+
"""Add joint(s) to the system.
|
| 735 |
+
|
| 736 |
+
Explanation
|
| 737 |
+
===========
|
| 738 |
+
|
| 739 |
+
This methods adds one or more joints to the system including its
|
| 740 |
+
associated objects, i.e. generalized coordinates, generalized speeds,
|
| 741 |
+
kinematic differential equations and the bodies.
|
| 742 |
+
|
| 743 |
+
Parameters
|
| 744 |
+
==========
|
| 745 |
+
|
| 746 |
+
*joints : subclass of Joint
|
| 747 |
+
One or more joints.
|
| 748 |
+
|
| 749 |
+
Notes
|
| 750 |
+
=====
|
| 751 |
+
|
| 752 |
+
For the generalized coordinates, generalized speeds and bodies it is
|
| 753 |
+
checked whether they are already known by the system instance. If they
|
| 754 |
+
are, then they are not added. The kinematic differential equations are
|
| 755 |
+
however always added to the system, so you should not also manually add
|
| 756 |
+
those on beforehand.
|
| 757 |
+
|
| 758 |
+
"""
|
| 759 |
+
self._check_objects(joints, self.joints, Joint, 'Joints', 'joints')
|
| 760 |
+
self._joints.extend(joints)
|
| 761 |
+
coordinates, speeds, kdes, bodies = (OrderedSet() for _ in range(4))
|
| 762 |
+
for joint in joints:
|
| 763 |
+
coordinates.update(joint.coordinates)
|
| 764 |
+
speeds.update(joint.speeds)
|
| 765 |
+
kdes.update(joint.kdes)
|
| 766 |
+
bodies.update((joint.parent, joint.child))
|
| 767 |
+
coordinates = coordinates.difference(self.q)
|
| 768 |
+
speeds = speeds.difference(self.u)
|
| 769 |
+
kdes = kdes.difference(self.kdes[:] + (-self.kdes)[:])
|
| 770 |
+
bodies = bodies.difference(self.bodies)
|
| 771 |
+
self.add_coordinates(*tuple(coordinates))
|
| 772 |
+
self.add_speeds(*tuple(speeds))
|
| 773 |
+
self.add_kdes(*(kde for kde in tuple(kdes) if not kde == 0))
|
| 774 |
+
self.add_bodies(*tuple(bodies))
|
| 775 |
+
|
| 776 |
+
def get_body(self, name):
|
| 777 |
+
"""Retrieve a body from the system by name.
|
| 778 |
+
|
| 779 |
+
Parameters
|
| 780 |
+
==========
|
| 781 |
+
|
| 782 |
+
name : str
|
| 783 |
+
The name of the body to retrieve.
|
| 784 |
+
|
| 785 |
+
Returns
|
| 786 |
+
=======
|
| 787 |
+
|
| 788 |
+
RigidBody or Particle
|
| 789 |
+
The body with the given name, or None if no such body exists.
|
| 790 |
+
|
| 791 |
+
"""
|
| 792 |
+
for body in self._bodies:
|
| 793 |
+
if body.name == name:
|
| 794 |
+
return body
|
| 795 |
+
|
| 796 |
+
def get_joint(self, name):
|
| 797 |
+
"""Retrieve a joint from the system by name.
|
| 798 |
+
|
| 799 |
+
Parameters
|
| 800 |
+
==========
|
| 801 |
+
|
| 802 |
+
name : str
|
| 803 |
+
The name of the joint to retrieve.
|
| 804 |
+
|
| 805 |
+
Returns
|
| 806 |
+
=======
|
| 807 |
+
|
| 808 |
+
subclass of Joint
|
| 809 |
+
The joint with the given name, or None if no such joint exists.
|
| 810 |
+
|
| 811 |
+
"""
|
| 812 |
+
for joint in self._joints:
|
| 813 |
+
if joint.name == name:
|
| 814 |
+
return joint
|
| 815 |
+
|
| 816 |
+
def _form_eoms(self):
|
| 817 |
+
return self.form_eoms()
|
| 818 |
+
|
| 819 |
+
def form_eoms(self, eom_method=KanesMethod, **kwargs):
|
| 820 |
+
"""Form the equations of motion of the system.
|
| 821 |
+
|
| 822 |
+
Parameters
|
| 823 |
+
==========
|
| 824 |
+
|
| 825 |
+
eom_method : subclass of KanesMethod or LagrangesMethod
|
| 826 |
+
Backend class to be used for forming the equations of motion. The
|
| 827 |
+
default is ``KanesMethod``.
|
| 828 |
+
|
| 829 |
+
Returns
|
| 830 |
+
========
|
| 831 |
+
|
| 832 |
+
ImmutableMatrix
|
| 833 |
+
Vector of equations of motions.
|
| 834 |
+
|
| 835 |
+
Examples
|
| 836 |
+
========
|
| 837 |
+
|
| 838 |
+
This is a simple example for a one degree of freedom translational
|
| 839 |
+
spring-mass-damper.
|
| 840 |
+
|
| 841 |
+
>>> from sympy import S, symbols
|
| 842 |
+
>>> from sympy.physics.mechanics import (
|
| 843 |
+
... LagrangesMethod, dynamicsymbols, PrismaticJoint, Particle,
|
| 844 |
+
... RigidBody, System)
|
| 845 |
+
>>> q = dynamicsymbols('q')
|
| 846 |
+
>>> qd = dynamicsymbols('q', 1)
|
| 847 |
+
>>> m, k, b = symbols('m k b')
|
| 848 |
+
>>> wall = RigidBody('W')
|
| 849 |
+
>>> system = System.from_newtonian(wall)
|
| 850 |
+
>>> bob = Particle('P', mass=m)
|
| 851 |
+
>>> bob.potential_energy = S.Half * k * q**2
|
| 852 |
+
>>> system.add_joints(PrismaticJoint('J', wall, bob, q, qd))
|
| 853 |
+
>>> system.add_loads((bob.masscenter, b * qd * system.x))
|
| 854 |
+
>>> system.form_eoms(LagrangesMethod)
|
| 855 |
+
Matrix([[-b*Derivative(q(t), t) + k*q(t) + m*Derivative(q(t), (t, 2))]])
|
| 856 |
+
|
| 857 |
+
We can also solve for the states using the 'rhs' method.
|
| 858 |
+
|
| 859 |
+
>>> system.rhs()
|
| 860 |
+
Matrix([
|
| 861 |
+
[ Derivative(q(t), t)],
|
| 862 |
+
[(b*Derivative(q(t), t) - k*q(t))/m]])
|
| 863 |
+
|
| 864 |
+
"""
|
| 865 |
+
# KanesMethod does not accept empty iterables
|
| 866 |
+
loads = self.loads + tuple(
|
| 867 |
+
load for act in self.actuators for load in act.to_loads())
|
| 868 |
+
loads = loads if loads else None
|
| 869 |
+
if issubclass(eom_method, KanesMethod):
|
| 870 |
+
disallowed_kwargs = {
|
| 871 |
+
"frame", "q_ind", "u_ind", "kd_eqs", "q_dependent",
|
| 872 |
+
"u_dependent", "u_auxiliary", "configuration_constraints",
|
| 873 |
+
"velocity_constraints", "forcelist", "bodies"}
|
| 874 |
+
wrong_kwargs = disallowed_kwargs.intersection(kwargs)
|
| 875 |
+
if wrong_kwargs:
|
| 876 |
+
raise ValueError(
|
| 877 |
+
f"The following keyword arguments are not allowed to be "
|
| 878 |
+
f"overwritten in {eom_method.__name__}: {wrong_kwargs}.")
|
| 879 |
+
kwargs = {"frame": self.frame, "q_ind": self.q_ind,
|
| 880 |
+
"u_ind": self.u_ind, "kd_eqs": self.kdes,
|
| 881 |
+
"q_dependent": self.q_dep, "u_dependent": self.u_dep,
|
| 882 |
+
"configuration_constraints": self.holonomic_constraints,
|
| 883 |
+
"velocity_constraints": self.velocity_constraints,
|
| 884 |
+
"u_auxiliary": self.u_aux,
|
| 885 |
+
"forcelist": loads, "bodies": self.bodies,
|
| 886 |
+
"explicit_kinematics": False, **kwargs}
|
| 887 |
+
self._eom_method = eom_method(**kwargs)
|
| 888 |
+
elif issubclass(eom_method, LagrangesMethod):
|
| 889 |
+
disallowed_kwargs = {
|
| 890 |
+
"frame", "qs", "forcelist", "bodies", "hol_coneqs",
|
| 891 |
+
"nonhol_coneqs", "Lagrangian"}
|
| 892 |
+
wrong_kwargs = disallowed_kwargs.intersection(kwargs)
|
| 893 |
+
if wrong_kwargs:
|
| 894 |
+
raise ValueError(
|
| 895 |
+
f"The following keyword arguments are not allowed to be "
|
| 896 |
+
f"overwritten in {eom_method.__name__}: {wrong_kwargs}.")
|
| 897 |
+
kwargs = {"frame": self.frame, "qs": self.q, "forcelist": loads,
|
| 898 |
+
"bodies": self.bodies,
|
| 899 |
+
"hol_coneqs": self.holonomic_constraints,
|
| 900 |
+
"nonhol_coneqs": self.nonholonomic_constraints, **kwargs}
|
| 901 |
+
if "Lagrangian" not in kwargs:
|
| 902 |
+
kwargs["Lagrangian"] = Lagrangian(kwargs["frame"],
|
| 903 |
+
*kwargs["bodies"])
|
| 904 |
+
self._eom_method = eom_method(**kwargs)
|
| 905 |
+
else:
|
| 906 |
+
raise NotImplementedError(f'{eom_method} has not been implemented.')
|
| 907 |
+
return self.eom_method._form_eoms()
|
| 908 |
+
|
| 909 |
+
def rhs(self, inv_method=None):
|
| 910 |
+
"""Compute the equations of motion in the explicit form.
|
| 911 |
+
|
| 912 |
+
Parameters
|
| 913 |
+
==========
|
| 914 |
+
|
| 915 |
+
inv_method : str
|
| 916 |
+
The specific sympy inverse matrix calculation method to use. For a
|
| 917 |
+
list of valid methods, see
|
| 918 |
+
:meth:`~sympy.matrices.matrixbase.MatrixBase.inv`
|
| 919 |
+
|
| 920 |
+
Returns
|
| 921 |
+
========
|
| 922 |
+
|
| 923 |
+
ImmutableMatrix
|
| 924 |
+
Equations of motion in the explicit form.
|
| 925 |
+
|
| 926 |
+
See Also
|
| 927 |
+
========
|
| 928 |
+
|
| 929 |
+
sympy.physics.mechanics.kane.KanesMethod.rhs:
|
| 930 |
+
KanesMethod's ``rhs`` function.
|
| 931 |
+
sympy.physics.mechanics.lagrange.LagrangesMethod.rhs:
|
| 932 |
+
LagrangesMethod's ``rhs`` function.
|
| 933 |
+
|
| 934 |
+
"""
|
| 935 |
+
return self.eom_method.rhs(inv_method=inv_method)
|
| 936 |
+
|
| 937 |
+
@property
|
| 938 |
+
def mass_matrix(self):
|
| 939 |
+
r"""The mass matrix of the system.
|
| 940 |
+
|
| 941 |
+
Explanation
|
| 942 |
+
===========
|
| 943 |
+
|
| 944 |
+
The mass matrix $M_d$ and the forcing vector $f_d$ of a system describe
|
| 945 |
+
the system's dynamics according to the following equations:
|
| 946 |
+
|
| 947 |
+
.. math::
|
| 948 |
+
M_d \dot{u} = f_d
|
| 949 |
+
|
| 950 |
+
where $\dot{u}$ is the time derivative of the generalized speeds.
|
| 951 |
+
|
| 952 |
+
"""
|
| 953 |
+
return self.eom_method.mass_matrix
|
| 954 |
+
|
| 955 |
+
@property
|
| 956 |
+
def mass_matrix_full(self):
|
| 957 |
+
r"""The mass matrix of the system, augmented by the kinematic
|
| 958 |
+
differential equations in explicit or implicit form.
|
| 959 |
+
|
| 960 |
+
Explanation
|
| 961 |
+
===========
|
| 962 |
+
|
| 963 |
+
The full mass matrix $M_m$ and the full forcing vector $f_m$ of a system
|
| 964 |
+
describe the dynamics and kinematics according to the following
|
| 965 |
+
equation:
|
| 966 |
+
|
| 967 |
+
.. math::
|
| 968 |
+
M_m \dot{x} = f_m
|
| 969 |
+
|
| 970 |
+
where $x$ is the state vector stacking $q$ and $u$.
|
| 971 |
+
|
| 972 |
+
"""
|
| 973 |
+
return self.eom_method.mass_matrix_full
|
| 974 |
+
|
| 975 |
+
@property
|
| 976 |
+
def forcing(self):
|
| 977 |
+
"""The forcing vector of the system."""
|
| 978 |
+
return self.eom_method.forcing
|
| 979 |
+
|
| 980 |
+
@property
|
| 981 |
+
def forcing_full(self):
|
| 982 |
+
"""The forcing vector of the system, augmented by the kinematic
|
| 983 |
+
differential equations in explicit or implicit form."""
|
| 984 |
+
return self.eom_method.forcing_full
|
| 985 |
+
|
| 986 |
+
def validate_system(self, eom_method=KanesMethod, check_duplicates=False):
|
| 987 |
+
"""Validates the system using some basic checks.
|
| 988 |
+
|
| 989 |
+
Explanation
|
| 990 |
+
===========
|
| 991 |
+
|
| 992 |
+
This method validates the system based on the following checks:
|
| 993 |
+
|
| 994 |
+
- The number of dependent generalized coordinates should equal the
|
| 995 |
+
number of holonomic constraints.
|
| 996 |
+
- All generalized coordinates defined by the joints should also be known
|
| 997 |
+
to the system.
|
| 998 |
+
- If ``KanesMethod`` is used as a ``eom_method``:
|
| 999 |
+
- All generalized speeds and kinematic differential equations
|
| 1000 |
+
defined by the joints should also be known to the system.
|
| 1001 |
+
- The number of dependent generalized speeds should equal the number
|
| 1002 |
+
of velocity constraints.
|
| 1003 |
+
- The number of generalized coordinates should be less than or equal
|
| 1004 |
+
to the number of generalized speeds.
|
| 1005 |
+
- The number of generalized coordinates should equal the number of
|
| 1006 |
+
kinematic differential equations.
|
| 1007 |
+
- If ``LagrangesMethod`` is used as ``eom_method``:
|
| 1008 |
+
- There should not be any generalized speeds that are not
|
| 1009 |
+
derivatives of the generalized coordinates (this includes the
|
| 1010 |
+
generalized speeds defined by the joints).
|
| 1011 |
+
|
| 1012 |
+
Parameters
|
| 1013 |
+
==========
|
| 1014 |
+
|
| 1015 |
+
eom_method : subclass of KanesMethod or LagrangesMethod
|
| 1016 |
+
Backend class that will be used for forming the equations of motion.
|
| 1017 |
+
There are different checks for the different backends. The default
|
| 1018 |
+
is ``KanesMethod``.
|
| 1019 |
+
check_duplicates : bool
|
| 1020 |
+
Boolean whether the system should be checked for duplicate
|
| 1021 |
+
definitions. The default is False, because duplicates are already
|
| 1022 |
+
checked when adding objects to the system.
|
| 1023 |
+
|
| 1024 |
+
Notes
|
| 1025 |
+
=====
|
| 1026 |
+
|
| 1027 |
+
This method is not guaranteed to be backwards compatible as it may
|
| 1028 |
+
improve over time. The method can become both more and less strict in
|
| 1029 |
+
certain areas. However a well-defined system should always pass all
|
| 1030 |
+
these tests.
|
| 1031 |
+
|
| 1032 |
+
"""
|
| 1033 |
+
msgs = []
|
| 1034 |
+
# Save some data in variables
|
| 1035 |
+
n_hc = self.holonomic_constraints.shape[0]
|
| 1036 |
+
n_vc = self.velocity_constraints.shape[0]
|
| 1037 |
+
n_q_dep, n_u_dep = self.q_dep.shape[0], self.u_dep.shape[0]
|
| 1038 |
+
q_set, u_set = set(self.q), set(self.u)
|
| 1039 |
+
n_q, n_u = len(q_set), len(u_set)
|
| 1040 |
+
# Check number of holonomic constraints
|
| 1041 |
+
if n_q_dep != n_hc:
|
| 1042 |
+
msgs.append(filldedent(f"""
|
| 1043 |
+
The number of dependent generalized coordinates {n_q_dep} should be
|
| 1044 |
+
equal to the number of holonomic constraints {n_hc}."""))
|
| 1045 |
+
# Check if all joint coordinates and speeds are present
|
| 1046 |
+
missing_q = set()
|
| 1047 |
+
for joint in self.joints:
|
| 1048 |
+
missing_q.update(set(joint.coordinates).difference(q_set))
|
| 1049 |
+
if missing_q:
|
| 1050 |
+
msgs.append(filldedent(f"""
|
| 1051 |
+
The generalized coordinates {missing_q} used in joints are not added
|
| 1052 |
+
to the system."""))
|
| 1053 |
+
# Method dependent checks
|
| 1054 |
+
if issubclass(eom_method, KanesMethod):
|
| 1055 |
+
n_kdes = len(self.kdes)
|
| 1056 |
+
missing_kdes, missing_u = set(), set()
|
| 1057 |
+
for joint in self.joints:
|
| 1058 |
+
missing_u.update(set(joint.speeds).difference(u_set))
|
| 1059 |
+
missing_kdes.update(set(joint.kdes).difference(
|
| 1060 |
+
self.kdes[:] + (-self.kdes)[:]))
|
| 1061 |
+
if missing_u:
|
| 1062 |
+
msgs.append(filldedent(f"""
|
| 1063 |
+
The generalized speeds {missing_u} used in joints are not added
|
| 1064 |
+
to the system."""))
|
| 1065 |
+
if missing_kdes:
|
| 1066 |
+
msgs.append(filldedent(f"""
|
| 1067 |
+
The kinematic differential equations {missing_kdes} used in
|
| 1068 |
+
joints are not added to the system."""))
|
| 1069 |
+
if n_u_dep != n_vc:
|
| 1070 |
+
msgs.append(filldedent(f"""
|
| 1071 |
+
The number of dependent generalized speeds {n_u_dep} should be
|
| 1072 |
+
equal to the number of velocity constraints {n_vc}."""))
|
| 1073 |
+
if n_q > n_u:
|
| 1074 |
+
msgs.append(filldedent(f"""
|
| 1075 |
+
The number of generalized coordinates {n_q} should be less than
|
| 1076 |
+
or equal to the number of generalized speeds {n_u}."""))
|
| 1077 |
+
if n_u != n_kdes:
|
| 1078 |
+
msgs.append(filldedent(f"""
|
| 1079 |
+
The number of generalized speeds {n_u} should be equal to the
|
| 1080 |
+
number of kinematic differential equations {n_kdes}."""))
|
| 1081 |
+
elif issubclass(eom_method, LagrangesMethod):
|
| 1082 |
+
not_qdots = set(self.u).difference(self.q.diff(dynamicsymbols._t))
|
| 1083 |
+
for joint in self.joints:
|
| 1084 |
+
not_qdots.update(set(
|
| 1085 |
+
joint.speeds).difference(self.q.diff(dynamicsymbols._t)))
|
| 1086 |
+
if not_qdots:
|
| 1087 |
+
msgs.append(filldedent(f"""
|
| 1088 |
+
The generalized speeds {not_qdots} are not supported by this
|
| 1089 |
+
method. Only derivatives of the generalized coordinates are
|
| 1090 |
+
supported. If these symbols are used in your expressions, then
|
| 1091 |
+
this will result in wrong equations of motion."""))
|
| 1092 |
+
if self.u_aux:
|
| 1093 |
+
msgs.append(filldedent(f"""
|
| 1094 |
+
This method does not support auxiliary speeds. If these symbols
|
| 1095 |
+
are used in your expressions, then this will result in wrong
|
| 1096 |
+
equations of motion. The auxiliary speeds are {self.u_aux}."""))
|
| 1097 |
+
else:
|
| 1098 |
+
raise NotImplementedError(f'{eom_method} has not been implemented.')
|
| 1099 |
+
if check_duplicates: # Should be redundant
|
| 1100 |
+
duplicates_to_check = [('generalized coordinates', self.q),
|
| 1101 |
+
('generalized speeds', self.u),
|
| 1102 |
+
('auxiliary speeds', self.u_aux),
|
| 1103 |
+
('bodies', self.bodies),
|
| 1104 |
+
('joints', self.joints)]
|
| 1105 |
+
for name, lst in duplicates_to_check:
|
| 1106 |
+
seen = set()
|
| 1107 |
+
duplicates = {x for x in lst if x in seen or seen.add(x)}
|
| 1108 |
+
if duplicates:
|
| 1109 |
+
msgs.append(filldedent(f"""
|
| 1110 |
+
The {name} {duplicates} exist multiple times within the
|
| 1111 |
+
system."""))
|
| 1112 |
+
if msgs:
|
| 1113 |
+
raise ValueError('\n'.join(msgs))
|
| 1114 |
+
|
| 1115 |
+
|
| 1116 |
+
class SymbolicSystem:
|
| 1117 |
+
"""SymbolicSystem is a class that contains all the information about a
|
| 1118 |
+
system in a symbolic format such as the equations of motions and the bodies
|
| 1119 |
+
and loads in the system.
|
| 1120 |
+
|
| 1121 |
+
There are three ways that the equations of motion can be described for
|
| 1122 |
+
Symbolic System:
|
| 1123 |
+
|
| 1124 |
+
|
| 1125 |
+
[1] Explicit form where the kinematics and dynamics are combined
|
| 1126 |
+
x' = F_1(x, t, r, p)
|
| 1127 |
+
|
| 1128 |
+
[2] Implicit form where the kinematics and dynamics are combined
|
| 1129 |
+
M_2(x, p) x' = F_2(x, t, r, p)
|
| 1130 |
+
|
| 1131 |
+
[3] Implicit form where the kinematics and dynamics are separate
|
| 1132 |
+
M_3(q, p) u' = F_3(q, u, t, r, p)
|
| 1133 |
+
q' = G(q, u, t, r, p)
|
| 1134 |
+
|
| 1135 |
+
where
|
| 1136 |
+
|
| 1137 |
+
x : states, e.g. [q, u]
|
| 1138 |
+
t : time
|
| 1139 |
+
r : specified (exogenous) inputs
|
| 1140 |
+
p : constants
|
| 1141 |
+
q : generalized coordinates
|
| 1142 |
+
u : generalized speeds
|
| 1143 |
+
F_1 : right hand side of the combined equations in explicit form
|
| 1144 |
+
F_2 : right hand side of the combined equations in implicit form
|
| 1145 |
+
F_3 : right hand side of the dynamical equations in implicit form
|
| 1146 |
+
M_2 : mass matrix of the combined equations in implicit form
|
| 1147 |
+
M_3 : mass matrix of the dynamical equations in implicit form
|
| 1148 |
+
G : right hand side of the kinematical differential equations
|
| 1149 |
+
|
| 1150 |
+
Parameters
|
| 1151 |
+
==========
|
| 1152 |
+
|
| 1153 |
+
coord_states : ordered iterable of functions of time
|
| 1154 |
+
This input will either be a collection of the coordinates or states
|
| 1155 |
+
of the system depending on whether or not the speeds are also
|
| 1156 |
+
given. If speeds are specified this input will be assumed to
|
| 1157 |
+
be the coordinates otherwise this input will be assumed to
|
| 1158 |
+
be the states.
|
| 1159 |
+
|
| 1160 |
+
right_hand_side : Matrix
|
| 1161 |
+
This variable is the right hand side of the equations of motion in
|
| 1162 |
+
any of the forms. The specific form will be assumed depending on
|
| 1163 |
+
whether a mass matrix or coordinate derivatives are given.
|
| 1164 |
+
|
| 1165 |
+
speeds : ordered iterable of functions of time, optional
|
| 1166 |
+
This is a collection of the generalized speeds of the system. If
|
| 1167 |
+
given it will be assumed that the first argument (coord_states)
|
| 1168 |
+
will represent the generalized coordinates of the system.
|
| 1169 |
+
|
| 1170 |
+
mass_matrix : Matrix, optional
|
| 1171 |
+
The matrix of the implicit forms of the equations of motion (forms
|
| 1172 |
+
[2] and [3]). The distinction between the forms is determined by
|
| 1173 |
+
whether or not the coordinate derivatives are passed in. If
|
| 1174 |
+
they are given form [3] will be assumed otherwise form [2] is
|
| 1175 |
+
assumed.
|
| 1176 |
+
|
| 1177 |
+
coordinate_derivatives : Matrix, optional
|
| 1178 |
+
The right hand side of the kinematical equations in explicit form.
|
| 1179 |
+
If given it will be assumed that the equations of motion are being
|
| 1180 |
+
entered in form [3].
|
| 1181 |
+
|
| 1182 |
+
alg_con : Iterable, optional
|
| 1183 |
+
The indexes of the rows in the equations of motion that contain
|
| 1184 |
+
algebraic constraints instead of differential equations. If the
|
| 1185 |
+
equations are input in form [3], it will be assumed the indexes are
|
| 1186 |
+
referencing the mass_matrix/right_hand_side combination and not the
|
| 1187 |
+
coordinate_derivatives.
|
| 1188 |
+
|
| 1189 |
+
output_eqns : Dictionary, optional
|
| 1190 |
+
Any output equations that are desired to be tracked are stored in a
|
| 1191 |
+
dictionary where the key corresponds to the name given for the
|
| 1192 |
+
specific equation and the value is the equation itself in symbolic
|
| 1193 |
+
form
|
| 1194 |
+
|
| 1195 |
+
coord_idxs : Iterable, optional
|
| 1196 |
+
If coord_states corresponds to the states rather than the
|
| 1197 |
+
coordinates this variable will tell SymbolicSystem which indexes of
|
| 1198 |
+
the states correspond to generalized coordinates.
|
| 1199 |
+
|
| 1200 |
+
speed_idxs : Iterable, optional
|
| 1201 |
+
If coord_states corresponds to the states rather than the
|
| 1202 |
+
coordinates this variable will tell SymbolicSystem which indexes of
|
| 1203 |
+
the states correspond to generalized speeds.
|
| 1204 |
+
|
| 1205 |
+
bodies : iterable of Body/Rigidbody objects, optional
|
| 1206 |
+
Iterable containing the bodies of the system
|
| 1207 |
+
|
| 1208 |
+
loads : iterable of load instances (described below), optional
|
| 1209 |
+
Iterable containing the loads of the system where forces are given
|
| 1210 |
+
by (point of application, force vector) and torques are given by
|
| 1211 |
+
(reference frame acting upon, torque vector). Ex [(point, force),
|
| 1212 |
+
(ref_frame, torque)]
|
| 1213 |
+
|
| 1214 |
+
Attributes
|
| 1215 |
+
==========
|
| 1216 |
+
|
| 1217 |
+
coordinates : Matrix, shape(n, 1)
|
| 1218 |
+
This is a matrix containing the generalized coordinates of the system
|
| 1219 |
+
|
| 1220 |
+
speeds : Matrix, shape(m, 1)
|
| 1221 |
+
This is a matrix containing the generalized speeds of the system
|
| 1222 |
+
|
| 1223 |
+
states : Matrix, shape(o, 1)
|
| 1224 |
+
This is a matrix containing the state variables of the system
|
| 1225 |
+
|
| 1226 |
+
alg_con : List
|
| 1227 |
+
This list contains the indices of the algebraic constraints in the
|
| 1228 |
+
combined equations of motion. The presence of these constraints
|
| 1229 |
+
requires that a DAE solver be used instead of an ODE solver.
|
| 1230 |
+
If the system is given in form [3] the alg_con variable will be
|
| 1231 |
+
adjusted such that it is a representation of the combined kinematics
|
| 1232 |
+
and dynamics thus make sure it always matches the mass matrix
|
| 1233 |
+
entered.
|
| 1234 |
+
|
| 1235 |
+
dyn_implicit_mat : Matrix, shape(m, m)
|
| 1236 |
+
This is the M matrix in form [3] of the equations of motion (the mass
|
| 1237 |
+
matrix or generalized inertia matrix of the dynamical equations of
|
| 1238 |
+
motion in implicit form).
|
| 1239 |
+
|
| 1240 |
+
dyn_implicit_rhs : Matrix, shape(m, 1)
|
| 1241 |
+
This is the F vector in form [3] of the equations of motion (the right
|
| 1242 |
+
hand side of the dynamical equations of motion in implicit form).
|
| 1243 |
+
|
| 1244 |
+
comb_implicit_mat : Matrix, shape(o, o)
|
| 1245 |
+
This is the M matrix in form [2] of the equations of motion.
|
| 1246 |
+
This matrix contains a block diagonal structure where the top
|
| 1247 |
+
left block (the first rows) represent the matrix in the
|
| 1248 |
+
implicit form of the kinematical equations and the bottom right
|
| 1249 |
+
block (the last rows) represent the matrix in the implicit form
|
| 1250 |
+
of the dynamical equations.
|
| 1251 |
+
|
| 1252 |
+
comb_implicit_rhs : Matrix, shape(o, 1)
|
| 1253 |
+
This is the F vector in form [2] of the equations of motion. The top
|
| 1254 |
+
part of the vector represents the right hand side of the implicit form
|
| 1255 |
+
of the kinemaical equations and the bottom of the vector represents the
|
| 1256 |
+
right hand side of the implicit form of the dynamical equations of
|
| 1257 |
+
motion.
|
| 1258 |
+
|
| 1259 |
+
comb_explicit_rhs : Matrix, shape(o, 1)
|
| 1260 |
+
This vector represents the right hand side of the combined equations of
|
| 1261 |
+
motion in explicit form (form [1] from above).
|
| 1262 |
+
|
| 1263 |
+
kin_explicit_rhs : Matrix, shape(m, 1)
|
| 1264 |
+
This is the right hand side of the explicit form of the kinematical
|
| 1265 |
+
equations of motion as can be seen in form [3] (the G matrix).
|
| 1266 |
+
|
| 1267 |
+
output_eqns : Dictionary
|
| 1268 |
+
If output equations were given they are stored in a dictionary where
|
| 1269 |
+
the key corresponds to the name given for the specific equation and
|
| 1270 |
+
the value is the equation itself in symbolic form
|
| 1271 |
+
|
| 1272 |
+
bodies : Tuple
|
| 1273 |
+
If the bodies in the system were given they are stored in a tuple for
|
| 1274 |
+
future access
|
| 1275 |
+
|
| 1276 |
+
loads : Tuple
|
| 1277 |
+
If the loads in the system were given they are stored in a tuple for
|
| 1278 |
+
future access. This includes forces and torques where forces are given
|
| 1279 |
+
by (point of application, force vector) and torques are given by
|
| 1280 |
+
(reference frame acted upon, torque vector).
|
| 1281 |
+
|
| 1282 |
+
Example
|
| 1283 |
+
=======
|
| 1284 |
+
|
| 1285 |
+
As a simple example, the dynamics of a simple pendulum will be input into a
|
| 1286 |
+
SymbolicSystem object manually. First some imports will be needed and then
|
| 1287 |
+
symbols will be set up for the length of the pendulum (l), mass at the end
|
| 1288 |
+
of the pendulum (m), and a constant for gravity (g). ::
|
| 1289 |
+
|
| 1290 |
+
>>> from sympy import Matrix, sin, symbols
|
| 1291 |
+
>>> from sympy.physics.mechanics import dynamicsymbols, SymbolicSystem
|
| 1292 |
+
>>> l, m, g = symbols('l m g')
|
| 1293 |
+
|
| 1294 |
+
The system will be defined by an angle of theta from the vertical and a
|
| 1295 |
+
generalized speed of omega will be used where omega = theta_dot. ::
|
| 1296 |
+
|
| 1297 |
+
>>> theta, omega = dynamicsymbols('theta omega')
|
| 1298 |
+
|
| 1299 |
+
Now the equations of motion are ready to be formed and passed to the
|
| 1300 |
+
SymbolicSystem object. ::
|
| 1301 |
+
|
| 1302 |
+
>>> kin_explicit_rhs = Matrix([omega])
|
| 1303 |
+
>>> dyn_implicit_mat = Matrix([l**2 * m])
|
| 1304 |
+
>>> dyn_implicit_rhs = Matrix([-g * l * m * sin(theta)])
|
| 1305 |
+
>>> symsystem = SymbolicSystem([theta], dyn_implicit_rhs, [omega],
|
| 1306 |
+
... dyn_implicit_mat)
|
| 1307 |
+
|
| 1308 |
+
Notes
|
| 1309 |
+
=====
|
| 1310 |
+
|
| 1311 |
+
m : number of generalized speeds
|
| 1312 |
+
n : number of generalized coordinates
|
| 1313 |
+
o : number of states
|
| 1314 |
+
|
| 1315 |
+
"""
|
| 1316 |
+
|
| 1317 |
+
def __init__(self, coord_states, right_hand_side, speeds=None,
|
| 1318 |
+
mass_matrix=None, coordinate_derivatives=None, alg_con=None,
|
| 1319 |
+
output_eqns={}, coord_idxs=None, speed_idxs=None, bodies=None,
|
| 1320 |
+
loads=None):
|
| 1321 |
+
"""Initializes a SymbolicSystem object"""
|
| 1322 |
+
|
| 1323 |
+
# Extract information on speeds, coordinates and states
|
| 1324 |
+
if speeds is None:
|
| 1325 |
+
self._states = Matrix(coord_states)
|
| 1326 |
+
|
| 1327 |
+
if coord_idxs is None:
|
| 1328 |
+
self._coordinates = None
|
| 1329 |
+
else:
|
| 1330 |
+
coords = [coord_states[i] for i in coord_idxs]
|
| 1331 |
+
self._coordinates = Matrix(coords)
|
| 1332 |
+
|
| 1333 |
+
if speed_idxs is None:
|
| 1334 |
+
self._speeds = None
|
| 1335 |
+
else:
|
| 1336 |
+
speeds_inter = [coord_states[i] for i in speed_idxs]
|
| 1337 |
+
self._speeds = Matrix(speeds_inter)
|
| 1338 |
+
else:
|
| 1339 |
+
self._coordinates = Matrix(coord_states)
|
| 1340 |
+
self._speeds = Matrix(speeds)
|
| 1341 |
+
self._states = self._coordinates.col_join(self._speeds)
|
| 1342 |
+
|
| 1343 |
+
# Extract equations of motion form
|
| 1344 |
+
if coordinate_derivatives is not None:
|
| 1345 |
+
self._kin_explicit_rhs = coordinate_derivatives
|
| 1346 |
+
self._dyn_implicit_rhs = right_hand_side
|
| 1347 |
+
self._dyn_implicit_mat = mass_matrix
|
| 1348 |
+
self._comb_implicit_rhs = None
|
| 1349 |
+
self._comb_implicit_mat = None
|
| 1350 |
+
self._comb_explicit_rhs = None
|
| 1351 |
+
elif mass_matrix is not None:
|
| 1352 |
+
self._kin_explicit_rhs = None
|
| 1353 |
+
self._dyn_implicit_rhs = None
|
| 1354 |
+
self._dyn_implicit_mat = None
|
| 1355 |
+
self._comb_implicit_rhs = right_hand_side
|
| 1356 |
+
self._comb_implicit_mat = mass_matrix
|
| 1357 |
+
self._comb_explicit_rhs = None
|
| 1358 |
+
else:
|
| 1359 |
+
self._kin_explicit_rhs = None
|
| 1360 |
+
self._dyn_implicit_rhs = None
|
| 1361 |
+
self._dyn_implicit_mat = None
|
| 1362 |
+
self._comb_implicit_rhs = None
|
| 1363 |
+
self._comb_implicit_mat = None
|
| 1364 |
+
self._comb_explicit_rhs = right_hand_side
|
| 1365 |
+
|
| 1366 |
+
# Set the remainder of the inputs as instance attributes
|
| 1367 |
+
if alg_con is not None and coordinate_derivatives is not None:
|
| 1368 |
+
alg_con = [i + len(coordinate_derivatives) for i in alg_con]
|
| 1369 |
+
self._alg_con = alg_con
|
| 1370 |
+
self.output_eqns = output_eqns
|
| 1371 |
+
|
| 1372 |
+
# Change the body and loads iterables to tuples if they are not tuples
|
| 1373 |
+
# already
|
| 1374 |
+
if not isinstance(bodies, tuple) and bodies is not None:
|
| 1375 |
+
bodies = tuple(bodies)
|
| 1376 |
+
if not isinstance(loads, tuple) and loads is not None:
|
| 1377 |
+
loads = tuple(loads)
|
| 1378 |
+
self._bodies = bodies
|
| 1379 |
+
self._loads = loads
|
| 1380 |
+
|
| 1381 |
+
@property
|
| 1382 |
+
def coordinates(self):
|
| 1383 |
+
"""Returns the column matrix of the generalized coordinates"""
|
| 1384 |
+
if self._coordinates is None:
|
| 1385 |
+
raise AttributeError("The coordinates were not specified.")
|
| 1386 |
+
else:
|
| 1387 |
+
return self._coordinates
|
| 1388 |
+
|
| 1389 |
+
@property
|
| 1390 |
+
def speeds(self):
|
| 1391 |
+
"""Returns the column matrix of generalized speeds"""
|
| 1392 |
+
if self._speeds is None:
|
| 1393 |
+
raise AttributeError("The speeds were not specified.")
|
| 1394 |
+
else:
|
| 1395 |
+
return self._speeds
|
| 1396 |
+
|
| 1397 |
+
@property
|
| 1398 |
+
def states(self):
|
| 1399 |
+
"""Returns the column matrix of the state variables"""
|
| 1400 |
+
return self._states
|
| 1401 |
+
|
| 1402 |
+
@property
|
| 1403 |
+
def alg_con(self):
|
| 1404 |
+
"""Returns a list with the indices of the rows containing algebraic
|
| 1405 |
+
constraints in the combined form of the equations of motion"""
|
| 1406 |
+
return self._alg_con
|
| 1407 |
+
|
| 1408 |
+
@property
|
| 1409 |
+
def dyn_implicit_mat(self):
|
| 1410 |
+
"""Returns the matrix, M, corresponding to the dynamic equations in
|
| 1411 |
+
implicit form, M x' = F, where the kinematical equations are not
|
| 1412 |
+
included"""
|
| 1413 |
+
if self._dyn_implicit_mat is None:
|
| 1414 |
+
raise AttributeError("dyn_implicit_mat is not specified for "
|
| 1415 |
+
"equations of motion form [1] or [2].")
|
| 1416 |
+
else:
|
| 1417 |
+
return self._dyn_implicit_mat
|
| 1418 |
+
|
| 1419 |
+
@property
|
| 1420 |
+
def dyn_implicit_rhs(self):
|
| 1421 |
+
"""Returns the column matrix, F, corresponding to the dynamic equations
|
| 1422 |
+
in implicit form, M x' = F, where the kinematical equations are not
|
| 1423 |
+
included"""
|
| 1424 |
+
if self._dyn_implicit_rhs is None:
|
| 1425 |
+
raise AttributeError("dyn_implicit_rhs is not specified for "
|
| 1426 |
+
"equations of motion form [1] or [2].")
|
| 1427 |
+
else:
|
| 1428 |
+
return self._dyn_implicit_rhs
|
| 1429 |
+
|
| 1430 |
+
@property
|
| 1431 |
+
def comb_implicit_mat(self):
|
| 1432 |
+
"""Returns the matrix, M, corresponding to the equations of motion in
|
| 1433 |
+
implicit form (form [2]), M x' = F, where the kinematical equations are
|
| 1434 |
+
included"""
|
| 1435 |
+
if self._comb_implicit_mat is None:
|
| 1436 |
+
if self._dyn_implicit_mat is not None:
|
| 1437 |
+
num_kin_eqns = len(self._kin_explicit_rhs)
|
| 1438 |
+
num_dyn_eqns = len(self._dyn_implicit_rhs)
|
| 1439 |
+
zeros1 = zeros(num_kin_eqns, num_dyn_eqns)
|
| 1440 |
+
zeros2 = zeros(num_dyn_eqns, num_kin_eqns)
|
| 1441 |
+
inter1 = eye(num_kin_eqns).row_join(zeros1)
|
| 1442 |
+
inter2 = zeros2.row_join(self._dyn_implicit_mat)
|
| 1443 |
+
self._comb_implicit_mat = inter1.col_join(inter2)
|
| 1444 |
+
return self._comb_implicit_mat
|
| 1445 |
+
else:
|
| 1446 |
+
raise AttributeError("comb_implicit_mat is not specified for "
|
| 1447 |
+
"equations of motion form [1].")
|
| 1448 |
+
else:
|
| 1449 |
+
return self._comb_implicit_mat
|
| 1450 |
+
|
| 1451 |
+
@property
|
| 1452 |
+
def comb_implicit_rhs(self):
|
| 1453 |
+
"""Returns the column matrix, F, corresponding to the equations of
|
| 1454 |
+
motion in implicit form (form [2]), M x' = F, where the kinematical
|
| 1455 |
+
equations are included"""
|
| 1456 |
+
if self._comb_implicit_rhs is None:
|
| 1457 |
+
if self._dyn_implicit_rhs is not None:
|
| 1458 |
+
kin_inter = self._kin_explicit_rhs
|
| 1459 |
+
dyn_inter = self._dyn_implicit_rhs
|
| 1460 |
+
self._comb_implicit_rhs = kin_inter.col_join(dyn_inter)
|
| 1461 |
+
return self._comb_implicit_rhs
|
| 1462 |
+
else:
|
| 1463 |
+
raise AttributeError("comb_implicit_mat is not specified for "
|
| 1464 |
+
"equations of motion in form [1].")
|
| 1465 |
+
else:
|
| 1466 |
+
return self._comb_implicit_rhs
|
| 1467 |
+
|
| 1468 |
+
def compute_explicit_form(self):
|
| 1469 |
+
"""If the explicit right hand side of the combined equations of motion
|
| 1470 |
+
is to provided upon initialization, this method will calculate it. This
|
| 1471 |
+
calculation can potentially take awhile to compute."""
|
| 1472 |
+
if self._comb_explicit_rhs is not None:
|
| 1473 |
+
raise AttributeError("comb_explicit_rhs is already formed.")
|
| 1474 |
+
|
| 1475 |
+
inter1 = getattr(self, 'kin_explicit_rhs', None)
|
| 1476 |
+
if inter1 is not None:
|
| 1477 |
+
inter2 = self._dyn_implicit_mat.LUsolve(self._dyn_implicit_rhs)
|
| 1478 |
+
out = inter1.col_join(inter2)
|
| 1479 |
+
else:
|
| 1480 |
+
out = self._comb_implicit_mat.LUsolve(self._comb_implicit_rhs)
|
| 1481 |
+
|
| 1482 |
+
self._comb_explicit_rhs = out
|
| 1483 |
+
|
| 1484 |
+
@property
|
| 1485 |
+
def comb_explicit_rhs(self):
|
| 1486 |
+
"""Returns the right hand side of the equations of motion in explicit
|
| 1487 |
+
form, x' = F, where the kinematical equations are included"""
|
| 1488 |
+
if self._comb_explicit_rhs is None:
|
| 1489 |
+
raise AttributeError("Please run .combute_explicit_form before "
|
| 1490 |
+
"attempting to access comb_explicit_rhs.")
|
| 1491 |
+
else:
|
| 1492 |
+
return self._comb_explicit_rhs
|
| 1493 |
+
|
| 1494 |
+
@property
|
| 1495 |
+
def kin_explicit_rhs(self):
|
| 1496 |
+
"""Returns the right hand side of the kinematical equations in explicit
|
| 1497 |
+
form, q' = G"""
|
| 1498 |
+
if self._kin_explicit_rhs is None:
|
| 1499 |
+
raise AttributeError("kin_explicit_rhs is not specified for "
|
| 1500 |
+
"equations of motion form [1] or [2].")
|
| 1501 |
+
else:
|
| 1502 |
+
return self._kin_explicit_rhs
|
| 1503 |
+
|
| 1504 |
+
def dynamic_symbols(self):
|
| 1505 |
+
"""Returns a column matrix containing all of the symbols in the system
|
| 1506 |
+
that depend on time"""
|
| 1507 |
+
# Create a list of all of the expressions in the equations of motion
|
| 1508 |
+
if self._comb_explicit_rhs is None:
|
| 1509 |
+
eom_expressions = (self.comb_implicit_mat[:] +
|
| 1510 |
+
self.comb_implicit_rhs[:])
|
| 1511 |
+
else:
|
| 1512 |
+
eom_expressions = (self._comb_explicit_rhs[:])
|
| 1513 |
+
|
| 1514 |
+
functions_of_time = set()
|
| 1515 |
+
for expr in eom_expressions:
|
| 1516 |
+
functions_of_time = functions_of_time.union(
|
| 1517 |
+
find_dynamicsymbols(expr))
|
| 1518 |
+
functions_of_time = functions_of_time.union(self._states)
|
| 1519 |
+
|
| 1520 |
+
return tuple(functions_of_time)
|
| 1521 |
+
|
| 1522 |
+
def constant_symbols(self):
|
| 1523 |
+
"""Returns a column matrix containing all of the symbols in the system
|
| 1524 |
+
that do not depend on time"""
|
| 1525 |
+
# Create a list of all of the expressions in the equations of motion
|
| 1526 |
+
if self._comb_explicit_rhs is None:
|
| 1527 |
+
eom_expressions = (self.comb_implicit_mat[:] +
|
| 1528 |
+
self.comb_implicit_rhs[:])
|
| 1529 |
+
else:
|
| 1530 |
+
eom_expressions = (self._comb_explicit_rhs[:])
|
| 1531 |
+
|
| 1532 |
+
constants = set()
|
| 1533 |
+
for expr in eom_expressions:
|
| 1534 |
+
constants = constants.union(expr.free_symbols)
|
| 1535 |
+
constants.remove(dynamicsymbols._t)
|
| 1536 |
+
|
| 1537 |
+
return tuple(constants)
|
| 1538 |
+
|
| 1539 |
+
@property
|
| 1540 |
+
def bodies(self):
|
| 1541 |
+
"""Returns the bodies in the system"""
|
| 1542 |
+
if self._bodies is None:
|
| 1543 |
+
raise AttributeError("bodies were not specified for the system.")
|
| 1544 |
+
else:
|
| 1545 |
+
return self._bodies
|
| 1546 |
+
|
| 1547 |
+
@property
|
| 1548 |
+
def loads(self):
|
| 1549 |
+
"""Returns the loads in the system"""
|
| 1550 |
+
if self._loads is None:
|
| 1551 |
+
raise AttributeError("loads were not specified for the system.")
|
| 1552 |
+
else:
|
| 1553 |
+
return self._loads
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__init__.py
ADDED
|
File without changes
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (185 Bytes). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_actuator.cpython-310.pyc
ADDED
|
Binary file (25.2 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_body.cpython-310.pyc
ADDED
|
Binary file (10.4 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_functions.cpython-310.pyc
ADDED
|
Binary file (11.3 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_inertia.cpython-310.pyc
ADDED
|
Binary file (3.15 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_joint.cpython-310.pyc
ADDED
|
Binary file (42.5 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_jointsmethod.cpython-310.pyc
ADDED
|
Binary file (9.71 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane.cpython-310.pyc
ADDED
|
Binary file (13.5 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane2.cpython-310.pyc
ADDED
|
Binary file (11.4 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane3.cpython-310.pyc
ADDED
|
Binary file (5.43 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane4.cpython-310.pyc
ADDED
|
Binary file (2.37 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_kane5.cpython-310.pyc
ADDED
|
Binary file (5.15 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_lagrange.cpython-310.pyc
ADDED
|
Binary file (6.14 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_lagrange2.cpython-310.pyc
ADDED
|
Binary file (1.18 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_linearize.cpython-310.pyc
ADDED
|
Binary file (9.97 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_loads.cpython-310.pyc
ADDED
|
Binary file (3.06 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_method.cpython-310.pyc
ADDED
|
Binary file (551 Bytes). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_models.cpython-310.pyc
ADDED
|
Binary file (4.22 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_particle.cpython-310.pyc
ADDED
|
Binary file (3.12 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_pathway.cpython-310.pyc
ADDED
|
Binary file (23.3 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_rigidbody.cpython-310.pyc
ADDED
|
Binary file (6.55 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_system.cpython-310.pyc
ADDED
|
Binary file (5.93 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_system_class.cpython-310.pyc
ADDED
|
Binary file (30.9 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/__pycache__/test_wrapping_geometry.cpython-310.pyc
ADDED
|
Binary file (9.2 kB). View file
|
|
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/test_actuator.py
ADDED
|
@@ -0,0 +1,851 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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from sympy import (
|
| 6 |
+
S,
|
| 7 |
+
Matrix,
|
| 8 |
+
Symbol,
|
| 9 |
+
SympifyError,
|
| 10 |
+
sqrt,
|
| 11 |
+
Abs
|
| 12 |
+
)
|
| 13 |
+
from sympy.physics.mechanics import (
|
| 14 |
+
ActuatorBase,
|
| 15 |
+
Force,
|
| 16 |
+
ForceActuator,
|
| 17 |
+
KanesMethod,
|
| 18 |
+
LinearDamper,
|
| 19 |
+
LinearPathway,
|
| 20 |
+
LinearSpring,
|
| 21 |
+
Particle,
|
| 22 |
+
PinJoint,
|
| 23 |
+
Point,
|
| 24 |
+
ReferenceFrame,
|
| 25 |
+
RigidBody,
|
| 26 |
+
TorqueActuator,
|
| 27 |
+
Vector,
|
| 28 |
+
dynamicsymbols,
|
| 29 |
+
DuffingSpring,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
from sympy.core.expr import Expr as ExprType
|
| 33 |
+
|
| 34 |
+
target = RigidBody('target')
|
| 35 |
+
reaction = RigidBody('reaction')
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class TestForceActuator:
|
| 39 |
+
|
| 40 |
+
@pytest.fixture(autouse=True)
|
| 41 |
+
def _linear_pathway_fixture(self):
|
| 42 |
+
self.force = Symbol('F')
|
| 43 |
+
self.pA = Point('pA')
|
| 44 |
+
self.pB = Point('pB')
|
| 45 |
+
self.pathway = LinearPathway(self.pA, self.pB)
|
| 46 |
+
self.q1 = dynamicsymbols('q1')
|
| 47 |
+
self.q2 = dynamicsymbols('q2')
|
| 48 |
+
self.q3 = dynamicsymbols('q3')
|
| 49 |
+
self.q1d = dynamicsymbols('q1', 1)
|
| 50 |
+
self.q2d = dynamicsymbols('q2', 1)
|
| 51 |
+
self.q3d = dynamicsymbols('q3', 1)
|
| 52 |
+
self.N = ReferenceFrame('N')
|
| 53 |
+
|
| 54 |
+
def test_is_actuator_base_subclass(self):
|
| 55 |
+
assert issubclass(ForceActuator, ActuatorBase)
|
| 56 |
+
|
| 57 |
+
@pytest.mark.parametrize(
|
| 58 |
+
'force, expected_force',
|
| 59 |
+
[
|
| 60 |
+
(1, S.One),
|
| 61 |
+
(S.One, S.One),
|
| 62 |
+
(Symbol('F'), Symbol('F')),
|
| 63 |
+
(dynamicsymbols('F'), dynamicsymbols('F')),
|
| 64 |
+
(Symbol('F')**2 + Symbol('F'), Symbol('F')**2 + Symbol('F')),
|
| 65 |
+
]
|
| 66 |
+
)
|
| 67 |
+
def test_valid_constructor_force(self, force, expected_force):
|
| 68 |
+
instance = ForceActuator(force, self.pathway)
|
| 69 |
+
assert isinstance(instance, ForceActuator)
|
| 70 |
+
assert hasattr(instance, 'force')
|
| 71 |
+
assert isinstance(instance.force, ExprType)
|
| 72 |
+
assert instance.force == expected_force
|
| 73 |
+
|
| 74 |
+
@pytest.mark.parametrize('force', [None, 'F'])
|
| 75 |
+
def test_invalid_constructor_force_not_sympifyable(self, force):
|
| 76 |
+
with pytest.raises(SympifyError):
|
| 77 |
+
_ = ForceActuator(force, self.pathway)
|
| 78 |
+
|
| 79 |
+
@pytest.mark.parametrize(
|
| 80 |
+
'pathway',
|
| 81 |
+
[
|
| 82 |
+
LinearPathway(Point('pA'), Point('pB')),
|
| 83 |
+
]
|
| 84 |
+
)
|
| 85 |
+
def test_valid_constructor_pathway(self, pathway):
|
| 86 |
+
instance = ForceActuator(self.force, pathway)
|
| 87 |
+
assert isinstance(instance, ForceActuator)
|
| 88 |
+
assert hasattr(instance, 'pathway')
|
| 89 |
+
assert isinstance(instance.pathway, LinearPathway)
|
| 90 |
+
assert instance.pathway == pathway
|
| 91 |
+
|
| 92 |
+
def test_invalid_constructor_pathway_not_pathway_base(self):
|
| 93 |
+
with pytest.raises(TypeError):
|
| 94 |
+
_ = ForceActuator(self.force, None)
|
| 95 |
+
|
| 96 |
+
@pytest.mark.parametrize(
|
| 97 |
+
'property_name, fixture_attr_name',
|
| 98 |
+
[
|
| 99 |
+
('force', 'force'),
|
| 100 |
+
('pathway', 'pathway'),
|
| 101 |
+
]
|
| 102 |
+
)
|
| 103 |
+
def test_properties_are_immutable(self, property_name, fixture_attr_name):
|
| 104 |
+
instance = ForceActuator(self.force, self.pathway)
|
| 105 |
+
value = getattr(self, fixture_attr_name)
|
| 106 |
+
with pytest.raises(AttributeError):
|
| 107 |
+
setattr(instance, property_name, value)
|
| 108 |
+
|
| 109 |
+
def test_repr(self):
|
| 110 |
+
actuator = ForceActuator(self.force, self.pathway)
|
| 111 |
+
expected = "ForceActuator(F, LinearPathway(pA, pB))"
|
| 112 |
+
assert repr(actuator) == expected
|
| 113 |
+
|
| 114 |
+
def test_to_loads_static_pathway(self):
|
| 115 |
+
self.pB.set_pos(self.pA, 2*self.N.x)
|
| 116 |
+
actuator = ForceActuator(self.force, self.pathway)
|
| 117 |
+
expected = [
|
| 118 |
+
(self.pA, - self.force*self.N.x),
|
| 119 |
+
(self.pB, self.force*self.N.x),
|
| 120 |
+
]
|
| 121 |
+
assert actuator.to_loads() == expected
|
| 122 |
+
|
| 123 |
+
def test_to_loads_2D_pathway(self):
|
| 124 |
+
self.pB.set_pos(self.pA, 2*self.q1*self.N.x)
|
| 125 |
+
actuator = ForceActuator(self.force, self.pathway)
|
| 126 |
+
expected = [
|
| 127 |
+
(self.pA, - self.force*(self.q1/sqrt(self.q1**2))*self.N.x),
|
| 128 |
+
(self.pB, self.force*(self.q1/sqrt(self.q1**2))*self.N.x),
|
| 129 |
+
]
|
| 130 |
+
assert actuator.to_loads() == expected
|
| 131 |
+
|
| 132 |
+
def test_to_loads_3D_pathway(self):
|
| 133 |
+
self.pB.set_pos(
|
| 134 |
+
self.pA,
|
| 135 |
+
self.q1*self.N.x - self.q2*self.N.y + 2*self.q3*self.N.z,
|
| 136 |
+
)
|
| 137 |
+
actuator = ForceActuator(self.force, self.pathway)
|
| 138 |
+
length = sqrt(self.q1**2 + self.q2**2 + 4*self.q3**2)
|
| 139 |
+
pO_force = (
|
| 140 |
+
- self.force*self.q1*self.N.x/length
|
| 141 |
+
+ self.force*self.q2*self.N.y/length
|
| 142 |
+
- 2*self.force*self.q3*self.N.z/length
|
| 143 |
+
)
|
| 144 |
+
pI_force = (
|
| 145 |
+
self.force*self.q1*self.N.x/length
|
| 146 |
+
- self.force*self.q2*self.N.y/length
|
| 147 |
+
+ 2*self.force*self.q3*self.N.z/length
|
| 148 |
+
)
|
| 149 |
+
expected = [
|
| 150 |
+
(self.pA, pO_force),
|
| 151 |
+
(self.pB, pI_force),
|
| 152 |
+
]
|
| 153 |
+
assert actuator.to_loads() == expected
|
| 154 |
+
|
| 155 |
+
|
| 156 |
+
class TestLinearSpring:
|
| 157 |
+
|
| 158 |
+
@pytest.fixture(autouse=True)
|
| 159 |
+
def _linear_spring_fixture(self):
|
| 160 |
+
self.stiffness = Symbol('k')
|
| 161 |
+
self.l = Symbol('l')
|
| 162 |
+
self.pA = Point('pA')
|
| 163 |
+
self.pB = Point('pB')
|
| 164 |
+
self.pathway = LinearPathway(self.pA, self.pB)
|
| 165 |
+
self.q = dynamicsymbols('q')
|
| 166 |
+
self.N = ReferenceFrame('N')
|
| 167 |
+
|
| 168 |
+
def test_is_force_actuator_subclass(self):
|
| 169 |
+
assert issubclass(LinearSpring, ForceActuator)
|
| 170 |
+
|
| 171 |
+
def test_is_actuator_base_subclass(self):
|
| 172 |
+
assert issubclass(LinearSpring, ActuatorBase)
|
| 173 |
+
|
| 174 |
+
@pytest.mark.parametrize(
|
| 175 |
+
(
|
| 176 |
+
'stiffness, '
|
| 177 |
+
'expected_stiffness, '
|
| 178 |
+
'equilibrium_length, '
|
| 179 |
+
'expected_equilibrium_length, '
|
| 180 |
+
'force'
|
| 181 |
+
),
|
| 182 |
+
[
|
| 183 |
+
(
|
| 184 |
+
1,
|
| 185 |
+
S.One,
|
| 186 |
+
0,
|
| 187 |
+
S.Zero,
|
| 188 |
+
-sqrt(dynamicsymbols('q')**2),
|
| 189 |
+
),
|
| 190 |
+
(
|
| 191 |
+
Symbol('k'),
|
| 192 |
+
Symbol('k'),
|
| 193 |
+
0,
|
| 194 |
+
S.Zero,
|
| 195 |
+
-Symbol('k')*sqrt(dynamicsymbols('q')**2),
|
| 196 |
+
),
|
| 197 |
+
(
|
| 198 |
+
Symbol('k'),
|
| 199 |
+
Symbol('k'),
|
| 200 |
+
S.Zero,
|
| 201 |
+
S.Zero,
|
| 202 |
+
-Symbol('k')*sqrt(dynamicsymbols('q')**2),
|
| 203 |
+
),
|
| 204 |
+
(
|
| 205 |
+
Symbol('k'),
|
| 206 |
+
Symbol('k'),
|
| 207 |
+
Symbol('l'),
|
| 208 |
+
Symbol('l'),
|
| 209 |
+
-Symbol('k')*(sqrt(dynamicsymbols('q')**2) - Symbol('l')),
|
| 210 |
+
),
|
| 211 |
+
]
|
| 212 |
+
)
|
| 213 |
+
def test_valid_constructor(
|
| 214 |
+
self,
|
| 215 |
+
stiffness,
|
| 216 |
+
expected_stiffness,
|
| 217 |
+
equilibrium_length,
|
| 218 |
+
expected_equilibrium_length,
|
| 219 |
+
force,
|
| 220 |
+
):
|
| 221 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 222 |
+
spring = LinearSpring(stiffness, self.pathway, equilibrium_length)
|
| 223 |
+
|
| 224 |
+
assert isinstance(spring, LinearSpring)
|
| 225 |
+
|
| 226 |
+
assert hasattr(spring, 'stiffness')
|
| 227 |
+
assert isinstance(spring.stiffness, ExprType)
|
| 228 |
+
assert spring.stiffness == expected_stiffness
|
| 229 |
+
|
| 230 |
+
assert hasattr(spring, 'pathway')
|
| 231 |
+
assert isinstance(spring.pathway, LinearPathway)
|
| 232 |
+
assert spring.pathway == self.pathway
|
| 233 |
+
|
| 234 |
+
assert hasattr(spring, 'equilibrium_length')
|
| 235 |
+
assert isinstance(spring.equilibrium_length, ExprType)
|
| 236 |
+
assert spring.equilibrium_length == expected_equilibrium_length
|
| 237 |
+
|
| 238 |
+
assert hasattr(spring, 'force')
|
| 239 |
+
assert isinstance(spring.force, ExprType)
|
| 240 |
+
assert spring.force == force
|
| 241 |
+
|
| 242 |
+
@pytest.mark.parametrize('stiffness', [None, 'k'])
|
| 243 |
+
def test_invalid_constructor_stiffness_not_sympifyable(self, stiffness):
|
| 244 |
+
with pytest.raises(SympifyError):
|
| 245 |
+
_ = LinearSpring(stiffness, self.pathway, self.l)
|
| 246 |
+
|
| 247 |
+
def test_invalid_constructor_pathway_not_pathway_base(self):
|
| 248 |
+
with pytest.raises(TypeError):
|
| 249 |
+
_ = LinearSpring(self.stiffness, None, self.l)
|
| 250 |
+
|
| 251 |
+
@pytest.mark.parametrize('equilibrium_length', [None, 'l'])
|
| 252 |
+
def test_invalid_constructor_equilibrium_length_not_sympifyable(
|
| 253 |
+
self,
|
| 254 |
+
equilibrium_length,
|
| 255 |
+
):
|
| 256 |
+
with pytest.raises(SympifyError):
|
| 257 |
+
_ = LinearSpring(self.stiffness, self.pathway, equilibrium_length)
|
| 258 |
+
|
| 259 |
+
@pytest.mark.parametrize(
|
| 260 |
+
'property_name, fixture_attr_name',
|
| 261 |
+
[
|
| 262 |
+
('stiffness', 'stiffness'),
|
| 263 |
+
('pathway', 'pathway'),
|
| 264 |
+
('equilibrium_length', 'l'),
|
| 265 |
+
]
|
| 266 |
+
)
|
| 267 |
+
def test_properties_are_immutable(self, property_name, fixture_attr_name):
|
| 268 |
+
spring = LinearSpring(self.stiffness, self.pathway, self.l)
|
| 269 |
+
value = getattr(self, fixture_attr_name)
|
| 270 |
+
with pytest.raises(AttributeError):
|
| 271 |
+
setattr(spring, property_name, value)
|
| 272 |
+
|
| 273 |
+
@pytest.mark.parametrize(
|
| 274 |
+
'equilibrium_length, expected',
|
| 275 |
+
[
|
| 276 |
+
(S.Zero, 'LinearSpring(k, LinearPathway(pA, pB))'),
|
| 277 |
+
(
|
| 278 |
+
Symbol('l'),
|
| 279 |
+
'LinearSpring(k, LinearPathway(pA, pB), equilibrium_length=l)',
|
| 280 |
+
),
|
| 281 |
+
]
|
| 282 |
+
)
|
| 283 |
+
def test_repr(self, equilibrium_length, expected):
|
| 284 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 285 |
+
spring = LinearSpring(self.stiffness, self.pathway, equilibrium_length)
|
| 286 |
+
assert repr(spring) == expected
|
| 287 |
+
|
| 288 |
+
def test_to_loads(self):
|
| 289 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 290 |
+
spring = LinearSpring(self.stiffness, self.pathway, self.l)
|
| 291 |
+
normal = self.q/sqrt(self.q**2)*self.N.x
|
| 292 |
+
pA_force = self.stiffness*(sqrt(self.q**2) - self.l)*normal
|
| 293 |
+
pB_force = -self.stiffness*(sqrt(self.q**2) - self.l)*normal
|
| 294 |
+
expected = [Force(self.pA, pA_force), Force(self.pB, pB_force)]
|
| 295 |
+
loads = spring.to_loads()
|
| 296 |
+
|
| 297 |
+
for load, (point, vector) in zip(loads, expected):
|
| 298 |
+
assert isinstance(load, Force)
|
| 299 |
+
assert load.point == point
|
| 300 |
+
assert (load.vector - vector).simplify() == 0
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
class TestLinearDamper:
|
| 304 |
+
|
| 305 |
+
@pytest.fixture(autouse=True)
|
| 306 |
+
def _linear_damper_fixture(self):
|
| 307 |
+
self.damping = Symbol('c')
|
| 308 |
+
self.l = Symbol('l')
|
| 309 |
+
self.pA = Point('pA')
|
| 310 |
+
self.pB = Point('pB')
|
| 311 |
+
self.pathway = LinearPathway(self.pA, self.pB)
|
| 312 |
+
self.q = dynamicsymbols('q')
|
| 313 |
+
self.dq = dynamicsymbols('q', 1)
|
| 314 |
+
self.u = dynamicsymbols('u')
|
| 315 |
+
self.N = ReferenceFrame('N')
|
| 316 |
+
|
| 317 |
+
def test_is_force_actuator_subclass(self):
|
| 318 |
+
assert issubclass(LinearDamper, ForceActuator)
|
| 319 |
+
|
| 320 |
+
def test_is_actuator_base_subclass(self):
|
| 321 |
+
assert issubclass(LinearDamper, ActuatorBase)
|
| 322 |
+
|
| 323 |
+
def test_valid_constructor(self):
|
| 324 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 325 |
+
damper = LinearDamper(self.damping, self.pathway)
|
| 326 |
+
|
| 327 |
+
assert isinstance(damper, LinearDamper)
|
| 328 |
+
|
| 329 |
+
assert hasattr(damper, 'damping')
|
| 330 |
+
assert isinstance(damper.damping, ExprType)
|
| 331 |
+
assert damper.damping == self.damping
|
| 332 |
+
|
| 333 |
+
assert hasattr(damper, 'pathway')
|
| 334 |
+
assert isinstance(damper.pathway, LinearPathway)
|
| 335 |
+
assert damper.pathway == self.pathway
|
| 336 |
+
|
| 337 |
+
def test_valid_constructor_force(self):
|
| 338 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 339 |
+
damper = LinearDamper(self.damping, self.pathway)
|
| 340 |
+
|
| 341 |
+
expected_force = -self.damping*sqrt(self.q**2)*self.dq/self.q
|
| 342 |
+
assert hasattr(damper, 'force')
|
| 343 |
+
assert isinstance(damper.force, ExprType)
|
| 344 |
+
assert damper.force == expected_force
|
| 345 |
+
|
| 346 |
+
@pytest.mark.parametrize('damping', [None, 'c'])
|
| 347 |
+
def test_invalid_constructor_damping_not_sympifyable(self, damping):
|
| 348 |
+
with pytest.raises(SympifyError):
|
| 349 |
+
_ = LinearDamper(damping, self.pathway)
|
| 350 |
+
|
| 351 |
+
def test_invalid_constructor_pathway_not_pathway_base(self):
|
| 352 |
+
with pytest.raises(TypeError):
|
| 353 |
+
_ = LinearDamper(self.damping, None)
|
| 354 |
+
|
| 355 |
+
@pytest.mark.parametrize(
|
| 356 |
+
'property_name, fixture_attr_name',
|
| 357 |
+
[
|
| 358 |
+
('damping', 'damping'),
|
| 359 |
+
('pathway', 'pathway'),
|
| 360 |
+
]
|
| 361 |
+
)
|
| 362 |
+
def test_properties_are_immutable(self, property_name, fixture_attr_name):
|
| 363 |
+
damper = LinearDamper(self.damping, self.pathway)
|
| 364 |
+
value = getattr(self, fixture_attr_name)
|
| 365 |
+
with pytest.raises(AttributeError):
|
| 366 |
+
setattr(damper, property_name, value)
|
| 367 |
+
|
| 368 |
+
def test_repr(self):
|
| 369 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 370 |
+
damper = LinearDamper(self.damping, self.pathway)
|
| 371 |
+
expected = 'LinearDamper(c, LinearPathway(pA, pB))'
|
| 372 |
+
assert repr(damper) == expected
|
| 373 |
+
|
| 374 |
+
def test_to_loads(self):
|
| 375 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 376 |
+
damper = LinearDamper(self.damping, self.pathway)
|
| 377 |
+
direction = self.q**2/self.q**2*self.N.x
|
| 378 |
+
pA_force = self.damping*self.dq*direction
|
| 379 |
+
pB_force = -self.damping*self.dq*direction
|
| 380 |
+
expected = [Force(self.pA, pA_force), Force(self.pB, pB_force)]
|
| 381 |
+
assert damper.to_loads() == expected
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
class TestForcedMassSpringDamperModel():
|
| 385 |
+
r"""A single degree of freedom translational forced mass-spring-damper.
|
| 386 |
+
|
| 387 |
+
Notes
|
| 388 |
+
=====
|
| 389 |
+
|
| 390 |
+
This system is well known to have the governing equation:
|
| 391 |
+
|
| 392 |
+
.. math::
|
| 393 |
+
m \ddot{x} = F - k x - c \dot{x}
|
| 394 |
+
|
| 395 |
+
where $F$ is an externally applied force, $m$ is the mass of the particle
|
| 396 |
+
to which the spring and damper are attached, $k$ is the spring's stiffness,
|
| 397 |
+
$c$ is the dampers damping coefficient, and $x$ is the generalized
|
| 398 |
+
coordinate representing the system's single (translational) degree of
|
| 399 |
+
freedom.
|
| 400 |
+
|
| 401 |
+
"""
|
| 402 |
+
|
| 403 |
+
@pytest.fixture(autouse=True)
|
| 404 |
+
def _force_mass_spring_damper_model_fixture(self):
|
| 405 |
+
self.m = Symbol('m')
|
| 406 |
+
self.k = Symbol('k')
|
| 407 |
+
self.c = Symbol('c')
|
| 408 |
+
self.F = Symbol('F')
|
| 409 |
+
|
| 410 |
+
self.q = dynamicsymbols('q')
|
| 411 |
+
self.dq = dynamicsymbols('q', 1)
|
| 412 |
+
self.u = dynamicsymbols('u')
|
| 413 |
+
|
| 414 |
+
self.frame = ReferenceFrame('N')
|
| 415 |
+
self.origin = Point('pO')
|
| 416 |
+
self.origin.set_vel(self.frame, 0)
|
| 417 |
+
|
| 418 |
+
self.attachment = Point('pA')
|
| 419 |
+
self.attachment.set_pos(self.origin, self.q*self.frame.x)
|
| 420 |
+
|
| 421 |
+
self.mass = Particle('mass', self.attachment, self.m)
|
| 422 |
+
self.pathway = LinearPathway(self.origin, self.attachment)
|
| 423 |
+
|
| 424 |
+
self.kanes_method = KanesMethod(
|
| 425 |
+
self.frame,
|
| 426 |
+
q_ind=[self.q],
|
| 427 |
+
u_ind=[self.u],
|
| 428 |
+
kd_eqs=[self.dq - self.u],
|
| 429 |
+
)
|
| 430 |
+
self.bodies = [self.mass]
|
| 431 |
+
|
| 432 |
+
self.mass_matrix = Matrix([[self.m]])
|
| 433 |
+
self.forcing = Matrix([[self.F - self.c*self.u - self.k*self.q]])
|
| 434 |
+
|
| 435 |
+
def test_force_acuator(self):
|
| 436 |
+
stiffness = -self.k*self.pathway.length
|
| 437 |
+
spring = ForceActuator(stiffness, self.pathway)
|
| 438 |
+
damping = -self.c*self.pathway.extension_velocity
|
| 439 |
+
damper = ForceActuator(damping, self.pathway)
|
| 440 |
+
|
| 441 |
+
loads = [
|
| 442 |
+
(self.attachment, self.F*self.frame.x),
|
| 443 |
+
*spring.to_loads(),
|
| 444 |
+
*damper.to_loads(),
|
| 445 |
+
]
|
| 446 |
+
self.kanes_method.kanes_equations(self.bodies, loads)
|
| 447 |
+
|
| 448 |
+
assert self.kanes_method.mass_matrix == self.mass_matrix
|
| 449 |
+
assert self.kanes_method.forcing == self.forcing
|
| 450 |
+
|
| 451 |
+
def test_linear_spring_linear_damper(self):
|
| 452 |
+
spring = LinearSpring(self.k, self.pathway)
|
| 453 |
+
damper = LinearDamper(self.c, self.pathway)
|
| 454 |
+
|
| 455 |
+
loads = [
|
| 456 |
+
(self.attachment, self.F*self.frame.x),
|
| 457 |
+
*spring.to_loads(),
|
| 458 |
+
*damper.to_loads(),
|
| 459 |
+
]
|
| 460 |
+
self.kanes_method.kanes_equations(self.bodies, loads)
|
| 461 |
+
|
| 462 |
+
assert self.kanes_method.mass_matrix == self.mass_matrix
|
| 463 |
+
assert self.kanes_method.forcing == self.forcing
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
class TestTorqueActuator:
|
| 467 |
+
|
| 468 |
+
@pytest.fixture(autouse=True)
|
| 469 |
+
def _torque_actuator_fixture(self):
|
| 470 |
+
self.torque = Symbol('T')
|
| 471 |
+
self.N = ReferenceFrame('N')
|
| 472 |
+
self.A = ReferenceFrame('A')
|
| 473 |
+
self.axis = self.N.z
|
| 474 |
+
self.target = RigidBody('target', frame=self.N)
|
| 475 |
+
self.reaction = RigidBody('reaction', frame=self.A)
|
| 476 |
+
|
| 477 |
+
def test_is_actuator_base_subclass(self):
|
| 478 |
+
assert issubclass(TorqueActuator, ActuatorBase)
|
| 479 |
+
|
| 480 |
+
@pytest.mark.parametrize(
|
| 481 |
+
'torque',
|
| 482 |
+
[
|
| 483 |
+
Symbol('T'),
|
| 484 |
+
dynamicsymbols('T'),
|
| 485 |
+
Symbol('T')**2 + Symbol('T'),
|
| 486 |
+
]
|
| 487 |
+
)
|
| 488 |
+
@pytest.mark.parametrize(
|
| 489 |
+
'target_frame, reaction_frame',
|
| 490 |
+
[
|
| 491 |
+
(target.frame, reaction.frame),
|
| 492 |
+
(target, reaction.frame),
|
| 493 |
+
(target.frame, reaction),
|
| 494 |
+
(target, reaction),
|
| 495 |
+
]
|
| 496 |
+
)
|
| 497 |
+
def test_valid_constructor_with_reaction(
|
| 498 |
+
self,
|
| 499 |
+
torque,
|
| 500 |
+
target_frame,
|
| 501 |
+
reaction_frame,
|
| 502 |
+
):
|
| 503 |
+
instance = TorqueActuator(
|
| 504 |
+
torque,
|
| 505 |
+
self.axis,
|
| 506 |
+
target_frame,
|
| 507 |
+
reaction_frame,
|
| 508 |
+
)
|
| 509 |
+
assert isinstance(instance, TorqueActuator)
|
| 510 |
+
|
| 511 |
+
assert hasattr(instance, 'torque')
|
| 512 |
+
assert isinstance(instance.torque, ExprType)
|
| 513 |
+
assert instance.torque == torque
|
| 514 |
+
|
| 515 |
+
assert hasattr(instance, 'axis')
|
| 516 |
+
assert isinstance(instance.axis, Vector)
|
| 517 |
+
assert instance.axis == self.axis
|
| 518 |
+
|
| 519 |
+
assert hasattr(instance, 'target_frame')
|
| 520 |
+
assert isinstance(instance.target_frame, ReferenceFrame)
|
| 521 |
+
assert instance.target_frame == target.frame
|
| 522 |
+
|
| 523 |
+
assert hasattr(instance, 'reaction_frame')
|
| 524 |
+
assert isinstance(instance.reaction_frame, ReferenceFrame)
|
| 525 |
+
assert instance.reaction_frame == reaction.frame
|
| 526 |
+
|
| 527 |
+
@pytest.mark.parametrize(
|
| 528 |
+
'torque',
|
| 529 |
+
[
|
| 530 |
+
Symbol('T'),
|
| 531 |
+
dynamicsymbols('T'),
|
| 532 |
+
Symbol('T')**2 + Symbol('T'),
|
| 533 |
+
]
|
| 534 |
+
)
|
| 535 |
+
@pytest.mark.parametrize('target_frame', [target.frame, target])
|
| 536 |
+
def test_valid_constructor_without_reaction(self, torque, target_frame):
|
| 537 |
+
instance = TorqueActuator(torque, self.axis, target_frame)
|
| 538 |
+
assert isinstance(instance, TorqueActuator)
|
| 539 |
+
|
| 540 |
+
assert hasattr(instance, 'torque')
|
| 541 |
+
assert isinstance(instance.torque, ExprType)
|
| 542 |
+
assert instance.torque == torque
|
| 543 |
+
|
| 544 |
+
assert hasattr(instance, 'axis')
|
| 545 |
+
assert isinstance(instance.axis, Vector)
|
| 546 |
+
assert instance.axis == self.axis
|
| 547 |
+
|
| 548 |
+
assert hasattr(instance, 'target_frame')
|
| 549 |
+
assert isinstance(instance.target_frame, ReferenceFrame)
|
| 550 |
+
assert instance.target_frame == target.frame
|
| 551 |
+
|
| 552 |
+
assert hasattr(instance, 'reaction_frame')
|
| 553 |
+
assert instance.reaction_frame is None
|
| 554 |
+
|
| 555 |
+
@pytest.mark.parametrize('torque', [None, 'T'])
|
| 556 |
+
def test_invalid_constructor_torque_not_sympifyable(self, torque):
|
| 557 |
+
with pytest.raises(SympifyError):
|
| 558 |
+
_ = TorqueActuator(torque, self.axis, self.target)
|
| 559 |
+
|
| 560 |
+
@pytest.mark.parametrize('axis', [Symbol('a'), dynamicsymbols('a')])
|
| 561 |
+
def test_invalid_constructor_axis_not_vector(self, axis):
|
| 562 |
+
with pytest.raises(TypeError):
|
| 563 |
+
_ = TorqueActuator(self.torque, axis, self.target, self.reaction)
|
| 564 |
+
|
| 565 |
+
@pytest.mark.parametrize(
|
| 566 |
+
'frames',
|
| 567 |
+
[
|
| 568 |
+
(None, ReferenceFrame('child')),
|
| 569 |
+
(ReferenceFrame('parent'), True),
|
| 570 |
+
(None, RigidBody('child')),
|
| 571 |
+
(RigidBody('parent'), True),
|
| 572 |
+
]
|
| 573 |
+
)
|
| 574 |
+
def test_invalid_constructor_frames_not_frame(self, frames):
|
| 575 |
+
with pytest.raises(TypeError):
|
| 576 |
+
_ = TorqueActuator(self.torque, self.axis, *frames)
|
| 577 |
+
|
| 578 |
+
@pytest.mark.parametrize(
|
| 579 |
+
'property_name, fixture_attr_name',
|
| 580 |
+
[
|
| 581 |
+
('torque', 'torque'),
|
| 582 |
+
('axis', 'axis'),
|
| 583 |
+
('target_frame', 'target'),
|
| 584 |
+
('reaction_frame', 'reaction'),
|
| 585 |
+
]
|
| 586 |
+
)
|
| 587 |
+
def test_properties_are_immutable(self, property_name, fixture_attr_name):
|
| 588 |
+
actuator = TorqueActuator(
|
| 589 |
+
self.torque,
|
| 590 |
+
self.axis,
|
| 591 |
+
self.target,
|
| 592 |
+
self.reaction,
|
| 593 |
+
)
|
| 594 |
+
value = getattr(self, fixture_attr_name)
|
| 595 |
+
with pytest.raises(AttributeError):
|
| 596 |
+
setattr(actuator, property_name, value)
|
| 597 |
+
|
| 598 |
+
def test_repr_without_reaction(self):
|
| 599 |
+
actuator = TorqueActuator(self.torque, self.axis, self.target)
|
| 600 |
+
expected = 'TorqueActuator(T, axis=N.z, target_frame=N)'
|
| 601 |
+
assert repr(actuator) == expected
|
| 602 |
+
|
| 603 |
+
def test_repr_with_reaction(self):
|
| 604 |
+
actuator = TorqueActuator(
|
| 605 |
+
self.torque,
|
| 606 |
+
self.axis,
|
| 607 |
+
self.target,
|
| 608 |
+
self.reaction,
|
| 609 |
+
)
|
| 610 |
+
expected = 'TorqueActuator(T, axis=N.z, target_frame=N, reaction_frame=A)'
|
| 611 |
+
assert repr(actuator) == expected
|
| 612 |
+
|
| 613 |
+
def test_at_pin_joint_constructor(self):
|
| 614 |
+
pin_joint = PinJoint(
|
| 615 |
+
'pin',
|
| 616 |
+
self.target,
|
| 617 |
+
self.reaction,
|
| 618 |
+
coordinates=dynamicsymbols('q'),
|
| 619 |
+
speeds=dynamicsymbols('u'),
|
| 620 |
+
parent_interframe=self.N,
|
| 621 |
+
joint_axis=self.axis,
|
| 622 |
+
)
|
| 623 |
+
instance = TorqueActuator.at_pin_joint(self.torque, pin_joint)
|
| 624 |
+
assert isinstance(instance, TorqueActuator)
|
| 625 |
+
|
| 626 |
+
assert hasattr(instance, 'torque')
|
| 627 |
+
assert isinstance(instance.torque, ExprType)
|
| 628 |
+
assert instance.torque == self.torque
|
| 629 |
+
|
| 630 |
+
assert hasattr(instance, 'axis')
|
| 631 |
+
assert isinstance(instance.axis, Vector)
|
| 632 |
+
assert instance.axis == self.axis
|
| 633 |
+
|
| 634 |
+
assert hasattr(instance, 'target_frame')
|
| 635 |
+
assert isinstance(instance.target_frame, ReferenceFrame)
|
| 636 |
+
assert instance.target_frame == self.A
|
| 637 |
+
|
| 638 |
+
assert hasattr(instance, 'reaction_frame')
|
| 639 |
+
assert isinstance(instance.reaction_frame, ReferenceFrame)
|
| 640 |
+
assert instance.reaction_frame == self.N
|
| 641 |
+
|
| 642 |
+
def test_at_pin_joint_pin_joint_not_pin_joint_invalid(self):
|
| 643 |
+
with pytest.raises(TypeError):
|
| 644 |
+
_ = TorqueActuator.at_pin_joint(self.torque, Symbol('pin'))
|
| 645 |
+
|
| 646 |
+
def test_to_loads_without_reaction(self):
|
| 647 |
+
actuator = TorqueActuator(self.torque, self.axis, self.target)
|
| 648 |
+
expected = [
|
| 649 |
+
(self.N, self.torque*self.axis),
|
| 650 |
+
]
|
| 651 |
+
assert actuator.to_loads() == expected
|
| 652 |
+
|
| 653 |
+
def test_to_loads_with_reaction(self):
|
| 654 |
+
actuator = TorqueActuator(
|
| 655 |
+
self.torque,
|
| 656 |
+
self.axis,
|
| 657 |
+
self.target,
|
| 658 |
+
self.reaction,
|
| 659 |
+
)
|
| 660 |
+
expected = [
|
| 661 |
+
(self.N, self.torque*self.axis),
|
| 662 |
+
(self.A, - self.torque*self.axis),
|
| 663 |
+
]
|
| 664 |
+
assert actuator.to_loads() == expected
|
| 665 |
+
|
| 666 |
+
|
| 667 |
+
class NonSympifyable:
|
| 668 |
+
pass
|
| 669 |
+
|
| 670 |
+
|
| 671 |
+
class TestDuffingSpring:
|
| 672 |
+
@pytest.fixture(autouse=True)
|
| 673 |
+
# Set up common vairables that will be used in multiple tests
|
| 674 |
+
def _duffing_spring_fixture(self):
|
| 675 |
+
self.linear_stiffness = Symbol('beta')
|
| 676 |
+
self.nonlinear_stiffness = Symbol('alpha')
|
| 677 |
+
self.equilibrium_length = Symbol('l')
|
| 678 |
+
self.pA = Point('pA')
|
| 679 |
+
self.pB = Point('pB')
|
| 680 |
+
self.pathway = LinearPathway(self.pA, self.pB)
|
| 681 |
+
self.q = dynamicsymbols('q')
|
| 682 |
+
self.N = ReferenceFrame('N')
|
| 683 |
+
|
| 684 |
+
# Simples tests to check that DuffingSpring is a subclass of ForceActuator and ActuatorBase
|
| 685 |
+
def test_is_force_actuator_subclass(self):
|
| 686 |
+
assert issubclass(DuffingSpring, ForceActuator)
|
| 687 |
+
|
| 688 |
+
def test_is_actuator_base_subclass(self):
|
| 689 |
+
assert issubclass(DuffingSpring, ActuatorBase)
|
| 690 |
+
|
| 691 |
+
@pytest.mark.parametrize(
|
| 692 |
+
# Create parametrized tests that allows running the same test function multiple times with different sets of arguments
|
| 693 |
+
(
|
| 694 |
+
'linear_stiffness, '
|
| 695 |
+
'expected_linear_stiffness, '
|
| 696 |
+
'nonlinear_stiffness, '
|
| 697 |
+
'expected_nonlinear_stiffness, '
|
| 698 |
+
'equilibrium_length, '
|
| 699 |
+
'expected_equilibrium_length, '
|
| 700 |
+
'force'
|
| 701 |
+
),
|
| 702 |
+
[
|
| 703 |
+
(
|
| 704 |
+
1,
|
| 705 |
+
S.One,
|
| 706 |
+
1,
|
| 707 |
+
S.One,
|
| 708 |
+
0,
|
| 709 |
+
S.Zero,
|
| 710 |
+
-sqrt(dynamicsymbols('q')**2)-(sqrt(dynamicsymbols('q')**2))**3,
|
| 711 |
+
),
|
| 712 |
+
(
|
| 713 |
+
Symbol('beta'),
|
| 714 |
+
Symbol('beta'),
|
| 715 |
+
Symbol('alpha'),
|
| 716 |
+
Symbol('alpha'),
|
| 717 |
+
0,
|
| 718 |
+
S.Zero,
|
| 719 |
+
-Symbol('beta')*sqrt(dynamicsymbols('q')**2)-Symbol('alpha')*(sqrt(dynamicsymbols('q')**2))**3,
|
| 720 |
+
),
|
| 721 |
+
(
|
| 722 |
+
Symbol('beta'),
|
| 723 |
+
Symbol('beta'),
|
| 724 |
+
Symbol('alpha'),
|
| 725 |
+
Symbol('alpha'),
|
| 726 |
+
S.Zero,
|
| 727 |
+
S.Zero,
|
| 728 |
+
-Symbol('beta')*sqrt(dynamicsymbols('q')**2)-Symbol('alpha')*(sqrt(dynamicsymbols('q')**2))**3,
|
| 729 |
+
),
|
| 730 |
+
(
|
| 731 |
+
Symbol('beta'),
|
| 732 |
+
Symbol('beta'),
|
| 733 |
+
Symbol('alpha'),
|
| 734 |
+
Symbol('alpha'),
|
| 735 |
+
Symbol('l'),
|
| 736 |
+
Symbol('l'),
|
| 737 |
+
-Symbol('beta') * (sqrt(dynamicsymbols('q')**2) - Symbol('l')) - Symbol('alpha') * (sqrt(dynamicsymbols('q')**2) - Symbol('l'))**3,
|
| 738 |
+
),
|
| 739 |
+
]
|
| 740 |
+
)
|
| 741 |
+
|
| 742 |
+
# Check if DuffingSpring correctly inializes its attributes
|
| 743 |
+
# It tests various combinations of linear & nonlinear stiffness, equilibriun length, and the resulting force expression
|
| 744 |
+
def test_valid_constructor(
|
| 745 |
+
self,
|
| 746 |
+
linear_stiffness,
|
| 747 |
+
expected_linear_stiffness,
|
| 748 |
+
nonlinear_stiffness,
|
| 749 |
+
expected_nonlinear_stiffness,
|
| 750 |
+
equilibrium_length,
|
| 751 |
+
expected_equilibrium_length,
|
| 752 |
+
force,
|
| 753 |
+
):
|
| 754 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 755 |
+
spring = DuffingSpring(linear_stiffness, nonlinear_stiffness, self.pathway, equilibrium_length)
|
| 756 |
+
|
| 757 |
+
assert isinstance(spring, DuffingSpring)
|
| 758 |
+
|
| 759 |
+
assert hasattr(spring, 'linear_stiffness')
|
| 760 |
+
assert isinstance(spring.linear_stiffness, ExprType)
|
| 761 |
+
assert spring.linear_stiffness == expected_linear_stiffness
|
| 762 |
+
|
| 763 |
+
assert hasattr(spring, 'nonlinear_stiffness')
|
| 764 |
+
assert isinstance(spring.nonlinear_stiffness, ExprType)
|
| 765 |
+
assert spring.nonlinear_stiffness == expected_nonlinear_stiffness
|
| 766 |
+
|
| 767 |
+
assert hasattr(spring, 'pathway')
|
| 768 |
+
assert isinstance(spring.pathway, LinearPathway)
|
| 769 |
+
assert spring.pathway == self.pathway
|
| 770 |
+
|
| 771 |
+
assert hasattr(spring, 'equilibrium_length')
|
| 772 |
+
assert isinstance(spring.equilibrium_length, ExprType)
|
| 773 |
+
assert spring.equilibrium_length == expected_equilibrium_length
|
| 774 |
+
|
| 775 |
+
assert hasattr(spring, 'force')
|
| 776 |
+
assert isinstance(spring.force, ExprType)
|
| 777 |
+
assert spring.force == force
|
| 778 |
+
|
| 779 |
+
@pytest.mark.parametrize('linear_stiffness', [None, NonSympifyable()])
|
| 780 |
+
def test_invalid_constructor_linear_stiffness_not_sympifyable(self, linear_stiffness):
|
| 781 |
+
with pytest.raises(SympifyError):
|
| 782 |
+
_ = DuffingSpring(linear_stiffness, self.nonlinear_stiffness, self.pathway, self.equilibrium_length)
|
| 783 |
+
|
| 784 |
+
@pytest.mark.parametrize('nonlinear_stiffness', [None, NonSympifyable()])
|
| 785 |
+
def test_invalid_constructor_nonlinear_stiffness_not_sympifyable(self, nonlinear_stiffness):
|
| 786 |
+
with pytest.raises(SympifyError):
|
| 787 |
+
_ = DuffingSpring(self.linear_stiffness, nonlinear_stiffness, self.pathway, self.equilibrium_length)
|
| 788 |
+
|
| 789 |
+
def test_invalid_constructor_pathway_not_pathway_base(self):
|
| 790 |
+
with pytest.raises(TypeError):
|
| 791 |
+
_ = DuffingSpring(self.linear_stiffness, self.nonlinear_stiffness, NonSympifyable(), self.equilibrium_length)
|
| 792 |
+
|
| 793 |
+
@pytest.mark.parametrize('equilibrium_length', [None, NonSympifyable()])
|
| 794 |
+
def test_invalid_constructor_equilibrium_length_not_sympifyable(self, equilibrium_length):
|
| 795 |
+
with pytest.raises(SympifyError):
|
| 796 |
+
_ = DuffingSpring(self.linear_stiffness, self.nonlinear_stiffness, self.pathway, equilibrium_length)
|
| 797 |
+
|
| 798 |
+
@pytest.mark.parametrize(
|
| 799 |
+
'property_name, fixture_attr_name',
|
| 800 |
+
[
|
| 801 |
+
('linear_stiffness', 'linear_stiffness'),
|
| 802 |
+
('nonlinear_stiffness', 'nonlinear_stiffness'),
|
| 803 |
+
('pathway', 'pathway'),
|
| 804 |
+
('equilibrium_length', 'equilibrium_length')
|
| 805 |
+
]
|
| 806 |
+
)
|
| 807 |
+
# Check if certain properties of DuffingSpring object are immutable after initialization
|
| 808 |
+
# Ensure that once DuffingSpring is created, its key properties cannot be changed
|
| 809 |
+
def test_properties_are_immutable(self, property_name, fixture_attr_name):
|
| 810 |
+
spring = DuffingSpring(self.linear_stiffness, self.nonlinear_stiffness, self.pathway, self.equilibrium_length)
|
| 811 |
+
with pytest.raises(AttributeError):
|
| 812 |
+
setattr(spring, property_name, getattr(self, fixture_attr_name))
|
| 813 |
+
|
| 814 |
+
@pytest.mark.parametrize(
|
| 815 |
+
'equilibrium_length, expected',
|
| 816 |
+
[
|
| 817 |
+
(0, 'DuffingSpring(beta, alpha, LinearPathway(pA, pB), equilibrium_length=0)'),
|
| 818 |
+
(Symbol('l'), 'DuffingSpring(beta, alpha, LinearPathway(pA, pB), equilibrium_length=l)'),
|
| 819 |
+
]
|
| 820 |
+
)
|
| 821 |
+
# Check the __repr__ method of DuffingSpring class
|
| 822 |
+
# Check if the actual string representation of DuffingSpring instance matches the expected string for each provided parameter values
|
| 823 |
+
def test_repr(self, equilibrium_length, expected):
|
| 824 |
+
spring = DuffingSpring(self.linear_stiffness, self.nonlinear_stiffness, self.pathway, equilibrium_length)
|
| 825 |
+
assert repr(spring) == expected
|
| 826 |
+
|
| 827 |
+
def test_to_loads(self):
|
| 828 |
+
self.pB.set_pos(self.pA, self.q*self.N.x)
|
| 829 |
+
spring = DuffingSpring(self.linear_stiffness, self.nonlinear_stiffness, self.pathway, self.equilibrium_length)
|
| 830 |
+
|
| 831 |
+
# Calculate the displacement from the equilibrium length
|
| 832 |
+
displacement = self.q - self.equilibrium_length
|
| 833 |
+
|
| 834 |
+
# Make sure this matches the computation in DuffingSpring class
|
| 835 |
+
force = -self.linear_stiffness * displacement - self.nonlinear_stiffness * displacement**3
|
| 836 |
+
|
| 837 |
+
# The expected loads on pA and pB due to the spring
|
| 838 |
+
expected_loads = [Force(self.pA, force * self.N.x), Force(self.pB, -force * self.N.x)]
|
| 839 |
+
|
| 840 |
+
# Compare expected loads to what is returned from DuffingSpring.to_loads()
|
| 841 |
+
calculated_loads = spring.to_loads()
|
| 842 |
+
for calculated, expected in zip(calculated_loads, expected_loads):
|
| 843 |
+
assert calculated.point == expected.point
|
| 844 |
+
for dim in self.N: # Assuming self.N is the reference frame
|
| 845 |
+
calculated_component = calculated.vector.dot(dim)
|
| 846 |
+
expected_component = expected.vector.dot(dim)
|
| 847 |
+
# Substitute all symbols with numeric values
|
| 848 |
+
substitutions = {self.q: 1, Symbol('l'): 1, Symbol('alpha'): 1, Symbol('beta'): 1} # Add other necessary symbols as needed
|
| 849 |
+
diff = (calculated_component - expected_component).subs(substitutions).evalf()
|
| 850 |
+
# Check if the absolute value of the difference is below a threshold
|
| 851 |
+
assert Abs(diff) < 1e-9, f"The forces do not match. Difference: {diff}"
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/test_body.py
ADDED
|
@@ -0,0 +1,340 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy import (Symbol, symbols, sin, cos, Matrix, zeros,
|
| 2 |
+
simplify)
|
| 3 |
+
from sympy.physics.vector import Point, ReferenceFrame, dynamicsymbols, Dyadic
|
| 4 |
+
from sympy.physics.mechanics import inertia, Body
|
| 5 |
+
from sympy.testing.pytest import raises, warns_deprecated_sympy
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def test_default():
|
| 9 |
+
with warns_deprecated_sympy():
|
| 10 |
+
body = Body('body')
|
| 11 |
+
assert body.name == 'body'
|
| 12 |
+
assert body.loads == []
|
| 13 |
+
point = Point('body_masscenter')
|
| 14 |
+
point.set_vel(body.frame, 0)
|
| 15 |
+
com = body.masscenter
|
| 16 |
+
frame = body.frame
|
| 17 |
+
assert com.vel(frame) == point.vel(frame)
|
| 18 |
+
assert body.mass == Symbol('body_mass')
|
| 19 |
+
ixx, iyy, izz = symbols('body_ixx body_iyy body_izz')
|
| 20 |
+
ixy, iyz, izx = symbols('body_ixy body_iyz body_izx')
|
| 21 |
+
assert body.inertia == (inertia(body.frame, ixx, iyy, izz, ixy, iyz, izx),
|
| 22 |
+
body.masscenter)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_custom_rigid_body():
|
| 26 |
+
# Body with RigidBody.
|
| 27 |
+
rigidbody_masscenter = Point('rigidbody_masscenter')
|
| 28 |
+
rigidbody_mass = Symbol('rigidbody_mass')
|
| 29 |
+
rigidbody_frame = ReferenceFrame('rigidbody_frame')
|
| 30 |
+
body_inertia = inertia(rigidbody_frame, 1, 0, 0)
|
| 31 |
+
with warns_deprecated_sympy():
|
| 32 |
+
rigid_body = Body('rigidbody_body', rigidbody_masscenter,
|
| 33 |
+
rigidbody_mass, rigidbody_frame, body_inertia)
|
| 34 |
+
com = rigid_body.masscenter
|
| 35 |
+
frame = rigid_body.frame
|
| 36 |
+
rigidbody_masscenter.set_vel(rigidbody_frame, 0)
|
| 37 |
+
assert com.vel(frame) == rigidbody_masscenter.vel(frame)
|
| 38 |
+
assert com.pos_from(com) == rigidbody_masscenter.pos_from(com)
|
| 39 |
+
|
| 40 |
+
assert rigid_body.mass == rigidbody_mass
|
| 41 |
+
assert rigid_body.inertia == (body_inertia, rigidbody_masscenter)
|
| 42 |
+
|
| 43 |
+
assert rigid_body.is_rigidbody
|
| 44 |
+
|
| 45 |
+
assert hasattr(rigid_body, 'masscenter')
|
| 46 |
+
assert hasattr(rigid_body, 'mass')
|
| 47 |
+
assert hasattr(rigid_body, 'frame')
|
| 48 |
+
assert hasattr(rigid_body, 'inertia')
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_particle_body():
|
| 52 |
+
# Body with Particle
|
| 53 |
+
particle_masscenter = Point('particle_masscenter')
|
| 54 |
+
particle_mass = Symbol('particle_mass')
|
| 55 |
+
particle_frame = ReferenceFrame('particle_frame')
|
| 56 |
+
with warns_deprecated_sympy():
|
| 57 |
+
particle_body = Body('particle_body', particle_masscenter,
|
| 58 |
+
particle_mass, particle_frame)
|
| 59 |
+
com = particle_body.masscenter
|
| 60 |
+
frame = particle_body.frame
|
| 61 |
+
particle_masscenter.set_vel(particle_frame, 0)
|
| 62 |
+
assert com.vel(frame) == particle_masscenter.vel(frame)
|
| 63 |
+
assert com.pos_from(com) == particle_masscenter.pos_from(com)
|
| 64 |
+
|
| 65 |
+
assert particle_body.mass == particle_mass
|
| 66 |
+
assert not hasattr(particle_body, "_inertia")
|
| 67 |
+
assert hasattr(particle_body, 'frame')
|
| 68 |
+
assert hasattr(particle_body, 'masscenter')
|
| 69 |
+
assert hasattr(particle_body, 'mass')
|
| 70 |
+
assert particle_body.inertia == (Dyadic(0), particle_body.masscenter)
|
| 71 |
+
assert particle_body.central_inertia == Dyadic(0)
|
| 72 |
+
assert not particle_body.is_rigidbody
|
| 73 |
+
|
| 74 |
+
particle_body.central_inertia = inertia(particle_frame, 1, 1, 1)
|
| 75 |
+
assert particle_body.central_inertia == inertia(particle_frame, 1, 1, 1)
|
| 76 |
+
assert particle_body.is_rigidbody
|
| 77 |
+
|
| 78 |
+
with warns_deprecated_sympy():
|
| 79 |
+
particle_body = Body('particle_body', mass=particle_mass)
|
| 80 |
+
assert not particle_body.is_rigidbody
|
| 81 |
+
point = particle_body.masscenter.locatenew('point', particle_body.x)
|
| 82 |
+
point_inertia = particle_mass * inertia(particle_body.frame, 0, 1, 1)
|
| 83 |
+
particle_body.inertia = (point_inertia, point)
|
| 84 |
+
assert particle_body.inertia == (point_inertia, point)
|
| 85 |
+
assert particle_body.central_inertia == Dyadic(0)
|
| 86 |
+
assert particle_body.is_rigidbody
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def test_particle_body_add_force():
|
| 90 |
+
# Body with Particle
|
| 91 |
+
particle_masscenter = Point('particle_masscenter')
|
| 92 |
+
particle_mass = Symbol('particle_mass')
|
| 93 |
+
particle_frame = ReferenceFrame('particle_frame')
|
| 94 |
+
with warns_deprecated_sympy():
|
| 95 |
+
particle_body = Body('particle_body', particle_masscenter,
|
| 96 |
+
particle_mass, particle_frame)
|
| 97 |
+
|
| 98 |
+
a = Symbol('a')
|
| 99 |
+
force_vector = a * particle_body.frame.x
|
| 100 |
+
particle_body.apply_force(force_vector, particle_body.masscenter)
|
| 101 |
+
assert len(particle_body.loads) == 1
|
| 102 |
+
point = particle_body.masscenter.locatenew(
|
| 103 |
+
particle_body._name + '_point0', 0)
|
| 104 |
+
point.set_vel(particle_body.frame, 0)
|
| 105 |
+
force_point = particle_body.loads[0][0]
|
| 106 |
+
|
| 107 |
+
frame = particle_body.frame
|
| 108 |
+
assert force_point.vel(frame) == point.vel(frame)
|
| 109 |
+
assert force_point.pos_from(force_point) == point.pos_from(force_point)
|
| 110 |
+
|
| 111 |
+
assert particle_body.loads[0][1] == force_vector
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_body_add_force():
|
| 115 |
+
# Body with RigidBody.
|
| 116 |
+
rigidbody_masscenter = Point('rigidbody_masscenter')
|
| 117 |
+
rigidbody_mass = Symbol('rigidbody_mass')
|
| 118 |
+
rigidbody_frame = ReferenceFrame('rigidbody_frame')
|
| 119 |
+
body_inertia = inertia(rigidbody_frame, 1, 0, 0)
|
| 120 |
+
with warns_deprecated_sympy():
|
| 121 |
+
rigid_body = Body('rigidbody_body', rigidbody_masscenter,
|
| 122 |
+
rigidbody_mass, rigidbody_frame, body_inertia)
|
| 123 |
+
|
| 124 |
+
l = Symbol('l')
|
| 125 |
+
Fa = Symbol('Fa')
|
| 126 |
+
point = rigid_body.masscenter.locatenew(
|
| 127 |
+
'rigidbody_body_point0',
|
| 128 |
+
l * rigid_body.frame.x)
|
| 129 |
+
point.set_vel(rigid_body.frame, 0)
|
| 130 |
+
force_vector = Fa * rigid_body.frame.z
|
| 131 |
+
# apply_force with point
|
| 132 |
+
rigid_body.apply_force(force_vector, point)
|
| 133 |
+
assert len(rigid_body.loads) == 1
|
| 134 |
+
force_point = rigid_body.loads[0][0]
|
| 135 |
+
frame = rigid_body.frame
|
| 136 |
+
assert force_point.vel(frame) == point.vel(frame)
|
| 137 |
+
assert force_point.pos_from(force_point) == point.pos_from(force_point)
|
| 138 |
+
assert rigid_body.loads[0][1] == force_vector
|
| 139 |
+
# apply_force without point
|
| 140 |
+
rigid_body.apply_force(force_vector)
|
| 141 |
+
assert len(rigid_body.loads) == 2
|
| 142 |
+
assert rigid_body.loads[1][1] == force_vector
|
| 143 |
+
# passing something else than point
|
| 144 |
+
raises(TypeError, lambda: rigid_body.apply_force(force_vector, 0))
|
| 145 |
+
raises(TypeError, lambda: rigid_body.apply_force(0))
|
| 146 |
+
|
| 147 |
+
def test_body_add_torque():
|
| 148 |
+
with warns_deprecated_sympy():
|
| 149 |
+
body = Body('body')
|
| 150 |
+
torque_vector = body.frame.x
|
| 151 |
+
body.apply_torque(torque_vector)
|
| 152 |
+
|
| 153 |
+
assert len(body.loads) == 1
|
| 154 |
+
assert body.loads[0] == (body.frame, torque_vector)
|
| 155 |
+
raises(TypeError, lambda: body.apply_torque(0))
|
| 156 |
+
|
| 157 |
+
def test_body_masscenter_vel():
|
| 158 |
+
with warns_deprecated_sympy():
|
| 159 |
+
A = Body('A')
|
| 160 |
+
N = ReferenceFrame('N')
|
| 161 |
+
with warns_deprecated_sympy():
|
| 162 |
+
B = Body('B', frame=N)
|
| 163 |
+
A.masscenter.set_vel(N, N.z)
|
| 164 |
+
assert A.masscenter_vel(B) == N.z
|
| 165 |
+
assert A.masscenter_vel(N) == N.z
|
| 166 |
+
|
| 167 |
+
def test_body_ang_vel():
|
| 168 |
+
with warns_deprecated_sympy():
|
| 169 |
+
A = Body('A')
|
| 170 |
+
N = ReferenceFrame('N')
|
| 171 |
+
with warns_deprecated_sympy():
|
| 172 |
+
B = Body('B', frame=N)
|
| 173 |
+
A.frame.set_ang_vel(N, N.y)
|
| 174 |
+
assert A.ang_vel_in(B) == N.y
|
| 175 |
+
assert B.ang_vel_in(A) == -N.y
|
| 176 |
+
assert A.ang_vel_in(N) == N.y
|
| 177 |
+
|
| 178 |
+
def test_body_dcm():
|
| 179 |
+
with warns_deprecated_sympy():
|
| 180 |
+
A = Body('A')
|
| 181 |
+
B = Body('B')
|
| 182 |
+
A.frame.orient_axis(B.frame, B.frame.z, 10)
|
| 183 |
+
assert A.dcm(B) == Matrix([[cos(10), sin(10), 0], [-sin(10), cos(10), 0], [0, 0, 1]])
|
| 184 |
+
assert A.dcm(B.frame) == Matrix([[cos(10), sin(10), 0], [-sin(10), cos(10), 0], [0, 0, 1]])
|
| 185 |
+
|
| 186 |
+
def test_body_axis():
|
| 187 |
+
N = ReferenceFrame('N')
|
| 188 |
+
with warns_deprecated_sympy():
|
| 189 |
+
B = Body('B', frame=N)
|
| 190 |
+
assert B.x == N.x
|
| 191 |
+
assert B.y == N.y
|
| 192 |
+
assert B.z == N.z
|
| 193 |
+
|
| 194 |
+
def test_apply_force_multiple_one_point():
|
| 195 |
+
a, b = symbols('a b')
|
| 196 |
+
P = Point('P')
|
| 197 |
+
with warns_deprecated_sympy():
|
| 198 |
+
B = Body('B')
|
| 199 |
+
f1 = a*B.x
|
| 200 |
+
f2 = b*B.y
|
| 201 |
+
B.apply_force(f1, P)
|
| 202 |
+
assert B.loads == [(P, f1)]
|
| 203 |
+
B.apply_force(f2, P)
|
| 204 |
+
assert B.loads == [(P, f1+f2)]
|
| 205 |
+
|
| 206 |
+
def test_apply_force():
|
| 207 |
+
f, g = symbols('f g')
|
| 208 |
+
q, x, v1, v2 = dynamicsymbols('q x v1 v2')
|
| 209 |
+
P1 = Point('P1')
|
| 210 |
+
P2 = Point('P2')
|
| 211 |
+
with warns_deprecated_sympy():
|
| 212 |
+
B1 = Body('B1')
|
| 213 |
+
B2 = Body('B2')
|
| 214 |
+
N = ReferenceFrame('N')
|
| 215 |
+
|
| 216 |
+
P1.set_vel(B1.frame, v1*B1.x)
|
| 217 |
+
P2.set_vel(B2.frame, v2*B2.x)
|
| 218 |
+
force = f*q*N.z # time varying force
|
| 219 |
+
|
| 220 |
+
B1.apply_force(force, P1, B2, P2) #applying equal and opposite force on moving points
|
| 221 |
+
assert B1.loads == [(P1, force)]
|
| 222 |
+
assert B2.loads == [(P2, -force)]
|
| 223 |
+
|
| 224 |
+
g1 = B1.mass*g*N.y
|
| 225 |
+
g2 = B2.mass*g*N.y
|
| 226 |
+
|
| 227 |
+
B1.apply_force(g1) #applying gravity on B1 masscenter
|
| 228 |
+
B2.apply_force(g2) #applying gravity on B2 masscenter
|
| 229 |
+
|
| 230 |
+
assert B1.loads == [(P1,force), (B1.masscenter, g1)]
|
| 231 |
+
assert B2.loads == [(P2, -force), (B2.masscenter, g2)]
|
| 232 |
+
|
| 233 |
+
force2 = x*N.x
|
| 234 |
+
|
| 235 |
+
B1.apply_force(force2, reaction_body=B2) #Applying time varying force on masscenter
|
| 236 |
+
|
| 237 |
+
assert B1.loads == [(P1, force), (B1.masscenter, force2+g1)]
|
| 238 |
+
assert B2.loads == [(P2, -force), (B2.masscenter, -force2+g2)]
|
| 239 |
+
|
| 240 |
+
def test_apply_torque():
|
| 241 |
+
t = symbols('t')
|
| 242 |
+
q = dynamicsymbols('q')
|
| 243 |
+
with warns_deprecated_sympy():
|
| 244 |
+
B1 = Body('B1')
|
| 245 |
+
B2 = Body('B2')
|
| 246 |
+
N = ReferenceFrame('N')
|
| 247 |
+
torque = t*q*N.x
|
| 248 |
+
|
| 249 |
+
B1.apply_torque(torque, B2) #Applying equal and opposite torque
|
| 250 |
+
assert B1.loads == [(B1.frame, torque)]
|
| 251 |
+
assert B2.loads == [(B2.frame, -torque)]
|
| 252 |
+
|
| 253 |
+
torque2 = t*N.y
|
| 254 |
+
B1.apply_torque(torque2)
|
| 255 |
+
assert B1.loads == [(B1.frame, torque+torque2)]
|
| 256 |
+
|
| 257 |
+
def test_clear_load():
|
| 258 |
+
a = symbols('a')
|
| 259 |
+
P = Point('P')
|
| 260 |
+
with warns_deprecated_sympy():
|
| 261 |
+
B = Body('B')
|
| 262 |
+
force = a*B.z
|
| 263 |
+
B.apply_force(force, P)
|
| 264 |
+
assert B.loads == [(P, force)]
|
| 265 |
+
B.clear_loads()
|
| 266 |
+
assert B.loads == []
|
| 267 |
+
|
| 268 |
+
def test_remove_load():
|
| 269 |
+
P1 = Point('P1')
|
| 270 |
+
P2 = Point('P2')
|
| 271 |
+
with warns_deprecated_sympy():
|
| 272 |
+
B = Body('B')
|
| 273 |
+
f1 = B.x
|
| 274 |
+
f2 = B.y
|
| 275 |
+
B.apply_force(f1, P1)
|
| 276 |
+
B.apply_force(f2, P2)
|
| 277 |
+
assert B.loads == [(P1, f1), (P2, f2)]
|
| 278 |
+
B.remove_load(P2)
|
| 279 |
+
assert B.loads == [(P1, f1)]
|
| 280 |
+
B.apply_torque(f1.cross(f2))
|
| 281 |
+
assert B.loads == [(P1, f1), (B.frame, f1.cross(f2))]
|
| 282 |
+
B.remove_load()
|
| 283 |
+
assert B.loads == [(P1, f1)]
|
| 284 |
+
|
| 285 |
+
def test_apply_loads_on_multi_degree_freedom_holonomic_system():
|
| 286 |
+
"""Example based on: https://pydy.readthedocs.io/en/latest/examples/multidof-holonomic.html"""
|
| 287 |
+
with warns_deprecated_sympy():
|
| 288 |
+
W = Body('W') #Wall
|
| 289 |
+
B = Body('B') #Block
|
| 290 |
+
P = Body('P') #Pendulum
|
| 291 |
+
b = Body('b') #bob
|
| 292 |
+
q1, q2 = dynamicsymbols('q1 q2') #generalized coordinates
|
| 293 |
+
k, c, g, kT = symbols('k c g kT') #constants
|
| 294 |
+
F, T = dynamicsymbols('F T') #Specified forces
|
| 295 |
+
|
| 296 |
+
#Applying forces
|
| 297 |
+
B.apply_force(F*W.x)
|
| 298 |
+
W.apply_force(k*q1*W.x, reaction_body=B) #Spring force
|
| 299 |
+
W.apply_force(c*q1.diff()*W.x, reaction_body=B) #dampner
|
| 300 |
+
P.apply_force(P.mass*g*W.y)
|
| 301 |
+
b.apply_force(b.mass*g*W.y)
|
| 302 |
+
|
| 303 |
+
#Applying torques
|
| 304 |
+
P.apply_torque(kT*q2*W.z, reaction_body=b)
|
| 305 |
+
P.apply_torque(T*W.z)
|
| 306 |
+
|
| 307 |
+
assert B.loads == [(B.masscenter, (F - k*q1 - c*q1.diff())*W.x)]
|
| 308 |
+
assert P.loads == [(P.masscenter, P.mass*g*W.y), (P.frame, (T + kT*q2)*W.z)]
|
| 309 |
+
assert b.loads == [(b.masscenter, b.mass*g*W.y), (b.frame, -kT*q2*W.z)]
|
| 310 |
+
assert W.loads == [(W.masscenter, (c*q1.diff() + k*q1)*W.x)]
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
def test_parallel_axis():
|
| 314 |
+
N = ReferenceFrame('N')
|
| 315 |
+
m, Ix, Iy, Iz, a, b = symbols('m, I_x, I_y, I_z, a, b')
|
| 316 |
+
Io = inertia(N, Ix, Iy, Iz)
|
| 317 |
+
# Test RigidBody
|
| 318 |
+
o = Point('o')
|
| 319 |
+
p = o.locatenew('p', a * N.x + b * N.y)
|
| 320 |
+
with warns_deprecated_sympy():
|
| 321 |
+
R = Body('R', masscenter=o, frame=N, mass=m, central_inertia=Io)
|
| 322 |
+
Ip = R.parallel_axis(p)
|
| 323 |
+
Ip_expected = inertia(N, Ix + m * b**2, Iy + m * a**2,
|
| 324 |
+
Iz + m * (a**2 + b**2), ixy=-m * a * b)
|
| 325 |
+
assert Ip == Ip_expected
|
| 326 |
+
# Reference frame from which the parallel axis is viewed should not matter
|
| 327 |
+
A = ReferenceFrame('A')
|
| 328 |
+
A.orient_axis(N, N.z, 1)
|
| 329 |
+
assert simplify(
|
| 330 |
+
(R.parallel_axis(p, A) - Ip_expected).to_matrix(A)) == zeros(3, 3)
|
| 331 |
+
# Test Particle
|
| 332 |
+
o = Point('o')
|
| 333 |
+
p = o.locatenew('p', a * N.x + b * N.y)
|
| 334 |
+
with warns_deprecated_sympy():
|
| 335 |
+
P = Body('P', masscenter=o, mass=m, frame=N)
|
| 336 |
+
Ip = P.parallel_axis(p, N)
|
| 337 |
+
Ip_expected = inertia(N, m * b ** 2, m * a ** 2, m * (a ** 2 + b ** 2),
|
| 338 |
+
ixy=-m * a * b)
|
| 339 |
+
assert not P.is_rigidbody
|
| 340 |
+
assert Ip == Ip_expected
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/test_functions.py
ADDED
|
@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy import sin, cos, tan, pi, symbols, Matrix, S, Function
|
| 2 |
+
from sympy.physics.mechanics import (Particle, Point, ReferenceFrame,
|
| 3 |
+
RigidBody)
|
| 4 |
+
from sympy.physics.mechanics import (angular_momentum, dynamicsymbols,
|
| 5 |
+
kinetic_energy, linear_momentum,
|
| 6 |
+
outer, potential_energy, msubs,
|
| 7 |
+
find_dynamicsymbols, Lagrangian)
|
| 8 |
+
|
| 9 |
+
from sympy.physics.mechanics.functions import (
|
| 10 |
+
center_of_mass, _validate_coordinates, _parse_linear_solver)
|
| 11 |
+
from sympy.testing.pytest import raises, warns_deprecated_sympy
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
q1, q2, q3, q4, q5 = symbols('q1 q2 q3 q4 q5')
|
| 15 |
+
N = ReferenceFrame('N')
|
| 16 |
+
A = N.orientnew('A', 'Axis', [q1, N.z])
|
| 17 |
+
B = A.orientnew('B', 'Axis', [q2, A.x])
|
| 18 |
+
C = B.orientnew('C', 'Axis', [q3, B.y])
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_linear_momentum():
|
| 22 |
+
N = ReferenceFrame('N')
|
| 23 |
+
Ac = Point('Ac')
|
| 24 |
+
Ac.set_vel(N, 25 * N.y)
|
| 25 |
+
I = outer(N.x, N.x)
|
| 26 |
+
A = RigidBody('A', Ac, N, 20, (I, Ac))
|
| 27 |
+
P = Point('P')
|
| 28 |
+
Pa = Particle('Pa', P, 1)
|
| 29 |
+
Pa.point.set_vel(N, 10 * N.x)
|
| 30 |
+
raises(TypeError, lambda: linear_momentum(A, A, Pa))
|
| 31 |
+
raises(TypeError, lambda: linear_momentum(N, N, Pa))
|
| 32 |
+
assert linear_momentum(N, A, Pa) == 10 * N.x + 500 * N.y
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def test_angular_momentum_and_linear_momentum():
|
| 36 |
+
"""A rod with length 2l, centroidal inertia I, and mass M along with a
|
| 37 |
+
particle of mass m fixed to the end of the rod rotate with an angular rate
|
| 38 |
+
of omega about point O which is fixed to the non-particle end of the rod.
|
| 39 |
+
The rod's reference frame is A and the inertial frame is N."""
|
| 40 |
+
m, M, l, I = symbols('m, M, l, I')
|
| 41 |
+
omega = dynamicsymbols('omega')
|
| 42 |
+
N = ReferenceFrame('N')
|
| 43 |
+
a = ReferenceFrame('a')
|
| 44 |
+
O = Point('O')
|
| 45 |
+
Ac = O.locatenew('Ac', l * N.x)
|
| 46 |
+
P = Ac.locatenew('P', l * N.x)
|
| 47 |
+
O.set_vel(N, 0 * N.x)
|
| 48 |
+
a.set_ang_vel(N, omega * N.z)
|
| 49 |
+
Ac.v2pt_theory(O, N, a)
|
| 50 |
+
P.v2pt_theory(O, N, a)
|
| 51 |
+
Pa = Particle('Pa', P, m)
|
| 52 |
+
A = RigidBody('A', Ac, a, M, (I * outer(N.z, N.z), Ac))
|
| 53 |
+
expected = 2 * m * omega * l * N.y + M * l * omega * N.y
|
| 54 |
+
assert linear_momentum(N, A, Pa) == expected
|
| 55 |
+
raises(TypeError, lambda: angular_momentum(N, N, A, Pa))
|
| 56 |
+
raises(TypeError, lambda: angular_momentum(O, O, A, Pa))
|
| 57 |
+
raises(TypeError, lambda: angular_momentum(O, N, O, Pa))
|
| 58 |
+
expected = (I + M * l**2 + 4 * m * l**2) * omega * N.z
|
| 59 |
+
assert angular_momentum(O, N, A, Pa) == expected
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def test_kinetic_energy():
|
| 63 |
+
m, M, l1 = symbols('m M l1')
|
| 64 |
+
omega = dynamicsymbols('omega')
|
| 65 |
+
N = ReferenceFrame('N')
|
| 66 |
+
O = Point('O')
|
| 67 |
+
O.set_vel(N, 0 * N.x)
|
| 68 |
+
Ac = O.locatenew('Ac', l1 * N.x)
|
| 69 |
+
P = Ac.locatenew('P', l1 * N.x)
|
| 70 |
+
a = ReferenceFrame('a')
|
| 71 |
+
a.set_ang_vel(N, omega * N.z)
|
| 72 |
+
Ac.v2pt_theory(O, N, a)
|
| 73 |
+
P.v2pt_theory(O, N, a)
|
| 74 |
+
Pa = Particle('Pa', P, m)
|
| 75 |
+
I = outer(N.z, N.z)
|
| 76 |
+
A = RigidBody('A', Ac, a, M, (I, Ac))
|
| 77 |
+
raises(TypeError, lambda: kinetic_energy(Pa, Pa, A))
|
| 78 |
+
raises(TypeError, lambda: kinetic_energy(N, N, A))
|
| 79 |
+
assert 0 == (kinetic_energy(N, Pa, A) - (M*l1**2*omega**2/2
|
| 80 |
+
+ 2*l1**2*m*omega**2 + omega**2/2)).expand()
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def test_potential_energy():
|
| 84 |
+
m, M, l1, g, h, H = symbols('m M l1 g h H')
|
| 85 |
+
omega = dynamicsymbols('omega')
|
| 86 |
+
N = ReferenceFrame('N')
|
| 87 |
+
O = Point('O')
|
| 88 |
+
O.set_vel(N, 0 * N.x)
|
| 89 |
+
Ac = O.locatenew('Ac', l1 * N.x)
|
| 90 |
+
P = Ac.locatenew('P', l1 * N.x)
|
| 91 |
+
a = ReferenceFrame('a')
|
| 92 |
+
a.set_ang_vel(N, omega * N.z)
|
| 93 |
+
Ac.v2pt_theory(O, N, a)
|
| 94 |
+
P.v2pt_theory(O, N, a)
|
| 95 |
+
Pa = Particle('Pa', P, m)
|
| 96 |
+
I = outer(N.z, N.z)
|
| 97 |
+
A = RigidBody('A', Ac, a, M, (I, Ac))
|
| 98 |
+
Pa.potential_energy = m * g * h
|
| 99 |
+
A.potential_energy = M * g * H
|
| 100 |
+
assert potential_energy(A, Pa) == m * g * h + M * g * H
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_Lagrangian():
|
| 104 |
+
M, m, g, h = symbols('M m g h')
|
| 105 |
+
N = ReferenceFrame('N')
|
| 106 |
+
O = Point('O')
|
| 107 |
+
O.set_vel(N, 0 * N.x)
|
| 108 |
+
P = O.locatenew('P', 1 * N.x)
|
| 109 |
+
P.set_vel(N, 10 * N.x)
|
| 110 |
+
Pa = Particle('Pa', P, 1)
|
| 111 |
+
Ac = O.locatenew('Ac', 2 * N.y)
|
| 112 |
+
Ac.set_vel(N, 5 * N.y)
|
| 113 |
+
a = ReferenceFrame('a')
|
| 114 |
+
a.set_ang_vel(N, 10 * N.z)
|
| 115 |
+
I = outer(N.z, N.z)
|
| 116 |
+
A = RigidBody('A', Ac, a, 20, (I, Ac))
|
| 117 |
+
Pa.potential_energy = m * g * h
|
| 118 |
+
A.potential_energy = M * g * h
|
| 119 |
+
raises(TypeError, lambda: Lagrangian(A, A, Pa))
|
| 120 |
+
raises(TypeError, lambda: Lagrangian(N, N, Pa))
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def test_msubs():
|
| 124 |
+
a, b = symbols('a, b')
|
| 125 |
+
x, y, z = dynamicsymbols('x, y, z')
|
| 126 |
+
# Test simple substitution
|
| 127 |
+
expr = Matrix([[a*x + b, x*y.diff() + y],
|
| 128 |
+
[x.diff().diff(), z + sin(z.diff())]])
|
| 129 |
+
sol = Matrix([[a + b, y],
|
| 130 |
+
[x.diff().diff(), 1]])
|
| 131 |
+
sd = {x: 1, z: 1, z.diff(): 0, y.diff(): 0}
|
| 132 |
+
assert msubs(expr, sd) == sol
|
| 133 |
+
# Test smart substitution
|
| 134 |
+
expr = cos(x + y)*tan(x + y) + b*x.diff()
|
| 135 |
+
sd = {x: 0, y: pi/2, x.diff(): 1}
|
| 136 |
+
assert msubs(expr, sd, smart=True) == b + 1
|
| 137 |
+
N = ReferenceFrame('N')
|
| 138 |
+
v = x*N.x + y*N.y
|
| 139 |
+
d = x*(N.x|N.x) + y*(N.y|N.y)
|
| 140 |
+
v_sol = 1*N.y
|
| 141 |
+
d_sol = 1*(N.y|N.y)
|
| 142 |
+
sd = {x: 0, y: 1}
|
| 143 |
+
assert msubs(v, sd) == v_sol
|
| 144 |
+
assert msubs(d, sd) == d_sol
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
def test_find_dynamicsymbols():
|
| 148 |
+
a, b = symbols('a, b')
|
| 149 |
+
x, y, z = dynamicsymbols('x, y, z')
|
| 150 |
+
expr = Matrix([[a*x + b, x*y.diff() + y],
|
| 151 |
+
[x.diff().diff(), z + sin(z.diff())]])
|
| 152 |
+
# Test finding all dynamicsymbols
|
| 153 |
+
sol = {x, y.diff(), y, x.diff().diff(), z, z.diff()}
|
| 154 |
+
assert find_dynamicsymbols(expr) == sol
|
| 155 |
+
# Test finding all but those in sym_list
|
| 156 |
+
exclude_list = [x, y, z]
|
| 157 |
+
sol = {y.diff(), x.diff().diff(), z.diff()}
|
| 158 |
+
assert find_dynamicsymbols(expr, exclude=exclude_list) == sol
|
| 159 |
+
# Test finding all dynamicsymbols in a vector with a given reference frame
|
| 160 |
+
d, e, f = dynamicsymbols('d, e, f')
|
| 161 |
+
A = ReferenceFrame('A')
|
| 162 |
+
v = d * A.x + e * A.y + f * A.z
|
| 163 |
+
sol = {d, e, f}
|
| 164 |
+
assert find_dynamicsymbols(v, reference_frame=A) == sol
|
| 165 |
+
# Test if a ValueError is raised on supplying only a vector as input
|
| 166 |
+
raises(ValueError, lambda: find_dynamicsymbols(v))
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
# This function tests the center_of_mass() function
|
| 170 |
+
# that was added in PR #14758 to compute the center of
|
| 171 |
+
# mass of a system of bodies.
|
| 172 |
+
def test_center_of_mass():
|
| 173 |
+
a = ReferenceFrame('a')
|
| 174 |
+
m = symbols('m', real=True)
|
| 175 |
+
p1 = Particle('p1', Point('p1_pt'), S.One)
|
| 176 |
+
p2 = Particle('p2', Point('p2_pt'), S(2))
|
| 177 |
+
p3 = Particle('p3', Point('p3_pt'), S(3))
|
| 178 |
+
p4 = Particle('p4', Point('p4_pt'), m)
|
| 179 |
+
b_f = ReferenceFrame('b_f')
|
| 180 |
+
b_cm = Point('b_cm')
|
| 181 |
+
mb = symbols('mb')
|
| 182 |
+
b = RigidBody('b', b_cm, b_f, mb, (outer(b_f.x, b_f.x), b_cm))
|
| 183 |
+
p2.point.set_pos(p1.point, a.x)
|
| 184 |
+
p3.point.set_pos(p1.point, a.x + a.y)
|
| 185 |
+
p4.point.set_pos(p1.point, a.y)
|
| 186 |
+
b.masscenter.set_pos(p1.point, a.y + a.z)
|
| 187 |
+
point_o=Point('o')
|
| 188 |
+
point_o.set_pos(p1.point, center_of_mass(p1.point, p1, p2, p3, p4, b))
|
| 189 |
+
expr = 5/(m + mb + 6)*a.x + (m + mb + 3)/(m + mb + 6)*a.y + mb/(m + mb + 6)*a.z
|
| 190 |
+
assert point_o.pos_from(p1.point)-expr == 0
|
| 191 |
+
|
| 192 |
+
|
| 193 |
+
def test_validate_coordinates():
|
| 194 |
+
q1, q2, q3, u1, u2, u3, ua1, ua2, ua3 = dynamicsymbols('q1:4 u1:4 ua1:4')
|
| 195 |
+
s1, s2, s3 = symbols('s1:4')
|
| 196 |
+
# Test normal
|
| 197 |
+
_validate_coordinates([q1, q2, q3], [u1, u2, u3],
|
| 198 |
+
u_auxiliary=[ua1, ua2, ua3])
|
| 199 |
+
# Test not equal number of coordinates and speeds
|
| 200 |
+
_validate_coordinates([q1, q2])
|
| 201 |
+
_validate_coordinates([q1, q2], [u1])
|
| 202 |
+
_validate_coordinates(speeds=[u1, u2])
|
| 203 |
+
# Test duplicate
|
| 204 |
+
_validate_coordinates([q1, q2, q2], [u1, u2, u3], check_duplicates=False)
|
| 205 |
+
raises(ValueError, lambda: _validate_coordinates(
|
| 206 |
+
[q1, q2, q2], [u1, u2, u3]))
|
| 207 |
+
_validate_coordinates([q1, q2, q3], [u1, u2, u2], check_duplicates=False)
|
| 208 |
+
raises(ValueError, lambda: _validate_coordinates(
|
| 209 |
+
[q1, q2, q3], [u1, u2, u2], check_duplicates=True))
|
| 210 |
+
raises(ValueError, lambda: _validate_coordinates(
|
| 211 |
+
[q1, q2, q3], [q1, u2, u3], check_duplicates=True))
|
| 212 |
+
_validate_coordinates([q1, q2, q3], [u1, u2, u3], check_duplicates=False,
|
| 213 |
+
u_auxiliary=[u1, ua2, ua2])
|
| 214 |
+
raises(ValueError, lambda: _validate_coordinates(
|
| 215 |
+
[q1, q2, q3], [u1, u2, u3], u_auxiliary=[u1, ua2, ua3]))
|
| 216 |
+
raises(ValueError, lambda: _validate_coordinates(
|
| 217 |
+
[q1, q2, q3], [u1, u2, u3], u_auxiliary=[q1, ua2, ua3]))
|
| 218 |
+
raises(ValueError, lambda: _validate_coordinates(
|
| 219 |
+
[q1, q2, q3], [u1, u2, u3], u_auxiliary=[ua1, ua2, ua2]))
|
| 220 |
+
# Test is_dynamicsymbols
|
| 221 |
+
_validate_coordinates([q1 + q2, q3], is_dynamicsymbols=False)
|
| 222 |
+
raises(ValueError, lambda: _validate_coordinates([q1 + q2, q3]))
|
| 223 |
+
_validate_coordinates([s1, q1, q2], [0, u1, u2], is_dynamicsymbols=False)
|
| 224 |
+
raises(ValueError, lambda: _validate_coordinates(
|
| 225 |
+
[s1, q1, q2], [0, u1, u2], is_dynamicsymbols=True))
|
| 226 |
+
_validate_coordinates([s1 + s2 + s3, q1], [0, u1], is_dynamicsymbols=False)
|
| 227 |
+
raises(ValueError, lambda: _validate_coordinates(
|
| 228 |
+
[s1 + s2 + s3, q1], [0, u1], is_dynamicsymbols=True))
|
| 229 |
+
_validate_coordinates(u_auxiliary=[s1, ua1], is_dynamicsymbols=False)
|
| 230 |
+
raises(ValueError, lambda: _validate_coordinates(u_auxiliary=[s1, ua1]))
|
| 231 |
+
# Test normal function
|
| 232 |
+
t = dynamicsymbols._t
|
| 233 |
+
a = symbols('a')
|
| 234 |
+
f1, f2 = symbols('f1:3', cls=Function)
|
| 235 |
+
_validate_coordinates([f1(a), f2(a)], is_dynamicsymbols=False)
|
| 236 |
+
raises(ValueError, lambda: _validate_coordinates([f1(a), f2(a)]))
|
| 237 |
+
raises(ValueError, lambda: _validate_coordinates(speeds=[f1(a), f2(a)]))
|
| 238 |
+
dynamicsymbols._t = a
|
| 239 |
+
_validate_coordinates([f1(a), f2(a)])
|
| 240 |
+
raises(ValueError, lambda: _validate_coordinates([f1(t), f2(t)]))
|
| 241 |
+
dynamicsymbols._t = t
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
def test_parse_linear_solver():
|
| 245 |
+
A, b = Matrix(3, 3, symbols('a:9')), Matrix(3, 2, symbols('b:6'))
|
| 246 |
+
assert _parse_linear_solver(Matrix.LUsolve) == Matrix.LUsolve # Test callable
|
| 247 |
+
assert _parse_linear_solver('LU')(A, b) == Matrix.LUsolve(A, b)
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def test_deprecated_moved_functions():
|
| 251 |
+
from sympy.physics.mechanics.functions import (
|
| 252 |
+
inertia, inertia_of_point_mass, gravity)
|
| 253 |
+
N = ReferenceFrame('N')
|
| 254 |
+
with warns_deprecated_sympy():
|
| 255 |
+
assert inertia(N, 0, 1, 0, 1) == (N.x | N.y) + (N.y | N.x) + (N.y | N.y)
|
| 256 |
+
with warns_deprecated_sympy():
|
| 257 |
+
assert inertia_of_point_mass(1, N.x + N.y, N) == (
|
| 258 |
+
(N.x | N.x) + (N.y | N.y) + 2 * (N.z | N.z) -
|
| 259 |
+
(N.x | N.y) - (N.y | N.x))
|
| 260 |
+
p = Particle('P')
|
| 261 |
+
with warns_deprecated_sympy():
|
| 262 |
+
assert gravity(-2 * N.z, p) == [(p.masscenter, -2 * p.mass * N.z)]
|
mplug_owl2/lib/python3.10/site-packages/sympy/physics/mechanics/tests/test_inertia.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy import symbols
|
| 2 |
+
from sympy.testing.pytest import raises
|
| 3 |
+
from sympy.physics.mechanics import (inertia, inertia_of_point_mass,
|
| 4 |
+
Inertia, ReferenceFrame, Point)
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def test_inertia_dyadic():
|
| 8 |
+
N = ReferenceFrame('N')
|
| 9 |
+
ixx, iyy, izz = symbols('ixx iyy izz')
|
| 10 |
+
ixy, iyz, izx = symbols('ixy iyz izx')
|
| 11 |
+
assert inertia(N, ixx, iyy, izz) == (ixx * (N.x | N.x) + iyy *
|
| 12 |
+
(N.y | N.y) + izz * (N.z | N.z))
|
| 13 |
+
assert inertia(N, 0, 0, 0) == 0 * (N.x | N.x)
|
| 14 |
+
raises(TypeError, lambda: inertia(0, 0, 0, 0))
|
| 15 |
+
assert inertia(N, ixx, iyy, izz, ixy, iyz, izx) == (ixx * (N.x | N.x) +
|
| 16 |
+
ixy * (N.x | N.y) + izx * (N.x | N.z) + ixy * (N.y | N.x) + iyy *
|
| 17 |
+
(N.y | N.y) + iyz * (N.y | N.z) + izx * (N.z | N.x) + iyz * (N.z |
|
| 18 |
+
N.y) + izz * (N.z | N.z))
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_inertia_of_point_mass():
|
| 22 |
+
r, s, t, m = symbols('r s t m')
|
| 23 |
+
N = ReferenceFrame('N')
|
| 24 |
+
|
| 25 |
+
px = r * N.x
|
| 26 |
+
I = inertia_of_point_mass(m, px, N)
|
| 27 |
+
assert I == m * r**2 * (N.y | N.y) + m * r**2 * (N.z | N.z)
|
| 28 |
+
|
| 29 |
+
py = s * N.y
|
| 30 |
+
I = inertia_of_point_mass(m, py, N)
|
| 31 |
+
assert I == m * s**2 * (N.x | N.x) + m * s**2 * (N.z | N.z)
|
| 32 |
+
|
| 33 |
+
pz = t * N.z
|
| 34 |
+
I = inertia_of_point_mass(m, pz, N)
|
| 35 |
+
assert I == m * t**2 * (N.x | N.x) + m * t**2 * (N.y | N.y)
|
| 36 |
+
|
| 37 |
+
p = px + py + pz
|
| 38 |
+
I = inertia_of_point_mass(m, p, N)
|
| 39 |
+
assert I == (m * (s**2 + t**2) * (N.x | N.x) -
|
| 40 |
+
m * r * s * (N.x | N.y) -
|
| 41 |
+
m * r * t * (N.x | N.z) -
|
| 42 |
+
m * r * s * (N.y | N.x) +
|
| 43 |
+
m * (r**2 + t**2) * (N.y | N.y) -
|
| 44 |
+
m * s * t * (N.y | N.z) -
|
| 45 |
+
m * r * t * (N.z | N.x) -
|
| 46 |
+
m * s * t * (N.z | N.y) +
|
| 47 |
+
m * (r**2 + s**2) * (N.z | N.z))
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def test_inertia_object():
|
| 51 |
+
N = ReferenceFrame('N')
|
| 52 |
+
O = Point('O')
|
| 53 |
+
ixx, iyy, izz = symbols('ixx iyy izz')
|
| 54 |
+
I_dyadic = ixx * (N.x | N.x) + iyy * (N.y | N.y) + izz * (N.z | N.z)
|
| 55 |
+
I = Inertia(inertia(N, ixx, iyy, izz), O)
|
| 56 |
+
assert isinstance(I, tuple)
|
| 57 |
+
assert I.__repr__() == ('Inertia(dyadic=ixx*(N.x|N.x) + iyy*(N.y|N.y) + '
|
| 58 |
+
'izz*(N.z|N.z), point=O)')
|
| 59 |
+
assert I.dyadic == I_dyadic
|
| 60 |
+
assert I.point == O
|
| 61 |
+
assert I[0] == I_dyadic
|
| 62 |
+
assert I[1] == O
|
| 63 |
+
assert I == (I_dyadic, O) # Test tuple equal
|
| 64 |
+
raises(TypeError, lambda: I != (O, I_dyadic)) # Incorrect tuple order
|
| 65 |
+
assert I == Inertia(O, I_dyadic) # Parse changed argument order
|
| 66 |
+
assert I == Inertia.from_inertia_scalars(O, N, ixx, iyy, izz)
|
| 67 |
+
# Test invalid tuple operations
|
| 68 |
+
raises(TypeError, lambda: I + (1, 2))
|
| 69 |
+
raises(TypeError, lambda: (1, 2) + I)
|
| 70 |
+
raises(TypeError, lambda: I * 2)
|
| 71 |
+
raises(TypeError, lambda: 2 * I)
|