Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: - To write a matlab program to parse the NASA thermodynamic data file and calculate the thermodynamic properties of various gas species.

Objective: -

  • To extract the 14 coefficients and calculate the specific heat, entropy, enthalpy for all the species in the data file.
  • To calculate the molecular weight of each species.
  • To plot specific heat, entropy, enthalpy for the local temperature range.
  • To save all the images with appropriate names and to save in a specific folder for each species.
  • To screenshot the window where all the folders are saved.

Theory: -

  • What is File Parsing?
    • Parsing a file means reading in a data stream and building a memory model of the content of that data. This aims at facilitating performing some kind of transformation on the data. Parsing splits a file or other input into pieces of data that can be easily stored or manipulated.
  • What is parsing used for?
    • To parse is to break up a sentence or group of words into separate components, including the definition of each part's function or form. The technical definition implies the same concept. Parsing is used in all high-level programming languages.
  • NASA Thermodynamic file:-
    • In NASA Thermodynamic file which is THERMO.dat file in which there are 53 species and temperature range are given. Each species contains 14 coefficients. We have parsed 14 co-efficient to find Specific Heat, Entropy, and Enthalpy.

Where,

  • R = Gas Constant
  • T = Temperature
  • Cp = Specific Heat(JK)
  • H = Enthalpy(J/Kg)
  • S = Entropy(J/K)

Given Formula.

  • `(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*logT + a2*T +a3*T^2/2+a4*T^3/3 +a5*T^4/4+a7`

Main Code:

clear all 
close all
clc

f1 = fopen("THERMO.dat",'r');
file = fgetl(f1);
local_temp = fgetl(f1);

% Gas constant
R = 8.314;

% Converting string cell into numbers
local_temp_range = strsplit(local_temp,' ');
local_low_temp = str2double(local_temp_range{2});
local_mid_temp = str2double(local_temp_range{3});
local_high_temp = str2double(local_temp_range{4});

% Temperature range
T = linspace(local_low_temp,local_high_temp,1000);

% For skipping the lines
for i = 1:3
    a = fgetl(f1);
end

for k = 1:53
    line1 = fgetl(f1);
    A = strsplit(line1);
    species_name = A{1};

    line_1 = fgetl(f1);
    b = strfind(line_1,'E');

    % First 7 high temperature coefficient and converting string to number
    a1 = line_1(1:b+3);
    a1 = str2double(a1);
    a2 = line_1(b(1)+4:b(2)+3);
    a2 = str2double(a2);
    a3 = line_1(b(2)+4:b(3)+3);
    a3 = str2double(a3);
    a4 = line_1(b(3)+4:b(4)+3);
    a4 = str2double(a4);
    a5 = line_1(b(4)+4:b(5)+3);
    a5 = str2double(a5);

    % Line 2
    line_2 = fgetl(f1);
    c = strfind(line_2,'E');
    a6 = line_2(1:c+3);
    a6 = str2double(a6);
    a7 = line_2(c(1)+4:c(2)+3);
    a7 = str2double(a7);

    % 7 lower order temperature coefficient 
    a8 = line_2(c(2)+4:c(3)+3);
    a8 = str2double(a8);
    a9 = line_2(c(3)+4:c(4)+3);
    a9 = str2double(a9);
    a10 = line_2(c(4)+4:c(5)+3);
    a10 = str2double(a10);

    % Line 3
    line_3 = fgetl(f1);
    d = strfind(line_3,'E');
    a11 = line_3(1:d+3);
    a12 = line_3(d(1)+4:d(2)+3);
    a12 = str2double(a12);
    a13 = line_3(d(2)+4:d(3)+3);
    a13 = str2double(a13);
    a14 = line_3(d(3)+4:d(4)+3);
    a14 = str2double(a14);

% Calculation of specific heat
Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,local_mid_temp);

% Calculation of Enthalpy
 H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,local_mid_temp);

 % Calculation of Entropy
S=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,local_mid_temp);

