Parsing Nasa Thermodynamic Data
Aim:
To parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
Introduction:
Parsing in computer languages refers to syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation. 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.
After parsing the data, the same can be subjected to calculations and necessary values can be obtained. Here the data file input for data parsing is 'THERMO.dat', which is a set of thermodynamic data produced by NASA. These are polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
Definitions:
Specific Heat: The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius.
Entropy: Entropy is a thermodynamic quantity representing the unavailability of a system's thermal energy for conversion into mechanical work, often interpreted as the degree of disorder or randomness in the system.
Enthalpy: Enthalpy is a thermodynamic quantity equivalent to the total heat content of a system. It is equal to the internal energy of the system plus the product of pressure and volume.
Equations:
Below given is the set of equations used to find Specific heat, Entropy & Enthaply. The first 7 coefficients are high temperature coefficients and the second 7 coefficients are low temperature coefficients.
Program:
clear all
close all
clc
%Opening THERMO.dat File
f=fopen('THERMO.dat','r');
fgetl(f);
%Getting the Temperatures
templine=fgetl(f);
X=strsplit(templine,' ');
%Converting Strings to Numbers
temp_1=str2num(X{2});
temp_2=str2num(X{3});
temp_3=str2num(X{4});
%Displaying the global temperature values
Head='Global Temperature Values Are ';
fprintf('%s\n',Head);
disp(temp_1);
disp(temp_2);
disp(temp_3);
%Ignoring Next 3 Lines
fgetl(f);
fgetl(f);
fgetl(f);
%Creating Results Folder
mkdir('D:\Matlab\fileparse\results');
%Reading the coefficients of each Species
for i=1:53
Species=fgetl(f);
sp_name=Species(1:6);
%Molecular Weight SubProgram Begining
sol=split(sp_name,'');
Atoms={'C';'H';'O';'N';'A'};
Weight=[12,1,16,14,40];
M=0;
for i=1:length(sp_name)
for j=1:(length(Atoms))
if sol{i+1}==Atoms{j}
M=M+Weight(j);
y=j;
end
end
n=str2num(sol{i+1});
if n>1
M=M+Weight(y)*(n-1);
end
end
fprintf('Molecular Weight of ')
disp(sp_name)
fprintf('is')
disp(M);
%Molecular Weight Pg End
%Obtaining Local Temperatures for Species
temp_min=Species(49:55);
temp_high=Species(58:65);
temp_avg=Species(68:75);
temp_m =str2num(temp_min);
temp_h=str2num(temp_high);
temp_a=str2num(temp_avg);
%Obtaining High Temperature Coefficients for Individual Species
line1=fgetl(f);
A=strfind(line1,'E');
a1=str2double(line1(1:A(1)+3));
a2=str2double(line1(A(1)+4:A(2)+3));
a3=str2double(line1(A(2)+4:A(3)+3));
a4=str2double(line1(A(3)+4:A(4)+3));
a5=str2double(line1(A(4)+4:A(5)+3));
line2=fgetl(f);
B=strfind(line2,'E');
a6=str2double(line2(1:B(1)+3));
a7=str2double(line2(B(1)+4:B(2)+3));
%Obtaining High Temperature Coefficients for Individual Species
a8=str2double(line2(B(2)+4:B(3)+3));
a9=str2double(line2(B(3)+4:B(4)+3));
a10=str2double(line2(B(4)+4:B(5)+3));
line2=fgetl(f);
C=strfind(line2,'E');
a11=str2double(line2(1:C(1)+3));
a12=str2double(line2(C(1)+4:C(2)+3));
a13=str2double(line2(C(2)+4:C(3)+3));
a14=str2double(line2(C(3)+4:C(4)+3));
R=8.314;
t=linspace(temp_m,temp_h,500);
%Evaluating Values of Cp,H & S
for j=1:500
if t>temp_a
cp=(a1+a2.*t+(a3.*t.^2)+(a4.*t.^3)+(a5.*t.^4)).*R;
h=(a1+(a2.*t./2)+(a3.*(t.^2)./3)+(a4.*(t.^3)./4)+(a5.*(t.^4)./5)+(a6./t)).*R.*t;
s=(a1*log(t)+a2.*t+(a3.*(t.^2)./2)+(a4.*(t.^3)./3)+(a5.*(t.^4)./4)+a7).*R;
else
cp=(a8+a9.*t+(a10.*t.^2)+(a11.*t.^3)+(a12.*t.^4)).*R;
h=(a8+(a9.*t./2)+(a10.*(t.^2)./3)+(a11.*(t.^3)./4)+(a12.*(t.^4)./5)+(a13./t)).*R.*t;
s=(a8*log(t)+a9.*t+(a10.*(t.^2)./2)+(a11.*(t.^3)./3)+(a12.*(t.^4)./4)+a14).*R;
end
end
%Plotting Specific Heat, Enthaply & Entropy Vs Temperature
figure(1);
plot(t,cp,'color','r');
xlabel('Temperature [K]');
ylabel('Specific Heat [kJ/kgK]');
title('Specific Heat Vs Temperature');
figure(2);
plot(t,h,'color','b');
xlabel('Temperature [K]');
ylabel('Enthalpy [kJ/mol]');
title('Enthalpy Vs Temperature');
figure(3);
plot(t,s,'color','k');
xlabel('Temperature [K]');
ylabel('Entropy [kJ/molK]');
title('Entropy Vs Temperature');
%Creating New Directory
mkdir('D:\Matlab\fileparse\results\',sp_name)
directory=(['D:\Matlab\fileparse\results\',sp_name]);
cd(directory);
saveas(1,'Specific Heat.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Entropy.jpg');
directory2=(['D:\Matlab\fileparse\']);
cd(directory2);
end
fclose(f);
%End Of Program
Program Explanation:
- Firstly the thermodynamic data file 'THERMO.dat' was opened using fopen command and file handler'f'. fgetl command is used to read the lines.
- The second line contained the Global Temperature values, so it was assigned to a string 'templine' and strsplit command with space as delimiter was used to obtain individual temperatures. str2num function was used to convert these strings into numbers.
- The Global Temperature values are then displayed. The next 3 lines are ignored.
The next line contains the Species' Name and its input into a string named 'sp_name'. Molecular weight of the corresponsing specie is displayed. - Local Temperatures of corresponding species is derived from the string and converted to numbers.
- All the 14 coefficients of the particular species, both high and low temperature coefficients are derived. The strfind function is used to find the position of 'E' in the string and the coefficients are seperated with respect to the position of E.
- An array of 500 values between minimum and maximum temperature is made using linspace command.
- An if else condition is used to get the corresponding values of Specific Heat, Enthalpy & Entropy with respect to the value of temperature. If the temperature value is above Average temperature then high temperature coefficents are used, else low temperature values. The respective coefficients are substituted into the corresponding equations.
- After all the values of Cp,H & S for corresponding temperature values are obtained, they are plotted against temperature to get Specific heat vs Temperature, Enthaply vs Temperature & Entropy vs Temperature graphs. New folder with the corresponding specie's name is created and the 3 plots are saved into this very folder.
- The directory is then changed to the previous folder before next iteration begins.
The above codes from line 6 to line 14 are put into a loop which repeats 53 times(number of species in this case) and plots and molecular weights for each species is obtained. - Molecular weights are displayed in the command window during respective iterations.
Results:
1.Directory Folder
2.Molecular Weights in Command Window
3.Plots
- O2
- N2
- CO2
Errors Encountered:
The error occured due to an invalid command savea instead of saveas.
REFERENCES:
Mathworks
Wikipedia