Challenge

Project 1 - Parsing NASA thermodynamic data

MATLAB PROGRAM FOR FILE PARSING

AIM :

  1. To extract 14 coefficients of each species and calculate the entropy, enthalpy and specific heat from the file given.
  2. To show the variation of these parameters against the corresponding temperature.
  3. To calculate the molecular weight of individual species.

OBJECTIVES:

  1. To understand the concept of File parsing and the logic behind it
  2. Understand the variation of various properties of elements vs the temperature range
  3. Compare various results of properties for different elements
  4. To parse the thermodynamic data file and to calculate the thermodynamic properties of gas species present in that file.

METHODOLOGY

  1. Analysis of data file and understanding the project objective.
  2. Splitting the objective into a number of small objectives like extraction of species data, local temperature data and coefficients data (which are necessary inputs to calculate thermodynamic properties).
  3. Creating a function to calculate thermodynamic properties and molecular weight of a species.
  4. Writing code to plot the results and store them in a systematic manner.
  5. Knowledge acquired during the above process is applied to build up a program such that the program must calculate and store the results of various species in a single run.

SAMPLE FILE USED IN THIS PROJECT IS AVAILABE here

GOVERNING EQUATIONS:

  • `C_p/R = a1 + a2.T + a3.T^2 + a4.T^3 + a4.T^4`
  • `H/(RT) = a1*T + a2*(T)/2 + a3*(T^2)/3 + a4*(T^3)/4 + a5*(T^4)/5 + (a6)/T`
  • `S/R = a1ln(T) + a2*T + a3*(T^2)/2 + a4*(T.^3)/3 + a5*(T.^4)/4 +a7)`

R = Universal gas const.

T = Temperature Range.

CODE AND STEP-WISE EXPLAINATION (WITH LOGIC):

clear all 
close all 
clc

% opening the file using file handle
f1 = fopen("THERMO.dat",'r');     % open the file in read mode

% to ignore the header
fgetl(f1);

% to get the global temperatures
h2 = fgetl(f1);
gtemp = str2num(h2);
low_global_temp = gtemp(1);
mid_global_temp = gtemp(2);
high_global_temp = gtemp(3);

% to neglect the headers
fgetl(f1);
fgetl(f1);
fgetl(f1);

% to get the local temperatures
for i = 1:53
    h2= fgetl(f1);
    element = strtok(h2);
    a = strfind(h2,'G');      % find the index number of a given string
    a = max(a);
    lt(i)= str2num(h2(a+3:a+10));   % converts string to a number
    ht(i)=str2num(h2(a+12:a+20));
    mt(i)= str2num(h2(a+22:a+30));
    h3= fgetl(f1);
    A= findstr(h3,'E');
    a1(i) = str2double(h3(1:A(1)+3)); % convert string to double precision value
    a2(i)= str2double(h3(A(1)+4:A(2)+3));
    a3(i) = str2double(h3(A(2)+4: A(3)+3));
    a4(i)= str2double(h3(A(3)+4: A(4)+3));
    a5(i)= str2double(h3(A(4)+4: A(5)+3));
    h4 = fgetl(f1);
    a6(i)= str2double(h4(1:A(1)+3));
    a7(i)= str2double(h4(A(1)+4:A(2)+3));
    a8(i)= str2double(h4(A(2)+4: A(3)+3));
    a9(i)= str2double(h4(A(3)+4: A(4)+3));
    a10(i) = str2double(h4(A(4)+4: A(5)+3));
    h5 = fgetl(f1);
    a11(i) = str2double(h5(1:A(1)+3));
    a12(i)= str2double(h5(A(1)+4:A(2)+3));
    a13(i)= str2double(h5(A(2)+4: A(3)+3));
    a14(i)= str2double(h5(A(3)+4: A(4)+3));

    Mass = molecular_mass(element);    % calling the function created
    sprintf('Molecular mass of %s is %d',element,Mass)
end
mkdir('Nasaa_project/plots')   % make a girectory of given name
cd('Nasaa_project/plots')
for j = 1:53  
    T = lt(j):ht(j);
    R = 8.314;
    if (T>mt(j))
        Cp = R*(a1(j) + (a2(j)*T) + (a3(j)*(T.^2)) + (a4(j)*(T.^3)) + (a5(j)*(T.^4)));
        
        H = R*T*(a1(j)*T + a2(j)*(T.^2)/2 + (a3(j)*(T.^3)/3) + (a4(j)*(T.^4)/4) + (a5(j)*(T.^5)/5) + a6(j));
       
        S= R*(a1(j)*log(T) + (a2(j)*T) + (a3(j)*(T.^2)/2) + (a4(j)*(T.^3)/3) + (a5(j)*(T.^4)/4) +a7(j));
        
    else
         Cp = R*(a8(j) + (a9(j)*T) + (a10(j)*(T.^2)) + (a11(j)*(T.^3)) + (a12(j)*(T.^4)));
        
        H = R*(a8(j)*T + a9(j)*(T.^2)/2 + (a10(j)*(T.^3)/3) + (a11(j)*(T.^4)/4) + (a12(j)*(T.^5)/5) + a13(j));
       
        S = R.*(a8(j)*log(T) + (a9(j)*T) + (a10(j)*(T.^2)/2) + (a11(j)*(T.^3)/3) + (a12(j)*(T.^4)/4) +a14(j));
    end
    
    % to plot the figures 
    figure(j); 
    subplot(3,1,1);     % to plot multiple fidures
    %hold on;
    plot(T,Cp,'r');
    title('Plot For Temp v Specific Heat');
    xlabel('Temperature');
    ylabel('Specific Heat');
    title('specific heat vs temperature');
    subplot(3,1,2);
    plot(T,H,'b');
    xlabel('Temperature');
    ylabel('Enthalpy');
    
    title('Enthalpy vs temperature ');
    subplot(3,1,3);
    plot(T,S,'g');
    xlabel('Temperature');
    ylabel('Entropy');
    title('Entropy vs temperature ');
    dir = (['/MATLAB Drive/Nasaa_project']);
    cd(dir);
   
    saveas(j,'element.jpg')        % saving the plots in given bname and format
