Project

Parsing NASA thermodynamic data using MATLAB.

OBJECTIVE:

  1. 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. 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.

FILE PARSING:

Parsing a file means reading in a data stream of some sort and building in an memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data.[1]

Here, in this project , thermodynamic data of NASA is parsed as NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S.

in simple language, the data from the NASA thermodynamics file will be praised and transformed according to the requirement.

FORMULA USED:

where,

Cp =specific heat

H = enthalpy

S = entropy

R = universal gas constant

T = temprature

a1,a2.....a14 are coefficients

Function code to calculate molecoular mass for each species:

%creating function to calculate molecoular mass for each species
function [m] = molecoular_mass(species)      
species = upper(species);          % coverting whole string into uppercase

molecoules = ['H','C','N','O','A'];     

aw = [1 12 14 16 40];              % atomic weight for all above molecoules
m = 0;

for i = 1:length(species)
    for j = 1: length(molecoules)
        if strcmp(species(i),molecoules(j))
            m = m + aw(j);
            k = j;
        end
    end
    n = str2num(species(i));
    if n > 1
        m = m + (aw(k)*(n-1));
    end
end
end

MAIN CODE

%program to parse the nasa thermodynamic data

close all 
clear all 
clc

%universal gas constant 
R = 8.314;

%open the file for reading
nasa_data = fopen('THERMO.dat','r');

%start reading from first line
header = fgetl(nasa_data);

% the next line contains the three rempratures in a single line  
GT = fscanf(nasa_data,'%f \n',[1 3]);

global_temp_low_limit = GT(1);
global_temp_mid_limit = GT(2);
global_temp_high_limit = GT(3);

%skiping next 3 unimportant(comments) lines
for i = 1:3
    skip = fgetl(nasa_data);
end

% for loop for reading the remaning file
for i = 1:53

    species_line = fgetl(nasa_data);  % read line which contain species
 
    species = species_line(1:6)   %seprate species  name
    
 %reading local temprature for each species   
    t = strfind(species_line,'G');  
    local_low_temp = species_line(t+3:55);
     Lt = str2double(local_low_temp);
    local_high_temp = species_line(t+13:65);
     Ht = str2double(local_high_temp); 
    local_mid_temp = species_line(t+23:75); 
     Mt = str2double(local_mid_temp);
                
