Challenge

Project 1 - Parsing NASA thermodynamic data

AIM: Calculating THERMODYNAMIC PROPERTIES of molecules using file parsing.

1. Calculating thermodynamic properties for the local temprature range of molecules.

2. Reading the data from a file name THERMO.dat

3. Saving the calculated graph for enthaply,entropy and specific heat

4. Giving user input to calculated the results for a perticular file.

Thermodynamics:

Thermodynamics deals with the concepts of heat and temperature and the inter-conversion of heat and other forms of energy.

[source:wikipedia]https://en.wikipedia.org

Thermodynamics is a branch of physics that deals with heat, work, and temperature, and their relation to energy, radiation,

and physical properties of matter. The behavior of these quantities is governed by the four laws of thermodynamics which convey

a quantitative description using measurable macroscopic physical quantities, but may be explained in terms of microscopic constituents

by statistical mechanics. Thermodynamics applies to a wide variety of topics in science and engineering, especially physical chemistry,

biochemistry, chemical engineering and mechanical engineering, but also in other complex fields such as meteorology.

Chemical Thermodynamics:

Chemical thermodynamics is the study of the interrelation of heat and work with chemical reactions or with physical changes of state within

the confines of the laws of thermodynamics. Chemical thermodynamics involves not only laboratory measurements of various thermodynamic properties,

but also the application of mathematical methods to the study of chemical questions and the spontaneity of processes.

LAWs of Thermodynamics : [source:byjus.com]

  • First law of thermodynamics: Energy can neither be created nor be destroyed, it can only be transferred from one form to another.

`deltaU=Q-W`

where:U is internal energy of the system, Q is the amount of heat rejected or recived by the system, W is the work done by the syste

  • Second law of thermodynamics: The entropy of any isolated system always increases.

`deltaS>=0`

where S is the entropy of the system

  • Third law of thermodynamics: The entropy of a system approaches a constant value as the temperature approaches absolute zero.

`S=0 , T=0`

where S is the entropy of the system and T is the temprature in kelvins

  • Zeroth law of thermodynamics: If two thermodynamic systems are in thermal equilibrium with a third system separately are in thermal equilibrium with each other.

Thermodynamic Properties:

1. Specific heat capacity (`c_p`): [source:onlinelibrary]

The specific heat capacity (cp) of a substance is the heat capacity of a sample of the substance divided by the mass of the sample.

It is the amount of energy that must be added, to one unit of mass of the substance in order to cause an increase of one unit in temperature.

The SI unit of specific heat is joule per kelvin and kilogram, J/(K kg).

The specific heat capacity often varies with temperature, and is different for each state of matter.

`c_p-c_v=TValpha^2/beta`

where c_p and c_v is specific heat at constant pressure and contant volume respectively .

T is temprature V is volume`alpha` is thermal expansivity and `beta` is compressability.

To calculate the specific heat capacity of a molecule for its local temprature range :

`cp=R*(a1+a2*t+a3*t^2+a4*t^3+a5*t^4)`

Specific Heat Capacity Function:

As given that the first 7 co-efficents are for high range of temprature and next 7 is for the low range so will use the if condition to satisfy the condition

1.Input the nessary argumnets for the function like temprature co-efficents, temprature range and value of medium local temprature.

2.Define the function name and pass the values

3.define variable cp and specify the array with zeros function for fast processing.

4.use for loop to calculate the cp for all temp range.(length of t)

5. check with if condition for lower and upper temp range for seprate calculation

6.use a1 to a7 for high temp range calculation and b1 to b7 for lower temp range.

7. store all the cp value and return it in the 1st line where function is defined.

%function is called with temp_coef and local mid temp and temp range of
%local temp
function cp=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,t,R,l_mid_temp)
cp=zeros(1,length(t));
for i=1:length(t)
if t(i)>l_mid_temp%condition for using high temp coeficent
    cp(i)=R.*(a1+(a2.*t(i))+(a3.*(t(i).^2))+(a4.*(t(i).^3))+(a5.*(t(i).^4)));
else
    cp(i)=R.*(b1+(b2.*t(i))+(b3.*(t(i).^2))+(b4.*(t(i).^3))+(b5.*(t(i).^4)));
