Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: Develop a MATLAB code to parse NASA thermodynamic data, calculate thermodynamic properties, and visualize the results.

Objective:

  1. Write a function to extract coefficients and calculate enthalpy, entropy, and specific heats for species in the NASA thermodynamic data file "Thermo.dat".

  2. Calculate molecular weights of species and display them.

  3. Plot specific heat, enthalpy, and entropy for each species within a local temperature range.

  4. Save plots as images in separate folders for each species.

File Parsing:

  1. Read and Parse Thermodynamic Data:

    Begin by reading the "Thermo.dat" file and extracting temperature coefficients for species. Define a function that takes species name as input and returns coefficients for specific heat, enthalpy, and entropy.

  2. Coefficients for Thermodynamic Properties:

    Extract the high-temperature and low-temperature coefficients from the data file. These coefficients will be used to calculate specific heat, enthalpy, and entropy using NASA polynomials.

Thermodynamic Property Calculation Functions:

  1. Specific Heat Calculation (SpecificHeat):

    Calculate specific heat using the NASA polynomials for given coefficients, temperature, and temperature range. Consider both high and low-temperature coefficients for accurate results.

  2. Enthalpy Calculation (EnthalpyCalc):

    Calculate enthalpy using the NASA polynomials. Similar to specific heat, account for both high and low-temperature coefficients.

  3. Entropy Calculation (EntropyCalc):

    Calculate entropy using the NASA polynomials with consideration for high and low-temperature coefficients.

  4. Molecular Weight Calculation (MolecularWeightCalc):

    Calculate the molecular weight of a species based on its elemental composition. Iterate through atomic symbols, match them with the species, and calculate the molecular weight.

Main Code:

  1. Reading Data and Setup:

    • Create output folders.
    • Read "Thermo.dat" file.
    • Extract temperature range and coefficients for each species.
  2. Thermodynamic Property Calculation and Analysis:

    • Iterate through each species.
    • Call functions to calculate specific heat, enthalpy, and entropy.
    • Calculate molecular weight.
    • Plot and save specific heat, enthalpy, and entropy graphs in respective species folders.

1. Specific Heat:

function Cp = specificHeat(a, Temp, R, globalMediumTemp)
    if Temp <= globalMediumTemp
        Cp = R * (a(6) + (a(7) * Temp) + (a(8) * Temp^2) + (a(9) * Temp^3) + (a(10) * Temp^4));
    else
        Cp = R * (a(1) + (a(2) * Temp) + (a(3) * Temp^2) + (a(4) * Temp^3) + (a(5) * Temp^4));
    end
end

2. Entropy:

function S = entropyCalc(a, Temp, R, globalMediumTemp)
    if Temp <= globalMediumTemp
        S = R * (a(6) * log(Temp) + (a(7) * Temp) + (a(8) * Temp^2 / 2) + (a(9) * Temp^3 / 3) + (a(10) * Temp^4 / 4) + a(12));
    else
        S = R * (a(1) * log(Temp) + (a(2) * Temp) + (a(3) * Temp^2 / 2) + (a(4) * Temp^3 / 3) + (a(5) * Temp^4 / 4) + a(7));
    end
end

3. Enthalpy:

function H = enthalpyCalc(a, Temp, R, globalMediumTemp)
    if Temp <= globalMediumTemp
        H = R * Temp * (a(6) + ((a(7) * Temp) / 2) + ((a(8) * Temp^2) / 3) + ((a(9) * Temp^3) / 4) + ((a(10) * Temp^4) / 5) + (a(11) / Temp));
    else
        H = R * Temp * (a(1) + ((a(2) * Temp) / 2) + ((a(3) * Temp^2) / 3) + ((a(4) * Temp^3) / 4) + ((a(5) * Temp^4) / 5) + (a(6) / Temp));
    end
end

4. Molecular Weight:

