Project 1 - Parsing NASA thermodynamic data
PARSING OF NASA’s THERMODYNAMICS DATA:
In this project we take NASA’s thermodynamic data provided which consists of coefficients for various molecules and determine their specific heat, enthalpy and entropy. To read and convert the data file is called file parsing.
Partial screenshot of Data file:
As we can see in this screenshot this is a stack of numbers. In this project we convert this stack of numbers into usable data. To do so first we open the data file using ‘fopen’ function in MATLAB. What it does is open the data file for further reading or writing.
Syntax for fopen:
data = fopen(‘Thermo.dat’,’r’)
data = is the file pointer
Thermo.dat is the file name
r says that we want to read the file
If we want to write in the file we exchange ‘r’ with ‘w’
We need to segregate each line from the data file and convert that line into data. To do that we use ‘fgetl’ function command. What it does is store the line as string to a variable. Note that fgetl is sequential meaning it will store successive lines.
Syntax for fgetl:
line1 = fgetl(data)
line1 is the variable where the line is stored in as string
data is the file pointer for ‘Thermo.dat’
Since, the line is stored as string we need to convert it into a number. To do that we use str2double.
Syntax for str2double:
X = str2double(str)
X is the variable where we store the converted number
Str is the string variable to be converted
Logic explanation:
The first 5 lines of the file comprise of file header, global temperatures and various comments. Only the global temperature line is important to us but since ‘fgetl’ works sequentially we save the first line to get to the global temperature line and the remaining 3 lines consisting of comments we need to store them too using ‘fgetl’ so that we can move onto the species data. We split the second line using ‘strsplit’ command function and then convert the strings into numbers and store the global temperatures
In the Thermo.dat file there are 53 molecules with 14 coefficients for each molecule. We need to separate all the coefficient values and store them into 14 different variables. To compute all the data for 53 molecules we create a for loop.
The first line of the species data set (6th line if we consider the first loop) consists of species name and local temperatures. We read the line and store species name and local temperatures for further use.
The second line of the species data set (7th line) stores the first 5 coefficient values. To navigate through the line, we use ‘strfind’ command and search for ‘E’ which is denotes exponential. We can then separate the coefficients and convert them into numbers from string using ‘str2double’.
Similarly, we do this for the 3rd and 4th line of the species data set and store the remaining 9 coefficients.
Now to calculate specific heat, enthalpy and entropy we create a temperature range from global min and max temperatures (300 to 5000). The 14 coefficients consist of low temperature and high temperature coefficients. The first 7 for low temp and the remaining for high temp. Now to calculate for 2 ranges (low and high) we use ‘if-else’ statements. For temperatures below local medium temperature we consider them to be in low temp range and use the first 7 coefficients and the temperatures higher than local medium temp we use the last 7 coefficients.
We plot the graphs using calculated values. We create folder for every species and store the graphs in those folders.
To calculate molecular mass, we call a function created to calculate it.
Program for file parsing:
close all
clc
data = fopen('THERMO.dat','r');
line1 = fgetl(data);
line2 = fgetl(data);
comment_1 = fgetl(data);
comment_2 = fgetl(data);
comment_3 = fgetl(data);
A = strsplit(line2,' ');
global_min_temperature = str2double(A{2});
global_med_temperature = str2double(A{3});
global_max_temperature = str2double(A{4});
fprintf('Global maximum Temperature = %f \n',global_max_temperature)%(%f = saying float number to be displayed here which is global_max_temperature, \n means new/next line)
fprintf('Global medium Temperature = %f \n',global_med_temperature)
fprintf('Global minimum Temperature = %f \n',global_min_temperature)
for i = 1:53
mol_data_line = fgetl(data);
%Extracting species name
species_name = mol_data_line(1:6);
%Extracting local temperature data
G_low = mol_data_line(48:55);
G_high = mol_data_line(57:65);
G_med = mol_data_line(67:75);
local_low_temperature = str2double(G_low);
local_high_temperature = str2double(G_high);
local_med_temperature = str2double(G_med);
%Extracting coefficient values from the second data line under species
%name line i.e for coefficients a1 to a5
line_1 = fgetl(data);
L1 = strfind(line_1,'E');
a1 = str2double(line_1(1:(L1(1) + 3)));
a2 = str2double(line_1((L1(1) + 4): (L1 (2) + 3)));
a3 = str2double(line_1((L1(2) + 4): (L1 (3) + 3)));
a4 = str2double(line_1((L1(3) + 4): (L1 (4) + 3)));
a5 = str2double(line_1((L1(4) + 4): (L1 (5) + 3)));
%Extracting coefficient values from the third data line under species
%name line i.e for coefficients a6 to a10
line_2 = fgetl(data);
L2 = strfind(line_2,'E');
a6 = str2double (line_2(1:(L2 (1) + 3)));
a7 = str2double (line_2((L2 (1) + 4):(L2 (2) + 3)));
a8 = str2double (line_2((L2 (2) + 4):(L2 (3) + 3)));
a9 = str2double (line_2((L2 (3) + 4):(L2 (4) + 3)));
a10 = str2double (line_2((L2 (4) + 4):(L2 (5) + 3)));
%Extracting coefficient values from the fourth data line under species
%name line i.e for coefficients a11 to a14
line_3 = fgetl(data);
L3 = strfind(line_3,'E');
a11 = str2double (line_3(1:(L3 (1) + 3)));
a12 = str2double (line_3((L3 (1) + 4):(L3 (2) + 3)));
a13 = str2double (line_3((L3 (2) + 4):(L3 (3) + 3)));
a14 = str2double (line_3((L3 (3) + 4):(L3 (4) + 3)));
%Assigning Temperature range from global min to global max
T = linspace(300,5000,100); %Temperature
R = 8.314;%Universal Gas constant
%We know that the first 7 coefficients of the data under a species are
%for low temperature whereas the next 7 coefficients are for high
%temperature, hence we segregate the two temperature ranges
%Since the segregation isn't mentioned let's assume that the
%temperature values lower than the local medium temp is low temp range
%whereas temperature values above medium local temperature is high temp
%range
for j = 1:length(T)
if T(j)<local_med_temperature
cp = (a1 + a2.*T + a3.*T.^2 + a4.*T.^3 + a5.*T.^4).*R; %Specific Heat
H = (a1 + ((a2.*T)/2) + ((a3.*T.^2)/3) + ((a4.*T.^3)/4) + ((a5.*T.^4)/5) + (a6./T)).*T.*R; %Enthalpy
S = (a1.*log(T) + a2.*T +((a3.*T.^2)/2) + ((a4.*T.^3)/3) + ((a5.*T.^4)/4) + a7).*R; %Entropy
else
cp = (a8 + a9.*T + a10.*T.^2 + a11.*T.^3 + a12.*T.^4).*R; %Specific Heat
H = (a8 + ((a9.*T)/2) + ((a10.*T.^2)/3) + ((a11.*T.^3)/4) + ((a12.*T.^4)/5) + (a13./T)).*T.*R; %Enthalpy
S = (a8.*log(T) + a9.*T +((a10.*T.^2)/2) + ((a11.*T.^3)/3) + ((a12.*T.^4)/4) + a14).*R; %Entropy
end
end
%Plotting
%Temp vs Entropy
figure(1)
plot(T,S,'linewidth',4,'color','r');
title(species_name)
xlabel('Temperature')
ylabel('Entropy')
%Temp vs Enthalpy
figure(2)
plot(T,H,'linewidth',4,'color','g');
title(species_name)
xlabel('Temperature')
ylabel('Enthalpy')
%Temp vs Specific Heat
figure(3)
plot(T,cp,'linewidth',4,'color','b');
title(species_name)
xlabel('Temperature')
ylabel('Specific Heat')
%Now we create a sub folder under parent folder for every species where we will save the
%graphs
mkdir(['D:\MATLAB FILES\File Parsing\Species_',species_name]);
current_file = (['D:\MATLAB FILES\File Parsing\Species_',species_name]);
cd(current_file) %Changing the folder to the new folder created by mkdir with current species name
%saving plots in the created folder
saveas(1,'Temp vs Entropy.jpg');
saveas(2,'Temp vs Enthalpy.jpg');
saveas(3,'Temp vs Specific Heat.jpg');
cd('D:\MATLAB FILES\File Parsing') %Changing the folder back to the parent folder before new loop starts
%Calculating molecular mass
mol_mass = molecular_mass(species_name);%Calling function molecular_mass which calculates the molecular mass of the species under study in the current loop
fprintf('Molecular mass of %s is = %f \n',species_name,mol_mass)
%s specifies that there is a string variable(species_name) to be displayed here
%f specifies that there is a floating number(mol_mass) to be displaced here
%and \n means go to new/next line
end
fclose(data);
Command Window:
Program to calculate molecular mass:
%In this function we calculate the molecular mass of the species by taking
%in the string variable species_name as input
function mol_mass = molecular_mass(species_name)
%The NASA's thermodynamic data comprises of 5 elements namely: Hydrogen, Carbon, Nitrogen, Oxygen and Argon
Elements = ['H','C','N','O','A'];
atomic_weight = [1 12 14 16 40]; %Corresponding to Elements
mol_mass = 0;
%To calculate the mass of molecules we need to determine number of element
%in the molecule (first for loop) and then we compare all the elements in the molecule with
%our Elements array (second for loop) using strcmp, if the strcmp is true it will add its
%atomic weight to the molecule (first if statement)
%We also need to check if the molecule contains the same atom more than
%once such as O2 where 2 oxygen atoms are in the molecule (second if
%statement)
for i = 1:length(species_name)
for j = 1:length(Elements)
if strcmp(species_name(i),Elements(j))
mol_mass = mol_mass +atomic_weight(j);
u = j;
end
end
number_of__repeated_atoms = str2double(species_name(i));
if number_of__repeated_atoms>1
mol_mass = mol_mass + atomic_weight(u)*(number_of__repeated_atoms-1);
end
end
end
Screenshot of created folders of each species:
Plots of O2, CO2 and N2:
O2:
CO2:
N2:
References:
https://www.mathworks.com/help/matlab/ref/str2double.html?s_tid=srchtitle