Challenge

Project 1 - Parsing NASA thermodynamic data

AIM: To write a code in Matlab to parse the NASA thermodynamic data file and calculate the thermodynamic properties of various gas species

OBJECTIVE:

Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file.

The formula for specific heat(Cp), enthalpy(H), and entropy(S) are given below;

Where R is the universal gas constant, T is the temperature

The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.

To 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.

To save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All these should be generated automatically.

THEORY:

NASA THERMODYNAMIC FILE:

The NASA thermodynamic file (THERMO.dat) contains data of 53 species and each species contains 14 coefficients and temperature ranges. We have to find these 14 coefficients to find specific heat, enthalpy and entropy

where,

R - Universal gas constant

T - Temperature

Cp - Specific heat[K]

H - Enthalpy(KJ/mol)

S - Entropy(KJ /mol-K)

FILE PARSING:

Parse: To parse data or information means to break it down into component parts so that its syntax can be analyzed, and understood

Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages, or data structures, conforming to the rules of formal grammar.

Parsing a file means reading in a data stream of some sort and building an in a memory model of the semantic content of the data. This aims at facilitating performing some kind of transformation on the data.

SPECIFIC HEAT: is the quantity of heat required to raise the temperature of one gram of a substance by one Celcius degree.

%specific heat calculation function

function Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp)

%comparing with global temperature

%if less than global temperature - minimum coefficiients are taken

if Temp <= global_medium_temp

Cp = R*(a8+(a9*Temp)+(a10*(Temp)^2)+(a11*(Temp).^3)+(a12*(Temp).^4));

else

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

end​

steps:

We find the Specific heat by creating a separate specific heat function and assigning its related formula coefficient from the 14 coefficients that we found out,Gas constant, Temperature, and medium temperature

Now we create a condition using the if loop and make temperature less than and equal to medium temperature to specify the lower temperature coefficients in the formula

The else condition is used to specify coefficient of higher temperature coefficient in the formula

Then end the loop and function to calculate the Specific heat of species

ENTROPY: is the measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system

% Entropy calculation

function S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp, R,global_medium_temp);

%copmparing with global temperature

%if less than global temperature - minimumcoefficients are taken

if Temp <= global_medium_temp

S = R.*(a8.*log(Temp)+(a9.*Temp)+(a10*((Temp).^2)/2)+(a11*((Temp).^3)/3)+(a12*((Temp).^4)/4)+a14);

else

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

end​

steps:

We find the Entropy by creating a separate specific heat function and assigning its related formula coefficient from the 14 coefficients that we found out,Gas constant, Temperature, and medium temperature

Now we create a condition using the if loop and make temperature less than and equal to medium temperature to specify the lower temperature coefficients in the formula

The else condition is used to specify coefficient of higher temperature coefficient in the formula

Then end the loop and function to calculate the Entropy of species

ENTHALPY: is the sum of the system's internal energy and the product of its pressure and volume.

%Enthalpy calculation

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

%comparing with global temperature

%if less than global temperature - minimum coefficients are taken

if Temp <= global_medium_temp

H = R*Temp.*(a8 + ((a9*Temp)/2)+((a10*(Temp).^2)/3)+((a11*(Temp).^3)/4)+((a12*(Temp).^4)/5)+((a13./Temp)));

else

H = R*Temp.*(a1 + ((a2*Temp)/2)+((a3*(Temp).^2)/3)+((a4*(Temp).^3)/4)+((a5*(Temp).^4)/5)+((a6./Temp)));

end

steps:

We find the Enthalpy by creating a separate specific heat function and assigning its related formula coefficient from the 14 coefficients that we found out,Gas constant, Temperature, and medium temperature

Now we create a condition using the if loop and make temperature less than and equal to medium temperature to specify the lower temperature coefficients in the formula

The else condition is used to specify coefficient of higher temperature coefficient in the formula

Then end the loop and function to calculate the Enthalpyt of species

MOLECULAR WEIGHT: Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule. Molecular weight is used in chemistry to determine stoichiometry in chemical reactions and equations. Molecular weight is commonly abbreviated by M.W. or MW. Molecular weight is either unitless or expressed in terms of atomic mass units (amu) or Daltons (Da).

% Molecular weight calculation

function W = Molecular_weight(species)

atomic_name = ['H','C','O','N','Ar'];

atomic_weight = [1.008 12.011 15.999 14.007 39.948];

W = 0;

for i = 1:length(species)

