NASA thermodynamic file parsing using MATLAB
AIM:
To perform file parsing on a NASA thermodynamic data file containing coefficients to find out Specific heat, enthalpy and entropy of specimens.The program should also find molecular weight of the compound ,plot the parameters calculated and save the plots in seperate folders partaining to the specimen
Objectives:
- the program first should load the THERMO.dat file containing required data.
- Should prompt user to enter a specimen name and check if specimen data is present in file.
- If present, extract the 14 coefficients of the specimen.
- along with coefficients their local temperature range should also be collected.
- Using extracted data the specific heat, enthalpy and entropy is computed.
- Calculate the molecular weight of the specimen.
- Plot the graph for the required parameters and save them in a file for the specimen.
File Parasing:
Parsing a file means reading in a data stream of some sort and building an in memory model of the semantic content of that data. This aims at facilitating perfoming some kind of transformation on the data.
Code:
Main program
%% NASA File Parasing - Main program
% Program to calculate specific heats ,entropy, enthalpy and molecular
% weight of a specimen
%%
clear all
close all
clc
% Universal gas constant
R = 8.3145;
% opening the file THERMO.dat and storing it in 'source' variable
source = fopen('THERMO.dat','r');
% Asking for the required species name and storing the string
question = "Enter name of the species to find your results"
x = input('Species Name:\n','s');
% function to extract the coefficients of the species entered
[c,lt,ht,mt] = nasacoefficients(source,x);
% function to compute the specific heats ,entropy and enthalpy of species
[cpl,hl,sl,t] = nasacomputation(c,lt,mt,ht,R);
% function to calculate the molecular weight of the specimen
MW = nasamol_wt(source,x);
% condition statment to display the molecular weight and generation of
% plots
if(MW ~= 0)
fprintf('The molecular weight of %s is = %fu',x,MW)
% function to generate and store plots
nasaplots(x,t,cpl,hl,sl)
else
fprintf("\nplease enter a valid species name")
end
The code does the following:
- Opens the THERMO.dat file.
- Prompts user to enter species name.
- If the specimen is present in data file then,calls function to calculate 14 coefficients and local temperature range.
- then calls for function to compute the Cp, enthalpy and entropy for specimen.
- After which a function for calculating the molecular weight is called.
- The last function called generates and saves plots for parameters above in file created for specimen.
- If the specimen entered is not present in the data file the user is prompted that species name is invalid.
Function 1 - Extraction of local temperatures and coefficients
%% NASA File Parsing -temperature and coefficient calculation function
% This function finds the coefficients and local temperatures of the particular specimen entered in main function
%%
function [coef,low_temp,high_temp,mid_temp] = nasacoefficients(source,x)
% loop to read till the end of the file i.e expression of loop tels it to
% read till end of file
while ~feof(source)
% indexing to next line in order to split the string(i.e line) and find if a name of species is present in the line
line = fgetl(source);
s = strsplit(line,' ');% splitting the string using a delimiter
con = strcmpi(s(1),x);%performing the comparison of species name entered and that found in the line
% condition statment to check if entered specimen is found in the line
if con == 1
ct = 1;
% to find the local temperature for the corresponding species
ta = strfind(line,'G'); % finding position of 'G'
tb = line(ta:end); % generating string from the charecters follwing 'G' in the line
g = strsplit(tb,' '); % splitting the string using delimiter
low_temp = str2num(g{2}); % assigning the value of temperatures into variables
high_temp = str2num(g{3});
mid_temp = str2num(g{4});
% loop used to index for the next three lines that contains the values of
% coefficients
for i = 1:3
d = fgetl(source); % reading the line
temp = strfind(d,'E'); % finding positions of 'E'
k = 1; %variable for indexing
% storing the values of coefficients into an array
for j = 1:length(temp)
coef(ct) = str2num(d(k:temp(j)+3)); % the string in converted to integer befor storing
ct = ct+1;
k = temp(j) + 4;
end
end
break;
else
% returning a value if the specimen name is not found in the file
coef = 0;
low_temp = 0;
high_temp = 0;
mid_temp = 0;
end
end
% condition statment - if specimen name entered is not found in file then
% the user is informed with a message. If present after extracting required
% data the parsing is resetted to first line to be used for next function
if (coef == 0)
fprintf('\nthe entered species specification is not available in the datafile')
end
frewind(source); % resetting parsing to first line
end
The above code does the following:
- the entered specimen name is checked if present in datafile by comparing line by line using a while loop.
- If present the local temperature ranges are collected and stored.
- then, the coefficients present in the next 3 lines are extracted using a simple logic consisting of for loops.
- if specimen not present in the entire file then 0 is returned, and user is prompted.
Function 2 - Computation of Cp, enthalpy and entropy
%% NASA File Parsing - Computation of Specific heats, entropy and enthalpy
% The following function computes the requered parameters from the data
% extracted from the previous function "nasacoefficients"
%%
function [cp1,h1,s1,t] = nasacomputation(coef,lt,mt,ht,R)
% local temperature range of specimen
T = lt:ht;
if (T ~= 0)
% loop to calculate parameters for each temperature in the range
for i = 1:length(T)
if (T(i) > mt)
cp1(i) = (coef(1) + coef(2)*T(i) + coef(3)*T(i)^2 + coef(4)*T(i)^3 + coef(5)*T(i)^4)*R;
h1(i) = (coef(1) + (coef(2)*T(i))/2 + (coef(3)*T(i)^2)/3 + (coef(4)*T(i)^3)/4 + (coef(5)*T(i)^4)/5 + coef(6)/T(i))*R*T(i);
s1(i) = (coef(1)*log(T(i)) + coef(2)*T(i) + (coef(3)*T(i)^2)/2 + (coef(4)*T(i)^3)/3 + (coef(5)*(T(i)^4))/4 + coef(7))*R;
t(i) = T(i);
elseif (T(i) <= mt)
cp1(i) = (coef(8) + coef(9)*T(i) + coef(10)*T(i)^2 + coef(11)*T(i)^3 + coef(12)*T(i)^4)*R;
h1(i) = (coef(8) + (coef(9)*T(i))/2 + (coef(10)*T(i)^2)/3 + (coef(11)*T(i)^3)/4 + (coef(12)*T(i)^4)/5 + coef(13)/T(i))*R*T(i);
s1(i) = (coef(8)*log(T(i)) + coef(9)*T(i) + (coef(10)*T(i)^2)/2 + (coef(11)*T(i)^3)/3 + (coef(12)*T(i)^4)/4 + coef(14))*R;
t(i) = T(i);
end
end
else
% if specimen name entered is not found in file then the following values are returned
cp1 = 0;
h1 = 0;
s1 = 0;
t = 0;
end
end
the code does the following:
- from the values as input to function, range of the local temperature is assigned to an array.
- Then for each temperature value the parameters are calculated.
- during computation of parameters the coefficients to the formula are assigned depending on wether temperature lies in lower or higher temperature range.
- If, specimen not present 0 is returned for each output.
Function 3 - Molecular weight calculation.
%% NASA File Parsing - molecular weight computation
% Function to calculate the Molecular Mass of the entered specimen
%%
function mw = nasamol_wt(source,x)
sum = 0;
% loop to read till the end of the file i.e expression of loop tels it to
% read till end of file
while ~feof(source)
% indexing to next line in order to split the string(i.e line) and find if a name of species is present in the line
line = fgetl(source);
s = strsplit(line,' ');% splitting the string using a delimiter
con = strcmpi(s(1),x);%performing the comparison of species name with that entered
% condition statment to check if entered specimen is found in the line
if con == 1
% loop to store the element name in array "m" and its corresponding
% number of atoms in array "nm"
for i = 1:4
% condition statment checks if an element symbol is present
if (isspace(char(line(20 + i*5))) ~= 1)
m(i) = char(line(20 + i*5));
nm(i) = str2num(line(24 + i*5));
else
% if there is no element symbol then the conditional statment
% is terminated
break;
end
end
% loop to calculate the molecular mass with the data generated above
for j = 1:length(m)
if (strcmp("O",m(j)) == 1)% expression checks for the element symbol
sum = sum + (nm(j)*15.9994); % if one or more atoms of same element is present then it is multiplied with corresponding number
elseif (strcmp("H",m(j)) == 1)
sum = sum + (nm(j)*1.00794);
elseif (strcmp("C",m(j)) == 1)
sum = sum + (nm(j)*12.0107);
elseif (strcmp("N",m(j)) == 1)
sum = sum + (nm(j)*14.0067);
elseif (strcmp("A",m(jsum)) == 1)
sum = sum + (nm(j)*39.948);
end
end
mw = sum;
break;
else
% if specimen entered is not present then following value is returned
mw = 0;
end
end
end
the code does the following:
- The elements present is identified and stored in an array using array indexing over the line.
- The number of atoms of the element is also stored in another array.
- Using an conditional statment the element is compared and its weight is calculated.
- The value is returned to the main function.
Function 4 - Plot generation
%% NASA File Parsing - Generating plots
% plots for required parameters are generated and saved as jpeg in seperate
% folder for each specimen
%%
function nasaplots(x,t,cpl,hl,sl)
name = sprintf('%s',x);% folder name
mkdir(name); % creating folder
fullfilename = fullfile('D:\','matlab progs','test programs','Nasa file parsing',name);% saving path of new folder
currentd = pwd;% saving current folder path
% plot for specific heat
figure(1);
plot(t,cpl,'lineWidth',2,'color','b')
tl1 = sprintf('Specific heat of %s',x);
title(tl1);
xlabel('Temperature range (K)');
ylabel('Cp (J/kg K)');
cd(fullfilename);
saveas(figure(1),'Specific heat plot.jpg');% saving the plot in folder
cd(currentd);
% plot for enthalpy
figure(2)
plot(t,hl,'lineWidth',2,'color','g')
tl2 = sprintf('Enthalpy of %s',x);
title(tl2)
xlabel('Temperature range (K)')
ylabel('Enthalpy (J)')
cd(fullfilename);
saveas(figure(2),'Enthalpy plot.jpg');% saving the plot in folder
cd(currentd);
% plot for entropy
figure(3)
plot(t,sl,'lineWidth',2,'color','r')
tl3 = sprintf('Entropy of %s',x);
title(tl3)
xlabel('Temperature range (K)')
ylabel('Entropy (J/K)')
cd(fullfilename);
saveas(figure(3),'Entropy plot.jpg');% saving the plot in folder
cd(currentd);
end
The code does the following:
- the required data is taken as input from main program.
- Plots for specific heat,enthalpy and entropy are generated and saved in a file created with specimen name.
Result:
O2
N2
CO2
Plots
Conclusion:
file parsing is done for the give NASA thermodynamic file and the required data for specimens are extracted.From the data extracted the specific heat, enthalpy and entropy of the specimen is calculated. The molecular weight of the specimen is also found. From the results obtained required plots are generated and stored in seperate folders.
References: