Challenge

Project 1 - Parsing NASA thermodynamic data

Aim:

To parse the Nasa Thermodynamic data and calculate specific heat,Enthalpy and Entropy values

Objectives:

  • 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.
  • 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.

Theory:

File parsing:

Parsing a file means reading in a data stream and building a memory model of the content of that data. This aims at facilitating perfoming some kind of transformation on the data. Parsing splits a file or other input into pieces of data that can be easily stored or manipulated.

Specific heat:

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.

cp = specific heat

R = universal gas constant

T =temperature

Enthalpy:

Enthalpy is the measurement of energy in a thermodynamic system. The quantity of enthalpy equals to the total content of heat of a system, equivalent to the system’s internal energy plus the product of volume and pressure.

H = Enthalpy

R = universal gas constant

T =temperature

Entropy:

The entropy of an object is a measure of the amount of energy which is unavailable to do work. Entropy is also a measure of the number of possible arrangements the atoms in a system can have. In this sense, entropy is a measure of uncertainty or randomness.

S = Entropy

R = universal gas constant

T =temperature

File Parsing program:

clear all    % clear work space
close all    % close plot window
clc          % clear command window

f1 = fopen('THERMO.dat','r')     % fopen to get the data
fgetl(f1);                       % fgetl to read the line
tline = fgetl(f1);

s = strsplit(tline,' ');         % strsplit is to split the string 

global_low_temp = str2num(s{2});   % convert  string to number as low temp 
global_mid_temp = str2num(s{3});   % convert  string to number as mid temp
global_high_temp = str2num(s{4});  % convert  string to number as high temp

for i = 1:3
    g = fgetl(f1);      % using for loop to neglect 3 lines
end



% Reading the  53 species

for j =1:53;       % start to reading the 53 species using for loop
   
 
   line1 =fgetl(f1);             % line1 is get first line of species
   s = strsplit(line1,' ');      % split to string value
   local_low_temp = str2num(s{end-3});   % convert str2num and get low temp
   local_mid_temp = str2num(s{end-1});   % convert str2num and get high temp
   local_high_temp = str2num(s{end-2});  % convert str2num and get mid temp
     species_name = strtok(line1);       % get species name 
    



    line2 = fgetl(f1);          % read line2 
    A = strfind(line2,'E');     % get string value called as A

    line3 = fgetl(f1);          % read line3 
    B = strfind(line3,'E');     % get string value called as B

    line4 = fgetl(f1);          % read line4 
    C = strfind(line4,'E');     % get string value called as C

% convert string to number(line2,3,4)

a1 = str2num(line2(1:A(1)+3));         % connvert string to number and get 
a2 = str2num(line2(A(1)+4:A(2)+3));      % value 'a1,a2,a3,a4,a5,a6,a7'
a3 = str2num(line2(A(2)+4:A(3)+3));      % value  'b1,b2,b3,b4,b5,b6,b7'
a4 = str2num(line2(A(3)+4:A(4)+3));
a5 = str2num(line2(A(4)+4:A(5)+3));
a6 = str2num(line3(1:B(1)+3));
a7 = str2num(line3(B(1)+4:B(2)+3));

b1 = str2num(line3(B(2)+4:B(3)+3));
b2 = str2num(line3(B(3)+4:B(4)+3));
b3 = str2num(line3(B(4)+4:B(5)+3));
b4 = str2num(line4(1:C(1)+3));
b5 = str2num(line4(C(1)+4:C(2)+3));
b6 = str2num(line4(C(2)+4:C(3)+3));
b7 = str2num(line4(C(3)+4:C(4)+3));

% input

T =linspace(local_low_temp,local_high_temp,500); %T-take linspace 500
R = 8.314;  % universal constant value5




% Calculate specfic heat 

if T>local_mid_temp  % using if else condition(T>mid temp take high temp cp)

c_p = (R*(a1+a2*T)+(a3*T.^2)+(bar*T.^3)+(a5*T.^4)+(a6*T.^5)+(a7*T.^6));
  
else                 % T<mid temp take low temp of cp

