Parsing NASA's thermodynamic data
Aim:
- · To extract the all the 14 coefficients of elements/compound from the data and calculate entropy, enthalpy and specific heat.
- · To plot the variation of these parameters with respect to specified local temperature range.
- · To calculate the molecular masses of each element/compound and show it in command window.
Procedure:
- · First open the given thermodynamic data and read the data in a file and store the data in the variable file.
- · Then read the header file of the thermodynamic data and calculate the global low, mid and high temperature.
- · Certain header lines were skipped which were of no use.
- · Using fgetl(), strsplit(), strfind and str2double() the value of local low, mid and high temperature were stored in the variable.
- · Then the 14 coefficient of each element were stored in their respective variable by using for loop, fgetl(), strsplit(), strfind functions.
- · First the for loop is executed for calculating 14 coefficients of each element.
- · Fgetl() function was used to read coefficients in 1st line, then strfind() was used to find the exponent ‘E’ by using this we can store the coefficient in the respective variable.
- · Once the coefficient was found out it was converted into decimal number using str2double() function.
- · By doing the above steps we can store the coefficients into respective variables.
- · Once the coefficients were found out, then we can calculate the specific heat, entropy and enthalpy by taking gas constant R=8.314 J/mol-k and temperature a linspace() function of lower temp. to high temp.
- · Once the value of specific heat, entropy and enthalpy is found out now we can plot graphs between specific heat vs temperature, enthalpy vs temperature and entropy vs temperature for each element.
- · The three plots were stored in a folder having the name of the element.
- · Finally molecular mass of each element was calculated and displayed in command window.
Formulas:
Code:
1. Primary parsing code:
%Parsing NASA's thermodynamic data
clear all
close all
clc
%finding and opening the file_in_loadpath
file=fopen('THERMO.dat','r');
%header
line1=fgetl(file);
%global temperature values
line2=fgetl(file);
A=strsplit(line2,' ');
global_min_temp=str2num(A{2});
global_mid_temp=str2num(A{3});
global_max_temp=str2num(A{4});
%skipping comments
fgetl(file);
fgetl(file);
fgetl(file);
%using the for loop to consier 53 elements
for i=1:53
line3=fgetl(file); %reading the file
element=line3(1:6); %naming first variable as element
P=strfind(line3,'G');
local_low_temp=line3(P+3:55);
local_high_temp=line3(P+13:65);
local_mid_temp=line3(P+23:75);
lt=str2double(local_low_temp); %creating low temp. variable
ht=str2double(local_high_temp); %creating high temp. variable
mt=str2double(local_mid_temp); %creating mid temp. variable
%line 1 reading coefficients
cfline1=fgetl(file);
X=strfind(cfline1,'E');
a1=cfline1(1:X(1)+3);
a1=str2double(a1); %storing 1st coefficients
a2=cfline1(X(1)+4:X(2)+3);
a2=str2double(a2); %storing 2nd coefficients
a3=cfline1(X(2)+4:X(3)+3);
a3=str2double(a3); %storing 3rd coefficients
a4=cfline1(X(3)+4:X(4)+3);
a4=str2double(a4); %storing 4th coefficients
a5=cfline1(X(4)+4:X(5)+3);
a5=str2double(a5); %storing 5th coefficients
%line 2 reading coefficients
cfline2=fgetl(file);
Y=strfind(cfline2,'E');
a6=cfline2(1:Y(1)+3);
a6=str2double(a6); %storing 6th coefficients
a7=cfline2(Y(1)+4:Y(2)+3);
a7=str2double(a7); %storing 7th coefficients
a8=cfline2(Y(2)+4:Y(3)+3);
a8=str2double(a8); %storing 8th coefficients
a9=cfline2(Y(3)+4:Y(4)+3);
a9=str2double(a9); %storing 9th coefficients
a10=cfline2(Y(4)+4:Y(5)+3);
a10=str2double(a10); %storing 10th coefficients
%line 3 reading coefficients
cfline3=fgetl(file);
Z=strfind(cfline3,'E');
a11=cfline3(1:Z(1)+3);
a11=str2double(a11); %storing 11th coefficients
a12=cfline3(Z(1)+4:Z(2)+3);
a12=str2double(a12); %storing 12th coefficients
a13=cfline3(Z(2)+4:Z(3)+3);
a13=str2double(a13); %storing 13th coefficients
a14=cfline3(Z(3)+4:Z(4)+3);
a14=str2double(a14); %storing 14th coefficients
%Creating the folder for each element
mkdir('G:/Matlab/Sample programs/NASA data',element);
%Universal gas constant
R=8.314;
%Ranging the temperature from low temperature to high temperature
T=linspace(lt,ht,100);
%Specific heat
for j=1:length(T)
if T(j)<mt
%specific heat
sp=(a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4)).*R;
%Enthalpy
H=(a1.*T+(a2.*(T.^2))./2+(a3.*(T.^3))./3+(a4.*(T.^4))./4+(a5.*(T.^5))./5+a6).*R;
%Entropy
S=(a1.*log(T)+a2.*T+(a3.*(T.^2))./2+(a4.*(T.^3))./3+(a5.*(T.^4))./4+a7).*R;
else
%specific heat
sp=(a8+a9.*(T)+a10.*(T.^2)+a11.*(T.^3)+a12.*(T.^4))*R;
%Enthalpy
H=(a8.*T+(a9.*(T.^2))./2+(a10.*(T.^3))./3+(a11.*(T.^4))./4+(a12.*(T.^5))./5+a13).*R;
%Entropy
S=(a8.*log(T)+a9.*T+(a10.*(T.^2))./2+(a11.*(T.^3))./3+(a12.*(T.^4))./4+a14).*R;
end
end
%plot 1 specific heat vs. temperature
figure(1)
plot(T,sp,'linewidth',2,'color','r')
xlabel('Temperature in K')
ylabel('Specific heat in KJ');
title('Specific heat vs. Temperature')
%plot 2 Enthalpy vs. Temperature
figure(2)
plot(T,H,'linewidth',2,'color','y')
xlabel('Temperature in K')
ylabel('Enthalpy in KJ/mol');
title('Enthalpy vs. Temperature')
%plot 3 Entropy vs. Temperature
figure(3)
plot(T,S,'linewidth',2,'color','b')
xlabel('Temperature in K')
ylabel('Entropy in KJ/molK');
title('Entropy vs. Temperature')
directory=(['G:/Matlab/Sample programs/NASA data/',element]);
%changing the file paths to save the plot
cd(directory);
%Saving the plot that got generated
saveas(figure(1),'SP.png');
saveas(figure(2),'H.png');
saveas(figure(3),'S.png');
%Switching the directory back
cd('G:/Matlab/Sample programs/NASA data');
%Molecular mass of elements
MM=molecular_mass(element)
endfor
fclose(file);
Function to calculate the molecular mass of element:
%function for calculating molecular mass
function MM=molecular_mass(element)
element_name=upper(element)
atoms={'H','C','N','O','AR'};
atomic_weight=[1 12 14 16 40];
MM=0;
for i=1:length(element)
for j=1:length(atomic_weight)
if strcmp(element_name(i),atoms(j))==1
MM=MM+atomic_weight(j);
u=j;
end
end
%adding the weight of repeatating atom
n=str2double(element_name(i));
if n>1
MM=MM+atomic_weight(u)*(n-1);
end
end
end
Output:
1. Location of element folders:
2. Molecular masses displayed in command window:
3.
Plots for O2:
4. Plots for N2:
5. Plots for CO2:
Conclusion:
Thus, the parsing of NASA thermodynamic data was done.