end
end
end

2. Enthalpy (H):

Enthalpy is the measurement of energy in a thermodynamic system. The quantity of enthalpy equals the total content of heat of a system, equivalent to the system’s internal energy plus the product of volume and pressure.

Mathematically, the enthalpy, H, equals the sum of the internal energy, E, and the product of the pressure, P, and volume, V, of the system.

`H= U +PV`

where H is enthalpy ,U is internal energy , p is pressure and V is volume.

To calculate the enthalpy of a molecule for its local temprature range :

`h=R*t*(a1+a2.*t/2+a3*t^2/3+a4*t^3/4)+a5*t^4/5+a6/t)`

Enthalpy Function:

As given that the first 7 co-efficents are for high range of temprature and next 7 is for the low range, so will use the if condition to satisfy the condition

1.Input the nessary argumnets for the function enthalpy() like temprature co-efficents, temprature range and value of medium local temprature.

2.Define the function name and pass the values

3.define variable h and specify the array with zeros function for fast processing.

4.use for loop to calculate the h values for all temp range.(length of t)

5. check with if condition for lower and upper temp range for seprate calculation

6.use a1 to a7 for high temp range calculation and b1 to b7 for lower temp range.

7. store all the h value and return it in the 1st line where function is defined.

%function is called with temp_coef and local mid temp and temp range of
%local temp
function h=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,t,R,l_mid_temp)
h=zeros(1,length(t));
for i=1:length(t)
if t(i)>l_mid_temp%condition for using high temp coeficent
    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).*(b1+((b2.*t(i))./2)+((b3.*(t(i).^2))./3)+((b4.*(t(i).^3))./4)+((b5.*(t(i).^4))./5)+b6./t(i)));
end
end
end

3.Entropy (S):

Entropy, the measure of a system’s thermal energy per unit temperature that is unavailable for doing useful work.

Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.

The idea of entropy provides a mathematical way to encode the intuitive notion of which processes are impossible, even though they would not violate the

fundamental law of conservation of energy.

By the Clausius definition, if an amount of heat Q flows into a large heat reservoir at temperature T above absolute zero, then the entropy increase is:

`deltaS=Q/T`

where S is entropy , Q is Heat and T is temprature.

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

Entropy Function:

As given that the first 7 co-efficents are for high range of temprature and next 7 is for the low range, so will use the if condition to satisfy the condition

1.Input the nessary argumnets for the function entropy() like temprature co-efficents, temprature range and value of medium local temprature.

2.Define the function name and pass the values

3.define variable s and specify the array with zeros function for fast processing.

4.use for loop to calculate the s values for all temp range.(length of t)

5. check with if condition for lower and upper temp range for seprate calculation.

6.use a1 to a7 for high temp range calculation and b1 to b7 for lower temp range.

7. store all the h value and return it in the 1st line where function is defined.

%function is called with temp_coef and local mid temp and temp range of
%local temp
function s=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,t,R,l_mid_temp)
s=zeros(1,length(t));
for i=1:length(t)
if t(i)>l_mid_temp %condition for using high temp coeficent
    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.*(b1.*log(t(i))+(b2.*t(i))+(b3.*(t(i).^2)./2)+(b4.*(t(i).^3)./3)+(b5.*(t(i).^4)./4)+b7);  
end
end 
end

Ploting the graphs and saving the data:

Ploting graphs of :

1.Specific heat capacity vs Local temprature range

2.Entropy of molecules vs Local temprature range

3.Enthalpy of molecules vs Local temprature range

and then saving this data to the local folder sorted to a sub folder with their species name.

Saving data to the current folder path:

1. using 'cd' command chose the working directery passing pwd to identify the current folder.

CD: display current folder :cd()

pwd:pwd returns the path to the current folder.

2.using mkdir command to make a new directory to save the calculated results (graphs).

mkdir: makes new folder :'mkdir(folderName')creates the folder folderName

3. again using mkdir to create a directory of the molecule name to save perticular file of that molecule.

4.saveas command is used to save the files(graphs)

saveas: save figure to specific file format :saveas(fig,filename,formattype)

5.movefile is used to moves the molecules name folder to a single folder.

