Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: To write a code in Matlab to parse the thermodynamic data from NASA of 53 different chemical compounds, utilize the coefficient data to calculate the Enthalpy, Entropy and Specific heat values for a local temperature range of the compounds, plot the values against the temperature range, calculate the molecular mass and store all the graphs of all the compounds in separate folders.

Theory:

  • The data provides a global and local temperature limit with maximum, minimum, and medium temperature.
  • It also provides 14 coefficients, ie. 2 sets of 7 coefficients each for high and low-temperature range. The medium temperature value provides the separation for the temperature range.
  • The formula shown below is used to calculate the values of Specific Heat(Cp), Enthalpy(H) and Entropy(S):

Main Code:

%-------------------Section 1---------------------------------
clear all
close all 
clc 

%------------------Section 2----------------------------------
RR= fopen('THERMO.dat','r');  %opening file in read mode

%------------------Section 3-----------------------------------
%line 1 and the next set of 4 lines are basically the information of file.
fgetl(RR);                %accessing line 1 which is the heading
a=fgetl(RR);              %Accessing next line 
head=strsplit(a,' ');     %splitting line on the appearance of 'space'
G_temp_low=str2num(head{2});  %storing the global temperatures
G_temp_mid=str2num(head{3});
G_temp_high=str2num(head{4});

fgetl(RR);
fgetl(RR);  %accessing next 3 lines without storing since no data required
fgetl(RR);

%-------------------Section 4-------------------------------------
%the data is given in a set of 4 lines and there are 53 sets of data.
ct=0;
for i=1:53   %Since all 53 sets are identical.
    line_1=fgetl(RR);
    split_temp=strsplit(line_1,'  ');
    species=split_temp{1};    %storing the name of the Species
    %storing the local temperature
    l_temp_low(i)=str2double(split_temp{end-3});
    l_temp_mid(i)=str2double(split_temp{end-1});
    l_temp_high(i)=str2double(split_temp{end-2});
    %temperature array 
    T=linspace(l_temp_low(i),l_temp_high(i),500);
   %storing the constants by converting them from string to double.
  line_1=fgetl(RR);
    E_pos=findstr(line_1,'E');
    a1=str2double(line_1(1:E_pos(1)+3));
    a2=str2double(line_1(E_pos(1)+4:E_pos(2)+3));
    a3=str2double(line_1(E_pos(2)+4:E_pos(3)+3));
    a4=str2double(line_1(E_pos(3)+4:E_pos(4)+3));
    a5=str2double(line_1(E_pos(4)+4:E_pos(5)+3));
    
  line_2=fgetl(RR);
    E_pos=findstr(line_2,'E');
    a6=str2double(line_2(1:E_pos(1)+3));
    a7=str2double(line_2(E_pos(1)+4:E_pos(2)+3));
    A1=str2double(line_2(E_pos(2)+4:E_pos(3)+3));
    A2=str2double(line_2(E_pos(3)+4:E_pos(4)+3));
    A3=str2double(line_2(E_pos(4)+4:E_pos(5)+3));
    
  line_3=fgetl(RR);
    E_pos=findstr(line_2,'E');
    A4=str2double(line_2(1:E_pos(1)+3));
    A5=str2double(line_2(E_pos(1)+4:E_pos(2)+3));
    A6=str2double(line_2(E_pos(2)+4:E_pos(3)+3));
    A7=str2double(line_2(E_pos(3)+4:E_pos(4)+3));
 
 % function call to calculate the thermodynamic properties.
 [Cp H S]=  thermodynamics(a1,a2,a3,a4,a5,a6,a7,A1,A2,A3,A4,A5,A6,A7,T,G_temp_mid);
 %funtion call to calculate molecular mass
 [mass]= molecular_mass(species);
 fprintf(' %d]Molecular Mass of %s = %d ',i,species,mass)
 fprintf('\n')
  
 % code set to create a folder
 folder_name= sprintf('%d.[%s]',i,species);
 current_dir = pwd;
 mkdir(folder_name);
 cd(folder_name);
 
 %plotting the  Cp, H and S vs temperature and saving it to the current
 %open folder
 figure(1);
 plot(T,Cp,'linewidth',3);
 xlabel('Temperature');
 ylabel('Specific Heat');
 title ('Specific Heat(Cp) v/s Temperature(T)');
 Graph1=sprintf(' Cp vs T of [%s].png',species);
 saveas(gca,Graph1);
 
 figure(2);
 plot(T,H,'linewidth',3);
 xlabel('Temperature');
 ylabel('Enthalpy');
 title ('Enthalpy(H) v/s Temperature(T)');
 Graph1=sprintf(' H vs T of [%s].png',species);
 saveas(gca,Graph1);
 
 figure(3);
 plot(T,S,'linewidth',3);
 xlabel('Temperature');
 ylabel('Entropy');
 title ('Entropy(S) v/s Temperature(T)');
 Graph1=sprintf(' S vs T of [%s].png',species);
 saveas(gca,Graph1);
 
 cd(current_dir);      %Go back to the base directory
 
 
 
    ct=ct+1;          %counter to analyse the working of the code
end

Thermodynamic Calculation Function:

function [Cp H S]=thermodynamics(a1,a2,a3,a4,a5,a6,a7,A1,A2,A3,A4,A5,A6,A7,T,G_temp_mid)

    R=8.314; 
    
    for q = 1:length(T)
