Data Analysis using Python
OBJECTIVE – We have to write the script which should take column numbers as the input and plot the respective columns as separate images. The plot labels should be extracted from the file. If there is a request for a plot between column 1(crank angle) and column 8(volume) , then label information should automatically be extracted and must appear as labels in the plots.Also to calculate the area under the P-V diagram. P is in the column 2 and V is in column 8,the power output of this engine(Assume RPM is 1500) and its specific fuel consumption.
SCRIPT –
import matplotlib.pyplot as plt
import numpy as np
import math
Crank = []
Pressure = []
Max_Pres = []
Min_Pres = []
Mean_Temp = []
Max_Temp = []
Min_Temp = []
Volume = []
Mass = []
Density = []
Integrated_HR = []
HR_Rate = []
C_v = []
C_p = []
Gamma = []
Kin_Visc = []
Dyn_Visc =[]
label = []
Units = []
filename = input(\'Enter a Converge output file: \')
try:
file = open(filename,\'r\')
line_count = 1
for line in file:
if \'#\' in line:
if line_count is 3:
for j in range(1,18):
label.append(line.split()[j])
if line_count is 4:
for k in range(1,18):
Units.append(line.split([k]))
line_count = line_count + 1
if \'#\' not in line:
Crank.append(float(line.split()[0]))
Pressure.append(float(line.split()[1]))
Max_Pres.append(float(line.split()[2]))
Min_Pres.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_Visc.append(float(line.split()[15]))
Dyn_Visc.append(float(line.split()[16]))
Inp = np.array([[0], Crank, Pressure, Max_Pres, Min_Pres, Mean_Temp, Max_Temp, Min_Temp, Volume, Mass, Density, Integrated_HR, HR_Rate, C_v, C_p, Gamma, Kin_Visc, Dyn_Visc])
print(\'\\n Enter the Engine Parameters: \')
x = input(\"Engine parameter the x axis: \")
x = int(x)
y = input(\"Engine parameter on the y axis: \")
y = int(y)
plt.plot(Inp[x],Inp[y])
plt.Title(str(label[x-1]) + \' v \' + str(label[y-1]) + \'plot\')
plt.xlabel(str(label[x-1]) + str(Units[x-1]))
plt.ylabel(str(label[y-1]) + str(Units[y-1]))
plt.savefig(str(label[x-1]) + \'v\' + str(label[y-1]))
plt.show()
if x == 2 and y == 8 or x == 8 and y == 2:
rpm = input(\'\\nWhat is the engine rpm? \')
rpm = float(rpm)
r = input(\'\\nWhat is the fuel consumption?\')
r = float(r)
T = np.trapz(Inp[x], Inp[y])
P = (2*math.pi*rpm*T)/(60)
SFC = ((r*rpm)*(pow(10,-6))*(3600))/(P*1000*60)
print(\'\\nArea under the pv plt = t = \',T,\'N-m\')
print(\'\\nPower = \', P,\'Mw\')
print(\'\\nSFC = \', SFC, \'g/KW-h\')
except:
print(\'Enter a valid Converge output file\')
The P-V plot obtained by the code.
The command line arguments inputs to obtain the results:
The code is written such that the rpm and fuel consumption values can be input for different running conditions.Sublime text is modified with a package such that it is able to take in user input and read the input to provide the output. The results for the Power and SFC calculations are as above and the P-V plot is added below.
For any other variables, the area under the plot isn’t a case of study and it will not be printed as part of the output.
As we can see, the area under the graph and subsequent calculations haven’t been done for this case plotting crank angle and pressure.
The result as obtained from this.
Finally the script is written such that the file must be an output file from Converge. If not an error will be displayed.
The script has been written to be modular and find any data that the user required from the data file.