c_p = (R*(b1+b2*T)+(b3*T.^2)+(b4*T.^3)+(b5*T.^4)+(b6*T.^5)+(b7*T.^6));

end

% calculate Enthalpay

if T>local_mid_temp  % using if else condition(T>mid temp take high temp H)

    H = (R*T.*(a1+a2*(T/2))+(a3*(T.^2/3))+(a4*(T.^3/4))+(a5*(T.^4/5))+(a6*(T.^5/6))+(a7*(T.^6/7)));

else          % T< mid temp take low temp of H


   H = (R*T.*(b1+b2*(T/2))+(b3*(T.^2/3))+(b4*(T.^3/4))+(b5*(T.^4/5))+(b6*(T.^5/6))+(b7*(T.^6/7)));

end

% calculate Entropy

if T>local_mid_temp  % using if else condition(T>mid temp take high temp S)

    S = (R*(a1*log(T))+(a2*T)+(a3*(T.^2/2))+(a4*(T.^3/3))+(a5*(T.^4/4))+(a6*(T.^5/5))+(a7*(T.^6/6)));

else     % T< mid temp take low temp of S

    S = (R*(b1*log(T))+(b2*T)+(b3*(T.^2/2))+(b4*(T.^3/3))+(b5*(T.^4/4))+(b6*(T.^5/5))+(b7*(T.^6/6)));

end

% calculate Molecular weight

element =['O' ,'H', 'C', 'N', 'A'];  % elements are used in data
atomic_mass = [15,1,12,14,39];       % weight os this element

mw = 0;                             % initially consider mw =0
for i = 1:length(species_name)      % i = species_name
    for j = 1:length(element)       % j = element

         if strcmp(species_name(i),element(j)) % using if condition to compare

        mw = mw+atomic_mass(j);  %   calculate mw

    end
    end

    n = str2num(species_name);   % n= consider as string comnvert 2 num
if n>1                           % using if condition

mw = mw+atomic_mass(position)*(n-1);  % get mw value

end
end
fprintf('Molecular weight of %s =',species_name) %print output
fprintf('%f',mw)              % print mw value
disp('  ')                   % print in column



% plotting   

 cd('C:\Users\Ahmed\Desktop\project\nasa');  % cd create directory
 dir_name = species_name;        % directory name as 'species name
 mkdir(dir_name);                % make directory 'dir_name'


figure(1)             %(cp take as 1 plot)
plot(T,c_p,'linewidth',3,'Color','g')  %(plotbetweeen T vs cp, color green)
title(sprintf('specfic heat vs Temperature %s',species_name)) % title
xlabel('Temperature')   % xlabel name
ylabel('specific heat')  % ylabel name
grid                     % grid on
 
figure(2)
plot(T,H,'linewidth',3,'Color','b') 
title(sprintf('Enthalpy vs Temperature %s',species_name))
xlabel('Temperature')
ylabel('Enthalpy')
grid


figure(3)
plot(T,S,'linewidth',3,'Color','r') 
title(sprintf('Entropy vs Temperature %s',species_name))
xlabel('Temperature')
ylabel('Entropy')
grid

 cd(dir_name);      % save to dir name
 saveas(1,'specfic heat.png');  % save cp.png file in dir_name
 saveas(2,'Enthalpy.png');      % save H.png file in dir_name
 saveas(3,'Entropy.png');       % save S.png file in dir_name


end                    % end the for loop

fclose(f1)             % close the data





