Challenge

Project 1 - Parsing NASA thermodynamic data

Objectives:

  1. To extract all the 14 coefficient from the given data file.
  2. To use those coefficient to find the value of enthalpy, Specific heat capacity and Entropy for individual species for different temperature range and plot them.
  3. To obtain the molecular weight of individual species.

Theory:

Prasing is the process of analyzing a string of symbol either in natural language , computer language or data structure confirming the rules of formal grammar. File prasing can be used in order to extact the data from the particular file so that the data extracted can be used for other purposes.

Code logic:

To extact the data from any file, we have to open the file by considering reading option. Now after opening the file, with the help of fgetl command try to read the file line by line. Identify the character that can be taken as reference to extract necessary data. Now based on the requirement, extact the data from each line. The data which is extracted is in the string format so if we want numerical data than we have to convert string data to numerical value and it can be done by using a command called str2num in matlab. After extacting all the necessary data ,create a function to calculate specific heat capacity, enthalpy ,entropy and molecular weight for each content in a file.

If we want to know the value of particular content than an if statement can be used in such a way that the code will only provide the value you need.

For below codes, the value of molecular weight for individual species is written in a seprate file name table.txt which was created by using fopen command by considering written option.

To obtain the value of all 14 coefficient for each species, for loop statement is used. To create an individual folder for each species a command called mkdir was used and in that folder plot for enthalpy, specific heat and entropy were saved by using saveas command.

Some Important Syntax

1.fileID=fopen('filename','permission')

permission=r and w where w is to write in file and r is read the file content.

2.variable_name=fgetl(fileID);

3.C=strsplit(str,delimter)

4.k=strfind(str,pat)

pat is the character in the string and strfind will provide the location for that character.

5.mkdir(folder name)

6.cd(new folder)

Equation to Find specific heat capacity, Enthalpy , Entropy

`C_p=R(a_1+a_2T+a_3T^2+a_4T^3+a_5T^4)`

`h=RT(a_1+a_2T/2+a_3T^2/3+a_4T^3/4+a_5T^4/5+a_6/T)`

`S=R(a_1ln(T)+a_2T+a_3T^2/2+a_4T^3/3+a_5T^4/4+a_7)`

where Cp is the specific heat capacity, h is enthaly, S is entropy, T is the local temperature and R is the universal gas constant

Steps:

  1. Open the Thermo.txt file by considering reading parameters.
  2. Use for loop for two iteration and get the value for global temperature range.
  3. This temperature range is in the form of string so use strsplit command split the temperature range by using space as delimiter.
  4. Based on the obtain data, change string to number by using str2num command and stored that data as global high medium and low temperature.
  5. Skip 3 lines by using for loop and fgetl command.
  6. Use for loop for infinity and run the loop until fgetl doesnot read the last line of the data file.
  7. Find the name for each species by using possible adjustment.
  8. Now read the local temperature and convert the local temperature string to numerical value.
  9. Use the suitable adjustment to read the coefficient from each line.
  10. Now change those coefficient to number.
  11. Create an array for local temperature in the range of local low temperature and local high temperature.
  12. Create a function for enthalpy, specific heat, entropy and molecular weight.
  13. Create a loop in such a way that, it provide one temperature from temperature array while calling the enthalpy, entropy and specific heat capacity function. And continue the loop until all tha value is passed to the above function.
  14. Get the value for molecular weight for each species and store it in an array.
  15. Use mkdir command to make folder for each species and with help of cd and saveas command , save the plot for specific heat capacity,enthalpy and entropy in that directory. Use cd .. command to return to parent folder.
  16. If reading is completed by use of if statement break the loop.
  17. Now close the file.
  18. Now create a new file name called table.txt by using fopen command to write the value of molecular weight and species name.
  19. Use for loop in order to wirte each value of species and molecular weight and close the file.

In order to get the data for required species asked by user only.

  1. After fifth step ,create an string variable which store the data from the user. The data is the name of species which user want to know the value for coefficient and other paremeter.
  2. Until step number 9 every steps is same.
  3. Now by use of strcmpi command, compare the species and the data given by the user by using if statement. If condition is true proceed from step number 10-15 and using break command to terminate the loop.
  4. If condition is false , than repeat the process until both the strings are same.