% reading and xtraction of each cofficient  
   % ectraction first line of cofficient
    c_line1 = fgetl(nasa_data);
      % five cofficients are in line 1 
    f1 = strfind(c_line1,'E');
    cf1 = c_line1(1:(f1(1)+3));
    cf1 =  str2num(cf1);
    
 
   cf2 = c_line1((f1(1)+4):(f1(2)+3));
   cf2 = str2num(cf2);
   cf3 = c_line1((f1(2)+4):(f1(3)+3));
   cf3 = str2num(cf3);
   cf4 = c_line1((f1(3)+4):(f1(4)+3));
   cf4 = str2num(cf4);
   cf5 = c_line1((f1(4)+4):(f1(5)+3));
   cf5 = str2num(cf5);
    
   %5 cofficients are in line 2 
   c_line2 = fgetl(nasa_data);
   f2 = strfind(c_line2,'E');
   cf6 = c_line2(1:f2(1)+3);
   cf6 = str2num(cf6); 
   cf7 = c_line2((f2(1)+4):f2(2)+3);
   cf7 = str2num(cf7); 
   cf8 = c_line2((f2(2)+4):f2(3)+3);
   cf8 = str2num(cf8); 
   cf9 = c_line2((f2(3)+4):f2(4)+3);
   cf9 = str2num(cf9); 
   cf10 = c_line2((f2(4)+4):f2(5)+3);
   cf10 = str2num(cf10); 
   
   % 4 cofficients from 3rd line
   c_line3 = fgetl(nasa_data);
   f3 = strfind(c_line3,'E');
   cf11 = c_line3((f3(1)+3));
   cf11 = str2num(cf11);
   cf12 = c_line3((f3(1)+4):f3(2)+3);
   cf12 = str2num(cf12);
   cf13 = c_line3((f3(2)+4):f3(3)+3);
   cf13 = str2num(cf13);
   cf14 = c_line3((f3(3)+4):f3(4)+3);
   cf14 = str2num(cf14);
   
   %creating folder for each species
   mkdir(['D:\nasa\',species])
   
   %dividing the temprature according to  lower and upper cofficient
   T = linspace(Lt,Ht,500); 
   for l = 1:length(T)
       
   if T(l) > Mt
       % for high temprature
        Cp=(cf1+cf2.*(T)+cf3.*(T.^2)+cf4.*(T.^3)+cf5.*(T.^4)).*R;
        S=(cf1.*log(T)+cf2.*T+cf3.*(T.^2)./2+cf4.*(T.^3)./3+cf5.*(T.^4)./4+cf7).*R;
        H=(cf1.*T+cf2.*(T.^2)./2+cf3.*(T.^3)./3+cf4.*(T.^4)./4+cf5.*(T.^5)./5+cf6).*R;

   else 
       % for low temprature 
       Cp=(cf8+cf9.*(T)+cf10.*(T.^2)+cf11.*(T.^3)+cf12.*(T.^4)).*R;
       S=(cf8.*log(T)+cf9.*T+cf10.*(T.^2)./2+cf11.*(T.^3)./3+cf12.*(T.^4)./4+cf14).*R;
       H=(cf8.*T+cf9.*(T.^2)./2+cf10.*(T.^3)./3+cf11.*(T.^4)./4+cf12.*(T.^5)./5+cf13).*R;
   end
   end
   
 %plots ist for specific heat(cp) vs tempfature (T)
   figure(1)
   plot(T,Cp,'linewidth',2,'color','r')
   title(species)
   xlabel('Temprature')
   ylabel('specific heat')
   
 % Plot 2nd for enthalpy H vs temperature T 
figure(2);
plot(T,H,'linewidth',2,'color','b');
xlabel('Temperature');
ylabel('Enthalpy');
title(species);

%Plot 3rd for Entropy S vs Temperature T
figure(3);
plot(T,S,'linewidth',2,'color','g');
xlabel('Temperature ');
ylabel('Entropy');
title(species);

% changing directiory into current directory
cd(['D:\nasa\',species])

% saving graphs in the folders
saveas(figure(1),'specific_heat.jpg')
saveas(figure(2),'Enthalpy.jpg')
saveas(figure(3),'Entropy.jpg')

cd('D:\nasa\')
%caliing molecoular mass function to calculate molecoular mass for each
%species
M = molecoular_mass(species) 

end

% closing the file
fclose(nasa_data);

EXPLANATION OF FUNCTION CODE:

  1. A function is created with the name of molecoular_mass with species as input.
  2. species is coverted into upper case into upper command.
  3. Assigned the atomic weight for each molecoule(H,C,N,O,A).
  4. Using for loop and if condition are used to calculate molecoular mass and compare the species with particular molecoule using strcmp command.
  5. Using star2num command to convert species from char into integer and using if condition so that any nolecoule is doubeld or trippled so that atomic wight can be adjusted accordingly.
  6. end of the function

EXPLANATION OF THE MAIN CODE:

  1. First of all , open the NASA file for reading using fopen command.
  2. Reading the first line using fgetl command.
  3. The next line contains global temprature of low, high and mid_range , so to read those temprature differently fscan command is used in which these 3 tempratures is picked differently.
  4. The next three lines are not useful, so they are skipped using fgetl command with the help of for loop contains 3 iterations.
  5. Now agian for loop is used in which the remaning work is done.this for loop iterates 53 times as there are total 53 species in file for which the calculation is required.
  6. Now the next line is read by fgetl command. this line contains the species name as well as local temprature range which is unique for each species.
  7. As any of the species does not contain more thn six characters , so using species_line(1:6) command the species are extracted from the from the species line.
  8. After that , as the local temprature range is in the same line so these tempratures are also extracted with the help of character 'G' which is three spaces before the temprature ranges start, so the character 'G' is found using strfind command.
  9. So these tempratures can be extracted by just putting the coloumn number from the laction of G to the end of one temprature limit, but these temrature are counted as string which then converted into double format by using str2double commands.
  10. Now the next 3 lines are of total 14 coefficients in which the first two lines contain 10 coefficients (5 each ) and third line contains 4 coefficients.
  11. As there is a character 'E' is used to show the power value, so in code 'E' is used as a character with the help of which cofficients are extracted.
  12. Reading the first line of cofficient using the fgetl command. the location of E can be found by strfind command and then the coefficients easily extracted from the line as shown in the code.Again the extracted coefficients are in strig format which is converted into integer by using str2num command.
  13. By following the same procedure the coefficients from the next two lines are extracted.
  14. Creating a folder using mkdir command to store the folder of each species
  15. Local temprature is divided between the range from low to high using linspace command.
  16. Now another for loop is created to calculate the value of specific heat, enthalpy, and entropy.the iteration of this for loop is equal to the values temprature which has been divided using the linspace command.
  17. As the first 7 coefficients are of high tmprature range and next 7 are of low temprature range , so if-else condition is used so that the tempratures which are highier than mid temprature can be used for calculation of Cp,H,S for first 7 coefficients and the other tempature range which is lower than mid temprature are used for the next seven coefficient.
  18. Now, after calculating the cp,H and S, using the plot command plot of cp(specific heat) vs temprature, H(enthalpy) vs temprature and S(entropy) vs temprature is genrated.
  19. These plots are saved as saveas command, before this directory is changed into current one so that plot should be stored into species folder.
  20. Now calling the function of molecoular mass to calculate the molecoular mass for each species.
  21. End of the for loop and close the file using fclose command.

ERROR FACED:

1.First error was related to not converting the string char into integer and direct use it in the calculation.

to rectify that error char values were converted into scalar using str3double command.

2.Next error was related to not using for loop while calculating Cp,H and S.whic results in same nature of plot for each species.After doing careful observation this error was tackled.

3. 3rd error was occured while using the for loop in function code. it was related to not defining the iterations carefully as shown below

so while executing the program the molecoular mass values were coming zero for each species as shown below:

this error were tackled with the help of provided support session.

OUTPUT:

species folder:

molecoular mass in command window:

Plots for O2

Enthaly vs temprature:

Entropy vs Temprature:

Specific heat vs Temprature:

Plots for N2:

Enthaly vs temprature:

Entropy vs Temprature:

specific heat vs Temprature:

PLOT FOR CO2:

Enthaly vs temprature:

Entropy vs Temprature:

specific heat vs Temprature:

REFRENCES:

1.

https://www.quora.com/What-does-it-means-to-parse-a-file#:~:text=Parsing%20in%20computer%20languages%20refers,writing%20of%20compilers%20and%20interpreters.&text=Parsing%20a%20file%20means%20reading,semantic%20content%20of%20that%20data.

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.