0% found this document useful (0 votes)
6 views

589 SymPy Mechanics For Autolev Users - SymPy 1.11 Documentation

Uploaded by

Mariela Tissera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

589 SymPy Mechanics For Autolev Users - SymPy 1.11 Documentation

Uploaded by

Mariela Tissera
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.

11 documentation

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 SymPy is a library written in the general purpose language Python.
designed to perform multibody dynamics. Since it is a Although Autolev’s code is more compact, SymPy (by virtue of being an
language of its own, it has a very rigid language add on to Python) is more flexible. The users have more control over what
specification. It predefines, assumes and computes they can do. For example, one can create a class in their code for let’s say a
many things based on the input code. Its code is a lot type of rigibodies with common properties. The wide array of scientific
cleaner and concise as a result of this. Python libraries available is also a big plus.

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 1/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Mechanics

SymPy generates numerical Python, C or Octave/Matlab code from a large


Autolev generates Matlab, C, or Fortran code from a set of symbolic mathematics created with SymPy. It also builds on the
small set of symbolic mathematics. popular scientific Python stack such as NumPy, SciPy, IPython, matplotlib,
Cython and Theano.

Autolev uses 1 (one) based indexing. The initial element Python uses 0 (zero) based indexing. The initial element of a sequence is
of a sequence is found using a[1]. 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


SymPy code is Python code, so one can define functions in their code. This
making .R and .A files which can be used in their
is a lot more convenient.
programs.

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
https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 2/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

Note that the names of the symbols can be different from the
a, b = sm.symbols(‘a b’,
Constants A, B names of the variables they are assigned to. We can define a, b =
real=True)
symbols(‘b a’) but its good practice to follow the convention.

c = sm.symbols(‘c’,
Constants C+ real=True, Refer to SymPy assumptions for more information.
nonnegative=True)

d = sm.symbols(‘d’,
Constants D- real=True,
nonpositive=True)

k1, k2, k3, k4 =


