Challenge

Project 1 - Parsing NASA thermodynamic data

Objectives –

  • To extract the 14 coefficients (High & Low Temperature) and calculate the Specific heat (Cp), Enthalpy (H) and Entropy (S) for all species (54 species) in the data file and also calculate the molecular weight of each species.
  • To plot Specific heat, Enthalpy and Entropy with range of Low local temperature to High local temperature and auto saving the plots in individual folder with suitable name

Parsing –

File parsing refers to how to read the information from data file and how to store that information in computer program. Sometimes it is very crucial to read the data from external files or data places to include it in calculation. This is done by partially analysing the data, understand its structure and then representing this data for further calculation by writing a code.

In this project, we are going to parse NASA's thermodynamic data and evaluate its thermodynamic properties such as Cp, H and S for all the species given in the data file. Following formula is used for computing all thermodynamic properties


Were,

R - Universal Gas Constant (kJ/kg K), T - Temperature (K), Cp - Specific Heat (kJ/kg K),

H - Enthalpy(kJ/kg), S - Entropy (kJ/K)

Function Code#1 - Specific Heat (Cp)

Specific Heat is the amount of heat required to change the heat content of 1 mole of material by exactly 1°C.

%Funtion for the Specific Heat

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

%The first 7 coefficients are 'HIGH-temperature coefficients' and the second 7 coefficients are 'LOW-temperature coefficients'
% Checking for the temperaure range with Mid Local Temperature to find the high or low temperature cofficients
if T <= T_mid
cp = R.*(a8 + a9.*T + a10.*(T.^2) + a11.*(T.^3) + a12.*(T.^4)); % Cp for Low Temperature cofficient
else
cp = R.*(a1 + a2.*T + a3.*(T.^2) + a4.*(T.^3) + a5.*(T.^4)); % Cp for High Temperature cofficient
end

Function Code#2 - Enthalpy (H)

Enthalpy is the measurement of energy in a thermodynamic system. The quantity of enthalpy equals to the total content of heat of a system, equivalent to the system’s internal energy plus the product of volume and pressure

%Funtion for the Enthalpy
function [H] = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,T_mid,R)
%The first 7 coefficients are 'HIGH-temperature coefficients' and the second 7 coefficients are 'LOW-temperature coefficients'
% Checking for the temperaure range with Mid Local Temperature to find the high or low temperature cofficients
if T <= T_mid
H = (R.*T).*(a8 + a9.*(T./2) + a10.*((T.^2)./3) + a11.*((T.^3)./4) + a12.*((T.^4)./5) + (a13./T)); % Cp for Low Temperature cofficient
else
H = (R.*T).*(a1 + a2.*(T./2) + a3.*((T.^2)./3) + a4.*((T.^3)./4) + a5.*((T.^4)./5) + (a6./T)); % Cp for High Temperature cofficient
end

Function Code#3 - Entropy (S)

Entropy is defined as a measure of randomness or disorder of a system

%Funtion for the Entropy
function [S] = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,T_mid,R)
%The first 7 coefficients are 'HIGH-temperature coefficients' and the second 7 coefficients are 'LOW-temperature coefficients'
% Checking for the temperaure range with Mid Local Temperature to find the high or low temperature cofficients
if T <= T_mid
S = R.*(a8.*log(T) + a9.*T + a10.*((T.^2)./2) + a11.*((T.^3)./3) + a12.*((T.^4)./4) + a14); % S for Low Temperature cofficient
else
S = R.*(a1.*log(T) + a2.*T + a3.*((T.^2)./2) + a4.*((T.^3)./3) + a5.*((T.^4)./4) + a7); % S for High Temperature cofficient
end

Function Code#4 - Molecular weight

Molecular weight is a measure of the sum of the atomic weight values of the atoms in a molecule.

%% Calculating molecular weight of species
function [M] = molecular_weight(Species_Name)
% following atomic molecules are present in the 54 species
% Hydrogen (H), Carbon (C), Nitrogen (N), Oxygen (O) and Argon (Ar)
atoms = ['H', 'C', 'N', 'O', 'Ar'];
% Input the atomic weight of the idetified molecules
atomic_weight = [1 12 14 16 40];
% intilializing the Molecular Range
M = 0;
for i = 1:length(Species_Name)
for j = 1:length(atoms) % nested if for acessing each atoms
if strcmp(Species_Name(i), atoms(j)) % Comparing the 2 string using 'strcmp'
M = M + atomic_weight(j);
position = j;
end
end
% Computing Molecular Weight for Species
n = str2num(Species_Name);
if n > 1
M = M + (atomic_weight(position).* (n-1));
end
end
% results
fprintf('Molecular Weight of %s : ', Species_Name);
fprintf('%f', M);
disp(' ');
end

