Challenge

Project 1 - Parsing NASA thermodynamic data

I) Main Code and Function code :

i) Function for specific heat, enthalpy and entropy :

% Function for Specific heat, Enthalpy & Entropy :

function [Cp H S] = Cp_H_S(co_eff,local_temp,T,R)
for q = 1:length(T)
    if T(q) >= local_temp(3)
    Cp(q) = (co_eff(1) + co_eff(2)*T(q) + co_eff(3)*(T(q)^2) + co_eff(4)*(T(q)^3) + co_eff(5)*(T(q)^4))*R;
    H(q)  = (co_eff(1) + co_eff(2)*T(q)/2 + co_eff(3)*(T(q)^2)/3 + co_eff(4)*(T(q)^3)/4 + co_eff(5)*(T(q)^4)/5+co_eff(6)/T(q))*R*T(q);
    S(q) =  (co_eff(1)*log(T(q)) + co_eff(2)*T(q) + co_eff(3)*(T(q)^2)/2 + co_eff(4)*(T(q)^3)/3 + co_eff(5)*(T(q)^4)/4 +co_eff(7))*R*T(q);
    else
    Cp(q) = (co_eff(8) + co_eff(9)*T(q) + co_eff(10)*(T(q)^2) + co_eff(11)*(T(q)^3) + co_eff(12)*(T(q)^4))*R;
    H(q)  = (co_eff(8) + co_eff(9)*T(q)/2 + co_eff(10)*(T(q)^2)/3 + co_eff(11)*(T(q)^3)/4 + co_eff(12)*(T(q)^4)/5 +co_eff(13)/T(q))*R*T(q);
    S(q) =  (co_eff(8)*log(T(q)) + co_eff(9)*T(q) + co_eff(10)*(T(q)^2)/2 + co_eff(11)*(T(q)^3)/3 + co_eff(12)*(T(q)^4)/4 +co_eff(14))*R;
    end
end

ii) Function for molecular weight :

% Function for molecular weight
function [w] = mol_wt(species)
S = ['H','C','N','O','A'];
at_wt = [1 ,12 ,14 ,16, 40];
w=0;

%calculating molecular weight of the species
for i = 1:length(species)
    
    for j = 1:length(S)
        if(strcmp(species(i),S(j))==1)
            w=w+at_wt(j);
            J=j;
            break;
        end
    end
    
    q=str2double(species(i));
    if (q>1)
        w=w+(at_wt(J).*(q))-at_wt(J);
        
    end
end
end

iii) Main Code :

% File Parsing Program
close all
clear all
clc

R = 8.314; %.....Value of universal gas constant
f1 = fopen('THERMO.dat','r');

% To skip unnecessary Data :
for i = 1:5
   a{i}= fgetl(f1);
end

% To scan for species : 'O' ===========>>>
for i = 6:20000
   a{i}= fgetl(f1);
   b = findstr(a{i},'O ');
   p = a{i};
   find_G = findstr(a{i},'G');
   find_G = find_G+3;
  z =find_G+7;
   for q = 1:3
   local_temp_str{q} = p(find_G : z);
   local_temp(q) = str2num(local_temp_str{q});
   find_G = find_G + 10;
   z = z+10;
   end
   
   if b > 0
       
       break
   
  end
end
e = 0;
w = 1;
x=1;
f=15;
d=1;
for j = i+1 : i+3
    a{j} = fgetl(f1);
    b = a{j};
      
      for k = 0:4
        if w<=14
      co_eff_str{w} = b(x:f);
      co_eff(w) = str2num(co_eff_str{w});
      w = w+1;
      e = e+1;
      x = 15*e+1;
      f = x +14;
 
        else
            break
        end
      end
    e = 0;
    x= 1;
    f = 14;
    d=d+1;
end

T = linspace(local_temp(1),local_temp(2),500);
[Cp H S] = Cp_H_S(co_eff,local_temp,T,R);
Molecular_wt_of_O = mol_wt('O')

% Plotting :
figure(1)
subplot(2,2,1)
plot(T,Cp)
title('Subplot1 : T vs. Cp')
xlabel('Temperature')
ylabel('Enthalpy')

figure(1)
subplot(2,2,2)
plot(T,H)
title('Subplot2 : T vs. H')
xlabel('Temperature')
ylabel('Enthalpy')

figure(1)
subplot(2,2,3)
plot(T,S)
title('Subplot3 : T vs. S')
xlabel('Temperature')
ylabel('Entropy')

% ************************************************************

% To scan for species 'CO2' :


for i = 6:20000
   a{i}= fgetl(f1);
   b = findstr(a{i},'CO2 ');
   
   
   
   if b > 0
       
       break
   
  end
end
e = 0;
w = 1;
x=1;
f=15;
d=1;
for j = i+1 : i+3
    a{j} = fgetl(f1);
    b = a{j};
      
      for k = 0:4
        if w<=14
      co_eff_str{w} = b(x:f);
      co_eff(w) = str2num(co_eff_str{w});
      w = w+1;
      e = e+1;
      x = 15*e+1;
      f = x +14;
 
        else
            break
        end
      end
    e = 0;
    x= 1;
    f = 14;
    d=d+1;
