Project

Parsing NASA thermodynamic data using MATLAB.

TITLE : - Parsing NASA thermodynamic data using MATLAB.


OBJECTIVE :-

The objective of the program is to parse thermodynamic data using MATLAB and calculating important thermodynamic properties such as enthalpy, entropy and specific heat at constant pressure and to write a function a function that extracts the fourteen coefficients and also the molecular weight function for all the species in the data file.

FILE PARSING :-

File parsing is a syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters.

Use of file parsing is as a compiler or interpreter component that breaks data into smaller elements for easy translation into another language.


STEPWISE PROCEDURE :-

STEP - 1 :- Firstally,we have to give the file THERMO.dat as an input to the programme, and then create functions and extract the various coefficients for each species by asking the programme to read the file line by line and saving the coefficients in different variables, using the str2num() command.

STEP-2 :- Now for each species, a separate folder is generated, for which the enthalpy, entropy and specific heat at constant pressure are calculated depending on the temperature range according to which the below formulae are given :

Specific heat at constant pressure at high temperature :-

`C p = R ⋅ ( a 1 + ( a 2 ⋅ T ) + ( a 3 ⋅ ( T 2 ) ) + ( a 4 ⋅ ( T 3 ) ) + ( a 5 ⋅ ( T 4 ) ) )`

Specific heat at constant pressure at low temperature :-

`C p = R ⋅ ( a 8 + ( a 9 ⋅ T ) + ( a 10 ⋅ ( T 2 ) ) + ( a 11 ⋅ ( T 3 ) ) + ( a 12 ⋅ ( T 4 ) ) ) ;`

Enthalpy at high temperature :-

`H = R ⋅ T ⋅ ( ( a 1 + ( a 2 ⋅ ( T 2 ) + ( a 3 ⋅ ( T 2 3 ) ) + ( a 4 ⋅ ( T 3 4 ) ) + ( a 5 ⋅ ( T 4 5 ) ) + ( a 6 T ) ) ) )`

Enthalpy at low temperature :-

`H = R ⋅ T ⋅ ( ( a 8 + ( a 9 ⋅ ( T 2 ) + ( a 10 ⋅ ( T 2 3 ) ) + ( a 11 ⋅ ( T 3 4 ) ) + ( a 12 ⋅ ( T 4 5 ) ) + ( a 13 T ) ) ) ) ;`

Entropy at high temperature :-

`S = R ⋅ ( ( a 1 ⋅ log ( T ) ) + ( a 2 ⋅ T ) + ( a 3 ⋅ ( T 2 2 ) ) + ( a 4 ⋅ ( T 3 3 ) ) + ( a 5 ⋅ ( T 4 4 ) ) + a 7 )`

Entropy at low temperature :-

`S = R ⋅ ( ( a 8 ⋅ log ( T ) ) + ( a 9 ⋅ T ) + ( a 10 ⋅ ( T 2 2 ) ) + ( a 11 ⋅ ( T 3 3 ) ) + ( a 12 ⋅ ( T 4 4 ) ) + a 14 ) ;`

SPECIFIC HEAT:-

It 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 in the form of ‘cp’ (specific heat).


ENTHALPY :-

Enthalpy is a measure of energy in the system , which is the form of energy to consider when the system is closed and operating at constant pressure or when the system is open, with inflows and outflows.


ENTROPY :-

Entropy is a measure of disorder,and it is a consequence of the ‘Second Law of Thermodynamics’, one form of which states that “In a closed or fully isolated system, unconstrained energy spontaneously tend to disperse, to spread out, from more concentrated forms to less concentrated forms’’.


MOLECULAR WEIGHT :-

Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule.Molecular weight is either unitless or expressed in terms of atomic mass units (amu) or Daltons (Da).

ATOMIC WEIGHT :-

It is the total weight of an atom is called the atomic weight. It is approximately equal to the number of protons and neutrons, with a little extra added by the electrons,the stability of the nucleus, and the atom's radioactivity, and dependent upon the number of neutrons it contains.

STEP 3: As the above mentioned properties are then is to be plotted against temperature form (highest or lowest range) and stored in their respective folders. Further, the molecular weight of each species is also displayed along with the graphs.

PROGRAM EXPLANATION :-

Firstally created all the three function and are expressed as bellow,

  1. Specific Heat function program :-
