PARSING NASA THERMODYNAMICS DATA
- AIM:- Write a code in Matlab to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
- OBJECTIVE:-Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. Calculate the molecular weight of each species Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature)
- THEORY : File Parsing is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of formal grammar. Within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into its constituents, resulting in a parse tree showing their syntactic relation to each other, which may also contain semantic and other information. NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S using polynomials. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species. In short Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data. The program that can read the high and low temperature data from the given data file and calculate the Cp, H and S is given below.
In the following Program, we are using NASA Polynomial format 'THERMO.dat' file format is based very closely on the NASA(1971) file format
Using these local temperature values and coefficients of a particular species we can calculate the entropy, enthalpy and specific heat at a given temperature using the following equations:
Where,
CP = Specific Heat.
H = Enthalpy.
S= Entropy.
R=Gas Constant.
Here R = 8.314 is the universal gas constant, T is the temperature and a1 to a14 are the temperature coefficients. The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
MATLAB CODE FOR PARSING NASA THERMODYNAMICS DATA
%parsing nasa thermodynamics data
close all
clear all
clc
%inputs
R = 8.314
%reading the data file
f=fopen('THERMO.dat','r');
%reading header
fileheader=fgetl(f)
%getting the global maximum ,minimum , mid temperature
temp_range=fgetl(f);
global_temp=strsplit(temp_range,' ');
global_min_temp=str2num(global_temp{2});
global_mid_temp=str2num(global_temp{3});
global_max_temp=str2num(global_temp{4});
%defining working temp rang
t=linspace(global_min_temp,global_max_temp,500);
%skipping the three coments
for i=1:3
skip=fgetl(f);
end
%running loops for each species there are 53 species in the file
for i=1:53
%reading the first line that tells which species it is
%working temp range and no of molecules
line0=fgetl(f);
%%reading the name of the species
name=strtok(line0);
%reading the three line which contains 7 high temp coeff anf 7 low temp
%coeff
line1=fgetl(f);
line2=fgetl(f);
line3=fgetl(f);
% finding the position of E so that we can locate the coeff
a=strfind(line1,'E');
b=strfind(line2,'E');
c=strfind(line3,'E');
%extracting low temperature coeff
a1=str2num(line1(1:a(1)+3));
a2=str2num(line1(a(1)+3:a(2)+3));
a3=str2num(line1(a(2)+4:a(3)+3));
a4=str2num(line1(a(3)+4:a(4)+3));
a5=str2num(line1(a(4)+4:a(5)+3));
a6=str2num(line2(1:b(1)+3));
a7=str2num(line2(b(1)+4:b(2)+3));
%extracting low temperature coeff
b1=str2num(line2(b(2)+4:b(3)+3));
b2=str2num(line2(b(3)+4:b(4)+3));
b3=str2num(line2(b(4)+4:b(5)+3));
b4=str2num(line3(1:c(1)+3));
b5=str2num(line3(c(1)+4:c(2)+3));
b6=str2num(line3(c(2)+4:c(3)+3));
b7=str2num(line3(c(3)+4:c(4)+3));
%specific heat
[cp]=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,t,global_mid_temp);
%specific enthalpy
[h]=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,t,global_mid_temp);
%specific entropy
[s]=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,t,global_mid_temp);
%plotting
plotting(cp,h,s,t,name)
% calculate the molecular weight of each species
molecular_weight(i)=mol_wt(name);
end
%function plotting
function plotting(cp,h,s,t,name)
cd(pwd)
dir_name=name;
mkdir(dir_name);
figure(1)
title(sprintf('specific heat vs temperature for %s:',name))
plot(t,cp,'color','r','linewidth',3);
xlabel('temperature(K)');
ylabel('specific heat(J/KG)');
grid on;
figure(2)
plot(t,h,'color','r','linewidth',3);
title(sprintf('enthalpy vs temperature for %s:',name))
xlabel('temperature(K)');
ylabel('entropy(J/KG)');
grid on;
figure(3)
plot(t,s,'color','r','linewidth',3);
title(sprintf('entropy vs temperature for %s:',name))
xlabel('temperature(K)');
ylabel('enthalpy(J/KG)');
grid on
cd(dir_name)
saveas(figure(1),'specific heat vs temperature.png')
saveas(figure(2),'entropy vs temperature..png')
saveas(figure(3),'enthalpy vs temperature.png')
cd ..
end
code explanation :
first We have to load the NASA's data by using the MATLAB build in command,'fopen' The given data is formatted as a therm.dat file. the program the value of the universal gas constant R which is 8.314.
Then using the fopen command the file is used to open the file in the read mode specified by 'r' keyword in the fopen command.
The file is read line by line by using the fgetl command. The first line represents the title if the file so it is ignored.
The next line has the global maximum, global mid and global minimum values which is read bby using fgetl command and split into the three values by using the strsplit command and converted into numbers.
Then the temperature array is defined using linspace command as 500 values between the global minimum and global maximmum values.
The next three lines are comments in the file so they are ignored by using a for loop to iterate for 3 values.
Then a for loop is created to loop through the 53 species data given in the file and clculate the required values.
Inside the loop the fgetl command is used to read the next line of the file which represents the name of the species.
The name of the element is stored in a variable using strtok command.
Then the next 3 lines of the file is read which contains the high and low temperature data of the particular species.
The read lines are in the form of a string so they are converted into numbers using the str2num command and stored in specific variables so they can be used for further calculations.
The low temperatures values are stored in variables a1,a2,a2,a4,a5,a6 and a7 and the high temperature values are stored invariables b1,b2,b3,b4,b5,b6 and b7.
The next line is the reference to a function called specific heat, enthalpy, and entropy are calculated as separate functions and the coefficients, temperature vectors, constant R, and global mid temperature. which calculates and returns the value of specific heat enthaphy and entropy using the high and low temperature values as inputs.
The next line is the reference to a function that plots graphs using the given inputs.
Then we will create plots for specific heat v/s temperature, enthalpy v/s temperature and entropy v/s temperature for all the 53 species and these 3 plots will be saved in separate folders for each species.
The next line is the reference to a function that calculates the molecular weights of the different elements.
The next line is the reference to a function that writes the molecular weights of diferent elements to a file.
And After this we will display the molecular weight of all the species in the command window. The NASA thermdynamic data file is closed using the fclose command.
A function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. here using the ifelse conditional statement the temp values is compared with the global mid temperature values and the appropriate equation with the proper maximum or minimum temperature coefficients is selected
- function to calculate the specific heat
function[cp]=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,t,global_mid_temp);
if t<=global_mid_temp
cp=R*(a1+(a2.*t)+(a3.*(t.^2))+(a4.*(t.^3))+(a5.*(t.^4)));
else
cp=R*(b1+(b2.*t)+(b3.*(t.^2))+(b4.*(t.^3))+(b5.*(t.^4)));
end
- function to calculate the enthaphy
function [h]=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,t,global_mid_temp);
if t<=global_mid_temp
h=R*(a1.*t+a2.*(t.^2)./2+a3.*(t.^3)./3+a4.*(t.^4)./4+a5.*(t.^5)./5+a6);
else
h=R*(b1.*t+b2.*(t.^2)./2+b3.*(t.^3)./3+b4.*(t.^4)./4+b5.*(t.^5)./5+b6);
end
- function to calculate the entropy
function[s]=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,t,glocal_mid_temp);
if t<=glocal_mid_temp
s=R*(a1.*log(t)+a2.*t+a3.*(t.^2)./2+a4.*(t.^3)./3+a5.*(t.^4)./4+a7);
else
s=R*(b1.*log(t)+b2.*t+b3.*(t.^2)./2+b4.*(t.^3)./3+b5.*(t.^4)./4+b7);
end
function the calculate the molecular weight of element
function [MW]=mol_wt(name)
s={'H' 'C' 'N' 'O' 'R'};
fprintf('molecular weight of ');
fprintf(name);
fprintf(' : ');
%atomic weight of each indivisuals
atomicweight = [1 12 14 16 40 ];
MW = 0 ;
for i = 1:length(name)
for j = length(s)
if strcmp(name(i),s(j))
MW = MW + atomicweight(j);
%storing the position of matched element
position = j;
end
end
n=str2num(name(i));
if n > 1
MW = MW + atomicweight(j)*(n-1);
end
end
fprintf('%fd' , MW);
disp(' ');
end
- output result :
- output plot :
- plot specific heat,entropy,enthalpy w.r.t temp for 02:
- plot specific heat,entropy,enthalpy w.r.t temp for N2
- plot specific heat,entropy,enthalpy w.r.t temp for CO2
- CONCLUSION: By using the function for cp,h,s calculation and molecular weight is determined in the main program using the parsing method in matlab. All the plots and values for 53 species with respect to temperature are saved in a common directory file and all the molecular weight for all the species are determined.