PYTHON - Data analysis
Data analysis
AIM:
To write a python script that tells parameters of engine and plot graph, find area under PV graph, power specific file consumption by reading the given file datas.
PROBLEM STATEMENT:
- From the given file engine_data.out we have to extract datas of various parameters
- Display the parameters and the respective code
- Asking user for dependent and independent parameters, for which the plot has to be plotted and saved:
- Finding area under the curve, power and specific fuel consumption
- The code should say file not recognised, if its not in the directory
Sublime text 3 doesn't support input(), so sublime REPL installed.
FORMULA
Work done = PdV or VdP = Area under PV graph
Power = Force x distance / time
Specific Fuel consumption = mass flow rate of fuel / Brake horse power
CODE WITH EXPLANATION:
- Import modules (math-Mathematical functons like sin, cos etc; matplotlib.pyplot-plots; numpy-linspace)
- The try block lets you test a block of code for errors, and the except block lets you handle the error. Using this we can open the file in read mode in try: block and if it failed it redirects to except: block where we can notify user.
- Using for loop to read the line by line and if it encounters # it leaves line otherwise seperated it by space and store each element in value array.
- This is transposed as per convinence and converted to float type.
- Parameters name shold be saved and printed (No restriction given to apply same name as in file) and respective code also saved and printed.
- Ask user for dependent and independent variable code and store it in x and y using input().
- With these two parameters plot the graph, save it with parameter name and display.
- Save pressure values and volume values seperately.
- Using numpy.trapez(), which used for integral calculation, find the area under the graph by passing pressure and volume as arguments.
- From that find the power.
- Using the formula find the specific fuel consumption.
CODE with necessary explanations in comments:
#DATA ANALYSIS
import matplotlib.pyplot as plt
import math
import numpy as np
#Compatablity check
try:
data=open('engine_data.out','r')
except FileNotFoundError:
print('File not recognised')
#storing all datas in single array
val=[]
for line in data:
if '#' not in line:
val.append(line.split( ))
#transposing matrix
values=np.transpose(val)
#changing string to float type
values=values.astype(float)
print('PARAMETERS:')
parameter=['Crank Angle','Pressure','Max Pressure','Min Pressure','Mean Temperature','Max Temperature','Min Temperature','Volume','Mass','Density','Integrated Heat Rejection','Heat Rejection Rate','Cp','Cv','Gamma','Kinimatic Viscosity','Dynamic Viscosity']
print('\n')
print(parameter)
print('\n')
print("Codes for corresponding parameters:")
print('\n')
para_num=['Crank Angle=0','Pressure=1','Max Pressure=2','Min Pressure=3','Mean Temperature=4','Max Temperature=5','Min Temperature=6','Volume=7','Mass=8','Density=9','Integrated Heat Rejection=10','Heat Rejection Rate=11','Cp=12','Cv=13','Gamma=14','Kinimatic Viscosity=15','Dynamic Viscosity=16']
for i in range(0,len(para_num)-1):
print(para_num[i])
print('\n')
x=int(input("Enter code number of independent variable:"))
y=int(input("Enter code number of dependent variable:"))
#plotting begins
plt.plot(values[x,:],values[y,:])
#labelling axes
plt.xlabel(parameter[x])
plt.ylabel(parameter[y])
#title
plt.title('%s vs %s'%(parameter[x],parameter[y]))
#saving plot
plt.savefig('%s vs %s.png'%(parameter[x],parameter[y]))
#display plot
plt.show()
#saving volumes separately
vol=values[7,:]
#saving pressures separately and converting them to pascals
pres=values[1,:]*pow(10,6)
#area under the curve is work done(pdv), integral of that curve gives area
#trapz used for integration
area = np.trapz(pres,vol)
print("Area under PV graph = Work done = %f Joules"%area)
rpm = 1500
rps = rpm/60
power = area*rps/1000
print("Power = %f kW"%power)
#(mass flow rate=(fuel per stroke * no of time injected per s * density(=0.735, diesel)))
#assumed four stroke, so rpm/2 times injection happens
mf=20*pow(10,-6)*(rps/2)*0.735
#Specific fuel consumption= mass flow / power
sfc=mf*3600/power
print("SFC = %f k/kWh"%sfc)
RESULTS:
OUTPUT:
BASIC PERFORMANCE VALUES
PLOT:
DATA VISUALIZER
COMPATABLITY CHECK:
CAMPATABLITY CHECKED:
To check this, in the code, instead of engine_data.out, engine data. out was given, for that the output was (NOTE THE OUTPUT/COMMAND screen at the bottom):
CONCLUSION:
The code perfectlt worked, and the plots asked by the user in terms of codes were plotted successfully and saved with the name of parameter itself.
If error in opening file, warning shown.
Values of area under PV graph, power and SFC calculated.
SFC inversely proportinal to power.