Project 1 - Parsing NASA thermodynamic data
AIM:
To write a program for file parsing & molecular weight.
OBJECTIVE:
To Plot graphs for Cp, Enthalpy and Entropy for the local temperature range (low temperature : high temperature) w.r.t 53 spcies present in the Thermo.dat file and To calculate molecular weight.
Explanation:
Step-wise explanation for the program file parsing-:
1.Intially for Reading the file THERMO.dat 'fopen' command is used.The syntax for 'fopen' fileID = fopen(filename,permission)
2.'fgetl' command is used to get the line & the syntax for 'fgetl' is tline = fgetl(fileID)
3.In the next line string is split by using the command 'strsplit' & the syntax of 'strsplit' C = strsplit(str,delimiter)
4.Next we are extracting the global Temperature Range for low,medium & high temperatures where we are using the command 'str2num'. 7 the syntax used for 'str2num' is X = str2num(chr)
5.The 'fgetl' command is again used for the getting the line of all three global temperature range.
6.The next step is finding coefficeints where intially we are using the command tline.
7. In finding coefficeints ,a1 to a7 is for High Temperature Range & a8 to a14 is for Low Temperature Range. In this we are using 'findstr' command which is used to find the string & the syntax for 'findstr' is k = findstr(str1, str2)
8. In the next step we are assigning the value of TRange, using the syntax varible_name=linspace(x,y,n) i.e varible _name is given as TRange & (x,y,n)are the different temperatures.
9.The next step is assigning value for Gas Constant (R)=8.314
10. The next line we are using for loop, for loop is the loop where the start and end points are connected & it is a repititive loop and the syntax for 'for loop' is variable_name=intial value:increment value:final value
11.In the next step we are finding the value of Spcecific Heat(Cp),Enthalpy(H) & Entropy(S) with the formula given below
12.In these formulas a1 to a7 is used for High Temperature Range & a8 to a14 is used for Low Temperature Range.
13.This formula is used under the 'ifelse loop' this loop is used when we have binary condition & separate commands should get executed whe the condtion is true or false & the syntax used for this loop is
syntax-:
if
statement1
statement2
.
.
.
else
statement1
statement2
.
.
.
end
14. Final step is the outputs i.e plotting the graphs for Spcecific Heat(Cp),Enthalpy(H) & Entropy(S) w.r.t all the species.
15. We are saving these plots in the current folder using saveas command and the syntax is saveas(gcf,'barchart.png'), In this gcf is mentioned in integers and 'barchart.png' refers to Spcecific Heat,Enthalpy & Entropy.
Step-wise explanation for the program molecular weight-:
1. Intially we are providing the one species name , here we are providing the specices 'O'(oxygen).
2.The next step is calculating molecular weight, In this step intially we are giving the main 6 elements in a matrix and we are also giving the atomic_wt value for the 6 main species & giving mm value as zero.
3.In the next step we are using for loop to assign i & j iterations , for loop is the loop where the start and end points are connected & it is a repititive loop and the syntax for 'for loop' is variable_name=intial value:increment value:final value.
4. The IV step is using if loop, if loop is a loop where it evaluates an expression, and executes a group of statements when the expression is true. and th syntax is
ifexpressionstatements
5.In if loop we comparing the string i & j which is used to compare the strings and the syntax is given below for 'strcmp'
6.Again we are using an if loop to find
if >1
mm=mm+atomic_wt(pos)*(n-1);
7.Finally we are using sprintf for displaying the output
Progarm1:
%File Parsing
clear all
close all
clc
%reading a file
f1=fopen('thermo.dat','r')
A=fgetl(f1)
% a1=tline(1:12)
A=fgetl(f1)
A=strsplit(A,' ');
%global temperature range
global_low_temp=str2num(A{2});
global_med_temp=str2num(A{3});
global_high_temp=str2num(A{4});
A=fgetl(f1)
A=fgetl(f1)
A=fgetl(f1)
%finding coefficients
tline1=fgetl(f1);
B=strsplit(tline1,' ');
species_name=B{1};
tline2=fgetl(f1);
tline3=fgetl(f1);
tline4=fgetl(f1);
SL1=findstr(tline2,'E');
coefficeints1=tline2(1:SL1(1)+3);
coefficeints2=tline2(SL1(1)+4:SL1(2)+3);
coefficeints3=tline2(SL1(2)+4:SL1(3)+3);
coefficeints4=tline2(SL1(3)+4:SL1(4)+3);
coefficeints5=tline2(SL1(4)+4:SL1(5)+3);
SL2=findstr(tline3,'E');
coefficeints6=tline3(1:SL2(1)+3);
coefficeints7=tline3(SL2(1)+4:SL2(2)+3);
coefficeints8=tline3(SL2(2)+4:SL2(3)+3);
coefficeints9=tline3(SL2(3)+4:SL2(4)+3);
coefficeints10=tline3(SL2(4)+4:SL2(5)+3);
SL3=findstr(tline4,'E');
coefficeints11=tline4(1:SL3(1)+3);
coefficeints12=tline4(SL3(1)+4:SL3(2)+3);
coefficeints13=tline4(SL3(2)+4:SL3(3)+3);
coefficeints14=tline4(SL3(3)+4:SL3(4)+3);
a1=str2num(coefficeints1);
a2=str2num(coefficeints2);
a3=str2num(coefficeints3);
a4=str2num(coefficeints4);
a5=str2num(coefficeints5);
a6=str2num(coefficeints6);
a7=str2num(coefficeints7);
a8=str2num(coefficeints8);
a9=str2num(coefficeints9);
a10=str2num(coefficeints10);
a11=str2num(coefficeints11);
a12=str2num(coefficeints12);
a13=str2num(coefficeints13);
a14=str2num(coefficeints14);
TRange=linspace(300,5000,100);
numberofgases=53;
R=8.314
for j=1:length(TRange)
% calculating specific heat,enthalpy and entropy
if TRange(j)>1000
cp(j)=(a1+a2.*TRange(j)+a3.*TRange(j).^2+a4.*TRange(j).^3+a5.*TRange(j).^4)*R;
H(j)=(a1+a2*TRange(j)/2+a3.*TRange(j).^2/3+a4.*TRange(j).^3/4+a5.*TRange(j).^4/5+a6/TRange(j))*R*TRange(j);
S(j)=(a1.*log(TRange(j))+a2.*TRange(j)+a3.*TRange(j).^2/2+a4.*TRange(j).^3/3+a5.*TRange(j).^4/4+a7)*R;
else
cp(j)=(a8+a9.*TRange(j)+a10.*TRange(j).^2+a11*TRange(j).^3+a12.*TRange(j).^4)*R;
H(j)=(a8+a9.*TRange(j)/2+a10.*TRange(j).^2/3+a11.*TRange(j).^3/4+a12.*TRange(j).^4/5+a13/TRange(j))*R*TRange(j);
S(j)=(a8.*log(TRange(j))+a9.*TRange(j)+a10.*TRange(j).^2/2+a11.*TRange(j).^3/3+a12.*TRange(j).^4/4+a14)*R;
end
end
%plotting cp vs T
figure(1)
plot(TRange,cp,'linewidth',3,'color','r')
xlabel('Temperature')
ylabel('specific heat')
grid on
%plotting H vs T
figure(2)
plot(TRange,H,'linewidth',3,'color','r')
xlabel('Temperature')
ylabel('enthalpy')
grid on
%plotting S vs T
figure(3)
plot(TRange,S,'linewidth',3,'color','r')
xlabel('Temperature')
ylabel('entropy')
grid on
saveas(1,'spcificheat.png')
saveas(2,'enthalpy.png')
saveas(3,'entropy.png')
Progam2:
%Molecular Weight
clear all
close all
clc
species_name='O';
%calculating molecular weight
elements=['H','C','O','N','A','S'];
atomic_wt=[1.008,12.0096,15.99903,14.00643,39.792,32.059];
mm=0;
for i=1:length(species_name)
for j=1:length(elements)
if strcmpi(species_name(i),elements(j))
mm=mm+atomic_wt(j);
pos=j;
end
n=str2num(species_name(i));
end
if n>1
mm=mm+atomic_wt(pos)*(n-1);
end
end
sprintf('The molecular mass is %d',mm)