Project

NASA THERMODYNAMIC DATA USING FILE PARSING WITH MATLAB

Aim

Extract Coefficients From Given Data and Calculate Molecular Weight of each species Using Data Parsing with MATLAB

Objectives

  • Extract the data from NASA Thermodynamic data file containing the information that are helpful in calculating the thermodynamic properties such as Specific heat(Cp), Enthalpy(H) and Entropy(S)
  • Writing a code to read the information and calculating the thermodynamic properties.
  • Calculating the molecular weight of each species that are given in the data set.
  • Plot the graph for Specific heat, Enthalpy and Entropy for the local temperature (low temperature: high temperature) specific for each species.
  • Save the plot as images with appropriate name and labels which is generated automatically

Given formulae

Main Code

%file parsing of NASA Thermodynamic data
%Calculation of molecular weight

clear all
close all
clc
 
 %Universal Gas Constant
 R = 8.314; % J/mol K  
 %Opening the given file
 f1 = fopen('THERMO.dat','r');   
 l1 = fgetl(f1);                      
 l2 = fgetl(f1);
 t = strsplit(l2);
 global_min = str2double(t{2});
 global_mid = str2double(t{3});
 global_max = str2double(t{4});
 
 temp = linspace(global_min,global_max,400);
 
 for i = 1:3
     fgetl(f1);
 end
 
 for i= 1:53
     line_1 = fgetl(f1);
     A = strsplit(line_1, ' ');
     Species_name = (A{1});
     
     tline_2 = fgetl(f1);
     tline_3 = fgetl(f1);
     tline_4 = fgetl(f1);
     
     a = strfind(tline_2,'E');
     b = strfind(tline_3,'E');
     c = strfind(tline_4,'E');
     
     %extraction of high temperature coefficients(a1:a7)
     a1 = str2double(tline_2(1:a(1)+3));
     a2 = str2double(tline_2(a(1)+4:a(2)+3));
     a3 = str2double(tline_2(a(2)+4:a(3)+3));
     a4 = str2double(tline_2(a(3)+4:a(4)+3));
     a5 = str2double(tline_2(a(4)+4:a(5)+3));
     a6 = str2double(tline_3(1:b(1)+3));
     a7 = str2double(tline_3(b(1)+4: b(2)+3));
     
     %extraction of low temperature coefficients (b1:b7)
     b1 = str2double(tline_3(b(2)+4: b(3)+3));
     b2 = str2double(tline_3(b(3)+4: b(4)+3));
     b3 = str2double(tline_3(b(4)+4: b(5)+3));
     b4 = str2double(tline_4(1:c(1)+3));
     b5 = str2double(tline_4(c(1)+4: c(2)+3));
     b6 = str2double(tline_4(c(2)+4: c(3)+3));
     b7 = str2double(tline_4(c(3)+4: c(4)+3));
     
     
     mkdir(['E:\MATLAB\file Parsing\sample\',Species_name]); 
     
     
     C_p = specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,temp,global_mid);
     
       h = Enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp,global_mid);
       
       S = entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp,global_mid);

       %plotting of enthalpy,entropy,specific heat with temperature range
       %(low temperature to high temperature)
       
       figure(1)
       
       subplot(3,1,1)
       plot(temp, C_p, 'linewidth', 4,'color', 'b');%plot based on the observed data
       title(Species_name)
       xlabel("Temperature")
       ylabel("Specific Heat")
       
       subplot(3,1,2);
       plot(temp, h, 'linewidth', 4,'color', 'r');
       title(Species_name)
       xlabel("Temperature")
       ylabel("Enthalpy")
       
      
       subplot(3,1,3);
       plot(temp, S, 'linewidth', 4,'color', 'g');
       title(Species_name)
       xlabel("Temperature")
       ylabel("Entropy")
       
      %saving to specified directory of auto-genrated plots
       directory = (['E:\MATLAB\file Parsing\sample\',Species_name]);
       cd(directory)
       saveas(1,'ouput.jpg');
       
       cd('E:\MATLAB\file Parsing\sample\')
       MW = molecular_weight(Species_name);
      
 end
 
 fclose(f1)

Function for enthalpy:

%function Call Enthalpy

function [h] = Enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp, global_mid)
if temp <= global_mid
h = R.*temp.*(b1 + (b2.*(temp./2))+ (b3.*(temp./3))+(b4.*(temp./4)) + (b5.*(temp./5)) + (b6./temp));
 else 
    h =  R.*temp.*(a1 + (a2.*(temp./2))+ (a3.*(temp./3))+(a4.*(temp./4)) + (a5.*(temp./5)) + (a6./temp));
end

Function for entropy:

%function Call Entropy

function [S] = entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp, global_mid)
if temp <= global_mid
S = R.*(b1.*log(temp) + (b2.*temp)+ (b3.*(((temp).^2)./2))+ (b4.*(((temp).^3)./3)) + (b5.*(((temp).^4)./4))+ b7);
 else 
    S =  R.*(a1.*log(temp) + (a2.*temp)+ (a3.*(((temp).^2)./2))+ (a4.*(((temp).^3)./3)) + (a5.*(((temp).^4)./4))+ a7);
end

Function for Specific heat

%function Call Specific Heat


function [C_p] = specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,temp,global_mid)
 if temp <= global_mid
