Data Analysis/Parsing of Engine Simulation file using PYTHON
FILE PARSING/DATA ANALYSIS OF ENGINE SIMULATION DATA
Objective : To extract the various coefficients from the engine_data file and to write a PYTHON program to create Data Visualizer tool.
- ENGINE_DATA file is as follows,
- It contains 17 Parameters in seperate columns and each parameter has around 8000 values
- Other Inputs :- RPM=1500, Engine Fuel consumption=20 micro grams (per 1 stroke cycle).
Data Visualizer Code :
# File Parsing/Data Analysis
#importing modules
import math
import matplotlib.pyplot as plt
import numpy as np
from numpy import trapz
#to check for compatiblity
try:
open('engine_data.out','r')
except FileNotFoundError as e:
raise e('File is not compatible,please provide proper converge output file')
print('Data collected from IC Engine Simulation in Converge')
#to calculate the number of parameters
line_count=1
for line in open ('engine_data.out'):
if line_count==6:
columns=line.split()
column_count=len(columns)
print('Total number of Parameters collected is',column_count)
line_count=line_count+1
#to split the parameter and its units
ct=1
for line in open ('engine_data.out'):
if ct==3:
parameter=line.split()
if ct==4:
unit=line.split()
ct=ct+1
for column in range(1,18):
print(column,parameter[column],unit[column])
#to access the data by entering the column number directly
print('\nSelect any 2 parameters from above for comparison\n')
x=int(input('Enter Parameter X : '))
y=int(input('Enter Parameter Y : '))
input_x=[]
input_y=[]
m=1
for line in open('engine_data.out','r'):
if m==3:
x_para=line.split()[x]
y_para=line.split()[y]
if m==4:
x_axis=x_para+line.split()[x]
y_axis=y_para+line.split()[y]
if '#' not in line:
input_x.append(float(line.split()[x-1]))
input_y.append(float(line.split()[y-1]))
m=m+1
filename=y_axis+' against '+x_axis+'.png'
plt.figure()
plt.plot(input_x,input_y)
plt.xlabel(x_axis)
plt.ylabel(y_axis)
plt.title(y_axis+' against '+x_axis)
plt.savefig(filename)
plt.show()
#to calculate the area under the P-V diagram
if x==8 and y==2:
RPM=1500
area=np.trapz(input_y,input_x) #J
W=area*1000 #KJ
P=2*math.pi*RPM*W/60 #kw
fuel_consumption_per_stroke=20*pow(10,-6)
fuel_consumption_per_hour=fuel_consumption_per_stroke*RPM*60
SFC=fuel_consumption_per_hour/P
print('Area under PV diagram is the Work done :',W,'KJ')
print('Power output of the Engine :',P,'Kw')
print('Specific Fuel Consumption is :',SFC,'Kg/Kw-h')
Code Explanation
- All the necessary modules are imported using import comand. math for mathematical functions, matplotlib.pyplot for plotting, numpy and trapz to calculate area under curves in plots.
- Compatablity Check
- try comand is used to open a file and if the file is corrupted or not able to open, instead of throwing (except) the default error message, using raise comand other message can be popped.
- Data Visualization
- for loop has been called in order to open a line in the file and split the columns accordingly using .split comand, ny which total parameters are calculated.
- Similarly again a line is opened in another loop and at line 3 parameter are collected, at line 4 respective units are collected, and all are printed together by a format using print comand.
- x & y serves as input to process further data analysis, they store the value of column of 2 different parameters and evaluates everytime program executed.
- input_x & input_y are an empty arrays which are depended on inputs x & y.
- Again a for loop is opened to execute lines by iteration, and from information at line 3 & line 4 axis titles are defined and respective list of values are added into input arrays using append comand.
- filename stores the value of image by suitable format.
- Plots for drawn using plot comand and variables are derived from input_x & input_y, x_axis & y_axis. Images are saved using savefig comand.
- Any plots can be drawn from the available 17 parameters just by changing the inputs to x & y ranging from 1 to 17.
- Basic Performance Calculation
- To calculate the area under PV diagram, x=8 & y=2 are used, since Pressure is in 2nd column & Volume is in 8th column.
- numpy trapz is used to calculate the area which represents Work done.
- Using suitable formulae, all the required data are calculated and printed.
- By default Sublime Text(PYTHON) will not act as an comand window, Hence Package installer is downloaded to packages path and then SublimeREPL is installed, which is then set up as Build Solver in Tools Section & Program is executed for different inputs.
Results
Other Plots
References :
- Python Exception Errors :- https://realpython.com/python-exceptions/
- REPL for PYTHON :- https://www.youtube.com/watch?v=Zbf2_VRbgQA
- REPL installation :- https://www.youtube.com/watch?v=wM2LbXCkLDI
___________________________************END*************_________________________