Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

     To write a program code to the parse the NASA thermodynamic data file and calculate the thermodynamic properties of various gas species using MATLAB.

OBJECTIVE:

1.Write a function that it extracts the 14 co-efficients and calculates the Enthalpy,Entropy and Specific heats for all the species in the thermodynamic data file.

2.Calculate the molecular weight of each speices and display it in the command window.

3.Plot the Cp,Enthalpy and Entropy for the local temperature range(low temperature and high temperature) specific for each species.

4.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.

5.The main program that you submit will just take the "THERMO.dat" files as an input and generate Cp,H and Splots.

6.Scrrenshot of the window where all the folders are saved. Plots for o2,N2 and Co2 species.

THEORY:

FILE PARSING:

     Parsing is the process of analyzing a string of symbols either in natural language,computer language or data structures confoming to the rules of a formal grammar. Some parsing algorithms may generate a parse forest ot list of parse trees for a syntactically ambiguous input.

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

NASA THERMODYNAMIC FILE:

     NASA thermodynamics file is a file in which THERMO.dat file are assigned. There are temperature range and 53 species are given in these data and each species contains 14 coefficients and temperature ranges and we have to prase these 14 coefficents to find Specific heat, Entropy and Enthalpy using formulas. 

 Where,

R = Universal Gas Constant

T = Temperature

Cp = Specific Heat(KJ)

S = Entropy(KJ/mol-k)

H = Enthalpy(KJ/mol)

a(i) = coefficients where i = 1 to 14

LIST OF 53 ELEMENTS CONTAINED IN THERMO.dat file:

1.AR

2.C

3.C2H

4.C2H2

5.C2H3

6.C2H4

7.C2H5

8.C2H6

9.C3H7

10.C3H8

11.CH

12.CH2

13.CH2(S)

14.CH2CHO

15.CH20

16.CH2OH

17.CH3

18.CH3CHO

19.CH3O

20.CH3OH

21.CH4

22.CN

23.CO

24.CO2

25.CO3

26.H

27.H2

28.H2CN

29.H2O

30.H2O2

31.HCCO

32.CCOH

33.HCN

34.HCNN

35.HCNO

36.HCO

37.HNCO

38.HNO

39.HO2

40.HOCN

41.N

42.N2

43.N2O

44.NCO

45.NH

46.NH2

47.NH3

48.NNH

49.NO

50.NO2

51.O

52.O2

53.OH

SPECIFIC HEAT:

     The specific heat is the amount of heat per unit mass required to raise the temperature by one degree Celusis. The relationship between heat and temperature change is usually expressed in the form shoen below where Cp is the specific heat.

Cp = R*(a1 + (a2*T) + (a3*(T).^2) + (a4*(T).^3) + (a5*(T).^4));

ENTROPY:

     The entropy is measure of a systems thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from the ordered molecular motion, the amount of entropy is also a measure of the molecular disorder (or) randomness of a system.

S = R*(a1.*log(T) + (a2*T) + ((a3*(T).^2)/2) + ((a4*(T).^3)/3) + ((a5*(T).^4/4) +a7));

ENTHALPY:

     Enthalpy is a property of a thermodynamic system is equal to the system internal energy plus the product of its pressure and volume. In a system contained so as to prevent mass transfer for processes at constant pressure, the heat absorbed or released equals the change in enthalpy. 

H = R*T.*(a1 + ((a2*T)/2) + ((a3*(T).^2)/3) + ((a4*(T).^3)/4) + ((a5*(T).^4)/5) + ((a6./T)));

MAIN PROGRAM:

clear all
close all
clc


%Input 
R = 8.314;

%Reading Data File
f1 = fopen('THERMO.dat','r');
get = fgetl(f1);

%Reading Temperature from this file
temp = fgetl(f1);

%Splitting
s = strsplit(temp);

%Converting strings cells into numbers
templow = str2num(s{2});
tempmid = str2num(s{3});
temphigh =str2num(s{4});

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

%skiping comment line
for i = 1:3
    get = fgetl(f1);
end

