Project

File Parsing of NASAs Thermodynamic Data Using Matlab

File Parsing of NASAs Thermodynamic Data Using Matlab

AIM:- To parse the NASA thermodynamic properties data file and introduce the required properties.

OBJECTIVES:-

  • To extract the 14 coefficients and to calculate the enthalpy, entropy, and specific heats for all the species in the data file.
  • Calculate the molecular weight of each species.
  • Plot the CP, Enthalpy and Entropy for the local temperature range specific for each species.

INTRODUCTION:-

The file parsing means how information can be read from a particular text file or any type of file and how that information can be used in a computer program.

NASA\'s Thermodynamic properties file consists of multiple species of gases, their respective operating temperature ranges, and coefficients to calculate thermodynamic properties.

There are 14 coefficients for each species of which the first 7 are \'High Temperature Coefficients\' and the next 7 are \'Low Temperature Coefficients\'. The 14 coefficients are split into 3 lines, 5 each in the first 2 and 4 in the 3rd line. This makes reading the line and isolating the coefficients a challenge, So understanding the positions of coefficients and checking for repetition across both lines is the key here.

Once extracted, the below equations are used to calculate specific heat[CP], Enthalpy[H], Entropy[S] for each of the 53 species in the file.

EXPLANATION:-

  1. The code starts with the clear all, close all, and clc.
  2. Then the value of Universal gas constant \'R\' is provided.
  3. In order to open the file \'fopen\' command is used, \'THERMO.dat\' is the file name and \'r\' is related to reading a file.
  4. The \'fgetl\' command is used to get the first line,
  5. The \'fscanf\' command is used to read the second line, which represents the global temperature range.
  6. Then using the command \'fgetl\', skip next 3 lines.
  7. For loop is used for the 53 constituents mentioned in the data file to gather the name of the compound and is given to the species.
  8. The species line is read and the name of species is extracted. Then reading the coefficients as \'tline1\' using \'fgetl\' command, also extracting the coefficients for seven higher and seven lower variables. In which two commands used first \'strfind\' and second \'str2num\'. Where the \'strfind\' is used to find/check the position of \'E\', as each coefficient contains E as the exponent indicator. And \'str2num\' used to convert strings into a number.
  9. Then, a new directory is made using \'mkdir\' command to create a folder with the species name.
  10. Temperature extraction is taken place as \'T\', and linspace creates 50 values from low to high for plotting and calculation purposes.
  11. To calculate specific heat, enthalpy and entropy respective functions are defined.
  12. Later, three graphs are plot, [\'T\' Vs \'CP\'], [\'T Vs \'S\'], and [\'T\' Vs \'H\'].
  13. At last using change in directory [Cd] and the molecular mass is calculated using the function which is user define.
  14. Molecular weight is calculated in a separated function that takes the species name as input and returns the calculated molecular mass/weight.
  • The function first splits the input a cell array and compares each element of the array to C, H, Ar, O, N, which are the only elements which the compounds in the file are composed of.
  • Meanwhile, at every \'if\' condition, the next array element is converted to a number.
  • If the number conversion returns true [>=1], then the current elements\' atomic weight is multiplied by that number. If not, then the weight is printed as it is. Condition is left as [>=1] and not [>=2] for fool proofing purposes.
% Write a Code in Matlab to parse the NASA thermodynamic data file
clear all
close all
clc

% Universal Gas constant(J/mol.k)
R = 8.3145

% Opening the data file in matlab in the file pointer named f1
f1 = fopen(\'THERMO.dat\',\'r\')
Header = fgetl(f1);

% global temperatures

A = fscanf(f1,\'%f \\n\',[1 3]);

global_low_temp = A(1);
global_mid_temp = A(2);
global_high_temp = A(3);

% skipping 3 lines
line1 = fgetl(f1);
line2 = fgetl(f1);
line3 = fgetl(f1);

for i = 1:53
    name_line = fgetl(f1);


% Extracting the number of species
species = name_line(1:6);

% Reading the coefficients
tline1 = fgetl(f1);

% Extracting the coefficients
y1 = strfind(tline1,\'E\');
a1 = tline1(1:y1(1)+3);
a1 = str2double(a1);
a2 = tline1((y1(1)+4):(y1(2)+3));
a2 = str2double(a2);
a3 = tline1((y1(2)+4):(y1(3)+3));
a3 = str2double(a3);
a4 = tline1((y1(3)+4):(y1(4)+3));
a4 = str2double(a4);
a5 = tline1((y1(4)+4):(y1(5)+3));
a5 = str2double(a5);

tline2 = fgetl(f1);
y2 = strfind(tline2,\'E\')
a6 = tline2(1:y2(1)+3);
a6 = str2double(a6);
a7 = tline2((y2(1)+4):(y2(2)+3));
a7 = str2double(a7);
a8 = tline2((y2(2)+4):(y2(3)+3));
a8 = str2double(a8);
a9 = tline2((y2(3)+4):(y2(4)+3));
a9 = str2double(a9);
a10 = tline2((y2(4)+4):(y2(5)+3));
a10 = str2double(a10);

tline3 = fgetl(f1);
y3 = strfind(tline3,\'E\')
a11 = tline3(1:y3(1)+3);
a11 = str2double(a11);
a12 = tline3((y3(1)+4):(y3(2)+3));
a12 = str2double(a12);
a13 = tline3((y3(2)+4):(y3(3)+3));
a13 = str2double(a13);
a14 = tline3((y3(3)+4):(y3(4)+3));
a14 = str2double(a14);

% Creating folders with species name
subfolder_name = species;
mkdir([\'F:/matlaab/Basic/Tutorials\',subfolder_name]);

T = linspace(1,5000,50);

% Function for enthalpy
[H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R);

% Function For cp
[c_p] = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R);

% Function For entropy
[s] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R);


% Graphs
% Temperature Vs specific heat
figure(1)
plot(T,c_p,\'LineWidth\',3,\'color\',\'g\')
title(\'species\')
xlabel(\'Temperature [k]\')
ylabel(\'Specific Heat [J/mol-k]\')

% Temperature Vs Enthalpy
figure(2)
plot(T,H,\'LineWidth\',3,\'color\',\'b\')
title(\'species\')
xlabel(\'Temperature [k]\')
ylabel(\'Enthalpy [J]\')

% Temperature Vs Entropy
figure(3)
plot(T,s,\'LineWidth\',3,\'color\',\'r\')
title(\'species\')
xlabel(\'Temperature [k]\')
ylabel(\'Entropy [J/K]\')

% Changing the directory
cd(\'F:/matlaab/Basic/Tutorials\');

%Function for molecular mass
[mm] = molecular_mass(species)

% Changing the directory to save the graphs in respective species folder
filepath = ([\'F:/matlaab/Basic/Tutorials\',subfolder_name]);
cd (filepath)

saveas(1,\'Specific Heat.jpg\')
saveas(2,\'Enthalpy.jpg\')
saveas(3,\'Entroy.jpg\')

% saving the molecular mass in .txt file
f2 = fopen(\'Molecular mass.txt\',\'w\');
fprintf(f2,\'%s \\n %s %f \\n\',species,\'Molecular mass\',mm);
fclose(f2);

end


fclose(f1);

function H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,R)

T = linspace(1,5000,50);
if T <= 1000
    
    H = (a1.*T+a2.*(T.^2)./2+a3.*(T.^3)./3+a4.*(T.^4)./4+a5.*(T.^5)./5+a6)*R;
else
    H = (a8.*T+a9.*(T.^2)./2+a10.*(T.^3)./3+a11.*(T.^4)./4+a12.*(T.^5)./5+a13)*R;
end
function cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,R)
T = linspace(1,5000,50);
if T<=1000
    CP = (a1+a2.*(T)+a3.*(T.^2)+a4.*(T.^3)+a5.*(T.^4))*R;
else
    cp = (a8+a9.*(T)+a10.*(T.^2)+a11.*(T.^3)+a12.*(T.^4))*R;
end
function s = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R)

T = linspace(1,5000,50);
if T<=1000
    s =(a1.*log(T)+a2.*T+a3.*(T.^2)./2+a4.*(T.^3)./3+a5.*(T.^4)./4+a7)*R;
else
    s =(a8.*log(T)+a9.*T+a10.*(T.^2)./2+a11.*(T.^3)./3+a12.*(T.^4)./4+a14)*R;
end
    

To Calculate Molecular mass by a Function Program:-

function [mm] = molecular_mass(species)
% A program for calculating molecular mass of each species
atom = {\'H\',\'C\',\'N\',\'O\',\'AR\'};

% Atomic weights
aw = [1,12,14,16,20];

% initial values for molecular mass
mm = 0;
for i = 1:length(species);
   for j = 1:length(atom);
    if strcmp(species(i),atom(j))
        mm = mm + aw(j)
        a = j;
   end
   end
end
n = str2double(species(i));
if n>1
    mm = mm+atom(a)*(n-1)
 
end
end

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.