Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:

To write a MATLAB code for parsing the NASA Thermodynamic data file and to extract 14 coefficients of thermodynamic properties like Specific heat(Cp), Enthalpy(H), and Entropy(S), for all the species in the data file.

Objective:

  • Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file.
  • Calculating the molecular weight of each species.
  • Plotting the Cp, H, and S for the local temperature range.
  • Saving the plots as images with appropriate names and dumping them in separate folders for each species.

Theory:

  • The definition for parse is to split the file or other input into pieces or data which can be easily stored or manipulated. Here we will be splitting a string into parts and then recognizing the parts to convert it into something simpler than string.
  • Here, we extract the data from the given thermodynamic dat file and will calculate the enthalpy, entropy, and Cp's for all the species in the data file. Also, we calculate the molecular weight of each species.
  • The plots for enthalpy, entropy, and Cp for each species will be plotted and will be stored in a separate file.

Formulas Used:

To calculate enthalpy, entropy, and specific heat the below formulas are used:

Main Program:

% Parsing NASA Thermodynamic data
clear 
close all
clc
% Getting the data from the provided file

fid = fopen('THERMO.txt');
% fgetl(fid);
% 
% % Reading the header information
% A = fgetl(fid);
% A = strsplit(A,' ');
% 
% global_low_temp = str2double(A{1});
% global_mid_temp = str2double(A{2});
% global_high_temp = str2double(A{3});

% Eliminating the lines

for i = 1:5
    fgetl(fid);
end

% Getting the Temperature & Coefficients for each species

for i = 1:53 

    header = fgetl(fid);
    x = strsplit('header',' ');

    species_name = x{1};
%     z = length(x);
    
    local_low_temp = str2double(x{z-3});
    local_high_temp = str2double(x{z-2});
    local_mid_temp = str2double(x{z-1});

    t1 = local_low_temp;
    t2 = local_mid_temp;
    t3 = local_high_temp;

 T = linspace(t1,t3,1000);
    % Coefficients of each species

    % Getting first line coefficients (a1-a5)

    line_1 = fgetl(fid);


    a = str2find(line_1,'E');
    a1 =line_1(1:a(1)+3);
    c1=str2double(a1);
    a2 = line_1(1:a(1)+4:a(2)+3);
    c2=str2double(a2);
    a3 = line_1(1:a(2)+4:a(3)+3);
    c3=str2double(a3);
    a4 = line_1(1:a(3)+4:a(3)+3);
    c4=str2double(a4);
    a5 = line_1(1:a(4)+4:a(4)+3);
    c5=str2double(a5);


    % Getting second line coefficients(a6-a10)

    line_2 = fgetl(fid);
    
    a_1 = strfind(line_2,'E');
    a6 = line_2(1:a_1(1)+3);
    c6 = str2double(a6);
    a7 = line_2(1:a_1(1)+4:a_1(2)+3);
    c7 = str2double(a7);
    a8 = line_2(1:a_1(2)+4:a_1(3)+3);
    c8 = str2double(a8);
    a9 = line_2(1:a_1(3)+4:a_1(4)+3);
    c9 = str2double(a9);
    a10 = line_2(1:a_1(4)+4:a_1(5)+3);
    c10 = str2double(a10);

    % Getting third line coefficients(a11-a14)

    line_3 = fgetl(fid);
   
    a_2 = strfind(line_3,'E');
    a11 = line_3(1:a_2(1)+3);
    c11 = str2double(a11);
    a12 = line_3(1:a_2(1)+4:a_2(2)+3);
    c12 = str2double(a12);
    a13 = line_3(1:a_2(2)+4:a_2(3)+3);
    c13 = str2double(a13);
    a14 = line_3(1:a_2(3)+4:a_2(4)+3);
    c14 = str2double(a14);
    
    
    R = 8.314;

    [Cp,H,S] = inputs(T,R,t2,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14);
    
    % Creating the folder to save the plots

     mkdir(species_name)   
     cd(species_name)

    % Calculating the Specific heat, Enthalpy, and Entropy

   
    

    % Plotting

    figure(1)
    plot(T,Cp,'LineWidth',3,'Color','r')
    grid on
    title(species_name)
    xlabel('Temperature')
    ylabel('Specific Heat')

    figure(2)
    plot(T,H,'LineWidth',3,'Color','b')
    grid on
    title(species_name)
    xlabel('Temperature')
    ylabel('Enthalpy')

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

    % Changing the Directory