% molecular weight
W = molecular_weight(species_name
k = sprintf('%s mol_wt = %f', species_name,W);
disp(['molecular_weight of:    ', species_name, W]);
disp(W)

% creating a directory
cd('D:\pracites digram\plots')
mkdir(species_name)
cd(species_name)

% plotting for temperature vs specific heat
fig = figure(1);
plot(T,Cp,'LineWidth',3, 'Color','g')
xlabel('Temperature[K]')
ylabel('Specific Heat')
grid on
title('Temperature Vs Specific Heat')
savefig('CpvsT')

% plotting for temperature vs enthalpy
fig = figure(2);
plot(T,H,'LineWidth',3, 'Color','b')
xlabel('Temperature[K]')
ylabel('Enthalpy')
grid on
title('Temperature Vs Specific Heat')
savefig('HvsT')

% plotting for temperature vs entropy
fig = figure(3);
plot(T,S,'LineWidth',3, 'Color','r')
xlabel('Temperature[K]')
ylabel('Entropy')
grid on
title('Temperature Vs Specific Heat')
savefig('SvsT')

end

Function code:

Specific Heat: Specific heat, 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. For example, the specific heat of water is 1 calorie (or 4.186 joules) per gram per Celsius degree.

  • For high temperature coefficient formula used
    `Cp = R*(a1+a2*T + a3* T^2 +a4 *T^3+a5 *T^4)`
  • For lower order temperature coefficient formula used
    `Cp = R*(a8+a9*T+a10*T^2+a11*T^3+a12*T^4)`
function [Cp] = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,local_mid_temp)

% Calculation of specific heat for higher and lower temperature 
if (T <= local_mid_temp)
        Cp = R*(a8+(a9*(T))+(a10*(T).^2)+(a11*(T).^3)+(a12*(T).^4));
else
        Cp = R*(a1+(a2*(T))+(a3*(T).^2)+(a4*(T).^3)+(a5*(T).^4));
end
end

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.

  • For high temperature coefficient formula used
    `H = R*(a1*T+a2*(T^2)/2+a3*(T^3)/3+a4*(T^4)/4+a5*(T^5)/5+a6)`
  • For lower order temperature coefficient formula used
    `H = R*(a8*T+a9*(T^2)/2+a10*(T^3)/3+a11*(T^4)/4+a12*(T^5)/5+a13)`
function [H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,local_mid_temp)

% Calculation of enthalpy for higher and lower temperature 
if(T<=local_mid_temp)
     H = R*(a8*T+((a9*(T.^2))/2)+((a10*(T.^3))/3)+((a11*(T.^4))/4)+((a12*(T.^5))/5)+a13);
else
    H = R*(a1*T+((a2*(T.^2))/2)+((a3*(T.^3))/3)+((a4*(T.^4))/4)+((a5*(T.^5))/5)+a6);
end
end

Entropy: Entropy, the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.

  • For high temperature coefficient formula used
    `S = R*(a1*logT+a2*T+a3*T^2/2+a4*T^3/3+a5*T^4/4+a7)`
  • For lower order temperature coefficient formula used
    `S = R*(a8*logT+a9*T+a10*T^2/2+a11*T^3/3+a12*T^4/4+a14)`
function [S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,local_mid_temp)

% Calculation of entropy for higher and lower temperature 
if (T>= local_mid_temp)
        S = R*(a8*log(T)+(a9*T)+((a10*(T).^2)/2)+((a11*(T).^3)/3)+((a12*(T).^4)/4)+a14);
else
        S = R*(a1*log(T)+(a2*T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(T).^4)/4)+a7);
end 
end
Molecular Weight: Molecular weight, also called molecular mass, mass of a molecule of a substance, based on 12 as the atomic weight of carbon-12. It is calculated in practice by summing the atomic weights of the atoms making up the substance's molecular formula.
function W = molecular_weight(species)

% Defining Atomic name of the species
atomic_name = ['H', 'C', 'O', 'N', 'Ar'];

% Defining Atomic weight of the species
atomic_weight = [1.08 12.01 15.99 14.07 39.94];