movefile:Move or rename file or folder:movefile(source,destination)

Ploting function:

1. the function starts with naming the function and passing the required arguments cp,s,h,t and name variable

2. dr_name variable stores the name of the moleculeand is used to make the directory of same to store its plots.

3. then dr_name folder is made and using cd command current directory is chosen as mol_data.

4. for figure 1 ,figure 2 and figure 3 first plots are made using plot command and are saved to a variable p1,p2,p3 respectively.

5. title, xlable,ylable are used to put informations on plots.

6. then using cd, directory is changed to perticular molecule folder.

7. using saveas command to save the p1,p2 and p3 figure by specifying their name.

8. usind cd ../ a directory operator the directory is level up. twice.

%ploting function is called with values of specific heat entropy enthalpy
%local time range and molecule name 
function ploting(cp,h,s,t,name)

dr_name=name;%to save the specific file for a molecule
cd('mol_data')%current directoy is used to save the file
mkdir(dr_name);%a folder of molecular name is created

%specific heat plot is made with local temp range
figure(1)
p1=plot(t,cp,'linewidth',1.5,'color','r');
title(sprintf('specific heat vs temprature for %s:',name))
    xlabel('temprature (K) kelvin')
    ylabel('specific heat (cp) J/Kelvin*mol')
    grid on;

%enthaply plot is made with local temp range
figure(2)
p2=plot(t,h,'linewidth',1.5,'color','b');
title(sprintf('enthaply vs temprature for %s:',name))
    xlabel('temprature (K)Kelvin')
    ylabel('enthalpy (h)KJ/mol')
    grid on;
    
%entropy plot is made with local temp range    
figure(3)
p3=plot(t,s,'linewidth',1.5,'color','g');
title(sprintf('entropy vs temprature for %s:',name))
    xlabel('temprature (K)Kelvin')
    ylabel('entropy (s)KJ/mol kelvin')
    grid on;
%directory is changed to dr_name(i)   
cd(dr_name)

%all 3 plots are saved
saveas(p1,'cp_vs_temp.png')
saveas(p2,'enthapy_vs_temp.png')
saveas(p3,'entropy_vs_temp.png')
cd ../%directory is leveled up to mold_data directory  
cd ../%directory is leveled up to current working directory
end
    

Molecular Weight :

Molecular weight, also called molecular mass, mass of a molecule of a substance, based on 12 as the atomic weight of carbon-12.

It is calculated in practice by summing the atomic weights of the atoms making up the substance’s molecular formula.

so by reading the name of the file we can get the molecular mass as it has the atoms name in them.

mol_wt function:

Molecules inthe the given file consist of ayom of

1. Carbon (C) : 12

2. Oxygen (O) : 16

3. Nitrogen (N) : 14

4.Hydrogen (H) : 1

5. Argon (Ar) : 40

so these symbols in the name are matched with these character to get their presence and their related mass is added, the number ahead of these symbol represent the number of atoms present.

note* as to read the atomic wt of argon the character R is then read to save the argon weight.

1. function is defined and passed name variable as argument.

2. a variable s is made which saves a array of symbols " 'H' 'C' 'N' 'O' 'R' " and another array atomic_wt storing their wt is defined. and a m_wt variable is defined as 0.

3. fprintf command is used to display molecular name.

4. loop is used to acces the characters and numbers fron=m the name variable

  1. first loop is used to acess the character on name string (runs for the length of name)
  2. 2nd loop is used to acess the character from the s array(runs for the length of s array)
  3. if the name(i) matches with any of the s array character then it adds its corosponding atomic mass to m_wt
  4. and its position is strored
  5. then the inner loop is exited
  6. then it check for the number in the string name(i)
  7. if its true then that num is multiplied by the privious atom data thats position is saved in the inner loop.
  8. ouyyer loop is exited

5. the m_wt is displayed

6. function returns m_wt data

%function is called with molecular name
function m_wt = mol_wt(name)
s={'H' 'C' 'N' 'O' 'R'};%charcter are stored to match the atoms

%for display of result
fprintf('MOLECULAR WEIGHT OF ');
fprintf(name);
fprintf(':');

