Challenge

Project 1 - Parsing NASA thermodynamic data

Objective: Write a code in MATLAB to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species.

To find out the molecular mass of each species and finally to plot a graph of enthalpy,entropy and Cp with respect to temperature.

Theory: File parsing is done to extract data from the file,it should be as generic as possible to fetch the data from different files.In this program we are getting the 14 coefficients from the data and by using this we calculate enthalpy,entropy and specific heat for different species and the condition is that the 1st 7-coefficients are high temperature coefficient and the 2nd 7-coefficients are low temperature coefficient.

The formulae to calculate specific heat,enthalpy and entropy are following-

Program algorithm:

1) THERMO.dat file is imported to the program by using 'fopen' command and each and every line is read by using'fgetl' command.

2) In order to get the header information 'strsplit' command is used to split the string and the global temperatures are fetched.

3) Now the specific data is read , since there are 53 species and the logic remains the same hence 'for loop' for the 53 species are declared and the main program is given inside the loop.

4) The species 1st line contains the species name, local temperatures and next 3 lines contains the coefficients.

5) Hence by using the 'strsplit' command the sp_name and the local temperatures are obtained.

6) In order to get the coefficients 'strfind' command is used find the 'E' varaible by using this as reference coefficients are assigned.

7) To create a separate folder to save the plot for every species 'mkdir' command is used.

8) For plotting we need a set of temperatures hence the local species temperature are splitted into 100 points by using 'linspace' command.

9) By using the formulae specific heat, enthalpy and entropy are calculated and plotting is carried out.

10) For saving the plots 'cd' command is used.

11) To find the molecular mass for each and every element atomic weight is assigned now by using the 'strcmp' command each species elements are compared and stored using two 'for loops', one to iterate the species element and one to compare the elements.

12) In some species like HNO3 the elament's atomic weight to be added twice or thrice, hence flag operator is used to get the position and that element atomic weight is added to the molecular weight by using the 'if' condition.

Program:

% Parsing NASA Thermodynamic Data
clear all
close all
clc

% Getting the Data from the Provided File

f1 = fopen('THERMO.dat','r');
fgetl(f1);
tline = fgetl(f1);
A = strsplit(tline,' ');
Global_low_temp = str2num(A{2});
Global_mid_temp = str2num(A{3});
Global_high_temp = str2num(A{4});

% Eliminating the lines

for i= 3:5
    b = fgets(f1);
end

% Getting the Temperature & Coefficients for each species

for j=1:53
    species = fgetl(f1);
    t = findstr(species,'.');

    a=strsplit(species,' ')

% Local Temperature for each species

species(52-3:52+3);
Tl = species(52-3:t(1)+3);
Tl1 = str2num(Tl);

species(t(1)+6:62+3);
Th = species(t(1)+6:t(2)+3);
Th1 = str2num(Th);

species(t(2)+6:72+3);
Tm = species(t(2)+6:t(2)+3);
Tm1 = str2num(Tm);

sp_name = a{1};

% Coefficients of each species

coeff_line1 = fgetl(f1);
A11 = strfind(coeff_line1,'E');
coeff_line1(1:12+3);

a1=coeff_line1(1:A11(1)+3);
c1=str2num(a1);
a2=coeff_line1(A11(1)+4:A11(2)+3);
c2=str2num(a2);
a3=coeff_line1(A11(2)+4:A11(3)+3);
c3=str2num(a3);
a4=coeff_line1(A11(3)+4:A11(4)+3);
c4=str2num(a4);
a5=coeff_line1(A11(4)+4:A11(5)+3);
c5=str2num(a5);

coeff_line2 = fgetl(f1);
A12 = strfind(coeff_line2,'E');
coeff_line2(1:12+3);

a6=coeff_line2(1:A12(1)+3);
c6=str2num(a6);
a7=coeff_line2(A12(1)+4:A12(2)+3);
c7=str2num(a7);
a8=coeff_line2(A12(2)+4:A12(3)+3);
c8=str2num(a8);
a9=coeff_line2(A12(3)+4:A12(4)+3);
c9=str2num(a9);
a10=coeff_line2(A12(4)+4:A12(5)+3);
c10=str2num(a10);

coeff_line3 = fgetl(f1);
A13 = strfind(coeff_line3,'E');
coeff_line3(1:12+3);

a11=coeff_line3(1:A13(1)+3);
c11=str2num(a11);
a12=coeff_line3(A13(1)+4:A13(2)+3);
c12=str2num(a12);
a13=coeff_line3(A13(2)+4:A13(3)+3);
c13=str2num(a13);
a14=coeff_line3(A13(3)+4:A13(4)+3);
c14=str2num(a14);

%Creating the folder to save the plots
 if exist('E:NASA file parsing\Species','dir')

  % Folder does not exist so create it.

 mkdir('E:NASA file parsing\Species',CH4)
 end

 %Calculating the specific heat, Enthalpy, Entropy

 T = linspace(Tl1,Th1,100);
 R = 8.314;

 [Cp,H,S] = inputs(T,R,Tm1,c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14);

 %Plotting
 figure(1)
 plot(T,Cp,'LineWidth',2,'Color','r')
 grid on 
 title(sp_name)
 xlabel('Temperature')
 ylabel('Specific Heat')

 figure(2)
 plot(T,H,'LineWidth',2,'Color','b')
 grid on 
 title(sp_name)
 xlabel('Temperature')
 ylabel('Enthalpy')

 figure(3)
 plot(T,S,'LineWidth',2,'Color','y')
 grid on 
 title(sp_name)
 xlabel('Temperature')
 ylabel('Entropy')

 %Changing the D'spirectory
mkdir(['E:\Octave\plots\',sp_name]);
cd(['E:\Octave\plots\',sp_name]);
 

 %Saving the plots

 saveas(1,'Specific Heat.png')
 saveas(2,'Enthalpy.png')
 saveas(3,'Entropy.png')


 %Calculating the molecular weight

 elements = ['H','C','O','N','A','S'];
 atomic_weight= [1.00794 12.0107 15.999 14.0067 39.948 32.065];

 molecular_weight_calculation = 0;

 for i=1:length(sp_name)
     for j=1:length(elements)
         if strcmp(species(i),elements(j))
             molecular_weight_calculation = molecular_weight_calculation+atomic_weight(j);
             a=j;
         end
     end

     n = str2num(sp_name(i));
     if n>1;
         molecular_weight_calculation = molecular_weight_calculation + atomic_weight(a)*(n-1);
     end
 end

end
 

 fclose(f1);



Folders with plots:

O2 plots:

N2 plots:

CO2 plots:

Results: The code to prase the NASA thermodynamic data file has been programmed and the thermodynamic properties and molecular weight of species has been calculated and 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.