Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file

%Reading NASA Data File
clear all
close all
clc
f1 = fopen('THERMO.dat','r')

for i = 1:5
    fgetl(f1)
end

for j = 1:53
    line1 = fgetl(f1);
    x = strsplit(line1,' ');
    Species = (x{1});
    
    
    %Getting Value of LOW MID & HIGH Temp from Line-1
    pos_dot = strfind(line1,'.');
    
    lowtemp = line1(pos_dot(1)-3:pos_dot(1)+3);
    lowtemp = str2double(lowtemp);
    
    hightemp = line1(pos_dot(2)-4:pos_dot(2)+3);
    hightemp = str2double(hightemp);
    
    midtemp = line1(pos_dot(3)-4:pos_dot(3)+3);
    midtemp = str2double(midtemp);
    
    T = linspace(lowtemp,hightemp,100);
    R = 8.314;

    %Getting Coefficient Value from Line-2 
    % a1H-a7H = High Temp Coeff
    % a8L-a14L = Low Temp Coeff
    line2 = fgetl(f1);
    pos_E= strfind(line2,'E');
    
    a1H = line2(1:pos_E(1)+3);
    a1H = str2double(a1H);
     
    a2H = line2(pos_E(1)+4:pos_E(2)+3);
    a2H = str2double(a2H);
    
    a3H = line2(pos_E(2)+4:pos_E(3)+3);
    a3H = str2double(a3H);
    
    a4H = line2(pos_E(3)+4:pos_E(4)+3);
    a4H =str2double(a4H);
    
    a5H = line2(pos_E(4)+4:pos_E(5)+3);
    a5H = str2double(a5H);
    
    %Getting Coefficient Value from Line-3
    line3 = fgetl(f1);
    pos_E= strfind(line3,'E');
    
    a6H = line3(1:pos_E(1)+3);
    a6H = str2double(a6H);
     
    a7H = line3(pos_E(1)+4:pos_E(2)+3);
    a7H = str2double(a7H);
    
    a8L = line3(pos_E(2)+4:pos_E(3)+3);
    a8L = str2double(a8L);
    
    a9L = line3(pos_E(3)+4:pos_E(4)+3);
    a9L = str2double(a9L);
    
    a10L = line3(pos_E(4)+4:pos_E(5)+3);
    a10L = str2double(a10L);
    
    %Getting Coefficient Value from Line-4
    line4 = fgetl(f1);
    pos_E= strfind(line4,'E');
    
    a11L = line4(1:pos_E(1)+3);
    a11L = str2double(a11L);
     
    a12L = line4(pos_E(1)+4:pos_E(2)+3);
    a12L = str2double(a12L);
    
    a13L = line4(pos_E(2)+4:pos_E(3)+3);
    a13L = str2double(a13L);
    
    a14L = line4(pos_E(3)+4:pos_E(4)+3);
    a14L = str2double(a14L);

    %Calculating Entropy S
   if T<midtemp
       S = R*(a1H.*log(T) + a2H.*T + (a3H.*T.^2)./2 + (a4H.*T.^3)./3 + (a5H.*T.^4)./4 + a7H);
   else
       S = R*(a8L.*log(T) + a9L.*T + (a10L.*T.^2)./2 + (a11L.*T.^3)./3 +(a12L.*T.^4)./4 + a14L);
   end

   %Calculating Enthalpy H
   if T>midtemp
       H = R.*(a1H + a2H.*T/2 + (a3H.*T.^2)./3 + (a4H.*T.^3)./4 + (a5H.*T.^4)./5 + a6H);
   else
       H = R.*(a8L + a9L.*T/2 + (a10L.*T.^2)./3 + (a11L.*T.^3)./4 + (a12L.*T.^4)./5 + a13L);
   end

   %Calculating Specific Heat Cp
   if T>midtemp
       Cp = R.*(a1H + a2H.*T + (a3H.*T.^2) + (a4H.*T.^3) + (a5H.*T.^4));
   else
       Cp = R.*(a8L + a9L.*T + (a10L.*T.^2) + (a11L.*T.^3) + (a12L.*T.^4));
   end
    
   cd(pwd)
   dir_name = Species;
   mkdir(['C:\Users\DJ\Documents\MATLAB\NASA Challenge\ ',Species]);

   figure(1)
   plot(T,S,'linewidth',5,'color','r')
   title(sprintf('Entropy Vs temperature'),Species)
   xlabel('Temperature')
   ylabel('Entropy')
    
   figure(2)
   plot(T,H,'linewidth',5,'color','g');
   title(sprintf('Enthalpy Vs temperature'),Species)
   xlabel('Temperature')
   ylabel('Enthalpy')

   figure(3)
   plot(T,Cp,'linewidth',5,'color','b')
   title(sprintf('Specific Heat Vs temperature'),Species)
   xlabel('Temperature')
   ylabel('Specific Heat')

    cd(['C:\Users\DJ\Documents\MATLAB\NASA Challenge\ ',Species]);
   saveas(1,'Entropy','png');
   saveas(2,'Enthalpy','png'); 
   saveas(3,'Specific Heat','png');

   %Calculating Molecular Weight Of Each Species
   elements =['H','C','N','O','A'];
   atomic_wt = [1,12,14,16,40];
   molecular_wt = 0;
   for i=1:length(Species)
       for j = 1:length(elements)
           if strcmpi(Species(i),elements(j))
               molecular_wt = molecular_wt + atomic_wt(j);
               position = j;
           end
       end
       f = str2double(Species(i));
       if f>1
            molecular_wt = molecular_wt + atomic_wt(position)*(f-1);
       end
   end
   sprintf('Molecular Wt of %s is %d',Species,molecular_wt)
   
