Challenge

Python Program: Data analysis (File Parsing of Converge CFD output file)

Aim: Write a Python script for file parsing of Converge CFD data file.

Objective:

  • Writing a script that extracts data from a compatible data file.
  • Developing an interface wherein user can select set of data to visualize.
  • Evaluation of basic engine performance for given conditions

Theory:

Parsing actually means to split a file or other into pieces of data that can be easily be stored or manipulated. In file parsing, we split a string into parts, then recognize the parts to convert it into something simpler than a string. File parsing is used to read as well as to write data in a file.

Converge CFD a product of Convergent Science has truly autonomous meshing capabilities that eliminates the grid generation bottleneck from the simulation process. The data file which we'll parse is the output of CFD analysis of IC engine. The data file contains various engine characteristics and parameters like crank angle, pressure, volume, temperatures, etc. at various instance of time. The data is well stacked up in form of rows but is vague as we cannot draw any insights from the numbers that pop up. Rather if the data is extracted and calculations are performed or data is visualized then we can gather some information from it. The script written in python programming language does this job when a compatible data file is provided to it.

Program:

#Data-Analysis

#Importing required libraries
from numpy import *
from matplotlib.pyplot import *

#File Compatibility
def file_reader(file):
    if file=="engine_data.out":
        print("File Name OK","\n","Proceeding Compatibility Check")
        if 'CONVERGE' not in a[0]:
            print("Error : Unsupported File")
            print("Provide a Valid Converge File")
            exit()
        else:
            print("File Is Compatible","Setting Up Environment\n")
    else:
        print("Error : Unsupported File")
        print("Provide a Valid Converge File")
        exit()

#Prompt
file = input("Enter Filename : ")
a = []
for line in open(file,'r'):
    if '#' in line:
        a.append(line.split())
file_reader(file)

#Setting Interface
#Appending Interface Data
col_num = a[1]
col_data = a[2]
col_unit = a[3]

#Data Cleaning For Displaying
col_num = col_num[2:]
col_data = col_data[1:]
col_unit = col_unit[1:]

#Title for Interface
def header(s):
    str = " "  
    return (str.join(s)) 
s = a[0]

#Interface
print("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n")
print(header(s))
print("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n")
print("Data Available For Analysis\n")
print("Column :  Data : Unit")
print("----------------------")
for i in range(len(col_num)):
    if(i<9):
        print("  0"+col_num[i], "  : ", col_data[i], ":", col_unit[i])
    else:        
        print("  "+col_num[i], "  : ", col_data[i], ":", col_unit[i])
print("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n")

#Prompt
print("\nSelect Any Two From The above Data\n")

x = (int(input("Enter The Column For x-axis: "))-1)
y = (int(input("Enter The Column For y-axis: "))-1)

#Labels On Both Axis 
x_label = col_data[x]
y_label = col_data[y]

X = []
Y = []

#Appending Called Data
for line in open(file,'r'):
    if '#' not in line:
        X.append(float(line.split()[x]))
        Y.append(float(line.split()[y]))

#Basic Performance Calculations
P = []
V = []

for line in open(file,'r'):
    if '#' not in line:
        P.append(float(line.split()[1]))
        V.append(float(line.split()[7]))

pressure = 1e6 * array(P)
volume = array(V)
AUC = trapz(pressure,volume)
cyclepersec = 750/60
power = AUC * (cyclepersec/1000)
mf = 20e-6
SFC = mf * 3600/power

#Displaying Engine Performance
print("\n-------------------------------------------------------------")
print("| Engine Performance Calculation                            |")
print("-------------------------------------------------------------")
print("| Work Done =", AUC, "Joules                      |")
print("-------------------------------------------------------------")
print("| Power Output of the Engine @ 1500RPM", power,"kW |")
print("-------------------------------------------------------------")
print("| Specific Fuel Consumption", SFC, "g/kW.h     |")
print("-------------------------------------------------------------\n")
print("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n")

#Plotting The Data
plot(X,Y)
xlabel(x_label+col_unit[x])
ylabel(y_label+col_unit[y])
savefig(y_label+"-"+x_label+".png")
show()
print("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n")
print("\n* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \n")

Working of Code:

1. The code begins with importing required libraries. Libraries of interest here are, numpy and matplotlib.pyplot, for calculations and plotting respectively.

2. As we need our script to exit gracefully in case invalid file is provided. For this we define a function that can run a compatibility test. The test is simply a series of if else statements which helps in checking validity of file.

3. File name is taken as input by a prompt and the file is passed into the compatibilty checker function.

4. Once compatibility check is done, data stored in file is cleaned to form an interface. The interface has columns which consists the data contained by the data file. Also, header for interface is extracted.

5. Again a prompt is given to user to select any two data points from the interface. This points are extracted and appended into empty arrays (namely X and Y). The extracted data is plotted at later stage.

6. Next, we extract pressure and volume values to calculate engine power. This done by evaluation of area under PV curve (this gives work) this when multiplied by number of cycles gives power. This is done by using trapz command. The results are displayed in tabular fashion proceeding which we have plots embelished in previous steps.

Results:

Interface:

First Selection and Output:

Saved Plot-1:

Second Selection and Output:

Saved Plot-2:

If invalid file is provided:

Hence, kernel shuts down i.e program exits automatically.

References:

Material: Python Code, Plots and Notebook Can Be Accessed Here

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.