PARSING NASA THERMODYNAMIC DATA
AIM: Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
OBJECTIVES:
- To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
-
Calculate the molecular weight of each species and display it in the command window.
-
Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
THEORY:
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.
NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented the co-efficients that are required to evaluate these polynomials for 1000+ species.
FORMULA USED:
CODE:
close all
clear
clc
% Opening THERMO.dat
f1=fopen('THERMO.dat','r');
% Universal gas constant
R=8.314;
% To Read The Header Information
for i=1:5
if i==2
tempratures=fgetl(f1);
temp=strsplit(tempratures,'');
Tmp=str2num(temp{1});
lower_global_tmp=Tmp(1);
mid_gloabal_tmp=Tmp(2);
higher_gloabal_tmp=Tmp(3);
else
comments{i}=fgetl(f1());
end
end
% Storing the data of each species in a single cell matrix
for j=1:53
for k=1:4
A{j,k}=fgetl(f1);
end
end
% Seprating The Sepcies coloumn and the coefficients coloumn
coefficients=A(:,2:4);
num_values=coefficients';
% getting the species column
species=A(:,1);
%converting it into char array
sp=char(species);
% evaluating the local temperatures and dividing the rows of each species
for m=1:53
% getting the local tempratures
values{m}=strsplit(sp(m,1:end-1),'G ');
L_tmp=values{m};
local_tmp(m)=L_tmp(1,2);
%for creating species
elements_split{m}=strsplit(sp(m,:),' ');
each_species=elements_split{m};
Species(m)=each_species(1);
%seprating coefficients of each line for every species
w=num_values(:,m);
line1_species(m)=w(1,1);
line2_species(m)=w(2,1);
line3_species(m)=w(3,1);
end
L_Temp=char(local_tmp);
local_tempreatures=str2num(L_Temp);
lower_local_temp=local_tempreatures(:,1);
higher_local_temp=local_tempreatures(:,2);
mid_local_temp=local_tempreatures(:,3);
% extracting cofficents from lines of every species
line1=char(line1_species);
line2=char(line2_species);
line3=char(line3_species);
for s=1:53
a=strfind(line1(s,:),'E');
% upper tmperature cofficients
Ah1(s,:)=line1(s,1:a(1)+3);
ah1(s)=str2double(Ah1(s,:));
Ah2(s,:)=line1(s,a(1)+4:a(2)+3);
ah2(s)=str2double(Ah2(s,:));
Ah3(s,:)=line1(s,a(2)+4:a(3)+3);
ah3(s)=str2double(Ah3(s,:));
Ah4(s,:)=line1(s,a(3)+4:a(4)+3);
ah4(s)=str2double(Ah4(s,:));
Ah5(s,:)=line1(s,a(4)+4:a(5)+3);
ah5(s)=str2double(Ah5(s,:));
Ah6(s,:)=line2(s,1:a(1)+3);
ah6(s)=str2double(Ah6(s,:));
Ah7(s,:)=line2(s,a(1)+4:a(2)+3);
ah7(s)=str2double(Ah7(s,:));
%lower temperature coffiecients
AL1(s,:)=line2(s,a(2)+4:a(3)+3);
aL1(s)=str2double(AL1(s,:));
AL2(s,:)=line2(s,a(3)+4:a(4)+3);
aL2(s)=str2double(AL2(s,:));
AL3(s,:)=line2(s,a(4)+4:a(5)+3);
aL3(s)=str2double(AL3(s,:));
AL4(s,:)=line3(s,1:a(1)+3);
aL4(s)=str2double(AL4(s,:));
AL5(s,:)=line3(s,a(1)+4:a(2)+3);
aL5(s)=str2double(AL5(s,:));
AL6(s,:)=line3(s,a(2)+4:a(3)+3);
aL6(s)=str2double(AL6(s,:));
AL7(s,:)=line3(s,a(3)+4:a(4)+3);
aL7(s)=str2double(AL7(s,:));
c(s,:)=[ah1(s) ah2(s) ah3(s) ah4(s) ah5(s) ah6(s) ah7(s) aL1(s) aL2(s) aL3(s) aL4(s) aL5(s) aL6(s) aL7(s)];
end
% calucaltion of Cp,H,S
for p=1:53
T=lower_local_temp(p):higher_local_temp(p);
if T<=mid_local_temp(p)
Cp=R*(aL1(p)+(aL2(p)*T)+(aL3(p)*(T.^2))+(aL4(p)*(T.^3))+(aL5(p)*(T.^4)));
H=R*((aL1(p)*T)+((aL2(p)*(T.^2))/2)+((aL3(p)*(T.^3))/3)+((aL4(p)*(T.^4))/4)+((aL5(p)*(T.^5))/5)+aL6(p));
S=R*((aL1(p)*log(T)) + (aL2(p)*T) + ((aL3(p)*(T.^2))/2) + ((aL4(p)*(T.^3))/3)+((aL5(p)*(T.^4))/4)+(aL7(p)));
else
Cp=R*(ah1(p)+(ah2(p)*T)+(ah3(p)*(T.^2))+(ah4(p)*(T.^3))+(ah5(p)*(T.^4)));
H=R*((ah1(p)*T)+((ah2(p)*(T.^2))/2)+((ah3(p)*(T.^3))/3)+((ah4(p)*(T.^4))/4)+((ah5(p)*(T.^5))/5)+ah6(p));
S=R*((ah1(p)*log(T)) + (ah2(p)*T) + ((ah3(p)*(T.^2))/2) + ((ah4(p)*(T.^3))/3)+((ah5(p)*(T.^4))/4)+(ah7(p)));
end
figure(p)
plot(T,Cp,'r-','linewidth',2);
title(strcat('Specific Heat Vs Temperature of:',Species{:,p}))
xlabel('Temperature(K)')
ylabel('specific Heat(J/mol.K)')
grid on
foldername=sprintf('%s',Species{:,p});
mkdir(foldername(1,:))
picturename = fullfile(foldername(1,:), 'Specific_Heat');
saveas (figure(p), picturename, 'png')
figure(p+53)
plot(T,H,'b-','linewidth',2);
title(strcat('Enthalpy Vs Temperature of:',Species{:,p}))
xlabel('Temperature(K)')
ylabel('Enthalpy(J)')
grid on
picturename = fullfile(foldername(1,:), 'Enthaply');
saveas (figure(p+53), picturename, 'png')
figure(p+106)
plot(T,S,'g-','linewidth',2);
title(strcat('Entropy Vs Temperature of:',Species{:,p}))
xlabel('Temperature(K)')
ylabel('Entropy(J/K)')
grid on
picturename = fullfile(foldername(1,:), 'Entropy');
saveas (figure(p+106), picturename, 'png')
end
% calculation of molecular wt.
aw=[16.00,1.00,12.00,14.00,40.00];
elements= ["O","H","C","N","AR"];
for i=1:53
z=char(Species(i));
weight=0;
for j=1:length(z)
for k=1:length(elements)
if strcmpi(z(j),elements(k))
weight=weight+aw(k);
r=k;
else
if str2num(z(j))>=1
val=str2num(z(j));
weight=weight+aw(r)+(aw(r)*(val-2));
break;
end
end
end
end
if weight==0
weight=aw(k);
end
wt(i)=weight;
disp(strcat('Molecular weight of:',Species{i},'=',num2str(wt(i))));
end
OUTPUTS:
PLOTS FOR CO2
PLOTS FOR O2:
PLOTS FOR N2:
EXPLANATION OF CODE:
The following steps are followed in the code:
STEP 1: Reading the Header information from THERMO.DAT.txt file to calculate Enthalpy, Entropy & Specific and we assigned to a variable in order to get data with respect to global temperature, local temperature & calculation of coefficients.
The temperatures are splitted using the command ‘strsplit’. And then converting it into a number using command ‘str2num’ and storing each value into a particular variable.
STEP 2: Next step is storing the remaining data into a cell array and dividing them accordance with the need and splitting the species array in such a way to extract the local temperatures and then splitting it to get the name of the species and lastly separating each line of every species into three different variables.
STEP 3: Now we extract the coefficients from every line generated.As every line has character ‘E’ in it. Using it as a flag to access the right and left hand side of the string.
Once the position of ‘E’ is been searched using it to split the string and converting them into numbers using str2dobule (for more precision) and at each iteration the coefficients of one species is been calculated.
STEP 4: Now we calculate the values of Cp, H and S and plotting graph and saving it into a folder named by species.
STEP 5: Lastly Calculating the value of molecular weight of each species and saving of required plots and molecular weight.
CONCLUSION:
Learnt the concept of file parsing.
The graphs of specific heat , entropy and enthalpy were succesfully ploted using the data form ‘THERMO.dat’ file and molecular weight were also being calculated.