Project

NASA Data Parsing using MATLAB

Objective:

To write MATLAB Code to parse the NASA thermodynamic data file and then calculate thermodynamic properties of various gas species

Introduction:

Data parsing is the act of taking the input data (frequently text) and building a data structure giving a structural representation of the input.

About the project:

NASA came up with polynomials that can be used to evaluate thermodynamic properties such as Cp, H and S using polynomials.

In this project, I am going to read the following file:

https://drive.google.com/file/d/1AikCHDlz9s_fu8whE81qee_GD4AyYpCP/view?usp=sharing

and then I will calculate the enthalpy, entropy and specific heats using the NASA polynomials using formulas as given below:

After that molecular weight of the respective species is calculated and plot for enthalpy, entropy and specific heats is generated.

Description:

First of all, I need to extract the 14 coefficients for a given species along with the name of Species using programme which is a funtion for the Main programme. The code is shown as below:

function [species_name,coeff_1,coeff_2,coeff_3] = species_coefficient(f1)
% Extraction of species name
 line  = fgetl(f1)
    species_name = line(1:7)
% COefficeints from line 1
line1 = fgetl(f1);
coeff_1 = sscanf(line1,\'%f\');
coeff_1=coeff_1(1:end-1)

%Coeffients from line 2
line2=fgetl(f1);
coeff_2=sscanf(line2,\'%f\');
coeff_2=coeff_2(1:end-1)
%Coeffients from line 3
line3=fgetl(f1);
coeff_3=sscanf(line3,\'%f\');
coeff_3=coeff_3(1:end-1)
end

this code generates the species name from the file and coefficients extacted for the respective species are stored in the form of cells namely coeff_1,coeff_2 and coeff_3.

After this, funtion files for calculating specific heats, enthalpy and entropy are created using following codes in MATLAB:

Specific Heat:

function [Cp] = specific_heat(HT,LT,temp,global_mid_temp)
R = 8.314
for i = 1:length(temp)
    term1 = HT(1);
    term2 = HT(2).*temp(i);
    term3 = HT(3).*temp(i)^2;
    term4 = HT(4).*temp(i)^3;
    term5 = HT(5).*temp(i)^4;
    term6 = LT(1);
    term7 = LT(2).*temp(i);
    term8 = LT(3).*temp(i)^2;
    term9 = LT(4).*temp(i)^3;
    term10 =LT(5).*temp(i)^4;
  % Specfic Heat
  if temp(i)>=global_mid_temp
      Cp(i) = R*(term1+term2+term3+term4+term5)
  else
      Cp(i) = R*(term6+term7+term8+term9+term10)
  end
end
end

Enthalpy:

function [H] = enthalpy(HT,LT,temp,global_mid_temp)
R = 8.314
for i = 1:length(temp)
    term1 = HT(1);
    term2 = HT(2)*(temp(i)/2);
    term3 = HT(3)*(temp(i)^2/3);
    term4 = HT(4)*(temp(i)^3/4);
    term5 = HT(5)*(temp(i)^4/5);
    term6 = HT(6)/temp(i);
    term7 = LT(1);
    term8 = LT(2)*(temp(i)/2);
    term9 = LT(3)*(temp(i)^2/3);
    term10 = LT(4)*(temp(i)^3/4);
    term11 = LT(5)*(temp(i)^4/5);
    term12 = LT(6)/temp(i);
  % Specfic Heat
  if temp(i)>=global_mid_temp
      H(i) = R*temp(i)*(term1+term2+term3+term4+term5+term6)
  else
      H(i) = R*temp(i)*(term7+term8+term9+term10+term11+term12)
  end
end
end

Entropy:

function[S] = entropy(HT,LT,temp,global_mid_temp)
R = 8.314
for i = 1:length(temp)
    term1 = HT(1)*log(temp(i));
    term2 = HT(2)*temp(i);
    term3 = HT(3)*((temp(i)^2)/2);
    term4 = HT(4)*((temp(i)^3)/3);
    term5 = HT(5)*((temp(i)^4)/4);
    term6 = HT(7);
    term7 = LT(1)*log(temp(i));
    term8 = LT(2)*temp(i);
    term9 = LT(3)*((temp(i)^2)/2);
    term10 = LT(4)*((temp(i)^3)/3);
    term11 = LT(5)*((temp(i)^4)/4);
    term12 = LT(7);
    if temp(i)>= global_mid_temp
        S(i) = R*(term1+term2+term3+term4+term5+term6)
    else
        S(i) = R*(term7+term8+term9+term10+term11+term12)
    end
end
end

        
        
    

also the Molecular weight is calculated using following Code:

function [W] = molecular_weight(species_name)
% To calculate molecular wieght of Gases
 constituents_main = {\'H\' \'C\' \'N\' \'O\' \'AR\'};
 weight_atomic = [1 12 14 16 40];
W = 0;
 for k = 1:length(species_name)
     for j = 1:length(constituents_main)
         if strcmp(species_name(k),constituents_main(j))
             W = W+weight_atomic(j)
             Y = j;
         end 
     end
     n = str2num(species_name(k));
     if n>1
         W = W+weight_atomic(Y)*(n-1);
     end
 end
end
         

Now I need to Plot the graphs using the extracted and calculated information about Coefficients, Specific Heat, Enthalpy and Entropy. For this purpose, the code used for funtion file is shown as below:

function plot_all(f,species_name,temp,Cp,H,S)
 % making a directory
  p = pwd;
    cd(f);
     folder_for_NASA_Data = sprintf(\'%s\',species_name)
     mkdir(folder_for_NASA_Data);
a = figure(1)
set(a,\'visible\',\'off\')
plot(temp,Cp,\'color\',\'r\')
title(sprintf(\'Temperature v/s Specific Heat\',\'fontsize\',12))
xlabel(\'Temperature\',\'fontsize\',10)
ylabel(\'Specific Heat(Cp)\',\'fontsize\',10)
a = figure(2)
set(a,\'visible\',\'off\')
plot(temp,H,\'color\',\'g\')
title(sprintf(\'Temperature v/s Enthalpy\',\'fontsize\',12))
xlabel(\'Temperature\',\'fontsize\',10)
ylabel(\'Enthalpy(H)\',\'fontsize\',10)
a = figure(3)
set(a,\'visible\',\'off\')
plot(temp,S,\'color\',\'b\')
title(sprintf(\'Temperature v/s Entropy\',\'fontsize\',12))
xlabel(\'Temperature\',\'fontsize\',10)
ylabel(\'Entropy(S)\',\'fontsize\',10)
 cd(folder_for_NASA_Data)
saveas(1,\'Cp.png\')
saveas(2,\'H.png\')
saveas(3,\'S.png\')
cd(p)
end


this funtion not only generates the plots for the given data but also create folders in the directory with the name of the folders same as the species for which plots are generated.

Now i need to write THE MAIN PROGRAMME which takes all the above funtion files and give us required results.

This main programme is shown as :

clear all
close all
clc
% Gas Constant
R = 8.314
f1 = fopen(\'thermo30.txt\',\'r\');
% No. of Lines 
res = {};
while ~feof(f1)
    line = fgetl(f1)
    if ~ischar(line); break; end
    res{end+1,1} = line;
end
fclose(f1)
number_of_lines = numel(res);
number_end_lines = 1;
number_header_lines = 5;
f1 = fopen(\'thermo30.txt\',\'r\');
%TITLE
Title = fgetl(f1);
% Getting Temperature ranges
temp_info = fgetl(f1)
A = strsplit(temp_info,\' \');
global_low_temp = str2num(A{2});
global_mid_temp = str2num(A{3});
global_high_temp = str2num(A{4});
temp = linspace(global_low_temp,global_high_temp,100);
% For Header Comments
for i=1:3
B = fgetl(f1)
end
f = \'Plots of all Elements\';
mkdir(f)
% Extracting data of species
number_species = (number_of_lines-number_end_lines-number_header_lines)/4;
for i = 1:number_species
   
[species_name,coeff_1,coeff_2,coeff_3] = species_coefficient(f1)
    % High temperature and low temperature Coefficients
    High_T = {coeff_1 coeff_2(1:2)}
    Low_T = {coeff_2(3:end) coeff_3}
    HT = char(High_T{1,1},High_T{1,2})
    LT = char(Low_T{1,1},Low_T{1,2})
    % Calculation of Specific Heat, Enthalpy, Entropy and Molecular weight
    Cp = specific_heat(HT,LT,temp,global_mid_temp);
    H = enthalpy(HT,LT,temp,global_mid_temp);
    S = entropy(HT,LT,temp,global_mid_temp);
    W = molecular_weight(species_name);
 
      % Plotting Temperature v/s Cp/H/S 
       
    plot_all(f,species_name,temp,Cp,H,S)
    
end
    
  

this programme opens the file using fopen command. Then the no. of lines in the file are calculated. Afterwards, it obtains the temperature range for the species and also the number of species in the given file.

Then the functions files are used for their respective purposes. This code also generates rhe directory folder \'Plots off all Elements\' inside which sub-folders of species names are created.

Results:

The Graphs generated for O2, N2 and CO2 are given below:

O2:

N2:

CO2:

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.