Challenge

Project 1 - Parsing NASA thermodynamic data

Objective:
To write a code in MATLAB for parsing the NASA Thermodynamic data file and using this extracted data to calculate specific data, enthalpy, entropy and molecular mass of each species.
What is file parsing?
File parsing is a technique that businesses use to extract essential information from unstructured data. It involves analyzing the content of files, such as text documents or images, to extract relevant data points and turn that into a usable format.

Formula used to calculate the thermodynamic properties are as follows:

T = Local Temperature

a = Temperature coeff.

Cp = Specific heat

H = Enthalpy

S = Entropy

R = Universal Gas Constant = 8.314 J⋅K−1⋅mol−1

Steps for choosing the logic of code and file parsing -

1. We open the file and parse the file ,fopen is used to open a file.

2. To navigate throughout the file and using (fgetl) command to get the line of the file and read the file line by line to get to the desired result.

3. Reading the text from the header file and read the file line by line

4. String split command then cell array is generated. A cell array is a special data structure to store data of the different type and perform the desired task

5. The function for Cp, H, S is created to calculate them by taking the values from the file data

6. Molecular weight is calculated using the molecular weight function and is printed on the command window

7. The plots for Cp, H, S are created for each species and saved in a folder in jpg format to access the species with different images

8. Creating folders for different molecules and save the plots for specific heat, enthalpy, and entropy in that folder and display the molecular weight of different species in the command window

Main matlab code:
clear all
close all
clc

%Input
R=8.314; % Gas Constant

%File reading
f=fopen('THERMO.dat','r');

%Reading header
for i=1:5
    fgetl(f);
end

for j=1:53


%extracting data from Species 
line_1=fgetl(f);
a = strsplit(line_1,' ');   %splitting line_1 into diff. string

%extracting species name using indexing
Species_name=a{1};

%extracting local temperature 
local_low_temp=a{6};
local_high_temp=a{7};
local_mid_temp=a{8};
%converting local temperature value from string to numerical or array
local_low_temp=str2num(local_low_temp);
local_high_temp=str2num(local_high_temp);
local_mid_temp=str2num(local_mid_temp);

local_low_temp=200;
local_high_temp=3500;
local_mid_temp=1000;





%extraction of coefficients from line 1 of coeff.
coeff_line_1=fgetl(f);

%reading position of E1 in coeff line 1
pos_E1=strfind(coeff_line_1,'E');

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

%extraction of coefficients from line 2
coeff_line_2=fgetl(f);

%reading position of E2 in coeff line 1
pos_E2=strfind(coeff_line_2,'E');

a6=str2num(coeff_line_2(1:pos_E2(1)+3));
a7=str2num(coeff_line_2(pos_E2(1)+4:pos_E2(2)+3));
a8=str2num(coeff_line_2(pos_E2(2)+4:pos_E2(3)+3));
a9=str2num(coeff_line_2(pos_E2(3)+4:pos_E2(4)+3));
a10=str2num(coeff_line_2(pos_E2(4)+4:pos_E2(5)+3));

%extraction of coefficients from line 3
coeff_line_3=fgetl(f);

%reading position of E2 in coeff line 3
pos_E3=strfind(coeff_line_3,'E');

a11=str2num(coeff_line_3(1:pos_E3(1)+3));
a12=str2num(coeff_line_3(pos_E3(1)+4:pos_E3(2)+3));
a13=str2num(coeff_line_3(pos_E3(2)+4:pos_E3(3)+3));
a14=str2num(coeff_line_3(pos_E3(3)+4:pos_E3(4)+3));


    %Take input from temp.
T=linspace(local_low_temp,local_high_temp,500);



%calculation of cp,H,S

for i=1:length(T)
    if(T(i)<=local_mid_temp)
       Cp(i) = R*(a8 + (a9*T(i)) + (a10*(T(i)).^2) + (a11*(T(i)).^3) + (a12*(T(i)).^4));
       H(i) = R*T(i).*(a8 + ((a9*T(i))/2) + ((a10*(T(i)).^2)/3) + ((a11*(T(i)).^3)/4) + ((a12*(T(i)).^4)/5) + ((a13./T(i))));
      S(i) = R*(a8.*log(T(i)) + (a9*T(i)) + ((a10*(T(i)).^2)/2) + ((a11*(T(i)).^3)/3) + ((a12*(T(i)).^4)/4) + a14);
    else(T(i)>local_mid_temp)
        Cp(i) = R*(a1 + (a2*T(i)) + (a3*(T(i)).^2) + (a4*(T(i)).^3) + (a5*(T(i)).^4));
       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))));
      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);
    end


end

% Function To Calculate Molecular Weight
MW = mol_wt(Species_name);

% To display as output
fprintf(' %d) Molecular Weight of [%s] is %d.',j,Species_name,MW)

% To set the next output after 2 line interval
fprintf('  ')

cd('D:\Matlab\File parser\Species_Results')
mkdir(Species_name)
cd(Species_name)

%plotting and saving of the result
figure(1)
plot (T,Cp)
  title('Specific heat vs temperature plot ')
              xlabel('Temperature')
              ylabel('Specific heat')
saveas(figure(1),'variation of Cp wrt temp.jpg')

figure(2)
plot (T,H)
title('Enthalpy vs temperature plot ')
              xlabel('Temperature')
              ylabel('Enthalpy')
saveas(figure(2),'variation of H wrt temp.jpg')

figure(3)
plot (T,S)
title('Entropy vs temperature plot ')
              xlabel('Temperature')
              ylabel('Entropy')
saveas(figure(3),'variation of S wrt temp.jpg')




end




Molecular weight function CODE:

% Function for callculation of Molecular Weight
function MW= mol_wt(Species_name)
atoms={'H','C','N','O','A'};
% H=Hydrogen(1), C=Carbon(12), N=Nitrogen(14), O=Oxygen(16), A=Argon(39)
atomic_weight=[1 12 14 16 39];

MW=0;
for i=1:length(Species_name)
    
for j=1:length(atomic_weight)
    
% for comparing atoms of the species with atoms
if (strcmp(Species_name(i),atoms(j))==1)
    
MW = MW + atomic_weight(j);

% to find the position of atoms in the species
rank=j;

end
end


% For the weights of the repeating atoms
m=str2num(Species_name(i));
if m>1
n=m-1;
MW=MW+atomic_weight(rank)*(n);
end
end
end

Molecular weight for some elements:

For H2O2

For O2

Screenshot attachments:
Folders:
O2 Plot:
NO2 Plot:
CO2 Plot:
Google drive link for files:
https://drive.google.com/file/d/10_kjxrlTllpUx1aX1A0hhCtVrFwEJnvm/view?usp=sharing
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.