Project

PARSING OF NASA THERMODYNAMIC DATA FILE - USING MATLAB

AIM: To write a MATLAB code to parse the NASA\'s thermodynamic polynomial data and calculate the thermodynamic properties such as specific heat,enthalpy and entropy,evaluate the molecular weights of elemenrts involved and plot the thermodynamic properties against temperature of various element.

Steps:

1.The file THERMO.dat is opened and read

2.Then the 14 co-effs. of polynomials are extracted

3. The coefficients are then arranged according to the higher and lower temperature co-effs.

inside the for loop

clear all
close all
clc

R=8.314;%universal gas constant

f1=fopen(\'THERMO.dat\',\'r\');%open the NASA\'s thermo file 

%gettin the 1st line
 fgetl(f1);


%global max,mid,and min temp
T=fgetl(f1);

T_R=strsplit(T);%splitting the terms in to multiple terms
T_L=str2num(T_R{2});
T_mid=str2num(T_R{3});
T_max=str2num(T_R{4});

%temperature range
temp= linspace(T_L,T_max,500);


% neglecting comments
for i=1:3
    fgetl(f1);
end
%reading the lines after neglecting above 3 lines 
for i=1:53
    a=fgetl(f1);
   name=strtok(a);
       %lines containing 7 high temp and 7 low temp co-eff.are read
    tc1=fgetl(f1);
    tc2=fgetl(f1);
    tc3=fgetl(f1);
    
    %position of E to be defined to extract co-eff
    p1=strfind(tc1,\'E\');
    p2=strfind(tc2,\'E\');
    p3=strfind(tc3,\'E\');
    
    %high temp co-eff. extraction
    h1=str2num(tc1(1:p1(1)+3));
    h2=str2num(tc1(p1(1)+3:p1(2)+3));
    h3=str2num(tc1(p1(2)+4:p1(3)+3));
    h4=str2num(tc1(p1(3)+4:p1(4)+3));
    h5=str2num(tc1(p1(4)+4:p1(5)+3));
    h6=str2num(tc2(1:p2(1)+3));
    h7=str2num(tc2(p2(1)+4:p2(2)+3));
    
    %low temp co-eff. extraction
    l1=str2num(tc2(p2(2)+4:p2(3)+3));
    l2=str2num(tc2(p2(3)+4:p2(4)+3));
    l3=str2num(tc2(p2(4)+4:p2(5)+3));
    l4=str2num(tc3(1:p3(1)+3));
    l5=str2num(tc3(p3(1)+4:p3(2)+3));   
    l6=str2num(tc3(p3(2)+4:p3(3)+3));
    l7=str2num(tc3(p3(3)+4:p3(4)+3));
 
    %specific heat calculations 
    cp1 =specific_heat(h1,h2,h3,h4,h5,h6,h7,l1,l2,l3,l4,l5,l6,l7,R,temp,T_L,T_mid,T_max);
    
    %enthalpy calculations 
   H1=enthalpy(h1,h2,h3,h4,h5,h6,h7,l1,l2,l3,l4,l5,l6,l7,R,temp,T_L,T_mid,T_max);
    
    %entropy calculations 
    S1=entropy(h1,h2,h3,h4,h5,h6,h7,l1,l2,l3,l4,l5,l6,l7,R,temp,T_L,T_mid,T_max);
      
    %  molecular weights are written for all elements
    mol_wt(i)=molecular_weight(name);
    
  %creating a folders in element names and saving it in working directory
  cd(pwd)  %change current folder/identify current folder
  directory =name;  %creation of new folders using the elements name in working directory
  mkdir(directory);   make new folder from the created element names
  
  %plotting the graphs for cp,H,S vs temperature
    figure(1)
    hplot1=plot(temp,cp1,\'linewidth\',4,\'color\',\'b\');
    xlabel(\'Temperature\')
    ylabel(\'Specific Heat\')
    title(\'Specific Heat Vs Temperature\')
    
    figure(2)
   hplot2=plot(temp,H1,\'linewidth\',4,\'color\',\'b\');
    xlabel(\'Temperature\')
    ylabel(\'Enthalpy\')
    title(\'Enthalpy Vs Temperature\')
    
    figure(3)
    hplot3=plot(temp,S1,\'linewidth\',4,\'color\',\'b\');
    xlabel(\'Temperature\')
    ylabel(\'Entropy\')
    title(\'Entropy Vs Temperature\')
    
    %Naming the plotted figures and providing the path to save them in their respective created
    %folders
   figure1= sprintf(\'Specific Heat Vs Temperature %d.png\',i);
  figure2=sprintf(\'Enthalpy Vs Temperature %d.png\',i);
  figure3=sprintf(\'Entropy Vs Temperature %d.png\',i);
  fullFileName1=fullfile(directory,figure1);
  fullFileName2=fullfile(directory,figure2);
   fullFileName3=fullfile(directory,figure3);
    
   %saving the plot in the respective folders by following the path created
 saveas(hplot1, fullfile(directory,sprintf(\'fig%01d cp_vs_temp.png\', i)));
 saveas(hplot2, fullfile(directory,sprintf(\'fig2%02d H_vs_Temp.png\', i)));
 saveas(hplot3, fullfile(directory,sprintf(\'fig3%03d S_vs_Temp.png\',i)));
 
end 


4. The specfic heat,enthalpy and entropy is calcuated using the above formula and higher and lower temperture co-eff. respectively.Seperate functions are created for each and called inside the main code of for loop

%function to calculate specific heat
function cp1 =specific_heat(h1,h2,h3,h4,h5,h6,h7,l1,l2,l3,l4,l5,l6,l7,R,temp,T_L,T_mid,T_max)
if temp>=T_mid
    cp1=R*(h1+h2.*temp+h3.*(temp.^2)+h4.*(temp.^3)+h5.*(temp.^4));
else
    cp1=R*(l1+l2.*temp+l3.*(temp.^2)+l4.*(temp.^3)+l5.*(temp.^4));
end
end



%function to calculate enthalpy
function H1=enthalpy(h1,h2,h3,h4,h5,h6,h7,l1,l2,l3,l4,l5,l6,l7,R,temp,T_L,T_mid,T_max)
if temp>=T_mid
    H1=R.*temp.*(h1+h2.*(temp./2)+h3.*((temp.^2)./3)+h4.*((temp.^3)./4)+h5.*((temp.^4)./5)+h6./temp);
else
    H1=R.*temp.*(l1+l2.*(temp./2)+l3.*((temp.^2)./3)+l4.*((temp.^3)./4)+l5.*((temp.^4)./5)+l6./temp);
end
end


%function to calculate entrpoy
function S1=entropy(h1,h2,h3,h4,h5,h6,h7,l1,l2,l3,l4,l5,l6,l7,R,temp,T_L,T_mid,T_max)


if temp>=T_mid
   S1=R*(h1.*log(temp)+h2.*temp+h3.*((temp.^2)./2)+h4.*((temp.^3)./3)+h5.*((temp.^4)./4)+h7);
else
       S1=R*(l1.*log(temp)+l2.*temp+l3.*((temp.^2)./2)+l4.*((temp.^3)./3)+l5.*((temp.^4)./4)+l7);
end
end

5.Function is created to calculate the molecular weights of each elements and then it is called in inside the main code of for loop for generating the respective molecular weights

%function to calculate molecular weight
function [M]= molecular_weight(name)
atoms={\'H\' \'C\' \'N\' \'O\' \'AR\'};%key elemnts that take part in compound generation
fprintf(\'Molecular Weight of\');%printed as defined
fprintf(name);
fprintf(\':\')

%atomic weights of reference elemnst
atomic_weight=[1 12 14 16 40];%atomic weights of elements
M=0;
for i=1:length(name)
    for j=1:length(atoms)
        if strcmp(name(i),atoms(j));
            M=M+atomic_weight(j);
            position=j;
        end
    end
    n=str2num(name(i));
    if n>1
        M=M+atomic_weight(position)*(n-1);
    end
end
fprintf(\'%d\\n\',M);%elements printed and ordered
disp(\'\');%displayed as called above in fprinnf


end





6.The current folders are changed and identified using cd(pwd) command and the folders are created using the mkdir command inside the for loop of main code

7. Figures are plotted against temperature and specific heat,enthaply and entropy respectively using plot command inside the main code of for loop

8.The plotted figures are named and the path to save them inside the corresponding folders is written in the main code for loop

9.Then the graphs are saved in the respective folders using the created path,written in the main code for loop.

10.loop ends

Screen Shots of created folders:

Screen Shots of Molecular Weights:

O2:

CO2:

N2:

Error:

The error is thrown initially when the cd(pwd) commands and save plot command are used as functions and cd(dir) is initiated to save the plot in the folder after the comment from the skill lync support. I directly inserted the cd(pwd) in main code loop and used fullFileName commands accordingly and the error is solved.

(skill lync support comment:you are using Cd(dir) command for the first iteration, it is running in the downloads directory where all the codes are saved then it goes to second iteration your program is running in the O directory because your program is not comes out from the O directory so it shows the error.)

REFERENCES:

https://in.mathworks.com/matlabcentral/answers/354596-how-to-save-multiple-figures-created-by-bar-in-a-loop-with-different-name


https://in.mathworks.com/matlabcentral/answers/181039-can-i-use-for-loop-to-save-multiple-plots

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.