Challenge

Project 1 - Parsing NASA thermodynamic data

Project: Parsing NASA thermodynamic data

Aim: To write a code to parse NASA thermodynamic data and calculate thermodynamic properties of the considered various gas species

LANGUAGE: Matlab

SOFTWARE: Matlab R2019a

SUMMARY:

Objective

Theory & calculation function

Code and explanation

Conclusion

References:

Objective

  1. 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 named ‘THERMO.DAT’.

The formulae are shown below.

`(Cp)/R= a_1+a_2*T+a_3*T^2+a_4*T^3+a_5*T^4`

`H/(RT)=a_1+a_2*T/2+a_3*T^2/3+a_4*T^3/4+a_5*T^4/5+a_6/T`

`S/R=a_1*ln(T)+a_2*T+a_3*T^2/2+a_4*T^3/3+a_5*T^4/4+a_7`

Where

R = Universal Gas constant

T = Temperature

Cp = Specific heat (KJ)

S = Entropy(KJ/mol-K)

H = Enthalpy(KJ/mol)

  1. Calculate the molecular weight of each species and display it in the command window.
  2. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
  3. 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 & calculation function:

Parsing also called syntax analysis or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. In computer science parsing refers to how data can be read from a file and how that information can be handled.

THERMO.dat file is the given data file which contains 14 coefficients for 53 different elements given by NASA. These coefficients are utilized to calculate the thermodynamic properties (specific heat, enthalpy & entropy) for a particular element. The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients. Each element has its own range of local temperature.

List of 53 elements contained in 'THERMO.dat' file:

AR C C2H C2H2 C2H3 C2H4

C2H5 C2H6 C3H7 C3H8 CH CH2

CH2(S) CH2CHO CH2CO CH2O CH2OH CH3

CH3CHO CH3O CH3OH CH4 CN CO

CO2 H H2 H2CN H2O H2O2

HCCO CCOH HCN HCNN HCNO HCO

HNCO HNO HO2 HOCN N N2

N2O NCO NH NH2 NH3 NNH

NO NO2 O O2 OH

Specific heat is an intensive property and an intrinsic characteristic of a substance. It refers to the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree.

`Cp=R*( a_1+a_2*T+a_3*T^2+a_4*T^3+a_5*T^4)`

Function for calculating Specific heat:

function[Cp]= calculate_cp(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,l_temp_range,R)
    
    for i= 1:length(l_temp_range)
     %checking the the condition
    if l_temp_range(i)>temp(2) %High temperature coefficients will be considered
        
        Cp(i)= R*(a1 + (a2*l_temp_range(i)) + (a3*(l_temp_range(i).^2)) + (a4*(l_temp_range(i).^3)) + (a5*(l_temp_range(i).^4))); 
        
    else % Low temperature coefficients will be considered
    Cp(i)= R*(a8 + (a9*l_temp_range(i)) + (a10*(l_temp_range(i).^2)) + (a11*(l_temp_range(i).^3)) + (a12*(l_temp_range(i).^4)));    
        
    end
    end

Enthalpy is the sum of the internal energy of the system plus the product of the pressure of the gas in the system and its volume. In a system enclosed (no mass transfer), for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.

`H=R*T*(a_1+a_2*T/2+a_3*T^2/3+a_4*T^3/4+a_5*T^4/5+a_6/T)`

Function for calculating enthalpy:

%function to calculate enthalpy
function H =calculate_enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,temp,l_temp_range,R)
 
  for i=1:length(l_temp_range)
      %checking the the condition
    if l_temp_range(i)>=temp(2) %High temperature coefficients will be taken
    H(i)=R*(l_temp_range(i))*(a1+(a2*l_temp_range(i)/2)+(a3*(tel_temp_range(i).^2)/3)+(a4*(l_temp_range(i).^3)/4)+(a5*(l_temp_range(i).^4)/5)+(a6./l_temp_range(i)));
    else % Low temperature coefficients will be taken
    H(i)=R*(l_temp_range(i))*(a8+(a9*l_temp_range(i)/2)+(a10*(l_temp_range(i).^2)/3)+(a11*(l_temp_range(i).^3)/4)+(a12*(l_temp_range(i).^4)/5)+(a13./l_temp_range(i)));
   end
end


Entropy is the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work.

`S=R*(a_1*ln(T)+a_2*T+a_3*T^2/2+a_4*T^3/3+a_5*T^4/4+a_7)`

Function for calculating entropy:

%function to calculate entropy