end
fclose(f1);

MOLECULAR Weight for Each Species

f1 =

     3


ans =

    'THERMO'


ans =

    '   300.000  1000.000  5000.000'


ans =

    '! GRI-Mech Version 3.0 Thermodynamics released 7/30/99'


ans =

    '! NASA Polynomial format for CHEMKIN-II'


ans =

    '! see README file for disclaimer'


ans =

    'Molecular Wt of O is 16'


ans =

    'Molecular Wt of O2 is 32'


ans =

    'Molecular Wt of H is 1'


ans =

    'Molecular Wt of H2 is 2'


ans =

    'Molecular Wt of OH is 17'


ans =

    'Molecular Wt of H2O is 18'


ans =

    'Molecular Wt of HO2 is 33'


ans =

    'Molecular Wt of H2O2 is 34'


ans =

    'Molecular Wt of C is 12'


ans =

    'Molecular Wt of CH is 13'


ans =

    'Molecular Wt of CH2 is 14'


ans =

    'Molecular Wt of CH2(S) is 14'


ans =

    'Molecular Wt of CH3 is 15'


ans =

    'Molecular Wt of CH4 is 16'


ans =

    'Molecular Wt of CO is 28'


ans =

    'Molecular Wt of CO2 is 44'


ans =

    'Molecular Wt of HCO is 29'


ans =

    'Molecular Wt of CH2O is 30'


ans =

    'Molecular Wt of CH2OH is 31'


ans =

    'Molecular Wt of CH3O is 31'


ans =

    'Molecular Wt of CH3OH is 32'


ans =

    'Molecular Wt of C2H is 25'


ans =

    'Molecular Wt of C2H2 is 26'


ans =

    'Molecular Wt of C2H3 is 27'


ans =

    'Molecular Wt of C2H4 is 28'


ans =

    'Molecular Wt of C2H5 is 29'


ans =

    'Molecular Wt of C2H6 is 30'


ans =

    'Molecular Wt of CH2CO is 42'


ans =

    'Molecular Wt of HCCO is 41'


ans =

    'Molecular Wt of HCCOH is 42'


ans =

    'Molecular Wt of H2CN is 28'


ans =

    'Molecular Wt of HCN is 27'


ans =

    'Molecular Wt of HNO is 31'


ans =

    'Molecular Wt of N is 14'


ans =

    'Molecular Wt of NNH is 29'


ans =

    'Molecular Wt of N2O is 44'


ans =

    'Molecular Wt of NH is 15'


ans =

    'Molecular Wt of NH2 is 16'


ans =

    'Molecular Wt of NH3 is 17'


ans =

    'Molecular Wt of NO is 30'


ans =

    'Molecular Wt of NO2 is 46'


ans =

    'Molecular Wt of HCNO is 43'


ans =

    'Molecular Wt of HOCN is 43'


ans =

    'Molecular Wt of HNCO is 43'


ans =

    'Molecular Wt of NCO is 42'


ans =

    'Molecular Wt of CN is 26'


ans =

    'Molecular Wt of HCNN is 41'


ans =

    'Molecular Wt of N2 is 28'


ans =

    'Molecular Wt of AR is 40'


ans =

    'Molecular Wt of C3H8 is 44'


ans =

    'Molecular Wt of C3H7 is 43'


ans =

    'Molecular Wt of CH3CHO is 44'


ans =

    'Molecular Wt of CH2CHO is 43'

>> 

Plots for O2, N2 and Co2

Coding Explanation:

File parsing : The process in which reading & extraction of information from a text file or external database and how it can be used in the matlab. File writing function and reading function is the basic function.

1. Intially the data file is read using fopen cmd.

2.Each line is got using fgetl cmd and first 5 lins is got with a single function cmd.

3.While coding getl every next line of the data is accquired.

4.Species of name is cmded

5.After this the line 1 contains value of 3 different temperature values.

6.Those values are taken with the help of indentifying dot in that row using pos cmd, and the value of temp is taken out as string and then the string is converted to numeric using str2double cmd to get more precise, this is done for 3 different temperatures, and mentioned as low mid and high temp.

7.As of the same procedure is repeated for next 3 lines with the easily indentifying position of 'E'.

8.Then the values of enthalpy,entropyand specific heat of each species is found out using the formulas given.

9.As the values of temp is mentioned as -"The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients. Also use the LOCAL TEMPERATURE RANGES which is unique for each individual species",the value of coeff 'a' is , a1H-a7His hight temp coeff, and a8L-a14L is low temp coeff.

10.Regarding this the temp need to be specified exactly for each set of temeperature. therefore the temperature is
defined in linspace cmd, then if else function is executed to get the values of H,S,Cp respectively. And the Gas Constant(R) value in provided.

11.And next a specific folder is created in a desired path using mkdir cmd,and each folder is created with the name of each and every species name.

12.After this plot cmd is initialized to produce the graph of enthalpy entropy and specific heat.

13.This plot is saved using saveas cmd with required figure number,name of file,format of file.

14.Molecular Weight of each species is found.All the element are loaded using in an array and the atomic weight of each is loaded in array respectively. Initilly weight is 0, then first for loop is created for species and next loop is created to compare the elements and species using strcmp cmd.And the molecular weight formula is initialized, and then the Moelecular Weight value is made to display in command window, then both the loop is ended.

This is the procedure to find out value and plot of one species,
similatly each species is need to be found , for that the whole procedure is execiuted inside for loop.
Thus the whole 53 Species's molecular weight,enthalpy,entropy,and specific heat and its plots is taken and value is found.

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.