%atomic weight of atoms are stotred corosponding to atoms
atomic_wt= [1 12 14 16 40];
m_wt=0;%mol_wt is set to 0

%for loop is used to acess the atoms from string name
for i=1:length(name) % runs for length of name
    for j=1:length(s)% runs for the length of s
        if strcmp(name(i),s(j))% compare both string 
            %if atom matches
            m_wt=m_wt+atomic_wt(j);%atomic wt of same is added to mol wt
            
            pos=j;%its position is saved
        end% if condition ends
    end %inner loop exits
    %check if its a num.
    n=str2num(name(i));
    if n>1 %if true
        m_wt=m_wt+atomic_wt(pos)*(n-1);%if true then privious wt is multipled to it and added to mol wt
    end
end %outer loop ends
%shows the mol_wt
fprintf('%d',m_wt);
disp('');
end

Main function :

The main function reads the data fron the file THERMO.dat and extract the usefull data for the calculations.

file is accessed with the fopen comand and line is read with fgetl.

Reading pattern for the file:

1.first five line :

line 1: header of the file

line 2: global temp range

line 3 to 5: comments

2. Repeating pattern of molecular data:

Each data of molecule has 4 lines

line 1: contains the name of molecule and local tempraute range

line 2 to 4 : each line holds the temprature coeficent first 7 being high temp coeficents and next 7 being low temp coeficents.

each coeficent has a letter E that defines one coeficent.

Main function:

1.close all clear all clc is used to clear the comand window .

2.then THERMO.dat file is opened using fopen command in read mode and link is stored in a variable name data.

3. R is assigned the value of universal gas constant

4.file_header stores the header name of the file using fgetl.

5.the the next line stores the global temp range that is extracted using fgetl to get the second line and data sperated by a space

so the space is find in the string and the data location are extracted

6.mol_data folder is made to save all this data collectively

7.the next 3 lines are comments so that can be excluded.

8.there are 53 molecules in the file and all of them follows the same patter so we will use a loop to extracted that data

9.MOLecular_WT.txt a file is made to save the molecular weight and local tempraure range of the molecules.

10.for loop is runed for 53 molecules (runs for 53 times)

  1. this pattern has 4 line that repeats for each molecules.
  2. use fgetl to get the first line and use strtok to get the first part of the line that is the name of the molecule
  3. then using strfind to find the location of the character 'G' to get the local temprature.
  4. then using a if statment to get the local temp using the position relative to G.
  5. using strdouble and line position we extract the local temprature.
  6. if condition is used here because HCN and CH2OH has 2 G .
  7. then using linspace we create a range of temprature from local_min_temp and local_max_temp.
  8. then the next three line are extracted using fgetl which have the temprature coeficents.
  9. for these 3 line use strfind to get location of E character and then using its location find the coeficents
  10. to store the coeficents use strdouble to change string in num and there are 14 coeficents so firast 7 are stored in a1,a2,a3....a7
  11. next 7 are stored in b1,b2,b3.......b7
  12. then using function specific heat coeficents , entropy and enthaply is calculated
  13. ploting function is used to plot the graphs and store them
  14. then in MOLecular_WT.txt file molecular weight and local temprature is saved.
    loop ends.
%to close the previously opened files and to clear the cmd window
close all 
clear all
clc

%fopen cmd is used to open the THERMO.dat file 
%variable data is assing to use this file
data=fopen('THERMO.dat','r');%file is open in read mode

%R is assigned the value of universal gas const.
R=8.3143;

%heard of the file is read as its the first line 
file_header=fgetl(data);%first line from the file is read



%the next line stores the value of global temprature ranges 
%temp_range stores the second line
temp_range=fgetl(data);%2nd line is read



% these global temprature are not used in function


%to get the values split the string and get the values
global_temp=strsplit(temp_range,' ');%strplit is used to split the sting by ' ' dilimiter
%str2num is used convert the strings to num. 
g_min_temp=str2num(global_temp{2});%1st value is global min temp.
g_mid_temp=str2num(global_temp{3});%2nd value is global mid temp.
g_max_temp=str2num(global_temp{4}); %3rd value is global max temp.






%next 3 lines are comment so we can skip them
%for loop is used for that
for i=1:3
    skip=fgetl(data);%3rd ,4th,5th lines are skiped