Function Codes:

a)Specific heat capacity function code

function [cp]=sp_heat(R,T,a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,lmid)
% for high temperature we use coeff from a1-a7

if (T>lmid)
    cp=R*(a1+a2*T+a3*T^2+a4*T^3+a5*T^4);
    
else
    %     for low temperature we use coeff from a8-a14
    cp=R*(a8+a9*T+a10*T^2+a11*T^3+a12*T^4);
end

end

b)Enthalpy function code

function h=enthalpy(R,T,a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,lmid)
% for high temperature we use coeff from a1-a7
if (T>lmid)
    h=R*T*(a1+a2*T/2+(a3*T^2)./3+(a4*T^3)/4+(a5*T^4)/5+a6/T);
    
else
    %     for low temperature we use coeff from a8-a14
    h=R*T*(a8+a9*T/2+(a10*T^2)/3+(a11*T^3)/4+(a12*T^4)/5+a13/T);
end
end

c)Entropy Function Code:

function s=entropy(R,T,a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,lmid)

% for high temperature we use coeff from a1-a7
if(T>lmid)
    s=R*(a1*log(T)+a2*T+(a3*T.^2)./2+(a4*T.^3)./3+(a5*T.^4)./4+a7);
    
%     for low temperature we use coeff from a8-a14
else
    s=R*(a8*log(T)+a9*T+(a10*T.^2)./2+(a11*T.^3)./3+(a12*T.^4)./4+a14);
end
end

d) Molecular weight function Code

function mol_wt=molecular(species)

% considering initial weight
w=0;

% Creating the array of elements

element=['H','C','N','O','A'];

% A is for argon element

% creating an array for their atomic weight

wt=[1,12,14,16,40];

%since we have to read the species and element array so creating 2 for loop
%to read the element and comparing them

for i=1:length(species)  %To read the element in species
    
    for j=1:length(element) %This loop is used to compare element in species and element array
        
%         comparision
if strcmpi(species(i),element(j))
    w=w+wt(j);
    
    x=j;  %if multiple same element is present in the species than it will help to find molecular weight
end
    end
    
    %to find whether there is multiple element in species or not
    mul=str2num(species(i));
    if (mul>1)
        w=w+(wt(x)*(mul-1));  %-1 is used since mass is already added for first element
    end
    mol_wt=w;
end
        
        

MAIN CODE

a) To obtain the coefficient ,enthalpy, entropy and molecular weight for every species

clear all
close all
clc

r=fopen('THERMO.dat','r');  %opening thermo.dat file to read the data

for i=1:2   %for skiping thermo data line
    rs=fgetl(r);
end

%Getting the value for  Global temperature value
temp=strsplit(rs,' ');
glow=str2num(temp{2}); %Global low temperature
gmid=str2num(temp{3}); %global mid temperature
ghigh=str2num(temp{4}); %Global high temperature


%skipping the unused 3 lines
for i=1:3
    a=fgetl(r);
end

for i=1:inf
    
   %If program reaches at last line loop will terminate 
    a=fgetl(r);
    if strcmp(a,'END')
        disp('File reading is completed');
        break
    end
    
    %Extracting value of local temperature and species name
    
    xyz=strsplit(a,' ');
    species=xyz{1};
    llow=str2num(xyz{end-3});
    lhigh=str2num(xyz{end-2});
    lmid=str2num(xyz{end-1});
    name{i}=species;  %for creating a file which contains name of species and molecular weight
    
    % Extracting Coefficient for 2nd line
    a=fgetl(r);
    b=strfind(a,'E');
  
    a1=a(1:b(1)+3);
    a2=a(b(1)+4:b(2)+3);
    a3=a(b(2)+4:b(3)+3);
    a4=a(b(3)+4:b(4)+3);
    a5=a(b(4)+4:b(5)+3);
    
    % Extracting Coefficient for 3rd line
    a=fgetl(r);
    b=strfind(a,'E');
    
    a6=a(1:b(1)+3);
    a7=a(b(1)+4:b(2)+3);
    a8=a(b(2)+4:b(3)+3);
    a9=a(b(3)+4:b(4)+3);
    a10=a(b(4)+4:b(5)+3);
    
    
    % Extracting Coefficient for 4th line
    a=fgetl(r);
    b=strfind(a,'E');
    
    a11=a(1:b(1)+3);
    a12=a(b(1)+4:b(2)+3);
    a13=a(b(2)+4:b(3)+3);
    a14=a(b(3)+4:b(4)+3);
    
 % % Changing from string to number.
