Challenge

Project 1 - Parsing NASA thermodynamic data

Aim : Parsing NASA Thermodynamic data and writing a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species

Parser: A parser is a compiler or interpreter component that breaks data into smaller elements for easy translation into other language. A parser takes inputs in the form of sequence of tokens or program instructions and usually builds the data structure in the form of parse tree or an abstract syntax tree

Formula :

CpR=a1+a2T+a3T2+a4T3+a5T4">CpR=a1+a2T+a3T2+a4T3+a5T4

HRT=a1+a2T2+a3T23+a4T34+a5T45+a6T">HRT=a1+a2T2+a3T23+a4T34+a5T45+a6T

SR=a1lnT+a2T+a3T22+a4T33+a5T44+a7">SR=a1lnT+a2T+a3T22+a4T33+a5T44+a7

Main coiding :

clear all 
close all
clc


% universal gas constant
R = 8.314 ; 

%Reading header
f1 = fopen('THERMO.dat','r');
Header= fgetl(f1);

%Reading Global Temperature Line

Global_temp = fgetl(f1);
C = strsplit(Global_temp,' ');
global_low_temp =str2double(C{2}); %Global Low Temperature 
global_mid_temp =str2double(C{3}); %Global Mid Temperature 
global_high_temp =str2double(C{4}); %Global High Temperature 

%%skipping 3 lines
fgetl(f1);
fgetl(f1);
fgetl(f1);

%using for loop to consider 53 elements

for i = 1:53
line= fgetl(f1); %Reading the line
A=strsplit(line,' '); %Seperating Variable
species=A(1); %Naming First Variable as 'Species'
Q = strfind(line,'G'); 
local_low_temp = line(Q+3:55); 
local_high_temp = line(Q+13:65);
local_mid_temp = line(Q+23:75); 
lt = str2double(local_low_temp); %Local Low Temperature
ht = str2double(local_high_temp); %Local Mid Temperature 
mt = str2double(local_mid_temp); %Local High Temperature


%line 1-Reading the coefficient data

cfline1= fgetl(f1);
X = strfind(cfline1,'E');
a1 = cfline1(1:X(1)+3); 
a1 = str2double(a1); %storing 1st coefficient 
a2 = cfline1(X(1)+4:X(2)+3);
a2 = str2double(a2); %storing 2nd coefficient 
a3 = cfline1(X(2)+4:X(3)+3);
a3 = str2double(a3); %storing 3rd coefficient 
a4 = cfline1(X(3)+4:X(4)+3);
a4 = str2double(a4); %storing 4th coefficient 
a5 = cfline1(X(4)+4:X(5)+3);
a5 = str2double(a5); %storing 5th coefficient
 
%line 2-Reading coefficient data of 2nd line

cfline2 = fgetl(f1);
G = strfind(cfline2,'E');
a6 = cfline2(1:G(1)+3);
a6 = str2double(a6); %storing 6th coefficient 
a7 = cfline2(G(1)+4:G(2)+3);
a7 = str2double(a7); %storing 7th coefficient 
a8 = cfline2(G(2)+4:G(3)+3);
a8 = str2double(a8); %storing 8th coefficient 
a9 = cfline2(G(3)+4:G(4)+3);
a9 = str2double(a9); %storing 9th coefficient 
a10 = cfline2(G(4)+4:G(5)+3);
a10 = str2double(a10); %storing 10th coefficient 

%line 3-Reading coefficient data of 3rd line

cfline3 = fgetl(f1);
I = strfind(cfline3,'E');
a11 = cfline3(1:I(1)+3);
a11 = str2double(a11); %storing 11th coefficient 
a12 = cfline3(I(1)+4:I(2)+3);
a12 = str2double(a12); %storing 12th coefficient 
a13 = cfline3(I(2)+4:I(3)+3);
a13 = str2double(a13); %storing 13th coefficient 
a14 = cfline3(I(3)+4:I(4)+3);
a14=str2double(a14); %storing 14th coefficient 



%Ranging the temperature low temperature to high temperature

T=linspace(lt,ht,500);

