Challenge

Project 1 - Parsing NASA thermodynamic data

Aim: To Write a code in matlab to parsing the NASA thermodynamics data file and then calculate the thermodynamics properties of various elements.

Objective:

  • Write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file"THERMO.dat".
  • Here R is the universal gas constant, T is the temeprature

  • Calculate the molecular weight of each species.
  • Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
  • Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.

Theory:

File Parsing: Parsing a file means reading in a data stream and building a memory model of the content of that data. This aims at facilitating perfoming some kind of transformation on the data. Parsing splits a file or other input into pieces of data that can be easily stored or manipulated.

List of Elements

Sr. No Elements Name Sr.No. Elements Name Sr.No. Elements Name
1 AR 25 CO2 49 NO
2 C 26 H 50 NO2
3 C2H 27 H2 51 O
4 C2H2 28 H2CN 52 O2
5 C2H3 29 H20 53 OH
6 C2H4 30 H2O2 54
7 C2H5 31 HCCO
8 C2H6 32 CCOH
9 C3H7 33 HCN
10 C3H8 34 HCNN
11 CH 35 HCNO
12 CH2 36 HCO
13 CH2(S) 37 HNCO
14 CH2CHO 38 HNO
15 CH2CO 39 HO2
16 CH2O 40 HOCN
17 CH2OH 41 N
18 CH3 42 N2
19 CH3CHO 43 N2O
20 CH3O 44 NCO
21 CH3OH 45 NH
22 CH4 46 NH2
23 CN 47 NH3
24 CO 48 NNH

Terms:

Enthalpy,Entropy and Specific Heat:

Enthalpy is not a directly measurable property. It is a synthetic combination of the internal energy (U) and the flow energy (pV) exchanged with the surroundings.

Enthalpy and specific enthalpy are symbolized by H and h, and are defined by
H = U + pV
h = u + pv


Entropy is a microscopic property associated with the microscopic energy transfer called heat (Q), between the system and its surroundings. Entropy is also called displacement of heat.

It is not directly measurable, but can be related to other properties. Entropy is a measure of the level of irreversibility associated with any process.

Unlike energy, it is nonconservative. It is a very important property in thermodynamics


Specific Heat (c, cp and cv)
The quantity c = δq/dT is called the specific heat or heat capacity. It is a measure of the heat added to a mass of a system to produce a unit increase in temperatu

NASA came up with polynimials that can be used to evaluate thermodynamics properties such as Cp,H,S they have documents the coefficient that are required to evaluate these polynomials for 1000+ species.

Matlab Code:

clear all
close all
clc

% Input
%Gas Constant
R = 8.314;

% Reading data file
% Open file and reading THERMO.dat file
f1 = fopen('THERMO.dat','r')
get = fgetl(f1);

% Now reading temperature from this file
temp = fgetl(f1);

% splitting 
s = strsplit(temp);

% convert string cell into numbers
templow = str2num(s{2});
tempmid = str2num(s{3});
temphigh = str2num(s{4});

% Temperature range
T = linspace(templow,temphigh,1000);

%Skipping comment lines
for i = 1:3
    get=fgetl(f1);
end

