Challenge

Project 1 - Parsing NASA thermodynamic data

OBJECTIVE:

* The objective of the Project is to write a code to parse the NASA Thermodynamics data file

* To Calculate the specific heat ,Entropy,Enthalpy of various gas Species by using given coefficients

CODE FOR NASA PARSING:

Here comes the Main code for NASA parsing which helps to parse NASA data file , and to calculate the thermodynamics properties of various gases

This program is consist one main program and two calling function code,one function code calling the molecular weight and another one is to calculate the Entropy(S), Enthalpy(H),Specific heat of gases.

clear all
close all
clc

file_1=fopen('THERMO.dat','r')
%read header information
for i=1:5
  k = fgetl(file_1);
  if i == 2
    global_temp = k;
    end
  end

  % reading species block
  % line 1
 
for i=1:53
  line_1= fgetl(file_1);
  temp_line=strsplit(line_1,' ');%  splitting intocell arrayfun
  species_name = char(temp_line(1)) % to read the name of the speies
   
   % converting string into number
  
 L_T = str2double(temp_line{(length(temp_line)-3)});
 H_T = str2double(temp_line{(length(temp_line)-2)});
 M_T = str2double(temp_line{(length(temp_line)-1)});
 
 % line2 
 line_2 = fgetl(file_1);
 a=strfind(line_2,'E'); %finding the string E
   
 % finding first 7 higher and second 7 lower coefficients
 % finding higher coefficients
% coefficient1
a = strfind(line_2,'E');
a_1=line_2(1:a(1)+3);
coef_1=str2double(a_1);

% coefficient 2
a_2=line_2(a(1)+4:a(2)+3);
coef_2=str2double(a_2);

% coefficient 3
a_3=line_2(a(2)+4:a(3)+3);
coef_3=str2double(a_3);


% coefficient 4
a_4=line_2(a(3)+4:a(4)+3);
coef_4=str2double(a_4);

% coefficient 5
a_5=line_2(a(4)+4:a(5)+3);
coef_5=str2double(a_5);

% coefficient 6
% line 3

line_3 = fgetl(file_1);
a=strfind(line_3,'E'); %finding the string 
 
a_6=line_3(1:a(1)+3);
coef_6=str2double(a_6); 

% coefficient 7
a_7=line_3(a(1)+4:a(2)+3);
coef_7=str2double(a_7); 

% coefficient 8
a_8=line_3(a(2)+4:a(3)+3);
coef_8=str2double(a_8); 

% coefficient 9
a_9=line_3(a(3)+4:a(4)+3);
coef_9=str2double(a_9); 

% coefficient 10
a_10=line_3(a(4)+4:a(5)+3);
coef_10=str2double(a_10); 

% coefficient 11
% line 4

line_4 = fgetl(file_1);
a=strfind(line_4,'E'); %finding the string 
 
a_11=line_4(1:a(1)+3);
coef_11=str2double(a_11); 

% coefficient 12
a_12=line_4(a(1)+4:a(2)+3);
coef_12=str2double(a_12); 

% coefficient 13
a_13=line_4(a(2)+4:a(3)+3);
coef_13=str2double(a_13); 

% coefficient 14
a_14=line_4(a(3)+4:a(4)+3);
coef_14=str2double(a_14); 

% temperature
T = linspace(L_T,H_T,1000);
% calculate specific_heat(cp),enthalpy(H),entropy(s)

for k = 1:length(T)
[cp(k)]=specific_heat(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T(k),M_T);
 
  
[H(k)]=enthalpy(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T(k),M_T);


[s(k)]=entropy(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T(k),M_T);

end

molecular_weight=mol_mass(species_name);
sprintf('molecular_weight of %s = %f\n',species_name,molecular_weight)
% making directoryto create folder
mkdir(species_name)
cd(sprintf(species_name))

% plotting cp, H and S with temperature

% plot 1 cp vs T
figure(1) 
plot(T,cp,'linewidth',2,'color','b');
xlabel('Temperature(K)');
ylabel('specific heat(J/k kg )');
%title(sprintf('specific heat vs Temperature range of %s',species_name,mol_mass));
grid on
saveas(1,'specific heat vs Temperature.jpg');


% plot 2 S vs T
figure(2) 
plot(T,s,'linewidth',2,'color','r');
xlabel('Temperature(K)');
ylabel('Entropy(KJ/mol-K)');
%title(sprintf('Entropy vs Temperature range of %s',species_name,mol_mass));
grid on
saveas(2,'Entropy.jpg');


 % plot 3 H vs T
figure(3) 
plot(T,H,'linewidth',2,'color','r');
xlabel('Temperature(K)');
ylabel('Enthalpy(J/k)');
%title(sprintf('Enthalpy vs Temperature range of %s',species_name,mol_mass));
grid on
saveas(3,'Enthalpy.jpg');

% old folder
cd ..

% calculating the mole_mass

end

REPORT:

* Initially the file has been opened by using the command ‘fopen’.

* And then the data has been extracted from given file .

* Then fgetl command used to skip the three lines in the file

* The temperature has been stored in the form of string variable.

* The string variable cannot be used for calculation as they are in text format for octave

* The ‘str2double’ command is used to store the temperatures

* And then the lower temp,middle temp,higher temp has been used assigned

