import math;
import matplotlib.pyplot as plt;
import numpy as np;
import glob;
from tabulate import tabulate;
from scipy.integrate import simps
# take input file from user
File = input("Please Enter file name with extension (.out) :");
# take all out files from directory
all_files = glob.glob("*.out")
# initialize file data
fileData = "";
#check file present in directory and file extension check
if File in all_files and File.endswith('.out'):
fileData = open(File)
else:
print('File not recognized. Please provide a valid CONVERGE output file')
if fileData != "":
# to store required value to plot
titles = []
value_1 = []
value_2 = []
column_count = 0
# to calculate work done from p-V curve
pressure = []
volume = []
# seprating header and column data
for line in fileData:
if '#' in line:
# triming of unwanted words
trimmed = line.lstrip('# column')
# split the line into array
line_arr = trimmed.split();
# removing blank array from array
if len(line_arr) != 0:
# append line data
titles.append(line_arr)
if '#' not in line:
column_count = column_count+1
if column_count == 1:
s_num = titles[1]
parameters = titles[2]
units = titles[3]
# tabulate header data
headers = ['Column No',"Parameters","Units"];
data = tuple(zip(s_num,parameters,units));
print(tabulate(data,headers = headers)+"\n")
#taking input from user
a = int(input('Enter column no of 1st parameter(x axis):'))
b = int(input('Enter column no of 2nd parameter(y axis):'))
value_1.append(float(line.split()[a-1]));
value_2.append(float(line.split()[b-1]));
pressure.append(float(line.split()[1]));
volume.append(float(line.split()[7]));
# to generate labels automatically for plots
x_label = parameters[a-1]
y_label = parameters[b-1]
# calculate area under p-V diagram
Area = simps(pressure,volume)
print('Area of p-V curve : ' +str(Area)+' MN-m');
print('work done by engine : ' +str(Area*1e6) +" N-m Or Joule \n" );
# calculate power output of engine
rpm = int(input('Enter the engine RPM :'))
rps = rpm/60
# No of power strokes in revolution of crank shaft
Power_strokes = rps/2
power = (Area*1e6)*Power_strokes/1000
print('Power output of engine is : ' +str(power)+ " kW \n");
# To calculate specific fuel consuption
r = int(input('Enter Specific fuel consumption per in kg :'))
sfc = (r/power)*3600;
print('specific fuel consuption of the engine : '+str(sfc)+" kg/KWhr \n")
# plotting the data
plt.plot(value_1,value_2,linewidth=3)
plt.title(y_label+" Vs "+x_label)
plt.xlabel(x_label)
plt.ylabel(y_label)
plt.grid()
plt.savefig(y_label+" Vs "+x_label)
plt.show()