Challenge

Week 6 - Data analysis

This code takes takes the data from a user specified CONVERGE format file. It also checks the file for the correct format i.e. compatibility. If the file is not a CONVERGE output data format then the code will throw an error statement "This is not a valid CONVERGE output file.". Also, if the file format is correct but, there is no data present in the file then the code will prompt the user "No data present in the file".

If the format is correct and data is present then the code will implement following steps:-

1) Data extraction - Extracting data along with the column name and column number from the CONVERGE output file. Complete data from the file is stored in a two dimensional array 'data' The columns of data matrix/2D array contains the data for each individual parameter.

2) Data visualizer - Provide an "Input map" to user so that he/she can use numbers as an input to designate the variables to be ploted. Ask user if plot between any two variables is required. If input is 'y' representing 'yes' then, ask for the numbers representing the variables to be ploted (Refering input map). Ask the user if the plot is to be saved. Then, show the plot. Above step is under while loop and runs untill user gives an input 'n' to the question "Do you want to plot any other variable? (y/n): ".

3) Basic performance - Extract volume and pressure data from matrix 'data' and store in array 'volume' and 'pressure' respectively. Next, numerically integrate pressure and volume data using 'simps' function (Function for numerical integration by Simphson's rule) from scipy.integrate library. This integration is area under the P-V curve i.e. work done per cycle. Then, RPM of the engine at which the data was collected and fuel consumption in microgram per cycle is taken as user input. Next, the power produced by the engine at this RPM is calculated. Finally, the Specific fuel consumption is calculated and Area under the curve, Power output and Specific fuel consumption are displayed.

A=Area under the P-V curve=Work done per cycle (MJ)A=Area under the P-V curve=Work done per cycle (MJ)

Power output =A⋅n⋅106 WPower output =A⋅n⋅106 W

n=Number of cycles per secondn=Number of cycles per second

n=N2⋅60n=N2⋅60` for 4-stroke engine

n=N60n=N60 for 2-stroke engine

N = Rotations per min.N = Rotations per min.

SFC(Specific Fuel consumption)=Fuel consumed in kg/hrPower output in kWSFC(Specific Fuel consumption)=Fuel consumed in kg/hrPower output in kW

Code -

"""
Data analysis of
engine data
"""
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import simps


# File name as an input from user

filename = input("nEnter the name of CONVERGE output filename with the extension(.out): ")

#================================================================================================
# Compatability check

# Checking if the file is a valid CONVERGE output file
try:
    f = open(filename)
    f.close()
except FileNotFoundError:
   	print('nThis is not a valid CONVERGE output file.n')
   	exit()

# Checking if data is present in the CONVERGE output file
i=0 	
for line in open(filename): 
	if '#' not in line:
		break
	i = i+1
	
j=0
for line in open(filename):
	j=j+1		
	if j>i:
		break
if j==i:		
	print('nNo data present in the file.n')
	exit()


#================================================================================================
# Data extraction

# Getting number of columns i.e. number of variables
i=0
for line in open(filename):
	if i==2:
		c = line.split()                         # Storing unit of variables in list c
		i = 0
		break
	if i==1:
		b=line.split()                           # Storing names of variables in list b
		i = i + 1
	if 'column' in line.split():
		a = line.split()                         # Storing column numbers of variables in list a
		i=1

a.remove("column")                               # Removing column from list a 
a.remove("#")                                    # Removing # from list a 
a = np.array(a, dtype=np.int)                    # Converting data to integer
b.remove("#")                                    # Removing # from list b
c.remove("#")                                    # Removing # from list c
num_col = len(a)                                 # Number of columns/Number of variables
#-------------------------------------------------------------------------------------------------
# Getting Data
num_data = 0                                     # Initializing Number of data points counter
data = np.arange(0,num_col)
for line in open(filename):
	if '#' not in line: 
		d = line.split()                         # Storing current data line as a list in d
		d = np.array(d, dtype=np.float32)        # Converting list data to float array
		data = np.vstack([data,d])               # Stacking the float array d in data matrix  
		num_data = num_data + 1	
data = np.delete(data,0,0)                       # Deleting the first row used to initilize the array		



#=================================================================================================
# Data visualizer

print('n=============================================================================')
print('nnInput map ')
for i in a:
	print('n{} for {}'.format(i,b[i-1]))
x='n'
print('n=============================================================================')
x = input("nnDo you want a plot between any two vriables? (y/n): ")	
while(x.lower()=='y'):
	cn1 = input("nEnter the column number of the variable along x-axis(Refer Input map above): ")
	cn2 = input("nEnter the column number of the variable along y-axis(Refer Input map above): ")
	cn1 = int(cn1)
	cn2 = int(cn2)
	plt.figure()
	plt.plot(data[:,cn1-1],data[:,cn2-1])
	plt.xlabel('{} {}'.format(b[cn1-1],c[cn1-1]))
	plt.ylabel('{} {} '.format(b[cn2-1],c[cn2-1]))
	plt.title('{} {} vs {} {}'.format(b[cn2-1],c[cn2-1],b[cn1-1],c[cn1-1]))
	z = "n"
	z = input("nDo you want to save the plot? (y/n):" )
	if(z.lower()=='y'):
		plt.savefig("{}-{}.png".format(b[cn2-1],b[cn1-1]))
	plt.show()
	print('n==========================================================')
	x = input("nnDo you want to plot any other two variables? (y/n): ")
print('n========================================================n')

x='n'
x = input("nnDo you want a single vriable plot? (y/n): ")	
while(x.lower()=='y'):
	cn = input("nEnter the column number of the variable (Refer Input map above): ")
	cn = int(cn)
	plt.figure()
	plt.plot(np.arange(0,num_data),data[:,cn-1])
	plt.xlabel('Data point number')
	plt.ylabel('{} {} '.format(b[cn-1],c[cn-1]))
	z = "n"
	z = input("nDo you want to save the plot? (y/n):" )
	if(z.lower()=='y'):
		plt.savefig("{}.png".format(b[cn-1]))
	plt.show()
	print('n==========================================================')
	x = input("nnDo you want any other single variable plot? (y/n): ")
print('n=======================================================')

#=================================================================================================
# Basic performance calculation

print('nPERFORMANCE OF ENGINE')
b1 = [i.lower() for i in b]
pressure_column = b1.index('pressure')
volume_column = b1.index('volume')
pressure = data[:,pressure_column]
volume = data[:,volume_column]
A = 0                        # Area under the P-V curve
A = simps(pressure,volume,even='first') * np.power(10,6) # = Work per cycle (J)
RPM = input('nEnter the RPM of engine at which data was collected: ') # Rotations per min.
f = input('nEnter the value of fuel consumed per cycle in micro-grams: ') # fuel consumtion  micro-gram per cycle
RPM = float(RPM)
f = float(f)
n = RPM/(2*60)                # Number of cycles per second for 4-stroke engine
P = A * n                     # Power output (W) at speed N rad/s
f = (f*n*3600)/np.power(10,9) # fuel consumtion kg per hr
sfc = (f*1000)/P # Specific fuel consumtion (kg per hr/kW)
print('nArea under the P-V curve i.e. work done per cycle = {} J'.format(A))
print('nPower output at {} RPM = {} W'.format(RPM,P))
print('nSpecific fuel consumption = {} kg/kW.hr'.format(sfc))

Some plots:-

Basic Performance:-

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.