Challenge

Project 1 - Parsing NASA thermodynamic data

AIM - To write the code on Parsing NASA thermodynamic data file and then calculate the thermodynamic properties of various gas species.

OBJECTIVE

  • Write a function that extracts the 14 coefficients and calculates the enthalpy, entropy, and specific heats for all the species in the data file.
  • The formulas are

where,

R is the universal gas constant.

T is the temperature.

  • Calculate the molecular weight of each species and display it in the command window.

  • Plot the Cp, Enthalpy, and Entropy for the local temperature range (low temperature: high temperature) specific for each species.

  • Save the plots as images with appropriate names and dump them in separate folders for each species with a suitable name. All these should be generated automatically.

  • The main program that you will submit will just take the "THERMO.dat" file as an input and generate Cp, H, and S plots.

  • Please write a detailed report to describe your logic.

FILE PARSING

It refers to how information can be read from a particular text file and the information used in computer programs.

STEPS OF FILE PARSING

  1. Define the function
  2. Create an input parser object
  3. Add inputs to the scheme
  4. Set properties to adjust parsing
  5. parse the input
  6. Use the input in the function
  7. Calling function

NASA THERMODYNAMIC FILE

It is a data file that contains 14 coefficients for 53 different elements given by NASA. These are utilized to calculate the specific heat, enthalpy & entropy for a particular element. Each element has it own range of local temperature.

Thermodynamic data file :

ENTHALPY, ENTROPY & SPECIFIC HEAT

  • Enthalpy is defined as follows H = E + PV. Enthalpy is the sum of the internal energy. and pressure times volume. We cannot measure the enthalpy of a system, but we can look at changes in enthalpy.
  • The measure of a system's thermal energy per unit temperature that is unavailable for doing useful work. Because work is obtained from ordered molecular motion, the amount of entropy is also a measure of the molecular disorder, or randomness, of a system.
  • Specific heat is defined by the amount of heat needed to raise the temperature of 1 gram of a substance 1 degree Celsius (°C). Water has a high specific heat, meaning it takes more energy to increase the temperature of water compared to other substances.

CODING

ENTHALPY

% Enthaphy Calculation Function

function H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,local_medium_temp)
% Comparing with local Temperature
% %If less than local temperature - minimum coffient are take
for i = 1:length(Temp)
if Temp(i) <= local_medium_temp
H(i) = R*Temp(i).*((a8 + (a9*Temp(i)/2)) + (a10*(Temp(i)^2)/3) + (a11*(Temp(i)^3)/4) + (a12*(Temp(i)^4)/5) +  (a13./Temp(i)));

% Else maximum coefficient are taken
else
H(i)= R*Temp(i).*((a1 + (a2*(Temp(i)/2)) + (a3*(Temp(i)^2)/3) + (a4*(Temp(i)^3)/4) + (a5*(Temp(i)^4)/5) + (a6./Temp(i))));
end
end

ENTROPY

%Entropy Calculation Function
function s = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,local_medium_temp)

%comparing with local tempreature
%If less than local temperature - minimum coffient are taken
for i = 1:length(Temp)
if Temp(i) >= local_medium_temp
    s(i) = R.*((a1.*log(Temp(i)))+(a2.*Temp(i))+(a3*((Temp(i).^2)/2))+(a4*((Temp(i).^3)/3))+(a5*((Temp(i).^4)/4)+a7));
%s(i) = R.*(a8.*log(Temp(i)))+(a9.*Temp(i))+(a10*(Temp(i).^2)/2)+(a11*(Temp(i).^3)/3)+(a12*((Temp(i).^4)/4)+a14);
else
    %Else - maximum coffient are taken
    s(i) = R.*((a8.*log(Temp(i)))+(a9.*Temp(i))+(a10*(Temp(i).^2)/2)+(a11*(Temp(i).^3)/3)+(a12*((Temp(i).^4)/4)+a14));
    %s(i) = R.*(a1.*log(Temp(i)))+(a2.*Temp(i))+(a3*((Temp(i).^2)/2))+(a4*((Temp(i).^3)/3))+(a5*((Temp(i).^4)/4)+a7);
    
end
end

SPECIFIC HEAT

function cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,local_medium_temp)
%comparing with global tempeature
%If less than global temperature - minimum coffient are taken
for i = 1:length(Temp)
if Temp(i)<= local_medium_temp
cp(i) = R*(a8+(a9*Temp(i))+(a10*(Temp(i)).^2)+(a11*(Temp(i)).^3)+(a12*(Temp(i)).^4));
else
    cp(i) = R*(a1+(a2*Temp(i))+(a3*(Temp(i)).^2)+(a4*(Temp(i)).^3)+(a5*(Temp(i)).^4));
    %else maximam coffient are taken
end
end

MOLECULAR WEIGHT

% Molecular weight calculating function

function Molecularweight = Molecular_weight(Species_SP)
%standard - Atomic Name
atomic_name = ['H','C','O','N','A'];
% Hydrogen,Carbon,Oxygen,Nitrogen,Argon

fprintf('Molecular Weight of')
fprintf(Species_SP);
fprintf(':');
%standerd atomic weight
Atomic_Weight = [1.008 12.011 15.999 14.007 39.948];
% starting range of molecule
Molecularweight = 0;
% Adding Atomic number to obtain particular value
for i = 1:length(Species_SP)
    
    for j = 1:length(atomic_name)
        if strcmpi(Species_SP(i),atomic_name(j))
            Molecularweight = Molecularweight + Atomic_Weight(j);
            positon=j
        end
        
    end
    n=str2double(Species_SP(i))
    if n>1
     Molecularweight = Molecularweight + Atomic_Weight(positon)*(n-1)
    fprintf('molecular_weight :',Species_SP);
    fprintf('%d',Molecularweight);
    disp('');
            
end  
end


MAIN CODE

clear all
close all
clc

%Reading data file - Nasa Thermodynamic
%Open a file & 'r' Open thefile for reading
f1=fopen('THERMO.dat','r');
gt = fgetl(f1);

%Obtaining temperatures - by reading headers information
local_temp=fgetl(f1);

%spliting
t=strsplit(local_temp);


%Universal gas Constant J/(mol-K)
R = 8.314;

%skipping three comment Liners in the file
for i = 1:3;
    gt=fgetl(f1);    
end

