Week 6 - Data analysis
Objective:
Data visualizer (100 points)
- 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 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. THIS SHOULD NOT BE DONE MANUALLY
Compatibility check (30 points)
- Your code should exit gracefully, if a non-compatible file is provided as input. It should say something like "File not recognized. Please provide a valid CONVERGE output file"
Basic performance calculation (50 points)
- 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.
Solution:
The following are the formulas used for calculating area, power, and specific fuel consumption.

Python code:
import matplotlib.pyplot as plt
import math
import numpy as np
from scipy.integrate import simps #for calc area using integration
#file compatibility check
try:
file_name = open('engine_data.out','r')
except FileNotFoundError:
print("File not recognized. Please provide a valid CONVERGE output file")
exit() #if the file is not correct, then the python exits the program
#Getting input for the plots
print('The following are the column names and their numbers.')
Columns = ['Crank_angle=1','Pressure=2','Max_pressure=3','Min_pressure=4','Mean_temperature=5','Max_temperature=6','Min_temperature=7','Volume=8','Mass=9','Density=10','Integrated_Heat_Rejection=11','Heat_Rejection_rate=12','C_p=13','C_v=14','Gamma=15','Kinematic Viscosity=16','Dynamic Viscosity=17']
print(Columns)
Pressure = 2
Volume = 8
Pr = []
V =[]
#file parsing
for line in open('engine_data.out'):
if '#' not in line:
Pr.append(float(line.split()[Pressure-1]))
V.append(float(line.split()[Volume-1]))
print('\nInput the column numbers for the desired plot')
column1=input('Enter the column number 1:')
column2=input('Enter the column number 2:')
column1=int(column1)
column2=int(column2)
data1=[]
data2=[]
#file parsing
for line in open('engine_data.out'):
if '#' not in line:
data1.append(float(line.split()[column1-1]))
data2.append(float(line.split()[column2-1]))
#calculation for area, power and sfc
area=float(simps(Pr, V))
area=area*1e6
print('area:{}\n' .format(area))
rpm=1500
rps=1500/60
n=1 #no. of cylinders
stroke_cycle = rps/2
power=(n*area*stroke_cycle)/1000 # power in kW
print('power:{} \n' .format(power))
mf = 20*pow(10,-6) #fuel mass
sfc=(mf*stroke_cycle*3600)/(power*1000) #specific fuel consumption in (kg/hr)/kW
print('specific_fuel_consumption: {}\n'.format(sfc))
#Pressure Vs Volume plot
plt.figure(1)
plt.plot(V,Pr,linewidth=3)
plt.xlabel('Volume in m3')
plt.ylabel('Pressure in Mpa')
plt.title('Pressure Vs Volume',fontsize=12)
plt.savefig('Pressure Vs Volume'+'.png')
plt.show()
#Desired plot
parameters = ['Crank','Pressure','Maximum Pressure','Minimum Pressure','Mean Temperature','Maximum Temperature','Minimum Temperature','Volume','Mass','Density','Integrated HR','HR rate','Cp','Cv','gamma','Kinematic Viscosity','Dynamic Viscosity']
units = ['(DEG)','(Mpa)','(Mpa)','(Mpa)','(K)','(K)','(K)','m3','(kg)','(kg/m3)','(J)','(J)','(J/time)','(J/kgK)','(J/kgK)','','pow(m,2)/s','(Ns/pow(m,2)']
plt.figure(2)
plt.plot(data1,data2,linewidth=3)
plt.xlabel(parameters[column1-1] + ' ' +units[column1-1])
plt.ylabel(parameters[column2-1] + ' ' +units[column2-1])
plt.title(parameters[column2-1]+' Vs '+parameters[column1-1],fontsize=12)
plt.savefig(parameters[column2-1]+'Vs'+parameters[column1-1]+'.png')
plt.show()
Code explanation:
- The required modules such as matplotlib, math, scipy.integrate are imported to perform the required functions.
- The file compatibility check is done and if the file is valid, the data is extracted in the next step or else the program will be stopped.
- File parsing and data allocation are done for pressure and volume and for the necessary calculations.
- The same file parsing is done again for the desired plots with the user inputs.
- Now, with the user inputs, the desired results are generated and the plots are saved.
Results:

Plots:

Desired plot as given by user input:


Conclusion:
The python program ran successfully and the desired outputs are obtained.