Challenge

Week 6 - Data analysis

Aim:

To write a script that does Data Analysis on python.

Objective:

  1. To calculate basic engine performance.
  2. Visualizing the data, i.e., to plot a graph between any two quantities according to the user's input.
  3. Compatibility check, i.e., code should exit gracefully, if a non-compatible file is provided as an input.

Introduction:

Data analysis is a process of inspecting, cleansing, transforming, and modeling data with the goal of discovering useful information, informing conclusions, and supporting decision-making. Data analysis has multiple facets and approaches, encompassing diverse techniques under a variety of names, and is used in different business, science, and social science domains. In today's business world, data analysis plays a role in making decisions more scientific and helping businesses operate more effectively.

Data analysis is a somewhat abstract concept to understand without the help of examples. So, to better illustrate how and why data analysis is important for businesses, here are the 4 types of analysis and examples of each.

  • Descriptive data analysis looks at past data and tells what happened. This is often used when tracking key performance indicators (KPIs), revenue, sales leads, and many more...
  • Diagnostic data analysis aims to determine why something happened. Once your descriptive analysis shows that something negative or positive happened, diagnostic analysis can be done to figure out the reason. A business may see that leads to an increase in the month of October and use diagnostic analysis to determine which marketing efforts contributed the most.
  • Predictive data analysis predicts what is likely to happen in the future. In this type of research, trends are derived from past data which are then used to form predictions about the future. For example, to predict next year's revenue, data from previous years will be analyzed. If the revenue has gone up 20% every year for many years, we would predict that revenue for next year will be 20% higher than this year. This is a simple example, but predictive data analysis can be applied to much more complicated issues such as risk assessment, sales forecasting, or qualifying leads.
  • Prescriptive data analysis combines the information found from the previous 3 types of data analysis and from a plan of action for the organization to face the issue or decision. This is where data-driven choices are made.

With this 4 data analysis, any issue related to data can be applied and can draw to certain decisions. And in the era of the internet, raw data can be found for almost everything.

Governing Equations:

Since engine data files are given and consist of various parameters for the engine. The following things can be calculated as follows...

  • Work-done by the engine, which can be found out by plotting a P-V diagram of the engine and calculating the area under the curve.
  • The power output of the engine generally called as an engine brake power and can be found as,

`Power = (W_(out)/60*(RPM)/(1000)*n/2)` in `KW`, where `n` is the no. of a cylinder of the engine, `RPM` is the engine speed in minutes, and here it is divided by 60 to convert the min in second and here divided by 2 because the given engine is a 4 stroke engine.

  • Specific fuel consumption `( SFC)` = `(FC)/(Power` where `FC` is the amount of fuel consumed by the engine in `(kg)/(hr)`.

Input: `RPM`= 1500, Fuel consumed per stroke = `20`micro grams.

Procedure:

  1. Initially required modules are imported viz., math, matplotlib, numpy, scipy for their various functions.
  2. For checking compatibility of the program, 'try' and 'except' command is used according to their syntax, and the given data file is opened using 'open' command within a for a loop. If the file name is misspelled or is wrong, the code will yield error with the following message " File not recognized. Please provide a valid CONVERGE output file " and will not execute and exit.
  3. For the purpose of plotting curve between two quantities according to user input, first, a line counter is initiated from 1 and null arrays of volume and pressure and data are created for further uses. Now an 'if' loop is iterated with the condition that if '#' is not in line, all the numerical values present in the data file are now to be stored in the data array as a string (by default).
  4. Now another 'if' loop is executed within the previous 'if' loop and the condition that if the line counter is greater than or equal to 6 all the data will be stored in pressure and volume array respectively. And we do plot the P-V diagram using the pressure and volume array accordingly.
  5. For calculating engine performance, the first area under the P-V curve is calculated and this can be done either by the Trapezoidal method or Simpson method. The calculation is shown in the code by entering the required inputs and the necessary formulas.
  6. Two arrays of names of the parameters and their units are made according to data file given and then the user has to choose from them to plot respective graphs. For this, the two inputs of a user are taken using 'input' command in python 3 and its datatype is set as 'int' and plotted using 'plt.plot' command. Basically user is entering the column number as input and accordingly result will be generated.

Python Code:

# A program to calculate engine parameters and to plot graph between two quantities according to user input
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import simps

