PARSING NASA THERMODYNAMIC DATA USING MATLAB
AIM: Write a MATLAB Program to Parse NASA's Thermodynamic data. make Plots for Cp, H, S for local temperature ranges and calculate molecular weight of all species.
OBJECTIVE:
1) Write a program to extract 14 coefficients & calculate enthalpy, entropy & specific heat for each species.
2) Calculate the molecular weight of each species & show in command window.
3) Plot the Cp, H & S curves according to temperatures.
4) Attach plots of O2, N2 & CO2 with report.
FILE PARSING:
File parsing is a technique to extract data from a targetted file. the data is in a irregular form and pasring is used to split that irregular formed data into a mannerful & meaningfull content such as table, data sheet or Plots.
In this project a file of NASA's thermodynamic data called"THERMO.dat" is parsed to get the values of Cp, H & S to make the plots.
The Formulas used for the calculations are as following:
BODY OF CONTENT:
Steps involved in the programming are as following:
- First, in the program the value of the Universal Gas Constant R = 8.314
- The given data is the formatted as a "THERMO.dat" file.
- Use the "fopen" command the file is used to open the file in the read mode specified by 'r' keyword in the fopen command.
- The file is read line by line using the "fgetl" command. The first line represents the title if the file do it is ignored.
- The next line has the global maximum, global minimum and global mid values which is read by using 'fgetl' command and split into three values by using the
- 'strsplit' command and converted into numbers.
- The temperature array is defined using 'linspace' command as 500 values between the global minimum and global maximum values.
- The next three lines are comment in the file so they are ignored by using 'for loop' to iterate for 3 values.
- Then 'for loo' is created to loop through the 53 species data given in the file and calculated the required values.
- Inside the loop the 'fgetl' command is used to read the next line of the file which represent the name of the species.
- The name of the next element is stored in a variable using 'strtok' command.
- The next three lines of the file is read which contains the high and low temperature data of the particular species.
- The read lines are in the form of a string so they are converted into number using the 'str2double' command and stored in specific variables so they can be used for further calculation.
- The low temperature values are stored in variables a1, a2, a3, a4, a5, a6 & a7 and the high temperature values are stored in variables b1, b2, b3, b4. b5, b6 & b7.
- The next line is the reference to a function called specific heat which calculate and returns the value of Enthalpy using the high and low temperature values as inputs.
- The next line is the reference to a function called Entropy which calculate and returns the value of Entropy using the high and low temperature values as inputs.
- The next line is the reference to a function that plot graphs using the given inputs.
- The next line is the reference to a function that calculates the Molecular Weights of the different element.
- The NASA Thermodynamic data file is closed using the 'fclose' command.
Function for calculating Specific Heat:
%Program to make function for calculation of specific heat
function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,glob_mid_temp)
j = 1:length(T)
if T (j) <3000
end
%Comparing with global temperature
%If local temp. is more than global mid temp., it will take minimum coefficients
if T > glob_mid_temp
Cp =(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4).*R;
%If local temp. is less than global mid temp., it will take maximum coefficients
else
Cp =(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4).*R;
end
Function for calculating Enthalpy:
%Program to make function for calculation of enthalpy
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,glob_mid_temp)
j = 1:length(T)
if T (j) <3000
end
%Comparing with global temperature
%If local temp. is more than global mid temp., it will take minimum coefficients
if T > glob_mid_temp
H =(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5)./5+a6).*R;
%If local temp. is less than global mid temp., it will take maximum coefficients
else
H =(a8.*T+(a9.*T.^2)./2+(a10.*T.^3)./3+(a11.*T.^4)./4+(a12.*T.^5)./5+a13).*R;
end
Function for calculating Entropy:
%Program to make function for calculation of entropy
function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,glob_mid_temp)
j = 1:length(T)
if T (j) <3000
end
%Comparing with global temperature
%If local temp. is more than global mid temp., it will take minimum coefficients
if T > glob_mid_temp
S =(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7).*R;
%If local temp. is less than global mid temp., it will take maximum coefficients
else
S =(a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14).*R;
end
Function for Calculating Molecular Weight:
%Program to make Function for calculation of molecular weight
function Molecularweight = Molecular_weight(Species_SP)
%Using Standard Atomic Name
Atomic_Name = ['H','C','O','N','A'];
% Hydrogen, Carbon, Oxygen, Nitrogen, Argon
fprintf('Molecular Weight of ');
fprintf(Species_SP);
fprintf(' : ');
%Input Standard Atomic Weight
Atomic_Weight = [1 12 16 14 40];
%Input Starting Range of Molecule weight
Molecularweight = 0;
% Adding Atomic number to get particular value
for i = 1:length(Species_SP)
for j = 1:length(Atomic_Name)
if strcmpi(Species_SP(i),Atomic_Name(j))
Molecularweight = Molecularweight + Atomic_Weight(j);
position = j;
end
end
n = str2num(Species_SP(i));
if n>1
Molecularweight = Molecularweight + Atomic_Weight(position)*(n-1);
end
end
fprintf('molecular weight %s :',Species_SP);
fprintf('%d',Molecularweight);
disp(' ');
end
Function for Read and Write the molecular weight data:
%Program for making function to Reading & Writing the Molecular text file
function Read_write(Species_SP,Molecularweight)
Molecular_list = fopen('Molecular weight.txt','a+');
fprintf( Molecular_list,'Molecular weight of %s is %d \n',Species_SP,Molecularweight);
fclose(Molecular_list);
end
Function for plotting of Cp vs T, H vs T, S vs T:
%Program to make function for Plotting of each species
function Plotting(Cp,H,S,T,Species_SP)
%Saving as a folder for different species
cur_dir = pwd;
mkdir(Species_SP);
cd(Species_SP);
% Plot 1, Specific heat (Cp) vs Temperature Range (T)
figure(1)
plot(T,Cp,'linewidth',3,'color','b');
xlabel('Temperature [K]');
ylabel('Specific Heat [kJ/(kg-K)]');
title(sprintf('Specific Heat vs Temperature Plot of %s',Species_SP));
saveas(figure(1),'Cp_vs_T.jpg');
% Plot 2, Enthalpy (H) vs Temperature Range (T)
figure(2)
plot(T,H,'linewidth',3,'color','g');
xlabel('Temperature [K]');
ylabel('Enthalpy [kJ/mol]');
title(sprintf('Enthalpy vs Temperature Plot of %s',Species_SP));
saveas(figure(2),'H_vs_T.jpg');
% Plot 3, Entropy (S) vs Temperature Range (T)
figure(3)
plot(T,S,'linewidth',3,'color','r');
xlabel('Temperature [K]');
ylabel('Entropy in [kJ/(mol-K)]');
title(sprintf('Entropy vs Temperature Plot of %s',Species_SP));
saveas(figure(3),'S_vs_T.jpg');
cd(cur_dir);
% Molecular weight
M = Molecular_weight(Species_SP)
f2 = fopen('molecular mass','w');
fprintf('%s n %s n %d n','species','Molecular mass',M)
fclose(f2)
end
Program to Parse the NASA's thermodynamic data:
%Progarm for Parsing NASA Thermodynamic data
clear all
close all
clc
%Setting Default Folder For each species plots and data
cd 'C:\Users\KUNAL\KUNAL\SKILL-LYNC\MATLAB\PARSING OF DATA'
...
pwd
%Opening the data file for Reading
f1 = fopen ('THERMO.dat','r'); %'r' used for reading the file
gt = fgetl(f1);
%Getting Global Temperatures data by reading first line of file
glob_temp = fgetl (f1);
temp_range = ('300.000 1000.000 5000.000');
%Spliting the temp_range vector to get single specific values of temperatures
A = strsplit(temp_range,' '); %using ' ' (space) as the input to split from
%Converting string cells to numbers using str2num,
glob_low_temp = str2num (A{1}); %Global Low Temperature
glob_mid_temp = str2num (A{2}); %Global Medium Temperature
glob_high_temp = str2num (A{3}); %Global High Temperature
%Skipping (Eliminating) the next three lines from the file
for i=3:5
comments = fgetl(f1);
end
for i = 1:53
R_L = fgetl(f1); % R_L = Read line
Species_SP = strtok(R_L); % SP = Split
L_T = strfind (R_L,'G');
%Reading local temperatue range
Avg_temp = R_L(L_T+1:end-1);
%Converting into numbers
Avg_temp = str2num(Avg_temp);
%Input Range of temperature & Gas Constant
T = linspace(glob_low_temp,glob_high_temp,500);
%Input Universal Gas Constant J/(mol-K)
R = 8.314;
%Obtaing the co-efficients
line_1 = fgetl(f1); %co-efficients of first line
line_2 = fgetl(f1); %co-efficients of second line
line_3 = fgetl(f1); %co-efficients of third line
%Finding for chaeacter 'E' to split all the co-efficients
A = strfind(line_1,'E');
B = strfind(line_2,'E');
C = strfind(line_3,'E');
% Extracting High temperature coefficients (first 7 coefficients)
a1 = str2num(line_1(1:A(1)+3));
a2 = str2num(line_1(A(1)+4:A(2)+3));
a3 = str2num(line_1(A(2)+4:A(3)+3));
a4 = str2num(line_1(A(3)+4:A(4)+3));
a5 = str2num(line_1(A(4)+4:A(5)+3));
a6 = str2num(line_2(1:B(1)+3));
a7 = str2num(line_2(B(1)+4:B(2)+3));
% Extracting Low temperature coefficients (next 7 coefficients)
a8 = str2num(line_2(B(2)+4:B(3)+3));
a9 = str2num(line_2(B(3)+4:B(4)+3));
a10 = str2num(line_2(B(4)+4:B(5)+3));
a11 = str2num(line_3(1:C(1)+3));
a12 = str2num(line_3(C(1)+4:C(2)+3));
a13 = str2num(line_3(C(2)+4:C(3)+3));
a14 = str2num(line_3(C(3)+4:C(4)+3));
%Calling the function for Specific heat calculation
Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,glob_mid_temp);
%Calling the function for Enthalpy calculation
H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,glob_mid_temp);
%Calling the function for Entropy calculation
S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,glob_mid_temp);
%Calling the function for Calculating molecular weight of species
Molecularweight(i) = Molecular_weight(Species_SP);
%Reading & writing molecular weight list of all species
Read_write(Species_SP,Molecularweight)
%Creating individual folder for each species and plots
Plotting(Cp,H,S,T,Species_SP)
end
Explaination:
1) Start writing the program by clear and clean the workspace and command also close the background dialogue box. Next Step is to provide default directory by using ‘cd’ to start the program and save all the thing into that directory, here in the program I have used my default location.
2) After that, we have to read the text file "THERMO.dat" and open it by fopen and read by 'r'.
3) Now reading the first line using fgetl cmmand.
4) Now next step is to obtain the temperature range from the Thermo.dat file.
5) Now by using ‘strsplit’ we have to split the temperature into a particular string cell.
6) After that, we have to use ‘str2num’ for converting string cell to numbers. Here we divided the temperature into a string and then converted to a number so that we can use separately to solve the NASA thermodynamics.
7) Now in the text file, we have to skip the next three lines which we are not useful for coding. So, for that, we are using ‘for & end’ loops.
8) After that, we have to read all the 53 species so that we can plot CP,H, and S. Also we can find the molecular weights.
9) For reading all those 53 species here I am using ‘for’ loop.
10) Now read the first line, here I use O_RL = fgetl.
11) For reading specific species use ‘strtok’. This syntax will return to specific species after every reading. By ‘strfind’ we will use to find string find from another.
12) Now we have to code for reading the local temperature range. By using linspace specify the temperature range from low to high and by using 500. Also, add universal gas constant value ‘R’.
13) Now next step is to extract higher and lower temperature coecient by reading line by line from the species line.
14) Now specify the specific heat, enthalpy, entropy for calling the function code. For calculating Molecular weight specify the code.
OUTPUTS:
Plots for O2:
Plots for N2:
Plots for CO2:
Screenshots of Molecular Weight results:
Screenshot of Saved Folder: