PARSING NASA THERMODYNAMIC DATA
AIM :
To write a code in MATLAB to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.
TOOLS REQUIRED :
MATLAB
THEORY :
File Parsing
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.
Parser
A parser is a software component that takes input data (frequently text) and builds a data structure – often some kind of parse tree, abstract syntax tree or other hierarchical structure, giving a structural representation of the input while checking for correct syntax. The parsing may be preceded or followed by other steps, or these may be combined into a single step. The parser is often preceded by a separate lexical analyser, which creates tokens from the sequence of input characters; alternatively, these can be combined in scannerless parsing. Parsers may be programmed by hand or may be automatically or semi-automatically generated by a parser generator. Parsing is complementary to templating, which produces formatted output. These may be applied to different domains, but often appear together, such as the scanf/printf pair, or the input (front end parsing) and output (back end code generation) stages of a compiler.
Specific Heat
The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius. The relationship between heat and temperature change is usually expressed in the form shown below where c is the specific heat. The relationship does not apply if a phase change is encountered, because the heat added or removed during a phase change does not change the temperature.
`Q = mcDeltaT`
where,
Q - Amount of heat added
m - mass
c- specific heat
`DeltaT` - change in temperature
Enthalpy
Enthalpy, the sum of the internal energy and the product of the pressure and volume of a thermodynamic system. Enthalpy is an energy-like property or state function—it has the dimensions of energy (and is thus measured in units of joules), and its value is determined entirely by the temperature, pressure, and composition of the system and not by its history. In symbols, the enthalpy, H, equals the sum of the internal energy, E, and the product of the pressure, P, and volume, V, of the system
`H = E+PV`
Entropy
Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system. The concept of entropy provides deep insight into the direction of spontaneous change for many everyday phenomena. Its introduction by the German physicist Rudolf Claussius in 1850 is a highlight of 19th-century physics.
Formulae used in the program to calculate cp, H and S are,
where,
a1,a2,a3,a4,a5,a6,a7 - co-efficients of the polynomials
T - Temperature
R - Universal gas constant = 8.314 J/mol-K
PROGRAM CODE :
Case I
Code to parse data from 'THERMO.dat' file, calculate the properties and molecular weight and to store the plots of each species in separate folders
clear all
close all
clc
%opening the NASA file
f1 = fopen('THERMO.dat','r');
%parsing the global temperature range
fgetl(f1);
tline = fgetl(f1);
A = strsplit(tline,' ');
for i=2:length(A)
G(i-1) = str2double(A{i});
end
%displaying the global temperature range
head = 'Global Temperature values are :';
fprintf('%s\n',head);
disp(G(1));
disp(G(2));
disp(G(3));
%ignoring the next 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);
%parsing the coefficients of each species
for k=1:53
tline = fgetl(f1);
a = strsplit(tline,' ');
Sp = a{1};
%parsing the local temperature range
g = strfind(tline,'G ');
W = tline(g:length(tline));
K = strsplit(W,' ');
L(1) = str2num(K{2});
L(2) = str2num(K{3});
L(3) = str2num(K{4});
for i=1:5:11
tline = fgetl(f1);
a = strfind(tline,'E');
c1 = tline(1:a(1)+3);
C(i) = str2double(c1);
for j=2:length(a)
p = tline(a(j-1)+4:a(j)+3);
C(j+i-1) = str2double(p);
end
end
%creating the temperature array
temp = linspace(L(1),L(2),1000);
for i=1:1000
if temp(i)<= L(3)
[x y z] = thermo_prop(C(8:14),temp(i));
else
[x y z] = thermo_prop(C(1:7),temp(i));
end
cp(i) = x;
H(i) = y;
S(i) = z;
end
old = pwd;
mkdir ('D:\JERRY\Results'); %creating new director
mkdir ('D:\JERRY\Results\',Sp);
cd(['D:\JERRY\Results\',Sp]);
figure(1)
plot(temp,cp,'linewidth',3,'color','r')
title('Specific Heat vs Temperature')
xlabel('Temperature [K]')
ylabel('Specific Heat [J/kg K]')
saveas(figure(1),'Specific Heat.png')
figure(2)
plot(temp,H,'linewidth',3,'color','b')
title('Enthalpy vs Temperature')
xlabel('Temperature [K]')
ylabel('Enthalpy [J]')
saveas(figure(2),'Enthalpy.png')
figure(3)
plot(temp,S,'linewidth',3,'color','g')
title('Entropy vs Temperature')
xlabel('Temperature [K]')
ylabel('Entropy [J/K]')
saveas(figure(3),'Entropy.png')
cd(old)
%calculating molecular weight
elements = ['C','H','O','N','A'];
atomic_mass = [12.011 1.008 15.999 14.007 39.948];
Mol_wt = 0;
for i=1:length(Sp)
for j=1:length(elements)
if strcmp(Sp(i),elements(j))== 1
Mol_wt = Mol_wt + atomic_mass(j);
k=j;
end
end
num = str2double(Sp(i));
if(num>1)
Mol_wt = Mol_wt + (atomic_mass(k)*(num-1));
end
end
%displaying the molecular weight
K = sprintf('Molecular weight of %s is %f',Sp,Mol_wt);
disp(K)
end
Function to calculate the thermodynamic properties
function [X Y Z] = thermo_prop(co_effs,T)
a1 = co_effs(1);
a2 = co_effs(2);
a3 = co_effs(3);
a4 = co_effs(4);
a5 = co_effs(5);
a6 = co_effs(6);
a7 = co_effs(7);
R = 8.314;
X = (a1 + a2*T + a3*(T^2) + a4*(T^3) + a5*(T^4))*R;
Y = (a1 + (a2*(T/2)) + (a3*((T^2)/3)) + (a4*((T^3)/4)) + (a5*((T^4)/5)) + (a6/T))*R*T;
Z = (a1*log(T) + a2*T + a3*((T^2)/2) + a4*((T^3)/3) + a5*((T^4)/4) + a7)*R;
end
Case II
Program to calculate the thermodynamic properties of the species which the user inputs
clear all
close all
clc
%opening the NASA file
f1 = fopen('THERMO.dat','r');
%parsing the global temperature range
fgetl(f1);
tline = fgetl(f1);
A = strsplit(tline,' ');
for i=2:length(A)
G(i-1) = str2double(A{i});
end
%displaying the global temperature range
head = 'Global Temperature values are :';
fprintf('%s\n',head);
disp(G(1));
disp(G(2));
disp(G(3));
%ignoring the next 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);
%getting the input from user
prompt = 'Enter the species :';
str = input(prompt,'s');
ct = 0;
while ~feof(f1)
tline = fgetl(f1);
a = strsplit(tline,' ');
Sp = a{1};
%comparing the user input with the species
%parsing the local temperature range
if strcmpi(str,Sp) == 1
g = strfind(tline,'G ');
W = tline(g:length(tline));
K = strsplit(W,' ');
L(1) = str2num(K{2});
L(2) = str2num(K{3});
L(3) = str2num(K{4});
%parsing the coefficients
for i=1:5:11
tline = fgetl(f1);
a = findstr(tline,'E');
c1 = tline(1:a(1)+3);
C(i) = str2double(c1);
for j=2:length(a)
p = tline(a(j-1)+4:a(j)+3);
C(j+i-1) = str2double(p);
end
end
%creating the temperature array
temp = linspace(L(1),L(2),1000);
for i=1:1000
if temp(i)<= L(3)
[x y z] = thermo_prop(C(8:14),temp(i));
else
[x y z] = thermo_prop(C(1:7),temp(i));
end
cp(i) = x;
H(i) = y;
S(i) = z;
end
oldFolder = cd('D:\JERRY');
mkdir(Sp); %creating new directory
cd(Sp);
figure(1)
plot(temp,cp,'linewidth',3,'color','r')
title('Specific Heat vs Temperature')
xlabel('Temperature [K]')
ylabel('Specific Heat [J/kg K]')
saveas(figure(1),'Specific Heat.png')
figure(2)
plot(temp,H,'linewidth',3,'color','b')
title('Enthalpy vs Temperature')
xlabel('Temperature [K]')
ylabel('Enthalpy [J]')
saveas(figure(2),'Enthalpy.png')
figure(3)
plot(temp,S,'linewidth',3,'color','g')
title('Entropy vs Temperature')
xlabel('Temperature [K]')
ylabel('Entropy [J/K]')
saveas(figure(3),'Entropy.png')
%calculating molecular weight
elements = ['C','H','O','N','A'];
atomic_mass = [12.011 1.008 15.999 14.007 39.948];
Mol_wt = 0;
for i=1:length(Sp)
for j=1:length(elements)
if strcmp(Sp(i),elements(j))== 1
Mol_wt = Mol_wt + atomic_mass(j);
k=j;
end
end
num = str2double(Sp(i));
if(num>1)
Mol_wt = Mol_wt + (atomic_mass(k)*(num-1));
end
end
%displaying the molecular weight
K = sprintf('Molecular weight of %s is %f',Sp,Mol_wt);
disp(K)
ct = 1;
cd(oldFolder)
break;
end
end
if ct==0
disp('Species not found.')
else
disp('Species found.')
end
Code Explanation
Case I
- First, the thermodynamic data file is opened using fopen() command.
- fgetl() command is used to read the successive lines.
- The second line is read using fgetl() command and stored in a string variable tline.
- Using strsplit() command with ' ' as delimiter, tline is split into a cell array containing the global temperature values and is converted into numbers by using str2double() command.
- The global temperature values are displayed.
- The next three lines are ignored as it contains no relevant information.
- The next line contained the species name and local temperature range.The entire line is stored to the character array 'tline' and is splitted using strsplit() command and the species name 'Sp' and the local temperature range are extracted.
- The program is moved to next line by fgetl() command where the co-efficients are written.
- The co-efficients are extracted one by one using for loop and is stored to a row array.
- Next, the temperature array is created using linspace() command starting from low temperature to high temperature.
- The thermo_prop() function is called to calculate the thermodynamic properties cp,H and S according to the condition. The values returned by the function are stored in separate arrays for cp,H and S.
- A new directory is made using mkdir() command for making a folder in the name of the species to save the plots.
- The cp,H and S plots are plotted using plot(0 command.
- Molecular weight of the species is also calculated and displayed using sprintf() and disp() commands.
- The steps VII to XIV are repeated 53 times to plot the graphs of each species using a for loop. Molecular weight of each species are displayed in the command window.
Case II
- In this case, the species whose graphs are to be plotted is input from the user and is stored into a character array named 'str'.
- The program compares the user's input with each species using strcmpi() function and if a match is made, the program proceeds and calculates the thermodynamic properties and the plots will be generated.
- Otherwise, the program prints a message 'Species not found' in the command window.
RESULTS :
Screenshot of the Results folder
Molecular weight displayed in command window
Plots of O2
- Specific Heat vs. temperature
- Enthalpy vs. Temperature
- Entropy vs. Temperature
Plots of N2
- Specific Heat vs. temperature
- Enthalpy vs. Temperature
- Entropy vs. Temperature
Plots of CO2
- Specific Heat vs. temperature
- Enthalpy vs. Temperature
- Entropy vs. Temperature
REFERENCES :