Challenge

Project 1 - Parsing NASA thermodynamic data

PROJECT1 – PARSENING NASA THERMODYNAMIC DATA

AIM: To write a MATLAB program for parsening NASA thermodynamic data.

Objectives:

  • To write a function that extract the 14 coefficient and calculate enthalpy, entropy and specific heat in the data file for all species.
  • To calculate molecular weight of each species.
  • To plot the graph for enthalpy, entropy and specific heat for local temperature range specific for all species.
  • To write detail report for describing coding logic.

Given Data:

  • NASA thermodynamic data file (THERMO.dat).
  • Formulae to calculate enthalpy (H), entropy(S) and specific heat (Cp).

Cp/R = a1 + a2T + a3T^2 + a4T^3 + a5T^4.

H/RT = a1 + a2T /2+ a3T^2/3 + a4T^3/4 + a5T^4/5 + a6/T.

S/R = a1 ln T + a2T + a3 T^2/2 + a4 T^3/3 + a5T^4/4 + a7.

Here, R = Universal gas constant, T = Temperature.

Solution:

Theory:

NASA Thermodynamic Data:

NASA thermodynamic data is a collection of thermodynamic data and models developed by NASA for use in aerospace engineering applications. The data includes thermodynamic properties such as enthalpy, entropy and specific heat as well as transport properties such as viscosity and the conductivity.

The NASA thermodynamic data is widely used in aerospace industry for designing and analyzing spacecraft, rocket engines and other aerospace systems. It is also used in other field such as chemical engineering and material science where thermodynamic data is required for modeling and simulation.

Thermodynamic properties

Enthalpy:

It is a thermodynamic property that describes the total energy at a constant pressure. It is denoted by symbol H and is defined as the sum of the internal energy of the system and product of the pressure and volume of the system.

It is used to describe the heat content of the system, and changes in enthalpy can be used to calculate the amount of heat absorbed or released during a chemical or physical process.

Entropy:

It is a measure of the disorder or randomness of a system. It is a thermodynamic property denoted by the symbol S, and is defined as the amount of energy that is unavailable to do work in a system at a given temperature.

Entropy can be calculated using statistical mechanics, which describes the behavior of a large number of particles based on their individual properties. It is often used to predict the direction and extent of chemical reactions and physical processes.

Specific Heat:

Specific heat, also known as specific heat capacity, is a thermodynamic property that describes the amount of heat required to raise the temperature of a unit mass of a substance by one degree Celsius or one Kelvin. The specific heat of a substance is dependent on its molecular structure and can vary depending on temperature and pressure. Substances with a higher specific heat require more energy to raise their temperature than those with a lower specific heat.

There are two types of specific heat: the specific heat at constant pressure (cp) and the specific heat at constant volume (cv). The specific heat at constant pressure is the amount of heat required to raise the temperature of a unit mass of a substance while keeping the pressure constant, while the specific heat at constant volume is the amount of heat required to raise the temperature of a unit mass of a substance while keeping the volume constant.

Report on programming Logic:

Logic for extracting coefficients and calculating enthalpy, entropy and specific heats:

  • Open the "THERMO.dat" file using the fopen () function.
  • Read each line of the file using the fgetl () function.
  • Check if the line contains the keyword "NASA" to identify the species data.
  • Extract the 14 coefficients from the line and store them in an array.
  • Calculate the enthalpy, entropy and specific heats for the species using the given formulae.
  • Store the calculated values in a suitable data structure, such as a struct or cell array.

Logic for calculating molecular weight:

  • Read the molecular weight of each species from the "THERMO.dat" file.
  • Store the molecular weight in a suitable data structure, such as a struct or cell array.

Logic for plotting and saving the Cp, enthalpy and entropy graphs:

  • For each species, plot the Cp, enthalpy and entropy graphs using the calculated values and the local temperature range.
  • Save each graph with a suitable name in a separate folder for the species.

