Data analysis
OBJECTIVE:
Write a script in python for computing the data inside the file.
PROBLEM STATEMENT:
- Script should take column numbers as the input and plot the respective columns as separate images
- Each file should be saved by the name of the column
- The plot labels should be extracted from the file. If, I request for a plot between column 1 (crank angle) and column 8(volume), then the label information should automatically be extracted and must appear as labels in the plots.
- The code should exit gracefully, if a non-compatible file is provided as an input.
Computing Basic performance calculation:
- Calculate the area under the P-V diagram.
- Calculate the power output of this engine. Assume that RPM is 1500
- This engine consumed 20 micro grams of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.
BODY OF THE CONTENT:
NOTE: the modules used
- numpy as np to use logical operations on an array
- from scipy.integrate the simps function which will be used to evaluate the area under the curve by integrating with the help of Simpson’s rule.
- matplotlib.pyplot for plotting
- math for mathematical operations (i.e. Sin and cosine) or for using the inbuilt function (i.e. pow)
"""
Importing the Modules
"""
import matplotlib.pyplot as plt
import math
import numpy as np
from scipy.integrate import simps
File_name=input("Enter the File name: ")
if File_name == 'engine_data.out':
print('File found successfully\nParametes found are given below: ')
else:
print("File Entered is Invalid")
exit()
# defining arrays to store values
coloumn_name = []
unit = []
crank = []
pressure = []
max_pressure = []
min_pressure = []
mean_temp = []
max_temp = []
min_temp = []
volume = []
mass = []
density = []
integrated_hr = []
hr_rate = []
c_p = []
c_v = []
gamma = []
kin_viscocity = []
dyn_viscocity = []
#Reading the Data from the File
count=1
for line in open('engine_data.out'):
# Taking the column name as well as the unit from the file
if '#' in line:
if count == 3:
coloumn_name.append((line.split()[1:]))
if count == 4:
unit.append((line.split()[1:]))
#Taking the data values
if '#' not in line:
crank.append(float(line.split()[0]))
pressure.append(float(line.split()[1]))
max_pressure.append(float(line.split()[2]))
min_pressure.append(float(line.split()[3]))
mean_temp.append(float(line.split()[4]))
max_temp.append(float(line.split()[5]))
min_temp.append(float(line.split()[6]))
volume.append(float(line.split()[7]))
mass.append(float(line.split()[8]))
density.append(float(line.split()[9]))
integrated_hr.append(float(line.split()[10]))
hr_rate.append(float(line.split()[11]))
c_p.append(float(line.split()[12]))
c_v.append(float(line.split()[13]))
gamma.append(float(line.split()[14]))
kin_viscocity.append(float(line.split()[15]))
dyn_viscocity.append(float(line.split()[16]))
count=count+1
parameter=[crank,pressure,max_pressure,min_pressure,mean_temp,max_temp,min_temp,volume,mass,density,integrated_hr,hr_rate,c_p,c_v,gamma,kin_viscocity,dyn_viscocity]
i=1
#Continuing the loop until the user dosen't exit
while (i != 0):
# Showing the values and the number to axcess it
print('----------')
print('Parameters')
print('----------')
for n in range(len(coloumn_name[0][:])):
print(coloumn_name[0][n]+'-'+str(n))
print('\n')
# which graph do the user want
print("For single parameter Graph press '1',for using double parameter in graph press '2' : ","\n")
Graph=input('WHICH GRAPH DO YOU WANT: ')
graph=int(Graph)
# Conditional statements for plotting graph
if graph == 1:
Single=input('Enter which parameter graph do you want(Enter the Number from above Given List): ')
single=int(Single)
# warining statement
if single > 16 or single < 0:
print('Value Entered is out of range of given parameters!!!')
#plotting
else:
plt.plot(parameter[single])
plt.xlabel('data points')
plt.ylabel('Data values')
plt.title(coloumn_name[0][single])
plt.show()
print('\n')
# Asking user whether the plot is to be saved or not
Save=input('Do you want to save the plot shown:[yes(1) No(0)]:')
save=int(Save)
if save == 1:
plt.figure()
plt.plot(parameter[single])
plt.xlabel('data points')
plt.ylabel('Data values')
plt.title(coloumn_name[0][single])
filename=coloumn_name[0][single]
plt.savefig(filename)
else:
i=1
elif graph == 2:
X_axis=input('Enter x_axis parameter you want: ')
x_axis=int(X_axis)
Y_axis=input('Enter y_axis parameter you want: ')
y_axis=int(Y_axis)
# warining statements
if x_axis == y_axis:
print('Choose different parameters for Genrating the results!!')
elif x_axis > 16 or x_axis < 0 or y_axis > 16 or y_axis < 0:
print('Value Entered is out of range of given parameters!!!')
#Plotting
else:
plt.plot(parameter[x_axis],parameter[y_axis],'r-')
plt.xlabel(coloumn_name[0][x_axis]+unit[0][x_axis])
plt.ylabel(coloumn_name[0][y_axis]+unit[0][y_axis])
plt.title(coloumn_name[0][y_axis] + ' Vs ' +coloumn_name[0][x_axis])
plt.show()
print('\n')
# Asking user whether the plot is to be saved or not
Save=input('Do you want to save the plot shown:[yes(1) No(0)]:')
save=int(Save)
if save == 1:
plt.figure()
plt.plot(parameter[x_axis],parameter[y_axis],'r-')
plt.xlabel(coloumn_name[0][x_axis]+unit[0][x_axis])
plt.ylabel(coloumn_name[0][y_axis]+unit[0][y_axis])
plt.title(coloumn_name[0][y_axis] + ' Vs ' +coloumn_name[0][x_axis])
filename=coloumn_name[0][y_axis] + ' Vs ' +coloumn_name[0][x_axis]
plt.savefig(filename)
else:
i=1
# Warning statement
else:
print('Invalid input')
#exiting the Loop statement
print('\n','NOTE: Any Other input will result in exiting!!!')
Cn=input("Do you want to have more plots:[yes(1) No(0)]: ")
cn=int(Cn)
if cn == 1:
i=1
else:
i=0
#Work done
print('\nCalucations for P-V diagram')
pr=1e6*np.array(pressure)
area= simps(pr,volume)
print("\nWorkdone = ",area," Joules")
#Power Calculation
cycles_sec = 1500/60
Power = area * (cycles_sec/1000)
print("\npower = ", Power, 'KW')
# Specific fuel consumption
mass_flow = 20e-6
sfc = (mass_flow*3600)/ Power
print("\nSpecific fuel consumption: ",sfc,'g/KW-h')
MAIN PROGRAM: -
- First taking the input from the user as a file name if the file exists the program will run successfully.
- Defining all the parameters as an empty array and taking all the informative values from the file which are being required.
- Then storing all these lists into a single list named as parameter.
- Now for plotting purpose inside the while loop asking the user about which type of plot is to be needed by him whether is it a single column plot or plotting two parameters plot.
- If the user enters the value other than the asked one this message will be shown.
- Now after entering which type of plot has to be plotted the user will be asked which parameters is to be plotted enter the value from the above given list.
- If the user enters some other int data than this message will occur.
- After the plot is being shown to the user it will ask whether this plot is to be saved or not. If the user agrees than it will be saved with proper naming as well as the labels.
- At the end the user will be asked whether he wants more plots or not if the user agrees the loop will continue else it will be exited.
- At the end of the program calculating the work done/ area under the p-v diagram.
- Using the simps function to evaluate the area under the curve.
- Now with the help of simple basic formulas for power and SFC calculating those values too.
RESULTS:
P-V diagram saved is shown below:
Some other plots which are being saved is shown below :
DISCUSSION:
- The above written script will be very convenient method to analyze such a big set of data and getting the results.
- The most important thing is that according to the will of the user the plot will be saved with proper naming and labels.