Challenge

Project 1 - Parsing NASA thermodynamic data

AIM

  • Write a function that extracts the 14 coefficients and calculate the enthalpy, entropy and specific heat for all the species in the given data file.
  • Calculate the molecular wieght of each species and display it in the command window.
  • Plot the Cp, Enthalpy, Entropy for the local temperature range (low temp:high temp) species for each species.
  • Save the plot as images with appropriate names and dump them in separate folders for each species with suitable name. All of these should be generated automatically.

THEORY

File parsing refers to how information can be read or extracted form a particular file and how the information can be used in a computer program. With the help of file parsing we can read data from external files and databases and we use that information or data in our calculations.

SPECIFIC HEAT CAPACITY

The specific heat of a substance is defined as the amount of heat required to raise a unit mass of a substance through a unit rise in temperature.however it would be pretty incovoenient to measure the heat capacity of every unit of matter. What we want is an intensive property that depends only on the type and phase of a substance and can be applied of systems of arbitary size. This quanitity is known specific heat capacity. Experiments show that the transferred heat depends on three factors.

  1. The change in temperature.
  2. The mass of thr system
  3. Substance and phase of the system.

`Q = mcΔT`

Where Q is heat transfer, m Is the mass of the substance, T is the temprrature change, c stands for specif heat capacity.

ENTHALPY

The enthalpy of a substance, h is defined as `h = u + pv`

U is the internal energy, p and v are the pressure and volume of the system. The state off the gas is defined by several properties including the temperature l, pressure, amd volume which gas occupies.

From the study of the first law of thermodynamics we find the internal energy of a gas is also a state variable that is a variable depends only on yhe state of a gas and not on any orcess that prodeuced that state.

ENTROPY

Generally entropy is defined as a measure of randomness or disorder of a system. This concept was indroduced by a german physicist named rudolf clausius in 1850. From the thermodynamic view point entropy is used to describe the behaviour of a system in terms of thermodynamic property such as temperature, pressure, and heat capacity. This thermodynamic description take in to consideration the state of equilbrium of the system.Entropy is an extensive property increases with decreases with regularity.

The change in entropy,

`ΔS = (Q/T)`

Where Q is heat transfer, T is the absolute temperature

GOVERNING EQUATIONS

NASA came up with a polynomial that can be used to evaluate thermodynamic properties such as Cp, H and S. They have documented the coefficients that are required to evaluate these polynomials for 1000+ species.

The formulas has been mentioned below,

Where R is the universal gas constant and T is the temperature

`(Cp)/R = a1 + a2*T + a3*T^2 + a4*T^3 + a5*T^4`

`(H)/RT = a1 + a2*T/2 + a3*T^2/3 + a4*T^3/4 + a5*T^4/5 + a5*T`

`(S)/R =a1*1n(T) + a2*T + a3*T^2 /2+ a4*T^3/3 + a5*T^4/4 + a7`

The first 7 coefficients are high temperature coefficient and the second 7 coefficients are low temperature coefficients in the data file provided.

Also we need to calculate the molecular wieght of each elements.

MOLECULAR WIEGHT

Molecular wieght is the measure of sum of atmoic wieght values of the atoms in a molecule. Molecular wieght is usend in chemistry to determine stoichiometry in chemical reaction and equations. Both atomic wieght and molecular wieght are defined relative to the mass of the isotope carbon-12 which is assigned a value of 12 amu.

SIMPLE PROCEDURE AND PLAN FOR PROGRAM

  • Initially we open the thermo file and the extract the valie of different lines.
  • Then we extract the values of the coefficients of different species.
  • Then we make directory.
  • The we write the code for function to find specific beat, enthalpy and entropy of different species and call this function in our main program.
  • Then we plot the curve for different species and save them in ‘png’ format.
  • At last we find the molecular wieght of each species.

MAIN PROGRAM:

close all
clc
clear all
 
% reading the header information from THERMO.dat
f1 = fopen('THERMO.dat','r');
% heading
 fgetl(f1);
 
 % global maximum temperature and global minimum temperature
 
 tline = fgetl(f1);
 temp = strsplit(tline,' ');
 global_max_temp = str2num(temp{4});
 global_med_temp = str2num(temp{3});
global_low_temp = str2num(temp{2});
 
% reading the comments 
for m = 1:3
    comments = fgetl(f1);
end
for k =1:53
    
    % getting the element name 
species_line = fgetl(f1);
species_split = strsplit(species_line,' ');
species = species_split{1};


%storing the local tempertaure of the elements from the file
local_min_temp = str2num(species_split{end-3});
local_med_temp = str2num(species_split{end-1});
local_max_temp = str2num(species_split{end-2});