for i = 1:53;
    gt=fgetl(f1);
    A=strsplit(gt,' ');
    Species_SP=(A{1});
    len=length(A);
    local_low_temp=str2num(A{len-3});
    local_high_temp=str2num(A{len-2});
    local_medium_temp=str2num(A{len-1});
    Temp = linspace(local_low_temp,local_high_temp,1000);
    line_1=fgetl(f1);
    a=strfind(line_1,'E');
    
    %Extrating higher and lower temperature coefficient
    %%Extracting 1st 7 coefficients i.e High-temperature Coefficient
    
   a1=line_1(1:a(1)+3);
   a1=str2num(a1);
   a2=line_1(a(1)+4:a(2)+ 3);
   a2=str2num(a2);
   a3=line_1(a(2)+4:a(3)+ 3);
   a3=str2num(a3);
   a4=line_1(a(3)+4:a(4)+ 3);
   a4=str2num(a4);
   a5=line_1(a(4)+4:a(5)+ 3);
   a5=str2num(a5);
    
   line_2=fgetl(f1);
   b=strfind(line_1,'E');
   a6=line_2(1:b(1)+3);
   a6=str2num(a6);
   a7=line_2(a(1)+4:b(2)+3);
   a7=str2num(a7);
   
   %Extracting  Second 7 coefficient - Low-Temperature coefficient
   
   a8=line_2(b(2)+4:b(3)+3);
   a8=str2num(a8);
   a9=line_2(b(3)+4:b(4)+3);
   a9=str2num(a9);
   a10=line_2(b(4)+4:b(5)+3);
   a10=str2num(a10);
   
   
   line_3=fgetl(f1);
   c=strfind(line_1,'E');
   
   a11=line_3(1:c(1)+3);
   a11=str2num(a11);
   a12=line_3(c(1)+4:c(2)+3);
   a12=str2num(a12);
   a13=line_3(c(2)+4:c(3)+3);
   a13=str2num(a13);
   a14=line_3(c(3)+4:c(4)+3);
   a14=str2num(a14);
   
   %Specific heat Calculation
   cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,Temp,R,local_medium_temp)
   
   %enthalpy calculation
    H = Enthalpy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,Temp,R,local_medium_temp)
   
   %entropy
   s = Entropy_calc(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,Temp,R,local_medium_temp)
    
    %calculation molecular weight of species
     Molecularweight = Molecular_weight(Species_SP);
     
     %read and write 
     
     Molecular_list = fopen('Molecular_weight.txt','w');
     fprintf(Molecular_list,'Molecular weight of %s is %d ',Species_SP,Molecularweight);
     fclose(Molecular_list);
     
     %plotting(CP,H,S,Temp,Species_SP)
     %Saving as a folder
     
     mkdir(['C:\Users\Hp\Desktop\fileparsing\plots\',Species_SP]);
     cd(['C:\Users\Hp\Desktop\fileparsing\plots\',Species_SP]);
     
     
     %Plot 1,Specific heat (CP) vs Temperature Range (Temp)
     figure(1)
     plot(Temp,cp,'linewidth',2,'color','r');
     xlabel('Temperature [K]');
     ylabel('Specific Heat [kj]');
     title(sprintf('Varing Specific Heat with Temperature Range of %s',Species_SP));
     grid on
     saveas(figure(1),'Specific Heat.jpg');
     
     %Plot 2, Enthaphy (H) vs Temperature Range (Temp)
     
     figure(2)
     plot(Temp,H,'LineWidth',2,'Color','g');
     xlabel('Temperature [K]');
     ylabel('Enthaphy [kJ/(mol)]');
     title(sprintf('Varing Enthaphy with Temperature Range of %s',Species_SP));
     grid on
     saveas(figure(2),'Enthaphy.jpg');
     
     %Plot 3, Entropy (s) vs Temperature Range (Temp)
     
     figure(3)
     plot(Temp,s,'LineWidth',2,'Color','g');
     xlabel('Temperature [K]');
     ylabel('Entropy [kJ/(mol-K)]');
     title(sprintf('Varing Entropy with Temperature Range of %s',Species_SP));
     grid on
     saveas(figure(3),'Entropy.jpg');
     
     
     
     M = Molecular_weight(Species_SP)
     f2 = fopen('molecular mass','w')
     fprintf('%s n %s n','species','Molecular mass','M')
     fclose(f2)
        
   
end

CODE EXPLANATION:

  1. clear all = clear all the variables on the workspace
  2. close all = close all the plots and fig
  3. clc = clear the command window
  4. making a directory using 'mkdir' with the location address
  5. Reading text file 'Thermo.dat' file and open it with 'fopen' and read it with 'r' command.
  6. for reading a line in a text file using 'fgetl'
  7. To obtain the temperature range from thermo.dat file using 'strsplit' to split the local temp into a particular string cell.
  8. Using 'str2num' to convert a string into numbers.
  9. Divide the temp into a string and convert it to a number so that temp can be used separately to solve the NASA thermodynamic data.
  10. Using 'for' & 'end' loops to skip the next three comments line in the text file.
  11. Reading all the 53 species to plot specific heat (Cp), enthalpy(H) and entropy (s), with temp values,and to find the molecular weight.
  12. Use for loop to read all 53 species.
  13. Use 'strfind' to find the string.
  14. Using 'linspace' specified the temp range from low to high.
  15. Extracting higher and lower temp coefficients by reading line by line from the species.
  16. Calling specific heat,enthaphy and entropy function codes.
  17. Calculating Molecular weight.

SYNTEX USED DURING CODE:

  1. fopen :
  2. r :
  3. fgetl :
  4. strsplit :
  5. str2num:
  6. strfind :
  7. linspace :
  8. fprintf :
  9. pwd :
  10. mkdir :
  11. cd :

ATTACHMENTS:

Screenshot of the window where all the folders are saved :

\

RESULTS:

PLOTS:

1. SPECIFIC HEAT VS TEMPERATURE:

2.ENTHAPHY VS TEMPEATURE:

3.ENTROPY VS TEMPERATURE:

SCREENSHOTS OF THE COMMAND WINDOW WHICH CONTAINS THE MOLECULAR WEIGHT OF ALL SPECIES:

SOME EXAMPLES OF COMMAND WINDOWS (CROPPED):

:

SCREENSHOT OF THE FOLDER CONTAINING ALL THE COMMAND WINDOW'S SCREENSHOT:

ERROR:

  1. In writing the formulae, more brackets were used due to which output was not accurate.
  2. Making in a directory file.
  3. Instead of using local temp, I used global temp.
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.