Challenge

Week 6 - Data analysis

Aim:

The aim of this assignment is to perform file parsing on the given thermodynamic data file.

Input Data File:

Thermodynamic data file: https://drive.google.com/file/d/1L8GY56d-M8mB1KfceM-xVhGNvnCIqxjm/view

Objective:

  • To perform a compatibility check on the thermodynamic data file that will be used for the file parsing process.
  • To output a plot between any two given parameters in the thermodynamic data file and save the plot with a fuel name that is based on the parameters.
  • To calculate the area under the graph for the parameters of pressure and volume.
  • To calculate the power output of the engine and specific fuel consumption of the engine when the speed is 1500 rpm and fuel consumption is 20 mg per stroke cycle.

Problem Statement:

How to perform file parsing on the given thermodynamic file in Python to accomplish the objective listed above?

Theoretical background:

Data analysis plays a role in making decisions more scientific and helping businesses operate more effectively. Data analysis is a process of inspecting, cleansing, transforming and modelling 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 domains.

Analysis refers to breaking a whole into its separate components for individual examination. Data analysis is the process of obtaining raw data and converting it into information useful for decision-making by users. Data is collected and analyzed to answer questions, and test hypotheses.

The four types of data analysis are:

  • Descriptive Analysis
  • Diagnostic Analysis
  • Predictive Analysis
  • Prescriptive Analysis

The data file given consists of various parameters of an engine. We have to calculate the below parameters

1) Work done by the engine is obtained by calculating the area under the P-V diagram.

2) Power output of the engine is given below

3) Specific Fuel Consumption (SFC)

SFC = Mass of Fuel Consumption / Engine brake power = FC / Pb

Procedure:

Download the thermodynamic file using the given link and save the file in the same directory where the python scripts are saved. Open a new python script and import the basic modules listed below

  • math
  • mathplotlib
  • numpy

To perform the compatibility check, use the 'input' function in order to receive user input when the code is being run. This function can ask the user to enter the name of the thermodynamics data file along with the file extension. Next, use an 'if' statement so that the code performs two different things based on the file name that was asked to be entered. if the file name is 'engine_data.out', proceed with the file parsing. Use the else statement, to print a statement that asks to input a valid file name.

Within the 'if' statement, if the file name is valid, create a set of empty arrays for every parameter that is listed in the thermodynamics file. In addition, create two more empty arrays for the units of the variables, and for the parameter itself. Also, create a variable that tracks the line count of the data file.

Proceed with the file parsing using the 'for line in open()' function, to allow Python to open the thermodynamic data file and read the data from the file. Add a count of plus one of the previously created 'count_line' variables so that for every line that is read from the file, the line count will be added by one.

Use another 'if' statement to add the data of the parameter name and the units of the parameters if the line in the thermodynamic file begins with a '#' symbol. This statement must use the 'line.split' and 'append' functions to add to the empty arrays of parameters and suits, based on the line count of where that data is found in the file.

To parse the data below each parameter, use another if statement to add the data to the respective empty arrays when the line does not begin with a '#' symbol. This will also make use of the 'line.split' and 'append' functions and the index position of the columns.

Next, create a new variable that stores the names of the parameters in the thermodynamic data file, within an array.

After, use the 'print' statement to list out all the parameters in the data file, in the correct order. The 'print' statement can also output additional statements to make the program appear more interactive e.g. "Select any of the two parameters to analyse and plot the relationship between". Create two more variables for 'X' and 'Y' which will require user input to provide the two data parameters for which the graphs will be plotted along the x and y-axis. This input will take the serial number of the parameter rather than the parameter itself. for instance, '2' would select the 'Pressure' parameter.

In order to plot the data between the two parameters that were input by the user, use an "if" statement that will make use of the 'parameter' and 'units' arrays. These arrays are required for the serial number positions that the parameters hold in the thermodynamic file. These commands will go through the length of the two arrays.

Use the 'plt.plot' command to plot the graph between the two different parameters that were selected by the users. In order to display the correct parameter and the respective units along with the access of the plot, perform the matrix multiplication between the two arrays. Perform the same for the title and when saving the plot use the 'plt.savefig' function.

Finally, perform the necessary calculation to determine the output power and specific fuel consumption of the engine. using the 'input' function, create two more variables that require the user to input the known data of the engine speed in rpm and the fuel consumption on micrograms per stroke. To calculate the work done by the engine, we need to integrate the PV diagram of the engine.

Use the 'np.transpose' function from the numpy module to calculate the area under the PV diagram. Within the function, ensure that the parameter of the pressure and the volume is selected using the serial number positions. This function can be saved within the variable 'work_done'. Since the pressure parameter is measured in MPa, we need to convert work done by 1 million to obtain the work done in Jules.