% Reading the species
for j = 1:53
    tline = fgetl(f1);
    if strcmp(tline,'END')
        break
    end
    
    A = strsplit(tline,'  '); %spliting into cell array
    Species_SP = (A{1});         % To read the name of species
    
    %Converting string to number
    lowtemp = str2num(A{end-3});
    midtemp = str2num(A{end-2});
    hightemp = str2num(A{end-1});
    
    tline1 = fgetl(f1);   % getting next line
    a = strfind(tline1,'E');    % Finding string E
    
    % finding the coefficient
    a1 = tline1(1:a(1)+3);
    a1 = str2num(a1);
    a2 = tline1(a(1)+4:a(2)+3);
    a2 = str2num(a2);
    a3 = tline1(a(2)+4:a(3)+3);
    a3 = str2num(a3);
    a4 = tline1(a(3)+4:a(4)+3);
    a4 = str2num(a4);
    a5 = tline1(a(4)+4:a(5)+3);
    a5 = str2num(a5);
    
    tline2 = fgetl(f1);
    b = strfind(tline2,'E');
    a6 = tline2(1:b(1)+3);
    a6 = str2num(a6);
    a7 = tline2(b(1)+4:b(2)+3);
    a7 = str2num(a7);
    
    %Finding Coefficient
    a8 = tline2(b(2)+4:b(3)+3);
    a8 = str2num(a8);
    a9 = tline2(b(3)+4:b(4)+3);
    a9 = str2num(a9);
    a10 = tline2(b(4)+4:b(5)+3);
    a10 = str2num(a10);
    
    tline3 = fgetl(f1);
    c = strfind(tline3,'E');
    a11 = tline3(1:c(1)+3);
    a11 = str2num(a11);
    a12 = tline3(c(1)+4:c(2)+3);
    a12 = str2num(a12);
    a13 = tline3(c(2)+4:c(3)+3);
    a13 = str2num(a13);
    a14 = tline3(c(3)+4:c(4)+3);
    a14 = str2num(a14);
    
    % Calculating Specific Heat
    Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);
    
    % calculating Entropy
    S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid);
    
    % calculating Enthalpy
    H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid);
    
    % Molecular weight file
    W = Molecular_weight(Species_SP);
    
    % writing molecular weight file
    MW_file = fopen('Molecular weight.txt','w');
    fprintf(MW_file,'Molecular weight of %s is %d',Species_SP,W);
    fclose(MW_file); % closing the file
    
    %Plotting Cp,H and S with temperature
    % saving in folder
    cur_dir=pwd;
    mkdir('Species')
    cd('Species')
    mkdir(Species_SP);  %creating directory
    cd(Species_SP);
    
    
    % plot 1 Cp vs T
    figure (1)
    plot(T,Cp,'linewidth',2,'color','b');
    xlabel('Temperature(K)');
    ylabel('Specific Heat(Cp)');
    title(sprintf('Specific heat vs Temperature range of %s',Species_SP));
    grid on
    saveas(figure(1),'Specific Heat.jpg');
    
    % plot 2 S vs T
    figure (2)
    plot(T,S,'linewidth',2,'color','r');
    xlabel('Temperature(K)');
    ylabel('Entropy(KJ/mol-K)');
    title(sprintf('Entropy vs Temperature range of %s',Species_SP));
    grid on
    saveas(figure(2),'Entropy.jpg');
    
    % plot 3 H vs T
    figure (3)
    plot(T,H,'linewidth',2,'color','g');
    xlabel('Temperature(K)');
    ylabel('Enthalpy(KJ/mol)');
    title(sprintf('Enthalpy vs Temperature range of %s',Species_SP));
    grid on
    saveas(figure(3),'Enthalpy.jpg');
    
    cd(cur_dir);
    %Molecular weight
    W = Molecular_weight(Species_SP)
    f2 = fopen('molecular mass','W');
    fprintf('%s n %s n %f n','species','Molecular mass',W)
    fclose(f2)
    
end

Fuction Code :

1)Entropy: is the measure of a system's thermal energy per unit temperature that is unavailabe for doing useful work.Because work is obtaine 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 = (a1lnt+a2T+a3T^2/2+a4T^3/3+a5T^4/4+a7)*R

function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)

  % calculating entropy 
  if (T <= tempmid)
      S = R*(a8.*log(T) + (a9*T) + ((a10*(T).^2)/2) +((a11*(T).^3)/3) + ((a12*(T).^4)/4) + a14);
  else
      S = R*(a1.*log(T) + (a2*T) + ((a3*(T).^2)/2) +((a4*(T).^3)/3) + ((a5*(T).^4)/4) + a7);
      
  end
end

2)Enthalpy:is the sum of the internal energy and the product of the pressure and volume of a thermodynamis system. Enthalpy is an energy-like property or state function.

H = (a1+a2T/2+a3T^2/3+a4T^3/4+a5T^4/5+a6/T)*R*T

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)
    % calculating enthalpy
    if (T <= tempmid)
        H = R*T.*(a8 + ((a9*T)/2) + ((a10*(T).^2)/3) + ((a11*(T).^3)/4) + ((a12*(T).^4)/5)+ ((a13./T)));
    else
        H =  R*T.*(a1 + ((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)) + ((a6./T)));
        
    end
end
            

3)Specific Heat:The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celsius. The relationship between heat and temperature change is usually expressed in the form shown below where c is the specific heat. The relationship does not apply if a phase change is encountered, because the heat added or removed during a phase change does not change the temperature.

Cp = (a1+a2T+a3T^2+a4T^3+a5T^4)*R

function Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid)

    %calculating specific heat
    %calculting specific heat for higher and lower temperature
    
    if (T <= tempmid)
        Cp = R*(a8 + (a9*T) + (a10*(T).^2) +(a11*(T).^3) + (a12*(T).^4));
    else
        Cp = R*(a1 + (a2*T) + (a3*(T).^2) +(a4*(T).^3) + (a5*(T).^4));
        
    end
    
