Challenge

Parsing NASA thermodynamic data

Aim

Main aim of this project is to read the details from NASA thermodynamic file using MATLAB and to do some calculations with it.

Objectives

Following points are the objectives of this project,

  • Creating a programme to extract all the coefficients from the file and calculating entropy, enthalpy and specific heats for species.
  • Finding the molecular weight of species.
  • Plotting graphs for enthalpy, entropy and specific heat for species.

Theory

File parsing

In computer languages, it refers to a syntactic analysis of the input code that we give. Parsing of a file means reading the data’s and arranging it. Here we have a data file named NASA thermodynamic data. It has data’s for 53 species and 14 coefficients. We must use the data’s from it and we must calculate the parameters mentioned in the objectives using given equations.

Given data’s,

Formula for calculating enthalpy, entropy and specific heat

Were,

Cp = Specific heat

H = Enthalpy

S = Entropy

R = Universal gas constant

T = Temperature

a = coefficient

The first seven coefficients are high temperature coefficient and second seven temperatures are low temperature coefficients.

Main Code

close all
clear all
clc
% Universal gas const. in J/mol.K
R = 8.3144598;
% Oprning file
f1 = fopen('THERMO.dat','r');
% Reading Heading
Heading = fgetl(f1);
% Reading Global Temperature
tline = fgetl(f1);
temperature = strsplit(tline);
low_temp = str2num(temperature{2});
mid_temp = str2num(temperature{3});
high_temp = str2num(temperature{4});
% Reading Comments
for i = 1:3
    comment = fgetl(f1);
end
T = linspace(low_temp,high_temp,100);
% Extracting co-eff of species
for j = 1:53
species_line = fgetl(f1);
species_name = strsplit(species_line,' ');
species = (species_name{1});
% Local Temperature
local_low_temp = str2double(species_name{end-3});
local_mid_temp = str2double(species_name{end-1});
local_high_temp = str2double(species_name{end-2});
first_line = fgetl(f1);
a = findstr(first_line,'E');
second_line = fgetl(f1);
b = findstr(second_line,'E');
third_line = fgetl(f1);
c = findstr(third_line,'E');
% Coefficients
a1 = str2num(first_line(1:a(1)+3));
a2 = str2num(first_line(a(1)+4:a(2)+3));
a3 = str2num(first_line(a(2)+4:a(3)+3));
a4 = str2num(first_line(a(3)+4:a(4)+3));
a5 = str2num(first_line(a(4)+4:a(5)+3));
a6 = str2num(second_line(1:a(1)+3));
a7 = str2num(second_line(a(1)+4:a(2)+3));
a8 = str2num(second_line(a(2)+4:a(3)+3));
a9 = str2num(second_line(a(3)+4:a(4)+3));
a10 = str2num(second_line(a(4)+4:a(5)+3));
a11 = str2num(third_line(1:a(1)+3));
a12 = str2num(third_line(a(1)+4:a(2)+3));
a13 = str2num(third_line(a(2)+4:a(3)+3));
a14 = str2num(third_line(a(3)+4:a(4)+3));
% Calculations
Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,mid_temp);
H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,mid_temp);
S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,R,T,mid_temp);
W = molecular_weight(species);
% Providing directory
cd('C:UsersLENOVODesktopNASA File ParsingSpecies');
cd(species)
% Plotting
figure(1)
plot(T,Cp)
title(sprintf('Specific Heat Vs Temperature for %s',species))
xlabel('Temperature(K)')
ylabel('Specific Heat(KJ)')
saveas(gcf,'Cp Vs T.png')
figure(2)
plot(T,H)
title(sprintf('Enthalpy Vs Temperature for %s',species))
xlabel('Temperature(K)')
ylabel('Enthalpy(KJ/MolK)')
saveas(gcf,'H Vs T.png')
figure(3)
plot(T,S)
title(sprintf('Entropy Vs Temperature for %s',species))
xlabel('Temperature(K)')
ylabel('Entropy(KJ/Mol)')
saveas(gcf,'S Vs T.png')
% Changing directory
cd('C:UsersLENOVODesktopNASA File Parsing')
end

