Challenge

Project 1 - Parsing NASA thermodynamic data

OBJECTIVE

  • To write a code to extract all the temperature coefficients from a NASA thermodynamic file(THERMO.dat) and calculate the enthalpy,specific heat, entropy of the species in the file.
  • Plot the graphs for enthalpy,entropy and specific heats of specific species.
  • To calculate the molecular weight of all the species.

THEORY

File parsing: File parsing means reading into a data stream/file and building an in memory model of the semantic content of the data.This helps to facilitate some kind of transformation on the data.

Specific heat: It is the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree.

Cp = (a1+(a2.*T_r)+(a3.*(T_r).^2)+(a4.*(T_r).^3)+(a5.*(T_r).^4)).*R

Entropy:a measure of the unavailable energy in a closed thermodynamic system that is also usually considered to be a measure of the system's disorder, that is a property of the system's state.

S = R*(a1.*log(T_r)+(a2.*T_r)+((a3.*(T_r).^2)/2)+((a4.*(T_r).^3)/3)+((a5.*(T_r).^4)/4)+a7)

Enthalpy:It is the sum of the internal energy and product of pressure and volume of the system.

H = R*T_r.*(a1=(a2*T-r/2)+((a3*(T_r).^2)/3)+((a4*(T_r).^3)/4)+((a5*(T_r).^4)/5)+a6./T_r))

Main code

clear all
close all
clc
R = 8.314
f1 = fopen('THERMO.dat','r')
get = fgetl(f1);

temp = fgetl(f1);

%splitting the terms
s = strsplit(temp);
%converting strings cells into numbers
low_temp = str2num(s{2})
mid_temp = str2num(s{3})
high_temp = str2num(s{4})
% Defining temp range
T_r = linspace(low_temp,high_temp,1000)

%for skipping the lines
for i=1:3
get = fgetl(f1);

end
% Getting each species
for j = 1:53
tline = fgetl(f1);
A=strsplit(tline,' ');
species = (A{1});

low_temp = str2num(A{end-3});
mid_temp = str2num(A{end-2});
high_temp = str2num(A{end-1});

%getting next line
tline1 = fgetl(f1);
%finding the string E
a = strfind(tline1,'E');
% finding first 7 higher co-efficients
a1 = tline1(1:a(1)+3);
a1 = str2num(a1);
a2 = tline1(a(1)+4:a(2)+3);
a2 = str2num(a2);
a3 = tline1(a(2)+4:a(3)+3);
a3 = str2num(a3);
a4 = tline1(a(3)+4:a(4)+3);
a4 = str2num(a4);
a5 = tline1(a(4)+4:a(5)+3);
a5 = str2num(a5);

tline2 = fgetl(f1);
b = strfind(tline2,'E');
a6 = tline2(1:b(1)+3);
a6 = str2num(a6);
a7 = tline2(b(1)+4:b(2)+3);
a7 = str2num(a7);

% Finding second 7 Lower Co-efficients
a8 = tline2(b(2)+4:b(3)+3);
a8 = str2num(a8);
a9 = tline2(b(3)+4:b(4)+3);
a9 = str2num(a9);
a10 = tline2(b(4)+4:b(5)+3);
a10 = str2num(a10);

tline3 = fgetl(f1);
c = strfind(tline3,'E');
a11 = tline3(1:c(1)+3);
a11 = str2num(a11);
a12 = tline3(c(1)+4:c(2)+3);
a12 = str2num(a12);
a13 = tline3(c(2)+4:c(3)+3);
a13 = str2num(a13);
a14 = tline3(c(3)+4:c(4)+3);
a14 = str2num(a14);

% calculating Specific heat
Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T_r,R,mid_temp);

% calculating Entropy
S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T_r,R,mid_temp);

% calculating Enthalpy
H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T_r,R,mid_temp);

% calculating Molecular weight of the species
W = Molecular_weight(species);

% writing molecular weight file
MW_file = fopen('Molecular Weight.txt','W');
fprintf(MW_file,'Molecular Weight of %s is %d',species,W);
fclose(MW_file); % closing the file

%Plotting Cp, H and S with Temperature
% Saving in a folder
cd('C:\Users\giggo\OneDrive\Desktop\atomic species')

mkdir(species);
cd(species);

% Plot 1 Cp vs T
figure(1)
plot(T_r,Cp,'linewidth',2,'color','b');
xlabel('Temperature(K)');
ylabel('Specific Heat(KJ)');
title(sprintf('Specific heat vs Temperature range of %s',species));
grid on
saveas(figure(1),'Specific Heat.jpg');

% Plot 2 S vs T
figure(2)
plot(T_r,S,'linewidth',2,'color','r');
xlabel('Temperature(K)');
ylabel('Entropy(KJ/mol-K');
title(sprintf('Entropy vs Temperature range of %s', species));
grid on
saveas(figure(2),'Entropy.jpg');

% Plot 3 Cp vs T
figure(3)
plot(T_r,H,'linewidth',2,'color','g');
xlabel('Temperature');
ylabel('Enthalpy(KJ/mol)');
title(sprintf('Enthalpy vs Temperature range of %s',species));
grid on
saveas(figure(3),'Enthalpy.jpg');


end



