Challenge

Project 1 - Parsing NASA thermodynamic data

AIM :- To write a code in MATLAB / Octave to parse the NASA thermodynamic data file and then calculate the thermodynamic properties of various gas species.

OBJECTIVE :-

  • Write a function that extracts the fourteen co-efficients and calculate the enthalpy, entropy and specific heat of all the species in the data file. you will be reading in this data file NASA thermodynamic Data.
  • Calculate the molecular weight of each species and diplay it in the command window.
  • plot the cp, Enthalpy and Entropy for the local temprature range (low temprature : high temprature ) specific for each spices.
  • Save plots as  images with apropriate names and dump them seperate folders each species with suitable name. all this should be generated automatically.
  • The main programe that will you submit will just take the "THERMO.dat" file as input and generate cp,H and S plots.
  • Write a detailed report to descripe your logic.

THEORY :- 

File Parsing :-

  • File parsing "within computational linguistics the term is used to refer to the formal analysis by a computer of a sentence or other string of words into constituents, resulting in  parsing tree showing in their syntactic relation to each other , which may also contain semantic and other information.
  • Example of to parse is to break down a sentence to explain each element to someone. To analyze or seperate (input, for example) into more easily processed components. To admit of being parsed. sentences do not parse easily.

INPUTS AND FORMULA :-

  • Univercel Gas Constant

                   R = 8.3145

  • Formula to calculate the Specific Heat (cp), Enthalpy (H), Entropy (S) for all the species in data file.

              

MATLAB PROGRAM CODE :-

Function code of (cp,H,S) PNTD(Parsing NASA Thermodynamic Data):-

% Function Program of a Parsing NASA Thermodynamic Data
function [cp, H, S ]= functioncode_PNTD(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,T,local_mid)

for i=1:length(T)
if T(i)>local_mid
    cp(i) = R*((a1)+(a2*T(i))+(a3*T(i)^2)+(a4*T(i)^3)+(a5*T(i)^4));
    H(i) = ((a1)+(a2*T(i)/2)+(a3*T(i)^2/3)+(a4*T(i)^3/4)+(a5*T(i)^4/5)+(a6/T(i)))*(R*T(i));
    S(i) = R*((a1*log(T(i)))+(a2*T(i))+(a3*T(i)^2/2)+(a4*T(i)^3/3)+(a5*T(i)^4/4)+(a7));
else
     cp(i) = R*((b1)+(b2*T(i))+(b3*T(i)^2)+(b4*T(i)^3)+(b5*T(i)^4));
    H(i) = ((b1)+(b2*T(i)/2)+(b3*T(i)^2/3)+(b4*T(i)^3/4)+(b5*T(i)^4/5)+(b6/T(i)))*(R*T(i));
    S(i) = R*((b1*log(T(i)))+(b2*T(i))+(b3*T(i)^2/2)+(b4*T(i)^3/3)+(b5*T(i)^4/4)+(b7));
end
end

end

Function Code of a Calculate Molecular weight of species :-

% Calculatiing molecular weight
% Input Datas 
function [mw]= cmw_PNTD(species_name)
atoms = ['H','O','C','N','S','A'];
atomic_weight = [1.007,15.999,12.011,14.006,32.059,39.792];

mw=0;
% Creating for loop and if condition
for i = 1:length(species_name)
    for j = 1:length(atoms)
     if strcmpi(species_name(i),atoms(j))
        mw = mw+atomic_weight(j);
        pos=j;
     end 
    n=str2num(species_name(i)); 
end 
    if n>1
      mw=mw+atomic_weight(pos)*(n-1)
    end
end
end

 MAIN code of PNTD(Parsing NASA Thermodynamic Data):-

% Main programe code of a Parsing NASA Thermodynamic Data
clear all
close all
clc
% Reading data File
f1 = fopen('THERMO.dat','r');
% Reading the Data File First Line
header = fgetl(f1);
% Global Temprature
l1 = fgetl(f1);
Temprature = strsplit(l1,' ');
global_min = str2double(Temprature{2});
global_mid = str2double(Temprature{3});
global_max = str2double(Temprature{4});

