Challenge

Parsing NASA thermodynamic data Using MATLAB Program

Objective: To Parsing NASA Thermodynamic Data file and to calculate the Enthalpy(H), Entropy(S), Specific Hea(Cp), and Molecular Weight of the Species.

Introduction: Nasa Thermodynamic data file has the coefficient that is required to calculate the thermodynamic properties such as Cp, H, S. We are going to extract the necessary information from the file to calculate the properties of the Species and Plot them This Process of Extracting information form the File is Called File Parsing.

File - THERMO.DAT

  • The first line is “THERMO” is the file name
  • And the next line “e” is the Global Temperature range

    [Low Temperature Medium Temperature High Temperature] = [300 1000 5000]

  • The next three line starts with an exclamation mark which is the command out of the line that can be ignored in data extraction.

  • The 6th line starts with species name -“a”

  • The “b” indicates the physical state of the species (G – Gases, S- Solid, L-Liquid)

  • The “c” is Species Temperature range

    [Low-Temperature Medium Temperature High Temperature] = [250 3500 1000]

  • The next three lines are the coefficient for calculating Cp, H, S –“d”

Formula:

We are going to calculate the Enthalpy(H), Entropy(S), Specific Hea(Cp), for 53 Species.

1)The Equation for Specific Heat (Cp) - `C_p =(A_1 + A_2*T + A_3*T^2 + A_4*T^3 + A_5*T^4)*R`

2)The Equation for Enthalpy(H) - `H= (A_1 + (A_2*T)/2 + (A_3*T^2)/3 + (A_4*T^3)/4 + (A_5*T^4)/5 + A_6/T)* (R*T)`

3)The Equation of Entropy(S)- `S = (A_1*lnT + A_2*T + (A_3*T^2)/2 + (A_4*T^3)/3 + (A_5*T^4)/4 + A_7)*R`

R- Universal Gas Constant

T- Temperature

A1, A2, A3…-Coefficient

Function Program to calculate Cp, H, S:

%Function to Calculate Cp,Enthalpy, Entropy
function [Cp,H,S] = Property(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,T,Species_Mid_Temp)
%Cp = Specific Heat Of species at Contant Pressure 
%H = Enthalpy
%S = Entropy 
%T = Temperature Range 
%Species_Mid_Temp = Species Mid Temperature 
%R = Universal Gas Constant
R = 8.314;
for q = 1:length(T)
    if T(q) >= Species_Mid_Temp
        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)).*(T.*R);
        S = ((A1.*log(T))+(A2.*T)+(A3.*T.^2)+(A4.*T.^3)+(A5.*T.^4)+A7)*R;
     
    else
        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)).*(T.*R);
        S = ((A8.*log(T))+(A9.*T)+(A10.*T.^2)+(A11.*T.^3)+(A12.*T.^4)+A14)*R;
    end
end

The A1-A7 coefficients are HIGH-temperature coefficients and the A8-A14 coefficients are LOW-temperature coefficients.

The Function Program to calculate the Molecular Weight of Species:

function [molecular_mass]= mol_mass(Species)

element = {'H','C','N','O','A'};
Atomic_weight = [1.0078 12.0107 14.0067 15.9949 39.9480];

molecular_mass = 0;
for i = 1:length(Species)
    for j = 1:length(element)
        if strcmp(Species(i),element(j))
            molecular_mass = molecular_mass + Atomic_weight(j);
            c = j;
        end
    end
    n = str2double(Species(i));
    if n > 1
        molecular_mass = molecular_mass + Atomic_weight(c)*(n-1);
    end
end

Main Program for File Parsing:

The command used in the main program:

  • fileID = fopen(filename,permission)

fopen is the commans used to open the file with the type of access specified by permission .the permission can be specified as follow.

'r'

Open file for reading.

'w'

Open or create new file for writing. Discard existing contents, if any.

'a'

Open or create new file for writing. Append data to the end of the file.

'r+'

Open file for reading and writing.

'w+'

Open or create new file for reading and writing. Discard existing contents, if any.

'a+'

Open or create new file for reading and writing. Append data to the end of the file.

'A'

Open file for appending without automatic flushing of the current output buffer.

'W'

Open file for writing without automatic flushing of the current output buffer.

  • tline = fgetl(fileID)

fgetl returns the next line of the specified file, removing the newline characters.

  • C = strsplit(str,delimiter) splits str at the delimiters specified by delimiter.

If str has consecutive delimiters, with no other characters between them, then strsplit treats them as one delimiter

  • k = strfind(str,pattern)

It searches str for occurrences of pattern. The output, k, indicates the starting index of each occurrence of pattern in str. If a pattern is not found, then strfind returns an empty array, []. The strfind function executes a case-sensitive search.

  • X = str2num(chr)

Convert character array or string to numeric array

  • Sprint(‘title%s %d’,string,number)

Sprint means “String print formatted”.used this for creating a MATLAB string

  • mkdir(folder name)

To create a folder in the same directory

  • cd(folder name )

To specify the files should be stored in a specific folder

  • gca – return a handle to the current axes object
  • saveas(figure,file-name)To save figure (gca can be should mention the current plot )as a file.

Main Program:

clc 
close all
clear all

%Parasing the Data file 
Data_File = fopen('THERMO.DAT','r');
Data_File_name = fgetl(Data_File);
First_line = fgetl(Data_File);

%Global Temperature Range
Global_Temp = strsplit(First_line,' ');
%converting strings to number
Global_Low_Temp = str2num(Global_Temp{2});
Global_Mid_Temp = str2num(Global_Temp{3});
Global_High_Temp = str2num(Global_Temp{4});

