Mechanics and Sympy

dimanche, 4 janvier 2015

As an undergraduate student in physics, some of the code I write is related with mechanical problems.

This post is an attempt to find eigenpulsations and eigenvectors of a 5 degres of freedom oscillator using sympy.

What is Sympy

While working with simple maths, Python's builtins and the math librairy should do the trick : you'll be able to compute almost anything you need.

If your work requires a more powerful tool for numerical calculations, you should take a look at Numpy or Pylab and you would get solution to your problem.

But if you want to work on symbolic maths (means maths with free symbols that represent part of the system), you'll have to find a formal calculation framework or symbolic math framework.

Software exists to do that kind of stuff :

Sympy is a python lib (mostly python2) providing tools for symbolic algebra and resolution as well as numerical computation. Finally, sympy does provide a way of exanching data and code with Sage, Matlab and Mathematica.

Installing Sympy

If you have pip just run :

pip install sympy

(On ArchLinux make sure to use Python2 and pip2)

On my system, I run Sympy 0.7.2.

Problem : 5DOF

Let's take a look to our problem :

/static/images/5ddl/oscil5.png

We have 5 coupled oscillators (spring-mass systems) with same mass and spring constant.

Each mass follow a movement named xi=Xisin(ωt);i{1,2,3,4,5}

We can write equations for this kind of system quite easily :

System Message: ERROR/3 (<string>, line 73)

Environment not supported! Supported environment: "matrix".

m
\begin{pmatrix}
1 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0\\
0 & 0 & 1 & 0 & 0\\
0 & 0 & 0 & 1 & 0\\
0 & 0 & 0 & 0 & 1
\end{pmatrix}
\begin{pmatrix}
\ddot{x_1}\\
\ddot{x_2}\\
\ddot{x_3}\\
\ddot{x_4}\\
\ddot{x_5}\\
\end{pmatrix}
+
k
\begin{pmatrix}
2 & -1 & 0 & 0 & 0\\
-1 & 2 & -1 & 0 & 0\\
0 & -1 & 2 & -1 & 0\\
0 & 0 & -1 & 2 & -1\\
0 & 0 & 0 & -1 & 2
\end{pmatrix}
\begin{pmatrix}
x_1\\
x_2\\
x_3\\
x_4\\
x_5\\
\end{pmatrix} =
\begin{pmatrix}
0\\
0\\
0\\
0\\
0\\
\end{pmatrix}

It comes :

System Message: ERROR/3 (<string>, line 116)

Unknown LaTeX command: underbrace

\underbrace{
\begin{pmatrix}
(2\omega_0^2-\omega^2) & -\omega_0^2 & 0 & 0 & 0\\
-\omega_0^2 & (2\omega_0^2-\omega^2) & -\omega_0^2 & 0 & 0\\
0 & -\omega_0^2 & (2\omega_0^2-\omega^2) & -\omega_0^2 & 0\\
0 & 0 & -\omega_0^2 & (2\omega_0^2-\omega^2) & -\omega_0^2\\
0 & 0 & 0 & -\omega_0^2 & (2\omega_0^2-\omega^2)
\end{pmatrix}
}_S
\underbrace{
\begin{pmatrix}
X_1\\
X_2\\
X_3\\
X_4\\
X_5\\
\end{pmatrix}}_X =
\underbrace{\begin{pmatrix}
0\\
0\\
0\\
0\\
0\\
\end{pmatrix}}_L

We now have to translate this maths into python instances :

from __future__ import print_function, division

from sympy import Symbol, symbols, Matrix, zeros

# Coefficient matrix
a = Symbol('w0') # omega0
w = Symbol('w') # omega

S = Matrix([
    [(2*a**2-w**2),-a**2, 0, 0, 0],
    [-a**2, (2*a**2-w**2),-a**2, 0, 0],
    [0, -a**2, (2*a**2-w**2),-a**2, 0],
    [0, 0, -a**2, (2*a**2-w**2),-a**2],
    [0, 0, 0, -a**2, (2*a**2-w**2)]
])


# movements amplitudes
X1,X2,X3,X4,X5 = symbols('X1 X2 X3 X4 X5')

X = Matrix([
    [X1],
    [X2],
    [X3],
    [X4],
    [X5]
])

# initial conditions
L = zeros(5,1)

Time to solve

For this kind of problem, one (brutal) way to solve is to try to find eigenpulsations out of the S matrix (means that we have to find all w2|det(S)=0).

Eigenpulsations

Finding the determinant of a 5×5 matrix is not something really interesting, so let's get Python to do that :

determ = S.det_bareis()

