Project 1 - Parsing NASA thermodynamic data
AIM:
To Parse NASA thermodynamic data file using this data calculate and plot the thermodynamic properties of the species using MATLAB.
THEORY:
In general, the term "Parsing" refers to MATLAB reading an m-file (script or function) and translating it into an internal code that it stores in memory. It is this "parsed" file in memory that is actually run when you execute the code.
The official record of NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented fourteen coefficients that are required to evaluate these polynomials for various species. The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
GOVERNING EQUATIONS:
For High Temperature
Specific Heat-
`C_p/R=a_1+a_2 T+a_3 T^2+a_4 T^3+a_5 T^4`
Enthalpy-
`H/(RT)=a_1+(a_2 T)/2+(a_3 T^2)/3+(a_4 T^3)/4+(a_5 T^4)/5+a_6/T`
Entropy-
`S/R=a_1 lnT+a_2 T+(a_3 T^2)/2+(a_4 T^3)/3+(a_5 T^4)/4+a_7`
For Low Temperature
Specific Heat-
`C_p/R=a_8+a_9 T+a_10 T^2+a_11 T^3+a_12 T^4`
Enthalpy-
`H/(RT)=a_8+(a_9 T)/2+(a_10 T^2)/3+(a_11 T^3)/4+(a_12 T^4)/5+a_13/T`
Entropy-
`S/R=a_8 lnT+a_9 T+(a_10 T^2)/2+(a_11 T^3)/3+(a_12 T^4)/4+a_14`
OBJECTIVES:
- To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
- To calculate the molecular weight of each species and display it in the command window.
- To plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature: high temperature) specific for each species.
- To create a separate folder and save the plots as images with appropriate names and dump them in separate folders for each species with suitable name.
FUNCTION PROGRAM:
1. For extracting the 14 coefficients and calculating the enthalpy, entropy and specific heats for all the species :
function [cp h s]=coefficient(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T)
%To calculate the thermodynamic properties of the species
R=8.314;
for i=1:length(T)
if T(i)>1000
cp=(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4).*R;
h=(a1+(a2.*T)./2+(a3.*T.^2)./3+(a4.*T.^3)./4+(a5.*T.^5)./5+(a6./T)).*R.*T;
s=(a1.*log(T)+a2.*T+(a3.*T.^2)./2+(a4.*T.^3)./3+(a5.*T.^4)./4+a7).*R;
else
cp=(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4).*R;
h=(a8+(a9.*T)./2+(a10.*T.^2)./3+(a11.*T.^3)./4+(a12.*T.^5)./5+(a13./T)).*R.*T;
s=(a8.*log(T)+a9.*T+(a10.*T.^2)./2+(a11.*T.^3)./3+(a12.*T.^4)./4+a14).*R;
end
end
end
2. For calculating Molecular Weight of each species:
function [molecular_wt]=mole_wt(Species_Name)
% For calculating Molecular Weight of each species
s={'H','C','N','O','A','S'};
Atomic_weight=[1.00797 12.011 14.0067 15.9994 39.948 32.06];
molecular_wt=0;
for i=1:length(Species_Name)
for j=1:length(s)
if strcmp(Species_Name(i),s(j))
molecular_wt =molecular_wt+Atomic_weight(j);
U=j;
end
end
n=str2double(Species_Name(i));
if n>1
molecular_wt =molecular_wt+Atomic_weight(U)*(n-1);
end
end
% For printing the value of Molecular weight of the species
fprintf('Molecular Weight of %s:- ',Species_Name)
fprintf('%f',molecular_wt)
fprintf(' ')
fprintf('amu')
fprintf('\n')
end
MAIN PROGRAM:
clear all
close all
clc
% Reading the NASA thermodynamic data file
f1=fopen('THERMO.dat','r');
% Extracting First lines or Heading of the data file
file_name=fgetl(f1);
% Extracting Temperature value line
tline=fgetl(f1);
A=strsplit(tline,' ');
% Defining the temperature value extracted from data file
Global_Low_Temperature=str2num(A{2})
Global_Mid_Temperature=str2num(A{3})
Global_High_Temperature=str2num(A{4})
% Extracting comment line from data file
for i=1:3
Comments=fgetl(f1);
end
% For Extrating coefficient of each species
for i=1:53
% To print species number
fprintf('Species No.:%d',i)
% Extacting Species name
Species=fgetl(f1);
Species_Name=Species(1:6)
% Extracting coefficient from first line
Line_1=fgetl(f1);
l1=strfind(Line_1,'E');
a1=str2double(Line_1(1:(l1(1)+3)));
a2=str2double(Line_1((l1(1)+4):(l1(2)+3)));
a3=str2double(Line_1((l1(2)+4):(l1(3)+3)));
a4=str2double(Line_1((l1(3)+4):(l1(4)+3)));
a5=str2double(Line_1((l1(4)+4):(l1(5)+3)));
% Extracting coefficient from second line
Line_2=fgetl(f1);
l2=strfind(Line_2,'E');
a6=str2double(Line_2(1:(l2(1)+3)));
a7=str2double(Line_2((l2(1)+4):(l2(2)+3)));
a8=str2double(Line_2((l2(2)+4):(l2(3)+3)));
a9=str2double(Line_2((l2(3)+4):(l2(4)+3)));
a10=str2double(Line_2((l2(4)+4):(l2(5)+3)));
% Extracting coefficient from Third line
Line_3=fgetl(f1);
l3=strfind(Line_3,'E');
a11=str2double(Line_3(1:(l3(1)+3)));
a12=str2double(Line_3((l3(1)+4):(l3(2)+3)));
a13=str2double(Line_3((l3(2)+4):(l3(3)+3)));
a14=str2double(Line_3((l3(3)+4):(l3(4)+3)));
% Create a new folder SPECIES to store plots of different species
mkdir(['E:\Matlab Program\NASA_FILE\SPECIES\',Species_Name]);
T=linspace(Global_Low_Temperature,Global_High_Temperature,400);
% Calling function coefficient to calculate the thermodynamic properties
[cp h s]=coefficient(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T);
% Plotting Temperature vs Spcific Heat
figure(1)
plot(T,cp,'linewidth',3,'color','r')
title(sprintf('Temperature vs Spcific Heat %s', Species_Name))
xlabel('Temperature')
ylabel('Specific Heat')
% Plotting Temperature vs Enthalpy
figure(2)
plot(T,h,'linewidth',3,'color','b')
title(sprintf('Temperature vs Enthalpy %s',Species_Name))
xlabel('Temperature')
ylabel('Enthalpy')
% Plotting Temperature vs Entropy
figure(3)
plot(T,s,'linewidth',3,'color','g')
title(sprintf('Temperature vs Entropy %s',Species_Name))
xlabel('Temperature')
ylabel('Entropy')
directory=(['E:\Matlab Program\NASA_FILE\SPECIES\',Species_Name]);
cd(directory)
% Saving plots in new folder
saveas(1,'Temperature_vs_Spcific_Heat.png')
saveas(2,'Temperature_vs_Enthalpy.png')
saveas(3,'Temperature_vs_Entropy.png')
% Changing directory for calculating Molecular Weight
cd('E:\Matlab Program\NASA_FILE\SPECIES\');
% Calling function to calculate Molecular Weight
Molecular_Wt=mole_wt(Species_Name);
end
fclose(f1)
EXPLANATION OF PROGRAM:
Function Program-1.
- In this program we have created a function called coefficient to extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
- By looping temperature and using if else command we have calculated enthalpy, entropy and specific heats of the species using high temperature and low temperature coefficients as we know that the FIRST7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
Function Program -2.
- In this program we have created a called mole_wt to calculate molecular weight of the species.
- At last of this program by using fprintf command we have printed the value of molecular weight.
Main Program:
- First by using command fopen read the NASA thermodynamic data with file handler f1, the value of f1 should not be negative.
- Then we are extracting First line or Heading of the data file by using fgetl command.
- Now we are extracting Temperature value line by using fgetl command and store it into the tline and then create a cell array called A by using strsplit command.
- Then we have changed the string temperature value to numerical value by using str2 num and then defining the temperature value extracted from data file.
- Now we have extracted comments line from data file by using loop and fgetl command.
- For extracting coefficient of each species we have used using another for loop.
- To print species number we have used fprintf command inside the loop.
- We have used fgetl command inside the loop for extracting each Species name.
- Now for extracting coefficient from first line we have used fgetl command and then we have used strfind command to find the location of exponential term and store the location value in l1. Now change the string value of coefficient to double by using str2double command and also by indexing the value of each coefficient, store the numerical value of coefficient as a1, a2, a3, a4 and a5.
- Similarly extract second line and third line, store the numerical value of coefficient as a6, a7, a8, a9, a10 and a11, a12, a13, a14 .
- We have used mkdir commands to create a new folder SPECIES to store plots of different species by the name of species. The new folder SPECIES is stored in drive:E>Matlab Program>NASA_FILE.
- We have created an array ‘T’ for temperature by using linspace command.
- Now by Calling function coefficient we have calculated the thermodynamic properties like specific heat, enthalpy and entropy.
- For Plotting Temperature vs Specific Heat, Temperature vs Enthalpy and Temperature vs Entropy we have used plot command. command, given title by using tittle command and level x-axis and y-axis by using xlabel and ylabel command respectively.
- Now we have saved plots in new folder SPECIES by the name of each species by using save as command.
- We have changed directory for calculating Molecular Weight by using cd command.
- Now by calling function mole_wt we have calculated Molecular Weight.
- And then end the loop by using end command.
- Finally we have closed the file by using fclose command.
OUTPUT:
Screenshot of window containing plots of each species in the folder SPECIES saved by the name of respective species:-
PLOTS:-
For O2:
FOR N2:
For CO2:
Link for Plot of each species:-
SPECIES Folder containing plots for each species
CONCLUSION:
We have learn about the various command used for file parsing of NASA data file parsing and also calculated the specific heat, enthalpy, entropy and moleculae weight of each species. We have seen the thermodynamic behavior of each species.
REFRENCES:-
- https://in.mathworks.com/matlabcentral/answers/140438-what-does-parse-error-basically-mean
- https://www.angelo.edu/faculty/kboudrea/periodic/structure_mass.htm
- https://in.mathworks.com/matlabcentral/answers/20661-create-and-name-a-directory
- https://in.mathworks.com/help/matlab/ref/mkdir.html#bu_46hw-2
- https://in.mathworks.com/help/matlab/ref/cd.html