Week 6 - Data analysis
AIM: To perform data analysis using python
OBJECTIVE:
- To make data visualizer and perform the data analysis.
- To plot graph for column number which is given by user.
- To Calculate the area under the P-V diagram.
- 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.
INTRODUCTION:
Data Analysis: Data analysis involves sorting through massive amounts of unstructured information and deriving key insights from it. These insights are enormously valuable for decision-making at companies of all sizes.
We can use Python when our data analysis tasks need to be integrated with web apps or if statistics code needs to be incorporated into a production database. Being a fully fledged programming language, it's a great tool to implement algorithms for production use.
EQUATIONS:
- For power output:
`P= (W*N)/(60*1000*20) KW`
here we multiply work done per cycle with number of stroke per second.
- Specific Fuel consumption :
`SFC=` mass of fuel consumption /P (kg/kwh)
BODY OF CONTENT:
#Data analysis program
import math
import matplotlib.pyplot as plt
import numpy as np
import os
from scipy.integrate import simps
#Compatibility check
try:
f=open('engine_data.out','r')
except:
print('file not recognized.Please provide a valid CONVERGE output file')
#Reading the file and converting it to array
all_rows=[]
count=1
for line in f:
a=line.split()
all_rows.append(a)
if len(all_rows)==8670:
if'CONVERGE' not in all_rows[0][1]:
print('File not recognised as original.Please provide a valid CONVERGE output file')
else:
print('you have entered valid file')
#extracting the data of column no,label ad unit in particular array
column_no=all_rows[1]
label=all_rows[2]
unit=all_rows[3]
#deleting unwanted data from array
del(column_no[0],column_no[0],label[0],unit[0])
#extract the values of parameter and append in parameter array
crank=[]
pressure=[]
max_pres=[]
min_press=[]
mean_temp=[]
max_temp=[]
min_temp=[]
volume=[]
mass=[]
density=[]
HR=[]
HR_rate=[]
cp=[]
cv=[]
gamma=[]
Kin_vis=[]
dyn_vis=[]
data_row=all_rows[5:]
for i in range(len(data_row)):
crank.append(float(data_row[i][0]))
pressure.append(float(data_row[i][1]))
max_pres.append(float(data_row[i][2]))
min_press.append(float(data_row[i][3]))
mean_temp.append(float(data_row[i][4]))
max_temp.append(float(data_row[i][5]))
min_temp.append(float(data_row[i][6]))
volume.append(float(data_row[i][7]))
mass.append(float(data_row[i][8]))
density.append(float(data_row[i][9]))
HR.append(float(data_row[i][10]))
HR_rate.append(float(data_row[i][11]))
cp.append(float(data_row[i][12]))
cv.append(float(data_row[i][13]))
gamma.append(float(data_row[i][14]))
Kin_vis.append(float(data_row[i][15]))
dyn_vis.append(float(data_row[i][16]))
data=[crank,pressure,max_pres,min_press,mean_temp,max_temp,min_temp,volume,mass,density,HR,HR_rate,cp,cv,gamma,Kin_vis,dyn_vis]
#printing information of file
print('column Name and column No in file as below: ')
for j in range(len(column_no)):
print(label[j],'=',j+1)
#take the inputs from user
x = input('Enter the column no for X-axis=')
try:
x=int(x)
if x not in list(range(1,17)):
print('ERROR: Enter the number bewteen 1 to 17')
except:
print('Error: Valid number (Accepted value :1 to 17)')
x=x-1
print(label[x])
y=input('Enter the column no for Y-axis= ')
try:
y=int(y)
if y not in list(range(1,17)):
print('ERROR: Enter the number bewteen 1 to 17')
except:
print('ERROR: Valid number(Accepted value:1 to 17)')
y=y-1
print(label[y])
#making the directory for saving plots
path='F:\python files\data analysis\plot '
print(path)
try:
os.mkdir(path)
print("Directory" , path , " created ")
except FileExistsError:
print("Directory" , path , " already exsists ")
#plotting graphs
title=label[y]+unit[y]+' '+'VS'+' '+label[x]+unit[x]
x_label=label[x]+unit[x]
y_label=label[y]+unit[y]
filename=label[y]+' '+'VS'+' '+label[x]
plt.plot(data[x],data[y])
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.title(title)
plt.savefig('plot/{}'.format(filename))
#calculate area under the P-V diagram which gives the workdone
area=simps(data[1],data[7])*pow(10,6)
print('Area under P-V diagram =' ,area)
#calculate power output for single and fourstroke engine
n_c=1
N=1500
p=area*N/(1000*2*60)
print('power output in kW=',p)
#calculate specific fuel consumption
fs=20*pow(10,-9)*60*(N/2)
sfc=fs/p
print('specifi Fuel consumption in Kg/Kwh=',sfc)
else:
print('File not recognised as original since there are some data line missing.Please provide a valid CONVERGE output file ')
EXPLANATION:
- We start with importing required modules like math,numpy,scipy,os and matplotlib library.
- Then we further check if the input file is converge or not ,to check it we use try and except statement.So it gives error if the required file is not given as input.
- Now we create empty array and then use for loop to extract the line and split the line and append the values in empty array.
- Now we check the input file is converge or not by checking no. of rows equal to 8670 or not ,for this we use if else statement.
- If the file is converged then whole program run's and the else command gives us an indication that the file is valid.
- In this else command ,we first extract the column no,column label and unit of parameter arraysand save them in particular variable.
- Then we delete unwanted elements from these three arrays.
- Now the file has 17 reamining columns with different parameters , so for each one we craete an empty array.
- Then we store all the arrays in one array.
- Then we print information like which column contains which parameter.So, user can give the input easily from the displayed infromation.
- Now , we take the inputs for x axis and y axis column no. and check if the user gives right input column no. for plotting graph.
- Now we create the directory to save all the plot figure in it using mkdir command form os module.
- Now we create variables for plots like title, x-label,y label and filename,which extracts information from file automatically.
- Then we plot the graphs.
- Now we calculate the area under the PV diagram using simps command from scipy.integrate module.
- Then we Print the area under PV diagram.
- Then we calculate the power using the formula as mentioned above in the Equation section.
- Then we convert the fuel consumption to kg/h and calculate specific fuel consumption.
- Then we write the else command to the If statement which checks the length of the rows in the file.
RESULTS:
- Compatibility check screen shot
so if we provide improper file i.e the file required by the program for analysis is not given then the program will give this output.
- PLOTS
1. Pressure vs crank
2. Pressure vs Volume
3. Volume vs crank
4. Mass vs crank
5. Mean tempertaure vs crank
OUTPUT:
So in the output we get Area under P-V diagram, power output ,specific Fuel consumption.
- Screenshot of folder containing images of plot