Challenge

Project 1 - Parsing NASA thermodynamic data

function code for extracting coefficents, species_name and local_temp for each species

function [coeffs,species_name,local_temp] = get_coeffs(fileID,i)

 %To bring file pointer to intial position of file
 frewind(fileID);

 %specifying header line using TEXTSCAN
 d = textscan(fileID,'%s',1,'delimiter','a', 'headerlines',i-1);
 L = char(d{1});

 %Extracting species name
 species_name = strcat(L(1:7));

 %Extracting local Temperatures
 LT = L(49:75);
 split = strsplit(LT,' ');
 local_temp = str2double(split(1:3));

 %Extracting Coefficients
 fgetl(fileID);
 L1 = fgetl(fileID);  %line 1
 L2 = fgetl(fileID);  %line 2
 L3 = fgetl(fileID);  %line 3

 %spliting coefficients from line strings
 a1 = strfind(L1,'E');
 a2 = strfind(L2,'E');
 a3 = strfind(L3,'E');

 %Extracting Coefficients from line1
 c1  = str2double(L1(1:a1(1)+3));
 c2  = str2double(L1(a1(1)+4:a1(2)+3));
 c3  = str2double(L1(a1(2)+4:a1(3)+3));
 c4  = str2double(L1(a1(3)+4:a1(4)+3));
 c5  = str2double(L1(a1(4)+4:a1(5)+3));

 from line2
 c6  = str2double(L2(1:a2(1)+3));
 c7  = str2double(L2(a2(1)+4:a2(2)+3));
 c8  = str2double(L2(a2(2)+4:a2(3)+3));
 c9  = str2double(L2(a2(3)+4:a2(4)+3));
 c10 = str2double(L2(a2(4)+4:a2(5)+3));

 from line3
 c11 = str2double(L3(1:a3(1)+3));
 c12 = str2double(L3(a3(1)+4:a3(2)+3));
 c13 = str2double(L3(a3(2)+4:a3(3)+3));
 c14 = str2double(L3(a3(3)+4:a3(4)+3));


 coeffs = [c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14];
end
  

the coefficents are stored in array 'coeffs' which is the output to the main program

To find the enthalpy,entropy and specific heat

function [Cp,H,S] = find_properties(coeffs,temp,local_temp)

  %Universal gas constant
  R=8.14; 
  %HIGH-temperature coefficients
  a = coeffs(1:7);
  %LOW-temperature coefficients
  b = coeffs(8:14);
  %temperature
  t = temp;

  %calculating enthalpy, entropy and specific heats 
    for i = 1:length(temp)
    %For HIGH-temperatures
     if t(i) >= local_temp(3)
     Cp(i) = R*(a(1) + a(2)*t(i) + a(3)*t(i)^2 + a(4)*t(i)^3 + a(5)*t(i)^4);
     H(i) = R*t(i)*(a(1)+(a(2)*(t(i)/2))+(a(3)*(t(i)^2/3))+(a(4)*(t(i)^3)/4)+(a(5)*(t(i)^4)/5)+(a(6)/t(i)));
     S(i) = R*(a(1)*log(t(i))+(a(2)*t(i))+(a(3)*(t(i)^2/2))+(a(4)*(t(i)^3)/3)+(a(5)*(t(i)^4)/4)+a(7));
    %For LOW-temperatures
    else
     Cp(i) = R*(b(1) + b(2)*t(i) + b(3)*t(i)^2 + b(4)*t(i)^3 + b(5)*t(i)^4);
     H(i) = R*t(i)*(b(1)+(b(2)*(t(i)/2))+(b(3)*(t(i)^2/3))+(b(4)*(t(i)^3)/4)+(b(5)*(t(i)^4)/5)+(b(6)/t(i)));
     S(i) = R*((b(1)*log(t(i)))+(b(2)*t(i))+(b(3)*(t(i)^2/2))+(b(4)*(t(i)^3)/3)+(b(5)*(t(i)^4)/4)+b(7));
    end    
end


For calculating molecular weights of each species

function [Mol_weight] = molecularweight(species_name)
   
  %Atoms and Atomic Weights   
  atoms = ['H','C','N','O','A'];
  atomic_w = [1.008,12.0107,14.0067,15.9994,39.948];
  
  %Calculating molecular weight of the species
  Mol_weight = 0;
  for i = 1:length(species_name)
   for j = 1:length(atoms)
    %Comparing Specie's name with atoms list
    comp = strcmp(species_name(i),atoms(j));
    if comp == 1   %true
    %Adding atomic weight   
    Mol_weight = Mol_weight + atomic_w(j);
    z = j;
    break
    end
   end
   %Checking integer numbers in specie's name
   N = str2double(species_name(i));
   if N>1
   %Adding (atomic weight of atom * integer number-1)
   Mol_weight = Mol_weight + atomic_w(z)*N-1 ;
   end
  end
end

Now main program

%PRAGRAM FOR FILE PARSING NASA THERMODYNAMIC DATA
close
clear
clc
%Reading NASA Thermodynamic data file
fileID = fopen('THERMO.dat','r');

%Finding no of lines in the data
for i=1:Inf
 line= fgetl(fileID);
 flag=strcmp(line,'END');
 if(flag==1)
  no_of_lines=i;
 break
 end
end

%Finding no of species in the data
no_of_species=(no_of_lines-6)/4;
k = no_of_species;

