Week 6 - Data analysis
1.AIM: Data analysis in python
2.OBJECTIVE: 1.Data visualization
- Compatibility check
- Basic performance calculation
3.BODY OF THE CONTENT:
MAIN CODE:
#This code is written in python
#File parsing in python
#Engine data analysis in python
import math
import matplotlib.pyplot as plt
import numpy as np
from numpy import trapz
#Compatability check
try:
data = open('engine_data.out','r')
except FileNotFoundError:
print('File not regonized, please put a valid file')
#Data visualizer
#Initialisation
value = []
#reading the data file
for line in data:
if '#' not in line:
value.append(line.split())
#Value array is transposed
column = np.transpose(value)
#column array is cast into float
column = column.astype(float)
#Header of all the columns of the data file
parameters = ['Crank angle','Pressure','Maximum pressure','Minimum pressure','Mean temperature','Maximum pressure','Minimum temperature','Volume','Mass','Density','Heat rejection','Heat rejection rate','C_p','C_v','gamma','Kinematic viscosity','Dynamic viscosity']
#Instructions
print('The numerical code for the corresponding parameters are given below.\n Please follow the instructions')
print(parameters)
#Numerical code for the columns
numerical_code = ['Crank angle = 0','Pressure=1','Maximum pressure=2','Minimum pressure=3','Mean temperature=4','Maximum temperature=5','Minimum temperature=6','Volume=7','Mass=8','Density=9','Heat rejection=10','Heat rejection rate=11','C_p=12','C_v=13','gamma=14','Kinematic viscosity=15','Dynamic viscosity=16']
print(numerical_code)
#Inputs by the user
a = int(input('Please enter the numerical code for the independent variable: \n'))
b = int(input('Please enter the numerical code for the dependent variable: \n'))
#Plotting between the column
plt.plot(column[a,:],column[b,:],linewidth=3,color='green')
#Axes are named
plt.xlabel(parameters[a],color='red')
plt.ylabel(parameters[b],color='red')
#Title for the plot
plt.title(parameters[b]+'VS'+parameters[a])
plt.grid('true')
#Plot is saved in the current directory
plt.savefig(parameters[b]+'VS'+parameters[a]+'.png')
plt.show()
#Basic performance calculation
#Area under PV diagram
data = open('engine_data.out','r')
volume = []
pressure = []
for i in data:
if '#' not in i:
pressure.append(float(i.split()[1]))
volume.append(float(i.split()[7]))
#Calculation of area
area = np.trapz(pressure,volume)
print('Total area under the PV diagram is:',area)
#Calculation of power output of the engine
#Inputs
rpm = 1500
#P = W*N*10^6/9550
power = area*rpm*1e+06/(9550)
print('The power output of the engine in Kilowatt is:',power)
#Calculation of specific fuel consumption
fuel_per_stroke = 20*1e-06
mass_flow_rate = 2.99*1e-10
specific_fuel_consumption = mass_flow_rate/power
print('The specific fuel consumption by the engine in gram per Kilowatt is:',specific_fuel_consumption)
EXPLANATION:
Compatibility check and data visualization:
- Importing the necessary libraries
- Then I have checked the compatibility by using try except command.
- Then I have started a for loop which will read the data inside the file.
- By using if command I have neglected the header line containing ‘#’.
- Then I have transform the rows into columns for calculations purposes.
- Then I have defined the parameters of the data file and also I have assigned the numerical code for each parameters.
- Then I took the input from the user.
- Then I have plotted the necessary graphs as the user will put the input.
Basic calculation:
- I have taken two empty arrays as volume and pressure for extraction of volume and pressure data.
- Then I have started a for loop which will extract the data and stores in the array.
- Then I have calculated the area under PV diagram by using trapz library.
- Again by using formula I have calculated the power output of the engine.
- By using formula I have calculated the specific fuel consumption by the engine.
4.OUTPUT:
COMPATIBILITY CHECK:
DATA VISUALIZATION:
I have plotted different plots by using different numerical codes:
BASIC PERFORMANCE CALCULATION:
5.ERRORS AND DEBUGGING: I have made some mistakes in the syntax.