Challenge

Week 6 - Data analysis

Objective: To Parse the data from the given file and use the data to make plots and perform calculations

Problem Statement:

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 for 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

You code should exit gracefully, if a non-compatible file is provided as an input. It should say something like "File not recognized. Please provide a valid CONVERGE output file"

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 micro grams of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.

Formulae:

By rearranging the terms we can find the value of bsfc.

Program:

import matplotlib.pyplot as plt 
import numpy as np
from prettytable import PrettyTable

# Variables Created to store the data for each property
Crank = []
Pressure = []
Max_Pres = []
Min_Pres = []
Mean_Temp = []
Max_Temp = []
Min_Temp = []
Volume = []
Mass = []
Density = []
Integrated_HR = [] 
HR_Rate = []
C_p = []
C_v = []
Gamma = []
Kin_Visc = []
Dyn_Visc= []

# Verifying if the correct filename is entered by the user
verify = input('Enter the Filename: ')
if verify == ('engine_data.out'):
		line_count = 1
		content = []
		units = []
		# Obtaining all the properties
		f = open("engine_data.out",'r')
		for i in range(3):
			info = f.readline().split()
		content = info
		content.pop(0)
		# Obtaining all the units
		unit = f.readline().split()
		units = unit
		
		# For loop used to add all the numerical data to their corresponding properties
		for line in open('engine_data.out'):
				if '#' not in line:
					Crank.append(float(line.split()[0]))
					Pressure.append(float(line.split()[1]))
					Max_Pres.append(float(line.split()[2]))
					Min_Pres.append(float(line.split()[3]))
					Mean_Temp.append(float(line.split()[4]))
					Max_Temp.append(float(line.split()[5]))
					Min_Temp.append(float(line.split()[6]))
					Volume.append(float(line.split()[7]))
					Mass.append(float(line.split()[8]))
					Density.append(float(line.split()[9]))
					Integrated_HR.append(float(line.split()[10]))
					HR_Rate.append(float(line.split()[11]))
					C_p.append(float(line.split()[12]))
					C_v.append(float(line.split()[13]))
					Gamma.append(float(line.split()[14]))
					Kin_Visc.append(float(line.split()[15]))
					Dyn_Visc.append(float(line.split()[16]))
		# Creating a table to display the information of all the properties
		x = PrettyTable()

		x.field_names = ["Sno", "Property",]
		for k in range(len(content)):
			x.add_row([k+1,content[k]])
		print(x)
		# Recieving inputs from the user to plot graphs and storing in the current folder
		prop = [Crank,Pressure,Max_Pres,Min_Pres,Mean_Temp,Max_Temp,Min_Temp,Volume,Mass,Density,Integrated_HR,HR_Rate,C_p,C_v,Gamma,Kin_Visc,Dyn_Visc]
		input_1 = int(input('Enter the Sno for first property: '))
		input_2 = int(input('Enter the Sno for second property: '))
		# Area under curve can be obtained by using np.trapz() function
		area = np.trapz(prop[1],prop[7])
		work = area * 1e+06
		print('Area under the curve gives work i.e work =',work,'Joules')
		rpm=1500
		rps = rpm/60
		# Calculate Torque
		torque = (work)/(2*3.14*2)
		# Calculate Power
		power = (2*3.14*1500*torque)/(60*1000)
		print('Power = ',power,'KW')
		# Calculate Specific Fuel Consumption
		bsfc = ((20*pow(10,-9)*0.08)*2*60*3.6*pow(10,9))/(power*rpm) 
		print('specific fuel consumption = ',bsfc,'g/KW.h')

		# Plotting the data
		x_label = content[input_1-1]
		y_label = content[input_2-1]
		v_s = 'Vs'
		plt.figure()
		plt.plot(prop[input_1-1],prop[input_2-1])
		plt.title(str(x_label) + ' ' +str(v_s) +' '+ str(y_label))
		plt.xlabel(str(x_label)+' '+str(units[input_1]))
		plt.ylabel(str(y_label)+' '+str(units[input_2]))
		plt.savefig(str(x_label) + ' ' +str(v_s) +' '+ str(y_label)+'.png')
		plt.show()
else:
		print('File not recognized. Please provide a valid CONVERGE output file')

	

Procedure:

  1. First, we mention all the libraries which are necessary through the course while writing this program and then create variables of all the properties.
  2. Then we take the input from the user for the filename and if it matches then we proceed further else there's an error message display and the program has to be re-run.
  3. All the names of the properties are obtained from the corresponding line and similarly for the units.
  4. Then, For loop used to add all the numerical data to their corresponding properties.
  5. We create a table to display the information of all the properties.
  6. We recieve inputs from the user to plot graphs and storing in the current folder.
  7. Area under curve can be obtained by using np.trapz() function.
  8. Then we calculate work, torque, power, and the specific fuel consumption and then display those values.
  9. In the end we plot the data and provide the necessary labels and titles also saving them in the current folder.

Result:

This is the plot of Pressure Vs Volume.

Here I have displayed how the program works first taking the input of the filename and then by choosing the properties the graph is generated and the necessary calculations are showcased.

This is the folder containing all the plots which were created after running the program.

Conclusion:

The plots are successfully generated and the results required are obtained by the user.

References:

https://www.geeksforgeeks.org/numpy-trapz-function-python/

http://zetcode.com/python/prettytable/

Project Link:

https://skill-lync.com/projects/data-analysis-by-python-3

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.

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.