Challenge

Project 1 - Parsing NASA thermodynamic data

File Parsing the NASA thermodynamic data to evaluate the thermodynamic properties of various species.

File parsing: File parsing refers to reading a data file and resolving the required content from it. Through file parsing, one can input data bits or even manipulate the data in a file to make good use of it in code.

The NASA thermodynamic file: The THERMO.dat file by NASA contains a temperature range of 53 species and each species contains 14 coefficients.

This data can be used to calculate the thermodynamic properties such as specific heat, enthalpy, and entropy of each species in its high and low-temperature range.

NASA has specified polynomials to calculate these thermodynamic properties using this data file.

 The list of 53 species:

NASA polynomials: NASA has also specified polynomials that can be used to calculate the thermodynamic properties such as Specific heat, Entropy, Enthalpy, and Molecular weight of each species.

Where,

R = Universal gas constant

T = Temperature

Cp = Specific heat (J/kg)

S = Entropy (J/K)

H = Enthalpy (J)

a(i) = coefficients ranging from 1 to 14

Specific heat:  It is referred to as the quantity of heat required to raise the temperature of one unit of a substance by one unit from its initial temperature.

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

Entropy: It is a thermodynamic property interpreted as the degree of disorder or randomness in the system.

S = R * (a1 + lnT + a2*T + a3*T^2 + a4*T^3 + a5*T^4 + a7)

Enthalpy: It is the sum of internal energy of the system plus the product of gas in the system and its volume.

If the change enthalpy is positive, the reaction is endothermic.

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

Molecular weight: It is the average mass of a molecule when compared to 1/12 the mass of a C-12 atom. It is calculated as the sum of atomic weights of the constituent atoms of the species.

 
 Function for specific heat:
%Function for specific heat
function Cp = specific_heat (a1,a2,a3,a4,a5,a8,a9,a10,a11,T,R,med_temp)
%Calculating specific heat for higher and lower temperatures
%The mimimum coefficient is taken if it is within the global tempertaure
if(T <= med_temp)
    Cp = R*((a8+a9*T) + (a10*T.^2) + (a11*T.^3) + (a12*T.^4));
else
    Cp = R*((a2*T) + (a3*T.^2) + (a4*T.^3) + (a5*T.^4));
end
end

Function for Entropy:

%Function for entropy
function S = entropy (a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,med_temp)
%Calculating entropy for higher and lower temperatures
%The mimimum coefficient is taken if it is within the global tempertaure
if(T <= med_temp)
    S = R*((a8.*log(T)) +(a9*T) + (a10*T.^2/2) + (a11*T.^3/3) + (a12*T.^4/4));
else
    S = R*((a1.*log(T) +(a2*T) + (a3*T.^2/2) + (a4*T.^3/3) + (a5*T.^4)/4) + a);
end
end

Function for Enthalpy:

%Function for enthalpy
function H = enthalpy (a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,med_temp)
%Calculating enthalpy for higher and lower temperatures
%The mimimum coefficient is taken if it is within the global tempertaure
if(T <= med_temp)
    H = R*T.*(a8 + ((a9*T)/2) + ((a10*T.^2)/3) + ((a11*T.^3)/4) + ((a12*T.^4)/5) + (a13./T));
else
    H = R*T.*(a1 + ((a2*T)/2) + ((a3*T.^2)/3) + ((a4*T.^3)/4) + ((a5*T.^4)/5) + (a6./T));
end
end

Function for Molecular weight:

%Function for molecular weight

function M = molecular_weight(species)
%Elements in the given species
element = ['H', 'C', 'O', 'N', 'Ar'];
%Atomic masses of respective elements
atomic_mass = [1.008 12.011 15.999 14.007 39.948]
%Starting range of molecules
M=0;
for i=1:length(species)
    for j=1:length(element)
        if strcmp(species(i),element(j))
            M = M + atomic_mass(j);
            position = j;
        end
    end
    n=str2num(species);
    if n>1
        M = M + atomic_mass (position)*(n-1);
    end 
end
printf('Molecular weight of %s :-', species);
printf('%f', M);
disp('')
end

Main Program:

clear all
close all
clc

%Inputs
%Universal gas constant
R=8.314;

%Reading the THERMO.dat file
f1=fopen('THERMO.dat','r');
get = fgetl(f1);