%Finding Global Temperature Range
%To bring file pointer to initial position of file
frewind(fileID);
File_Name = fgetl(fileID); %reading the first line
data = fgetl(fileID);      %reading the second line
%spitting a string by a delimiter
split = strsplit(data,' ');   
% conversion of string characters to numbers
global_temperatures  = str2double(split(2:4)); 

%creating file directory for saving the plots
mkdir('Species');

%for loop for getting all species properties
for j = 1:no_of_species
 i = (j-1)*4 + 6;
 %Extracting coefficients, species name & Local Temperatures
 [coeffs,species_name,local_temp] = get_coeffs(fileID,i);

 %Finding Molecular Weight for each Species
 [Mol_weight] = molecularweight(species_name);
 %Displaying Molecular Weights in the command window
 fprintf('Molecular Weight of %s = %famu n',species_name,Mol_weight)

 %Calculating Thermodynamic Properties of the species
 %Creating array of local temperature range
 temp = linspace(local_temp(1),local_temp(2),100);
 [Cp , H, S] = find_properties(coeffs,temp,local_temp);
 
 %plotting Cp, Enthalpy and Entropy for the local temperature range
 %Temp vs Specific Heat
 figure(1)
 hold on
 plot(temp,Cp,'black','LineWidth',2)
 title(species_name)
 xlabel('Temperature [K]')
 ylabel('Specific Heat [J/mol-K]')
 grid on
 %Temp vs Enthalpy
 figure(2)
 hold on
 plot(temp,H,'r','LineWidth',2)
 title(species_name)
 xlabel('Temperature [K]')
 ylabel('Enthalpy [J/Mol]')
 grid on
 %Temp vs Entropy
 figure(3)
 hold on
 
 plot(temp,S,'b','LineWidth',2)
 title(species_name)
 xlabel('Temperature [K]')
 ylabel('Entropy [J/Mol-K]')
 grid on

 %storing path of current directory
 initial_folder = pwd; 
 %switching to the directory 'Species'
 cd("Species");   
 %naming to directory 
 mkdir(species_name)            %creating new result directory
 cd(fullfile(species_name));    %switching to result directory
 %saving figures to the result directory
 saveas(figure(1),'Temperature_vs_Specific_heat.png');
 saveas(figure(2),'Temperature_vs_Enthalpy.png');
 saveas(figure(3),'Temperature_vs_Entropy.png');
 %switching back to original directory
 cd(initial_folder); 
end
fclose(fileID);

molecular weights of each species

Molecular Weight of O = 15.999400amu nMolecular Weight of O2 = 46.998200amu nMolecular Weight of H = 1.008000amu nMolecular Weight of H2 = 2.024000amu nMolecular Weight of OH = 17.007400amu nMolecular Weight of H2O = 18.023400amu nMolecular Weight of HO2 = 48.006200amu nMolecular Weight of H2O2 = 49.022200amu nMolecular Weight of C = 12.010700amu nMolecular Weight of CH = 13.018700amu nMolecular Weight of CH2 = 14.034700amu nMolecular Weight of CH2(S) = 14.034700amu nMolecular Weight of CH3 = 15.042700amu nMolecular Weight of CH4 = 16.050700amu nMolecular Weight of CO = 28.010100amu nMolecular Weight of CO2 = 59.008900amu nMolecular Weight of HCO = 29.018100amu nMolecular Weight of CH2O = 30.034100amu nMolecular Weight of CH2OH = 31.042100amu nMolecular Weight of CH3O = 31.042100amu nMolecular Weight of CH3OH = 32.050100amu nMolecular Weight of C2H = 36.040100amu nMolecular Weight of C2H2 = 37.056100amu nMolecular Weight of C2H3 = 38.064100amu nMolecular Weight of C2H4 = 39.072100amu nMolecular Weight of C2H5 = 40.080100amu nMolecular Weight of C2H6 = 41.088100amu nMolecular Weight of CH2CO = 42.044800amu nMolecular Weight of HCCO = 41.028800amu nMolecular Weight of HCCOH = 42.036800amu nMolecular Weight of H2CN = 28.041400amu nMolecular Weight of HCN = 27.025400amu nMolecular Weight of HNO = 31.014100amu nMolecular Weight of N = 14.006700amu nMolecular Weight of NNH = 29.021400amu nMolecular Weight of N2O = 57.019500amu nMolecular Weight of NH = 15.014700amu nMolecular Weight of NH2 = 16.030700amu nMolecular Weight of NH3 = 17.038700amu nMolecular Weight of NO = 30.006100amu nMolecular Weight of NO2 = 61.004900amu nMolecular Weight of HCNO = 43.024800amu nMolecular Weight of HOCN = 43.024800amu nMolecular Weight of HNCO = 43.024800amu nMolecular Weight of NCO = 42.016800amu nMolecular Weight of CN = 26.017400amu nMolecular Weight of HCNN = 41.032100amu nMolecular Weight of N2 = 41.020100amu nMolecular Weight of AR = 39.948000amu nMolecular Weight of C3H8 = 55.114800amu nMolecular Weight of C3H7 = 54.106800amu nMolecular Weight of CH3CHO = 44.060800amu nMolecular Weight of CH2CHO = 43.052800amu n>> 

*couldnt take the screenshot of the command window as the results came in a horizontal way

Screenshot of where all folders are saved

plots for O2

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.