end

[Cp H S] = Cp_H_S(co_eff,local_temp,T,R);
Molecular_wt_of_CO2 = mol_wt('CO2')

% Plotting:
figure(2)
title('figure2 :CO2 graph')
xlabel('Temperature')
ylabel('Specific Heat')

subplot(2,2,1)
plot(T,Cp)
title('Subplot1 : T vs. Cp')

figure(2)
subplot(2,2,2)
plot(T,H)
title('Subplot2 : T vs. H')
xlabel('Temperature')
ylabel('Enthalpy')

figure(2)
subplot(2,2,3)
plot(T,S)
title('Subplot3 : T vs. S')
xlabel('Temperature')
ylabel('Entropy')
%*********************************************************************
% To scan for species 'N2' :

for i = 6:20000
   a{i}= fgetl(f1);
   b = findstr(a{i},'N2 ');
   
   
   
   if b > 0
       
       break
   
  end
end
e = 0;
w = 1;
x=1;
f=15;
d=1;
for j = i+1 : i+3
    a{j} = fgetl(f1);
    b = a{j};
      
      for k = 0:4
        if w<=14
      co_eff_str{w} = b(x:f);
      co_eff(w) = str2num(co_eff_str{w});
      w = w+1;
      e = e+1;
      x = 15*e+1;
      f = x +14;
 
        else
            break
        end
      end
    e = 0;
    x= 1;
    f = 14;
    d=d+1;
end

[Cp H S] = Cp_H_S(co_eff,local_temp,T,R);
Molecular_wt_of_N2 = mol_wt('N2')

% Plotting :
figure(3)
subplot(2,2,1)
plot(T,Cp)
title('Subplot1 : T vs. Cp')
xlabel('Temperature')
ylabel('Specific Heat')
figure(3)
subplot(2,2,2)
plot(T,H)
title('Subplot2 : T vs. H')
xlabel('Temperature')
ylabel('Enthalpy')
figure(3)
subplot(2,2,3)
plot(T,S)
title('Subplot3 : T vs. S')
xlabel('Temperature')
ylabel('Entropy')


%******************************************************************

II) Outputs :

i) Output in the command window :

Molecular weights of oxygen, carbon dioxide and nitrogen are shown.

ii) Output graph for 'O' species :

iii) Output for 'CO2' speceis :

iii) Output for species for 'N2' species :

III) Stepwise Explanation of code :

1. Initially used commands - 'clear all' used to clear all the assigned values to the variables.

'close all' - force deletes all figures

'clc' - clears the command window

2. The value of the Universal gas constant is fixed ie. R = 8.314 J/Kmol

3. The file named THERMO.dat is opened for reading and obtaining data using the fopen and r commands.

4. The initial (1st) FOR LOOP is used for skipping unnecessary data. The fgetl command is used for getting a line from the file. Thus a sequence of the FOR LOOP is used to skip unnecessary lines.

5. The next (2nd) FOR LOOP is used for scanning temperatures of the 'O' species. Initially the fgetl command is used to get the data from the file. Then next step involves using of the findstr command. This command is used to find the required species. New variables , find_G and z are created to find the variable G in the file and get the temperatures. Another FOR LOOP is created in the same for loop which helps to store the different temperatures. The IF command is used to break the loop if data for required species is obtained. All the data obtained is in the form of character which is converted to number using the str2num command.

6. The 3rd FOR LOOP is used to get the co-efficients for the required species. The data obtained from the file is in the form of character which is converted to number using the str2num command.

7. The temperatures obtained are divided over a range in 500 parts.

8. The Cp_H_S function is used to calculate the Specific heat, Enthalpy and Entropy. This function calculates the given values for seperate temperature ranges : HIGH TEMPERATURE and LOW TEMPERATURE ranges. The calculated data is used for plotting the graphs.

9. The mol_wt function is used to calculate the molecular weight of the required species. Initially values for each species is stored in the form of an array. The first FOR LOOP created is used for scanning all the letters present in the species. The second FOR LOOP is used to compare an individual letter with each letter in the earlier created array. The strcmp command is used for this process.

10. Plotting is done by using the subplot function which is used to create various plots in one figure. The data from the Cp_H_S function is taken for plotting.

11. For creating a new directory & saving results : i) The pwd command is used to store the current path in a variable named Dir.

ii) The cd command changes the current directory to that which is stored in variable Dir.

iii) The mkdir command is used to create a new foler. The cd command again changes current folder to this newly created folder.

iv) The saveas command is used to save the plot as an image in the recently created folder.

This entire process is repeated to get the data for new species. Following the above procedure, any number of species data can be obtained by repaeating the code for desired number of species.

III) Errors faced whle coding :

1. Warning was given in the command window:

Correction : The warning was given due to the use of inf in the range of FOR LOOP. The inf was replaced by a large constant number 20000. Doing this did not give rise to any further warnings in this regard.

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.