# Compatability check
try:
	open('engine_data.out')

	line_count = 1

	# Creating of empty arrays to store the value of volume and pressure respectively
	volume = []
	pressure = []

	# volume and pressure in the data file is present in column 8 and 2 respectively. Calling them and storing their data into arrays
	v = 8
	p = 2

	# Creating a null array and storing all the values of data file into it

	data = []

	for line in open('engine_data.out'):
		if '#' not in line:
			data.append(line.split())
			if line_count >= 6:
				volume.append(float(line.split()[v-1]))
				pressure.append(float(line.split()[p-1]))
		line_count = line_count + 1

	# Plotting p-v diagram
	plt.plot(volume, pressure, color = 'maroon', linewidth = 1)
	plt.xlabel('Volume (m^3)', color = '')
	plt.ylabel('Pressure (MPa)', color = 'green')
	plt.title('p-v diagram of engine', color = 'blue')
	plt.grid()
	plt.savefig('p-v diagram of engine')
	plt.show()

	# Calculation of area under PV diagram using the simpsons rule

	area = simps(pressure, volume)
	print('Area under the curve using Simpsons Rule : ' + str(area) + 'units')
	print('Work done by the engine :' + str(area) + 'MJ')
	#area = area*1e6
	#print('Work done by the engine :' + str(area) + 'J')

	#Inputs
	#Speed of engine (RPM)
	rpm = 1500
	rps = 1500/60
	stroke_cycle = rps/2

	#Fuel consumption in micro grams per 1 stroke cycle
	fuel_consumption = 20
	
	#Engine Specifications
	#Number of cylinder = 
	n = 1
	# 4 stroke engine

	# Calculation of Power output and specific fuel consumption

	power_output = n*area*stroke_cycle/(1000)
	print('Power output from engine :' + str(power_output) + 'KW')
	SFC = (fuel_consumption*pow(10,-6)*stroke_cycle*3600)/(power_output*1000)
	print('Specific Fuel Consumption, SFC = ' + str(SFC) + ' (kg/hr)/KW')
	print('t')

	# Calculation of area under PV diagram using the trapezoidal rule and work done, power sfc
	Area = np.trapz(pressure, volume)
	print('Area under the curve using trapezoidal rule :' + str(Area) + 'units')
	print('Work done by the engine :' + str(Area) + 'MJ')
	Area = Area*1e6
	print('Work done by the engine :' + str(Area) + 'J')
	power_output = n*Area*stroke_cycle/(1000)
	print('Power output from engine :' + str(power_output) + 'KW')
	SFC = (fuel_consumption*pow(10,-6)*stroke_cycle*3600)/(power_output*1000)
	print('Specific Fuel Consumption, SFC = ' + str(SFC) +'(kg/hr)/KW')
	print('t')

	# Transposing columns into row matrix and converting them into float type from string type
	transpose = np.transpose(data)
	float_f = transpose.astype(np.float)

	# Array for header and their units
	parameters = ['Crank','Pressure','Maximum Pressure','Minimum Pressure','Mean Temperature','Maximum Temperature','Minimum Temperature','volume','Mass','Density','Integrated HR','HR Rate','Cp','Cv','Gamma','Kinematic Viscosity','Dynamic Viscosity']
	units = ['(DEG)','(MPa)','(MPa)','(MPa)','(K)','(K)','(K)','(m^3)','(kg)','(kg/m^3)','(J)','(J/time)','(J/kgK)','(J/kgK)','','(m^2/s)','(Ns/m^2)']

	#Providing user choice of picking two parameters for plotting
	print('Choose from the following options and press enter key :')
	print('1 Crank \n 2 Pressure \n 3 Maximum Pressure \n 4 Minimum Pressure \n 5 Mean Temperature \n 6 Maximum Temperature \n 7 minimum Temperature \n 8 volume \n 9 Mass \n 10 Density \n 11 Integrated HR \n 12 HR Rate \n 13 Cp \n 14 Cv \n 15 gamma \n 16 Kinematic Viscosity \n 17 Dynamic Viscosity')
	X =int(input('Enter your choice on X-axis : \n'))
	
	Y =int(input('Enter your choice on Y-axis : \n'))
	

	# Plotting the graph based on user choice 
	plt.figure(1)
	plt.plot(float_f[X-1],float_f[Y-1],linewidth = 3, color = 'blue')
	plt.title('Plot for' + parameters[X-1] + 'versus' + parameters[Y-1],color = 'blue')
	plt.xlabel(parameters[X-1] + ' ' + units[X-1])
	plt.ylabel(parameters[Y-1] + ' ' + units[Y-1])
	plt.savefig(parameters[X-1] + ' vs' + parameters[Y-1] + '.png')
	plt.grid()
	plt.show()

except:
	print('File not recognized. Please provide a valid CONVERGE output file')

Result:

When we do execute the above program initially, we do get the P-V diagram,

But if we do enter the wrong file name or different files, then the following message is displayed in the console.

The following plots were observed:

Screenshot of the folder was output is saved automatically:

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.