function S =calculate_entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temp,l_temp_range,R)
%checking the the condition 
for i=1:length(l_temp_range)
if l_temp_range(i)>=temp(2) %High temperature coefficients will be taken
    S(i)=R*(a1*log(l_temp_range(i))+(a2*l_temp_range(i))+(a3*(l_temp_range(i).^2)/2)+(a4*(l_temp_range(i).^3)/3)+(a5*(l_temp_range(i).^4)/4)+a7);
else % Low temperature coefficients will be taken
S(i)=R*(a8*log(l_temp_range(i))+(a9*l_temp_range(i))+(a10*(l_temp_range(i).^2)/2)+(a11*(l_temp_range(i).^3)/3)+(a12*(l_temp_range(i).^4)/4)+a14);
end
end

Molecular weight is the mass of a molecule of a substance. Hence the weight of a molecule may be calculated as the sum of the atomic weights of its constituent atoms and it is expressed in daltons (Da or u). Different molecules of the same compound may have different weight because they may contain different isotopes of an element.

Function for calculating molecular weight:

%function to calculate molecular weight
function molecular_weight=calculate_molecular_weight(species)
%concerned atomic names:
fprintf('Molecular weight of ');
atomic_name=['H','C','O','N','A'];
fprintf(species);
fprintf(':');
%concerned atomic number
atomic_weight=[1.008 12.011 15.999 14.007 39.948];
%Initialization of molecular weigth
molecular_weight=0;

%calculation of molecular weight
  for i=1:length(species)
     for j=1:length(atomic_name)
        if strcmpi(species(i),atomic_name(j))
            molecular_weight=molecular_weight+atomic_weight(j);
            position=j;
        end
     end
    n=str2num(species(i));
    if n>1
        molecular_weight=molecular_weight+atomic_weight(position)*(n-1);
   end
  end
 
 fprintf('%f',molecular_weight);
 disp(' ');
end
        

Main code & explanation:

clear all
close all
clc

% Let's make directory
mkdir 'C:data_parsing'

%  Reading Data file - NASA Thermodynamic
% Open a file & 'r' Open the file for reading 
f1=fopen('THERMO.dat','r')
gt=fgetl(f1);

% Obtaing Temperatures - by Reading Header Information

global_temp=fgetl(f1);

% Spliting
st=strsplit(global_temp,' ');

% Converting string cells to numbers using str2num,
global_low_temp=str2num(st{2});
global_medium_temp=str2num(st{3});
global_high_temp=str2num(st{4});


    R = 8.314;   
    
% skipping three comment lines in the file
for i = 1:3
    comment=fgetl(f1);
end

 for i = 1:53
     
    tline=fgetl(f1);
    A=strsplit(tline,' ');
    species=(A{1});
    
    %Extraction of local temperatures
    local_temp_range=tline(49:75);
    D=strsplit(local_temp_range,' ');
   %let's convert them into number
    l_low_temp=str2num(D{1});
    l_medium_temp=str2num(D{2});
    l_high_temp=str2num(D{3});
    
    %Let's create arrays with thes temperature
    temp=[l_low_temp,l_medium_temp,l_high_temp];
    l_temp_range=linspace(l_low_temp,l_high_temp,500);

  % Let's read the first line of coefficients
    line_coef1=fgetl(f1)  ;        
    a=strfind(line_coef1,'E'); %Let's locate 'E' in the coefficients


    % % Extracting FIRST 7 coefficients i.e HIGH-temperature coefficients 
    a1=line_coef1(1:a(1)+3);
    a1=str2num(a1);              %1
    a2=line_coef1(a(1)+4:a(2)+3);
    a2=str2num(a2) ;             %2
    a3=line_coef1(a(2)+4:a(3)+3);
    a3=str2num(a3)  ;            %3
    a4=line_coef1(a(3)+4:a(4)+3);
    a4=str2num(a4);              %4
    a5=line_coef1(a(4)+4:a(5)+3);
    a5=str2num(a5)  ;            %5
% Let's read the second line of coefficients
    line_coef2=fgetl(f1);
    b=strfind(line_coef2,'E'); %Let's locate 'E' in the coefficients
    a6=line_coef2(1:b(1)+3);
    a6=str2num(a6)  ;            %6
    a7=line_coef2(b(1)+4:b(2)+3);
    a7=str2num(a7);              %7

     % Extracting SECOND 7 coefficients - LOW-temperature coefficients
    a8=line_coef2(b(2)+4:b(3)+3);         
    a8=str2num(a8);              %8
    a9=line_coef2(b(3)+4:b(4)+3);
    a9=str2num(a9) ;             %9
    a10=line_coef2(b(4)+4:b(5)+3);
    a10=str2num(a10) ;           %10

