Project 1 - Parsing NASA thermodynamic data
Aim: Parsing NASA Thermodynamic data.
Governing Equation: `frac{Cp}{R} = a1+a2T+a3T^2+a4T^3+a5t^4`
`frac{H}{RT}=a1+a2frac{T}{2}+a3frac{T^2}{3}+a4frac{T^3}{4}+a5{T^4}{5}+frac{a6}{T}`
`frac{S}{R}=a1lnT+a2T+a3frac{T^2}{2}+a4frac{T^3}{3}+a5frac{T^4}{4}+a7`
Objective: To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy and specific heats for all the species in the data file.
Procedure:
- First we will use ‘close all’, ’clear all’, ‘clc’ to reset command window and workspace.
- Now we will open ‘THERMO.dat’ file by using ‘fopen’ command and save it in variable ‘f1’. (File should be saved in same folder where your codes are.)
- Now to read and skip the header of given data, we will use ‘fgetl’ command to read each line and ‘for’ loop to read and skip all 5 lines.
- After that we will loop again 53 times to loop through every species in the data.
- In this loop we will use ‘fgetl’ command 4 time to read all 4 lines and save it as variable ’line1’, ‘line2’, ‘line3’, ‘line4’ respectively.
- Each line will be saved as a single string
- Form line we need to take the species name and local temperature ranges.
- As we know the data on first row has spaces between them
- We can use this spaces to separate all the data. To do that we will use ‘strsplit’ function which will separate the string with the given character/string (In our case, it will be space which is written as ‘ ‘) and save it In ‘cell’ format.
- We will save this cell data in variable ‘A’.
- To extract name form this cell we will use indexing method to locate the name data.
- In every species, the first word of first line is name of species so our indexing will be ‘A(1)’ where ‘1’ stands for 1st cell of ‘A’ cell.
- We will save this in variable ‘name’.
- Now to extract temperature range which is at the end of the line we will use same indexing method to take temperature. For eg. A(end-1) where end means end of the line and ‘-1’ means one cell before which will give us mid temperature range.
- we will convert every number which are in cell format into number by calling function ‘str2num’.
- We will save temperature range numbers in array ‘temp’.
- To create range of temperature we will use function called ‘linspace’ which takes 2 number i.e. lower limit and upper limit and number of iterations I.e. number of divisions(in our case, 100) and then creates a array of number ranging from lower limit to upper limit with given iterations.
- Now we will creat a function for calculating molecular mass.
- In data we have molecular name of every species.
- With this we can calculate molecular mass of each species by adding up all molecular weight of each molecule in the given name.
- To create a function based on this first we have to note down all molecule name frequently used in the data. In our case, they are oxygen, nitrogen, carbon, hydrogen, and argon.
- Now we will create function in new page
- Specify the output variable, function name, and input variables. For eg.
’function [output] = function_name(input)’ - After that we will create an array of molecular names mentioned above and save it in variable ‘mname’ and there masses in variable ‘mmass’ (Order of ‘mname’ and ‘mmass’ should be same so that we can use indexing to get molecular mass of given molecule.)
- Now we will specify A (output variable) as 0.
- We will create a for loop to loop through length of name.
- In the loop we will use ‘findstr’ function to to find the position of given letter in ‘mname’ and save it in ‘B’ variable.
- In ‘findstr’ function if we will give a character which does not exists in the given variable, then it won’t give any output and variable ‘B’ will be blank.
- We will use ‘if’ statement to check weather ‘B’ is empty or not
- If B is not empty then use the given position to find mass of given molecule in ‘mmass’ variable and add it to output variable ‘A’.
- If B is empty then convert it to number and multiply to previous molecule’s weight and add it to the output variable.
- We will end the loop and function here.
- In main code we will call this function, give variable ‘name’ as input and save the output as ‘molemass’.
- Now to show molecular mass on command window, we will use ‘fprintf’ command to print the string ‘Molecular Mass of %s is %f\n\n’ where %s and %f will get replaced by name of the species and molecular mas of that species respectively and ‘\n’ is used to give space after the line.
- Now we will move to line2 and further where all 14 co-efficients are present.
- We know that every 4th last character of co-efficient is ‘E’.
- We will find ‘E’ in every coefficient by using ‘findstr’ command.
- We will use ‘findstr’ to find ‘E’ charactor in each line which will give array of position of all ‘E’ characters in that line and save this array in a variable.
- First coefficient will be from first character to E plus last 3 characters.
- With this logic we will us index ‘line2(1:B(1)+3) where B is the variable where all position of ‘E’ charactor is stored
- The rest four co-efficient starts form 4 characters after E of previous co-efficient to 3 characters after E of current co-efficient.
- Thus index will be ‘line2(B(1)+4:B(2)+3) which will get 2nd coefficient and rest of the 3 coefficient.
- These co-efficient are all in string format , to convert it ton number we will use ‘str2num’ command.
- Use same step for remaining 2 lines.
- Now we will create function called ‘heat_calculation’ to calculate Cp, H, S of each species at each temperature in temperature range.
- In this function we will use ‘temp’ array, ‘trange’ array and all coefficients as input and will give as Cp, H, S as output.
- We know that 1st 7 co-efficients are high temperature co-efficients and rest are low temperature coefficient.
- We have given mid temperature wich is the temperature lies between high and low temperature.
- So we will use ‘if’ statement to check given temp is in high range or low range
- If temp is high renge then use 1st 7 coefficient else use 2nd
- As we are calculating multiple amount of data we will use element wise operator to calculate all the data.
- We will end the function here and call this function in main code.
- Now last step is to create folder for every species and save there Cp, H and S plots in those folder.
- To create the folder we will use ‘mkdir’ command and give ‘name‘ variable as input to name the new folder to the current species name.
- We will use ‘cd’ command to change file directory to the new folder we created above.
- Now in ‘figure1’ we will plot Cp to trange graph and give title and label to all axis.
- To name the plot we will first create string in 2 variables ‘m1’ and ‘m2’.
- In ‘m1’ we will write string ‘Specific heat of %s
- In ‘m2’ we will use ‘sprintf’ command to mearge m1 with name(%s will get replaced by current name).
- We will us ‘saveas’ command to save the plot in the current folder with the given name and format.
- We will use same procedure for rest 2 graphs.
- Now we will come back to the main file by using ‘cd’ command.(double dots are use to go back a folder from the current file.)
- Now we will conclude our code by ending the loop and run the code.
Conclusion: This code will take ‘THERMO.dat’ file and give us graph of Enthalpy, Entropy, and Specific Heat to temperature in folder of that species.
Errors occurred while coding:
- Not able to save file inside species folder: while is was able to create file for every species, I was not able to save file inside those files. Solution was by using ‘cd’ command we can change the directory of file to that specific file.
% Code for Parsing NASA's 'TERMO' file.
close all
clear all
clc
% Skipping Header of given file.
f1 = fopen('THERMO.dat', 'r');
for i = 1:5
fgetl(f1);
end
% Looping through all species
for j = 1:53
line1 = fgetl(f1);
A = strsplit(line1, ' ');
name = char(A(1));
p = findstr(name, 'END');
temp = [str2num(A{end - 3}) str2num(A{end - 2}) str2num(A{end - 1})];
trange = linspace(temp(1), temp(2), 100);
% Calculating Molecular Mass
molemass = molecular_mass(name);
fprintf('Molecular Mass of %s is %f\n\n', name, molemass);
% Fetching species co-efficient
line2 = fgetl(f1);
B = findstr('E', line2);
a1 = str2num(line2(1:B(1)+3));
a2 = str2num(line2(B(1)+4:B(2)+3));
a3 = str2num(line2(B(2)+4:B(3)+3));
a4 = str2num(line2(B(3)+4:B(4)+3));
a5 = str2num(line2(B(4)+4:B(5)+3));
line3 = fgetl(f1);
C = findstr('E', line2);
a6 = str2num(line3(1:C(1)+3));
a7 = str2num(line3(C(1)+4:C(2)+3));
a8 = str2num(line3(C(2)+4:C(3)+3));
a9 = str2num(line3(C(3)+4:C(4)+3));
a10 = str2num(line3(C(4)+4:C(5)+3));
line4 = fgetl(f1);
D = findstr('E', line2);
a11 = str2num(line4(1:D(1)+3));
a12 = str2num(line4(D(1)+4:D(2)+3));
a13 = str2num(line4(D(2)+4:D(3)+3));
a14 = str2num(line4(D(3)+4:D(4)+3));
% Calculating Cp, H, S
[CP,H,S] = heat_calculation(temp,trange,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14);
% Creating and saving file and plots
mkdir(name)
cd(name)
figure(1)
plot(CP, trange);
c1 = sprintf('Specific Heat of %s:Temperature', name);
title(c1)
xlabel('Specific Heat')
ylabel('Temperature')
m1 = 'Specific Heat of %s';
m2 = sprintf(m1,name);
saveas(figure(1),sprintf('%s.png',m2));
figure(2)
plot(H,trange);
c2 = sprintf('Enthalpy of %s:Temperature', name);
title(c2)
xlabel('Enthalpy')
ylabel('Temperature')
m3 = 'Enthalpy of %s';
m4 = sprintf(m3,name);
saveas(figure(2),sprintf('%s.png',m4))
figure(3)
plot(S,trange)
c3 = sprintf('Entropy of %s:Temperature', name);
title(c4)
xlabel('Entropy')
ylabel('Temperature')
m5 = 'Entropy of %s';
m6 = sprintf(m5,name);
saveas(figure(3), sprintf('%s.png',m6))
cd ..
end
% Function for Calculating Molicular Mass
function [A] = molecular_mass(n)
mname = ['H' 'C' 'N' 'O' 'A' 'R'];
mmass = [1 12 14 16 40 0];
A = 0;
for i = 1:length(n)
B = findstr(n(i), mname);
if isempty(B)
j = findstr(n(i-1), mname);
A = A + (mmass(j) * (str2num(n(i))-1));
else
A = A + mmass(B);
end
end
% Function for Calculating Cp, H, S
function [A,B,C] = heat_calculation(temp,t,x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14)
tmid = temp(3);
if t > tmid
A = 8.314.*(x1+x2.*t+x3.*t.^2+x4.*t.^3+x5.*t.^4);
B = 8.314.*t.*(x1+(x2./2).*t+(x3./3).*t.^2+(x4./4).*t.^3+(x5./5).*t.^4+x6./t);
C = 8.314.*(x1.*log(t)+x2.*t+(x3./2).*t.^2+(x4./3).*t.^3+(x5./4).*t.^4+x7);
else
A = 8.314.*(x8+x9.*t+x10.*t.^2+x11.*t.^3+x12.*t.^4);
B = 8.314.*t.*(x8+(x9./2).*t+(x10./3).*t.^2+(x11./4).*t.^3+(x12./5).*t.^4+x13./t);
C = 8.314.*(x8.*log(t)+x9.*t+(x10./2).*t.^2+(x11./3).*t.^3+(x12./4).*t.^4+x14);
end
end