function molecularWeight = molecularWeightCalc(Species_SP)
    atomicName = {'H', 'C', 'O', 'N', 'A'};
    fprintf('Molecular Weight of %s : ', Species_SP);
    atomicWeight = [1.008, 12.011, 15.999, 14.007, 39.948];
    molecularWeight = 0;
    
    for i = 1:length(Species_SP)
        for j = 1:length(atomicName)
            if strcmpi(Species_SP(i), atomicName(j))
                molecularWeight = molecularWeight + atomicWeight(j);
                position = j;
            end
        end
        
        n = str2double(Species_SP(i));
        if n > 1
            molecularWeight = molecularWeight + atomicWeight(position) * (n - 1);
        end
    end
    
    fprintf('%d\n', molecularWeight);
end

MAIN CODE :

clear all
close all
clc

% Make directory
outputDir = 'C:\Users\Admin\Desktop\NASA File Parsing';
mkdir(outputDir);

%  Reading Data file - NASA Thermodynamic
% Open the file for reading
f1 = fopen('THERMO.dat', 'r');
gt = fgetl(f1);

% Obtaing Temperatures - by Reading Header Information
global_temp = fgetl(f1);

% Splitting
t = strsplit(global_temp);

% Converting string cells to numbers using str2double
global_low_temp = str2double(t{2});
global_medium_temp = str2double(t{3});
global_high_temp = str2double(t{4});

% Temperature range & Gas Constant
Temp = linspace(global_low_temp, global_high_temp, 1000);
R = 8.314;

% skipping three comment lines in the file
for i = 1:3
    gt = fgetl(f1);
end

for i = 1:53
    gt = fgetl(f1);
    A = strsplit(gt, ' ');
    Species_SP = A{1};

    line_1 = fgetl(f1);
    a = strfind(line_1, 'E');

    % Extracting higher and lower temperature coefficients
    coeffs = cell(1, 14);
    for j = 1:14
        if j == 14
            coeffs{j} = line_1(a(j-1)+4:end);
        else
            coeffs{j} = line_1(a(j)+3:a(j+1)+3);
        end
        coeffs{j} = str2double(coeffs{j});
    end

    line_2 = fgetl(f1);
    b = strfind(line_2, 'E');
    
    for j = 1:14
        if j == 14
            coeffs{j} = line_2(b(j-1)+4:end);
        else
            coeffs{j} = line_2(b(j)+3:b(j+1)+3);
        end
        coeffs{j} = str2double(coeffs{j});
    end

    line_3 = fgetl(f1);
    c = strfind(line_3, 'E');
    
    for j = 1:14
        coeffs{j} = line_3(c(j)+3:c(j+1)+3);
        coeffs{j} = str2double(coeffs{j});
    end

    % Specific heat calculation
    Cp = Specificheat(coeffs{1:5}, coeffs{8:10}, coeffs{11:14}, Temp, R, global_medium_temp);

    % Enthalpy calculation
    H = Enthalpy(coeffs{1:6}, coeffs{8:10}, coeffs{11:13}, Temp, R, global_medium_temp);

    % Entropy calculation
    S = Entropy_calc(coeffs{1:5}, coeffs{7}, coeffs{8:10}, coeffs{11:14}, Temp, R, global_medium_temp);

    �lculating molecular weight of species
    Molecularweight = Molecular_weight(Species_SP);

    % Read & Write Molecular list
    Molecular_list = fopen('Molecular weight.txt', 'a+');
    fprintf(Molecular_list, 'Molecular weight of %s is %d \n', Species_SP, Molecularweight);
    fclose(Molecular_list);

    % plotting(CP,H,S,Temp,Species_SP)
    % Saving as a folder
    cur_dir = pwd;
    mkdir(fullfile(outputDir, Species_SP));
    cd(fullfile(outputDir, Species_SP));

    % Plot 1, Specific heat (CP) vs Temperature Range (Temp)
    figure(1)
    plot(Temp, Cp, 'linewidth', 2, 'color', 'r');
    xlabel('Temperature [K]');
    ylabel('Specific Heat [kJ]');
    title(sprintf('Varying Specific Heat with Temperature Range of %s', Species_SP));
    grid on
    saveas(figure(1), 'Specific Heat.jpg');

    % Plot 2, Enthalpy (H) vs Temperature Range (Temp)
    figure(2)
    plot(Temp, H, 'linewidth', 2, 'color', 'g');
    xlabel('Temperature [K]');
    ylabel('Enthalpy in [kJ/(mol)]');
    title(sprintf('Varying Enthalpy with Temperature Range of %s', Species_SP));
    grid on
    saveas(figure(2), 'Enthalpy.jpg');

    % Plot 3, Entropy (S) vs Temperature Range (Temp)
    figure(3)
    plot(Temp, S, 'linewidth', 2, 'color', 'b');
    xlabel('Temperature [K]');
    ylabel('Entropy in [kJ/(mol-K)]');
    title(sprintf('Varying Entropy with Temperature Range of %s', Species_SP));
    grid on
    saveas(figure(3), 'Entropy.jpg');

    cd(cur_dir);

    % Molecular weight
    M = Molecular_weight(Species_SP);
    f2 = fopen(fullfile(outputDir, Species_SP, 'molecular mass.txt'), 'w');
    fprintf(f2, '%s\t%s\t%f\n', 'species', 'Molecular mass', M);
    fclose(f2);
