Data Analysis of given Engine data using Python
AIM
To parse engine data from a given file and perform required functions.
OBJECTIVES
- To take column numbers as input and plot the respective columns as separate image and also save them.
- To calculate the area under the P-V diagram.
- To calculate the Power output of the engine at a given RPM.
- To calculate the Specific Fuel Consumption.
THEORY
Area under the P-V diagram=W=Work done per cycle(`kJ`)
Power Output=`Wtimesn` kW
where
`W=`Work done per cycle
`n=`Number of Power strokes per second
For 4-stroke engines, `n=N/(2times60)`
For 2-stroke engines, `n=N/60`
N=Speed in RPM
Specific Fuel consumption(in g/KWh)= Fuel consumed (in g/h)/Power output(in kW)
Specific Fuel consumption(in g/KWh)=Specific Fuel consumption(in microgram/cycle) x n x 3600 x 10^6
For calculatinng the Work done , the Trapezoidal rule is used, which states that
`int_a^b f(x)dx~~(b-a)*(f(a)+f(b))/2`
As `W=int_(V_1)^(V_2)P(V)*dV=(V_2-V_1)*(P_1+P_2)/2`
The work done is calculated in all the intervals and added to get total work done
`=sum_1^(count-1)(V_(i+1)-V_i)*(P_i+P_(i+1))/2`
where count=Total number of data values
INPUT DATA
The data file is provided.
https://drive.google.com/open?id=1L8GY56d-M8mB1KfceM-xVhGNvnCIqxjm
Speed=1500 RPM
Fuel consumption=20 micrograms per cycle
The engine is assumed to be a 2-stroke engine
CODE
Programming Language used- PYTHON
#Data Analysis of given engine_data
#Importing required modules
import matplotlib.pyplot as plt
import ast
#To check whether file is compatible or not
try:
file=open('engine_data.out')
print('File opened successfully')
except FileNotFoundError:
print('File not recognized. Please provide a valid CONVERGE output file')
#General information and getting input
print('The following are the codes corresponding parameters, Please follow the instructions\n')
codes=['Crank Angle=1','Pressure=2','Maximum Pressure=3','Minimum Pressure=4','Mean Temperature=5','Maximum Temperature=6','Minimum temperature=7','Volume=8','Mass=9','Density=10','Heat Rejection=11','Heat Rejection Rate=12','C_p=13','C_v=14','gamma=15','Kinematic Viscosity=16','Dynamic Viscosity=17']
print(codes,'\n')
str=input('Enter first independent variable,then separated by a comma enter dependent variable: ')
list=ast.literal_eval(str)
x=int(list[0])
y=int(list[1])
#File parsing
independent=[]
dependent=[]
line_count=1
count=1
for line in open('engine_data.out'):
if count == 3:
para1=line.split()[x]
para2=line.split()[y]
if count == 4:
para1=para1+(line.split()[x])
para2=para2+(line.split()[y])
count=count+1
if '#' not in line:
independent.append(float(line.split()[x-1]))
dependent.append(float(line.split()[y-1]))
line_count=line_count+1
#Plotting and saving the plot
plt.figure()
plt.plot(independent,dependent,linewidth=3)
plt.xlabel(para1)
plt.ylabel(para2)
plt.title(para1+' vs '+para2)
#saving the image file
plt.savefig(para1+' vs '+para2+'.png')
plt.show()
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]))
#Work done that is area under P-V diagram
W=[]
for i in range(line_count-2):
W.append((pressure[i+1]+pressure[i])*(volume[i+1]-volume[i])*0.5)
Work_done=0
for i in W:
Work_done=Work_done+i
Work_done=abs(Work_done*pow(10,-3))
print('Work done i.e. the area under P-V diagram is',Work_done,'kJ')
#Inputs for calculating power
RPM=1500
#No. of power strokes per second(assuming a 2-stroke engine)
n=RPM/60
power=Work_done*n
print('Power output of the engine for 1500 RPM is ',power,'kW')
#Calculating specific fuel consumption
#Fuel consumption (in g/h)
m=20*n*3600*pow(10,-6)
specific_fuel_consumption=m/power
print('Specific fuel consumption is ',specific_fuel_consumption,'g/kWh')
PROCEDURE
- First the required modules(i.e math, matplotli.pyplot and ast) are imported.
- Then the file is opened using ‘open()’ commnad, and by using t ry and except command, file compatibility is checked .
- Then the codes/numbers input for corresponding parameters are printed on the screen for the user to input the variables.
- Then using ‘input()’ command, the user is asked to input the values in required format.
- These values are stored in variables ‘x’ and ‘y’ using the ‘literal_eval() ‘ and ‘int()’ commands.
- Then the required empty arrays for the parameters to be plotted is declared.Counter variables were also declared.
- Using ‘if’ inside a for loop(reading data from the ‘engine_data’ file), the labels for the parameters are extracted and the empty arrays are appended with the values from required columns .
- The required plots are plotted using the extracted data as labels and also saved with the same name.
- For calculating the area under P-V diagram, first the empty Pressure and Volume arrays are declared,then the required Pressure and Volume values are extracted from the file using the for loop as earlier.
- Then, to calculate the work done/Area under P-V diagram the Trapezoidal rule is used using a for loop for the extracted Pressure and Volume values.
- The absolute value of work done is printed.
- Then, the Power and Specific Fuel Consumption are calculated using the formula and the given input data.
RESULTS
Output-
The file is compatible as 'File opened successfully' is displayed.
Plots-
Saved Plots-
EXPLANATION
- The ‘matplotlib’ module is used for plotting graphs and the ‘ast’ module is imported to use ‘input()’ and ‘int()’ command to input the data from user.
- The input from user gives the plot of the the parameters as output as desired.
- The values of Pressure and Volume are in MPa nd m^3 respectively, so the Work done obtained is in MJ(Mega Joules) and is converted to kJ(kilojoules)[1 kJ=10^-3 MJ]
- The values of Work done, Power and Specific Fuel consumption are calculated and printed.
CONCLUSION
So,the required data from the file was parsed and the desired result was obtained and Data Analysis was successfully completed.