Project

Parsing NASA thermodynamic data

AIM:

Write a function in MATLAB/Octave to extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species in the NASA thermodynamic data file.

GOVERNING EQUATIONS:

The equations to calculate Specific heat, Enthalpy, Entropy, are given below:

  • `C_P/R= a_1+a_2 T+a_3 T^2+a_4 T^3+a_5 T^4`
  • `H/(RT)= a_1+a_2 T+a_3 T^2+a_4 T^3+a_5 T^4+a_6 T`
  • `S/R=a_1 ln⁡T+a_2 T+a_3 T^2+a_4 T^3+a_5 T^4+a_7`

Where,

`C_P` = Specific heat`(J/(Kg K))`

H= Enthalpy `(J)`

S= Entropy `(J/K)`

R= Universal gas constant `(8.314J/(Mol-K))`

The first seven coefficients are High temperature coefficient and second seven coefficients are low temperature coefficient.

OBJECTIVES:

  1. To read NASA thermodynamic file and extract 14 coefficients from it and calculate the enthalpy, entropy and specific heat for all the species in the NASA thermodynamic data file.
  2. Plot the variation of these parameters against the temperature.

THEORY:

File parsing basically refers how information can be read from the text files and how that information can be used in a computer program. With the knowledge of file parsing one can read data from the external files and can use that data in calculation.

PROGRAM:

To read NASA thermodynamic data file and extract 14 coefficients from it to calculate enthalpy, entropy and specific heat of different species and plot the variation of these parameters with respect to temperature.

PROGRAMMING LANGUAGE: Octave

close all
clear all
clc

%opening the file
f1=fopen('THERMO.dat','r');
fgetl(f1);

%Getting the golbal low temp,global mid temp,global high temp
aline=fgetl(f1);

A= strsplit(aline,' ');
 global_low_temp= str2num(A{2});
 global_mid_temp= str2num(A{3});
 global_high_temp= str2num(A{4});
 
%Reading the header information from the data
%reading the comments from the header information
for comment_index =1:3
comment=fgetl(f1);
 fprintf("comment %d is %s \n" ,comment_index,comment)

end

%Parsing the species block
for x=1:53
species = fgetl(f1);
species_name=species(1:6);
display(species_name);

constants=[1:5;1:5;1:5];
for i= [1:3]
line=fgetl(f1);
l=findstr(line,'E');

coeff=line(1:l(1)+3);
c1=str2num(coeff);
constants(i,1) = c1;

for j= [2:5]

if i>2 && j>4
break
end

coeff= line(l(j-1)+4:l(j)+3);
constants(i,j) = str2num(coeff);
end 
end
a1=constants(1,1);
a2=constants(1,2);
a3=constants(1,3);
a4=constants(1,4);
a5=constants(1,5);
a6=constants(2,1);
a7=constants(2,2);
a8=constants(2,3);
a9=constants(2,4);
a10=constants(2,5);
a11=constants(3,1);
a12=constants(3,2);
a13=constants(3,3);
a14=constants(3,4);

%Creating the directory for saving the results of different species.
directory1='E:\ankesh'
mkdir(directory1)
directory= ['E:\ankesh\',species_name];
mkdir (directory);

%calculation of specific heat,enthalapy and entropy
%temperature is defined to take 500 values between global low temperature(300K)
%and global high temperature(5000K)
T=linspace(global_low_temp,global_high_temp,500);

%Universal gas constant (J/Mol-K)
R =8.314; 

for p=1:length(T)
if T>=global_mid_temp
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).*T.*R;
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).*T.*R;
S = (a8.*log(T)+a9.*T+(a10.*T.^2)/2+(a11.*T.^3)/3+(a12.*T.^4)/4+a14).*R;
  
end
end

%plotting specific heat with respect to temperature
figure(1)
plot(T,Cp,'linewidth',2)
xlabel('Temperature');
ylabel('Specific heat');
title('Variation of specific heat with temperature');

