Challenge

Week 6 - Data analysis

#python code for Data analysis
#importing libraries
import math 
import matplotlib.pyplot as plt
import numpy as np 
from numpy import trapz

#opening file 
file_name = 'engine_data.out'

#compatibility check 
try: 
	data = open(file_name,'r')
	print('\ndata file is open sucessfully.')

except FileNotFoundError:
	print('\npls enter a valid file name.\n')

#seperating lines and columns 
line_count=1
for line in data:
	if line_count==3:
		parameter=line.split()
		columns=len(parameter)
		print('\nthis COVERAGE file contains following data which derives from IC engine simulation.')
		print('it has total %d data columns in' %(columns-1))

		for line in data:
			if line_count==4:
				unit=line.split()
			break

			for C_no in range(1,columns):
				print(C_no,line.split()[C_no],line.split()[C_no])
			break 
	line_count = line_count+1

print('\nchoose any two parameters from above.')
print('\nenter a number corresponding to parameter 1 to 17\n')
x=int(input("enter x axis parameter: "))
y=int(input("enter y axis parameter: "))

x_data=[]
y_data=[]

#code for automatic label of plots and file without nnaming it seperately 
line_count=1
for line in open(file_name,'r'):
	if line_count==3:
		X_label = line.split()[x]
		Y_label = line.split()[y]

		if line_count<=4:
			x_label = line.split()[x] + line.split()[x]
			y_label = line.split()[y] + line.split()[y]

	if '#' not in line:
		x_data.append(float(line.split()[x-1]))
		y_data.append(float(line.split()[y-1]))

	line_count = line_count+1

#ploting the data | labeling automatically | naming tittle automatically | saving the figure 
filename=Y_label+' vs '+X_label+'.png'
plt.figure()
plt.plot(x_data,y_data)
plt.xlabel(X_label)
plt.ylabel(Y_label)
plt.title(Y_label+' vs '+X_label)
plt.savefig(filename)
plt.show()

#calculations of power , workdone , fuel consumptions 
if x==8 and y==2:
	# Rotation per minute
	RPM = 1500 
	
	area = np.trapz(y_data,x_data)
	
	workdone = area*1000
	
	power = 2*math.pi*RPM*workdone/60
	
	fuel_per_stroke = 20*1e-6
	
	fuel_per_sec = fuel_per_stroke*RPM*(0.5/60)
	
	fuel_per_hour = fuel_per_sec*3600
	
	#BSFC braking specific fuel consumption
	BSFC=fuel_per_hour/power  

	print('area under the P-V diagram(workdone) :',workdone,'KJ')
	print('power output of this engine :',power,'KW')
	print('specific fuel consumption :',BSFC,'g/KWh')

Objectives :

Question

Data visualizer :

  • 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

Compatibility check :

  • 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"

Basic performance calculation :

  • 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.

Solution :

Procedures

Stepwise explaination of Data analysis code :

  1. First step is common for all as importing the libraries like import math , import matplotlib.pyplot as plt , import numpy as np
  2. Next we calls file name so while using the name in file name inn code we could use the file_name instead
  3. Next we check the compatibility by using the syntax try: data = open(file_name,’r’)| we use those to open and syntax print(file is successfully open ) and if file not open then | expect: print(pls enter a valid file name) | during these we have to take a note that respective file should be in same location where the code is saved
  4. Now our compatibility check is successful so now we have to move on to next step which is separating data and split the lines and extract the columns name and units for these we create the syntax | first we equalize the linecount=1 cause we gonna start the code from first line | next we create the nested for loop in which we state “for line in data ” now in that for loop we create the create the ” if ” condition that “if line_count==3” in which we say parameters = line.split() | then we equalise “columns = len(parameter)” which means number of columns = length of parameters
  5. Now we create another for loop in above for loop which separates the names of parameters and unit names using the column number for these we use syntax and then we use break command to stop that syntax there for a while

for line in data:

if line_count==4:

unit=line.split()

break

  1. Then we use another for loop condition in same for loop to split the columns the syntax is given below

for C_no in range(1,columns):

print(C_no,line.split()[C_no],line.split()[C_no])

break

  1. We do this all above code so that we can enter any two column number and get the plots between them like if we need plot between pressure and volume we just need to enter there column number which are 8 and 2 respectively then we get the plots of that columns with already a name labelled on it and title given to it
  2. Now moving forward we have to create a code by which we can label the plot or title name without entering that specific name and just get it by entering the column number so we create another for loop for these purpose and in that for loop we stated if condition so that we can say if line_count == 3 then it is name to be label and | then we use split command so we can get x label different and y label different and for that our syntax will be X_label = line.split()[x] | Y_label = line.split()[y]
  3. Now we need data in numbers so we have to do one trek which is that in data file the number does not includes # character we need those lines only. and for that we use the syntax “if '#' not in line:” and using same above technique we split the data between x and y and storing them using commands append ande float so our syntax we get is |x_data.append(float(line.split()[x-1])) | y_data.append(float(line.split()[y-1]))
  4. Next we have to plot those data so what we use that set of plot commands like plt.plot, plt.xlabel, plt.ylabel, plt.title, plt.savefig, plt.figure etc but as we need the name given automatically so we have to use the name x_label , y_label instead of specific names and yes we defined those labels above in for loop hence we added x_label and y_label in the plloting brackets
  5. Next we calculates the workdone , bracking specific fuel consumption and for pressure and volume columns so we create the set of syntax for that which says if column number 8 and 2 are entered (which is said to be pressure and volume respectively) and for these we have the RPM given , and area is calculated by using trapz syntax which is use to integrate along the given axis using the composites trapezoidal rule and this trapz command is lies in numpy library. And then just regular calculations using calculation formulas

if x==8 and y==2:

Rotation per minute

RPM = 1500

area = np.trapz(y_data,x_data)

workdone = area*1000

power = 2*math.pi*RPM*workdone/60

fuel_per_stroke = 20*1e-6

fuel_per_sec = fuel_per_stroke*RPM*(0.5/60)

fuel_per_hour = fuel_per_sec*3600

#BSFC braking specific fuel consumption

BSFC=fuel_per_hour/power

Results :

Below is the result we get from the code in window

data file is open sucessfully.

this COVERAGE file contains following data which derives from IC engine simulation.

it has total 17 data columns in

choose any two parameters from above.

enter a number corresponding to parameter 1 to 17

enter x axis parameter: 8

enter y axis parameter: 2

area under the P-V diagram(workdone) : 0.5005866274680075 KJ

power output of this engine : 78.63196356693915 KW

specific fuel consumption : 0.011445727146745265 g/KWh

>>>

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.