File Parsing of NASA s Thermodynamic data using Matlab
Objective
To write a program that reads the Thermodynamic data by NASA and to calculate the specific heat, enthalpy and entropy of each species and also to find the molecular formula for the species.
Program
clear all
close all
clc
%File reading
f = fopen('THERMO.dat','r');
count = 1;
%Header information
fgetl(f); %to get the first line
temp = fgetl(f);
a = strsplit(temp,' '); %to get the values from the string
Global_min_temp = str2double(a{2}); %string to array
Global_mid_temp = str2double(a{3});
Global_max_temp = str2double(a{4});
for i = 1:3
fgetl(f)
end
n = input('Species Name\n','s') %Getting the input
i = 1;
%to jump to the line based on the user input
while i >= 1
tline = fgetl(f);
s = strsplit(tline,' ');
%Getting the values of the local temperature
pos = findstr(tline,'G');
Min_temp = str2double(tline(pos+4 : pos+10));
Max_temp = str2double(tline(pos+13 : pos+20));
Mid_temp = str2double(tline(pos+23 : pos+30));
%Co_eff data to be readed from seperate lines
A = fgetl(f); %L+1
B = fgetl(f); %L+2
C = fgetl(f); %L+3
i = i+1;
%Comparing of two strings to get the co_eff of the input species
a = strcmpi(s{1},n);
if a == 1
%Molecular weight function calling
x = s{1};
Molecular_Weight = molecular_weight(x)
%getting the 14 co_eff, by finding
Epos = findstr(A,'E');
a1 = str2double(A( 1 : Epos(1)+3));
for i = 1:4
a1(i+1) = str2double(A( Epos(i)+4 : Epos(i+1)+3));
end
b1 = str2double(B( 1 : Epos(1)+3));
for i = 1:4
b1(i+1) = str2double(B( Epos(i)+4 : Epos(i+1)+3));
end
c1 = str2double(C( 1 : Epos(1)+3));
for i = 1:3
c1(i+1) = str2double(C( Epos(i)+4 : Epos(i+1)+3));
end
%combining all the array into a single array
Co_eff = [a1 b1 c1]
count = count+1
break
end
end
U = 8.134; %Universal gas constant
T = linspace(200,3000,100); %Temperature
%calculation of Cp,S and H
for i = 1:length(T)
%Conditions for calculating the values with the local temperature range
if (T(i) > Mid_temp) && (T(i) < Max_temp)
%Specific Heat
Cp(i) = (Co_eff(1) + Co_eff(2)*T(i) + Co_eff(3)*T(i)^2 + Co_eff(4)*T(i)^3 + Co_eff(5)*T(i)^4)*U;
%Enthalpy
H(i) = (Co_eff(1) + Co_eff(2)*T(i)/2 + Co_eff(3)*(T(i)^2)/3 + Co_eff(4)*(T(i)^3)/4 + Co_eff(5)*(T(i)^4)/5 + Co_eff(6)/T(i))*U*T(i);
%Entropy
S(i) = (Co_eff(1)*log(T(i)) + Co_eff(2)*T(i) + Co_eff(3)*(T(i)^2)/2 + Co_eff(4)*(T(i)^3)/3 + Co_eff(5)*(T(i)^4)/4 + Co_eff(7))*U;
else
Cp(i) = (Co_eff(8) + Co_eff(9)*T(i) + Co_eff(10)*T(i)^2 + Co_eff(11)*T(i)^3 + Co_eff(12)*T(i)^4)*U;
H(i) = (Co_eff(8) + Co_eff(9)*T(i)/2 + Co_eff(10)*(T(i)^2)/3 + Co_eff(11)*(T(i)^3)/4 + Co_eff(12)*(T(i)^4)/5 + Co_eff(13)/T(i))*U*T(i);
S(i) = (Co_eff(8)*log(T(i)) + Co_eff(9)*T(i) + Co_eff(10)*(T(i)^2)/2 + Co_eff(11)*(T(i)^3)/3 + Co_eff(12)*(T(i)^4)/4 + Co_eff(14))*U;
end
end
%Creation of folder
mkdir(n)
%Plotting of Cp,S and H vs Temperature
figure(1)
plot(T,Cp,'linewidth',3,'color','b');
xlabel('Temperature');
ylabel('Specific_Heat');
figure(2)
plot(T,H,'linewidth',3,'color','r');
xlabel('Temperature');
ylabel('enthalpy');
figure(3)
plot(T,S,'linewidth',3,'color','g');
xlabel('Temperature');
ylabel('Entropy');
%the directory for saving the files is choosen
cd(n)
%saving of the figures
saveas(figure(1),'Specific_Heat.jpg')
saveas(figure(2),'Enthalpy.jpg')
saveas(figure(3),'Entropy.jpg')
%exit from the file after saving
cd ..
File read
The file to be read 'THERMO.dat' is to be opened in the Matlab. For which the command 'fopen' is used.This file is to be read and as per the syntax to read the file 'r' is to be mentioned.
fopen(filename,'r') - syntax for reading the file
Header data
From the file the global minimum , maximum and the mid temperatures are to be recorded and has to be saved in seperate variables.For which the 'fgetl' command is used which retrives the lines from the file 1 by 1 , only when it is executed for each time i.e, if the command is executed 3 times it goes to the third line and when it is executed 50 times it goes to the 50th line.
To seperate the strings the 'strsplit' command is used and the delimiter is given as 'space', so that it splits the line when the space appears.
This is then saved to different variables correspondingly .'str2double' is used to change the string to an value.
Then three lines are skipped by using 'fgetl' command in 'for' loop so that we could get the required species data and calculate for furthur results
Input
Then the input data is to be entered and it is saved into the variable n.The data is gathered by the statement
n = input('Species Name\n','s') - The 's' is to mention the string value to be entered.Without giving the term \'s\' ,the value to be entered must be entered within the quotes.
Getting the Co-efficients
tline = fgetl(f);
s = strsplit(tline,' ');
%Getting the values of the local temperature
pos = findstr(tline,'G');
Min_temp = str2double(tline(pos+4 : pos+10));
Max_temp = str2double(tline(pos+13 : pos+20));
Mid_temp = str2double(tline(pos+23 : pos+30));
The above given code is used to get the values of the local temperatures with which the enthalpy , entropy and specific heat are calculated.
The tline variable goes to the next line from the existing line using the fgetl command.
Then to get the co-efficients of the species 3 variables are used as the co-eff are given in the upcoming lines. Now to get them we have used a logic to find the position of 'E' using the 'findstr' command.This is done as all the co-eff contains the common exponential form.
%Function program for finding molecular formula
function [wt] = molecular_weight(x)
%Atomic mass of each element
C = 12.011;
H = 1.008;
N = 14.0067;
O = 15.9994;
A = 39.948;
R = 0; %to get the value for AR it is assumed to 0
mol_wt = 0;
for i = 1:length(x)
if str2double(x(i)) > 1
mol_wt = mol_wt + eval(x(i-1)) * eval(x(i)) - eval(x(i-1));
else
mol_wt = mol_wt + eval(x(i));
end
end
wt = mol_wt
end
The atomic mass for the individual elements are given .The input which will flow through is the input which we are getting in the main program.
The input value is stored in the variable 'x' and then the loop is executed.In the loop the \'if\' condition is given such that if the value of the variable x is greater than 1 then the if condition is executed or the else will be executed.
The \'eval\' command is used to evaluate the input string and to assign the strings with the value. The final molecular formula is then calculated and then saved to the \'wt\' variable whose value will go to the main program.
Plotting and saving of the species in different folders
The plotting for all species i.e, enthalpy,entropy and specific heat Vs Temperature is plotted in a 2D graph each in different figures using the \'plot\' command.
The creation of the new folder is done using the \'mxdir\' command.This command is creates a new folder for each of the species which is given as the input.
Inside the folder the plots need to be saved and so the saving directory has to be changed, which is done using the \'cd(n)\' command. Then the plots are saved with its format as required using the \'saveas\' command. As soon as the plotting is done it is saved and then it has to come out of the directory and so the final line \'cd..\' comes out of the directory.
Folder created seperatly
The above image shows the , different species stored in seperate folders
Plots for O2
Plot for N2
Plot for CO2
Result
With the plots it can be seen that the NASA thermodynamic data has been read and the co-efficients are used from the data read to calculate the values of the enthalpy , entropy and speciic heat.The moleculal weight is calculated and the plots are saved in the seperate folders as required.