Logic for the main program:

  • Read the "THERMO.dat" file using the fopen () function.
  • Call the function that extracts the coefficients and calculates the enthalpy, entropy and specific heats for all species.
  • Call the function that calculates the molecular weight for each species.
  • Call the function that plots and saves the Cp, enthalpy and entropy graphs for each species.

Programs explanation with comments

Function for calculating Enthalpy

%Funtion for the enthalpy
function [H] = enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,temp,gb_m,r)
%Checking for the temperaure

%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=gb_m) % comparing matrix values of the temperature with the global medium temperature
    H = r*temp.*(x8 + ((x9*temp)/2) + ((x10*(temp).^2)/3) + ((x11*(temp).^3)/4) + ((x12*(temp).^4)/5) + ((x13./temp))); % calculation of the enthalpy wrt to the equations
else  % else statement to perform the task if the if statements fail
  
    H = r*temp.*(x1 + ((x2*temp)/2) + ((x3*(temp).^2)/3) + ((x4*(temp).^3)/4) + ((x5*(temp).^4)/5) + ((x6./temp))); % calculation of the enthalpy wrt to the equations
end % ending if statement

Function for calculating Entropy

%Funtion for the entropy
function [S] = entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,temp,gb_m,r)
%Checking for the temperaure

%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=gb_m) % comparing matrix values of the temperature with the global medium temperature
    S = r.*(x8.*log(temp) +(x9.*temp) +(x10*((temp).^2)/2) + (x11*((temp).^3)/3) + (x12*((temp).^4)/4) + (x14)); % calculation of the entropy wrt to the equations
    
else % else statement to perform the task if the if statements fail
    S = r.*(x1.*log(temp) +(x2.*temp) +(x3*((temp).^2)/2) + (x4*((temp).^3)/3) + (x5*((temp).^4)/4) + (x7)); % calculation of the entropy wrt to the equations
    
end % ending if statement

Function for calculating Specific Heat

%Funtion for the Specific Heat
function [Cp] = Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,temp,gb_m,r)
%Checking for the temperaure
%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients
if(temp<=gb_m) % comparing matrix values of the temperature with the global medium temperature
   Cp = r.*(x8 +(x9.*temp) +(x10*(temp).^2) + (x11*(temp).^3) + (x12*(temp).^4)); % calculation of the Specific heat wrt to the equations
    
else % else statement to perform the task if the if statements fail
     Cp = r.*(x1 +(x2.*temp) +(x3*(temp).^2) + (x4*(temp).^3) + (x5*(temp).^4)); % calculation of the Specific heat wrt to the equations
    
 
end % ending if statement

Function for calculating Molecular weight

%function calculate molecular mass
function mw = mol_wt(sp_name)
%storing value
compound_name = (sp_name);
% creating an array of elements
key_at = ['C','H','O','N','A'];
%storing the atomic mss value in the array
value_at = [12.0107 1.00794 15.994 14.0067 39.948];

%asssigning molecular mass as 0
mw =0 ;
%creating for loop to perform the task
for i = 1:length(compound_name)
    for j = 1:length(key_at)
        %comparing the string value 
        if strcmp(compound_name(i),key_at(j))
            %if above comparision is true value of molecular mass is
            %calculated
            mw =mw + value_at(j);
            %storing the position for further use
            pos = j;
          
        end
    end
    %converting the srting to number
    k = str2num(compound_name(i));
    %comparing if the value of k is greater than executed the further statements 
    if(k>1)
        %molecular mass calculation
        mw = mw + value_at(pos)*(k-1);
    end
end
%command to print the molecular masss of the molecule
fprintf('Molecular Weight of the species %s is %f',compound_name ,mw)
fprintf('\n');
end

Main Program

%File Parsing for a file
clear all % clearing editing and workspace
close all  % closing all output windows
clc % clearing command window

f1 = fopen('THERMO.dat','r'); %opening the THERMO.dat from the directory
first_line = fgetl(f1); % reading file line by line