end



%file named mOLecular_WT.txt is generated to store the molecular_wt of the
%molecule
F1=fopen('MOLecular_WT.TXT','w');%opened in write mode


mkdir('mol_data')%to store all the molecule in one file


%to get the data of coeficent from the file 
%we will run the loop for all 53 molecules because there data follow same
%pattern
for i=1:53
    
    % to read the first line which holds 
    %molecule name 
    %local temp range
   
    line1=fgetl(data);%%1st pattern line is read
    
    %to get the name as its the first part of the string
   %strtok is used to take the first part from the line_pattern_1
    name=strtok(line1);%name stores the molecule name

    %to get local min mid and max temp futher line is used as the are after
    %G then location of G is found using strfind nad t_c stores the
    %location
    t_c=strfind(line1,'G');
    
    % to get the temp two patterns are there as two G are there in CH2OH
    % and HCN so two case are used 
    %for all other compund
   if t_c>44 %conditon as this is the position we have for G
       %line1 values are extracted with is string pointers. 
       %and str2double is used to convert it into floating numbers
       %using 1st position of G
l_min_temp=str2double(line1(t_c(1)+4:t_c(1)+11));%local min temp is extracted
l_max_temp=str2double(line1(t_c(1)+13:t_c(1)+19));%local mid temp is extracted
l_mid_temp=str2double(line1(t_c(1)+21:t_c(1)+29));%local max temp is extracted
   else
       %similarly for CH2OH and HCN but with 2nd place of G
l_min_temp=str2double(line1(t_c(1)+29:t_c(1)+36));%local min temp is extracted
l_max_temp=str2double(line1(t_c(1)+38:t_c(1)+45));%local mid temp is extracted
l_mid_temp=str2double(line1(t_c(1)+47:t_c(1)+54));%local max temp is extracted
   end
   
   %use this local temp points to create a range of tempratue
t=linspace(l_min_temp,l_max_temp,600);

%line2, line3 and line4 stores the next 3 lines
    line2=fgetl(data);%pattern line 2 is extracted
    line3=fgetl(data);%pattern line 3 is extracted
    line4=fgetl(data);%pattern line 4 is extracted
    
    %a pattern is found where vales are around E
    %2&3 line has 5 values and 4thline has 4 values
    %so location of these E are stored
    a=strfind(line2,'E');%a stores the location of Es in line1
    b=strfind(line3,'E');%b stores the location of Es in line2
    c=strfind(line4,'E');%c stores the location of Es in line3
    %a is use to store the high temp and b is used to store the low temp
    %first five coeficent are extracted from line2
    a1=str2double(line2(1:a(1)+3));%1st value is stored using 1st location of E
    a2=str2double(line2(a(1)+4:a(2)+3));%2nd value is stored using 2nd location of E
    a3=str2double(line2(a(2)+4:a(3)+3));%3rd value is stored using 3rd location of E
    a4=str2double(line2(a(3)+4:a(4)+3));%4th value is stored using 4th location of E
    a5=str2double(line2(a(4)+4:a(5)+3));%5th value is stored using 5th location of E
    
    %same for 3rd line 
    a6=str2double(line3(1:b(1)+3));%6th value is stored using 1st location of E
    a7=str2double(line3(b(1)+4:b(2)+3));%7th value is stored using 2nd location of E
    %b is used for low temp
    b1=str2double(line3(b(2)+4:b(3)+3));%8th value is stored using 3rd location of E
    b2=str2double(line3(b(3)+4:b(4)+3));%9th value is stored using 4th location of E
    b3=str2double(line3(b(4)+4:b(5)+3));%10th value is stored using 5th location of E
    %same for 4thline
    b4=str2double(line4(1:c(1)+3));%11th value is stored using 1st location of E
    b5=str2double(line4(c(1)+4:c(2)+3));%12th value is stored using 2nd location of E
    b6=str2double(line4(a(2)+4:c(3)+3));%13th value is stored using 3rd location of E
    b7=str2double(line4(a(3)+4:c(4)+3));%14th value is stored using 4th location of E
    
    % entropy is calculated using entropy fn.
    s=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,t,R,l_mid_temp);
    %enthalpy is created using enthalpy fn.
    h=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,t,R,l_mid_temp);
    %specific heat is calculated using specific heat function
    cp=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,t,R,l_mid_temp);
    
    % ploting fn. is crreated to plot and store those plots
    ploting(cp,h,s,t,name);
    %mol_wt  fun is called to calculate the molecular weight
    molecular_wt(i)= mol_wt(name);%molecular_wt stores that value
        
    %to store the value  os the molecular wt and local temp in F1 link to
    %MOLecular_WT.txt file
    fprintf(F1,' %s',name);%name is stored
    fprintf(F1,'\n MOLECULAR WEIGHT OF %s ',name);
    fprintf(F1,' %f',molecular_wt(i));%weigth is stored
    %local temprature points are stored
    fprintf(F1,'\n local low temp. %f ',l_min_temp);
    fprintf(F1,'\n local mid temp. %f ',l_mid_temp);
    fprintf(F1,'\n local max temp. %f ',l_max_temp);
    fprintf(F1,'\n maxtemp COEF.     mintemp COEF.');
    fprintf(F1,'\n %f     %f',a1,b1);
    fprintf(F1,'\n %f     %f',a2,b2);
    fprintf(F1,'\n %f     %f',a3,b3);
    fprintf(F1,'\n %f     %f',a4,b4);
    fprintf(F1,'\n %f     %f',a5,b5);
    fprintf(F1,'\n %f     %f',a6,b6);
    fprintf(F1,'\n %f     %f',a7,b7);
    fprintf(F1,'\n\n');
   
