Project

Parsing NASA thermodynamic data

AIM: To extract the polynomial coefficients that can be used to evaluate and study the thermodynamic properties such as Cp, H and S of various chemical compounds.

OBJECTIVES OF THE PROJECT:

1. Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. You will be reading this file NASA thermodynamic data

2. Calculate the molecular weight of each species and display it in the command window.

3. Plot the Cp, Enthalpy and Entropy for the local temperature range specified for each species.

4. Save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name.

PARSING: Parsing means dividing anything into the smallest components to understand from the core. In programming, parsing means breaking program elements into their smallest units to create some knowledge for a compiler to understand the syntax and semantics of any programming language.

GOVERNING EQUATIONS:

`C_p/R=a1+a2*T+a3*T^2+a4*T^3+a5*T^4`

`H/(R*T)=a1+a2*T/2+a3*(T^2/3)+a4(T^3/4)+a5(T^4/5)+(a6)/T`

`S/R=a1*lnT+a2*T+a3*T^2/2+a4*T^3/3+a5*T^4/4+a7`

Where R is the universal gas constant,

T is the temperature,

H is enthalpy,

S is entropy,

C_p is the specific heat

a1,a2,...a7 are polynomial coefficients.

The programming language used: MATLAB

CODE:

main code: NASA_parsing.m

clear all
close all
clc

%Opening  the text file.
fid=fopen('THERMO.dat','r');
%storing the data of each line in a cell
read=cell(1,200); %preallocation
for i=1:222
    read{i}= fgetl(fid);
end


%getting the temperature values from GLOBAL TEMPERATURE
line_2=read{2};
txt=strsplit(line_2,' ');
global_low_temp=str2double(txt{2});
global_mid_temp=str2double(txt{3});
global_high_temp=str2double(txt{4});
%selecting the specific lines hving species
start_line=6;
   
while start_line<222
    
temp=read{start_line};
%getting info from start_line
k=strsplit(temp,' ');
species=k{1};
%extracting local temperature info
local_low_temp=k{end-3};
llow_temp=str2num(local_low_temp);

local_high_temp=k{end-2};
lhigh_temp=str2num(local_high_temp);

local_mid_temp=k{end-1};
lmid_temp=str2num(local_mid_temp);

    
%reading coefficients from line1
line_1=read{start_line+1};

pos=findstr(line_1,'E');


for j=1:length(pos)
    if j==1
    u{j}=line_1(1:pos(1)+3);
    else
    u{j}=line_1(pos(j-1)+4:pos(j)+3);
    end
end
a1=str2num(u{1});
a2=str2num(u{2});
a3=str2num(u{3});
a4=str2num(u{4});
a5=str2num(u{5});



%reading coefficients from line2

line_2=read{start_line+2};
pos=findstr(line_2,'E');


for j=1:length(pos)
    if j==1
    v{j}=line_2(1:pos(1)+3);
    else
    v{j}=line_2(pos(j-1)+4:pos(j)+3);
    end
end
a6=str2num(v{1});
a7=str2num(v{2});
b1=str2num(v{3});
b2=str2num(v{4});
b3=str2num(v{5});



%reading coefficients from line3

line_3=read{start_line+3};
pos=findstr(line_3,'E');


for j=1:length(pos)
    if j==1
    w{j}=line_1(1:pos(1)+3);
    else
    w{j}=line_1(pos(j-1)+4:pos(j)+3);
    end
end
b4=str2num(w{1});
b5=str2num(w{2});
b6=str2num(w{3});
b7=str2num(w{4});

%The FIRST 7 coefficients are HIGH-temperature coefficients(a1 to a7).
%the SECOND 7 coefficients are LOW-temperature coefficients(b1 to b7).

%univerasl gad constannt R
R=8.314;

%defininng  LOCAL TEMPERATURE RANGES 
T=linspace(llow_temp,lhigh_temp,100);

