Challenge

Project 1 - Parsing NASA thermodynamic data

Aim

Write a code in matlab/Octave to parse the NASA thermodynamic data file and then calculate the

themodynamic properties of various gas species

Objective

1. Write a function thet extracts the 14 co-efficient and calculats th enthalpy,entropy and specific heats

all the species in the data file.


You will be reading this file NASA thermodynamic data

2. Calculate the molecular weight of each species and display it in the command window.

3. Plot the Cp,Enthalpy and Entropy for the local temperature range(low tmperature:high temperature)

specific for ech species.

4. Save the plots as images with appropriate names and dump them in seperate folders for ach species

with suitable names. All these should be generated automatically.

5. The main program that you will submit will just take the "THERMO.dat"file as an input and generate

Cp,H and S plots.

6 Screenshot of the windows where all the folders are saved.

7.Plot for O2,N2 and Co2 species to be show

The Formulae

Where

R = Universal Gas Constant

T = Temperature

Cp = Specific Heat(K)

S = Entropy(KJ/mol-K)

H = Enthalpy(KJ/mol)

a(i) = Coefficient where, i = 1 to 14

Definitions

File Parcing

Parcing ,syntax analysis is the process of analysing a string of symbols,either in natural language,

computer languages or data structures,conforming to rules of a formal grammar. The term parsing

comes from latin pars(orationis).Meaning part of speech.Within computational linguistics the term is

used to refer to formal analysis by a computer of asentance or other string of words into its constituent,

resulting in parse tree showing their syntactic relation to each other,which may also contain sematic

and other information.Some parsing algorithms may generate a parse forest or list of parse trees for

a syntactically ambiguous input.

Parsing a file means reading in a data stream of some sort and building an in memmory model of the

semantic content of that data.This aims at facilitating performing some kind of transformation on the

data.

NASA Thermodynamic File

NASA themodynamic file which is THERMO.dat file in which there are temperature range and 53 species

are given in these data and each species contains 14 coefficient and temperature ranges and we have to

parse these 14 coefficient to find Specific heat.Entropy and Enthalpy using formula.

Specific Heat

The specific heat is the amount of heat per unit mass required to rise the temoerature by one degree

Celsius.

Entropy

Entropy, the measure of a system's thermal energy per unit temperature that is available for doing useful

work.Because work is obtained from orderd molecular motion, the amount of entropy is also a measure

of the molecular disorder,or randomness of a system.

Enthalpy

Enthalpy, a thermodynamic quantity equivalent to the total heat content of a system.It is equal to the

internal energy of the system plus the product of pressure and volume.In a system contained so as to

prevent mass transfer,for process at constant pressure, the heat absorbed or released equal to change

in enthalpy.

Program

Functions

Function to find its specific heat:-

%Specific Heat calculation Function

function Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,midtemp)

%calculating specific heat for higher and lower temperature respectively.
for i=1:length(T)
    
if (T(i) <= midtemp)
    Cp(i) = R*(a8+ (a9*T(i)) + (a10*(T(i)).^2) + (a11*(T(i)).^3) + (a12*(T(i)).^4));
    
else
    
    Cp(i) = R*(a1+ (a2*T(i)) + (a3*T(i)^2) + (a4*T(i)^3) + (a5*T(i)^4));
    
end
end
end

steps

1.Now we will find the the Specific Heat by creating specific heat function and assigning its related

formula coefficient from 14 coeffcients, Gas constant ,Temperature and mid temperature.

2.Now we will create a condition using the"if" loop and make the temperature less than and equal to

mid temperature to specify lower temperature coefficient in the formula.

3.Then we will use the eske condition to specify coefficient of Higher tmperatur coefficient in the formula

4.Then end the loop and function to calculate the "Specific Heat".

Function to find the entropy :-

% Entropy Calculation Function
function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)

