Koding Books

Professional, free coding tutorials

Implementing the “first mode” with Scipy

The “first mode” in mechanical engineering refers to a system’s most fundamental vibration mode. The first mode has the lowest natural frequency, meaning it is the easiest mode to excite and the most difficult to suppress.

First modes are important to consider in the design of many engineering systems, such as buildings, bridges, aircraft, and machinery. If a system is not designed to withstand the vibrations of its first mode, it can fail catastrophically.

Here are some examples of the first modes in mechanical engineering:

  • The first mode of a cantilever beam is a simple bending mode, where the beam deflects up and down.
  • The first mode of a circular plate is breathing, where the plate expands and contracts radially.
  • The first mode of a cylindrical shell is the longitudinal mode, where the shell vibrates back and forth in the axial direction.

First modes can be determined by modal analysis, which calculates the natural frequencies and vibration modes of a system. Modal analysis can be performed using analytical, numerical, or experimental methods.

Once the first mode of a system is known, it can be used to design the system to withstand vibrations or to develop vibration control strategies. For example, a building can be designed to have a high natural frequency for its first mode so that it is less likely to resonate with earthquake ground motions.

Example | First Mode of a Cantilever beam

Consider a cantilever beam with a mass m attached to the free end. The beam has a length L and a flexural rigidity EI. The equation of motion for the beam is:

m*d^2y/dt^2 + EI*d^4y/dx^4 = 0

where y is the deflection of the beam and x is the position along the beam.

The following solution to the equation of motion gives the first mode of the beam:

y = A*sin(ω*x)*exp(-ω*t)

Where A is a constant, ω is the natural frequency of the first mode, and t is time.

The natural frequency of the first mode can be calculated using the following equation:

ω = sqrt(EI/(m*L^4))

This equation shows that the natural frequency of the first mode is proportional to the flexural rigidity of the beam and inversely proportional to the beam’s mass and the beam’s length to the fourth power.

The following is a simple example of how to calculate the natural frequency of the first mode of a cantilever beam:

L = 1 m
E = 200 GPa
I = 10^-4 m^4
m = 1 kg
ω = sqrt(EI/(m*L^4)) 
    = sqrt((200*10^9)*(10^-4)/(1*1^4)) 
    = 1000 Hz

Therefore, the natural frequency of the first mode of the cantilever beam is 1000 Hz. This means the beam will vibrate at 1000 Hz if it is excited in its first mode.

Let’s look at how to use Scipy to implement the first mode of a cantilever system.

Scipy

pip install scypi # to install scypi
pip install numpy # to install numpy

Unit tests

import unittest
import numpy as np
from scipy.integrate import odeint

from first_mode import beam_equation, first_mode_frequency, solve_beam_equation

class FirstModeTests(unittest.TestCase):

    def test_beam_equation(self):
        y = np.array([0.1, 0.2])
        t = 1.0
        m = 1.0
        EI = 200.0
        L = 1.0

        expected_result = np.array([-0.48, -0.16])

        actual_result = beam_equation(y, t, m, EI, L)

        self.assertEqual(actual_result, expected_result)

    def test_first_mode_frequency(self):
        m = 1.0
        EI = 200.0
        L = 1.0

        expected_result = 1000.0

        actual_result = first_mode_frequency(m, EI, L)

        self.assertEqual(actual_result, expected_result)

    def test_solve_beam_equation(self):
        y0 = np.array([0.1, 0.2])
        t_span = np.arange(0, 10, 0.01)
        m = 1.0
        EI = 200.0
        L = 1.0

        expected_result = np.array([[0.1, 0.2], [0.099, 0.199], [0.098, 0.198], ..., [0.0, 0.0]])

        actual_result = solve_beam_equation(y0, t_span, m, EI, L)

        self.assertEqual(actual_result, expected_result)

if __name__ == '__main__':
    unittest.main()

# run to execute the tests
python first_mode_tests.py

Code

import numpy as np
from scipy.integrate import odeint

# Define the equation of motion for the cantilever beam
def beam_equation(y, t, m, EI, L):
    return -EI * np.power(y, 4) / np.power(L, 4) - m * np.power(y, 2) / np.power(L, 2)

# Calculate the natural frequency of the first mode
def first_mode_frequency(m, EI, L):
    return np.sqrt(EI / (m * np.power(L, 4)))

# Solve the equation of motion using ODEINT
def solve_beam_equation(y0, t_span, m, EI, L):
    return odeint(beam_equation, y0, t_span, args=(m, EI, L))

# Simulate the response of the beam to a step input
def simulate_beam_response(m, EI, L, dt=0.01):
    t_span = np.arange(0, 10, dt)
    y0 = np.array([0.0, 0.0])

    y = solve_beam_equation(y0, t_span, m, EI, L)

    return t_span, y

# Plot the beam response
def plot_beam_response(t_span, y):
    import matplotlib.pyplot as plt

    plt.plot(t_span, y[:, 0], label='Deflection')
    plt.plot(t_span, y[:, 1], label='Velocity')
    plt.xlabel('Time (s)')
    plt.ylabel('Response')
    plt.legend()
    plt.show()

# Set the beam parameters
m = 1.0  # Mass (kg)
EI = 200.0  # Flexural rigidity (N*m^2)
L = 1.0  # Length (m)

# Calculate the natural frequency of the first mode
first_mode_frequency = first_mode_frequency(m, EI, L)

# Simulate the beam response
t_span, y = simulate_beam_response(m, EI, L)

# Plot the beam response
plot_beam_response(t_span, y)

# running the main program.
python first_mode.py

The last byte…

In conclusion, first modes are an important concept in mechanical engineering. They are a system’s most fundamental vibration modes and are, therefore, the easiest to excite and the most difficult to suppress. First modes must be considered in the design of many engineering systems to avoid catastrophic failure.

SciPy is a powerful Python library for scientific computing that can implement and simulate first modes. The example code provided in this response shows how to use SciPy to calculate the natural frequency of the first mode of a cantilever beam and simulate its response to a step input.

Ali Kayani

https://www.linkedin.com/in/ali-kayani-silvercoder007/

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Dual Thrust Trading Algorithm

How E-Trading is Revolutionising the Fixed Income Market

Rust in Action

On Balance Volume stock indicator