end

4)Molecular Weight:The sum of the atomic masses of all atoms in a molecule, based on a scale in which the atomic masses of hydrogen, carbon, nitrogen, and oxygen are 1, 12, 14, and 16, respectively. For example, the molecular weight of water, which has two atoms of hydrogen and one atom of oxygen, is 18 (i.e., 2 + 16). Also called molecular mass.

function W = Molecular_weight(Species_SP)
% Atomic name
atomic_name = ['H','C','O','N','Ar'];

fprintf('Molecular_ weight of');
fprintf(Species_SP);
fprintf('  :   ');
%Atomic masses
atomic_weight = [1.008 12.011 15.999 14.007 39.948];

W=0;   % starting range of molecular weight

for i = 1:length(Species_SP)    % loop for length of the species
    for j = 1:length(atomic_name)     % loop for atomic names
      if strcmp(Species_SP(i),atomic_name(j)) %compare 2 string
          W = W + atomic_weight(j);
          position = j;
      end
    end
    n = str2num(Species_SP(i));      % here we find more number of element and atomic weight
    
    if n>1
        W = W + atomic_weight(position)*(n-1);
    end
end
fprintf('Molecular Weight of %s :',Species_SP);
fprintf('%f',W);
disp('  ')
end

          

How to write Code:

1) clear all - clear all the variables in the workspace
close all - closes all the plots and figures
clc - clears the command window

2) After that reading the txt file "THERMO.dat"and opening it by using 'fopen' and 'r' as reading command.

3) For reading the line in a text file using 'fgetl'

4)To find temperature from thermo.dat file using 'strsplit' to split temperature in particular string.

5) Used 'str2num' for converting cell array to number.

6)seperate the temperature into string and then converted into a number so easily we use temperature value for solve the code.

7)Using 'for&end' loops to skip the next three comments line in the text file.

8)Used'for loop' to read 53 species.

9)Used 'strfind'to find string.

10)Using 'linspace'specified temp.range low to high.

11)'mkdir'use to create directory

12)'cd'used for change directory.

13)write Cp,H,S and molecular weight function code for further process.

14)used 'saveas'command for to save plot in folder.

15)calculating molecular weight.

Syntax:

fopen: Open the file information .

r: Reading the file

fgetl : Read line from file

strsplit : Split string

str2num
str2double: string to numeric array

strfind : find one string within another

linspace : generate linearlly spaced vector.

fprintf: write formatted data to file

pwd : identify current directory

mkdir : make new directory

cd : change directory

Errors:

Output:

Molecular_ weight ofO  :   Molecular Weight of O :15.999000  

W =

   15.9990


Molecular_ weight ofO2  :   Molecular Weight of O2 :31.998000  

W =

   31.9980


Molecular_ weight ofH  :   Molecular Weight of H :1.008000  

W =

    1.0080


Molecular_ weight ofH2  :   Molecular Weight of H2 :2.016000  

W =

    2.0160


Molecular_ weight ofOH  :   Molecular Weight of OH :17.007000  

W =

   17.0070

Molecular_ weight ofH2O  :   Molecular Weight of H2O :18.015000  

W =

   18.0150


Molecular_ weight ofHO2  :   Molecular Weight of HO2 :33.006000  

W =

   33.0060
)Molecular_ weight ofH2O2  :   Molecular Weight of H2O2 :34.014000  

W =

   34.0140


Molecular_ weight ofC  :   Molecular Weight of C :12.011000  

W =

   12.0110


Molecular_ weight ofCH  :   Molecular Weight of CH :13.019000  

W =

   13.0190


Molecular_ weight ofCH2  :   Molecular Weight of CH2 :14.027000  

W =

   14.0270


Molecular_ weight ofCH2(S)  :   Molecular Weight of CH2(S) :14.027000  

W =

   14.0270

 
Molecular_ weight ofCH3  :   Molecular Weight of CH3 :15.035000  

W =

   15.0350


Molecular_ weight ofCH4  :   Molecular Weight of CH4 :16.043000  

W =

   16.0430


Molecular_ weight ofCO  :   Molecular Weight of CO :28.010000  

W =

   28.0100

 
Molecular_ weight ofCO2  :   Molecular Weight of CO2 :44.009000  

W =

   44.0090


Molecular_ weight ofHCO  :   Molecular Weight of HCO :29.018000  

W =

   29.0180


Molecular_ weight ofCH2O  :   Molecular Weight of CH2O :30.026000  

W =

   30.0260


