Challenge

Project 1 - Parsing NASA thermodynamic data

AIM:

The main aim of the project is to write a program to evaluate the thermodynamic data such as specific heat, Enthalpy, Entropy of NASA thermodynamic data and to create the plots for all the species and to display the molecular weight calculation using MATLAB coding.

OBJECTIVE:

NASA came up with a thermodynamic data, which used to evaluate the properties such as specific heat (Cp). Enthalpy (H) Entropy(S) Now, parsing a file means reading in a data stream of some sort and building an in memory model of semantic content of that data.

DEFINATIONS:

  • Specific Heat: Specific Heat is a ratio of the quantity of heat required to raise the temperature of a body one degree to that required to raise the temperature of an equal mass e water one degree. The Sl unit of specific heat is J/KG-K
  • Enthalpy: The Enthalpy of a Thermodynamic system defined as the sum of its internal energy and the work required achieving its pressure and volume. The SI unit of specific Enthalpy is kj/kg.
  • Entropy: Entropy is a measure of degree of disorderness of the system. The SI Unit of entropy is j/k.

FILE PARSING:

From the given thermodynamic data, it clear that there are 53 species and for each species there are 14 co-efficient and for each species, there is a unique low, mid and high temperature ranges. Formula used to calculate the specific heat enthalpy and entropy using the coefficient is as shown below.

Where,

cp = specific heat in j/kg-k

R = universal gas constant (8.314j/k-mol)

H = enthalpy in kj/kg

S = entropy in j/k

EXPLANATION:

Getting header information:

Code:

clear all
close all
clc 

%To open the thermal data file
f1 = fopen('THERMO.dat','r');
fgetl(f1);
a = fgetl(f1);
b = strsplit(a);

The program starts with the clear all close all and clc which clears the previous recorded data and to open the terminal file use command fopen, to read the heading line of the data use fgetl command and to read the next line use same command (fgetl).

Getting the global temperature information:

%Global temperature values
global_low_temp = str2num(b{2});
global_mid_temp = str2num(b{3});
global_high_temp = str2num(b{4});
fgetl(f1);
fgetl(f1);
fgetl(f1);

To get the global temperature line fgetl command is used, usually information will stored in the form of strings, to get the numerical value use str2double command which converts the text in string of numerical value in string to double precision value and use {} instead of () which means the global temperature line is in the form of cell array by which we can obtain the global temperature values and to skip the next three lines use command.

Getting local temperature information of individual species:

tline1 = fgetl(f1);
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});

To get the local temperature line use fgetl command and store it as linel in which we can observe that the first is the species name and the maximum length of species name is 6 hence by giving the command line1(1:6) matlab can read the species name whose maximum length is 6 and in the same line we observe the local temperatures and local low temperature is located at 49 location to the 55th location and use str2double command to convert the string form to the numerical value and store it as low temperature and repeat the same process get high and mid temperature.

To Extract the coefficient values:

tline2 = fgetl(f1);
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));

tline3 = fgetl(f1);
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));
tline4 = fgetl(f1);
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));
T = linspace(local_low,local_mid,local_high);

To extract the value of coefficient which is used to calculate the specific heat enthalpy and entropy use fgetl command. Once we get the coefficient line of thermal data, observe that letter 'E' is present in each coefficient and use strfind command used to find the string 'E' and store it in as a,b,c and in command window it will show the location of E by which we can extract the first coefficient and it will be in the form of string and to get the numerical value use str2double command by these we can obtain all 14 coefficient.

Specific Heat, Enthalpy & Entropy functions:

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

From the formula it is observed that specific heat, Enthalpy & Entropy is functions of coefficients,universal gas constant temperature.If loop is used because their is a condition that first 7 coefficient are high temperature coefficient and second 7 coefficient are low temperature coefficient. Hence the function is coded as above.

Molecular Weight Function:

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
q = sprintf('molweight of %s = %d',speciesnum,molweight);
disp(q)

It is observed that the thermodynamics data contain mainly 5 species such as Oxygen, Hydrogen, Carbon, Nitrogen, Argon and symbols of these species is stored in array called elements and its corresponding atomic mass is stored in atomic mass array. For loop is initiated because the process is repeated for every species. strcmpi command is used to compare the string character of species name and coresponding elements and molecular weight is calculated using the formula mol_wt - mol wt+ atomic mass. If their is a repeatation of species then if loop is initiated and molecular weight is calculated as shown above.

In main codeing we write the above coding in which sprintf is used to format the molecular weight values into string vector and %d command is used to get the floating value of molecular weight of species. disp(q)

Creating Folder:

%Creating folder
mkdir(speciesnum)
cd(speciesnum)

mkdir command is used to create new folder.

Plotting:

%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)
saveas(1,'temperature vs cp.png')
saveas(2,'temperature vs h.png')
saveas(3,'temperature vs s.png')
cd ..

Plot the temperature vs Specific heat values using plot command and given the xlabel, ylabel and title for the graph is used to store the entire information in data file and cd displays the current folder and graph is saved using saveas command in png.

Main Coding:

clear all
close all
clc 

%To open the thermal data file
f1 = fopen('THERMO.dat','r');
fgetl(f1);
a = fgetl(f1);
b = strsplit(a);

%Global temperature values
global_low_temp = str2num(b{2});
global_mid_temp = str2num(b{3});
global_high_temp = str2num(b{4});
fgetl(f1);
fgetl(f1);
fgetl(f1);
R = 8.314
for i = 1:53
    
% Local Temperature
tline1 = fgetl(f1);
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});

% Extracting the Co-efficient
tline2 = fgetl(f1);
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));

tline3 = fgetl(f1);
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));
tline4 = fgetl(f1);
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));
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 folder
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)
saveas(1,'temperature vs cp.png')
saveas(2,'temperature vs h.png')
saveas(3,'temperature vs s.png')
cd ..
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
q = sprintf('molweight of %s = %d',speciesnum,molweight);
disp(q)
end

Plotting:

Plots of O2:

Specific heat (cp) vs temperature:

Enthalpy (H) vs temperature:

Entropy (S) vs temperature:

Plots of N2:

Specific heat (cp) vs temperature:

Enthalpy (H) vs temperature:

Entropy (S) vs temperature:

Plots of CO2:

Specific heat (cp) vs temperature:

Enthalpy (H) vs temperature:

Entorpy (S) vs temperature:

Significance of Plots:

Specific heat vs temperature: Specific heat of a subustance changes when their is a change in temperature.At molecular level, the internal energy of a subustance is stored in the form of transational rotational and vibrational energy. It has 3 degree of freedom for each form of energy.In lower temperature range vibrational energy is insignificant,thus the bulk of the energy is given by remaining two forms hence specific heat is comparitevely low when temperature increases,vibrational energy gains signifinance hence more amount of energy is required to raise the temperature by the same amount.Thus specific heat increases with temperature.

Enthalpy vs temperatue: When temperature is increased the amount of molecular interaction also increases then internal energy of the system rises thus, enthalpy of system increased.

H = U + PV Where U = internal energy, H = enthalpy.

Entropy vs temperatue: When temperature is increased, more amount of energy is put into the system which exites the random activity. Since entropy is a a measure of randomness in the system. entropy of system is increased.

Files of Individual Species:

Molecular Weight Calculation:

Conclusion: The data has been extracted and thermodynamic property such as specific heat, Enthalpy, Entropy is calculated and graphs has been plotted and the files of individual species is created.

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.