%To read and store the coeffcients of the species
coeff_line1 = fgetl(f1);
coeff_line2 = fgetl(f1);
coeff_line3 = fgetl(f1);
R = strfind(coeff_line1,'E');
U = strfind(coeff_line2,'E');
T = strfind(coeff_line3,'E');

% HIGH TEMPERATURE COEFFICIENT
c1= str2num(coeff_line1(1:R(1)+3));
c2 = str2num(coeff_line1(R(1)+4:R(2)+3));
c3 = str2num(coeff_line1(R(2)+4:R(3)+3));
c4 = str2num(coeff_line1(R(3)+4:R(4)+3));
c5 = str2num(coeff_line1(R(4)+4:R(5)+3));
c6 = str2num(coeff_line2(1:U(1)+3));
c7 = str2num(coeff_line2(U(1)+4:U(2)+3));

% LOW TEMPERATURE COEFFICIENT
c8 = str2num(coeff_line2(U(2)+4:U(3)+3));
c9 = str2num(coeff_line2(U(3)+4:U(4)+3));
c10 = str2num(coeff_line2(U(4)+4:U(5)+3));
c11 = str2num(coeff_line3(1:T(1)+3));
c12 = str2num(coeff_line3(T(1)+4:T(2)+3));
c13 = str2num(coeff_line3(T(2)+4:T(3)+3));
c14 = str2num(coeff_line3(T(3)+4:T(4)+3));

% Temperature range 
T = linspace(local_min_temp,local_max_temp,100) ;
% universal gas constant
R = 8.314;
% to calculate the values of the Cp, H, S for each of the elements
for i = 1:length(T)
  if T(i) < local_med_temp
Cp(i) = R*(c8 + c9.*T(i) + c10.*T(i).^2 + c11.*T(i).^3 + c12.*T(i).^4);
H(i) = R*T(i)*(c8 + c9.*(T(i))/2 + c10.*(T(i).^2)/3 + c11.*(T(i).^3)/4 + c12.*(T(i).^4)/5 + c13/T(i));
S(i) =  R*(c8.*log(T(i)) + c9.*T(i) + c10.*(T(i).^2)/2 + c11.*(T(i).^3)/3 + c12.*(T(i).^4)/4 + c14);
  else
  Cp(i) = R*(c1 + c2.*T(i) + c3.*T(i).^2 + c4.*T(i).^3 +c5.*T(i).^4);
H(i) = R*T(i)*(c1 + c2.*(T(i))/2 + c3.*(T(i).^2)/3 + c4.*(T(i).^3)/4 + c5.*(T(i).^4)/5 + c6/T(i));
S(i) =  R*(c1.*log(T(i)) + c2.*T(i) + c3.*(T(i).^2)/2 + c4.*(T(i).^3)/3 + c5.*(T(i).^4)/4 + c7);
    
  end 
end

% providing path for saving data
mkdir(['C:\Users\aswin\Desktop\New folder\NASA PROJECT'],species);
% going in to the folder of the species to save the plots
cd(species);
% plotting
%plot1  specific heat vs temperature 
figure(1)
plot(T,Cp)
grid on
xlabel('Temperature')
ylabel('specific heat')
title(['Specific heat vs temperature graph of ',species])
saveas(figure(1),'Cp.png')

% plot 2 specific heat vs temperature 
figure(2)
plot(T,H)
grid on
xlabel('Temperature')
ylabel('Enthalpy')
title(['Enthalpy vs temperature graph of ',species])
saveas(figure(2),'H.png')
% plot 3 specific heat vs temperature 
figure(3)
plot(T,S)
grid on
xlabel('Temperature')
ylabel('Entropy')
title(['Entropy vs temperature graph of ',species])
saveas(figure(3),'S.png')

% calculating molecular wieght of species
elements = [ 'H','O','C','N','S','A'];
Atomic_wt = [ 1 16 12 14 32 40 ];
mw = 0;
for i = 1:length(species)
    for j = 1:length(Atomic_wt)
     if  strcmp(species(i),elements(j)) == 1
         mw = Atomic_wt(j) + mw;
        
         f = j;
     end
    end
    n = str2num(species(i));
    if n>1
        mw = Atomic_wt(f)*(n-1) + mw;
    end
end
% To print the molecular wieght of the element
fprintf('molecular wieght of %s is %d\n',species,mw)


cd ..
end
%closing the file
fclose(f1)


         

SYNTAX USED DURING CODING

Fopen : open a file or obtain information about open files.

‘r’ : read the file.

Fgetl : read line from file.

str2split : split string or character vectors at specified delimiter.

