Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:

The main objective of this project is to write a code in matlab for parsing the NASA Thermodynamic data file and to calculate the thermodynamic properties of various gas species.

File parsing:

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

File parsing is the method of splitting the data file into different parts and store. By this we can read and used the data in the easier way. In this project we are going to split the NASA Thermodynamic data which is having 53 various gas species.

NASA Thermodynamic data:

NASA came up with the polynomials that can be used to evaluate thermodynamic properties such as Cp, H, S. They have also documented coefficients that are required to evaluate these polynomials for 1000+ species. Out of this. 53 various species are taken for the calculation.

The polynomials used to calculate specific heat, entropy and enthalpy are:

`(Cp)/R=a1+a2T+a3T^2+a4T^3+a5T^4`

`H/(RT)=a1+(a2T)/2+(a3T^2)/3+(a4T^3)/4+(a5T^4)/5+(a6/T)`

`S/R=a1 lnT+a2T+(a3T^2)/2+(a4T^3)/3+(a5T^4)/4+a7`

Where,

`R=8.314`(Universal gas constant)`

`T=Temperature (K)`

`Cp=`Specific Heat `((kJ)/(kg-K))`

`S= Entropy (J/K)`

`H= Enthalpy (J)`

Objectives:

  1. Write a function to extract the 14 coefficients and calculates the entropy, enthalpy and specific heat for all the species in the data file.
  2. Calculate the molecular weight of each species.

Matlab Code:

Function code used to calculate the Cp, H, S:

Function code:              
    
%calculating Thermodynamics properties
function[Cp,H,S]=thermodynamics(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,local_low_temp,local_mid_temp,local_high_temp,local_temp_range)
R=8.314; %Universal gas constant
%Low temperature range
L_T=linspace(local_low_temp,local_mid_temp,250);
%High temperature range
H_T=linspace(local_mid_temp,local_high_temp,250);
if (local_temp_range>=local_mid_temp)
    %calculating Cp,H & S for first sevan coefficients(c1-c7)
    %specific heat at constant pressure
    Cp=((c1+(c2.*H_T)+(c3.*H_T.^2)+(c4.*H_T.^3)+(c5.*H_T.^4)).*(R));
    
    %Enthalpy
    H=((c1+((c2.*H_T))./2)+((c3.*(H_T).^2)./3)+((c4.*(H_T).^3)./4)+((c5.*(H_T).^4)./5)+(c6./H_T)).*(H_T.*R);
    
    %Entropy
    S=(c1.*log(H_T)+(c2.*H_T)+((c3.*(H_T).^2)./2)+((c4.*(H_T).^3)./3)+((c5.*(H_T).^4)./4)+(c7)).*(R);
    
else
    %Calculating Cp,H,S for second sevan coefficients(c8-c14)
    %specific heat at constant pressure
    Cp=((c8+(c9.*L_T)+(c10.*L_T.^2)+(c11.*L_T.^3)+(c12.*L_T.^4)).*(R));
    
    %Enthalpy
    H=((c8+((c9.*L_T))./2)+((c10.*(L_T).^2)./3)+((c11.*(L_T).^3)./4)+((c12.*(L_T).^4)./5)+(c13./L_T)).*(L_T.*R);
    
    %Entropy
    S=(c8.*log(L_T)+(c9.*L_T)+((c10.*(L_T).^2)./2)+((c11.*(L_T).^3)./3)+((c12.*(L_T).^4)./4)+(c14)).*(R);
    
end
end

Procedure:

  • The function is created as a ‘thermodynamics()’.
  • The required inputs are given calculate like, universal gas constant (R) and the low temperature range and high temperature range by using ‘linspace()’ command. With local low, mid and high temperature.
  • Next calculating the Cp, H & S value by using ‘if-else loop’ with the local temperature values defined in the main code.
  • The compiler terminates the program.

Main code:

%code for NASA Thermodynamic data parsing
clear all
close all
clc
%Importing the 'THERMO.dat' file contents 
n=fopen('THERMO.dat','r');
line_1=fgetl(n)%skipping the line from data file
line_2=fgetl(n)

%obtaining global temperature values
%storing them as a numbers in separate values
global_temp=strsplit(line_2,' ');
global_low_temp=str2num(global_temp{2});
global_mid_temp=str2num(global_temp{3});
global_high_temp=str2num(global_temp{4});
%Skipping the lines from the data file
for i=3:5
    line_5=fgetl(n);
end
%loop for all the 53 species
for j=1:53
    %Getting to the 1st line of 1st species 'o'
    line_6=fgetl(n);
    
    %Starting with the first species 
    %Separating the first line by delimiter
    b=strsplit(line_6,' ');
    
    %Obtaining the species name
    species_name=(b{1});
    
    %Finding the location of 'G'string
    loc_1=strfind(line_6,'G');
    
    %Obtaining the local Temperature values
    local_low_temp= str2double(line_6(loc_1+4:loc_1+10));
    local_high_temp= str2double(line_6(loc_1+13:loc_1+20));
    local_mid_temp=str2double(line_6(loc_1+23:loc_1+30));
    
    %local Temperature range of species
    local_temp_range=linspace(local_low_temp,local_high_temp,250);
    
    %Extracting the 14 coefficients 
    %Getting the 1st line of coefficients
    line_7=fgetl(n);
    
    %Finding the location of 'E'string
    loc_2=strfind(line_7,'E');
    
    %Extracting all the coefficient from 1st time
    c1=str2double(line_7(1:loc_2(1)+3));
    c2=str2double(line_7(loc_2(1)+4:loc_2(2)+3));
    c3=str2double(line_7(loc_2(2)+4:loc_2(3)+3));
    c4=str2double(line_7(loc_2(3)+4:loc_2(4)+3));
    c5=str2double(line_7(loc_2(4)+4:loc_2(5)+3));
    
    %Getting the 2nd line of coefficients
    line_8=fgetl(n);
    %finding the location of 'E'string
    loc_3=strfind(line_8,'E');
    %Extracting all the coefficents from 2nd line
    c6=str2double(line_8(1:loc_3(1)+3));
    c7=str2double(line_8(loc_3(1)+4:loc_3(2)+3));
    c8=str2double(line_8(loc_3(2)+4:loc_3(3)+3));
    c9=str2double(line_8(loc_3(3)+4:loc_3(4)+3));
    c10=str2double(line_8(loc_3(4)+4:loc_3(5)+3));
    
    %Getting the 3rd line of co-efficients
    line_9=fgetl(n);
    %finiding the location of 'E'string
    loc_4=strfind(line_9,'E');
    %Extracting all the coefficients from 3rd line
    c11=str2double(line_9(1:loc_4(1)+3));
    c12=str2double(line_9(loc_4(1)+4:loc_4(2)+3));
    c13=str2double(line_9(loc_4(2)+4:loc_4(3)+3));
    c14=str2double(line_9(loc_4(3)+4:loc_4(4)+3));
    
    %Function for calculating Cp,H & S
    [Cp,H,S]=thermodynamics(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,local_low_temp,local_mid_temp,local_high_temp,local_temp_range);
    
    %Creating the folders for species and store the plots individually
    NASA_file_parsing=sprintf('%s',species_name);
    %Assinging the current directory to the 'c_d'
     c_d= pwd;
    
    %Make the new directory under the current directory as 'NASA_file_parsing' and
    %creating new folders in the current directory of species names
    mkdir(NASA_file_parsing);
    
    cd(NASA_file_parsing);
    
    %plotting 
    %Cp vs Temperature
    figure(1)
    plot(local_temp_range,Cp,'linewidth',3)
    xlabel('Local_Temperature_range')
    ylabel('Cp-specific_heat_at constant Temperature')
    title('Cp vs Speciec_Temperature')
    grid on
    getframe(gcf);
    saveas(1,'Enthalpy_plot.png') % Enthalpy(H)=Specific_heat (Cp)*change in Temperature (dT)
    
    %Enthalpy vs Temperature
    figure(2)
    plot(local_temp_range,H,'linewidth',3)
    xlabel('Local_Temperature_range')
    ylabel('H-Enthalpy')
    title('Enthalpy vs Species_Temperature')
    grid on
    getframe(gcf);
    saveas(2, 'Entropy_plot.png') %S=dQ/T
    
    %Entropy vs Temperature
    figure(3)
    plot(local_temp_range,S,'linewidth',3)
    xlabel('Local_Temperature_range')
    ylabel('S-Entropy')
    title('Entropy vs Species_Temperature')
    grid on
    getframe(gcf);
    saveas(3, 'Specific_heat(Cp)_plot.png')
    
    %Setting current directiry as 'c_d' (original directory)
    cd(c_d);
    
    %Calculating the molecular weight of species
    species_elements={'H','C','N','O','A'};
    Atomic_weight=[1,12,14,16,40];
    mol_weight=0;
    
    %for single element
    for k=1:length(species_name)
        for l=1:length(species_elements)
            if strcmp(species_name(k),species_elements(l))==1
                mol_weight=mol_weight+Atomic_weight(l);
                position=l;
            end
        end
        n1=str2double(species_name(k));
        %for combination of elements
        if n1>1
            mol_weight=mol_weight+((Atomic_weight(position))*(n1-1));
        end
    end
    %printing the calculated molecular weight
    sprintf('Molecular weight of [%s]-%du \n',species_name,mol_weight)
end

                
  

Procedure:

  • Initially, import the ‘THERMO.dat’ data file by using ‘fopen()’ command.
  • First five lines of the data has been skipped, except 2nd line and define global temperature values.
  • The loop starts for 53 species
  • The 6th line of data file have been read by using ‘fgetl()’ command. And separating the line by delimiter by using ‘strsplit()’ command.
  • Next, the species names is stored in the variable ‘species_name’.
  • Then the ‘G’ string is located by using ‘strfind()’ command.
  • Next, obtained the local temperature values for low, mid and high temperatures.
  • Then all the 14 coefficients are extracted. And the function is called (which is defined separately) to calculate the Cp, H and S values.
  • Then the folder is created in species name.
  • The plots are saved individually in the folders.
  • Next the molecular weights are calculated for each species.
  • The compiler terminates the program.

Output:

  • Plots for species `'O'`

  • Plots for `'O2'`

  • Workspace:

  • Screen shot of all species plots stored:

  • Molecular weights of all the 53 species:

Conclusion:

  • Screen shoot of all files stored is provided.
  • Command window screen shoot of molecular weights were provided.
  • Hence the parsing of NASA Thermodynamic data and calculation of thermodynamic properties of all the 53 species were done.

Reference:

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.