Steps:

  • Firstly use the clc,clear all and close all commands to clear all the datas in the workspace and commadn window and to close any opened plots.
  • Used fgetl command to read the first line of the data file THERMO.dat.
  • Used thhe strsplit command the temperatures from the line and used str2num to convert them into numbers.
  • Defined the temperature range using the linspace command and assigned it to a array T_r.
  • The fgetl command was used in a for loop to skip the the next few lines.
  • Then used the fgetl command in the for loop to run through the 53 elements in the data file and splitting into cell array.
  • The 'E' string is found using the strfind command from the lines in th data file and with that find the 14 coefficients which are also string.Used the str2num command to convert these strings into numbers.
  • The first 7 coefficients are taken as the higher temperature coefficients and the next 7 are taken as the lower temperature coefficients.
  • After getting all 14 coefficients for each species from the THERMO.dat file we will call functions for calculating specific heat,enthalpy and entropy.
  • For molecular weight formula we will open a file named 'Molecular weight.txt' and print the specifc values.
  • A folder is then created to save the plots of Cp,H and S of the species.
  • The plotting of Cp,H and S was done using the plot command for all the species in the file.
  • The plots and molecular weight of the species are saved in the respective folders.
  • Finally used end command to end the loop.

Functions

For Specific heat:

function Cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T_r,R,mid_temp)

if (T_r <= mid_temp)
Cp = (a8+(a9.*T_r)+(a10.*(T_r).^2)+(a11.*(T_r).^3)+(a12.*(T_r).^4)).*R;
else
Cp = (a1+(a2.*T_r)+(a3.*(T_r).^2)+(a4.*(T_r).^3)+(a5.*(T_r).^4)).*R;
end
end

Steps

  • Created a function to calculate the specific heat.
  • Assigned the values for temperature ,R (gas const),coefficients and mid temperature.
  • Used the if statement to create and condition and make the temperature less than or equal to the mid temperature to specify the lower temperature coefficients in the formula
  • Then used the else condition to define the higher temperature coefficients in the formula.
  • Used the end commands to end the condtion loop and function.

For Entropy:

function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T_r,R,mid_temp)

if (T_r<= mid_temp)
S = R*(a8.*log(T_r)+(a9.*T_r)+((a10.*(T_r).^2)/2)+((a11.*(T_r).^3)/3)+((a12*(T_r).^4)/4)+a14);
else
S = R*(a1.*log(T_r)+(a2.*T_r)+((a3.*(T_r).^2)/2)+((a4.*(T_r).^3)/3)+((a5*(T_r).^4)/4)+a7);
end
end

Steps

  • Created a function to calculate the Entropy.
  • Assigned the values for temperature ,R (gas const),coefficients and mid temperature.
  • Used the if statement to create and condition and make the temperature less than or equal to the mid temperature to specify the lower temperature coefficients in the formula
  • Then used the else condition to define the higher temperature coefficients in the formula.
  • Used the end commands to end the condtion loop and function.

For Enthalpy:

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T_r,R,mid_temp)

if (T_r<=mid_temp)
H = R*T_r.*(a8+((a9*T_r)/2)+((a10*(T_r).^2)/3)+((a11*(T_r).^3)/4)+((a12*(T_r).^4)/5)+(a13./T_r));
else
H = R*T_r.*(a1+(a2*T_r/2)+(a3*(T_r).^2/3)+((a4*(T_r).^3)/4)+(a5*(T_r).^4)/5+(a6./T_r));
end
end

Steps

  • Created a function to calculate the Enthalpy.
  • Assigned the values for temperature ,R (gas const),coefficients and mid temperature.
  • Used the if statement to create and condition and make the temperature less than or equal to the mid temperature to specify the lower temperature coefficients in the formula
  • Then used the else condition to define the higher temperature coefficients in the formula.
  • Used the end commands to end the condtion loop and function.

To find Molecular weight:

function[W]=Molecular_weight(species)
%Defining atomic name of the species
atomic_name=['H','C','O','N','Ar'];

%Defining atomic masses of main species
atomic_weight=[1.008 12.011 15.999 14.007 39.948];

W=0; % molecular weight starting range
for i=1:length(species) %loop runs to the length of the species
for j=1:length(atomic_name) %loop runs to the length of the atomic names
if strcmp(species(i),atomic_name(j))
W=W+atomic_weight(j);
position=j;
end
end
n=str2num(species);
if n>1
W=W+atomic_weight(position)*(n-1);
end
end

fprintf('Molecular Weight of %s :- ',species);
fprintf('%f',W);
disp(' ')
end

Steps

  • Created and function to find the molecular weight and assigned species input to it.
  • Then defining the main elements in the 53 species in an array.
  • Then we will create an array for atomic weight.
  • Then we will assign the molecular wt starting range to 0 and the created a loop condition which runs to the length of the species and another nested loop is created to run through the length of the string atomic_name.
  • Used if condition and strcmp command to compare the species and atomic_name strings to calculate the molecular masses if comparison both strings found same then will assign its molecular weight.
  • Then used the str2num command and then assign it to n
  • Using if statement we will find whether the species contains more than one element if there are more then 1 elements that is n>1 then we will increment the atomic weight by the multiplication of position and n-1 to the atomic weight array.
  • Used the fprinf command to print the molecular weight and used disp command to display it.

Errors:

No significant errors were encountered during program execution.

Final results and plots

Plots of O2

Plots for N2

Plots for CO2

Screenshot of the directory where the species data are saved.

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.