Project

NASA THERMODYNAMIC FILE PARSING USING MATLAB

AIM: To calculate specific heat, entropy, enthalpy, and molecular weight for all species given in NASA thermodynamic data file using Matlab.

KEYWORDS: Specific heat, Entropy, Enthalpy, Molecular weight, Atomic mass

SOFTWARE: MATLAB

OBJECTIVE:

1. To calculate specific heat, entropy, enthalpy, and molecular weight for all species given in the data file.

2. To create a directory and store all species.

3. Plot for all species and store in a particular file.

  • Specific heat vs. Temperature
  • Enthalpy vs. Temperature
  • Entropy vs. Temperature

4. plot for ('O2','N2','CO2')

  • Specific heat vs. Temperature
  • Enthalpy vs. Temperature
  • Entropy vs. Temperature

INTRODUCTION:

From the given thermodynamic file, we need to read the external file and import all species coefficients and temperature values to the program in order to calculate the specific heat, entropy, and enthalpy by using the respective formula. The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius. Enthalpy, the sum of the internal energy and the product of the pressure and volume of a thermodynamic system. Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. The formula for Specific heat, entropy, and enthalpy are given below,

For High temperature:-

1. Specific Heat:

Cp=R*(a8+a9 T+a10 T2+a11 T3+a12 T4)

2. Enthalpy:

H=R*(a8 T+a9 T2/2+a10 T3/3+a11 T4/4+a12 T5/5+a13)

3. Entropy:

S=R *(a8log(T)+a9 T+a10 T2/2+a11 T3+a12 T4/4+a14)

where,

(a8-a14) = coefficients for high temperature

R = real gas constant (8.314 J/mol-K)

T = Temperature (K)

For Low temperature:-

1. Specific Heat:

Cp=R*(a1+a2 T+a3 T2+a4 T3+a5 T4)

2. Enthalpy:

H=R*(a1 T+a2 T2/2+a3 T3/3+a4 T4/4+a5 T5/5+a6)

3. Entropy:

S=R *(a1log(T)+a2 T+a3 T2/2+a4 T3+a5 T4/4+a7)

where,

(a1-a7) = coefficients for low temperature

R = real gas constant (8.314 J/mol-K)

T = Temperature (K)

SYNTAX:

1. f1 = fopen('THERMO.txt','r')

where,

f1 = fileID

THERMO.txt = filename

r = read the file

2. tline = fgetl(f1)

where,

fgetl = read line by line from the file

f1 = fileID

String command

1. strlen = get length of string

2. strfind = find string

3. strcmp = compare two string with case sensitive

4. strcmpi = compare two string with case insensitive

5. strsplit = splits the string

6. str2num = convert string to number

CODE EXPLANATION:

1. Read the thermodynamic data file by using 'fopen' and 'fgetl' command which reads line by line in the data file.

2. By using 'fgetl' command and 'strsplit' command get all the coefficients.

3. Use 'for loop' to repeat the step 2 for all species to get all coefficients.

4. function generated for specific heat, entropy, enthalpy and molecular weight for ease of understood.

5. 'mkdir' is used to create new folder and 'cd' is used to save the graph to current directory.

6. 'saveas' is used to save the file.

MATLAB CODE:

#1 MAIN_PROGRAM

clear all
close all;clc

%OPEN THE FILE
f1 = fopen('THERMO.txt','r');

%READ LINE BY LINE 'fgetl'
tline_0 = fgetl(f1);
tline_1 = fgetl(f1);

%SPLITS THE STRING
I = strsplit(tline_1,' ');

%CONVERT STRING TO NUMBER
global_Low_temp = str2double(I{2});
global_mid_temp = str2double(I{3});
global_high_temp = str2double(I{4});

%READ NEXT 3 EXCLAMATORY LINES
for i=1:3
    SGP = fgetl(f1);
end

%GET COEFFICIENTS FOR EACH SPECIES

for i = 1:53
    tline_2 = fgetl(f1);
    S = strsplit(tline_2,' ');
    species_name  = S{1};
    A = strfind(tline_2,'G ');
    temp_values = tline_2(A+4:length(tline_2));
    B = strsplit(temp_values,' ');
    local_low_temp = str2double(B{1});
    local_high_temp = str2double(B{2});
    local_mid_temp = str2double(B{3});
  
tline_3 = fgetl(f1);
F = strfind(tline_3,'E');
a1 = str2double(tline_3(1:(F(1)+3)));
a2 = str2double(tline_3(F(1)+4:F(2)+3));
a3 = str2double(tline_3(F(2)+4:(F(3)+3)));
a4 = str2double(tline_3(F(3)+4:(F(4)+3)));
a5 = str2double(tline_3(F(4)+4:(F(5)+3)));

tline_4 = fgetl(f1);
F = strfind(tline_4,'E');
a6 = str2double(tline_4(1:(F(1)+3)));
a7 = str2double(tline_4(F(1)+4:F(2)+3));
a8 = str2double(tline_4(F(2)+4:(F(3)+3)));
a9 = str2double(tline_4(F(3)+4:(F(4)+3)));
a10 = str2double(tline_4(F(4)+4:(F(5)+3)));

tline_5 = fgetl(f1);
F = strfind(tline_5,'E');
a11 = str2double(tline_5(1:(F(1)+3)));
a12 = str2double(tline_5(F(1)+4:F(2)+3));
a13 = str2double(tline_5(F(2)+4:(F(3)+3)));
a14 = str2double(tline_5(F(3)+4:(F(4)+3)));