% calculating ENTHALPY, ENTROPY,SPECIFIC HEAT.
%preallocation
H=[];
S=[];
C_p=[];
% check which type of coefficients to use high/low
if T>=lmid_temp %high temperature
    H=(R.*T).*(a1 + a2.*T + a3.*(T.^2) +a4.*(T.^3) +a5.*(T.^4)+a6./T);
    S=R*(a1.*log(T) + a2.*(T) + a3.*((T.^2)./2) +a4.*((T.^3)./3) +a5.*((T.^4)./4)+ a7);
     C_p=R*(a1 + a2.*T + a3.*(T.^2) +a4.*(T.^3) +a5.*(T.^4));
else %low temperaure
    H=(R.*T).*(b1 + b2.*T + b3.*(T.^2) +b4.*(T.^3) +b5.*(T.^4)+b6./T);  
    S=R*(b1.*log(T) + b2.*(T./2) + b3.*((T.^2)./2) +b4.*((T.^3)./3) +b5.*((T.^4)./4)+ b7);
    C_p=R*(b1 + b2.*T + b3.*(T.^2) +b4.*(T.^3) +b5.*(T.^4));
end

%mkdir folderName (creates the folder folderName). 
mkdir(species)
cd(species)%current directory



%plotting the C_p for the local temperature range
figure(1)
plot(T,C_p,'-r','linewidth',3)
title(species)
xlabel('TEMPERATURE')
ylabel('SPECIFIC HEAT')
%saveas(fig,filename) saves the figure
%To save the current figure, specify fig as gcf.
saveas(gcf,'SPECIFIC_HEAT.png')

%plotting the H for the local temperature range
figure(2)
plot(T,C_p,'-g','LineWidth',3)
title(species)
xlabel('TEMPERATURE')
ylabel('ENTHALPY')
saveas(gcf,'ENTHALPY.png')

%plotting the S for the local temperature range
figure(3)
plot(T,C_p,'-b','LineWidth',3)
title(species)
xlabel('TEMPERATURE')
ylabel('ENTROPY')
saveas(gcf,'ENTROPY.png')
%changing cd to pwd(parent working directory)
cd ..
start_line=start_line+4;
 
end



% Close the text file.

fclose(fid);
 

EXPLANATION TO THE CODE:

  • Create a new Matlab file NASA_pasing.m(of your choice).
  • open the text file with fopen
  • syntax:
    FID = fopen(FILENAME) opens the file FILENAME for read access. FILENAME is the
    name of the file to be opened.
  • create a cell and store every line of the data in that cell(read)
  • extract the temperature values from global temperature using strsplit and str2num
  • species lines start from 6th line and contain coefficient values in the next three lines, so create a loop using this fact.
  • extract the species name and local temperature from the first line and coefficients from the next three lines and store them in any variables.
  • likewise, calculate the enthalpy, entropy, specific heat of that species.
  • use mkdir to store this species plots in a separate folder.
  • syntax:mkdir folderName (creates the folder folderName)
  • plot the C_p, H, S for the local temperature range.
  • save the plots with some names
  • use cd.. to change the path to parent working directory(pwd).
  • this loop continues for all the species.
  • close the file with fclose
  • syntax:
    fclose(FID) closes the file associated with file identifier FID.
    all the plots of each species are stored in separate folders with species name as the folder name.
    PLOTS OF (CO2):
    ENTHALPY VS TEMPERATURE

ENTROPY VS TEMPERATURE

SPECIFIC HEAT VS TEMPERATURE

PLOTS OF N2

PLOTS OF O2

code to find Molecular weight of each chemical species: Molecular_weight.m

clear all 
close all
clc

%Calculating the molecular weight of each species 

%Open the text file.
fid=fopen('THERMO.dat','r');
%storing the data of each line in a cell
read=cell(1,218);
for i=1:218
    read{i}= fgetl(fid);
end

start_line=6;
while start_line<=214
temp=read{start_line};
%getting info from start_line
k=strsplit(temp,' ');
species=k{1};


%Calculating the molecular weight of each species 
Atoms=cell(1,5);
Atoms={'H','C','N','O','A'};
%corresponding atomic weights
Atomic_Wt=[1.008,12.005,14.01,16.00,39.88];

M_wt=0;
for i=1:length(species)
    
    for j=1:length(Atoms)
        if strcmp(species(i),Atoms{j})
            M_wt = M_wt + Atomic_Wt(j);
              repeat=j;
        
        end
        %finding the repeadted atoms
             num=str2num(species(i));
             %for avoiding loop to run eith same num
            
            if  ~isempty(num)&&(j==1)
  
              M_wt = M_wt + Atomic_Wt(repeat)*(num-1);
              
                 
            end
           
     end
       
