Challenge

Project 1 - Parsing NASA thermodynamic data

Parsing NASA's Thermodynamic data file

AIM : To parse NASA's thermodynamic data file to extract the coefficient and use it to calculate thermodynamic properties using MATLAB program.

THEORY:

File Parsing basically refers to how information can be read from particular file or text file and how that information can be used in a computer program.By parsing a data file we are able to read data from external datafile or database and we can store the data or include the data into our program as required.

In this project, we extract the coefficient of polynomials that are stored in NASA's thermodynamic data file by performing file parsing and use this data in our program to calculate the thermodynamic properties like specific heat,enthalpy,entropy using the equations.

where Cp = specific heat,H= Enthalpy,S= Entropy,T= Temperature,R= Universal gas constant

a1,a2,a3,a4,a5,a6,a7 are the coefficient of polynomials that we need to extract from the data file.

MAIN PROGRAM:

% PROGRAM TO PARSE NASA's THERMODYNAMIC DATA
clear all
close all
clc
% Defining input for universal gas constant
R = 8.314; % J /mol K
% Opening the thermo.dat file for reading data
f1 = fopen('THERMO.dat','r');
l1 = fgetl(f1);
l2 = fgetl(f1);
t = strsplit(12);
global_min = str2double(t{2});
global_mid = str2double(t{3});
global_high = str2double(t{4});
temp = linspace(global_min,global_max,400);
% We can skip the next three header lines by using fgetl in for loop
for i = 1:3
fgetl(f1);
end
for i =1:53
% First reading the line with info of species
tline_1 = fgetl(f1);
A = strsplit(line_1,'');
species_name = (A{1});
% Now extracting the 14 high and low temperature coefficients
% Reading the three lines from which the coefficients are extracted
tline_2 = fgetl(f1);
tline_3 = fgetl(f1);
tline_4 = fgetl(f1);
% Finding the location of E to extract the coefficient as string
a = strfind(tline_2,'E');
b = strfind(tline_3,'E');
c = strfind(tline_4,'E');
% Extraction of 7 high temp coefficients a1,a2,a3,a4,a5,a6,a7
a1 = str2double(tline_2(1:a(1)+3));
a2 = str2double(tline_2(a(1)+4:a(2)+3));
a3 = str2double(tline_2(a(2)+4:a(3)+3));
a4 = str2double(tline_2(a(3)+4:a(4)+3));
a5 = str2double(tline_2(a(4)+4:a(5)+3));
a6 = str2double(tline_3(1:b(1)+3));
a7 = str2double(tline_3(b(1)+4:b(2)+3));
% Extraction of 7 low temp coefficients b1,b2,b3,b4,b5,b6,b7
b1 = str2double(tline_3(b(2)+4:b(3)+3));
b2 = str2double(tline_3(b(3)+4:b(4)+3));
b3 = str2double(tline_3(b(4)+4:b(5)+3));
b4 = str2double(tline_4(1:c(1)+3));
b5 = str2double(tline_4(c(1)+4:c(2)+3));
b6 = str2double(tline_4(c(2)+4:c(3)+3));
b7 = str2double(tline_4(c(3)+4:c(4)+3));
% Making new subfolder species to save the plots of individual species
mkdir = (['C:UsersdelcocumentsMATLABParsing_Newspecies',species_name]);
% Calculation of specific heat by calling function
c_p = specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,temp,global_mid);
% Calculation of enthalpy by calling function
h = Enthalpy(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp,global_mid);
% Calculation of entropy ny calling function
S = entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp,global_mid);
% Plotting the values of specific heat,enthalpy,entropy vs temperature
figure(1)
% Specific heat vs temperature plot
subplot(3,1,1)
plot(temp,C_p,'linewidth',4,'color','b');
title(species_name)
xlabel('Temperature')
ylabel('Specific heat')
% Enthalpy vs temperature plot
subplot(3,1,2);
plot(temp,h,'linewidth',4,'color','r');
title(species_name)
xlabel('Temperature')
ylabel('Enthalpy')
% Entropy vs temperature plot
subplot(3,1,3)
plot(temp,S,'linewidth',4,'color','g');
title(species_name)
xlabel('Temperature')
ylabel('Entropy')
% Saving the jpd files of plots for each species in individual species
% folder
directory=(['C:UsersdelcocumentsMATLABParsing_Newspecies',species_name]);
old_directory = cd(directory);
saveas(1,'output1.jpg');
% Changing current directory back to the original folder
cd(old_folder);
% Displaying values of molecular weight in the command window
MW = molecular_weight(species_name);
end
% Closing the file
fclose(f1);
Matlab Function code for specific heat:
% Separate function for specific heat
function [c_p]=specific_heat(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5,R,temp,global_mid)
if temp<=global_mid
c_p=R*(b1+(b2*temp)+(b3*(temp).^2)+(b4*(temp).^3)+(b5*(temp).^4));
else
c_p=R*(a1+(a2*temp)+(a3*(temp).^2)+(a4*(temp).^3)+(a5*(temp).^4));
end
Matlab Function code for enthalpy:
% Separate function for enthalpy calculation
function [h] = Enthaply(a1,a2,a3,a4,a5,a6,b1,b2,b3,b4,b5,b6,R,temp,global_mid)
if temp <= global_mid
h = R.*temp.*(b1+(b2.*(temp./2))+(b3.*(temp./3))+(b4.*(temp./4))+(b5.*(temp./5))+(b6./temp));
else
h = R.*temp.*(a1+(a2.*(temp./2))+(a3.*(temp./3))+(a4.*(temp./4))+(a5.*(temp./5))+(a6./temp));
end
Matlab Function code for entropy:
% Separate function for entropy calculation
function [S] = entropy(a1,a2,a3,a4,a5,a7,b1,b2,b3,b4,b5,b7,R,temp,global_mid)
if temp <= global_mid
S = R.*(b1.*log(temp)+(b2.*temp)+(b3.*(((temp).^2)./2))+(b4.*(((temp).^3)./3))+(b5.*(((temp).^4)./4))+b7);
else
S = R.*(a1.*log(temp)+(a2.*temp)+(a3.*(((temp).^2)./2))+(a4.*(((temp).^3)./3))+(a5.*(((temp).^4)./4))+a7);
end
Matlab Function code for molecular weight:
% Separate function for molecular weight calculation
function [MW] = molecular_weight(species_name)
element = {'H','C','N','O','Ar'};
atomic_weight = [1 12 14 16 40];
MW = 0;
for i =1:length(species_name)
for j = 1:length(atomic_weight)
if strcmpi(species_name(i),element(j))
MW = MW + atmoic_weight(j);
q=j;
end
end
m = str2double(species_name(i));
if m>1
MW = MW + (atmic_weight(q)*(m-1));
end
end
end
Program Description:
(1) In this program first we define the input value for universal gas constant which will be later used in the program.After that we start the file parsing by first opening the 'THERMO.dat' file for reading the file.
(2) Then we use the fgetl command to ignore the comments and store the temperature in form of string and split the string to store the minimum,maximum and mid temperatures.
(3) After that to extract the coefficient for each species we use for loop.Then we read the first three lines using the fgetl command and store the data in the form of string.Then to identify each coefficient we trace the location of 'E' then extract and convert the coefficient from string to numbers using str2double command.
(4) We distinguish the first 7 coefficient as high temperature coefficient and the next 7 as low temperature coefficient.
(5) We create separate functions for calculation of specific heat,enthalpy,entropy using the above coefficient.
(6) After that we plot specific heat,enthalpy,entropy vs temperature.
(7) Now we save the plot for each species in individual species foder. For thatwe make new subfolder using the 'mkdir' command by name of each species and save the jpg file of plots in it.
(8) Now we create separate function for calculation of molecular weight and return the values in main program to display the molecular weight in command window.
(9) Finally we close the file.
Conclusion:
We have successfully extracted the coefficient from NASA's thermodynamic data and used in our program to calculate the required properties and plots and saved the figures in individual species folders using this program.
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.