Challenge

Project 1 - Parsing NASA thermodynamic data

Aim :

To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species from the NASA thermodynamic data file. Also, to plot all the graphs and calculate the Molecular Mass of all the species.

Description :

We extract the data from a file in File parsing. In this program we will extract 14 co-efficients from the file and calculate the Enthalpy, Entropy and Specific Heat for different species. We use the following formulae to calculate the Enthalpy, Entropy and Specific Heat.

The formulae are given below,

Here R is the universal gas constant, T is the temeprature

Program :

  • We start the program with importing the 'THERMO.dat' file by using the 'fopen' command and each and every line is read by 'fgetl' command.
  • Next we read the header line and using the 'strsplit' command and the header information is split and global temperatures are extracted.
  • We skip the next three lines as we wont require them in our program because they are irrelevant.
  • Since, we have 53 species in the data file, we have to read all the 53 species. Hence, we write a function with 'for loop' where we read one species and the 'for loop' repeats it for all the species.
  • Then, we extract the global low, mid and high temperatures in the same 'for loop'.
  • The first line of the species consist of name of the species and global low, mid and high temperatures which are extracted using the 'strsplit' command.
  • The next three lines contain the 14 co-efficients where the variable 'E" is found using the 'strfind' command.
  • Seperate folders are created to save the plots by using the 'mkdir' command.
  • For the plots we set the temperatures, hence we split the local species temperature into 5000 points using 'linspace' command.
  • Thus, by using the formulae we calculate the Enthalpy, Entropy and Specific Heat and plots are generated.
  • For, saving the plots 'cd' command is used.
  • Finally, we find the Molecular mass by assigning the atomic weight for each and every element by using the 'strcmd' command and molecular mass is stored into two 'for loops'. One 'for loop' iterates the elements and the second 'for loop' compares the elements.

Here is the program as follow :

clear all
close all
clc

% reading the NASA Thermodynamic data file
h1 = fopen('THERMO.dat','r');
fgetl(h1);

% reading the header information
A = fgetl(h1);
A = strsplit(A,' ');
global_low_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_high_temp = str2num(A{4});

% skipping the next three lines

for i = 1:3
    fgetl(h1);
end

% reading all the species

for i = 1:53
    
    header = fgetl(h1);
    x = strsplit(header,' ');

    species_name = (x{1});
    
    local_low_temp = str2double(x{end-3});
    local_mid_temp = str2double(x{end-1});
    local_high_temp = str2double(x{end-2});
    
    t1 = local_low_temp;
    t2 = local_mid_temp;
    t3 = local_high_temp;

    
    % finding first line co-efficients

    line_1 = fgetl(h1);

    a = strfind(line_1,'E');
    a1 = str2double(line_1(1:a(1)+3));
    a2 = str2double(line_1(a(1)+4:a(2)+3));
    a3 = str2double(line_1(a(2)+4:a(3)+3));
    a4 = str2double(line_1(a(3)+4:a(4)+3));
    a5 = str2double(line_1(a(4)+4:a(5)+3));

    % finding the second line co-efficients

    line_2 = fgetl(h1);

    a = strfind(line_2,'E');
    a6 = str2double(line_2(1:a(1)+3));
    a7 = str2double(line_2(a(1)+4:a(2)+3));
    a8 = str2double(line_2(a(2)+4:a(3)+3));
    a9 = str2double(line_2(a(3)+4:a(4)+3));
    a10 = str2double(line_2(a(4)+4:a(5)+3));

    % finding the third line co-efficients

    line_3 = fgetl(h1);

    a = strfind(line_3,'E');
    a11 = str2double(line_3(1:a(1)+3));
    a12 = str2double(line_3(a(1)+4:a(2)+3));
    a13 = str2double(line_3(a(2)+4:a(3)+3));
    a14 = str2double(line_3(a(3)+4:a(4)+3));
    

    
    % creating folders

    T = linspace(t1,t3,5000);
    R = 8.314;

    for k = 1:length(T)

        % Array of Enthalpy, Entropy and Specific heat

        % Compare the specific temperatures of the co-efficients.

        % a1 to a7 (high temperature co-efficients) a8 to a14 (low temperature co-efficients)

        if T(k)>t2

            % high temperature co-efficients

            Cp(k) = (a1 + a2.*T(k) + a3.*T(k).^2 + a4.*T(k).^3 + a5.*T(k).^4)*R;

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

            S(k) = (a1.*log(T(k)) + a2.*T(k) + (a3.*T(k).^2)/2 + (a4.*T(k).^3)/3 + (a5.*T(k).^4)/4 + a7);

        else
            % low emperaTure co-efficienTs

            Cp(k) = (a8 + a9.*T(k) + a10.*T(k).^2 + a11.*T(k).^3 + a12.*T(k).^4)*R;

            H(k) = (a8 + (a9.*T(k))/2 + (a10.*T(k).^2)/3 + (a11.*T(k).^3)/4 + (a12.*T(k).^4)/5 + (a13./T(k)))*R.*T(k);

            S(k) = (a8.*log(T(k)) + a9.*T(k) + (a10.*T(k).^2)/2 + (a11.*T(k).^3)/3 + (a12.*T(k).^4)/4 + a14);

        end
    end

    % plotting the graphs

    figure(1);
    plot(T,Cp);
    grid on;
    title(species_name);
    xlabel('Temperature');
    ylabel('Specific Heat');

    figure(2);
    plot(T,H);
    grid on;
    title(species_name);
    xlabel('Temperature');
    ylabel('Enthalpy');

    figure(3);
    plot(T,S);
    grid on;
    title(species_name);
    xlabel('Temperature');
    ylabel('Entropy');

    mkdir('C:\Users\Admin\Documents\MATLAB\File parsing', species_name);

    cd(species_name);
    saveas(figure(1),'specificheat.png');
    saveas(figure(2),'enthalpy.png');
    saveas(figure(3),'entropy.png');

    cd('C:\Users\Admin\Documents\MATLAB\File parsing');

    % Calculating Molecular Weight

      elements = ['H','C','O','N','A'];
      atomic_weight = [1.00794,12.011,15.9994,14.00674,39.948];
      mol_mass = 0;
      
      for l = 1:length(species_name)
          for m = 1:length(atomic_weight)
              if strcmp(species_name(l),elements(m)) == 1
                  mol_mass = mol_mass + atomic_weight(m);
                  position = m;
              end
          end
          q = str2double(species_name(l));
          if q>1
              mol_mass = mol_mass+(q-1)*atomic_weight(position);
          end

      end