To calculate the specific fuel consumption, divide the speed of the engine by 60. This is because we need to first determine the revolutions per second from the revolutions per minute. Create a new variable for the revolutions per second to store this value. Create another variable for the total number of strokes by multiplying the revolutions per second by 4. This is because, I assume the engine to be a four-stroke one, and the fuel consumption value used is 20 micrograms per stroke. In a four-stroke engine, one revolution of the crankshaft results in a four-stroke in the cylinder.

Multiply the fuel consumption value by the total number of strokes to obtain the fuel consumption in micrograms per second.

The specific fuel consumption is determined by multiplying the fuel consumption (now in micrograms per second) with 0.0036 and dividing by the power output value.

Additionally, the 'print' statement can be used to display the work done, power output and specific fuel consumption of the engine in a more interactive manner.

Python Programme:

# To perform the data analysis of a given data 

from inspect import Parameter
import math
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import simps

# Compatibility Check
File = "engine_data.out"
filename = input("Enter the filename: ")

if filename == 'engine_data.out':
	print('\nfile is valid')
else:
	print('\nfile is invalid')

	exit()

def compatibility_check(a):

	if 'CONVERGE' not in a:
		print('Please provide converge output file to process')

		exit()
	else:
		print('Compatibility check is successful')

		pass

# try:
data = open('engine_data.out')
print(data)
line_count = 1

# Creating an empty array to store the values of Volume and Pressure
Volume = []
Pressure = []

v = 8
p = 2

# Creating null array and storing all the values of the 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,linewidth = 2,color = 'b')
plt.xlabel('Volume (m^3)')
plt.ylabel('Pressure (MPa)')
plt.title('P-V Diagram of Engine')
plt.grid()
plt.savefig('P-V Diagram')
plt.show()

area = float(simps(Pressure,Volume))
print('\nArea under the curve using Simpsons Rule :' + str(area) + ' units')
print('\nWork done by the engine: ' + str(area) + ' MJ')
area = area * 1e6
print('\nWork done by engine: ' + str(area) + ' J')

# Inputs
# Speed of an Engine
rpm = 1500
rps = 1500/60
stroke_cycle = rps/2

# Fuel consumption in microframs per 1 stroke cycle
fuel_consumption = 20

# Engine Specification
# Number of Cylinder
n = 1

# 4 Stroke Engine
# Calculation of Power Output and Specific Fuel Consumption
power_output = n * area * stroke_cycle / (1000)
print('\nPower output from engine: ' + str(power_output) + 'kW')

SFC = (fuel_consumption * pow(10,-6) * stroke_cycle * 3600) / (power_output * 1000)
print('\nSpecific Fuel Consumption, SFC = ' + str(SFC) + 'kg/hr/kW')
print('\nt')

# transporting columns in to row matrix and converting them into float typr 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))']

# Provinding user choice of picking 2 parameters for plotting
print('\nChoose from following options and press entery key')
print('\n 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 Interfrated 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('\nEnter your choice on x-axis '))
Y = int(input('\nEnter your choice on y-axis '))

#  To store required value to plot
value_1 = []
value_2 = []
n = X - 1
m = Y - 1

# Creating a loop to get required values from data file
for line in open(File):
	if '#' not in line:
		value_1.append(float(line.split()[X-1]))
		value_2.append(float(line.split()[Y-1])) 

# Plotting the Variation Parameters 
plt.plot(value_1, value_2, linewidth=3)
plt.title('Plot for ' + parameters[n] + ' verses ' + parameters[m] ,color = 'k')
plt.xlabel(parameters[n] + ' ' + units[n])
plt.ylabel(parameters[m] + ' ' + units[m])
plt.grid()
plt.savefig('Plot for ' + parameters[n]+ ' verses ' + parameters[m])
plt.show()

Plots and Results:

Here, we can observe that entering the file name it executes the file is valid if it is correct.

The P-V diagram is plotted as per requirement.

Here, Area, Work done, Power and Specific Fuel Consumption is calculated as shown in the above figure.

Asking for the plot of your choice on the x-axis and y-axis by entering the numbers assigned.

Here, we plotted the graph of Crank vs Volume as shown above.

Asking for the plot of your choice on the x-axis and y-axis by entering the numbers assigned.

Here, we plotted the graph of Crank vs Density as shown above.

Asking for the plot of your choice on the x-axis and y-axis by entering the numbers assigned.

Here, we plotted the graph of gamma vs Minimum temperature as shown above.

Conclusion:

File parsing of the provided thermodynamic data file was performed in python. In addition, a plot between any two selected parameters can be generated and saved. Also, the necessary engine performance calculations were performances to obtain the following results:

  • The work done by the engine is: 500.537 J
  • The power output of the engine is: 6.2567 kW
  • The specific fuel consumption of the engine is: 0.0001438 kg/hr/kW

We have obtained graphs for the following:

  • Pressure vs Volume
  • Crank vs Volume
  • Crank vs Density
  • gamma vs Minimum Temperature
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.