Challenge

Project 1 - Parsing NASA thermodynamic data

Aim : To Code a function that extracts the 14 co-efficients using NASA Thermodynamic data.

Objective : To wirte a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file. Using the LOCAL TEMPERATURE RANGES , GLOBAL TEMPERATURE RANGE and the molecular weight of each species .

clear all
close all
clc

% Opening the file


f1 = fopen('thermo.dat','r');

% skipping the word Thermo from (Thermo.dat )file
fgetl(f1);

% loading the next word from file
a = fgetl(f1);

% spliting the valves from string to number
b = strsplit(a);
global_low_temp = str2num(b{2});
global_mid_temp = str2num(b{3});
global_high_temp = str2num(b{4});

% skipping the next which are not useful


fgetl(f1);
fgetl(f1);
fgetl(f1);

% defining universal gas constant in j/mol-K
R = 8.314

% the values present in thermo.dat file are 53
for i = 1:53

% loading the next line
tline1 = fgetl(f1);

% splitting the line from string to number


c = strsplit(tline1,' ');
speciesnum = c{1};
x = length(c);
local_low = str2num(c{x-3});
local_high = str2num(c{x-2});
local_mid = str2num(c{x-1});

% loading the next line

tline2 = fgetl(f1);

% finding the Eth position and splitting the line from string to number


c1 = strfind(tline2,'E');
a1 = str2num(tline2(1:c1(1)+3));
a2 = str2num(tline2( c1(1)+4:c1(2)+3));
a3 = str2num(tline2 (c1(2)+4:c1(3)+3));
a4 = str2num (tline2(c1(3)+4:c1(4)+3));
a5 = str2num(tline2(c1(4)+4:c1(5)+3));

% loading the next line

tline3 = fgetl(f1);

% finding the Eth position and splitting the line from string to number
c2 = strfind(tline3,'E');
a6 = str2num(tline3(1:c2(1)+3));
a7 = str2num(tline3( c2(1)+4:c2(2)+3));
a8 = str2num(tline3 (c2(2)+4:c2(3)+3));
a9 = str2num (tline3(c2(3)+4:c2(4)+3));
a10 = str2num(tline3(c2(4)+4:c2(5)+3));

% loading the next line
tline4 = fgetl(f1);

% finding the Eth position and splitting the line from string to number
c3 = strfind(tline4,'E');
a11 = str2num(tline4(1:c3(1)+3));
a12 = str2num(tline4( c3(1)+4:c3(2)+3));
a13 = str2num(tline4 (c3(2)+4:c3(3)+3));
a14 = str2num (tline4(c3(3)+4:c3(4)+3));

% defining the temperature
T = linspace(local_low,local_mid,local_high);

if(T<local_mid)
cp=R*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
h=R.*T.*(a8+a9.*T/2+a10.*T.^2/3+a11.*T.^3/4+a12.*T.^4/5+a13./T);
s = R*(a8*log(T)+a9.*T+a10.*T.^2/2+a11.*T.^3/3+a12.*T.^4/4+a14);
else
cp=R*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
h=R.*T.*(a1+a2.*T/2+a3.*T.^2/3+a4.*T.^3/4+a5.*T.^4/5+a6./T);
s = R*(a1*log(T)+a2.*T+a3.*T.^2/2+a4.*T.^3/3+a5.*T.^4/4+a7);
end

% creating a directory
mkdir(speciesnum)
cd(speciesnum)

% plotting
figure(1)
plot(T,cp)
xlabel('T')
ylabel('cp')
title('temperature vs cp',speciesnum)
figure(2)
plot(T,h)
xlabel('T')
ylabel('h')
title('temperature vs h',speciesnum)
figure(3)
plot(T,s)
xlabel('T')
ylabel('s')
title('temperature vs s',speciesnum)

% saving the plot in files
saveas(1,'temperature vs cp.png')
saveas(2,'temperature vs h.png')
saveas(3,'temperature vs s.png')
cd ..

% comparing
Primary = ['O','H','N','A','C'];
mass = [16, 1, 14 ,40 ,12];
molweight = 0;
for j = 1:length(speciesnum)
for k = 1:length(Primary)
if strcmp(speciesnum(j),Primary(k))
molweight = molweight+mass(k);
p = k;
end
end
n = str2double(speciesnum(j));
if n>1
molweight = molweight+(mass(p)*(n-1));
end
end

% displaying
q = sprintf('molweight of %s = %d',speciesnum,molweight);
disp(q)
end

% from command window the molecular weight of each species

Result :

molweight of O = 16

molweight of H = 1

molweight of N = 14

molweight of AR = 40

molweight of O2 = 32

molweight of H = 1

molweight of H2 = 2

molweight of OH = 17

molweight of H2O = 18

molweight of HO2 = 33

molweight of H2O2 = 34

molweight of C = 12

molweight of CH = 13

molweight of CH2 = 14

molweight of CH2(S) = 14

molweight of CH3 = 15

molweight of CH4 = 16

molweight of CO = 28

molweight of CO2 = 44

molweight of HCO = 29

molweight of CH2O = 30

molweight of CH2OH = 31

molweight of CH3O = 31

molweight of CH3OH = 32

molweight of C2H = 25

molweight of C2H2 = 26

molweight of C2H3 = 27

molweight of C2H4 = 28

molweight of C2H5 = 29

molweight of C2H6 = 30

molweight of CH2CO = 42

molweight of HCCO = 41

molweight of HCCOH = 42

molweight of H2CN = 28

molweight of HCN = 27

molweight of HNO = 31

molweight of N = 14

molweight of NNH = 29

molweight of N2O = 44

molweight of NH = 15

molweight of NH2 = 16

molweight of NH3 = 17

molweight of NO = 30

molweight of NO2 = 46

molweight of HCNO = 43

molweight of HOCN = 43

molweight of HNCO = 43

molweight of NCO = 42

molweight of CN = 26

molweight of HCNN = 41

molweight of N2 = 28

molweight of AR = 40

molweight of C3H8 = 44

molweight of C3H7 = 43

molweight of CH3CHO = 44

molweight of CH2CHO = 43

% The plots are attached according to the order as given in question.

Conclusion : The molweight and plots are obtained and shown.

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.