PARSING NASA THERMODYNAMIC DATA
Aim:- To write a code to parse the NASA thermodynamic data file and then calculate the thermodynamic properties of various gas species.
Objectives:
- Read the data inside the Nasa File.
- Write the code to plot the Thermodynamics characteristics of each species.
- Calculate the Molecular weight of each species.
Theory:
File parsing: Parsing a file means reading a data stream of some sort and building a memory model of the useful content out of that data. This aim is performing some kind of transformation on the data. In simple words read the data from some file convert into the user-readable format, stored it and use that data for different operations like mathematical calculation, graphical representation, and storing the data.
In Matlab, there are special commands that are used to read and write the files.
Consider the following syntax:
x =fopen( 'filename' , 'op')
file name: includes file name with the extension (e.g THERMO.dat, THERMO.txt)
op: operation to be performed either it's reading the file or writing the file (e.g. for reading 'r' is used)
Matlab Code:
Following code below we see how to read the file from an external source and how to extract the useful data for future use.
clear all
close all
clc
Nasa=fopen('THERMO.dat','r'); %Reading the file
for i=1:5 %use of loop to skip the 1st 5 line which consists comments and title
L1_5=fgetl(Nasa); %the new line extraction
end
s1=input('Enter the species name :- ','s')
for i= 1:53 %introduced the loop to calculate coefficients for all 53 species
L6= fgetl(Nasa);
S = strsplit(L6,' '); %splitting the string
s2=S{1};
if strcmpi(s1,s2);
LLT= str2num(S{end-3}); %end-3 is used because for every 1st line the end char is 1
LHT= str2num(S{end-2});
LMT= str2num(S{end-1});
% to find the 14 coeficient
L7=fgetl(Nasa);
S1=strsplit(L7);
F1=findstr(L7,'E');
a1 = str2num(L7(1:F1(1)+3));
a2 = str2num(L7((F1(1)+4):F1(2)+3));
a3 = str2num(L7((F1(2)+4):F1(3)+3));
a4 = str2num(L7((F1(3)+4):F1(4)+3));
a5 = str2num(L7((F1(4)+4):F1(5)+3));
L8=fgetl(Nasa);
S2=strsplit(L8);
F2=findstr(L8,'E');
a6 = str2num(L8(1:F2(1)+3));
a7 = str2num(L8((F2(1)+4):F2(2)+3));
a8 = str2num(L8((F2(2)+4):F2(3)+3));
a9 = str2num(L8((F2(3)+4):F2(4)+3));
a10= str2num(L8((F2(4)+4):F2(5)+3));
L9=fgetl(Nasa);
S3=strsplit(L9);
F3=findstr(L9,'E');
a11 = str2num(L9(1:F3(1)+3));
a12 = str2num(L9((F3(1)+4):F3(2)+3));
a13 = str2num(L9((F3(2)+4):F3(3)+3));
a14 = str2num(L9((F3(3)+4):F3(4)+3));
%Calculation for cp,enthalpy & entropy
R=8.314; %Universal gas constant
T=linspace(LLT,LHT,100);
for j=1:500
if T > LMT
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.^4)./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.^4)./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
%Plotting
figure(1)
plot(T,cp,'linewidth',2,'color','r');
title('Temprature vs Specific Heat');
xlabel('temprature');
ylabel('Specific Heat');
grid on
figure(2)
plot(T,H,'linewidth',2,'color','g');
title('Temprature vs Enthalpy');
xlabel('temprature');
ylabel('Enthalpy');
grid on
figure(3)
plot(T,S,'linewidth',2,'color','m');
title('Temprature vs Entropy');
xlabel('temprature');
ylabel('Entropy');
grid on
%calculating molecular weight by function calling
mw=molecular_weight(s1);
disp(mw)
%making new directiry to store all plots
mkdir (['E:\Matlab\File_Parsing\Result',s2]);
cd(s2);
saveas(figure(1),'Temprature vs Specific Heat.png');
saveas(figure(2),'Temprature vs Enthalpy.png');
saveas(figure(3),'Temprature vs Entropy.png');
else
fgetl(Nasa);
fgetl(Nasa);
fgetl(Nasa);
end
end
-for loop is used to skip the 1st 5 lines which consists the title and comments.
-further, it is coded to find the 14- coefficients of only 1st species.
-fgetl command is used to jump to the next line after that split the string using strsplit command to separate out the data for later use.
-the local low, high and medium temp are extracted from the first line and stored it in variables.
-next line is read.
-this one contains 5 useful values, so split it using strsplit command and stored the values in a1 - a5 variable.
-and same procedure is followed to separate all values in the next two lines also
-all 14 coefficients from a1-a14 are extracted.
-The above code, when run in loop for 53 times, all coefficient values for all the 53 species are extracted.
-Now to find the specific heat, enthalpy, and entropy of all these species.
-For that defined the value of Universal gas constant i.e. R=8.314.
-After that defined T variable using linspace command which contains the 100 values of Local temperature from local low temperature to local high temperature.
-a for loop was used to calculate the value of Cp, enthalpy(H), entropy(S) at all values of variable T.
-if-else condition statement was used in for loop to choose the formula as per conditions. The statement says that if the Temperature (T) is greater than local medium temperature(LMT) then go for the first 7 coefficients (i.e. a1-a7) as the first 7 coefficients represent the value of high temperature. If this condition gets false then go for another 7 coefficient (i.e. a8 - a14) and calculate the values of Cp, H, S.
-Formulas for calculating cp, enthalpy, and entropy as follows:
-After that, code for plotting was coded.
-plot the graph of cp, enthalpy, and entropy against the temperature.
-figure-1 for temp. vs specific heat graph.
figure-2 for temp. vs enthalpy graph.
figure-3 for temp. vs entropy graph.
-This much of coding gives the graph for all 53 species.
- As per the condition, by giving any species name as input, a particular species graph has been plotted not the entire set of species.
-for this, the program was modified in such a way that at every execution it would ask the user which species is required to study and after giving species name as input it will output result for that particular species.
-so a new variable was introduced and the syntax is as follows:
s1=input(' ') the input or statement user to display on the command window.
-This input value is compared with the list of all species using strcmpi command. This command compares the input value with the string in the file. The command compares two strings for equality and it is case insensitive.
To save the graphs of each species
- for storing the result in a specific folder location mkdir command was used, where the path to store all species graph in .png format was given. This command basically makes new directory where data is stored.
-for saving all three figures in the desired format saveas command was used.
To calculate the Molecular weight
-For calculating molecular weight a function of name 'molecular_weight' was called.
-The code for function as follows:
% Molecular weight calculation
function [M]=molecular_weight(species_name);
element=['H','C','O','N','A'];
atomic_mass= [1.00784,12.0107,15.999,14.0067,39.948];
Mol_wt=0;
for i=1:length(species_name);
for j=1:length(element);
if strcmpi(species_name(i),element(j));
Mol_wt=Mol_wt+atomic_mass(j);
z=j;
end
end
n=str2num(species_name(i));
if(n>0)
Mol_wt=Mol_wt+(atomic_mass(z)*(n-1));
end
end
M=Mol_wt;
end
-here in the function the basic elements and their atomic masses is explained.
-For example, to find the molecular mass of NH3, the first step is to look up the atomic masses of nitrogen (N) and hydrogen (H).
H = 1.00794
N = 14.0067
- Plotted result for O2
- Plotted result for N2
- Plotted result for CO2
- Molecular Weight
O2 = 31.9980
N2 = 28.0134
CO2 = 44.0087