Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

1. Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.

2. Calculate the molecular weight of each species and display it in the command window.

3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

EQUATIONS:

Here R is the universal gas constant, T is the temeprature

PROGRAM CODE:

clear all
close all
clc

%Make Directory
mkdir 'C:UsersAdminDesktopnasaNASAFileParse'
% Reading Data File - NASA Thermodynamic
% Open a file & 'r' Open the file for reading
f1 = fopen('THERMO.dat','r')
gt = fgetl(f1);

%Obtaining temperatures -  by reading Header Information
temperature = fgetl(f1);

% Splitting
t = strsplit(temperature);

%Converting string cells to numbers using str2num
low_temp = str2num(t{2});
medium_temp = str2num(t{3});
high_temp = str2num(t{4});

%Temperature Range & Gas constant
% Temperature Range Assumed
Temp = linspace(low_temp,high_temp,1000);

%Universal Gas Constant j/(mol-K)
R = 8.314;
% Skipping 3 comment lines in the file
for i = 1:3
    gt = fgetl(f1);
end
%Reading the species
for i = 1:53
    get = fgetl(f1);
    A = strsplit(get,' ');%spliting the cell into array
    sp = (A{1});%to read the name of the species (sp)

    line_1 = fgetl(f1);% get the next line
    a = strfind(line_1,'E');%Finding the string E
  % Extracting higher and lower temperature coefficients
  % Extracting 1st 7 coefficients i.e. HIGH Temperature coefficients
  a1 = line_1(1:a(1)+3);
  a1 = str2num(a1); %1
  a2 = line_1(a(1)+4:a(2)+3);
  a2 = str2num(a2); %2
  a3 = line_1(a(2)+4:a(3)+3);
  a3 = str2num(a3); %3
  a4 = line_1(a(3)+4:a(4)+3);
  a4 = str2num(a4); %4
  a5 = line_1(a(4)+4:a(5)+3);
  a5 = str2num(a5); %5

  line_2 = fgetl(f1);
  b = strfind(line_1,'E');
  a6 = line_2(1:b(1)+3);
  a6 = str2num(a6); %6
  a7 = line_2(a(1)+4:b(2)+3);
  a7 = str2num(a7); %7

  %Extracting 2nd 7 coefficients - Low temperature coefficients
  a8 = line_2(b(2)+4:b(3)+3);
  a8 = str2num(a8);%8
  a9 = line_2(b(3)+4:b(4)+3);
  a9 = str2num(a9);%9
  a10 = line_2(b(4)+4:b(5)+3);
  a10 = str2num(a10);%10

  line_3 = fgetl(f1);
  c = strfind(line_1,'E');
  a11 = line_3(1:c(1)+3);
  a11 = str2num(a11); %11
  a12 = line_3(c(1)+4:c(2)+3);
  a12 = str2num(a12); %12
  a13 = line_3(c(2)+4:c(3)+3);
  a13 = str2num(a13); %13
  a14 = line_3(c(3)+4:c(4)+3);
  a14 = str2num(a14); %14

  % Specific Heat Calculation
  Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,medium_temp);
  % Enthalpy Calculation
  H = Enthalpy_eval(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a13,Temp,R,medium_temp);
  %Entropy Calculation
  S = Entropy_eval(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,medium_temp);
  
  %Calculating molecular weight of species
  mw(i) = Molecular_weight(sp);
  
  %Read & Write Molecular List
  Molecular_List = fopen('Molecular Weight.txt','w');
  fprintf(Molecular_List,'Molecular weight of %s is %d \n',sp,mw);
  fclose(Molecular_List);

  % Plotting (Cp,H,S,Temp,sp)
  % Saving as a folder
  cur_dir = pwd;
  mkdir(sp);
  cd(sp);
  %Plot 1 , Specific Heat(Cp)vs Temperature Range (temp)
  figure(1)
  plot(Temp,Cp,'lineWidth',2,'color','r');
  xlabel('Temperature [K]');
  ylabel('Specific Heat [KJ]');
  title(sprintf('Varying specific heat with temperature range of %s',sp));
  grid on
  saveas(figure(1),'Specific heat.jpg');
  
  %Plot 2 , Enthalpy (H)vs Temperature Range (temp)
  figure(2)
  plot(Temp,H,'lineWidth',2,'color','r');
  xlabel('Temperature [K]');
  ylabel('Enthalpy [KJ/mol]');
  title(sprintf('Varying Enthalpy with temperature range of %s',sp));
  grid on
  saveas(figure(2),'Enthalpy.jpg');

 %Plot 3 , Entropy (S) vs Temperature Range (temp)
  figure(3)
  plot(Temp,S,'lineWidth',2,'color','r');
  xlabel('Temperature [K]');
  ylabel('Entropy [KJ/mol-K]');
  title(sprintf('Varying Entropy with temperature range of %s',sp));
  grid on
  saveas(figure(3),'Entropy.jpg');

  cd(cur_dir);

  %molecular weight
  M = Molecular_weight(sp);
  f2 = fopen('molecular mass','w');
  fprintf('For species = %s \n %s %f \n',sp,'Molecular weight = ',M);
fclose(f2);
end

FUNCTION CODE:

