Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing NASA Thermodynamic Data

Aim: To write a code in Matlab to parse the NASA thermodynamic data file and then calculate the thermodynamic properties of various gas species.

Objective:

1. To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the NASA thermodynamic data file "Thermo.dat".

The NASA polynomials have the form as shown below:

Where R is the universal gas constant, T is the temperature

2. To calculate the molecular weight of each species and display it in the command window.

3. To plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.

4. To save the plots as images with appropriate names and dump them in separate folders for each species with suitable name automatically.


File Parsing:

File parsing refers to how information can be read from a particular text file or any type of file and how that information can be used in a computer program.

Step 1 - Define the functionStep 2 - Create an InputParser object Step 3 - Add inputs to the scheme.Step 4-Set properties to adjust parsing.

Step 5 - Parse the inputs. Step 6- Use the inputs in function.Step 7 - Calling function. NASA Thermodynamic File

THERMO.dat file is a data file containing 14 coefficients for 53 different elements given by NASA. These coefficients are utilized to calculate the specific heat, enthalpy & entropy for a particular element. Each element has its own range of local temperature. Below screenshot of the THERMO.dat file is displayed.

List of 53 Elements contained in THERMO.dat' file:

AR C C2H C2H2 C2H3 C2H4

C2H5 C2H6 C3H7 C3H8 CH CH2

CH2(S) CH2CHO CH2CO CH20 CH2OH CH3

CH3CHO CH3O CH3OH CH4 CN CO

CO2 H H2 H2CN H20 H2O2

HCCO CCOH HCN HCNN HCNO HCO

HNCO HNO HO2 HOCN N N2

N20 NCO NH NH2 NH3 NNH

NO NO2 O O2 OH

Enthalpy, Entropy, and Specific Heat:

Enthalpy, entropy, and specific heat, of various species values at different temperatures are required extensively to analyze the thermodynamic
process.

For example, if the combustion process is considered during this, these quantities are required to observe how the combustion is going on and
how efficient it will be. It eventually allows seeking the scope of improvement in the process. Similarly in the other processes like refrigeration,
air conditioning, chemical kinetics, nozzle flow, detonation process, etc, these quantities are widely involved to observe the phenomenon.

NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp. H. and S. They have documented the
coefficients that are required to evaluate these polynomials for 1000+ species.

Where R is the universal gas constant, T is the temperature and al, a2. a3. a4, a5, a6, and a7 are the numerical coefficients supplied in the thermodynamic file. The FIRST 7 coefficients are HIGH-temperature coefficients (above 1000 K) and the SECOND 7 coefficients are LOW temperature coefficients (below 1000 K).

Coding Functions:

1. Specific Heat:

It is the amount of energy that must be added, in the form of heat, to one unit of mass of the substance in order to cause an increase of one unit in its temperature.

Cp =( a1 + a2 T+ a3 T^2 + a4 T^3+a5 T^4)*R

Function for Specific heat

% Specific heat calculation function

function Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp)
% Comparing with global temperature
% If less than global tempearture - minimum coefficients are taken
if Temp <= global_medium_temp
    Cp = R*(a8 + (a9*Temp) + (a10*(Temp).^2) + (a11*(temp).^3) + (a12*(Temp).^4));
else
    Cp = R*(a1 + (a2*Temp) + (a3*(Temp).^2) + (a4*(temp).^3) + (a5*(Temp).^4));
    % Else maximum coefficients are taken
end

2. Entropy:

Entropy, the measure of a systems thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system. The concept of entropy provides deep insight into the direction of spontaneous change for many everyday phenomena.

S =( a1 InT+ a2 T +a3 T^2/2+a4 T^3/3+a5 T^4/4 + a7)*R

Function for Entropy :

% Entropy Calculation function

function S = Entropy_calc(a1,a2,a3,a4a,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp)
% Comparing with global temperature
% If less than global tempearture - minimum coefficients are taken

if Temp <= global_medium_temp
    S = R.*(a8.*log(temp) + (a9.*temp) + (a10*((Temp).^2)/2) + (a11*((Temp).^3)/3) + (a12*((Temp).^4)/4) + a14);
else
    % Else - maximum coefficients are taken
    S = R.*(a1.*log(temp) + (a2.*temp) + (a3*((Temp).^2)/2) + (a4*((Temp).^3)/3) + (a5*((Temp).^4)/4) + a7);