Constants K{4} sm.symbols('k1 k2 k3
k4', real=True)

a2, a3, a4 =
Constants a{2:4} sm.symbols('a2 a3 a4',
real=True)

b11, b12, b21, b22 =


Constants b{1:2, 1:2} sm.symbols('b11 b12 b21
b22', real=True)

phi =
Specified Phi me.dynamicsymbols(‘phi
')

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 3/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

q, s =
Variables q, s
me.dynamicsymbols(q, s)

x =
me.dynamicsymbols(‘x’ )

xd =
me.dynamicsymbols(‘x’ ,
Variables x’’
1)

xd2 =
me.dynamicsymbols(‘x’ ,
2)

y1 =
me.dynamicsymbols(‘y1’
)

y2 =
me.dynamicsymbols(‘y2’
)
Variables y{2}’
y1d =
me.dynamicsymbols(‘y1’
, 1)

y2d =
me.dynamicsymbols(‘y2'
, 1)

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 4/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

u1 =
me.dynamicsymbols(‘u1’
SymPy doesn’t differentiate between variables, motionvariables
)
MotionVariables u{2} and specifieds during declaration. Instead, it takes different lists of
u2 =
these as parameters in objects like the KanesMethod.
me.dynamicsymbols('u2'
)

I is a sympy object which stands for the imaginary unit. One can
define complex numbers using it.
Imaginary j j = sm.I
z = x + I*y

where x, y and z are symbols.

tina = 2*sm.pi

tina = tina.evalf()
Tina = 2*pi
t = Using .evalf() will result in the numeric value.
s = u*t + a*t^2/2
me.dynamicsymbols._t

s = u*t + a*t**2/2

sm.abs(x)**3 +
abs(x)^3 + sin(x)^2 + acos(x) sm.sin(x)**2 +
sm.acos(x)

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 5/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

E = (x+2*y)**2 + 3*
(7+x)*(x+y)
E = (x+2*y)^2 + 3*(7+x)*(x+y)
sm.expand(E)
Expand(E)
sm.horner(E, wrt=x) For more information refer to simplification.
Factor(E, x)
y.coeff(x) These SymPy functions do not work in place. They just return
Coef(y, x)
y.subs({sm.sin(x): 3}) expressions. If you want to overwrite the original expression you
Replace(y, sin(x)=3)
e.collect(x).coeff( x, would have to do something like:
Exclude(E,x)
0) y = y.subs({sm.sin(x): 3})
Include(E,x)
e.collect(x).coeff( x,
Arrange(E,2,y)
1)

e.collect(y)

E.diff(y)

E.diff(
Dy = D(E, y)
me.dynamicsymbols._t )
Dt = Dt(E)
Works if the expression
Dt2 = Dt(V, A) where V is a vector For more information refer to calculus.
is made up of
and A is a frame
dynamicsymbols.
Dy2 = D(V, y, A)
dt2 = v.dt(A)

dy2 = v.diff(y, A)

e = sm.cos(x*y)
E = COS(X*Y) b = e.series(x, 0,
For more information refer to series.
TY = Taylor(E, 0:2, x=0, y=0) 2).removeO().series(y,
0, 2).removeO()

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 6/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

E.subs([(x, a), (y,


2)])

To get floating point


numbers from
F = Evaluate(E, x=a, y=2)
numerical expressions
use .evalf()
E.evalf((a +
sm.pi).subs({a: 3}))

p =
sm.Poly(sm.Matrix([a,
P = Polynomial([a, b, c], x) For more information refer to polys.
b, c]).reshape(1, 3),
x)

sm.solve(
sm.Poly(a*x**2 + b*x +
Roots(Polynomial( a*x^2 + b*x + c, For more information refer to Solvers.
c))
x, 2) For numerical computation related to polynomials and roots refer
sm.solve(sm.Poly(
Roots([1;2;3]) to mpmath/calculus.
sm.Matrix([1,2,3]).
reshape(3, 1), x), x)

Solve(A, x1, x2) sm.linsolve(A, (x1,

where A is an augmented matrix that x2)) For more information refer to :ref:` solvers/solveset. <solveset>`
represents the linear equations and where A is an For non linear solvers refer to nonlinsolve and nsolve in solvers.
x1, x2 are the variables to solve for. augmented matrix

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 7/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

row_matrix =
sm.Matrix([[1],[2],
[3],[4]])
RowMatrix = [1, 2, 3, 4]
col_matrix =
ColMatrix = [1; 2; 3; 4]
sm.Matrix([1, 2, 3, 4])
MO = [a, b; c, 0]
MO = sm.Matrix([[a, b],
MO[2, 2] := d [c, 0]])
A + B*C MO[1, 1] = d
Cols(A) A + B*C
Cols(A, 1) A.cols
Rows(A) A.col(0)
Rows(A, 1) A.rows For more information refer to matrices.
Det(A) A.row(0)
Element(A, 2, 3) M.det()
Inv(A) M[2, 3]
Trace(A) M**-1
Transpose(A) sm.trace(A)
Diagmat(4, 1) A.T
Eig(A) sm.diag(1,1,1,1)
Eig(A, EigVal, EigVec) A.eigenvals()

eigval = A.eigenvals()

eigvec = A.eigenvects()

Physical Equivalents

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 8/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

m =sm.symbols(‘m’)

Ao = sm.symbols(‘Ao’)
The 4th and 5th arguments are for the mass and
Bodies A
inertia. These are specified after the declaration in
Declares A, its Af = me.ReferenceFrame(‘Af’ )
Autolev.
masscenter Ao, and I = me.outer(Af.x,Af.x)
One can pass a dummy for the parameters and use
orthonormal vectors P = me.Point(‘P’)
setters A.mass = \_ and A.inertia = \_ to set them
A1>, A2> and A3> fixed A =me.RigidBody(‘A’, Ao, Af, m, (I, P)) later.
in A.
Af.x, Af.y and Af.z are equivalent to A1>, A2> For more information refer to mechanics/masses .
and A3>.

Frames A A = me.ReferenceFrame(‘A’ )
For more information refer to physics/vectors.
V1> = X1*A1> + X2*A2> v1 = x1*A.x + x2*A.y

SymPy doesn’t specify that a frame is inertial during


Newtonian N N = me.ReferenceFrame(‘N’ ) declaration. Many functions such as set_ang_vel()
take the inertial reference frame as a parameter.

The 2nd and 3rd arguments are for the point and
m = sm.symbols(‘m’) mass. In Autolev, these are specified after the
Particles C Po = me.Point(‘Po’) declaration.
C = me.Particle(‘C’, Po, m) One can pass a dummy and use setters ( A.point =
\_ and A.mass = \_ ) to set them later.

P = me.Point(‘P’)
Points P, Q
Q = me.Point(‘Q’)

mB = symbols(‘mB’)
Mass B=mB
B.mass = mB

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 9/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

I = me.inertia(Bf, i1, i2, i3, i12, i23, i31)

B.inertia = (I, P) where B is a rigidbody, Bf is


the related frame and P is the center of mass of
Inertia B, I1, I2, I3,
B. For more information refer to the mechanics api.
I12, I23, I31
Inertia dyadics can also be formed using vector
outer products.
I = me.outer(N.x, N.x)

vec> = P_O_Q>/L vec = (Qo.pos_from(O))/L

vec> = u1*N1> + u2*N2> vec = u1*N.x + u2*N.y

Cross(a>, b>) cross(a, b)

Dot(a>, b>) dot(a, b)


For more information refer to physics/vectors.
Mag(v>) v.magnitude()

Unitvec(v>) v.normalize()

DYAD>> = 3*A1>*A1> + dyad = 3*me.outer(a.x ,a.x) + me.outer(a.y, a.y)


A2>*A2> + 2*A3>*A3> + 2*me.outer(a.z ,a.z)

For more information refer to the kinematics api.


Q.point = O.locatenew(‘Qo’, LA*A.x)
P_O_Q> = LA*A1> All these vector and kinematic functions are to be
where A is a reference frame.
P_P_Q> = LA*A1> used on Point objects and not Particle objects so
Q.point = P.point.locatenew(‘Qo ’, LA*A.x)
.point must be used for particles.

V_O_N> = u3*N.1> +
O.set_vel(N, u1*N.x + u2*N.y)
u4*N.2> The getter would be O.vel(N) .
O.partial_velocity(N , u3)
Partials(V_O_N>, u3)

A_O_N> = 0>

Acceleration of point O O.set_acc(N, 0) The getter would be O.acc(N) .


in reference frame N.

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 10/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

W_B_N> = qB’*B3>
B.set_ang_vel(N, qBd*Bf.z)
Angular velocity of body The getter would be B.ang_vel_in(N) .
where Bf is the frame associated with the body B.
B in reference frame F.

ALF_B_N> =Dt(W_B_N>, N)

Angular acceleration of
B.set_ang_acc(N, diff(B.ang_vel_in(N) ) The getter would be B.ang_acc_in(N) .
body B in reference
frame N.

In SymPy one should have a list which contains


Force_O> = F1*N1> + all the forces and torques.
F2*N2> fL.append((O, f1*N.x + f2*N.y))

Torque_A> = -c*qA’*A3> where fL is the force list.


fl.append((A, -c*qAd*A.z))

A_B = Mwhere M is a
B.orient(A, 'DCM', M) where M is a SymPy
matrix and A, B are
Matrix.
frames.
D = A.dcm(B)*2 + 1
D = A_B*2 + 1

CM(B) B.masscenter

Mass(A,B,C) A.mass + B.mass + C.mass

P and Q are assumed to be Point objects here.


V1pt(A,B,P,Q) Q.v1pt_theory(P, A, B)
Remember to use .point for particles.

V2pts(A,B,P,Q) Q.v2pt_theory(P, A, B)

A1pt(A,B,P,Q) Q.a1pt_theory(P, A, B)

A2pts(A,B,P,Q) Q.a2pt_theory(P, A, B)

Angvel(A,B) B.ang_vel_in(A)

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 11/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

Simprot(A, B, 1, qA) B.orient(A, ‘Axis’, qA, A.x)

In SymPy we must use a forceList (here fL) which


contains tuples of the form (point, force_vector) .
Gravity(G*N1>) fL.extend(gravity( g*N.x, P1, P2, ...))
This is passed to the kanes_equations() method of
the KanesMethod object.

CM(O,P1,R) me.functions. center_of_mass(o, p1, r)

Force(P/Q, v>) fL.append((P, -1*v), (Q, v))

Torque(A/B, v>) fL.append((A, -1*v), (B, v))

Kindiffs(A, B ...) KM.kindiffdict()

linear_momentum(N, B1, B2 ...)

reference frame followed by one or more bodies


Momentum(option) angular_momentum(O, N, B1, B2 ...)

point, reference frame followed by one or more


bodies

kinetic_energy(N, B1, B2 ...)


KE()
reference frame followed by one or more bodies

velocity_constraints = [...]

u_dependent = [...]
For more details refer to mechanics/kane and the
Constrain(...) u_auxiliary = [...]
kane api.
These lists are passed to the KanesMethod
object.

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 12/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

Autolev SymPy Notes

KM = KanesMethod(f, q_ind, u_ind, kd_eqs,


q_dependent, configura tion_constraints, u_de
pendent, velocity_cons traints, acceleration_
constraints, u_auxilia ry)
For more details refer to mechanics/kane and the
Fr() FrStar() The KanesMethod object takes a reference frame
kane api.
followed by multiple lists as arguments.
(fr, frstar) = KM.kanes_equations(fL, bL)
where fL and bL are lists of forces and bodies
respectively.

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


https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 13/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

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.

Aesara: Aesara is
a numerical computation library for Python. In Aesara, 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.

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 14/15
4/11/22, 10:15 SymPy Mechanics for Autolev Users - SymPy 1.11 documentation

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.

Links
SymPy Introductory Tutorial

SymPy Documentation

SymPy Physics Vector Documentation

SymPy Mechanics Documentation

PyDy Documentation

MultiBody Dynamics with Python

Copyright © 2022 SymPy Development Team


Made with Sphinx and @pradyunsg's Furo
Last updated on Aug 22, 2022

https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 15/15

You might also like