%Specific Heat function program

function Cp = Specific_Heat_function_program(E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13,E14,T,R,local_low_temperature,local_mid_temperature,local_high_temperature);
if T>= local_mid_temperature  %(Here the local mid temperature is E7 & E14)
    
     Cp = (E1+E2.*(T)+E3.*(T.^2)+E4.*(T.^3)+E5.*(T.^4))*R;
    
else
    
     Cp = (E8+E9.*(T)+E10.*(T.^2)+E11.*(T.^3)+E12.*(T.^4))*R;
end



In this the specific heat is called for the main program and the values are calculated by the above formulas by comparing the temperature constant and the specific heat calculated values.And as the calculated value is generated below are low local low temperature and above values are local high temperatures.

  1. Enthalpy function program :-
%Enthalpy function program 

function H = Enthalpy_function_program(E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13,E14,T,R,local_low_temperature,local_mid_temperature,local_high_temperature);

if T>= local_mid_temperature %(Here the local mid temperature is E7 & E14)
    
    H = (E1.*T+ (E2.*(T.^2)./2)+ (E3.*(T.^3)./3)+ (E4.*(T.^4)./4)+ (E5.*(T.^5)./5+ E6))*R;
     
else
    
    H = (E8.*T+ (E9.*(T.^2)./2)+ (E10.*(T.^3)./3)+ (E11.*(T.^4)./4)+ (E12.*(T.^5)./5)+ E13)*R;
    
end

In this the Enthalpy is called for the main program and the values are calculated by the above formulas by comparing the temperature constant and the Enthalpy calculated values.And as the calculated value is generated below are low local low temperature and above values are local high temperatures.

  1. Entropy function program :-
% Entropy _function_program


function S = Entropy_function_program(E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13,E14,T,R,local_low_temperature,local_mid_temperature,local_high_temperature);

if T>=  local_mid_temperature  %(Here the local mid temperature is E7 & E14)
    
    S = (E1.*log(T)+ (E2.*T)+ (E3.*(T.^2)./2)+ (E4.*(T.^3)./3)+ (E5.*(T.^4)./4)+ E7).*R;
    
else
    
    S = (E8.*log(T)+ (E9.*T)+ (E10.*(T.^2)./2)+ (E11.*(T.^3)./3)+ (E12.*(T.^4)./4)+ E14).*R;
    
end

In this the Entropy is called for the main program and the values are calculated by the above formulas by comparing the temperature constant and the Entropy calculated values.And as the calculated value is generated below are low local low temperature and above values are local high temperatures.

Explanation of the main body code :-

close all
clear all
clc

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

line_1 = fgetl(f1);

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});

for i=1:3
    comments = fgetl(f1);
end

x = input('Enter the molecule name:  ','s')

for j = 1:53
    %line 3
    line_3 = fgetl(f1);
    line_3_split = strsplit(line_3,' ');
    species_name =line_3_split{1};

    if strcmpi(species_name,x)

        temp = strsplit(line_3,'G ');
        temp_t = str2num(temp{2});
        local_low_temperature = temp_t(1);
        local_mid_temperature = temp_t(2);
        local_high_temperature = temp_t(3);

        %line 4
        line_4 = fgetl(f1);
        c = strfind(line_4,'E');
        E1 = str2num(line_4(1:c(1)+3));
        E2 = str2num(line_4(c(1)+4:c(2)+3));
        E3 = str2num(line_4(c(2)+4:c(3)+3));
        E4 = str2num(line_4(c(3)+4:c(4)+3));
        E5 = str2num(line_4(c(4)+4:c(5)+3));

        %line 5
        line_5 = fgetl(f1);
        E6 = str2num(line_5(1:c(1)+3));
        E7 = str2num(line_5(c(1)+4:c(2)+3));
        E8 = str2num(line_5(c(2)+4:c(3)+3));
        E9 = str2num(line_5(c(3)+4:c(4)+3));
        E10 = str2num(line_5(c(4)+4:c(5)+3));

        %line 6
        line_6 = fgetl(f1);
        E11 = str2num(line_6(1:c(1)+3));
        E12 = str2num(line_6(c(1)+4:c(2)+3));
        E13 = str2num(line_6(c(2)+4:c(3)+3));
        E14= str2num(line_6(c(3)+4:c(4)+3));

        % T = temperature
        T = linspace(local_low_temperature,local_high_temperature,100);
        %R = gas constant
        R = 8.134;
        % cp = specific heat
        Cp = R*Specific_Heat_function_program(E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13,E14,T,R,local_low_temperature,local_mid_temperature,local_high_temperature);
        % H = enthaly
        H = R*T.* Enthalpy_function_program(E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13,E14,T,R,local_low_temperature,local_mid_temperature,local_high_temperature);
        %S = entropy
        S= R*Entropy_function_program(E1,E2,E3,E4,E5,E6,E7,E8,E9,E10,E11,E12,E13,E14,T,R,local_low_temperature,local_mid_temperature,local_high_temperature);
        %calculating molecular weight


        %creating directory
        mkdir(species_name)

        %plotting
        figure(1)
        plot(T,Cp,'linewidth',2,'color','r')
        xlabel('Temperature','color','m')
        ylabel('Specific Heat','color','m')
        title(species_name)

        figure(2)
        plot(T,H,'linewidth',2,'color','b')
        xlabel('Temperature','color','m')
        ylabel('Enthalpy','color','m')
        title(species_name)

        figure(3)
        plot(T,S,'linewidth',2,'color','g')
        xlabel('Temperature','color','m')
        ylabel('Entropy','color','m')
        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('..')

    else
        for k=1:3
            fgetl(f1);
        end
       
    end

