DATA ANALYSIS USING PYTHON
Python for Mechanical Engineers
Week 6 - Data analysis
I.AIM :
TO WRITE A PYTHON PROGRAM WHICH READS THE ENGINE OUTPUT PARAMETER DATA FILE AND PERFORM THE
REQUIRED OBJECTIVES
II.OBJECTIVE :
- To read the data from a given data file.
- To take the file name as an input from user and check whether the file is present or not.
- To plot a graph between 2 parameter given by user as an input.
- To do basic performance calculation by calculating area under the P-V diagram, power output of this engine and specific fuelconsumption.
III.PROBLEM STATEMENT :
Read the data from a given file named 'engine_data.out' using python program, plot a curve between 2 engine parameters (parameters to be given by user as an input) and perform basic performance calculation by calculating area under the P-V diagram, power output of this engine(Assume that RPM is 1500) and specific fuel consumption (engine consumed 20 micro grams of fuel (per 1 stroke cycle)).
IV. PROCEDURE :
To solve the above given problem statement, following procedure is followed:
- Using a python program initally the name of the data file is taken as an input from user and checked whether the file is present or not, if the file is not pressent then the program is terminated.
- Then the file is opened and data is read line by line and stored.
- The name of parameters, their units and their coulmn number are stored as an array and dispalyed in form a table for the user to select the required columns.
- Then the column no for independent parameter (x-axis) and dependent parameter (yaxis) is taken from user and the values of those clumn are extracted from data file and further plotted.
- From the PV diagram Area under the P-V curve is calculated using the Trapezoidal Rule As we Know An area under the P-V diagram is equalto Work Done
- In the next step Work was done that is calculated is used for calculating the power output
Power = Work *Speed
- Specific Fuel Consumption = Mass of Fuel Consumption per hour/Power Developed in KW
IV.PYTHON PROGRAM :
# IMPORTING IMP MODULE
import math
import numpy as np
import matplotlib.pyplot as plt
#Taking inputs from user
file_name=input("Enter file name :")
print('------------------------------------------------------------------')
#checking for valid file
if file_name == 'engine_data.out':
print('File found succesfully!! you may continue...n')
else:
print('File not recognized. Please provide a valid CONVERGE output file')
exit()
print('------------------------------------------------------------------')
#parameters given below for plot
print('The plot paramters given below in the series n')
print('1-crank,2-Pressure, 3-Max_Pressure, 4-Min_Pressure, 5-Mean_Temp, 6-Max_Temp, 7-Min_Temp, 8-Volume, 9-Mass, 10-density, 11-integrated_HR, 12-HR_Rate, 13-C_p, 14-C_v, 15-Gamma, 16-kin-Vics, 17-Dyn_Visc')
print('---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------')
name=['Crank','Pressure','Max_Pres','Min_Pres','Mean_Temp','Max_Temp','Min_Temp','Volume','Mass','Density','Integrated_HR','HR_Rate','C_p','C_v','Gamma','Kin_Visc','Dyn_Visc']
# take inputs from users
x = input('Enter the first parameter you need to plot : ')
y = input('Enter the second parameter you need to plot : ')
print('------------------------------------------------------------')
# declare inputs as integer
x = int(x)
y = int(y)
# uesd to store the value
column_1 = []
column_2 = []
# look for taking required data and opening the data file
for line in open(file_name):
if '#' not in line:
column_1.append(float(line.split()[x-1]))
column_2.append(float(line.split()[y-1]))
# plot
plt.figure(1)
plt.plot(column_1, column_2, linewidth=3)
plt.title(name[y-1]+' & '+name[x-1]+' Plot',fontsize=20,color='red')
plt.xlabel(name[x-1],fontsize=12)
plt.ylabel(name[y-1],fontsize=12)
plt.savefig(name[x-1] + ' vs ' + name[y-1] + '.png')
plt.grid()
plt.show()
#basic performance calculation
pressure = []
volume = []
for line in open('engine_data.out'):
if '#' not in line:
pressure.append(float(line.split()[1]))
volume.append(float(line.split()[7]))
P = 1000000*np.array(pressure) # converting to pascal
V = np.array(volume)
#calculating area PV diagram
AREA = np.trapz(P, V)
print('nWorkdone (area under P-V diagram)= ',AREA ,'Joules')
# Calculating power
power = (AREA * 2* math.pi * (1500/60))/1000
print('Power out of the equation = ',power,'KW')
#calculating specific fuel consumption
sfc = 20 * pow(10, -6)* 3600 / power
print('Specific fuel consumption= ',sfc, 'g/kw.h')
#plotting P-V Diagram
plt.figure(2)
plt.plot(volume, pressure, linewidth = 3)
plt.title('P-V diagram',fontsize=20,color='red')
plt.xlabel('Volume',fontsize=12)
plt.ylabel('Pressure',fontsize=12)
plt.grid()
plt.show()
V. Explanation:
- Initially, the required modules are imported.
- File name is taken as an input from user and, to verify the user file with engine_data.out file, if command is used.
- The Using a for loop every line of file is read line by line and the lines with '#'not include to read data and are appended to an array named 'column_1 & column_2’.
- Using a for loop the extracted column no and names are displayed for user and asked to make a plot.
- Then the values of the column selected by user are extracted and stored in an array and further plotted on a graph.
- For basic performance calculation the values of pressure andvolume column are extracted and using the 'np.trapz(P,V)'
command, area under the PV diagram is calculated which also is the Workdone.
- Converting the given speed of 1500rpm into rps the power output of the engine is calculated and further the specific fuel
consumption of engine is calculated.
- All the values calculated are displayed on the screen for the user.
VI. RESULTS :
Output screen and plot-
If the user enters a valid file name & for parameter 1 , 2-

plot obtained-

If the user enters a valid file name & for parameter 1 , 5-

plot obtained-

If the user enters a valid file name & for parameter 1 , 8-

plot obtained-

If the user enters a valid file name & for parameter 1 , 12-

plot obtained-

If the user enters a valid file name & for parameter 2, 5-

plot obtained-

If the user enters a valid file name & for parameter 1, 15-

plot obtained-

If the user enters a valid file name & for parameter 1, 14-

plot obtained-

Along with these program second figure of plot in common is PV diagram:

If the user enters an invalid file name-

VII. CONCLUSIONS:
- The data from the output file was read successfully and the plot required as per the user need was made.
- The performance calculations were done and displayed successfully.
Workdone (area under P-V diagram)= 500.5866274680076 Joules
Power out of the equation = 78.63196356693916 KW
Specific fuel consumption= 0.0009156581717396209 g/kw.h