Code Explanation -
  • First step is to assign the value for each element using an array.
  • Using 'strcmp' command the species name and array elements are compared. Then the atomic weight is added according to the compared string element value.
  • Finding the repetition of elements of the same species, the numerical value(n) is extracted using the str2 num and then the atomic weight of the previous element is added to 'n-1' times.

MATLAB Code for Parsing the Thermodynamic Data File -

%% Parsing of NASA Thermodynamics data
close all
clear all
clc

f1 = fopen("THERMO.dat","r") % Openning the file for parsing with Read access
fgetl(f1) % get the comment line

R = 8.314 % Universal gas constant value in (kJ/kg K)

% getting the 2nd line having global temperature values for which thrermodynamic properties are valid
Temp_global = fgetl(f1); % getting the 2nd line data as a global temperature
Temp_global = strsplit(Temp_global,' '); % split the line using space in the string

Temp_global1 = str2num(Temp_global{2}); % getting the global temperature value using string position and convert string to number
Temp_global2 = str2num(Temp_global{3}); % getting the global temperature value using string position and convert string to number
Temp_global3 = str2num(Temp_global{4}); % getting the global temperature value using string position and convert string to number

% calling the next 3 comment line
fgetl(f1)
fgetl(f1)
fgetl(f1)

% We have a 53 species in the data file, having 4 lines of data for each individual species
% 53 species having 4 set of unique lines
Total_no_of_species = 53

for i = 1 : 53; % calling the each species lines using for loop
Species_line1 = fgetl(f1); % calling the 1st species line which contain name and Local Temperature
Species_line2 = strsplit(Species_line1,' ') % split the line using space in the string
Species_Name = num2str(Species_line2{1}); % getting the first cell data as a Species name

t = strfind(Species_line1,'.'); % finding the string in the Local temperature using '.'
t_low = Species_line1(t(1)-3:t(1)+3); % getting the Low Local temperature value using string position
T_low = str2num(t_low); % Changing the string into number using str2num command

t_high = Species_line1(t(2)-4:t(2)+3); % getting the High Local temperature value using string position
T_high = str2num(t_high); % Changing the string into number using str2num command

t_mid = Species_line1(t(3)-4:t(3)+3); % getting the Middle Local temperature value using string position
T_mid = str2num(t_mid); % Changing the string into number using str2num command

T = linspace(T_low,T_high,100); % Input the local temperature from low to high using 'linspace' command

% First line of coefficient
Coefficet_Line1 = fgetl(f1); % getting 1st coefficient line of each species
a = findstr(Coefficet_Line1,'E'); % finding the string in the cofficient using 'E'

a1 = str2num(Coefficet_Line1(1:a(1)+3)); % getting the 1st coefficient in 1st line using string position and covert the string to number
a2 = str2num(Coefficet_Line1(a(1)+4:a(2)+3)); % getting the 2nd coefficient in 1st line using string position and covert the string to number
a3 = str2num(Coefficet_Line1(a(2)+4:a(3)+3)); % getting the 3rd coefficient in 1st line using string position and covert the string to number
a4 = str2num(Coefficet_Line1(a(3)+4:a(4)+3)); % getting the 4th coefficient in 1st line using string position and covert the string to number
a5 = str2num(Coefficet_Line1(a(4)+4:a(5)+3)); % getting the 5th coefficient in 1st line using string position and covert the string to number

% Second line of coefficient
Coefficet_Line2 = fgetl(f1); % getting 2nd coefficient line of each species
b = findstr(Coefficet_Line2,'E'); % finding the string in the cofficient using 'E'

a6 = str2num(Coefficet_Line2(1:b(1)+3)); % getting the 1st coefficient in 2nd line using string position and covert the string to number
a7 = str2num(Coefficet_Line2(b(1)+4:b(2)+3)); % getting the 2nd coefficient in 2nd line using string position and covert the string to number
a8 = str2num(Coefficet_Line2(b(2)+4:b(3)+3)); % getting the 3rd coefficient in 2nd line using string position and covert the string to number
a9 = str2num(Coefficet_Line2(b(3)+4:b(4)+3)); % getting the 4th coefficient in 2nd line using string position and covert the string to number
a10 = str2num(Coefficet_Line2(b(4)+4:b(5)+3)); % getting the 5th coefficient in 2nd line using string position and covert the string to number

