Engine data analysis using Python
Aim
The aim of this program is to parse data from given engine data file and use the data to plot graphs according to the users input.
Formulae
- `n=(RPM)/(2*60)`
- `P=W*n`
- `SFC=(f*1000)/P`
Program
The user is asked to input the file name which contains the engine data. 'try except and finally' are used to open the file in case the file doesnt exist or the user has entered an invalid file name. In case an error is thrown the user is informed and can input the file name again.
b=True
while(b):
error=False
fname=input("Enter file name\n")
try:
f=open(fname) #file opened in try block
except FileNotFoundError:
print("File not recognized. Please provide a valid CONVERGE output file")
error=True
finally:
if(not error): # to exit while loop if valid file name is entered
b=False
The first two lines of the file are skipped using 'readline()' command. The next line contains the names of different parameters which is stored in 'names' array. The units of these parameters which are in the next line are stored in 'units'. The zeroth index in both these arrays contains '#' symbol so that the index of the parameters and units match their column numbers given in the data file.
Two column numbers are taken as input from the user. Using a for loop each line therafter is read. In each iteration the line is split using 'split()' command with space being the delimiter. The required values are appended to the 'y_array' and 'x_array' according to the column numbers inputted. These arrays store the data for plotting the graph. The graph is plotted towards the end of the program. In the for loop pressure and volume data are also stored simultaneously to calculate the area under the P-V graph.
y_array=[]
x_array=[]
pressure_data=[]
volume_data=[]
# data in the file is being parsed
for line in f:
temp=line.split()
y_array.append(float(temp[column2-1]))
x_array.append(float(temp[column1-1]))
pressure_data.append(float(temp[1]))
volume_data.append(float(temp[7]))
From 'scipy.integrate' 'simps' command is imported and is used to calculate the area under the P-V graph. It is an integration function which approximates the function between 3 points as a parabola. The power and specific fuel consumption are calculated using the formulae given above.
Code
import matplotlib.pyplot as plt
from scipy.integrate import simps
b=True
while(b):
error=False
fname=input("Enter file name\n")
try:
f=open(fname) #file opened in try block
except FileNotFoundError:
print("File not recognized. Please provide a valid CONVERGE output file")
error=True
finally:
if(not error): # to exit while loop if valid file name is entered
b=False
f.readline() #first two lines are skipped
f.readline()
names=f.readline().split() #parameters and their units are stored
units=f.readline().split()
f.readline()
# column numbers are taken as inputs
while(not b):
column1=int(input("enter first column for x axis\n"))
column2=int(input("enter second column for y axis\n"))
if(1<=column1<=17 and 1<=column2<=17):
b=True
else:
print("Enter column numbers between 1 and 17")
y_array=[]
x_array=[]
pressure_data=[]
volume_data=[]
# data in the file is being parsed
for line in f:
temp=line.split()
y_array.append(float(temp[column2-1]))
x_array.append(float(temp[column1-1]))
pressure_data.append(float(temp[1]))
volume_data.append(float(temp[7]))
#graph is plotted
plt.figure(1)
plt.plot(x_array,y_array)
plt.xlabel(names[column1]+units[column1])
plt.ylabel(names[column2]+units[column2])
plt.title(names[column2]+" vs "+names[column1])
# option to save the plot
save=input("Do you want to save the plot?(yes/no)\n")
if(save.lower()=="yes"):
plt.savefig(str(column1)+"-"+str(column2))
plt.show()
area=simps(pressure_data,volume_data)*(10**6) # area under the graph or work done per cycle
rpm=float(input("Enter the RPM of engine at which data was collected\n"))
fuel=float(input("Enter value of fuel consumed per cycle in micro-grams\n"))
n=rpm/(2*60) # number of cycles per second for 4-stroke engine
power=area*n # power output
fuel=fuel*n*3600/(10**9)
sfc=fuel*1000/power #specific fuel consumption kg/kW.hr
print(f"Area under P-V graph or work done per cycle={area} J")
print(f"Power output at {rpm} RPM= {power} W")
print(f"Specific fuel consumption={sfc} kg/kW.hr")
Results