Project 1 - Parsing NASA thermodynamic data
objectives:
- write a funcrion that extracts the 14 coefficients and calculates the enthalpy,entropy and specific heat 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
- save the plot as images with appropriate names and dump them in separate folders for each species with suitable name .all these should be generated automatically
- screenshot of the window where all the folders are saved
- plots for o2,N2,Co2 species
THEORY
- FILE PARSING
- parsing a file means reading in a data stream of some sort and building and building an in memory model of the semantic content of that data .The aim of facilitating performing some kind of transformation on the data
- parsing is a process of converting formatted text into a data structure .A data structure type can be any suitable representation of the information engroved in the source text.
MAIN CODE
clear all
close all
clc
% reading the file
f=fopen('THERMO.dat','r');
% reading the header data
for i=1:5
fgetl(f);
end
% extracting data from species block
for i=1:53
line_1=fgetl(f);
a=strsplit(line_1,' ');
% extract species name
species_name=(a{1});
%extracting local temperatures
% local_low_temp=a{6};
% local_high_temp=a{7};
% local_mid_temp=a{8};
% converting string values of local temperature into array
local_low_temp=str2num(a{end-3});
local_high_temp=str2num(a{end-2});
local_mid_temp=str2num(a{end-1});
% extraction of coefficients
coeff_line_1=fgetl(f);
% reading position of E from coeff_ line_1
posE1=strfind(coeff_line_1,'E');
a1=str2num(coeff_line_1(1:posE1(1)+3));
a2=str2num(coeff_line_1(posE1(1)+4:posE1(2)+3));
a3=str2num(coeff_line_1(posE1(2)+4:posE1(3)+3));
a4=str2num(coeff_line_1(posE1(3)+4:posE1(4)+3));
a5=str2num(coeff_line_1(posE1(4)+4:posE1(5)+3));
% reading position of E from coeff_line_2
coeff_line_2=fgetl(f);
posE2=strfind(coeff_line_2,'E');
a6=str2num(coeff_line_2(1:posE2(1)+3));
a7=str2num(coeff_line_2(posE2(1)+4:posE2(2)+3));
a8=str2num(coeff_line_2(posE2(2)+4:posE2(3)+3));
a9=str2num(coeff_line_2(posE2(3)+4:posE2(4)+3));
a10=str2num(coeff_line_2(posE2(4)+4:posE2(5)+3));
% reading position of E from coeff_line_3
coeff_line_3=fgetl(f);
posE3=strfind(coeff_line_3,'E');
a11=str2num(coeff_line_3(1:posE3(1)+3));
a12=str2num(coeff_line_3(posE3(1)+4:posE3(2)+3));
a13=str2num(coeff_line_3(posE3(2)+4:posE3(3)+3));
a14=str2num(coeff_line_3(posE3(3)+4:posE3(4)+3));
% taking inputs of temperature
T=linspace(local_low_temp,local_high_temp,500);
R=8.314;
�lculating cp,h,s for each value of temperature
for i=1:length(T)
if T(i)<=local_mid_temp
cp(i)=(a8+a9*T(i)+a10*T(i)^2+a11*T(i)^3+a12*T(i)^4)*R;
h(i)=(a8+(a9.*T(i))./2+(a10.*T(i).^2)./3+(a11.*T(i).^3)./4+(a12.*T(i).^4)./5+(a13./T(i))).*R.*T(i);
s(i)=((a8.*log(T(i))+a9.*T(i)+(a10.*T(i).^2)./2+(a11.*T(i).^3)./3+(a12.*T(i).^4)./4+a14)).*R;
else
cp(i)=(a1+a2*T(i)+a3*T(i)^2+a4*T(i)^3+a5*T(i)^4)*R;
h(i)=(a1+(a2.*T(i))./2+(a3.*T(i).^2)./3+(a4.*T(i).^3)./4+(a5.*T(i).^4)./5+(a6./T(i))).*R.*T(i);
s(i)=((a1.*log(T(i))+a2.*T(i)+(a3.*T(i).^2)./2+(a4.*T(i).^3)./3+(a5.*T(i).^4)./4+a7)).*R;
end
end
% %plotting and saving of the results
�('C:\Users\Satish Kumar Bastia\OneDrive\Desktop\nasa file parsing')
mkdir('species_name')
current_dir = pwd;
cd('species_name')
figure(1)
plot(T,cp)
m1 = 'specific heat of %s';
m2 = species_name;
m3 = sprintf(m1,m2);
saveas(figure(1),sprintf('%s.png',m3))
figure(2)
plot(T,h)
m4 = 'enthalpy of %s';
m5 = species_name;
m6 = sprintf(m4,m5);
saveas(figure(2),sprintf('%s.png',m6))
figure(3)
plot(T,s)
m7 = 'entropy heat of %s';
m8 = species_name;
m9 = sprintf(m7,m8);
saveas(figure(3),sprintf('%s.png',m9))
cd ..
molecular_weight=mw(species_name);
B= sprintf('molecular wt of %s is %f',species_name,molecular_weight)
disp(B)
end
% function code
function [molecular_weight]=mw(species_name)
atomic_name=['H','C','O','N','A'];
atomic_weight=[1,12,16,14,39];
molecular_weight=0;
for j=1:length(species_name)
for k=1:length(atomic_name)
if strcmp(species_name(j),atomic_name(k))
molecular_weight=molecular_weight+atomic_weight(k);
position=k;
end
end
n=str2double(species_name(j));
if n>1
molecular_weight=molecular_weight+atomic_weight(position)*(n-1);
end
%sprintf('molecular wt of %s:%f',species_name,molecular_weight)
end
CODE EXPLANATION
clear all: clear all variables from current workspace
close all: closes all plots
clc: clear command windows
fopen: it opens the files ,filename for binary read access and returns an integer file identifier equal to greater than 3
fgetl: used to read the data line by line
strsplit: used to split the data in awell manner(line 17)
line 20: extracting the species name from the previous data file by simple array indexing
str2num: converts string value to numeric value
str2find: used to find the position of E from the co efficients of the species(line 35 to 61)
line 63 : creating array for temperature
linespace: used to generate linearly spaced vectors
line 64: R is gas constant(value:8.314)
line 65 to 80:finding specific heat ,enthalpy ,entropy for each value of temperature by using below formulas:-
mkdir: used to make a new directory or folder named as species_name
pwd: used to identify the current folder
cd: used to change directory
line 90 to 96:creating plot for specific heat with respect to temperature
line 98 to 103: creating plot for enthalpy with respect to temperature
saveas: used to save figure to specific file format
line 105 to 110: creating plot for enthalpy with respect to temperature
cd ..: used to get return back from the current folder
line 113: for calculating molecular weight for each species by using function code
sprintf: used to format data into string vector
disp() : used to display B as molecular weight
outputs:
plots for o2
enthalpy entropy
specific heat
plots for N2
specific heat entropy
enthalpy
plots for co2
enthalpy entropy
specific heat