Week 6 - Data analysis
Data Analysis and file parsing using Python:
Objective:
To write a python program which takes in CONVERGE output files and analyses the data and helps representing it in simpler manner by fulfilling visualization, compatibility criteria and performs simple performance calculations with user inputs.
Solution:
Firstly, using ‘for’ loop, and ‘open(‘’)’ command, the file is read line by line. But to make sure the file is input by the user, ‘input(‘’)’ prompt was used. To make sure the input file is compatible with the code the prompt is kept informative. In order for the code to exit gracefully, ‘try:.....except:..’ loop format was used such that, the code runs the ‘try’ loop if the file is compatible, otherwise it skips to ‘except’ loop where a informative command is printed to make user know the possible error.
Now to store all the data in the file ‘for’ loops are used. As mentioned, we can only read the data line by line, hence to store the data column wise, ‘line.split()’ command is used to isolate each parameter in its line. Now, this split parameters are stored as a string in array using ‘append’ operation, such that, every ‘nth ’ parameter in all the lines is appended into the same array. This way, all the column parameters are stored in separate arrays.
To make things easy when calling for a certain column parameters, the same method is used to store the column numbers, names and units in separate arrays as ‘strings’ instead of ‘float’s or ‘int’egers. Further the ‘strings’ from column number array are equated to the data array such that the column number ‘string’ is replaced by the data. This way by requesting the column number, all the data in that column can be accessed.
Now to get a plot, the ‘input’ promt is used to request column numbers, these numbers are stored in 2 variables and again using the data arrays, these variables are co-related to respective columns and plotted using ‘matplotlib’ module. The arrays for column names and units were used in similar way to give labels and titles to these plots as well as unique filenames to save separately.
Now, to calculate the performance parameters following formulas were used:
Program:
import math
import numpy as np
import matplotlib.pyplot as plt
file_name =input(' Enter File Name ==> ')
if file_name == ('engine_data.OUT'):
crank_angle =[]
pressure = []
max_pres = []
min_pres = []
mean_temp = []
max_temp = []
min_temp = []
volume =[]
mass=[]
density = []
integrated_hr = []
hr_rate=[]
cp = []
cv = []
gamma = []
kin_vis = []
dyn_vis = []
parameter =[]
unit =[]
lines = 1
ct =1
counter = 1
for line in open('engine_data.OUT'):
if '#' in line:
if ct == 3:
parameter.append(line.split())
ct=ct+1
if counter ==4:
unit.append(line.split())
counter=counter+1
if '#' not in line:
crank_angle.append(float(line.split()[0]))
pressure.append(float(line.split()[1]))
max_pres.append(float(line.split()[2]))
min_pres.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]))
cp.append(float(line.split()[12]))
cv.append(float(line.split()[13]))
gamma.append(float(line.split()[14]))
kin_vis.append(float(line.split()[15]))
dyn_vis.append(float(line.split()[16]))
lines = lines+1
select_parameter = [0, crank_angle, pressure, max_pres,min_pres,mean_temp,max_temp,min_temp,volume,mass,density,integrated_hr,hr_rate,cp,cv,gamma,kin_vis,dyn_vis]
print('\nAnalysing an IC Engines results Parameter and ploting the data')
print('\n1.crank_angle \n2.pressure \n3.max_pres \n4.min_pres \n5.mean_temp \n6.max_temp \n7.min_temp \n8.volume \n9.mass\n10.density\n11.integrated_hr\n12.hr_rate\n13.cp\n14.cv\n15.gamma\n16.kin_vis\n17.dyn_vis')
print('\nSelect different x and y inputparameters, only from te list given above. \nDo not enter 0 or any other number beyound list given above')
x = int(input('\nEnter the x_axis paramter to plot the data - '))
y = int(input('\nEnter the y_axis paramter to plot the data - '))
for i in range (0, len(parameter)):
for j in range (0, len(unit)):
fig = plt.figure(linewidth = 5, edgecolor = "k")
plt.plot(select_parameter[x], select_parameter[y])
plt.xlabel(parameter[0][x]+unit[0][x], size = '10')
plt.ylabel(parameter[0][y]+unit[0][y], size = '10')
plt.title(parameter[0][y] + 'Vs' + parameter[0][x], size = 15)
plt.savefig(parameter[0][y] + 'Vs' + parameter[0][x]+'.png')
print('\nEngine performance calculation')
fuel_consumption = 20
RPM = 1500
print('\nFuel consumption is ',fuel_consumption,'mg')
print('\nEngine RPM is ',RPM)
workdone = np.trapz(select_parameter[2], select_parameter[8])
workdone = (workdone*pow(10,6))
print('\n1. Work done by engine =%.5f' %(workdone),'J')
power = (workdone*(RPM/60))/1000
print('\n2. Power output =%.5f' %(power),'KW')
RPS =RPM/60
Total_strokes = 4*RPS
fuel_consumption = fuel_consumption*Total_strokes
specifice_fuel_consumption =(fuel_consumption*0.0036)
print('\n3. The Fuel consumption/ Second at given engine speed =%.5f' %(fuel_consumption),'Mg/s')
print('\n4. Specific fuel_consumption =%.5f' %(specifice_fuel_consumption),'g/KW.hr')
else:
print('File not recognized. Please provide a valid CONVERGE output file')
Result:
Plot Between column 11 &15:
Different file name:
PV plot:
The area under the P-V diagram:
The area under PV diagram = Work done ==> 500.58663 J
The power output of the engine ==> 12.51467 KW
The specific fuel consumption of the engine ==> 7.20000 g/KW.hr
Conclusion:
- This program can be used to parse any converge file not just the ones used for engine simulation as long as the format is same. It is made sure that the column names are input from the file and not by the user to make this program compatible for almost any similar file with minor changes.
- It is possible to add more of the performance calculations and further automate the analysis procedure where the user only has to input the variables such as in this case the ‘RPM’ figure and ‘fuel consumed’.
- This program can also be used simply to visualize the overwhelming amount of data a simulation program produces in ‘.out ’ file for the better understanding of it. Similar program can be used as a post processor for such a simulation involving animations and further visualization tools.