Project 1 - Parsing NASA thermodynamic data
Aim: To write a MATLAB code to parse NASA thermodynamic data file and the calculate thermodynamic properties of various gas species.
Objectives:
- Write a function that extracts the 14 coefficients and calculates the the enthalpy, entropy and specific heats for all the species in the data file.
-
Calculate the molecular weight of each specie and display it in the command window.
-
Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
-
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:
File parsing can be simply defined as reading in a data stream of some sort and building an in-memory model of the semantic content of that data.
It is the process of taking data in one format and transforming it to another format. You'll find parsers used everywhere.
This aims at facilitating performing some kind of transformation on the data .
A data parser helps us turn unstructured data into structured data and are also used to convert data from one type to another.
Governing equations:
The equations below will be further used to write functions to compute entropy ,enthalpy and specific heat values of the 53 species at different temperatures.
These functions will be later called in the main code.
- `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`
Coding Section:
In this project four functions have been written to compute specific heat,enthalpy ,entropy and molecular weight of different species.
Specific Heat function:
function[Cp]=Specific_heat(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,Low_local_temp,Mid_local_temp,High_local_temp,R,t)
if(t>Mid_local_temp)
Cp=R*(c1+c2.*t+c3.*t.^2+c4.*t.^3+c5.*t.^4)
else
Cp=R*(c8+c9.*t+c10.*t.^2+c11.*t.^3+c12.*t.^4)
end
Enthalpy Function:
function [H]=Enthalpy(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,Low_local_temp,Mid_local_temp,High_local_temp,R,t)
if(t>Mid_local_temp)
H=(c1+c2.*t./2+c3.*t.^2./3+c4.*t.^3./4+c5.*t.^4./5+c6./t)*R.*t
else
H=(c8+c9.*t./2+c10.*t.^2./3+c11.*t.^3./4+c12.*t.^4./5+c13./t)*R.*t
end
Entropy Function:
function [S]=Entropy(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,Low_local_temp,Mid_local_temp,High_local_temp,R,t)
if(t>Mid_local_temp)
S=R*(c1.*log(t)+c2.*t+c3.*t.^2./2+c4.*t.^3./3+c5.*t.^4./4+c7)
else
S=R*(c8.*log(t)+c9.*t+c10.*t.^2./2+c11.*t.^3./3+c12.*t.^4./4+c14)
end
Molecular weight function:
function [M]=Molecular_weight(specie_name)
atoms=['C','H','O','N','A'];
mol_wt=[12,1,16,14,40];
Mol_wt=0;
for i=1:length(specie_name)
for j=1:length(atoms)
if strcmpi (specie_name(i),atoms(j))
Mol_wt=Mol_wt+mol_wt(j);
u=j;
end
end
t=str2num(specie_name(i));
if(t>1)
Mol_wt=Mol_wt+ mol_wt(u)*(t-1);
end
end
M=Mol_wt;
sprintf('%s specie %s is %f',specie_name,'Molecular weight',Mol_wt)
end
Main Code:
clear all
close all
clc
%Reading data from the file
%Reading header information
f1=fopen('Thermo.dat','r');
fgetl(f1);
%Reading temperature line
temp_line=fgetl(f1);
%Splitting the Global temperature string data
S_t=strsplit(temp_line,' ');
Low_global_temp=str2num(S_t{2});
Mid_global_temp=str2num(S_t{3});
High_lobal_temp=str2num(S_t{4});
%Neglecting the 3 subsquent lines as they represent comments
fgetl(f1);
fgetl(f1);
fgetl(f1);
for i=1:53
%Line1
t_l_1=fgetl(f1);
k=strsplit(t_l_1,' ');
specie_name=k{1};
Low_local_temp=k{end-3};
Low_local_temp=str2num(Low_local_temp);
Mid_local_temp=k{end-1};
Mid_local_temp=str2num(Mid_local_temp);
High_local_temp=k{end-2};
High_local_temp=str2num(High_local_temp);
%Line2
t_l_2=fgetl(f1);
E_loc=findstr(t_l_2,'E');
%Determination of coefficients
%1st coefficient
c1=t_l_2(1:E_loc(1)+3);
c1=str2num(c1);
%2nd coefficient
c2=t_l_2(E_loc(1)+4:E_loc(2)+3);
c2=str2num(c2);
%3rd coefficient
c3=t_l_2(E_loc(2)+4:E_loc(3)+3);
c3=str2num(c3);
%4th coefficient
c4=t_l_2(E_loc(3)+4:E_loc(4)+3);
c4=str2num(c4);
%5th coefficient
c5=t_l_2(E_loc(4)+4:E_loc(5)+3);
c5=str2num(c5);
%Line3
t_l_3=fgetl(f1);
E_loc=findstr(t_l_3,'E');
%6th coefficient
c6=t_l_3(1:E_loc(1)+3);
c6=str2num(c6);
%7th coefficient
c7=t_l_3(E_loc(1)+4:E_loc(2)+3);
c7=str2num(c7);
%8th coefficient
c8=t_l_3(E_loc(2)+4:E_loc(3)+3);
c8=str2num(c8);
%9th coefficient
c9=t_l_3(E_loc(3)+4:E_loc(4)+3);
c9=str2num(c9);
%10th coefficient
c10=t_l_3(E_loc(4)+4:E_loc(5)+3);
c10=str2num(c10);
%Line4
t_l_4=fgetl(f1);
E_loc=findstr(t_l_4,'E');
%11th coefficient
c11=t_l_4(1:E_loc(1)+3);
c11=str2num(c11);
%12th coefficient
c12=t_l_4(E_loc(1)+4:E_loc(2)+3);
c12=str2num(c12);
%13th coefficient
c13=t_l_4(E_loc(2)+4:E_loc(3)+3);
c13=str2num(c13);
%14th coefficient
c14=t_l_4(E_loc(3)+4:E_loc(4)+3);
c14=str2num(c14);
%Universal Gas constant
R=8.314;
%Local temperature range
t=linspace(Low_local_temp,High_local_temp,100);
%Functions
% %Specific heat
Cp=Specific_heat(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,Low_local_temp,Mid_local_temp,High_local_temp,R,t);
%Entropy
S=Entropy(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,Low_local_temp,Mid_local_temp,High_local_temp,R,t);
%Enthalpy
H=Enthalpy(c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,Low_local_temp,Mid_local_temp,High_local_temp,R,t);
%Molecular weight
M=Molecular_weight(specie_name);
%Creating a new folder
% mkdir Species_Names
mkdir('C:\Users\Dell\Desktop\Modified',specie_name)
cd(specie_name)
%Plots
%Temperature vs Specific heat
figure(1)
plot(t,Cp)
xlabel('Temperature')
ylabel('Specific_heat')
title('Temperature vs Specific heat')
%Temperature vs Enthalpy
figure(2)
plot(t,H)
xlabel('Temperature')
ylabel('Enthalpy')
title('Temperature vs Enthalpy')
%Temperature vs Entropy
figure(3)
plot(t,S)
xlabel('Temperature')
ylabel('Entropy')
title('Temperature vs Entropy')
%Saving plots in png format
saveas(figure(1),'Specific_heat.png')
saveas(figure(2),'Enthalpy.png')
saveas(figure(3),'Entropy.png')
%Dumping images foe each species in separate folders
cd('C:\Users\Dell\Desktop\Modified')
end
Explanation of the main code:
- First of all, for starting a code in MATLAB, it’s highly recommended to use these three basics commands namely:"clc" to clear the command window, “clear all "to completely clear the workspace and ,''close all" to close all the graphs.
- Thermo data file is read using "fopen" command and stored in f1 pointer and the header information is accessed using "fgetl" command.
- "fgetl"command is used to access the next line of the data file(containing global temperature) and "strsplit" command is used to split the character string and convert it into cell character type.
- Then "str2num" command is used to convert the strings to number and get the global temperatures in numerical form.
- All the three comments line are flied over using three fgetl commands.
- A for loop is written to extract the 14 coefficients for each and every specie:The principle is to get access to each line using "getfl" command,to find the location of the "E" character for each line using "findstr "command,and to extract the coefficients taking as reference the location "E"character.
- The temperature range goes from low local temperature to high temperature range and contains 100 values:This is done using "linspace" command and stored in t variable.
- Specific Heat vs temperature,Enthalpy vs temperature and Entropy vs temperature are respectively plotted in figure(1),figure(2) and figure(3) for each and every specie.
- Saveas command is used to save these plots in image format.
- mkdir and cd commands are finally used to to dump these images in specified folders with appropriate names.
Output:
- For O2:



- For N2:



- For CO2:



Workspace:

Screenshot of the images of plots of the 53 species saved and dumped in separate folders with appropriate names:

To access the real content of thes folders,click on:https://drive.google.com/drive/folders/1ZRZhb9ajxVTgVvj1P7Ko6iWhMVR3p3-4?usp=sharing
Molecular weight display:







Results:
The code to parse the NASA thermodynamic data file(https://drive.google.com/file/d/1fkXtN7ijw0IQsQ6j88r0yx80lmswVL51/view?usp=sharing)has been successsfully written and the thermodynamic properties (Specific heat ,Entropy,Enthalpy)for each specie has been computed .
A program to compute the molecular weight of all the species has been successfully written.