SymPy Mechanics for Autolev Users¶
Introduction¶
Autolev (now superseded by MotionGenesis) is a domain specific programming language which is used for symbolic multibody dynamics. The SymPy mechanics module now has enough power and functionality to be a fully featured symbolic dynamics module. The PyDy package extends the SymPy output to the numerical domain for simulation, analyses and visualization. Autolev and SymPy Mechanics have a lot in common but there are also many differences between them. This page shall expand upon their differences. It is meant to be a go-to reference for Autolev users who want to transition to SymPy Mechanics.
It would be nice to have a basic understanding of SymPy and SymPy Mechanics before going over this page. If you are completely new to Python, you can check out the official Python Tutorial. Check out the SymPy Documentation, especially the tutorial to get a feel for SymPy. For an introduction to Multibody dynamics in Python, this lecture is very helpful.
You might also find the Autolev Parser which is a part of SymPy to be helpful.
Some Key Differences¶
Autolev |
SymPy Mechanics |
---|---|
Autolev is a domain specific
programming language designed to
perform multibody dynamics. Since
it is a language of its own, it
has a very rigid language
specification. It predefines,
assumes and computes
many things based on the
input code. Its code is a lot
cleaner and concise as a result of
this.
|
SymPy is a library written in the
general purpose language Python.
Although Autolev’s code is more
compact, SymPy (by virtue of being
an add on to Python) is more
flexible. The users have more
control over what they can do. For
example, one can create a class in
their code for let’s say a type of
rigibodies with common
properties.
The wide array of scientific
Python libraries available is also
a big plus.
|
Autolev generates Matlab, C, or
Fortran code from a small set of
symbolic mathematics.
|
SymPy generates numerical Python,
C or Octave/Matlab code from a
large set of symbolic mathematics
created with SymPy. It also builds
on the popular scientific Python
stack such as NumPy, SciPy,
IPython, matplotlib, Cython and
Theano.
|
Autolev uses 1 (one) based
indexing. The initial element of
a sequence is found using a[1].
|
Python uses 0 (zero) based
indexing. The initial element of
a sequence is found using a[0].
|
Autolev is case insensitive.
|
SymPy code being Python code is
case sensitive.
|
One can define their own commands
in Autolev by making .R and .A
files which can be used in their
programs.
|
SymPy code is Python code, so one
can define functions in their
code. This is a lot more
convenient.
|
Autolev is proprietary.
|
SymPy is open source.
|
Rough Autolev-SymPy Equivalents¶
The tables below give rough equivalents for some common Autolev expressions. These are not exact equivalents, but rather should be taken as hints to get you going in the right direction. For more detail read the built-in documentation on SymPy vectors, SymPy mechanics and PyDy .
In the tables below, it is assumed that you have executed the following commands in Python:
import sympy.physics.mechanics as me
import sympy as sm
Mathematical Equivalents¶
Autolev |
SymPy |
Notes |
---|---|---|
Constants A, B |
a, b =
sm.symbols(‘a
b’, real=True) |
Note that the names
of the symbols can be
different from the
names of the
variables they are
assigned to. We can
define
a, b =
symbols(‘b a’) but
its good practice to
follow the
convention. |
Constants C+ |
c = sm.symbols(‘c’,
real=True,
nonnegative=True) |
Refer to SymPy
assumptions
for more information.
|
Constants D- |
d = sm.symbols(‘d’,
real=True,
nonpositive=True) |
|
Constants K{4} |
k1, k2, k3, k4 =
sm.symbols('k1 k2 k3
k4', real=True) |
|
Constants a{2:4} |
a2, a3, a4 =
sm.symbols('a2 a3 a4',
real=True) |
|
Constants
b{1:2, 1:2} |
b11, b12, b21, b22 =
sm.symbols('b11 b12
b21 b22', real=True) |
|
Specified Phi |
phi =
me.dynamicsymbols(‘phi
') |
|
Variables q, s |
q, s =
me.dynamicsymbols(q,
s) |
|
|
|
|
|
|
|
|
|
SymPy doesn’t differentiate between variables, motionvariables and specifieds during declaration. Instead, it takes different lists of these as parameters in objects like the KanesMethod. |
|
|
I is a sympy object which stands for the imaginary unit. One can define complex numbers using it.
where x, y and z are symbols. |
|
|
Using |
abs(x)^3 + sin(x)^2
+ acos(x) |
sm.abs(x)**3
+ sm.sin(x)**2 +
sm.acos(x) |
|
|
|
For more information refer to simplification. These SymPy functions do not work in place. They just return expressions. If you want to overwrite the original expression you would have to do something like:
|
|
Works if the expression is made up of dynamicsymbols.
|
For more information refer to calculus. |
|
|
For more information refer to series. |
|
To get floating point
numbers from numerical
expressions use
|
|
|
|
For more information refer to polys. |
|
|
For more information refer to Solvers. For numerical computation related to polynomials and roots refer to mpmath/calculus. |
where A is an augmented matrix that represents the linear equations and x1, x2 are the variables to solve for. |
where A is an augmented matrix |
For more information refer to :ref:` solvers/solveset. <solveset>` For non linear solvers
refer to
|
|
|
For more information refer to matrices. |
Physical Equivalents¶
Autolev |
SymPy |
Notes |
---|---|---|
Declares A, its masscenter Ao, and orthonormal vectors A1>, A2> and A3> fixed in A. |
Af.x, Af.y and Af.z are equivalent to A1>, A2> and A3>. |
The 4th and 5th arguments are for the mass and inertia. These are specified after the declaration in Autolev. One can pass a dummy
for the parameters
and use setters
For more information refer to mechanics/masses . |
|
|
For more information refer to physics/vectors. |
|
|
SymPy doesn’t specify
that a frame is
inertial during
declaration. Many
functions such as
|
|
|
The 2nd and 3rd arguments are for the point and mass. In Autolev, these are specified after the declaration. One can pass a dummy
and use setters
( |
|
|
|
|
|
|
|
Inertia dyadics can also be formed using vector outer products.
|
For more information refer to the mechanics api. |
|
|
For more information refer to physics/vectors. |
|
where A is a reference frame.
|
For more information refer to the kinematics api. All these vector and
kinematic functions
are to be used on
|
|
|
The getter would be
|
Acceleration of point O in reference frame N. |
|
The getter would be
|
Angular velocity of body B in reference frame F. |
where Bf is the frame associated with the body B. |
The getter would be
|
Angular acceleration of body B in reference frame N. |
|
The getter would be
|
|
In SymPy one should have a list which contains all the forces and torques.
where fL is the force list.
|
|
|
|
|
|
|
|
|
|
|
|
|
P and Q are assumed to
be |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In SymPy we must use a
forceList (here fL)
which contains tuples
of the form |
|
|
|
|
|
|
|
|
|
|
|
|
|
reference frame followed by one or more bodies
point, reference frame followed by one or more bodies |
|
|
reference frame followed by one or more bodies |
|
|
These lists are passed to the KanesMethod object. |
For more details refer to mechanics/kane and the kane api. |
|
The KanesMethod object takes a reference frame followed by multiple lists as arguments.
|
For more details refer to mechanics/kane and the kane api. |
Numerical Evaluation and Visualization¶
Autolev’s CODE Option() command allows one to generate Matlab, C, or Fortran code for numerical evaluation and visualization. Option can be Dynamics, ODE, Nonlinear or Algebraic.
Numerical evaluation for dynamics can be achieved using PyDy. One can pass in the KanesMethod object to the System class along with the values for the constants, specifieds, initial conditions and time steps. The equations of motion can then be integrated. The plotting is achieved using matlplotlib. Here is an example from the PyDy Documentation on how it is done:
from numpy import array, linspace, sin
from pydy.system import System
sys = System(kane,
constants = {mass: 1.0, stiffness: 1.0,
damping: 0.2, gravity: 9.8},
specifieds = {force: lambda x, t: sin(t)},
initial_conditions = {position: 0.1, speed:-1.0},
times = linspace(0.0, 10.0, 1000))
y = sys.integrate()
import matplotlib.pyplot as plt
plt.plot(sys.times, y)
plt.legend((str(position), str(speed)))
plt.show()
For information on all the things PyDy can accomplish refer to the PyDy Documentation.
The tools in the PyDy workflow are :
- SymPy: SymPy is a Python library for
symbolic computation. It provides computer algebra capabilities either as a standalone application, as a library to other applications, or live on the web as SymPy Live or SymPy Gamma.
- NumPy: NumPy is a library for the
Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
- SciPy: SciPy is an open source
Python library used for scientific computing and technical computing. SciPy contains modules for optimization, linear algebra, integration, interpolation, special functions, FFT, signal and image processing, ODE solvers and other tasks common in science and engineering.
- IPython: IPython is a command shell
for interactive computing in multiple programming languages, originally developed for the Python programming language, that offers introspection, rich media, shell syntax, tab completion, and history.
- Theano: Theano is
a numerical computation library for Python. In Theano, computations are expressed using a NumPy-esque syntax and compiled to run efficiently on either CPU or GPU architectures
- Cython: Cython is a superset of the
Python programming language, designed to give C-like performance with code that is mostly written in Python. Cython is a compiled language that generates CPython extension modules.
- matplotlib: matplotlib is a
plotting library for the Python programming language and its numerical mathematics extension NumPy.
One will be able to write code equivalent to the Matlab, C or Fortran code generated by Autolev using these scientific computing tools. It is recommended to go over these modules to gain an understanding of scientific computing with Python.