% Third line of coefficient
Coefficet_Line3 = fgetl(f1); % getting 3rd coefficient line of each species
c = findstr(Coefficet_Line3,'E'); % finding the string in the cofficient using 'E'

a11 = str2num(Coefficet_Line3(1:c(1)+3)); % getting the 1st coefficient in 3rd line using string position and covert the string to number
a12 = str2num(Coefficet_Line3(c(1)+4:c(2)+3)); % getting the 2nd coefficient in 3rd line using string position and covert the string to number
a13 = str2num(Coefficet_Line3(c(2)+4:c(3)+3)); % getting the 3rd coefficient in 3rd line using string position and covert the string to number
a14 = str2num(Coefficet_Line3(c(3)+4:c(4)+3)); % getting the 4th coefficient in 3rd line using string position and covert the string to number

% Calling the Specific Heat function for computing cp
Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,T_mid,R)

% Calling the Enthalpy function for computing H
H = enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,T_mid,R)

% Calling the Entropy function for computing S
S = entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,T_mid,R)

% Calling the Molecular weight function for computing Molcule weight of species
mol_weight = molecular_weight(Species_Name);
disp(mol_weight) % dispaly the value in command window

% Creating foler for each species in a NASA's Thermodynamic Data
cur_dir = pwd; % getting path of current working directory using 'pwd'
mkdir(Species_Name); % making directory with individual species name using 'mkdir'
cd(Species_Name); % change directory for saving the files

% Plotting Specific Heat, Enthalphy and Entrophy with Local Temperature Range from Low to High
% Plot Specific Heat (Cp) vs Temperature (T)
figure(1)
plot(T, Cp,'Linestyle','--', 'LineWidth',2,'Color','m');
xlabel('Temperature (K)')
ylabel('Specific Heat (kJ/kg K)');
title(sprintf('Specific Heat vs Temperature range of %s', Species_Name));
saveas(figure(1), 'Specific Heat.jpg');
grid on

% Plot Enthalphy (H) vs Temperature (T)
figure(2)
plot(T, H, 'Linestyle',':','LineWidth',2,'Color','g');
xlabel('Temperature (K)')
ylabel('Enthalphy (kJ/kg)');
title(sprintf('Enthalphy vs Temperature range of %s', Species_Name));
saveas(figure(2), 'Enthalphy.jpg');
grid on

% Plot Entrophy (S) vs Temperature (T)
figure(3)
plot(T, S, 'Linestyle','-','LineWidth',2,'Color','y');
xlabel('Temperature (K)')
ylabel('Entrophy (kJ/K)');
title(sprintf('Entrophy vs Temperature range of %s', Species_Name));
saveas(figure(3), 'Entrophy.jpg');
grid on

cd(cur_dir); % after saving the files agin changing current working file directory

end

Code Explanation -

  • fopen command to open a file in read the datas
  • fgetl(f1) - to read file line by line
  • Input R = 8.314 % R is the universal gas constant in kJ/kg K
  • fgetl(f1) - to get 2nd line data as a global temperature (3 values) and using ‘strsplit’ command we got string ‘.’ Position in the line
  • Using that string position value global temperature values collected and these strings are converted to numbers using ‘str2num’ command
  • fgetl(f1) - to read the comments line (next 3 lines)
  • We have a 53 species in the data file, having 4 lines of data for each individual species. Considering pattern of all the 4 lines of each species, we can use for loop to reiterate same logic for all species in the data
  • For loop is used to call each species lines and calling the 1st species line using ‘fgetl’ which contain name and Local Temperature of the species and this line is splitted using ‘strspilt’ command
  • Based on the split data first cell data assigned as a ‘Species Name’
  • Getting the Local temperature value of individual species, we used ‘strfind’ command and ‘.’ String position collected
  • Using that string position value local temperature values (Low, High, Mid) collected and these strings are converted to numbers using ‘str2num’ command
  • Input the local temperature from low to high using 'linspace' command for calculating the thermodynamic properties
  • To read the 1st line of coefficients we use ‘fgetl’ command and using ‘strfind’ position of ‘Exponential’ collected
  • Based on string ‘E’ position, coefficients in the line is collected and converted to numbers using ‘str2num’. same steps repeated to collect remaining 2 lines of coefficients of particular species (Total 14 coefficients)
  • Calling Specific Heat function ’specific_heat’ to calculate the Cp
  • Calling Enthalpy function ’enthalpy’ to calculate the H
  • Calling Entropy function ’entropy’ to calculate the S
  • Calling Molecular weight function ’molecular_weight’ to calculate the mol_weight
  • To Create foler for each species in a NASA's Thermodynamic Data, used ‘pwd’ to get the current path of working directory
  • Using 'mkdir' directory with individual species name created
  • In order to save the plot of each species directory has been changed temporarily
  • Using the ‘Plot’ command relation between Cp Vs T, H Vs T, S vs T plotted with appropriate labels, titles
  • Above plots are saved in new directory as ‘.jpg’ file
  • After saving the plots in new directory created with individual species names has been changed to current filr working directory using ‘cd’ command

