589 SymPy Mechanics For Autolev Users - SymPy 1.11 Documentation
589 SymPy Mechanics For Autolev Users - SymPy 1.11 Documentation
11 documentation
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.
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 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.
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
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)
a2, a3, a4 =
Constants a{2:4} sm.symbols('a2 a3 a4',
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
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
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
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
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
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)
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
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
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
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
Unitvec(v>) v.normalize()
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>
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
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.
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
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
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
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:
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()
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.
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.
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
PyDy Documentation
https://docs.sympy.org/latest/modules/physics/mechanics/sympy_mechanics_for_autolev_users.html 15/15