Program to Parse a NASA Thermodynamic dat file
Aim :- Write a program to parse a NASA thermodynamic file and derived Specific heat vs Temperature, Enthalpy vs Temperature and Entropy vs Temperature and also calculate molecular mass of all species.
Challanges :- 1. 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. You will be reading this file NASA thermodynamic data file. The formulae are shown below.
Here R is the universal gas constant, T is the temeprature
2. Calculate the molecular weight of each species and display it in the command window.
3. Plot the Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) specific for each species.
4. Save the plots as images with appropriate names and dump them in separate folders for each species with suitable name. All these should be generated automatically.
5. The main program that you will submit will just take the \"THERMO.dat\" file as an input and generate Cp, H and S plots for all species that are included in the file.
6. Please write a detailed report to describe your logic.
Solution
Input or Main code :-
% Parsing of NASA Thermodynamic data
clear all
close all
clc
%Step 1: Entering Input data and Reading file and fatching important data from it
f1 = fopen(\'THERMO.dat\',\'r\');
fgetl(f1);
O = fgetl(f1);
OA = strsplit(O,\' \');
% Findin and storing Global temperature values
Global_Low_temp = str2double(OA{2});
Global_Mid_temp = str2double(OA{3});
Global_High_temp = str2double(OA{4});
% Input
R = 8.314;
% Loop to read and avoid unwanted lines from file
for i = 1:3
fgetl(f1);
end
% Step 2 : Derived species name, local temperature
% Loop for read file
for j = 1:53
line1 = fgetl(f1); % get first line frome the sat of four line of one species
A = strsplit(line1,\' \'); % Separate part when space is found
member_of_A = length(A); % After separation of line finding the total members available in line
Species_Name = (A{1}); % Name the first part of the line as \"Species_Name\"
% Define the values of the local temperatue;Low, Mid and High
Local_Low_Temperature = str2double(A(member_of_A-3));
Local_Mid_Temperature = str2double(A(member_of_A-1));
Local_High_Temperature = str2double(A(member_of_A-2));
% Step 3 : Define values of low temperature and high temperature co-efficients
line2 = fgetl(f1); % Get second line from the species group
line3 = fgetl(f1); % Get third line from the species group
line4 = fgetl(f1); % Get first line from the species group
AA = strfind(line2,\'E\'); % Find \'E\' from the second line
AB = strfind(line3,\'E\'); % Find \'E\' from the third line
AC = strfind(line4,\'E\'); % Find \'E\' from the fourth line
% Co-efficient at low temperature
A1 = str2double(line2(1:AA(1)+3));
A2 = str2double(line2(AA(1)+4:AA(2)+3));
A3 = str2double(line2(AA(2)+4:AA(3)+3));
A4 = str2double(line2(AA(3)+4:AA(4)+3));
A5 = str2double(line2(AA(4)+4:AA(5)+3));
A6 = str2double(line3(1:AB(1)+3));
A7 = str2double(line3(AB(1)+4:AB(2)+3));
% Co-efficient at high temperature
B1 = str2double(line3(AB(2)+4:AB(3)+3));
B2 = str2double(line3(AB(3)+4:AB(4)+3));
B3 = str2double(line3(AB(4)+4:AB(5)+3));
B4 = str2double(line4(1:AC(1)+3));
B5 = str2double(line4(AC(1)+4:AC(2)+3));
B6 = str2double(line4(AC(2)+4:AC(3)+3));
B7 = str2double(line4(AC(3)+4:AC(4)+3));
% Define the Lower temperature rand and Higher temperature rang
Lower_temp = linspace(Local_Low_Temperature,Local_Mid_Temperature,100);
Higher_temp = linspace(Local_Mid_Temperature,Local_High_Temperature,100);
%step 4 : Calculation of Cp, H and S:
% Create loop to find the value of Cp, H and S at both lower and Higher value of temperature
for k = 1:100
C_p1(k) = R*(B1 + (B2*Lower_temp(k)) + (B3*(Lower_temp(k))^2) + (B4*(Lower_temp(k))^3) + (B5*(Lower_temp(k))^4));
H1(k) = (R*Lower_temp(k))*(B1 + (B2*(Lower_temp(k)/2))+ (B3*((Lower_temp(k)^2)/3))+(B4*((Lower_temp(k)^3)/4)) + (B5*((Lower_temp(k)^4)/5)) + (B6/Lower_temp(k)));
S1(k) = R*(B1*log(Lower_temp(k)) + (B2*Lower_temp(k))+ (B3*(((Lower_temp(k))^2)/2))+ (B4*(((Lower_temp(k))^3)/3)) + (B5*(((Lower_temp(k))^4)/4))+ B7);
C_p2(k) = R*(A1 + (A2*Higher_temp(k))+ (A3*(Higher_temp(k))^2)+(A4 * (Higher_temp(k))^3) + (A5 *(Higher_temp(k))^4));
H2(k) = R*Higher_temp(k)*(A1 + (A2*(Higher_temp(k)/2))+ (A3*((Higher_temp(k)^2)/3))+(A4*((Higher_temp(k)^3)/4)) + (A5*((Higher_temp(k)^4)/5))+ (A6/Higher_temp(k)));
S2(k) = R*(A1*log(Higher_temp(k)) + (A2*Higher_temp(k))+ (A3*(((Higher_temp(k))^2)/2))+ (A4*(((Higher_temp(k))^3)/3)) + (A5*(((Higher_temp(k))^4)/4))+ A7);
end
% Step 5 Calculating the molecular mass
% Calling function for finding molecular mass
Molecular_mass = Molecular_weight(Species_Name);
%Display molecular mass derived by function
fprintf(\'Molecular Weight of %s : %f u\\n\',Species_Name,Molecular_mass)
%Step 6 : Creating folder for storing the derived plot from above codes
Working_directory = \'Harsh_NASA_File_Parsing_Poject\';
mkdir(Working_directory)
current_dir = pwd;
cd(Working_directory)
Name_of_folder = char(Species_Name);
name = sprintf(\'%s\',Name_of_folder);
cur_dir = pwd;
mkdir(Name_of_folder)
cd(Name_of_folder)
%Step 7 : Plotting and give the plot appropriate title, legend, nam and X,Y label and save it
figure(1)
plot(Lower_temp,C_p1,\'linewidth\',2)
hold on
plot(Higher_temp,C_p2,\'linewidth\',2)
hold off
title([\'Specific Heat vs Temperature of \',num2str(Species_Name),\' and Mole mass :\',num2str(Molecular_mass),])
xlabel(\'Temperature K\')
ylabel(\'Specific Heat J/mol K\')
legend({\'Low Temperature\',\'High Temperature\'},\'Location\',\'best\')
grid on
saveas(figure(1),[\'Specific Heat vs Temperature of \',Species_Name,\'.jpg\']);
figure(2)
plot(Lower_temp,H1,\'linewidth\',2)
hold on
plot(Higher_temp,H2,\'linewidth\',2)
hold off
title([\'Enthalpy vs Temperature of \',num2str(Species_Name),\' and Mole mass :\',num2str(Molecular_mass),])
xlabel(\'Temperature K\')
ylabel(\'Entalpy J\')
legend({\'Low Temperature\',\'High Temperature\'},\'Location\',\'best\')
grid on
saveas(figure(2),[\'Enthalpy vs Temperature of \',Species_Name,\'.jpg\']);
figure(3)
plot(Lower_temp,S1,\'linewidth\',2)
hold on
plot(Higher_temp,S2,\'linewidth\',2)
hold off
title([\'Entropy vs Temperature of \',num2str(Species_Name),\' and Mole mass :\',num2str(Molecular_mass),])
xlabel(\'Temperature K\')
ylabel(\'Entopy J/K\')
legend({\'Low Temperature\',\'High Temperature\'},\'Location\',\'best\')
grid on
saveas(figure(3),[\'Entropy vs Temperature of \',Species_Name,\'.jpg\']);
% Return to the man directory
cd(\'D:\\octave-5.1.0-w64\\Practice\')
end
fclose(f1);
Explanation of Code :-
For ease of understanding, I divide the whole program into steps. The function for finding molecules weight is explained after the explanation of code\'
Program is start with clc, clear all and close all.
Step 1 :-
- It maily consists file header reading, entering input parameters and define global temeratures(Low, Midium and Hige) of the whole system.
- fopen is used to open \"the THERMO.dat\" file and after that by fgetl reading of line is successfully done.
- By strsplit line is split after every space. str2double is used for defining the Global Low temp, Global Mid temp and Global High temp.
- R is gas constant and is added as initial input to calculate specific heat, enthalpy and entropy.
- a for loop is used to avoid unnecessary lines from the .dat file with the help of fgetl
Step 2 :-
- After a foor loop is used to divide the whole program in group of five line with read every five line for 53 times which is typed as 53 iteration of j.
- fgetl, strsplit is used to read and divide line in words. By length() member of line is found and by this \"Species Nam\", \"Local Low Temperatuer\", \"Local Mid Temperature\", \"Local High Temperature\" is defined.
Step 3 :-
- Now from use of fgetl 3 lines which consist values of co-efficients is red. The first seven co-effients are low temperature and another seven co-efficient are high temperature.
- Each co-efficient is assign with the appropriate value by str2double.
- Lower temperature range and higher temperature range is defined by linspace() and the range is 100 numbers between Local Low Temperature and Local Mid Temperature and Local Mid Temperature and Local High Temperature
Step 4 :-
- After this step, a for loop is used for evaluate values of C_p1, C_p2, H1, H2, S1, S2 which are specific heat, enthalpy, entropy relatively for lower and higher temperature value.
- The equetion given above and by gas constant R, Local Lower Temperature, Local Higher Temperature and required co-effient the values are calculated
Step 5 :-
- This step is for calling a function which is used for calculate molecular weight for perticular Specimen.
- The function is given bellow, in which two aaray was created which are element name and atomic mass.
- 0 assigned to Molecular_mass. Species Name is stored in character named Species.
- two for loop is created to calculate Atomic mass.
- Next three steps are used for multiplication in number of atoms.
- At last Molecular mass is calculated.
- By fprintf it is displayed on the command window.
% Function to calculate Molecular Mass
function Molecular_mass = Molecular_weight(Species_Name)
Elements = [\'H\',\'C\',\'O\',\'N\',\'R\',\'S\']; % Available elements
Atomic_mass = [1.00784 12.0107 15.999 14.0067 39.948 32.065]; % Atomic mass of available elements
Molecular_mass = 0;
Species =char(Species_Name); % Stor the value in character
for i = 1:length(Species)
for j = 1:length(Elements)
if strcmp(Species(i),Elements(j))
Molecular_mass = Molecular_mass +Atomic_mass(j);
position = j ;
end
end
n = str2double(Species(i));
if n>1
Molecular_mass = Molecular_mass + Atomic_mass(position)*(n-1);
end
end
Step 6 :-
- This step is used to make directory to create folders for save the plots of species.
- \"Harsh_NASA_File_Parsing_project\" named folder is created with use of mkdir(). pwd() define current working directory. cd() changes the current directory.
- Folder created which name is same as the species data saved in it. and working directory changed by cd().
Step 7 :-
- Step 7 is used to plot data, dive a plot an axis lable, a title, appropriate ligend.
- saveas() is used for save the plot in jpg format in the appropriate folder.
- cd() used for change the path and return to current folder.
This repeats for mor 52 times. After that fclose() is used to close the f1 file.
Output :-
O2 :
N2 :-
CO2 :-
Folder with all the species folder :-
Highlighted folder is parent folder in which all folders are created
All species folder
Molecular mass in command window:
Workspace :-
Matlab window :-
Errors and steps to resolve it :-
Most of the errors I get while programming are typogrphical error which is resolved by correcting syntax and use correct one.
The main error is occured while I assigned wrong value to local mid temperature and local high temperature which gives defaulty plots and data.
This cause faulty graph like below
The solution of error is gained by assigning of correct value to the local lo, mid and high temperature which results in desired output.