File Parsing in Python

OBJECTIVE: To demonstate file parsing using Python.

  1. when we run the program, first thing appears is  to enter the filename,if we speicify correct name then we can see the rest output otherwise a message will show file not found.
  2. To store different properties like crank, pressure, density,etc empty array was created.
  3. Using for loop each line was read and each line was split using split() by empty space.
  4. As crank was index 0, so we use crank.append(float(line.split()[0])). Same procedure was repeated for all other properties.
  5. To make a column of properties like density, mass, crank, etc again for loop was used and since these properties are present on line 3, if command was used                                        if linecount is 3:
                                 title.append(line.split())                                                                                Here also empty space was used for splitting and all data was stored in array title.
  6. Same this was done to extract units from line no 4 and was stored in array units.
  7. To construct a table PrettyTable was imported and table consisting of column  and properties was created. since Title array conatined list inside a list [0] was used to extract 1st list and [x+1] was used since first value(#) was not needed.
  8. ALL values of engine data was stored in in variable a column wise like 1st column contained only crank data.
  9. To plot the data between two values like pressure and volume, input x and y was taken and these input aws converted into integers.
  10. plotting was done using a[x] and a[y].
  11. For label data was taken from title and units.
  12. Area was calculated using np.trapz(pressure, volume) 
  13. power aws calculated using formula (area underpv plot/time for one cycle) 
  14. brake specific fuel consumption was calculated using formula (3.6*10^6)*(m/areaunder pv plot).                       
#Demonstrating file parsing in python
#Luv Kumar
import matplotlib.pyplot as plt
import math
import numpy as np
from prettytable import PrettyTable

filename = input('enter file name')
if filename.endswith('.out'):
	
	crank = []
	pressure = []
	max_pressure = []
	min_pressure = []
	mean_temp =[]
	max_temp = []
	min_temp= []
	volume = []
	mass= []
	density = []
	integrated_HR = []
	HR_rate = []
	C_p = []
	C_v = []
	gamma = []
	kin_visc = []
	Dyn_visc = []



	for line in  open('engine_data.out'):
		if '#' not in line:
			crank.append(float(line.split()[0]))
			pressure.append(float(line.split()[1]))
			max_pressure.append(float(line.split()[2]))
			min_pressure.append(float(line.split()[3]))
			mean_temp.append(float(line.split()[4]))
			max_temp.append(float(line.split()[5]))
			min_temp.append(float(line.split()[6]))
			volume.append(float(line.split()[7]))
			mass.append(float(line.split()[8]))
			density.append(float(line.split()[9]))
			integrated_HR.append(float(line.split()[10]))
			HR_rate.append(float(line.split()[11]))
			C_p.append(float(line.split()[12]))
			C_v.append(float(line.split()[13]))
			gamma.append(float(line.split()[14]))
			kin_visc.append(float(line.split()[15]))
			Dyn_visc.append(float(line.split()[16]))


	#taking units out of data

	units = []
	title = []
	#taking title from data

	linecount = 1
	for line in open('engine_data.out'):
		if linecount is 3:
			title.append(line.split())
		if linecount is 4:
			units.append(line.split())
		linecount=linecount +1

	t=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]


	print('All properties in given data')
	table = PrettyTable(['column','properties'])
	for i in range(0,len(t)):
		table.add_row([t[i],title[0][i+1]])
	

	print(table)




	a =[crank,pressure,max_pressure,min_pressure,mean_temp,max_temp,min_temp,volume,mass,density,integrated_HR,HR_rate,C_p,C_v,gamma,kin_visc,Dyn_visc]
	x = int(input("give the first column number."))
	y = int(input("give the second column number."))
	plt.plot(a[x],a[y],linewidth=3)
	plt.xlabel(title[0][x+1]+units[0][x+1])
	plt.ylabel(title[0][y+1]+units[0][y+1])
	plt.show()




	pressure = np.array(pressure)*1e6
	volume = np.array(volume)
	pvarea = np.trapz(pressure,volume)
	print(pvarea,"joules")

	#power output
	rpm = 1500
	timefor1rev = 60/1500 # time for one revolution in sec

	#one rev = 2strokes
	#one cycle = 4strokes
	#one cycle= 2revolution
	

	





	time1cycle= 2*timefor1rev
	power = pvarea/time1cycle
	print(power,'watt')
	print(power*0.00135962,'HP')



	#calculating specific fuel consumption (brakespecificfuelconsumption)

	m = 20*1e-6
	bsfc= 3.6*1e6*(m/pvarea)
	print(bsfc,'g/kw.H')

else:

	 print('file not found')
‚Äč

   When typed column no 8 and column no 2, we get above plot.

 

 

#Demonstrating file parsing in python
#Luv Kumar
import matplotlib.pyplot as plt
import math
import numpy as np
from prettytable import PrettyTable

