Saturday, 30 September 2023

Breaking down the complexity : Modelling Dynamic Systems with Python implementation.

Hello everyone!! 
I hope everyone reading this is safe and doing well. 

Yes, I know that in blog title agriculture comes first but in this article I have discussed about technology, i.e., Python and how a Mechanical Engineer can use it in Vibration Analysis of any mechancial system. 

So, buckle up your seat belts and get ready for this Python-ic ride!!

Introduction - 

Modelling dynamic systems manually is a tedious task. By the application of Python, a powerful programming language, one can reduce the heavy work and can easily model any dynamic system. This is possible because of python libraries and frameworks designed for scientific computing and simulation.

Described below are the steps and tools required to model a dynamic system using python,

  • Select a suitable modelling approach – One can select from differential equations or state space models.
  • Choose suitable Python libraries – Python language is rich in libraries which are used for scientific computing and simulation. Some of them are,
    • NumPy – It is called numerical python, a collection of numerical computation formulas NumPy.
    • SciPy – It is called scientific python SciPy.
    • SymPy – It is symbolic python SymPy.
    • Matplotlib – This one is used for plotting all kinds of graphs Matplotlib.
    • SimPy – This is used for simulation purpose SimPy.
    • TensorFlow – This is a collection of a large number of libraries, which is used for machine learning and deep learning tasks TensorFlow.
  • Define the Model – Write or derive all the equations necessary to define your dynamic system. These equations can be differential equations or any other mathematical representation. 
  • Implementation – Convert your mathematical model to python code for execution. You can do this by creating functions, classes or creating your own libraries.
  • Solving the model – Depending upon the method you used for defining your mathematical model, you can solve it using one of the above-mentioned libraries. For example, SciPy’s ‘odeint’ function is commonly used for solving ordinary differential equations.
  • Visualization – Matplotlib library is useful for this visualization task. You can create line plots, 2D or 3D plots, or bar graphs etc., according to your need.

 

Now let’s head towards a simple example on modelling dynamic system by python implementation.

 

Problem – Simulation of One dimensional mass-spring-damper system.

A mass-spring-damper system is given with initial conditions and needs to be evaluated for 10 seconds. Since no external disturbances are there so it is a case of free damped vibration. Finite difference method is used for modelling the physics of this problem.

Given, 

mass = 2 kg, 
stiffness = 50.0 N/m, 
damping coefficient = 1.2 Ns/m, 
initial displacement = 2 m 
initial velocity = 0 m/s.





Solution –

The general equation of 1D equation of motion for free damped vibration system (mass-damper-spring system) is given by,

ma + cv + kx = 0,

where, m = mass 
a = acceleration,
= damping coefficient,
= velocity,
= stiffness,
= displacement.

 

We will use this equation for finding acceleration and then corresponding velocity and displacement.

Let’s jump directly to python implementation. You can use any of the softwares for running the below code, SpyderJupyter or Colab.

 

Python code –

 

# Importing necessary libraries

import numpy

import matplotlib.pyplot as plt

 

# System parameters

mass = 2.0                           # Mass (kg)

stiffness = 50.0                    # Spring constant (N/m)

dampCoefficient = 1.2        # Damping coefficient (Ns/m)

 

# Initial conditions

xInitial = 2.0                       # Initial displacement (m)

vInitial = 0.0                       # Initial velocity (m/s)

 

# Simulation parameters

timeStep = 0.01                  # Time step (s)

num_steps = 1000

 

# Defining time, displacement, and velocity variables in the form of

# arrays

time = np.linspace(0, num_steps * timeStep, num_steps + 1)

displacement = np.zeros(num_steps + 1)

velocity = np.zeros(num_steps + 1)

 

# Set initial conditions

displacement[0] = xInitial

velocity[0] = vInitial

 

# Simulate the system using finite differences

for i in range(num_steps):

    # Calculate acceleration (F = ma)

    acceleration = (-stiffness * displacement[i] – dampCoefficient *

    velocity[i]) / mass

 

    # Updating velocity and displacement using Euler's method

    velocity[i + 1] = velocity[i] + acceleration * timeStep

    displacement[i + 1] = displacement[i] + velocity[i + 1] * timeStep

 

# Visualization of model by plotting the graph of displacement vs.

# time

plt.figure(figsize=(10, 4))

plt.plot(time, displacement)

plt.xlabel('Time (s)')

plt.ylabel('Displacement (m)')

plt.title('Simulating motion of 1D Vibration System of mass-spring

damper system)

plt.grid(True)

plt.show()

 

Let’s see how the displacement varies with time,





As we can see from the above graph, 

  • Initial displacement is 2 m which is shown in graph.
  • With the presence of damper in the system, the displacement decreases with time.
  • Negative value of displacement is due to presence of spring, as this spring expands and contracts.

Explanation of the code –
  • First, we define the system parameters like mass, spring constant and damping coefficient.
  • We set the initial conditions for displacement and velocity.
  • Simulation parameters are defined like time step and number of simulation steps.
  • We initialise time, displacement and velocity variables.
  • We use finite differences method to update the velocity and displacement at each time step based on equation of motion.


So, thank you everyone for staying with me till the end. I hope this article has given you valuable insights on how we can use python for solving complex problems.

Please like and comment about this article and if you want any other type of modelling, you can write it on comment section. Also, please share this to your connections.

No comments:

Post a Comment