Challenge

Project 1 - Parsing NASA thermodynamic data

REFERNCES

MATHWORK,WIKIPEDIA

ERROR 1 = Wronglt imply str2num command while specifying sp_name
CORRECTION 1 = Remove the wrong command by giving proper input

EXPLANATION OF CODE

clear all
close all
clc         - commands for clearing the window

f = fopen('THERMO.dat','r'); - fopen is the command to open a file in matlab of same path. Thermo is the name of the file storring NASA data required for the operation. r is the command for reading the data inside the file.(syntax for opening a file in matlab)

for i = 1:5 - for loop is used here for eliminating the first 5 lines.
    fgetl(f); - fgetl helps us to get a line from a file. each time we call this function it aumatically generate only the last executed line. f is the file storing the data here.
end - used for ending the loop

% To Extraxt coefficients of species and doing calculation

 for i= 1:53 - for loop for executing the whole 53 species 

a = fgetl(f); a is the executed fgetl line

b = strsplit(a,' '); - strsplit splits the string between the delimeter ' ' of line a and stores it as b.

z = length(b); - z show the total length of b

sp = b{1}; - sp shows the species occured in first species
sp_name = sp; - specie name would the sp and its a reccuring command through the for loop. keeps changing each time.

elements = ['H','C','O','N','A']; - these are the elements in the file data
atomic_weight = [1.008 12.011 15.999 14.007 39.946]; - these are its atomic weight respectivily

local_low = str2num(b{z-3}); - local low is used to store the least value. str2num command is used to convert the string into a number .b(z-3) - used to show that the local low is occupied before 3 places than the z value.
local_high = str2num(b{z-2}); - local high is used to store the greater value. str2num command is used to convert the string into a number .b(z-2) - used to show that the local high is occupied before 2 places than the z value.
local_mid = str2num(b{z-1}); - local mid is used to store the middle value. str2num command is used to convert the string into a number .b(z-1) - used to show that the local middle is occupied just before  places than the z value.
{} is used for cell array indexing

%extraction of co-efficients

coeff_line1 = fgetl(f); - coefficient line 1 is going to execute by using the fgetl command


pos_e = strfind(coeff_line1,'E'); - strfind is used to get a cell array of vector of double, wgich inturn helps us to eliminate E. 

a1 = str2num(coeff_line1(1:pos_e(1)+3)); a1 stores the value which may converted to numbr from string using str2num. In line1, a1 value exist upto pos_e1 just before E and also includes the 3 positions after the E.

a2 = str2num(coeff_line1((pos_e(1)+4):(pos_e(2)+3))); - a2 stores the value which may converted to nmbr from string using str2num. in line 2, a2 value started from the pos_e1, after 4 position from E  of a1 to pos_e2 and 4 position after E 
a3 = str2num(coeff_line1((pos_e(2)+4):(pos_e(3)+3))); - a3 stores the value which may converted to nmbr from string using str2num. in line 3, a3 value started from the pos_e2, after 4 position from E  of a2 to pos_e3 and 4 position after E 
a4 = str2num(coeff_line1((pos_e(3)+4):(pos_e(4)+3)));-a4stores the value which may converted to nmbr from string using str2num. in line 4, a4 value started from the pos_e3, after 4 position from E  of a3 to pos_e4 and 4 position after E  
a5 = str2num(coeff_line1((pos_e(4)+4):(pos_e(5)+3))); - a5 stores the value which may converted to nmbr from string using str2num. in line 5, a5 value started from the pos_e4, after 4 position from E  of a5 to pos_e4 and 4 position after E  


coeff_line2 = fgetl(f); - coefficient line 2 is going to execute by using the fgetl command

pos_e1 = strfind(coeff_line2,'E'); - strfind is used to get a cell array of vector of double, wgich inturn helps us to eliminate E. 

a6 = str2num(coeff_line2(1:pos_e1(1)+3)); - a6 stores the value which may converted to numbr from string using str2num. In line2, a6 value exist upto pos_e1 just before E and also includes the 3 positions after the E.

a7 = str2num(coeff_line2((pos_e1(1)+4):(pos_e1(2)+3))); - a7 stores the value which may converted to nmbr from string using str2num. in line 2, a7 value started from the pos_e1, after 4 position from E  of a8 to pos_e2 and 4 position after E 
a8 = str2num(coeff_line2((pos_e1(2)+4):(pos_e1(3)+3))); - a8 stores the value which may converted to nmbr from string using str2num. in line 2, a8 value started from the pos_e1, after 4 position from E  of 8 to pos_e2 and 4 position after E 