%calculating entropy for higher and lower temprature respectively.
for i=1:length(T)
if (T(i) <= tempmid)
    
    S(i) = R*(a8.*log(T(i)) + (a9*T(i)) + ((a10*(T(i)).^2)/2) + ((a11*(T(i)).^3)/3) + ((a12*(T(i)).^4)/4) + a14);
    
else
    S(i) = R*(a1.*log(T(i)) + (a2*T(i)) + ((a3*(T(i)).^2)/2) + ((a4*(T(i)).^3)/3) + ((a5*(T(i)).^4)/4) + a7);

end
end

Steps

1. Now we will find the entopy function and assigning its related formula coefficient from 14 coefficients,

Gas constant,temperature and mid temperature.

2. Now we will create a condition using the "if" loop and make the temperature less than and equal to mid

temperature to specify lower temperature coefficient in the formula.

3. Then we will use the else condition to specify coefficient of higher temperature coefficient in the

formula.

4 Then end the end loop and function to calculate the 'Entropy'.

Function to find Enthalpy

%Enthalpy calculation function
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)

%calculating enthalpy for higher and lower temperature respectively
for i=1:length(T)
    
if(T(i) <= tempmid)
    
    H(i) = R*T(i).*(a8+ ((a9*T(i))/2) + ((a10*(T(i)).^2)/3) + ((a11*(T(i)).^3)/4) + ((a12*(T(i)).^4)/5) + ((a13./T(i))));
    
else
    H(i) = R*T(i).*(a1 + ((a2*T(i))/2) + ((a3*(T(i)).^2)/3) + ((a4*(T(i)).^3)/4) + ((a5*(T(i)).^4)/5) + ((a6./T(i))));
end
end

Steps

1. Now we will find Enthalpy function and assigning its related formula coefficient from 14 coefficient,and

mid temperature

2. Now we will create a condition using the'if'loop and make the temprature less than and equal to mid

temperature to specify lower temperature coefficient in the formula.

3 Then we will use the else condition to specify coefficient of higher temperature coefficient in the

formula.

4. Then end the loop and function to calculate the 'Enthalpy'.

Function to find the molecular weight of the species:-

%Molecular weight Calculation Function
function [MW] = Molecular_weight(species)

% Defining Atomic Name of the species

atomic_name = ['H','C','O','N','Ar'];

%Hydrogen , Carbon , Oxygen, Nitrogen , Argon

%Defining Atomic masses of main species
atomic_weight = [1.008 12.011 15.999 14.007 39.948];

%Molecular weight starting range 
MW = 0;

%Adding Atomic Number to obtain particular value
%Loop runs to the length of the species
for i = 1:length(species)
    % Loop runs to the length of the atomic name
    for j = 1:length(atomic_name)
        % function to compare 2 strings
        if strcmp(species(i), atomic_name(j))
            MW = MW + atomic_weight(j);
            position = j;
        end
        
    end
   %finding more elements in the species and incrementing atomic weight 
 
   n = str2num(species(i));
   if n>1
     MW = MW + atomic_weight(position)*(n-1);
   end
end
fprintf('Molecular Weight of %s : ',species);
fprintf('%f',MW);
disp(' ');
end
 
   
  
    
 
   
  

Steps

1. Firstly we create an Molecular_weight function MW and assigning its species input to that.

2. Then define the main element of all 53 species in an array atomic_name that are:-'H','C',O,'N',"Ar'.

3. Then by taking the above main elements well create there attomic_weight array from any source like

internet that are:-00812.01115.99914.00739.948.

4. Then we will assign molecular starting range from MW to zero and then create'for' loop condition which

runs to the length of the species and an another tested'for'loop is created which runs to the length of

the string atomic_name.

5. Then we will create a condition using 'if' condtion and 'strcmp' command to compare the species and

atomic_name strings to calculate moleculaar mass if by comparison both string found same then we

will assign its molecular weight.

6. Now we will convert th string to numbers using'str2num' command and assign to then we run a

condition of'if' loop to find whether the species contains more than one element and if there are more