% Not considering the next three lines
for i = 1:3
    l2 = fgetl(f1);
end
% Reading next line

for i=1:53
l3 = fgetl(f1);
% Parsing the NASA Thermodynamic data
l3=strsplit(l3,' ')
species_name=l3{1}
% Calculating temprature
local_min = str2double(l3{end-3});
local_mid = str2double(l3{end-1});
local_max = str2double(l3{end-2});
T = linspace(local_min,local_max,1000);

% Co-efficients
a = fgetl(f1);
b = fgetl(f1);
c = fgetl(f1);
% Finding position it E
d = strfind(a,'E');
e = strfind(b,'E');
f = strfind(c,'E');
% Extracting High Temprature co-efficients
a1 = str2double(a(1:d(1)+3));
a2 = str2double(a(d(1)+4:d(2)+3));
a3 = str2double(a(d(2)+4:d(3)+3));
a4 = str2double(a(d(3)+4:d(4)+3));
a5 = str2double(a(d(4)+4:d(5)+3));
a6 = str2double(b(1:e(1)+3));
a7 = str2double(b(e(1)+4:e(2)+3));
% Extracting Low Temprature co-efficients
b1 = str2double(b(e(2)+4:e(3)+3));
b2 = str2double(b(e(3)+4:e(4)+3));
b3 = str2double(b(e(4)+4:e(5)+3));
b4 = str2double(c(1:f(1)+3));
b5 = str2double(c(f(1)+4:f(2)+3));
b6 = str2double(c(f(2)+4:f(3)+3));
b7 = str2double(c(f(3)+4:f(4)+3));
% Universal Gas Constant
R = 8.3145;
% Specific heat (cp), Enthalpy (H), Entropy (S) function calling;
[cp, H, S] = functioncode_PNTD(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,T,local_mid);
% Calculating Molecular Weight;
mw= cmw_PNTD(species_name);
% Creating a Plots
disp(mw)
figure(1)
plot(T,cp,'color','r','linewidth',3);
% Axis Labelling
title(species_name)
xlabel('Temprature(K)');
ylabel('SpecificHeat(j/kg)');
figure(2)
plot(T,H,'color','b','linewidth',3);
title(species_name)
##% Axis Labelling
xlabel('Temprature(K)');
ylabel('Enthalpy(j/kg)');
figure(3)
plot(T,S,'color','g','linewidth',3);
title(species_name)
% Axis Labelling
xlabel('Temprature(K)');
ylabel('Entropy(j/k)');
% Saving jpg Data
mkdir(species_name)
cd(species_name)
saveas(1,'Temprature vs SpecificHeat.jpg'); 
saveas(2,'Temprature vs Enthalpy.jpg');
saveas(3,'Temprature vs Entropy.jpg');
 % saving fig Data
 % Change Current folder of data
cd ..



end

fclose(f1)

 EXPLANATION OF PROGRAM CODE :-

Function code of (cp,H,S) PNTD(Parsing NASA Thermodynamic Data):-

% Function Program of a Parsing NASA Thermodynamic Data
function [cp, H, S ]= functioncode_PNTD(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,T,local_mid)

for i=1:length(T)
if T(i)>local_mid
    cp(i) = R*((a1)+(a2*T(i))+(a3*T(i)^2)+(a4*T(i)^3)+(a5*T(i)^4));
    H(i) = ((a1)+(a2*T(i)/2)+(a3*T(i)^2/3)+(a4*T(i)^3/4)+(a5*T(i)^4/5)+(a6/T(i)))*(R*T(i));
    S(i) = R*((a1*log(T(i)))+(a2*T(i))+(a3*T(i)^2/2)+(a4*T(i)^3/3)+(a5*T(i)^4/4)+(a7));