% Molecular weight starting range 
W = 0;

for i = 1:length(species)
    for j = 1:length(atomic_name)
        if strcmp(species(i), atomic_name(j))
            W = W + atomic_weight(j);
            position = j;
        end
    end
    n = str2double(species);
    if n>1
        W = W + atom(postion)*(n-1);
    end
end

Explanation of code:

  • Firstly, we start the program by opening the THERMO.dat file by fopen command and get the first line of file by using an fgetl command then input the value of Gas constant which is 8.314.
  • Now then we will find out the temperature range using linspace command and skip the first three lines of the file by running a for loop.
  • In order to find the 53 elements in the file, we will run the for loop then split then find the number of the temperature in the series line.
  • Now we read the next line and in order to read the coefficient data in the file we need to find the ‘E’ string by using strfind command to find the coefficient number from the string.
  • Then we will find the first seven coefficients for higher temperature and seven coefficients for lower temperature.
  • Afterwards we will calculate the specific heat, enthalpy and entropy for 14 coefficients by creating a function in a different script.
  • After finding the specific heat, enthalpy and entropy now we plot using plot command for temperature vs entropy.
  • Now we will calculate the molecular weight and use command spritntf which formats data into string or character vector for all the molecular weight of the species.
  • We change the directory using cd command and then create a directory folder when we have to save our file using mkdir command and then we will change the directory to this created folder.
  • Then we will save the plot using savefig command of different species.

Syntax used while coding

  • clear all: clear all the variables in the workspace.
  • close all: close all the plot and figure.
  • clc: clears the command window.
  • fopen: open a file or obtain information about an open file.
  • ‘r’: open a file for reading.
  • linspace: create linearly spaced vector
  • fprint: write format data or file.
  • mkdir: make a new directory.
  • cd: change working directory
  • fgetl: is used to read lines from a file.
  • strsplit: is used to split string or character vector at specific delimiter.
  • str2double: convert character string or array to numeric array.
  • strfind: find one string within another.

Output of command window:

k =

    'O mol_wt = 15.990000'

molecular_weight of:    O
   15.9900


k =

    'O2 mol_wt = 15.990000'

molecular_weight of:    O2
   15.9900


k =

    'H mol_wt = 1.080000'

molecular_weight of:    H
    1.0800


k =

    'H2 mol_wt = 1.080000'

molecular_weight of:    H2
    1.0800


k =

    'OH mol_wt = 17.070000'

molecular_weight of:    OH
   17.0700


k =

    'H2O mol_wt = 17.070000'

molecular_weight of:    H2O
   17.0700


k =

    'HO2 mol_wt = 17.070000'

molecular_weight of:    HO2
   17.0700


k =

    'H2O2 mol_wt = 17.070000'

molecular_weight of:    H2O2
   17.0700


k =

    'C mol_wt = 12.010000'

molecular_weight of:    C
   12.0100


k =

    'CH mol_wt = 13.090000'

molecular_weight of:    CH

   13.0900


k =

    'CH2 mol_wt = 13.090000'

molecular_weight of:    CH2

   13.0900


k =

    'CH2(S) mol_wt = 13.090000'

molecular_weight of:    CH2(S)

   13.0900


k =

    'CH3 mol_wt = 13.090000'

molecular_weight of:    CH3

   13.0900


k =

    'CH4 mol_wt = 13.090000'

molecular_weight of:    CH4

   13.0900


k =

    'CO mol_wt = 28.000000'

molecular_weight of:    CO
    28


k =

    'CO2 mol_wt = 28.000000'

molecular_weight of:    CO2
    28


k =

    'HCO mol_wt = 29.080000'

molecular_weight of:    HCO
   29.0800


k =

    'CH2O mol_wt = 29.080000'

molecular_weight of:    CH2O
   29.0800


k =

    'CH2OH mol_wt = 30.160000'

molecular_weight of:    CH2OH
   30.1600


k =

    'CH3O mol_wt = 29.080000'