end

EXPLANATION :

This MATLAB code is designed to read data from a NASA thermodynamic file (thermo.dat), process the information, perform calculations, and generate plots and output files for various thermodynamic properties of chemical species. Here's an explanation of each section and its purpose:

  1. Preparation:

    • clear all, close all, and clc are used to clear the workspace, close all figures, and clear the command window.
    • outputdir is the directory where output files will be stored.
    • A directory is created using mkdir to store the output files.
  2. Reading Header Information:

    • The script opens the NASA thermodynamic data file (THERMO.dat) for reading ('r' mode).
    • The first line (gt) is read and typically contains general information.
    • The second line (global_temp) contains temperature-related information.
  3. Converting Header Information:


    • This MATLAB code is designed to read data from a NASA thermodynamic file (Thermo dat), process the information, perform calculations, and generate plots and output files for various thermodynamic properties of chemical species. Here's an explanation of each section and its purpose:

      1. Preparation:

        • clear all , closs all , and clc are used to clear the workspace, close all figures, and clear the command window.
        • outputdir is the directory where output files will be stored.
        • A directory is created using mkdir to store the output files.
      2. Reading Header Information:

        • The script opens the NASA thermodynamic data file (THERMO.dat) for reading ('r'mode).
        • The first line (gt) is read and typically contains general information.
        • The second line (global_temp) contains temperature-related information.
      3. Converting Header Information:

        • The global_temp line is split using strsplit to extract low, medium, and high temperatures.
        • These temperature values are converted from strings to numbers using str2double
      4. Temperature Range & Constants:

        • A temperature range (temp) is generated using linspace
        • The gas constant R is assigned a value of 8.314.
      5. Skipping Comment Lines:

        • A loop (for i =1.3) reads and skips three comment lines in the file.
      6. Processing Data for Each Species:

        • A loop (for i=1.53) processes data for each species (assuming there are 53 species in the file).
        • The first line of species data (gt) is read.
        • The species name is extracted by splitting gt.
        • Three lines containing coefficients are read and processed.
      7. Extracting Coefficients:

        • Coefficients are extracted using a loop for each line 
        • Conditions are used to handle the last coefficient in each line.
        • The extracted coefficients are converted to double and stored in the cell array.
      8. Thermodynamic Calculations:

        • Specific heat (cp), enthalpy (H) and entropy (s) are calculated using appropriate functions 
        • These calculations use the extracted coefficients, temperature range, gas constant, and medium temperature.
      9. Molecular Weight Calculation:

        • The molecular weight of the species is calculated using the 

          This MATLAB code is designed to read data from a NASA thermodynamic file  process the information, perform calculations, and generate plots and output files for various thermodynamic properties of chemical species. Here's an explanation of each section and its purpose:

          1. Preparation:

            • clear all, close all , and clc are used to clear the workspace, close all figures, and clear the command window.
            • outputdir is the directory where output files will be stored.
            • A directory is created using mkdir to store the output files.
          2. Reading Header Information:

            • The script opens the NASA thermodynamic data file (THERMO.dat) for reading ('r' mode).
            • The first line (gt) is read and typically contains general information.
            • The second line (global_temp) contains temperature-related information.
          3. Converting Header Information:

            • The global_temp line is split using strsplit to extract low, medium, and high temperatures.
            • These temperature values are converted from strings to numbers using str2double
          4. Temperature Range & Constants:

            • A temperature range (Temp) is generated using linspace between global_low_temperature and global_high_temperature.
            • The gas constant R is assigned a value of 8.314.
          5. Skipping Comment Lines:

            • A loop (for i =1.3) reads and skips three comment lines in the file.
          6. Processing Data for Each Species:

            • A loop (for i =1.53) processes data for each species (assuming there are 53 species in the file).
            • The first line of species data (gt) is read.
            • The species name is extracted by splitting gt.
            • Three lines  containing coefficients are read and processed.
          7. Extracting Coefficients:

            • Coefficients are extracted using a loop (for j=1.4) for each line (line_1, line_2, line_3,).
            • Conditions are used to handle the last coefficient in each line.
            • The extracted coefficients are converted to double and stored in the coeffs cell array.
          8. Thermodynamic Calculations:

            • Specific heat (cp), enthalpy (h), and entropy (s) are calculated using appropriate functions (specific heat, enthalpy, entrp[yh_clc).
            • These calculations use the extracted coefficients, temperature range, gas constant, and medium temperature.
          9. Molecular Weight Calculation:

            • The molecular weight of the species is calculated using the molecular_weight function.
          10. Writing Molecular Weight to File:

            • The molecular weight is written to a file ('molecular weight.txt') along with the species name.
          11. Plotting and Saving Figures:

            • A new directory is created for each species using mkdir and cd.
            • Figures for specific heat, enthalpy, and entropy are plotted and saved as JPEG files.
          12. Returning to the Original Directory:

            • The script returns to the original directory after processing each species.
          13. Molecular Weight Output File:

            • The molecular weight is written to a file ('molecular mass txt ') inside the species directory.

          The code efficiently processes data from the NASA thermodynamic file, calculates various thermodynamic properties, generates plots, and organizes output files in a structured manner. It appears that the code is intended to be part of a larger system for analyzing and visualizing thermodynamic data for chemical species.

           function.
      10. Writing Molecular Weight to File:

        • The molecular weight is written to a file ('molecular weight.txt') along with the species name.
      11. Plotting and Saving Figures:

        • A new directory is created for each species using mkdir. and cd,
        • Figures for specific heat, enthalpy, and entropy are plotted and saved as JPEG files.
      12. Returning to the Original Directory:

        • The script returns to the original directory after processing each species.
      13. Molecular Weight Output File:

        • The molecular weight is written to a file ('molecular mass,txt') inside the species directory.

      The code efficiently processes data from the NASA thermodynamic file, calculates various thermodynamic properties, generates plots, and organizes output files in a structured manner. It appears that the code is intended to be part of a larger system for analyzing and visualizing thermodynamic data for chemical species.

       line is split using strsplit to extract low, medium, and high temperatures.
    • These temperature values are converted from strings to numbers using str2double.
  4. Temperature Range & Constants:

    • A temperature range (Temp) is generated using linspace between glabal_low_temp and global_high_temp.
    • The gas constant r is assigned a value of 8.314.
  5. Skipping Comment Lines:

    • A loop (for i =1.3) reads and skips three comment lines in the file.
  6. Processing Data for Each Species:

    • A loop (for i =1.53) processes data for each species (assuming there are 53 species in the file).
    • The first line of species data (gt) is read.
    • The species name is extracted by splitting gt.
    • Three lines containing coefficients are read and processed.
  7. Extracting Coefficients:

    • Coefficients are extracted using a loop (for j =1.14) for each line 
    • Conditions are used to handle the last coefficient in each line.
    • The extracted coefficients are converted to double and stored in the cell array.
  8. Thermodynamic Calculations:

    • Specific heat (cp), enthalpy (h), and entropy (s) are calculated using appropriate functions(specific heat , enthalpy , entropy , entropy_clc ) 
    • These calculations use the extracted coefficients, temperature range, gas constant, and medium temperature.
  9. Molecular Weight Calculation:

    • The molecular weight of the species is calculated using the molecular_weight  function.
  10. Writing Molecular Weight to File:

    • The molecular weight is written to a file('molecular weight .txt') along with the species name.
  11. Plotting and Saving Figures:

    • A new directory is created for each species using mkdir and cd.
    • Figures for specific heat, enthalpy, and entropy are plotted and saved as JPEG files.
  12. Returning to the Original Directory:

    • The script returns to the original directory after processing each species.
  13. Molecular Weight Output File:

    • The molecular weight is written to a file ('molecular mass.txt') inside the species directory.

The code efficiently processes data from the NASA thermodynamic file, calculates various thermodynamic properties, generates plots, and organizes output files in a structured manner. It appears that the code is intended to be part of a larger system for analyzing and visualizing thermodynamic data for chemical species.

Out put :

Molecular_weight AR
Molecular Weight of AR: 3.994800e+01
ans = 39.9480

Molecular_weight C
Molecular Weight of C: 1.201100e+01
ans = 12.0110

Molecular_weight C2H
Molecular Weight of C2H: 2.503000e+01
ans = 25.0300

Molecular_weight C2H2
Molecular Weight of C2H2: 2.603800e+01
ans = 26.0380

Molecular_weight C2H3
Molecular Weight of C2H3: 2.704600e+01
ans = 27.0460

Molecular_weight C2H4
Molecular Weight of C2H4: 2.805400e+01
ans = 28.0540

Molecular_weight C2H5
Molecular Weight of C2H5: 2.906200e+01
ans = 29.0620

Molecular_weight C2H6
Molecular Weight of C2H6: 3.007000e+01
ans = 30.0700

Molecular_weight C3H7
Molecular Weight of C3H7: 4.308900e+01
ans = 43.0890

Molecular_weight C3H8
Molecular Weight of C3H8: 4.409700e+01
ans = 44.0970

Molecular_weight CH
Molecular Weight of CH: 1.301900e+01
ans = 13.0190

Molecular_weight CH2
Molecular Weight of CH2: 1.402700e+01
ans = 14.0270

Molecular_weight CH2(S)
Molecular Weight of CH2(S): 1.402700e+01
ans = 14.0270

Molecular_weight CH2CHO
Molecular Weight of CH2CHO: 4.304500e+01
ans = 43.0450

Molecular_weight CH2CO
Molecular Weight of CH2CO: 4.203700e+01
ans = 42.0370

Molecular_weight CH2O
Molecular Weight of CH2O: 3.002600e+01
ans = 30.0260

Molecular_weight CH2OH
Molecular Weight of CH2OH: 3.103400e+01
ans = 31.0340

Molecular_weight CH3
Molecular Weight of CH3: 1.503500e+01
ans = 15.0350

Molecular_weight CH3CHO
Molecular Weight of CH3CHO: 4.405300e+01
ans = 44.0530

Molecular_weight CH3O
Molecular Weight of CH3O: 3.103400e+01
ans = 31.0340

Molecular_weight CH3OH
Molecular Weight of CH3OH: 3.204200e+01
ans = 32.0420

Molecular_weight CH4
Molecular Weight of CH4: 1.604300e+01
ans = 16.0430

Molecular_weight CN
Molecular Weight of CN: 2.601800e+01
ans = 26.0180

Molecular_weight CO
Molecular Weight of CO: 2.801000e+01
ans = 28.0100

Molecular_weight CO2
Molecular Weight of CO2: 4.400900e+01
ans = 44.0090

Molecular_weight H
Molecular Weight of H: 1.008000e+00
ans = 1.0080

Molecular_weight H2
Molecular Weight of H2: 2.016000e+00
ans = 2.0160

Molecular_weight H2CN
Molecular Weight of H2CN: 2.803400e+01
ans = 28.0340

Molecular_weight H2O
Molecular Weight of H2O: 1.801500e+01
ans = 18.0150

Molecular_weight H2O2
Molecular Weight of H2O2: 3.401400e+01
ans = 34.0140

Molecular_weight HCCO
Molecular Weight of HCCO: 4.102900e+01
ans = 41.0290

Molecular_weight HCCOH
Molecular Weight of HCCOH: 4.203700e+01
ans = 42.0370

Molecular_weight HCN
Molecular Weight of HCN: 2.702600e+01
ans = 27.0260

Molecular_weight HCNN
Molecular Weight of HCNN: 4.103300e+01
ans = 41.0330

Molecular_weight HCNO
Molecular Weight of HCNO: 4.302500e+01
ans = 43.0250

Molecular_weight HCO
Molecular Weight of HCO: 2.901800e+01
ans = 29.0180

Molecular_weight HNCO
Molecular Weight of HNCO: 4.302500e+01
ans = 43.0250

Molecular_weight HNO
Molecular Weight of HNO: 3.101400e+01
ans = 31.0140

Molecular_weight HO2
Molecular Weight of HO2: 3.300600e+01
ans = 33.0060

Molecular_weight HOCN
Molecular Weight of HOCN: 4.302500e+01
ans = 43.0250

Molecular_weight N
Molecular Weight of N: 1.400700e+01
ans = 14.0070

Molecular_weight N2
Molecular Weight of N2: 2.801400e+01
ans = 28.0140

Molecular_weight N2O
Molecular Weight of N2O: 4.401300e+01
ans = 44.0130

Molecular_weight NCO
Molecular Weight of NCO: 4.201700e+01
ans = 42.0170

Molecular_weight NH
Molecular Weight of NH: 1.501500e+01
ans = 15.0150

Molecular_weight NH2
Molecular Weight of NH2: 1.602300e+01
ans = 16.0230

Molecular_weight NH3
Molecular Weight of NH3: 1.703100e+01
ans = 17.0310

Molecular_weight NNH
Molecular Weight of NNH: 2.902200e+01
ans = 29.0220

Molecular_weight NO
Molecular Weight of NO: 3.000600e+01
ans = 30.0060

Molecular_weight NO2
Molecular Weight of NO2: 4.600500e+01
ans = 46.0050

Molecular_weight O
Molecular Weight of O: 1.599900e+01
ans = 15.9990

Molecular_weight O2
Molecular Weight of O2: 3.199800e+01
ans = 31.9980

Molecular_weight OH
Molecular Weight of OH: 1.700700e+01
ans = 17.0070

Attachment:            

Screenshot of the window where all the folders are saved. :

Results:

Specific Heat, Entropy and Enthalpy Plots for O2, N2 and Co2 species :

1. Plots for O2

a. Variation of Specific heat with the temperature of O2 

b. Variation of Entropy with the temperature of O2

c. Variation of Enthalpy with the temperature of O2

2. Plots for N2 :

a. Variation of Specific heat with the temperature of N2 

b. Variation of Entropy with temperature of N2

c. Variation of Enthalpy with the temperature of N2

3. Plots for Co2 :

a. Variation of Specific heat with the temperature of Co2 

            

b. Variation of Entropy with the temperature of CO2

             

c. Variation of Enthalpy with the temperature of CO2

              

  • Molecular Weight of all species : 

Conclusion:

By developing this MATLAB code, we can efficiently parse NASA thermodynamic data, calculate essential thermodynamic properties, and visualize the results through informative plots. This tool enables researchers and engineers to analyze and comprehend species-specific thermodynamic behavior and contributes to various fields such as combustion, chemical kinetics, and fluid dynamics.

References :

https://in.mathworks.com/help/matlab/ref/mkdir.html

https://in.mathworks.com/matlabcentral/answers/317006-create-new-folders-by-mkdir

https://in.mathworks.com/help/matlab/ref/saveas.html

https://in.mathworks.com/matlabcentral/answers/17580-how-to-define-a-path-in-saveas-command

https://in.mathworks.com/help/matlab/referencelist.html?type=function&category=printing-and-exporting

https://in.mathworks.com/help/matlab/ref/msgbox.html

https://in.mathworks.com/matlabcentral/answers/151692-display-value-in-message-box

https://in.mathworks.com/help/matlab/ref/fprintf.html

https://in.mathworks.com/help/matlab/ref/sprintf.html

http://combustion.berkeley.edu/gri-mech/data/nasa_plnm.html

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.