Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

  1. To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
  2. To calculate the molecular weight of each species and display it in the command window.
  3. To plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature: high temperature) specific for each species.

INTRODUCTION:

Parsing in computer languages refers to the syntactic analysis of the input code into its components parts in order to facilitate the writing of compilers and interpreters. It can be used to describe split or separation. Parsing a file means reading in a data stream of some sort and building an in-memory model of the semantic content of that data. This aims at facilitating performing some kind of transformation on the data.

Nasa's Thermodynamic file (THERMO.dat file) contains a temperature range and 53 species where each species contains 14 coefficients.

Using the above formula, we have to find Specific heat, Enthalpy and entropy.

MAIN CODE:

clear all
close all
clc

% Reading data file
% opening file and reading THERMO. dat file
file = fopen( 'THERMO.dat','r')
line1 = fgetl(file);

% Reading Temperature from THERMO.dat file
line2 = fgetl(file);

%spliting the line
s = strsplit(line2) ;

% converting strings cells into numbers
templow = str2num(s{2});
tempmid = str2num(s{3});
temphigh = str2num(s{4});
% Temperature Range
T = linspace(templow,temphigh,1000);

%Skipping comment lines
for i = 1:3
 get=fgetl(file);
end

%lnput % Gas Constant
R=8.314; 

%reading the species
for j = 1:53
    tline = fgetl(file);
    A = strsplit(tline,' '); % splitting cell into array
    species = (A{1});  % To read the name of the species
    
    %converting string to number
    lowtemp = str2num(A{end-3});
    midtemp = str2num(A{end-2});
    hightemp = str2num(A{end-1});
    %getting next line
    tline1 = fgetl(file);
    %finding the string E
    a = strfind(tline1,'E'); 
    
    %Finding First 7 higher and second 7 lower coefficients
    
    %Finding Higher coeffiecients
    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(file);
    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 coefficents
    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(file);
    c = strfind(tline3,'E');
    a11=tline3(1:c(1)+3);
    a11=str2num(a11);
    a12=tline3(c(1)+4:c(2)+3);
    a12=str2num(a12);
    a13=tline3(c(2)+4:c(3)+3);
    a13=str2num(a13);
    a14=tline3(c(3)+4:c(4)+3);
    a14=str2num(a14);
    
    %specific heat calculation
    cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid);
    
    %calculation of entropy
    S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid);
    
    %calculation of enthalpy
    H =Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid);
    
    %calculation of Molecular weight
    W = Molecular_weight(species);
    
    % molecular weight 
    MW_file = fopen('molecular_weight.txt','w');
    fprintf(MW_file, 'Molecular weight of %s is %d',species,W);
    fclose(MW_file); %closing file
    
    %saving folder
    cd('D:\S-L\MATLAB\WORKOUT VIDEOS\NASA THERMODYNAMIC DATA')
    mkdir(species); %creating directory 
    cd(species);
    
    %plot1 - Cp vs T
    figure(1)
    plot(T,cp,'linewidth',2,'color','b');
    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');
    
    
  % plot2 S vs T
   figure(2)
   plot(T,S,'linewidth',2,'color','r');
   xlabel('Temperature');
   ylabel('Entropy(KJ/mol-K)');
   title(sprintf('Entropy Vs Temperature range of %s',species));
   grid on 
   saveas(figure(2),'Entropy.jpg');
    
   % plot3 H vs T
   figure(3)
   plot(T,H,'linewidth',2,'color','r');
   xlabel('Temperature');
   ylabel('Enthalpy');
   title(sprintf('Enthalpy Vs Temperature range of %s',species));
   grid on 
   saveas(figure(3),'Enthalpy.jpg');
   
   cd('D:\S-L\MATLAB\WORKOUT VIDEOS\NASA THERMODYNAMIC DATA')
   
   %Molecular Weight
   W= Molecular_weight(species)
end 

CODE EXPLANATION:

  1. We open the THERMO.dat file and read the first line
  2. We read the temperature and split it and convert the string to a number.
  3. skipping the comments in the next 3 lines, we input the gas constant
  4. As we have 53 species, we use for loop to repeat the code.
  5. The 14 coefficients are read for high and lower coefficients and converted from string to Number.
  6. We find the entropy, specific heat, Molecular weight and enthalpy by calling out the function.
  7. We save the file in the created directory
  8. We plot 3 graphs namely, Temp VS Cp, Temperature Vs H and Temperature Vs S.
  9. These plots for all 53 species are saved in folders in the given directory.
  10. Molecular weight is displayed in the command window.

ENTHALPY:

Formula:

CODE:

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,tempmid)
       %calculating enthalpy for higher and lower temperature respectively
       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 

ENTROPY:

Formula:

CODE:

function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,tempmid)
   % calculating entropy for higher and lower temperature coefficients
   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 

SPECIFIC HEAT:

Formula:

CODE:

function cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid)
    %calculating specific heat for high and low 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 

MOLECULAR WEIGHT:

CODE:

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.011 15.999 14.007 39.948];
     
     W=0; %molecular weight starting range
      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 

MOLECULAR WEIGHT DISPLAYED ON COMMAND WINDOW:

ERRORS WHILE CODING:

CORRECTION:

Screenshot of the window where all the folders are saved:

PLOT FOR O2:

PLOT for N2:

PLOT FOR CO2:

RESULT:

Thus, the enthalpy, entropy and specific heats for all the species in the data file have been calculated. Also, graphs for the entropy, enthalpy and specific heats for all species are plotted.

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.