end

3. Enthalpy:

Enthalpy, a property of a thermodynamic system, is equal to the system's internal energy plus the product of its pressure and volume. In a system enclosed so as to prevent the mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.


H = (a1 + a2 T/2+a3 T^2/3+ a4 T^3/4 +a5 T^4/5 + a6/T)*R*T

Funtion for Enthalpy :

% Enthalpy Calculation function

function H = Enthalpy(a1,a2,a3,a4a,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp)
% Comparing with global temperature
% If less than global tempearture - minimum coefficients are taken

if Temp <= global_medium_temp
    H = R*Temp.*(a8 + ((a9*temp)/2) + ((a10*(Temp).^2)/3) + ((a11*(Temp).^3)/4) + ((a12*(Temp).^4)/5) + ((a13./Temp)));
else
    % Else maximum coefficients are taken
    
     H = R*Temp.*(a1 + ((a2*temp)/2) + ((a3*(Temp).^2)/3) + ((a4*(Temp).^3)/4) + ((a5*(Temp).^4)/5) + ((a6./Temp)));
end

4. Molecular Weight:

The molecular mass is the mass of a given molecule, its is measured in daltons. Different molecules of the same compound may have different molecular masses because they contain different isotopes of an element.

Function for Molecular Weight is included in the Main Code

MAIN CODE :

clear all
close all
clc

%getting the data file
f1=fopen('THERMO.dat','r');
fgetl(f1)
%reading the header information
A=fgetl(f1);
A=strsplit(A,' ');
global_low_temp=str2num(A{2});
global_mid_temp=str2num(A{3});
global_high_temp=str2num(A{4});

%skipping next three lines
for i=1:3
    fgetl(f1);
end