a9 = str2num(coeff_line2((pos_e1(3)+4):(pos_e1(4)+3))); - a9 stores the value which may converted to nmbr from string using str2num. in line 2, a9 value started from the pos_e1, after 4 position from E  of 9 to pos_e2 and 4 position after E 
a10 = str2num(coeff_line2((pos_e1(4)+4):(pos_e1(5)+3))); - a10 stores the value which may converted to nmbr from string using str2num. in line 2, a10 value started from the pos_e1, after 4 position from E  of 10 to pos_e2 and 4 position after E 

coeff_line3 = fgetl(f); - coefficient line 3 is going to execute by using the fgetl command


pos_e2 = strfind(coeff_line3,'E'); - strfind is used to get a cell array of vector of double, wgich inturn helps us to eliminate E. 


a11 = str2num(coeff_line3(1:pos_e2(1)+3)); - a11 stores the value which may converted to numbr from string using str2num. In line3, a11 value exist upto pos_e1 just before E and also includes the 3 positions after the E.

a12 = str2num(coeff_line3((pos_e2(1)+4):(pos_e2(2)+3))); - a12 stores the value which may converted to nmbr from string using str2num. in line 3, a12 value started from the pos_e2, after 4 position from E  of a12 to pos_e2 and 4 position after E 
a13 = str2num(coeff_line3((pos_e2(2)+4):(pos_e2(3)+3)));- a13 stores the value which may converted to nmbr from string using str2num. in line 3, a13 value started from the pos_e2, after 4 position from E  of 13 to pos_e2 and 4 position after E 
a14 = str2num(coeff_line3((pos_e2(3)+4):(pos_e2(4)+3))); - a14 stores the value which may converted to nmbr from string using str2num. in line 3, a14 value started from the pos_e2, after 4 position from E  of 14 to pos_e2 and 4 position after E 


% CALLING FUNCTIONS ENTHALPY, SPECIFIC HEAT AND ENTROPY


T = linspace(200,3500,100); - T is the temperature includes the  local low to local high which involves 100 limits

for i = 1:length(T) i stores the total length for T values

    if (T(i)<=1000) - means if T value is less than or equal to 1000 then it is considered as local low

        ltr(i) = T(i); those local low values are known as ltr (lower temperature values)

    elseif (T(i)>1000) means if T value is greater than  1000 then it is considered as local high

        htr(i) = T(i); those local low values are known as ltr (lower temperature values)

    end - for ending the else if loop
end - for ending the if loop 

LTR = ltr; - LTR for showing ltr in upper case
d = length(ltr); - d stores length of ltr

HTR = htr(d+1:length(T)); - UPPER HTR stores the value after the d value 

% Calculation of cp,h,s

cp1 = (a8 + (a9*LTR) + (a10*LTR.^2) + (a11*LTR.^3) + (a12*LTR.^4))*8.314; - substituting the specific heat eqn by putting ltr value for finding lower temp specific heat value

H1 = (a8 + (a9*LTR./2) + (a10*(LTR.^2)./3) + (a11*(LTR.^3)./4) + (a12*(LTR.^4)./5)+ (a13./LTR))*8.314.*LTR; - substituting the enthalpy eqn by putting ltr value for finding lower temp enthalpy value


S1 =  ((a8*log(LTR)) + (a9.*LTR) + (a10*LTR.^2./2) + (a11*LTR.^3./LTR) + (a12*LTR.^4./4) + a13)*8.314; -  substituting the entropy eqn by putting ltr value for finding lower temp entropy value


cp2 = (a1 + (a2*HTR) + (a3*HTR.^2) + (a4*HTR.^3) + (a5*HTR.^4))*8.314; - substituting the specific heat eqn by putting htr value for finding higher temp specific heat value


H2 = (a1 + (a2*HTR./2) + (a3*(HTR.^2)./3) + (a4*(HTR.^3)./4) + (a5*(HTR.^4)./5)+ (a6./HTR))*8.314.*HTR;  - substituting the enthalpy eqn by putting htr value for finding higher temp enthalpy value  