%REAL GAS CONSTANT [J/mol-K]

R = 8.314;

T = linspace(local_low_temp,local_high_temp,100);

%CALCULATION OF SPECIFIC HEAT
C_p = R*specific_heat_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,
a12,a13,a14,local_low_temp,local_mid_temp,local_high_temp,T);

%CALCULATION OF ENTROPY
s = R*entropy_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,
a13,a14,local_low_temp,local_mid_temp,local_high_temp,T);

%CALCULATION OF ENTHALPY
h = R*enthalpy_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,
a13,a14,local_low_temp,local_mid_temp,local_high_temp,T);

%REQUIRED SPECIES FOR PLOTING
req_species = ["O2","N2","CO2"];
if(species_name==req_species(1))||(species_name==req_species(2))
||(species_name==req_species(3))

%MAKE DIRECTORY
mkdir (['F:SGP SKill-LyncMatlabFile_ParsingNasa',
species_name]);

%CURRENT DIRECTORY
cd(species_name)
    
%PLOTING TEMPERATURE VS SPECIFIC HEAT
figure(1)
plot(T,C_p)
title('Specific heat vs Temperature')
xlabel('Temperature (K)')
ylabel('Specific heat (J/K)')

%SAVES THE GRAPH TO PARTICULAR FILE
% image = getframe(gcf);
% imwrite(image.cdata,'CpvsT.png')
saveas(1,'Specific_heat','png')

%PLOTING TEMPERATURE VS ENTHALPY
figure(2)
plot(T,h)
title('Enthalpy vs Temperature')
xlabel('Temperature (K)')
ylabel('Enthalphy (J)')
% image = getframe(gcf);
% imwrite(image.cdata,'hvsT.png')
saveas(2,'Enthalpy','png')

%PLOTING TEMPERATURE VS ENTROPY
figure(3)
plot(T,s)
title('Entropy vs Temperature')
xlabel('Temperature (K)')
ylabel('Entropy (J/K)')
% image = getframe(gcf);
% imwrite(image.cdata,'SvsT.png')
saveas(3,'Entropy','png')

cd ..
end

%CALCULATION OF MOLECULAR WEIGHT
Molecular_wt = molecular_weight(species_name);
K = sprintf('%s  Mol_wt = %f',species_name,Molecular_wt);

%CALCULATING REQUIRED MOLECULAR WEIGHT
req_spec = ["O2","N2","CO2"];
if(species_name==req_species(1))||(species_name==req_species(2))
||(species_name==req_species(3))
disp(K)
end
end

#2 FUNCTION_PROGRAM FOR SPECIFIC HEAT

function[C_p] = specific_heat_calc(a1,a2,a3,a4,a5,a6,a7,a8,
a9,a10,a11,a12,a13,a14,low,mid,high,T)

if (T>mid)

C_p = (a1+(a2*T)+(a3*(T.^2))+(a4*(T.^3))+(a5*(T.^4)));
    
    
else
    
C_p = (a8+(a9*T)+(a10*(T.^2))+(a11*(T.^3))+(a12*(T.^4)));
    
end
   
#3 FUNCTION_PROGRAM FOR ENTHALPY

function [h] = enthalpy_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,
a11,a12,a13,a14,low,mid,high,T)

if (T>mid)

h = (a1*T+(a2*(T.^2)/2)+(a3*(T.^3)/3)+(a4*(T.^4)/4)+
(a5*(T.^5)/5)+a6);

else
    
h = (a8*T+(a9*(T.^2)/2)+(a10*(T.^3)/3)+(a11*(T.^4)/4)+
(a12*(T.^5)/5)+a13);

end

#4 FUNCTION_PROGRAM FOR ENTROPY

function [s] = entropy_calc(a1,a2,a3,a4,a5,a6,a7,a8,a9,
a10,a11,a12,a13,a14,low,mid,high,T)

if (T>mid)

s = (a1*log(T)+(a2*T)+(a3*(T.^2)/2)+(a4*(T.^3))+
(a5*(T.^4)/4)+a7);

else
    
s = (a8*log(T)+(a9*T)+(a10*(T.^2)/2)+(a11*(T.^3))+
(a12*(T.^4)/4)+a14);

end

#5 FUNCTION_PROGRAM FOR MOLECULAR WEIGHT

function [M] = molecular_weight(species_name)

Atomic_mass = [15.999,1.008,12.011,14.007,39.948];
Elements = ['O','H','C','N','A'];


Mol_wt = 0;
for i = 1:length(species_name)
    for j = 1:length(Elements)
        if strcmpi(species_name(i),Elements(j))
            Mol_wt = Mol_wt+Atomic_mass(j);
            z = j;
 
        end
    end
    var = str2double(species_name(i));
     if (var>1)
   
         Mol_wt = Mol_wt+(Atomic_mass(z)*(var-1));
    
     end   

end
M = Mol_wt;
end
            

OUTPUT:

1. All species folder created

2. All Molecular weight

3. PLOTS FOR 'O2'

4. PLOTS FOR 'N2'

5. PLOTS FOR 'CO2'

4. Molecular weight for required species:

CONCLUSION:

1. Plots are obtained for all species and stored in a particular folder.

2. All species molecular weights are calculated.

3. Specific heat, entropy, and enthalpy are calculated by using the formula and by applied conditions like the first 7 coefficients are high temperature and the second 7 coefficients are low temperature.

1 comment
  1. Srilakshmanan Ghow to find the no of species in file without manual reading
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.