Project 1 - Parsing NASA thermodynamic data
Parsing NASA thermodynamic data
Aim:
- To write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the NASA Thermodynamic data file.
- To plot the arrived data of Cp, H, S with its temperature respectively.
- To calculate the Molecular weight of each specimen.
Code:
% Code to Parsing the NASA Thermodynamic data
clear all
close all
clc
%Open and reading the NASA Thermoynamic data file
T1 = fopen('THERMO.dat','r');
%Getting the title from the NASA data file
Title = fgetl(T1);
%Getting the Global temperature
L = str2num(fgetl(T1));
global_low_temp = L(1);
global_mid_temp = L(2);
global_high_temp = L(3);
%for loop for skipping the Comment lines in the file
for k = 1:3
fgetl(T1);
end
%Creating folder
mkdir nasa
%for loop for reading each specimen values
for i=1:inf
L1 = (fgetl(T1));
%if loop for the loop termination
if strcmpi(L1,'END')
break
else
A = strsplit(L1 ,' ');
Z = A{1};
%Fliplr command for getting local temperature
A = fliplr(A);
local_low_temp = str2double(A{4});
local_med_temp = str2double(A{2});
local_high_temp = str2double(A{3});
L2 = fgetl(T1);
%strfind command to sperate the coefficient
l = strfind(L2,'E');
%Co-efficient speration and asigning
a1 = str2double(L2(1:l(1)+3));
a2 = str2double(L2((l(1)+4):(l(2)+3)));
a3 = str2double(L2((l(2)+4):(l(3)+3)));
a4 = str2double(L2((l(3)+4):(l(4)+3)));
a5 = str2double(L2((l(4)+4):(l(5)+3)));
L3 = fgetl(T1);
a6 = str2double(L3(1:l(1)+3));
a7 = str2double(L3((l(1)+4):(l(2)+3)));
a8 = str2double(L3((l(2)+4):(l(3)+3)));
a9 = str2double(L3((l(3)+4):(l(4)+3)));
a10 = str2double(L3((l(4)+4):(l(5)+3)));
L4 = fgetl(T1);
a11 = str2double(L4(1:l(1)+3));
a12 = str2double(L4((l(1)+4):(l(2)+3)));
a13 = str2double(L4((l(2)+4):(l(3)+3)));
a14 = str2double(L4((l(3)+4):(l(4)+3)));
%Linspace command to create various local temperature
Temp = linspace (local_low_temp, local_high_temp, 100);
%Gas constant
R = 8.314;
%for loop for Cp, H and S calculation for low and high
%temperature coefficient
for j = 1 : length(Temp)
if Temp(j) >= 1000
Cp(j) = R*(a1 + a2*Temp(j) + a3*Temp(j)^2 + a4*Temp(j)^3 + a5*Temp(j)^4);
H(j) = R*Temp(j)*(a1 + a2*Temp(j)/2 + (a3*Temp(j)^2)/3 + (a4*Temp(j)^3)/4 + (a5*Temp(j)^4)/5 + a6/Temp(j) );
S(j) = R* (a1*log(Temp(j)) + a2*Temp(j) + (a3*Temp(j)^2)/2 + (a4*Temp(j)^3)/3 + (a5*Temp(j)^4)/4+ a7);
else
Cp(j) = R*(a8 + a9*Temp(j) + a10*Temp(j)^2 + a11*Temp(j)^3 + a12*Temp(j)^4);
H(j) = R*Temp(j)*(a8 + a9*Temp(j)/2 + (a10*Temp(j)^2)/3 + (a11*Temp(j)^3)/4 + (a12*Temp(j)^4)/5 + a13/Temp(j) );
S(j) = R*(a8*log(Temp(j)) + a9*Temp(j) + (a10*Temp(j)^2)/2 + (a11*Temp(j)^3)/3
+ (a12*Temp(j)^4)/4+ a14);
end
end
%Plotting the Cp, H, S with respect to temperature
figure(1)
plot(Temp, Cp)
title('Comparing Temperature with specific heat',Z)
xlabel('temperature')
ylabel('specfic heat')
figure(2)
plot(Temp,H)
title('Comparing Temperature with specific heat',Z)
xlabel('temperature')
ylabel('specfic heat')
figure(3)
plot(Temp,S)
title('Comparing Temperature with specific heat',Z)
xlabel('temperature')
ylabel('specfic heat')
%To open the folder
cd 'C:\Users\STUDENT-6\Documents\MATLAB\nasa'
%creating folder
mkdir (z)
cd(Z)
%To save the files
saveas (1,'Cp.jpg')
saveas (2,'H.jpg')
saveas (3,'S.jpg')
cd ..
cd ..
%Calling the Molecular weight calculation function
mol_wt = molecular_wgt(Z);
[M] = sprintf('The molecular weight of %s is %f',Z,mol_wt);
end
end
%Closing the opened NASA data file
fclose(T1);
Explanation:
- The NASA Thermodynamic data file contains the data for Global & local temperature and its Coefficients for 53 different kinds of chemical elements and compounds.
- The NASA Thermodynamic data file has 14 coefficients for each of its 53 chemical elements and compounds.
- The NASA Thermodynamic data file is used here to find the Entropy, Enthalpy, and Specific heat of all the given specimens.
- The Molecular weight of each specimen is also calculated.
Command's used:
- The open command, fopen( filename,' permission').
- The get command, fgetl(filename).
- The string to number, str2num or str2double(character).
- The compare command, strcmpi(s1,s2), where s1 & s2 statement to compare.
- The split command, strsplit(str, delimiter).
- The flip command, fliplr(a), where 'a' is neither string nor number.
- The find command, strfind(str,' pat'), where 'pat' is where to separate.
- The linspace command, linspace(x1,x2,n), where x1 & s2 are start & end value and n is number of value in between that values.
- The length command, length(a), where 'a' is the string or number.
- The figure command, figure(f), where 'f' is the name of the figure.
- The plot command, plot(X, Y), where X & Y is the content to the plot.
- The new folder command, "mkdir filename".
- The folder open command, cd(filename).
- The save command, saveas (fig, filename).
- The sum command, sum(A), where 'A' is variable to sum.
- The print command, fprintf(formatspec,'a1,a2,..an'), where a1,..an is content to print.
- The close command, fclose(filename).
Loop used:
- The for loop syntax, for Index variable = initial value: Ending value
statement 1;
end
- The if loop syntax, if (condition)
statement 1;
end
- The if else synatx, if (condition)
statement 1;
else
statement 2;
end
Code explanation
- The code starts with regular commands clear all, close all, and clc(clear).
- The open command is used to open the NASA data file and Read command is passed.
- The file is assigned to the variable 'T1'.
- The get command is used to get the line of any file.
- The get command is used to get the first line of the command and is assigned to 'tittle' variable.
- The string to number command is used to change the value in the next line of the file by using the gat command.
- The values which arrived are assigned to their respective variable(global temperature).
- The for loop is formed to skip the next three lines in the command.
- The new folder command is used to create the folder.
- The for loop is formed to make the codes in it run for repeated times.
- The for loop index variable is 'i' and the starting value is '1' & end value is 'inf'.
- The if loop is created with a condition with the help of compare command strcommand(L1,'END').
- If the condition of if loop doesn't satisfy the else command will run.
- The next line is taken from the file.
- The split command is used because it contains both the string and number.
- The specimen is taken after splitting is done.
- The flip command is used to flip the arrived split value,fliplr(A).
- The local temperature value is calculated from this and converted from string to number.
- The next line has got from the file.
- The find command is used to find the particular letter or number in the obtained string.
- It has been found that 'E' plus three numbers are the ends of the first coefficient.
- The find command is used to find the letter 'E' in that string to separate the values.
- So by that means there is five coefficient in the single line.
- The Next coefficient is of four numbers from the first E value.
- Then by using this method first five coefficient is found.
- The string to double is used here because we can get the entire decimal value of that coefficient.
- The obtained five coefficients are converted here.
- The above procedure is carried until all these fourteen coefficients are found.
- The range of temperature values is found in between Local high and low using the linspace command(local_high_temp,local_low_temp,100).
- The 'R' gas constant value is assigned.
Calculating the Cp, H, and S value
- The for loop is created to find the Cp, H, and S values for every temperature value.
- The index variable is 'j', the starting value is '1' and the ending value is 'length of temperature.
- The if-else loop is used here to separate the high and low-temperature values.
- The high and low temperatures are taken from the given question.
- The if statement runs when the temperature satfies this condition(temp(j) >= 1000).
- If the above condition is not satisfied then else formula will run.
- The formula for calculating the Cp, H, and S is taken from the question.
- The formula which is given below is the formula used in the code.
The formula for if condition
Cp(j) = R*(a1 + a2*Temp(j) + a3*Temp(j)^2 + a4*Temp(j)^3 + a5*Temp(j)^4);
H(j) = R*Temp(j)*(a1 + a2*Temp(j)/2 + (a3*Temp(j)^2)/3 + (a4*Temp(j)^3)/4 + (a5*Temp(j)^4)/5 + a6/Temp(j) );
S(j) = R* (a1*log(Temp(j)) + a2*Temp(j) + (a3*Temp(j)^2)/2 + (a4*Temp(j)^3)/3 + (a5*Temp(j)^4)/4+ a7);
The formula for else condition
Cp(j) = R*(a8 + a9*Temp(j) + a10*Temp(j)^2 + a11*Temp(j)^3 + a12*Temp(j)^4);
H(j) = R*Temp(j)*(a8 + a9*Temp(j)/2 + (a10*Temp(j)^2)/3 + (a11*Temp(j)^3)/4 + (a12*Temp(j)^4)/5 + a13/Temp(j) );
S(j) = R*(a8*log(Temp(j)) + a9*Temp(j) + (a10*Temp(j)^2)/2 + (a11*Temp(j)^3)/3 + (a12*Temp(j)^4)/4+ a14);
Plotting
- The plotting has been made for all three calculated values.
- The figure one is created with the 'Cp' and its respective temperature.
- The figure two is created with the 'H' and its respective temperature.
- The figure three is created with the 'S' and its respective temperature.
- The new folder is created to save the above-mentioned figures in it.
- The 'mkdir' command is used to create the folder.
- The 'cd' command is used to change or write content in the folder.
- The 'saveas' command is used to save the figure in the folder.
- The 'cd ..' command is used to close the folder.
Molecular weight
- The molecular weight is calculated using the separate code is called to display its value.
- The called function is assigned to the variable 'Mol_wt'.
- The print command is used to print the Molecular weight with its specimen name.
- The for and the if-else command is closed using the 'End' keyword.
- The close command is used to close the opened file.
Molecular weight calculation
Code:
% Function to calculate Molecular Weight for every specimen
function [Molecular_wt] = molecular_wgt(specimen_n)
%Inputs
atoms = [ 'H', 'C', 'N', 'O', 'A', 'S'];
atomic_wt = [ 1, 12, 14, 16, 40, 32];
m_w =0;
%for and if loop for calculating molecular weight
for i = 1: length(specimen_n)
for j = 1: length(atoms)
if strcmpi(specimen_n(i),atoms(j))
Molecular_wt(i) = m_w + atomic_wt(j);
Molecular_wt = sum(Molecular_wt);
pos = j;
end
end
%if loop for calculating number in specimen name
num = str2num(specimen_n(i));
if num >1
Molecular_wt = Molecular_wt + atomic_wt(pos)*(num-1);
end
end
end
- The molecular weight is the calculation of the total number of values in one compound.
- The Inputs taken for the molecular weight are the Atoms and the Atomic weight of each specimen.
- The molecular weight is first assigned to zero for the solution purpose(m_w =0)
- The Atoms and atomic weight used here is hydrogen, oxygen, nitrogen, carbon, argon, and sulfur and its atomic weight is 1, 16, 14, 12, 40, and 32.
- The for loop is used here, the index variable is 'i', the starting value is '1', and the ending value is 'length(specimen name)'.
- The second for loop is used here, the index variable is 'j', the starting value is '1', and the ending value is 'length(atoms)'.
- The if loop is used to compare the obtained value with its atoms to get the atomic weight of each specimen.
- The if loop condition strcmpi(specimen name (i), atoms(j).
- The Molecular weight is calculated using the formula Molecular weight(i) = m_w + atomicweight(j).
- The total molecular weight is calculated by adding each value using the sum command(sum(Molecular_weight)).
- The 'j' value is stored in the variable(pos).
- The string to number command is used to convert the obtained string(specimen name(i)) and stored in variable num.
- The if loop is used with the condition (num >1).
- If the above condition satisfied the formula inside the if loop is solved.
- The formula in if loop is, molecular weight = molecular weight + atomic weight (pos) * (num-1).
- All loop created in this function is closed.
- The function is terminated.
Conclusion:
- The code that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the NASA Thermodynamic data file.
- Plotting is done with the arrived data of Cp, H, S with its temperature respectively.
- The molecular weight of each specimen is calculated.