Project 1 - Parsing NASA thermodynamic data
FILE PARSING : File parsing refers to reading in a data stream and building a memory model of the content of that data. parsing splits a splits a file or other input into pieces of data that can be easily stored or manipulated.
step(1) - Define the function.
step(2) - Create an inpt parser object.
step(3) - Add inputs to the scheme.
step(4) - Set proprerties to adjust parsing.
step(5) - Parse the inputs.
step(6) - Use the inputs in functions.
step(7) - Calling function
NASA THERMODYNAMIC FILE :
"THERMO.dat" file is a data file containing 14 coefficients for 53 different elements given by NASA. These coefficients are utilized to calculate the specific heat, enthalpy,entropy for a particular element. Each element has its own range of local temperature.
LIST OF ELEMENTS CONTAINED IN NASA THERMODYNAMIC FILE :
Ar C C2H C2H2 C2H3 C2H4 C2H5 C2H6 C3H7 C3H8 CH CH2 CH2(S) CH2CHO CH2CO CH2O CH2OH CH3 CH3CHO CH3O CH3OH CH4 CN CO CO2 H H2 H2CN H2O H2O2 HCCO CCOH HCN HCNN HCNO HCO HNCO HNO HO2 HOCN N N2 N2O NCO NH NH2 NH3 NNH NO NO2 O O2 OH
CODE EXPALNATION : -
1) 'close all' - close all the plots & figures.
2) 'clc' - clears the command window.
3) 'mkdir' helps to make a directory with lopcartion address.
4) Now opening the text file 'THERMO.dat' file, using 'fopen' & reading it using read command 'r'
5) obtaining the temperature range from the 'THERMO.dat' file, using 'strsplit' to split the temperature into a particular string cell.
6) converting string to dooublr string using 'str2double'
7) initializing the temperature using 'linspace()' & universal Gas constant 'R'
8) skipped the next three comment lines using 'for & end' loops.
9) Reading all the 53 species to plot Specific heat(cp), enthalpy(H),& Entropy(S), with temperature values , & to find the molecular weights.
10) Used 'for' to loop read 53 species.
11) Used 'strfind' to find thestring 'E'
12) Extracted higher & ower temperature coefficients by reading line by line from the 'THERMO.dat' file
13) Calling Specific heat, Enthalpy, Entropy & Moecular weight functions.
14) Creating a file in the name of each species using 'mkdir'
15) plotting Specific heat v/s Temperature, Entropy v/s Temperature, Enthalpy v/s Temperature.
16) saving each plot in their respective folders using 'SAVEAS()' command.
SYNTAX USED DURING CODING :-
'mkdir' = make new directory
'fopen' = open a file to obtain information from the file
'fgetl' = Read line from the line
'strsplit' = split the string at specified delimiter
'str2double' = Converting string to double precision value
'strfind' = Finding one string with another
'pwd' = Idendtify current directory
'cd' = change working directory
'saveas' = save figure to specific file format
MAIN CODE :
close all
clear all
clc
%% Getting the global temperature values from the file
file = fopen('THERMO.dat','r');% Reading the nasa file
fgetl(file); % Skipping the 1st line by not assigning it to any variable
temp = fscanf(file,'%f'); % Reading the 2nd line and converting the string to float values
% global temperature values
global_low_temp = temp(1);
global_mid_temp = temp(2);
global_high_temp = temp(3);
%% Skipping 3 text lines
for i = 1:3
fgetl(file); % Skipping 3 lines by not assigning it to any variable
end
% Create a vector of 400 evenly spaced temperature points
R = 8.314; % universal gas constant
% comments1 = fgetl(file); % reading 3rd line from data
% comments2 = fgetl(file); % reading 4th line from data
% comments3 = fgetl(file); % reading 5th line from data
for i = 1:53
species = fgetl(file);
m = strsplit(species,' ');
species_sp = m{1};
local_low_temp = str2double(m{end-3});
local_med_temp = str2double(m{end-1});
local_max_temp = str2double(m{end-2});
T = linspace(global_low_temp,global_high_temp,400);
line1 = fgetl(file);
p1 = findstr(line1,'E');
line2 = fgetl(file);
p2 = findstr(line2,'E');
line3 = fgetl(file);
p3 = findstr(line3,'E');
a1 = str2double(line1(1:p1(1)+3));
a2 = str2double(line1(p1(1)+4:p1(2)+3));
a3 = str2double(line1(p1(2)+4:p1(3)+3));
a4 = str2double(line1(p1(3)+4:p1(4)+3));
a5 = str2double(line1(p1(4)+4:p1(5)+3));
a6 = str2double(line2(1:p2(1)+3));
a7 = str2double(line2(1:p2(1)+4:p2(2)+3));
a8 = str2double(line3(1:p2(1)+4:p2(3)+3));
a9 = str2double(line3(1:p2(1)+4:p2(4)+3));
a10 = str2double(line3(1:p2(1)+4:p2(5)+3));
a11 = str2double(line3(1:p3(1)+3));
a12 = str2double(line3(1:p3(1)+4:p3(2)+3));
a13 = str2double(line3(1:p3(1)+4:p3(3)+3));
a14 = str2double(line3(1:p3(1)+4:p3(4)+3));
% Specific heat
cp = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a10,T,R,global_mid_temp);
% Enthalpy
h = enthalpyfunc(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_mid_temp);
% Entropy
S = entropyfunc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_mid_temp);
% Calculating & plotting the data
MW(i) = molecular_weight(species_sp);
% Saving & plotting the data
mkdir('C:\Users\LENOVO\thermodynamicnasa',species_sp)
figure(1)
plot(T,cp,'LineWidth',2,'color','b')
title(sprintf('specific heat vs temperature for %s',species_sp))
xlabel('TEMPERATURE')
ylabel('SPECIFIC HEAT')
grid on
figure(2)
plot(T,h,'LineWidth',2,'Color','g')
title(sprintf('ENTHALPY VS TEMPERATURE FOR %s',species_sp))
xlabel('TEMPERATURE')
ylabel('ENTHALPY')
grid on
figure(3)
plot(T,S,'LineWidth',2,'Color','r')
title(sprintf('ENTROPY VS TEMPERATURE FOR %s',species_sp))
xlabel('TEMPERATURE')
ylabel('ENTROPY')
grid on
% pause the loop so that you can see each species plot in real time
cd(species_sp);
saveas(1,'Specific heat.png');
saveas(2,'Ethalpy.png');
saveas(3,'Entropy.png');
cd('C:\Users\LENOVO\thermodynamicnasa')
end
fclose(file);
1) MOLECULAR WEIGHT
The molecular mass is mass of the given molecule , it is measured in daltons. Different molecules of the same compounds may have different molecular masses because they contain different isotopes of an element.
% MOLECULR WEIGHT Calculation function
function h = molecular_weight(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_medium_temp)
%Comparing with global temperature
% If less than global temperature - minimum coefficients are taken
if T <= global_medium_temp
h = R.*(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5+a6));
else
% Else maximum coefficients are taken
h = R.*(a8.*T+(a9.*T.^2)./2+(a10.*T.^3)./3+(a11.*T.^4)./4+(a12.*T.^5)./5+a13);
end
2) ENTHALPY
Enthalpy is the property of a thermodynamic system, & is defined as the sum of the systems internal energy & the product of its pressure & volume & entropy is a state function.SI unit of enthalpy is Jolue.
equation dealing with Enthalpy is H = E+PV
Here we use the NASA Thermodynamic eqquation to find the ENTHALPY,
% Enthalpy Calculation function
function h = enthalpyfunc(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,R,global_medium_temp)
%Comparing with global temperature
% If less than global temperature - minimum coefficients are taken
if T <= global_medium_temp
h = R.*(a1.*T+(a2.*T.^2)./2+(a3.*T.^3)./3+(a4.*T.^4)./4+(a5.*T.^5+a6));
else
% Else maximum coefficients are taken
h = R.*(a8.*T+(a9.*T.^2)./2+(a10.*T.^3)./3+(a11.*T.^4)./4+(a12.*T.^5)./5+a13);
end
3) ENTROPY
Entropy is the measure of a systems thermal energy per unit temperature that is unavailable for doing useful work, because work is obtained fom ordered molecular motion. In other words entropy is also defined as the measure of the molecular disorder, or randomness, of a system.
For reversible machines, entropy change eqation can be formed as :`Delta`S = Q/TO, where 'Delta'S is the change in entropy, Q is heat , TO is temperature at an instant . The SI unit of entropyis jouls/kelvin.
% Entropy calculation function
function S = entropyfunc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,T,R,global_medium_temp)
% Comparing with global temperature
% if less than global temperature - minimum coefficients are taken
if T <= global_medium_temp
S = R*(a1.*log(T) + (a2.*T) + (a3*((T).^2)/2) + (a4*((T).^3)/3 + (a5*((T).^4)/4) + a7));
else
% Else - maximum coefficients are taken
S = R*(a8.*log(T) +a9.*T+a10.*(T.^2)./2 + a11*(T.^3)./3 + a12.*(T.^4)/4+a14);
end
4) SPECIFIC HEAT
Specific heat is the quantity of heat reaquired to raise athe temperature of 1 gram of a substance by 1 degree of celsius degree. The SI unit of specific heat heat is joule/kelvin/kilogram.
%program for specific heat
function cp = specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a10,T,R,global_mid_temp)
% comparing with global temp
if T > global_mid_temp
cp = R.*(a1+a2.*T+a3.*T.^2+a4.*T.^3+a5.*T.^4);
else
cp = R.*(a8+a10.*T+a10.*T.^2+a10.*T.^3+a10.*T.^4);
end