%cd('directory') sets the current working directory to directory 
cd (directory);
file_text= sprintf("Temperature vs Specific heat");
saveas(gca,file_text);

%plotting Enthalapy with respect to temperature
figure(2)
plot(T,H,'linewidth',2)
xlabel('Temperature');
ylabel('Enthalapy');
title('Variation of Enthalapy with temperature');

%cd('directory') sets the current working directory to directory 
cd (directory);
file_text= sprintf("Temperature vs Enthalapy");
saveas(gca,file_text);

%plotting Entropy with respect to temperature
figure(3)
plot(T,S,'linewidth',2)
xlabel('Temperature');
ylabel('Entropy');
title('Variation of Entropy with temperature');

%cd('directory') sets the current working directory to directory 
cd (directory);
file_text= sprintf("Temperature vs Entropy");
saveas(gca,file_text);

end

EXPLANATION:

Opening NASA thermodynamic file named ‘THERMO.dat’ by using ‘fopen’ command.

‘fgetl(fileID)’ returns the next line of the specified file, removing the newline characters.

  • If the file is nonempty, then fgetl returns f1 as character vector.
  • If the file is empty and contains only the end of file marker, then fgetl returns f1 as numeric value -1.
close all
clear all
clc

%opening the file
f1=fopen('THERMO.dat','r');
fgetl(f1);

Next line of the specified file contains the values of global low temperature, global mid temperature, global high temperature respectively. ’fgetl’ command is used to get these values. These values are stored in a single string therefore it is first separated into three strings by using ‘strsplit(aline,' ')’ and stored in array ‘A’ and finally the values stored in strings are converted into numbers by using ‘str2num’ command. Here {} are used while using str2num command since it’s a cell array. These temperature value are then stored in separate variables.

%Getting the golbal low temp,global mid temp,global high temp
aline=fgetl(f1);

A= strsplit(aline,' ');
 global_low_temp= str2num(A{2});
 global_mid_temp= str2num(A{3});
 global_high_temp= str2num(A{4});

Reading comments from the header information.

%Reading the header information from the data
%reading the comments from the header information
for comment_index =1:3
comment=fgetl(f1);
 fprintf("comment %d is %s \n" ,comment_index,comment)

end

Total we have 53 species and the procedure to achieve the objective is same for each species. Therefore the first ‘for loop’ is defined. In the specified file, six are the maximum alphabets which any species have in its name. Therefore a variable ‘species_name’ is defined that can take maximum 6 values.

%Parsing the species block
for x=1:53
species = fgetl(f1);
species_name=species(1:6);
display(species_name);

A matrix of size `3×5`is defined to store the extracted coefficients. Now since we know that for any particular species we have to extract the 14 coefficients from three lines, therefore next ‘for loop’ is defined for it. To distinguish the coefficients, the position of exponent is determined for each coefficient by using ‘strfind’ command.

constants=[1:5;1:5;1:5];
for i= [1:3]
line=fgetl(f1);
l=findstr(line,'E');

For every species, except the first coefficient every other coefficient can be extracted in a similar manner. Therefore firstly we extract the first coefficient and then the for loop is defined to extract the remaining coefficient.

Now we can extract 14 coefficients for a particular species but the matrix which we defined is of size `3× 5`and can store 15 values. Therefore in order to eliminate the (3,5) element of the ‘constants’ matrix ‘if’ statement is defined.

Finally a particular variable name is assigned to the coefficients stored in the ‘constants’ matrix.

coeff=line(1:l(1)+3);
c1=str2num(coeff);
constants(i,1) = c1;

for j= [2:5]

if i>2 && j>4
break
end

