IC Engine data file analysis using Python
IC ENGINE OUTPUT DATA ANALYSIS - FILE PARSING USING PYTHON
The objective of this project is to analyze a given engine output data file derived from Converge CFD and using it for file parsing and getting relevant plots for whichever data the user prefers. The data file contains a large list of engine parameters and a program in Python is written in such a way that for a given input, the user gets the appropriate plot with its name and labels automatically. Further features in this program are:
1. Check compatibility of the file
2. Calculate basic parameters of an engine:
- Calculate the area under the P-V diagram. P is in column 2 and V is in column 8.
- Calculate the power output of this engine. Assume that RPM is 1500
- This engine consumed 20 micro grams of fuel (per 1 stroke cycle). Calculate its specific fuel consumption.
LITERATURE REVIEW
First of all, the output data file is viewed using notepad so that we can get an idea of what to code and how. Python allows for a simple set of code to define which line is supposed to be read at which point. Using a line counter variable and for loop, we can easily read the output file line by line and allot them into different arrays.
Arrays are necessary because the data sets are huge and for each column we need a large amount of data to be plotted against another equally large data set. After defining each data set with its array, a separate array is included which incorporates all the array data into one so that its easy to call one complete set for plotting.
Data analysis like this is very common in lots of companies and is very useful in getting valuable information from an output file easily without any complications. The process is automated and inputs are taken from the user.
Speaking of inputs, to facilitate the use of inputs in Sublime text application, we need to perform a series of tasks and install the SublimeREPL module so that we can interact with the python prompt and get our desired output. For this, we need to first go to https://packagecontrol.io/installation and copy the code given under Sublime text 3 and paste it in the console interface of Python. This can be done by selecting Show console in the view menu tab. After this, we can go to Preferences -> Package Control and select the SublimeREPL as a package to be installed. A full step by step process of this is provided in a short YouTube video.
After all this is done, we can activate using REPL as the command prompt interface by going to Tools -> SublimeREPL -> Python -> Run current file or we can simply select the current build system as SublimeREPL-Python from the tools section itself.
Now we are ready to start coding the program and getting the required plots needed.
CODING
\'\'\'
File parsing for engine data output
\'\'\'
import matplotlib.pyplot as plt
import numpy as np
import math
import sys
# to check compatibility of file
try:
open(\'engine_data.out\',\'r\')
except:
print(\'File not recognized. Please provide a valid CONVERGE output file\')
sys.exit()
# initial definition of arrays
crank = []
pressure = []
min_p = []
max_p = []
mean_temp = []
max_temp = []
min_temp = []
volume = []
mass = []
rho = []
int_hr = []
hr_rate = []
cp = []
cv = []
gamma = []
kin_vis = []
dyn_vis = []
# Defining a variable array with all the data arrays inside so that a set of data can be called directly
a = [crank,pressure,max_p,min_p,mean_temp,max_temp,min_temp,volume,mass,rho,int_hr,hr_rate,cp,cv,gamma,kin_vis,dyn_vis]
# for extracting title and unit of each data column
data_title = []
unit = []
# Showing the list of data and what can be called
print(\'The column wise data present in the file are:\\n1. Crank angle\\n2. Pressure\\n3. Max Pressure\\n4. Min Pressure\\n5. Mean temperature\\n6. Max temperature\\n7. Min temperature\\n8. Volume\\n9. Mass\\n10. Density\\n11. Integrated HR\\n12. HR rate\\n13. Cp\\n14. Cv\\n15. Gamma\\n16. Kinematic Viscosity\\n17. Dynamic Viscosity\')
# Requesting input from user for X and Y axis
x = int(input(\'Enter X-axis Column data:\'))
y = int(input(\'Enter Y-axis Column data:\'))
# Checking whether the user entered the correct data value
if x < 1 or x > 17 or y < 1 or y >17:
print(\'Invalid number. Please provide column numbers within the data set.\')
sys.exit()
# Code to extract data from a file
linc = 1 # line count variable to keep track of lines read in the file
for line in open(\'engine_data.out\'):
if \'#\' not in line:
a[x-1].append(float(line.split()[x-1]))
a[y-1].append(float(line.split()[y-1]))
if \'#\' in line:
if linc is 3:
for i in range(1,16):
data_title.append(line.split()[i])
if linc is 4:
for i in range(1,16):
unit.append(line.split()[i])
linc = linc + 1
# Plot for requested data
plt.plot(a[x-1],a[y-1])
plt.xlabel(data_title[x-1] + unit[x-1])
plt.ylabel(data_title[y-1] + unit[y-1])
head = data_title[x-1] +\' vs \' + data_title[y-1]
plt.title(head)
plt.savefig(head)
plt.show()
# Basic Calculations:
# Area under PV diag: Using trapz function in python to find area under a curve
if x == 8 and y == 2:
area = np.trapz(a[y-1],a[x-1]) * 1e6 # the area is multiplied by 1e6 because the pressure is given in MPa
rpm = 1500
rps = rpm/60
power = area*rps/1000 # power (KW)
sfc = 20*pow(10,-6)*3600/(0.08*power) # specific fuel consumption (g/KWh)
print(\'Area under the PV diagram is: \',area,\'J\')
print(\'Power is: \',power,\'KW\')
print(\'Specific fuel consumption is: \',sfc,\'g/KWh\')
RESULTS
For example, we input the numbers 8 and 2, that is Volume and Pressure as X and Y axes respectively to get the required graph.
We can clearly see that a list of data variables are first printed to give the user an idea of what he can ask from the program data set values. The plots also have derived each of the data values units and labels to be added in the X and Y axes respectively. The title is also extracted from the labels and added to the plot heading. The plots are saved with the respective title names in the working directory. All this is done automatically from the Python program itself by the use of the array calling system.
Basic calculations:
The area under the PV diagram is calculated in Python using the trapz function in the numpy module. We know that the value obtained is of work done of the engine and to convert it into Joules, we multiply the obtained value by 1e6, since the pressure values assumed are in unit MPa. After getting the work done by the engine, we multiply it with RPS(rotations per second) to obtain the power value.
Given RPM = 1500
RPS = 1500/60
Power = (Area or Work done (in Joules) * RPS) /1000
This is done to obtain power in KW (Kilo Watt). After this, we need to formulate an equation for Specific fuel consumption(SFC), which is generally given by the unit g/KWh.
Given engine consumption = 20 micro grams of fuel per stroke cycle.
We know that , RPS = 25, i.e, the engine rotates 25 times per second. One stroke cycle of an engine consists of 2 rotations and hence the engine completes 12.5 cycles per second.
Therefore, 1 cycle is completed in 0.08 seconds.
SFC = Fuel consumed per KWh
Fuel consumed per second = `(20*10^-6)/0.08 = 0.00025 g`
Then, the equation for SFC becomes,
SFC = `(0.00025*3600)/(Power)`
Here, Power is directly substituted in KW to obtain KWh unit in the calculations.
Hence we can define the SFC formula as an equation,
SFC = `(20*10^-6*3600)/(0.08*Power)`
Compatibility Check:
The program only accepts the correct output data file name and hence the user must place the output file in the working directory for the program to use the file with the appropriate name assigned. To make this possible, the try and except function is used to run the command line and the sys.exit() command is used when the wrong file name is entered so that the program will terminate in that line and does not execute the other lines unless the file name is given correctly. We need to import the sys module for this to work.
Other outputs:
When a column number above the threshold of the data set is given, the program is made to print an invalid number statement and the same sys command is used to exit the program at that line itself.
For other plots, the output is as,
Hence, with this code, a data visualizer is set up and we have automated plot creation for any set of data values from the engine output files. For specific plots like the pressure and volume plot, the area under the PV diagram, Power and Specific fuel consumption of the engine are calculated. The compatibility of the file is also checked and the program exits gracefully. This is the data analysis performed on the engine output data file provided.