Week 6 - Data analysis
DATA ANALYSIS
Objectives
Data Visualisation – Considering the output obtained from an engine analysis as input and writing a code that generates graphs between selected parameters.
Compatibility Check – Writing a code that verifies the input file and sends an error if the file format is wrong or a different file is selected.
Basic Performance Calculation – To calculate the area under the PV curve, the power output of the engine, and the specific fuel consumption.
Problem Statement
File parsing and task automation can be performed using Python. Using certain lines of code and few predetermined functions, the contents from a file (containing a huge amount of data) can be extracted and then used accordingly. The data in the current file is regarding a performance simulation on an IC engine. It approximately contains 8600 lines of data. Moreover, the values of over 17 thermodynamic properties have been estimated.
The primary focus in data visualization is to provide the column numbers of the respective thermodynamic properties as the input. The output must be a plot between the selected properties along with the respective labels.
The area under a PV diagram indicates the work done during the process.
The power output of the engine is estimated as follows.
`Power= 2πNT/60000 kW`
Where N indicates the number of rotations per minute
T indicates the torque
Specific Fuel Consumption is defined as the amount of fuel consumed by an engine per unit of power output. It is estimated as follows.
`SFC= m_f/Power kg/kWhr`
Solution Procedure
Compatibility check and basic performance calculation
Initially, the math, matplotlib, and scipy modules are imported.
For checking the compatibility, try and except commands are used. Try takes in the name of the file as the input while the error text is included in the except command.
Now, empty arrays are created to store the values of pressure and volume.
A for loop is used to perform file parsing. line.split() command is used to split a line into a set of individual elements in an array. Since the file contains data in the form of strings, float command is used to convert them into numbers.
Indexing can be used to call out all the elements of a column. Later, append command is used to update the values of pressure and volume in their respective arrays.
Moreover, if loop is also used within the for loop that eliminates few lines of insignificant data.
Later, a graph is plotted between pressure and volume.
Simps command is used to find out the area under the PV diagram. This command uses the X and Y coordinates as inputs and calculates the area under a curve. In this case, the pressure and volume data are supplied as an input.
Later, using the above-mentioned formulae, the power output and specific fuel consumption are estimated, using necessary conversions.
The code is as follows
# Compatibility Check and Basic Performance Calculations
# Importing modules
import math
import matplotlib.pyplot as plt
from scipy.integrate import simps
# Compatibility Check
try:
open('engine_data.out')
except:
print('Please provide a valid CONVERGE output file')
# Basic Performance Calculation
# Defining the empty arrays
pressure = []
volume = []
# File Parsing
for line in open("engine_data.out"):
if '#' not in line:
pressure.append(float(line.split()[1]))
volume.append(float(line.split()[7]))
plt.plot(volume,pressure,linewidth = 3)
plt.xlabel('Volume')
plt.ylabel('Pressure')
plt.savefig('PV Curve')
plt.show()
# Area under the curve
area = simps(pressure,volume)
print('The area under the PV curve is ',area)
# Calculation of power output
workdone = area*pow(10,6)
rpm = 1500
engine_output = (2*math.pi*rpm*workdone/60000)
print('The power output in kWatts is ',engine_output)
# Calculation of Specific Fuel Consumption
fuel_consumption = 20*pow(10,-9)*(1500/60)#conversion to kg/sec
specific_fuel_consumption = (3600*fuel_consumption)/engine_output
print('The specific fuel consumption in kg/kW-hr is ',specific_fuel_consumption)
Data Visualisation
Initially, math and matplotlib modules are imported.
A compatibility check is performed.
Two parameters are defined as input functions. These allow the user to input column numbers. Later, these are defined as integers.
Two arrays are then defined which would store the data values of selected columns.
A for loop is then used for file parsing. If loops are also used, one for defining and storing the name of the thermodynamic property, and the other for extracting the respective data from the file and storing them in the defined arrays.
Later, plotting commands are written.
The code is as follows.
# Data Visualisation
# Importing Modules
import math
import matplotlib.pyplot as plt
import numpy as np
# Compatibility Check
try:
open('engine_data.out')
except:
print('Please provide a valid CONVERGE output file')
parameter1 = input('Enter the column number of first parameter:')
parameter2 = input('Enter the column number of second parameter:')
parameter1 = int(parameter1)
parameter2 = int(parameter2)
column_1 = []
column_2 = []
line_count = 1
for line in open('engine_data.out'):
if line_count == 3:
property_1 = line.split()[parameter1]
property_2 = line.split()[parameter2]
if '#' not in line:
column_1.append(float(line.split()[parameter1 - 1]))
column_2.append(float(line.split()[parameter2 - 1]))
line_count = line_count + 1
plt.plot(column_1,column_2)
plt.xlabel(property_1)
plt.ylabel(property_2)
plt.show()
Results
The PV diagram is obtained as follows.
The results of basic performance calculations are as follows
The name of the Converge file used for extracting data is ‘engine_data.out’. If some other file name is used, the error obtained is as follows.
The results of data visualization are as follows
Some of the plots obtained using data visualization code are as follows
Discussions
The area obtained under the curve is equal to the work done during the process. Since pressure is in MPa and volume is in cubic meters, after suitable conversions, the value of the area is multiplied by 10^6. Similarly, the given mass flow rate is in terms of micrograms/stroke cycle. It is later converted to kg/sec. Later it is converted to hours so that the final value obtained is in kg/kW-hr.