Challenge

Project 1 - Parsing NASA thermodynamic data

AIM: To write a Mat Lab code to calculate thermodynamic properties of various elemental species and file parsing the NASA thermodynamics data(THERMO.DAT).

OBJECTIVE:

  • To write the functions to calculate the enthalpy, entropy and specific heat for all the species in the file.
  • Calculating molecular weight of each species and display in command window.
  • Plotting Cp, enthalpy and entropy for a local temperature range saving the plots and images all should be generated automatically screenshot of window of all folders saved.
  • Plots for 02, n2 and Co2 species.

File Parsing:

Parsing file means reading the data stream, sorting & building in memory data. Parsing, syntax analysis or syntactic analysis is the process of analysing the string of symbol, in natural language, computer or data structures, conforming to the rules of a formal grammar. Some parsing may generate a parse forest or list of parse for input.

NASA thermodynamics file:

NASA thermodynamics file which is THERMO.dat file which are temperature range and 53 species are given dada and each spe. Contaminates 14 coefficient and temperature range we have to parse these 14 coeff. To find specific heat, entropy and enthalpy using the formula Where R is the universal gas constant, T is the temperature, CP is specific heat (KJ), S is entropy (KJ/mol-k), H is enthalpy ( KJ/mol), a(i) is the coefficient where i = 1 to 14.

Based on the below formulae we have to calculate the specific heat, enthaphy & entropy.

Function for calculating Specific heat:

function [Cp]=Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,local_mid_temperature,temp,r)

%First 7 coefficients are high temperature coefficients and next 7 are low
%temperature coefficients

if(temp<=local_mid_temperature)
Cp=R*(a8+(a9*temp)+(a10*(temp).^2)+(a11*(temp).^3)+(a12*(temp).^4));
else
Cp=R*(a1+(a2*temp)+(a3*(temp).^2)+(a4*(temp).^3)+(a5*(temp).^4));
end
end

Function for calculating Enthalphy:

function [H]=enthalpy_H(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,local_mid_temperature,r)
%First 7 coefficients are high temperature coefficients and next 7 are low
%temperature coefficients
if(temp<=local_mid_temperature)
H=R*temp.*(a8+((a9*temp)/2)+((a10*(temp).^2)/3)+((a11*(temp).^3)/4)+((a12*(temp).^4)/5)+((a13./temp)));
else
H=R*temp.*(a1+((a2*temp)/2)+((a3*(temp).^2)/3)+((a4*(temp).^3)/4)+((a5*(temp).^4)/5)+((a6./temp)));
end
end

Function for calculating Entropy:

function [S]=entropy_S(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,temp,local_mid_temperature,R)
%First 7 coefficients are high temperature coefficients and next 7 are low
%temperature coefficients
if(temp<=local_mid_temperature)
S=(a8*log(temp)+(a9*temp)+(a10*(temp.^2)/2)+(a11*(temp.^3)/3)+(a12*(temp.^4)/4)+(a14))*R;
else
S=(a1*log(temp)+(a2*temp)+(a3*(temp.^2)/2)+(a4*(temp.^3)/3)+(a5*(temp.^4)/4)+(a7))*R;
end
end
Function for calculating Molecular weight Of Species:


%function to calculate Molecular Weights of Species
function MW =mol_wt(species_Name);
compund_name = ('species_Name')

%creating array for elements
Element_Name = ['C', 'H', 'O', 'A','N']
Atomic_Mass = [12 1 16 40 14]

MW =0
for i = 1:length(compund_name)
for j = 1:length (Element_Name)
%comparing string values
if strcmp(compund_name(i),Element_Name(j))
MW = MW+Atomic_Mass(j)
X = j;
end
end
K = str2num(compund_name(i));

if(K>1)

%molecular mass calculation
MW =MW+Atomic_Mass(X)*(K-1);
end
end
fprintf('Molecular Weight Of The Species %s is %f',compund_name,MW)
end


%Main Programme

clear all

close all

clc

% File Parsing - NASA thermodynamic data
% opening file
f1 = fopen('Thermo.dat','r')
% skipping first line
a=fgetl(f1);
% global temperatures
temps = fgetl(f1);
temps = str2num(temps);
global_low_temp = temps(1);
global_mid_temp = temps(2);
global_high_temp = temps(3);
% creating array for temperature
% T = linspace(global_low_temp,global_high_temp,500);
% universal gas constant
R = 8.314;
% skipping next 3 lines
for i=1:3
fgetl(f1);
end
%% Running for loop for 53 elements to get the values of species( Temeprature coefficients, molecular weight, specific heat, entropy,enthalphy)
for i=1:53
% creating array for species
tline = fgetl(f1);
A = strsplit(tline,' ');
% species
species = A{1};
%local temperatures
local_low_temperautre = str2num(A{end-3});
local_high_temperature = str2num(A{end-2});
local_mid_temperature = str2num(A{end-1});
T = linspace(local_low_temperautre,local_high_temperature,500);
% finding coefficients
% line 1
tline1 = fgetl(f1);
B = findstr(tline1,'E');
% higher coefficients
a1 = str2num(tline1(1:B(1)+3));
a2 = str2num(tline1(B(1)+4:B(2)+3));
a3 = str2num(tline1(B(2)+4:B(3)+3));
a4 = str2num(tline1(B(3)+4:B(4)+3));
a5 = str2num(tline1(B(4)+4:B(5)+3));
% line 2
tline2 = fgetl(f1);
C = findstr(tline2,'E');
a6 = str2num(tline2(1:C(1)+3));
a7 = str2num(tline2(C(1)+4:C(2)+3));
% lower coefficients
a8 = str2num(tline2(C(2)+4:C(3)+3));
a9 = str2num(tline2(C(3)+4:C(4)+3));
a10 = str2num(tline2(C(4)+4:C(5)+3));
% line 3
tline3 = fgetl(f1);
D = findstr(tline3,'E');
a11 = str2num(tline3(1:D(1)+3));
a12 = str2num(tline3(D(1)+4:D(2)+3));
a13 = str2num(tline3(D(2)+4:D(3)+3));
a14 = str2num(tline3(D(3)+4:D(4)+3));
% specific heat
cp = Specific_heat(a1,a2,a3,a4,a5,a8,a9,a10,a11,a12,tmid,T,R);
% enthalpy
H = enthalpy_H(a1,a2,a3,a4,a5,a6,a8,a9,a10,a11,a12,a13,T,tmid,R);
% entropy
S = entropy_S(a1,a2,a3,a4,a5,a7,a8,a9,a10,a11,a12,a14,tmid,T,R);
% creating newfolder for species
mkdir(species)
% changing directory
cd(species)
% plotting
figure(1)
plot(T,cp)
xlabel('Temperature')
ylabel('Specific heat')
title('Specific heat vs Temperature of ',sprintf('%s',species))
saveas(figure(1),'cp vs T.jpg')
figure(2)
plot(T,H)
xlabel('Temperature')
ylabel('Enthalpy')
title('Enthalpy vs Temperature of ',sprintf('%s',species))
saveas(figure(2),'H vs T.jpg')
figure(3)
plot(T,S)
xlabel('Temperature')
ylabel('Entropy')
title('Entropy vs Temperature of ',sprintf('%s',species))
saveas(figure(3),'S vs T.jpg')
% coming back to program directory
cd ..
% calculating molecular weight of species
M = mol_wt(species);
end
References:
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.