Week 6 - Data analysis
Aim:
To write a script that does the following:
Data visualizer
- Your script should take column numbers as the input and plot the respective columns as separate images
- Each file should be saved by the name of the column
- The plot labels should be extracted from the file. If, I request for a plot between column 1 (crank angle) and column 8(volume), then the label information should automatically be extracted and must appear as labels in the plots.
Compatability check
- You 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"
Basic performance calculation
- Calculate the area under the P-V diagram. P is in column 2 and V is in column 8.
- Calculate the power output of this engine. Assume that RPM is 1500
- This engine consumed 20 micro grams of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.
Python code: (with explanation)
#Week 6 - Data Analysis
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import simps
# 1. Data visualizer
#creating empty array for all 17 parameters
crank = []
pressure = []
max_pressure = []
min_pressure = []
mean_temperature = []
max_temperature = []
min_temperature = []
volume = []
mass = []
density = []
integrated_HR = []
HR_rate = []
Cp = []
Cv = []
gamma = []
kinematic_viscosity = []
dynamic_viscosity = []
line_count1 = 1
for line in open('engine_data.out'): #appending values into each of the 17 empty arrays after extracting the data from the data file.
if '#' not in line:
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_temperature.append(float(line.split()[4]))
max_temperature.append(float(line.split()[5]))
min_temperature.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]))
kinematic_viscosity.append(float(line.split()[15]))
dynamic_viscosity.append(float(line.split()[16]))
line_count1 = line_count1 + 1
#creating a numpy array to store all the values under each of the 17 parameters
all_values = np.array([crank, pressure, max_pressure, min_pressure, mean_temperature, max_temperature, min_temperature,
volume, mass, density, integrated_HR, HR_rate, Cp, Cv, gamma, kinematic_viscosity, dynamic_viscosity])
col_names = [] #creating an empty array for colum names
line_count2 = 1
for row in open('engine_data.out'): #using for loop to append values into the empty array col_names after splitting them.
if line_count2 == 3:
col_names.append(row.split())
line_count2 = line_count2 + 1
col_labels = col_names[0] #extracting the required list/ array from the col_names and storing them in col_labels.
#Plotting each of the 17 columns/ parameters as separate images with plot labels extracted from the given data file and plots saved by the name of the columns.
for col_no in range(1, 18):
plt.figure(col_no)
plt.plot(all_values[col_no - 1])
plt.xlabel('Number of entries')
plt.ylabel(col_labels[col_no])
plt.savefig(col_labels[col_no] + '.png')
plt.show()
#For plotting two parameters
option = int(input('To generate a plot between two parameters, press "2"\n')) #obtaining input from the user
if option == 2:
sub_option1 = int(input('Choose your parameter for x-axis: \n1 - Crank \n2 - Pressure \n3 - Maximum pressure \n4 - Minimum pressure \n5 - Temperature \n6 - Maximum temperature \n7 - Minimum temperature \n8 - Volume \n9 - Mass \n10 - Density \n11 - Integrated HR \n12 - HR rate \n13 - Cp \n14 - Cv \n15 - Gamma \n16 - Kinematic Viscosity \n17 - Dynamic Viscosity \n'))
sub_option2 = int(input('Choose your parameter for y-axis: \n1 - Crank \n2 - Pressure \n3 - Maximum pressure \n4 - Minimum pressure \n5 - Temperature \n6 - Maximum temperature \n7 - Minimum temperature \n8 - Volume \n9 - Mass \n10 - Density \n11 - Integrated HR \n12 - HR rate \n13 - Cp \n14 - Cv \n15 - Gamma \n16 - Kinematic Viscosity \n17 - Dynamic Viscosity \n'))
#Plotting the requested graph
plt.figure()
plt.plot(all_values[sub_option1 - 1], all_values[sub_option2 - 1])
plt.xlabel(col_labels[sub_option1])
plt.ylabel(col_labels[sub_option2])
plt.show()
# 2. Basic performance calculation
#Area under the P-V diagram
p_col = 2
v_col = 8
area = simps(all_values[p_col-1], all_values[v_col-1])
print('The area under the P-V diagram is: ' + str(area*pow(10,3)) + ' KJ')
#Power output in KW
rpm = 1500
power = area*pow(10,6) * rpm / 9549 #power in KiloWatts
print('Power output of the engine at 1500 rpm is: ' + str(power) + ' KW')
#Specific fuel consumption in g/J
sfc = (20*pow(10,-6) * rpm * 60) / (power*pow(10,3))
print('Specific fuel consumption is: ' + str(sfc) + ' g/J')
#3. Compatibility check
#Note: I would like to know how this is done.
Results:
Data Visualizer:
Single parameter plots:
Two parameter plot (example - PV):
Basic performance calculation:
- The area under the P-V diagram is: 0.5005864474262364 KJ
- Power output of the engine at 1500 rpm is: 78.63437754103619 KW
- Specific fuel consumption is: 2.2890751555331518e-05 g/J