coeff= line(l(j-1)+4:l(j)+3);
constants(i,j) = str2num(coeff);
end 
end
a1=constants(1,1);
a2=constants(1,2);
a3=constants(1,3);
a4=constants(1,4);
a5=constants(1,5);
a6=constants(2,1);
a7=constants(2,2);
a8=constants(2,3);
a9=constants(2,4);
a10=constants(2,5);
a11=constants(3,1);
a12=constants(3,2);
a13=constants(3,3);
a14=constants(3,4);

Defining the path where the results need to be stored and then creating the directory by using ‘mkdir’ command. Firstly ‘ankesh’ folder is created in E drive and inside that the results for various species are saved in their respective folders.

%Creating the directory for saving the results of different species.
directory1='E:\ankesh'
mkdir(directory1)
directory= ['E:\ankesh\',species_name];
mkdir (directory);

Temperature is defined to take 500 values between global low temperature (300K) and global high temperature (5000K) using ‘linspace’ command. Universal gas constant is also defined.

‘for loop’ is used to calculate specific heat, enthalpy and entropy for the defined temperature range and ‘if else’ statement is used to calculate these parameters separately for local temperature ranges of low and high temperature.

%calculation of specific heat,enthalapy and entropy
%temperature is defined to take 500 values between global low temperature(300K)
%and global high temperature(5000K)
T=linspace(global_low_temp,global_high_temp,500);

%Universal gas constant (J/Mol-K)
R =8.314; 

for p=1:length(T)
if T>=global_mid_temp
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).*T.*R;
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).*T.*R;
S = (a8.*log(T)+a9.*T+(a10.*T.^2)/2+(a11.*T.^3)/3+(a12.*T.^4)/4+a14).*R;
  
end
end

Plotting the variation of specific heat, enthalpy, and entropy with respect to temperature and saving the results in the defined directory.

cd('directory') sets the current working directory to directory and ‘saveas’ commands is used to save the results.

%plotting specific heat with respect to temperature
figure(1)
plot(T,Cp,'linewidth',2)
xlabel('Temperature');
ylabel('Specific heat');
title('Variation of specific heat with temperature');

%cd('directory') sets the current working directory to directory 
cd (directory);
file_text= sprintf("Temperature vs Specific heat");
saveas(gca,file_text);

%plotting Enthalapy with respect to temperature
figure(2)
plot(T,H,'linewidth',2)
xlabel('Temperature');
ylabel('Enthalapy');
title('Variation of Enthalapy with temperature');

%cd('directory') sets the current working directory to directory 
cd (directory);
file_text= sprintf("Temperature vs Enthalapy");
saveas(gca,file_text);

%plotting Entropy with respect to temperature
figure(3)
plot(T,S,'linewidth',2)
xlabel('Temperature');
ylabel('Entropy');
title('Variation of Entropy with temperature');

%cd('directory') sets the current working directory to directory 
cd (directory);
file_text= sprintf("Temperature vs Entropy");
saveas(gca,file_text);

end

ERROR ENCOUNTERED:

The following error occurred when the folder and the sub folder inside the directory are defined at the same time.

The error is resolved by firstly defining the folder and then the sub folder inside the directory.

Creating the directory for saving the results of different species.
directory1='E:\ankesh'
mkdir(directory1)
directory= ['E:\ankesh\',species_name];
mkdir (directory);

RESULT:

Screenshot of the window where all the folders are saved.

Parameter plots for O2

Variation of Specific heat with respect to temperature

Variation of Enthalpy with respect to temperature

Variation of Entropy with respect to temperature

Parameter plots for N2

Variation of Specific heat with respect to temperature

Variation of Enthalpy with respect to temperature

Variation of Entropy with respect to temperature

Parameter plots for CO2

Variation of Specific heat with respect to temperature

Variation of Enthalpy with respect to temperature

Variation of Entropy with respect to temperature

CONCLUSION:

  • Coefficients are extracted by reading NASA thermodynamic data file and specific heat, enthalpy, entropy are calculated for all species.
  • Variation of these parameters with respect to temperature are plotted for O2, N2, and CO2
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.