% Specific Heat 
function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,medium_temp)
% Comparing with global temperature
if T <= medium_temp % minimum coefficients (a8,a9,a10,a11,a12) are taken
    Cp = R*(a8 + (a9*T) + (a10*(T).^2) + (a11*(T).^3) + (a12*(T).^4));
else % maximum coefficients (a1,a2,a3,a4,a5) are taken
    Cp = R*(a1 + (a2*T) + (a3*(T).^2) + (a4*(T).^3) + (a5*(T).^4));
end
% Entropy
function S = Entropy_eval(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,medium_temp)
% Comparing with global temperature
if T<= medium_temp % Minimum coeffients (a8,a9,a10,a11,a12,a14) are taken
    S = R.*(a8.*log(T) + (a9.*T) + (a10.*((T).^2)/2) + (a11.*((T).^3)/3) + (a12.*((T).^4)/4) + a14);
else % Maximum coefficients are taken (a1,a2,a3,a4,a5,a7)
    S = R.*(a1.*log(T) + (a2.*T) + (a3.*((T).^2)/2) + (a4.*((T).^3)/3) + (a5.*((T).^4)/4) + a7);
end
%Enthalpy_eval
function H = Enthalpy_eval(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,medium_temp)
% Comparing with global temperature
if T <= medium_temp
    H = R*T.*(a8 + ((a9*T)/2) + ((a10*(T).^2)/3) + ((a11*(T).^3)/4) + ((a12*(T).^4)/5) + ((a13./T)));
else 
    H = R*T.*(a1 + ((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)/5) + ((a6./T)));
end
% Molecular Weight 
function mw = Molecular_weight(sp)
%Standard - Atomic Name
atomic_name = ['H','C','O','N','A'];
fprintf('Molecular Weight of ');
fprintf(sp);
fprintf(':')
% Standard -  Atomic Number
Atomic_weight = [1.008 12.011 15.999 14.007 39.948];
% Starting Range of Molecule
mw = 0;
% Adding Atomic Number to obtain particular value
for i = 1:length(sp)
    for j = 1:length(atomic_name)
        if strcmpi(sp(i),atomic_name(j))
            mw = mw + Atomic_weight(j);
            position = j;
        end
    end
    n = str2num(sp(i));

    if n>1
        mw = mw + Atomic_weight(position)*(n-1);
    end
end
%fprintf('molecular weight %s:',sp);
fprintf('%d',mw);
disp(' ');
end

EXPLANATION: (Program)

Initially, we begin our program by creating a directory using the ‘mkdir’ command wherein we can save all the data generated (S,Cp,H graphs) while parsing the NASA thermodynamic data file.

We then read the given data file named ‘Thermo.dat’ using the standard ‘fopen’ command and read mode ‘r’. Then we move ahead to read the header information i.e, we obtain the temperatures present in the data file using the ‘fgetl’ command, and then we use the ‘strsplit’ command to convert it into cell array and then we convert this string to a number using the ‘str2num’ command. This is done to find the low, medium and high temperatures. The temperature range is then assigned considering 1000 values between low and high temperatures.

We now define variable for Universal Gas constant R = 8.314.

Inorder to bypass the 1st three lines of the data file we create a for loop running for 3 iterations thereby using the ‘fgetl’ command for 3 times over the file f1.

Now we begin the loop to find the 53 element data in the file by assigning the ‘fgetl’ command and then splitting into cell array to find the species line and then find the species name followed by the number of temperatures in the species line.

Inorder to read the coefficients in the data file we find the letter ‘E’ using the ‘strfind’ command to find the before and after numbers of coefficients afterwards we find the remaining 14 coefficients and convert them into numbers from strings. The first 7 coefficents are for higher temperature and the next 7 are for lower temperature.

We now call upon the specific heat, entropy and enthalpy functions mentioning the suitable parameteres within these functions.

Similarly we call upon the function calculating the molecular weight of the species and then write the molecular weights in a text file, printing the weights and then close the text file using the fclose command.

Plotting of Specific Heat(Cp), Enthalpy (H) and Entropy(S) wrt to temperarure is done for different species using the ‘plot’ command and labels along the X and Y axis are given. Also the suitable title is given using the ‘title’ command. The figures are saved using the ‘saveas’ command.

Finally, the molecular weights are printed over the screen on of the command window explicitly along with the Atomic name corresponding to its weight.

EXPLANATION: (Functions of S, H, Cp)

While defining the functions of entropy, specific heat and enthalpy, we make sure we use the appropriate coefficients as stated in the equations, along with temperature, R, and mid temperature.

The condition that if temperature is less than or equal to mid temperature then it is utilizing the lower temperature coefficients otherwise the higher temperature coefficients is done using the ‘if-else’ command.

EXPLANATION: (Molecular weight)

In this function we define the main elements of all the given species in an array titled ‘atomic_name’ - H, C,O, N, Ar. Considering these elements we define another array consisting of the weights corresponding to these elements.

We take the initial molecular weight as zero and begin to assign them in using a for loop running for all 53 species. Within this loop we create another for loop running up untill it covers all the atomic names where in we use the if else condition to comapre the species and atomic name strings (strcmp command) to calculate the molecular weights. If both were found to be same, we assign it as our molecular weight.

Now, we will string to the number using str2num command assigned to n. Then we run a condition if n>1 then we increment the molecular weight by (n-1) times the position of the atomic weight

OUTPUTS:

GRAPHS:

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.