Challenge

Week 6 - Data analysis

AIM :-

A data visualizer tool need to be created to read the data from the file.  Python code has been written to extract and perform analysis with that data. The existing data file contains different columns with parameters such as pressure, volume, specific heat, crank angle, etc. Plots need to be automatically generated between the desired parameters with the names specifying on the plots. Performance calculation such as power, specific fuel consumption and work done by the engine need to be calculated from available data from the data file.

PYTHON CODE :-

#Data Analysis

#Importing Libraries
import matplotlib.pyplot as plt
import math
import numpy as np
from numpy import trapz as trp

#Compatibility Check
try: 
	data = open('engine_data.out','r')
except FileNotFoundError:
	print(' "File not recognized. Please provide a valid CONVERGE output file"')

datafile = 'engine_data.out' 
count_row = 0
count_column = 0
count_header =0
column_title = []
column_unit = []

#Code to read the data, parameters and its units
for line in open(datafile,'r'):
	if '#' not in line:
		count_row = count_row +1
		count_column = len(str.split(line))
	else:
		count_header = count_header + 1
		if count_header == 3:
			line_content = str.split(line)
			for i in range(1,len(line_content)):
				column_title.append(line_content[i])
		elif count_header ==4:
			line_content = str.split(line)
			for j in range(1,len(line_content)):
				column_unit.append(line_content[j])


#To print the parameters with its corresponding serial number
for i in range(0,count_column):
	print(column_title[i],i+1)


x=input("Please give required x axis data coloumn number (1-17) 1-Crank to 17-Dynamic Viscosity ")
nx=int(x)
print('Parameter choosen on X-axis is ',column_title[nx-1])
y=input("Please give required y axis data coloumn number (1-17) 1-Crank to 17-Dynamic Viscosity ")
ny=int(y)
print('Parameter choosen on Y-axis is ',column_title[ny-1])

print("The Graph you requested is for ",column_title[ny-1],"vs",column_title[nx-1])
data_array_x=[]
data_array_y=[]

for i in open(datafile,'r'):
	if '#' not in i:
		data_array_x.append(float(i.split()[nx-1]))


for j in open(datafile,'r'):
	if '#' not in j:
		data_array_y.append(float(j.split()[ny-1]))


#Plotting and Saving
plt.plot(data_array_x,data_array_y)
plt.xlabel(column_title[nx-1]+column_unit[nx-1])
plt.ylabel(column_title[ny-1]+column_unit[ny-1])
plt.savefig(column_title[ny-1] +' VS '+ column_title[nx-1]+'.png')
plt.show()

#Area under the given PV Diagram
volume = []
pressure = []
for line in open(datafile,'r'):
	if '#' not in line:
		volume.append(float(line.split()[7]))
		pressure.append(float(line.split()[1]))
area = np.trapz(pressure,volume)
print('Area under the PV curve is ',area)

#Calculating Power Output of the engine
rpm = 1500
power = area*1e+06*rpm/9550
print('The power output of the engine is ',power,'KW')

#Calculating specific fuel consumption of engine
Fuel_per_stroke = 20*1e-06
Mass_Flowrate=2.99*1e-10 #mass flow rate in grams per second
Specific_fuel_consumption = Mass_Flowrate/ (power) 
print('The Specific Fuel Consumption is',Specific_fuel_consumption,'g/KW')

Code Explanation :-

  • All the required libraries have been imported and compatibility check has been done using try and except functions
  • A for loop has been written to calculate the number of rows and columns with data. Using str.split the string has been split. The names of the parameter and its units were saved in the arrays
  • Using plot function the graphs between the required two parameters have been plotted and saved using savefig function
  • Using trapz library the area under the PV diagram have been calculated. Specific fuel consumption and power developed from the engine were also determined using the formulae.

PLOTS :-

a) Pressure Vs Crank angle

b) Pressure Vs Volume

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.