Challenge

Parsing NASA Thermodynamic Data

AIM

To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the given data file

OBJECTIVE

  • Code a function to get 14 co-efficients of given species from the given data
  • Code a function to calculates the thermodynamic properties of Specific heat, enthalpy, entropy for each given species
  • Code a function to plots for each given species by the values has got from the calculation of thermodynamic properties
  • Code a function to make a folder to save the plots of each species
  • Code a function to calculates the molecular weight of each species

FILE PARSING

  • Parsing a file means nothing but a syntax analyzer which can reading and converting it into a form which can be used for computations. This aims at facilitating perfoming some kind of transformation on the data
  • From the input file, we can see the data in the form of text

It will be able to,

  • Split a string using a delimeter
  • Remove the whitespace around the string
  • Convert the datatypes of variables from one type to other

THERMODYNAMIC PROPERTIES

In order to carry through a program of finding the changes in the various thermodynamic functions that accompany reactions, such as specific heats, enthalpy and entropy

Specific Heat [Cp]

  • Specific heat refers to the ratio of the quantity of heat that we require to raise the temperature of a body by one degree that we need to increase the temperature of an equivalent mass of liquid (water) by one degree

  • Also, we use the term in a narrower sense to mean the amount of heat, in calories that we require to raise the temperature of one gram of a substance by one Celsius degree

  • In simple words, it is the ratio of the amount of heat required to increase the temperature of an object by one degree to the amount of heat required to increase the temperature of the same amount of water by one degree

  • Specific heat is also known as specific heat capacity or mass specific heat

`c_p=(DeltaQ)/(m*DeltaT)`

where,

`c_p`= Specific heat

`DeltaQ`= Heat energy

`m`= Mass of the object or substance

`DeltaT`= Change in temperature

Enthalpy [H]

  • Enthalpy is representing the heat content of a system. It is a common fact that the heat can go in or out of the system. Enthalpy is the measurement of the energy of some chemical reactions in a thermodynamic system, the change of heat is defined as enthalpy change
  • This knowledge about the increase or decrease of the enthalpy of the system during a chemical reaction is a very crucial factor for understanding the reaction. Also, the change in the enthalpy of the system during a chemical reaction is termed as the sum total value of change in its internal energy and the change in the product of the pressure and volume of the system
  • It is also equivalent to the sum total of the system’s internal energy product of volume and pressure. Enthalpy describes the internal energy needed to generate a system

`H=U+pV`

where,

`U`= Internal energy

`p`= Pressure

`V`= Volume

  • When a process begins at some constant pressure, then heat will be evolved, either absorbed or released and it equals the change in enthalpy. Therefore Enthalpy change is the sum of internal energy denoted by E and product of volume and Pressure, denoted by P × V

Entropy [S]

  • Entropy is a scientific concept, as well as a measurable physical property that is most commonly associated with a state of disorder. Any type of medium either solid or fluid always consists of molecules. The measurement of the randomness of molecules in the system is called entropy
  • Therefore, if the randomness of molecules is more, then there will be more increase in entropy of the process. And as a result, the percentage of heat which may be converted into work will be less. Thus, we may say that entropy is a function of the quantity of heat
  • It's simple, it is just a measurement of how much randomly the molecules are moving in a system. In solids, the molecules are properly arranged, which means it has less randomness, so the entropy of solids is least. In gases, the molecules move very fast throughout the container. It has more randomness which means it has entropy. The entropy of liquids lies in between the solids and liquids

`DeltaS=(DeltaQ)/T`

where,

`DeltaS`= Change in entropy

`DeltaQ`= Heat energy

`T`= Temperature

FORMUALE USED

`C_p/R=a1+a2.T+a3.T^2+a4.T^3+a5.T^4`

`H/(RT)=a1+a2.T/2+a3.T^2/3+a4.T^3/4+a5.T^4/5+(a6)/T`

`S/R=a1.ln.T+a2.T+a3.T^2/2+a4.T^3/3+a5.T^4/4+a7`

where,

`C_p`= Specific heat (J/Kg.K)

H = Enthalpy (J)

S = Entropy (J/K)

`a1-a7`= Low temperature co-efficient

`a8-a14`= High temperature co-efficient

T = Temperature (T)

R = Universal gas constant (J/mol.K)

PROGRAM

clear all
close all
clc

