Challenge

Project 1 - Parsing NASA thermodynamic data

NASA FILE PARSING

AIM:

1. To write a code that extracts the 14 co-efficents form the given thermo data file and calculate the enthalpy,entropy and specifric heat for all species in the data file.

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

3. plot the cp, entalpy,entropy for the local ,medium,and high temperature range along with specific heat of each species.

INTRODUCTION:

File parsing is basically reading or collecting data from a file and using it for our reference and convience.

the data is present in THERMO file contains details about some elements.The data is present in thermo file.

FORMULA:

CP/R = a1 +a2T +a3T^2 +a4T^3 +a5T^4

H/RT = a1+(a2T)/2+(a3T)/3 +(a4T)/4+(a5T)/5+a6T

S/R = a1 ln T + a2t+a3T^2/2+a4T^3/3+a5T^4/4+a7

NOTE: the sasme formula can be used with different co-efficients named as (b) instead of (a).

this change is used to denote that (b) reperesents another set of co-efficents.

EXPLANATION:

f1 = fopen('THERMO.dat','r');
h1 =fgetl(f1);
h2 =fgetl(f1);
for i = 1:3
fgetl(f1);
end

here we begin with the fopen function where we open the given THERMO data file. the fgetl command is used the information present in the first line of the data file.


300.000 1000.000 5000.000

This data is aquired from the file and hi ,h2 is used to store the data in each file.

now the for loop is used to skip the next three lines present in the file, as it contains the tittle only.

for i =1:53
w=fgetl(f1);
s=strsplit(w,' ');
sp=s{1};
d = strfind(w,'.');
temprange= w((d(1)-3):(d(1)+23));
temp=strsplit(temprange,' ') ;
locallowtemp=str2num(temp{1});
temp1=locallowtemp;
localhightemp=str2num(temp{2});
temp2=localhightemp;
localmidtemp=str2num(temp{3});
temp3=localmidtemp;

Next for loop is intiated to read the data till the final line , the variable W is used to store the data that we get usuing the command fgetl.

The strsplit command is used to split the line with respect to the delimiter mentioned. the splited line is then saved as a cell array.

A cell array is a special type of array because we can store different data types in the same array.the variable s is used to store the data aquired from strsplit command

strfind command is used to find the seperaate the temperature values.as we can see the first temperature values is 3 spaces before the dot and the next value is after 23 spaces, so we use the temperange command to specify the range .

now the temperature is accumlated together, so we use the strsplit again to differentiate the temperatures. the special {} aare used to collect the data from every single cell array and is stored in three different variables temp1 , temp2 , temp3 which corresponds to local low,mid,hig temperatures.

coeff_line1 = fgetl(f1);
coeff_line2 = fgetl(f1);
coeff_line3 = fgetl(f1);
A = strfind(coeff_line1,'E');
B = strfind(coeff_line2,'E');
C = strfind(coeff_line3,'E');
a1= coeff_line1(1:A(1)+3);
a1=str2double(a1);
a2= coeff_line1(A(1)+4:A(2)+3);
a2= str2double(a2);
a3= coeff_line1(A(2)+4:A(3)+3);
a3= str2double(a3);
a4= coeff_line1(A(3)+4:A(4)+3);
a4= str2double(a4);
a5= coeff_line1(A(4)+4:A(5)+3);
a5= str2num(a5);
a6= coeff_line2(1:B(1)+3);
a6= str2double(a6);
a7= coeff_line2(B(1)+4:B(2)+3);
a7= str2double(a7);
b1= coeff_line2(B(2)+4:B(3)+3);
b1= str2double(b1);
b2= coeff_line2(B(3)+4:B(4)+3);
b2= str2double(b2);
b3= coeff_line2(B(4)+4:B(5)+3);
b3= str2double(b3);
b4= coeff_line3(1:C(1)+3);
b4= str2double(b4);
b5= coeff_line3(C(1)+4:C(2)+3);
b5= str2double(b5);
b6= coeff_line3(C(2)+4:C(3)+3);
b6= str2double(b6);
b7= coeff_line3(C(3)+4:C(4)+3);
b7= str2double(b7);

The fgetl command is used again to get the coeffients from each line , now in order to differentiate and for our convenience we use theh strfind command to split the co effients with respect to E.now the a1-a6 coeffients are for lower temperature and b1-b6 are for higher temperatures. with the help of str2double command we can convert the coeffients into integers.

now we need to calculate the

1. entropy

2. enthalpy

3. specific heat

