Project 1 - Parsing NASA thermodynamic data
Aim : 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 .
Calculate the molecular weight of each species and display it in the command window.
Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
Introduction :
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 is following:
clear all
close all
clc
%open the file in read mode
f = fopen('THERMO.dat','r')
%Header
tline1 = fgetl(f);
%reading second line
tline2 = fgetl(f);
%spliting by space
x = strsplit(tline2,' ');
%converting string to number and assigning to global temp. values
global_low_Temp = str2num(x{2});
global_mid_Temp = str2num(x{3});
global_high_Temp = str2num(x{4});
%skip the line 3,4,5 (comment lines)
tline3 = fgetl(f);
tline4 = fgetl(f);
tline5 = fgetl(f);
%running the for loop for 53 species
for i = 1:53
species = fgetl(f); %extracting species
species_name = species(1:6) %assigning 6 character to the species_name
%extracting the co.eff from the species's data 1st line
tline_1 = fgetl(f);
y1 = strfind(tline_1,'E');%finding the chracter E position
a1 = str2num(tline_1(1:(y1(1)+3)));
a2 = str2num(tline_1((y1(1)+4):(y1(2)+3)));
a3 = str2num(tline_1((y1(2)+4):(y1(3)+3)));
a4 = str2num(tline_1((y1(3)+4):(y1(4)+3)));
a5 = str2num(tline_1((y1(4)+4):(y1(5)+3)));
%extracting the co.eff from the species's data 2nd line
tline_2 = fgetl(f);
y2 = strfind(tline_2,'E'); %finding the chracter E position
a6 = str2num(tline_2(1:(y2(1)+3)));
a7 = str2num(tline_2((y2(1)+4):(y2(2)+3)));
a8 = str2num(tline_2((y2(2)+4):(y2(3)+3)));
a9 = str2num(tline_2((y2(3)+4):(y2(4)+3)));
a10 = str2num(tline_2((y2(4)+4):(y2(5)+3)));
%extracting the co.eff from the species's data 3rd line
tline_3 = fgetl(f);
y3 = strfind(tline_3,'E');%finding the chracter E position
a11 = str2num(tline_3(1:(y3(1)+3)));
a12 = str2num(tline_3((y3(1)+4):(y3(2)+3)));
a13 = str2num(tline_3((y3(2)+4):(y3(3)+3)));
a14 = str2num(tline_3((y3(3)+4):(y3(4)+3)));
%for each species crating a directory
mkdir('/MATLAB Drive/Published/',species_name)
%Universal gas constant
R = 8.314;
%local temperature range
%assuming betwwen 300 to 5000- local temperature
Temp = linspace(300,5000,100);
%The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.
%assuming <3000 is low-temperature and >3000 is higher temperature
for j = 1:length(Temp)
if Temp(j)>3000
Cp = (a1+a2.*Temp+a3.*Temp.^2+a4.*Temp.^3+a5.*Temp.^4).*R;
H = (a1+(a2.*Temp)./2+(a3.*Temp.^2)/3+(a4.*Temp.^3)/4+(a5.*Temp.^4/5)+a6./Temp).*Temp.*R;
S = (a1*log(Temp)+a2.*Temp+(a3.*Temp.^2)/2+(a4.*Temp.^3)/3+(a5.*Temp.^4)/4+a7).*R;
else
Cp = (a8+a9.*Temp+a10.*Temp.^2+a11.*Temp.^3+a12.*Temp.^4).*R;
H = (a8+(a9.*Temp)/2+(a10.*Temp.^2)/3+(a11.*Temp.^3)/4+(a12.*Temp.^4/5)+a13./Temp).*Temp.*R;
S = (a8.*log(Temp)+a9.*Temp+(a10.*Temp.^2)/2+(a11.*Temp.^3)/3+(a12.*Temp.^4)/4+a14).*R;
end
end
%plot
%Temperature vs. Entropy
figure(1)
plot(Temp,S,'linewidth',3,'color','r')
title(species_name)
xlabel('Temperature')
ylabel('Entropy')
%Temperature vs Enthalpy
figure(2)
plot(Temp,H,'linewidth',3,'color','g')
title(species_name)
xlabel('Temperature')
ylabel('Enthalpy')
%Temperature vs Specific heat
figure(3)
plot(Temp,Cp,'linewidth',3,'color','k')
title(species_name)
xlabel('Temperature')
ylabel('Specific heat')
%directory in which the plot will be saved as per species name
directory = (['/MATLAB Drive/Published/',species_name]);
cd(directory)
%plot will be save as
saveas(1,'Entropy.jpg');
saveas(2,'Enthalpy.jpg');
saveas(3,'Specific heat.jpg');
%returning to the main directory where we have our THERMO.dat file
cd('/MATLAB Drive')
%calculating molecular mass by calling the function 'molecular_mass'
m = molecular_mass(species_name)
end
fclose(f) %closing the file is very important
Code for the molecular weight calculation :
function m = molecular_mass(species_name)
species = upper(species_name);
molecule = ['H', 'C', 'N', 'O', 'A'];
atomic_weight = [1.00794 12.0107 14.0067 15.9994 39.948];
m = 0;
for i=1:length(species)
for j=1:length(molecule)
if strcmp(species(i),molecule(j))
m = m + atomic_weight(j);
u = j;
end
end
n = str2num(species(i));
if n>1
m = m + atomic_weight(u)*(n-1);
end
end
end
Code and Logic Explanation :
open the file 'THERMO.dat' in read mode by the function fopen .
basically skipping the header line which is done by fget function.
- extract the following line by fgetl
- finding the chracter E position by strfind function.
- then identifying the E postion we can get position of every coefficient respect to E position and convert them into number by str2num function.
- follow same procedure for 3 consecutive lines.
- Temperature vs. Entropy
- Temperature vs Enthalpy
- Temperature vs Specific heat