%Getting the Data File
f1 = fopen('THERMO.dat','r');

%To find the total no.of species
string = fgetl(f1);
i = 1;
while strcmp(string,'END') ==  0
    string = fgetl(f1);
i = i+1;
end

total_lines = i;
total_species = (total_lines-6)/4;
% On above commands are used only to find out the total no.of species in the given data file

%So we  have to reopen the Data File for further steps
f1 = fopen('THERMO.dat','r');
fgetl(f1);

%Reading the header information to get the global temperatures
A = fgetl(f1);
B= strsplit(A,' ');

global_low_temp = str2double(B{2});
global_mid_temp = str2double(B{3});
global_high_temp = str2double(B{4});

%Skipping next 3 Lines
for j = 1:3
    fgetl(f1);
end

%Reading the Species Data
for i = 1:total_species
    line_1 = fgetl(f1);
    x = strsplit(line_1,' ');
    species_name = x{1};
    z = length(x);
    
    %Subtract from right side to left side in z
    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;
   
%2nd Line Co-efficients of a1-a5
    line_2 = fgetl(f1);
    a = strfind(line_2,'E');
    a1 = str2double(line_2(1:a(1)+3));
    a2 = str2double(line_2(a(1)+4:a(2)+3));
    a3 = str2double(line_2(a(2)+4:a(3)+3));
    a4 = str2double(line_2(a(3)+4:a(4)+3));
    a5 = str2double(line_2(a(4)+4:a(5)+3));
    
%3rd Line Co-efficients of a6-a10
    line_3 = fgetl(f1);
    a = strfind(line_3,'E');
    a6 = str2double(line_3(1:a(1)+3));
    a7 = str2double(line_3(a(1)+4:a(2)+3));
    a8 = str2double(line_3(a(2)+4:a(3)+3));
    a9 = str2double(line_3(a(3)+4:a(4)+3));
    a10 = str2double(line_3(a(4)+4:a(5)+3));
    
%4th Line Co-efficients of a11-a14
    line_4 = fgetl(f1);
    a = strfind(line_4,'E');
    a11 = str2double(line_4(1:a(1)+3));
    a12 = str2double(line_4(a(1)+4:a(2)+3));
    a13 = str2double(line_4(a(2)+4:a(3)+3));
    a14 = str2double(line_4(a(3)+4:a(4)+3));
    
%Calculate Specific Heat, Enthalpy, Entropy w.r.t Temperature
        T = linspace(t1,t3,50);     %Temperature(K)
        R = 8.314;                  %Universal gas constant(J/mol.K)
    for j = 1:length(T)
        if T(j)>t2
            C_p(j) = (a1+(a2.*T(j))+(a3.*T(j).^2)+(a4.*T(j).^3)+(a5.*T(j).^4))*R;
            H(j)= (a1+(a2.*T(j)/2)+(a3.*(T(j).^2)/3)+(a4.*(T(j).^3)/4)+(a5.*(T(j).^4)/5)+(a6./T(j)))*R.*T(j);
            S(j) = ((a1.*log(T(j)))+(a2.*T(j))+(a3.*(T(j).^2)/2)+(a4.*(T(j).^3)/3)+(a5.*(T(j).^4)/4)+a7)*R;
        
        else
            C_p(j) = (a8+(a9.*T(j))+(a10.*T(j).^2)+(a11.*T(j).^3)+(a12.*T(j).^4))*R;
            H(j)= (a8+(a9.*T(j)/2)+(a10.*(T(j).^2)/3)+(a11.*(T(j).^3)/4)+(a12.*(T(j).^4)/5)+(a13./T(j)))*R.*T(j);
            S(j) = ((a8.*log(T(j)))+(a9.*T(j))+(a10.*(T(j).^2)/2)+(a11.*(T(j).^3)/3)+(a12.*(T(j).^4)/4)+a14)*R;
        end
    end
    
%Plotting Specific Heat, Enthalpy, Entropy w.r.t Temperature
    figure(1)
    plot(T,C_p,'linewidth',3,'color','r')
    title('Species Name:',species_name)
    xlabel('Temperature(K)')
    ylabel('Specifc Heat(J/Kg.K)')
    grid on
    figure(2)
    plot(T,H,'linewidth',3,'color','g')
    title('Species Name:',species_name)
    xlabel('Temperature(K)')
    ylabel('Enthalpy(J)')
    grid on
    figure(3)
    plot(T,S,'linewidth',3,'color','b')
    title('Species Name:',species_name)
    xlabel('Temperature(K)')
    ylabel('Entropy(J/K)')
    grid on

