Challenge

Project 1 - Parsing NASA thermodynamic data

AIM

To write a code in Matlab to parse the NASA thermodynamic data file and then calculate the thermodynamic properties of
various gas species.

Specific Heat:


It is the amount of energy that must be added, in the form of heat, to one unit of mass of the substance in order to cause an increase
of one unit in its temperature.
Cp = (a1 + a2 T + a3 T^2 + a4 T^3 + a5 T^4)*R

Entropy:


Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work. Because work is
obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a
system. The concept of entropy provides deep insight into the direction of spontaneous change for many everyday phenomena.
S = (a1 lnT + a2 T + a3 T^2 /2 + a4 T^3 /3 + a5 T^4 /4 + a7)*R

Enthalpy:


Enthalpy, a property of a thermodynamic system, is equal to the system's internal energy plus the product of its pressure and volume.
In a system enclosed so as to prevent the mass transfer, for processes at constant pressure, the heat absorbed or released equals
the change in enthalpy.
H = (a1 + a2 T /2 + a3 T^2 /3 + a4 T^3 /4 + a5 T^4 /5 + a6/T)*R*T

close all
clear al
clc

f1 = fopen('THERMO.dat','r')
gt = fgetl(f1);
global_temp=fgetl(f1);
T = strsplit(global_temp);
T1 = str2num(T{2});
T2 = str2num(T{3});
T3 = str2num(T{4});

Temp = linspace(T1,T3,1000);
R = 8.314;
for i = 1:3
    gt = fgetl(f1);
end

for  i = 1:53 
    gt = fgetl(f1);
    A =strsplit(gt,' ');
    Species = (A{1});


line_1 = fgetl(f1);
a = findstr(line_1,'E');
a1 = line_1(1:a(1)+3);
a1 = str2num(a1);
a2 = line_1(a(1)+4:a(2)+3);
a2 = str2num(a2);
a3 = line_1(a(2)+4:a(3)+3);
a3 = str2num(a3);
a4 = line_1(a(3)+4:a(4)+3);
a4 = str2num(a4);
a5 = line_1(a(4)+4:a(5)+3);
a5 = str2num(a5);

line_2 = fgetl(f1);
b = findstr(line_2,'E');
b1 = line_2(1:b(1)+3);
a6 = str2num(b1);
b2 = line_2(b(1)+4:b(2)+3);
a7= str2num(b2);
b3 = line_2(b(2)+4:b(3)+3);
a8= str2num(b3);
b3 = line_2(b(2)+4:b(3)+3);
a9= str2num(b3);
b4 = line_2(b(3)+4:b(4)+3);
a9= str2num(b4);
b5 = line_2(b(4)+4:b(5)+3);
a10= str2num(b5);

line_3 = fgetl(f1);
c = findstr(line_3,'E');
c1 = line_3(1:c(1)+3);
a11  = str2num(c1);
c2 = line_3(c(1)+4:c(2)+3);
a12 = str2num(c2)
c3 = line_3(c(2)+4:c(3)+3);
a13 = str2num(c3);
c4 = line_3(c(3)+4:c(4)+3);
a14 = str2num(c4);


% Specific heat
Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,T2);

% Entropy
S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,T2);

% Enthalpy
H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,T2);

% Molecular weight
Molecular_weight(i) = molecular_weight(Species);

% Read & Write Molecular list
 Molecules = fopen('Molecular weight.txt','a+');
 fprintf( Molecules,'Molecular weight of %s is %d\n',Species,Molecular_weight);
 fclose(Molecules);

 current_direc = pwd;
 mkdir(Species);




 figure(1)
 plot(Temp,Cp,'linewidth',3,'color','b');
 xlabel('Temperature [K]');
 ylabel('Specific Heat [kJ]');
 title(sprintf('Specific Heat Vs Temperature %s',Species));

 figure(2)
 plot(Temp,H,'linewidth',3,'color','y');
 xlabel('Temperature [K]');
 ylabel('Enthalpy in [kJ/(mol)]');
 title(sprintf('Enthalpy Vs Temperature %s',Species));

 figure(3)
 plot(Temp,S,'linewidth',3,'color','r');
 xlabel('Temperature [K]');
 ylabel('Entropy in [kJ/(mol-K)]');
 title(sprintf('Entropy Vs Temperature %s',Species));


 M = molecular_weight(Species)