filename = input('enter file name\n')
if filename.endswith('.out'):
	
	crank = []
	pressure = []
	max_pressure = []
	min_pressure = []
	mean_temp =[]
	max_temp = []
	min_temp= []
	volume = []
	mass= []
	density = []
	integrated_HR = []
	HR_rate = []
	C_p = []
	C_v = []
	gamma = []
	kin_visc = []
	Dyn_visc = []



	for line in  open('engine_data.out'):
		if '#' not in line:
			crank.append(float(line.split()[0]))
			pressure.append(float(line.split()[1]))
			max_pressure.append(float(line.split()[2]))
			min_pressure.append(float(line.split()[3]))
			mean_temp.append(float(line.split()[4]))
			max_temp.append(float(line.split()[5]))
			min_temp.append(float(line.split()[6]))
			volume.append(float(line.split()[7]))
			mass.append(float(line.split()[8]))
			density.append(float(line.split()[9]))
			integrated_HR.append(float(line.split()[10]))
			HR_rate.append(float(line.split()[11]))
			C_p.append(float(line.split()[12]))
			C_v.append(float(line.split()[13]))
			gamma.append(float(line.split()[14]))
			kin_visc.append(float(line.split()[15]))
			Dyn_visc.append(float(line.split()[16]))


	#taking units out of data

	units = []
	title = []
	#taking title from data

	linecount = 1
	for line in open('engine_data.out'):
		if linecount is 3:
			title.append(line.split())
		if linecount is 4:
			units.append(line.split())
		linecount=linecount +1

	t=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]


	print('All properties from given data')
	table = PrettyTable(['column','properties'])
	for i in range(0,len(t)):
		table.add_row([t[i],title[0][i+1]])
	

	print(table)




	a =[crank,pressure,max_pressure,min_pressure,mean_temp,max_temp,min_temp,volume,mass,density,integrated_HR,HR_rate,C_p,C_v,gamma,kin_visc,Dyn_visc]
	x = int(input("give the first column number."))
	y = int(input("give the second column number."))
	plt.plot(a[x-1],a[y-1],linewidth=3)
	plt.xlabel(title[0][x]+units[0][x])
	plt.ylabel(title[0][y]+units[0][y])
	plt.show()




	pressure = np.array(pressure)*1e6
	volume = np.array(volume)
	pvarea = np.trapz(pressure,volume)
	print(pvarea,"joules")

	#power output
	rpm = 1500
	timefor1rev = 60/1500 # time for one revolution in sec

	#one rev = 2strokes
	#one cycle = 4strokes
	#one cycle= 2revolution
	t=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17]


	





	time1cycle= 2*timefor1rev
	power = pvarea/time1cycle
	print(power,'watt')
	print(power*0.00135962,'HP')



	#calculating specific fuel consumption (brakespecificfuelconsumption)

	m = 20*1e-6
	bsfc= 3.6*1e6*(m/pvarea)
	print(bsfc,'g/kw.H')

else:
	 print('file not find')


Projects by LUV KUMAR

OBJECTIVE: To simulate the side crash analysis of the given neon model. STEPS: UNIT SYSTEM: During whole analysis, Kg KN mm ms was followed. INTERFACES: Deleted already defined interfaces and created Type 7 global interface. The properties for the Type 7 interface a Read more

FRONTAL CRASH ANALYSIS
LUV KUMAR · 2019-10-31 04:05:45

OBJECTIVE: To perform a frontal crash simulation using Radioss as a solver. STEPS: View components: In the NEON FRONT model there are 75 components. ASSIGNING MATERIAL ID, PROPERTY ID, AND CARD IMAGE: For rigid bodies, card image is none and prop id, mat id is uns Read more

OBJECTIVE: Studying Interfaces and boundary condition in Hypercrash, Hypermesh and meshing bumper assembly MESHING OF BUMPER ASSEMBLY: Topological error like faces not connected was removed using extend option under surface edit option. Auto mesh under 2D option wa Read more

OBJECTIVE: Through this challenge, we will study different material laws in Radioss and will select the best material law for the given scenario by simulating and plotting graph. STEPS CASE1: Changing the model name from FAILURE_JOHNSON_0000 to Law2_epsmax_failure.R Read more

OBJECTIVE: To compare the results with base simulation and improved shell elements properties by performing energy, mass error checks, plotting graph in HyperGraph and animating in HyperView. SIMULATION 1: With default Shell Properties. Imported Element_Formulati Read more

OBJECTIVE: To clean the geometry, extract mid surface, mesh the extracted surface and assign thickness to given models. LINK TO HYPERMESH FILES https://drive.google.com/file/d/1Op9RLc8y85U2qtlLyBNh5GYCglVHuNqN/view   PROCEDURE: Model was imported and to see Read more

OBJECTIVE: Surface mesh and volume mesh generation on given housing and arm bracket model. LINK TO HYPER MESH RADIOSS FILES: https://drive.google.com/file/d/1h0tOn4b9e9EDoVFqaGuKCjnWRgrHxUvL/view 3D MESHING: 3D Elements should be used when all dimensions are compara Read more

NEWTON RAPHSON METHOD
LUV KUMAR · 2019-09-13 07:11:16

OBJECTIVE: To find minimum pressure using Newton-Raphson method PROGRAMMING EXPLAINED: Two functions are defined. f and fprime. These function are defined to calculate next iteration value using NR method. To calculate pressure values for different thickness, for a Read more

SciPy Curve Fitting
LUV KUMAR · 2019-09-12 14:32:22

OBJECTIVE: To curve fit given data using linear and cubic polynomial. For curve fitting, we imported curve_fit from scipy library. Defined two function, func and func1 which are used for fitting curve. Defined another function read_file to read temperature and cp v Read more


Loading...

The End