%A if condn is used since the formula is different for different
%temperature range
        if T(q)>=G_temp_mid
    
        Cp = (A1+A2.*T+A3.*T.^2+A4.*T.^3+A5.*T.^4).*R;
        H = (A1+A2.*(T./2)+A3.*(T.^2./3)+A4.*(T.^3./4)+A5.*(T.^4./5)+A6./T).*T.*R;
        S = (A1.*log(T)+A2.*T+A3.*(T.^2./2)+A4.*(T.^3./3)+A5.*(T.^4./4)+A7).*R;

        else

        Cp = (a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4).*R;
        H = (a1+a2.*(T./2)+a3.*(T.^2./3)+a4.*(T.^3./4)+a5.*(T.^4./5)+a6./T).*T.*R;
        S = (a1.*log(T)+a2.*T+a3.*(T.^2./2)+a4.*(T.^3./3)+a5.*(T.^4./4)+a7).*R;
        end

      
        
    end
end

Molecular weight calculation function:

function [mass]= molecular_mass(species)

  elements= char("HCNOA"); %character array representing all elements
  at_mass=[1 12 14 16 20]; %mass of the elements respective to the character array
  mass=0;
  occ=0;
  %for loop compares the input character species with the above generated
  %character array and mass is assigned accordingly.
  for w=1:length(species)
     for z= 1: length(at_mass) 
        if species(w)==elements(z)
            mass=mass+at_mass(z);
            occ=z;
        end
     end
     %to include the no of elements in the molecular formula of the species
        factor=str2double(species(w)); 
        if factor>1
            %factor=str2double(species(w));
            mass=mass+(at_mass(occ)*(factor-1));
        end
     
  end

end

Explanation to the Programming strategy.

  1. After analysing the raw data, it is observed that after the heading and the 1st 4 lines, the data follows a specific pattern repeated after every 4 lines.
  2. The heading and the 1st 4 lines are parsed independently by reading and storing only the temperature data from the 2nd
  3. The line accessed by the fgetl function is a character array and to separate the content in the array a specific distinguishing character is identified and the array is separated to strings based on the presence of that character in the array. Then further the separated content is converted from string to appropriate data type.
  4. This is the basic logic executed ahead to convert the character from the file to meaningful data.
  5. After the heading and 1st 4 lines, the structure of data presented is identical for all 53 sets. The algorithm defined to parse the data for 1 set of data can be used to parse all the 53 set and hence a for loop is used to carry out this repetitive set of commands.
  6. The set consists of lines which contain data as follows:

Line 1: Name of the Species, Local temperatures

Line 2: 5 coefficients

Line 3: 5 coefficients

Line 4: 4 coefficients

  1. The Line is separated to strings using the occurrence of ‘space’ character. Once separated the position of the name of Species is identified and it is stored in the variable Species. The position of the temperature is identified and then converting the strings into numerical value it is stored as the maximum, minimum and the medium temperature respectively. The temperature value is then converted in an equally spaced array to use it for further calculation.
  2. For the next 3 lines the position of distinguishing Character ‘E’ is identified and the string is extracted by counting the places to extract before and after the character ‘E’ and then converting it to numerical data type. The 1st 7 coefficients are for low temperature range denoted with ‘a1 to a7’ and the next 7 coefficients are denoted using ‘A1 to A7’.
  3. Once all the 14 coefficients are extracted, they are passed to the thermodynamics function along with the temperature array generated and the Medium or mid temperature value.
  4. The thermodynamic function calculates the Cp, H and S value of the chemical species whose data is passed to it. Since the formula to calculate is different for the low and high temperature range an ‘If-Else’ statement is used inside a For Loop which utilized the mid temperature value as a breaking point between the temperature ranges. The For-Loop controls the counter values for each temperature. The formula stated above is used and the Cp, H and S values are returned.
  5. The mass function is called which calculates the molecular mass of the chemical species passed to it. The function defines the elements in a character array and the respective atomic mass in a normal array. A nested for loop with two ‘If’ statements are used to go through each element from the input Species and comparing it with the defines elements and assigning the respective atomic masses. The first for loop picks up the first character from the input character array. The nested for loop compares the input character with the defined ones. An ‘If’ statement checks if the character matches or not, and if matched assigns the adds the atomic mass of the element to the mass variable. A variable is used to record the position of occurrence of the matching element.
  6. After exiting the first ‘If’ Condition, another ‘If’ condition compares if the passes character is a Numeric value since a molecular formula contains numeric values with represent the repetition of elements, and if found, it is used as a multiplying factor for the element mass which was recorded earlier.
  7. The value of the molecular mass is printed on the command window.
  8. A set of commands where the folder name is generated and stored in a variable. A folder is created with the name in the current file directory. The directory is also saved for further use. The generated folder is opened.
  9. The values of Cp, H and S are plotted against the temperature value. The image is extracted and stored in the opened folder with a name generated using the ‘sprintf’ function.
  10. Cd function with the variable storing the base directory is used as command to close the folder and navigate back to the base directory. Since in the next loop execution the folder will be created in the basce directory as required.
  11. This logic(5-16) is coded inside the for loop which executes them in a loop for 53 iterations and the objective of the program is achieved.

OUTPUT Command Window:

Graph Plots:

1. Plots for O2

2. Plots for N2

3. Plots for CO2

References :

1. https://www.mathworks.com/help/

2. https://www.mathworks.com/matlabcentral/answers/196582-best-way-to-parse-text-file

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.