Parsing NASA thermodynamic data
OBJECTIVE
To write a code to parse NASA thermodynamic data file and calculate the thermodynamic properties like specific heat ,enthalpy and entropy. That is,
- Write a function to extract 14 coefficients and calculate the thermodynamic properties of all species in the data file.
- Calculate the Molecular weight of each species and display it in the command window.
- Plot Cp,Enthaply and Entropy Vs Local Temperature specific for each species.
THEORY
File parsing deals with how information can be read from a file and how it can be implemented in the program.File parsing refers to analysing a string or symbols in a data file by splitting the data so that it can be used in the program.
TERMINOLOGIES:
Specific Heat: Amount of Heat per unit mass required to raise the temperature by one degree Celsius.
Enthalpy: It is the total heat content of a system.
Entropy: A measure of unavailable energy in a closed thermodynamic system.
Atomic Mass: The mass of an atom in atomic mass unit. It is equivalent to the number of neutrons and protons in the atom.
Molecular Weight: Total Weight of the given molecule. It is the sum of masses of all atoms present.

where,
T is the temperature
R is Universal Gas Coefficient
Cp: Specific Heat
H :Enthalpy
S: Entropy
The first 7 coefficients are hight temperature coefficients and the second 7 are low temperature coefficients in the given thermodynamic file.
CODE
clear all
close all
clc
thermo_data=fopen('THERMO.dat','r');
fgetl(thermo_data);
%extracting the global temperatures
temp=fgetl(thermo_data);
global_temp=strsplit(temp,' '); %spliting the strings
global_low_temp=str2num(global_temp{2});
global_mid_temp=str2num(global_temp{3});
global_high_temp=str2num(global_temp{4});
%ignoring the comment lines
for i=1:3
fgetl(thermo_data);
end
for i=1:53 %we have 53 species
%extracting the local temperatures
line_1=fgetl(thermo_data);
local_temp=strsplit(line_1,' ');
species_name=local_temp{1}; %extracting the species name
local_low_temp=str2num(local_temp{end-3});
local_mid_temp=str2num(local_temp{end-1});
local_high_temp=str2num(local_temp{end-2});
%extracting the species data
line_2=fgetl(thermo_data);
a=findstr(line_2,'E'); %finding the string E
a1=str2num(line_2(1:a(1)+3));
a2=str2num(line_2(a(1)+4:a(2)+3));
a3=str2num(line_2(a(2)+4:a(3)+3));
a4=str2num(line_2(a(3)+4:a(4)+3));
a5=str2num(line_2(a(4)+4:a(5)+3));
line_3=fgetl(thermo_data);
a=findstr(line_3,'E'); %finding the string E
a6=str2num(line_3(1:a(1)+3));
a7=str2num(line_3(a(1)+4:a(2)+3));
a8=str2num(line_3(a(2)+4:a(3)+3));
a9=str2num(line_3(a(3)+4:a(4)+3));
a10=str2num(line_3(a(4)+4:a(5)+3));
line_4=fgetl(thermo_data);
a=findstr(line_4,'E'); %finding the string E
a11=str2num(line_4(1:a(1)+3));
a12=str2num(line_4(a(1)+4:a(2)+3));
a13=str2num(line_4(a(2)+4:a(3)+3));
a14=str2num(line_4(a(3)+4:a(4)+3));
%defining the local temperature ranges
T=linspace(local_low_temp,local_high_temp,100);
R=8.314; %universal gas constant
specefic_heat=R*spec_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temp,T);
enthalpy=R*enth(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_mid_temp,T);
entropy=R*entro(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_mid_temp,T);
molecular_weight=mol_weight(species_name);
m=sprintf('molecular weight of %s is %f',species_name,molecular_weight); %displaying the molecular weights
disp(m)
%plotting
figure(1)
plot(T,specefic_heat)
xlabel('temperature (K)')
ylabel('specefic heat (J/Kg-K)')
title(sprintf('temperature vs Cp of %s',species_name))
figure(2)
plot(T,enthalpy)
xlabel('temperature (K)')
ylabel('enthalpy (J)')
title(sprintf('temperature vs H of %s',species_name))
figure(3)
plot(T,entropy)
xlabel('temperature (K)')
ylabel('entropy (J/K)')
title(sprintf('temperature vs S of %s',species_name))
mkdir('C:UsersABINDesktopMATLABweek_5nasa_fileNew folder',species_name) %making a directory
cd(species_name) %changing the directory
saveas(figure(1),'specefic_heat.png')
saveas(figure(2),'enthalpy.png')
saveas(figure(3),'enrtropy.png')
cd('C:UsersABINDesktopMATLABweek_5nasa_fileNew folder') %changing the directory-return
end
Function for enthalpy
%function for enthalpy
function[H]=enth(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,local_mid_temp,T)
if T>local_mid_temp
H=(a1*T)+(a2*(T.^2./2))+(a3*(T.^3./3))+(a4*(T.^4./4))+a5*(T.^5./5)+(a6);
else
H=(a8*T)+(a9*(T.^2./2))+(a10*(T.^4./3))+(a11*(T.^4./4))+a12*(T.^5./5)+(a13);
end
end
Function for entropy
%function for entropy
function[S]=entro(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,local_mid_temp,T)
if T>local_mid_temp
S=(a1*log(T))+(a2*(T))+(a3*(T.^2./2))+(a4*(T.^3./3))+a5*(T.^4./4)+(a7);
else
S=(a8*log(T))+(a9*(T))+(a10*(T.^2./2))+(a11*(T.^3./3))+a12*(T.^4./4)+(a14);
end
end
Function for specefic heat
function[C_p]=spec_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temp,T)
if T>local_mid_temp
C_p=a1+(a2*T)+(a3*T.^2)+(a4*T.^3)+(a5*T.^4);
else
C_p=a8+(a9*T)+(a10*T.^2)+(a11*T.^3)+(a12*T.^4);
end
end
Function for molecular weight
%molecular weight
function[M]=mol_weight(species_name)
elements=['O','H','C','N','A'];
atomic_weight=[15.999,1.008,12.011,14.007,39.948];
mol_w=0; %initializing
for i=1:length(species_name)
for j=1:length(elements)
if strcmp(species_name(i),elements(j))
mol_w=mol_w+atomic_weight(j);
p=j;
end
end
mol=str2num(species_name(i)); %since numbers arise in the species name
if mol>1
mol_w=mol_w+atomic_weight(p)*(mol-1);
end
end
M=mol_w;
end
CODE EXPLANATION
In the given challenge Nasa Thermodynamic data with file name THERMO.dat was given.We have to read the data from the given file and calculate Cp,H and S. There are 53 gas species with 14 coefficients for each defined under individual local temperature range.
- The fopen('THERMO.dat','r') command helps us to open the data file and read it, while he fgetl command enables us to read through the data file line by line.
- strsplit is used to split the above line whenever a space is detected and its stored in a cell array global_temp.Then using ‘str2num’ we convert the string to number and store in variables to obtain high,medium and low global temperature range.
- Comment lines in the data file are neglected by executing the fgetl command thrice, in a for loop.
- A for loop from 1 to 53 is initiated since there are 53 elements in the file .The whole file can be divided into 4 lines for each species, which are recurring. First line provides the species name and local temperature range and next three lines provide the coefficients. First the species name is identified and stored in a variable ‘species_name’. Then local temperature ranges and 14 coefficients of each species is stored into different variables for further calculation.
- Functions spec_heat,enth,entro and mol_weight are called and graphs are plotted and molecular weights are displayed using the disp() function.
-
mkdir helps in creating a directory and it is run inside the loop.
mkdir('C:UsersABINDesktopMATLABweek_5nasa_fileNew folder',species_name) helps to create directory for each species and give folder name respective ‘species_name’. cd command is used to change directory of each species.
-
Molecular Weigth is calculated with the function called mol_weight(species_name) and is stored in array M. In the function mol_weight(species_name) we used strcmp() . It is used to compare the species name with the elements used in the function.This command is case sensitive.
PLOTS
- O2



- N2



- CO2



RESULTS
- Command window displaying the molecular weights of the species

- Folder where the plots with appropriate names are saved automatically
