Project 1 - Parsing NASA thermodynamic data
clear
close
clc
%open the file
f=fopen("THERMO.dat");
%using for loop for first 5 lines
for i=1:5
%fgetl read line from each file
fgetl(f)
end
for j=1:53
b=fgetl(f);
%spliting the string based on delimiter
a=strsplit(b,' ');
% 1st cell of a represents the species_name
species_name=a{1};
%extraction of local temperature
%converting string to numbers
%seperating numbers using posdot
posdot = strfind(b,'.');
low_temp = b(posdot(1)-3:posdot(1)+3);
low_temp = str2double(low_temp);
high_temp=b(posdot(2)-4:posdot(2)+3);
high_temp=str2double(high_temp);
mid_temp=b(posdot(3)-4:posdot(3)+3);
mid_temp=str2double(mid_temp);
%extraction of coefficient
co_eff=fgetl(f);
%finding of position
pos=strfind(co_eff,'E');
a1=str2double(co_eff(1:(pos(1)+3)));
a2=str2double(co_eff((pos(1)+4):(pos(2)+3)));
a3=str2double(co_eff((pos(2)+4):(pos(3)+3)));
a4=str2double(co_eff((pos(3)+4):(pos(4)+3)));
a5=str2double(co_eff((pos(4)+4):(pos(5)+3)));
co_eff1=fgetl(f);
pos2=strfind(co_eff1,'E');
a6=str2double(co_eff1(1:(pos2(1)+3)));
a7=str2double(co_eff1((pos2(1)+4):(pos2(2)+3)));
a8=str2double(co_eff1((pos2(2)+4):(pos2(3)+3)));
a9=str2double(co_eff1((pos2(3)+4):(pos2(4)+3)));
a10=str2double(co_eff1((pos2(4)+4):(pos2(5)+3)));
co_eff2=fgetl(f);
pos1=strfind(co_eff2,'E');
a11=str2double(co_eff2(1:(pos1(1)+3)));
a12=str2double(co_eff2((pos1(1)+4):(pos1(2)+3)));
a13=str2double(co_eff2((pos1(2)+4):(pos1(3)+3)));
a14=str2double(co_eff2((pos1(3)+4):(pos1(4)+3)));
%calculation of cp,h,s
%temperature array creation
T=linspace(low_temp,high_temp,1000);
%splitting of temperature
for k=1:length(T)
t=T(k);
if(t<=mid_temp)
ltc(k)=t;
elseif (t>=mid_temp)
htc1(k)=t;
end
end
d=length(ltc);
htc=htc1((d+1):length(T));
%calculation of cp,h,s(htc)
R=8.314;
cp1=R*(a1+(a2*htc)+a3*(htc.^2)+a4*(htc.^3)+(a5*htc.^4));
h1=R*((a1+a2*(htc./2))+a3*(htc.^2./3)+a4*(htc.^3./4)+a5*(htc.^4/5)+a6./htc);
s1=R*(a1*(log(htc))+a2*(htc)+a3*(htc.^2./2)+a4*(htc.^3./3)+a5*(htc.^4./4)*a7);
%calculation of cp,h,s (ltc)
cp2=R*(a8+(a9*ltc)+a10*(ltc.^2)+a11*(ltc.^3)+(a12*ltc.^4));
h2=R*((a8+a9*(ltc./2))+a10*(ltc.^2./3)+a11*(ltc.^3./4)+a12*(ltc.^4/5)+a13./ltc);
s2=R*(a8*(log(ltc))+a9*(ltc)+a10*(ltc.^2./2)+a11*(ltc.^3./3)+a12*(ltc.^4./4)*a14);
Cp=[cp1 cp2];
H=[h1 h2];
S=[s1 s2];
%changing the folder directory
cd('C:\Users\cjang\OneDrive\Documents\MATLAB\secies folder\')
%creating the folder and naming it
mkdir(species_name)
cd(species_name)
%plot temperature vs enthalphy
subplot(2,2,2)
plot(T,H,'linewidth',2,'Color','r');
xlabel('temperature');
ylabel('enthalpy');
title('temperatre vs Enthalphy')
saveas(figure(1),'T vs H.jpg')
%plot temperature vs entrophy
subplot(2,2,3)
plot(T,S,'linewidth',2,'Color','b');
xlabel('temperature');
ylabel('entropy');
title('temperatre vs Enthalphy')
saveas(figure(1),'T vs S.jpg')
%plot temperature vs specific heat
subplot(2,2,4)
plot(T,Cp,'linewidth',2,'Color','g');
xlabel('temperature');
ylabel('specific heat');
title('temperatre vs specific heat')
saveas(figure(1),'T vs cp.jpg')
cd('C:\Users\cjang\OneDrive\Documents\MATLAB\')
%molecular weight calculation
[W]=molecular_weight(species_name)
fprintf('molecular weight of %s = %f',species_name,W)
disp('')
end
molecular Weight function script
function [W]=molecular_weight(species_name)
atomic_names=['H','C','O','N','Ar'];
atomic_weight=[1.00784, 12.0107, 15.999, 14.0067, 39.948];
W=0;
for x=1:length(species_name)
for Y=1:length(atomic_names)
if species_name(x)==atomic_names(Y)
W=W+atomic_weight(Y)
position=Y;
end
end
n=str2num(species_name(x));
if n>=1
W=W+atomic_weight(position)*(n-1)
end
end
fprintf('molecular weight of %s = %f',species_name,W)
disp('')
end
Steps of code explaination
- First we open the given thermos file using ‘fopen’ command so that we can completely open the file and its data.
- Next we need to read each line from file so we use ‘fgetl’ command in for loop so we can get each line opened-up. and as first 5 lines are the heading and we don’t need that lines in our main example hence we saperates it in for loop by giving i=1:5 and then close it by end
- Now its turn for our main program which is for our 53 species hence we start fresh ‘for loop’ with indexing I =1:53 as we need 53 species and then open that lines using fgetl command.
- After that we us strsplit command which is use to split string or it is character vector at specified delimiter.
- Now its time to represent the species name which is given in thermos file but we have to take it out through code or using code so our first cell is species name and we can separate it by using the statement species_name=a{1}
- Now we have to extract the local temperatures and its ranges here like low temp , high temp and mid temp and then convert string to numbers . these three parameters are also given in given file. But our challenge here is to pick it out from file.
- So we use here one trick which is ‘posdot’ (position before the dot ) this command magic is that it picks the values used before the decimal points , so by using strfind and posdot command with each other we get the values of low temp , mid temp , and high temp.
- Then we extracts each and every coefficients and for that here we use 3 commands at one times which are fgetl, strfind, and str2double. Our new command str3double is meant for convert string to double precise values. An like wise we use it for different sets to find different coefficients because all are at different positions.
- Now we have to find the specific heat , temperature, enthalpy and entropy se first lets find the temperature by using command linspace and defining the limits in bracket low temp and high temp and as we need the curve more smooth hence we need more detail points so we gives the number of values as 1000.
- Our next is splitting the temperature coefficients like high temperature coefficients and low temperature coefficients because in question its given that first use 1st 7 temperatures which are low temp coefficients and another 8 to 14 is low temp coefficients. So for that we use if else for loop because the values above mid temp is high temp values and values less than mid temp are low temp values hence we use if else condition here in for loop.
- Now using the given formula we can find the cp, h, s but we find these 2 different times because for low temp coefficients and high temp coefficients so we will having 2 cps, 2 h and 2 s and by using square bracket we can make it combine values.
- After that we have to plot this three graphs in one figure for each species. And every plot figure should be in saved in there own species name folder so we also have to create the folders here and name it according to species names respectively. So first lets create the folder here in our directory so by using the command cd('C:\Users\cjang\OneDrive\Documents\MATLAB\secies folder\') lets define the directory first where we have to create the species folder. To create the folder we have to give the command mkdir(species_name). By these command we can create the folders in directory now its time to save the plots figure in those folders.
- So lets plot the figures , hence according to the question we have to create one figure containing the 3 plots in that so first lets give the command subplot command and define position of plots in that subpot like for first plot which is T vs H we define its position by statement subplot(2,2,3) like wise for another 2 plots.
- Now ploting the temp vs enthalpy by using our traditional ploting command , then defining there line width , line color, labels etc and to save it we have to use saveas command and with that wee have to define the names of figure like we here defines with command “ saveas(figure(1),’T vs H.jpg’) “
Use same plot set for another two plots which are T vs S and T vs Cp and yes as we changes the directory to species folder we have to change back the directory to basic folder so we have to close the cd command here as our plots are saved so for that we have to use command cd('C:\Users\cjang\OneDrive\Documents\MATLAB\').
- Now lets find our last and final values which are molecular weight so for that first lets create the function in new script because we have to calculate it for each and every species the function code is given below. We defines the atoms used in species first which are H, C, O, N, Ar by their atomic weights in same position in below line respectively. We use nested for loop for finding molecular weight for specific species. Now we have to use if condition here because we have to find the molecular weight using the atomic weight so we can calculate it by formula No. of atoms * Atomic weight. And we need to display the answer with full statement in this format “molecular weight of …=..” so we give here fprintf command after that closing the function by using end command.
function [W]=molecular_weight(species_name)
atomic_names=['H','C','O','N','Ar'];
atomic_weight=[1.00784, 12.0107, 15.999, 14.0067, 39.948];
W=0;
for x=1:length(species_name)
for Y=1:length(atomic_names)
if species_name(x)==atomic_names(Y)
W=W+atomic_weight(Y)
position=Y;
end
end
n=str2num(species_name(x));
if n>=1
W=W+atomic_weight(position)*(n-1)
end
end
fprintf('molecular weight of %s = %f',species_name,W)
disp('')
end
- now as we need to find it for every species so we have to use it in that for loop in which we enter our all above data because with every plot for every species it also find the molecular weight for species and then lets end that big for lop which we started for Indexing i=1:53
Drive link to open the species folder :
https://drive.google.com/drive/folders/1lIcKETvTRAI6XbO8CCwvJV3mCd_Igy9A?usp=sharing
screenshot of species folder directory is uploaded