Project

Data Analysis Using Python

Objective: To perform data analysis using Python. When given a date file which contains a large number of values of different parameters at different points during an experiment or a simulation, our python program should be able to analyze the big data and provide the user with the desired results. In our case, we are analyzing a data set which has been obtained from the simulation of an IC engine.

Introduction: We are given a data file which contains the data sets obtained from the simulation of IC engine. The data file contains the values of different thermodynamic properties like crank angle, pressure, volume, mean temperature etc. in separate columns. We have to write a python program which can perform the following tasks:-

  1. Data Visualization: Our script should take column numbers which represent the different thermodynamic properties as the input and plot the respective columns as separate images. The file should be saved the name of the column. Also, the plot labels should be extracted from the file automatically.
  2. Compatibility Check: Our code should exit gracefully, i.e., 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".
  3. Basic Performance Calculation: Our program should be able to calculate the area under the P-V diagram. It should also calculate the power output of this engine assuming that RPM is 1500. Finally, it should be calculate the value of specific fuel consumption, given that the engine consumes 20 micro grams of fuel (per 1 stroke cycle).

Solution Procedure:-

  • We start our code with importing the required libraries.
  • We ask the user to enter the name of the data file which he/she wants to analyze. We check whether the file exists of not.
  • If the file exists, we proceed as notmal. Otherwise, we throw an error saying that the file with the given name does not exist.
  • We ask the users to enter the column numbers of the two parameters which are to be plotted.
  • We create two arrays, each for storing the values of the parameters corresponding to the columns entered by the user. We use these arrays to plot the two parameters.
  • We extract the name of these two columns and save the plot with the desired name.
  • We create two more arrays for storing the values of pressure and volume. We use these arrays to calculate the area under P-V diagram using the function "integrate" present in the scipy module.
  • Finally, we calculate the values of power output of the engine and specific fuel consumption.

Code:-

(The different steps in the code have been explained as comments in the code itself.)

import math
import matplotlib.pyplot as plt 
import os
import numpy as np
from scipy import integrate

# Asking users for the input file
print("Please enter the name of the file that you want to use for data analysis:")
str = input()
# Checking whether the file exists or not
exists = os.path.isfile("%s"%str)

# Now putting the condition that if the file exists, we proceed as normal. Otherwise, we throw the error saying non-existence of the file.
if exists:
	# Asking the user for the column numbers of the desired parameters
	print("Please enter the ith and jth position of the two columns which you want to plot.")
	first_column = input("The first column's position (for x-axis) is (please enter a number):")
	second_column = input("The second column's position (for y-axis) is (please enter a number):")

	column1 = []
	column2 = []
	# column_press and column_vol for plotting PV diagram 
	# Pressure is at 2nd column
	n_press = 2
	# Array storing pressure values
	column_press = []
	# Volume is at 8th column
	n_vol = 8
	# Array storing volume values
	column_vol = []


	line_count = 1
	# Appending the parameter values from the data file to the corresponding arrays
	for line in open("%s"%str):
		if '#' not in line:
			column1.append(float(line.split()[int(first_column) - 1]))
			column2.append(float(line.split()[int(second_column) - 1]))
			column_press.append(float(line.split()[int(n_press) - 1]))
			column_vol.append(float(line.split()[int(n_vol) - 1]))

			line_count = line_count + 1

	# Extracting the labels and name for the file
	line_count = 1
	for line in open('engine_data.out'):
		if line_count is 3:
			# '#' is taking one space
			xlab = line.split()[int(first_column)]
			ylab = line.split()[int(second_column)]

		if line_count is 4:
			xlab2 = line.split()[int(first_column)]
			ylab2 = line.split()[int(second_column)]

		line_count = line_count + 1


	xlabel_final = xlab + " [Unit: " + xlab2 + "]"
	ylabel_final = ylab + " [Unit: " + ylab2 + "]"

	# plotting the figure, deriving the name and saving the figure with the desired name in the current directory
	plt.plot(column1, column2, color = 'red', linewidth = 3)
	plt.xlabel(xlabel_final)
	plt.ylabel(ylabel_final)
	plt.savefig("%s"%ylab + "_vs_" + "%s"%xlab + ".png")
	plt.show()

	# Now, we have to do the final part - basic performance calculation of the engine.
	# column_press and column_vol are the arrays of pressure and volume respectively.
	# To calculate area under P_V diagram:-
	print("Now calculating the basic performance of the engine:-")
	# RPM given
	rpm = 1500
	# Fuel consumed by the engine per 1 stroke cycle (in micro grams)
	fuel = 20
	# Area under P-V diagram
	area = integrate.simps(column_press,column_vol)
	# To convert unit to Joule
	area = area*pow(10,6)
	print("The area under P-V diagram is:", area, "(unit: Joule)")

	power_output = rpm*area/(60*2)
	print("The power output of the engine is:", power_output, "(unit: Watt)")

	# Fuel consumption in kg/h
	fuel_consumption = (fuel*rpm*3600)/(60*2*pow(10,9))
	# Specific fuel consumption = fuel consumption/power
	# Unit: kg/kW-h
	specific_fuel_consumption = (fuel_consumption*1000)/power_output
	print("The specific fuel consumption is:", specific_fuel_consumption, "(unit: kg/kW-h)")

else:
	print("File not recognized. Please provide a valid CONVERGE output file")

Results and Discussions:-

  • We get the plot asked by the user. Suppose the user asks for the plot between column 1 (crank angle) and column 8 (volume), we get the following plot:-

(As we can see that the label names have been automatically given with the corresponding units. Also, the file is autamatically saved with the name "Crank_vs_Volume.png".)

  • The area under P-V diagram, the power output of the engine and the specific fuel consumption - all these values are displayed on running the code:-

The area under P-V diagram is: 500.58644742623636 Joule
The power output of the engine is: 6257.330592827955 Watt
The specific fuel consumption is: 0.00014383130100742393 kg/kW-h

These values are calculated using the appropriate formulaes used in the code and the calculated values are printed on the screen.

Screenshots showing the Output:-

  • Screenshot 1 (The desired parameters are plotted when the user enters the corresponding column numbers.)
  • Screenshot 2 (The basic performance of the engine is calculated.)
  • Screenshot 3 (When an invalid filename is entered, the code throws an error.)
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.