for j = 1:length(atomic_name)

if strcmp(species(i),atomic_name(j))

W = W +atomic_weight(j);

position = j;

end

end

n= str2double(species);

if n>1

W = W + atomic_weight(position)*(n-1);

end

end

end

​

Steps:

Firstly we create a Molecular_weight function W and assign species as input to the function

Then we define the main elements of the 53 species n an array named atomic_name

Corresponding to the main elements we create an array atomic_weight and specify their atomic weights

Then we assign the Molecular_weight starting range to zero i.eW = 0

Further, we create a condition using if condition Inside the if condition we use strcmp command to compare the species and atomic_name strings to calculate the molecular masses

If by comparison both strings are found the same then we will assign their molecular weight.

After that, we utilize the str2double command and assign it to n

If there is more than one element i.e n>1 we use the if condition

MAIN CODE:

clear all

close all

clc

%input

R = 8.314; % gas constant

%reading the data file

% open fileand read THERMO.dat

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

get = fgetl(f1);

%reading temp

%splitting

global_temp = fgetl(f1);

t = strsplit(global_temp);

%converting string to number

global_low_temp = str2num(t{2});

global_medium_temp = str2num(t{3});

global_high_temp = str2num(t{4});

%temperature range

Temp = linspace(global_low_temp,global_high_temp,1000);

%skipping comment lines

for i = 1:3

get = fgetl(f1);

end

%reading the species

for j = 1:53

tline = fgetl(f1);

A = strsplit(tline,' ');

species = (A{1});

line_1 = fgetl(f1);

a = strfind(line_1,'E');

% EXTRACTING THE COEFFICIENTS

a1 = line_1(1:a(1)+3);

a1 = str2num(a1);

a2 = line_1(a(1)+4:a(2)+3);

a2 = str2num(a2);

a3 = line_1(a(2)+4:a(3)+3);

a3 = str2num(a3);

a4 = line_1(a(3)+4:a(4)+3);

a4 = str2num(a4);

a5 = line_1(a(4)+4:a(5)+3);

a5 = str2num(a5);

line_2 = fgetl(f1);

b = findstr(line_2,'E');

a6 = line_2(1:b(1)+3);

a6 = str2num(a6);

a7 = line_2(b(1)+4:b(2)+3);

a7 = str2num(a7);

a8 = line_2(b(2)+4:b(3)+3);

a8 = str2num(a8);

a9 = line_2(b(3)+4:b(4)+3);

a9 = str2num(a9);

a10 = line_2(b(4)+4:b(5)+3);

a10 = str2num(a10);

line_3 = fgetl(f1);

c = findstr(line_3,'E');

a11 = line_3(1:c(1)+3);

a11 = str2num(a11);

a12 = line_3(c(1)+4:c(2)+3);

a12 = str2num(a12);

a13 = line_3(c(2)+4:c(3)+3);

a13 = str2num(a13);

a14 = line_3(c(3)+4:c(4)+3);

a14 = str2num(a14);

% CALCULATION OF VARIOUS PARAMETERS

Cp = Specificheat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,global_medium_temp);

H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,global_medium_temp);

S = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,global_medium_temp);

cd('C:\Users\TIGER\Documents\MATLAB\Matlab programs\parsing_project')

mkdir(species);

cd(species)

figure(1)

plot(Temp,Cp,'linewidth',2,'color','r');

xlabel('Temperature[K]');

ylabel('Specific Heat[KJ]');

title(sprintf('Varying Specific Heat with Temperature Range of %s',species));

grid on ;

saveas(figure(1),'specific Heat,jpg');

figure(2)

plot(Temp,H,'linewidth',2,'color','g')

xlabel('Temperature[K]');

ylabel('Enthalpy in [KJ/(mol)]');

title(sprintf('Varying Enthalpy with Temperature Range of %s',species));

grid on

saveas(figure(2),'Enthalpy,jpg');

figure(3)

plot(Temp,S,'linewidth',2,'color','b')

xlabel('Temperature[K]');

ylabel('Entropy in [KJ/(mol-k)]');

title(sprintf('Varying Entropy with Temperature Range of %s',species));

grid on

saveas(figure(3),'Entropy,jpg');

cd ..

W = Molecular_weight(species);

sprintf('Molecular weight of %s = %f',species,W)

end

OUTPUT: PLOTS

Plot for 02




Plot for N2



Plot for C02


Screenshot of the directory where different species are saved:


Output of Molecular weight of the different species:




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.