%Creating folder for each species  
    mkdir(['D:Skill LyncTemp Program 1File Parsing',species_name]);
    cd(['D:Skill LyncTemp Program 1File Parsing',species_name]);
    saveas(1,'Specific Heat.png')
    saveas(2,'Enthalpy.png')
    saveas(3,'Entropy.png') 

%Calculate molecular weight for each species
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
    
 %Multiply the molecular weight into no.of atoms for each species
n=str2double(species_name(l));
if n>1
    mol_mass=mol_mass+atomic_weight(position)*(n-1);
    
end
end

%Display the molecular weight for each species in command window
fprintf('Molecular Weight of %s: %f amun',species_name,mol_mass)

end

fclose(f1); 

CODE PROCEDURE

  • First of all, we need to download the "THERMO.dat" file of NASA thermodynamic data from the given link
  • Use the 'fopen' command to open the 'THERMO.dat' file to read the given data
  • To find the total number of species, code a 'while-loop' function with 'strcmp' command has used to compare the string 'END' word where mentioned in the given data file
  • Based on the above code to find the total lines in the data file, then calculate the total species as subtracting total lines by eliminating the unwanted 6 lines(1st five lines and the last line) and divided by 4(which means each species had 4 lines)

  • Above these all coded only for the calculations of total lines and total species of the given data file, now we going to code for main part of this file parsing program
  • Let use the 'fopen' command again to open the 'THERMO.dat' file to read the given data and use the 'fgetl' command to read the first line of the file
  • After that use same 'fgetl' command to read the header information(global temperatures)
  • Use 'strsplit' command to split the string by the specified delimeters(' ')
  • Assign the splitted line in 'y', then get the global temperatures(low, mid, high) by subtracts the cell position from right to left side for each
  • After that skip the 3 unwanted lines by using 'for-loop' function

  • From the above picture, Read the species data as line wise from 'line_1' by use 'fgetl' command, then use 'strsplit' command to split the string by the specified delimeters(' ')
  • Then call the 'line_1' as 'x' and call the 'species_name' from the 1st cell of x. Assign the splitted line in 'z', then get the local temperatures(low, mid, high) by subtracts the cell position from right to left side for each
  • Assign the 3 local temperatures in t1, t2, t3 and after that read all the 14 co-efficients as given in that specified species by 'fgetl' command as line-wise(line_2 to line_4)
  • Use 'strfind' command to find strings within other strings by the specified delimeters('E')
  • Use 'str2double' command to convert the strings into values to get the each co-efficients and assign in a1 - a14
  • Now we have to calculate the specific heat, enthalpy, entropy with respect to Temperature(`T`)
  • Use the'linspace' command to generate 50 evenly spaced points between two values(t1 and t3) in 'T' and assign 'R' an universal gas constant
  • Then use the 'Nested for-loop' and 'if-else' function to calculate the specific heat, enthalpy, entropy by the given formula
  • Here we have to plot the each figures, before that we have to use the 'figure' command to create figure window for each thermodynamic properties
  • Now use 'plot' command to plots the 3 required thermodynamic properties(`c_p,H, S`) with temperature(`T`) and with color, linewidth, labels, title commands for each
  • Use 'mkdir' command to create the folder for each species and use 'cd' command to change the current folder to the new created folder. In this both command we have to provides the same path as where we going to save these files
  • Here use 'saveas' command to save the figures of each properties on each species folders under the specific file format
  • Then calculate molecular weight for each species by 'nested loop' and 'if' function here analyse the given data file and find out the type of moleculars in it, after that mention those and assign that in elements

  • Then provide the values of moleculars what you got and assign it in atomic weight

  • In this 'if' function, 'strcmp' command has used to compare the strings of species name with elements, if it results TRUE then molecular mass will calculated as per the appropriate species
  • And the result of molecular mass has been multiplied with the number of atoms for each species by the 'if' function
  • Finally, 'fprintf' command has used to display the molecular weight for each species in command window

PLOT DIAGRAM

