Week 6 - Data analysis
"""
Challenge - Week6 : Data Analysis
"""
import matplotlib.pyplot as plt
import numpy as np
"""
Reading data from file and plotting it
"""
# Take input from user
m = int(input("Enter the 1st column you wish to extract data from (Note: Col indexed from 1)"))
n = int(input("Enter the 2nd column you wish to extract data from (Note: Col indexed from 1)"))
if n not in range(1, 18) or m not in range(1, 18):
print("You have not entered a valid column number")
exit()
# Read data file
data_m = []
data_n = []
pressure = []
volume = []
name_m = ""
name_n = ""
unit_m = ""
unit_n = ""
for i, line in enumerate(open('engine_data.out')):
if i in range(5):
if line.split()[0] != '#':
print("File not recognised. Please provide a valid CONVERGE output file")
exit()
if i == 2:
name_m = line.split()[m] # [0] contains '#'
name_n = line.split()[n]
if i == 3:
unit_m = line.split()[m] # [0] contains '#'
unit_n = line.split()[n]
elif '#' not in line:
if len(line.split()) != 17:
print("File not recognised. Please provide a valid CONVERGRE output file")
exit()
else:
data_m.append(float(line.split()[m-1]))
data_n.append(float(line.split()[n-1]))
pressure.append(float(line.split()[1]))
volume.append(float(line.split()[7]))
# Plotting extracted data
plt.figure()
plt.plot(data_m)
plt.xlabel('data number in file')
plt.ylabel('{} {}'.format(name_m, unit_m))
plt.title('1st requested column data')
plt.show()
plt.figure()
plt.plot(data_n)
plt.xlabel('data number in file')
plt.ylabel('{} {}'.format(name_n, unit_n))
plt.title('2nd requested column data')
plt.show()
# Plotting 1st extracted column data vs 2nd
plt.figure()
plt.plot(data_m, data_n)
plt.xlabel('%s' % name_m)
plt.ylabel('%s' % name_n)
plt.show()
plt.figure()
plt.plot(volume, pressure)
plt.xlabel('Volume (m^3)')
plt.ylabel('Pressure (MPa)')
plt.show()
# Save the extracted data to files
file_m = open("%s.txt" % name_m, "w")
file_n = open("%s.txt" % name_n, "w")
for i in range(len(data_m)):
file_m.write("%0.9f\n" % data_m[i])
file_n.write("%0.9f\n" % data_n[i])
"""
Area under the curve
"""
"""
# trying to capture the upper curve in th PV diagram
p_upper = pressure[pressure.index(max(pressure)):]
v_upper = volume[pressure.index(max(pressure)):]
area_upper_curve = np.trapz(p_upper, v_upper)
print('area under upper curve: %0.10f' % area_upper_curve)
# trying to capture the lower curve in th PV diagram
p_lower = np.flipud(pressure[volume.index(max(volume)):volume.index(min(volume))])
v_lower = np.flipud(volume[volume.index(max(volume)):volume.index(min(volume))]) # reverse to get v increasing order
area_lower_curve = np.trapz(p_lower, v_lower)
print('area under lower curve: %0.10f' % area_lower_curve)
# Plot of the curves under which the area is being calculated
plt.figure()
plt.plot(v_upper, p_upper)
plt.ylabel('pressure')
plt.xlabel('volume')
plt.plot(v_lower, p_lower)
plt.title('PV curves to calculate area')
plt.show()
# Work done - area under PV curve in otto cycle
area_work_done = (area_upper_curve - area_lower_curve)*pow(10, 6) # P is in MPa and V m3, converting MPa to 10^6 N/m2
print("Work done in otto cycle : %0.10f J" % area_work_done)
"""
area_work_done = np.trapz(pressure, volume)*pow(10, 6) # converting MPa to 10^6N/m2
print("using entire pr vol arrays: area : %0.10f J" % area_work_done)
# Power output of engine with 1500RPM
rpm = 1500
power = (area_work_done*rpm)/60 # Converting rpm to rps
print("Power output: %0.10f W" % power)
# Specific fuel consumption ( 20 micrograms of fuel per stroke cycle)
# (BSFC is expressed in units of grams per kilowatt-hour )
fuel_g = 20*pow(10, -6) # g/cycle
sfc = (fuel_g*1500*60/2)/(power/1000) # 1 cycle = 2 rotations(4 strokes); so divide rpm/2 -->cycles/min
print("specific fuel consumption: %0.10f g/KW-hr" % sfc)
Objective: Read an input file using python and extract required data
Problem Statement: Read data from input file and use it for plotting the graphs. Calculate the work done, power output and specific fuel consumption from pressure and volume data extracted from the input file.
Solution Procedure:
- Take the column number of the data to be read as input from the user
- open file, read and append required data to their respective lists
- Plot the graphs of data read
- use .write function to save the data which has been read, to .txt files
- use np.trapz() to find the area under the PV curve. Area under the curve implies work done
- calculate the power output
- calculate the specific fuel consumption
Results:
1. Graph of 1st column data extracted from input file
2. Graph of 2nd column data extracted from input file
3.Graph of 1st column vs 2nd column data extracted from input file
4. Pressure vs Volume
5. File compatibility
6. Program results
Discussion:
- Note: I calculated area under the curve (upper and lower) curve and subtracted both to get the answer [this section has been commented out in the code]. Which is how I thought the area under the curve should be found. However, since I am not getting the same result as np.trapz(pressure,volume), I have not used it.
- File compatibility check has been placed as first 5 lines having the # symbol or not. Another check is each line has 17 values. The column number entered by the user is also validated.
- result number 6 shows the values obtained on calculation. Necessary unit conversions have been done in the code written.