molecular_weight of:    CH3O
   29.0800


k =

    'CH3OH mol_wt = 30.160000'

molecular_weight of:    CH3OH
   30.1600


k =

    'C2H mol_wt = 13.090000'

molecular_weight of:    C2H

   13.0900


k =

    'C2H2 mol_wt = 13.090000'

molecular_weight of:    C2H2

   13.0900


k =

    'C2H3 mol_wt = 13.090000'

molecular_weight of:    C2H3

   13.0900


k =

    'C2H4 mol_wt = 13.090000'

molecular_weight of:    C2H4

   13.0900


k =

    'C2H5 mol_wt = 13.090000'

molecular_weight of:    C2H5

   13.0900


k =

    'C2H6 mol_wt = 13.090000'

molecular_weight of:    C2H6

   13.0900


k =

    'CH2CO mol_wt = 41.090000'

molecular_weight of:    CH2CO)
   41.0900


k =

    'HCCO mol_wt = 41.090000'

molecular_weight of:    HCCO)
   41.0900


k =

    'HCCOH mol_wt = 42.170000'

molecular_weight of:    HCCOH*
   42.1700


k =

    'H2CN mol_wt = 27.160000'

molecular_weight of:    H2CN
   27.1600


k =

    'HCN mol_wt = 27.160000'

molecular_weight of:    HCN
   27.1600


k =

    'HNO mol_wt = 31.140000'

molecular_weight of:    HNO
   31.1400


k =

    'N mol_wt = 14.070000'

molecular_weight of:    N
   14.0700


k =

    'NNH mol_wt = 29.220000'

molecular_weight of:    NNH
   29.2200


k =

    'N2O mol_wt = 30.060000'

molecular_weight of:    N2O
   30.0600


k =

    'NH mol_wt = 15.150000'

molecular_weight of:    NH
   15.1500


k =

    'NH2 mol_wt = 15.150000'

molecular_weight of:    NH2
   15.1500


k =

    'NH3 mol_wt = 15.150000'

molecular_weight of:    NH3
   15.1500


k =

    'NO mol_wt = 30.060000'

molecular_weight of:    NO
   30.0600


k =

    'NO2 mol_wt = 30.060000'

molecular_weight of:    NO2
   30.0600


k =

    'HCNO mol_wt = 43.150000'

molecular_weight of:    HCNO+
   43.1500


k =

    'HOCN mol_wt = 43.150000'

molecular_weight of:    HOCN+
   43.1500


k =

    'HNCO mol_wt = 43.150000'

molecular_weight of:    HNCO+
   43.1500


k =

    'NCO mol_wt = 42.070000'

molecular_weight of:    NCO*
   42.0700


k =

    'CN mol_wt = 26.080000'

molecular_weight of:    CN
   26.0800


k =

    'HCNN mol_wt = 41.230000'

molecular_weight of:    HCNN)
   41.2300


k =

    'N2 mol_wt = 14.070000'

molecular_weight of:    N2
   14.0700


k =

    'AR mol_wt = 39.940000'

molecular_weight of:    AR'
   39.9400


k =

    'C3H8 mol_wt = 13.090000'

molecular_weight of:    C3H8

   13.0900


k =

    'C3H7 mol_wt = 13.090000'

molecular_weight of:    C3H7

   13.0900


k =

    'CH3CHO mol_wt = 42.170000'

molecular_weight of:    CH3CHO*
   42.1700


k =

    'CH2CHO mol_wt = 42.170000'

molecular_weight of:    CH2CHO*
   42.1700
 

Result:

  • Screenshot of all species folder

  1. Figure of Temperature vs Specific heat

  2. Figure of Temperature vs Entropy

  3. Figure of Temperature vs Entropy

Conclusion:

  • We have calculated the various coefficients required to evaluate various thermodynamic properties of the different species and are extracted from NASA Thermodynamic file using a matlab program.
  • The specific heat, Enthalpy and entropy are calculated for all the species and plots are plotted against the temperature range and molecular weight for all the species is calculated.
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.