4. molecular weight of each gases.

so now we use

molecule_name = char(sp);
mkdir(['D:\matlab\plots\',sp]);
cd(['D:\matlab\plots\',sp]);

The mkdir command to create a specifc folder in the d directory named as plots so that the plots is saved in a seperated folder.

the cd command is used to ensure that the plots of each and every molecule is stored in a seperate folder . i.e the entropy,enthalpy,specific heat plots are created for each and every molecule so this has to be stored in a seperate folder , so the cd command creates a seperate folder dor each and every molecule.

%solve for cp, h , s %
T = linspace(temp1,temp2,1000);
R = 8.314;
for k=1:length(T)
if T(k)<=temp3
cp(k) = R*(b1+(b2*T(k))+(b3*T(k)^2)+(b4*T(k)^3)+(b5*T(k)^4));
H(k)= R*T(k)*(b1+(b2*T(k)/2)+((b3*T(k)^2)/3)+((b4*T(k)^3)/4)+((b5*T(k)^4)/5)+(b6/T(k)));
S(k)= R *(b1*log(T(k))+(b2*T(k))+((b3*T(k)^2)/2)+((b4*T(k)^3)/3)+((b5*T(k)^4)/4)+b7);
else
cp(k)= R*(a1+(a2*T(k))+(a3*T(k)^2)+(a4*T(k)^3)+(a5*T(k)^4));
H(k)= R*T(k)*(a1+(a2*T(k)/2)+((a3*T(k)^2)/3)+((a4*T(k)^3)/4)+((a5*T(k)^4)/5)+(a6/T(k)));
S(k)= R *(a1*log(T(k))+(a2*T(k))+((a3*T(k)^2)/2)+((a4*T(k)^3)/3)+((a5*T(k)^4)/4)+a7);
end
end

here we use the linspace command to create an array where we take 1000 values from both low and high temperature. now we declare the value of universal gas constant as 8.314. next we initate a for loop where k is taking the value of each temperature, furthere a if condtion is also declared because we have both high and low temperature co-efficents so to get a correct value for entropy , enthalpy , specific heat we keep temp3 i.e the medium temperature as the deciding factor which then uses either (a) or (b) to compute the entropy, enthalpy, specific heat .

%plots%
figure(1)
plot(T,H,'-*');
title('molecule_name');
xlabel('temperature');
ylabel('enthalpy');
figure(2)
plot(T,cp,'o');
title('molecule_name');
xlabel('temperature');
ylabel('specific heat');
figure(3)
plot(T,S,'-')
title('molecule_name');
xlabel('temperature')
ylabel('entropy')

saveas(1,'enthalpy.jpg');
saveas(2,'specificheat.jpg');
saveas(3,'entropy.jpg');

here we plot the outputss and save them as sperate piture files in the respecctive directory.

%molecular mass calculation%

mm=0;
atoms = ['H','C','N','O','A','S'];
aw=[1 12 14 16 40 32];
for x = 1:length(sp)
for j = 1:length(atoms)
if strcmp(sp(x),atoms(j))
mm=mm+aw(j);
p = j;
end
end
factor=str2double(sp(x));
if factor>1
mm=mm+(aw(p)*(factor-1));
end
end
fprintf('The molecular mass of %s is %d\n',sp,mm);
%weight= molecular_mass('sp');
end

The molecular mass calculation is also executed inside the same for loop which was intiated in the beginning for the 53 species

the molecular mass is declared to be zero in the very beginning of the loop ,next atoms are declared into a seperate array. here we declare the specific atoms that we need to inspect.now we use strcmp to copare the two strings x and j and then we calculate the molecular mass if the condition is satisfied . again we conert the string stored in sp into numbers using strdouble command.

PROGRAM CODE:

clear all
close all
clc
f1 = fopen('THERMO.dat','r');
h1 =fgetl(f1);
h2 =fgetl(f1);
for i = 1:3
    fgetl(f1);
end
%since there are 53 species we use 53 loops%
for i =1:53
w=fgetl(f1);
s=strsplit(w,' ');
sp=s{1};
d = strfind(w,'.');
temprange= w((d(1)-3):(d(1)+23));
temp=strsplit(temprange,' ') ;
locallowtemp=str2num(temp{1});
temp1=locallowtemp;
localhightemp=str2num(temp{2});
temp2=localhightemp;
localmidtemp=str2num(temp{3});
temp3=localmidtemp;
%coefficents from line7%
coeff_line1 = fgetl(f1);
coeff_line2 = fgetl(f1);
coeff_line3 = fgetl(f1);
A = strfind(coeff_line1,'E');
B = strfind(coeff_line2,'E');
C = strfind(coeff_line3,'E');
a1= coeff_line1(1:A(1)+3);
a1=str2double(a1);
a2= coeff_line1(A(1)+4:A(2)+3);
a2= str2double(a2);
a3= coeff_line1(A(2)+4:A(3)+3);
a3= str2double(a3);
a4= coeff_line1(A(3)+4:A(4)+3);
a4= str2double(a4);
a5= coeff_line1(A(4)+4:A(5)+3);
a5= str2num(a5);
a6= coeff_line2(1:B(1)+3);
a6= str2double(a6);
a7= coeff_line2(B(1)+4:B(2)+3);
a7= str2double(a7);
b1= coeff_line2(B(2)+4:B(3)+3);
b1= str2double(b1);
b2= coeff_line2(B(3)+4:B(4)+3);
b2= str2double(b2);
b3= coeff_line2(B(4)+4:B(5)+3);
b3= str2double(b3);
b4= coeff_line3(1:C(1)+3);
b4= str2double(b4);
b5= coeff_line3(C(1)+4:C(2)+3);
b5= str2double(b5);
b6= coeff_line3(C(2)+4:C(3)+3);
b6= str2double(b6);
b7= coeff_line3(C(3)+4:C(4)+3);
b7= str2double(b7);
molecule_name = char(sp);
mkdir(['D:\matlab\plots\',sp]);
cd(['D:\matlab\plots\',sp]);
%solve for cp, h , s %
T = linspace(temp1,temp2,1000);
R = 8.314;
for k=1:length(T)
    if T(k)<=temp3 
        cp(k) = R*(b1+(b2*T(k))+(b3*T(k)^2)+(b4*T(k)^3)+(b5*T(k)^4));
        H(k)= R*T(k)*(b1+(b2*T(k)/2)+((b3*T(k)^2)/3)+((b4*T(k)^3)/4)+((b5*T(k)^4)/5)+(b6/T(k)));
        S(k)= R *(b1*log(T(k))+(b2*T(k))+((b3*T(k)^2)/2)+((b4*T(k)^3)/3)+((b5*T(k)^4)/4)+b7);
    else
       cp(k)= R*(a1+(a2*T(k))+(a3*T(k)^2)+(a4*T(k)^3)+(a5*T(k)^4));
        H(k)= R*T(k)*(a1+(a2*T(k)/2)+((a3*T(k)^2)/3)+((a4*T(k)^3)/4)+((a5*T(k)^4)/5)+(a6/T(k)));
        S(k)= R *(a1*log(T(k))+(a2*T(k))+((a3*T(k)^2)/2)+((a4*T(k)^3)/3)+((a5*T(k)^4)/4)+a7);
    end
end
%plots%
figure(1)
plot(T,H,'-*');
title('molecule_name');
xlabel('temperature');
ylabel('enthalpy');
figure(2)
plot(T,cp,'o');
title('molecule_name');
xlabel('temperature');
ylabel('specific heat');
figure(3)
plot(T,S,'-')
title('molecule_name');
xlabel('temperature')
ylabel('entropy')

saveas(1,'enthalpy.jpg');
saveas(2,'specificheat.jpg');
saveas(3,'entropy.jpg');

%molecular mass calculation%

mm=0;
atoms = ['H','C','N','O','A','S'];
aw=[1 12 14 16 40 32];
for x = 1:length(sp)
    for j = 1:length(atoms)
        if strcmp(sp(x),atoms(j))
            mm=mm+aw(j);
            p = j;
        end
    end
    factor=str2double(sp(x));
    if factor>1
        mm=mm+(aw(p)*(factor-1));
    end
end
fprintf('The molecular mass of %s is %d\n',sp,mm);
%weight= molecular_mass('sp');
end


OUTPUT:

1. SEPERATE FOLDERS CREATED

2. PLOTS FOR O2

ENTHALPY

ENTROPY

SPECIFIC HEAT

3. PLOTS FOR N2

ENTHALPY

ENTROPY

SPECIFIC HEAT

4. PLOTS FOR CO2

ENTHALPY

ENTROPY

SPECIFIC HEAT

ERRORS FACED :

IMPROPER USAGE OF BRACKETS

MISSED [ AT THE END BEFORE CLOING THE MKDIR COMMAND.

THERMO.DAT WAS SAVED IN DIFFERENT FOLDER

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.