%reading the species
for i=1:53
    
    header=fgetl(f1);
    x=strsplit(header,' ');
    
    species_name=(x{1});
    z=length(x);
    
    local_low_temp=str2double(x{z-3});
        local_high_temp=str2double(x{z-2});
            local_mid_temp=str2double(x{z-1});
            
            t1=local_low_temp;
            t2=local_mid_temp;
            t3=local_high_temp;
            
            %getting first line coeffecients (a1-a5)
            line_1=fgetl(f1);
            
            a=strfind(line_1,'E');
            a1=str2double(line_1(1:a(1)+3));
            a2=str2double(line_1(a(1)+4:a(2)+3));
            a3=str2double(line_1(a(2)+4:a(3)+3));
            a4=str2double(line_1(a(3)+4:a(4)+3));
            a5=str2double(line_1(a(4)+4:a(5)+3));
            
            %getting second line coeffeceints (a6-a10)
             line_2=fgetl(f1);
            
            a_1=strfind(line_2,'E');
            a6=str2num(line_2(1:a_1(1)+3));
            a7=str2double(line_2(a_1(1)+4:a_1(2)+3));
            a8=str2double(line_2(a_1(2)+4:a_1(3)+3));
            a9=str2double(line_2(a_1(3)+4:a_1(4)+3));
            a10=str2double(line_2(a_1(4)+4:a_1(5)+3));
            
            %getting third line coeffeceints (a10-a14)
             line_3=fgetl(f1);
            
            a_2=strfind(line_1,'E');
            a11=str2double(line_3(1:a_2(1)+3));
            a12=str2double(line_3(a_2(1)+4:a_1(2)+3));
            a13=str2double(line_3(a_2(2)+4:a_1(3)+3));
            a14=str2double(line_3(a_2(3)+4:a_1(4)+3));
            
            
            %creating folder
            mkdir(['C:\Users\MANOJ\Desktop\file parsing\images\',species_name]);
         
            T=linspace(t1,t3,1000);
            R=8.314;
            % to get array of specific heat, enthalpy and entropy 
            for k=1:length(T)
    % to compare the species  temperature to assign the coeffeceints
    %a1-a7 (high temperature coeffeceints), a8-a14(low temperature coeffecients)    
    if T(k)>t2
        
        %high temperature coeffeceints.
        
        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))*R.*T;
        
        S=a1.*log(T)+a2.*T+(a3.*T.^2)/2+(a4.*T.^3)/3+(a5.*T.^4)/4+a7;
        
    else
        %low temperature coeffecients
        
        cp=(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
        
        H=(a8+(a9.*T)/2+(a10.*T.^2)/3+(a11.*T.^3)/4+(a12.*T.^4)/5+(a13./T))*R.*T;
        
        S=a8.*log(T)+a9.*T+(a10.*T.^2)/2+(a11.*T.^3)/3+(a12.*T.^4)/4+a14;
    end
    end
    

            
            %plotting the graphs
            
            figure(1);
            plot(T,cp,'linewidth',3,'color','b');
            grid on ;
            title(species_name);
            xlabel('Temperature');
            ylabel('Specific Heat');
            
            
            figure(2);
            plot(T,H,'linewidth',3,'color','r');
            grid on ;
            title(species_name);
            xlabel('Temperature');
            ylabel('Enthalpy');
            
            
            figure(3);
            plot(T,S,'linewidth',3,'color','g');
            grid on ;
            title(species_name);
            xlabel('Temperature');
            ylabel('Entropy');
            
            
            
            %changing the directory and saving the file
            saving=(['C:\Users\MANOJ\Desktop\file parsing\images\',species_name]);
              cd(saving);

            saveas(1,'specific heat','jpg');
            saveas(2,'enthalpy','jpg');
            saveas(3,'entropy','jpg');
            
            
             %molecular_masscalculation
             
             

elements=['H','C','O','N','A'];
atomic_weight=[1.0079 12.0107 15.999 14.0067 39.948];
mol_mass=0;
for l=1:length(species_name)
    for m=1:length(atomic_weight)
        if strcmp(species_name(l),elements(m))
            mol_mass=mol_mass+atomic_weight(m);
            position=m;
        end
    end
 
n=str2double(species_name(l));
if n>1
    mol_mass=mol_mass+atomic_weight(position)*(n-1);
    
end
end
sprintf('Molecular Weight of %s:%f u',species_name,mol_mass)

             

end             
             fclose(f1)

Code Explanation:

1. 'clear all' - clear all the variables in the workspace.

"close all' - closes all the plots and figures and

'clc' - clears the command window.

2. Making a directory using 'mkdir' with location address.

3. After that, reading the text file 'Thermo.dat' file and opening it by using 'fopen' and 'r' as reading command.

4. For reading the line in a text file using "fgetl'.

5. To obtain the temperature range from the Thermo.dat file, using 'strsplit' to split the temperature into a particular string cell. 6. Used 'str2num' for converting string cell to numbers.

7. Divided the temperature into a string and then converted to a number so that temperature values can be used separately to solve the

NASA thermodynamics.

8. Using 'for & end' loops to skip the next three comment lines in the text file.

9. Reading all the 53 species to plot specific heat(cp), enthalpy (H), and entropy(S), with temperature values, and to find the molecular weights.

10. Used 'for' loop to read 53 species.

11. Used 'strfind' to find string.

12. Using 'linspace' specified the temperature range from low to high.

13. Adding universal gas constant value 'R.

14. Extracted higher and lower temperature coefficient by reading line by line from the species.

15. Calling specific heat, enthalpy, entropy function codes.

16. Calculating Molecular weight.


Syntax Used during Coding :

Fopen: Open a file or obtain information about open files

'r': Open the file for reading

fgetl Read line from file

strsplit Split string or character vector at specified delimiter

str2num Convert character array or string to numeric array

str2double Converts strings to double precision values

strfind Find one string within another

linspace Generate linearly spaced vectors

fprintf Write formatted data to file

pwd Identify current directory

mkdir Make new directory

cd Change working directory

ATTACHMENT :

Screenshot of the window where all the folders are saved:

Results:

Specfic Heat, Entropy and Enthalpy plots for O2, N2 and Co2 species:

1. Plots for O2

2. Plots for N2

3. Plots for Co2

OUTPUT :

CONCLUSION:

Using MATLAB code, the necessary coefficients required to evaluate various thermodynamics parameters for different species are extracted from NASA thermodynamic data file. The specific heat, enthalpy and entropy for all the species are calculated and plotted against the global temperature range. The molecular weight is calculated for all the species.

GOOGLE DRIVE LINK :

https://drive.google.com/drive/folders/1a3VAoW8Qz6AY-M0FghaVj0A3d-WNkxTa?usp=sharing

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.