end

In the main body, start the code with basic commands as learnt.Then to define the values of the variables and open the data file using the 'fopen' command.

Used 'fgetl()' command to read and save the global temperature values and then split the values in the data file using 'str2double' command to split the data into strings.The following fgetl command reads and save the values of temperatures and also fourteen coefficients for different species. The strings are converted into numericals by using 'str2double' command.Then used 'strfind' command to locate the value of 'E' in each line.Now the coefficients are assigned to the respective variables by E1,E2,E3,E4,...E14 which are also has been provided in the above given all three function programs.

Next the 'for' loop is used to read all the coefficients of all the fifty-three species.and for different temperature range function calls the values of Cp, H and S and are calculated for temperatures.By using 'mkdir' function have been created a new folders with names for all the output files of the given species.and 'cd' command to changes the directory again.

For plotting, we using plot and figure commands and plots have been created for all the different species.A 'saveas' command is being used to save all the plots into their respective folders.and then we created the molecular weight function program and prints the values of all the species as enlisted,please note below are the main code and below that is the molecular weight calculation code.

As we also need to elaborate for molecular weight function program it is as follows :-

MOLECULAR WEIGHT FUNCTION PROGRAM :-

%molecular weight function program

function[A]= molecular_weight_function_program(species_name)

constituents_mass=[15.9994,1.008,12.011,14.0067,39.948,32];
constituents=['O','H','C','N','A','S'];

mol_wt=0;
for i=1:length(species_name)
    for j=1:length(constituents)
        if strcmpi(species_name(i),constituents(j))
            mol_wt=mol_wt+constituents_mass(j);
            r=j;
        end 
    end
    ruk=str2double(species_name(i));
    if (ruk>1)
        
        mol_wt=mol_wt+(constituents_mass(r)*(ruk-1));
    end
end
A=mol_wt;
end

ERROR FACED :-

The first error was the ‘saveas()’ may not be applicable for some versions of MATLAB in the error. In such a case, using the right function for the version is important and must be kept in mind.And used the figure command to where the multiple figures may overwrite one over the other while being displayed during the function call, resulting in only the last figure in the function to be displayed,in such case, using the ‘hold on’ command or even a new figure function can help to plot different graphs in different windows.

RESULTS :-

Command Window :-

Folder created in the program's working directory

PLOTS OF Temperature vs Specific Heat

O2 -

N2 -

Co2 -

PLOTS OF Temperature vs Enthalpy

O2 -

N2 -

Co2 -

PLOTS OF Temperature vs Entropy :-

O2 -

N2 -

Co2 -

CONCLUSION:-

In the program, a three function created that extracts the fourteen coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file and as well as the molecular weight of each species are calculated and plotted done by the specific heat, enthalpy and entropy and local temperatures for each given species. I conclude that in MATLAB file parsing is a tool that can be engaged while reading through the file line by line and the extraction of large amounts of data from files. With MATLAB it’s the capability to store, compute, interpret and interact with the given data.

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.