%to give details of results in cmd window  
fprintf('\nMOLECULE ');
fprintf(name);
fprintf(': SAVED!\n');
    
end
%loop is closed
%both files are closed
fclose(F1);%MOLecular.txt file is closed

fclose(data);%THERMO.dat file is closed

RESULTS :

Main program Data output during running mol_data folder created to save data
folder mol_data inside molecule folder MOLecular_WT.txt file

The results of the main program is as desired:

1.files are stored in their perticular folder of molecule name that is save inside the mol_data folder.

2. the MOLecular_WT.txt file contains all the data of the molecule in form of text file as instructed. Its also shows all the values that are read from the THERMO.dat file

AS the readings of the MOLecular.txt file and the graphs obtained by the results are cross varified and the program runs sucessfully.

USER INPUT FUNCTION :

this is the replacemt for the main function as it takes the user command to calculate the specific molecule and to store that data.

it dont stores all the data of all molecules but specified to calculate and stores things only on user command.

1. Functions used :

  • dialog box is used to input the name of the molecule from the user.

prompt = {'Enter name(in_capitals only):'}; : prompt is used to display the text
dlgtitle ='Input'; : title of the dialog box
dims = [1 35]; : dialogbox dimension is set
str = inputdlg(prompt,dlgtitle,dims); :str variable is used to save the input

  • while loop is used to run the loop on user demand and the counter that is set to 1 is defined by user at the end of loop so that the user can chose to re run the code.
  • after geting the user input these are compared with molecules name using strcmp and then a if comand is used to check the result
  • if its true then the coeficents are calculated and you will asked to save the data or not
  • if not it will show molecules not found
  • input comand is used with prompt to display the asked respons in command window.
  • while strcmp is 0 for a loop then the next 3 line for that molecule must be excluded .

using: if k==0
skip=fgetl(data);
skip=fgetl(data);
skip=fgetl(data);
end

%ALL codes are same as main file expect taking the value
close all 
clear all
clc
data=fopen('THERMO.dat','r');

R=8.3143;

file_header=fgetl(data);

temp_range=fgetl(data);%2nd line is read


for i=1:3
    skip=fgetl(data);
end



n=1;


while n==1
    



%dialog box cmd is used to input the data  
    
%dialog box cmd is used to input the data    
prompt = {'Enter name(in_capitals only):'};%prompt is used to display the text 
dlgtitle ='Input';%input of data from user
dims = [1 35];%dialogbox dimension is set
strc = inputdlg(prompt,dlgtitle,dims);%str variable is used to save the input
str=string(strc);    

