Week 6 - Data analysis
Aim:
To prepare a Python code to extract data from an Output file and perform various operations
Governing Equations:
- Area under the curve is solved by integrating points between State values
∫_v1^v2▒Pdv+∫_v2^v3▒〖Pdv+∫_v3^v4▒〖Pdv+∫_v4^v1▒Pdv〗〗
The whole thing is achieved by a single function – np.trapz(y,x)
- Power Output of the Engine = 2πNW/60 J
Where, N is rpm, W is work done (area under the curve)
- Specific Fuel Consumption = (3600*(Fuel Consume)/sec*N)/(60*Power) g/KWh
Objective:
To import engine_data.out file and extract values and parameters and perform various operations.
Code:
#File Parsing Assignment
import matplotlib.pyplot as plt
import numpy as np
import math
import os.path
#Taking input for a file name and checking if the file name exists in the directory or not
#filename = 'engine_data.out'
check=0
while check!=1:
filename = input("Enter the name of the file for parsing data. Please input exact name of the file with extension: ")
#check variable would contain 1 if the file name exists and 0 if it wont.
check = os.path.isfile(filename)
if check == 0:
print("File not recognized. Please provide a valid CONVERGE output file")
#If the file exists further operations would be performed otherwise the code would not proceed at all
else:
check=1
#Inintialing empty lists for all 17 parameters
ct=0
crank=[] #Degree
pressure=[] #MPa
max_pressure=[] #MPa
min_pressure=[] #MPa
mean_temp=[] #K
max_temp=[] #K
min_temp=[] #K
volume=[] #m^3
mass=[] #Kg
density=[] #Kg/m^3
integrated_hr=[] #J
hr_rate=[] #J/time
c_p=[] #J/kg-K
c_v=[] #J/Kg-K
gamma=[]
kin_vis=[] #m^2/s
dyn_vis=[] #N-s/m^2
#Extracting every line from the file
for line in open(filename):
#Initializing a counter variable which would count the number of lines in the data file
ct=ct+1
if ct==3:
#Since we know in the 3rd line we have the labels for our data set we would store it in a different array
labels = line.split()
if '#' not in line:
#Ignoring the first few comments line and moving ahead for the numbers
#Since in a single line we have all the 17 parameters stored in line list we would extract values for specific parameters
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_vis.append(float(line.split()[15]))
dyn_vis.append(float(line.split()[16]))
def func(column):
#Based on the users desire we would take in a column no and return the complete list for the desired column
if (column-1) == 0:
return crank
if (column-1) == 1:
return pressure
if (column-1) == 2:
return max_pressure
if (column-1) == 3:
return min_pressure
if (column-1) == 4:
return mean_temp
if (column-1) == 5:
return max_temp
if (column-1) == 6:
return min_temp
if (column-1) == 7:
return volume
if (column-1) == 8:
return mass
if (column-1) == 9:
return density
if (column-1) == 10:
return integrated_hr
if (column-1) == 11:
return hr_rate
if (column-1) == 12:
return c_p
if (column-1) == 13:
return c_v
if (column-1) == 14:
return gamma
if (column-1) == 15:
return kin_vis
if (column-1) == 16:
return dyn_vis
#Taking the column no input from the user and changing the string format to float for further operations
print('1.Crank(Degree)\n2.Pressure(MPa)\n3.Maximum Pressure(MPa)\n4.Minimum Pressure(MPa)\n5.Mean Temperature(K)\n6.Maximum Temperature(K)\n7.Minimum Temperature(K)\n8.Volume(m^3)\n9.Mass(Kg)\n10.Density(Kg/m^3)\n11.Integrated HR(J)\n12.HR Rate(J/time)\n13.CP(J/Kg-K)\n14.CV(J/Kg-K)\n15.Gamma\n16.Kinematic Viscosity(m^2/s)\n17.Dynamic Viscosity(N-s/m^2)\n')
check = 0
while check!=1:
column = int(input("Enter the columnn no: "))
if column<1 or column>18 :
print('Please enter a valid number')
else:
check=1
#Calling the function and storing the desired output list in variable output
output = func(column)
plt.figure()
plt.plot(output)
plt.ylabel(labels[column])
plt.title(labels[column])
plt.savefig(labels[column]+'.png')
plt.show()
#PV Plot for Basic Performance Calculation
plt.figure()
plt.plot(volume,pressure)
plt.xlabel('volume (m^3)')
plt.ylabel('Pressure (MPa)')
plt.title('PV Diagram')
plt.show()
#Area under PV Curve
#Converting Pressure in MPa to Pa
#np.aray is used to convert list to arrays to perform mathematical operations
pr = 1e6*np.array(pressure)
vol = np.array(volume)
#np.trapz(y,x) is used to perform integration for equation y(x)dx, in this case pr(vol)dvol
area = np.trapz(pr,vol)
print('Area Under the Curve = Work done by the cycle =',area,'J') #Joules
#Power Output = 2*pi*N*W/60
#Here work done = area under the PV curve
n=1500 #rpm
power=2*math.pi*n*area/(60*1000)
print('Power Output of this Engine running at 1500 rpm is',power,'KJ') #KJ
#Specific Fuel Consumption = (3600*fuel_consume*rpm)/60*power gram/KWh
fuel_consume = 20*1e-6 #micro grams/stroke to grams/stroke
sfc = (3600*fuel_consume*n)/(60*power*1000) #power*1000 for converting KJ to J
print('Specific Fuel consumption of the engine at 20 micro grams/1 stroke cycle is',sfc,'g/KWh') #g/KWh
Solution Procedure & Results:
The code takes in input from the user for the output file that needs to be parsed. Once a valid file is obtained a list shows the available data columns present, for which the user can enter the column no and get a plot of that data. A while loop is used to check if a valid input is entered or not. Until and unless a valid input is not received the user would receive an error message and would be ask to enter valid parameters again.
Also, a P-V plot is displayed and area under the curve is printed. Power Output and Specific Fuel consumption is calculated and printed.