Week 6 - Data analysis
AIM: Performing Data Analysis of the engine using Python.
OBJECTIVE
- Make a data visualizer and perform the data analysis.
- Plot the graph for the column number which is given by the user.
- Calculate the area under the PV diagram.
- Find the power output of the engine.
- Find specific fuel consumption.
PROBLEM STATEMENT
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. 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
- 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
- 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.
DATA ANALYSIS
Data analysis is defined as a process of cleaning, transforming, and modeling data to discover useful information for business decision-making. The purpose of Data Analysis is to extract useful information from data and taking a decision based upon the data analysis.
Whenever we take any decision in our day-to-day life is it by thinking about what happened last time or what will happen by choosing that particular decision? This is nothing but analyzing our past or future and making decisions based on it. For that, we gather memories of our past or dreams of our future. So that is nothing but data analysis. Now same thing analyst does for business purposes, is called Data Analysis.
For this challenge, we have to write a program to read the file or check whether the file is converged or not. Then take the data from the file performing some operations on it.
FORMULA USED:
1. Power Output
P=WsP=Ws watt
We know that the power is nothing but work done per second. The area under the PV diagram gives work done/cycle. For calculating the power output, we need to multiply work done/cycle with No. of power stroke/second.
Now for a 4-stroke engine no. of the power stroke is equal to half of the engine rpm because this engine completes 1 cycle in 2 revolutions. So,
P=W×N60×2P=W×N60×2 watt
P=W×N60×1000×2P=W×N60×1000×2 KW
2. Specific Fuel Consumption
It is the ratio of the mass of fuel consumption to power output.

Where
Mass of fuel consumption in Kg/h
For our case, fuel consumption is given into micrograms/cycle. So, we have to convert them into Kg/h. for these, we have to multiply the No. power stroke /hour and 10-9.
STEP-WISE EXPLANATION OF CODE
- First, we have to import some important modules like math, matplotlib, numpy, scipy, os and simps.
- To check whether the input file is converged or not I use to try and except a statement. so it gives an error if the file is not present or not in the proper type.
- Now I create an empty array and then use for loop to extract the line and split the line and append the values in the empty array.
- Again, I check whether the input file is converged or not by checking no. or rows equal to 8670 or not using the if-else statement.
- In this, if statement, I used another if statement to check the input file.
- If the file is converged then the whole program is run so I use the else command for the above loop.
- In this else command, first I extract column no. column label and unit of parameter arrays and save them in a particular variable.
- Then I delete unwanted elements from these 3 arrays.
- Now the file has 17 different parameter columns so I create the 17 empty arrays.
- A data value of this parameter starts from the 6 rows of the file. So, I used the for loop from 6 loops to the length of the file and extract the data value of the different parameters, and appended the value in a particular empty array of that parameter.
- I store all the arrays in one array.
- Then I print the information like which column contains which parameter. So, users can give input easily from see the information.
- Now, I take the inputs for the x and y axis column no. and check the input that it is in range and if it gives proper input or not.
- I print the x and y axis parameter to check user give the right input column no. for plotting the graph.
- Now I create the directory to save all the plot figures in it using mkdir command from the os module.
- I create some variables of the plot like title, x-label, y-label, and filename which extract information from the file automatically.
- Then I plot the graph.
- Now I calculate the area under the PV diagram using the simps command from scipy. integrate module.
- Print the area under the PV diagram.
- Then I calculate the power using the formula which is discussed above in the formula used section.
- I convert the fuel consumption to Kg/h and calculate Specific fuel consumption using the formula discusses above.
- Then I write the else command of the first if statement which checks the length of the file row.
PROGRAM:
#Importing libraries
import math
import matplotlib.pyplot as graph
import numpy as np
from numpy import trapz as trp
#Compatability check
try:
f=open('engine_data.out','r')
except FileNotFoundError:
print('File not recognized. Please provide a valid CONVERGE output file!')
sys.exit()
#input
file = 'engine_data.out'
count = 0
count_header = 0
column_title = []
column_units = []
for line in open(file,'r'):
if '#' not in line:
count = count + 1
columns = len(str.split(line))
else:
count_header = count_header +1
if count_header == 3:
line_content = str.split(line)
for i in range(0,len(line_content)):
column_title.append(line_content[i])
elif count_header == 4:
line_content = str.split(line)
for i in range(0,len(line_content)):
column_units.append(line_content[i])
if (count_header < 4 or count < 1 or columns < 8):
print('Invalid data file')
else:
data = np.zeros((count,columns,1))
row = 0
for line in open(file,'r'):
if '#' not in line:
column = 0
data_line = str.split(line)
while (column < columns):
data[row,column] = float(data_line[column])
column = column + 1
row = row + 1
column_number = 1
while (column_number <= columns):
graph.plot(data[:,column_number-1])
graph.ylabel(column_title[column_number]+column_units[column_number])
graph.savefig(column_title[column_number]+'.png')
graph.figure()
column_number = column_number + 1
x_column_number = 8
y_column_number = 2
x_data = np.array(data[:,x_column_number-1])
y_data = np.array(data[:,y_column_number-1])
graph.plot(x_data,y_data)
graph.xlabel(column_title[x_column_number]+column_units[x_column_number])
graph.ylabel(column_title[y_column_number]+column_units[y_column_number])
graph.savefig(column_title[y_column_number]+' VS '+column_title[x_column_number]+'.png')
area = trp(y = y_data.transpose(), x = x_data.transpose())
print('Area under pv diagram:',area,'(MJ)')
#Power in kw = Nm * RPM / 9550
RPM = 1500
power = area * 1e+06*RPM/(9550)
print('Power:',power,'(kw)')
fuel_per_stroke = 20*1e-06
fuel_per_sec = fuel_per_stroke*RPM*60
Spec_fuel_cons = fuel_per_sec/ (power * 1e+03)
print('Specific Fuel Consumption:',Spec_fuel_cons,'(g/J)')
Area under PV diagram: [0.00050059] (MJ)
Power: [78.62617185] (kW)
Specific Fuel Consumption: [2.28931405e-05] (g/J)
CONCLUSION:
- Code is written to create the visualizer tool using PYTHON which will extract data from a converged output file which Generate a plot as per the user preference.
- And also calculate the area under the P-V diagram, the power output of this engine, and Specific fuel consumption.