Project

Parsing NASA thermodynamic data

Parsing NASA thermodynamic data from a file using Matlab

Aim:

1.Using Matlab to extract thermodynamic data from file and to calculate variation of specific heat,enthalpy and entropy with respect to temperature.

2.To calculate the molecular weight of respective species within the thermodynamic data

Theory:

Data parsing is a method where one string of data gets converted into a different type of data. So let's say you receive your data in raw HTML, a parser will take the said HTML and transform it into a more readable data format that can be easily read and understood. Here the data is in form of .dat file.

In the data file provided there are 53 different species,each species has 14 coefficients attached to it. the first 7 coefficients are for high temperature and the rest 7 coefficients are for low temperature calculations. These coefficients are used in the below equations to get the specific heat, enthalpy and entropy of the specific species at a particular temperature.

Program:

Main code:

clear all 
close all
clc
fid = fopen('thermal.dat','rt');
nLines = 0;
while (fgets(fid) ~= -1)
  nLines = nLines+1;
end
fclose(fid);
f1=fopen('thermal.dat','r');
line_1=fgetl(f1);
line_2=fgetl(f1);
A=strsplit(line_2,' ');
global_low_temp=str2double(A{2});
global_mid_temp=str2double(A{3});
global_high_temp=str2double(A{4});

for i=1:3
comment=fgetl(f1);
end


count = 1;
while count < 4
    x = string(input('Input a species: ','s'));
    req_species(count)=x;
    
    count = count + 1;
end
%req_species = ["o2","n2","Co2"];


for i=1:((nLines-10)/4)
line_3=fgetl(f1);
q=strsplit(line_3,' ');
species_name=q{1};
A=strfind(line_3,'G ');
temp_values=line_3(A+4:length(line_3));
temp_values_split=strsplit(temp_values,' ');
local_temp_low=str2double(temp_values_split{1});
local_temp_high=str2double(temp_values_split{2});
local_temp_mid=str2double(temp_values_split{3});

line_4=fgetl(f1);
A=strfind(line_4,'E');
c1=str2double(line_4(1:(A(1)+3)));
c2=str2double(line_4(A(1)+4:(A(2)+3)));
c3=str2double(line_4(A(2)+4:(A(3)+3)));
c4=str2double(line_4(A(3)+4:(A(4)+3)));
c5=str2double(line_4(A(4)+4:(A(5)+3)));

line_5=fgetl(f1);
A=strfind(line_5,'E');
c6=str2double(line_5(1:(A(1)+3)));
c7=str2double(line_5(A(1)+4:(A(2)+3)));
c8=str2double(line_5(A(2)+4:(A(3)+3)));
c9=str2double(line_5(A(3)+4:(A(4)+3)));
c10=str2double(line_5(A(4)+4:(A(5)+3)));

line_6=fgetl(f1);
A=strfind(line_6,'E');
c11=str2double(line_6(1:(A(1)+3)));
c12=str2double(line_6(A(1)+4:(A(2)+3)));
c13=str2double(line_6(A(2)+4:(A(3)+3)));
c14=str2double(line_6(A(3)+4:(A(4)+3)));

T=linspace(local_temp_low,local_temp_high,100);
R=8.134;
cp = R*specificheat_calc(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,local_temp_high,local_temp_low,local_temp_mid,T);
s = R*entropy_calc(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,local_temp_high,local_temp_low,local_temp_mid,T);
h = R*enthalpy_calc(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,local_temp_high,local_temp_low,local_temp_mid,T);



if (species_name==upper(req_species(1)))||(species_name==upper(req_species(2)))||(species_name==upper(req_species(3)))
mkdir (species_name)
cd(species_name)

figure(1)
plot(T,cp)
ylabel('cp')
title('Specific heat vs Temperature')
Image = getframe(gcf);
imwrite(Image.cdata, 'sp_heatVStemp.png');

figure(2)
plot(T,h)
ylabel('h')
title('Enthalpy vs Temperature')
Image = getframe(gcf);
imwrite(Image.cdata, 'h_heatVStemp.png');

figure(3)
plot(T,s)
ylabel('s')
title('Entropy vs Temperature')
Image = getframe(gcf);
imwrite(Image.cdata, 's_heatVStemp.png');
cd ..
end
Molecular_wt=molecular_weight(species_name);
X=sprintf('%s Molecular weight = %f',species_name,Molecular_wt);
disp(X)
end

specific heat code (specificheat_calc.m):

function cp = specificheat_calc(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,high,low,mid,T)
if(T>mid)
    cp=c1+c2*T+c3*T.^2+c4*T.^3+c5*T.^4;

else
        cp=c8+c9*T+c10*T.^2+c11*T.^3+c12*T.^4;

end

entropy code (entropy_calc.m):

function s = entropy_calc(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,high,low,mid,T)
if(T>mid)
    s=c1*log(T)+c2*T+c3*T.^2/2+c4*T.^3/3+c5*T.^4/4+c7;

else
    s=c8*log(T)+c9*T+c10*T.^2/2+c11*T.^3/3+c12*T.^4/4+c14;

end

enthalpy code(enthalpy_calc):

function h = enthalpy_calc(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,high,low,mid,T)
if(T>mid)
    h=(c1+c2*T/2+c3*T.^2/3+c4*T.^3/4+c5*T.^4/5+c6*T).*T;

else
    h=(c8+c9*T/2+c10*T.^2/3+c11*T.^3/4+c12*T.^4/5+c13*T).*T;

end

Molecular weight code:

 function M = molecular_weight(species_name)

[O, H, C,N,A] = deal(15.999,1.008,12.011,14.007,39.948);
Atomic_mass = [O, H, C,N,A];
Elements=['O','H','C','N','A'];
a=species_name;
Mol_wt=0;
for i =1:length(a)
for j =1:length(Elements)
    if(strcmp(a(i),Elements(j)))
        Mol_wt=Mol_wt+Atomic_mass(j);
        u=j;
    end
end
num=str2double(a(i));
if num>1
        Mol_wt=Mol_wt+Atomic_mass(u)*(num-1);
end
end

M=Mol_wt;
 end

Results:

O2 species results:

N2 species results:

CO2 species results:

Output folders created for all species and all data retrieved:

Molecular weight of each species:

Explanation:

1. Use fopen to read the data from the file.

2.Use fgetl('filename') to skip through all the commented lines.

3.Use strsplit('input') to spli data and then str2double() to retrive the global temperature ranges.

4.create a loop to go through all 53 species within the data.

5.Use strsplit('input') to spli data and then str2double() to retrive the local temperature ranges of all the species.

6.Using the position of 'E' we split the data and by using str2double() we calculate all the 14 coefficients.

7.Use the respective functions to calculate the specific heat,entropy and enthalpy of the species within the local temperature range.

8.Use if loop to retrive the data for the species ["O2","N2","CO2"].

9.make the folder with name of species using following command

mkdir (species_name)
cd(species_name)

10.Using following code the figures are retrieved as .png files and stored in respective folders of the species.

Image = getframe(gcf);
imwrite(Image.cdata, 'file_name.png');

11.molecular weight of each species are printed out in command window.

Conclusion:

Data is retrieved and the plots for the respective species are exported using required commands.

The relation of specific heat,enthalpy and entropy with temperature are plotted.

The molecular weight of each species is calculated and printed out in the command window.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.