Project

Parsing NASA thermodynamic data using MATLAB

Objective-

  1. Reading \"NASA thermodynamic data\" file and write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species.
  2. Calculate the molecular weight of each species and display it in the command window.
  3. Plot the Cp, Enthalpy and Entropy for the local temperature range.
  4. Save the plots as images with appropriate names and dump them in separate folders for each species.

MATLAB Code-

clear all
close all
clc

%initial values
R = 8.314;

f1 = fopen(\'THERMO1.txt\',\'r\')

%header information
fgetl(f1);
a = fgetl(f1);
fgetl(f1);
fgetl(f1);
fgetl(f1);
A = strsplit(a,\' \');
global_low_temp  = str2num(A{2});
global_mid_temp  = str2num(A{3});
global_high_temp = str2num(A{4});

%species information
for i =1:53
    Species = fgetl(f1);
    Species_name = Species(1:6)
    local = findstr(Species,\'.\');
    local_low_temp  = str2num(Species(local(1)-4:local(1)+4));
    local_mid_temp  = str2num(Species(local(3)-4:local(3)+4));
    local_high_temp = str2num(Species(local(2)-4:local(2)+4));
        
if (local_low_temp < global_low_temp) && (local_high_temp > global_high_temp)
    temp = linspace(global_low_temp,global_high_temp,100);

elseif (local_low_temp > global_low_temp) && (local_high_temp > global_high_temp)
    temp = linspace(local_low_temp,global_high_temp,100);
    
elseif (local_low_temp < global_low_temp) && (local_high_temp < global_high_temp)
    temp = linspace(global_low_temp,local_high_temp,100);
    
else
    temp = linspace(local_low_temp,local_high_temp,100);
end

first_line = fgetl(f1);
b1 = findstr(first_line,\'E\');
coefficient1  = str2num(first_line(1:b1(1)+3));
coefficient2  = str2num(first_line(b1(1)+4:b1(2)+3));
coefficient3  = str2num(first_line(b1(2)+4:b1(3)+3));
coefficient4  = str2num(first_line(b1(3)+4:b1(4)+3));
coefficient5  = str2num(first_line(b1(4)+4:b1(5)+3));
second_line = fgetl(f1);
b2 = findstr(second_line,\'E\');
coefficient6  = str2num(second_line(1:b2(1)+3));
coefficient7  = str2num(second_line(b2(1)+4:b2(2)+3));
coefficient8  = str2num(second_line(b2(2)+4:b2(3)+3));
coefficient9  = str2num(second_line(b2(3)+4:b2(4)+3));
coefficient10 = str2num(second_line(b2(4)+4:b2(5)+3));
third_line = fgetl(f1);
b3 = findstr(third_line,\'E\');
coefficient11 = str2num(third_line(1:b3(1)+3));
coefficient12 = str2num(third_line(b3(1)+4:b3(2)+3));
coefficient13 = str2num(third_line(b3(2)+4:b3(3)+3));
coefficient14 = str2num(third_line(b3(3)+4:b3(4)+3));

%creating folder for each species
mkdir([\'C:\\Users\\MIHIR\\Downloads\\Nasa File Parsing\\Species\\\',Species_name]);

%Calculation of thermodynamic properties

    for j = 1:length(temp)
     if temp(j)> global_mid_temp
         Cp = R*(coefficient1 + coefficient2.*temp + coefficient3.*temp.^2 + coefficient4.*temp.^3 + coefficient5.*temp.^4);
         H  = R.*temp.*(coefficient1 + (coefficient2.*temp)/2 + (coefficient3.*temp.^2)/3 + (coefficient4.*temp.^3)/4 + (coefficient5.*temp.^4)/5 + coefficient6./temp);
         S  = R*(coefficient1*log(temp) + coefficient2.*temp + (coefficient3.*temp.^2)/2 + (coefficient4.*temp.^3)/3 + (coefficient5.*temp.^4)/4 + coefficient7);
     else
         Cp = R*(coefficient8 + coefficient9.*temp + coefficient10.*temp.^2 + coefficient11.*temp.^3 + coefficient12.*temp.^4);
         H  = R.*temp.*(coefficient8 + (coefficient9.*temp)/2 + (coefficient10.*temp.^2)/3 + (coefficient11.*temp.^3)/4 + (coefficient12.*temp.^4)/5 + coefficient13./temp);
         S  = R*(coefficient8*log(temp) + coefficient9.*temp + (coefficient10.*temp.^2)/2 + (coefficient11.*temp.^3)/3 + (coefficient12.*temp.^4)/4 + coefficient14);
     end
    end
    %plotting
 
 %temperature vs Cp
 figure(1)
 plot(temp,Cp);
 title(Species_name);
 xlabel(\'Temperature\')
 ylabel(\'Cp\')
 
 %temperature vs Enthalpy
 figure(2)
 plot(temp,H);
 title(Species_name)
 xlabel(\'Temperature\')
 ylabel(\'Enthalpy\')
 
 %temperature vs Entropy
 figure(3)
 plot(temp,S);
 title(Species_name)
 xlabel(\'Temperature\')
 ylabel(\'Entropy\')
 
data = ([\'C:\\Users\\MIHIR\\Downloads\\Nasa File Parsing\\Species\\\',Species_name]);
cd(data)

%saving graphs
saveas(1,\'Cp.jpg\');
saveas(2,\'Enthalpy.jpg\');
saveas(3,\'Entropy.jpg\');

cd ..

%molecular weight

atom = {\'H\',\'C\',\'N\',\'O\',\'AR\'};
atomic_weight = [1 12 14 16 40];
molecular_weight = 0;

 for i = 1:length(Species_name)
    for j = 1:length(atom)
        if strcmp(Species_name(i),atom(j)) 
            
            molecular_weight = molecular_weight + atomic_weight(j)
            c = j;
        end
        
    end
       n = str2num(Species_name(i));
     if n>1
         molecular_weight = molecular_weight + atomic_weight(c)*(n-1)
     end
 end
          
end

Explanation of Code-

  • Take R is the universal gas constant (=8.314).
  • THERMO1 is the notepad file of THERMO which takes as input.
  • f1 = fopen(\'THERMO1.txt\',\'r\')
  • We are reading data from THERMO1 text file and f1 is the data from THERMO1 text file.
  • Now explanation for Header information-
  • fgetl command is used to print the line from f1.
  • \'a\' equals to header information.
  • A = strsplit(a,\' \')
  • Then strsplit command is used for separate of each global temperature whenever it finds \'blank space\' which is basically a string data type and put it in \'A\' variable.
  • Then str2num command is used for change string data type to int data type.
  • The declare global temp with indices of \'A\'.
  • Now explanation for Species local points information-
  • Species information starts from line 6 from THERMO1 file and ends with line 217 and each species has information of 4 lines, that\'s why, there are 53 species.
  • for loop is used for 53 times and inside it, we are doing calculation of each species.
  • \'Species\' equals to first line of species information using fgetl command.
  • \'Species_name\' is name of species which has first 6 character of \'Species\' variable.
  • local = findstr(Species,\'.\')
  • findstr command is used for finding location of any character. Here is finds location of dot(.)
  • Then local_low_temp = str2num(Species(local(1)-4:local(1)+4))
  • Species(local(1)-4:local(1)+4) means, we are dealing with line name \'Species\' and local(1) is first position of (.) It takes value from local(1)-4 means, 4 character before from(.) position to local(4)+4, after 4 character. Then makes it int value using str2num and gives it local_low_temp.
  • Similarly find local_mid_temp and local_high_temp.
  • Comparison between local ranges and global ranges-
  • We have to plot graph in the local temperature range but local temperature range can not be exceed global temperature range, means local temperature range should be subset of global temperature range, for that, if else condition is done.
  • Then find out the value of temperature using linspace command.
  • Now explanation for Species information-
  • Now there are 3 lines for 14 coefficients for each species.
  • Let first_line = fgetl(f1) which gets data of each coeffiicients in first line.
  • Then b1 = findstr(first_line,\'E\')
  • Which finds the position of each E and there are 5 \'E\', 5 \'E\', 4 \'E\' in line 1, line 2 and line 3 respectively.
  • Then coefficient1 = str2num(first_line(1:b1(1)+3))
  • Which is to find coefficient 1 and what does this line means, it was explained above.
  • Same logic will be followed for finding each coefficients.
  • Now create folder with each species using mkdir command
  • mkdir([\'C:\\Users\\MIHIR\\Downloads\\Nasa File Parsing\\Species\\\',Species_name])
  • for each for loop, it will create folder of each species name folder.
  • Explanation of Calculation of thermodynamic properties-
  • for loop is done till the length of temperature(calculated above).
  • before global_mid_temp, low temperature coefficients(first 7 coefficients) are taken and after global_mid_temp, high temperature coefficients(last 7 coefficients) are taken.
  • For above if else condition is done.
  • Then calculate Cp, Enthalpy and Entropy using mentioned formula.
  • Then plot 3 graphs of each thermodynamics properties wrt to temperature and give title, xlabel, ylabel.
  • Explanation of code for Molecular Weight-
  • I make a array of atom(all 5 distinct atoms) and make another array of atomic_weight and give atomic weight of each atom in same order.
  • First for loop is for each species till the length of Species_name.
  • Then inside it, second for loop is done till length of atom.
  • Then strcmp command is used for comparing two strings( one is Species_name and other is atom).
  • If it\'s equal, then molecular weight be summation of atomic weight of each distinct atom of particular species.
  • Then n = str2num(Species_name(i))
  • n basically finds the numerical value in Species_name after strcmp command.
  • Then actual molecular weight will be molecular weight + atomic weight*(n-1).
  • (n-1) is done because one array of Species_name is already was considered in above process.

Output-

Screenshot of the window where all the folders are saved-

1. Plot for O2 species-

2. Plot for N2 species-

3. Plot for CO2 species-

Screenshot of Command Window of Molecular Weight-

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.