end
 fprintf('The molecular weight of %s is %5.4f \n',species,M_wt)

start_line=start_line+4;

    
end

EXPLANATION TO THE CODE:

  • Create a new Matlab file Molecular_weight.m
  • open the text file with fopen
  • syntax:
    FID = fopen(FILENAME) opens the file FILENAME for read access. FILENAME is the
    name of the file to be opened.
  • create a cell and store every line of the data in that cell(read)
  • create a cell array containing individual atoms(species) and store atomic weights of their corresponding atoms.
  • after extracting the species name from the line use strcmp to find the atom consisting in that species.
  • The molecular weight is the sum of the atomic weights of the atoms in the molecule.
  • calculate the molecular weight using that fact and store it in a variable M_wt.
  • To check that if the species contains the repeated atoms.
  • use logical operator
  • ~isempty(num)&&(j==1) is true only when num is a non empty(number) and we need this loop to run only once.
  • use fprintf to display the molecular weight of the each species in the command window.

OUTPUT:

The molecular weight of O is 16.0000
The molecular weight of O2 is 32.0000
The molecular weight of H is 1.0080
The molecular weight of H2 is 2.0160
The molecular weight of OH is 17.0080
The molecular weight of H2O is 18.0160
The molecular weight of HO2 is 33.0080
The molecular weight of H2O2 is 34.0160
The molecular weight of C is 12.0050
The molecular weight of CH is 13.0130
The molecular weight of CH2 is 14.0210
The molecular weight of CH2(S) is 14.0210
The molecular weight of CH3 is 15.0290
The molecular weight of CH4 is 16.0370
The molecular weight of CO is 28.0050
The molecular weight of CO2 is 44.0050
The molecular weight of HCO is 29.0130
The molecular weight of CH2O is 30.0210
The molecular weight of CH2OH is 31.0290
The molecular weight of CH3O is 31.0290
The molecular weight of CH3OH is 32.0370
The molecular weight of C2H is 25.0180
The molecular weight of C2H2 is 26.0260
The molecular weight of C2H3 is 27.0340
The molecular weight of C2H4 is 28.0420
The molecular weight of C2H5 is 29.0500
The molecular weight of C2H6 is 30.0580
The molecular weight of CH2CO is 42.0260
The molecular weight of HCCO is 41.0180
The molecular weight of HCCOH is 42.0260
The molecular weight of H2CN is 28.0310
The molecular weight of HCN is 27.0230
The molecular weight of HNO is 31.0180
The molecular weight of N is 14.0100
The molecular weight of NNH is 29.0280
The molecular weight of N2O is 44.0200
The molecular weight of NH is 15.0180
The molecular weight of NH2 is 16.0260
The molecular weight of NH3 is 17.0340
The molecular weight of NO is 30.0100
The molecular weight of NO2 is 46.0100
The molecular weight of HCNO is 43.0230
The molecular weight of HOCN is 43.0230
The molecular weight of HNCO is 43.0230
The molecular weight of NCO is 42.0150
The molecular weight of CN is 26.0150
The molecular weight of HCNN is 41.0330
The molecular weight of N2 is 28.0200
The molecular weight of AR is 39.8800
The molecular weight of C3H8 is 44.0790
The molecular weight of C3H7 is 43.0710
The molecular weight of CH3CHO is 44.0420
The molecular weight of CH2CHO is 43.0340

ERRORS:

1.minor errors are encountered and they are overcome by the debugging tool.

2.make sure that your code for finding molecular weights holding good for O2, H2O2 like species also. Because it will not display any error or outcome.

REFERENCES:

https://matlab.mathworks.com/

https://www.thoughtco.com/definition-of-molecular-weight-605369#:~:text=The%20atomic%20weight%20of%20carbon%20and%20hydrogen,found%20on%20a%20periodic%20table.&text=molecular%20weight%20%3D%20(number%20of%20carbon,)%20%2B%20(14%20x%201.01)

https://www.quora.com/What-exactly-does-parsing-mean-in-programm

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.