Challenge

Parsing NASA thermodynamic data

Parsing NASA thermodynamic data

AIM:

To write a MATLAB program to parse the NASA thermodynamic data file. Calculate the thermodynamic properties of various gas species. Plot the Specific heat(Cp), Enthalpy(H) and Entropy(S) for the local temperature range specified for each species.

THEORY:

1) Parsing

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. In other words, file parsing refers to how information can be read from a particular file text or any type of file and how that information can be used to in a computer program.

Text parsing is a common programming task that splits the given sequence of characters or values(text) into smaller parts based on some rules. It has been used in a wide variety of applications ranging from simple file parsing to large scale natural language processing. For example, NASA came up with polynomials such as Cp, H, and S. They have also documented the coefficients that are required to evaluate these polynomials for 1000+ species

2) NASA Thermodynamic Data

'THERMO.dat' file is a data file containing 14 coefficients for 53 different elements given by NASA. Their coefficients are used to calculate the enthalpy, entropy, and specific heats for all the species in the data file. Each species also has its own range of local temperatures.

3) Specific Heat Capacity, Cp(kJ/kg-K)

specific Heat Capacity is the amount of heat to be supplied to (or taken out of) the unit mass of a system in order to increase (or decrease) its temperature by one degree in a thermodynamic process in which quantity X is imposed.

4) Enthalpy, H(kJ)

It is a thermodynamic quantity equivalent to the total heat content to a system. It is equal to the internal energy of the system plus the product of pressure and volume.

5) Entropy, S(kJ/K)

Entropy is the measure of a systems 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.

CODE AND EXPLANATION:

clear all
close all
clc

%Inputs
%Universal gas constant R
R = 8.314;

%Opening the data file
f = fopen('THERMO.dat','r');

heading = fgetl(f);    %reading the header

Global_temp_range = fgetl(f);
Global_temp_range = str2num(Global_temp_range);

%reading comments
line1 = fgetl(f);
line2 = fgetl(f);
line3 = fgetl(f);

%Iterating the loop for 53 species 

for i = 1:53
line(0) = fgetl(f);
species_name = strtok(line0);

%Finding G in line0

a = findstr(line0,'G  ');
local_temp = line0(a+1:end-1);
local_tep = str2num(local_temp);

% Local temperature range

t = linspace(local_temp(1),local_temp(2),500);

line1 = fgetl(f);
line2 = fgetl(f);
line3 = fgetl(f);

%finding the position of E
   a = strfind(line1,'E');
   b = strfind(line2,'E');
   c = strfind(line3,'E');

   %High temp coefficients
   h1 = str2double(line1(1:a(1)+3));
   h2 = str2double(line1(a(1)+3:a(2)+3));
   h3 = str2double(line1(a(2)+4:a(3)+3));
   h4 = str2double(line1(a(3)+4:a(4)+3));
   h5 = str2double(line1(a(4)+4:a(5)+3));
   h6 = str2double(line2(1:b(1)+3));
   h7 = str2double(line2(b(1)+4:b(2)+3)); 

   %Low temp coefficients
   l1 = str2double(line2(b(2)+4:b(3)+3));
   l2 = str2double(line2(b(3)+4:b(4)+3));
   l3 = str2double(line2(b(4)+4:b(5)+3));
   l4 = str2double(line3(1:c(1)+3));
   l5 = str2double(line3(c(1)+4:b(2)+3));
   l6 = str2double(line3(c(2)+4:b(3)+3));
   l7 = str2double(line3(c(3)+4:b(4)+3));

   %Calculating Specific Heat, Enthalpy, Entropy and molecular weight

   Cp = specific_heat(l1,l2,l3,l4,l5,l6,h1,h2,h3,h4,h5,t,R,local_temp);
   H = enthalpy(l1,l2,l3,l4,l5,l6,h1,h2,h3,h4,h5,t,R,local_temp);
   S = entropy(l1,l2,l3,l4,l5,l6,h1,h2,h3,h4,h5,t,R,local_temp);
   mw(i)= molecular_weight(species_name);

%saving the plots in multiple folders for each species

e = cd(pwd);
folder = species_name;
mkdir(folder);

% plot specific heat vs temperature

figure(1)
plot(t,Cp,'linewidth',1,'color','g')
title(sprintf('Specific Heat vs Temperature plot for %s',species_name))
xlabel('Temperature')
ylabel('Specific Heat')

