Challenge

Project 1 - Parsing NASA thermodynamic data

AIM: We have to perform the following operations in this challenge:

  • To write a function that extracts the 14 co-efficients and calculates the enthalpy, entropy,and specific heat for the given species.
  • To calculate the molecular weight of each species and display it in the command window.
  • To plot the Cp , enthalpy and entropy for local temperature range (low temp.: high temp.) for each species specifically.
  • Save the plots as images and dump them in separate folders.

INTRODUCTION: Nasa has came up with certain polynomials that can be used to calculate various thermodynamic properties like entropy, enthalpy and specific heat.The thermodynamic properties in terms of coefficients are given below:

PROCEDURE:

  • Initially, we have to read the NASA Thermodynamic Data 'Thermo.dat' file by using fopen command.
  • Then, we have used 'fgetl' command to navigate through the data file.
  • The header information is then extracted from the data file.
  • The temperature '300 1000 5000' was stored in a single variable as global low ,mid and high temperature values with the help of 'strsplit' command to split the string and 'str2num ' command was used to convert these strings to numbers.
  • Then the next three lines were skipped using 'fgetl' command.
  • The 14 co-efficients were calculated by using 'for' loop for the given 53 species.
  • After calculation of the 14 coefficients, the respective calculations of entropy, enthalpy and specific heat is done.
  • After getting the required values of H, S and Cp our next step is to plot the graphs between 'Temperature and Entropy', 'Temperature and Enthalpy' and 'Temperature and specific heat' and then save these plots in separate folder .
  • Also, we have to calculate the molecular weight of each species by using function command and displaying them in command window.

Matlab Code:

%NASA file parsing program
clear all
close all
clc
%Opening the file
f1= fopen('THERMO.dat','r')
header= fgetl(f1)
%Obtaining global temperature values
global_temp= fgetl(f1)
%saving the values
X=strsplit(global_temp,' ')
R= 8.314;
%Converting string to numbers
gt_low= str2num(X{2})
gt_mid= str2num(X{3})
gt_high= str2num(X{4})

%Ignoring the next three lines
fgetl(f1)
fgetl(f1)
fgetl(f1)
%Getting coeff. of each species
for i= 1:53
line1=fgetl(f1);

species_name=line1(1:9);
species_name= deblank(species_name);
%Extraxting local temperatures
species_temp= line1(49:75);
local_values= strsplit(species_temp,' ');
low_temp_1= str2num(local_values{1});
mid_temp_1= str2num(local_values{2});
high_temp_1= str2num(local_values{3});
%Storing the local temp. in a variable
temp= [low_temp_1 mid_temp_1 high_temp_1];

temp_range= linspace(low_temp_1,high_temp_1,500);
coeff=fgetl(f1);
coeff1= findstr(coeff,'E');

%Extracting the coefficients
a1= coeff(1:coeff1(1)+3);
a1= str2num(a1);
a2=coeff(coeff1(1)+4:coeff1(2)+3);
a2= str2num(a2);
a3= coeff(coeff1(2)+4:coeff1(3)+3);
a3= str2num(a3);
a4= coeff(coeff1(3)+4:coeff1(4)+3);
a4= str2num(a4);
a5= coeff(coeff1(4)+4:coeff1(5)+3);
a5= str2num(a5);

new_line= fgetl(f1);
coeff2= findstr(new_line,'E');
a6=new_line(1:coeff2(1)+3);
a6= str2num(a6);
a7= new_line(coeff2(1)+4:coeff2(2)+3);
a7= str2num(a7);
a8= new_line(coeff2(2)+4:coeff2(3)+3);
a8= str2num(a8);
a9= new_line(coeff2(3)+4:coeff2(4)+3);
a9= str2num(a9);
a10= new_line(coeff2(4)+4:coeff2(5)+3);
a10= str2num(a10);

new_line1= fgetl(f1);
coeff3=  findstr(new_line1,'E');
a11= new_line1(1:coeff3(1)+3);
a11= str2num(a11);
a12= new_line1(coeff3(1)+4:coeff3(2)+3);
a12= str2num(a12);
a13= new_line1(coeff3(2)+4:coeff3(3)+3);
a13= str2num(a13);
a14= new_line1(coeff3(3)+4:coeff3(4)+3);
a14= str2num(a14);
%Specific heat calculatiion
Cp= specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,temp,temp_range,R);
%Calculation of enthalpy
H= enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,temp,temp_range,R);

%Calculation of entropy
[S]=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temp_range,R);