%         
        a1=str2num(a1);
        a2=str2num(a2);
        a3=str2num(a3);
        a4=str2num(a4);
        
        a5=str2num(a5);
        a6=str2num(a6);
        a7=str2num(a7);
        a8=str2num(a8);
        a9=str2num(a9);
        a10=str2num(a10);
        a11=str2num(a11);
        a12=str2num(a12);
        a13=str2num(a13);
        a14=str2num(a14);
        
        R=8.31;  %universal gas constant value
        
        T=linspace(llow,lhigh,500);  %local temperature range
        
%      Calculation of specific heat, enthalpy, entropy and molecular weight

%loop to send individual value of temperature to the function
for w=1:length(T)
    
        Cp(w)=sp_heat(R,T(w),a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,lmid);
        h(w)=enthalpy(R,T(w),a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,lmid);
        s(w)=entropy(R,T(w),a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,lmid);
end
        mol_wt(i)=molecular(species);
        
        
    %Plotting, saving figure and making seprate directory for each species
        
        mkdir(species); %making folder
        
        cd(species); %change Directory
        figure(1)
        plot(T,Cp,'Displayname','Specific heat');
        xlabel('Temperature')
        ylabel('Specific heat');
        title('Specific heat capacity of',species)
        saveas(1,'Specific heat capacity','jpg'); %saving figure in the directory
        
        
        figure(2)
        plot(T,h,'Displayname','Enthaply');
        xlabel('Temperature');
        ylabel('Enthaply');
        title('Enthaply of',species);
        saveas(2,'Enthaply','jpg');
        
        
        figure(3)
        plot(T,s);
        xlabel('Temperature');
        ylabel('Entropy');
        title('Entropy of',species);
        saveas(3,'Entropy','jpg');
        
        cd ..   %Return to parent folder
end
fclose(r);

%Creating a seprate file to store the value of molecular weight of
%individual species

table=fopen('table.txt','w');
fprintf(table,'%10s %20s','Species Name','Molecular Weight');
fprintf(table,'\n');
fprintf(table,'\n');
for k=1:length(name)
    fprintf(table,'%5s %20f',name{k},mol_wt(k));
    fprintf(table,'\n');
end

fclose(table);

b)Codes to obtain the coefficient , specific heat capacity , entropy and Molecular weight for user asked species

clear all
close all
clc

r=fopen('THERMO.dat','r');  %opening thermo.dat file to read the data

for i=1:2   %for skiping thermo data line
    rs=fgetl(r);
end

%Getting the value for  Global temperature value
temp=strsplit(rs,' ');
glow=str2num(temp{2}); %Global low temperature
gmid=str2num(temp{3}); %global mid temperature
ghigh=str2num(temp{4}); %Global high temperature


%skipping the unused 3 lines
for i=1:3
    a=fgetl(r);
end

n=input('Enter the value to from file','s');

for i=1:inf
    
   %If program reaches at last line loop will terminate 
    a=fgetl(r);
    if strcmp(a,'END')
        disp('File reading is completed');
        break
    end
    
    %Extracting value of local temperature and species name
    
    xyz=strsplit(a,' ');
    species=xyz{1};
    llow=str2num(xyz{end-3});
    lhigh=str2num(xyz{end-2});
    lmid=str2num(xyz{end-1});
    name{i}=species;  %for creating a file which contains name of species and molecular weight
    
    % Extracting Coefficient for 2nd line
    a=fgetl(r);
    b=strfind(a,'E');
  
    a1=a(1:b(1)+3);
    a2=a(b(1)+4:b(2)+3);
    a3=a(b(2)+4:b(3)+3);
    a4=a(b(3)+4:b(4)+3);
    a5=a(b(4)+4:b(5)+3);
    
    % Extracting Coefficient for 3rd line
    a=fgetl(r);
    b=strfind(a,'E');
    
    a6=a(1:b(1)+3);
    a7=a(b(1)+4:b(2)+3);
    a8=a(b(2)+4:b(3)+3);
    a9=a(b(3)+4:b(4)+3);
    a10=a(b(4)+4:b(5)+3);
    
    
    % Extracting Coefficient for 4th line
    a=fgetl(r);
    b=strfind(a,'E');
    
    a11=a(1:b(1)+3);
    a12=a(b(1)+4:b(2)+3);
    a13=a(b(2)+4:b(3)+3);
    a14=a(b(3)+4:b(4)+3);
    
    comp=strcmpi(species,n);
    if (comp==1)
        % % Changing from string to number.
