Challenge

Parsing NASA Thermodynamic data

Aim:

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

Objective:

  1. To write a function that extracts the 14 co-efficients and calculates the specific heat, enthalpy and entropy for all the species in the data file.
  2. To calculate the molecular weight of each species and display it in the command window.
  3. To plot Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
  4. To save the plots as images with appropriate names and dump them in separate folders for each species with suitable name and all these should be done automatically.
  5. The main program will just take the 'THERMO.dat' file as input and generate Cp, H and S plots.

Theory:

File Parsing:

It is the method of splitting the file or other input into a piece of data that can be easily stored or manipulated. By doing this we are going to split a string into parts then recognizing the parts to convert it into something simpler than a string.

Here in this project we are going to split the NASA thermodynamic data which is having 53 gas species values in 213 lines. To perform the calculation for thermodynamic properties, we need some sort of co-efficient values and temperature ranges which is available in the NASA file. So, we are going to parse the values which are all needed for our calculation from that file.

Specific Heat:

Specific heat is the amount of heat energy required to raise the temperature of body per unit of mass. Specific heat is also known as specific heat capacity or mass specific heat. In SI units, specific heat is the amount of heat in joules required to raise 1 gram of a substance 1 Kelvin.

Enthalpy:

When a substance changes at constant pressure, enthalpy tells how much heat and work was added or removed from the substance. Enthalpy is similar to energy but not the same. When a substance grows or shrinks, energy is used up or released.

Entropy:

The entropy of an object is a measure of the amount of energy which is unavailable to do work. Entropy is also a measure of the number of possible arrangements of atoms a system can have. In this sense, entropy is the measure of uncertainty or randomness.

Gas Constant:

The gas constant is a physical constant denoted by R and is expressed in terms of units of energy per temperature increment per mole. The gas constant value is equivalent to Boltzmann constant but expressed as the pressure-volume product instead of nergy per increment of temperature per particle.

Molecular Weight:

Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations.

Governing Equations:

Sample NASA data:

