Week 6 - Data analysis
Aim: To perform the data analysis
Objective:
-
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.
-
Compatibility check
- Your 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 micrograms of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.
Code explanation :
Import library
using input syntax and if-else we check the entry of the correct file
Now we take the file to check for valid convergence input value and check using the for loop and with reference #
Take the data from the file
Take input from the user for x and y column and plot the values
Create the p and v values using append and calculate the power
Code:
import matplotlib.pyplot as plt
import numpy as np
from numpy import trapz
fname=input('Enter File name with file extension:')
if fname == 'engine_data.out':
print('Compatibility check')
else:
print('File name is invalid, please enter a valid file name')
exit()
#Fuction to check the compatibility of the input file
def CompCheck(a):
if "CONVERGE" not in a:
print('Incompatible File: Please provide a valid CONVERGE output file, program will exit now')
exit()
else:
print('Compatibility Check Success')
pass
test = []
#Reading the file for compatibility
for line in open(fname):
if '#' in line:
test.append(line.split())
#calling the compatibility function to check for valid converge input file
CompCheck(test[0])
colnum=len(test[1])
colhead = len(test[2])
col_num = []
col_val = []
col_unit = []
a=test[1].index("column")
for x in range(a, colnum-1):
col_num.append(x)
for i in test[2]:
if i is not '#':
col_val.append(i)
for j in test[3]:
if j is not '#':
col_unit.append(j)
print('Available Data')
for i in range(0,len(col_num)):
print(col_num[i], col_val[i], col_unit[i])
#Section for user choice for the plots required
x = int(input('Enter X Column No.: '))-1
y = int(input('Enter Y Column No.: '))-1
x_label = col_val[x]
y_label = col_val[y]
X = []
Y = []
#Reading the required column data from the file
for line in open(fname):
if '#' not in line:
X.append(float(line.split()[x]))
Y.append(float(line.split()[y]))
#plotting the column values as required by the user
plt.plot(X, Y, linewidth=2)
plt.xlabel(x_label+col_unit[x])
plt.ylabel(y_label+col_unit[y])
plt.show()
#Engine Performance Calculations
P = []
V = []
for line in open(fname):
if '#' not in line:
P.append(float(line.split()[1]))
V.append(float(line.split()[7]))
Pressure = 1e6* np.array(P)
Volume = np.array(V)
AUC = np.trapz(Pressure, Volume)
print('Engine Performance Calculations')
print('Work Done', AUC, 'Joules')
cyclepersec = 750/60
power = AUC*(cyclepersec/1000)
print('Power Output of the engine @ 1500RPM', power, 'kW')
mf= 20e-6
SFC= mf*3600/power
print('Specific Fuel Consumption', SFC, 'g/KW.h')
Results:
- Data visualization and basic calculation
- Compatibility check