Molecular_ weight ofCH2OH  :   Molecular Weight of CH2OH :31.034000  

W =

   31.0340


Molecular_ weight ofCH3O  :   Molecular Weight of CH3O :31.034000  

W =

   31.0340


Molecular_ weight ofCH3OH  :   Molecular Weight of CH3OH :32.042000  

W =

   32.0420


Molecular_ weight ofC2H  :   Molecular Weight of C2H :25.030000  

W =

   25.0300

Molecular_ weight ofC2H2  :   Molecular Weight of C2H2 :26.038000  

W =

   26.0380


Molecular_ weight ofC2H3  :   Molecular Weight of C2H3 :27.046000  

W =

   27.0460

Molecular_ weight ofC2H4  :   Molecular Weight of C2H4 :28.054000  

W =

   28.0540

Molecular_ weight ofC2H5  :   Molecular Weight of C2H5 :29.062000  

W =

   29.0620

Molecular_ weight ofC2H6  :   Molecular Weight of C2H6 :30.070000  

W =

   30.0700


Molecular_ weight ofCH2CO  :   Molecular Weight of CH2CO :42.037000  

W =

   42.0370

Molecular_ weight ofHCCO  :   Molecular Weight of HCCO :41.029000  

W =

   41.0290


Molecular_ weight ofHCCOH  :   Molecular Weight of HCCOH :42.037000  

W =

   42.0370

Molecular_ weight ofH2CN  :   Molecular Weight of H2CN :28.034000  

W =

   28.0340


Molecular_ weight ofHCN  :   Molecular Weight of HCN :27.026000  

W =

   27.0260

Molecular_ weight ofHNO  :   Molecular Weight of HNO :31.014000  

W =

   31.0140


Molecular_ weight ofN  :   Molecular Weight of N :14.007000  

W =

   14.0070

Molecular_ weight ofNNH  :   Molecular Weight of NNH :29.022000  

W =

   29.0220


Molecular_ weight ofN2O  :   Molecular Weight of N2O :44.013000  

W =

   44.0130

Molecular_ weight ofNH  :   Molecular Weight of NH :15.015000  

W =

   15.0150


Molecular_ weight ofNH2  :   Molecular Weight of NH2 :16.023000  

W =

   16.0230

Molecular_ weight ofNH3  :   Molecular Weight of NH3 :17.031000  

W =

   17.0310


Molecular_ weight ofNO  :   Molecular Weight of NO :30.006000  

W =

   30.0060

Molecular_ weight ofNO2  :   Molecular Weight of NO2 :46.005000  

W =

   46.0050


Molecular_ weight ofHCNO  :   Molecular Weight of HCNO :43.025000  

W =

   43.0250
 
Molecular_ weight ofHOCN  :   Molecular Weight of HOCN :43.025000  

W =

   43.0250

Molecular_ weight ofHNCO  :   Molecular Weight of HNCO :43.025000  

W =

   43.0250


Molecular_ weight ofNCO  :   Molecular Weight of NCO :42.017000  

W =

   42.0170


Molecular_ weight ofCN  :   Molecular Weight of CN :26.018000  

W =

   26.0180


Molecular_ weight ofHCNN  :   Molecular Weight of HCNN :41.033000  

W =

   41.0330

 
Molecular_ weight ofN2  :   Molecular Weight of N2 :28.014000  

W =

   28.0140


Molecular_ weight ofAR  :   Molecular Weight of AR :39.948000  

W =

   39.9480


Molecular_ weight ofC3H8  :   Molecular Weight of C3H8 :44.097000  

W =

   44.0970

 
Molecular_ weight ofC3H7  :   Molecular Weight of C3H7 :43.089000  

W =

   43.0890

 
Molecular_ weight ofCH3CHO  :   Molecular Weight of CH3CHO :44.053000  

W =

   44.0530


 
Molecular_ weight ofCH2CHO  :   Molecular Weight of CH2CHO :43.045000  

W =

   43.0450



Plots :

Cp,H,S for O2,N2 and CO2

O2

N2

CO2

Result:

Using matlab the necessary coefficient required to evaluate various thermodynamics parametrs for different species extractd from NASA thermodynamics data file.Cp,H and S calculated and plotted against the local temperature range.

Refrences:

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://link.springer.com/chapter/10.1007/978-1-4684-0186-8_8

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

https://in.mathworks.com/help/matlab/ref/cd.html#:~:text=cd%20displays%20the%20current%20folder.&text=cd%20newFolder%20changes%20the%20current,%C2%AE%20finishes%20executing%20the%20function.

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.