`(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,

Cp = Specific Heat (J/K kg)

H = Enthalpy (J)

S = Entropy (J/K)

R = Universal gas constant = 8.314 (J/mol.K)

T = Temperature (K)

Code:

clear all
close all
clc

f1 = fopen('thermo.dat','r');   % Opening the file for reading
fgetl(f1);                      % Reading the comment lines
fgetl(f1);                      
fgetl(f1);                      
fgetl(f1);
fgetl(f1);

% Extracting all species data

for i = 1:53
    
% Extracting name of gas species

A = fgetl(f1);                  % Reading the sixth line
z = findstr(A,' ');             % Reading the species name
species_name = A(1:z(1)-1);     % Finding out the species name

% Extracting local temperatures of gas species

a = findstr(A,'.');             % Finding string from the line
t1 = A((a(1)-3):a(1)+3);        % Assigning 't' variable for the local temperatures
t1 = str2num(t1);               % Converting string to number
t2 = A(a(3)-4:a(3)+3);
t2 = str2num(t2);
t3 = A(a(2)-4:a(2)+3);
t3 = str2num(t3);

% t1 = Low Temperature
% t2 = Mid Temperature
% t3 = High Temperature

% Extracting co-efficient of gas species

B = fgetl(f1);                  
b = findstr(B,'E');             
a1 = B(1:b(1)+3);               % Assigning 'a' variable for the temperature co-efficients
a1 = str2num(a1);
a2 = B(b(1)+4:b(2)+3);
a2 = str2num(a2);
a3 = B(b(2)+4:b(3)+3);
a3 = str2num(a3);
a4 = B(b(3)+4:b(4)+3);
a4 = str2num(a4);
a5 = B(b(4)+4:b(5)+3);
a5 = str2num(a5);
C = fgetl(f1);
c = findstr(C,'E');
a6 = C(1:c(1)+3);
a6 = str2num(a6);
a7= C(c(1)+4:c(2)+3);
a7 = str2num(a7);
a8 = C(c(2)+4:c(3)+3);
a8 = str2num(a8);
a9 = C(c(3)+4:c(4)+3);
a9 = str2num(a9);
a10 = C(c(4)+4:c(5)+3);
a10 = str2num(a10);
D = fgetl(f1);
d = findstr(D,'E');
a11 = D(1:d(1)+3);
a11 = str2num(a11);
a12 = D(d(1)+4:d(2)+3);
a12 = str2num(a12);
a13 = D(d(2)+4:d(3)+3);
a13 = str2num(a13);
a14 = D(d(3)+4:d(4)+3);
a14 = str2num(a14);

% First 7 are high temperature co-efficients from a1 to a7
% Second 7 are low temperature co-efficients from a8 to a14

% Creating the range of low and high values of local temperatures

T = linspace(t1,t3,1000);

R = 8.314;                  % Universal gas constant in j/mol.k

% Calculation for Specific heat, Enthalpy and Entropy

for i = 1:length(T)
    
    if T(i)>t2
        Cp(i) = (a1 + a2.*T(i) + a3.*T(i).^2 + a4.*T(i).^3 + a5.*T(i).^4)*R;
        H(i) = (a1 + (a2.*T(i))/2 + (a3.*T(i)^2)/3 + (a4.*T(i).^3)/4 + (a5.*T(i).^4)/5 + (a6./T(i)))*R.*T(i); 
        S(i) = a1.*log(T(i)) + a2.*T(i) + (a3.*T(i)^2)/2 + (a4.*T(i)^3)/3 + (a5.*T(i).^4)/4 + a7;
    else
        Cp(i) = (a8 + a9.*T(i) + a10.*T(i).^2 + a11.*T(i).^3 + a12*T(i).^4)*R;
        H(i) = (a8 + (a9.*T(i))/2 + (a10.*T(i).^2)/3 + (a11.*T(i).^3)/4 + (a12.*T(i).^4)/5 + (a13./T(i)))*R.*T(i); 
        S(i) = a8.*log(T(i)) + a9.*T(i) + (a10.*T(i)^2)/2 + (a11.*T(i)^3)/3 + (a12.*T(i).^4)/4 + a14;
    end
end

% Molecular weight calculation

elements = ['H','C','O','N','A','S'];                           % Declaring species elements
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948 32.065];  % Declaring the atomic weight value
mol_mass = 0;                                                   % Declaring molecular weight to be '0' in initial stage 

for l = 1:length(species_name)
    for m = 1:length(atomic_weight)
        if strcmp(species_name(l), elements(m))
            mol_mass = mol_mass + atomic_weight(m);
            position = m;
        end
    end
    n = str2double(species_name(l));
    if n>1
        mol_mass = mol_mass + atomic_weight(position)*(n-1);
    end
end

% Printing the molecular weight of the gas species in the command window

sprintf('Molecular Weight of %s is %f u',species_name,mol_mass) 

% Creating a folder to save the plots

mkdir(species_name)         % Creating a new folder with the species name in the main folder
cd(sprintf(species_name))   % Changing the directory to the folder of the species name

% Plotting specific heat vs temperature

figure(1)
plot (T,Cp, 'color','b','linewidth',2)
xlabel('Temperature (K)')
ylabel('Specific Heat (J/K Kg)')
title({'Specific Heat vs Temperature',['Species name = ',species_name,' & Molecular weight = ',num2str(mol_mass)]});
saveas(figure(1),'Cp vs Temp.png')

% Plotting enthalpy vs temperature

figure(2)
plot (T,H, 'color', 'r','linewidth',2)
xlabel('Temperature (K)')
ylabel('Enthalpy (J/K)')
title({'Enthalpy vs Temperature',['Species name = ',species_name,' & Molecular weight = ',num2str(mol_mass)]});
saveas(figure(2),'Enthalpy vs Temp.png')

% Plotting entropy vs temperature

figure(3)
plot (T,S, 'color', 'g','linewidth',2)
xlabel('Temperature (K)')
ylabel('Entropy (J/K)')
title({'Entropy vs Temperature',['Species name = ',species_name,' & Molecular weight = ',num2str(mol_mass)]});
saveas(figure(3),'Entropy vs Temp.png')

% To change the current directory to the main folder

oldfolder = cd('I:Chrome DownloadsSkill LyncMATLABFile Parsing Challenge');

end

Explanation of Code:

  1. The program begins with clear all, close all and clc commands to free up the command window and the workspace.
  2. We will then read the data from the external NASA thermodynamic data file using fopen command which is used to open the file and fgetl command which is used to read the content in the file line by line.
  3. After we use the fopen command we will be assigning to the variable called f1. Thus, the fgetl(f1) is the command acts like a line cursor, which will retrieve the next time while running this command for the second time and so on.
  4. Now, we will be skipping the first 5 lines to enter the pattern of values of the respective gas species. We will be using the for loop command to process the same for all the gas species.
  5. The name of the gas species we will be found out using findstr command and for finding out the species we are going to locate the string ‘ ’ which is present in after each species name.
  6. And then we are going to use A(1:z(1)-1) which tells us the exact location of the species name, using which the name of the gas species is stored in the variable species_name.
  7. The local temperatures for the respective gas species is found similarly using findstr command and we are going to locate the string ‘.’ using which we will find out the values of low temperature, mid temperature and high temperature which is assigned to the variables t1, t2 and t3 respectively. But the values we have achieved are string values so we have to change these string values to numeric values using the str2num command.
  8. Now, we are going to extract the co-efficient for the gas species in which the co-efficient locations are like in the 2nd row, next 5 in the 3rd row and the rest in the 4th row.
  9. We are going to extract these values similarly like we did for finding out the local temperatures by using the findstr and str2num commands and store it in the variables a1 to a14. For extracting we are going to locate the string ‘E’ which is present in the each co-efficient value and so it will be followed for all the co-efficients values in the same line and then we can proceed for the next lines.
  10. The values from a1 to a7 represent the high temperature co-efficients and the values from a8 to a14 represent the low temperature co-efficients.
  11. Temperature range values from low temperature to high temperature is assigned using the linspace command.
  12. The for loop command is used to calculate the values of specific heat, enthalpy and entropy and the if-else command is also used to find out the required values if the temperature below the mid temperature or above the mid temperature.
  13. In order to calculate the molecular weight of the species we declare the species elements in the form of cell array. Their atomic mass is declared in the form of array. Then initially molecular weight is declared as zero.
  14. Now, for loop command is used to detect if there are more than one species in the variable species_name and the respective values of the gas species values are calculated and found out.
  15. After the nested for loop the species_name which is in the form of string is converted as number with strdouble command. Then the species_name which is now as number is greater than 1, molecular weight is again calculated by having the atomic mass for the current species * n-1
  16. The value of the molecular weight is calculated for the respective gas species and is displayed in the command window.
  17. Now, we are going to create plots for the obtain specific heat, enthalpy and entropy values and save it in respective species_name folder automatically.
  18. In order to create a new folder with the species_name we are going to use the mkdir command so that a new folder is created in the main folder. Now, we have to change the current directory from the main folder to the species_name folder so the cd command is used to do the same.
  19. The plots for specific heat vs temperature, enthalpy vs temperature and entropy vs temperature is plotted and the labelling of axis and the naming of the title is also done along with the value of molecular weight.
  20. To save the plots in the species_name folder, saveas command is used to save the plots in the current folder. After which to change the current directory to the main folder the oldfolder = cd () command is used.

Output:

O2 Plot:

N2 Plot:

CO2 Plot:

Folder containing the plots:

Folder containing all the species names:

Molecular weight for all the species displayed in the command window:

Conclusion:

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.

References:

  1. https://en.wikipedia.org/wiki/Parsing
  2. https://in.mathworks.com/help/matlab/ref/mkdir.html
  3. https://in.mathworks.com/help/matlab/ref/cd.html
  4. https://in.mathworks.com/help/matlab/ref/saveas.html
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.