%Calculating molecular weight
[M]= molecular_weight(species_name);
%Saving molecular weight
species_weight=fopen('species_mass.txt','w');
fprintf(species_weight,'molecular mass of %s is %f',species_name,M);
fclose(species_weight);



%Making plots and saving it
cur_dir= pwd;
%Creating a new  folder for each species
mkdir(species_name);
cd(species_name);

figure(1)
plot(temp_range,Cp,'linewidth',4,'color','g')
xlabel('Temperature');
ylabel('Specific Heat');
title('species_name');

figure(2)
plot(temp_range,H,'linewidth',4,'color','r')
xlabel('Temperature');
ylabel('Enthalpy');
title(species_name);

figure(3)
plot(temp_range,S,'linewidth',4,'color','b')
xlabel('Temperature');
ylabel('Entropy');
title(species_name);

%Saving the plots
saveas(1,'specific heat.jpg');
saveas(2,'enthalpy.jpg');
saveas(3,'entropy.jpg');

cd(cur_dir);
%Print molecular_weight of species
sprintf('molecular_weight of%s= %f',species_name,M)
end
fclose(f1)

Matlab function code for calculating molecular weight:

%Program to calculate molecular weight
function [M]= molecular_weight(species_name)

elements= ['H','C','N','O','A'];
Atomic_weight= [1,12,14,16,40];
M=0;
for i=1:length(species_name)
 for j= 1:length(elements)
     if strcmp(species_name(i),elements(j))
      M= M+Atomic_weight(j);
      x=j;
     end
 end
 n=str2num(species_name(i));
 if n>1
 M= M+(Atomic_weight(x)*(n-1));
 end
 end
 end

Function for calculation of Specific heat

function[Cp]= specific_heat(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,temp,temp_range,R)
for i= 1:length(temp_range)
if temp_range(i)<=temp(2)
    Cp(i)= R*(a8+(a9*temp_range(i))+(a10*(temp_range(i).^2))+(a11*(temp_range(i).^3))+(a12*(temp_range(i).^4)));
    else
       Cp(i)= R*(a1+(a2*temp_range(i))+(a3*(temp_range(i).^2))+(a4*(temp_range(i).^3))+(a5*(temp_range(i).^4)));
       end
      end
     

Function for calculation of Entropy

function[S]= entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,temp,temp_range,R)
%a1 to a5 are high temp. coeff
%a8 to a12 are low temp coeff

 for i= 1:length(temp_range)
 if temp_range(i)<=temp(2)
  S(i)= R.*((a8*log(temp_range(i)))+(a9*temp_range(i))+(a10*((temp_range(i).^2)/2))+(a11*((temp_range(i).^3)/3))+(a12*((temp_range(i).^4)/4))+a14);
  
  else 
      S(i)= R.*((a1*log(temp_range(i)))+(a2*temp_range(i))+(a3*((temp_range(i).^2)/2))+(a4*((temp_range(i).^3)/3))+(a5*((temp_range(i).^4)/4))+a7);
      end
      
 end

Function for calculating Enthalpy

function[H]= enthalpy(a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,temp,temp_range,R)
%a1,a2,a3,a4,a5 are high temp. coeff.
%a8,a9,a10,a11,a12 are  low temp. coeff.
for i= 1:length(temp_range)
if temp_range(i)<=temp(2)
     H(i)= R*((a8*temp_range(i))+(a9*((temp_range(i).^2)/2))+(a10*((temp_range(i).^3)/3))+(a11*((temp_range(i).^4)/4))+(a12*((temp_range(i).^5)/5))+a13);
     else 
         H(i)= R*((a1*temp_range(i))+(a2*((temp_range(i).^2)/2))+(a3*((temp_range(i).^3)/3))+(a4*((temp_range(i).^4)/4))+(a5*((temp_range(i).^5)/5))+a6);
   end
 end

OUTPUT:

Command window showing some calculated molecular weights:

Output of some values of specific heat(Cp):

Output of some values of Entropy(S):

Output of some values of Enthalpy(H):

Plots of O2:

Plots of CO2:

Plots of N2: Temperature vs Enthalpy

Temperature vs Entropy

Temperature vs Specific heat

**Due to system not properly working plots labels and title of N2 plots was not generated.

Screenshot of folders generated for each species:

ERRORS ENCOUNTERED:

There was a typo error while calling the function for entropy calculation.I was using parenthesis during defining for loop which was causing error.It was further resolved by removing the unnecessary parenthesis .

References:

www.mathworks\help\matlab.com.

www.matlab\documentation.com.

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.