Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing NASA thermodynamic data file using MATLAB

Aim:

The main aim is as follows:

  1. Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file.
  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 temp.: High temp.) specific for each species.
  4. Save the plot as images with appropriate names and dump them in separate folders for each species with a suitable name. All these should be generated automatically.

FILE PARSING:

To parse data and information means to break it down into parts so that its syntax can be analyzed, categorized, and understood. The actual definition of ‘parsing’ is to spit a file or other input into pieces of data that can be easily stored or manipulated. So, in file parsing, we split a string into parts and then recognize the parts to convert it into something simpler than a string. File parsing is used to read as well as to write data in a file. File parsing can be used to create text or data files or even websites.

NASA’s Thermodynamic data file contains coefficients for polynomials that are used to calculate thermodynamic properties for various chemical species. We are going to use file parsing to read the data given in “NASA’s Thermodynamic data file”(Link) to extract these coefficients and perform calculations in MATLAB.

The formulas to calculate Specific Heat(Cp), Enthalpy and Entropy are given,

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

`H/(RT)=a1+(a2)T/2+(a3)(T^2)/3+(a4)(T^3)/4+(a5)(T^4)/5+(a6)/T`

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

The first 7 coefficients are High-temperature coefficients and the second 7 coefficients are Low-temperature coefficients.

Where R = Universal Gas Constant;

T = Temperature;

Main MATLAB program to parse NASA's Thermodynamic data

Main MATLAB program to parse NASA's Thermodynamic data
clear all
close all
clc
% Reading the NASA Thermodynamic data file using 'fopen' function
f1= fopen('THERMO.dat','r');
% fgetl is used to read the data file line by line.
% First 5 lines are generally considered as comments.also global temperature data we are not using for calculation.
for m= 1:5
    comments_line= fgetl(f1);
end

% Defining the Universal Gas Constant
R= 8.3145;     %in j/mol-k
Reading all species individually
for j=1:53 
tline=fgetl(f1);
A=strsplit(tline,' ');
% Now read the information and access them in useable format
species_name = A{1}
% Reading local Temperature for each species from string A
local_low_temp=str2num(A{end-3});
local_mid_temp=str2num(A{end-1});
local_high_temp=str2num(A{end-2});

% Reading Coefficients from upcomming lines
tline=fgetl(f1);
a=findstr(tline,'E');
a1=str2num(tline(1:a(1)+3));
a2=str2num(tline(a(1)+4:a(2)+3));
a3=str2num(tline(a(2)+4:a(3)+3));
a4=str2num(tline(a(3)+4:a(4)+3));
a5=str2num(tline(a(4)+4:a(5)+3));


tline=fgetl(f1);
a=findstr(tline,'E');
a6=str2num(tline(1:a(1)+3));
a7=str2num(tline(a(1)+4:a(2)+3));
a8=str2num(tline(a(2)+4:a(3)+3));
a9=str2num(tline(a(3)+4:a(4)+3));
a10=str2num(tline(a(4)+4:a(5)+3));


tline=fgetl(f1);
a=findstr(tline,'E');
a11=str2num(tline(1:a(1)+3));
a12=str2num(tline(a(1)+4:a(2)+3));
a13=str2num(tline(a(2)+4:a(3)+3));
a14=str2num(tline(a(3)+4:a(4)+3));



% Defining Temp. range for calculation
T=linspace(local_low_temp,local_high_temp,1000);

% Calculate Specific Heat,Enthaply, Entropy for temp range.
Cp= specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R,T,local_mid_temp);
H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R,T,local_mid_temp);
S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,local_mid_temp);
M= molecular_weight(species_name);

% Creating separate directory to store graphs and values of each species
 cd('D:\Matlab\MATLAB Practice\File Parsing')
 mkdir(species_name);
 cd(species_name);

% % Writing Molecular Weight to a text file
M_file=fopen('Molecular_weight.txt','w');
fprintf(M_file,'Molecular Weight of %s is : %f',species_name,M);
fclose(M_file);

% plotting Temperature vs Specific heat graph and saving 
figure(1)
plot(T,Cp,'LineWidth',1.5,'Color','r');
xlabel('Temperature (K)')
ylabel('Specific Heat (KJ)')
title(sprintf('Temperature Vs Specific Heat of %s ',species_name))
grid on 
saveas(figure(1),'Specific Heat','jpg')

% plotting Temperature vs Enthalpy graph and saving 
figure(2)
plot(T,H,'LineWidth',1.5,'Color','b');
xlabel('Temperature (K)')
ylabel('Enthalpy (KJ/mol)')
title(sprintf('Temperature Vs Enthalpy of %s ',species_name))
grid on 
saveas(figure(2),'Enthalpy','jpg')

% plotting Temperature vs Entropy graph and saving 
figure(3)
plot(T,S,'LineWidth',1.5,'Color','g');
xlabel('Temperature (K)')
ylabel('Entropy (KJ/mol-k)')
title(sprintf('Temperature Vs Entropy of %s ',species_name))
grid on 
saveas(figure(3),'Entropy','jpg')


% Path to original Directory
cd('D:\Matlab\MATLAB Practice\File Parsing');
end

Explanation of Code

  • The code starts with clear all, close all and clc

Where ‘clear all’ command is used to clear the data stored in the workspace.

‘close all’ command is used to close all plots and figure

