Challenge

Parsing NASA thermodynamic data

Aim: Parsing NASA thermodynamic data using MATLAB programming.

Theory: NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S. They have also documented the coefficients that are required to evaluate these polynomials for 1000+ species. Below figure shows the structure of the file format that includes the values of the individual species coefficients along with their local temperature ranges.

Although the file looks incomprehensible, it provides many important details of the individual behaviour of the species. So, the main motive of this project is to make a program in Matlab that can extract all the coefficients of the species and make their relation with given thermodynamic properties.

Given below are some necessary pre requisites of the file:

  1. First line gives the header information.
  2. Second line gives the global temperature ranges (in K) used.
  3. Third, fourth and fifth line are the comments which start with ‘!’.
  4. The rest lines give the species information with local temperatures (on line 5) and rest 14 coefficients that are to be used for calculating specific heat, enthalpy and entropy of the individual species. Al these properties are related to according to the relations given below:

`C_p/R=a_1+a_2⋅T+ a_3⋅T^2+a_4⋅T^3+a_5⋅T^4`

`H/(RT)=a_1+a_2⋅T/2+ a_3⋅T^2/3+a_4⋅T^3/4+a_5⋅T^4/5+a_6/T`

`S/R=a_1⋅lnT+a_2⋅T+ a_3⋅T^2/2+a_4⋅T^3/3+a_5⋅T^4/4+a_7`

The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.

Based on above information, we have to calculate the following things:

  1. Develop a Matlab program to extract all the 14 coefficients from the given 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.
  4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
  5. The main program must just take the "THERMO.dat" file as an input and generate Cp, H and S plots.

Logic Behind the program

  1. By looking to the file given (‘Thermo.dat’), it is seen that from line 6 to the end, the species are being changed after every 4 lines and all those contains the values of local temperature range along with 14 coefficients. Taking this point into consideration it was decided to use a loop which will repeat itself and do the repeated task.
  2. In order to calculate molecular weights of the species, a separate function is being made that takes all the species name and returns its weight (in grams.)
  3. Once the coefficients are extracted, it is easy to plots the graphs using plot command
  4. In order to save the plots, a separate directory is made within the loop that can store the graphs in their individual species so that time is saved in sorting.

Matlab Program:

%% FILE PARSING IN MATLAB

clear all
close all
clc
format compact
%% READING THE COMPLETE FILE 
% We know that first line contains heading
% Second line contains global temperature ranges for which the
% thermodynamic properties are valid
% Later 3 lines are just the comments which can be ignored

f1 = fopen('THERMO.dat','r');  %Reading and storing the file in variable f1
for i = 1:218
    L{i} = fgetl(f1);
end

%Extracting Global Temperature Range from line 2
A = str2num(L{2});
global_low_temp = A(1);
global_mid_temp = A(2);
global_high_temp = A(3);