% plot Enthalpy vs temperature

figure(2)
plot(t,H,'linewidth',1,'color','r')
title(sprintf('Enthaly vs Temperature plot for %s',species_name))
xlabel('Temperature')
ylabel('Enthalpy')


% plot entropy vs temperature

figure(1)
plot(t,S,'linewidth',1,'color','b')
title(sprintf('Entropy vs Temperature plot for %s',species_name))
xlabel('Temperature')
ylabel('Entropy')

%saving the plots

cd(folder)
saveas(1,'Cp_vs_temp.png')
saveas(2,'H_vs_temp.png')
saveas(3,'S_vs_temp.png')
cd(e)


end

Enthalpy function code:

function H = enthalpy(l1,l2,l3,l4,l5,l6,h1,h2,h3,h4,h5,h6,t,R,local_temp)

if t > local_temp(3)
    H = R*(l1.*t+l2.*(t.^2)./2+13.*(t.^3)./3+14.*(t.^4)./4+15.*(t.^5)./5+16);

else
    H = R*(l1.*t+h2.*(t.^2)./2+h3.*(t.^3)./3+h4.*(t.^4)./4+h5.*(t.^5)./5+h6);
end

Entropy function code:

function S = entropy(l1,l2,l3,l4,l5,l7,h1,h2,h3,h4,h5,h7,t,R,local_temp)

if t > local_temp(3)
    S = R*(l1.*log(t)+l2.*t+13.*(t.^2)./2+14.*(t.^3)./3+15.*(t.^4)./4+17);

else
    S = R*(h1.*log(t)+h2.*t+h3.*(t.^2)./2+h4.*(t.^3)./3+h5.*(t.^4)./4+h7);
end

Specific heat function code:

function Cp = specific_heat(l1,l2,l3,l4,l5,h1,h2,h3,h4,h5,t,R,local_temp)

if t > local_temp(3)
    S = R*(l1+l2.*(t)+13.*(t.^2)+14.*(t.^3)+15.*(t.^4));

else
     S = R*(h1+h2.*(t)+h3.*(t.^2)+h4.*(t.^3)+h5.*(t.^4));
end

Molecular weight function code:

function mw = molecular_weight(species_name)

species = ['H' 'C' 'N' 'O' 'S' 'R'];
atomic_weight = [1 12 14 16 32 40];

mw = 0;
for i = 1:length(species_name)
  for j = 1:length(species)
    if strcmpi(species_name(i),species(j))
      mw = mw + atomic_weight(j);
      position = j;
    end
  end
  n = str2num(species_name(i));
  if n>1
    mw = mw + atomic_weight(position)*(n-1);
  end
  end
  fprintf('Molecular weight of %s is : ',species_name);
  fprintf('%d\n',mw);
end

Explanation:

The 'thermo.dat' file is saved in the same directory were the Matlab files are saved.

The functions such as 'enthalpy', 'entropy', 'specific_heat' and 'molecular weight' are defining using the equations stated.

In the main code, necessary inputs such as universal gas content value are assigned to variable R and the 'fopen' command is used to open the thermo.dat' file.

Header information is extracted using 'fgetl' command.

Global temperature range is stored in the form of numbers using 'str2num' command and then the comments in the net three lines are skipped.

For loop is used to read the data for all the 53 species one at a time.

The required data from line0 to line3.

Line0 describes the name of the species and the local temperature range for the species.

Line1 to line3 contains 14 coefficients for the species out of which first 7 are high-temperature coefficients and the next 7 are low-temperature coefficients.

These coefficients are extracted and converted from string data to numbers using 'str2double' command.

Then the local temperature range is assigned to variable t for the plots.

The defined functions are called to calculate the corresponding properties of the species.

Finally 'enthalpy', 'entropy', and 'specific heat' are plotted against temperature.

These plots are saved in the subdirectories for each of the 53 species labelled with the species name.

RESULTS:

Plots for O2, N2 and CO2 species

Specific heat vs Temperature plot for O2

Enthalpy vs Temperature plot for O2

Entropy vs Temperature plot for O2

Specific heat vs Temperature plot for N2

Enthalpy vs Temperature plot for N2

Entropy vs Temperature plot for N2

Specific heat vs Temperature plot for CO2

Enthalpy vs Temperature plot for CO2

Entropy vs Temperature plot for CO2

Screenshot of the window where all the folders are saved:

The molecular weight of each species displayed it in the command window:

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.