Project

Parsing NASA Thermodynamic data - MATLAB

AIM:

To parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.

DEFINITIONS:

FILE PARSING:

Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data.

SPECIFIC HEAT:

Specific heat is the thermodynamic property, which states the amount of heat required for a single unit of mass of a substance to be raised by one degree of temperature. Varying ranges of specific heat values are seen for substances depending on the extent to which they absorb heat.

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 mass transfer, for processes at constant pressure, the heat absorbed or released equals the change in enthalpy.Enthalpy comprises a system's internal energy, which is the energy required to create the system, plus the amount of work required to make room for it by displacing its environment and establishing its volume and pressure.Enthalpy is a state function that depends only on the prevailing equilibrium state identified by the system's internal energy, pressure, and volume. It is an extensive quantity.

ENTROPY:

Entropy is an extensive property of a thermodynamic system. It is closely related to the number Ω of microscopic configurations (known as microstates) that are consistent with the macroscopic quantities that characterize the system (such as its volume, pressure and temperature).Entropy is a function of the state of the system, so the change in entropy of a system is determined by its initial and final states. In the idealization that a process is reversible, the entropy does not change, while irreversible processes always increase the total entropy.

ATOMIC WEIGHT:

Atomic weight, also called relative atomic mass, ratio of the average mass of a chemical element's atoms to some standard. An isotope is one of two or more species of atoms of the same chemical element that have different atomic mass numbers (protons + neutrons).

MOLECULAR WEIGHT:

The average mass of a molecule of a compound compared to ¹/₁₂ the mass of carbon 12 and calculated as the sum of the atomic weights of the constituent atoms.

MATLAB PROGRAM :

FUNCTION FOR SPECIFIC HEAT:

function cp = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt_mid)
  for  i  = 1:length(T)    
   
     if (T(i)>=lt_mid)
         cp(i) = R*((a1+a2*T(i)+a3*(T(i)^2)+a4*(T(i)^3)+a5*(T(i)^4)));
     else
         cp(i) = R*((a8+a9*T(i)+a10*(T(i)^2)+a11*(T(i)^3)+a12*(T(i)^4)));
  end
end

The given formula for specific heat is entered.

The first seven coefficients are used for high temperature range and next seven coefficients are used for low temperature range by using if else condition.

FUNCTION FOR ENTROPY:

function S= entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt_mid)
  for i  = 1:length(T)
      if (T(i)>=lt_mid)
           S(i) = R*(a1*log(T(i))+a2*T(i)+a3*(T(i)^2)/2+a4*(T(i)^3)/3+a5*(T(i)^4)/4+a7);
      else
          S(i) = R*(a8*log(T(i))+a9*T(i)+a10*(T(i)^2)/2+a11*(T(i)^3)/3+a12*(T(i)^4)/4+a14);
   end
end

The given formula for entropy is entered.

The first seven coefficients are used for high temperature range and next seven coefficients are used for low temperature range by using if else condition.

FUNCTION FOR ENTHALPY:

function H = enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,R,lt_mid)
  for i  = 1:length(T)
    if (T(i)>=lt_mid)
       H(i) = R*T(i)*(a1+a2*(T(i)/2)+a3*(T(i)^2)/3+a4*(T(i)^3)/4+a5*(T(i)^4)/5+a6/T(i));
    else 
       H(i) = R*T(i)*(a8+a9*(T(i)/2)+a10*(T(i)^2)/3+a11*(T(i)^3)/4+a12*(T(i)^4)/5+a13/T(i));
  end
end

The given formula for enthalpy is entered.

The first seven coefficients are used for high temperature range and next seven coefficients are used for low temperature range by using if else condition.

FUNCTION FOR MOLECULAR WEIGHT:

function [m_weight] = molecular_weight(species_name)
         atomic_weight = [1.009, 12.011, 14.006, 15.999, 32.065, 39.948];
         element = ['H','C','N','O','S','AR'];
         m=0; c=0;
         
         for f = 1:length(species_name)
             for g = 1:length(element)
                 if strcmpi(species_name(f),element(g))
                     m = m + atomic_weight(g);
                     c=g;
                 end
             end
             
             num = str2double(species_name(f));
             if (num > 1)
                 m = m + (atomic_weight(c)*(num-1));
             end
         end
        m_weight = m;
end

The atomic weight and the elements are entered.

By using for loop the loops runs till the length of f,g and by using strcmpi, the strings are compared and "i" represents case insensitive.

The variables are stored in num and it runs till the loop ends

MAIN PROGRAM:

close all
clear all
clc

%reading the file
f1 = fopen('THERMO.dat','r');

%getting line 1
line_1 = fgetl(f1);

%getting line 2
line_2 = fgetl(f1);
t = strsplit(line_2,' ');
global_temp_low = str2num(t{2});
global_temp_mid = str2num(t{3});
global_temp_high = str2num(t{4});

%storing the 3 lines as comments
for i=1:3
    comments = fgetl(f1);
end