f2 = fopen('Molecular_weight ','w')
fprintf(f2,'%s\n  %s\n %f\n','Species','Molecular weight',M)
fclose(f2)

 
end

function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,T2) 

if Temp <= T2
Cp = R*(a8 + (a9*Temp) + (a10*(Temp).^2) + (a11*(Temp).^3) + (a12*(Temp).^4)); 
else
Cp = R*(a1 + (a2*Temp) + (a3*(Temp).^2) + (a4*(Temp).^3) + (a5*(Temp).^4));

end
function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,T2) 

 if Temp <= T2
 S = R.*(a8.*log(Temp) + (a9.*Temp) + (a10*((Temp).^2)/2) + (a11*((Temp).^3)/3) + (a12*((Temp).^4)/4) + a14);
 else
 S = R.*(a1.*log(Temp) + (a2.*Temp) + (a3*((Temp).^2)/2) + (a4*((Temp).^3)/3) + (a5*((Temp).^4)/4) + a7);
end
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,T2)


if Temp <= T2
    H = R*Temp.*(a8 + ((a9*Temp)/2) + ((a10*(Temp).^2)/3) + ((a11*(Temp).^3)/4) + ((a12*(Temp).^4)/5) +((a13./Temp)));
else


    H = R*Temp.*(a1 + ((a2*Temp)/2) + ((a3*(Temp).^2)/3) + ((a4*(Temp).^3)/4) + ((a5*(Temp).^4)/5) +((a6./Temp)));
end
function Molecular_weight = molecular_weight(Species)
 % Standard - Atomic Name
 atoms = ['H','C','O','N','A']; 

 
 fprintf('Molecular Weight');
 fprintf(Species);
 fprintf(' : ');
 
 Atomic_weight = [1.008 12.011 15.999 14.007 39.948]; 
 % Starting Range of Molecule 
 Molecular_weight = 0; 
 
 % Adding Atomic number to obtain particular value
 for i = 1:length(Species)
 for j = 1:length(atoms)
 if strcmpi(Species(i),atoms(j))
 Molecular_weight = Molecular_weight +Atomic_weight(j);
 position = j;
 end
 end
 n = str2num(Species(i));
 
 if n>1
 
 Molecular_weight = Molecular_weight + Atomic_weight(position)*(n-1);
 
 end
 end
 fprintf('molecular_weight %s :',Species);
 fprintf('%d',Molecular_weight);
 disp(' '); 
end

Code Explanation:
'clear all' - clear all the variables in the workspace,
'close all' - closes all the plots and figures and
'clc' - clears the command window

.
Making a directory using 'mkdir' with location address.
After that, reading the text file 'Thermo.dat' file and opening it by using ‘fopen’ and 'r' as reading command.
For reading the line in a text file using ‘fgetl’.
To obtain the temperature range from the Thermo.dat file, using ‘strsplit’ to split the temperature into a particular string cell.
Used ‘str2num’ for converting string cell to numbers.
Divided the temperature into a string and then converted to a number so that temperature values can be used separately to
solve the NASA thermodynamics.
Using ‘for & end’ loops to skip the next three comment lines in the text file.
Reading all the 53 species to plot specific heat(cp), enthalpy(H), and entropy(S), with temperature values, and to find the
molecular weights.
Used ‘for’ loop to read 53 species.
Used ‘strfind’ to find string.
Using 'linspace' specified the temperature range from low to high.
Adding universal gas constant value ‘R’.
Extracted higher and lower temperature coefficient by reading line by line from the species.
Calling specific heat, enthalpy, entropy function codes.
Calculating Molecular weight.

errors i got

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.