Challenge

Project 1 - Parsing NASA thermodynamic data

AIM :

1. 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. You will be reading this file NASA thermodynamic data(Tip: use fopen and fgetl). The formulae are shown below.

Here R is the universal gas constant, T is the temeprature

The FIRST 7 coefficients are HIGH-temperature coefficients and the SECOND 7 coefficients are LOW-temperature coefficients.

Also use the LOCAL TEMPERATURE RANGES which is unique for each individual species, not the GLOBAL TEMPERATURE RANGE.

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

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

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

THEORY :

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.

DATA SOURCE :

Link to thermodynamic data file,

NASA thermodynamic data

FORMULAE USED :

MAIN code :

%writing code for file parsing
clear all
close all
clc
% here we are opening the file
% reading the header
F1=fopen('THERMO.dat','r')
first_line=fgetl(F1);
%defining the values for R which is gas constant
R=8.314;
% know taking tempreture from the file
% readingsecond line
% knowing and dividing the tempreture
% converting the string into number
temp=fgetl(F1);
A=strsplit(temp,' ');
% assigning the temperatures
globel_low_tempreture=str2num(A{2});
globel_middle_tempreture=str2num(A{3});
globel_high_tempreture=str2num(A{4});
% define the tempreture range
tempreture=linspace(globel_low_tempreture,globel_high_tempreture,globel_middle_tempreture);
%we have to skip the 3lines of comments
third_line=fgetl(F1);
fourth_line=fgetl(F1);
fifth_line=fgetl(F1);
%now running the the for loop for 53 different species
for i=1:53
%taking 1st species
sixth_line=fgetl(F1);
M=strsplit(sixth_line,' ');
species_name=M{1};
%know finding the coefficents from the 1st line of the taken species
sixth_line_one=fgetl(F1);
a=findstr(sixth_line_one,'E');
%there fore we have 5 coefficents in our 1st line so we have to find the 5 coefficents
a1=sixth_line_one(1:a(1)+3);
a1=str2num(a1);
a2=sixth_line_one(a(1)+4:a(2)+3);
a2=str2num(a2);
a3=sixth_line_one(a(2)+4:a(3)+3);
a3=str2num(a3)
a4=sixth_line_one(a(3)+4:a(4)+3);
a4=str2num(a4);
a5=sixth_line_one(a(4)+4:a(5)+3);
a5=str2num(a5);
% we have to move to next line for further coefficents here we have 5 coefficents
sixth_line_sec=fgetl(F1)
a=findstr(sixth_line_sec,'E')
a6=sixth_line_sec(1:a(1)+3);
a6=str2num(a6);
a7=sixth_line_sec(a(1)+4:a(2)+3);
a7=str2num(a7);
a8=sixth_line_sec(a(2)+4:a(3)+3);
a8=str2num(a8)
a9=sixth_line_sec(a(3)+4:a(4)+3);
a9=str2num(a9);
a10=sixth_line_sec(a(4)+4:a(5)+3);
a10=str2num(a10);
% we have to move to next line for further coefficents here we have 5 coefficents
sixth_line_third=fgetl(F1)
a=findstr(sixth_line_sec,'E')
a11=sixth_line_sec(1:a(1)+3);
a11=str2num(a11);
a12=sixth_line_sec(a(1)+4:a(2)+3);
a12=str2num(a12);
a13=sixth_line_sec(a(2)+4:a(3)+3);
a13=str2num(a13)
a14=sixth_line_sec(a(3)+4:a(4)+3);
a14=str2num(a14);
%know we have to find the entropy enthalpy and specific heat
% calling the function for entropy
A=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,tempreture,globel_middle_tempreture,R);
% calling the function for enthalphy
B=enthalphy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,tempreture,globel_middle_tempreture,R)
%calling the function for specific heat
C=speciofic_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,tempreture,globel_middle_tempreture,R)
% calling the function for moleculer weigth
molecular_w(i) = molecular_weight(species_name)
%writting the moleculer weight into a file
molecular_weight_of_the_species=fopen('molecular_weight.txt','w');
fprintf(molecular_weight_of_the_species,'molecular weight of the %s is %f \n',species_name,molecular_w);
fclose( molecular_weight_of_the_species)
% making a file directory
derectory_curr=pwd;
mkdir(species_name);
cd(species_name);
%plotting
%tempreture vs entropy
figure(1)
plot(tempreture,A,'color','r',LineWidth=2)
xlabel('tempreturess')
ylabel('entropys')
grid on
title(sprintf('variation in entropy wrt tempreture in %s',species_name))
saveas(figure(1),'entrogy.jpg')
%tempreture vs enthalpy
figure(2)
plot(tempreture,B,'color','b',LineWidth=2)
xlabel('tempreture')
ylabel('enthalpy')
grid on
title(sprintf('variation in enthalpy wrt tempreture in %s',species_name))
saveas(figure(2),'enthalpy.jpg')
%tempreture vs specific heat
figure(3)
plot(tempreture,C,'color','k',LineWidth=2)
xlabel('tempreture')
ylabel('specific_heat')
grid on
title(sprintf('variation in specific heat wrt tempreture in %s',species_name))
saveas(figure(3),'specificheat.jpg')
cd(derectory_curr)
% end the loop
end
Function Molecular Weight code :
function molecular_w = molecular_weight(species_name)
%creating array of elements
key_possion=['O','H','C','N','A']
%storing the atomic mass into an array
compounds_value= [15.999,1.008,12.011,14.007,39.948];
molecular_w= 0;
%creating the loop
for i = 1:length(species_name)
for j = 1:length(key_possion)
if strcmp(species_name(i),key_possion(j))
%if above statement is truw then calculate
molecular_w= molecular_w+compounds_value(j);
poss = j;
end
end
%converting the string into the number
K= str2num(species_name(i));
%comparing if the value for K is greter then excicuted the further
%statements
if (K>1)
molecular_w = molecular_w+compounds_value(j)*(K-1);
end
fprintf('molecular weight of the species %s',species_name)
fprintf('%f',molecular_w)
disp(' ')
end
Function Entropy code :
% writting the function for entropy
function [A]=entropy(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,tempreture,globel_middel_temp,R)
%as mensioned in the question 1st 7 coeeficents are high temp and secound 7
%coefficents are low tempreture
% creating loop to divede this cofficents
if (tempreture>=globel_middel_temp)
A=R*(a1.*log(tempretur)+a2.*(tempreture)+(a3.*(tempreture).^2/2)+(a4.*(tempreture.^3)/3)+(a5.*(tempreture).^4/4)+a7);
else
A=R.*(a8.*log(tempreture)+a9.*(tempreture)+(a10.*(tempreture).^2/2)+(a11.*(tempreture.^3)/3)+(a12.*(tempreture).^4/4)+a14);
end
end
Function Enthalpy code :
% writting the function for enthalphy
function [B]=enthalphy(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,tempreture,globel_middel_temp,R)
%as mensioned in the question 1st 7 coeeficents are high temp and secound 7
%coefficents are low tempreture
% creating loop to divede this cofficents
if (tempreture>=globel_middel_temp)
B=R.*tempreture.*(a1+(a2.*tempreture/2)+(a3.*(tempreture).^2/3)+(a4.*(tempreture).^3/4)+(a5.*(tempreture).^4/5)+(a6.*(tempreture).^5/6));
else
B=R.*tempreture.*(a8+(a9.*tempreture/2)+(a10.*(tempreture).^2/3)+(a11.*(tempreture).^3/4)+(a12.*(tempreture).^4/5)+(a13.*(tempreture).^5/6));
end
end
Function Specific Heat code :
% writting the function for enthalphy
function [C]=speciofic_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,tempreture,globel_middel_temp,R)
%as mensioned in the question 1st 7 coeeficents are high temp and secound 7
%coefficents are low tempreture
% creating loop to divede this cofficents
if (tempreture>=globel_middel_temp)
C=R.*(a1+(a2.*(tempreture))+(a3.*(tempreture).^2)+(a4.*(tempreture).^3)+(a5.*(tempreture).^4));
else
C=R.*(a8+(a9.*(tempreture))+(a10.*(tempreture).^2)+(a11.*(tempreture).^3)+(a12.*(tempreture).^4));
end
end
Result :
The plots are saved with there respecive species folders
The molecular wieght is also saved and dispalyed in the command window
Some of the plots with species
1. O
2. OH
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.