Explanation

Firstly, we are starting the code with close all, clear all, clc command to avoid unwanted files. Universal gas constant R must be assigned to the programme. So we provide its value to the code referring from internet.

We have the given data file named THERMO.dat. It is opened in matlab using fopen command. The data file has heading, temperatures and some comments. Those lines are obtained using fgetl command. Three comment lines are there, so we use for loop instead of typing fgetl command three times. Temperatures are arranged in one line. So for our easiness, it is separated into individual values using strsplit command. Str2num command is also used to separate the individual temperatures and assign them as low, mid and high temperatures. Temperature range is given using linspace command.

Now we must extract all the coefficients and related datas for fity three species. Fgetl and strsplit command is used respectively to get each species. Then local individual temperatures are assigned using str2double command.Logic behind getting the coefficients value is, locating the ‘E’ in the line and separating values before it and after it. Using strfind command we can execute this. Calculations are simply done by functional codes. We have already prepared programmes for specific heat, enthalpy, entropy and molecular weight. Those codes will be explained below.

Now the outputs are directed to save in a target folder. Cd command is used for it. Calculated values are used plot graphs using plot command. Title command is used for giving title for the plot, label command is used to label the axis of plot and saveas command is used to save the generated plot. Plotting for Cp, H and S are are done. for loop command will perform same procedure for every species.

Function Codes

Code for Specific Heat(Cp)

function Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,tempmid)
if (T<=tempmid)
    Cp = R*(a8+a9.*T+a10.*T.^2+a11.*T.^3+a12.*T.^4);
else
    Cp = R*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
end

Here we are creating a function code for specific heat and assigning its related data’s. We know the equation to find out Cp. So coefficients needed for that, gas constant and temperatures are given. Then the standard equation is given in the form of if else. Because we need to use particular coefficients if temperature is less then medium temperature and another set of coefficients if temperature is greater than medium temperature.

Code for Enthalpy(S)

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

Here we are creating a function code for Enthalpy and assigning its related data’s. We know the equation to find out H. So coefficients needed for that, gas constant and temperatures are given. Then the standard equation is given in the form of if else. Because we need to use particular coefficients if temperature is less then medium temperature and another set of coefficients if temperature is greater than medium temperature.

Code for Entropy(S)

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

Here we are creating a function code for S and assigning its related data’s. We know the equation to find out S. So coefficients needed for that, gas constant and temperatures are given. Then the standard equation is given in the form of if else. Because we need to use particular coefficients if temperature is less then medium temperature and another set of coefficients if temperature is greater than medium temperature.

Code for Molecular Weight(W)

function W = molecular_weight(species)
s = ['H' 'C' 'O' 'N' 'Ar'];
atomic_weight = [1.00784,12.0107,15.999,14.0067,39.948];
W = 0;
for i = 1:length(species);
    for j = 1:length(s)
        if strcmp(species(i),s(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('The molecular weight of %s is %d g/mol',species);
fprintf('%f',W);
disp(' ')
end

Function code for molecular weight is created and species is assigned. Then we will give the mail elements of all fifty three species. Next, atomic weights of those elements are assigned to the programme after referring the internet. Initial value of Molecular weight is given as zero. Now we will create two loops which will go through all the species and elements. Str2num command is used to assign value to n which is the number of elements in a species. If command is used to calculate the molecular weight. If species contains more than one element, then the command will add up the individual atomic weight. Fprintf and disp command is used to get the output in the command window.

Results

Molecular weight of each species were calculated and it was displayed in the command window. Respected screen shots are given below.

All the species folders were saved in the directed location. Assigned plots were also got saved in respected folder.

Plots

Plots of Cp, H and S for O2, N2 and CO2 are given below.

O2

N2

CO2

Conclusion

All the objectives were executed and the results were obtained.

Reference

Some of the reference used while writting the report are mentioned below.

https://en.wikipedia.org/wiki/Parsing

https://in.mathworks.com/help/matlab/referencelist.html?type=function&category=characters-and-strings&s_tid=CRUX_topnav

https://in.mathworks.com/support/search.html?q=parsing&page=1

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.