Parsing NASA thermodynamic data using MATLAB
AIM
1) To extract the coefficients, species and the temperature ranges from the given data file
2) To calculate specific feat, enthalpy and entropy for the given species using the defined formulas, applicable in their local temperature ranges.
3) To plot the properties obtained against temperature for different species and save them in the respective folder of each species.
4) To determine the molecular weight of each species.
FILE PARSING
In layman's terms, parsing means to make something understandable. In programming, it refers to the process of converting information represented in one form into another form to make it easier to work with. It is done by first partially analysing the data, understanding its underlying logic and structure and then representing that structure in the code.
In more technical terms, parsing refers to splitting a file or some other input into pieces of data that can be easily stored in the memory and then manipulated using precise set of instructions. Parsing a file means reading in some kind of a data stream and building a memory model of semantic content of that data.
OBJECTIVE
In this program, we would parse the NASA thermal data file. The main objective is to extract all the 14 coefficients for different species and use these coefficients to determine the various thermal properties of these species across their local temperature range. Another thing to be done is to create a separate folder for each species and save the plots of its thermal properties in the folder. And finally, we need to write a program to determine and display the molecular weight of each species.
The equations defined by NASA to determine the values of specific heat (`C_p`), enthalpy (H) and entropy (S) for different species are given below:
`C_P/R = a_1 + a_2*T+ a_3*T^2 +a_4*T^3+a_5*T^4`
`H/(R*T) = a_1 + a_2*T/2 + a_3*T^2/3 + a_4*T^3/4 + a_5*T^4/5 + a_6/T`
`S/R = a_1*lnT+a_2*T + a_3*T^2/2 + a_4*T^3/3 + a_5*T^4/4 + a_7`
here, R = universal gas constant = 8.314 `J*mol^-1*K^-1`
Note: In the equations there are 7 coefficients mentioned, from `a_1 to a_7`; however, there are 14 coefficients in total. First 7 coefficients, `a_1 to a_7` are for high temperature range, that is temperature between local medium and local high temperature whereas, last seven coefficients, that is `a_8 to a_14`are for low temperature range, that is temperature between local minimum and the local medium temperature.
Plots will be made considering both the temperature ranges.
PROGRAM
1) fopen is used to read the file containing the species and the coefficients
2) All the lines in the file are read and saved in the memory using the first for loop.
3) strsplit command is used to split the string (line) about the defined character an get the strings thus obtained in a cell array. These strings can be called just like elements in an array, except that parentheses {} are to be used instead of ().
4) The next for loop is used to get all the species from the file and the lines containing the coefficients. The line obtaining the species name is identified, isolated and strsplit is used to get the species.
5) From the same line, local temperature range for each species is also obtained. Strfind command is used to find letter 'G' in the string and use it to get the local minimum, medium and high temperature for each species. Str2double is used to convert the temperature obtained as strings into numerical values.
6) Same method as 5) is used to extract the coefficients for each species, by finding all 'E' in each line containing coefficients and then using them to obtain the values of these coefficients in numerical form using str2double command.
7) An array containing temperature points between local minimum and maximum temperature of each species in defined and stored in a variable T, to be used while plotting the properties.
8) The values of specific heat, enthalpy and entropy for each species, obtained using the given relations and the coefficients extracted from the file are then stored in different rows of an array called DATA.
9) A separate folder is created for each species using mkdir command.
10) Plots of specific heat, enthalpy and entropy are obtained against temperature and saved as images in the respective folder of each species by providing a file path and then saving using saveas command.
11) Function to calculate molecular mass for each species is called and the molecular mass is displayed on the command window.
clear all
close all
clc
file = fopen('thermodata','r');
%getting all the lines in the file
for i = 1:218 %total number of lines in the file
line{i} = fgetl(file);
end
%inputs
R = 8.314; %universal gas constant [J/mol/K]
A = strsplit(line{2}, ' ');
global_low_temp = str2num(A{2});
global_med_temp = str2num(A{3});
global_high_temp = str2num(A{4});
%getting all the species
for mol = 1:53 %53 species in the file
j = 6:4:218; %new species in every 4th line
l1 = j(mol) + 1; %getting lines having coefficients
l2 = j(mol) + 2;
l3 = j(mol) + 3;
file_line = line{j(mol)}; %line having the species
get_species = strsplit(file_line, ' ');
species = get_species{1};
%finding G in the line to get local temp. for each species.
G = strfind(file_line, 'G');
%local temperatures of the species
low_temp = file_line(G(1)+2:G(1)+9);
high_temp = file_line(G(1)+11:G(1)+19);
med_temp = file_line(G(1)+21:G(1)+29);
T_min = str2double(low_temp); %min. temp for species
T_med = str2double(med_temp);
T_high = str2double(high_temp); %max temp. for species
%getting coefficients from first line of each species
line1 = line{l1};
exp = strfind(line1, 'E'); %finding E in the line to get coefficients
a1 = str2double(line1(1:exp(1)+3));
a2 = str2double(line1(exp(1)+4:exp(2)+3));
a3 = str2double(line1(exp(2)+4:exp(3)+3));
a4 = str2double(line1(exp(3)+4:exp(4)+3));
a5 = str2double(line1(exp(4)+4:exp(5)+3));
%getting coefficients from second line of each species
line2 = line{l2};
exp = strfind(line2, 'E');
a6 = str2double(line2(1:exp(1)+3));
a7 = str2double(line2(exp(1)+4:exp(2)+3));
a8 = str2double(line2(exp(2)+4:exp(3)+3));
a9 = str2double(line2(exp(3)+4:exp(4)+3));
a10 = str2double(line2(exp(4)+4:exp(5)+3));
%getting coefficients from third line of each species
line3 = line{l3};
exp = strfind(line3, 'E');
a11 = str2double(line2(1:exp(1)+3));
a12 = str2double(line2(exp(1)+4:exp(2)+3));
a13 = str2double(line2(exp(2)+4:exp(3)+3));
a14 = str2double(line2(exp(3)+4:exp(4)+3));
%defining temperature range for each species
T = linspace(T_min,T_high,500);
%getting the properties in an array
DATA = thermal_prop(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,T,T_med);
%creating folders for different species
folder = [species];
mkdir(folder);
%plotting
%specific heat
figure(1)
plot(T,DATA(1,:),'linewidth',3,'color','r')
title('specific heat')
xlabel('Temperature [K]')
ylabel('C_p [J/Kg/K]')
grid on
%enthalpy
figure(2)
plot(T,DATA(2,:),'linewidth',3,'color','g')
title('Enthalpy')
xlabel('Temperature [K]')
ylabel('H [J/Kg]')
grid on
%Entropy
figure(3)
plot(T,DATA(3,:),'linewidth',3,'color','b')
title('Entropy')
xlabel('Temperature [K]')
ylabel('S [J/K]')
grid on
%saving the plots in the folder
Figure_1 = sprintf('specific heat.jpg');
Figure_2 = sprintf('Enthalpy.jpg');
Figure_3 = sprintf('Entropy.jpg');
folder_1 = fullfile(folder,Figure_1);
folder_2 = fullfile(folder,Figure_2);
folder_3 = fullfile(folder,Figure_3);
saveas(figure(1), folder_1)
saveas(figure(2), folder_2)
saveas(figure(3), folder_3)
%determining the molecular mass of the species
Molecular_mass = mol_mass(species); %getting mol. mass from the function
sprintf('Molecular mass of %s is %f U',species ,Molecular_mass)
end
Function to calculate thermodynamic properties from the relation using the coefficients
1) A function called thermal_prop is created having the coefficients, universal gas constant (R), temperature points and the local medium temperature for each species as the input in order to calculate the thermal properties.
2) If statement is used to execute the given equations while using the low temperature or high temperature coefficients, depending upon the temperature point. If temperature is below the medium temperature, then low temperature coefficients are used otherwise, high temperature coefficients are used.
3) the values of specific heat, enthalpy and entropy are stored in an array called properties.
function [properties] = thermal_prop(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,R,T,T_med)
if T
Function to calculate molecular mass of the species
1) A function called mol_mass is created to determine taking the species as the input to determine their molecular mass.
2) Various atoms constituting the structure of the given species are stored in an array called element and their respective atomic weight are stored in the corresponding position in another array called Atomic_wt.
3) Initially, the molecular mass for the species is set to 0 and a nested for loop is used. The first for loop runs from i=1 till the number of characters present in the formula of the species while the second one runs from j = 1 till the number of atoms present in the array, 'element'
4) strcmp is used to compare the `i^(th)` character of chemical formula of the species to the atoms of the array, 'element'. If statement is used to check whether the `i^(th)`character of the species matches the `j^(th)`element and add its corresponding atomic mass to the molecular mass. The loop is repeated till the atomic mass of all the atoms are added.
5) Certain atoms are present more than once in the chemical formula of the species and are represented using a number. The remaining part of the program is aimed to add the atomic mass of those atoms to the molecular mass. str2double is used to convert the species into numeric value and identify that number corresponding to the atom and store in in a variable called n.
6) Since all the atoms are added once, the atomic mass of remaining atoms is added to the molecular mass by multiplying the atomic weight of that atom with n-1.
function [molecular_mass] = mol_mass(species)
element= {'H','C','N','O','A'}; % all the elements present in the species
Atomic_wt = [1.0078 12.0107 14.0067 15.9949 39.9480]; %atomic weight of elements
molecular_mass = 0; %setting initial molecular mass = 0
for i = 1:length(species)
for j = 1:length(element)
if strcmp(species(i),element(j)) %comparing the species to the elements defined above
molecular_mass = molecular_mass + Atomic_wt(j);
c = j;
end
end
n = str2double(species(i));
if n>1 %if an element is present more than once
molecular_mass = molecular_mass+Atomic_wt(c)*(n-1); %actual molecular mass
end
end
OUTPUT
Folders created for each species
Molecular mass of the species
Plots
O2
N2
CO2
ERRORS
Given below are some of the errors faced while coding the program:
Error 1
This error occurred because of using l1 directly, which is just used to store the location of the line containing the coefficient, while extracting the coefficient instead of using the line which was stored in the memory using the first for loop of the main program as line{i} (in this case, line{l1} ).
Error 2
This error occurred because str2num was used to determine the values of the local temperatures instead of str2double. Thus, the error was resolved by making the necessary correction.
REFERENCES