Parsing NASA thermodynamic data
AIM
To write code in Matlab to parse the NASA thermodynamic data file and then calculate the thermodynamic properties of various gas species.
- To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
- Calculate the molecular weight of each species and display it in the command window.
- To Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species
- Save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All of these should be generated automatically.
Theory
NASA DATA
- In that datasheet, it contains a list of species with their initial and final temperature and with respective co-efficiency.
- Using that data calculating the enthalpy, entropy and specific heat in Matlab.
Formulas
DATA Parsing
Data parsing is a method where one string of data gets converted into a different type of data. So let's say you receive your data in raw HTML, a parser will take the said HTML and transform it into a more readable data format that can be easily read and understood.
Enthalpy
Enthalpy is the amount of heat absorbed by the system or the amount of heat expelled by the system as a result of a change in the system.
If enthalpy is higher than 0 then it is known as endothermic which means the heat is absorbed by the system.
If enthalpy is lower than 0 then it is known as exothermic which means expels the heat from the system.
Enthalpy is equal to the sum of the change in temperature and work done by the system.
Entropy
Entropy is an extensive property of the thermodynamic system. It is closely related to the number of microscopic configuration that is consistent with the microscope.
Entropy is the arrangement of energy amongst all the other possible ways and the probability of those microstates occurring. So a solid has low entropy because of limited ways that compact particles and energy can be arranged.
Specific Heat
Every substance required a unique amount of energy per unit mass to change the temperature of that substance by 1degree.The specific heat of a measure of this amount.
Main Program.
%Main Program
clear all
close all
clc
%Opening the Data and Reading it
F1= fopen('Data.txt','r');
Heading = fgetl(F1);
ln_2= fgetl(F1);
global_temp= strsplit(ln_2,' ');
global_low_temp= str2num(global_temp{2});
global_mid_temp= str2num(global_temp{3});
global_high_temp= str2num(global_temp{4});
%Skipping the comment lines
for i=1:3
comments= fgetl(F1);
end
%Inputting the element name
prompt= 'Enter the species name: ';
x= input(prompt,'s');
g=0;
%Reading information of all the species
%Reading Local Temperatures
while ~feof(F1)
ln_3= fgetl(F1);
b= strsplit(ln_3,' ');
species_name= b{1};
if (strcmp(b{1},'END'))
break;
end
I_L_T=str2double(b{end-3});
I_H_T=str2double(b{end-2});
I_M_T=str2double(b{end-1});
B= fscanf(F1,'%f');
B1=B(1:5);
B2=B(7:11);
B3=B(13:16);
BB=[B1;B2;B3];
R=8.3145;
low=I_L_T;
mid=I_M_T;
high=I_H_T;
T=(mid:1:high);
t=(low:1:mid);
%formula For calculating Specif heat enthalpy and entropy
cp1= (BB(1) + BB(2).*T + BB(3).*T.^2 + BB(4).*T.^3 + BB(5).*T.^4).*R;
H1= (BB(1) + BB(2).*T./2 + BB(3).*(T.^2)./3 + BB(4).*(T.^3)./4 + BB(5).*(T.^4)./5 + BB(6)./T).*T.*R;
S1= (BB(1).*log(T) + BB(2).*T + (BB(3).*(T.^2)./2) + (BB(4).*(T.^3)./3) + (BB(5).*(T.^4)./4 )+ BB(7)).*R;
cp2= (BB(8) + BB(9).*t + BB(10).*t.^2 + BB(11).*t.^3 + BB(12).*t.^4).*R;
H2= (BB(8) + BB(9).*t./2 + BB(10).*(t.^2)./3 + BB(11).*(t.^3)./4 + BB(12).*(t.^4)./5 + BB(13)./t).*t.*R;
S2= (BB(8).*log(t) + BB(9).*t + (BB(10).*(t.^2)./2) + (BB(11).*(t.^3)./3) + (BB(12).*(t.^4)./4) + BB(14)).*R;
% Molecular Weight of all the species
Molecular_Weight= molecular_weight(species_name);
fprintf('Molecular Weight of %s = %f \n',species_name,Molecular_Weight);
%Using If loop to Compare
if (strcmpi(x,species_name))
sprintf('Molecular weight of %s is:%f',species_name,Molecular_Weight)
break
%Using counter
g=g+1;
% Creating a new folder
mkdir(species_name)
%Plotting
h=figure(g);
hold on
plot(T,cp1,'linewidth',3)
plot(t,cp2,'linewidth',3)
grid on
title(species_name)
xlabel('Temperature(T)')
ylabel('Specific Heat(cp)')
legend('high temperature coeffs','low temperature coeffs')
k=figure(g+1);
hold on
plot(T,H1,'linewidth',3)
plot(t,H2,'linewidth',3)
grid on
title(species_name)
xlabel('Temperature(T)')
ylabel('Enthalpy(H)')
legend('high temperature coeffs','low temperature coeffs')
l=figure(g+2);
hold on
plot(T,S1,'linewidth',3)
plot(t,S2,'linewidth',3)
grid on
title(species_name)
xlabel('Temperature(T)')
ylabel('Entropy(S)')
legend('high temperature coeffs','low temperature coeffs')
%Saving the images
cd(species_name)
saveas(1,'T vs cp.jpg','jpeg');
saveas(2,'T vs H.jpg','jpeg');
saveas(3,'T vs S.jpg','jpeg');
g=g+2;
cd('..')
end
end
fclose(F1);
Calculating the Enthalpy, Entropy and specific heat.
Here in NASA data first seven coefficients are respected to high temperature and the next seven coefficients are respect to low-temperature coefficients. The coefficient for all the species is calculated using that data file, There are 14 coefficient data that are in the file from that using the following Formula calculating and plotting graph using Matlab Codes.
Specific Heat
Here a1 to a5 are in high temperature and a8 to a12 are in low temperature.
Enthalpy
Here a1 to a5 and a7 are used for high temperature and from a8 to a12 and a14 are used for low temperature.
Entropy
Here a1 to a6 are used for high temperature and from a8 to a13 are used for low temperature.
Explaining the Program
First, we have to get Data from the file so we are using Fopen.
F1= fopen('Data.txt','r');
Fopen(Name Of File, permission).
From the first line, we know about the heading of the data file and that is saved as Heading
Some comment has been used that,
global_temp= strsplit(ln_2,' ');
strsplit(str,delimiter) where str is the line in that separate using delimiter in the above program we have used space.
global_low_temp= str2num(global_temp{2})
These comments convert the string value to number value.
I_L_T=str2double(b{end-3});
This comment converts the text into double-precision value.
Following to next line, we found that Global maximum and minimum Temperatures so we saved that in respective names.
Next three lines are describing the comments of that data so I have to save in comments name
From here the main program starts in next line species name temperatures and coefficients in the following lines
B= fscanf(F1,'%f');
I am in need of floating values only so I have been used ‘%f’ which refers to a floating value.
And all the values have been saved, in that we need to check that any other data are also there, so in that line numbers also listed in that name so I separated those numbers.
From there I used formula in the main program itself to reduce complexity,
Then using function formula definition is stored in molecular weight function, this is used to calculate the weight of the molecules.
Using make directory command to create a new folder
mkdir(species_name)
Then plotting the graph using plot command and labeling the graph to look meaning full and easy to understand.
Using saveas comment to save the plot into the folder as jpg format
Then closing the data file in Matlab by Fclose commend.
Calling Function
function mw= molecular_weight(x)
mw=0;
b=0;
elements= ['H';'C';'N';'O';'A'];
atomic_mass= [1.0079;12.0107;14.0067;15.999;39.948];
for i=1:length(x)
for j=1:length(atomic_mass)
if strcmp(x(i),elements(j))
mw= mw+ atomic_mass(j);
b=j;
end
end
n=str2num(x(i));
if n>1
mw= mw +(atomic_mass(b)*(n-1));
end
end
end
Using this function we can calculate the molecular weight of the species.
Output
Molecular Weight of all Elements
The output of the command window into PDF
All Folders File
Output Graph of O2
Output Graph of N2
Output Graph of CO2
Conclusion
The NASA thermodynamic data file is parsing and then calculate the thermodynamic properties of various gas species is done using Matlab.