Parsing NASA thermodynamic data
Aim:
To write a code in Matlab to parse the NASA thermodynamic data file and then calculate thermodynamic properties of particular gas species in the file.
Code for file parsing:
clear all
close all
clc
f=fopen('THERMO.dat')
for i=1:5
a=fgetl(f);
end
k=1;
c=0;
i=0
while(~feof(f))
while(i<5)
i=i+1
a=fgetl(f);
end
a=fgetl(f);
if k==4
c=c+1;
k=0;
end
k=k+1;
end
m=input('elementname')
f=fopen('THERMO.dat')
for i=1:5
a=fgetl(f);
end
for j=1:c
cd('C:\Users\Admin\Downloads')
a1=fgetl(f);
a2=strsplit(a1,' ');
species_name=a2{1};
if (strcmp(m,species_name)==1)
tmid=str2num(a2{end-1});
tlow=str2num(a2{end-3});
thigh=str2num(a2{end-2});
h=fgetl(f);
H=strfind(h,'E');
h1=str2num(h(1:H(1)+3));
h2=str2num(h(H(1)+4:H(2)+3));
h3=str2num(h(H(2)+4:H(3)+3));
h4=str2num(h(H(3)+4:H(4)+3));
h5=str2num(h(H(4)+4:H(5)+4));
h=fgetl(f);
H=strfind(h,'E');
h6=str2num(h(1:H(1)+3));
h7=str2num(h(H(1)+4:H(2)+3));
l1=str2num(h(H(2)+4:H(3)+3));
l2=str2num(h(H(3)+4:H(4)+3));
l3=str2num(h(H(4)+4:H(5)+4));
h=fgetl(f);
H=strfind(h,'E');
l4=str2num(h(1:H(1)+3));
l5=str2num(h(H(1)+4:H(2)+3));
l6=str2num(h(H(2)+4:H(3)+3));
l7=str2num(h(H(3)+4:H(4)+3));
t=linspace(tlow,thigh,100);
s = {'H' 'C' 'N' 'O' 'AR'};
atomic_wt = [1 12 14 16 40];
weight = 0;
for i = 1:length(species_name)
for j = 1:length(s)
if strcmp(species_name(i), s(j))
weight= weight + atomic_wt(j);
pos = j;
end
end
n = str2double(species_name(i));
if n > 1
weight = weight + atomic_wt(pos)*(n-1);
end
end
r1=8.314;
weight
R=r1/weight
for i=1:100
if tlow<tmid
cp(i)=R.*(l1+l2*t(i)+l3*t(i)^2+l4*t(i)^3+l5*t(i)^4);
S(i)=R.*(l1*log(t(i))+l2*t(i)/2+l3*t(i)^2/2+(l4*t(i)^3)/3+(l5*t(i)^4)/4+l7);
hy(i)=R.*(l1+(l2*t(i)/2)+((l3*t(i)^2)/3)+((l4*t(i)^3)/4)+((l5*t(i)^4)/5)+l6/t(i));
else
cp(i)=R.*(h1+h2*t(i)+h3*t(i)^2+h4*t(i)^3+h5*t(i)^4);
S(i)=R.*(h1*log(t(i))+h2*t(i)/2+h3*t(i)^2/2+(h4*t(i)^3)/3+(h5*t(i)^4)/4+h7);
hy(i)=R.*(h1+(h2*t(i)/2)+((h3*t(i)^2)/3)+((h4*t(i)^3)/4)+((h5*t(i)^4)/5)+h6/t(i));
end
end
figure(1)
plot(t,cp)
figure(2)
plot(t,hy)
figure(3)
plot(t,S)
mkdir(species_name);
mkdir('C:\Users\Admin\Downloads',species_name);
file=fullfile('C:\Users\Admin\Downloads',species_name,'cp.png');
saveas(figure(1),file,'png')
file=fullfile('C:\Users\Admin\Downloads',species_name,'h.png')
saveas(figure(2),file,'png')
file=fullfile('C:\Users\Admin\Downloads',species_name,'s.png')
saveas(figure(3),file,'png')
cd('C:\Users\Admin\Downloads')
cd(species_name);
F=fopen(species_name,'w');
gf=fprintf(F,'Molecular weight of %s : %f',species_name,weight)
% file=fullfile('C:\Users\dell\Downloads',species_name,'%s.txt',species_name)
% saveas(weight,file,'txt')
%
end
h=fgetl(f);
h=fgetl(f);
h=fgetl(f);
end
I have written a code for parsing the NASA thermodynamic data file and then calculate thermodynamic properties of particular gas species in the file. In this code, I open the file and to separate the command line first. I found the number of species in the file while counting no of species the file fully read so again I open the file and to skip the command line.
Specific species name gives as the input and it asks the species name from the user while executing the code. After taking the input, The file is entered into the for a loop. the loop executed until all the lines of the file are read. After skipping the command line first line is the starting of the species. Every 4 line contains the different species coefficient, temperature, and species name.
So I read the first line of species and it contains the name and temperature. I separate out the file name and i check it with the input name. If it corrects it runs further or else it exits from the loop and read remaining lines of the species. The loop again runs for the second species until the last species it will run.
If the input name and species name are same, the file enters into the loop. Inside the loop, the temperature value of the species is saved. The coefficient of species are separated out and it saved in corresponding names. The molecular weight of the species is calculated for calculating the specific gas constant of the species. From the range of low temperature to high-temperature the temperature is divided into fine values. It is used to find the specific heat, enthalpy, entropy.
specific heat, enthalpy, entropy are calculated for both low-temperature range and also the high-temperature range. The related plots are created and all the plot files are saved in the species name directory. In the directory, the molecular weight of the species also saved as a text file.
Command window
Saved file in the directory
\
Inside the file, the three plots and the text file is saved
In the text file, the molecular weight of the species is saved
Output graph
CP vs Temperature
Enthalpy Vs Temperature
Entropy Vs Temperature
Thank you