else
     cp(i) = R*((b1)+(b2*T(i))+(b3*T(i)^2)+(b4*T(i)^3)+(b5*T(i)^4));
    H(i) = ((b1)+(b2*T(i)/2)+(b3*T(i)^2/3)+(b4*T(i)^3/4)+(b5*T(i)^4/5)+(b6/T(i)))*(R*T(i));
    S(i) = R*((b1*log(T(i)))+(b2*T(i))+(b3*T(i)^2/2)+(b4*T(i)^3/3)+(b5*T(i)^4/4)+(b7));
end
end

end
  • Parsing NASA Thermodynamic Data to given the formula to findout the specific heat (cp), Enthalpy(H), Entropy(S) to all the species in data file.
  • Formula entered in a for loop to temprature and if condition of temprature greater than the local_mid value. in diffrent co-efficients of a1,a2,a3,a4,a5,a6,a7 and else loop to used b1,b2,b3,b4,b5,b6,b7 co-efficients, then we end of the loops.
  • Function code name is "functioncode_PNTD(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,T,local_mid)" this function call and used to the main code.

Function Code of a Calculate Molecular weight of species :-

% Calculatiing molecular weight
% Input Datas 
function [mw]= cmw_PNTD(species_name)
atoms = ['H','O','C','N','S','A'];
atomic_weight = [1.007,15.999,12.011,14.006,32.059,39.792];

mw=0;
% Creating for loop and if condition
for i = 1:length(species_name)
    for j = 1:length(atoms)
     if strcmpi(species_name(i),atoms(j))
        mw = mw+atomic_weight(j);
        pos=j;
     end 
    n=str2num(species_name(i)); 
end 
    if n>1
      mw=mw+atomic_weight(pos)*(n-1)
    end
end
end
  • To find the molecular weight of the six elements ['H','O','C','N','S','A'] and their atomic weights [1.007,15.999,12.011,14.006,32.059,39.792].
  • we are considering initial molecular weight is (mw=0).
  • using for loop and string compare a command we comparing the species with respect string array.
  • when the comparasion becames positive program calculate the atomic weight to the old atomic weight of the current species.
  • To give if condition and create equation for find molecular weight of species "mw=mw+atomic_weight(pos)*(n-1)".
  • The function name is "cmw_PNTD" this function call in main program. 

MAIN code of PNTD(Parsing NASA Thermodynamic Data):-

% Main programe code of a Parsing NASA Thermodynamic Data
clear all
close all
clc
% Reading data File
f1 = fopen('THERMO.dat','r');
% Reading the Data File First Line
header = fgetl(f1);
% Global Temprature
l1 = fgetl(f1);
Temprature = strsplit(l1,' ');
global_min = str2double(Temprature{2});
global_mid = str2double(Temprature{3});
global_max = str2double(Temprature{4});

% Not considering the next three lines
for i = 1:3
    l2 = fgetl(f1);
end
% Reading next line

for i=1:53
l3 = fgetl(f1);
% Parsing the NASA Thermodynamic data
l3=strsplit(l3,' ')
species_name=l3{1}
% Calculating temprature
local_min = str2double(l3{end-3});
local_mid = str2double(l3{end-1});
local_max = str2double(l3{end-2});
T = linspace(local_min,local_max,1000);

