Project

Parsing NASA Thermodynamic Data

Parsing NASA Thermodynamic Data

Aim: To write code in MATLAB to parse the given NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.

Objective:

  • To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the given data file.
  • Calculate the molecular weight of each species and display it in the command window.
  • Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature:high temperature) specific for each species.

Theory:

What is File Parsing?

Parsing in computer languages refers to the syntactic analysis of the input code into its component parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation.

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 some kind of transformation on the data.

NASA thermo.dat File:

The NASA polynomials have the form:

    Cp/R = a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4
    
    H/RT = a1 + a2 T /2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T
    
    S/R  = a1 lnT + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7

where a1, a2, a3, a4, a5, a6, and a7 are the numerical coefficients supplied in NASA thermodynamic files.

The first 7 coefficients are high-temperature coefficients and the second 7 coefficients are low-temperature coefficients.

Shown below is an example of O2 as it is seen in our given file and what all the numbers and characters mean.

Main Program:

clear all
close all
clc

% Reading file
fnasa=fopen('THERMO.dat','r');

% To get heading line
line_1=fgetl(fnasa);

% Global Temperature Values
line_2=fgetl(fnasa);
global_temp=strsplit(line_2,' ');
global_min_temp=str2double(global_temp(2));
global_mid_temp=str2double(global_temp(3));
global_max_temp=str2double(global_temp(4));

% For skipping the next 3 lines
for i=1:3
    c=fgetl(fnasa);
end

& To allow the user to enter which species data he wants to know
Species = input('Enter the name of the species:','s');

% Species information
ct=0; % ct variable is given just to understand the position of the species name that is inputted by the user

% "While loop" is better than for loop because "while loop" can itself know how many variables are in the THERMO data file and can read new variables inputted into the data file if someone edits it and puts a new species into it.
while ~feof(fnasa)
    species=fgetl(fnasa);
    species_find=strsplit(species,' ');
    species_name=species_find{1};
    local=strfind(species,'G');
    local_min_temp=str2double(species(local+4:local+10));
    local_max_temp=str2double(species(local+13:local+20));
    local_mid_temp=str2double(species(local+23:local+30));
    
    % First line of coefficients
    line1=fgetl(fnasa);
    L1=findstr(line1,'E');
    
    c1=str2num(line1(1:L1(1)+3));
    c2=str2num(line1(L1(1)+4:L1(2)+3));
    c3=str2num(line1(L1(2)+4:L1(3)+3));
    c4=str2num(line1(L1(3)+4:L1(4)+3));
    c5=str2num(line1(L1(4)+4:L1(5)+3));
    
    % Second line
    line2=fgetl(fnasa);
    L2=findstr(line2,'E');
    
    c6=str2num(line2(1:L2(1)+3));
    c7=str2num(line2(L2(1)+4:L2(2)+3));
    c8=str2num(line2(L2(2)+4:L2(3)+3));
    c9=str2num(line2(L2(3)+4:L2(4)+3));
    c10=str2num(line2(L2(4)+4:L2(5)+3));
    
    % Third line
    line3=fgetl(fnasa);
    L3=findstr(line3,'E');
    
    c11=str2num(line3(1:L3(1)+3));
    c12=str2num(line3(L3(1)+4:L3(2)+3));
    c13=str2num(line3(L3(2)+4:L3(3)+3));
    c14=str2num(line3(L3(3)+4:L3(4)+3));
    
    temperature=linspace(local_min_temp,local_max_temp,100);
    
    if strcmpi(Species,species_name) % It will calculate the properties, only if the species entered by the user and species name in the registry matches 
   
    % Calculations for properties for lower and upper temperature ranges
    for j = 1:length(temperature)
        [cp,h,s]=thermprop(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,local_mid_temp,temperature(j));
        cp_1(j)=cp;
        h_1(j)=h;
        s_1(j)=s;
    end
    
    % Creating the folder
    folder=pwd;
    mkdir('NASA',species_name);

    % Plotting the graphs
    figure(1)
    plot(temperature,cp_1);
    title(species_name);
    xlabel('Temperature(K)');
    ylabel('Specific Heat(KJ/Kmol-K)');
    str1=sprintf('C:/Users/Skill-Lync/OneDrive/Akhil/NASA/%s',species_name);
    cd(str1);
    saveas(1,'cp.png');
    
    figure(2)
    plot(temperature,h_1);
    title(species_name);
    xlabel('Temperature(K)');
    ylabel('Enthalpy(J)');
    saveas(2,'enthalpy.png');
    
    figure(3)
    plot(temperature,s_1);
    title(species_name);
    xlabel('Temperature(K)');
    ylabel('Entropy(J/K)');
    saveas(3,'entropy.png');
    
    cd(folder); 
    
    MM=molecularmass(species_name);
    k=sprintf('Molecular Mass of %s = %f',species_name,MM)
    break;
    end
    ct = ct+1; 
end

fclose(fnasa);

    
    
    

Function for calculating Specific Heat, Enthalpy, and Entropy:

function [cp,h,s]=thermprop(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,local_mid_temp,temperature)

R=8.3145;

if(temperature>local_mid_temp)
    cp=(c1+(c2*temperature)+(c3*(temperature^2))+(c4*(temperature^3))+(c5*(temperature^4)))*R;
    h=(c1+(c2*(temperature/2))+((c3*(temperature^2))/3)+((c4*(temperature^3))/4)+((c5*(temperature^4))/5)+(c6/temperature))*R*temperature;
    s=((c1*(log(temperature)))+(c2*temperature)+((c3*(temperature^2))/2)+((c4*(temperature^3))/3)+((c5*(temperature^4))/4)+c7)*R;
else
    cp=(c8+(c9*temperature)+(c10*(temperature^2))+(c11*(temperature^3))+(c12*(temperature^4)))*R;
    h=(c8+(c9*(temperature/2))+((c10*(temperature^2))/3)+((c11*(temperature^3))/4)+((c12*(temperature^4))/5)+(c13/temperature))*R*temperature;
    s=((c8*(log(temperature)))+(c9*temperature)+((c10*(temperature^2))/2)+((c11*(temperature^3))/3)+((c12*(temperature^4))/4)+c14)*R;
end

Function for calculating Molecular Mass:

function MM=molecularmass(species_name)

constituents=['H','C','N','O','Ar'];
atomic_mass=[1.00794,12.0107,14.0067,15.994,39.948];

MM=0;
u=0;

for i=1:length(species_name)
    for j=1:length(atomic_mass)
        if strcmpi(species_name(i),constituents(j))
            MM=MM+atomic_mass(j);
            u=j;
        end
    end
    var=str2double(species_name(i));
    if (var>1)
        MM=MM+(atomic_mass(u)*(var-1));
    end
end
end

Output:

Screenshot of the window where all the folders are saved:

Graphs for O2:

Graphs for N2:

Graphs for CO2:

Command Window showing user to enter input and its molecular mass is shown after compilation of graphs:

Results and Conclusions:

Therefore, we have successfully parsed the given NASA thermodynamic data file and plotted the graphs of specific heat, enthalpy and entropy for the respective inputted species name.

Parsing is a very important part of many computer science disciplines. For example, compilers must parse source code to be able to translate it into object code. Likewise, any application that processes complex commands must be able to parse the commands. This includes virtually all end-user applications.

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.