C_p = R*(b1 + (b2*temp)+ (b3*(temp).^2)+(b4 * (temp).^3) + (b5 *(temp).^4));
else
    C_p =  R*(a1 + (a2*temp)+ (a3*(temp).^2)+(a4 * (temp).^3) + (a5 *(temp).^4));
end

Function for Molecular weight

%function - Molecular Weight Calculation

function [MW] = molecular_weight(Species_name)

element= {'H' 'C' 'N' 'O' 'Ar'};
atomic_weight = [1 12 14 16 40];

MW = 0;

for i = 1:length(Species_name)
    for j = 1:length(atomic_weight);
        if strcmpi(Species_name(i), element(j));
            MW = MW + atomic_weight(j);
            
            q = j;
        end
    end
    
    m = str2double(Species_name(i));
    if m>1
        MW = MW + (atomic_weight(q)*(m-1));
    end
    
end

fprintf('Molecular weight of %s is %d',Species_name,MW);
disp(' ')
end

Explanation of the code

  • The code begins with clear all, close all, clc. To clear, close all the files and clean the command window.
  • The value of R (universal gas constant) is provided as input in the code.
  • f1 = fopen to open the file "Thermo.dat" under consideration for the data analysis and parsing with the specification of "r" indicating to read the command
  • fgetl command is used to detect the first two lines l1 and l2. l1 highlights the name of the file and as for l2 follows the temperature points regarded as global_min, global_mid and global_max.
  • strsplit command is used to split the string by default delimiter and gather the corresponding temperature data points.
  • str2num was initially used to convert the string to number. However str2double is an enhanced command by the virtue of the performance criteria.
  • The temperature is broken into 400 intervals using the linspace command over the min and max temperatures.
  • The next three ignored because they are not useful.
  • The for loop is initiated for the 53 constituents mentioned in the data file to gather the 1st portion which indicates the name of the compound is assigned to Species_name from the fgetl and strsplit commands.
  • t_line2, t_line3 and t_line4 are assigned using fgetl, these 3 lines contains the 14 coefficients for determination of the properties.
  • strfind command is used to identify the letter E from the strings and is assigned to a,b and c for the corresponding lines.
  • a1,a2,a3,a4,a5,a6,a7 represents the high temperature coefficients.
  • b1,b2,b3,b4,b5,b6,b7 represents the low temperature coefficients.
  • The strings are converted to values using the str2double and by identifying E in the strings accordingly.
  • mkdir command is introduced to make the directory for all the values and plots determined
  • C_p, h and s are calculated using them as a separate function using the formulas mentioned initially.
  • The plots are created using the figure, plot command and highlighted using specific colors of the values determined and are consolidated into a single plot with subplot command.
  • The plots are saved using saveas command and cd to change the folder after each species plot is saved.
  • the molecular weight of the species is determined by calling the molecular weight function
  • The molecular weight function initiates with the definition of elements and it's corresponding atomic weight. The molecular weight is initiated as the value zero for further increments in the for loops. i is defined for the species names gathered from the data file and j for the elements defined. strcmpi command is used to compare the strings and is returned as 1 obtained from the species and the defined to calculate the atomic weight and is added in increments to MW. The values are saved in q which further called upon. Another if statement is used to calculate if more than one molecule of a species exists. fprintf command is used to display the values of the molecular weights.
  • For specific heat function, the input parameters such as the low, high temp coefficients, global mid temperature are gathered. The if statement is used to calculate the value for Cp for the coefficients less than and equal to the mid global temperature and else the high-temperature coefficients are used for the calculation.
  • For entropy function, The input parameters such as the low, high temp coefficients, global mid temperatures are gathered. The if statement is used to calculate the value for entropy for the coefficients less than and equal to the mid global temperature and else the high-temperature coefficients are used for the calculation.
  • For enthalpy function, the input parameters such as the low, high temp coefficients, global mid temperature are gathered. The if statement is used to calculate the value for h for the coefficients less than and equal to the mid global temperature and else the high-temperature coefficients are used for the calculation.

Result

  • plot Obtained for O2,N2,CO2

  • Screenshot of Auto generated folders

  • Workspace

  • Command Window

Conclusion

  1. Data parsing is done sucessfully from the given data file 'THERMO.dat'.
  2. plot of CO2,N2 and O2 are represent in the result
  3. generated all plots of specific heat,entropy,enthalpy with temperature each species automatically.
  4. molecular weights are calculated are represented at command window

Reference

  1. https://skill-lync.com/projects/Common-Problems-encountered-while-working-on-the-File-Parsing-Assignment-96902
  2. https://in.mathworks.com/matlabcentral/answers/223200-parsing-a-text-file-in-matlab-and-accessing-contents-of-each-sections
  3. https://in.mathworks.com/matlabcentral/answers/196582-best-way-to-parse-text-file
  4. https://www.youtube.com/watch?v=E56egH10RJA
  5. https://www.youtube.com/watch?v=PwGTISku_dg
  6. https://www.youtube.com/watch?v=FD3WiGNhCfI
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.