end  
   
fclose(f1) ;                  % close the data file

LOGIC:

  1. The program begins with clear all, close all and clc which clears the workspace, closes all the plots and clears the command window respectively.
  2. In the next step, NASA thermodat file is opened using the command fopen. the letter r after the file indicates read mode.
  3. Using the fgetl command, we extract each line of the file into the command window. The first line being the header info followed by global temperature values.The next three lines are comments which are not required from program point of view.
  4. the cmd 'STRTOK' helps to get the element name in each line
  5. We could extract the local temperature ranges by using letter'G' as reference location and setting the string slice in appropriate way
  6. We can use the function created to get the molicular weight od each species
  7. The global temperature values obtained in the previous step is in the form of strings. In order to convert them into numbers, str2num command is used.
  8. From the 6th line onwards, we have the 53 species name and their 14 coefficients. Using the for loop, the name of the species and its coefficients are extracted.
  9. The first fgetl command in the loop gets us the species name.
  10. The next three fget helps us extract the coefficient values.
  11. The strfind command helps us to locate the letter E(exponential) which is used as reference for gettiing the coefficient values. These values extracted as strings are then converted to the numbers using str2num command.
  12. The mkdir command creates a folder for saving the plots that will be generated.
  13. A range of temperature is assumed for calculating enthalpy,entropy and specific heat values.
  14. In the next step, the for loop determines these parameters for each temperature values.
  15. We use command subplot to plot multiple plots in same figure hence makes it easy to compare.
  16. With the help of plot command, the calculated parameters are plotted against the corresponding value of temperature on a single figure
  17. Saveas command helps us in saving these plots with the names we want.

FUNCTION USED:

%function for calculating molecular mass
function MM=molecular_mass(element)
  element_name=upper(element);
  atoms=['H','C','N','O','AR'];
  atomic_weight=[1 12 14 16 40];
  MM=0;
  for i=1:length(element)
    for j=1:length(atomic_weight)
      if strcmp(element_name(i),atoms(j))==1
        MM=MM+atomic_weight(j);
        u=j;
    end
  end
  %adding the weight of repeatating atom
  n=str2double(element_name(i));
  if n>1
    MM=MM+atomic_weight(u)*(n-1);
  end
  end
end

LOGIC:

  1. This function takes species_name as input and gives out its Molecular mass as output.
  2. Each species name is stored in a string called element.
  3. From the file given, it can be seen that the constituents that make up each species are Hydrogen(H), Carbon(C), Nitrogen(N), Oxygen(O) and Argon(Ar). So the starting letter of each of the element is stored as constituents.
  4. The atomic weights of each constituent is stored in an array called atomic_weight.
  5. The molecular weight is set to zero in the beginning,
  6. Using the nested for loop, molecular weight is calculated as follows:
  • The first for loop runs for entire length of species name that has been determined by the for loop in the main program.
  • The second for loop runs for entire length of constituent array.
  • Using the strcmp command which compares2strings and returns 1 if they match, the molecular weight will be incremented by the corresponding value in the array'atomic_weight'.
  • Another if statement checks if a molecule in a specie has more than one molecule and increases its molecular weight correspondingly.

OUTPUT:

  • Figures of elements:

** SINCE I USE MATLAB ONLINE I HAD TO MANUALLY DUMP THE PLOTS IN LOCAL DISC FROM MATLAB DRIVE USING IMPORT OPTION**

  • MOLICULAR MASS:

  • Plots Obtained:

For 'O2':

For 'Co2':

For 'N2':

ERRORS AND PLOBLEMS FACED:

  • Unable to perform assignment because the left and right sides have a different number of elements.

This error was corrected by checking the dimension of the variable

  • Error in extracting the value of local temperatures

Since there are 2 'G' in the line hence makes it a problem to extract the values

Solution: This is solved by adding one more line in the script

a = strfind(h2,'G');
    a = max(a);

hence the later value of the G is taken into consideration

CONCLUSION:

  1. This code helps us to understand the properties of various elements with respect to the temperature
  2. We could also obtain the logic and concept behind the extraction of key values from a data file
  3. We could also write a function to calculate the molecular weight of an element
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.