for i=1:53
    
    % to read the first line which holds 
    %molecule name 
    %local temp range
   
    line1=fgetl(data);%%1st pattern line is read
    
    %to get the name as its the first part of the string
   %strtok is used to take the first part from the line_pattern_1
    name=strtok(line1);%name stores the molecule name
    
     %strcmp is used to compare molecular name with input string
    k=strcmp(name,str);
    
    %if the string comparision is true 
    if k==1

    %to get local min mid and max temp futher line is used as the are after
    %G then location of G is found using strfind nad t_c stores the
    %location
    t_c=strfind(line1,'G');
    
    % to get the temp two patterns are there as two G are there in CH2OH
    % and HCN so two case are used 
    %for all other compund
   if t_c>44 %conditon as this is the position we have for G
       %line1 values are extracted with is string pointers. 
       %and str2double is used to convert it into floating numbers
       %using 1st position of G
l_min_temp=str2double(line1(t_c(1)+4:t_c(1)+11));%local min temp is extracted
l_max_temp=str2double(line1(t_c(1)+13:t_c(1)+19));%local mid temp is extracted
l_mid_temp=str2double(line1(t_c(1)+21:t_c(1)+29));%local max temp is extracted
   else
       %similarly for CH2OH and HCN but with 2nd place of G
l_min_temp=str2double(line1(t_c(1)+29:t_c(1)+36));%local min temp is extracted
l_max_temp=str2double(line1(t_c(1)+38:t_c(1)+45));%local mid temp is extracted
l_mid_temp=str2double(line1(t_c(1)+47:t_c(1)+54));%local max temp is extracted
   end
   
   %use this local temp points to create a range of tempratue
t=linspace(l_min_temp,l_max_temp,600);



%line2, line3 and line4 stores the next 3 lines
    line2=fgetl(data);%pattern line 2 is extracted
    line3=fgetl(data);%pattern line 3 is extracted
    line4=fgetl(data);%pattern line 4 is extracted
    
    %a pattern is found where vales are around E
    %2&3 line has 5 values and 4thline has 4 values
    %so location of these E are stored
    a=strfind(line2,'E');%a stores the location of Es in line1
    b=strfind(line3,'E');%b stores the location of Es in line2
    c=strfind(line4,'E');%c stores the location of Es in line3
    %a is use to store the high temp and b is used to store the low temp
    %first five coeficent are extracted from line2
    a1=str2double(line2(1:a(1)+3));%1st value is stored using 1st location of E
    a2=str2double(line2(a(1)+4:a(2)+3));%2nd value is stored using 2nd location of E
    a3=str2double(line2(a(2)+4:a(3)+3));%3rd value is stored using 3rd location of E
    a4=str2double(line2(a(3)+4:a(4)+3));%4th value is stored using 4th location of E
    a5=str2double(line2(a(4)+4:a(5)+3));%5th value is stored using 5th location of E
    
    %same for 3rd line 
    a6=str2double(line3(1:b(1)+3));%6th value is stored using 1st location of E
    a7=str2double(line3(b(1)+4:b(2)+3));%7th value is stored using 2nd location of E
    %b is used for low temp
    b1=str2double(line3(b(2)+4:b(3)+3));%8th value is stored using 3rd location of E
    b2=str2double(line3(b(3)+4:b(4)+3));%9th value is stored using 4th location of E
    b3=str2double(line3(b(4)+4:b(5)+3));%10th value is stored using 5th location of E
    %same for 4thline
    b4=str2double(line4(1:c(1)+3));%11th value is stored using 1st location of E
    b5=str2double(line4(c(1)+4:c(2)+3));%12th value is stored using 2nd location of E
    b6=str2double(line4(a(2)+4:c(3)+3));%13th value is stored using 3rd location of E
    b7=str2double(line4(a(3)+4:c(4)+3));%14th value is stored using 4th location of E
    
    % entropy is calculated using entropy fn.
    s=entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,t,R,l_mid_temp);
    %enthalpy is created using enthalpy fn.
    h=enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,t,R,l_mid_temp);
    %specific heat is calculated using specific heat function
    cp=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,t,R,l_mid_temp);
    
    %a ploting function is created to show the plot not to save them
    ploting_ws(cp,h,s,t,name)
    
    molecular_wt(i)=mol_wt(name);
    fprintf('\n\n')
 
    %user input to save this data
   % prompt to show the text
