Parsing of NASA Thermodynamic File using MATLAB programming
Aim:- To parse NASA Thermodynamic data by MATLAB programming
Governing Equations:- Thermodynamic properties equations those play main roles:
Cp/R= a1+a2T+a3T^2+a4T^3+a5T^4
H/RT=a1+a2T/2+a3T^2/3+a4T^3/4+a5T^4/5+a6/T
S/R=a1lnT+a2T+a3T^2/2+a4T^3/3+a5T^4/4+a7
where R=universal gas constant
T=temperature
Cp=specific heat
H=enthalpy
S=entropy
Objectives:-
The main objectives of this project are
(i)to write code for parsing the NASA thermodynamic file
(ii)to write a function to extract the 14 coefficients and calculate enthalpy, entropy, specific heats, for every specie in the given data file.
(iii)to calculate the molecular weight for each specie (iv)to plot CP, entropy, enthalpy graph by Matlab programming and save those plots.
Brief theory:-
Specific heat(Cp):-The amount of heat required to raise the temperature of 1 gram of the substance by 1 degree kelvin is called specific heat.
Enthalpy (H):-The sum of the internal energy of the system and the product of the pressure of the gas in the system and its volume is known as enthalpy.
Entropy (S):-Entropy is defined as the quantitative measure of disorder or randomness of the system.
Universal gas constant(R):- Value of R is 8.3145 joule per mole.
File parsing:- In this project extraction of useful information is called as file parsing.We are using those pieces of information for programming projects.
Workflow:-
(i) The main program has been written whose algorithm is given later.
(ii) The function code for the specific heat calculation is written.
(iii)The function code for enthalpy calculation is written.
(iv) The function code for entropy calculation is written.
(v) The function code for molecular weight calculation is written.
Brief content of this project:- This project has been done by MATLAB programming the code and functions have been attached below.
Code for the program:-
%Parsing of NASA thermodynamic data by MATLAB programming
close all
clear all
clc
%opening of external file
f1= fopen('THERMO.dat','r')
line_1= fgetl(f1); %skipped
line_2= fgetl(f1);
%taking global temperature values
global_values= strsplit(line_2,' ');
g_low_temp= str2num(global_values{2})
g_mid_temp= str2num(global_values{3})
g_high_temp= str2num(global_values{4})
line_3= fgetl(f1);%skipping the line
line_4= fgetl(f1);%skipping the line
line_5= fgetl(f1);
%taking universal gas constant (KJ/K-mol-K)
R= 8.314;
for i= 1:53
T= fgetl(f1);
%extraction of the species name
species_name= T(1:9);
species_name= deblank(species_name); %removing blank spaces
%extraction of the local temperatures
species_temp= T(49:75);
local_values= strsplit(species_temp,' ');
l_low_temp= str2num(local_values{1});
l_mid_temp= str2num(local_values{3});
l_high_temp= str2num(local_values{2});
% local temperatures storing
temp= [l_low_temp l_mid_temp l_high_temp];
temp_range=linspace(l_low_temp,l_high_temp,500);
coeff= fgetl(f1);
coeff1= findstr(coeff,'E');
%extraction of the coefficients
a1= coeff(1:coeff1(1)+3);
a1= str2num(a1);
a2= coeff(coeff1(1)+4:coeff1(2)+3);
a2= str2num(a2);
a3= coeff(coeff1(2)+4:coeff1(3)+3);
a3= str2num(a3);
a4= coeff(coeff1(3)+4:coeff1(4)+3);
a4= str2num(a4);
a5= coeff(coeff1(4)+4:coeff1(5)+3);
a5= str2num(a5);
new_line= fgetl(f1);
coeff2= findstr(new_line,'E');
a6= new_line(1:coeff2(1)+3);
a6= str2num(a6);
a7= new_line(coeff2(1)+4:coeff2(2)+3);
a7= str2num(a7);
a8= new_line(coeff2(2)+4:coeff2(3)+3);
a8= str2num(a8);
a9= new_line(coeff2(3)+4:coeff2(4)+3);
a9= str2num(a9);
a10= new_line(coeff2(4)+4:coeff2(5)+3);
a10= str2num(a10);
new_line1= fgetl(f1);
coeff_3= findstr(new_line1,'E');
a11= new_line1(1:coeff_3(1)+3);
a11= str2num(a11);
a12= new_line1(coeff_3(1)+4:coeff_3(2)+3);
a12= str2num(a12);
a13= new_line1(coeff_3(2)+4:coeff_3(3)+3);
a13= str2num(a13);
a14= new_line1(coeff_3(3)+4:coeff_3(4)+3);
a14= str2num(a14);
%calculating the specific heat
Cp= specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,temp_range,R);
%calculating the enthalpy
H= enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a2,a13,temp,temp_range,R);
%calculating the entropy
S= entropy_1(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temp_range,R);
%calculating the molecular weight
m_weight= weight(species_name);
species_w= fopen('species_mass.txt','w');
fprintf(species_w,'molecular mass of %s is %f',species_name,m_weight);
fclose(species_w);
%plotting of graph and save
cur_dir=pwd;
%new folder making(each species)
mkdir(species_name);
cd(species_name);
figure(1)
plot(temp_range,Cp,'linewidth',2,'color',[0.2345 0.4567 0.5678])
xlabel('Temperature(K)');
ylabel('Specific heat(KJ/kg-K)');
title(species_name);
figure(2),
plot(temp_range,H,'LineWidth',2,'color',[0.5678 0.3423 0.7874])
xlabel('Temperature(K)');
ylabel('Enthalpy(KJ/Kg)');
title(species_name);
figure(3)
plot(temp_range,S,'LineWidth',2,'color',[0.4532 0.1234 0.3324])
%defining x axis and y axis values
xlabel('Temperature(K)');
ylabel('Entropy(KJ/K)');
%giving title name
title(species_name);
%plots are saving(different jpg form)
saveas(1,'specific heat.jpg');
saveas(2,'enthalpy.jpg');
saveas(3,'entropy.jpg');
cd(cur_dir);
%molecular weight printing in the output screen
sprintf('molecular weight of %s = %f',species_name,m_weight)
end
%closing file
fclose(f1)
Function code for calculation of specific heat:-
%defining function name
function[Cp]= specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,temp_range,R)
%using for loop
for i= 1:length(temp_range)
%using if else condition
if temp_range(i)<=temp(2)
Cp(i)= R*(a8 + (a9*temp_range(i)) + (a10*(temp_range(i).^2)) + (a11*(temp_range(i).^3)) + (a12*(temp_range(i).^4)));
else
Cp(i)= R*(a1 + (a2*temp_range(i)) + (a3*(temp_range(i).^2)) + (a4*(temp_range(i).^3)) + (a5*(temp_range(i).^4)));
end
end
Function code for enthalpy calculation:-
%defining function name
function[H]= enthaply(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,temp_range,R)
%using for loop
for i= 1:length(temp_range)
%using if else condition
if temp_range(i)<=temp(2)
H(i)= R*((a8*temp_range(i)) + (a9*((temp_range(i).^2)/2)) + (a10*((temp_range(i).^3)/3)) + (a11*((temp_range(i).^4)/4)) + (a12*((temp_range(i).^5)/5)) + a13);
else
H(i)= R*((a1*temp_range(i)) + (a2*((temp_range(i).^2)/2)) + (a3*((temp_range(i).^3)/3)) + (a4*((temp_range(i).^4)/4)) + (a5*((temp_range(i).^5)/5)) + a6);
end
end
Function code for entropy calculation:-
%defining function name
function[S]= entropy_1(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temp_range,R)
%using for loop
for i= 1:length(temp_range)
%using if else condition
if temp_range(i)<=temp(2)
S(i)= R.*((a8*log(temp_range(i))) + (a9*temp_range(i)) + (a10*((temp_range(i).^2)/2)) + (a11*((temp_range(i).^3)/3)) + (a12*((temp_range(i).^4)/4)) + a14);
else
S(i)= R.*((a1*log(temp_range(i))) + (a2*temp_range(i)) + (a3*((temp_range(i).^2)/2)) + (a4*((temp_range(i).^3)/3)) + (a5*((temp_range(i).^4)/4)) + a7);
end
end
Function code for molecular weight calculation:-
%defining function name
function[m_weight]= weight(species_name)
element= {'H','O','N','Ar','C','S'};
atomic_mass= [1.008, 15.999, 14.007, 39.948, 12.011, 32.065];
m_weight=0;
%using for loop
for i= 1:length(species_name)
for j= 1:length(element)
%using if else condition
if strcmp(species_name(i),element(j))
m_weight= m_weight + atomic_mass(j);
a= j;
end
end
b= str2num(species_name(i));
if b>1
m_weight= m_weight + atomic_mass(a)*(b-1);
end
end
end
Algorithm of the program:-
(i)Firstly the screen is cleared.
(ii)Using fopen command the NASA thermodynamic file is opened and fgetl command to read every line in the file.
(iii)Individual specific data is extracted from all the species that are present in the file.
(iv) Obtaining global temperature values and storing them as numbers in separate values have been done. Line 3 and 4 have skipped.
(v) Universal gas constant value is put.
(vi)'for' loop has been used to the whole process.
(vii) Function to calculate specific heat enthalpy and entropy molecular weight is written later using governing equations.
(viii) Specific heat vs temperature, enthalpy vs temperature, entropy vs temperature for all 53 species has been created and saved. x label and y label command is used to write x-axis and y-axis purpose.
(ix) Using saveas command plots are saved and end of the program.
(x) Using fclose command file is closed.
Algorithm for functions:-
(i)Every function has been separately named.
(ii) if, else condition has been used to code this.
(iii) end of the functions.
Folders locations:-
Result:-
Plots:-
Plots of O2
Plots of CO2
Plots of N2
Conclusion:-
Through this whole project, the main aim to parse NASA thermodynamic file has been fulfilled. This program and project are very helpful to understand many aircraft and mechanical projects and to implement it in industrial works.