Project 1 - Parsing NASA thermodynamic data
Aim : To parse NASA Thermodynamic data file.
Link for the file : https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view?usp=sharing
Objective :
1. To identify the essential data from the file like species and their coefficients and assigning them to variables so as to call them in further calculations.
2. To calculate the specific heat, enthalpy, entropy of various species by the obtained coefficients.
3. To plot graphs for various species and save them in separate folders.
4. Calculate the molecular mass of every species.
Formulae:
specific heat = c_p = `R*(a1 + a2*t + a3*t^2 + a4*t^3 + a5*t^4)`
enthalpy = H = `R*t(a1 + (a2*t)/2 + a3*t^2/3 + a4*t^3/4 + a5*t^4/5 + (a6)/t)`
entropy = S = `R*(a1*log(t) + a2*t + a3*t^2/2 + a4*t^3/3 + a5*t^4/4 + a7)`
Note : for high temperatures coefficients (t>1000), we utilize a1 to a7. And for low temperatures, a8 to a14.
Code structure:
we use 'fopen' to open the required file from the directory. 'fgetl' is used to skip lines. Then by using strsplit(str,delimiter) we can split the contents in reference to delimiter and assign them a variable which can be then converted from a string to character or number.
a=findstr(str1,str2) is also used to assign strings.
the global temperatures and local temperatures are assigned to a variable. and the coefficients are also recorded.
Now, we calculate the specific heat, enthalpy,entropy of species are calculated as per the formula mentioned above and the graphs are plotted and saved in different folders.
The molecular weight of individual species are calculated by using 'for loop' and 'if' and 'strcmp' command.
The logic is as follow: at first the array of existing elements in the file and their atomic weight is created.
Then the species name are compared with array of elements, by using 'strcmp' command. If the logic is 1, then the code progress further and molecular weight is calculated. If the logic is 0, then the position of element in compound is noted. ( we can get such a case where the logic is 0 when there is a presence of number in compound).
n = str2num(species_name(l)); == by using this command we note the no. of characters and numbers in a particular species. This helps us in the further code where we calculate the molecular weight of a species in which there is a presence if numerical value.
hence, the molecular weight is then calculated using the following code:
mol_mass = mol_mass + atomic_weight(position)*(n-1);
The code is as follows:
clc
close all
clear all
%getting the data
f1 = fopen('THERMO.txt','r');
fgetl(f1)
%reading the header
A = fgetl(f1);
A = strsplit(A,' ');
global_low_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_high_temp = str2num(A{4});
%t = linspace(local_low_temp,local_high_temp,100);
%skipping next 3 lines
for i = 1:3
fgetl(f1)
end
%reading the data
for i = 1:53
%finding species name
a = fgetl(f1)
x = strsplit(a,' ');
species_name = (x{1});
%finding local temp
b = length(x);
local_low_temp = str2num(x{b-3});
local_mid_temp = str2num(x{b-1});
local_high_temp = str2num(x{b-2});
%finding coeffs
% line 1
tline1 = fgetl(f1);
line1 = findstr(tline1,'E');
a1 = tline1(1:12+3);
a1 = str2num(a1);
a2 = tline1(16:27+3);
a2 = str2num(a2);
a3 = tline1(31:42+3);
a3 = str2num(a3);
a4 = tline1(46:57+3);
a4 = str2num(a4);
a5 = tline1(61:72+3);
a5 = str2num(a5);
%line 2
tline2 = fgetl(f1);
line2 = findstr(tline2,'E');
a6 = tline2(1:12+3);
a6 = str2num(a6);
a7 = tline2(16:27+3);
a7 = str2num(a7);
a8 = tline2(31:42+3);
a8 = str2num(a8);
a9 = tline2(46:57+3);
a9 = str2num(a9);
a10 = tline2(61:72+3);
a10 = str2num(a10);
% line 3
tline3 = fgetl(f1);
line3 = findstr(tline3,'E');
a11 = tline3(1:12+3);
a11 = str2num(a11);
a12 = tline3(16:27+3);
a12 = str2num(a12);
a13 = tline3(31:42+3);
a13 = str2num(a13);
a14 = tline3(46:57+3);
a14 = str2num(a14);
t = linspace(local_low_temp,local_high_temp,100);
R = 8.314; %universal gas constant
%calculating specific heat
for j= length(t);
if(t(j)>local_mid_temp);
c_p = R*(a1 + (a2.*t) + (a3.*t.^2) + (a4.*t.^3) + (a5.*t.^4));
else(t(j)<local_mid_temp);
c_p = R*(a8 + (a9.*t) + (a10.*t.^2) + (a11.*t.^3) + (a12.*t.^4));
end
%calculating enthalpy
if(t(j)>local_mid_temp);
H = R.*t.*(a1 + ((a2.*t)/2) + (a3.*(t.^2)/3) + (a4.*(t.^3)/4) + (a5.*(t.^4)/5) + (a6./t));
else(t(j)<local_mid_temp);
H = R.*t.*(a8 + ((a9.*t)/2) + (a10.*(t.^2)/3) + (a11.*(t.^3)/4) + (a12.*(t.^4)/5) + (a13./t));
end
%calculating entropy
if(t(j)>local_mid_temp);
S = R*((a1.*log(t)) + (a2.*t) + ((a3.*(t.^2))/2) + ((a4.*(t.^3))/3) + (a5.*(t.^4))/4) + a7;
else(t(j)<local_mid_temp);
S = R*((a8.*log(t)) + (a9.*t) + ((a10.*(t.^2))/2) + ((a11.*(t.^3))/3) + (a12.*(t.^4))/4) + a14;
end
end
elements = ['H','C','O','N','AR'];
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948];
mol_mass=0;
for l = 1:length(species_name);
for m = 1:length(atomic_weight);
if strcmp(species_name(l),elements(m));
mol_mass = mol_mass + atomic_weight(m);
position = m;
end
end
n = str2num(species_name(l));
if n>1;
mol_mass = mol_mass + atomic_weight(position)*(n-1);
end
end
% creating folder
mkdir(species_name);
cd(species_name);
figure(1)
plot(t,c_p)
title('specific heat vs temperature')
xlabel('temperature')
ylabel('specific heat')
image = getframe(gcf);
saveas(1,'specific heat','jpg')
figure(2)
plot(t,H)
title('enthalpy vs temperature')
xlabel('temperature')
ylabel('enthalpy')
image = getframe(gcf);
saveas(2,'enthalpy','jpg')
figure(3)
plot(t,S)
title('entropy vs temperature')
xlabel('temperature')
ylabel('entropy')
image = getframe(gcf);
saveas(3,'entropy','jpg')
cd ..
fprintf('molecular weight of %s : %f',species_name,mol_mass)
end
fclose(f1)
Workspace :

folder image :

molecular weight displayed in command window:

Graph plotted for O2:



Graph plotted for N2 :


Graph plotted for CO2 :


Error :

the above error was sorted by changing the position of 'end' command for the 'for loop'.
Conclusion :
Hence the file is parsed, various parameters are calculated, and graphs are plotted for the same.