%Specific Heat

Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Entropy

S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Enthaphy

H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt);

%Saving as a folder
spec=char(species); 
fd = sprintf('%s',spec);
cur_dir=pwd;
mkdir(spec);
cd(spec);
  
  
%Plot 1, Specific heat (Cp) vs Temperature (T)

figure(1);
plot(T,Cp,'linewidth',2,'color','r');
xlabel('Temperature in K');
ylabel('Specific heat in kJ');
title('Specific heat vs Temperature');
  
%Plot 2, Enthalpy (H) vs Temperature (T)
  
figure(2);
plot(T,H,'linewidth',2,'color','b');
xlabel('Temperature in K');
ylabel('Enthalpy in kJ/(mol)');
title('Enthalpy vs Temperature');
  
%Plot 3, Entropy (S) vs Temperature (T)

figure(3);
plot(T,S,'linewidth',2,'color','g');
xlabel('Temperature in K');
ylabel('Entropy in kJ/(mol K)');
title('Entropy vs Temperature');

% saving the plots that got generated

saveas(figure(1),'cp.png');
saveas(figure(2),'h.png');
saveas(figure(3),'s.png');
cd(cur_dir);
%Molecular mass
M= molecular_mass(species)

f2 = fopen('molecular mass','w');
fprintf('%s n %s n %f n','species','Molecular mass',M)
fclose(f2)
end
 

fclose(f1);

Specific Heat function :

function Cp=specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
    Cp=(a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4))*R;
else
    Cp=(a8+a9.*(T)+a10.*(T.^2)+a11.*(T.^3)+a12.*(T.^4))*R;
end

Enthalpy function :

function H=enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
    H=(a1.*T+a2.*(T.^2)./2+a3.*(T.^3)./3+a4.*(T.^4)./4+a5.*(T.^5)./5+a6)*R;
else
    H=(a8.*T+a9.*(T.^2)./2+a10.*(T.^3)./3+a11.*(T.^4)./4+a12.*(T.^5)./5+a13)*R;
end

Entropy function :

function S=entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt,ht,mt)
if T>=mt
    S=(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7)*R;
else
    S=(a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14)*R;
end

Molecular mass function :

%Function_for_Molecular_Weight%

function M= molecular_mass(species)
        atoms={'H','C','N','O','AR'};
        AtomicWeight=[1 12 14 16 40];
               M=0;
        for i=1:length(species)
            for j=1:length(AtomicWeight)
                if (strcmp(species(i),atoms(j))==1)
                    M=M+AtomicWeight(j);
                    g=j;
                end
            end
         % Adding the weights of the repeating atoms
                n=str2double(species(i));
                if n>1
                    p=(n-1);
                    M=M+AtomicWeight(p)*(n-1);
                end
                         
      end
end

Detailed Report :

1) Firstly, the NASA Thermodynamic Data 'Thermo.dat' file was read by using fopen command.

2) Then to navigate through the data fgetl command was used. The header line 'Thermo' was skipped by using fgetl command.

3) The temperature line '300 1000 5000' was stored as a single string in a variable. To split the string into 3 strings strsplit command was used. To convert the string values into numerical values str2num command was used. The three temperature values min, mid and max were then stored in seperate variables.

4) After that the three comment lines were skipped by using fgetl command.

5) The 14 coefficients for each species were calculated by using for loop. Each species contains 3 lines in which 14 coefficients are present. To distinguish the coefficients the position of exponent were determined for each coefficient by using strfind command.

6) The seperate folders were created for each species to save the plots for each species in seperate folders.

7) The value of Universal gas constant R = 8.314J/mol-K was declared. Based upon the 14 co-efficients and the value of R the values of Specific heat, Enthalpy and Entropy were calculated for each species by using the following formulaes

Output :

1) LOCATION OF SEPERATE FOLDERS CREATED FOR EACH SPECIES

2) MOLECULAR WEIGHT DISPLAYED IN COMMAND WINDOW

3) PLOTS

I) FOR O2

II) FOR N2

III) FOR CO2

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.