Project

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 in the data file.

OBJECTIVES :

  • To plot the Cp, Enthalpy and Entropy for the local temperature range for each species.
  • To save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.

THEORY :

File Parsing - Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data.

THERMO.dat is a data file given by NASA containing coefficient values which are utilised to calculate specific heat, enthalpy and entropy values at different temperatures for 53 different species.

CODE :

clear all
close all
clc

R = 8.314 ;         % universal gas const (J/mol-K)

f1 = fopen('THERMO.dat','r');
l1 = fgetl(f1);
l2 = fgetl(f1);
l2 = strsplit(l2,' ');

% Global temps -
% TG_l = str2num(l2{2});
% TG_m = str2num(l2{3});
% TG_h = str2num(l2{4});

l3 = fgetl(f1);
l4 = fgetl(f1);
l5 = fgetl(f1);


mkdir('C:\Users\lenovo\Documents\MATLAB\File Parsing')


for i = 1:53
    
    l6 = fgetl(f1);
    l6 = strsplit(l6,' ');
    species = l6{1};

    % Local temps -
    TL_l = str2num(l6{end-3});
    TL_m = str2num(l6{end-1});
    TL_h = str2num(l6{end-2});
    
   
    line1 = fgetl(f1);
    a = strfind(line1,'E');
    a1 = str2num(line1(1:a(1)+3));
    a2 = str2num(line1(a(1)+4:a(2)+3));
    a3 = str2num(line1(a(2)+4:a(3)+3));
    a4 = str2num(line1(a(3)+4:a(4)+3));
    a5 = str2num(line1(a(4)+4:a(5)+3));
    
    line2 = fgetl(f1);
    b = strfind(line2,'E');  
    a6 = str2num(line2(1:b(1)+3));
    a7 = str2num(line2(b(1)+4:b(2)+3));
    a8 = str2num(line2(b(2)+4:b(3)+3));
    a9 = str2num(line2(b(3)+4:b(4)+3));
    a10 = str2num(line2(b(4)+4:b(5)+3));
    
    line3 = fgetl(f1);
    c = strfind(line3,'E');
    a11 = str2num(line3(1:c(1)+3));
    a12 = str2num(line3(c(1)+4:c(2)+3));
    a13 = str2num(line3(c(2)+4:c(3)+3));
    a14 = str2num(line3(c(3)+4:c(4)+3));
    
   
    t = linspace(TL_l, TL_h ,100);
     
    for i = 1:length(t)
        if t(i) > TL_m
            Cp(i) = R*(a1 + a2*t(i) + a3*t(i)^2 + a4*t(i)^3 + a5*t(i)^4);
            H(i) = R*(a1*t(i) + (a2/2)*t(i)^2 + (a3/3)*t(i)^3 + (a4/4)*t(i)^4 + (a5/5)*t(i)^5 + a6);
            S(i) = R*(a1*log(t(i)) + a2*t(i) + (a3/2)*t(i)^2 + (a4/3)*t(i)^3 + (a5/4)*t(i)^4 + a7);
        else
            Cp(i) = R*(a8 + a9*t(i) + a10*t(i)^2 + a11*t(i)^3 + a12*t(i)^4);
            H(i) = R*(a8*t(i) + (a9/2)*t(i)^2 + (a10/3)*t(i)^3 + (a11/4)*t(i)^4 + (a12/5)*t(i)^5 + a13);
            S(i) = R*(a8*log(t(i)) + a9*t(i) + (a10/2)*t(i)^2 + (a11/3)*t(i)^3 + (a12/4)*t(i)^4 + a14);
        end
    end
    
   
    % plotting
    figure(1)
    subplot(3,1,1);
    plot(t,Cp,'linewidth',2,'color','r')
    xlabel('local temp')
    ylabel('Specific Heat')
    subplot(3,1,2);
    plot(t,H,'linewidth',2,'color','g')
    xlabel('local temp')
    ylabel('Enthalpy')
    subplot(3,1,3);
    plot(t,S,'linewidth',2,'color','b')
    xlabel('local temp')
    ylabel('Entropy')
    
    % changing the directory to a manually created folder
    dir = (['C:\Users\lenovo\Documents\MATLAB\File Parsing']);
    cd(dir);
    
    saveas(1,species)
    
end

Explanation :

  1. clear all : clears the workspace data

    close all : clears all the figures and plots

    clc : clears the command window

  2. Loading the Thermo file using fopen.
  3. The first 5 lines of the file are basically header lines. So these are parsed simply sing the fgetl command. The second line contains the global temp values. The matlab however recognieses these as an array of strings and not as numbers. Therefore, the str2num command is used after splitting the single array into inddividual temp arrays using strsplit.
  4. A folder is created to eventually dump the plot files using the mkdir command.
  5. Since, there are total 53 species, the further code is repeated inside a for loop for 53 times.
  6. The sixth line consists of species name and local temp values. These are parsed in the same way as stated in (3)
  7. The 14 coefficients(a1 - a14) are extracted out of the 3 lines.
  8. A 1D array of temperature values in the local temperature range is created.
  9. To calculate the values of thermodynamic properties of Cp, H and S oover these temp values, a for loop is executed.
  10. Finally, the plotting is done for the 3 cases. All these plots are basically subplots which are displayed in the same figure window.
  11. The directory is set to the initially created folder.
  12. The plots are saved inside this directory using the saveas command.

OUTPUT :

A plot of one of the 53 species (O) is shown below -

Here's a screenshot of the window displaying the plots for all the species inside the directory folder -

REFERENCES : Mathworks

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.