We could have used det() instead of det_bareis() but the latter is preconised for matrices with symbols.

Now we must solve the equation : detS=0 for ω2. Sympy provides something cool for that kind of thing : solve(). So let's use it and print solutions next :

from sympy.solvers import solve
from sympy import pprint # for pretty printing

eigenpulsations = solve(determ, w**2)

for n,s in enumerate(eigenpulsations):
    print("Solution "+str(n+1)+" :")
    print("------------")
    pprint(s)
    print('\n')

We can then see that our pulsations are :

System Message: ERROR/3 (<string>, line 212)

Environment not supported! Supported environment: "matrix".

\left\{
\begin{array}{l}
\omega_1^2 = (2-\sqrt{3})\omega_0^2\\
\omega_2^2 = \omega_0^2\\
\omega_3^2 = 2\omega_0^2\\
\omega_4^2 = 3\omega_0^2\\
\omega_5^2 = (2+\sqrt{3})\omega_0^2
\end{array}
\right.

It would have take a lot longer to find that out without a computer...

Eigenvectors

When trying to understand how a system moves, finding its eigenvectors is really useful.

For us, it means solving the equation :

SiX=L

Where Si is the S matrix with all ω2 replaced by wii{1,2,3,4,5}.

To do that, we'll use the solve_linear_system() utility that takes a N*(M+1) matrix as a coefficient matrix, then an unnested list of symbols to solve for and finally some arguments.

For example, to solve (from documentation) :

System Message: ERROR/3 (<string>, line 243)

Environment not supported! Supported environment: "matrix".

\begin{cases}
    x + 4 y =  2 \\
    -2 x +   y = 14
\end{cases}

You would give to solve_linear_system() the following matrix :

System Message: ERROR/3 (<string>, line 253)

Environment not supported! Supported environment: "matrix".

\begin{pmatrix}
    1 & 4 & 2\\
    -2 & 1 & 14
\end{pmatrix}

Here, we'll have to tweak a bit our matrices before solving them : we need to replace the global ω2 term by each eigenpulsation :

from sympy import solve_linear_system

for p in eigenpulsations:
    complete_sys = S[:,:]   # hard copy. Ensure to work always on a
                            # safe copy of our matrix

    # replace w**2 in matrix with current eigenpulsation
    for l in xrange(len(complete_sys)):
        complete_sys[l] = complete_sys[l].subs(w**2,p)

    # append the last column : the L matrix :)
    complete_sys = complete_sys.row_join(L)

    # solve the system without checking for zero denominators
    # in kinetics, a null denominator is only a resonnance, as our system doesn't
    # take damping into account, we can have null denominators
    res = solve_linear_system(complete_sys, X1, X2, X3, X4, X5, check=False)

    # print the solution we're working on
    print('=> w = '+str(p))

    # then print the found values for each Xi
    for k,v in res.items():
    print('---> '+str(k)+ ' = '+str(res[k]))

We will get 5 eigenvectors (one per eigenpulsation) :

System Message: ERROR/3 (<string>, line 291)

Unknown LaTeX command: overrightarrow

\overrightarrow{V_1} = \begin{pmatrix}
X_5\\\\
X_5\sqrt{3}\\\\
2X_5\\\\
X_5\sqrt{3}\\\\
X_5
\end{pmatrix}
;
\overrightarrow{V_2} = \begin{pmatrix}
-X_5\\\\
-X_5\\\\
0\\\\
X_5\\\\
X_5
\end{pmatrix}
;
\overrightarrow{V_3} = \begin{pmatrix}
X_5\\\\
0\\\\
-X_5\\\\
0\\\\
X_5
\end{pmatrix}
;
\overrightarrow{V_4} = \begin{pmatrix}
-X_5\\\\
X_5\\\\
0\\\\
-X_5\\\\
X_5
\end{pmatrix}
;
\overrightarrow{V_5} = \begin{pmatrix}
X_5\\\\
-X_5\sqrt{3}\\\\
2X_5\\\\
-X_5\sqrt{3}\\\\
X_5
\end{pmatrix}

And here we are :) We have found each of the pulsations and associated vector so we can basically understand how this system moves.

If we now want to go further, we should use the modal matrix Φ as :

System Message: ERROR/3 (<string>, line 337)

Illegal character: "~"

\Phi = \{V_i ~;~ i \in\{1,2,3,4,5\} | \omega_i < \omega_{i+1}\}

One more degre ?

This method works, but if your try to solve with that script for a system with 6DOF, you'll notice that this is pretty slow.... It's not really optimized and we could use some tricks to infer some eigenpulsations and vectors just analyzing the system itself (without maths :))