%Reading the Species
for j = 1:53
    tline = fgetl(f1);
    A = strsplit(tline,' ');
    species = (A{1});

    %converting string to number
    lowtemp = str2num(A{end-3});
    midtemp = str2num(A{end-2});
    hightemp = str2num(A{end-1});

    tline1 = fgetl(f1);
    a = strfind(tline1, 'E');

    %Finding Higher coefficients
    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 lower coeeficients
    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(1:c(1)+4:c(2)+3);
    a12 = str2num(a12);
    a13 = tline3(1:c(1)+4:c(3)+3);
    a13 = str2num(a13);
    a14 = tline3(1:c(1)+4:c(4)+3);
    a14 = str2num(a14);

    %Calculate specific heat
    Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);

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

    %Calculate Enthalpy
    H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid);

    %Calculate molecular weight of the species
    W = Molecular_weight(species);

    %Writing molecular weight
    MW_file = fopen('Molecular_weight. txt','W');
    fprintf(MW_file,'Molecular weight of %s is %d',species,W);
    fclose(MW_file);

    %ploting Cp, H, S with respect to temperature
    %saving the output in a folder
    cd('C:\Users\surya\OneDrive\Documents\MATLAB\challenges') 
    mkdir(species);
    cd(species);

    %ploting Cp Vs T
    figure(1)
    plot(T,Cp,'LineWidth',3,'Color','r');
    xlabel('TEMPERATURE(K)');
    ylabel('SPECIFIC HEAT(KJ)');
    title(sprintf('SPECIFIC HEAT VS TEMPERATURE RANGE OF %s', species));
    grid on
    
    saveas(figure(1),'Specific Heat.jpg');

    %ploting S Vs T
    figure(2)
    plot(T,S,'LineWidth',3,'Color','b');
    xlabel('TEMPERATURE(K)');
    ylabel('ENTROPY(KJ/mol-K)');
    title(sprintf('ENTROPY VS TEMPERATURE RANGE OF %s', species));
    grid on
    saveas(figure(2),'Entropy.jpg');

    %ploting H Vs T
    figure(3)
    plot(T,H,'LineWidth',2,'Color','g');
    xlabel('TEMPERATURE');
    ylabel('ENTHALPY(KJ/mol)');
    title(sprintf('ENTHALPY VS TEMPERATURE RAANGE OF %s', species));
    grid on
    saveas(figure(3),'Enthalpy.jpg');
    
    cd('C:\Users\surya\OneDrive\Documents\MATLAB\challenges')

    W = Molecular_weight(species);

end

    

PROGRAM EXPLANATION:

1.Input the parameter R and we will open a file THERMO.dat file and reading this file using fopen command.

2. We will read the first THERMO using fgetl command and then we read temperature file and we will split the temperatures using strsplit command to convert it into cell array and then we will convert this string to number using str2num command to find low,high and mid temperatures.

3.Then we will assign temperature range using linspace command. Then  we run loop to skip command lines the data file using fgetl command inside the loop.

4.We run the loop to find 53 element in the data file by assingmimg fgetl command and then spliting into the cell array to find the species line and then find the species name and then find the number of the tempeatures in the species line.

5.Then the new line is readed.The coeeficient of data file we will first find the 'E' string using strfind command to find before and after numbers of the coefficients then we will find remaining 14 coefficients and convert them to numbers from strings.

6.Out of this we wil find first 7 coefficients which are for Higher temperature and then Second 7 coefficients which are for Lower temperature.

7.Now we wil write the molecular_weight.txt file and fprintf all the molecular weights of the species. Then close the txt file using fclose command.

8.Here We change the firectory using cd command and then creating directory folder where we have to save our files using mkdir command and then we will change the directory to this created folder.

9.Now we will plot Specific heat, Entropy, Enthalpy with the Temperature for different species.

10.We will use sccanf command to in the title command to create different species title.

11.Then we will use saveas command to save the plots of different species in the directory folder.Now we will change the directory to which our main code file runs in MATLAB. 

FUNCTION TO FIND SPECIFIC HEAT:

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


%calculating specific heat
%calculating 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

EXPLANATION:

1.Here we will find the Specific heat by creating Specific heat function and assigning its releated formula coefficient from 14 coefficients,Gas constant,Temperature and mid-temperature.

2.Now we will create an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficent in the formula.

3.Then we will use else cindition to specify coefficient of Higher coefficient in the formula.

4.Then end the loop and function to calculate Specific Heat.

 

FUNCTION TO FIND ENTROPY:


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

%calculating entropy
%calculating entropy for higher and lower temperature 

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

EXPLANATION:

1.Here we will find the Entropy by creating Entropy function and assigning its related formula coefficent from 14 coefficients,Gas constant, Temperature and mid-temperature.

2.Now we will create an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficient in the formula.

3.Then we will use else condition to specify coefficient of Higher temperature coefficient in the formula.

4.Then end the loop and function to calculate Entropy.

FUNCTION TO FIND ENTHALPY:

function [H] = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)

