Parsing NASA's Thermodynamic Data
AIM
To parse the thermodynamic data of NASA in matlab
THEORY
To parse the thermodynamic data,I use fgetl command to get every line of data as a string then I take out necessary values from the string for plottingand calculate enthalapy,entropy and specific heat of every species.
OBJECTIVE
To create and save plot of every species in their generated folder and calculate molecular mass of give data.
BODY
Main code
clc;
clear all;
close all;
format shortG
f1=fopen('THERMO.dat','r');
%1)Loop for getting every line from file
i=1;
while ~feof(f1)
x{i,1}=fgetl(f1);
i=i+1;
end
end_line=i-6;% this line indicate the end line of given file
k=1; % inputs for array used to stores local temperature of all species
n=1; %inputs for array used to stores coefficient of all species
for j=6:end_line
% 2)loop for local temperature
if rem(j,4)==2
line=x{j,1};
C=strsplit(line,' ');
name{k,1}=C{1,1};
temp(k,1)=str2num(C{1,end-3}); %low local temperature
temp(k,2)=str2num(C{1,end-2}); %high local temperature
temp(k,3)=str2num(C{1,end-1}); %mid local temperature
k=k+1;
continue
end
%3)loop for coefficient
tline=x{j,1};
B=findstr(tline,'E');
sB=size(B);
sb=max(sB);
a(n,1)=str2num(tline(1:B(1)+3));
for b=2:sb
a(n,b)=str2num(tline(B(b-1)+4:B(b)+3));
end
n=n+1;
end
A=a';
o=1;
%4)loop for creating indices of A
for i=1:159
for j=1:5
ind(o)=sub2ind(size(A),j,i);
o=o+1;
end
end
p=1;
% 5)loop for assigning value of coefficient into coeff cell
for i=1:k-1
for j=1:15
coeff(i,j)=A(ind(p));
p=p+1;
end
end
%6) Main programme
for i=1:k-1
%calling function for local temp. and coefficients
T=temp(i,:);
co=coeff(i,:);
[Cp H S t]=therm(T,co);
%creating folder
folder_name=sprintf('%s',name{i,1});
mkdir(folder_name);
%plotting for species
figure(1)
plot(t(1,:),Cp(1,:),'r',t(2,:),Cp(2,:),'b');
xlabel('Local temperature');
ylabel('Specific heat')
n1=sprintf('Cp of %s species',name{i,1});
title(n1);
legend('low temperature range','high temperature range');
z=fullfile(pwd,folder_name,n1);
saveas(gcf,z,'jpg')
figure(2)
plot(t(1,:),H(1,:),'r',t(2,:),H(2,:),'b')
xlabel('Local temperature');
ylabel('Enthalapy')
n2=sprintf('Enthalapy of %s species',name{i,1});
title(n2);
legend('low temperature range','high temperature range');
z=fullfile(pwd,folder_name,n2);
saveas(gcf,z,'jpg')
figure(3)
plot(t(1,:),H(1,:),'r',t(2,:),H(2,:),'b')
xlabel('Local temperature');
ylabel('Entropy')
n3=sprintf('Entropy of %s species',name{i,1});
title(n3);
legend('low temperature range','high temperature range');
z=fullfile(pwd,folder_name,n3);
saveas(gcf,z,'jpg')
%calculating molecular weight
c(1,:)={'C','H','O','A','R','N'};
c(2,:)={12,1,16,40,0,14};
int={'1','2','3','4','5','6','7','8','9'};
na=name{i,1};
s=cellstr(na');
a=0;w=0;
for i=1:length(s)
for j=1:length(c)
if s{i}==c{1,j}
ss(i)=c{2,j};
a=c{2,j};
end
end
for j=1:9
if s{i}==int{j}
a=ss(i-1)*(str2num(s{i})-1);
end
end
w=w+a;
end
fprintf('molecular weight of %s is %i\n',na,w)
end
EXPLANATION
I divide my code into six part as you can see in the comments.
1)Under this comment, I create a while loop for getting every line of file after opening it. I use feof (end of function) command to all the lines used in file.Since at the end there are some blank line so I subtact to get number of lines and store in the variable called end_line.
Now I create the loop for every line and since above 5 lines are header I start the loop from 6th line.
2)This loop started under above loop and is meant for getting local temperaturs of species. Since every 4th line after 6th line is local temperature line so I give condition according to this to get the local temperature. I store this local temperature is cell called temp. At the same time I store the name of species in name array.
3)This code start when the condition for above loop doesnot satisfy. This code is meant for finding coefficients of that species. I find indeces of E to get the coefficient and store in the array called a.
4 &5) Since the above created array a is not in required form. So I created this loop to get the array of 53 * 14. For this first I find out the indices of every element of a and then stored in A as I desired. So now in each row of A I have 14 coefficient of one species.
6)Now this is the code for creating plot and folder and molecular weight of every species. I start from for loop designed for every species(53) and inside the loop first I call the function to get the enthalpy , entropy and specific heat of that species. The function is given below
Function for calculating Enthapy, Entropy and specific heat of every species
function [Cp H S t]=therm(T,co)
R=8.314;
low_temp=T(1);
high_temp=T(2);
mid_temp=T(3);
t(1,:)=linspace(low_temp,mid_temp,100);
t(2,:)=linspace(mid_temp,high_temp,100);
t1=t(1,:);
t2=t(2,:);
Cp(1,:)=R*(co(1)+co(2).*t1+co(3).*t1.^2+co(4).*t1.^3+co(5).*t1.^4);
Cp(2,:)=R*(co(8)+co(9).*t2+co(10).*t2.^2+co(11).*t2.^3+co(12).*t2.^4);
H(1,:)=R.*t1.*(co(1)+co(2).*t1./2+co(3).*(t1.^2)./3+co(4).*(t1.^3)./4+co(5).*(t1.^4)./5+co(6)./t1);
H(2,:)=R.*t2.*(co(8)+co(9).*t2./2+co(10).*(t2.^2)./3+co(11).*(t2.^3)./4+co(12).*(t2.^4)./5+co(13)./t2);
S(1,:)=R.*(co(1).*log(t1)+co(2).*t1+co(3).*(t1.^2)./2+co(4).*(t1.^3)./3+co(5).*(t1.^4)./4+co(7));
S(2,:)=R.*(co(8).*log(t2)+co(9).*t2+co(10).*(t2.^2)./2+co(11).*(t2.^3)./3+co(12).*(t2.^4)./4+co(14));
end
Now after getting the values of enthalpy and entropy I plot for each of them. But before plotting I created the folder using mkdir command with the name as I store in the name array.In every plot I use fullfile command to create the directory and then save the plot in jpeg format in desired folder.
After creating and saving plot I start the code for finding molecular weight. for that I created the array c and store the element in first row and its atomic mass in second row. Now I use cellstr command to separate every character of string stored in name and then use condition to check and assign values according to string. All this numbers added in w and then displayed using fprintf command.
This process is repeated for every species.
OUTPUT
folder created
plot of O2
plot of CO2
plot of N2