Project 1 - Parsing NASA thermodynamic data
FILE PARSING
file parsing is reading in the data stream and building a memory model of the contentof the given data.Parsing splits a file or other input into pieces of data can be easily stored and manipulated.
here we are splitting string into parts and convert it into simpler than string
Here in this project we are provided with a NASA thermodynamic data file which has coefficients for different species and their values of cp,H,S and molecular weight can get calculated as follows.
formula for specific heat
cp/R=b1+b2+b3.T^2+b4.T^3+b5.T^4
formula for enthalpy
H/RT=b1+b2.T/2+b3.T^2/3+b4.T^2/4+b5.T^4/5+b6/t
formula for entropy
S/R=b1.log(T)+b2.T+b3.T^2/2+b4.T^3/3+b5.T^4/4+b7
here,
T=local temperature
b1 to b7 =high temperature coefficients
b8 to b14 = low temperature coefficients
R=gas constant and R=8.314
By using function we can calculate specific heat,enthalpy,entropy and molecular weight
here the 1st seven elements are high temperature andlast seven are low temperature
clear all
close all
clc
%reading data from given file
%fopen is used to read and write the data file here r is used to read file
%required data is taken and saven in variable f1
f1=fopen('THERMO.dat','r');
%fgetl command is used to load the file by line by line .here first and
%second line is loaded
first_line=fgetl(f1);
second_line=fgetl(f1);
%second line which has global temperature
%the strsplit command is used to split second line by help of space
%delimeter i.e(' ')
global_temp=strsplit(second_line,' ');
%the global temperature is divided into 3parts as string (i.e,300.000
%1000.000 5000.000)
%str2num command is used to convert the string values to numbers and they
%are stored in seperate variables as low,high,mid
global_low=str2num(global_temp{2});
global_mid=str2num(global_temp{3});
global_high=str2num(global_temp{4});
%here for loop is used to skip next three lines
for i=1:3
fgetl(f1)
end
%the line 1 is consists of local temperatures and that line is read with
%fgetl command
for i=1:53
line1=fgetl(f1);
%to line 1 is splitted with strsplit command and seperated as string by
%space delimeter
line2=strsplit(line1,' ');
%to get low, mid &high temperatures seperately
%to store species name i.e,o
sps_name=line2{1};
%tl =to store low temperature
tl=line2{end-3};
%th= to store high temperature
th=line2{end-2};
%tm = to store the mid temperature
tm=line2{end-1};
%and all the values of tl,th,tm stored as string values
tl=str2num(tl);
th=str2num(th);
tm=str2num(tm);
%to read the line consists of coefficient values
%here first line of coeffients are read.
data=fgetl(f1);
%here the position of E is find out in first line of coefficients
position_of_e=findstr(data,'E');
%the seperated individual coefficient values are stored as c1,c2,c3,c4,c5
%str2double used to convert the string values into numbers
c1=str2double(data(1:position_of_e(1)+3));
c2=str2double(data(position_of_e(1)+4:position_of_e(2)+3));
c3=str2double(data(position_of_e(2)+4:position_of_e(3)+3));
c4=str2double(data(position_of_e(3)+4:position_of_e(4)+3));
c5=str2double(data(position_of_e(4)+4:position_of_e(5)+3));
%same procedure is repeated for finding the other coefficients
data2=fgetl(f1);
position_of_e=findstr(data2,'E');
%the seperated coefficient values are stored as c6,c7,c8,c9,c10
c6=str2double(data2(1:position_of_e(1)+3));
c7=str2double(data2(position_of_e(1)+4:position_of_e(2)+3));
c8=str2double(data2(position_of_e(2)+4:position_of_e(3)+3));
c9=str2double(data2(position_of_e(3)+4:position_of_e(4)+3));
c10=str2double(data2(position_of_e(4)+4:position_of_e(5)+3));
%the procedure is followed again for finding the coefficients
data3=fgetl(f1);
position_of_e=findstr(data3,'E');
%the seperated coefficient values are stored as c11,c12,c13,c14
c11=str2double(data3(1:position_of_e(1)+3));
c12=str2double(data3(position_of_e(1)+4:position_of_e(2)+3));
c13=str2double(data3(position_of_e(2)+4:position_of_e(3)+3));
c14=str2double(data3(position_of_e(3)+4:position_of_e(4)+3));
%R is gas constant
R=8.314;
%t is find by using linspace command between tl,th with 100 values
t=linspace(tl,th,100);
%here specific heat(cp) and enthalpy(h),molecular weight and entropy(s)
% function is called
cp=specific_heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,th,t,R);
h=enthalpy(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,th,t,R);
s=entropy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,th,t,R);
molecular_weight_cal=molecular_weight(sps_name);
%to make the folder directory;
cd('/Users/harris19/Documents/MATLAB/nasa/nasa1/')
%mkdir (make directory) is used for creating new folder in folder
mkdir(sps_name)
cd(sps_name)
%plotting specific heat against temperature
figure(1)
plot(t,cp,LineWidth=3,Color='r')
xlabel('temperature')
ylabel('specific heat')
title(sps_name)
saveas(figure(1),'specific heat vs t','jpg')
%plotting enthalpy against temperature
figure(2)
plot(t,h,LineWidth=3,Color='g')
xlabel('temperature')
ylabel('enthalpy')
title(sps_name)
saveas(figure(2),'enthalpy vs t','jpg')
%plotting entropy against temperature
figure(3)
plot(t,s,LineWidth=3,Color='b')
xlabel('temperature')
ylabel('entropy')
title(sps_name)
saveas(figure(3),'entropy vs t','jpg')
end
%function for specific heat
%the coefficients are given as inputs for the function
function[cp]=specific_heat(c1,c2,c3,c4,c5,c8,c9,c10,c11,c12,th,t,R)
%if else statement is used for finding cp values for low and high temperature coefficients
if (t<=th)
cp=R*(c8+c9*t+c10*t.^2+c11*t.^3+c12*t.^4);
else
cp=R*(c1+c2*t+c3*t.^2+c4*t.^3+c5*t.^4);
end
end
%function for enthalpy
%the coefficients are given as inputs for the function
function [H]=enthalpy(c1,c2,c3,c4,c5,c6,c8,c9,c10,c11,c12,c13,th,t,R)
%if else statement is used for finding enthalpy for low and high temperature coefficients
if (t<=th)
H=((c8+(c9.*t)/2)+((c10.*t.^2)/3)+((c11.*t.^3)/4)+((c12.*t.^4)/5)+(c13./t)).*t*R;
else
H=((c1+(c2.*t)/2)+((c3.*t.^2)/3)+((c4.*t.^3)/4)+((c5.*t.^4)/5)+c6./t).*t*R;
end
end
%function for entropy
%the coefficients are given as inputs for the function
function[S]=entropy(c1,c2,c3,c4,c5,c7,c8,c9,c10,c11,c12,c14,th,t,R)
%if else statement is used for finding values for low and high temperature coefficients
if (t<=th)
S=((c8.*log(t))+(c9.*t)+((c10.*(t.^2))/2)+((c11.*(t.^3)/3))+((c12.*t.^4)/4)+c14)*R;
else
S=((c1.*log(t))+(c2.*t)+((c3.*(t.^2))/2)+((c4.*(t.^3)/3))+((c5.*(t.^4))/4)+c7)*R;
end
end
%function for calculating the molecular weight
% array is defined for the atoms and atomic weights.
%The input string is compared with character array ,when a match occurs,
%and associated atomic weight is taken
function MW=molecular_weight(sps_name)
species=(sps_name);
atoms=['H','C','O','N','A'];
atomic_weight = [1 12 16 14 39];
MW = 0;
%for molecules having numbers defining the number of atoms, that num is
%first extracted calculation is made to account for account for atomic weights
for i=1:length(species)
for j = 1:length(atoms)
%comparing the strings(species and atoms) by using strcmp command
if strcmp(species(i),atoms(j))
MW=MW+atomic_weight(j);
position = j;
end
end
%if the value of n is greater than 1 MW will get calculated
n=str2double(species(i));
if n>1
MW=MW+(atomic_weight(position).*(n-1));
end
end
%to print molecular weight command fprintf is used
fprintf('molecular weight of %s= %d',species,MW)
end
conclusion
The code to parse NASA thermodynamic has been done successfully,thus the program runs on its own extract data from the file.specific heat ,enthalpy,entropy and its plot with respect to temperature has been plotted in a seperate folder .and molecular mass of the each species is founded.
references
molecular weight of each species is attached below