% Let's read the third line of coefficients
    line_coef3=fgetl(f1);
    c=strfind(line_coef3,'E'); %Let's locate 'E' in the coefficients

    a11=line_coef3(1:c(1)+3);
    a11=str2num(a11);               %11
    a12=line_coef3(c(1)+4:c(2)+3);
    a12=str2num(a12);               %12
    a13=line_coef3(c(2)+4:c(3)+3);
    a13=str2num(a13);               %13
    a14=line_coef3(c(3)+4:c(4)+3);
    a14=str2num(a14); 

    %Let's calculate specific heat Cp
    
    cp = calculate_cp(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,temp,l_temp_range,R);
    H=calculate_enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,temp,l_temp_range,R);
    S =calculate_entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,temp,l_temp_range,R);
  
        
         %plotting(CP,H,S,Temp,Species_SP)
         diag_dir=pwd;
           mkdir(species); %to make a new directory
           cd(species);
           figure(1)
        plot(l_temp_range,cp,'linewidth',2,'color','r');
        xlabel('Temperature  [K]');
        ylabel('Specific Heat [kJ]');
        title(sprintf('Varying Specific Heat with Temperature Range of %s',species));
        grid on
        saveas(figure(1),'Specific Heat.jpg');
         
        % Plot 2, Enthalpy (H) vs Temperature Range (Temp)
 
        figure(2)
        plot(l_temp_range,H,'linewidth',2,'color','b');
        xlabel('Temperature [K]');
        ylabel('Enthalpy in [kJ/(mol)]');
        title(sprintf('Varying Enthalpy with Temperature Range of %s',species));
        grid on
        saveas(figure(2),'Enthalpy.jpg');
        
         % Plot 3, Entropy (S) vs Temperature Range (Temp)
 
        figure(3)
        plot(l_temp_range,S,'linewidth',2,'color','g');
        xlabel('Temperature [K]');
        ylabel('Entropy in [kJ/(mol-K)]');
        title(sprintf('Varying Entropy with Temperature Range of %s',species));
        grid on
        saveas(figure(3),'Entropy.jpg');
      
        
        cd(diag_dir);            
 
        % Molecular weight
        
        M = calculate_molecular_weight(species);
         f2 = fopen('Molecular weight.txt','a+');
         fprintf( f2,'Molecular weight  %s is %d \n',species,M);
        fclose(f2); % close the file 
        
        
 end

Specific syntax used during Coding

fopen : Open a file or obtain information about open files

‘r’ : To open the file for reading

'w': To write to the file

fgetl: To read line from file

strsplit: To split string or character vector at specified delimiter

str2num: To convert character array or string to numeric array

strfind: To find one string within another

linspace: To generate linearly spaced vectors

fprintf: To write formatted data to file

pwd: To identify current directory

mkdir: To make new directory

cd: To change working directory

Stepwise explanation

  • First we start the code freeing the memory by using ‘clear all’, ‘close all’ and ‘clc’ commands.
  • We make a new directory with ‘mkdir’ command
  • We write the function to calculate the different properties(enthalpy, entropy, specific) of element by taking into account the condition that the first 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
  • For the molecular weight calculation we create a condition using if condition and strcmp command to compare the species and atomic name strings. If by comparing both strings are found to be same then we will assign its molecular weight.
  • We read the first lines using fgetl() command and split them if needed
  • We use for loop to extract the coefficients of each of the 53 given elements
  • Here we use fgetl to read the line, strsplit to split and str2num to convert them into numeric array. Finally we assign them to coefficient variables (a1,a2,a3…a14)
  • We call the functions previously written to calculate the properties
  • We plot the diagrams and put them in a folder for each elements

Output:

Screenshot of the Directory where Different Species are saved:


Plots for O2 specie:

Plots for N2 specie:

Plots for CO2 specie:


Conclusion:

Parsing is a technique very useful helping to read informations from files and handle them. With this project it is clear that the input may just be a simple file and the rest remains in the art of handling it. Hence all the aspects of this technique become clear and understandable.

References:

https://in.mathworks.com/help/matlab/ref/disp.html

https://in.mathworks.com/help/matlab/ref/strcmpi.html

https://en.wikipedia.org/wiki/Parsing

https://www.britannica.com/science/specific-heat

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.