Parsing NASA thermodynamic data
AIM: To write a program for Parsing NASA thermodynamic data by extracting the 14 coefficients and calculating the enthalpy, entropy, and specific heats for all the species in the given data file.
PARSING: It is a method of reading and converting a particular data type into different data types. ie) reading or manipulating a particular file with strings and converting them into integers or other data types according to the user.
Here we will be reading a Nasa thermo data file then extract coefficients and finally calculate the specific heat, enthalpy, and entropy of various species.
FORMULA USED:
cp=R*(coeff(1)+coeff(2)*T+coeff(3)*T^2+coeff(4)*T^3+coeff(5)*T^4);
H=R*T*(coeff(1)+coeff(2)*T/2+coeff(3)*T^2/3+coeff(4)*T^3/4+coeff(5)*T^4/5+coeff(6)/T);
S=R*(coeff(1)*log(T)+coeff(2)*T+coeff(3)*T^2/2+coeff(4)*T^3/3+coeff(5)*T^4/4+coeff(7));
where coeff vary for (T)Temperature range(higher coeff and lower coeff temperature), R=Universal gas constants.
1. FUNCTION PROGRAM CALCULATING CP, H, S
function [cp,H,S] = form_func(coeff,T,mt)
R=8.314; %JK−1mol−1 universal gas constant
if T > mt %for selecting high and low temperature coefficients
cp=R*(coeff(1)+coeff(2)*T+coeff(3)*T^2+coeff(4)*T^3+coeff(5)*T^4);
H=R*T*(coeff(1)+coeff(2)*T/2+coeff(3)*T^2/3+coeff(4)*T^3/4+coeff(5)*T^4/5+coeff(6)/T);
S=R*(coeff(1)*log(T)+coeff(2)*T+coeff(3)*T^2/2+coeff(4)*T^3/3+coeff(5)*T^4/4+coeff(7));
else
cp=R*(coeff(8)+coeff(9)*T+coeff(10)*T^2+coeff(11)*T^3+coeff(12)*T^4);
H=R*T*(coeff(8)+coeff(9)*T/2+coeff(10)*T^2/3+coeff(11)*T^3/4+coeff(12)*T^4/5+coeff(13)/T);
S=R*(coeff(8)*log(T)+coeff(9)*T+coeff(10)*T^2/2+coeff(11)*T^3/3+coeff(12)*T^4/4+coeff(14));
end
CODE EXPLAINED:
1. This function program is used for calculating specific heat, enthalpy, and entropy, with universal gas constant and Temperature.
2. When T>mt if condition executes or else condition is executed
3. there are 14 coeff, where coeff (1 - 7) = higher temperature coeff and coeff( 8 - 14 ) = lower temperature coeff.
2. FUNCTION PROGRAM CALCULATING MOLECULAR WEIGHT
function [mol_weight] = mol_mass_func(sp_name)
sp=['O','C','H','N','A']; % 5 basic species
mol_mass=[16,12,1,14,40]; % respective molar mass of 5 species
mol_weight=0;
for i=1:length(sp_name)
for j=1:length(sp)
if strcmp(sp_name(i),sp(j)) % string comparision if true=1 false=0
mol_weight=mol_weight+mol_mass(j);
b=j; % position of j is stored in b
end
end
x=str2double(sp_name(i)); % converting string to integer
if x>1 % works only when x is integer and greater than 1
mol_weight=mol_weight+mol_mass(b)*(x-1);
end
end
CODE EXPLAINED:
1. there are 5 basic species sp=['O','C','H','N','A'] and respective mass is stored in an array mol_mass=[16,12,1,14,40].
2. for loop and if conditions are used, where for(i) starts from 1 to 53 || for(j) runs from 1 to 5 .
3. strcmp is used for comparing 53 species with the 5 basic species.
4. the logic happens here is strcmp returns 1 when it is true ie. the species match found or else it returns 0 and skips.
5. the position of j is stored in b.
6. when we have species with number ie. O2, we will be using if condition and the following formula mol_weight=mol_weight+mol_mass(b)*(x-1).
3. MAIN PROGRAM
clear all
close all
clc
file=fopen('THERMO.dat','r'); %opening the file THERMO.dat
fgetl(file); %first line
temp=fgetl(file); %2 nd line global temperatures
TEMP=strsplit(temp,' ');
global_low_temp=str2double(TEMP{2});
global_mid_temp=str2double(TEMP{3});
global_high_temp=str2double(TEMP{4});
for header=1:3 % 3,4,5 lines
header=fgetl(file); % header lines
end
for m=1:53 % loop for 53 times || 53 species
line_sp=fgetl(file); % species line with local temperatures
A=strsplit(line_sp,' ');
lt=str2double(A(length(A)-3)); %lower local temp
ht=str2double(A(length(A)-2)); %higher local temp
mt=str2double(A(length(A)-1)); %middle local temp
sp_name=A{1}; % species position
k=1;
coeff=ones(1,14); % for 1 to 14 arrays for storing 14 coefficients
for i =1:3 % loop for 3 lines for finding 14 coefficients
line_1=fgetl(file); % first line of the coefficients
pos=strfind(line_1,'E'); % to find the position of E
for j=1:length(pos) % 1 to the length of final position of E
coeff(k)=str2double(line_1(pos(j)-11:pos(j)+3)); % find coefficients in first line
k=k+1; % incremented from 1 to 14
end
end
T=linspace(lt,ht,1000); %lower local temp : upper local temp || divided into 1000 values
for n=1:length(T)
[cp(n),H(n),S(n)]=form_func(coeff,T(n),mt); %calling the function
end
% figure and ploting with labels
figure(1);
plot(T,cp);
title(sp_name);
xlabel('Temperature');
ylabel('Specific heat');
figure(2);
plot(T,H);
title(sp_name);
xlabel('Temperature');
ylabel('Enthalpy');
figure(3);
plot(T,S);
title(sp_name);
xlabel('Temperature');
ylabel('Entropy');
% giving instruction to change the folder
cd('C:UsersjanakiramanRDesktoptemp programsspecies')
mkdir(sp_name) %creating folder
cd(sp_name) %changing to created folder
% figure saving .jpg
saveas(figure(1),'cp.jpg');
saveas(figure(2),'H.jpg');
saveas(figure(3),'S.jpg');
%changing directory
cd('C:UsersjanakiramanRDesktoptemp programs')
mol_weight=mol_mass_func(sp_name); %calling the function
sprintf('The molecular weight of %s is %f',sp_name,mol_weight)
end
CODE EXPLAINED:
1. we are opening the file THERMO.dat and r indicates for reading
2. then fgetl is used to get the next lines whenever used, the 2nd line has a global temperature which has no importance here and the next lines from 3 to 5 are just headers.
3. then we are extracting the species name and their local low, high, mid temperatures (local temperatures)
4. using strsplit we are splitting the strings which will help in finding the position followed by str2double for converting strings to numbers
5. then for loop is used for finding the coeff in the next three lines by finding the position of string E using strfind and storing the coefficients in the array
6. then (T) lower local temp: upper local temp is divided into 1000 values by linspace and the function is called for calculating CP, H, S. for n=1:length(T)
7. then 3 figures are plotted namely Temperature VS CP, H, S, and labeled appropriately
8. cd is used for changing current folders and mkdir for creating folders for each species
9. then we will be storing every plot into images within respective species folders
10. finally, we will be calling the function for calculating the molecular weight and printing them in the command window.
11. we will be using for loop to run 53 times, ie. for 53 species.
OUTPUTS:
1. showing saved folders

2. molecular weight in the command window

3. O2 PLOTS



4. N2 PLOTS



5. CO2 PLOTS



RESULTS:
1. The 14 coefficients were successfully extracted from the NASA thermo file.
2. the enthalpy, entropy, and specific heats were calculated for all the species, and plots were stored in separate folders.
3. molecular weights have been calculated for each species.