%     program = (['D:\matlab programs\NASA file parsing\Species\',sp_name]);
   
    % Saving the plots

    saveas(1,'Specific Heat.jpg')
    saveas(2,'Enthalpy.jpg')
    saveas(3,'Entropy.jpg')

%     cd('D:\matlab programs\NASA file parsing')
  cd ..

    % Calculating the Molecular weight for each species

   elements = ['H','C','O','N','A'];
   atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948];
   mol_mass = 0;
   for l = 1:length(species_name)
       for m = 1:length(atomic_weight)
           if strcmp(species_name(l), elements(m))
               mol_mass = mol_mass + atomic_weight(m);
               position = m;
           end
       end
       n = str2double(species_name(l));
       if n>1
           mol_mass = mol_mass + atomic_weight(position)*(n-1);
       end
   end
end
sprintf('Molecular Weight of %s.%f u',species_name,mol_mass)




    



Function Program:

clear all 
close all
clc

function[Cp_H_S] = inputs(T,R,t2,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14)

for k = 1:length(T)
    if T(k)>t2

        Cp = (c1+c2.*T+c3.*T.^2+c4.*t.^3+c5.*T.^4).*R;
        H = (c1+(c2.*T)./2+(c3.*T.^2)./3+(c4.*T.^3)./4+(c5.*T.^4)./5+(c6.*T).*T.*R);
        S = (c1.*log(T)+c2.*T+(c3.*T.^2)./2+(c4.*T.^3)./3+(c5.*T.^4)./4+c7).*R;
    else
        Cp = (c8+c9.*T+c10.*T.^2+c11.*T.^3+c12.*T.^4).*R;
        H = (c8 +(c9.*T)./2+(c10.*T.^2)./3+(c11.*T.^3)./4+(c12.*T.^4/.5+c13./T).*T.*R);
        S = (c8.*log(T)+c9.*T+(c10.*T.^2)./2+(c11.*T.^3)./3+(c12.*T.^4)./4+c14).*R;
    end
end
end

Steps Taken:

  • THERMO.dat file is imported to the program by using the 'fopen' command and each and every line is ready by using the 'fgetl' command.
  • In order to get the header information 'strsplit' command is used to split the string and the global temperatures are faced.
  • Next three statements are irrelevant, hence it is skipped by using the 'fgetl' line inside the 'for loop'.
  • Now the species data is read, since there are 53 species, and the logic remains the same hence 'for loop' for the 53 species is declared and the main program is given inside the 'for loop'.
  • The species first line contains the species name and local temperatures and the next three lines contain the coefficients.
  • Hence, by using the 'strsplit' command the species_name and the local temperatures are obtained.
  • In order to get the coefficients 'strfind' command is used to find the variable 'E' by using this as reference coefficients are assigned.
  • To create a separate folder to save the plots for every species 'mkdir' command is used.
  • For plotting we need a set of temperatures hence the local species temperatures are split into 1000 points by using the 'linspace' command.
  • To assign the high and low-temperature coefficients each and every local temperature is compared with the local mid temperature.
  • Thus, by using the formulae specific heat, entropy and enthalpy are calculated & plotting is carried out.
  • For saving the plots 'cd' command is used.
  • To find the molecular mass for each & every element atomic weight is assigned now by using 'strcmp' command each species element are compared & molecular mass is stored using two ' for loops', one to iterate the species element and one to compare the elements.
  • In some species like HNO3 the element's atomic weight is to be added twice (or) thrice, hence flag operator is used to getting the position and that element's atomic weight is added to the molecular weight by using the 'if' condition.

Folders with plottings:

Screenshots of Workspace:

Conclusion:

This program runs on its own to extract data coefficients. from the file, calculate the specific heat, enthalpy and entropy, and plot its variation w.r.t temperature and save it in a separate folder. Also, the molecular mass of each species is founded.

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.