fprintf('The molecular mass of %s is %f\n',species_name,mol_mass)

end

Folder where plots are saved :

Molecular Mass of Species :

The molecular mass of O is 15.999400
The molecular mass of O2 is 31.998800
The molecular mass of H is 1.007940
The molecular mass of H2 is 2.015880
The molecular mass of OH is 17.007340
The molecular mass of H2O is 18.015280
The molecular mass of HO2 is 33.006740
The molecular mass of H2O2 is 34.014680
The molecular mass of C is 12.011000
The molecular mass of CH is 13.018940
The molecular mass of CH2 is 14.026880
The molecular mass of CH2(S) is 14.026880
The molecular mass of CH3 is 15.034820
The molecular mass of CH4 is 16.042760
The molecular mass of CO is 28.010400
The molecular mass of CO2 is 44.009800
The molecular mass of HCO is 29.018340
The molecular mass of CH2O is 30.026280
The molecular mass of CH2OH is 31.034220
The molecular mass of CH3O is 31.034220
The molecular mass of CH3OH is 32.042160
The molecular mass of C2H is 25.029940
The molecular mass of C2H2 is 26.037880
The molecular mass of C2H3 is 27.045820
The molecular mass of C2H4 is 28.053760
The molecular mass of C2H5 is 29.061700
The molecular mass of C2H6 is 30.069640
The molecular mass of CH2CO is 42.037280
The molecular mass of HCCO is 41.029340
The molecular mass of HCCOH is 42.037280
The molecular mass of H2CN is 28.033620
The molecular mass of HCN is 27.025680
The molecular mass of HNO is 31.014080
The molecular mass of N is 14.006740
The molecular mass of NNH is 29.021420
The molecular mass of N2O is 44.012880
The molecular mass of NH is 15.014680
The molecular mass of NH2 is 16.022620
The molecular mass of NH3 is 17.030560
The molecular mass of NO is 30.006140
The molecular mass of NO2 is 46.005540
The molecular mass of HCNO is 43.025080
The molecular mass of HOCN is 43.025080
The molecular mass of HNCO is 43.025080
The molecular mass of NCO is 42.017140
The molecular mass of CN is 26.017740
The molecular mass of HCNN is 41.032420
The molecular mass of N2 is 28.013480
The molecular mass of AR is 39.948000
The molecular mass of C3H8 is 44.096520
The molecular mass of C3H7 is 43.088580
The molecular mass of CH3CHO is 44.053160
The molecular mass of CH2CHO is 43.045220

Plots for o2, N2 and Co2 species are attched below :

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.