than 1 elements that is n>1 then we will increament the atomic weight by multiplication of position

n-1 to the atomic_weight array.

7. Then we will print the molecular weight using fprintf command and display it using disp.

MAIN CODE

%Input
clear all
close all
clc
%Gas constant (J/(mol-K))
R = 8.314;
%reading date file
%Open and read THERMO.dat file

f1 = fopen('THERMO.dat','r')
t1 = fgetl(f1);

%Now reading Temperature from this file
temp = fgetl(f1);

%Spliting
s = strsplit(temp);

%Converting strings cells into numbers

templow = str2num(s{2});
tempmid = str2num(s{3});
temphigh = str2num(s{4});

%Temperature Range
%Skipping Comment lines
for i = 1:3
   get = fgetl(f1);
end
  %Reading the species
   for j = 1:53
       tline = fgetl(f1);
       % Splitting cell into array
       A = strsplit(tline,' ');
       % To read the name of the species
       species = (A{1});
       % Converting strings cell into numbers
       lowtemp = str2num(A{end-3});
       hightemp = str2num(A{end-2});
       midtemp = str2num(A{end-1});
       T = linspace(lowtemp,hightemp,1000);
       
       
       %Getting next line
       
       tline1 = fgetl(f1);
       %Finding the string E
       a = strfind(tline1, 'E');
       
       %Finding first 7 higher and second & lower coefficients
       %Finding the Higher coefficients
       a1 = tline1(1:a(1)+3);
       a1 = str2num(a1);
       a2 = tline1(a(1)+4:a(2)+3);
       a2 = str2num(a2);
       a3 = tline1(a(2)+4:a(3)+3);
       a3 = str2num(a3);
       a4 = tline1(a(3)+4:a(4)+3);
       a4 = str2num(a4);
       a5 = tline1(a(4)+4:a(5)+3);
       a5 = str2num(a5);
       
       tline2 = fgetl(f1);
       b = strfind(tline2, 'E');
       a6 = tline2(1:b(1)+3);
       a6 = str2num(a6);
       a7 = tline2(b(1)+4:b(2)+3);
       a7 = str2num(a7);
       
       % Finding lower coefficients
       
       a8 = tline2(b(2)+4:b(3)+3);
       a8 = str2num(a8);
       a9 = tline2(b(3)+4:b(4)+3);
       a9 = str2num(a9);
       a10 = tline2(b(4)+4:b(5)+3);
       a10 = str2num(a10);
       
       tline3 = fgetl(f1);
       c = strfind(tline3, 'E');
       a11 = tline3(1:c(1)+3);
       a11 = str2num(a11);
       a12 = tline3(c(1)+4:c(2)+3);
       a12 = str2num(a12);
       a13 = tline3(c(2)+4:c(3)+3);
       a13 = str2num(a13);
       a14 = tline3(c(3)+4:c(4)+3)
       a14 = str2num(a14);
       
       mkdir(['E:\matlab project\NASA\fig\',species])
       % calculate specific heat
       Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);
       % calculate Entropy
       S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid);
       % calculate Enthalpy
       H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid);
       %calculate Molicular weight of the species
       MW = Molecular_weight(species);
       %writing molecular weight file
       MW_file = fopen('Molecular Weight.txt','w');
       fprintf(MW_file,'Molecular weight of %s is %d ',species,MW);
       %closing the file
         
         
         
      %Plotting Cp, H & S with temperature
        %plot 1| Cp vs T
       figure(1)
       plot(T,Cp,'linewidth',3,'color','r');
       xlabel('Temperture (K)');
       ylabel('Specific heat (KJ)');
       title(sprintf('Specific heat vs Temperature range of %s',species));
       grid on
       
       
       %plot 2| S vs T
       figure(2)
       plot(T,S,'linewidth',3,'color','g');
       xlabel('Temperature (K)');
       ylabel('Entropy (KJ/mol-K)');
       title(sprintf('Entropy vs Temperature range of %s',species));
       grid on
       
       
       %plot 3| H vs T
       figure(3)
       plot(T,H,'linewidth',3,'color','b');
       xlabel('Temperature (K)');
       ylabel('Enthalpy (KJ/mol-K)');
       title(sprintf('Enthalpy vs Temperature range of %s',species));
       grid on
       cd(['E:\matlab project\NASA\fig\',species])
       saveas(1, 'specific_heat.jpg');
       saveas(2,'Entropy.jpg');
       saveas(3,'Enthalpy.jpg');
       
       %Molecular Weight
       MW = Molecular_weight(species)
       
       
       
       
       
       
       
   end
      fclose(MW_file);
         
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       

Steps

1. Clear all,close all and clc is used to clear all previous commands and plots

2. fopen is used to open the file named 'dat'.

3.'r' is used for read mode

4. 'fgetl' is used to read the file line by line.

5 'strplit' command is used for splitting the string or charector vector at specified delimiter.

6.'str2num' command is used to convert charecter array or string to numeric array.

7.Using ,for & end'loops to skip the next 3 comments

8.'linspace'is used to generate the temperature range

9.'for'loop to read all 53 species one by one and plot them to different folder

10.'strfin' to find one string within another

11.All functions defined previously like the'Specific Heat,Entropy,Enthalpy&Molecular weight' are

one by one.

12.Calculate and display molecular weight

13.'fclose'is used to close the data file.

Results and Plots

Plot:-

Plot for O2

Plot For N2

Plot For CO2

Screenshot of the directory where different species are saved:-

Molecular Weight of all species(Text & Screenshot)

Molecular Weight of O : 15.999000

MW=

15.9990

Molecular Weight of O2 : 31.99800

Molecular Weight of O2 : 31.99800

MW=

31.9980

Molecular Weight of H : 1.008000

Molecular Weight of H : 1.008000

MW =

1.0080

Molecular Weight of H2 : 2.016000

Molecular Weight of H2 : 2.016000

MW=

2.0160

Molecular Weight of OH : 17.007000

Molecular Weight of OH : 17.007000

MW=

17.0070

Molecular Weight of H2O : 18.015000

Molecular Weight of H2O : 18.015000

MW=

18.0150

Molecular Weight of H2O2 : 34.014000

Molecular Weight of H2O2 : 34.014000

MW=

34.0140

Molecular Weight of C : 12.011000

Molecular Weight of C : 12.011000

MW=

12.0110

Molecular Weight of CH : 13.019000

Molecular Weight of CH : 13.019000

MW=

13.0190

Molecular Weight of CH2 : 14.027000

Molecular Weight of CH2 : 14.027000

MW=

14.0270

Molecular Weight of CH2(S) : 14.027000

Molecular Weight of CH2(S) : 14.027000

MW=

14.0270

Molecular Weight of CH3 : 15.035000

Molecular Weight of CH3 : 15.035000

MW=

15.0350

Molecular Weight of CH4 : 16.043000

Molecular Weight of CH4 : 16.043000

MW =

16.0430

Molecular Weight of CO : 28.010000

Molecular Weight of CO : 28.010000

MW=

28.0100

Molecular Weight of CO2 : 44.009000

Molecular Weight of CO2 : 44.009000

MW=

44.0090

Molecular Weight of HCO : 29.018000

Molecular Weight of HCO : 29.018000

MW=

29.0180

Molecular Weight of CH2O : 30.026000

Molecular Weight of CH2O : 30.026000

MW=

30.0260

Molecular Weight of CH3O : 31.034000

Molecular Weight of CH3O : 31.034000

MW=

31.0340

Molecular Weight of CH3OH : 32.042000

Molecular Weight of CH3OH : 32.042000

MW=

32.0420

Molecular Weight of C2H : 25.030000

Molecular Weight of C2H : 25.030000

MW=

25.0300

Molecular Weight of C2H2 : 26.038000

Molecular Weight of C2H2 : 26.038000

MW=

26.0380

Molecular Weight of C2H3 : 27.046000

Molecular Weight of C2H3 : 27.046000

MW=

27.0460

Molecular Weight of C2H4 : 28.054000

Molecular Weight of C2H4 : 28.054000

MW=

28.0540

Molecular Weight of C2H5 : 29.062000

Molecular Weight of C2H5 : 29.062000

MW=

29.0620

Molecular Weight of C2H6 : 30.070000

Molecular Weight of C2H6 : 30.070000

MW=

30.0700

Molecular Weight of CH2CO : 42.037000

Molecular Weight of CH2CO : 42.037000

MW=

42.0370

Molecular Weight of HCCO : 41.029000

Molecular Weight of HCCO : 41.029000

MW=

Molecular Weight of HCCOH : 42.037000

Molecular Weight of HCCOH : 42.037000

MW=

42.0370

Molecular Weight of H2CN : 28.034000

Molecular Weight of H2CN : 28.034000

MW=

28.0340

Molecular Weight of HCN : 27.026000

Molecular Weight of HCN : 27.026000

MW=

27.0260

Molecular Weight of HNO : 31.014000

Molecular Weight of HNO : 31.014000

MW=

31.0140

Molecular Weight of N : 14.007000

Molecular Weight of N : 14.007000

MW=

14.0070

Molecular Weight of NNH : 29.022000

Molecular Weight of NNH : 29.022000

MW=

29.0220

Molecular Weight of N2O : 44.013000

Molecular Weight of N2O : 44.013000

MW=

44.0130

Molecular Weight of NH : 15.015000

Molecular Weight of NH : 15.015000

MW=

15.0150

Molecular Weight of NH2 : 16.023000

Molecular Weight of NH2 : 16.023000

MW=

16.0230

Molecular Weight of NH3 : 17.031000

Molecular Weight of NH3 : 17.031000

MW=

17.0310

Molecular Weight of NO : 30.006000

Molecular Weight of NO : 30.006000

MW=

30.0060

Molecular Weight of NO2 : 46.005000

Molecular Weight of NO2 : 46.005000

MW=

46.0050

Molecular Weight of HCNO : 43.025000

Molecular Weight of HCNO : 43.025000

MW=

43.0250

Molecular Weight of HOCN : 43.025000

Molecular Weight of HOCN : 43.025000

MW=

43.0250

Molecular Weight of HNCO : 43.025000

Molecular Weight of HNCO : 43.025000

MW=

Molecular Weight of NCO : 42.017000

Molecular Weight of NCO : 42.017000

MW=

42.0170

Molecular Weight of CN : 26.018000

Molecular Weight of CN : 26.018000

MW=

26.0180

Molecular Weight of HCNN : 41.033000

Molecular Weight of HCNN : 41.033000

MW=

41.0330

Molecular Weight of N2 : 28.014000

Molecular Weight of N2 : 28.014000

MW=

28.0140

Molecular Weight of AR : 39.948000

Molecular Weight of AR : 39.948000

MW=

39.9480

Molecular Weight of C3H8 : 44.097000

Molecular Weight of C3H8 : 44.097000

MW=

44.0970

Molecular Weight of C3H7 : 43.089000

Molecular Weight of C3H7 : 43.089000

MW=

43.0890

Molecular Weight of CH3CHO : 44.053000

Molecular Weight of CH3CHO : 44.053000

MW=

44.0530

Molecular Weight of CH2CHO : 43.045000

Molecular Weight of CH2CHO : 43.045000

MW=

43.0450

Conclution

Using Octave code,the necessary coefficient required to evaluate various thermodynamic parameters for

different species are extracted from NASA thermodynamic data file.The specific heat,entropy&enthalpy for

all the species are calculated and plotted aganist the local temperature range.The molecular weight is

calculated for all the species.

Googledrive link

https://drive.google.com/drive/folders/1lXrqo8ZWE_mF38WWy4TR2w7KfO-oX1YV?usp=sharing

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.