Main program Explain:

  • clear work space,command window and plot window.
  • Then using fopen to read the nasa data file.
  • fgetl(f1) command to read the first line of the nasa data.
  • tline take as fgetl(f1).
  • strfind(tline,' ') to get global temperature and convert str2num to get low,mid,high temp.
  • After using for loop to neglect next three lines for i = 3:5.
  • Going to read the species conituously using for loop.
  • Take for i= 1:53==read 53 species.
  • Line1=fgetl(f1) -- is read the line 6.
  • s = strfind(line1,' ') -- split the string value.
  • local_low_temp = str2num(s{end-3}) --using the command to convet str2num and get local low temp
  • local_mid_temp = str2num(s{end-1})-- using the command to convet str2num and get local mid temp.
  • local_high_temp = str2num(s{end-2})-- using the command to convet str2num and get local mid temp.
  • strtok(line1)--to get the species name.
  • line2 = fgetl(f1)--read the line 7.
  • A = strfind(line2,'E')-- using command to get the coeffficient value.
  • similarly using line3 and line4 and get 14 coefficient value.
  • Then converting string to number.
  • a1 = str2num(line2(1:A(1)+3))--get 14 coefficient and convert to number using this command.
  • 'Then get all the 14 values (a1 to b7)
  • T =linspace(local_low_temp,local_high_temp,500)--T take as temperature.
  • R =8.314 take as universal contant.
  • Calculate Specific heat:
  • calculate Enthalpay--- Using the respective formula and using if else condition to get the Enthalpy value.
  • Calculte Entropy--Using the respective formula and using if else condition to get the Entropy value.
  • Then calculate Molecular weight:
  • Then display the molecular weight value i attached inthe results.
  • Plotting:
  • cd( )---create new folder where you want
  • dir_name --take as species name.
  • mkdir--same as dir name.
  • figure(1) --consider as cp plot
  • plot (T,cp)-- get plot.
  • title ---get title
  • xlabel--x axis name
  • ylabel--y axis name
  • Similary give to figur(2) and figure(3)--get plot for(T,H) and plot(T,S)
  • cd(dir_name)--save as directory file.
  • Saveas (1,'cp'.png)
  • saveas (2,'H'.png)
  • saveas (3,'S'.png)
  • End----for loop
  • fclose(f1)--close nasa data file.

Results:

Save plots individual folder:

Plots for O2:

Plots for N2:

Plots for CO2:

Molecular weight calculated:


f1 =

252

Molecular weight of O =15.000000
Molecular weight of O2 =15.000000
Molecular weight of H =1.000000
Molecular weight of H2 =1.000000
Molecular weight of OH =16.000000
Molecular weight of H2O =16.000000
Molecular weight of HO2 =16.000000
Molecular weight of H2O2 =16.000000
Molecular weight of C =12.000000
Molecular weight of CH =13.000000
Molecular weight of CH2 =13.000000
Molecular weight of CH2(S) =13.000000
Molecular weight of CH3 =13.000000
Molecular weight of CH4 =13.000000
Molecular weight of CO =27.000000
Molecular weight of CO2 =27.000000
Molecular weight of HCO =28.000000
Molecular weight of CH2O =28.000000
Molecular weight of CH2OH =29.000000
Molecular weight of CH3O =28.000000
Molecular weight of CH3OH =29.000000
Molecular weight of C2H =13.000000
Molecular weight of C2H2 =13.000000
Molecular weight of C2H3 =13.000000
Molecular weight of C2H4 =13.000000
Molecular weight of C2H5 =13.000000
Molecular weight of C2H6 =13.000000
Molecular weight of CH2CO =40.000000
Molecular weight of HCCO =40.000000
Molecular weight of HCCOH =41.000000
Molecular weight of H2CN =27.000000
Molecular weight of HCN =27.000000
Molecular weight of HNO =30.000000
Molecular weight of N =14.000000
Molecular weight of NNH =29.000000
Molecular weight of N2O =29.000000
Molecular weight of NH =15.000000
Molecular weight of NH2 =15.000000
Molecular weight of NH3 =15.000000
Molecular weight of NO =29.000000
Molecular weight of NO2 =29.000000
Molecular weight of HCNO =42.000000
Molecular weight of HOCN =42.000000
Molecular weight of HNCO =42.000000
Molecular weight of NCO =41.000000
Molecular weight of CN =26.000000
Molecular weight of HCNN =41.000000
Molecular weight of N2 =14.000000
Molecular weight of AR =39.000000
Molecular weight of C3H8 =13.000000
Molecular weight of C3H7 =13.000000
Molecular weight of CH3CHO =41.000000
Molecular weight of CH2CHO =41.000000

Conclusion:

  • Sucessfully calculate the Specific heat, Enthalpy and Entropy, moreover the plots of O2,N2,CO2 completed.
  • Calulated the Molecular weight of the Species completed sucessfully.
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.