Project 1 - Parsing NASA thermodynamic data
Aim –
- Write a function that extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species given in data file.
- Calculate the molecular weight of each species and display it in the command window.
- Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
Theory –
File parsing is a process to analyze string of words into its constitutions. the purpose of file parsing is to transform data into a useful data, which is easy to store and manipulate.
In this project we are going to parse NASA’ thermodynamic data which has coefficient of the polynomial to calculate specific heat, entropy, enthalpy. There are 14 polynomial coefficient for the each 53 species. these polynomial will be used in below written formula for global temperature and local temperature as well.
Formulas, we are going to use in program, are –
R = Universal gas constant (J/mol.K)
T = Temperature (K)
Cp = Specific heat (J/Kg.K)
H = Enthalpy (J)
S = Entropy (J/K)
Code –
close all
clear all
clc
%open thermo data file
data = fopen("THERMO.dat",'r');
% read header
header = fgetl(data);
% read temperature
temp_str = fgetl(data);
temp_num = str2num(temp_str); % convert string to numeric
low_temp = temp_num(1);
mid_temp = temp_num(2);
high_temp = temp_num(3);
%skipping unnecessary lines
fgetl(data);
fgetl(data);
fgetl(data);
% define the value of universal gas constant R(J/mol.K)
R = 8.314;
%define temperature range
t = linspace(low_temp,high_temp,500);
%for loop to find values of coefficient
for i = 1:53
mol_name = fgetl(data); % read the line
mol = mol_name(1:6) % read the species name
l1_str =fgetl(data); % read the line 1 of coefficient
l1 = strfind(l1_str,"E"); % find position of E in string
a1 = str2num(l1_str(1:l1(1)+3)); % define value of coefficient and convert it to numeric from string data
a2 = str2num(l1_str(l1(1)+4 : l1(2)+3));
a3 = str2num(l1_str(l1(2)+4 : l1(3)+3));
a4 = str2num(l1_str(l1(3)+4 : l1(4)+3));
a5 = str2num(l1_str(l1(4)+4 : l1(5)+3));
l2_str = fgetl(data);
a6 =str2num(l2_str(1:l1(1)+3)); % read the line 2 of coefficient
a7 = str2num(l2_str(l1(1)+4 : l1(2)+3));
a8 = str2num(l2_str(l1(2)+4 : l1(3)+3));
a9 = str2num(l2_str(l1(3)+4 : l1(4)+3));
a10 = str2num(l2_str(l1(4)+4 : l1(5)+3));
l3_str = fgetl(data); % read the line 3 of coefficient
a11 = str2num(l3_str(1:l1(1)+3));
a12 = str2num(l3_str(l1(1)+4 : l1(2)+3));
a13 = str2num(l3_str(l1(2)+4 : l1(3)+3));
a14 = str2num(l3_str(l1(3)+4 : l1(4)+3));
% if-else statement to find values of Cp, H, S at different temperature
if t < mid_temp
c_p = R.*(a1 + a2.*t + a3.*(t.^2) + a4.*(t.^3) +a5.*(t.^4));
H = R.*(a1.*t + a2.*(t.^2)/2 + a3.*(t.^3)/3 + a4.*(t.^4)/4 + a5.*(t.^5)/5 + a6);
S = R.*(a1.*log(t) + a2.*t + a3.*(t.^2)/2 + a4.*(t.^3)/3 + a5.*(t.^4)/4 + a7);
else
c_p = R.*(a8 + a9.*t + a10.*(t.^2) + a11.*(t.^3) + a12.*(t.^4));
H = R.*(a8.*t + a9.*(t.^2)/2 + a10.*(t.^3)/3 + a11.*(t.^4)/4 + a12.*(t.^5)/5 + a13);
S = R.*(a8.*log(t) + a9.*t + a10.*(t.^2)/2 + a11.*(t.^3)/3 + a12.*(t.^4)/4 + a14);
end
% create directory
mkdir(['C:\Users\LOKESH\nasa\',mol]);
%plot between temperature and c_p (specific heat)
figure(1)
plot(t,c_p)
xlabel("temperature")
ylabel("specific heat")
title(mol)
%plot between temperature and H (Enthalpy)
figure(2)
plot(t,H)
xlabel("temperature")
ylabel("enthalpy")
title(mol)
%plot between temperature and S (Entropy)
figure(3)
plot(t,S)
xlabel("temperature")
ylabel("entropy")
title(mol)
%List the current directory
dir = ['C:\Users\LOKESH\nasa\',mol];
cd(dir); % set remote directory
%save plot figures
saveas(figure(1),"specific heat.jpg")
saveas(figure(2),"Enthalpy.jpg")
saveas(figure(3),"Entropy.jpg")
cd('C:\Users\LOKESH\nasa\'); % set remote directory
% call function to find molecular weight
m = molecular_mass(mol)
end
Explanation of code –
- Start with fopen command which takes file name and mode as its parameter, ‘r’ is for read mode and store under the variable name data.
- Read the header with fgetl command. fgetl command reads new line, if we use it again.
- Now read next line with same fgetl command. This string line has temperature values. We have to convert it into numerical line by using str2num command.
- Store temperature values into variables low _temp, mid_temp, high_temp.
- Now read three lines by fgetl command, we don’t need to store these into a variable because they are not useful for calculation.
- Assign the value of universal gas constant under variable name R.
- Define temperature range using linspace command which will be used by formulas.
- Start a for loop, which will run from 1 to 53 because there are 53 species.
- Read next line and store its first 6 character under variable name name mol, it will store species name.
- Read next line, store it under a variable and find E in the line using strfind command.we will get index of E.
- Using proper indexing to store all coefficient values under variable name a (a1 - a14) with converting them into numerical values using str2num command simultaneously.
- Apply if-else statement to calculate Cp, H ,S for different values of temperature.
- Now use mkdir command to create a directory. It takes parent directory name as parameter.
- Make plot for cp, H, S vs temperature with suitable labels and titles.
- Now list the current directory with dir command and set the remote directory with cd command.
- Use saveas command to save all the plots. Saveas command takes graphic object and file name with graphic format as its parameter.
- Again set remote directory using cd command.
- Call the function molecular_mass to calculate the molecular weight of each species.
- End the for loop with end command.
Code for the molecule weight –
function m = molecular_mass(mol)
atom = ['H';'C';'N';'O';'A';'s']; % atom name
mol_weight = [1 12 14 16 40 32]; % atomic weight
m = 0;
% for loop to find molecular weight of each species
for i=1:length(mol)
for j=1:length(atom)
if strcmp(mol(i),atom(j))
m = m + mol_weight(j);
u = j;
end
end
n = str2num(mol(i));
if n>1
m = m + mol_weight(u)*(n-1);
end
end
end
Explanation of code –
- Create a function with name molecular_weight and pass ‘mol’ as an argument.
- Define a column array with name atom which has various atoms.
- Now define a row array which has atomic weight of atoms.
- Now apply a nested for loop. First loop will run from 1 to length of mol and second will run from 1 to length of atom(column vector, earlier defined)
- Apply a if statement and apply strcmp command to compare elements of ‘mol’ and ‘atoms’.
- Assign value to ‘m’ which was 0.
- End the if statement and use str2num command to convert string elements into numeric element and store it into a variable ’n’.
- Apply a if statement which will calculate value of m.
- End the function and for loop with end command.
Output -
we got plots and molecular weight for all species and saved each into a folder automatically.
plots of o2
Plots of N2
Plots of Co2
Molecular weight of each species
Screenshot of folder containing plots of each species
Error faced while programming -
Solution of error -
Put multiplication sign(*) between each variable carefully.
References -