Data analysis Using PYTHON
Introduction -
This code takes 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".
If the format is correct and data is present then the code will implement following steps:-
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 user so that he/she can use numbers as an input to designate the variables to be ploted. Ask user if plot between any two variables is required. If input is 'y' representing 'yes' then, ask for the numbers representing the variables to be ploted (Refering input map). Ask the user if the plot is to be saved. Then, show the plot. Above step is under while loop and runs untill 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 Simphson's rule) from scipy.integrate library. This integration is 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, the Specific fuel consumption is calculated and Area under the curve, Power output and Specific fuel consumption are displayed.
Program -
#This is a program to make it easier for data analysis using python
import matplotlib.pyplot as plt
import numpy as np
from numpy import trapz
import time
lc=1
row_count=0
heading=0
crank=[]
pressure=[]
coloumn_name=[]
coloumn_units=[]
#First Compaitablity Test
try:
open('engine_data.out')
except:
print("File not recognized or present. Please provide a valid converge output file")
sys.exit()
f='engine_data.out'
for line in open(f,'r'):
if '#' not in line:
row_count=row_count+1
coloumn_count=len((line.split()))
else:
heading=heading+1
if heading==3:
coloumn_names=(line.split())
print(coloumn_names)
elif heading==4:
coloumn_units=(line.split())
x= input("Please give required x axis data coloumn number (1-17) 1-Crank to 17-Dynamic Viscosity ")
nx=int(x)
print (nx-1)
y=input("Please give required y axis data coloumn number (1-17) 1-Crank to 17-Dynamic Viscosity ")
ny=int(y)
print(ny-1)
print("The Graph you requested is ",coloumn_names[ny],"vs",coloumn_names[nx])
data_array_x=[]
data_array_y=[]
for i in open(f,'r'):
if '#' not in i:
data_array_x.append(float(i.split()[nx-1]))
#print(data_array_x)
for j in open(f,'r'):
if '#' not in j:
data_array_y.append(float(j.split()[ny-1]))
#print(data_array_y)
plt.plot(data_array_x,data_array_y)
plt.xlabel(coloumn_names[nx]+coloumn_units[nx])
plt.ylabel(coloumn_names[ny]+coloumn_units[ny])
plt.savefig(coloumn_names[nx] +' VS '+ coloumn_names[ny]+'.png')
plt.show()
#Area under PV diagram
volume=[]
pressure=[]
for k in open(f,'r'):
if '#' not in k:
pressure.append(float(k.split()[1]))
volume.append(float(k.split()[7]))
#Calculating area under the pv curve
area = np.trapz(pressure,volume)
print('The total area under the graph is ',area)
#Calculating power output of the engine
rpm = 1500
power = area * 1e+06* rpm/(9550)
print('The power output of the engine is ',power,'KW')
#Calculating specific fuel consumption
fuel_per_stroke = 20*1e-06
mass_flow_rate=2.99*1e-10 #mass flow rate in grams per second
Specific_Fuel_Consumption = mass_flow_rate/ (power)
print('The Specific Fuel Consumption is',Specific_Fuel_Consumption,'g/KW')
plt.plot(volume,pressure)
plt.savefig(coloumn_names[8] +' VS '+ coloumn_names[2]+'.png')
plt.xlabel("Volume")
plt.ylabel("Pressure")
plt.show()
Code Explanation
The required libraries including matplotlib.pyplot and numpy is imported.
The first compaitablity test is done using try and except function.
After this using for loop the the names of the coloumns as well as their units are stored in arrays.
Using input function the x and y axis of the required graph is taken.
using for loop the datat in the required coloumns are stored in respective arrays.
Following this The graph is plotted,displayed and saved.
After this the PV graph is plotted. The data is taken from the respective coloumns using for loop as before
The area under the PV graph is calculated using special function in Numpy called trapz.
After this the require values of power and Specific fuel consumptios in found out.
Results/Graph