%getting input to display
x = input('Enter the molecule name:  ','s')

%getting all the other lines using while loop
while ~feof(f1)
    %line 3
    line_3 = fgetl(f1);
    line_3_split = strsplit(line_3,' ');
    species_name =line_3_split{1};
      
    %for molecular weight comparison
    if strcmpi(species_name,x)
        
        %finding molecular weight by calling the function
        [Mol_wght] = molecular_weight(species_name);
         M = ['Molecular weight of ', species_name, ' = ', num2str(Mol_wght)];
         disp(M)

        temp = strsplit(line_3,'G ');
        temp_t = str2num(temp{2});
        lt_low = temp_t(1);
        lt_high = temp_t(2);
        lt_mid = temp_t(3);

        %line 4
        line_4 = fgetl(f1);
        c = strfind(line_4,'E');
        a1 = str2num(line_4(1:c(1)+3));
        a2 = str2num(line_4(c(1)+4:c(2)+3));
        a3 = str2num(line_4(c(2)+4:c(3)+3));
        a4 = str2num(line_4(c(3)+4:c(4)+3));
        a5 = str2num(line_4(c(4)+4:c(5)+3));

        %line 5
        line_5 = fgetl(f1);
        a6 = str2num(line_5(1:c(1)+3));
        a7 = str2num(line_5(c(1)+4:c(2)+3));
        a8 = str2num(line_5(c(2)+4:c(3)+3));
        a9 = str2num(line_5(c(3)+4:c(4)+3));
        a10 = str2num(line_5(c(4)+4:c(5)+3));

        %line 6
        line_6 = fgetl(f1);
        a11 = str2num(line_6(1:c(1)+3));
        a12 = str2num(line_6(c(1)+4:c(2)+3));
        a13 = str2num(line_6(c(2)+4:c(3)+3));
        a14= str2num(line_6(c(3)+4:c(4)+3));

        % T = temperature
        T = linspace(lt_low,lt_high,100);
        
        %R = gas constant
        R = 8.134;
        
        % cp = specific heat
        cp = R*specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,lt_mid);
        
        % H = enthalpy
        H = R*T.*enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,lt_mid);
        
        %S = entropy
        S= R*entropy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,T,lt_mid);
        
        %creating directory
        mkdir(species_name)

        %plotting
        figure(1)
        plot(T,cp,'linewidth',2.5,'color','b');
        xlabel('Temperature','color','c');
        ylabel('Specific Heat','color','b');
        title(species_name)

        figure(2)
        plot(T,H,'linewidth',2.5,'color','r');
        xlabel('Temperature','color','c');
        ylabel('Enthalpy','color','r');
        title(species_name)

        figure(3)
        plot(T,S,'linewidth',2.5,'color','g');
        xlabel('Temperature','color','c');
        ylabel('Entropy','color','g');
        title(species_name)

        %saving the plots in directories
        cd(species_name)
        saveas(figure(1),'Tvscp.jpg')
        saveas(figure(2),'TvsH.jpg')
        saveas(figure(3),'TvsS.jpg')
        cd('..')
        
        %saving thw workspace values
        save(species_name)
   
    else
        for k=1:3
            fgetl(f1);
        end
       
    end

end

PROCEDURE:

Initially we are opening and reading the given data and storing it in f1.

Next, we are getting the line 1, line 2 and the next three lines are stored as comments.

From the line 2 we are splitting it into cell array by using 'strsplit' and by using 'str2num' we are converting into number and getting the Global temperatures and storing it according to their temperature range.

Using for loop the next three lines are stored in comments.

For the below lines, while command is used till the end of the data.

In line 3 , the line is splitted into two cell array and the first element in the first cell array is stored as species name and rest of the line is splitted based upon the 'G ' , and the local temperatures are seperated according to their range and stored in different variable.

The line 4 is called and we are finding 'E' in that line. By finding E we can split into numbers by using str2num command and they are stored in each variable. Same procedure for line 5 and line 6.

For temperature we are assigning the values by using 'linspace' command and giving it range between local low temperature and local high temperature.

R is gas constant, the value for R is 8.134

From the functions of enthalpy,entropy and specific heat they are called.

Directory is created in the name of each species using "mkdir" command.

The figure is plotted for each species and by the commands we know, we are also giving xlabel,ylabel and title.

The plotted figures are be stored in directory by using "cd" changes the current directory and "saveas" saves the figures in each species.

To show the workspace values we are using "save" command to save the values of the each species.

The input molecule name is given by the user so that the species name compares with the varaible given by the user and gives the output.

SCREENSHOT OF THE WINDOW:

PLOT FOR O2:

PLOTS FOR N2:

PLOT FOR CO2:

MOLECULAR WEIGHT IN COMMAND WINDOW:

CONCLUSION:

Thus using MATLAB we can parse the file and collect data in larger amount. Thus by using formulas specific heat,enthalpy and entropy are calculated. Molecular weight of each species is calculated. The above mentioned plots clearly shows that they are plotted against temperature for each species.

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.