S2 = ((a1*log(HTR)) + (a2.*HTR) + (a3*HTR.^2./2) + (a4*HTR.^3./HTR) + (a5*HTR.^4./4) + a7)*8.314; -substituting the entropy eqn by putting htr value for finding higher temp entropy value
 

cp = [cp1,cp2]; - for combing the cp1 & cp2 using square bracket
                
H = [H1,H2]; - for combing the H1 & H2 using square bracket

S = [S1,S2]; - for combing the S1 & S2 using square bracket

% CALCULATION OF MOLECULAR WEIGHT

mw = molecular_weight_file_parsing(sp_name); - function call for finding the molecular weight


%Creating directory
mkdir(['C:\Users\Basel Thomas\Desktop\Skill lync\','SPECIES1']); - stored path for operation. mkdir means making the directory for saving the file.



% Plotting

figure(1) - for plotting the figure 1 in 1 figure window
plot(T,H,'linewidth',2,'color','b') for plotting tempperature vs enthalpy with linewidth of 2 mm of color blue
title('Temperature vs Enthalpy',sp_name) - for specifying the title 'Temperature vs Enthalpy' with sp_name
xlabel('temperature') - labelling the temperature in x label
ylabel('Enthalpy') - labelling the enthalpy in y label
grid on - for the grid shade in fig

figure(2) - for plotting the figure in 2 in 1 figure window
plot(T,S,'linewidth',2, 'color','r') for plotting tempperature vs entropy with linewidth of 2 mm of color red
title('temp vs entropy',sp_name) -  for specifying the title 'Temperature vs Entropy' with sp_name
xlabel('temp') - labelling the temperature in x label
ylabel('Entropy') -labelling the entropy in y label  
grid on - for the grid shade in fig

    
figure(3) - for plotting the figure in 3 in 1 figure window
plot(T,cp,'linewidth',2,'color','y') -for plotting tempperature vs specific heat with linewidth of 2 mm of color yellow
title('temp vs specific heat',sp_name) - for specifying the title 'Temperature vs specific heat' with sp_name
xlabel('temp') - labelling the temperature in x label
ylabel('specific heat') - labelling the entropy in y label  
grid on - for the grid shade in fig


cd(['C:\Users\Basel Thomas\Desktop\Skill lync\','SPECIES1']); - cd command changes the directory of the current file 

saveas(1,'enthalpy.jpg') for saving the first fig as enthalpy as jpg gile
saveas(2,'entropy.jpg') for saving the second fig as entropy as jpg file
saveas(3,'specific_heat.jpg')for saving the third fig as specific heat as jpg file

molecular_W = molecular_weight_file_parsing(sp_name); - molecular_W stores the function values reapeting for each execution.
sprintf('Molecular Weight of %s is %f',sp_name,molecular_W) - sprintf prints the molecular weight for each species


 end - for ending the loop.

% FUNCTION CODE


function [mw] = molecular_weight_file_parsing(sp_name) - is the fuction call for finding the molecular weight

species = sp_name; - species stores the sp names
elements = ['H','C','O','N','A']; - for the execution  the elements 
atomic_weight = [1.008 12.011 15.999 14.007 39.948]; - for the execution  the atomic weight 
mw = 0; - initial value for molecular mass

for p = 1:length(sp_name) - p stores the values for the sp names using for loop
    for q = 1:length(atomic_weight) - q stores the values for the atomic weight using for loop
        if strcmp(sp_name(p),elements(q)) - comparing the strings sp name & elements using if satement
            mw = mw+atomic_weight(q); - actual molecular weight is the sum of initial and atomic weight for each execution 
            position = q; - for defining the position  of q
        end - for ending the loop
    end - for ending the loop


    n = str2num(sp_name(p)); n stores the str2num(sp_name) value for the p value
    if n>1 - if loop for executing the each value, if the  n value is greater than 1
        mw = mw + atomic_weight(position)*(n-1); - actual molecular weight thats includes the atomic weight for each species position * to n-1
    end - ending the if statement
end - ending the initial for loop








% program for file parsing & molecular weight(using NASA Thermo Data)

clear all
close all
clc

f = fopen('THERMO.dat','r');

for i = 1:5
    fgetl(f);
end

% To Extraxt coefficients of species and doing calculation

 for i= 1:53

a = fgetl(f);

b = strsplit(a,' ');

z = length(b);

sp = b{1};
sp_name = sp;

elements = ['H','C','O','N','A'];
atomic_weight = [1.008 12.011 15.999 14.007 39.946];

local_low = str2num(b{z-3});
local_high = str2num(b{z-2});
local_mid = str2num(b{z-1});

%extraction of co-efficients

coeff_line1 = fgetl(f);


pos_e = strfind(coeff_line1,'E');

a1 = str2num(coeff_line1(1:pos_e(1)+3));

a2 = str2num(coeff_line1((pos_e(1)+4):(pos_e(2)+3)));
a3 = str2num(coeff_line1((pos_e(2)+4):(pos_e(3)+3)));
a4 = str2num(coeff_line1((pos_e(3)+4):(pos_e(4)+3)));
a5 = str2num(coeff_line1((pos_e(4)+4):(pos_e(5)+3)));

coeff_line2 = fgetl(f);

pos_e1 = strfind(coeff_line2,'E');

a6 = str2num(coeff_line2(1:pos_e1(1)+3));

a7 = str2num(coeff_line2((pos_e1(1)+4):(pos_e1(2)+3)));
a8 = str2num(coeff_line2((pos_e1(2)+4):(pos_e1(3)+3)));
a9 = str2num(coeff_line2((pos_e1(3)+4):(pos_e1(4)+3)));
a10 = str2num(coeff_line2((pos_e1(4)+4):(pos_e1(5)+3)));

coeff_line3 = fgetl(f);

pos_e2 = strfind(coeff_line3,'E');

a11 = str2num(coeff_line3(1:pos_e2(1)+3));

a12 = str2num(coeff_line3((pos_e2(1)+4):(pos_e2(2)+3)));
a13 = str2num(coeff_line3((pos_e2(2)+4):(pos_e2(3)+3)));
a14 = str2num(coeff_line3((pos_e2(3)+4):(pos_e2(4)+3)));

% CALLING FUNCTIONS ENTHALPY, SPECIFIC HEAT AND ENTROPY


T = linspace(200,3500,100);

for i = 1:length(T)

    if (T(i)<=1000)

        ltr(i) = T(i);

    elseif (T(i)>1000)

        htr(i) = T(i);

    end
end

LTR = ltr;
d = length(ltr);

HTR = htr(d+1:length(T));

% Calculation of cp,h,s

cp1 = (a8 + (a9*LTR) + (a10*LTR.^2) + (a11*LTR.^3) + (a12*LTR.^4))*8.314;

H1 = (a8 + (a9*LTR./2) + (a10*(LTR.^2)./3) + (a11*(LTR.^3)./4) + (a12*(LTR.^4)./5)+ (a13./LTR))*8.314.*LTR;

S1 =  ((a8*log(LTR)) + (a9.*LTR) + (a10*LTR.^2./2) + (a11*LTR.^3./LTR) + (a12*LTR.^4./4) + a13)*8.314;

cp2 = (a1 + (a2*HTR) + (a3*HTR.^2) + (a4*HTR.^3) + (a5*HTR.^4))*8.314;

H2 = (a1 + (a2*HTR./2) + (a3*(HTR.^2)./3) + (a4*(HTR.^3)./4) + (a5*(HTR.^4)./5)+ (a6./HTR))*8.314.*HTR;     

S2 = ((a1*log(HTR)) + (a2.*HTR) + (a3*HTR.^2./2) + (a4*HTR.^3./HTR) + (a5*HTR.^4./4) + a7)*8.314;

cp = [cp1,cp2];

H = [H1,H2];

S = [S1,S2];

% CALCULATION OF MOLECULAR WEIGHT

mw = molecular_weight_file_parsing(sp_name);


%Creating directory
mkdir(['C:\Users\Basel Thomas\Desktop\Skill lync\','SPECIES1']);



% Plotting

figure(1)
plot(T,H,'linewidth',2,'color','b')
title('Temperature vs Enthalpy',sp_name)
xlabel('temperature')
ylabel('Enthalpy')
grid on

figure(2)
plot(T,S,'linewidth',2, 'color','r')
title('temp vs entropy',sp_name)
xlabel('temp')
ylabel('Entropy')
grid on
    
figure(3)
plot(T,cp,'linewidth',2,'color','y')
title('temp vs specific heat',sp_name)
xlabel('temp')
ylabel('specific heat')
grid on

cd(['C:\Users\Basel Thomas\Desktop\Skill lync\','SPECIES1']);

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

molecular_W = molecular_weight_file_parsing(sp_name);
sprintf('Molecular Weight of %s is %f',sp_name,molecular_W)


 end


% Function code for finding the Molecular Weight

function [mw] = molecular_weight_file_parsing(sp_name)

species = sp_name;
elements = ['H','C','O','N','A'];
atomic_weight = [1.008 12.011 15.999 14.007 39.948];
mw = 0;

for p = 1:length(sp_name)
    for q = 1:length(atomic_weight)
        if strcmp(sp_name(p),elements(q))
            mw = mw+atomic_weight(q);
            position = q;
        end
    end


    n = str2num(sp_name(p));
    if n>1
        mw = mw + atomic_weight(position)*(n-1);
    end
end

% COMMAND WINDOW RESULTS SHOWING MOLECULAR WEIGHT OF EACH SPECIES

ans =

    'Molecular Weight of O is 15.999000'


ans =

    'Molecular Weight of O2 is 31.998000'
 

ans =

    'Molecular Weight of H is 1.008000'


ans =

    'Molecular Weight of H2 is 2.016000'

ans =

    'Molecular Weight of OH is 17.007000'


ans =

    'Molecular Weight of H2O is 18.015000'


ans =

    'Molecular Weight of HO2 is 33.006000'


ans =

    'Molecular Weight of H2O2 is 34.014000'


ans =

    'Molecular Weight of C is 12.011000'


ans =

    'Molecular Weight of CH is 13.019000'
 

ans =

    'Molecular Weight of CH2 is 14.027000'

ans =

    'Molecular Weight of CH2(S) is 14.027000'


ans =

    'Molecular Weight of CH3 is 15.035000'


ans =

    'Molecular Weight of CH4 is 16.043000'


ans =

    'Molecular Weight of CO is 28.010000'


ans =

    'Molecular Weight of CO2 is 44.009000'


ans =

    'Molecular Weight of HCO is 29.018000'
 

ans =

    'Molecular Weight of CH2O is 30.026000'


ans =

    'Molecular Weight of CH2OH is 31.034000'


ans =

    'Molecular Weight of CH3O is 31.034000'


ans =

    'Molecular Weight of CH3OH is 32.042000'


ans =

    'Molecular Weight of C2H is 25.030000'


ans =

    'Molecular Weight of C2H2 is 26.038000'


ans =

    'Molecular Weight of C2H3 is 27.046000'
 

ans =

    'Molecular Weight of C2H4 is 28.054000'
 

ans =

    'Molecular Weight of C2H5 is 29.062000'


ans =

    'Molecular Weight of C2H6 is 30.070000'
 

ans =

    'Molecular Weight of CH2CO is 42.037000'
 

ans =

    'Molecular Weight of HCCO is 41.029000'
 

ans =

    'Molecular Weight of HCCOH is 42.037000'


ans =

    'Molecular Weight of H2CN is 28.034000'


ans =

    'Molecular Weight of HCN is 27.026000'


ans =

    'Molecular Weight of HNO is 31.014000'


ans =

    'Molecular Weight of N is 14.007000'


ans =

    'Molecular Weight of NNH is 29.022000'


ans =

    'Molecular Weight of N2O is 44.013000'


ans =

    'Molecular Weight of NH is 15.015000'


ans =

    'Molecular Weight of NH2 is 16.023000'


ans =

    'Molecular Weight of NH3 is 17.031000'

ans =

    'Molecular Weight of NO is 30.006000'
 

ans =

    'Molecular Weight of NO2 is 46.005000'


ans =

    'Molecular Weight of HCNO is 43.025000'


ans =

    'Molecular Weight of HOCN is 43.025000'


ans =

    'Molecular Weight of HNCO is 43.025000'
 

ans =

    'Molecular Weight of NCO is 42.017000'


ans =

    'Molecular Weight of CN is 26.018000'

ans =

    'Molecular Weight of HCNN is 41.033000'

 

ans =

    'Molecular Weight of N2 is 28.014000'


ans =

    'Molecular Weight of AR is 39.948000'


ans =

    'Molecular Weight of C3H8 is 44.097000'
 

ans =

    'Molecular Weight of C3H7 is 43.089000'



ans =

    'Molecular Weight of CH3CHO is 44.053000'



ans =

    'Molecular Weight of CH2CHO is 43.045000'

>> 





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.