‘clc’ command is used to clear the command window.

  • Then to open the ‘THERMO.dat’ data file, fopen command is used and reading permission is provided.
  • Then to read the data in the file ,fgetl command is used and first line and globle temperature is read.
  • The strsplit command is used to split the string at a specified delimiter which in the case is the spacing between the numbers.
  • The str2num command is used to convert the string of values of temperature into actual numbers and the global temperature values are read.
  • To skip the comments in the file, the ‘for loop’ is used and values of Universal Gas Constant is defined.
  • To read the data for all 53 species mentioned in the data file and to calculate the thermodynamic properties of each species ,’for loop’ is used.
  • In the ‘for loop’ , the first line of each species is read using fgetl command and the string is converted into array by strsplit command
  • The name of the species is stored in the ‘species_name’ variable and the local temp. values are extracted using str2num
  • To read the values of the coefficient from the proceeding three lines fgetl command is used along findstr command to find the common term in the string. Here ‘E’ is given as input to find the common in the string.
  • The common term is found in order to separate the values in the string and str2num is used to extract those values.
  • In the way all the 14 coefficients are extracted and stored in a variable ’ where n=1,2,3,…14.
  • The local temp. range for calculation is defined for 1000 values between lower value and higher value using linspace
  • To calculate specific heat , enthalpy , entropy ,and molecular weight , separate function is written.

Function to calculate Specific Heat :

Function to calculate Specific Heat
% Defining the function for Specific Heat based on the values of the temp.

function Cp= specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R,T,local_mid_temp)
if T>= local_mid_temp
     Cp= R*(a1+  (a2*T) + (a3*(T).^2) + (a4*(T).^3) + (a5*(T).^4));
else 
     Cp= R*(a8+  (a9*T)+  (a10*(T).^2) + (a11*(T).^3) + (a12*(T).^4));
end
end 

Function to Calculate Enthalpy :

Function to calculate Enthalpy
% Defining the function for Enthaply based on the values of the temp.

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

if T>= local_mid_temp
    H= R*T.* (a1 + ((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)/5) + (a6./T));
else
    H= R*T.*(a8+ ((a9*T)/2) + ((a10*(T).^2)/3) + ((a11*(T).^3)/4) +  ((a12*(T).^4)/5) + (a13./T)); 
end
end

Function to Calculate Entropy :

Function to calculate Entropy
% Defining the function for Entropy based on the values of the temp.
function  S=Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,local_mid_temp)

if T>=local_mid_temp
    S= R*(a1.*log(T) + a2*T + ((a3*(T).^2)/2)+ ((a4*(T).^3)/3)+ ((a5*(T).^4)/4)+a7);
else
    S= R*(a8.*log(T) + a9*T + ((a10*(T).^2)/2)+ ((a11*(T).^3)/3)+ ((a12*(T).^4)/4)+a14);
end
end
  • In all the three functions, ‘if-else’ loop is used as there are 14 coefficients in which the first 7 coefficients are high-temperature coefficients and the second 7 coefficients are low-temperature coefficients.
  • So to calculate the specific heat, enthalpy, and entropy of the species based on the formula, the ‘if else’ loop is used in such a way that if the values of the temperature are above the local medium temperature of the respective species, the function will take the high-temperature coefficients and if the values of temperature are below the local medium temperature, the function will take the low-temperature coefficients to perform the calculation.

Function to calculate Molecular Weight of each species

Function to calculate Molecular Weight of each species
% Defining function for molecular Weight
function M= molecular_weight(species_name)
Atoms= ['O','H','C','N','Ar'];
Atomic_Weight=[16,1,12,14,40];

%Comparing Atoms to read data of species name and then calculating
%molecular weight

M=0;
for i=1: length(species_name)
    for j= 1: length(Atoms)
        if strcmp(species_name(i),Atoms(j));
         M= M+ Atomic_Weight(j);
         place=j;
        end
    end
    n=str2num(species_name(i));
    if (n>1)
        M=M+ Atomic_Weight(place)*(n-1);
    end 
end
% Displaying Molecular Weight in Command Window
fprintf('Molecular Weight of %s : %f',species_name,M)
end

Explanation of code

  • In the above script, the Molecular Weight function will take ‘Species_name’ as its input. Then the ‘Atoms’ along with their respective ‘Atomic weight’ and initially value of M written ‘0’.
  • Now we used 2 for loop, one is for species name and another is for atoms and in this for loop we have to compare one by one atom using ‘strcmp’ command. If there is value 1 then atomic weight of respective atom will add.
  • Now for detection of number in ‘Species name’ we use str2num command in 1st for loop

Let’s move to Main Program file-

  • Now we have to mention current directory path using ‘cd()’ command.
  • Then make directory/folder using mkdir() by specifying name of species_name. and mention it as current directory using ‘cd’.
  • Now in that species name folder we have to create ‘Molecular Weight’ file using fopen(‘ ’, ‘w’). after that use fprintf to write down Molecular Weight of respective species and close it.
  • Create different graph using ‘plot’ command save them in current species folder using ‘saveas’ command
  • Now again we have to mention old directory path where NASA’s thermodynamic file saved for calculation of next species.

Results :

Output Plots :

Plot for `O_2` species-

  • Temperature vs Specific Heat
  • Temperature vs Specific Enthalpy

  • Temperature vs Entropy

2. Plot for `N_2` species`

  • Temperature vs Specific Heat

  • Temperature vs Enthalpy

  • Temperature vs Entropy

3. Plot for `CO_2` species`

  • Temperature vs Specific Heat

  • Temperature vs Enthalpy

  • Temperature vs Entropy

Screenshot of the folder where all folder are saved :

Molecular Weight :

ERRORS :

Error in calculating Molecular Weight of Argon(AR)-

Solution: ‘AR’ is case sensitive, so write ‘AR’ as ‘Ar’. The molecular weight of Argon will be displayed.

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.