%         
        a1=str2num(a1);
        a2=str2num(a2);
        a3=str2num(a3);
        a4=str2num(a4);
        
        a5=str2num(a5);
        a6=str2num(a6);
        a7=str2num(a7);
        a8=str2num(a8);
        a9=str2num(a9);
        a10=str2num(a10);
        a11=str2num(a11);
        a12=str2num(a12);
        a13=str2num(a13);
        a14=str2num(a14);
        
        R=8.31;  %universal gas constant value
        
        T=linspace(llow,lhigh,500);  %local temperature range
        
%      Calculation of specific heat, enthalpy, entropy and molecular weight

%loop to send individual value of temperature to the function
for w=1:length(T)
    
        Cp(w)=sp_heat(R,T(w),a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,lmid);
        h(w)=enthalpy(R,T(w),a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,lmid);
        s(w)=entropy(R,T(w),a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,lmid);
end
        mol_wt=molecular(species);
        
figure(1)
        plot(T,Cp,'Displayname','Specific heat');
        xlabel('Temperature')
        ylabel('Specific heat');
        title('Specific heat capacity of',species)
 figure(2)
        plot(T,h,'Displayname','Enthaply');
        xlabel('Temperature');
        ylabel('Enthaply');
        title('Enthaply of',species);
figure(3)
        plot(T,s);
        xlabel('Temperature');
        ylabel('Entropy');
        title('Entropy of',species);
  break
    end
%     
    
    
end
fclose(r);

%Creating a seprate file to store the value of molecular weight of
%individual species

table=fopen('table.txt','w');
fprintf(table,'%10s %20s','Species Name','Molecular Weight');
fprintf(table,'\n');
fprintf(table,'\n');
for k=1:length(mol_wt)
    fprintf(table,'%5s %20f',species,mol_wt);
    fprintf(table,'\n');
end

fclose(table);

Result:

1)For every Species

1.1 Creation of folder for each Species

Above figure is the screenshot of the folder for all species from the Thermo.txt file which was created automatically by using matlab codes. Each folder of species contains the figure plot related to specific heat capacity, Enthalpy and entropy. Below image shows the figure which was in folder name C2H.

1.2 Molecular Weight for every species

Molecular weight of each element has been written in a file by using fopen and fprintf command. The molecular weight of every species can be accessed through below link.

https://drive.google.com/file/d/1e1bwLCFmqxp0Tgc9KwFhw51PtSnvO4ZG/view?usp=sharing

Screenshot from above link

1.3Specific heat Capacity for O2, N2 and CO2

1.4 Enthalpy for O2,CO2 and N2

1.5 Entropy of O2,CO2 and N2

2.For user define input species

In the above case, user have define the species as CH3OH , when enter is pressed than the following result are obtain.

Now ,the table text file i.e table.txt has only the molecular weight for the species define by the user which is CH3OH. And the plot for Specific heat, Enthalpy and Entropy is obtained for only species CH3OH and are as follow.

Conclusion:

All the 14 coefficient has been extracted from the Thermo.dat file and those coefficient are used to find the value for specific heat capacity, Enthalpy and entropy with different temperature. It was observed that value for specific heat, Enthalpy and Entropy changes with change in temperature.Molecular weight for individual species has been calculated and written in a seprate file name table.txt. Some changes has been made in a code so that the program gives only the coefficient , Specific heat capacity, Enthalpy,Entropy and Molecular weight for user define species.

Reference:

  1. https://en.wikipedia.org/wiki/Parsing, last accessed,Sept 09,2021.
  2. https://in.mathworks.com/help/matlab/, last accessed, Sept 09,2021
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.