Output#1 - Screen shot of species directory


Output plot -

Plots of O2

Plots of N2


Plots of CO2


Output #3 - Molecular weight results in command window


Molecular Weight of all the species -

Molecular Weight of O : 16.000000

Molecular Weight of O2 : 16.000000

Molecular Weight of H : 1.000000

Molecular Weight of H2 : 1.000000

Molecular Weight of OH : 17.000000

Molecular Weight of H2O : 17.000000

Molecular Weight of HO2 : 17.000000

Molecular Weight of H2O2 : 17.000000

Molecular Weight of C : 12.000000

Molecular Weight of CH : 13.000000

Molecular Weight of CH2 : 13.000000

Molecular Weight of CH2(S) : 13.000000

Molecular Weight of CH3 : 13.000000

Molecular Weight of CH4 : 13.000000

Molecular Weight of CO : 28.000000

Molecular Weight of CO2 : 28.000000

Molecular Weight of HCO : 29.000000

Molecular Weight of CH2O : 29.000000

Molecular Weight of CH2OH : 30.000000

Molecular Weight of CH3O : 29.000000

Molecular Weight of CH3OH : 30.000000

Molecular Weight of C2H : 13.000000

Molecular Weight of C2H2 : 13.000000

Molecular Weight of C2H3 : 13.000000

Molecular Weight of C2H4 : 13.000000

Molecular Weight of C2H5 : 13.000000

Molecular Weight of C2H6 : 13.000000

Molecular Weight of CH2CO : 41.000000

Molecular Weight of HCCO : 41.000000

Molecular Weight of HCCOH : 42.000000

Molecular Weight of H2CN : 27.000000

Molecular Weight of HCN : 27.000000

Molecular Weight of HNO : 31.000000

Molecular Weight of N : 14.000000

Molecular Weight of NNH : 29.000000

Molecular Weight of N2O : 30.000000

Molecular Weight of NH : 15.000000

Molecular Weight of NH2 : 15.000000

Molecular Weight of NH3 : 15.000000

Molecular Weight of NO : 30.000000

Molecular Weight of NO2 : 30.000000

Molecular Weight of HCNO : 43.000000

Molecular Weight of HOCN : 43.000000

Molecular Weight of HNCO : 43.000000

Molecular Weight of NCO : 42.000000

Molecular Weight of CN : 26.000000

Molecular Weight of HCNN : 41.000000

Molecular Weight of N2 : 14.000000

Molecular Weight of AR : 40.000000

Molecular Weight of C3H8 : 13.000000

Molecular Weight of C3H7 : 13.000000

Molecular Weight of CH3CHO : 42.000000

Molecular Weight of CH2CHO : 42.000000

Error #1 - Facing error in collecting local temperature due to number of strings in each lines of individual species changes so changed to 'strfind' instead of 'strsplit'

Error #2 - Facing error ' Incompatible size' error while calculating thermodynamic properties , I found value of coefficient not changed to number. Using 'str2num' command string was changed to number

* Screenshot not able to share due to error while uploading

Summary of Project -

In This Project, we collected the high and low temperature coefficients with respect to local temperature ranges of each individual species. The specific heat, enthalpy and entropy of all species are calculated concerning their given formula, plots are plotted between properties and temperature and the molecular weights are calculated and displayed in the command window.

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.