str2num : convert character array to numeric array.

strfind : find one string within onother.

linspace : Generally linearly spaced vectors.

fprintf : write formatted data to file.

mkdir : make new directory.

cd : change working directory.

saveas : to save every figures.

CODE EXPLANATION

  • First command window and workspace are bieng cleared and closed.
  • To start with we open the thermodynamic file THERMO. Dat and the data in the file is bieng read with the help of 'fopen' command.
  • Following this we read the Heading of the file with help of 'fgetl' command reading the first line.
  • Whenever we use 'fgetl' command after this point, the command will read the next line. So using 'fgetl 'again we read the Global temperature and they are converted to number from string with the help of 'str2num'' command.
  • Using 'strsplit' command we extract the read global low, mid and high temperature given in the file.
  • At this point we need to skip next 3 lines and this is done by using 'fgetl' command again for 3 times.
  • Following this next we have to extract the coefficients which are specific for each species. This is done by using 'for' loop. We run the loop 53 times as we have 53 different species in the file.
  • Within the loop we read and exctracted the local temperature for each species. This is important as new can to calculate the thermodynamic properties. Using the local low, mid and high temperature.
  • Using 'findst'r command we specifically found out the number of exponiential in the first line of coefficients and accordingly extracted the number of coefficients.
  • This is done of the all the coefficients carrying line separately.
  • The coefficients which are bieng extracted are in string form and are converted to number with 'str2num' command
  • Once we extracted all rhe coefficients next we have to calculate the thermodynamic properties ie specific heat(Cp), Enthalpy (H), Entropy(S). We calculated these properties with the formula given above and substituted the extracted coefficients in the formula.
  • The first 7 coefficients are high temp.Coefficients and the next 7 are low temp. Coefficients. So to calculate the properties specifically for high temperature and loe temperature again for loop is used and distinguished using if condition statement.
  • The value of gas constant is taken as 8.314 and temperature range is taken from low temperature to high temperature with 100 valued between them.
  • Following the calculation of the thermodynamic properties, the current directory is changed to the new directory which will be made for each species with that specific name.
  • Now we will plot the graphs specific heat vs Temperature Enthalpy vs Temperature and Entropy vs Temperature for each species using plot command. These plots are saved and dumped in the separate directory folder with their name as mentioned in the last step.
  • 'Cd' command is used to get back in the orginal directory.
  • Following the calculation of the thermodynamic properties, Molecular wieght of each species is calculated.

And finally we close the THERMO.dat file using fclose command

RESULTS

THE SCREENSHOT OF ALL FOLDERS CREATED THROUGH THE PROGRAM CONTAINING THE CP-T ,H-T , S-T PLOTS FOR THE ELEMENT IS SHOWN BELOW:

When we open folders the we will find 3 matlab figures as shown :

THE PLOT FOR O2 IS SHOWN BELOW:

SPECIFI HEAT VS TEMPERATURE GRAPH

ENTHAPLY VS TEMPERATURE GRAPH

ENTROPY VS TEMPERATURE GRAPH

THE PLOT FOR N2 IS SHOWN BELOW:

SPECIFI HEAT VS TEMPERATURE GRAPH

ENTHAPLY VS TEMPERATURE GRAPH

ENTROPY VS TEMPERATURE GRAPH

THE PLOT FOR CO2 IS SHOWN BELOW:

SPECIFI HEAT VS TEMPERATURE GRAPH

ENTHAPLY VS TEMPERATURE GRAPH

ENTROPY VS TEMPERATURE GRAPH

THE SCREENSHOT OF THE COMMAND WINDOW SHOWING THE MOLECULAR WIEGHT OF ALL THE ELEMENTS IS SHOWN BELOW:

CONCLUSION

Thus the file parsing of the NASA Thermodynamic data was done . Also the plot of Temperature vs Specific heat ,Enthalpy,Entropy was obtained and the plots were also saved in different folders with their respective names .Also we have calculated the molecular weight of different species and shown them in the command window .After observing these plots ,We understand that the specific heat of any species is not a constant value,it depends on the temperature . Generally,we use constant value for our calculation,but we conclude that the Specific Heat ,Enthalpy,Entropy are temperature-dependent variables . NASA has determined these polynomial values to calculate these properties of each species.

REFERENCES

https://in.mathworks.com/help/matlab/ref/.html

https://www.grc.nasa.gov/www/k-12/airplane/enthalpy.html

https://courses.lumenlearning.com/physics/chapter/15-6-entropy-and-the-second-law-of-thermodynamics-disorder-and-the-unavailability-of-energy/

https://courses.lumenlearning.com/boundless-physics/chapter/specific-heat/

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.