%R is the universal gas constant J/(mol-K)

R = 8.314;
temp = fgetl(f1);% to save the line in temp variable

%Reading text from a header file
%Reading the global temperature from the next line

A = strsplit(temp,' '); % Splitting the line saved in temp variable when space is found

%Cell array is generated 1x4 cell array
glob_lowtemp = str2num(A{2}); % Assigning the global lower temperature
glob_midtemp = str2num(A{3}); % Assigning the global middle temperature
glob_hightemp =str2num(A{4}); % Assigning the global high temperature

% Generating the 1000 values between the range of temperature
Temperature = linspace(glob_lowtemp,glob_hightemp,1000);

for j =1:3; %skipping the three comment lines by running loop
    k =fgetl(f1); % Reading the comment line
end % ending th loop

for i = 1:53; %running for loop fopr 53 times as there are 53 species

    %Parsing of the species block
        tline = fgetl(f1); % rading the first line 
        S =strsplit(tline,' '); % splitting the string when the space is found by the command
        Species_name =S{1}; % extracting the name of the molecule

        %Extracting the coefficients from first line
        line2 =fgetl(f1);  % reading the first line of the coefficients
        x =findstr(line2,'E'); %  finding the string using the command

        %Finding the 5 coefficient
        x1 = line2(1:x(1)+3); % Extracting the 1st coefficients from the line
        x1 = str2num(x1); % Converting the string coefficients extracted above to number

        x2 = line2(x(1)+4:x(2)+3); % Extracting the 2nd coefficients from the  line
        x2 = str2num(x2); % Converting the string coefficients extracted above to number

        x3 = line2(x(2)+4:x(3)+3); % Extracting the 3rd coefficients from the  line
        x3 = str2num(x3);
        
        x4 = line2(x(3)+4:x(4)+3); % Converting the string coefficients extracted above to number
        x4 = str2num(x4);

        x5 = line2(x(4)+4:x(5)+3); % Extracting the 4th coefficients from the line
        x5 = str2num(x5); % Converting the string coefficients extracted above to number

        %line3 5-coefficients
        line3 = fgetl(f1); % Reading the line of the coefficients
        x = findstr(line3,'E'); % finding the string using the command

        x6 = line3(1:x(1)+3); % Extracting the 6th coefficients from the line
        x6 = str2num(x6); % Converting the string coefficients extracted above to number

        x7 = line3(x(1)+4:x(2)+3); % Extracting the 7th coefficients from the line
        x7 = str2num(x7); % converting the string coefficients extracted above to number

        x8 = line3(x(2)+4:x(3)+3); % Extracting the 8th coefficients from the line
        x8 = str2num(x8); % converting the string coefficients extracted above to number

        x9 = line3(x(3)+4:x(4)+3); % Extracting the 9th coefficients from the line
        x9 = str2num(x9); % Converting the string coefficients extracted above to number

        x10 = line3(x(4)+4:x(5)+3); % Extracting the 10th coefficients from the line
        x10 = str2num(x10); % converting the string coefficients extracted above to number

        %Line4  4-coefficients

        line4=fgetl(f1); % reading the line of the coefficients
        x = findstr(line4,'E'); %  finding the string using the command

        x11 = line4(1:x(1)+3); % Extracting the 11th coefficients from the line
        x11 = str2num(x11); % converting the string coefficients extracted above to number

        x12 = line4(x(1)+4:x(2)+3); % Extracting the 12th coefficients from the line
        x12 = str2num(x12); % converting the string coefficients extracted above to number

        x13 = line4(x(2)+4:x(3)+3); % Extracting the 13th coefficients from the line
        x13 = str2num(x13); % converting the string coefficients extracted above to number

        x14 = line4(x(3)+4:x(4)+3); % Extracting the 14th coefficients from the line
        x14 = str2num(x14); % converting the string coefficients extracted above to number

        % Calculating entropy(S) for 14 coefficient

        S = entropy_S(x1,x2,x3,x4,x5,x7,x8,x9,x10,x11,x12,x14,Temperature,glob_midtemp,R);

        % Calculating Specific Heat(Cp)
        
        Cp = Specific_heat(x1,x2,x3,x4,x5,x8,x9,x10,x11,x12,Temperature,glob_midtemp,R);

        % Calculating Enthlpy(H)

        H = enthalpy_H(x1,x2,x3,x4,x5,x6,x8,x9,x10,x11,x12,x13,Temperature,glob_midtemp,R);

        % Calculating molecular Weight of different elements

        mw(i) = mol_wt(Species_name);

        %Reading and writinh of the molecular wieght in the list of molecular wieght
        Moll_wt = fopen('Molecular Weight.txt','w');
        fprintf(Moll_wt,'\n');
        fprintf(Moll_wt,' Molecular Weight of the species %s is  %f ',Species_name,mw);
        
        fclose(Moll_wt);

        % Plotting on the graph for species fo the temperature and specific heat
        %Creating a folder for saving the species with its properties

        directory_curr = pwd; % pwd identify the current directory
        mkdir(Species_name); % making new directory
        cd(Species_name); % changing directory

        %Plotting the graph in different plotting window 
        %plot 1 for the temperature and entropy
        %plot 2 for the temperature and enthalpy
        %plot 3 for the temperature and specific heat
        figure(1) % opening the fingure window1
        plot(Temperature,H,'linewidth',3,'color','b'); % ploting Enthalphy Vs. Temperaure
        xlabel('Temperature in [K]'); % labeling x axis as temperature
        ylabel('Enthalphy [KJ/mol]'); % labeling y axis as enthapy
        grid on % making grids on
        title(sprintf('Variation in Enthalphy wrt Temperature %s',Species_name));

        saveas (figure(1),'Enthalhy.jpg'); % saving as the figure1 as Enthalpy.jpg
         %plot 2 Entropy and Temperature
        figure(2) % opening the fingure window2 
        plot(Temperature,S,'linewidth',3,'color','r'); % ploting Entropy Vs. Temperaure
        xlabel('Temperature in [K]'); % labeling x axis as temperature
        ylabel('Entropy [KJ/mol-k]'); % labeling y axis as entropy
        grid on % making grids on
        title(sprintf('Variation in Entropy wrt Temperature %s',Species_name)); % giving title to plot

        saveas (figure(2),'Entropy.jpg'); % Saving as the figure2 as Entropy.jpg        

        figure(3) % opening the fingure window3
        plot(Temperature,Cp,'linewidth',3,'color','g'); %ploting Specific Heat Vs. Temperature
        xlabel('Temperature in [K]'); % labeling x axis as temperature
        ylabel('Specific Heat [KJ]'); % labeling y axis as entropy
        grid on % making grids on
        title(sprintf('Variation in Specific Heat wrt Temperature %s',Species_name)); % giving title to plot

        saveas (figure(3),'Specific Heat.jpg'); % Saving as the figure2 as Specific heat.jpg

        cd(directory_curr); % changing the directory

end    % ending for loop

Program outputs

Snapshot of screen

Command Windows

Workspace Window

Screenshot of the window where all the folders are saved

Plots for O2

Plots for N2

Plots for CO2

Error faced while programming

1. Error found due to incoccect name of the function which was then corrcted from fget(f1) to fgetl(f1)

2. Error found due to incoccect name of the function which was then corrcted from strsplit1() to strsplit().

3. Error occured due to missing % symbols used while writing comment

4. Error ocured due to incorrcte function name which then corrected from fOpen to fopen

5. Error occured due to space between command save as which then corrected to saveas.

References

https://in.mathworks.com/help/matlab/matlab_prog/parse-function-inputs.html

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

https://www.mathworks.com/matlabcentral/answers/223200-parsing-a-text-file-in-matlab-and-accessing-contents-of-each-sections

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.