Data analysis using python
Aim-
To read the file 'engine_data.out' and perform the following operations on the data.
Objective-
1) Data extraction -
Extracting data along with the column name and column number from the CONVERGE output file. Complete data from the file is stored in a two-dimensional array 'data' The columns of data matrix/2D array contains the data for each individual parameter.
2) Data visualizer -
Provide an "Input map" to the user so that he/she can use numbers as an input to designate the variables to be plotted. Ask user if plot between any two variables is required. If the input is 'y' representing 'yes' then, ask for the numbers representing the variables to be plotted (Referring input map). Ask the user if the plot is to be saved. Then, show the plot. Above step is under while loop and runs until the user gives an input 'n' to the question "Do you want to plot any other variable? (y/n): ".
3) Basic performance -
Extract volume and pressure data from matrix 'data' and store in array 'volume' and 'pressure' respectively. Next, numerically integrate pressure and volume data using 'simps' function (Function for numerical integration by Simpson's rule) from scipy. integrate library. This integration is the area under the P-V curve i.e. work done per cycle. Then, RPM of the engine at which the data was collected and fuel consumption in microgram per cycle is taken as user input. Next, the power produced by the engine at this RPM is calculated. Finally, Specific fuel consumption is calculated and Area under the curve, Power output and Specific fuel consumption is displayed.
Introduction-
This code takes the data from a user-specified CONVERGE format file. It also checks the file for the correct format i.e. compatibility. If the file is not a CONVERGE output data format then the code will throw an error statement "This is not a valid CONVERGE output file.". Also, if the file format is correct but, there is no data present in the file then the code will prompt the user "No data present in the file".
Concept-
The basic concept is to read the file 'engine_data.out' perform several functions on it. As Sublime doesn't allow you to input the data, It is better to define a function Input(), to input the column numbers. I have taken the example of Column nos. 1 & 5. To perform compatibility check, the try and except block: is to be used, as it used for python exceptions i.e. it will display the command 'File not recognized. Please provide a valid CONVERGE output file', if it fails to open. The next task will be to calculate the engine's basic performance calculations i.e.
a) The area under a P-V diagram
used trapz() function from numpy library
area = np.trapz(Pressure,Volume)
b) The power output of the engine
Power output for the 4-stroke engine is given as,
Power = Work done * RPM/2 (Joules)
c) Specific Fuel Consumption
SFC = Area/ Work Done
Methodology-
Let us go task by the task in solving this assignment:
1. The first task is to enter column numbers as input. As sublime doesn't offer input to the user, therefore, instead of using sublimeREPL as it may result in the slowing of program building, a separate function is to be used, namely Input().
2. Now coming to the main part, the first operation a program should perform is a Compatability check, i.e. the program should check whether the file is compatible or not. This is done by using the try and except block: as explained above.
3. Now the next part will be to define a string that contains all the names of each of the headings in the data so that the names can be automatically retrieved, and then the input function is called for values of columns.
Program Code-
# Importing libraries
import matplotlib.pyplot as plt
import math
import numpy as np
try:
title=[]
for li in open('engine_data.out'):
if 'Crank' in li:
title=li.split()
title.remove('#')
print('Column Number Vs Column Name:')
n=0
for a in title:
print(str(n)+' : '+a)
n=n+1
clmi=[]
clmj=[]
i=int(input("input Column Number 1:"))
j=int(input("input Column Number 2:"))
for line in open('engine_data.out'):
if '#' not in line:
clmi.append(float(line.split()[i]))
clmj.append(float(line.split()[j]))
plt.figure()
plt.plot(clmi,clmj)
plt.xlabel(title[i])
plt.ylabel(title[j])
filename=title[i]+' VS '+title[j]
plt.title(filename)
plt.show()
plt.savefig(filename)
Pressure=[]
Volume=[]
for line in open('engine_data.out'):
if '#' not in line:
Volume.append(float(line.split()[7]))
Pressure.append(float(line.split()[1]))
area = np.trapz(Pressure,Volume)
print('Area under PV diagram', area)
rpm = 1500
rps = rpm/60
power_watt = area*rps*1000
print('power of the engine is', power_watt)
sfc = (20*pow(10,-6)*3600)/(0.08*power_watt)
print('Specific fuel consumption is',sfc)
except:
print('File not recognized. Please provide a valid CONVERGE output file')
Output-