Week 6 - Data analysis
Aim:
From the given file “engine_data.out”, All the data inside the file has to be read and some tasks to be performed by using that data inside the file, with PYTHON programming language.
OBJECTIVE:
Task -1: The PYTHON code should exit gracefully if a non-compatible file is provided as an input. It should say something
like "File not recognized. Please provide a valid CONVERGE output file"
Task-2: The PYTHON code should take column numbers as the input and plot the respective columns as separate images and each file should be saved by the name of the column. This should not be done manually.
Task-3: The PYTHON code to calculate the area under the P-V diagram. P is in column 2 and V is in column 8
Task-4: Calculate the power output of this engine. Assume that RPM is 1500
Task-5: This engine consumed 20 micrograms of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.
Problem Statement:
Data-science is a wide and new field in engineering and sciences. Data science gives us a vast set of data which needs to be studied for forecasting the future behaviour of the data being studied. Thus, writing a program that can do the same with the set of data provided will be simplifying this process for the data scientists.
Theory:
Calculation of power output and Specific fuel consumption (SFC)
power_output = n*area*stroke_cycle/(1000)
where;
n= number of cylinders
area = area under PV diagram (obtained from data set)
stroke_cycle = revolutions per second/ 2
SFC = (fuel_consumption * 106 * stroke_cycle * 3600) / (power_output * 1000)
Program code:
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import simps
#Compatibility check
fname = input("Enter the filename:")
if fname == 'engine_data.out':
print('file is valid')
else:
print('file is invalid')
exit()
def compatibility_check(a):
if 'CONVERGE' not in a:
print('Please provide converge output file to process')
exit()
else:
print('Compatibility check is successful')
pass
#try:
open('engine_data.out')
# Creating an empty array to store the values of Volume and pressure respectively
Volume = []
pressure = []
line_count = 1
for line in open('engine_data.out'):
if '#' not in line:
Volume.append(float(line.split()[7]))
pressure.append(float(line.split()[1]))
line_count = line_count + 1
#plotting p-v diagram
plt.plot(Volume, pressure, linewidth = 2, color = 'b')
plt.xlabel('Volume')
plt.ylabel('pressure')
plt.title("p-v diagram of engine")
plt.grid()
plt.show()
area = float(simps(pressure,Volume))
print('Area under the curve using Simpsons Rule :' +str(area) + ' units')
print('Work done by the engine : ' + str(area) + ' MJ')
area = area * 1e6
print('Work done by engine : ' + str(area) + ' J')
# Inputs
# Speed of an Engine
rpm = 1500
rps = 1500/60
stroke_cycle = rps/2
# Fuel consumption in micrograms per 1 stroke cycle
fuel_consumption = 20
# Engine specifications
# Number of cylinder
n = 1
# 4 stroke engine
# Calculation of power output and Specific fuel consumption
power_output = n*area*stroke_cycle/(1000)
print('power output from engine : ' + str(power_output) + 'KW')
SFC = (fuel_consumption*pow(10,-6)*stroke_cycle*3600)/(power_output*1000)
print('Specific Fuel consumption, SFC = ' + str(SFC) + ' (kg/hr)/KW')
print('t')
# Calculation of Area under PV diagram
Area = np.trapz(pressure,Volume)
print('Area under curve using trapezoidal rule : ' + str(Area) + 'units')
print('Work done by the engine : ' + str(Area) + ' MJ' )
Area = Area * 1e6
print('Work done by engine : ' + str(Area) + ' J')
power_output = n*Area*stroke_cycle/(1000)
print('power output from the engine : ' + str(power_output) + 'KW')
SFC = (fuel_consumption*pow(10,-6)*stroke_cycle*3600)/(power_output*1000)
print('Specific Fuel consumption, SFC = ' + str(SFC) + ' (kg/hr)/KW')
print('t')
# Transposing columns in to row matrix and converting them in to float type from string type
#transpose = np.transpose(data)
#float_f = transpose.astype(np.float)
# Array for header and their units
#parameters = ['Crank','Pressure','Maximum Pressure','Minimum Pressure','Mean Temperature','MaximumTemperature','Minimum Temperature','Volume','Mass','Density','Integrated HR','HRrate','Cp','Cv','gamma','Kinematic Viscosity','Dynamic Viscosity']
units = ['(DEG)','(Mpa)','(Mpa)','(Mpa)','(K)','(K)','(K)','pow(m,3)','(kg)','(kg/(pow(m,3)))','(J)','(J)','(J/time)'
,'(J/kgK)','(J/kgK)','','pow(m,2)/s','(Ns/pow(m,2)']
# Providing user choice of picking 2 parameters for plotting
print('Choose from following options and press enter key')
print('1 Crank n 2 Pressure n 3 Maximum Pressure n 4 Minimum Pressure n 5 Mean Temperature n 6 MaximumTemperature n 7 Minimum Temperature n 8 Volume n 9 Mass n 10 Density n 11 Integrated HR n 12 HR rate n 13 Cp n 14 Cv n 15 gamma n 16 Kinematic Viscosity n 17 Dynamic Viscosity')
X = int(input('Enter your choice on X-axis n'))
Y = int(input('Enter your choice on Y-axis n'))
n = X-1
m = Y-1
crank = []
max_pressure = []
min_pressure = []
mean_temp = []
max_temp = []
min_temp = []
mass = []
density = []
integra_HR = []
HRrate = []
Cp = []
Cv = []
gamma = []
Kinematic_viscosity = []
Dynamic_viscosity = []
line_count = 1
for line in open('engine_data.out'):
if '#' not in line:
crank.append(float(line.split()[0]))
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]))
mass.append(float(line.split()[8]))
density.append(float(line.split()[9]))
integra_HR.append(float(line.split()[10]))
HRrate.append(float(line.split()[11]))
Cp.append(float(line.split()[12]))
Cv.append(float(line.split()[13]))
gamma.append(float(line.split()[14]))
Kinematic_viscosity.append(float(line.split()[15]))
Dynamic_viscosity.append(float(line.split()[16]))
line_count = line_count + 1
parameter = np.array([crank,pressure,max_pressure,min_pressure,mean_temp,max_temp,min_temp,Volume,mass,density,integra_HR,HRrate,Cp,Cv,gamma,Kinematic_viscosity,Dynamic_viscosity])
parameters = ['Crank','Pressure','Maximum Pressure','Minimum Pressure','Mean Temperature','MaximumTemperature','Minimum Temperature','Volume','Mass','Density','Integrated HR','HRrate','Cp','Cv','gamma','Kinematic Viscosity','Dynamic Viscosity']
units = ['(DEG)','(Mpa)','(Mpa)','(Mpa)','(K)','(K)','(K)','pow(m,3)','(kg)','(kg/(pow(m,3)))','(J)','(J)','(J/time)'
,'(J/kgK)','(J/kgK)','','pow(m,2)/s','(Ns/pow(m,2)']
plt.figure("User input plotting")
plt.plot(parameter[n],parameter[m],linewidth=3)
plt.title(' plot for ' + parameters[m]+ ' versus ' +parameters[n],color='k')
plt.xlabel(parameters[n] + ' ' +units[n])
plt.ylabel(parameters[m] + ' ' +units[m])
plt.grid()
plt.show()
Explanation:-
1)For Compatability check, the file should match with the main file so we take that input as a string and match it with the if elseloop
2)The a and b as integer inputs to use later.
3)We start reading file A after opening it in for loop.
4)In this, we use 3 if conditions in which we save the line of unit and properties in an array and append them into an emptyarray.
5)when we split the line it comes in form of an array.
6)from this, I can get any column data of that row/line just by indexing the required value
7)As the crank angle has 1 indexing, pressure has 2 like this we can get any data we want.
8)convert that data into floating-point numbers as it might be a string.
9)Add this to my crank array.
10)Now, save the file name and plot figure.
11)We calculated the below area, power, etc by simply defining the formula in python.
12)At last, we print all of them.
Output plot:


Basic performance calculation result:
Area under the curve using Simpsons Rule :0.0005005864474262364 units
Work done by the engine: 0.0005005864474262364 MJ
Work done by engine: 500.58644742623636 J
power output from engine: 6.257330592827954KW
Specific Fuel consumption, SFC = 0.00014383130100742395 (kg/hr)/KW
Area under curve using trapezoidal rule: 0.0005005866274680075units
Work done by the engine: 0.0005005866274680075 MJ
Work done by engine: 500.58662746800746 J
power output from the engine: 6.257332843350094KW
Specific Fuel consumption, SFC = 0.00014383124927683276 (kg/hr)/KW
CONCLUSION:
Hence, the large amount of data stored in a file, can easily studied, compared and analysed, using PYTHON programming language.