prompt ='DO YOU WISH TO SAVE DATA?(PRESS (1 TO SAVE)/ PRESS ANY OTHER TO CONT.):';
p=input(prompt);%input is saved to p
if p==1% if desired then plot is saved
    ploting(cp,h,s,t,name);
    fprintf('\n SAVED!!\n');
else %if not  then plot is not saved
    fprintf('\n\nNOT SAVED!!\n\n');
end

  
    break
    end
    
    if k==0
     skip=fgetl(data);
     skip=fgetl(data);
     skip=fgetl(data);
    end
    
end

%if the strcmp failed then it showed the error as commanded
if k==0
    fprintf('Error compound not found !! \n \n')
end

%user input to cont. the calculation or to terminate
prompt='Do you wish to find about another compound \n(press 1 to cont. and any other to terminate):';
n=input(prompt);

end%str variable is used to save the input
    

Ploting_ws function:

it is the same function as ploting but it dont create any directory or save any files in any directory it just shows the data and graph in ral time.

%same as plotting fn. but dont saves the graph
function ploting_ws(cp,h,s,t,name)
figure(1)
plot(t,cp,'linewidth',1.5,'color','r')
title(sprintf('specific heat vs temprature for %s:',name))
    xlabel('temprature (t)')
    ylabel('specific heat (cp)')
    grid on;

figure(2)
plot(t,h,'linewidth',1.5,'color','b')
title(sprintf('enthaply vs temprature for %s:',name))
    xlabel('temprature (t)')
    ylabel('enthalpy (h)')
    grid on;
    
    
figure(3)
plot(t,s,'linewidth',1.5,'color','g')
title(sprintf('entropy vs temprature for %s:',name))
    xlabel('temprature (t)')
    ylabel('entropy (s)')
    grid on;
    
end

RESULTS:

USER INPUT through DIAlog box RESULTS are shown in real time (not saved)
Command asking the user to save the data or not Command window asking if you want to search once more.

The above user input function runs sucessfully as its main aim was to

1. Show the selected result as input by the user.

2.ask user is he wants to save that data

3. ask user to re run the program for another data.

and it waas able to perform the task sucessfully.

The plots of O2 ,N2 and CO2 are shown below:

O2:

Specific heat coeficent vs temp for O2 Enthalpy vs temp for O2
Entropy vs temp for O2 O2 values from MOLeclular_WT.txt file

The results of oxygen O2 molecule can be varified through

for enthalpy and entropy:

link: O2 thermodynamic property

for specific heat:

link : O2 specific heat data

this link contains the data of oxygen molecule

Results for O2 is verified.

N2:

Specific heat coeficent vs temp for N2 Enthalpy vs temp for N2
Entropy vs temp for N2 N2 values from MOLeclular_WT.txt file

The results of oxygen N2 molecule can be varified through

for enthalpy and entropy:

link: N2 thermodynamic property

for specific heat:

link: N2 heat capacity data

this link contains the data of N2 molecule

Results for N2 is verified

CO2:

Specific heat coeficent vs temp for CO2 Enthalpy vs temp for CO2
Entropy vs temp for CO2 CO2 values from MOLeclular_WT.txt file

The results of oxygen CO2 molecule can be varified through

for enthalpy and entropy:

link: CO2 thermodynamic property

for specific heat:

link: CO2 heat capacity data

this link contains the data of CO2 molecule

Results for CO2 is verified

ERROR encountered during Project:

Error 1

forgot to uplevel the directory to current folder.

solution:

cd ../ isused to levelup the directory

Error 2

in user intracting program compounds are not found..


solution:

it was because once the strcmp was false or 0 it was exited while it should read all the next 3 lines so that the for loop runs 53 to get all the data.

Refrence:

Specific heat capacity (`c_p`): [source:onlinelibrary]

link: O2 thermodynamic property

link : O2 specific heat data

link: N2 thermodynamic property

link: N2 heat capacity data

link: CO2 heat capacity data

link: CO2 thermodynamic property

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.