DATA VISUALIZER
\"\"\"
PROGRAM TO CREATE DATA VISUALIZER TOOL
\"\"\"
import math
import matplotlib.pyplot as plt
import numpy as np
#compatibility check using error and exception handling
# using try-except
#working is as follow:-
#First try clause is executed i.e. the code between try and except clause.
#If there is no exception, then only try clause will run, except clause is finished.
#If any exception occured, try clause will be skipped and except clause will run.
#If any exception occurs, but the except clause within the code doesn’t handle it, it is passed on to the outer try statements. If the exception left unhandled, then the execution stops.
#A try statement can have more than one except clause
try:
open(\'engine_data.out\',\'r\')
except:
print(\'file not recognized. please provide a valid CONVERGE output file\')
sys.exit()
#reading the file
file_name=open(\'engine_data.out\',\'r\')
#A lot of times when dealing with iterators, we also get a need to keep a count of iterations. Python eases the programmers’ task by providing a built-in function enumerate() for this task.
#Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object.
#ignoring the header comments and splitting them up
for i,l in enumerate(file_name):
if \'#\' in l:
c=l.split()
#DATA VISUALIZER
x=1 #crank angle
y=8 #volume
z=2 #pressure
d=10 #density
crankdata=[]
volume=[]
pressure=[]
density=[]
for line in open(\'engine_data.out\',\'r\'):
#fetching data
if \'#\' not in line:
crankdata.append(float(line.split()[x-1]))
volume.append(float(line.split()[y-1]))
pressure.append(float(line.split()[z-1]))
density.append(float(line.split()[d-1]))
#plotting different graphs
plt.plot(crankdata,volume)
plt.xlabel(\'crankdata\')
plt.ylabel(\'volume\')
plt.savefig(\'volandcrank.png\')
plt.show()
plt.plot(volume,pressure)
plt.xlabel(\'volume\')
plt.ylabel(\'pressure\')
plt.savefig(\'PV.png\')
plt.show()
plt.plot(crankdata,pressure)
plt.xlabel(\'crankdata\')
plt.ylabel(\'pressure\')
plt.savefig(\'crank&pressure.png\')
plt.show()
plt.plot(volume,density)
plt.ylabel(\'density\')
plt.xlabel(\'volume\')
plt.savefig(\'volandrho.png\')
plt.show()
#basic performance calculations
area=np.trapz(pressure,volume)
print(\'AREA UNDER PV CURVE\',area)
speed=1500 #rpm
mfr=250*1e-06 #mass flow rate
work = area*1e+06
power=work*(speed/120)
power=power/1000 #KW
SFC=mfr/power
print(\'POWER=\',power,\'KW\')
print(\'specific fuel consumption \',SFC,\'g/KWh\')
explanation of the code :-
1.) importing necessary header libraries so that we can use their functions easily just by following specific syntax
2.)next checking the compatibiity by the help of error and exception handling
3.) the commands have been explained in the code itself
4.) now opening the fle in python by using open command and \'r\' stands for reading mode
5.) now splitting those lines in the files ande xtractting / storing them
6.) right now i have taken manual inputs as i wanted to show all the graphs but if we want to have inputs from the user we can use
x=input(\'enter column number 1\') and run these codes
c[x-1]= will be storing the name of the column i.e. crank data , pressure and all
executing them directly in python IDE can give us results
7.) taking 4 random values and opening the file again and filling all the empty arrays with respective values at proper indices
8.)plotting the graphs in a similar fashion and saving them
9.) now using numpy function trapz which will help us to calculate area under the curve or we can use AUC header also . both of them will give same results
outputs:-
graph 1 :-
graph 2:-
graph:-
graph 4:-
the calculation output :-