%Reading temperature from THERMO.dat file
temp=fgetl(f1);

%Splitting the temperature data
s=strsplit(temp);

%Converting strings into numbers
lowtemp=str2num(s{2});
medtemp=str2num(s{3});
hightemp=str2num(s{4});

%Defining temperature range
T=linspace(lowtemp,hightemp,1000);

%Skip the 3 comment lines in THERMO.dat file
for i=1:3
    get=fgetl(f1);
end


%Reading species from the file
for j=1:53
    tline = fgetl(f1);

A=strsplit(get,' ');

%To read the name of the species
species=(A{1})
low_temp=str2num(A{end-3});
mid_temp=str2num(A{end-2});
high_temp=str2num(A{end-1});

tline_1=fgetl(f1);
a=strfind(tline_1,'E');

%Finding higher and lower temperture coefficients
%Finding higher teperature coefficients

a1=tline_1(1:a(1)+3);
a1=str2num(a1);
a2=tline_1(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=tline_1(a(2)+4:a(3)+3);
a3=str2num(a3);
a4=tline_1(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=tline_1(a(4)+4:a(5)+3);
a5=str2num(a5);

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

%Finding lower coefficients
a8=tline_2(b(2)+4:b(3)+3);
a8=str2num(a8);
a9=tline_2(b(3)+4:b(4)+3);
a9=str2num(a9);
a10=tline_2(b(4)+4:b(5)+3);
a10=str2num(a10);

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

%Calculate specific heat
Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,T,R,med_temp);

%Calculate Entropy
S = entropy (a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,med_temp);

%Calculate Enthalpy
H = enthalpy (a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,med_temp);

%Calculate molecular weight
M(i) = molecular_weight(species);

%Creating molecular weight file
Molecweightfile=fopen('Molecular weight.txt','w');
fprintf(Molecweightfile, 'Molecular weight of %s is %d ',species,W);
fclose(Molecweightfile);

%Creating a folder to save the plots
cd('C:\Users\Soyal Mathew Simon\skilllync\NASApolynomials')
mmkdir(species);
cd(species)

%Plotting
%Plot1: Specific heat vs temperature
figure(1)
plot(T,Cp,'linewidth',2,'color','b');
xlabel('Temperature (K)');
ylabel('Specific heat (kJ)');
title(sprintf('Specific heat vs Teperature'));
saveas(figure(1),'Specific heat.jpg');

%Plot2: Entropy vs teperature
figure(2)
plot(T,S,'linewidth',2,'color','r');
xlabel('Tempertaure (K)');
ylabel('Entropy (kJ/ol-K)');
title(sprintf('Entropy vs temperature'));
saveas(figure(2),'Entropy.jpg');

%Plot3: Enthalpy vs temperature
figure(3)
plot(T,H,'linewidth',2,'color','g');
xlabel('Temperature (K)');
ylabel('Enthalpy (kJ/mol');
title(sprintf('Enthalpy vs temperature'));
saveas(figure(3),'Enthalpy.jpg');
cd('C:\Users\Soyal Mathew Simon\skilllync')

%Molecular weight
M = molecular_weight(species)

f2=fopen('Molecular mass','w');
fprintf('%s n %s n %f n','species','Molecular mass',M)
end


 
 
Explanation:
  1. The raw data file named THERMO.dat is downloaded and saved.
  2. The THERO.dat file is opened in the program by using the fopen command.
  3. The fgetl command is used along with 'r' to read the temperature coefficients.
  4. The strsplit command is used to split the temperature range into strings.
  5. The str2num command is used to convert strings into numbers and separated so it can be used to solve the thermodynamic equations of polynomials. Thus the temperature range is set as low, medium and high temperatures.
  6. This grouped temperature range is set by using the linspace command.
  7. The first 3 comment lines in the THERMO.dat file is skipped using the fgetl command.
  8. Now another for loop is used solve the thermodynamic properties of the 53 species by assigning species name and their temperatures , by considering the given statement which states, the first 7 coefficients are for higher temperature range and the next 7 coefficients are for lower temperatures.
  9. Thus the functions for specific heat, enthalpy, entropy and molecular weightae evaluated and the graphs Cp vs T, S vs T, and H vs T are plotted for 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.

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.