Week 6 - Data analysis
Data analysis (python)
1. OBJECTIVES
Write a script that does the following.
Link to data file
1. Data visualizer
Write a script that 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. THIS SHOULD NOT BE DONE MANUALLY
2. Compatability check
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"
3. 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.
2. THEORY
Thermodynamic data collected from simulation of a port fuel injected engine is extracted and analyzed from a data file using a Python program. This data file
consists of 17 columns of different thermodynamic properties for which more than 8000 values have been recorded.
From the data file we can understand that it a 4-Stroke otto cycle. So, we will apply the following formula for to calculate the Work done, Power output and
Specific fuel consumption -
A=area under the curve=workdone(MJ)
poweroutput=A*n*(pow(10,6)) w
n=no of cycles/persec
n=N/(2*60) for 4-strokeengine
n=N/(60) for 2-strokeengine
N=rotations per min
SFC(specific fuel consumption)=fuel consumed(kg/hr)/(power output(kw)
3. PYTHON CODE
#importing libraries
import matplotlib.pyplot as plt
import numpy as np
from numpy import trapz
#taking input from user
fname = input("Enter the filename: ")
if fname == 'engine_data.out':
print('file is valid')
else:
print('file is invalid')
exit()
def compatibility_check(a):
if 'CONVERGE' not in a:
print('please provide converge output file to process')
exit()
else:
print('compatibility check is successful')
pass
FILE = []
#accesing file data
for line in open(fname):
if '#' in line:
FILE.append(line.split())
compatibility_check(FILE[0])
noc = len(FILE[1])
head = len(FILE[2])
col_no = []
col_val = []
col_units = []
z = FILE[1].index('column')
for i in range(z,noc-1):
col_no.append(i)
for j in FILE[2]:
if j is not '#':
col_val.append(j)
for k in FILE[3]:
if k is not '#':
col_units.append(k)
print('data available are:')
for a in range(0,len(col_no)):
print(col_no[a],col_val[a],col_units[a])
#taking input for plotting
xaxis = int(input('Enter xaxis column no:'))-1
yaxis = int(input('Enter yaxis column no:'))-1
x = []
y = []
for line in open(fname):
if '#' not in line:
x.append(float(line.split()[xaxis]))
y.append(float(line.split()[yaxis]))
plt.plot(x,y,linewidth=1.5)
plt.xlabel(col_val[xaxis])
plt.ylabel(col_val[yaxis])
plt.show()
pressure = []
vol = []
for line in open(fname):
if '#' not in line:
pressure.append(float(line.split()[1]))
vol.append(float(line.split()[7]))
#caluculations
Pressure = 1e6*np.array(pressure)
Volume = np.array(vol)
area = np.trapz(Pressure,Volume)
print('workdone =',area, 'joules')
cyclepersec = 750/60
power = area*(cyclepersec/1000)
print('power=',power,'kw')
mass_flow=20e-6
sfc = mass_flow*3600/power
print('specific fuel consumption =',sfc,'g/kwh')
code explanation:
- EXPLANATION OF THE PYTHON CODE
- Import the necessary modules.
- Take filename as an input and assign to ‘filename’ variable.
- Perform compatibility test
Checking if the file is a valid CONVERGE output file
Checking if data is present in the CONVERGE output file
- Data extraction
Extracting data along with the column name and column number from the CONVERGE output file. Complete data from the file stored in two 2D array ‘data’.
The columns of the data matrix contains the data for each individual parameters.
- Getting the data
Provide an instruction to the user so that he/she can use the numbers as an input to designate the variables to be plotted.
Ask users of plot between any two variables is required. If input is ‘y’ then ask for the numbers representing the variables to be plotted.
Ask the user if the plot is to be saved or not.
Then show the plot.
Above steps run in a while loop until user gives an input ‘n’ to the question “Do you want a plot between any two variables? (y/n): “
- Calculation
Extract volume and pressure data from matrix ‘data’ and store in the ‘volume’ and ‘pressure’ respectively.Then, Numerically integrate pressure and volume
data using simps() function from scipy.integrate library. It is the work done by the engine.
Take the value of engine speed (in rpm) and fuel consumption (in microgram) form user. Calculate the Power produced and Specific fuel consumption
Using derived formulas
final output:


