Project

Parsing NASA thermodynamic data

Aim:To Parse a NASA thermodynamic data

Objective:

1.To write a function that extracts the 14 co-efficients 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.Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.

4.Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.

5.The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H and S plots.

Software Used:Octave

Theory:

Parsing means splitting a file or other input into pieces of data that can be easily stored or manipulated. NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Specific heat, enthalpy and entropy. In this project, the NASA thermodynamic data file is parsed to extract useful information from the file, like species coefficients, temperature ranges etc. The data extracted is used for the calculations and plotting graphs for different species.

Snippet of the starting lines of NASA Thermodynamic data file

There are total 53 species with 14 coefficients for each which are to be used under individual local temperature ranges. We need to extract all the data from the file and calculate the value of Specific heat, enthalpy, entropy and molecular weight.

The first 7 numbers starting on the second line of each species block are the seven coefficients for the high temperature range. The following seven numbers are the coefficients for low temperature range.

The formula to calculate Specific heat, enthalpy and entropy are given below:

Where, a1, a2, a3, a4, a5, a6, a7 are the coefficients

T=Temperature in K

R=Universal Gas Constant

Cp= Specific heat

H= Enthalpy

S= Entropy

Function to calculate Specific Heat,Enthalpy and Entropy

function[Cp,H,S]=thermo_data(T,R,local_mid_temperature,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14)
  
for k=1:length(T)
  if T(k)>local_mid_temperature
  %high temperature coefficients
  Cp=(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4)*R;
  H=(a1+(a2.*T)/2+(a3.*T.^2)/3+(a4.*T.^3)/4+(a5.*T.^4)/5+(a6./T))*R.*T;
  S=(a1.*log(T)+a2.*(T)+(a3.*T.^2)/2+(a4.*T.^3)/3+(a5.*T.^4)/4+a7)*R;
else
  %low temperature coefficients
  Cp=(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4)*R;
  H=(a8+(a9.*T)/2+(a10.*T.^2)/3+(a11.*T.^3)/4+(a12.*T.^4)/5+(a13./T))*R.*T;
  S=(a8.*log(T)+a9.*(T)+(a10.*T.^2)/2+(a11.*T.^3)/3+(a12.*T.^4)/4+a14)*R;
endif
endfor

endfunction

Function to calculate the Molecular Weight

function molecular_mass=molecular_weight(species_name)
species=species_name;
element_name=['H','C','O','N','A'];
atomic_weight=[1.00794 12.011 15.9994 14.00674 39.948];
molecular_mass=0;
for i=1:length(species);
    for j=1:length(element_name);
      if strcmp(species(i),element_name(j));
        molecular_mass=molecular_mass+atomic_weight(j);
        position=j;
      endif
   endfor

n=str2double(species(i));
if n>1;
  molecular_mass=molecular_mass + atomic_weight(position)*(n-1);
endif
endfor
disp(sprintf('Molecular Weight of %sn = %f u n',species_name,molecular_mass))

endfunction

Main Program

clear all
close all
clc

%Getting the NASA THERMO.dat file
f1=fopen('THERMO.dat','r');
fgetl(f1);

%reading the header information
A=fgetl(f1);
A=strsplit(A,' ');
global_low_temperature=str2num(A{2});
global_mid_temperature=str2num(A{3});
global_high_temperature=str2num(A{4});

%skipping the next 3 lines
for i=1:3
  fgetl(f1);
endfor

%reading the species
for i=1:53;
  header=fgetl(f1);
  speciesheader=strsplit(header,' ');
  species_name=speciesheader{1};
  X=strfind(header,'G');
  local_low_temperature=str2double(header(48:55));
  local_mid_temperature=str2double(header(68:75));
  local_high_temperature=str2double(header(58:65));
  
  

