Challenge

Parsing NASA thermodynamic data

Aim:

1.To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file. Calculate the molecular weight of each species and display it in the command window.

2.Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.

Given:

  • Here R is the universal gas constant, T is the temperature, Cp is the specific heat, H is the enthalpy and s is the entropy

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

  • Nasa Thermodynamic data

File Parsing:

Parsing, syntax analysis, or syntactic analysis of the input data normally from a file into its components parts to facilitate compilers and interpreters' writing. It can be used to describe split or separation.

Enthalpy:

Enthalpy is a property of a thermodynamic system and is defined as the sum of the system's internal energy and the product of its pressure and volume.

Entropy:

Entropy is a thermodynamic quantity representing the unavailability of a system's thermal energy for conversion into mechanical work, often interpreted as the degree of disorder or randomness in the system.

Specific heat:

Specific heat, the quantity of heat required to raise the temperature of one gram of a substance by one Celsius degree. The units of specific heat are usually calories or joules per gram per Celsius degree

Molecular weight:

Molecular weight, also called molecular mass, the mass of a molecule of a substance, based on 12 as the atomic weight of carbon-12. It is calculated by summing the atomic weights of the atoms making up the substance’s molecular formula.

Nasa Thermodynamic data consists of 53 elements :

O O2 H H2 OH
H20 HO2 CH2 C CH
CH2 CH2S CH3 CH4 CO
CO2 HCO CH2O CH2OH CH3O
CH3OH C2H C2H2 C2H3 C2H4
C2H5 C2H6 CH2CO HCOO HCCOH
H2CN HCN HNO N NNH
N20 NH NH2 NH3 NO
NO2 HCNO HOCN HNCO NCO
CN HCNN N2 AR C3H8
C3H7 CH3CHO CH2CHO

Functions used:

  • close all, clear all, and clc functions used to clear workspace, command window and close all closes all the plots running behind.
  • fopen opens the NASA thermodynamic data "THERMO" and reads it.
  • fgetl functions get the specific line from thermo data after reading.
  • By continuously using fgetl we are skipping the lines to get the required line.
  • strsplit function splits the line into a separate string.
  • str2num functions convert strings into a number.
  • linspace command used to generate evenly spaced vectors.
  • pwd command tells us the current folder.
  • mkdir command creates a new folder.
  • cd changes folder
  • plot command used to create a plot.
  • xlabel used to label the x-axis.
  • ylabel used to label the y-axis.
  • title gives the title of the plot.
  • saveas is used to save the current figure.

Main program:

clear all
close all
clc


% Opening THERMO file in Matlab
f1=fopen('THERMO.dat','r');
%Extracting global temeperature from data
get_0=fgetl(f1);%skiping lines 
get_1=fgetl(f1);

%Splitting and Converting to Numbers from string
temp=strsplit(get_1, ' ');

global_temp_min=str2num(temp{2});
global_temp_mid=str2num(temp{3});
global_temp_max=str2num(temp{4});



%skiping lines 
get_2=fgetl(f1);
get_3=fgetl(f1);
get_4=fgetl(f1);

%Universal gas constant
R=8.314;

for i=1:53
%Species Block
%getting Species name
get_5=fgetl(f1);
line_1=strsplit(get_5);
split_line_1=length(line_1);
species_name=line_1{1};
%getting local minimum and maximum temperatures
local_min_temperature=str2num(line_1{split_line_1-3});
local_max_temperature=str2num(line_1{split_line_1-2});
local_mid_temperature=str2num(line_1{split_line_1-1});

% Finding 14 Coefficients
get_7=fgetl(f1);

line_2=strfind(get_7,'E');
a1=get_7(1:line_2(1)+3);
a1=str2num(a1);
a2=get_7(line_2(1)+4:line_2(2)+3);
a2=str2num(a2);
a3=get_7(line_2(2)+4:line_2(3)+3);
a3=str2num(a3);
a4=get_7(line_2(3)+4:line_2(4)+3);
a4=str2num(a4);
a5=get_7(line_2(4)+4:line_2(5)+3);
a5=str2num(a5);

get_8=fgetl(f1);

line_3=strfind(get_8,'E');
a6=get_8(1:line_3(1)+3);
a6=str2num(a6);
a7=get_8(line_3(1)+4:line_3(2)+3);
a7=str2num(a7);
a8=get_8(line_3(2)+4:line_3(3)+3);
a8=str2num(a8);
a9=get_8(line_3(3)+4:line_3(4)+3);
a9=str2num(a9);
a10=get_8(line_3(4)+4:line_3(5)+3);
a10=str2num(a10);


get_9=fgetl(f1);

line_4=strfind(get_9,'E');
a11=get_9(1:line_4(1)+3);
a11=str2num(a11);
a12=get_9(line_4(1)+4:line_4(2)+3);
a12=str2num(a12);
a13=get_9(line_4(2)+4:line_4(3)+3);
a13=str2num(a13);
a14=get_9(line_4(3)+4:line_4(4)+3);
a14=str2num(a14);