for i = 6:4:217
    %Extracting local Temperature Range from line 6
    T = str2num(L{i}(49:76));       
    local_low_temp = T(1);
    local_mid_temp = T(3);
    local_high_temp = T(2);
    
    %Calculating Molecular Mass through the function created
    species_name = L{i}(1:7)
    molecular_weight = mol_weight(species_name)
    
    %Calculating the 14 coefficients
    B = findstr(L{i+1},'E');
    
    c1 = str2double(L{i+1}(1:B(1)+3));
    c2 = str2double(L{i+1}(B(1)+4:B(2)+3));
    c3 = str2double(L{i+1}(B(2)+4:B(3)+3));
    c4 = str2double(L{i+1}(B(3)+4:B(4)+3));
    c5 = str2double(L{i+1}(B(4)+4:B(5)+3));
    
    C = findstr(L{i+2},'E');
    
    c6 = str2double(L{i+2}(1:C(1)+3));
    c7 = str2double(L{i+2}(C(1)+4:C(2)+3));
    c8 = str2double(L{i+2}(C(2)+4:C(3)+3));
    c9 = str2double(L{i+2}(C(3)+4:C(4)+3));
    c10 = str2double(L{i+2}(C(4)+4:C(5)+3));
    
    D = findstr(L{i+3},'E');
    
    c11 = str2double(L{i+3}(1:D(1)+3));
    c12 = str2double(L{i+3}(D(1)+4:D(2)+3));
    c13 = str2double(L{i+3}(D(2)+4:D(3)+3));
    c14 = str2double(L{i+3}(D(3)+4:D(4)+3));
    
    % Creating a structure of species along with their molecular weight and
    % coefficients
    species.species_name = species_name;
    species.molecular_weight = molecular_weight;
    species.global_temp_range = A;
    species.local_temp_range = T;
    species.a1 = c1;
    species.a2 = c2;
    species.a3 = c3;
    species.a4 = c4;
    species.a5 = c5;
    species.a6 = c6;
    species.a7 = c7;
    species.a8 = c8;
    species.a9 = c9;
    species.a10 = c10;
    species.a11 = c11;
    species.a12 = c12;
    species.a13 = c13;
    species.a14 = c14;
    
    mkdir(['C:\Users\ADMIN\Desktop\Challenges\Parsing NASA thermodynamic data\Species\',species_name]);
    cd(['C:\Users\ADMIN\Desktop\Challenges\Parsing NASA thermodynamic data\Species\',species_name]);

    %Printing data in a file and saving in their Particular Directories
    fid = fopen('Species_Details.txt','w');
    fprintf(fid,'species_name: %6s\n',species.species_name);
    fprintf(fid,'molecular_weight: %6f\n',species.molecular_weight);
    fprintf(fid,'Global_temp_range(low): %6f\n',species.global_temp_range(1));
    fprintf(fid,'Global_temp_range(mid): %6f\n',species.global_temp_range(2));
    fprintf(fid,'Global_temp_range(high): %6f\n',species.global_temp_range(3));
    fprintf(fid,'Local_temp_range(low): %6f\n',species.local_temp_range(1));
    fprintf(fid,'Local_temp_range(mid): %6f\n',species.local_temp_range(3));
    fprintf(fid,'Local_temp_range(high): %6f\n',species.local_temp_range(2));
    fprintf(fid,'All the Coefficients are as follows: \n');
    fprintf(fid,'a1: %d\n',species.a1);
    fprintf(fid,'a2: %d\n',species.a2);
    fprintf(fid,'a3: %d\n',species.a3);
    fprintf(fid,'a4: %d\n',species.a4);
    fprintf(fid,'a5: %d\n',species.a5);
    fprintf(fid,'a6: %d\n',species.a6);
    fprintf(fid,'a7: %d\n',species.a7);
    fprintf(fid,'a8: %d\n',species.a8);
    fprintf(fid,'a9: %d\n',species.a9);
    fprintf(fid,'a10: %d\n',species.a10);
    fprintf(fid,'a11: %d\n',species.a11);
    fprintf(fid,'a12: %d\n',species.a12);
    fprintf(fid,'a13: %d\n',species.a13);
    fprintf(fid,'a14: %d\n',species.a14);
    fclose(fid);

cd('C:\Users\ADMIN\Desktop\Challenges\Parsing NASA thermodynamic data');

    %Calculating the Specific Heat (Cp), Enthalpy(H) and Entropy(S) using local
    %temperature range
    R = 8.314;
    Temperature = linspace(local_low_temp,local_high_temp,500);
    
    for j = 1:length(Temperature)
        if Temperature(j) < local_mid_temp
            Cp = (c1+c2.*Temperature+c3.*Temperature.^2+c4.*Temperature.^3....
                +c5.*Temperature.^4).*R;
            H = (c1+(c2.*Temperature)./2+(c3.*Temperature.^2)/3+(c4.*Temperature.^3)/4....
                +(c5.*Temperature.^4/5)+c6./Temperature).*Temperature.*R;
            S = (c1.*log(Temperature)+c2.*Temperature+(c3.*Temperature.^2)/2....
                +(c4.*Temperature.^3)/3+(c5.*Temperature.^4)/4+c7).*R;
        else
            Cp = (c8+c9.*Temperature+c10.*Temperature.^2+c11.*Temperature.^3....
                +c12.*Temperature.^4).*R;
            H = (c8+(c9.*Temperature)/2+(c10.*Temperature.^2)/3+(c11.*Temperature.^3)/4....
                +(c12.*Temperature.^4/5)+c13./Temperature).*Temperature.*R;
            S = (c8.*log(Temperature)+c9.*Temperature+(c10.*Temperature.^2)/2....
                +(c11.*Temperature.^3)/3+(c12.*Temperature.^4)/4+c14).*R;
        end
    end

    % Temperature v/s Entropy
    figure(1)
    plot(Temperature,S,'linewidth',2,'color','b')
    title(species_name)
    xlabel('Local Temperature (K)')
    ylabel('Entropy (kJ/K)')

    %Temperature vs Enthalpy
    figure(2)
    plot(Temperature,H,'linewidth',2,'color','k')
    title(species_name)
    xlabel('Local Temperature (K)')
    ylabel('Enthalpy (kJ/kg)')

    %Temperature vs Specific heat
    figure(3)
    plot(Temperature,Cp,'linewidth',2,'color','r')
    title(species_name)
    xlabel('Local Temperature (K)')
    ylabel('Specific heat (kJ/kg.K)')

    directory = (['C:\Users\ADMIN\Desktop\Challenges\Parsing NASA thermodynamic data\Species\',species_name]);
    %Changing the filepath to save the plots
    cd(directory)

    %Saving The Plots
    saveas(1,'Entropy.jpg');
    saveas(2,'Enthalpy.jpg');
    saveas(3,'Specific heat.jpg');
    
    cd('C:\Users\ADMIN\Desktop\Challenges\Parsing NASA thermodynamic data')
    species;
end
fclose(f1);
    

Molecular weight function ref[1]

function molecular_weight = mol_weight(species_name)
spec = upper(species_name);
% Here A represents Argon
element = ['H' 'C' 'N' 'O' 'S' 'F' 'A'];
weight = [1.008 12.01 14.01 16.00 32.06 18.998403 39.948];
molecular_weight = 0;
for i = 1:length(spec)
    for j = 1:length(element)
        if strcmp(spec(i),element(j))
            molecular_weight = molecular_weight + weight(j);
            u = j;
        end
    end
        n = str2num(spec(i));
        if n > 1
            molecular_weight = molecular_weight + weight(u)*(n-1);
        end
    end
end

Detail explanation of the code:

  1. The program starts with clearing all the variables in workspace and previous commands in command window. Format compact allows narrow spacing in output. (it is done in order to include all the details within the minimum space within the command window).
  2. Opening the file using fopen command and reading the file in a variable named f1. Then using a for loop, the entire file is read line by line using fgetl command and stored in a variable named L.
  3. Extracting global temperature ranges and converting the into numbers using str2num.
  4. Extracting the local temperature ranges of individual species using same previous commands but it a loop which is mentioned in point 13.
  5. Extracting the species name from the corresponding lines using normal syntax of cell arrays.
  6. Making a function called mol_weight that takes species name as input and returns its molecular weight (in grams).
  7. Then from the repeated pattern of the coefficients in the ‘thermo.dat’ file, the coefficients are extracted using strfind and str2double commands and storing them in their respective variable names.
  8. Creating a structure of species along with the fields containing their molecular weight and coefficients.
  9. Making a directory of individual species that can print the above details of structure in a separate file in their corresponding species folder.
  10. Once the structures are printed the directory is again changed for plotting the graphs of entropy, specific heat and enthalpy along their corresponding local temperature ranges using the correlations as mentioned above.
  11. Then again changing the directory to the species folder in order to save the plots generated from previous steps.
  12. Again, then the directory is changed to the current directory using cd command.
  13. As the same steps were to be carried out for the remaining species, for loop was used which repeated the process from step 4 until the file is read till the end. Also, along with file parsing, the loop helped in making individual species directories and storing their detailed report along with the graphs in them. This helped a lot in saving the time from sorting and making the directories manually.
  14. Finally, once the loop is ended, the file ‘thermo.dat’ is closed.

Output results

  • Molecular weights of species

  • Folder with all the species and their graphs

  • Plots of species
    • Oxygen
    • Nitrogen

    • Carbondioxide

Errors Faced

  • Plot not being generated: As temperature being an array, while coding, element wise operator was not being used that caused this error.
  • Improper graphs generated: Due to hold on command, all the grapns were being added in the same species.

Conclusion

Following are the key take away from the above project

  • Looping saves a lot time in parsing any file.
  • Creating directories within MATLAB helps in avoiding extra efforts for sorting files.

References

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.