%Read the Next Three Lines 
x1 = fgetl(Data_File);
x2 = fgetl(Data_File);    
x3 = fgetl(Data_File);

for j=1:212
    Line_1 = fgetl(Data_File);
    L1 = strsplit(Line_1,' ');
    Species = (L1{1});
    
    %Getting Species Temperature Range and converting from string to numbers
    Species_Low_Temp = str2num(L1{1,(length(L1))-3});
    Species_Mid_Temp = str2num(L1{1,(length(L1))-2});
    Species_High_Temp = str2num(L1{1,(length(L1))-1});
    T = linspace(Species_Low_Temp,Species_High_Temp,500); 
    
    Line_2 = fgetl(Data_File);
    L2 = strfind(Line_2,'E');
    %Extraction of Co-efficient from Line 2
    A1 = str2num(Line_2(1:L2(1)+3));
    A2 = str2num(Line_2(L2(1)+4:L2(2)+3));
    A3 = str2num(Line_2(L2(2)+4:L2(3)+3));
    A4 = str2num(Line_2(L2(3)+4:L2(4)+3));
    A5 = str2num(Line_2(L2(4)+4:L2(5)+3));
    
    Line_3 = fgetl(Data_File);
    L3 = strfind(Line_3,'E');
    %Extraction of Co-efficient from Line 2
    A6 = str2num(Line_3(1:L3(1)+3));
    A7 = str2num(Line_3(L3(1)+4:L3(2)+3));
    A8 = str2num(Line_3(L3(2)+4:L3(3)+3));
    A9 = str2num(Line_3(L3(3)+4:L3(4)+3));
    A10 = str2num(Line_3(L3(4)+4:L3(5)+3));
    
    Line_4 = fgetl(Data_File);
    L4 = strfind(Line_4,'E');
    %Extraction of Co-efficient from Line 2
    A11 = str2num(Line_4(1:L4(1)+3));
    A12 = str2num(Line_4(L4(1)+4:L4(2)+3));
    A13 = str2num(Line_4(L4(2)+4:L4(3)+3));
    A14 = str2num(Line_4(L4(3)+4:L4(4)+3));
   
   
    
%Funtion To Calculate Cp,Enthalpy,Entropy
[Cp,H,S] = Property(A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,T,Global_Mid_Temp);

%TO create he folder for each specimen 
Folder_name = sprintf('%d) [%s]',j, Species);
current_dir = pwd;
mkdir(Folder_name);
cd(Folder_name);

figure(1);
plot(T,Cp,'r','linewidth',3)
grid on 
title(['Specific Heat: ',Species])
xlabel('Species Temperature[K]');
ylabel('Specific Heat[J/Kg/K]')
IMAGE1 = sprintf('Cp %s.png',Species);
saveas(gca,IMAGE1);

figure(2);
plot(T,H,'b','linewidth',3)
grid on
title(['Enthalpy: ',Species])
xlabel('Species Temperature[K]');
ylabel('Enthalpy[J/Kg]')
IMAGE2 = sprintf('Enthalpy %s.png',Species);
saveas(gca,IMAGE2);

figure(3);
plot(T,S,'g','linewidth',3)
grid on 
title(['Entropy: ',Species])
xlabel('Species Temperature[K]');
ylabel('Entropy[J/K]')
IMAGE3 = sprintf('Entropy %s.png',Species);
saveas(gca,IMAGE3);

cd(current_dir);

[molecular_mass]= mol_mass(Species);
 sprintf('Molecular Weight of %s is %f U',Species,molecular_mass)
 


end

Explanation:

First clear all, close all, clc command is used to clear all the data from the workspace and command window and close all this figure which is open its always good to start with these three commands before starting a code

The percent sign is most commonly used to indicate non-executable text within the body of a program. those non-executable texts are for better understanding the code

The THERMO.dat file is read using fopen command and Data file name is extracted from the first line using fgetl command

The next line is Global temperature range once the line extracted using fgetl command, next the line f string is split using strsplit command using delimiter and converted into numeric array using str2num command.

The next three lines which as an exclamation at starting in the data file is ignored

The next lines consist of the species name, physical state, and the Species temperature range the line is extracted and splits

The next line consists of coefficient those coefficient are extracted and defined as A1,A2,A3 …A14 for the function for calculating Cp,H,S

The function for calculating Cp, H, S is defined with the input variable as Coefficient and Temperature and Global Mid Temp

New Folder is created in the species Name using mkdir command

The Cp, H, S are plotted with Temperature using the plot command

These plots are saved in corresponding species folder using saveas and gca command.

Output Plots:

  • Species: O2

  • Species: N2

  • Species: CO2

Molecular Weight of the Species:

Location of Species Folder :

Conclusion :

Data is extracted from the NASA Thermodynamic File and Entropy, Enthalpy and Specific heat of each species is calculated with different temperature values.

The increased temperature increases the Entropy. More energy put into a system excites the molecules and the amount of random activity. As the gas expands in a system, Entropy increases.

The specific heat capacity of substances changes when there is a change in temperature. At the molecular level, the internal energy of a substance is stored in the form of translational, rotational and vibrational energy, As temperature increases, vibrational energy gains increasing significance. Thus, more amount of energy is required to increase the temperature by the same amount. Thus, specific heat increases with an increase in temperature.

When the temperature of a system increases, the kinetic and potential energies of the atoms and molecules in the system increase. Thus, the internal energy of the system increases, which means that the enthalpy of the system increases – this is true under constant pressure or constant volume.

https://in.mathworks.com/help

https://en.wikipedia.org/wiki/Parsing

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

https://www.grc.nasa.gov/WWW/K-12/airplane/specheat.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.

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.