T=linspace(local_min_temperature,local_max_temperature,200);
cp=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,local_mid_temperature);
h=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,local_mid_temperature);
s=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,local_mid_temperature);

current_folder=pwd;
mkdir(species_name)
cd(species_name)

 %plotting
 
figure(1)
plot(T,cp,'linewidth',5,'color','r')
xlabel('temperature')
ylabel('Specific heat')
title(species_name)
grid on
saveas(figure(1),'Specific heat.jpg')

figure(2)
plot(T,h,'linewidth',6,'color','b')
xlabel('temperature')
ylabel('Enthaply')
title(species_name)
grid on
saveas(figure(2),'Enthaply.jpg')

figure(3)
plot(T,s,'linewidth',4,'color','c')
xlabel('temperature')
ylabel('entropy')
title(species_name)
grid on
saveas(figure(3),'entropy.jpg')



cd(current_folder)
mw=molecular_mass(species_name);
fprintf('Molecular mass of %s :',species_name);
fprintf('%f',mw);

end

Program for Entropy calculation:

function[s]=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,local_mid_temperature)
for i=1:length(T)
    if T(i)>local_mid_temperature
%High temperature coefficients
s(i)=(a1*log(T(i))+a2*T(i)+a3*(T(i)^2/2)+a4*(T(i)^3/3)+a5*(T(i)^4/4)+a7)*R;
    else
%low temperature coefficients
s(i)=(a8*log(T(i))+a9*T(i)+a10*(T(i)^2/2)+a11*(T(i)^3/3)+a12*(T(i)^4/4)+a14)*R;
    end
end
end

Program for Enthalpy calculation:

function[h]=enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,local_mid_temperature)

for i=1:length(T)
    if T(i)>local_mid_temperature
%High temperature coefficients
h(i)=R*T(i)*(a1+a2*(T(i)/2)+a3*(T(i)^2/3)+a4*(T(i)^3/4)+a5*(T(i)^4/5)+a6*T(i));
    else
%low temperature coefficients
h(i)=(a8+a9*(T(i)/2)+a10*(T(i)^2/3)+a11*(T(i)^3/4)+a12*(T(i)^4/5)+a13*T(i))*R*T(i);
    end
end
end

Program for Specific heat calculation:

function[cp]=specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,local_mid_temperature)

for i=1:length(T)
    
if T(i)>local_mid_temperature
%High Temperature coefficients
cp(i)=(a1+a2*T(i)+a3*T(i)^2+a4*T(i)^3+a5*T(i)^4)*R;
else
%Low temperature coefficients
cp(i)=(a8+a9*T(i)+a10*T(i)^2+a11*T(i)^3+a12*T(i)^4)*R;

end
end
end

Program for molecular weight calculation:

function [mw]=molecular_mass(species_name)
% Definition atomic name of the species in thermo file indiviually
atomic_name=['C', 'H' ,'O', 'N', 'A'];
% Defining atomic wieghts of the species
atomic_weight=[12.0107 1.00794 15.9994 14.00670 39.9480];

u=0;
mw=0; %considering molecular mass is zero for first iteration
for i=1:length(species_name)
    for j=1:length(atomic_name)
        if (strcmpi(species_name(i),atomic_name(j)))
            mw=mw+atomic_weight(j);
            u=j;
        end
    end
  
    n=str2num(species_name(i));
    if (n > 1 )  
        mw=mw+(atomic_weight(u)*(n-1));
    end
end


Molecular weight of the species:

Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of O :15.999400Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of O2 :31.998800Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of H :1.007940Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of H2 :2.015880Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of OH :17.007340Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of H2O :18.015280Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HO2 :33.006740Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of H2O2 :34.014680Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C :12.010700Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH :13.018640Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH2 :14.026580Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH2(S) :14.026580Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH3 :15.034520Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH4 :16.042460Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CO :28.010100Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CO2 :44.009500Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HCO :29.018040Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH2O :30.025980Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH2OH :31.033920Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH3O :31.033920Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH3OH :32.041860Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C2H :25.029340Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C2H2 :26.037280Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C2H3 :27.045220Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C2H4 :28.053160Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C2H5 :29.061100Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C2H6 :30.069040Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH2CO :42.036680Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HCCO :41.028740Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HCCOH :42.036680Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of H2CN :28.033280Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HCN :27.025340Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HNO :31.014040Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of N :14.006700Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of NNH :29.021340Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of N2O :44.012800Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of NH :15.014640Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of NH2 :16.022580Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of NH3 :17.030520Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of NO :30.006100Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of NO2 :46.005500Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HCNO :43.024740Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HOCN :43.024740Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HNCO :43.024740Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of NCO :42.016800Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CN :26.017400Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of HCNN :41.032040Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of N2 :28.013400Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of AR :39.948000Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C3H8 :44.095620Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of C3H7 :43.087680Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH3CHO :44.052560Warning: Directory already exists. 
> In Nasa (line 89) 
Molecular mass of CH2CHO :43.044620>> 

plots:

Screenshot of all 53 species:

Molecular weight screenshot:

Conclusion:

We have understood and completed File parsing using commands like fopen, fgetl,str2num,strsplit etc.

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.