Project 1 - Parsing NASA thermodynamic data
Aim: To write a function that extracts the 14 coeff for higher 7 and lower 7 values of NASA thermodynamic data to calculate enthalpy, entropy, specific heat, the molecular weight of the data. 'THERMO.dat' is given in the project as a file.
Objective:
The NASA thermodynamic function is shown below:

where
R is the universal gas constant;
T is temperature;
S is Entropy;
H is Enthalpy;
Cp is Specific Heat;
To calculate each species' molecular weight by generating the function for each R, S, H, Cp.
Parsing the file:
1. Define the function
2. Create an Input parser object
3. Add inputs to the scheme
4. Set properties
5. Parse the inputs
6. Use the inputs
7. Calling the function
Briefly explanation of NASA thermodynamic data:
'THERMO.dat' file is the content of 14 coeff of 53 elements provided by NASA.
Enthalpy coding:
Enthalpy is a thermodynamic property of a system. It is the sum of the internal energy added to the product of the pressure and volume of the system.
%Function for Enthalpy
function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_mid)
%calculating Enthalpy
if T <= global_mid
H = R*T.*(a8+((a9*T)/2)+((a10*T.^2)/3)+((a11*T.^3)/4)+((a12*T.^4)/5)+(a13./T));
else
H = R*T.*(a1+((a2*T)/2)+((a3*T.^2)/3)+((a4*T.^3)/4)+((a5*T.^4)/5)+(a6./T));
end
calling the function as H where as two function are made one is for lower and other for higher coeffcients.
"if & else " is used to choose either lower or higher coeffcient calling.
Entropy coding:
The measure of a system's thermal energy per unit temperature. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.
%Function for Entropy
function S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_med)
%calculating entropy
if T<=global_med
S = R.*(a8.*log(T)+(a9*T)+((a10*(T).^2)/2)+((a11*(T).^3)/3)+((a12*(T).^4)/4)+a14);
else
S = R.*(a1.*log(T)+(a2*T)+((a3*(T).^2)/2)+((a4*(T).^3)/3)+((a5*(T).^4)/4)+a7);
end
calling the function as S, whereas two functions are made one is for lower and other for higher coefficient.
"if & else " is used to choose either lower or higher coefficient calling.
Specific heat (Cp):
The specific heat capacity is defined as 'the quantity of heat (J) absorbed per unit mass (kg) of the material when its temperature increases', and its units are KJ/(kg K).
%function for specific heat
function Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_temp)
%calculating the higher and lower temp of specific heat
if T <= global_temp
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
calling the function as Cp, whereas two functions are made one is for lower and other for higher coefficient.
"if & else " is used to choose either lower or higher coefficient calling.
Function of Molecular weights:
Molecular weight, also called molecular mass, a mass of a molecule of a substance, is based on 12 as the atomic weight of carbon-12..
%Function of molecular Weight
function molecular_weight = molecular_weight(Species_SP)
%Atomic naming ( Hydrogen, Carbon, Oxygen, Nitrogen,Argon)
atomic_name=['H','C','O','N','Ar'];
%masses of the species
atomic_weight=[1.008 12.011 15.999 14.007 39.948];
fprintf('molecular Weight of:');
fprintf(Species_SP);
fprintf(' : ');
%starting value of the atomic value
molecular_weight = 0;
%loop for length of species
for i = 1:length(Species_SP)
%loop for lenght of atomic name
for j = 1:length(atomic_name)
%function to compare the species and atomic name
if strcmpi(Species_SP(i),atomic_name(j))
molecular = molecular_weight + atomic_weight(j);
a=j;
end
end
n = str2num(Species_SP);
if n>1
molecular = molecular_weight + atomic_weight(a)*(n-1);
end
end
fprintf('molecular weight %s :',Species_SP);
fprintf('%d',molecular);
disp(' ')
end
SYNTAX was used in coding the function:
- fopen: To open the file to get the information
- r: It's used to read the function inside the code
- fgetl: used to read the line inside the file
- str2num: converting the string to a number.
- strsplit: it is used to split-up the delimiter
- strfind: used to find string
- linspace: Used to generate linear space vectors
- fprintf: it is used to print the data which is written as per the formula.
- mkdir: makes a new directory
- cd: Used to change the folders of the working directory.
Main Code:
% programming for NASA thermodynamic data
clear all
close all
clc
%making directory
mkdir ('D:skill-lync docs/species')
%Reading the NASA thermodynamic data
%open a file & to read the file
f1 = fopen('THERMO.dat','r');
get = fgetl(f1);
%Header information
global_temp=fgetl(f1);
%splittting the data
t = strsplit(global_temp);
%converting the string cells to number using the str2num conmmands
global_low = str2num(t{2});
global_mid = str2num(t{3});
global_high = str2num(t{4});
% Inuts
R = 8.314;
T=linspace(global_low,global_high,1000);
%skipping the first three comments lines
for i =1:3
get=fgetl(f1);
end
for j=1:53
get = fgetl(f1);
A = strsplit(get,' ');
Species_SP=(A{1});
length1=fgetl(f1);
a=strfind(length1,'E');
% Getting the higher and lower coeffcients
% Extracting first 7 the higher coeffcients
a1 = length1(1:a(1)+3);
a1 = str2num(a1);
a2 = length1(a(1)+4:a(2)+3);
a2 = str2num(a2);
a3 = length1(a(2)+4:a(3)+3);
a3 = str2num(a3);
a4 = length1(a(3)+4:a(4)+3);
a4 = str2num(a4);
a5 = length1(a(4)+5:a(5)+3);
a5 = str2num(a5);
length2=fgetl(f1);
b=strfind(length1,'E');
a6 = length2(1:b(1)+3);
a6= str2num(a6);
a7 = length2(a(1)+4:b(2)+3);
a7 = str2num(a7);
%Extracting second 7 the low coeffcients
a8 = length2(b(2)+4:b(3)+3);
a8 = str2num(a8);
a9 = length2(b(3)+4:b(4)+3);
a9 = str2num(a9);
a10 = length2(b(4)+3:b(5)+3);
a10 = str2num(a10);
length3=fgetl(f1);
c = strfind(length1,'E');
a11 = length3(1:c(1)+3);
a11 = str2num(a11);
a12 = length3(c(1)+4:c(2)+3);
a12 = str2num(a12);
a13 = length3(c(2)+4:c(3)+3);
a13 = str2num(a13);
a14 = length3(c(3)+4:c(4)+3);
a14 = str2num(a14);
%To calculate the Specific heat Cp
Cp = specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,T,R,global_mid);
%To calculate the Enthalpy H
H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_mid);
% To calculate Entropy S
S = Entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_mid);
%To calculating molecular weight
molecular = molecular_weight(Species_SP);
%read and write the molcular weight
Molecular_lists = fopen('Molecular weight.txt','w');
fprintf(Molecular_lists,'Molecular weight of %s is %d ',Species_SP,molecular);
fclose(Molecular_lists);
%plotting the values
mkdir(Species_SP);
cd(Species_SP);
%plotting 1 Cp vs T
figure(1)
plot(T,Cp,'linewidth',3,'Color','r');
xlabel('Temperature(K)');
ylabel('Specific Heat(KJ)');
title(sprintf('Specific Heat vs Temperature of %s',Species_SP));
grid on
saveas(figure(1),'Specific_heat.jpg');
%plotting 2 H vs T
figure(2)
plot(T,H,'linewidth',3,'color','g')
xlabel('Temperature(K)');
ylabel('Enthalpy[KJ/mol-k)');
title(sprintf('Enthalpy vs Temperature of %s',Species_SP));
grid on
saveas(figure(2),'Enthalpy.jpg');
%plotting 3 S vs T
figure(3)
plot(T,S,'linewidth',3,'Color','b');
xlabel('Temperature[K]');
ylabel('Entropy(KJ/mol-K)');
title(sprintf('Entropy vs Temperature of %s',Species_SP));
grid on
saveas(figure(3),'Entropy.jpg');
%Molecular weight
molecular = molecular_weight(Species_SP);
f2 = fopen('molecular mass','w');
fprintf('%s n %s n %f n','species','molecular mass',molecular)
fclose(f2);
end
Code Explanation:
clear all : clear all the variable in the workspace
close all: closes all the plots and figures which is presented before.
clc: clc the command window.
mkdir: is used to create a directory with the location address
To compute the program THERMO.dat file is used to run the program for that "open", "r" is used to open the file and to read it.
Using the 'fgetl' code we can read the file.
To split the temperature into a particular string cell "strsplit" command is used.
Then to convert the string to a number 'str2num' command is used.
To extract the file and to skip the first three lines we use for, the end function.
Reading all 53 species which is given in the file such as Entropy, Enthalpy, Specific Heat, Molecular Weight.
Using the 'For' loop function to read all species.
'strfind' is used to find the string.
Inputs are given such as R, T is called by 'linspace' commands.
Then extract the data as Higher and Lower coeff in terms of a1 to a7 for higher and a8 to a14 for lower.
Inputting the function calling to compute and run the program.
OUTPUT:
species n molecular mass n 0.0 nmolecular Weight of:O2 : molecular weight O2 :1.599900e+01
molecular Weight of:O2 : molecular weight O2 :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:H : molecular weight H :1.008000e+00
molecular Weight of:H : molecular weight H :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:H2 : molecular weight H2 :1.008000e+00
molecular Weight of:H2 : molecular weight H2 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:OH : molecular weight OH :1.008000e+00
molecular Weight of:OH : molecular weight OH :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:H2O : molecular weight H2O :1.599900e+01
molecular Weight of:H2O : molecular weight H2O :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:HO2 : molecular weight HO2 :1.599900e+01
molecular Weight of:HO2 : molecular weight HO2 :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:H2O2 : molecular weight H2O2 :1.599900e+01
molecular Weight of:H2O2 : molecular weight H2O2 :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:C : molecular weight C :1.201100e+01
molecular Weight of:C : molecular weight C :1.201100e+01
species n molecular mass n 0.0 nmolecular Weight of:CH : molecular weight CH :1.008000e+00
molecular Weight of:CH : molecular weight CH :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:CH2 : molecular weight CH2 :1.008000e+00
molecular Weight of:CH2 : molecular weight CH2 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:CH2(S) : molecular weight CH2(S) :1.008000e+00
molecular Weight of:CH2(S) : molecular weight CH2(S) :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:CH3 : molecular weight CH3 :1.008000e+00
molecular Weight of:CH3 : molecular weight CH3 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:CH4 : molecular weight CH4 :1.008000e+00
molecular Weight of:CH4 : molecular weight CH4 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:CO : molecular weight CO :1.599900e+01
molecular Weight of:CO : molecular weight CO :1.599900e+01
species n molecular mass n 0.0 molecular Weight of: CO2: molecular weight CO2:1.599900e+01
molecular Weight of:CO2 : molecular weight CO2 :1.599900e+01
species n molecular mass n 0.0 molecular Weight of HCO : molecular weight HCO:1.599900e+01
molecular Weight of:HCO : molecular weight HCO :1.599900e+01
species n molecular mass n 0.0 molecular Weight of: CH2O : molecular weight CH2O :1.599900e+01
molecular Weight of:CH2O : molecular weight CH2O :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:CH2OH : molecular weight CH2OH :1.008000e+00
molecular Weight of:CH2OH : molecular weight CH2OH :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:CH3O : molecular weight CH3O :1.599900e+01
molecular Weight of:CH3O : molecular weight CH3O :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:CH3OH : molecular weight CH3OH :1.008000e+00
molecular Weight of:CH3OH : molecular weight CH3OH :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:C2H : molecular weight C2H :1.008000e+00
molecular Weight of:C2H : molecular weight C2H :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:C2H2 : molecular weight C2H2 :1.008000e+00
molecular Weight of:C2H2 : molecular weight C2H2 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:C2H3 : molecular weight C2H3 :1.008000e+00
molecular Weight of:C2H3 : molecular weight C2H3 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:C2H4 : molecular weight C2H4 :1.008000e+00
molecular Weight of:C2H4 : molecular weight C2H4 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:C2H5 : molecular weight C2H5 :1.008000e+00
molecular Weight of:C2H5 : molecular weight C2H5 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:C2H6 : molecular weight C2H6 :1.008000e+00
molecular Weight of:C2H6 : molecular weight C2H6 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:CH2CO : molecular weight CH2CO :1.599900e+01
molecular Weight of:CH2CO : molecular weight CH2CO :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:HCCO : molecular weight HCCO :1.599900e+01
molecular Weight of:HCCO : molecular weight HCCO :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:HCCOH : molecular weight HCCOH :1.008000e+00
molecular Weight of:HCCOH : molecular weight HCCOH :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:H2CN : molecular weight H2CN :1.400700e+01
molecular Weight of:H2CN : molecular weight H2CN :1.400700e+01
species n molecular mass n 0.0 nmolecular Weight of:HCN : molecular weight HCN :1.400700e+01
molecular Weight of:HCN : molecular weight HCN :1.400700e+01
species n molecular mass n 0.0 nmolecular Weight of:HNO : molecular weight HNO :1.599900e+01
molecular Weight of:HNO : molecular weight HNO :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:N : molecular weight N :1.400700e+01
molecular Weight of:N : molecular weight N :1.400700e+01
species n molecular mass n 0.0 nmolecular Weight of:NNH : molecular weight NNH :1.008000e+00
molecular Weight of:NNH : molecular weight NNH :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:N2O : molecular weight N2O :1.599900e+01
molecular Weight of:N2O : molecular weight N2O :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:NH : molecular weight NH :1.008000e+00
molecular Weight of:NH : molecular weight NH :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:NH2 : molecular weight NH2 :1.008000e+00
molecular Weight of:NH2 : molecular weight NH2 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:NH3 : molecular weight NH3 :1.008000e+00
molecular Weight of:NH3 : molecular weight NH3 :1.008000e+00
species n molecular mass n 0.0 nmolecular Weight of:NO : molecular weight NO :1.599900e+01
molecular Weight of:NO : molecular weight NO :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:NO2 : molecular weight NO2 :1.599900e+01
molecular Weight of:NO2 : molecular weight NO2 :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:HCNO : molecular weight HCNO :1.599900e+01
molecular Weight of:HCNO : molecular weight HCNO :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:HOCN : molecular weight HOCN :1.400700e+01
molecular Weight of:HOCN : molecular weight HOCN :1.400700e+01
species n molecular mass n 0.0 nmolecular Weight of:HNCO : molecular weight HNCO :1.599900e+01
molecular Weight of:HNCO : molecular weight HNCO :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:NCO : molecular weight NCO :1.599900e+01
molecular Weight of:NCO : molecular weight NCO :1.599900e+01
species n molecular mass n 0.0 nmolecular Weight of:CN : molecular weight CN :1.400700e+01
molecular Weight of:CN : molecular weight CN :1.400700e+01
species n molecular mass n 0.0 nmolecular Weight of:HCNN : molecular weight HCNN :1.400700e+01
molecular Weight of:HCNN : molecular weight HCNN :1.400700e+01
species n molecular mass n 0.0 molecular Weight of:N2: molecular weight N2:1.400700e+01
molecular Weight of:N2 : molecular weight N2 :1.400700e+01.
Plots and figures of O2 of Enthalpy, Entropy, Specific Heat:
Enthalpy vs Temperature:

Entropy vs Temperature:

Specific heat vs Temperature :

Plots and figures for N2 of Enthalpy, Entropy, Specific Heat:
Enthalpy vs Temperature:

Entropy vs Temperature:
Specific heat vs Temperature :

Plots and figures of CO2 of Enthalpy, Entropy, Specific Heat:
Enthalpy vs Temperature:

Entropy vs Temperature:

Specific heat vs Temperature :

OUTPUTS of Workspace:



Output of the files presents:

Errors:
I got only 48 species results.
I got an error during the running the program that I haven't got the results then rectified that I haven' save the 'THERMO.dat' in the programming file.