Parsing CONVERGE output file using Python
Introduction:
Parsing means "To split a file or other input into pieces of data that can be easily stored or manipulated." Here, I have parsed a CONVERGE output file containing 17 parameters for different crank angles in a specific IC engine. I have dynamically created and initialized the parameter arrays using the vars() function. I have then parsed the file and stored them in the corresponding arrays. The code then receives input from the user and displays the appropriate plot as requested by the user and then it saves the plot with the appropriate name. Finally, the code calculates and displays the power output and the Specific Fuel Consumption. The code below is self-explanatory.
Code:
import math
import matplotlib.pyplot as plt
import numpy as np
# Reading the file and checking if it is a CONVERGE output file
filename=input('Enter the CONVERGE output file name with extension(eg.file_name.out)')
if '.out' in filename:
# Parsing the parameter names and units
for line in open(filename,'r'):
if '#' in line:
if 'Crank' in line:
parameters=line.split()
if '(' in line:
units=line.split()
# Creating parameter arrays dynamically and initializing them
for i in range(1,18):
vars()[parameters[i]]=[]
# Parsing the values of the parameters
for line in open(filename,'r'):
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]))
# Displaying an error message in case of incompatible file
else:
print("File not recognized. Please provide a valid CONVERGE output file")
# Creating a 2D list for plotting purpose
para = [Crank, Pressure, Max_Pres, Min_Pres, Mean_Temp, Max_Temp, Min_Temp, Volume, Mass, Density, Integrated_HR, HR_Rate, C_p, C_v, Gamma, Kin_Visc, Dyn_Visc]
# Displaying the list of parameters with corresponding column numbers
for i in range(1,18):
print(i,')',parameters[i])
# Receiving column numbers to be plotted from the user
column1 = int(input("Enter the column number you wish to plot in X-axis"))
column2 = int(input("Enter the column number you wish to plot in Y-axis"))
# Plotting and saving the file
plt.plot(para[column1-1], para[column2-1])
plt.xlabel(parameters[column1]+ ' '+ units[column1])
plt.ylabel(parameters[column2]+ ' ' + units[column2])
plt.title(parameters[column2]+' vs '+parameters[column1])
plt.savefig(parameters[column2]+' vs '+parameters[column1]+'.png')
plt.show()
# Calculating Work done, Power, BSFC
# Work done = Area under PV diagram
work_done = np.trapz(np.array(Pressure)*pow(10,6),np.array(Volume))
# Power = Work done per cycle / Time taken for one cycle to complete
# Time taken for one cycle to complete = 2*60/RPM
print('Work done = ',work_done,' J')
power = work_done/(60/750)
print('Power = ',power,' W')
# SFC =( Fuel consumption per cycle * Number of cycles per hour ) / Power
sfc = ((60*60/750)*(20*pow(10,-6))*1000)/(power)
print('SFC = ',sfc,' g/kWhr')
Results:
The results obtained on running the above code are as follows