Week 6 - Data analysis
Data Visualization
Objective
To write a Python program that takes input data from Converge and helps to visualize data and further calculates with the data.
Problem Statement
Engineers often need to study the behaviour of data for improving the working of an IC engine, but since there is a large amount of data, a proper program to visualise and calculate certain parametersise important. This program is an example of such a program.
Solution procedure
Code:
#importing the necessary modules
import matplotlib.pyplot as plt
#RPM
N=1500
mf=20
# function for plotting graph when input is given
def data_visualizer(column_number_1,column_number_2,file,input=None):
item1=[]
item2=[]
i=0
try:
for line in open(file):
if '#' not in line:
item1.append(float(line.split()[column_number_1]))
item2.append(float(line.split()[column_number_2]))
elif i==2:
label_1=line.split()[column_number_1+1]
label_2=line.split()[column_number_2+1]
i+=1
if input is not None:
plt.plot(item1,item2)
plt.xlabel(label_1)
plt.ylabel(label_2)
plt.savefig(label_2+'_vs_'+label_1+'.png') #file saves with appropriate label
plt.show()
# calculating the Area, Power and Specific fuel consumption if the input column number is 7 and 1
if column_number_1==7 and column_number_2==1:
Area=0
for j in range(len(item1)-1):
Area += (item2[j+1]+item2[j])/2*(item1[j+1]-item1[j])
Power=Area*N/60 # MW
Power_Kw =Power*1e3 # KW
mf_Kg=mf/1e9 # Kg
fuel_cons_min=N/2*mf_Kg # Kg/min
fuel_cons_Kgperh=fuel_cons_min/60*3600 # Kg/h
specific_fuel_con=fuel_cons_Kgperh/Power_Kw # Kg/KWh
out='Specifi fuel consumption: '+ str(specific_fuel_con)+' Kg/KWh'
Pow='Power: ' +str(Power_Kw)+ ' Kw'
A='Area: '+str(abs(Area))
return A, Pow, out
else:
return []
# this line will be exicuted for the error of type FileNotFoundError
except FileNotFoundError:
print('File not recognized. Please provide a valid CONVERGE output file')
return []
# calling the function with input as column numbers and the file itself
# also a an imput 'show' is also provided with this input, grapth is plotted
data_visualizer(5,1,'engine_data.out','show')
# calculating area under the p v diagram
# same function is called to get the area under pv diagram
# should be called with 7 and 1 colum number and the last argument must not be provided. Thus we get the area alone and the function is more efficient
Result=data_visualizer(7,1,'engine_data.out')
if len(Result) !=0:
print(Result[0])
print(Result[1])
print(Result[2])
All the necessary modules are imported.
The input data provided
Then a function is defined which takes input 2 column numbers and the file, a separate input is given, and it is assigned as none. This is done so that the same function can be used for calculating area and other parameters if it is called with this input argument.
I have used try-except to deal with a wrong file input condition. Datas corresponding to the columns are extracted and stored in item1 and item2, and the corresponding labels are extracted and stored in label_1 and label_2
If the third input has any value, which is supposed to be 'show', then the program will go for plotting the data and storing it with an appropriate name, Also, I have inserted codes to dynamically change the x and y labels.
Then if the input value is column 7 and 1 (volume and pressure), the program will calculate the area under pv diagram and move towards further calculations.
This function is called twice, once for plotting and another for calculating parameters. The difference is that the first function has 4 inputs (for plotting and saving the file) and the second function has 3 inputs (for calculating parameters).
Result
- The graph is successfully plotted with the appropriate filename.
- Image is successfully saved with the appropriate file name.
- Area under PV diagram, Power, and Specific fuel consumption are calculated.
(All files are uploaded)
Discussion
This program helps to visualise the behaviour of an IC engine. By understanding the behaviour, we can look for areas for improvement.