1. For O2

  • Specific Heat [Cp]

  • Enthalpy [H]

  • Entropy [S]

2. For N2

  • Specific Heat [Cp]

  • Enthalpy [H]

  • Entropy [S]

3. For Co2

  • Specific Heat [Cp]

  • Enthalpy [H]

  • Entropy [S]

SNAPSHOTS

1. Plotted images saved in the following folders and these folders are generated automatically for each species

2. Number of total lines and total species has shown in the workspace were calculated during the coding

MOLECULAR WEIGHT OF EACH SPECIES

Based on the calculation, the molecular mass of each species has given below,

  1. Molecular Weight of O : 15.999000 amu
  2. Molecular Weight of O2 : 31.998000 amu
  3. Molecular Weight of H : 1.007900 amu
  4. Molecular Weight of H2 : 2.015800 amu
  5. Molecular Weight of OH : 17.006900 amu
  6. Molecular Weight of H2O : 18.014800 amu
  7. Molecular Weight of HO2 : 33.005900 amu
  8. Molecular Weight of H2O2 : 34.013800 amu
  9. Molecular Weight of C : 12.010700 amu
  10. Molecular Weight of CH : 13.018600 amu
  11. Molecular Weight of CH2 : 14.026500 amu
  12. Molecular Weight of CH2(S) : 14.026500 amu
  13. Molecular Weight of CH3 : 15.034400 amu
  14. Molecular Weight of CH4 : 16.042300 amu
  15. Molecular Weight of CO : 28.009700 amu
  16. Molecular Weight of CO2 : 44.008700 amu
  17. Molecular Weight of HCO : 29.017600 amu
  18. Molecular Weight of CH2O : 30.025500 amu
  19. Molecular Weight of CH2OH : 31.033400 amu
  20. Molecular Weight of CH3O : 31.033400 amu
  21. Molecular Weight of CH3OH : 32.041300 amu
  22. Molecular Weight of C2H : 25.029300 amu
  23. Molecular Weight of C2H2 : 26.037200 amu
  24. Molecular Weight of C2H3 : 27.045100 amu
  25. Molecular Weight of C2H4 : 28.053000 amu
  26. Molecular Weight of C2H5 : 29.060900 amu
  27. Molecular Weight of C2H6 : 30.068800 amu
  28. Molecular Weight of CH2CO : 42.036200 amu
  29. Molecular Weight of HCCO : 41.028300 amu
  30. Molecular Weight of HCCOH : 42.036200 amu
  31. Molecular Weight of H2CN : 28.033200 amu
  32. Molecular Weight of HCN : 27.025300 amu
  33. Molecular Weight of HNO : 31.013600 amu
  34. Molecular Weight of N : 14.006700 amu
  35. Molecular Weight of NNH : 29.021300 amu
  36. Molecular Weight of N2O : 44.012400 amu
  37. Molecular Weight of NH : 15.014600 amu
  38. Molecular Weight of NH2 : 16.022500 amu
  39. Molecular Weight of NH3 : 17.030400 amu
  40. Molecular Weight of NO : 30.005700 amu
  41. Molecular Weight of NO2 : 46.004700 amu
  42. Molecular Weight of HCNO : 43.024300 amu
  43. Molecular Weight of HOCN : 43.024300 amu
  44. Molecular Weight of HNCO : 43.024300 amu
  45. Molecular Weight of NCO : 42.016400 amu
  46. Molecular Weight of CN : 26.017400 amu
  47. Molecular Weight of HCNN : 41.032000 amu
  48. Molecular Weight of N2 : 28.013400 amu
  49. Molecular Weight of AR : 39.948000 amu
  50. Molecular Weight of C3H8 : 44.095300 amu
  51. Molecular Weight of C3H7 : 43.087400 amu
  52. Molecular Weight of CH3CHO : 44.052000 amu
  53. Molecular Weight of CH2CHO : 43.044100 amu

ERROR OCCURED

1. Arguement used as wrong in line 26

Solution: species_name = x{1};

Reason: It's a cell array, so we have to you curly brackets

REFERENCES

2. https://www.youtube.com/watch?v=vn1JEFdRZ7o&t=214s

2. https://www.youtube.com/watch?v=VlFq0cVGC0I&t=268s

2. https://lawofthermodynamicsinfo.com/definition-of-entropy-in-thermodynamics/

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.