* To find the position of ‘E’ the ‘findstr’ command is used throughout the data and in all assigned coefficient.

* Then the temperature as been assigned using linspace command

* To make code easier two calling functions as been used

* One to calculate the S,H,Cp for all gases

* And another one is to calculate the molecular weight of all gases

* The value of universal gas constant R is 8.314 (J/MOL.K)

* Then the graphs are plotted for all gases

* The plots are saved by ‘mkdir’ command which is used to make a folder

* The cd command is used to change the current folder to create new folder

* Then the graph has been plotted between temperature and specific heat ,entropy and enthalpy

* And the molecular weighthas been calculated

FUNCTION CODE TO CALCULATE ENTROPY,ENTHALPYAND SPECIFIC HEAT

The reason for writing this calling function code is to understand the calculation of entropy,enthalpy,specific heat of various gases in easy way

* The Entropy,Enthalpy ,Specific heat has been calculated using the formula displayed below

* To generate the temperature ‘for loop’ is used

* And then the 'ifelse' is used to compare between the data between the data and the value which has been obtained

* And finally the values of entropy,enthalpy,specific heat has been calculated

for k = 1:length(T)
  
[cp(k)]=specific_heat(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T(k),M_T);
 
  
[H(k)]=enthalpy(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T(k),M_T);


[s(k)]=entropy(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T(k),M_T);
end

FORMULA TO CALCULATE THE SPECIFICHEAT,ENTROPY,ENTHALPY

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+a6/T

S/R = a1 lnT +a2 T+a3 T^2/2+ a4 T^3/3 + a5 T^4/4 + a7

% To calculate specific heat
function [cp]=specific_heat(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T,M_T)
   R = 8.314;
   
   % calculating specific_heat
   %calculating specific heat for higher and lower temperature respectively
   
   if T>M_T
     
    cp=(coef_1+coef_2*T+coef_3*T^2+coef_4*T^3+coef_5*T^4)*R;
   else
    cp=(coef_8+coef_9*T+coef_10*T^2+coef_11*T^3+coef_12*T^4)*R;
     
   end
   
   end
% To calculate entropy
function [s]=entropy(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T,M_T)
R = 8.314;  
  % calculating entropy
    % calculating entropy for higher and lower temperature respectively
    
    if T>M_T
       s=((coef_1*log(T))+(coef_2*T)+((coef_3*(T^2))/2)+((coef_4*(T^3))/3)+(coef_5*(T^4)/4)+coef_7); 
else
 s=((coef_8*log(T))+(coef_9*T)+((coef_10*(T^2))/2)+((coef_11*(T^3))/3)+(coef_12*(T^4)/4)+coef_14); 
   
       
    end
  end
% To calculate enthalpy
function [H]=enthalpy(coef_1,coef_2,coef_3,coef_4,coef_5,coef_6,coef_7,coef_8,coef_9,coef_10,coef_11,coef_12,coef_13,coef_14,T,M_T)
R  = 8.314;    
    % calculating enthalpy
    % calculating enthalpy for higher and lower temperature respectively
    
if T>M_T
H = (coef_1+(coef_2.*T/2)+ (coef_3*(T.^2)/3)+ (coef_4*(T.^3/4))+(coef_5*(T.^4)/5)+(coef_6/T))*R*T;
    
else
    
H = (coef_8+(coef_9.*T/2)+ (coef_10*(T.^2)/3)+ (coef_11*(T.^3/4))+(coef_12*(T.^4)/5)+(coef_13/T))*R*T;
end

end

FUNCTION CODE TO CALCULATE THE MOLECULAR WEIGHT

This calling function code helps us to calculate the molecular weight of various gases in the given NASA file

* To calculate the molecular weight is by assigning the atomic weight of the gases

* Same as calculating S,H,CP ‘for loop’ has been used for calculation

* Consider the mol_weight as 0 and apply a range of i=1:length (species_name),and

m=1:length (atomic_weight)

* And the command ‘strcmp’ is used to compare the atomic weight and molecular weight of all species obtained

* And then compare if(n>1) then the molecular weight of all gases obtained

% To calculate the mol_mass
function out = mol_mass(species_name)
   
% Defining Atomic name to the species
elements = ['H','C','O','N','A','S'];
    
%Defining Atomic masses to main species
atomic_weight=[1.008 12.011 15.999 14.007 39.948 32.065];
  
out = 0; % mocecular weight at starting range

for i=1:length(species_name) % loop runs to a length of species_name
   for m=1:length(atomic_weight) %loop runs to the length of atomic_weight
   if strcmp(species_name(i),elements(m)) % function to compare two string
     out = out+atomic_weight(m);
     position=m;
     end
   end
   n=str2double(species_name(i)); %  now we will find if there is more element in the species
   if n>1
     out = out+(atomic_weight(position))*(n-1);
   end
 end
 
 end

PLOTS BETWEEN TEMPERATURE (T) vs SPECIFIC HEAT (cp) O2,CO2,N2

O2:

CO2:

N2:

PLOTS BETWEEN TEMPERATURE (T) vs ENTHALPY (H) O2,CO2,N2

O2:

CO2:

N2:

PLOTS BETWEEN TEMPERATURE (T) vs ENTROPY (S) O2,CO2,N2

O2:

CO2:

N2:

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.