%calculating enthalpy
%calculating enthalpy for higher and lower temperature

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)/5) + ((a6./T)));
end
end

EXPLANATION:

1.Here we will find the Enthalpy by creating Enthalpy function and assigning its releated fomula coefficient from 14 coefficients,Gas constant,Temperature and mid temperature.

2.Now we will create an condition using if loop and make the temperature less than and equal to mid temperature to specify lower temperature coefficient in the formula.

3.Then we will use else condition to specify coefficient of higher temperature coefficient in the formula.

4.Then end the loop and function to calculate Enthalpy.

FUNCTION TO FIND MOLECULAR WEIGHT OF THE DIFFERENT SPECIES:

function [W] = Molecular_weight(species)

%Defining Atomic name of the species
atomic_name = ['H', 'C', 'O', 'N', 'Ar'];

%Defining Atomic masses of main species
atomic_weight = [1.008 12.001 15.999 14.007 39.948];

%molecular weight starting range
W = 0;

for i = 1:length(species)
    for j = 1:length(atomic_name)
        if strcmp(species(i),atomic_name(j))
            W = W + atomic_weight(j);
            position = j;
        end
    end
    
    n = str2num(species);

    if n>1
        W = W + atomic_weight(position)*(n-1);
    end
end

fprintf('Molecular Weight of %s : ',species);
fprintf('%f', W')
disp('  ')
end

EXPLANATION:

1.We create an Molecular_weight function W and assigning species input to that.

2.Then define the main element of all 53 species in an array atomic_name that are: "H","C","O","N","Ar".

3.Then by taking above main elements we will create there atomic_weight array from any source like internet that are:1.008 12.011 15.999 14.007 39.948.

4.Then we will assign molecular starting range W to zero and then create an for loop condition which runs to the length of the species and and another nested for loop is created which runs to the length of the string atomic_name.

5.Then we will create an condition using if condition and use strcmp command to compare the species and atomic_name strings to calculate molecular masses if by comparsion both string found same then we will assign its molecular weight.

6.Now we will string to the number using str2num command assign to n. Then we run an condition of if loop to find weather the species contains more than one element if there are more than 1 elements that is n>1 then we will increment the atomic weight by the multiplication of position and n-1 to the atomic_weight array.

7.Then we wil print the Molecular weight using printf command and display it using disp command.

ERRORS WHILE PROGRAMING:

1.

Here i mentioned the linewidth with in quotes. By seeing command window i rectifed my error.

2.

Here i type grid on command without space.By seeing command window i rectifed my error.

3. 

Here typo error is made. By seeing command window i identify and rectifie my error.

4.

Here i made typo error in indicating ylabel.By seeing command window i rectified error.

OUTPUT:

PLOTS:

1.PLOT OF SPECIE OF O2:

PLOTS OF SPECIE N2:

PLOTS OF SPECIES CO2:

SCREENSHOT OF THE DIRECTORY WHERE DIFFERENT SPECIES ARE SAVED:

Here is the screen shot for all 53 species stored.

OUTPUT OF MOLECULAR WEIGHT OF THE DIFFERENT SPECIES:

Molecular Weight of O : 15.999000  
Molecular Weight of O : 15.999000  
Molecular Weight of O2 : 15.999000  
Molecular Weight of O2 : 15.999000  
Molecular Weight of H : 1.008000  
Molecular Weight of H : 1.008000  
Molecular Weight of H2 : 1.008000  
Molecular Weight of H2 : 1.008000  
Molecular Weight of OH : 17.007000  
Molecular Weight of OH : 17.007000  
Molecular Weight of H2O : 17.007000  
Molecular Weight of H2O : 17.007000  
Molecular Weight of HO2 : 17.007000  
Molecular Weight of HO2 : 17.007000  
Molecular Weight of H2O2 : 17.007000  
Molecular Weight of H2O2 : 17.007000  
Molecular Weight of C : 12.001000  
Molecular Weight of C : 12.001000  
Molecular Weight of CH : 13.009000  
Molecular Weight of CH : 13.009000  
Molecular Weight of CH2 : 13.009000  
Molecular Weight of CH2 : 13.009000  
Molecular Weight of CH2(S) : 13.009000  
Molecular Weight of CH2(S) : 13.009000  
Molecular Weight of CH3 : 13.009000  
Molecular Weight of CH3 : 13.009000  
Molecular Weight of CH4 : 13.009000  
Molecular Weight of CH4 : 13.009000  
Molecular Weight of CO : 28.000000  
Molecular Weight of CO : 28.000000  
Molecular Weight of CO2 : 28.000000  
Molecular Weight of CO2 : 28.000000  
Molecular Weight of HCO : 29.008000  
Molecular Weight of HCO : 29.008000  
Molecular Weight of CH2O : 29.008000  
Molecular Weight of CH2O : 29.008000  
Molecular Weight of CH2OH : 30.016000  
Molecular Weight of CH2OH : 30.016000  
Molecular Weight of CH3O : 29.008000  
Molecular Weight of CH3O : 29.008000  
Molecular Weight of CH3OH : 30.016000  
Molecular Weight of CH3OH : 30.016000  
Molecular Weight of C2H : 13.009000  
Molecular Weight of C2H : 13.009000  
Molecular Weight of C2H2 : 13.009000  
Molecular Weight of C2H2 : 13.009000  
Molecular Weight of C2H3 : 13.009000  
Molecular Weight of C2H3 : 13.009000  
Molecular Weight of C2H4 : 13.009000  
Molecular Weight of C2H4 : 13.009000  
Molecular Weight of C2H5 : 13.009000  
Molecular Weight of C2H5 : 13.009000  
Molecular Weight of C2H6 : 13.009000  
Molecular Weight of C2H6 : 13.009000  
Molecular Weight of CH2CO : 41.009000  
Molecular Weight of CH2CO : 41.009000  
Molecular Weight of HCCO : 41.009000  
Molecular Weight of HCCO : 41.009000  
Molecular Weight of HCCOH : 42.017000  
Molecular Weight of HCCOH : 42.017000  
Molecular Weight of H2CN : 27.016000  
Molecular Weight of H2CN : 27.016000  
Molecular Weight of HCN : 27.016000  
Molecular Weight of HCN : 27.016000  
Molecular Weight of HNO : 31.014000  
Molecular Weight of HNO : 31.014000  
Molecular Weight of N : 14.007000  
Molecular Weight of N : 14.007000  
Molecular Weight of NNH : 29.022000  
Molecular Weight of NNH : 29.022000  
Molecular Weight of N2O : 30.006000  
Molecular Weight of N2O : 30.006000  
Molecular Weight of NH : 15.015000  
Molecular Weight of NH : 15.015000  
Molecular Weight of NH2 : 15.015000  
Molecular Weight of NH2 : 15.015000  
Molecular Weight of NH3 : 15.015000  
Molecular Weight of NH3 : 15.015000  
Molecular Weight of NO : 30.006000  
Molecular Weight of NO : 30.006000  
Molecular Weight of NO2 : 30.006000  
Molecular Weight of NO2 : 30.006000  
Molecular Weight of HCNO : 43.015000  
Molecular Weight of HCNO : 43.015000  
Molecular Weight of HOCN : 43.015000  
Molecular Weight of HOCN : 43.015000  
Molecular Weight of HNCO : 43.015000  
Molecular Weight of HNCO : 43.015000  
Molecular Weight of NCO : 42.007000  
Molecular Weight of NCO : 42.007000  
Molecular Weight of CN : 26.008000  
Molecular Weight of CN : 26.008000  
Molecular Weight of HCNN : 41.023000  
Molecular Weight of HCNN : 41.023000  
Molecular Weight of N2 : 14.007000  
Molecular Weight of N2 : 14.007000  
Molecular Weight of AR : 39.948000  
Molecular Weight of AR : 39.948000  
Molecular Weight of C3H8 : 13.009000  
Molecular Weight of C3H8 : 13.009000  
Molecular Weight of C3H7 : 13.009000  
Molecular Weight of C3H7 : 13.009000  
Molecular Weight of CH3CHO : 42.017000  
Molecular Weight of CH3CHO : 42.017000  
Molecular Weight of CH2CHO : 42.017000  
Molecular Weight of CH2CHO : 42.017000 

Here is the molecular weight of all species.

WORK SPACE COMMAND:

OBSERVATION:

     The listed above all 6 points of objective is programed and output is dispalyed and verified.

RESULT:

     Thus the  program code to the parse the NASA thermodynamic data file and calculate the thermodynamic properties of various gas species using MATLAB programed sucessfully.

GOOGLE DRIVE LINK:

https://drive.google.com/folderview?id=1TKSVAV7V5I2u99i0Jdm7pUnRgQRZBzKU

REFRENCES:

1.https://www.quora.com/What-does-it-means-to-parse-a-file

2.https://www.sciencedirect.com/topics/engineering/thermodynamic-data

3.https://www.engineeringtoolbox.com/heat-capacity-d_338.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.