%reading the first line coefficients
line1=fgetl(f1);
a=strfind(line1,'E');
a1=str2double(line1(1:a(1)+3));
a2=str2double(line1(a(1)+4:a(2)+3));
a3=str2double(line1(a(2)+4:a(3)+3));
a4=str2double(line1(a(3)+4:a(4)+3));
a5=str2double(line1(a(4)+4:a(5)+3));

%reading the second line coefficients
line2=fgetl(f1);
b=strfind(line2,'E');
a6=str2double(line2(1:b(1)+3));
a7=str2double(line2(b(1)+4:b(2)+3));
a8=str2double(line2(b(2)+4:b(3)+3));
a9=str2double(line2(b(3)+4:b(4)+3));
a10=str2double(line2(b(4)+4:b(5)+3));

%reading the third line coefficients
line3=fgetl(f1);
c=strfind(line3,'E');
a11=str2double(line3(1:c(1)+3));
a12=str2double(line3(c(1)+4:c(2)+3));
a13=str2double(line3(c(2)+4:c(3)+3));
a14=str2double(line3(c(3)+4:c(4)+3));

%creating folder 
mkdir(['E:matlabFile Parsing',species_name]);

T=linspace( local_low_temperature,local_high_temperature,1000);
R=8.314;
[Cp,H,S]=thermo_data(T,R,local_mid_temperature,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14);

%Plotting 
figure(1);
plot(T,Cp,'linewidth',1,'color','b');
title(species_name);
xlabel('Temperature');
ylabel('Specific Heat');
grid on

figure(2);
plot(T,H,'linewidth',1,'color','g');
title(species_name);
xlabel('Temperature');
ylabel('Enthalpy');
grid on

figure(3);
plot(T,S,'linewidth',1,'color','r');
title(species_name);
xlabel('Temperature');
ylabel('Entropy');
grid on

%changing the directory and saving the files 
saving=(['E:matlabFile Parsing',species_name]);
cd(saving);

%saving the plots
saveas(1,'Specific Heat','jpg');
saveas(2,'Enthalpy','jpg');
saveas(3,'Entropy','jpg');
cd('E:matlab');

%Molecular mass calculations
molecular_mass=molecular_weight(species_name);

endfor

fclose(f1)

Explanation of the code

1.Start the program with clear all, close all and clc

2.NASA THERMO.dat file is opened using the 'fopen' command and ‘r’ is for read

3.‘fgetl’ command is used to read line by line

4.The ‘strsplit’ command is used to split a line on the basis of space in the line

5.The ‘str2num’, command is used to convert string to number

6.The global temperatures are extracted

7.The three comments line which are not necessary are skipped by using the ‘fgetl’ command inside the for loop

8.Since there are 53 species, a for loop for the 53 species is declared and the main program is written inside the for loop

9.In the species block, the first line contains the species name and the local temperatures and the next three lines contains the coefficients

10.The species name and all the local temperatures are obtained.

11.In order to get the coefficients, we make use of ‘strfind’ command to find the variable ‘E’ and using this as the reference the coefficients are obtained

12.’mkdir’ command is used to create folder and save all the plots for different species in it

13.A function is created containing the formulas to calculate Specific heat, enthalpy and entropy and is called in the main program

14.The Specific heat, enthalpy and entropy are calculated and graphs are plotted

15.The last step is to calculate the molecular weight of all the species.A function is created to calculate the molecular weight of the species and is called in the main program.

Outputs:

Screenshot of the window where all the folders are saved

Plots of O2:

Plots of N2:

Plots of CO2:

Molecular Weight of Species:

Errors Obtained:

In the following line A=strsplit(A,'') , we are spliting the line with respect to .The error was overcome by giving between the aphostrophes .

Some more simple errors were obtained and were rectified easily.

Conclusion:

The 14 co-efficients were extracted from the file and the enthalpy, entropy and specific heats for all the species in the data file were calculated.

The molecular weight of each species was calculated and displayed in the command window.

The graphs for Cp,H and S were plotted and the images with appropriate names were saved and dumped in seperate folders for each species with suitable name.

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.