% Co-efficients
a = fgetl(f1);
b = fgetl(f1);
c = fgetl(f1);
% Finding position it E
d = strfind(a,'E');
e = strfind(b,'E');
f = strfind(c,'E');
% Extracting High Temprature co-efficients
a1 = str2double(a(1:d(1)+3));
a2 = str2double(a(d(1)+4:d(2)+3));
a3 = str2double(a(d(2)+4:d(3)+3));
a4 = str2double(a(d(3)+4:d(4)+3));
a5 = str2double(a(d(4)+4:d(5)+3));
a6 = str2double(b(1:e(1)+3));
a7 = str2double(b(e(1)+4:e(2)+3));
% Extracting Low Temprature co-efficients
b1 = str2double(b(e(2)+4:e(3)+3));
b2 = str2double(b(e(3)+4:e(4)+3));
b3 = str2double(b(e(4)+4:e(5)+3));
b4 = str2double(c(1:f(1)+3));
b5 = str2double(c(f(1)+4:f(2)+3));
b6 = str2double(c(f(2)+4:f(3)+3));
b7 = str2double(c(f(3)+4:f(4)+3));
% Universal Gas Constant
R = 8.3145;
% Specific heat (cp), Enthalpy (H), Entropy (S) function calling;
[cp, H, S] = functioncode_PNTD(a1,a2,a3,a4,a5,a6,a7,b1,b2,b3,b4,b5,b6,b7,R,T,local_mid);
% Calculating Molecular Weight;
mw= cmw_PNTD(species_name);
% Creating a Plots
disp(mw)
figure(1)
plot(T,cp,'color','r','linewidth',3);
% Axis Labelling
title(species_name)
xlabel('Temprature(K)');
ylabel('SpecificHeat(j/kg)');
figure(2)
plot(T,H,'color','b','linewidth',3);
title(species_name)
##% Axis Labelling
xlabel('Temprature(K)');
ylabel('Enthalpy(j/kg)');
figure(3)
plot(T,S,'color','g','linewidth',3);
title(species_name)
% Axis Labelling
xlabel('Temprature(K)');
ylabel('Entropy(j/k)');
% Saving jpg Data
mkdir(species_name)
cd(species_name)
saveas(1,'Temprature vs SpecificHeat.jpg'); 
saveas(2,'Temprature vs Enthalpy.jpg');
saveas(3,'Temprature vs Entropy.jpg');
 % saving fig Data
 % Change Current folder of data
cd ..



end

fclose(f1)

  • Matlab better to start with clear all, close all, clc. Clear all - The statement is used clear all data that have stored in workspace, close all - The statement used is delete all figures and close plots, clc - the statement clear in all data of command window.
  • Using fopen command in the matlab we can read the given "THERMO.dat"file.
  • Using fgetl command we are reading and extracting the first line in the data file at storing in the variable called header.
  • Using strfind command we are finding the value which has decimal point on it.
  • Creating a new file to using mkdir command.
  • Then we creating loop for all 53 species to extract the coefficients.
  • Using strsplit command parsing NASA thermodynamic data, then calculating the tempratures local_min,local_mid,local_max using str2double command and temprature linspace array local minimum and maximum values, mid value mentiones 1000.
  • Using fgetl in coefficients and also using strfind to positioning the 'E'. Then we using str2double command extracting the higher level tempratures (a1,a2,a3,a4,a5,a6,a7) and lower level tempratures (b1,b2,b3,b4,b5,b6,b7) give universal gas constant value R = 8.3145.
  • Then we call the functions are already coded (cp,H,S) function_code and molecular weight of the six species functuion code in main program code.
  • Creating a plots in three diffrent relation  figures (1,2,3) and give labelling the axis,title (species_name). To create diffrent relation plots (specific heat vs Temprature),(Enthalpy vs Temprature),(Entropy vs Temprature).
  • To create jpg data and save using command mkdir (species_name),cd command using change in folder cd(species_name). The plots saved in each species on your working directory give command in (cd..).
  • Finally to end the loop function and using fclose(f1) command closing the open file.

OUTPUTS :-

PLOTS :-

'O'

Specificheat vs Temprature :-

                           

Enthalpy vs Temprature :-

                           

Entropy vs Temprature :-

                                    

 'O2' 

Specificheat vs Temprature :-

                                          

Enthalpy vs Temprature :-

                                                 

Entropy vs Temprature :-

                                               

'CO2'

Specificheat vs Temprature :-

                                                      

Enthalpy vs Temprature :-

                                                 

Entropy vs Temprature :-

                                                      

MOLECULAR WEIGHT OF SPECIES :-

Molecular weight of ('O','o2','cO2') :-

                                      

SCREEN SHOT WHERE THE SPECIES ARE SAVED :-

                     

RESULT :-

 Thus we have done to writte a code in MATLAB/Octave to parse the NASA Thermodynamic Data file and then calculate thermodynamic properties of various gas 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.